summaryrefslogtreecommitdiff
path: root/xtrapin.c
blob: 30de9d939a1c0165ab66edeae9e22b0ad36303e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* $XFree86: xc/programs/xtrap/xtrapin.c,v 1.2tsi Exp $ */
/*
 * @DEC_COPYRIGHT@
 */
/*
 * HISTORY
 * Log: xtrapin.c,v $
 * Revision 1.1.4.2  1993/12/14  12:37:20  Kenneth_Miller
 * 	ANSI-standardize code and turn client build on
 * 	[1993/12/09  20:15:45  Kenneth_Miller]
 *
 * Revision 1.1.2.2  1992/04/27  13:51:39  Leela_Obilichetti
 * 	Initial load of xtrap clients - from silver BL6
 * 	[92/04/27  13:49:16  Leela_Obilichetti]
 * 
 * EndLog$
 */
/*****************************************************************************
Copyright 1987, 1988, 1989, 1990, 1991 by Digital Equipment Corp., Maynard, MA

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

*****************************************************************************/
/*
 *
 *  CONTRIBUTORS:
 *
 *      Dick Annicchiarico
 *      Robert Chesler
 *      Dan Coutu
 *      Gene Durso
 *      Marc Evans
 *      Alan Jamison
 *      Mark Henry
 *      Ken Miller
 *
 */
#define ProgName "xtrapin"
/*
**++
**  FACILITY:  xtrapin - Sample client to test input to XTrap extension
**
**  MODULE DESCRIPTION:
**
**      This is the main module for a sample/test client
**      for the XTrap X11 Server Extension.  It accepts  
**      a script file and a transport method as input  
**      in addition to the standard X arguments (-d, etc.).
**      If no script file is provided, stdin is the default
**      and can be piped from the companion "xtrapout"
**      client (normally used with the -e argument which  
**      sends all core input events to stdout).
**
**
**  AUTHORS:
**
**      Kenneth B. Miller
**
**  CREATION DATE:  December 15, 1990
**
**  DESIGN ISSUES:
**
**      See the companion "xtrapout" client.
**
**      Also, getopt() is used to parse the command
**      line arguments prior to calling XtAppInitialize().
**      This is because DECwindows appears to remove the user-
**      defined arguments from the argv[] vector without actually
**      acting upon them.
**
**
**--
*/
#include <stdio.h>
#include <X11/extensions/xtraplib.h>
#include <X11/extensions/xtraplibp.h>
#include <unistd.h>
#include <stdlib.h>

#ifndef vaxc
#define globalref extern
#endif
#ifdef Lynx     
extern char *optarg;     
extern int optind;     
extern int opterr;     
#endif     

static Boolean grabFlag = False;

static FILE *ifp;
static XrmOptionDescRec optionTable [] = 
{
    {"-f",     "*script",    XrmoptionSepArg,  (caddr_t) NULL},
    {"-g",     "*grabServer",XrmoptionSkipArg, (caddr_t) NULL},
};

typedef struct
{   /* longword-align fields for arg passing */
    Time  ts;
    int   type;
    int   detail;
    int   x;
    int   y;
    int  screen; /* this will always be 0 till vectored events! */
} file_rec;

/* Forward declarations */
static Bool found_input_rec (FILE *ifp , file_rec *rec );

static Widget appW;
static Display *dpy;


