aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/Xi/extinit.c
blob: 619d0e4689b134be4b9036a7be53dd07f1168410 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
/************************************************************

Copyright 1989, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.

			All Rights Reserved

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 Hewlett-Packard not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
HEWLETT-PACKARD 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.

********************************************************/

/********************************************************************
 *
 *  Dispatch routines and initialization routines for the X input extension.
 *
 */
#define	 NUMTYPES 15

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include "inputstr.h"
#include "gcstruct.h"           /* pointer for extnsionst.h */
#include "extnsionst.h"         /* extension entry   */
#include <X11/extensions/XI.h>
#include <X11/extensions/XIproto.h>
#include <X11/extensions/XI2proto.h>
#include <X11/extensions/geproto.h>
#include "geext.h"              /* extension interfaces for ge */

#include "dixevents.h"
#include "exevents.h"
#include "extinit.h"
#include "exglobals.h"
#include "swaprep.h"
#include "privates.h"
#include "protocol-versions.h"

/* modules local to Xi */
#include "allowev.h"
#include "chgdctl.h"
#include "chgfctl.h"
#include "chgkbd.h"
#include "chgprop.h"
#include "chgptr.h"
#include "closedev.h"
#include "devbell.h"
#include "getbmap.h"
#include "getdctl.h"
#include "getfctl.h"
#include "getfocus.h"
#include "getkmap.h"
#include "getmmap.h"
#include "getprop.h"
#include "getselev.h"
#include "getvers.h"
#include "grabdev.h"
#include "grabdevb.h"
#include "grabdevk.h"
#include "gtmotion.h"
#include "listdev.h"
#include "opendev.h"
#include "queryst.h"
#include "selectev.h"
#include "sendexev.h"
#include "chgkmap.h"
#include "setbmap.h"
#include "setdval.h"
#include "setfocus.h"
#include "setmmap.h"
#include "setmode.h"
#include "ungrdev.h"
#include "ungrdevb.h"
#include "ungrdevk.h"
#include "xiallowev.h"
#include "xiselectev.h"
#include "xigrabdev.h"
#include "xipassivegrab.h"
#include "xisetdevfocus.h"
#include "xiproperty.h"
#include "xichangecursor.h"
#include "xichangehierarchy.h"
#include "xigetclientpointer.h"
#include "xiquerydevice.h"
#include "xiquerypointer.h"
#include "xiqueryversion.h"
#include "xisetclientpointer.h"
#include "xiwarppointer.h"
#include "xibarriers.h"

/* Masks for XI events have to be aligned with core event (partially anyway).
 * If DeviceButtonMotionMask is != ButtonMotionMask, event delivery
 * breaks down. The device needs the dev->button->motionMask. If DBMM is
 * the same as BMM, we can ensure that both core and device events can be
 * delivered, without the need for extra structures in the DeviceIntRec. */
const Mask DeviceKeyPressMask = KeyPressMask;
const Mask DeviceKeyReleaseMask = KeyReleaseMask;
const Mask DeviceButtonPressMask = ButtonPressMask;
const Mask DeviceButtonReleaseMask = ButtonReleaseMask;
const Mask DeviceProximityMask = (1L << 4);
const Mask DeviceStateNotifyMask = (1L << 5);
const Mask DevicePointerMotionMask = PointerMotionMask;
const Mask DevicePointerMotionHintMask = PointerMotionHintMask;
const Mask DeviceButton1MotionMask = Button1MotionMask;
const Mask DeviceButton2MotionMask = Button2MotionMask;
const Mask DeviceButton3MotionMask = Button3MotionMask;
const Mask DeviceButton4MotionMask = Button4MotionMask;
const Mask DeviceButton5MotionMask = Button5MotionMask;
const Mask DeviceButtonMotionMask = ButtonMotionMask;
const Mask DeviceFocusChangeMask = (1L << 14);
const Mask DeviceMappingNotifyMask = (1L << 15);
const Mask ChangeDeviceNotifyMask = (1L << 16);
const Mask DeviceButtonGrabMask = (1L << 17);
const Mask DeviceOwnerGrabButtonMask = (1L << 17);
const Mask DevicePresenceNotifyMask = (1L << 18);
const Mask DevicePropertyNotifyMask = (1L << 19);
const Mask XIAllMasks = (1L << 20) - 1;

int ExtEventIndex;
Mask ExtExclusiveMasks[EMASKSIZE];

static struct dev_type {
    Atom type;
    const char *name;
} dev_type[] = {
    {0, XI_KEYBOARD},
    {0, XI_MOUSE},
    {0, XI_TABLET},
    {0, XI_TOUCHSCREEN},
    {0, XI_TOUCHPAD},
    {0, XI_BARCODE},
    {0, XI_BUTTONBOX},
    {0, XI_KNOB_BOX},
    {0, XI_ONE_KNOB},
    {0, XI_NINE_KNOB},
    {0, XI_TRACKBALL},
    {0, XI_QUADRATURE},
    {0, XI_ID_MODULE},
    {0, XI_SPACEBALL},
    {0, XI_DATAGLOVE},
    {0, XI_EYETRACKER},
    {0, XI_CURSORKEYS},
    {0, XI_FOOTMOUSE}
};

CARD8 event_base[numInputClasses];
XExtEventInfo EventInfo[32];

static DeviceIntRec xi_all_devices;
static DeviceIntRec xi_all_master_devices;

/**
 * Dispatch vector. Functions defined in here will be called when the matching
 * request arrives.
 */
