1/************************************************************
2
3Copyright 1989, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.
26
27			All Rights Reserved
28
29Permission to use, copy, modify, and distribute this software and its
30documentation for any purpose and without fee is hereby granted,
31provided that the above copyright notice appear in all copies and that
32both that copyright notice and this permission notice appear in
33supporting documentation, and that the name of Hewlett-Packard not be
34used in advertising or publicity pertaining to distribution of the
35software without specific, written prior permission.
36
37HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43SOFTWARE.
44
45********************************************************/
46
47/********************************************************************
48 *
49 *  Dispatch routines and initialization routines for the X input extension.
50 *
51 */
52#define ARRAY_SIZE(_a)        (sizeof((_a)) / sizeof((_a)[0]))
53
54#define	 NUMTYPES 15
55
56#ifdef HAVE_DIX_CONFIG_H
57#include <dix-config.h>
58#endif
59
60#include "inputstr.h"
61#include "gcstruct.h"	/* pointer for extnsionst.h */
62#include "extnsionst.h"	/* extension entry   */
63#include <X11/extensions/XI.h>
64#include <X11/extensions/XIproto.h>
65#include <X11/extensions/XI2proto.h>
66#include <X11/extensions/geproto.h>
67#include "geext.h" /* extension interfaces for ge */
68
69#include "dixevents.h"
70#include "exevents.h"
71#include "extinit.h"
72#include "exglobals.h"
73#include "swaprep.h"
74#include "privates.h"
75#include "protocol-versions.h"
76
77/* modules local to Xi */
78#include "allowev.h"
79#include "chgdctl.h"
80#include "chgfctl.h"
81#include "chgkbd.h"
82#include "chgprop.h"
83#include "chgptr.h"
84#include "closedev.h"
85#include "devbell.h"
86#include "getbmap.h"
87#include "getbmap.h"
88#include "getdctl.h"
89#include "getfctl.h"
90#include "getfocus.h"
91#include "getkmap.h"
92#include "getmmap.h"
93#include "getprop.h"
94#include "getselev.h"
95#include "getvers.h"
96#include "getvers.h"
97#include "grabdev.h"
98#include "grabdevb.h"
99#include "grabdevk.h"
100#include "gtmotion.h"
101#include "listdev.h"
102#include "opendev.h"
103#include "queryst.h"
104#include "selectev.h"
105#include "sendexev.h"
106#include "chgkmap.h"
107#include "setbmap.h"
108#include "setdval.h"
109#include "setfocus.h"
110#include "setmmap.h"
111#include "setmode.h"
112#include "ungrdev.h"
113#include "ungrdevb.h"
114#include "ungrdevk.h"
115#include "xiallowev.h"
116#include "xiselectev.h"
117#include "xigrabdev.h"
118#include "xipassivegrab.h"
119#include "xisetdevfocus.h"
120#include "xiproperty.h"
121#include "xichangecursor.h"
122#include "xichangehierarchy.h"
123#include "xigetclientpointer.h"
124#include "xiquerydevice.h"
125#include "xiquerypointer.h"
126#include "xiqueryversion.h"
127#include "xisetclientpointer.h"
128#include "xiwarppointer.h"
129
130
131/* Masks for XI events have to be aligned with core event (partially anyway).
132 * If DeviceButtonMotionMask is != ButtonMotionMask, event delivery
133 * breaks down. The device needs the dev->button->motionMask. If DBMM is
134 * the same as BMM, we can ensure that both core and device events can be
135 * delivered, without the need for extra structures in the DeviceIntRec. */
136const Mask DeviceKeyPressMask             = KeyPressMask;
137const Mask DeviceKeyReleaseMask           = KeyReleaseMask;
138const Mask DeviceButtonPressMask          = ButtonPressMask;
139const Mask DeviceButtonReleaseMask        = ButtonReleaseMask;
140const Mask DeviceProximityMask            = (1L << 4);
141const Mask DeviceStateNotifyMask          = (1L << 5);
142const Mask DevicePointerMotionMask        = PointerMotionMask;
143const Mask DevicePointerMotionHintMask    = PointerMotionHintMask;
144const Mask DeviceButton1MotionMask        = Button1MotionMask;
145const Mask DeviceButton2MotionMask        = Button2MotionMask;
146const Mask DeviceButton3MotionMask        = Button3MotionMask;
147const Mask DeviceButton4MotionMask        = Button4MotionMask;
148const Mask DeviceButton5MotionMask        = Button5MotionMask;
149const Mask DeviceButtonMotionMask         = ButtonMotionMask;
150const Mask DeviceFocusChangeMask          = (1L << 14);
151const Mask DeviceMappingNotifyMask        = (1L << 15);
152const Mask ChangeDeviceNotifyMask         = (1L << 16);
153const Mask DeviceButtonGrabMask           = (1L << 17);
154const Mask DeviceOwnerGrabButtonMask      = (1L << 17);
155const Mask DevicePresenceNotifyMask       = (1L << 18);
156const Mask DeviceEnterWindowMask          = (1L << 18);
157const Mask DeviceLeaveWindowMask          = (1L << 19);
158const Mask DevicePropertyNotifyMask       = (1L << 20);
159const Mask XIAllMasks                     = (1L << 21) - 1;
160
161int ExtEventIndex;
162Mask ExtExclusiveMasks[EMASKSIZE];
163
164static struct dev_type
165{
166    Atom type;
167    char *name;
168} dev_type[] = {
169    {
170    0, XI_KEYBOARD}, {
171    0, XI_MOUSE}, {
172    0, XI_TABLET}, {
173    0, XI_TOUCHSCREEN}, {
174    0, XI_TOUCHPAD}, {
175    0, XI_BARCODE}, {
176    0, XI_BUTTONBOX}, {
177    0, XI_KNOB_BOX}, {
178    0, XI_ONE_KNOB}, {
179    0, XI_NINE_KNOB}, {
180    0, XI_TRACKBALL}, {
181    0, XI_QUADRATURE}, {
182    0, XI_ID_MODULE}, {
183    0, XI_SPACEBALL}, {
184    0, XI_DATAGLOVE}, {
185    0, XI_EYETRACKER}, {
186    0, XI_CURSORKEYS}, {
1870, XI_FOOTMOUSE}};
188
189CARD8 event_base[numInputClasses];
190XExtEventInfo EventInfo[32];
191
192static DeviceIntRec xi_all_devices;
193static DeviceIntRec xi_all_master_devices;
194
195/**
196 * Dispatch vector. Functions defined in here will be called when the matching
197 * request arrives.
198 */
199static int (*ProcIVector[])(ClientPtr) = {
200        NULL,                                   /*  0 */
201	ProcXGetExtensionVersion,               /*  1 */
202	ProcXListInputDevices,                  /*  2 */
203	ProcXOpenDevice,                        /*  3 */
204	ProcXCloseDevice,                       /*  4 */
205	ProcXSetDeviceMode,                     /*  5 */
206	ProcXSelectExtensionEvent,              /*  6 */
207	ProcXGetSelectedExtensionEvents,        /*  7 */
208	ProcXChangeDeviceDontPropagateList,     /*  8 */
209	ProcXGetDeviceDontPropagateList,        /*  9 */
210	ProcXGetDeviceMotionEvents,             /* 10 */
211	ProcXChangeKeyboardDevice,              /* 11 */
212	ProcXChangePointerDevice,               /* 12 */
213	ProcXGrabDevice,                        /* 13 */
214	ProcXUngrabDevice,                      /* 14 */
215	ProcXGrabDeviceKey,                     /* 15 */
216	ProcXUngrabDeviceKey,                   /* 16 */
217	ProcXGrabDeviceButton,                  /* 17 */
218	ProcXUngrabDeviceButton,                /* 18 */
219	ProcXAllowDeviceEvents,                 /* 19 */
220	ProcXGetDeviceFocus,                    /* 20 */
221	ProcXSetDeviceFocus,                    /* 21 */
222	ProcXGetFeedbackControl,                /* 22 */
223	ProcXChangeFeedbackControl,             /* 23 */
224	ProcXGetDeviceKeyMapping,               /* 24 */
225	ProcXChangeDeviceKeyMapping,            /* 25 */
226	ProcXGetDeviceModifierMapping,          /* 26 */
227	ProcXSetDeviceModifierMapping,          /* 27 */
228	ProcXGetDeviceButtonMapping,            /* 28 */
229	ProcXSetDeviceButtonMapping,            /* 29 */
230	ProcXQueryDeviceState,                  /* 30 */
231	ProcXSendExtensionEvent,                /* 31 */
232	ProcXDeviceBell,                        /* 32 */
233	ProcXSetDeviceValuators,                /* 33 */
234	ProcXGetDeviceControl,                  /* 34 */
235	ProcXChangeDeviceControl,               /* 35 */
236        /* XI 1.5 */
237        ProcXListDeviceProperties,              /* 36 */
238        ProcXChangeDeviceProperty,              /* 37 */
239        ProcXDeleteDeviceProperty,              /* 38 */
240        ProcXGetDeviceProperty,                 /* 39 */
241        /* XI 2 */
242        ProcXIQueryPointer,                     /* 40 */
243        ProcXIWarpPointer,                      /* 41 */
244        ProcXIChangeCursor,                     /* 42 */
245        ProcXIChangeHierarchy,                  /* 43 */
246        ProcXISetClientPointer,                 /* 44 */
247        ProcXIGetClientPointer,                 /* 45 */
248        ProcXISelectEvents,                     /* 46 */
249        ProcXIQueryVersion,                     /* 47 */
250        ProcXIQueryDevice,                      /* 48 */
251        ProcXISetFocus,                         /* 49 */
252        ProcXIGetFocus,                         /* 50 */
253        ProcXIGrabDevice,                       /* 51 */
254        ProcXIUngrabDevice,                     /* 52 */
255        ProcXIAllowEvents,                      /* 53 */
256        ProcXIPassiveGrabDevice,                /* 54 */
257        ProcXIPassiveUngrabDevice,              /* 55 */
258        ProcXIListProperties,                   /* 56 */
259        ProcXIChangeProperty,                   /* 57 */
260        ProcXIDeleteProperty,                   /* 58 */
261        ProcXIGetProperty,                      /* 59 */
262        ProcXIGetSelectedEvents                 /* 60 */
263};
264
265/* For swapped clients */
266static int (*SProcIVector[])(ClientPtr) = {
267        NULL,                                    /*  0 */
268	SProcXGetExtensionVersion,               /*  1 */
269	SProcXListInputDevices,                  /*  2 */
270	SProcXOpenDevice,                        /*  3 */
271	SProcXCloseDevice,                       /*  4 */
272	SProcXSetDeviceMode,                     /*  5 */
273	SProcXSelectExtensionEvent,              /*  6 */
274	SProcXGetSelectedExtensionEvents,        /*  7 */
275	SProcXChangeDeviceDontPropagateList,     /*  8 */
276	SProcXGetDeviceDontPropagateList,        /*  9 */
277	SProcXGetDeviceMotionEvents,             /* 10 */
278	SProcXChangeKeyboardDevice,              /* 11 */
279	SProcXChangePointerDevice,               /* 12 */
280	SProcXGrabDevice,                        /* 13 */
281	SProcXUngrabDevice,                      /* 14 */
282	SProcXGrabDeviceKey,                     /* 15 */
283	SProcXUngrabDeviceKey,                   /* 16 */
284	SProcXGrabDeviceButton,                  /* 17 */
285	SProcXUngrabDeviceButton,                /* 18 */
286	SProcXAllowDeviceEvents,                 /* 19 */
287	SProcXGetDeviceFocus,                    /* 20 */
288	SProcXSetDeviceFocus,                    /* 21 */
289	SProcXGetFeedbackControl,                /* 22 */
290	SProcXChangeFeedbackControl,             /* 23 */
291	SProcXGetDeviceKeyMapping,               /* 24 */
292	SProcXChangeDeviceKeyMapping,            /* 25 */
293	SProcXGetDeviceModifierMapping,          /* 26 */
294	SProcXSetDeviceModifierMapping,          /* 27 */
295	SProcXGetDeviceButtonMapping,            /* 28 */
296	SProcXSetDeviceButtonMapping,            /* 29 */
297	SProcXQueryDeviceState,                  /* 30 */
298	SProcXSendExtensionEvent,                /* 31 */
299	SProcXDeviceBell,                        /* 32 */
300	SProcXSetDeviceValuators,                /* 33 */
301	SProcXGetDeviceControl,                  /* 34 */
302	SProcXChangeDeviceControl,               /* 35 */
303        SProcXListDeviceProperties,              /* 36 */
304        SProcXChangeDeviceProperty,              /* 37 */
305        SProcXDeleteDeviceProperty,              /* 38 */
306        SProcXGetDeviceProperty,                 /* 39 */
307        SProcXIQueryPointer,                     /* 40 */
308        SProcXIWarpPointer,                      /* 41 */
309        SProcXIChangeCursor,                     /* 42 */
310        SProcXIChangeHierarchy,                  /* 43 */
311        SProcXISetClientPointer,                 /* 44 */
312        SProcXIGetClientPointer,                 /* 45 */
313        SProcXISelectEvents,                     /* 46 */
314        SProcXIQueryVersion,                     /* 47 */
315        SProcXIQueryDevice,                      /* 48 */
316        SProcXISetFocus,                         /* 49 */
317        SProcXIGetFocus,                         /* 50 */
318        SProcXIGrabDevice,                       /* 51 */
319        SProcXIUngrabDevice,                     /* 52 */
320        SProcXIAllowEvents,                      /* 53 */
321        SProcXIPassiveGrabDevice,                /* 54 */
322        SProcXIPassiveUngrabDevice,              /* 55 */
323        SProcXIListProperties,                   /* 56 */
324        SProcXIChangeProperty,                   /* 57 */
325        SProcXIDeleteProperty,                   /* 58 */
326        SProcXIGetProperty,                      /* 59 */
327        SProcXIGetSelectedEvents                 /* 60 */
328};
329
330/*****************************************************************
331 *
332 * Globals referenced elsewhere in the server.
333 *
334 */
335
336int IReqCode = 0;
337int IEventBase = 0;
338int BadDevice = 0;
339static int BadEvent = 1;
340int BadMode = 2;
341int DeviceBusy = 3;
342int BadClass = 4;
343
344int DeviceValuator;
345int DeviceKeyPress;
346int DeviceKeyRelease;
347int DeviceButtonPress;
348int DeviceButtonRelease;
349int DeviceMotionNotify;
350int DeviceFocusIn;
351int DeviceFocusOut;
352int ProximityIn;
353int ProximityOut;
354int DeviceStateNotify;
355int DeviceKeyStateNotify;
356int DeviceButtonStateNotify;
357int DeviceMappingNotify;
358int ChangeDeviceNotify;
359int DevicePresenceNotify;
360int DevicePropertyNotify;
361
362int RT_INPUTCLIENT;
363
364/*****************************************************************
365 *
366 * Externs defined elsewhere in the X server.
367 *
368 */
369
370extern XExtensionVersion XIVersion;
371
372
373Mask PropagateMask[MAXDEVICES];
374
375/*****************************************************************
376 *
377 * Versioning support
378 *
379 */
380
381DevPrivateKeyRec XIClientPrivateKeyRec;
382
383/*****************************************************************
384 *
385 * Declarations of local routines.
386 *
387 */
388
389static void
390XIClientCallback(CallbackListPtr        *list,
391                 pointer                closure,
392                 pointer                data)
393{
394    NewClientInfoRec *clientinfo = (NewClientInfoRec*)data;
395    ClientPtr pClient = clientinfo->client;
396    XIClientPtr pXIClient;
397
398    pXIClient = dixLookupPrivate(&pClient->devPrivates, XIClientPrivateKey);
399    pXIClient->major_version = 0;
400    pXIClient->minor_version = 0;
401}
402
403/*************************************************************************
404 *
405 * ProcIDispatch - main dispatch routine for requests to this extension.
406 * This routine is used if server and client have the same byte ordering.
407 *
408 */
409
410static int
411ProcIDispatch(ClientPtr client)
412{
413    REQUEST(xReq);
414    if (stuff->data >= ARRAY_SIZE(ProcIVector) || !ProcIVector[stuff->data])
415        return BadRequest;
416
417    return (*ProcIVector[stuff->data])(client);
418}
419
420/*******************************************************************************
421 *
422 * SProcXDispatch
423 *
424 * Main swapped dispatch routine for requests to this extension.
425 * This routine is used if server and client do not have the same byte ordering.
426 *
427 */
428
429static int
430SProcIDispatch(ClientPtr client)
431{
432    REQUEST(xReq);
433    if (stuff->data >= ARRAY_SIZE(SProcIVector) || !SProcIVector[stuff->data])
434        return BadRequest;
435
436    return (*SProcIVector[stuff->data])(client);
437}
438
439/**********************************************************************
440 *
441 * SReplyIDispatch
442 * Swap any replies defined in this extension.
443 *
444 */
445
446static void
447SReplyIDispatch(ClientPtr client, int len, xGrabDeviceReply * rep)
448					/* All we look at is the type field */
449{	/* This is common to all replies    */
450    if (rep->RepType == X_GetExtensionVersion)
451	SRepXGetExtensionVersion(client, len,
452				 (xGetExtensionVersionReply *) rep);
453    else if (rep->RepType == X_ListInputDevices)
454	SRepXListInputDevices(client, len, (xListInputDevicesReply *) rep);
455    else if (rep->RepType == X_OpenDevice)
456	SRepXOpenDevice(client, len, (xOpenDeviceReply *) rep);
457    else if (rep->RepType == X_SetDeviceMode)
458	SRepXSetDeviceMode(client, len, (xSetDeviceModeReply *) rep);
459    else if (rep->RepType == X_GetSelectedExtensionEvents)
460	SRepXGetSelectedExtensionEvents(client, len,
461					(xGetSelectedExtensionEventsReply *)
462					rep);
463    else if (rep->RepType == X_GetDeviceDontPropagateList)
464	SRepXGetDeviceDontPropagateList(client, len,
465					(xGetDeviceDontPropagateListReply *)
466					rep);
467    else if (rep->RepType == X_GetDeviceMotionEvents)
468	SRepXGetDeviceMotionEvents(client, len,
469				   (xGetDeviceMotionEventsReply *) rep);
470    else if (rep->RepType == X_GrabDevice)
471	SRepXGrabDevice(client, len, (xGrabDeviceReply *) rep);
472    else if (rep->RepType == X_GetDeviceFocus)
473	SRepXGetDeviceFocus(client, len, (xGetDeviceFocusReply *) rep);
474    else if (rep->RepType == X_GetFeedbackControl)
475	SRepXGetFeedbackControl(client, len, (xGetFeedbackControlReply *) rep);
476    else if (rep->RepType == X_GetDeviceKeyMapping)
477	SRepXGetDeviceKeyMapping(client, len,
478				 (xGetDeviceKeyMappingReply *) rep);
479    else if (rep->RepType == X_GetDeviceModifierMapping)
480	SRepXGetDeviceModifierMapping(client, len,
481				      (xGetDeviceModifierMappingReply *) rep);
482    else if (rep->RepType == X_SetDeviceModifierMapping)
483	SRepXSetDeviceModifierMapping(client, len,
484				      (xSetDeviceModifierMappingReply *) rep);
485    else if (rep->RepType == X_GetDeviceButtonMapping)
486	SRepXGetDeviceButtonMapping(client, len,
487				    (xGetDeviceButtonMappingReply *) rep);
488    else if (rep->RepType == X_SetDeviceButtonMapping)
489	SRepXSetDeviceButtonMapping(client, len,
490				    (xSetDeviceButtonMappingReply *) rep);
491    else if (rep->RepType == X_QueryDeviceState)
492	SRepXQueryDeviceState(client, len, (xQueryDeviceStateReply *) rep);
493    else if (rep->RepType == X_SetDeviceValuators)
494	SRepXSetDeviceValuators(client, len, (xSetDeviceValuatorsReply *) rep);
495    else if (rep->RepType == X_GetDeviceControl)
496	SRepXGetDeviceControl(client, len, (xGetDeviceControlReply *) rep);
497    else if (rep->RepType == X_ChangeDeviceControl)
498	SRepXChangeDeviceControl(client, len,
499				 (xChangeDeviceControlReply *) rep);
500    else if (rep->RepType == X_ListDeviceProperties)
501        SRepXListDeviceProperties(client, len, (xListDevicePropertiesReply*)rep);
502    else if (rep->RepType == X_GetDeviceProperty)
503	SRepXGetDeviceProperty(client, len, (xGetDevicePropertyReply *) rep);
504    else if (rep->RepType == X_XIQueryPointer)
505	SRepXIQueryPointer(client, len, (xXIQueryPointerReply *) rep);
506    else if (rep->RepType == X_XIGetClientPointer)
507        SRepXIGetClientPointer(client, len, (xXIGetClientPointerReply*) rep);
508    else if (rep->RepType == X_XIQueryVersion)
509        SRepXIQueryVersion(client, len, (xXIQueryVersionReply*)rep);
510    else if (rep->RepType == X_XIQueryDevice)
511        SRepXIQueryDevice(client, len, (xXIQueryDeviceReply*)rep);
512    else if (rep->RepType == X_XIGrabDevice)
513	SRepXIGrabDevice(client, len, (xXIGrabDeviceReply *) rep);
514    else if (rep->RepType == X_XIPassiveGrabDevice)
515	SRepXIPassiveGrabDevice(client, len, (xXIPassiveGrabDeviceReply *) rep);
516    else if (rep->RepType == X_XIListProperties)
517	SRepXIListProperties(client, len, (xXIListPropertiesReply *) rep);
518    else if (rep->RepType == X_XIGetProperty)
519	SRepXIGetProperty(client, len, (xXIGetPropertyReply *) rep);
520    else if (rep->RepType == X_XIGetSelectedEvents)
521	SRepXIGetSelectedEvents(client, len, (xXIGetSelectedEventsReply *) rep);
522    else if (rep->RepType == X_XIGetFocus)
523	SRepXIGetFocus(client, len, (xXIGetFocusReply *) rep);
524    else {
525	FatalError("XINPUT confused sending swapped reply");
526    }
527}
528
529/************************************************************************
530 *
531 * This function swaps the DeviceValuator event.
532 *
533 */
534
535static void
536SEventDeviceValuator(deviceValuator * from, deviceValuator * to)
537{
538    char n;
539    int i;
540    INT32 *ip B32;
541
542    *to = *from;
543    swaps(&to->sequenceNumber, n);
544    swaps(&to->device_state, n);
545    ip = &to->valuator0;
546    for (i = 0; i < 6; i++) {
547	swapl((ip + i), n);	/* macro - braces are required      */
548    }
549}
550
551static void
552SEventFocus(deviceFocus * from, deviceFocus * to)
553{
554    char n;
555
556    *to = *from;
557    swaps(&to->sequenceNumber, n);
558    swapl(&to->time, n);
559    swapl(&to->window, n);
560}
561
562static void
563SDeviceStateNotifyEvent(deviceStateNotify * from, deviceStateNotify * to)
564{
565    int i;
566    char n;
567    INT32 *ip B32;
568
569    *to = *from;
570    swaps(&to->sequenceNumber, n);
571    swapl(&to->time, n);
572    ip = &to->valuator0;
573    for (i = 0; i < 3; i++) {
574	swapl((ip + i), n);	/* macro - braces are required      */
575    }
576}
577
578static void
579SDeviceKeyStateNotifyEvent(deviceKeyStateNotify * from,
580			   deviceKeyStateNotify * to)
581{
582    char n;
583
584    *to = *from;
585    swaps(&to->sequenceNumber, n);
586}
587
588static void
589SDeviceButtonStateNotifyEvent(deviceButtonStateNotify * from,
590			      deviceButtonStateNotify * to)
591{
592    char n;
593
594    *to = *from;
595    swaps(&to->sequenceNumber, n);
596}
597
598static void
599SChangeDeviceNotifyEvent(changeDeviceNotify * from, changeDeviceNotify * to)
600{
601    char n;
602
603    *to = *from;
604    swaps(&to->sequenceNumber, n);
605    swapl(&to->time, n);
606}
607
608static void
609SDeviceMappingNotifyEvent(deviceMappingNotify * from, deviceMappingNotify * to)
610{
611    char n;
612
613    *to = *from;
614    swaps(&to->sequenceNumber, n);
615    swapl(&to->time, n);
616}
617
618static void
619SDevicePresenceNotifyEvent (devicePresenceNotify *from, devicePresenceNotify *to)
620{
621    char n;
622
623    *to = *from;
624    swaps(&to->sequenceNumber,n);
625    swapl(&to->time, n);
626    swaps(&to->control, n);
627}
628
629static void
630SDevicePropertyNotifyEvent (devicePropertyNotify *from, devicePropertyNotify *to)
631{
632    char n;
633
634    *to = *from;
635    swaps(&to->sequenceNumber,n);
636    swapl(&to->time, n);
637    swapl(&to->atom, n);
638}
639
640static void
641SDeviceLeaveNotifyEvent (xXILeaveEvent *from, xXILeaveEvent *to)
642{
643    char n;
644
645    *to = *from;
646    swaps(&to->sequenceNumber,n);
647    swapl(&to->length, n);
648    swaps(&to->evtype, n);
649    swaps(&to->deviceid, n);
650    swapl(&to->time, n);
651    swapl(&to->root, n);
652    swapl(&to->event, n);
653    swapl(&to->child, n);
654    swapl(&to->root_x, n);
655    swapl(&to->root_y, n);
656    swapl(&to->event_x, n);
657    swapl(&to->event_y, n);
658    swaps(&to->sourceid, n);
659    swaps(&to->buttons_len, n);
660    swapl(&to->mods.base_mods, n);
661    swapl(&to->mods.latched_mods, n);
662    swapl(&to->mods.locked_mods, n);
663}
664
665static void
666SDeviceChangedEvent(xXIDeviceChangedEvent* from, xXIDeviceChangedEvent* to)
667{
668    char n;
669    int i, j;
670    xXIAnyInfo *any;
671
672    *to = *from;
673    memcpy(&to[1], &from[1], from->length * 4);
674
675    any = (xXIAnyInfo*)&to[1];
676    for (i = 0; i < to->num_classes; i++)
677    {
678        int length = any->length;
679
680        switch(any->type)
681        {
682            case KeyClass:
683                {
684                    xXIKeyInfo *ki = (xXIKeyInfo*)any;
685                    uint32_t *key = (uint32_t*)&ki[1];
686                    for (j = 0; j < ki->num_keycodes; j++, key++)
687                        swapl(key, n);
688                    swaps(&ki->num_keycodes, n);
689                }
690                break;
691            case ButtonClass:
692                {
693                    xXIButtonInfo *bi = (xXIButtonInfo*)any;
694                    Atom *labels = (Atom*)((char*)bi + sizeof(xXIButtonInfo) +
695                                           pad_to_int32(bits_to_bytes(bi->num_buttons)));
696                    for (j = 0; j < bi->num_buttons; j++)
697                        swapl(&labels[j], n);
698                    swaps(&bi->num_buttons, n);
699                }
700                break;
701            case ValuatorClass:
702                {
703                    xXIValuatorInfo* ai = (xXIValuatorInfo*)any;
704                    swapl(&ai->label, n);
705                    swapl(&ai->min.integral, n);
706                    swapl(&ai->min.frac, n);
707                    swapl(&ai->max.integral, n);
708                    swapl(&ai->max.frac, n);
709                    swapl(&ai->resolution, n);
710                    swaps(&ai->number, n);
711                }
712                break;
713        }
714
715        swaps(&any->type, n);
716        swaps(&any->length, n);
717        swaps(&any->sourceid, n);
718
719        any = (xXIAnyInfo*)((char*)any + length * 4);
720    }
721
722    swaps(&to->sequenceNumber, n);
723    swapl(&to->length, n);
724    swaps(&to->evtype, n);
725    swaps(&to->deviceid, n);
726    swapl(&to->time, n);
727    swaps(&to->num_classes, n);
728    swaps(&to->sourceid, n);
729
730}
731
732static void SDeviceEvent(xXIDeviceEvent *from, xXIDeviceEvent *to)
733{
734    int i;
735    char n;
736    char *ptr;
737    char *vmask;
738
739    memcpy(to, from, sizeof(xEvent) + from->length * 4);
740
741    swaps(&to->sequenceNumber, n);
742    swapl(&to->length, n);
743    swaps(&to->evtype, n);
744    swaps(&to->deviceid, n);
745    swapl(&to->time, n);
746    swapl(&to->detail, n);
747    swapl(&to->root, n);
748    swapl(&to->event, n);
749    swapl(&to->child, n);
750    swapl(&to->root_x, n);
751    swapl(&to->root_y, n);
752    swapl(&to->event_x, n);
753    swapl(&to->event_y, n);
754    swaps(&to->buttons_len, n);
755    swaps(&to->valuators_len, n);
756    swaps(&to->sourceid, n);
757    swapl(&to->mods.base_mods, n);
758    swapl(&to->mods.latched_mods, n);
759    swapl(&to->mods.locked_mods, n);
760    swapl(&to->mods.effective_mods, n);
761    swapl(&to->flags, n);
762
763    ptr = (char*)(&to[1]);
764    ptr += from->buttons_len * 4;
765    vmask = ptr; /* valuator mask */
766    ptr += from->valuators_len * 4;
767    for (i = 0; i < from->valuators_len * 32; i++)
768    {
769        if (BitIsOn(vmask, i))
770        {
771            swapl(((uint32_t*)ptr), n);
772            ptr += 4;
773            swapl(((uint32_t*)ptr), n);
774            ptr += 4;
775        }
776    }
777}
778
779static void SDeviceHierarchyEvent(xXIHierarchyEvent *from,
780                                  xXIHierarchyEvent *to)
781{
782    int i;
783    char n;
784    xXIHierarchyInfo *info;
785
786    *to = *from;
787    memcpy(&to[1], &from[1], from->length * 4);
788    swaps(&to->sequenceNumber, n);
789    swapl(&to->length, n);
790    swaps(&to->evtype, n);
791    swaps(&to->deviceid, n);
792    swapl(&to->time, n);
793    swapl(&to->flags, n);
794    swaps(&to->num_info, n);
795
796    info = (xXIHierarchyInfo*)&to[1];
797    for (i = 0; i< from->num_info; i++)
798    {
799        swaps(&info->deviceid, n);
800        swaps(&info->attachment, n);
801        info++;
802    }
803}
804
805static void SXIPropertyEvent(xXIPropertyEvent *from, xXIPropertyEvent *to)
806{
807    char n;
808
809    *to = *from;
810    swaps(&to->sequenceNumber, n);
811    swapl(&to->length, n);
812    swaps(&to->evtype, n);
813    swaps(&to->deviceid, n);
814    swapl(&to->property, n);
815}
816
817static void SRawEvent(xXIRawEvent *from, xXIRawEvent *to)
818{
819    char n;
820    int i;
821    FP3232 *values;
822    unsigned char *mask;
823
824    memcpy(to, from, sizeof(xEvent) + from->length * 4);
825
826    swaps(&to->sequenceNumber, n);
827    swapl(&to->length, n);
828    swaps(&to->evtype, n);
829    swaps(&to->deviceid, n);
830    swapl(&to->time, n);
831    swapl(&to->detail, n);
832
833
834    mask = (unsigned char*)&to[1];
835    values = (FP3232*)(mask + from->valuators_len * 4);
836
837    for (i = 0; i < from->valuators_len * 4 * 8; i++)
838    {
839        if (BitIsOn(mask, i))
840        {
841            /* for each bit set there are two FP3232 values on the wire, in
842             * the order abcABC for data and data_raw. Here we swap as if
843             * they were in aAbBcC order because it's easier and really
844             * doesn't matter.
845             */
846            swapl(&values->integral, n);
847            swapl(&values->frac, n);
848            values++;
849            swapl(&values->integral, n);
850            swapl(&values->frac, n);
851            values++;
852        }
853    }
854
855    swaps(&to->valuators_len, n);
856}
857
858
859/** Event swapping function for XI2 events. */
860void
861XI2EventSwap(xGenericEvent *from, xGenericEvent *to)
862{
863    switch(from->evtype)
864    {
865        case XI_Enter:
866        case XI_Leave:
867        case XI_FocusIn:
868        case XI_FocusOut:
869            SDeviceLeaveNotifyEvent((xXILeaveEvent*)from, (xXILeaveEvent*)to);
870            break;
871        case XI_DeviceChanged:
872            SDeviceChangedEvent((xXIDeviceChangedEvent*)from,
873                                (xXIDeviceChangedEvent*)to);
874            break;
875        case XI_HierarchyChanged:
876            SDeviceHierarchyEvent((xXIHierarchyEvent*)from, (xXIHierarchyEvent*)to);
877            break;
878        case XI_PropertyEvent:
879            SXIPropertyEvent((xXIPropertyEvent*)from,
880                           (xXIPropertyEvent*)to);
881            break;
882        case XI_Motion:
883        case XI_KeyPress:
884        case XI_KeyRelease:
885        case XI_ButtonPress:
886        case XI_ButtonRelease:
887            SDeviceEvent((xXIDeviceEvent*)from, (xXIDeviceEvent*)to);
888            break;
889        case XI_RawMotion:
890        case XI_RawKeyPress:
891        case XI_RawKeyRelease:
892        case XI_RawButtonPress:
893        case XI_RawButtonRelease:
894            SRawEvent((xXIRawEvent*)from, (xXIRawEvent*)to);
895            break;
896        default:
897            ErrorF("[Xi] Unknown event type to swap. This is a bug.\n");
898            break;
899    }
900}
901
902/**************************************************************************
903 *
904 * Allow the specified event to have its propagation suppressed.
905 * The default is to not allow suppression of propagation.
906 *
907 */
908
909static void
910AllowPropagateSuppress(Mask mask)
911{
912    int i;
913
914    for (i = 0; i < MAXDEVICES; i++)
915	PropagateMask[i] |= mask;
916}
917
918/**************************************************************************
919 *
920 * Record an event mask where there is no unique corresponding event type.
921 * We can't call SetMaskForEvent, since that would clobber the existing
922 * mask for that event.  MotionHint and ButtonMotion are examples.
923 *
924 * Since extension event types will never be less than 64, we can use
925 * 0-63 in the EventInfo array as the "type" to be used to look up this
926 * mask.  This means that the corresponding macros such as
927 * DevicePointerMotionHint must have access to the same constants.
928 *
929 */
930
931static void
932SetEventInfo(Mask mask, int constant)
933{
934    EventInfo[ExtEventIndex].mask = mask;
935    EventInfo[ExtEventIndex++].type = constant;
936}
937
938/**************************************************************************
939 *
940 * Allow the specified event to be restricted to being selected by one
941 * client at a time.
942 * The default is to allow more than one client to select the event.
943 *
944 */
945
946static void
947SetExclusiveAccess(Mask mask)
948{
949    int i;
950
951    for (i = 0; i < MAXDEVICES; i++)
952	ExtExclusiveMasks[i] |= mask;
953}
954
955/**************************************************************************
956 *
957 * Assign the specified mask to the specified event.
958 *
959 */
960
961static void
962SetMaskForExtEvent(Mask mask, int event)
963{
964    int i;
965
966    EventInfo[ExtEventIndex].mask = mask;
967    EventInfo[ExtEventIndex++].type = event;
968
969    if ((event < LASTEvent) || (event >= 128))
970	FatalError("MaskForExtensionEvent: bogus event number");
971
972    for (i = 0; i < MAXDEVICES; i++)
973        SetMaskForEvent(i, mask, event);
974}
975
976/************************************************************************
977 *
978 * This function sets up extension event types and masks.
979 *
980 */
981
982static void
983FixExtensionEvents(ExtensionEntry * extEntry)
984{
985    DeviceValuator = extEntry->eventBase;
986    DeviceKeyPress = DeviceValuator + 1;
987    DeviceKeyRelease = DeviceKeyPress + 1;
988    DeviceButtonPress = DeviceKeyRelease + 1;
989    DeviceButtonRelease = DeviceButtonPress + 1;
990    DeviceMotionNotify = DeviceButtonRelease + 1;
991    DeviceFocusIn = DeviceMotionNotify + 1;
992    DeviceFocusOut = DeviceFocusIn + 1;
993    ProximityIn = DeviceFocusOut + 1;
994    ProximityOut = ProximityIn + 1;
995    DeviceStateNotify = ProximityOut + 1;
996    DeviceMappingNotify = DeviceStateNotify + 1;
997    ChangeDeviceNotify = DeviceMappingNotify + 1;
998    DeviceKeyStateNotify = ChangeDeviceNotify + 1;
999    DeviceButtonStateNotify = DeviceKeyStateNotify + 1;
1000    DevicePresenceNotify = DeviceButtonStateNotify + 1;
1001    DevicePropertyNotify = DevicePresenceNotify + 1;
1002
1003    event_base[KeyClass] = DeviceKeyPress;
1004    event_base[ButtonClass] = DeviceButtonPress;
1005    event_base[ValuatorClass] = DeviceMotionNotify;
1006    event_base[ProximityClass] = ProximityIn;
1007    event_base[FocusClass] = DeviceFocusIn;
1008    event_base[OtherClass] = DeviceStateNotify;
1009
1010    BadDevice += extEntry->errorBase;
1011    BadEvent += extEntry->errorBase;
1012    BadMode += extEntry->errorBase;
1013    DeviceBusy += extEntry->errorBase;
1014    BadClass += extEntry->errorBase;
1015
1016    SetMaskForExtEvent(DeviceKeyPressMask, DeviceKeyPress);
1017    AllowPropagateSuppress(DeviceKeyPressMask);
1018    SetCriticalEvent(DeviceKeyPress);
1019
1020    SetMaskForExtEvent(DeviceKeyReleaseMask, DeviceKeyRelease);
1021    AllowPropagateSuppress(DeviceKeyReleaseMask);
1022    SetCriticalEvent(DeviceKeyRelease);
1023
1024    SetMaskForExtEvent(DeviceButtonPressMask, DeviceButtonPress);
1025    AllowPropagateSuppress(DeviceButtonPressMask);
1026    SetCriticalEvent(DeviceButtonPress);
1027
1028    SetMaskForExtEvent(DeviceButtonReleaseMask, DeviceButtonRelease);
1029    AllowPropagateSuppress(DeviceButtonReleaseMask);
1030    SetCriticalEvent(DeviceButtonRelease);
1031
1032    SetMaskForExtEvent(DeviceProximityMask, ProximityIn);
1033    SetMaskForExtEvent(DeviceProximityMask, ProximityOut);
1034
1035    SetMaskForExtEvent(DeviceStateNotifyMask, DeviceStateNotify);
1036
1037    SetMaskForExtEvent(DevicePointerMotionMask, DeviceMotionNotify);
1038    AllowPropagateSuppress(DevicePointerMotionMask);
1039    SetCriticalEvent(DeviceMotionNotify);
1040
1041    SetEventInfo(DevicePointerMotionHintMask, _devicePointerMotionHint);
1042    SetEventInfo(DeviceButton1MotionMask, _deviceButton1Motion);
1043    SetEventInfo(DeviceButton2MotionMask, _deviceButton2Motion);
1044    SetEventInfo(DeviceButton3MotionMask, _deviceButton3Motion);
1045    SetEventInfo(DeviceButton4MotionMask, _deviceButton4Motion);
1046    SetEventInfo(DeviceButton5MotionMask, _deviceButton5Motion);
1047    SetEventInfo(DeviceButtonMotionMask, _deviceButtonMotion);
1048
1049    SetMaskForExtEvent(DeviceFocusChangeMask, DeviceFocusIn);
1050    SetMaskForExtEvent(DeviceFocusChangeMask, DeviceFocusOut);
1051
1052    SetMaskForExtEvent(DeviceMappingNotifyMask, DeviceMappingNotify);
1053    SetMaskForExtEvent(ChangeDeviceNotifyMask, ChangeDeviceNotify);
1054
1055    SetEventInfo(DeviceButtonGrabMask, _deviceButtonGrab);
1056    SetExclusiveAccess(DeviceButtonGrabMask);
1057
1058    SetEventInfo(DeviceOwnerGrabButtonMask, _deviceOwnerGrabButton);
1059    SetEventInfo(DevicePresenceNotifyMask, _devicePresence);
1060    SetMaskForExtEvent(DevicePropertyNotifyMask, DevicePropertyNotify);
1061
1062    SetEventInfo(0, _noExtensionEvent);
1063}
1064
1065/************************************************************************
1066 *
1067 * This function restores extension event types and masks to their
1068 * initial state.
1069 *
1070 */
1071
1072static void
1073RestoreExtensionEvents(void)
1074{
1075    int i, j;
1076
1077    IReqCode = 0;
1078    IEventBase = 0;
1079
1080    for (i = 0; i < ExtEventIndex - 1; i++) {
1081	if ((EventInfo[i].type >= LASTEvent) && (EventInfo[i].type < 128))
1082        {
1083            for (j = 0; j < MAXDEVICES; j++)
1084                SetMaskForEvent(j, 0, EventInfo[i].type);
1085        }
1086	EventInfo[i].mask = 0;
1087	EventInfo[i].type = 0;
1088    }
1089    ExtEventIndex = 0;
1090    DeviceValuator = 0;
1091    DeviceKeyPress = 1;
1092    DeviceKeyRelease = 2;
1093    DeviceButtonPress = 3;
1094    DeviceButtonRelease = 4;
1095    DeviceMotionNotify = 5;
1096    DeviceFocusIn = 6;
1097    DeviceFocusOut = 7;
1098    ProximityIn = 8;
1099    ProximityOut = 9;
1100    DeviceStateNotify = 10;
1101    DeviceMappingNotify = 11;
1102    ChangeDeviceNotify = 12;
1103    DeviceKeyStateNotify = 13;
1104    DeviceButtonStateNotify = 13;
1105    DevicePresenceNotify = 14;
1106    DevicePropertyNotify = 15;
1107
1108    BadDevice = 0;
1109    BadEvent = 1;
1110    BadMode = 2;
1111    DeviceBusy = 3;
1112    BadClass = 4;
1113
1114}
1115
1116/***********************************************************************
1117 *
1118 * IResetProc.
1119 * Remove reply-swapping routine.
1120 * Remove event-swapping routine.
1121 *
1122 */
1123
1124static void
1125IResetProc(ExtensionEntry * unused)
1126{
1127    ReplySwapVector[IReqCode] = ReplyNotSwappd;
1128    EventSwapVector[DeviceValuator] = NotImplemented;
1129    EventSwapVector[DeviceKeyPress] = NotImplemented;
1130    EventSwapVector[DeviceKeyRelease] = NotImplemented;
1131    EventSwapVector[DeviceButtonPress] = NotImplemented;
1132    EventSwapVector[DeviceButtonRelease] = NotImplemented;
1133    EventSwapVector[DeviceMotionNotify] = NotImplemented;
1134    EventSwapVector[DeviceFocusIn] = NotImplemented;
1135    EventSwapVector[DeviceFocusOut] = NotImplemented;
1136    EventSwapVector[ProximityIn] = NotImplemented;
1137    EventSwapVector[ProximityOut] = NotImplemented;
1138    EventSwapVector[DeviceStateNotify] = NotImplemented;
1139    EventSwapVector[DeviceKeyStateNotify] = NotImplemented;
1140    EventSwapVector[DeviceButtonStateNotify] = NotImplemented;
1141    EventSwapVector[DeviceMappingNotify] = NotImplemented;
1142    EventSwapVector[ChangeDeviceNotify] = NotImplemented;
1143    EventSwapVector[DevicePresenceNotify] = NotImplemented;
1144    EventSwapVector[DevicePropertyNotify] = NotImplemented;
1145    RestoreExtensionEvents();
1146}
1147
1148
1149/***********************************************************************
1150 *
1151 * Assign an id and type to an input device.
1152 *
1153 */
1154
1155void
1156AssignTypeAndName(DeviceIntPtr dev, Atom type, char *name)
1157{
1158    dev->xinput_type = type;
1159    dev->name = strdup(name);
1160}
1161
1162/***********************************************************************
1163 *
1164 * Make device type atoms.
1165 *
1166 */
1167
1168static void
1169MakeDeviceTypeAtoms(void)
1170{
1171    int i;
1172
1173    for (i = 0; i < NUMTYPES; i++)
1174	dev_type[i].type =
1175	    MakeAtom(dev_type[i].name, strlen(dev_type[i].name), 1);
1176}
1177
1178/*****************************************************************************
1179 *
1180 *	SEventIDispatch
1181 *
1182 *	Swap any events defined in this extension.
1183 */
1184#define DO_SWAP(func,type) func ((type *)from, (type *)to)
1185
1186static void
1187SEventIDispatch(xEvent * from, xEvent * to)
1188{
1189    int type = from->u.u.type & 0177;
1190
1191    if (type == DeviceValuator)
1192	DO_SWAP(SEventDeviceValuator, deviceValuator);
1193    else if (type == DeviceKeyPress) {
1194	SKeyButtonPtrEvent(from, to);
1195	to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
1196    } else if (type == DeviceKeyRelease) {
1197	SKeyButtonPtrEvent(from, to);
1198	to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
1199    } else if (type == DeviceButtonPress) {
1200	SKeyButtonPtrEvent(from, to);
1201	to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
1202    } else if (type == DeviceButtonRelease) {
1203	SKeyButtonPtrEvent(from, to);
1204	to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
1205    } else if (type == DeviceMotionNotify) {
1206	SKeyButtonPtrEvent(from, to);
1207	to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
1208    } else if (type == DeviceFocusIn)
1209	DO_SWAP(SEventFocus, deviceFocus);
1210    else if (type == DeviceFocusOut)
1211	DO_SWAP(SEventFocus, deviceFocus);
1212    else if (type == ProximityIn) {
1213	SKeyButtonPtrEvent(from, to);
1214	to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
1215    } else if (type == ProximityOut) {
1216	SKeyButtonPtrEvent(from, to);
1217	to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
1218    } else if (type == DeviceStateNotify)
1219	DO_SWAP(SDeviceStateNotifyEvent, deviceStateNotify);
1220    else if (type == DeviceKeyStateNotify)
1221	DO_SWAP(SDeviceKeyStateNotifyEvent, deviceKeyStateNotify);
1222    else if (type == DeviceButtonStateNotify)
1223	DO_SWAP(SDeviceButtonStateNotifyEvent, deviceButtonStateNotify);
1224    else if (type == DeviceMappingNotify)
1225	DO_SWAP(SDeviceMappingNotifyEvent, deviceMappingNotify);
1226    else if (type == ChangeDeviceNotify)
1227	DO_SWAP(SChangeDeviceNotifyEvent, changeDeviceNotify);
1228    else if (type == DevicePresenceNotify)
1229	DO_SWAP(SDevicePresenceNotifyEvent, devicePresenceNotify);
1230    else if (type == DevicePropertyNotify)
1231	DO_SWAP(SDevicePropertyNotifyEvent, devicePropertyNotify);
1232    else {
1233	FatalError("XInputExtension: Impossible event!\n");
1234    }
1235}
1236
1237/**********************************************************************
1238 *
1239 * IExtensionInit - initialize the input extension.
1240 *
1241 * Called from InitExtensions in main() or from QueryExtension() if the
1242 * extension is dynamically loaded.
1243 *
1244 * This extension has several events and errors.
1245 *
1246 * XI is mandatory nowadays, so if we fail to init XI, we die.
1247 */
1248
1249void
1250XInputExtensionInit(void)
1251{
1252    ExtensionEntry *extEntry;
1253    XExtensionVersion thisversion = { XI_Present,
1254        SERVER_XI_MAJOR_VERSION,
1255        SERVER_XI_MINOR_VERSION,
1256    };
1257
1258    if (!dixRegisterPrivateKey(&XIClientPrivateKeyRec, PRIVATE_CLIENT, sizeof(XIClientRec)))
1259        FatalError("Cannot request private for XI.\n");
1260
1261    if (!AddCallback(&ClientStateCallback, XIClientCallback, 0))
1262        FatalError("Failed to add callback to XI.\n");
1263
1264    extEntry = AddExtension(INAME, IEVENTS, IERRORS, ProcIDispatch,
1265			    SProcIDispatch, IResetProc, StandardMinorOpcode);
1266    if (extEntry) {
1267	IReqCode = extEntry->base;
1268	IEventBase = extEntry->eventBase;
1269	XIVersion = thisversion;
1270	MakeDeviceTypeAtoms();
1271	RT_INPUTCLIENT = CreateNewResourceType((DeleteType) InputClientGone,
1272					       "INPUTCLIENT");
1273	if (!RT_INPUTCLIENT)
1274	    FatalError("Failed to add resource type for XI.\n");
1275	FixExtensionEvents(extEntry);
1276	ReplySwapVector[IReqCode] = (ReplySwapPtr) SReplyIDispatch;
1277	EventSwapVector[DeviceValuator] = SEventIDispatch;
1278	EventSwapVector[DeviceKeyPress] = SEventIDispatch;
1279	EventSwapVector[DeviceKeyRelease] = SEventIDispatch;
1280	EventSwapVector[DeviceButtonPress] = SEventIDispatch;
1281	EventSwapVector[DeviceButtonRelease] = SEventIDispatch;
1282	EventSwapVector[DeviceMotionNotify] = SEventIDispatch;
1283	EventSwapVector[DeviceFocusIn] = SEventIDispatch;
1284	EventSwapVector[DeviceFocusOut] = SEventIDispatch;
1285	EventSwapVector[ProximityIn] = SEventIDispatch;
1286	EventSwapVector[ProximityOut] = SEventIDispatch;
1287	EventSwapVector[DeviceStateNotify] = SEventIDispatch;
1288	EventSwapVector[DeviceKeyStateNotify] = SEventIDispatch;
1289	EventSwapVector[DeviceButtonStateNotify] = SEventIDispatch;
1290	EventSwapVector[DeviceMappingNotify] = SEventIDispatch;
1291	EventSwapVector[ChangeDeviceNotify] = SEventIDispatch;
1292	EventSwapVector[DevicePresenceNotify] = SEventIDispatch;
1293
1294	GERegisterExtension(IReqCode, XI2EventSwap);
1295
1296
1297	memset(&xi_all_devices, 0, sizeof(xi_all_devices));
1298	memset(&xi_all_master_devices, 0, sizeof(xi_all_master_devices));
1299	xi_all_devices.id = XIAllDevices;
1300	xi_all_devices.name = "XIAllDevices";
1301	xi_all_master_devices.id = XIAllMasterDevices;
1302	xi_all_master_devices.name = "XIAllMasterDevices";
1303
1304	inputInfo.all_devices = &xi_all_devices;
1305	inputInfo.all_master_devices = &xi_all_master_devices;
1306
1307	XIResetProperties();
1308    } else {
1309	FatalError("IExtensionInit: AddExtensions failed\n");
1310    }
1311}
1312
1313