int
main(int argc, char *argv[])
{
    XETrapGetAvailRep ret_avail;
    XETrapGetCurRep   ret_cur;
    XETC    *tc;
    XtAppContext app;
    char *tmp = NULL;
    INT16 ch;
    file_rec rec;
    Time last_time = 0L;
    int *popterr;
    char **poptarg;
#ifndef vms
    popterr = &opterr;
    poptarg = &optarg;
#else
    popterr = XEgetopterr();
    poptarg = XEgetoptarg();
#endif

    ifp = NULL;
    *popterr = 0; /* don't complain about -d for display */
    grabFlag = False;
    while ((ch = getopt(argc, argv, "d:f:g")) != EOF)
    {
        switch(ch)
        {
            case 'f':
                if ((ifp = fopen(*poptarg,"rb")) == NULL)
                {   /* can't open it */
                    fprintf(stderr,"%s: could not open output file '%s'!\n",
                        ProgName, *poptarg);
                }
                break;
            case 'd':   /* -display, let's let the toolkit parse it */
                break;
            case 'g':
                grabFlag = True;
            default:
                break;
        }
    }
    ifp = (ifp ? ifp : stdin);

    appW = XtAppInitialize(&app,"XTrap",optionTable,(Cardinal)1L,
        (int *)&argc, (String *)argv, (String *)NULL,(ArgList)&tmp,
        0);

    dpy = XtDisplay(appW);
#ifdef DEBUG
    XSynchronize(dpy, True);
#endif
    printf("Display:  %s \n", DisplayString(dpy));

    if ((tc = XECreateTC(dpy,0L, NULL)) == NULL)
    {
        fprintf(stderr,"%s: could not initialize XTrap extension\n", ProgName);
        exit (1L);
    }
    (void)XEGetAvailableRequest(tc,&ret_avail);
    XEPrintAvail(stderr,&ret_avail);
    XEPrintTkFlags(stderr,tc);

    if (grabFlag == True)
    {   /* 
         * In order to ignore GrabServer's we must configure at least one 
         * trap.  Let's make it X_GrabServer.  We don't have to receive
         * a callback, though.
         */
        ReqFlags requests;
        (void)memset(requests,0L,sizeof(requests));
        BitTrue(requests, X_GrabServer);
        XETrapSetRequests(tc, True, requests);
        (void)XETrapSetGrabServer(tc, True);
    }

    (void)XEStartTrapRequest(tc);
    (void)XEGetCurrentRequest(tc,&ret_cur);
    XEPrintCurrent(stderr,&ret_cur);

    /* Open up script file */
    while (found_input_rec(ifp,&rec) == True)
    {
        /* if not pipe'd, delay time delta time recorded */
        if (ifp != stdin)
        {
            register INT32 delta, t1, t2;
            last_time = (last_time ? last_time : rec.ts);      /* first rec */
            rec.ts = (rec.ts ? rec.ts : last_time);    /* dual monitor bug! */
            t1 = rec.ts; t2 = last_time;        /* move to signed variables */
            delta = abs(t1 - t2);           /* protect from clock roll-over */
            msleep(delta);
            last_time = rec.ts;
        }
        XESimulateXEventRequest(tc, rec.type, rec.detail, rec.x, rec.y,
            rec.screen);
    }
      
    (void)XCloseDisplay(dpy);
    exit(0L);
}

static Bool found_input_rec(FILE *ifp, file_rec *rec)
{
    int found = False;
    char buff[BUFSIZ];
    char junk[16L];
    int  tmp[8L];

    while ((found != True) && (fgets(buff,BUFSIZ,ifp) != NULL))
    {
        if (!strncmp(buff, "Event:", strlen("Event:")))
        {   /* we want this record */
            if (sscanf(buff,
             "Event: %s (%d):det=%d scr=%d (%d,%d) root=%d Msk=%d TS=%d\n",
                junk, &(tmp[0L]), &(tmp[1L]), &(tmp[2L]), &(tmp[3L]), 
                &(tmp[4L]), &(tmp[5L]), &(tmp[6L]), &(tmp[7L])) != 9L)
            {
                fprintf(stderr, "%s:  Error parsing script input!\n\t'%s'\n",
                    ProgName, buff);
            }
            else
            {
                found = True;
                /* Sun's have problems with "byte" fields passed to scanf */
                rec->type   = tmp[0L];
                rec->detail = tmp[1L];
                rec->screen = tmp[2L];
                rec->x      = tmp[3L];
                rec->y      = tmp[4L];
                rec->ts     = tmp[7L];
            }
        }
        else if (!strncmp(buff, "Request:", strlen("Request:")))
        {   /* a valid thing to see */
            continue;
        }
        else
        {   /* this stuff doesn't look like what we'd expect */
            fprintf(stderr, "%s:  Not a valid script record!\n\t'%s'\n",
                ProgName, buff);
        }
    }

    return(found);
}