static int (*ProcIVector[]) (ClientPtr) = {
    NULL,                       /*  0 */
        ProcXGetExtensionVersion,       /*  1 */
        ProcXListInputDevices,  /*  2 */
        ProcXOpenDevice,        /*  3 */
        ProcXCloseDevice,       /*  4 */
        ProcXSetDeviceMode,     /*  5 */
        ProcXSelectExtensionEvent,      /*  6 */
        ProcXGetSelectedExtensionEvents,        /*  7 */
        ProcXChangeDeviceDontPropagateList,     /*  8 */
        ProcXGetDeviceDontPropagateList,        /*  9 */
        ProcXGetDeviceMotionEvents,     /* 10 */
        ProcXChangeKeyboardDevice,      /* 11 */
        ProcXChangePointerDevice,       /* 12 */
        ProcXGrabDevice,        /* 13 */
        ProcXUngrabDevice,      /* 14 */
        ProcXGrabDeviceKey,     /* 15 */
        ProcXUngrabDeviceKey,   /* 16 */
        ProcXGrabDeviceButton,  /* 17 */
        ProcXUngrabDeviceButton,        /* 18 */
        ProcXAllowDeviceEvents, /* 19 */
        ProcXGetDeviceFocus,    /* 20 */
        ProcXSetDeviceFocus,    /* 21 */
        ProcXGetFeedbackControl,        /* 22 */
        ProcXChangeFeedbackControl,     /* 23 */
        ProcXGetDeviceKeyMapping,       /* 24 */
        ProcXChangeDeviceKeyMapping,    /* 25 */
        ProcXGetDeviceModifierMapping,  /* 26 */
        ProcXSetDeviceModifierMapping,  /* 27 */
        ProcXGetDeviceButtonMapping,    /* 28 */
        ProcXSetDeviceButtonMapping,    /* 29 */
        ProcXQueryDeviceState,  /* 30 */
        ProcXSendExtensionEvent,        /* 31 */
        ProcXDeviceBell,        /* 32 */
        ProcXSetDeviceValuators,        /* 33 */
        ProcXGetDeviceControl,  /* 34 */
        ProcXChangeDeviceControl,       /* 35 */
        /* XI 1.5 */
        ProcXListDeviceProperties,      /* 36 */
        ProcXChangeDeviceProperty,      /* 37 */
        ProcXDeleteDeviceProperty,      /* 38 */
        ProcXGetDeviceProperty, /* 39 */
        /* XI 2 */
        ProcXIQueryPointer,     /* 40 */
        ProcXIWarpPointer,      /* 41 */
        ProcXIChangeCursor,     /* 42 */
        ProcXIChangeHierarchy,  /* 43 */
        ProcXISetClientPointer, /* 44 */
        ProcXIGetClientPointer, /* 45 */
        ProcXISelectEvents,     /* 46 */
        ProcXIQueryVersion,     /* 47 */
        ProcXIQueryDevice,      /* 48 */
        ProcXISetFocus,         /* 49 */
        ProcXIGetFocus,         /* 50 */
        ProcXIGrabDevice,       /* 51 */
        ProcXIUngrabDevice,     /* 52 */
        ProcXIAllowEvents,      /* 53 */
        ProcXIPassiveGrabDevice,        /* 54 */
        ProcXIPassiveUngrabDevice,      /* 55 */
        ProcXIListProperties,   /* 56 */
        ProcXIChangeProperty,   /* 57 */
        ProcXIDeleteProperty,   /* 58 */
        ProcXIGetProperty,      /* 59 */
        ProcXIGetSelectedEvents, /* 60 */
        ProcXIBarrierReleasePointer /* 61 */
};

/* For swapped clients */
static int (*SProcIVector[]) (ClientPtr) = {
    NULL,                       /*  0 */
        SProcXGetExtensionVersion,      /*  1 */
        SProcXListInputDevices, /*  2 */
        SProcXOpenDevice,       /*  3 */
        SProcXCloseDevice,      /*  4 */
        SProcXSetDeviceMode,    /*  5 */
        SProcXSelectExtensionEvent,     /*  6 */
        SProcXGetSelectedExtensionEvents,       /*  7 */
        SProcXChangeDeviceDontPropagateList,    /*  8 */
        SProcXGetDeviceDontPropagateList,       /*  9 */
        SProcXGetDeviceMotionEvents,    /* 10 */
        SProcXChangeKeyboardDevice,     /* 11 */
        SProcXChangePointerDevice,      /* 12 */
        SProcXGrabDevice,       /* 13 */
        SProcXUngrabDevice,     /* 14 */
        SProcXGrabDeviceKey,    /* 15 */
        SProcXUngrabDeviceKey,  /* 16 */
        SProcXGrabDeviceButton, /* 17 */
        SProcXUngrabDeviceButton,       /* 18 */
        SProcXAllowDeviceEvents,        /* 19 */
        SProcXGetDeviceFocus,   /* 20 */
        SProcXSetDeviceFocus,   /* 21 */
        SProcXGetFeedbackControl,       /* 22 */
        SProcXChangeFeedbackControl,    /* 23 */
        SProcXGetDeviceKeyMapping,      /* 24 */
        SProcXChangeDeviceKeyMapping,   /* 25 */
        SProcXGetDeviceModifierMapping, /* 26 */
        SProcXSetDeviceModifierMapping, /* 27 */
        SProcXGetDeviceButtonMapping,   /* 28 */
        SProcXSetDeviceButtonMapping,   /* 29 */
        SProcXQueryDeviceState, /* 30 */
        SProcXSendExtensionEvent,       /* 31 */
        SProcXDeviceBell,       /* 32 */
        SProcXSetDeviceValuators,       /* 33 */
        SProcXGetDeviceControl, /* 34 */
        SProcXChangeDeviceControl,      /* 35 */
        SProcXListDeviceProperties,     /* 36 */
        SProcXChangeDeviceProperty,     /* 37 */
        SProcXDeleteDeviceProperty,     /* 38 */
        SProcXGetDeviceProperty,        /* 39 */
        SProcXIQueryPointer,    /* 40 */
        SProcXIWarpPointer,     /* 41 */
        SProcXIChangeCursor,    /* 42 */
        SProcXIChangeHierarchy, /* 43 */
        SProcXISetClientPointer,        /* 44 */
        SProcXIGetClientPointer,        /* 45 */
        SProcXISelectEvents,    /* 46 */
        SProcXIQueryVersion,    /* 47 */
        SProcXIQueryDevice,     /* 48 */
        SProcXISetFocus,        /* 49 */
        SProcXIGetFocus,        /* 50 */
        SProcXIGrabDevice,      /* 51 */
        SProcXIUngrabDevice,    /* 52 */
        SProcXIAllowEvents,     /* 53 */
        SProcXIPassiveGrabDevice,       /* 54 */
        SProcXIPassiveUngrabDevice,     /* 55 */
        SProcXIListProperties,  /* 56 */
        SProcXIChangeProperty,  /* 57 */
        SProcXIDeleteProperty,  /* 58 */
        SProcXIGetProperty,     /* 59 */
        SProcXIGetSelectedEvents,       /* 60 */
        SProcXIBarrierReleasePointer /* 61 */
};

