summaryrefslogtreecommitdiff
path: root/src/XETrapInit.c
blob: 3a76d0e89a3f153b57ffe926b8c442b814afdb5a (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
/* $XFree86: xc/lib/XTrap/XETrapInit.c,v 1.3 2002/10/16 00:37:27 dawes Exp $ */
/*****************************************************************************
Copyright 1987, 1988, 1989, 1990, 1991, 1992 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.

*****************************************************************************/
#define NEED_EVENTS
#define NEED_REPLIES

#include <stdio.h>
#include <X11/extensions/xtraplib.h>
#include <X11/extensions/xtraplibp.h>
#include <X11/extensions/Xext.h>
#include <X11/extensions/extutil.h>

#ifndef XETrapNumberErrors
#define XETrapNumberErrors 0
#endif

#ifdef UWS40
#define _XSetLastRequestRead _SetLastRequestRead
#endif
#ifndef vms
extern unsigned long _XSetLastRequestRead(Display *dpy, xGenericReply *rep);
#else
static unsigned long _XSetLastRequestRead(Display *dpy, xGenericReply *rep);
#endif

static XExtensionInfo *xtrap_info = NULL;
static /* const */ char *xtrap_extension_name = XTrapExtName;

#define XTrapCheckExtension(dpy,i,val) \
    XextCheckExtension(dpy, i, xtrap_extension_name, val)
#define XTrapSimpleCheckExtension(dpy,i) \
    XextSimpleCheckExtension(dpy, i, xtrap_extension_name)

static XEXT_CLOSE_DISPLAY_PROTO(close_display);
static Bool wire_to_event(Display *dpy, XEvent *event, xEvent *wire_ev);
static Status event_to_wire(Display *dpy, XEvent *event, xEvent *wire_ev);

#ifdef X11R3
static int error_string();
#else
static XEXT_ERROR_STRING_PROTO(error_string);
#endif
static /* const */ XExtensionHooks xtrap_extension_hooks = {
    NULL,                               /* create_gc */
    NULL,                               /* copy_gc */
    NULL,                               /* flush_gc */
    NULL,                               /* free_gc */
    NULL,                               /* create_font */
    NULL,                               /* free_font */
    close_display,                      /* close_display */
    wire_to_event,                      /* wire_to_event */
    event_to_wire,                      /* event_to_wire */
    NULL,                               /* error */
    error_string                        /* error_string */
};

static /* const */ char *xtrap_error_list[] = {
    "BadTransport (I/O transport not available)",
    "BadMailbox (Cannot connect/disconnect to mailbox)",
    "BadIO (I/O error while reading/writing in extension)",
    "BadHostname (Cannot locate requested host)",
    "BadStatistics (Statistics not configured/available)",
    "BadDevices (Devices not properly vectored)",
    "BadSocket (Cannot connect to INTERNET socket)",
    "BadScreen (Cannot send event to given screen)",
    "BadSwapReq (Cannot trap extension requests for swapped client)",
};

static XEXT_GENERATE_FIND_DISPLAY (find_display, xtrap_info,
                                   xtrap_extension_name,
                                   &xtrap_extension_hooks,
                                   XETrapNumberEvents, NULL)

static XEXT_GENERATE_CLOSE_DISPLAY (close_display, xtrap_info)

static XEXT_GENERATE_ERROR_STRING (error_string, xtrap_extension_name,
                                   XETrapNumErrors, xtrap_error_list)

static Bool event_to_wire(Display *dpy, XEvent *libevent, xEvent *netevent)
{
    Bool status = False;
    XExtDisplayInfo *info = find_display(dpy);

    XTrapCheckExtension(dpy,info,False);

    /* If we had more then one event */
#if XETrapNumberErrors > 1
    switch((netevent->u.u.type & 0x7f) - info->codes->first_event)
    {   case XETrapData:
#endif
        {
            XETrapDataEvent *ev    = (XETrapDataEvent *) libevent;
            xETrapDataEvent *event = (xETrapDataEvent *) netevent;

            event->type           = ev->type;
            event->detail         = ev->detail;
            event->sequenceNumber = (ev->serial & 0xFFFF);
            event->idx            = ev->idx;
            (void)memcpy(event->data,ev->data,sizeof(event->data));
            status = True;
        }
#if XETrapNumberErrors > 1
    }
#endif
    return(status);
}

static Bool wire_to_event(Display *dpy, XEvent *libevent, xEvent *netevent)
{
    Bool status = False;
    XExtDisplayInfo *info = find_display(dpy);

    XTrapCheckExtension(dpy,info,False);

    /* If we had more then one event */
#if XETrapNumberErrors > 1
    switch((netevent->u.u.type & 0x7f) - info->codes->first_event)
    {   case XETrapData:
#endif
        {
            XETrapDataEvent *ev    = (XETrapDataEvent *) libevent;
            xETrapDataEvent *event = (xETrapDataEvent *) netevent;

            ev->type      = event->type & 0x7F;
            ev->detail    = event->detail;
            ev->serial    = _XSetLastRequestRead(dpy,(xGenericReply *)netevent);
            ev->synthetic = ((event->type & 0x80) != 0);
            ev->display   = dpy;
            ev->idx       = event->idx;
            (void)memcpy(ev->data,event->data,sizeof(ev->data));
            status = True;
        }
#if XETrapNumberErrors > 1
    }
#endif
    return(status);
}

/*
 * XETrapQueryExtension -
 *      Returns True if the DEC-XTRAP extension is available
 *      on the given display.  If the extension exists, the value of the
 *      first event code is stored into event_base and the value of the first
 *      error code is stored into error_base.
 */
Bool XETrapQueryExtension(Display *dpy,INT32 *event_base_return,
    INT32 *error_base_return, INT32 *opcode_return)
{
    Bool status = True;
    XExtDisplayInfo *info = find_display (dpy);

    if (XextHasExtension (info))
    {
        *event_base_return = (INT32)(info->codes->first_event);
        *error_base_return = (INT32)(info->codes->first_error);
	*opcode_return     = (INT32)(info->codes->major_opcode);
    }
    else
    {
        status = False;
    }
    return(status);
}

#ifdef vms
/* Hard-coded since this didn't make it into XLibShr's xfer vector */
/* From [.XLIBEL.SRC]XLibInt.c in VMS Source Pool */
unsigned long _XSetLastRequestRead(Display *dpy, xGenericReply *rep)
{
    register unsigned long      newseq, lastseq;

    /*
     * KeymapNotify has no sequence number, but is always guaranteed
     * to immediately follow another event, except when generated via
     * SendEvent (hmmm).
     */
    if ((rep->type & 0x7f) == KeymapNotify)
        return(dpy->last_request_read);

    newseq = (dpy->last_request_read & ~((unsigned long)0xffff)) |
             rep->sequenceNumber;
    lastseq = dpy->last_request_read;
    while (newseq < lastseq) {
        newseq += 0x10000;
        if (newseq > dpy->request) {
            (void) fprintf (stderr,
            "Xlib:  sequence lost (0x%lx > 0x%lx) in reply type 0x%x!\n",
                                    newseq, dpy->request,
                                   (unsigned int) rep->type);
            newseq -= 0x10000;
           break;
        }
    }

    dpy->last_request_read = newseq;
    return(newseq);
}
#endif