1876ff6feSmrg/* $XFree86: xc/programs/xtrap/xtrapout.c,v 1.1 2001/11/02 23:29:34 dawes Exp $ */
2876ff6feSmrg/*
3876ff6feSmrg * @DEC_COPYRIGHT@
4876ff6feSmrg */
5876ff6feSmrg/*
6876ff6feSmrg * HISTORY
7876ff6feSmrg * Log: xtrapout.c,v $
8876ff6feSmrg * Revision 1.1.4.2  1993/12/14  12:37:28  Kenneth_Miller
9876ff6feSmrg * 	ANSI-standardize code and turn client build on
10876ff6feSmrg * 	[1993/12/09  20:16:01  Kenneth_Miller]
11876ff6feSmrg *
12876ff6feSmrg * Revision 1.1.2.2  1992/04/27  13:51:57  Leela_Obilichetti
13876ff6feSmrg * 	initial load of xtrap clients - from silver BL6 pool
14876ff6feSmrg * 	[92/04/27  13:49:33  Leela_Obilichetti]
15876ff6feSmrg *
16876ff6feSmrg * EndLog$
17876ff6feSmrg */
18876ff6feSmrg/*****************************************************************************
19876ff6feSmrgCopyright 1987, 1988, 1989, 1990, 1991, 1993 by Digital Equipment Corp.,
20876ff6feSmrgMaynard, MA
21876ff6feSmrg
22876ff6feSmrgPermission to use, copy, modify, and distribute this software and its
23876ff6feSmrgdocumentation for any purpose and without fee is hereby granted,
24876ff6feSmrgprovided that the above copyright notice appear in all copies and that
25876ff6feSmrgboth that copyright notice and this permission notice appear in
26876ff6feSmrgsupporting documentation, and that the name of Digital not be
27876ff6feSmrgused in advertising or publicity pertaining to distribution of the
28876ff6feSmrgsoftware without specific, written prior permission.
29876ff6feSmrg
30876ff6feSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
31876ff6feSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
32876ff6feSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
33876ff6feSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
34876ff6feSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
35876ff6feSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
36876ff6feSmrgSOFTWARE.
37876ff6feSmrg
38876ff6feSmrg*****************************************************************************/
39876ff6feSmrg/*
40876ff6feSmrg *
41876ff6feSmrg *  CONTRIBUTORS:
42876ff6feSmrg *
43876ff6feSmrg *      Dick Annicchiarico
44876ff6feSmrg *      Robert Chesler
45876ff6feSmrg *      Dan Coutu
46876ff6feSmrg *      Gene Durso
47876ff6feSmrg *      Marc Evans
48876ff6feSmrg *      Alan Jamison
49876ff6feSmrg *      Mark Henry
50876ff6feSmrg *      Ken Miller
51876ff6feSmrg *
52876ff6feSmrg */
53876ff6feSmrg#define ProgName "xtrapout"
54876ff6feSmrg/*
55876ff6feSmrg**++
56876ff6feSmrg**  FACILITY:  xtrapout - Sample client to test output from XTrap extension
57876ff6feSmrg**
58876ff6feSmrg**  MODULE DESCRIPTION:
59876ff6feSmrg**
60876ff6feSmrg**      This is the main module for a sample/test client
61876ff6feSmrg**      for the XTrap X11 Server Extension.  It accepts
62876ff6feSmrg**      a script output file, a transport method,
63876ff6feSmrg**      and an "events mode" flag (-e) as input,
64876ff6feSmrg**      in addition to the standard X arguments (-d, etc.).
65876ff6feSmrg**      If no script file is provided, stdout is the default
66876ff6feSmrg**      and can be piped to the companion "xtrapin"
67876ff6feSmrg**      client (normally used with the -e argument which
68876ff6feSmrg**      sends all core input events to stdout).
69876ff6feSmrg**
70876ff6feSmrg**
71876ff6feSmrg**  AUTHORS:
72876ff6feSmrg**
73876ff6feSmrg**      Kenneth B. Miller
74876ff6feSmrg**
75876ff6feSmrg**  CREATION DATE:  March 28, 1990
76876ff6feSmrg**
77876ff6feSmrg**  DESIGN ISSUES:
78876ff6feSmrg**
79876ff6feSmrg**      See the companion "xtrapin" client.
80876ff6feSmrg**
81876ff6feSmrg**      Also, getopt() is used to parse the command
82876ff6feSmrg**      line arguments prior to calling XtAppInitialize().
83876ff6feSmrg**      This is because DECwindows appears to remove the user-
84876ff6feSmrg**      defined arguments from the argv[] vector without actually
85876ff6feSmrg**      acting upon them.
86876ff6feSmrg**
87876ff6feSmrg**
88876ff6feSmrg**--
89876ff6feSmrg*/
90876ff6feSmrg
9199435119Smrg#ifdef HAVE_CONFIG_H
9299435119Smrg#include "config.h"
9399435119Smrg#endif
9499435119Smrg
952f9f5fecSjoerg#include <inttypes.h>
96876ff6feSmrg#include <stdio.h>
97876ff6feSmrg#include <X11/extensions/xtraplib.h>
98876ff6feSmrg#include <X11/extensions/xtraplibp.h>
99876ff6feSmrg#include <signal.h>
100876ff6feSmrg#include <X11/keysym.h>
101876ff6feSmrg#include <unistd.h>
102876ff6feSmrg#include <stdlib.h>
103876ff6feSmrg
104876ff6feSmrg#include "XEKeybCtrl.h"
105876ff6feSmrg
106876ff6feSmrg#ifndef vaxc
107876ff6feSmrg#define globalref extern
108876ff6feSmrg#endif
109876ff6feSmrg#ifdef Lynx
110876ff6feSmrgextern char *optarg;
111876ff6feSmrgextern int optind;
112876ff6feSmrgextern int opterr;
113876ff6feSmrg#endif
114876ff6feSmrg
115876ff6feSmrg
116876ff6feSmrg/* Forward declarations */
11799435119Smrgstatic void SetGlobalDone (int unused );
118876ff6feSmrgstatic void print_req_callback (XETC *tc , XETrapDatum *data ,
11999435119Smrg    BYTE *my_buf );
120876ff6feSmrgstatic void print_evt_callback (XETC *tc , XETrapDatum *data ,
12199435119Smrg    BYTE *my_buf );
122876ff6feSmrg
123876ff6feSmrg
12499435119Smrgstatic FILE *ofp;
12599435119Smrgstatic Bool GlobalDone = False;
12699435119Smrgstatic XrmOptionDescRec optionTable [] =
127876ff6feSmrg{
128876ff6feSmrg    {"-f",     "*script",    XrmoptionSepArg,  (caddr_t) NULL},
129876ff6feSmrg    {"-e",     "*eventFlag", XrmoptionSkipArg, (caddr_t) NULL},
130876ff6feSmrg    {"-v",     "*verbose",   XrmoptionSkipArg, (caddr_t) NULL},
131876ff6feSmrg};
132876ff6feSmrg
1331e036838Sjoergstatic void SetGlobalDone(int unused)
134876ff6feSmrg{
135876ff6feSmrg    GlobalDone = 1L;
136876ff6feSmrg    fprintf(stderr,"Process Completed!\n");
137876ff6feSmrg    return;
138876ff6feSmrg}
139876ff6feSmrg
14099435119Smrgstatic void print_req_callback(XETC *tc, XETrapDatum *data, BYTE *my_buf)
141876ff6feSmrg{
142876ff6feSmrg    char *req_type;
143876ff6feSmrg    req_type = (data->u.req.reqType == XETrapGetExtOpcode(tc) ? "XTrap" :
144876ff6feSmrg        XERequestIDToString(data->u.req.reqType,tc));
145876ff6feSmrg    fprintf(ofp,"Request: %-19s (%d): length '%ld' client '%d' window=%ld\n",
146876ff6feSmrg        req_type, data->u.req.reqType, (long)data->hdr.count, data->hdr.client,
147876ff6feSmrg        (long)data->u.req.id);
148876ff6feSmrg}
149876ff6feSmrg
15099435119Smrgstatic void print_evt_callback(XETC *tc, XETrapDatum *data, BYTE *my_buf)
151876ff6feSmrg{
152876ff6feSmrg    static Time last_time = 0;
153876ff6feSmrg    int delta;
154876ff6feSmrg
1552f9f5fecSjoerg    delta = imaxabs((int)last_time ? (intmax_t)data->u.event.u.keyButtonPointer.time -
156876ff6feSmrg        (int)last_time  : (int)last_time);
157876ff6feSmrg    last_time = data->u.event.u.keyButtonPointer.time;
158876ff6feSmrg
159876ff6feSmrg    /* The "screen" and "root" fields aren't valid until "event"
160876ff6feSmrg     * vectoring becomes a reality.  Currently, XTrap intercepts
161876ff6feSmrg     * the core events prior to when fields other than rootX, rootY,
162876ff6feSmrg     * type, detail, time, and state are filled in.  This will be
163876ff6feSmrg     * addressed in the next release of XTrap (3.2?).
164876ff6feSmrg     */
165876ff6feSmrg    fprintf(ofp,
166876ff6feSmrg        "Event: %-15s (%d):det=%d scr=%d (%d,%d) root=%d Msk=%d TS=%d\n",
167876ff6feSmrg        XEEventIDToString(data->u.event.u.u.type,tc), data->u.event.u.u.type,
168876ff6feSmrg        data->u.event.u.u.detail, data->hdr.screen, /* Not really valid yet */
169876ff6feSmrg        data->u.event.u.keyButtonPointer.rootX,
170876ff6feSmrg        data->u.event.u.keyButtonPointer.rootY,
171876ff6feSmrg        (int)data->u.event.u.keyButtonPointer.root, /* Not really valid yet */
172876ff6feSmrg        (int)data->u.event.u.keyButtonPointer.state,
173876ff6feSmrg        (int)delta);
174876ff6feSmrg    fflush(ofp);
175876ff6feSmrg}
176876ff6feSmrgstatic Boolean eventFlag = False;
177876ff6feSmrgstatic Boolean verboseFlag = False;
178876ff6feSmrgstatic Widget appW;
179876ff6feSmrgstatic Display *dpy;
180876ff6feSmrg
181876ff6feSmrgint
182876ff6feSmrgmain(int argc, char *argv[])
183876ff6feSmrg{
184876ff6feSmrg    XETrapGetAvailRep ret_avail;
185876ff6feSmrg    XETrapGetCurRep   ret_cur;
186876ff6feSmrg    XETC    *tc;
187876ff6feSmrg    ReqFlags     requests;
188876ff6feSmrg    EventFlags   events;
189876ff6feSmrg    XtAppContext app;
190876ff6feSmrg    char *tmp = NULL;
191876ff6feSmrg    INT16 ch;
192876ff6feSmrg    int *popterr;
193876ff6feSmrg    char **poptarg;
194876ff6feSmrg#ifndef vms
195876ff6feSmrg    popterr = &opterr;
196876ff6feSmrg    poptarg = &optarg;
197876ff6feSmrg#else
198876ff6feSmrg    popterr = XEgetopterr();
199876ff6feSmrg    poptarg = XEgetoptarg();
200876ff6feSmrg#endif
201876ff6feSmrg
202876ff6feSmrg    eventFlag = False;
203876ff6feSmrg    ofp = NULL;
204876ff6feSmrg    *popterr = 0; /* don't complain about -d (display) */
205876ff6feSmrg    while ((ch = getopt(argc, argv, "d:evf:")) != EOF)
206876ff6feSmrg    {
207876ff6feSmrg        switch(ch)
208876ff6feSmrg        {
209876ff6feSmrg            case 'e':
210876ff6feSmrg                eventFlag = True;
211876ff6feSmrg                break;
212876ff6feSmrg            case 'v':
213876ff6feSmrg                verboseFlag = True;
214876ff6feSmrg                break;
215876ff6feSmrg            case 'f':
216876ff6feSmrg                if ((ofp = fopen(*poptarg,"wb")) == NULL)
217876ff6feSmrg                {   /* can't open it */
218876ff6feSmrg                    fprintf(stderr,"%s: could not open output file '%s'!\n",
219876ff6feSmrg                        ProgName, *poptarg);
220876ff6feSmrg                }
221876ff6feSmrg                break;
222876ff6feSmrg            case 'd':   /* -display, let's let the toolkit parse it */
223876ff6feSmrg                break;
224876ff6feSmrg            default:
225876ff6feSmrg                break;
226876ff6feSmrg        }
227876ff6feSmrg    }
228876ff6feSmrg    ofp = (ofp ? ofp : stdout);
229876ff6feSmrg
230876ff6feSmrg    appW = XtAppInitialize(&app,"XTrap",optionTable,(Cardinal)2L,
231876ff6feSmrg        (int *)&argc, (String *)argv, (String *)NULL,(ArgList)&tmp,
23299435119Smrg        0);
233876ff6feSmrg
234876ff6feSmrg    dpy = XtDisplay(appW);
235876ff6feSmrg#ifdef DEBUG
236876ff6feSmrg    XSynchronize(dpy, True);
237876ff6feSmrg#endif
238876ff6feSmrg    fprintf(stderr,"Display:  %s \n", DisplayString(dpy));
23999435119Smrg    if ((tc = XECreateTC(dpy,0L, NULL)) == NULL)
240876ff6feSmrg    {
241876ff6feSmrg        fprintf(stderr,"%s: could not initialize XTrap extension\n",ProgName);
242876ff6feSmrg        exit (1L);
243876ff6feSmrg    }
244876ff6feSmrg    XETrapSetTimestamps(tc,True, False);
245876ff6feSmrg    (void)XEGetAvailableRequest(tc,&ret_avail);
246876ff6feSmrg    XEPrintAvail(stderr,&ret_avail);
247876ff6feSmrg    XEPrintTkFlags(stderr,tc);
248876ff6feSmrg
249876ff6feSmrg    /* Need to prime events/requests initially turning all off  */
250876ff6feSmrg    (void)memset(requests,0L,sizeof(requests));
251876ff6feSmrg    (void)memset(events,0L,sizeof(events));
252876ff6feSmrg    /* Now turn on the ones you really want */
253876ff6feSmrg    (void)memset(events,0xFFL,XETrapMaxEvent);
254876ff6feSmrg    if (eventFlag == False)
255876ff6feSmrg    {   /* doesn't want just events */
256876ff6feSmrg        (void)memset(requests,0xFFL,XETrapMaxRequest);
257876ff6feSmrg        /* Turn off XTrap Requests for multi-client regression tests & XLib */
258876ff6feSmrg        BitFalse(requests, XETrapGetExtOpcode(tc));
259876ff6feSmrg        /* Turn off noisy events */
260876ff6feSmrg        BitFalse(events, MotionNotify);
261876ff6feSmrg    }
262876ff6feSmrg    if (verboseFlag == True)
263876ff6feSmrg    {   /* want's *all* requests/events */
264876ff6feSmrg        (void)memset(requests,0xFFL,XETrapMaxRequest);
265876ff6feSmrg        (void)memset(events,0xFFL,XETrapMaxEvent);
266876ff6feSmrg    }
267876ff6feSmrg    /* Tell the TC about it */
268876ff6feSmrg    XETrapSetRequests(tc, True, requests);
269876ff6feSmrg    XETrapSetEvents(tc, True, events);
270876ff6feSmrg    XETrapSetMaxPacket(tc, True, XETrapMinPktSize); /* just get the minimum */
271876ff6feSmrg
272876ff6feSmrg    /* Set up callbacks for data */
273876ff6feSmrg    XEAddRequestCBs(tc, requests, print_req_callback, NULL);
274876ff6feSmrg    XEAddEventCBs(tc, events, print_evt_callback, NULL);
275876ff6feSmrg
276876ff6feSmrg    (void)XEStartTrapRequest(tc);
277876ff6feSmrg    (void)XEGetCurrentRequest(tc,&ret_cur);
278876ff6feSmrg    XEPrintCurrent(stderr,&ret_cur);
279876ff6feSmrg
280876ff6feSmrg    /* Add signal handlers so that we clean up properly */
2811e036838Sjoerg    _InitExceptionHandling(SetGlobalDone);
2821e036838Sjoerg    (void)XEEnableCtrlKeys(SetGlobalDone);
283876ff6feSmrg
284876ff6feSmrg    XETrapAppWhileLoop(app,tc,&GlobalDone);
285876ff6feSmrg
286876ff6feSmrg    /* Make sure <CTRL> key is released */
287876ff6feSmrg    XESimulateXEventRequest(tc, KeyRelease,
288876ff6feSmrg        XKeysymToKeycode(dpy, XK_Control_L), 0L, 0L, 0L);
289876ff6feSmrg
290876ff6feSmrg    /* close down everything nicely */
291876ff6feSmrg    XEFreeTC(tc);
292876ff6feSmrg    (void)XCloseDisplay(dpy);
293876ff6feSmrg    (void)XEClearCtrlKeys();
294876ff6feSmrg    _ClearExceptionHandling();
295876ff6feSmrg    exit(0L);
296876ff6feSmrg}
297876ff6feSmrg
298