/*****************************************************************
 *
 * Globals referenced elsewhere in the server.
 *
 */

int IReqCode = 0;
int IEventBase = 0;
int BadDevice = 0;
static int BadEvent = 1;
int BadMode = 2;
int DeviceBusy = 3;
int BadClass = 4;

int DeviceValuator;
int DeviceKeyPress;
int DeviceKeyRelease;
int DeviceButtonPress;
int DeviceButtonRelease;
int DeviceMotionNotify;
int DeviceFocusIn;
int DeviceFocusOut;
int ProximityIn;
int ProximityOut;
int DeviceStateNotify;
int DeviceKeyStateNotify;
int DeviceButtonStateNotify;
int DeviceMappingNotify;
int ChangeDeviceNotify;
int DevicePresenceNotify;
int DevicePropertyNotify;

RESTYPE RT_INPUTCLIENT;

/*****************************************************************
 *
 * Externs defined elsewhere in the X server.
 *
 */

extern XExtensionVersion XIVersion;

Mask PropagateMask[EMASKSIZE];

/*****************************************************************
 *
 * Versioning support
 *
 */

DevPrivateKeyRec XIClientPrivateKeyRec;

/*****************************************************************
 *
 * Declarations of local routines.
 *
 */

static void
XIClientCallback(CallbackListPtr *list, pointer closure, pointer data)
{
    NewClientInfoRec *clientinfo = (NewClientInfoRec *) data;
    ClientPtr pClient = clientinfo->client;
    XIClientPtr pXIClient;

    pXIClient = dixLookupPrivate(&pClient->devPrivates, XIClientPrivateKey);
    pXIClient->major_version = 0;
    pXIClient->minor_version = 0;
}

/*************************************************************************
 *
 * ProcIDispatch - main dispatch routine for requests to this extension.
 * This routine is used if server and client have the same byte ordering.
 *
 */

static int
ProcIDispatch(ClientPtr client)
{
    REQUEST(xReq);
    if (stuff->data >= ARRAY_SIZE(ProcIVector) || !ProcIVector[stuff->data])
        return BadRequest;

    return (*ProcIVector[stuff->data]) (client);
}

/*******************************************************************************
 *
 * SProcXDispatch
 *
 * Main swapped dispatch routine for requests to this extension.
 * This routine is used if server and client do not have the same byte ordering.
 *
 */

static int
SProcIDispatch(ClientPtr client)
{
    REQUEST(xReq);
    if (stuff->data >= ARRAY_SIZE(SProcIVector) || !SProcIVector[stuff->data])
        return BadRequest;

    return (*SProcIVector[stuff->data]) (client);
}

/**********************************************************************
 *
 * SReplyIDispatch
 * Swap any replies defined in this extension.
 *
 */

static void
SReplyIDispatch(ClientPtr client, int len, xGrabDeviceReply * rep)
{
    /* All we look at is the type field */
    /* This is common to all replies    */
    if (rep->RepType == X_GetExtensionVersion)
        SRepXGetExtensionVersion(client, len,
                                 (xGetExtensionVersionReply *) rep);
    else if (rep->RepType == X_ListInputDevices)
        SRepXListInputDevices(client, len, (xListInputDevicesReply *) rep);
    else if (rep->RepType == X_OpenDevice)
        SRepXOpenDevice(client, len, (xOpenDeviceReply *) rep);
    else if (rep->RepType == X_SetDeviceMode)
        SRepXSetDeviceMode(client, len, (xSetDeviceModeReply *) rep);
    else if (rep->RepType == X_GetSelectedExtensionEvents)
        SRepXGetSelectedExtensionEvents(client, len,
                                        (xGetSelectedExtensionEventsReply *)
                                        rep);
    else if (rep->RepType == X_GetDeviceDontPropagateList)
        SRepXGetDeviceDontPropagateList(client, len,
                                        (xGetDeviceDontPropagateListReply *)
                                        rep);
    else if (rep->RepType == X_GetDeviceMotionEvents)
        SRepXGetDeviceMotionEvents(client, len,
                                   (xGetDeviceMotionEventsReply *) rep);
    else if (rep->RepType == X_GrabDevice)
        SRepXGrabDevice(client, len, (xGrabDeviceReply *) rep);
    else if (rep->RepType == X_GetDeviceFocus)
        SRepXGetDeviceFocus(client, len, (xGetDeviceFocusReply *) rep);
    else if (rep->RepType == X_GetFeedbackControl)
        SRepXGetFeedbackControl(client, len, (xGetFeedbackControlReply *) rep);
    else if (rep->RepType == X_GetDeviceKeyMapping)
        SRepXGetDeviceKeyMapping(client, len,
                                 (xGetDeviceKeyMappingReply *) rep);
    else if (rep->RepType == X_GetDeviceModifierMapping)
        SRepXGetDeviceModifierMapping(client, len,
                                      (xGetDeviceModifierMappingReply *) rep);
    else if (rep->RepType == X_SetDeviceModifierMapping)
        SRepXSetDeviceModifierMapping(client, len,
                                      (xSetDeviceModifierMappingReply *) rep);
    else if (rep->RepType == X_GetDeviceButtonMapping)
        SRepXGetDeviceButtonMapping(client, len,
                                    (xGetDeviceButtonMappingReply *) rep);
    else if (rep->RepType == X_SetDeviceButtonMapping)
        SRepXSetDeviceButtonMapping(client, len,
                                    (xSetDeviceButtonMappingReply *) rep);
    else if (rep->RepType == X_QueryDeviceState)
        SRepXQueryDeviceState(client, len, (xQueryDeviceStateReply *) rep);
    else if (rep->RepType == X_SetDeviceValuators)
        SRepXSetDeviceValuators(client, len, (xSetDeviceValuatorsReply *) rep);
    else if (rep->RepType == X_GetDeviceControl)
        SRepXGetDeviceControl(client, len, (xGetDeviceControlReply *) rep);
    else if (rep->RepType == X_ChangeDeviceControl)
        SRepXChangeDeviceControl(client, len,
                                 (xChangeDeviceControlReply *) rep);
    else if (rep->RepType == X_ListDeviceProperties)
        SRepXListDeviceProperties(client, len,
                                  (xListDevicePropertiesReply *) rep);
    else if (rep->RepType == X_GetDeviceProperty)
        SRepXGetDeviceProperty(client, len, (xGetDevicePropertyReply *) rep);
    else if (rep->RepType == X_XIQueryPointer)
        SRepXIQueryPointer(client, len, (xXIQueryPointerReply *) rep);
    else if (rep->RepType == X_XIGetClientPointer)
        SRepXIGetClientPointer(client, len, (xXIGetClientPointerReply *) rep);
    else if (rep->RepType == X_XIQueryVersion)
        SRepXIQueryVersion(client, len, (xXIQueryVersionReply *) rep);
    else if (rep->RepType == X_XIQueryDevice)
        SRepXIQueryDevice(client, len, (xXIQueryDeviceReply *) rep);
    else if (rep->RepType == X_XIGrabDevice)
        SRepXIGrabDevice(client, len, (xXIGrabDeviceReply *) rep);
    else if (rep->RepType == X_XIPassiveGrabDevice)
        SRepXIPassiveGrabDevice(client, len, (xXIPassiveGrabDeviceReply *) rep);
    else if (rep->RepType == X_XIListProperties)
        SRepXIListProperties(client, len, (xXIListPropertiesReply *) rep);
    else if (rep->RepType == X_XIGetProperty)
        SRepXIGetProperty(client, len, (xXIGetPropertyReply *) rep);
    else if (rep->RepType == X_XIGetSelectedEvents)
        SRepXIGetSelectedEvents(client, len, (xXIGetSelectedEventsReply *) rep);
    else if (rep->RepType == X_XIGetFocus)
        SRepXIGetFocus(client, len, (xXIGetFocusReply *) rep);
    else {
        FatalError("XINPUT confused sending swapped reply");
    }
}

