#define ProgName "xtrapout"
#include <stdio.h>
#include <X11/extensions/xtraplib.h>
#include <X11/extensions/xtraplibp.h>
#include <signal.h>
#include <X11/keysym.h>
#include <unistd.h>
#include <stdlib.h>
#include "XEKeybCtrl.h"
#ifndef vaxc
#define globalref extern
#endif
static void SetGlobalDone (void );
static void print_req_callback (XETC *tc , XETrapDatum *data ,
char *my_buf );
static void print_evt_callback (XETC *tc , XETrapDatum *data ,
char *my_buf );
FILE *ofp;
Bool GlobalDone = False;
XrmOptionDescRec optionTable [] =
{
{"-f", "*script", XrmoptionSepArg, (caddr_t) NULL},
{"-e", "*eventFlag", XrmoptionSkipArg, (caddr_t) NULL},
{"-v", "*verbose", XrmoptionSkipArg, (caddr_t) NULL},
};
static void SetGlobalDone(void)
{
GlobalDone = 1L;
fprintf(stderr,"Process Completed!\n");
return;
}
static void print_req_callback(XETC *tc, XETrapDatum *data, char *my_buf)
{
char *req_type;
req_type = (data->u.req.reqType == XETrapGetExtOpcode(tc) ? "XTrap" :
XERequestIDToString(data->u.req.reqType,tc));
fprintf(ofp,"Request: %-19s (%d): length '%ld' client '%d' window=%ld\n",
req_type, data->u.req.reqType, (long)data->hdr.count, data->hdr.client,
(long)data->u.req.id);
}
static void print_evt_callback(XETC *tc, XETrapDatum *data, char *my_buf)
{
static Time last_time = 0;
int delta;
delta = abs((int)last_time ? data->u.event.u.keyButtonPointer.time -
(int)last_time : (int)last_time);
last_time = data->u.event.u.keyButtonPointer.time;
fprintf(ofp,
"Event: %-15s (%d):det=%d scr=%d (%d,%d) root=%d Msk=%d TS=%d\n",
XEEventIDToString(data->u.event.u.u.type,tc), data->u.event.u.u.type,
data->u.event.u.u.detail, data->hdr.screen,
data->u.event.u.keyButtonPointer.rootX,
data->u.event.u.keyButtonPointer.rootY,
(int)data->u.event.u.keyButtonPointer.root,
(int)data->u.event.u.keyButtonPointer.state,
(int)delta);
fflush(ofp);
}
static Boolean eventFlag = False;
static Boolean verboseFlag = False;
static Widget appW;
static Display *dpy;
int
main(int argc, char *argv[])
{
XETrapGetAvailRep ret_avail;
XETrapGetCurRep ret_cur;
XETC *tc;
ReqFlags requests;
EventFlags events;
XtAppContext app;
char *tmp = NULL;
INT16 ch;
int *popterr;
char **poptarg;
#ifndef vms
popterr = &opterr;
poptarg = &optarg;
#else
popterr = XEgetopterr();
poptarg = XEgetoptarg();
#endif
eventFlag = False;
ofp = NULL;
*popterr = 0;
while ((ch = getopt(argc, argv, "d:evf:")) != EOF)
{
switch(ch)
{
case 'e':
eventFlag = True;
break;
case 'v':
verboseFlag = True;
break;
case 'f':
if ((ofp = fopen(*poptarg,"wb")) == NULL)
{
fprintf(stderr,"%s: could not open output file '%s'!\n",
ProgName, *poptarg);
}
break;
case 'd':
break;
default:
break;
}
}
ofp = (ofp ? ofp : stdout);
appW = XtAppInitialize(&app,"XTrap",optionTable,(Cardinal)2L,
(int *)&argc, (String *)argv, (String *)NULL,(ArgList)&tmp,
(Cardinal)NULL);
dpy = XtDisplay(appW);
#ifdef DEBUG
XSynchronize(dpy, True);
#endif
fprintf(stderr,"Display: %s \n", DisplayString(dpy));
if ((tc = XECreateTC(dpy,0L, NULL)) == False)
{
fprintf(stderr,"%s: could not initialize XTrap extension\n",ProgName);
exit (1L);
}
XETrapSetTimestamps(tc,True, False);
(void)XEGetAvailableRequest(tc,&ret_avail);
XEPrintAvail(stderr,&ret_avail);
XEPrintTkFlags(stderr,tc);
(void)memset(requests,0L,sizeof(requests));
(void)memset(events,0L,sizeof(events));
(void)memset(events,0xFFL,XETrapMaxEvent);
if (eventFlag == False)
{
(void)memset(requests,0xFFL,XETrapMaxRequest);
BitFalse(requests, XETrapGetExtOpcode(tc));
BitFalse(events, MotionNotify);
}
if (verboseFlag == True)
{
(void)memset(requests,0xFFL,XETrapMaxRequest);
(void)memset(events,0xFFL,XETrapMaxEvent);
}
XETrapSetRequests(tc, True, requests);
XETrapSetEvents(tc, True, events);
XETrapSetMaxPacket(tc, True, XETrapMinPktSize);
XEAddRequestCBs(tc, requests, print_req_callback, NULL);
XEAddEventCBs(tc, events, print_evt_callback, NULL);
(void)XEStartTrapRequest(tc);
(void)XEGetCurrentRequest(tc,&ret_cur);
XEPrintCurrent(stderr,&ret_cur);
_InitExceptionHandling((void_function)SetGlobalDone);
(void)XEEnableCtrlKeys((void_function)SetGlobalDone);
XETrapAppWhileLoop(app,tc,&GlobalDone);
XESimulateXEventRequest(tc, KeyRelease,
XKeysymToKeycode(dpy, XK_Control_L), 0L, 0L, 0L);
XEFreeTC(tc);
(void)XCloseDisplay(dpy);
(void)XEClearCtrlKeys();
_ClearExceptionHandling();
exit(0L);
}