/************************************************************************
 *
 * This function swaps the DeviceValuator event.
 *
 */

static void
SEventDeviceValuator(deviceValuator * from, deviceValuator * to)
{
    int i;
    INT32 *ip B32;

    *to = *from;
    swaps(&to->sequenceNumber);
    swaps(&to->device_state);
    ip = &to->valuator0;
    for (i = 0; i < 6; i++) {
        swapl(ip + i);
    }
}

static void
SEventFocus(deviceFocus * from, deviceFocus * to)
{
    *to = *from;
    swaps(&to->sequenceNumber);
    swapl(&to->time);
    swapl(&to->window);
}

static void
SDeviceStateNotifyEvent(deviceStateNotify * from, deviceStateNotify * to)
{
    int i;
    INT32 *ip B32;

    *to = *from;
    swaps(&to->sequenceNumber);
    swapl(&to->time);
    ip = &to->valuator0;
    for (i = 0; i < 3; i++) {
        swapl(ip + i);
    }
}

static void
SDeviceKeyStateNotifyEvent(deviceKeyStateNotify * from,
                           deviceKeyStateNotify * to)
{
    *to = *from;
    swaps(&to->sequenceNumber);
}

static void
SDeviceButtonStateNotifyEvent(deviceButtonStateNotify * from,
                              deviceButtonStateNotify * to)
{
    *to = *from;
    swaps(&to->sequenceNumber);
}

static void
SChangeDeviceNotifyEvent(changeDeviceNotify * from, changeDeviceNotify * to)
{
    *to = *from;
    swaps(&to->sequenceNumber);
    swapl(&to->time);
}

static void
SDeviceMappingNotifyEvent(deviceMappingNotify * from, deviceMappingNotify * to)
{
    *to = *from;
    swaps(&to->sequenceNumber);
    swapl(&to->time);
}

static void
SDevicePresenceNotifyEvent(devicePresenceNotify * from,
                           devicePresenceNotify * to)
{
    *to = *from;
    swaps(&to->sequenceNumber);
    swapl(&to->time);
    swaps(&to->control);
}

static void
SDevicePropertyNotifyEvent(devicePropertyNotify * from,
                           devicePropertyNotify * to)
{
    *to = *from;
    swaps(&to->sequenceNumber);
    swapl(&to->time);
    swapl(&to->atom);
}

static void
SDeviceLeaveNotifyEvent(xXILeaveEvent * from, xXILeaveEvent * to)
{
    *to = *from;
    swaps(&to->sequenceNumber);
    swapl(&to->length);
    swaps(&to->evtype);
    swaps(&to->deviceid);
    swapl(&to->time);
    swapl(&to->root);
    swapl(&to->event);
    swapl(&to->child);
    swapl(&to->root_x);
    swapl(&to->root_y);
    swapl(&to->event_x);
    swapl(&to->event_y);
    swaps(&to->sourceid);
    swaps(&to->buttons_len);
    swapl(&to->mods.base_mods);
    swapl(&to->mods.latched_mods);
    swapl(&to->mods.locked_mods);
}

static void
SDeviceChangedEvent(xXIDeviceChangedEvent * from, xXIDeviceChangedEvent * to)
{
    int i, j;
    xXIAnyInfo *any;

    *to = *from;
    memcpy(&to[1], &from[1], from->length * 4);

    any = (xXIAnyInfo *) &to[1];
    for (i = 0; i < to->num_classes; i++) {
        int length = any->length;

        switch (any->type) {
        case KeyClass:
        {
            xXIKeyInfo *ki = (xXIKeyInfo *) any;
            uint32_t *key = (uint32_t *) &ki[1];

            for (j = 0; j < ki->num_keycodes; j++, key++)
                swapl(key);
            swaps(&ki->num_keycodes);
        }
            break;
        case ButtonClass:
        {
            xXIButtonInfo *bi = (xXIButtonInfo *) any;
            Atom *labels = (Atom *) ((char *) bi + sizeof(xXIButtonInfo) +
                                     pad_to_int32(bits_to_bytes
                                                  (bi->num_buttons)));
            for (j = 0; j < bi->num_buttons; j++)
                swapl(&labels[j]);
            swaps(&bi->num_buttons);
        }
            break;
        case ValuatorClass:
        {
            xXIValuatorInfo *ai = (xXIValuatorInfo *) any;

            swapl(&ai->label);
            swapl(&ai->min.integral);
            swapl(&ai->min.frac);
            swapl(&ai->max.integral);
            swapl(&ai->max.frac);
            swapl(&ai->resolution);
            swaps(&ai->number);
        }
            break;
        }

        swaps(&any->type);
        swaps(&any->length);
        swaps(&any->sourceid);

        any = (xXIAnyInfo *) ((char *) any + length * 4);
    }

    swaps(&to->sequenceNumber);
    swapl(&to->length);
    swaps(&to->evtype);
    swaps(&to->deviceid);
    swapl(&to->time);
    swaps(&to->num_classes);
    swaps(&to->sourceid);

}

static void
SDeviceEvent(xXIDeviceEvent * from, xXIDeviceEvent * to)
{
    int i;
    char *ptr;
    char *vmask;

    memcpy(to, from, sizeof(xEvent) + from->length * 4);

    swaps(&to->sequenceNumber);
    swapl(&to->length);
    swaps(&to->evtype);
    swaps(&to->deviceid);
    swapl(&to->time);
    swapl(&to->detail);
    swapl(&to->root);
    swapl(&to->event);
    swapl(&to->child);
    swapl(&to->root_x);
    swapl(&to->root_y);
    swapl(&to->event_x);
    swapl(&to->event_y);
    swaps(&to->buttons_len);
    swaps(&to->valuators_len);
    swaps(&to->sourceid);
    swapl(&to->mods.base_mods);
    swapl(&to->mods.latched_mods);
    swapl(&to->mods.locked_mods);
    swapl(&to->mods.effective_mods);
    swapl(&to->flags);

    ptr = (char *) (&to[1]);
    ptr += from->buttons_len * 4;
    vmask = ptr;                /* valuator mask */
    ptr += from->valuators_len * 4;
    for (i = 0; i < from->valuators_len * 32; i++) {
        if (BitIsOn(vmask, i)) {
            swapl(((uint32_t *) ptr));
            ptr += 4;
            swapl(((uint32_t *) ptr));
            ptr += 4;
        }
    }
}

static void
SDeviceHierarchyEvent(xXIHierarchyEvent * from, xXIHierarchyEvent * to)
{
    int i;
    xXIHierarchyInfo *info;

    *to = *from;
    memcpy(&to[1], &from[1], from->length * 4);
    swaps(&to->sequenceNumber);
    swapl(&to->length);
    swaps(&to->evtype);
    swaps(&to->deviceid);
    swapl(&to->time);
    swapl(&to->flags);
    swaps(&to->num_info);

    info = (xXIHierarchyInfo *) &to[1];
    for (i = 0; i < from->num_info; i++) {
        swaps(&info->deviceid);
        swaps(&info->attachment);
        info++;
    }
}

static void
SXIPropertyEvent(xXIPropertyEvent * from, xXIPropertyEvent * to)
{
    *to = *from;
    swaps(&to->sequenceNumber);
    swapl(&to->length);
    swaps(&to->evtype);
    swaps(&to->deviceid);
    swapl(&to->property);
}

static void
SRawEvent(xXIRawEvent * from, xXIRawEvent * to)
{
    int i;
    FP3232 *values;
    unsigned char *mask;

    memcpy(to, from, sizeof(xEvent) + from->length * 4);

    swaps(&to->sequenceNumber);
    swapl(&to->length);
    swaps(&to->evtype);
    swaps(&to->deviceid);
    swapl(&to->time);
    swapl(&to->detail);

    mask = (unsigned char *) &to[1];
    values = (FP3232 *) (mask + from->valuators_len * 4);

    for (i = 0; i < from->valuators_len * 4 * 8; i++) {
        if (BitIsOn(mask, i)) {
            /* for each bit set there are two FP3232 values on the wire, in
             * the order abcABC for data and data_raw. Here we swap as if
             * they were in aAbBcC order because it's easier and really
             * doesn't matter.
             */
            swapl(&values->integral);
            swapl(&values->frac);
            values++;
            swapl(&values->integral);
            swapl(&values->frac);
            values++;
        }
    }

    swaps(&to->valuators_len);
}

static void
STouchOwnershipEvent(xXITouchOwnershipEvent * from, xXITouchOwnershipEvent * to)
{
    *to = *from;
    swaps(&to->sequenceNumber);
    swapl(&to->length);
    swaps(&to->evtype);
    swaps(&to->deviceid);
    swapl(&to->time);
    swaps(&to->sourceid);
    swapl(&to->touchid);
    swapl(&to->flags);
    swapl(&to->root);
    swapl(&to->event);
    swapl(&to->child);
}

static void
SBarrierEvent(xXIBarrierEvent * from,
              xXIBarrierEvent * to) {

    *to = *from;

    swaps(&from->sequenceNumber);
    swapl(&from->length);
    swaps(&from->evtype);
    swapl(&from->time);
    swaps(&from->deviceid);
    swaps(&from->sourceid);
    swapl(&from->event);
    swapl(&from->root);
    swapl(&from->root_x);
    swapl(&from->root_y);

    swapl(&from->dx.integral);
    swapl(&from->dx.frac);
    swapl(&from->dy.integral);
    swapl(&from->dy.frac);
    swapl(&from->dtime);
    swapl(&from->barrier);
    swapl(&from->eventid);
}

/** Event swapping function for XI2 events. */
void
XI2EventSwap(xGenericEvent *from, xGenericEvent *to)
{
    switch (from->evtype) {
    case XI_Enter:
    case XI_Leave:
    case XI_FocusIn:
    case XI_FocusOut:
        SDeviceLeaveNotifyEvent((xXILeaveEvent *) from, (xXILeaveEvent *) to);
        break;
    case XI_DeviceChanged:
        SDeviceChangedEvent((xXIDeviceChangedEvent *) from,
                            (xXIDeviceChangedEvent *) to);
        break;
    case XI_HierarchyChanged:
        SDeviceHierarchyEvent((xXIHierarchyEvent *) from,
                              (xXIHierarchyEvent *) to);
        break;
    case XI_PropertyEvent:
        SXIPropertyEvent((xXIPropertyEvent *) from, (xXIPropertyEvent *) to);
        break;
    case XI_Motion:
    case XI_KeyPress:
    case XI_KeyRelease:
    case XI_ButtonPress:
    case XI_ButtonRelease:
    case XI_TouchBegin:
    case XI_TouchUpdate:
    case XI_TouchEnd:
        SDeviceEvent((xXIDeviceEvent *) from, (xXIDeviceEvent *) to);
        break;
    case XI_TouchOwnership:
        STouchOwnershipEvent((xXITouchOwnershipEvent *) from,
                             (xXITouchOwnershipEvent *) to);
        break;
    case XI_RawMotion:
    case XI_RawKeyPress:
    case XI_RawKeyRelease:
    case XI_RawButtonPress:
    case XI_RawButtonRelease:
    case XI_RawTouchBegin:
    case XI_RawTouchUpdate:
    case XI_RawTouchEnd:
        SRawEvent((xXIRawEvent *) from, (xXIRawEvent *) to);
        break;
    case XI_BarrierHit:
    case XI_BarrierLeave:
        SBarrierEvent((xXIBarrierEvent *) from,
                      (xXIBarrierEvent *) to);
        break;
    default:
        ErrorF("[Xi] Unknown event type to swap. This is a bug.\n");
        break;
    }
}

/**************************************************************************
 *
 * Allow the specified event to have its propagation suppressed.
 * The default is to not allow suppression of propagation.
 *
 */

static void
AllowPropagateSuppress(Mask mask)
{
    int i;

    for (i = 0; i < MAXDEVICES; i++)
        PropagateMask[i] |= mask;
}

/**************************************************************************
 *
 * Record an event mask where there is no unique corresponding event type.
 * We can't call SetMaskForEvent, since that would clobber the existing
 * mask for that event.  MotionHint and ButtonMotion are examples.
 *
 * Since extension event types will never be less than 64, we can use
 * 0-63 in the EventInfo array as the "type" to be used to look up this
 * mask.  This means that the corresponding macros such as
 * DevicePointerMotionHint must have access to the same constants.
 *
 */

static void
SetEventInfo(Mask mask, int constant)
{
    EventInfo[ExtEventIndex].mask = mask;
    EventInfo[ExtEventIndex++].type = constant;
}

/**************************************************************************
 *
 * Allow the specified event to be restricted to being selected by one
 * client at a time.
 * The default is to allow more than one client to select the event.
 *
 */

static void
SetExclusiveAccess(Mask mask)
{
    int i;

    for (i = 0; i < MAXDEVICES; i++)
        ExtExclusiveMasks[i] |= mask;
}

/**************************************************************************
 *
 * Assign the specified mask to the specified event.
 *
 */

static void
SetMaskForExtEvent(Mask mask, int event)
{
    int i;

    EventInfo[ExtEventIndex].mask = mask;
    EventInfo[ExtEventIndex++].type = event;

    if ((event < LASTEvent) || (event >= 128))
        FatalError("MaskForExtensionEvent: bogus event number");

    for (i = 0; i < MAXDEVICES; i++)
        SetMaskForEvent(i, mask, event);
}

/************************************************************************
 *
 * This function sets up extension event types and masks.
 *
 */

static void
FixExtensionEvents(ExtensionEntry * extEntry)
{
    DeviceValuator = extEntry->eventBase;
    DeviceKeyPress = DeviceValuator + 1;
    DeviceKeyRelease = DeviceKeyPress + 1;
    DeviceButtonPress = DeviceKeyRelease + 1;
    DeviceButtonRelease = DeviceButtonPress + 1;
    DeviceMotionNotify = DeviceButtonRelease + 1;
    DeviceFocusIn = DeviceMotionNotify + 1;
    DeviceFocusOut = DeviceFocusIn + 1;
    ProximityIn = DeviceFocusOut + 1;
    ProximityOut = ProximityIn + 1;
    DeviceStateNotify = ProximityOut + 1;
    DeviceMappingNotify = DeviceStateNotify + 1;
    ChangeDeviceNotify = DeviceMappingNotify + 1;
    DeviceKeyStateNotify = ChangeDeviceNotify + 1;
    DeviceButtonStateNotify = DeviceKeyStateNotify + 1;
    DevicePresenceNotify = DeviceButtonStateNotify + 1;
    DevicePropertyNotify = DevicePresenceNotify + 1;

    event_base[KeyClass] = DeviceKeyPress;
    event_base[ButtonClass] = DeviceButtonPress;
    event_base[ValuatorClass] = DeviceMotionNotify;
    event_base[ProximityClass] = ProximityIn;
    event_base[FocusClass] = DeviceFocusIn;
    event_base[OtherClass] = DeviceStateNotify;

    BadDevice += extEntry->errorBase;
    BadEvent += extEntry->errorBase;
    BadMode += extEntry->errorBase;
    DeviceBusy += extEntry->errorBase;
    BadClass += extEntry->errorBase;

    SetMaskForExtEvent(DeviceKeyPressMask, DeviceKeyPress);
    AllowPropagateSuppress(DeviceKeyPressMask);
    SetCriticalEvent(DeviceKeyPress);

    SetMaskForExtEvent(DeviceKeyReleaseMask, DeviceKeyRelease);
    AllowPropagateSuppress(DeviceKeyReleaseMask);
    SetCriticalEvent(DeviceKeyRelease);

    SetMaskForExtEvent(DeviceButtonPressMask, DeviceButtonPress);
    AllowPropagateSuppress(DeviceButtonPressMask);
    SetCriticalEvent(DeviceButtonPress);

    SetMaskForExtEvent(DeviceButtonReleaseMask, DeviceButtonRelease);
    AllowPropagateSuppress(DeviceButtonReleaseMask);
    SetCriticalEvent(DeviceButtonRelease);

    SetMaskForExtEvent(DeviceProximityMask, ProximityIn);
    SetMaskForExtEvent(DeviceProximityMask, ProximityOut);

    SetMaskForExtEvent(DeviceStateNotifyMask, DeviceStateNotify);

    SetMaskForExtEvent(DevicePointerMotionMask, DeviceMotionNotify);
    AllowPropagateSuppress(DevicePointerMotionMask);
    SetCriticalEvent(DeviceMotionNotify);

    SetEventInfo(DevicePointerMotionHintMask, _devicePointerMotionHint);
    SetEventInfo(DeviceButton1MotionMask, _deviceButton1Motion);
    SetEventInfo(DeviceButton2MotionMask, _deviceButton2Motion);
    SetEventInfo(DeviceButton3MotionMask, _deviceButton3Motion);
    SetEventInfo(DeviceButton4MotionMask, _deviceButton4Motion);
    SetEventInfo(DeviceButton5MotionMask, _deviceButton5Motion);
    SetEventInfo(DeviceButtonMotionMask, _deviceButtonMotion);

    SetMaskForExtEvent(DeviceFocusChangeMask, DeviceFocusIn);
    SetMaskForExtEvent(DeviceFocusChangeMask, DeviceFocusOut);

    SetMaskForExtEvent(DeviceMappingNotifyMask, DeviceMappingNotify);
    SetMaskForExtEvent(ChangeDeviceNotifyMask, ChangeDeviceNotify);

    SetEventInfo(DeviceButtonGrabMask, _deviceButtonGrab);
    SetExclusiveAccess(DeviceButtonGrabMask);

    SetEventInfo(DeviceOwnerGrabButtonMask, _deviceOwnerGrabButton);
    SetEventInfo(DevicePresenceNotifyMask, _devicePresence);
    SetMaskForExtEvent(DevicePropertyNotifyMask, DevicePropertyNotify);

    SetEventInfo(0, _noExtensionEvent);
}

/************************************************************************
 *
 * This function restores extension event types and masks to their
 * initial state.
 *
 */

static void
RestoreExtensionEvents(void)
{
    int i, j;

    IReqCode = 0;
    IEventBase = 0;

    for (i = 0; i < ExtEventIndex - 1; i++) {
        if ((EventInfo[i].type >= LASTEvent) && (EventInfo[i].type < 128)) {
            for (j = 0; j < MAXDEVICES; j++)
                SetMaskForEvent(j, 0, EventInfo[i].type);
        }
        EventInfo[i].mask = 0;
        EventInfo[i].type = 0;
    }
    ExtEventIndex = 0;
    DeviceValuator = 0;
    DeviceKeyPress = 1;
    DeviceKeyRelease = 2;
    DeviceButtonPress = 3;
    DeviceButtonRelease = 4;
    DeviceMotionNotify = 5;
    DeviceFocusIn = 6;
    DeviceFocusOut = 7;
    ProximityIn = 8;
    ProximityOut = 9;
    DeviceStateNotify = 10;
    DeviceMappingNotify = 11;
    ChangeDeviceNotify = 12;
    DeviceKeyStateNotify = 13;
    DeviceButtonStateNotify = 13;
    DevicePresenceNotify = 14;
    DevicePropertyNotify = 15;

    BadDevice = 0;
    BadEvent = 1;
    BadMode = 2;
    DeviceBusy = 3;
    BadClass = 4;

}

/***********************************************************************
 *
 * IResetProc.
 * Remove reply-swapping routine.
 * Remove event-swapping routine.
 *
 */

static void
IResetProc(ExtensionEntry * unused)
{
    ReplySwapVector[IReqCode] = ReplyNotSwappd;
    EventSwapVector[DeviceValuator] = NotImplemented;
    EventSwapVector[DeviceKeyPress] = NotImplemented;
    EventSwapVector[DeviceKeyRelease] = NotImplemented;
    EventSwapVector[DeviceButtonPress] = NotImplemented;
    EventSwapVector[DeviceButtonRelease] = NotImplemented;
    EventSwapVector[DeviceMotionNotify] = NotImplemented;
    EventSwapVector[DeviceFocusIn] = NotImplemented;
    EventSwapVector[DeviceFocusOut] = NotImplemented;
    EventSwapVector[ProximityIn] = NotImplemented;
    EventSwapVector[ProximityOut] = NotImplemented;
    EventSwapVector[DeviceStateNotify] = NotImplemented;
    EventSwapVector[DeviceKeyStateNotify] = NotImplemented;
    EventSwapVector[DeviceButtonStateNotify] = NotImplemented;
    EventSwapVector[DeviceMappingNotify] = NotImplemented;
    EventSwapVector[ChangeDeviceNotify] = NotImplemented;
    EventSwapVector[DevicePresenceNotify] = NotImplemented;
    EventSwapVector[DevicePropertyNotify] = NotImplemented;
    RestoreExtensionEvents();

    free(xi_all_devices.name);
    free(xi_all_master_devices.name);
}

/***********************************************************************
 *
 * Assign an id and type to an input device.
 *
 */

void
AssignTypeAndName(DeviceIntPtr dev, Atom type, const char *name)
{
    dev->xinput_type = type;
    dev->name = strdup(name);
}

/***********************************************************************
 *
 * Make device type atoms.
 *
 */

static void
MakeDeviceTypeAtoms(void)
{
    int i;

    for (i = 0; i < NUMTYPES; i++)
        dev_type[i].type =
            MakeAtom(dev_type[i].name, strlen(dev_type[i].name), 1);
}

/*****************************************************************************
 *
 *	SEventIDispatch
 *
 *	Swap any events defined in this extension.
 */
#define DO_SWAP(func,type) func ((type *)from, (type *)to)

static void
SEventIDispatch(xEvent *from, xEvent *to)
{
    int type = from->u.u.type & 0177;

    if (type == DeviceValuator)
        DO_SWAP(SEventDeviceValuator, deviceValuator);
    else if (type == DeviceKeyPress) {
        SKeyButtonPtrEvent(from, to);
        to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
    }
    else if (type == DeviceKeyRelease) {
        SKeyButtonPtrEvent(from, to);
        to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
    }
    else if (type == DeviceButtonPress) {
        SKeyButtonPtrEvent(from, to);
        to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
    }
    else if (type == DeviceButtonRelease) {
        SKeyButtonPtrEvent(from, to);
        to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
    }
    else if (type == DeviceMotionNotify) {
        SKeyButtonPtrEvent(from, to);
        to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
    }
    else if (type == DeviceFocusIn)
        DO_SWAP(SEventFocus, deviceFocus);
    else if (type == DeviceFocusOut)
        DO_SWAP(SEventFocus, deviceFocus);
    else if (type == ProximityIn) {
        SKeyButtonPtrEvent(from, to);
        to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
    }
    else if (type == ProximityOut) {
        SKeyButtonPtrEvent(from, to);
        to->u.keyButtonPointer.pad1 = from->u.keyButtonPointer.pad1;
    }
    else if (type == DeviceStateNotify)
        DO_SWAP(SDeviceStateNotifyEvent, deviceStateNotify);
    else if (type == DeviceKeyStateNotify)
        DO_SWAP(SDeviceKeyStateNotifyEvent, deviceKeyStateNotify);
    else if (type == DeviceButtonStateNotify)
        DO_SWAP(SDeviceButtonStateNotifyEvent, deviceButtonStateNotify);
    else if (type == DeviceMappingNotify)
        DO_SWAP(SDeviceMappingNotifyEvent, deviceMappingNotify);
    else if (type == ChangeDeviceNotify)
        DO_SWAP(SChangeDeviceNotifyEvent, changeDeviceNotify);
    else if (type == DevicePresenceNotify)
        DO_SWAP(SDevicePresenceNotifyEvent, devicePresenceNotify);
    else if (type == DevicePropertyNotify)
        DO_SWAP(SDevicePropertyNotifyEvent, devicePropertyNotify);
    else {
        FatalError("XInputExtension: Impossible event!\n");
    }
}

/**********************************************************************
 *
 * IExtensionInit - initialize the input extension.
 *
 * Called from InitExtensions in main() or from QueryExtension() if the
 * extension is dynamically loaded.
 *
 * This extension has several events and errors.
 *
 * XI is mandatory nowadays, so if we fail to init XI, we die.
 */

void
XInputExtensionInit(void)
{
    ExtensionEntry *extEntry;

    XExtensionVersion thisversion = { XI_Present,
        SERVER_XI_MAJOR_VERSION,
        SERVER_XI_MINOR_VERSION,
    };

    if (!dixRegisterPrivateKey
        (&XIClientPrivateKeyRec, PRIVATE_CLIENT, sizeof(XIClientRec)))
        FatalError("Cannot request private for XI.\n");

    if (!AddCallback(&ClientStateCallback, XIClientCallback, 0))
        FatalError("Failed to add callback to XI.\n");

    if (!XIBarrierInit())
        FatalError("Could not initialize barriers.\n");

    extEntry = AddExtension(INAME, IEVENTS, IERRORS, ProcIDispatch,
                            SProcIDispatch, IResetProc, StandardMinorOpcode);
    if (extEntry) {
        IReqCode = extEntry->base;
        IEventBase = extEntry->eventBase;
        XIVersion = thisversion;
        MakeDeviceTypeAtoms();
        RT_INPUTCLIENT = CreateNewResourceType((DeleteType) InputClientGone,
                                               "INPUTCLIENT");
        if (!RT_INPUTCLIENT)
            FatalError("Failed to add resource type for XI.\n");
        FixExtensionEvents(extEntry);
        ReplySwapVector[IReqCode] = (ReplySwapPtr) SReplyIDispatch;
        EventSwapVector[DeviceValuator] = SEventIDispatch;
        EventSwapVector[DeviceKeyPress] = SEventIDispatch;
        EventSwapVector[DeviceKeyRelease] = SEventIDispatch;
        EventSwapVector[DeviceButtonPress] = SEventIDispatch;
        EventSwapVector[DeviceButtonRelease] = SEventIDispatch;
        EventSwapVector[DeviceMotionNotify] = SEventIDispatch;
        EventSwapVector[DeviceFocusIn] = SEventIDispatch;
        EventSwapVector[DeviceFocusOut] = SEventIDispatch;
        EventSwapVector[ProximityIn] = SEventIDispatch;
        EventSwapVector[ProximityOut] = SEventIDispatch;
        EventSwapVector[DeviceStateNotify] = SEventIDispatch;
        EventSwapVector[DeviceKeyStateNotify] = SEventIDispatch;
        EventSwapVector[DeviceButtonStateNotify] = SEventIDispatch;
        EventSwapVector[DeviceMappingNotify] = SEventIDispatch;
        EventSwapVector[ChangeDeviceNotify] = SEventIDispatch;
        EventSwapVector[DevicePresenceNotify] = SEventIDispatch;

        GERegisterExtension(IReqCode, XI2EventSwap);

        memset(&xi_all_devices, 0, sizeof(xi_all_devices));
        memset(&xi_all_master_devices, 0, sizeof(xi_all_master_devices));
        xi_all_devices.id = XIAllDevices;
        xi_all_devices.name = strdup("XIAllDevices");
        xi_all_master_devices.id = XIAllMasterDevices;
        xi_all_master_devices.name = strdup("XIAllMasterDevices");

        inputInfo.all_devices = &xi_all_devices;
        inputInfo.all_master_devices = &xi_all_master_devices;

        XIResetProperties();
    }
    else {
        FatalError("IExtensionInit: AddExtensions failed\n");
    }
}