xref: /OK3568_Linux_fs/external/xserver/dix/eventconvert.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2009 Red Hat, Inc.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun  * Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*4882a593Smuzhiyun  * DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun  * @file eventconvert.c
27*4882a593Smuzhiyun  * This file contains event conversion routines from InternalEvent to the
28*4882a593Smuzhiyun  * matching protocol events.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
32*4882a593Smuzhiyun #include <dix-config.h>
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include <stdint.h>
36*4882a593Smuzhiyun #include <X11/X.h>
37*4882a593Smuzhiyun #include <X11/extensions/XIproto.h>
38*4882a593Smuzhiyun #include <X11/extensions/XI2proto.h>
39*4882a593Smuzhiyun #include <X11/extensions/XI.h>
40*4882a593Smuzhiyun #include <X11/extensions/XI2.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include "dix.h"
43*4882a593Smuzhiyun #include "inputstr.h"
44*4882a593Smuzhiyun #include "misc.h"
45*4882a593Smuzhiyun #include "eventstr.h"
46*4882a593Smuzhiyun #include "exevents.h"
47*4882a593Smuzhiyun #include "exglobals.h"
48*4882a593Smuzhiyun #include "eventconvert.h"
49*4882a593Smuzhiyun #include "inpututils.h"
50*4882a593Smuzhiyun #include "xiquerydevice.h"
51*4882a593Smuzhiyun #include "xkbsrv.h"
52*4882a593Smuzhiyun #include "inpututils.h"
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun static int countValuators(DeviceEvent *ev, int *first);
55*4882a593Smuzhiyun static int getValuatorEvents(DeviceEvent *ev, deviceValuator * xv);
56*4882a593Smuzhiyun static int eventToKeyButtonPointer(DeviceEvent *ev, xEvent **xi, int *count);
57*4882a593Smuzhiyun static int eventToDeviceChanged(DeviceChangedEvent *ev, xEvent **dcce);
58*4882a593Smuzhiyun static int eventToDeviceEvent(DeviceEvent *ev, xEvent **xi);
59*4882a593Smuzhiyun static int eventToRawEvent(RawDeviceEvent *ev, xEvent **xi);
60*4882a593Smuzhiyun static int eventToBarrierEvent(BarrierEvent *ev, xEvent **xi);
61*4882a593Smuzhiyun static int eventToTouchOwnershipEvent(TouchOwnershipEvent *ev, xEvent **xi);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /* Do not use, read comments below */
64*4882a593Smuzhiyun BOOL EventIsKeyRepeat(xEvent *event);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /**
67*4882a593Smuzhiyun  * Hack to allow detectable autorepeat for core and XI1 events.
68*4882a593Smuzhiyun  * The sequence number is unused until we send to the client and can be
69*4882a593Smuzhiyun  * misused to store data. More or less, anyway.
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * Do not use this. It may change any time without warning, eat your babies
72*4882a593Smuzhiyun  * and piss on your cat.
73*4882a593Smuzhiyun  */
74*4882a593Smuzhiyun static void
EventSetKeyRepeatFlag(xEvent * event,BOOL on)75*4882a593Smuzhiyun EventSetKeyRepeatFlag(xEvent *event, BOOL on)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun     event->u.u.sequenceNumber = on;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /**
81*4882a593Smuzhiyun  * Check if the event was marked as a repeat event before.
82*4882a593Smuzhiyun  * NOTE: This is a nasty hack and should NOT be used by anyone else but
83*4882a593Smuzhiyun  * TryClientEvents.
84*4882a593Smuzhiyun  */
85*4882a593Smuzhiyun BOOL
EventIsKeyRepeat(xEvent * event)86*4882a593Smuzhiyun EventIsKeyRepeat(xEvent *event)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun     return ! !event->u.u.sequenceNumber;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /**
92*4882a593Smuzhiyun  * Convert the given event to the respective core event.
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * Return values:
95*4882a593Smuzhiyun  * Success ... core contains the matching core event.
96*4882a593Smuzhiyun  * BadValue .. One or more values in the internal event are invalid.
97*4882a593Smuzhiyun  * BadMatch .. The event has no core equivalent.
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * @param[in] event The event to convert into a core event.
100*4882a593Smuzhiyun  * @param[in] core The memory location to store the core event at.
101*4882a593Smuzhiyun  * @return Success or the matching error code.
102*4882a593Smuzhiyun  */
103*4882a593Smuzhiyun int
EventToCore(InternalEvent * event,xEvent ** core_out,int * count_out)104*4882a593Smuzhiyun EventToCore(InternalEvent *event, xEvent **core_out, int *count_out)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun     xEvent *core = NULL;
107*4882a593Smuzhiyun     int count = 0;
108*4882a593Smuzhiyun     int ret = BadImplementation;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun     switch (event->any.type) {
111*4882a593Smuzhiyun     case ET_Motion:
112*4882a593Smuzhiyun     {
113*4882a593Smuzhiyun         DeviceEvent *e = &event->device_event;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun         /* Don't create core motion event if neither x nor y are
116*4882a593Smuzhiyun          * present */
117*4882a593Smuzhiyun         if (!BitIsOn(e->valuators.mask, 0) && !BitIsOn(e->valuators.mask, 1)) {
118*4882a593Smuzhiyun             ret = BadMatch;
119*4882a593Smuzhiyun             goto out;
120*4882a593Smuzhiyun         }
121*4882a593Smuzhiyun     }
122*4882a593Smuzhiyun         /* fallthrough */
123*4882a593Smuzhiyun     case ET_ButtonPress:
124*4882a593Smuzhiyun     case ET_ButtonRelease:
125*4882a593Smuzhiyun     case ET_KeyPress:
126*4882a593Smuzhiyun     case ET_KeyRelease:
127*4882a593Smuzhiyun     {
128*4882a593Smuzhiyun         DeviceEvent *e = &event->device_event;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun         if (e->detail.key > 0xFF) {
131*4882a593Smuzhiyun             ret = BadMatch;
132*4882a593Smuzhiyun             goto out;
133*4882a593Smuzhiyun         }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun         core = calloc(1, sizeof(*core));
136*4882a593Smuzhiyun         if (!core)
137*4882a593Smuzhiyun             return BadAlloc;
138*4882a593Smuzhiyun         count = 1;
139*4882a593Smuzhiyun         core->u.u.type = e->type - ET_KeyPress + KeyPress;
140*4882a593Smuzhiyun         core->u.u.detail = e->detail.key & 0xFF;
141*4882a593Smuzhiyun         core->u.keyButtonPointer.time = e->time;
142*4882a593Smuzhiyun         core->u.keyButtonPointer.rootX = e->root_x;
143*4882a593Smuzhiyun         core->u.keyButtonPointer.rootY = e->root_y;
144*4882a593Smuzhiyun         core->u.keyButtonPointer.state = e->corestate;
145*4882a593Smuzhiyun         core->u.keyButtonPointer.root = e->root;
146*4882a593Smuzhiyun         EventSetKeyRepeatFlag(core, (e->type == ET_KeyPress && e->key_repeat));
147*4882a593Smuzhiyun         ret = Success;
148*4882a593Smuzhiyun     }
149*4882a593Smuzhiyun         break;
150*4882a593Smuzhiyun     case ET_ProximityIn:
151*4882a593Smuzhiyun     case ET_ProximityOut:
152*4882a593Smuzhiyun     case ET_RawKeyPress:
153*4882a593Smuzhiyun     case ET_RawKeyRelease:
154*4882a593Smuzhiyun     case ET_RawButtonPress:
155*4882a593Smuzhiyun     case ET_RawButtonRelease:
156*4882a593Smuzhiyun     case ET_RawMotion:
157*4882a593Smuzhiyun     case ET_RawTouchBegin:
158*4882a593Smuzhiyun     case ET_RawTouchUpdate:
159*4882a593Smuzhiyun     case ET_RawTouchEnd:
160*4882a593Smuzhiyun     case ET_TouchBegin:
161*4882a593Smuzhiyun     case ET_TouchUpdate:
162*4882a593Smuzhiyun     case ET_TouchEnd:
163*4882a593Smuzhiyun     case ET_TouchOwnership:
164*4882a593Smuzhiyun     case ET_BarrierHit:
165*4882a593Smuzhiyun     case ET_BarrierLeave:
166*4882a593Smuzhiyun         ret = BadMatch;
167*4882a593Smuzhiyun         break;
168*4882a593Smuzhiyun     default:
169*4882a593Smuzhiyun         /* XXX: */
170*4882a593Smuzhiyun         ErrorF("[dix] EventToCore: Not implemented yet \n");
171*4882a593Smuzhiyun         ret = BadImplementation;
172*4882a593Smuzhiyun     }
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun  out:
175*4882a593Smuzhiyun     *core_out = core;
176*4882a593Smuzhiyun     *count_out = count;
177*4882a593Smuzhiyun     return ret;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun /**
181*4882a593Smuzhiyun  * Convert the given event to the respective XI 1.x event and store it in
182*4882a593Smuzhiyun  * xi. xi is allocated on demand and must be freed by the caller.
183*4882a593Smuzhiyun  * count returns the number of events in xi. If count is 1, and the type of
184*4882a593Smuzhiyun  * xi is GenericEvent, then xi may be larger than 32 bytes.
185*4882a593Smuzhiyun  *
186*4882a593Smuzhiyun  * Return values:
187*4882a593Smuzhiyun  * Success ... core contains the matching core event.
188*4882a593Smuzhiyun  * BadValue .. One or more values in the internal event are invalid.
189*4882a593Smuzhiyun  * BadMatch .. The event has no XI equivalent.
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * @param[in] ev The event to convert into an XI 1 event.
192*4882a593Smuzhiyun  * @param[out] xi Future memory location for the XI event.
193*4882a593Smuzhiyun  * @param[out] count Number of elements in xi.
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * @return Success or the error code.
196*4882a593Smuzhiyun  */
197*4882a593Smuzhiyun int
EventToXI(InternalEvent * ev,xEvent ** xi,int * count)198*4882a593Smuzhiyun EventToXI(InternalEvent *ev, xEvent **xi, int *count)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun     switch (ev->any.type) {
201*4882a593Smuzhiyun     case ET_Motion:
202*4882a593Smuzhiyun     case ET_ButtonPress:
203*4882a593Smuzhiyun     case ET_ButtonRelease:
204*4882a593Smuzhiyun     case ET_KeyPress:
205*4882a593Smuzhiyun     case ET_KeyRelease:
206*4882a593Smuzhiyun     case ET_ProximityIn:
207*4882a593Smuzhiyun     case ET_ProximityOut:
208*4882a593Smuzhiyun         return eventToKeyButtonPointer(&ev->device_event, xi, count);
209*4882a593Smuzhiyun     case ET_DeviceChanged:
210*4882a593Smuzhiyun     case ET_RawKeyPress:
211*4882a593Smuzhiyun     case ET_RawKeyRelease:
212*4882a593Smuzhiyun     case ET_RawButtonPress:
213*4882a593Smuzhiyun     case ET_RawButtonRelease:
214*4882a593Smuzhiyun     case ET_RawMotion:
215*4882a593Smuzhiyun     case ET_RawTouchBegin:
216*4882a593Smuzhiyun     case ET_RawTouchUpdate:
217*4882a593Smuzhiyun     case ET_RawTouchEnd:
218*4882a593Smuzhiyun     case ET_TouchBegin:
219*4882a593Smuzhiyun     case ET_TouchUpdate:
220*4882a593Smuzhiyun     case ET_TouchEnd:
221*4882a593Smuzhiyun     case ET_TouchOwnership:
222*4882a593Smuzhiyun     case ET_BarrierHit:
223*4882a593Smuzhiyun     case ET_BarrierLeave:
224*4882a593Smuzhiyun         *count = 0;
225*4882a593Smuzhiyun         *xi = NULL;
226*4882a593Smuzhiyun         return BadMatch;
227*4882a593Smuzhiyun     default:
228*4882a593Smuzhiyun         break;
229*4882a593Smuzhiyun     }
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun     ErrorF("[dix] EventToXI: Not implemented for %d \n", ev->any.type);
232*4882a593Smuzhiyun     return BadImplementation;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun /**
236*4882a593Smuzhiyun  * Convert the given event to the respective XI 2.x event and store it in xi.
237*4882a593Smuzhiyun  * xi is allocated on demand and must be freed by the caller.
238*4882a593Smuzhiyun  *
239*4882a593Smuzhiyun  * Return values:
240*4882a593Smuzhiyun  * Success ... core contains the matching core event.
241*4882a593Smuzhiyun  * BadValue .. One or more values in the internal event are invalid.
242*4882a593Smuzhiyun  * BadMatch .. The event has no XI2 equivalent.
243*4882a593Smuzhiyun  *
244*4882a593Smuzhiyun  * @param[in] ev The event to convert into an XI2 event
245*4882a593Smuzhiyun  * @param[out] xi Future memory location for the XI2 event.
246*4882a593Smuzhiyun  *
247*4882a593Smuzhiyun  * @return Success or the error code.
248*4882a593Smuzhiyun  */
249*4882a593Smuzhiyun int
EventToXI2(InternalEvent * ev,xEvent ** xi)250*4882a593Smuzhiyun EventToXI2(InternalEvent *ev, xEvent **xi)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun     switch (ev->any.type) {
253*4882a593Smuzhiyun         /* Enter/FocusIn are for grabs. We don't need an actual event, since
254*4882a593Smuzhiyun          * the real events delivered are triggered elsewhere */
255*4882a593Smuzhiyun     case ET_Enter:
256*4882a593Smuzhiyun     case ET_FocusIn:
257*4882a593Smuzhiyun         *xi = NULL;
258*4882a593Smuzhiyun         return Success;
259*4882a593Smuzhiyun     case ET_Motion:
260*4882a593Smuzhiyun     case ET_ButtonPress:
261*4882a593Smuzhiyun     case ET_ButtonRelease:
262*4882a593Smuzhiyun     case ET_KeyPress:
263*4882a593Smuzhiyun     case ET_KeyRelease:
264*4882a593Smuzhiyun     case ET_TouchBegin:
265*4882a593Smuzhiyun     case ET_TouchUpdate:
266*4882a593Smuzhiyun     case ET_TouchEnd:
267*4882a593Smuzhiyun         return eventToDeviceEvent(&ev->device_event, xi);
268*4882a593Smuzhiyun     case ET_TouchOwnership:
269*4882a593Smuzhiyun         return eventToTouchOwnershipEvent(&ev->touch_ownership_event, xi);
270*4882a593Smuzhiyun     case ET_ProximityIn:
271*4882a593Smuzhiyun     case ET_ProximityOut:
272*4882a593Smuzhiyun         *xi = NULL;
273*4882a593Smuzhiyun         return BadMatch;
274*4882a593Smuzhiyun     case ET_DeviceChanged:
275*4882a593Smuzhiyun         return eventToDeviceChanged(&ev->changed_event, xi);
276*4882a593Smuzhiyun     case ET_RawKeyPress:
277*4882a593Smuzhiyun     case ET_RawKeyRelease:
278*4882a593Smuzhiyun     case ET_RawButtonPress:
279*4882a593Smuzhiyun     case ET_RawButtonRelease:
280*4882a593Smuzhiyun     case ET_RawMotion:
281*4882a593Smuzhiyun     case ET_RawTouchBegin:
282*4882a593Smuzhiyun     case ET_RawTouchUpdate:
283*4882a593Smuzhiyun     case ET_RawTouchEnd:
284*4882a593Smuzhiyun         return eventToRawEvent(&ev->raw_event, xi);
285*4882a593Smuzhiyun     case ET_BarrierHit:
286*4882a593Smuzhiyun     case ET_BarrierLeave:
287*4882a593Smuzhiyun         return eventToBarrierEvent(&ev->barrier_event, xi);
288*4882a593Smuzhiyun     default:
289*4882a593Smuzhiyun         break;
290*4882a593Smuzhiyun     }
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun     ErrorF("[dix] EventToXI2: Not implemented for %d \n", ev->any.type);
293*4882a593Smuzhiyun     return BadImplementation;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun static int
eventToKeyButtonPointer(DeviceEvent * ev,xEvent ** xi,int * count)297*4882a593Smuzhiyun eventToKeyButtonPointer(DeviceEvent *ev, xEvent **xi, int *count)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun     int num_events;
300*4882a593Smuzhiyun     int first;                  /* dummy */
301*4882a593Smuzhiyun     deviceKeyButtonPointer *kbp;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun     /* Sorry, XI 1.x protocol restrictions. */
304*4882a593Smuzhiyun     if (ev->detail.button > 0xFF || ev->deviceid >= 0x80) {
305*4882a593Smuzhiyun         *count = 0;
306*4882a593Smuzhiyun         return Success;
307*4882a593Smuzhiyun     }
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun     num_events = (countValuators(ev, &first) + 5) / 6;  /* valuator ev */
310*4882a593Smuzhiyun     if (num_events <= 0) {
311*4882a593Smuzhiyun         switch (ev->type) {
312*4882a593Smuzhiyun         case ET_KeyPress:
313*4882a593Smuzhiyun         case ET_KeyRelease:
314*4882a593Smuzhiyun         case ET_ButtonPress:
315*4882a593Smuzhiyun         case ET_ButtonRelease:
316*4882a593Smuzhiyun             /* no axes is ok */
317*4882a593Smuzhiyun             break;
318*4882a593Smuzhiyun         case ET_Motion:
319*4882a593Smuzhiyun         case ET_ProximityIn:
320*4882a593Smuzhiyun         case ET_ProximityOut:
321*4882a593Smuzhiyun             *count = 0;
322*4882a593Smuzhiyun             return BadMatch;
323*4882a593Smuzhiyun         default:
324*4882a593Smuzhiyun             *count = 0;
325*4882a593Smuzhiyun             return BadImplementation;
326*4882a593Smuzhiyun         }
327*4882a593Smuzhiyun     }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun     num_events++;               /* the actual event event */
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun     *xi = calloc(num_events, sizeof(xEvent));
332*4882a593Smuzhiyun     if (!(*xi)) {
333*4882a593Smuzhiyun         return BadAlloc;
334*4882a593Smuzhiyun     }
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun     kbp = (deviceKeyButtonPointer *) (*xi);
337*4882a593Smuzhiyun     kbp->detail = ev->detail.button;
338*4882a593Smuzhiyun     kbp->time = ev->time;
339*4882a593Smuzhiyun     kbp->root = ev->root;
340*4882a593Smuzhiyun     kbp->root_x = ev->root_x;
341*4882a593Smuzhiyun     kbp->root_y = ev->root_y;
342*4882a593Smuzhiyun     kbp->deviceid = ev->deviceid;
343*4882a593Smuzhiyun     kbp->state = ev->corestate;
344*4882a593Smuzhiyun     EventSetKeyRepeatFlag((xEvent *) kbp,
345*4882a593Smuzhiyun                           (ev->type == ET_KeyPress && ev->key_repeat));
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun     if (num_events > 1)
348*4882a593Smuzhiyun         kbp->deviceid |= MORE_EVENTS;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun     switch (ev->type) {
351*4882a593Smuzhiyun     case ET_Motion:
352*4882a593Smuzhiyun         kbp->type = DeviceMotionNotify;
353*4882a593Smuzhiyun         break;
354*4882a593Smuzhiyun     case ET_ButtonPress:
355*4882a593Smuzhiyun         kbp->type = DeviceButtonPress;
356*4882a593Smuzhiyun         break;
357*4882a593Smuzhiyun     case ET_ButtonRelease:
358*4882a593Smuzhiyun         kbp->type = DeviceButtonRelease;
359*4882a593Smuzhiyun         break;
360*4882a593Smuzhiyun     case ET_KeyPress:
361*4882a593Smuzhiyun         kbp->type = DeviceKeyPress;
362*4882a593Smuzhiyun         break;
363*4882a593Smuzhiyun     case ET_KeyRelease:
364*4882a593Smuzhiyun         kbp->type = DeviceKeyRelease;
365*4882a593Smuzhiyun         break;
366*4882a593Smuzhiyun     case ET_ProximityIn:
367*4882a593Smuzhiyun         kbp->type = ProximityIn;
368*4882a593Smuzhiyun         break;
369*4882a593Smuzhiyun     case ET_ProximityOut:
370*4882a593Smuzhiyun         kbp->type = ProximityOut;
371*4882a593Smuzhiyun         break;
372*4882a593Smuzhiyun     default:
373*4882a593Smuzhiyun         break;
374*4882a593Smuzhiyun     }
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun     if (num_events > 1) {
377*4882a593Smuzhiyun         getValuatorEvents(ev, (deviceValuator *) (kbp + 1));
378*4882a593Smuzhiyun     }
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun     *count = num_events;
381*4882a593Smuzhiyun     return Success;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun /**
385*4882a593Smuzhiyun  * Set first to the first valuator in the event ev and return the number of
386*4882a593Smuzhiyun  * valuators from first to the last set valuator.
387*4882a593Smuzhiyun  */
388*4882a593Smuzhiyun static int
countValuators(DeviceEvent * ev,int * first)389*4882a593Smuzhiyun countValuators(DeviceEvent *ev, int *first)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun     int first_valuator = -1, last_valuator = -1, num_valuators = 0;
392*4882a593Smuzhiyun     int i;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun     for (i = 0; i < sizeof(ev->valuators.mask) * 8; i++) {
395*4882a593Smuzhiyun         if (BitIsOn(ev->valuators.mask, i)) {
396*4882a593Smuzhiyun             if (first_valuator == -1)
397*4882a593Smuzhiyun                 first_valuator = i;
398*4882a593Smuzhiyun             last_valuator = i;
399*4882a593Smuzhiyun         }
400*4882a593Smuzhiyun     }
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun     if (first_valuator != -1) {
403*4882a593Smuzhiyun         num_valuators = last_valuator - first_valuator + 1;
404*4882a593Smuzhiyun         *first = first_valuator;
405*4882a593Smuzhiyun     }
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun     return num_valuators;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun static int
getValuatorEvents(DeviceEvent * ev,deviceValuator * xv)411*4882a593Smuzhiyun getValuatorEvents(DeviceEvent *ev, deviceValuator * xv)
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun     int i;
414*4882a593Smuzhiyun     int state = 0;
415*4882a593Smuzhiyun     int first_valuator, num_valuators;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun     num_valuators = countValuators(ev, &first_valuator);
418*4882a593Smuzhiyun     if (num_valuators > 0) {
419*4882a593Smuzhiyun         DeviceIntPtr dev = NULL;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun         dixLookupDevice(&dev, ev->deviceid, serverClient, DixUseAccess);
422*4882a593Smuzhiyun         /* State needs to be assembled BEFORE the device is updated. */
423*4882a593Smuzhiyun         state = (dev &&
424*4882a593Smuzhiyun                  dev->key) ? XkbStateFieldFromRec(&dev->key->xkbInfo->
425*4882a593Smuzhiyun                                                   state) : 0;
426*4882a593Smuzhiyun         state |= (dev && dev->button) ? (dev->button->state) : 0;
427*4882a593Smuzhiyun     }
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun     for (i = 0; i < num_valuators; i += 6, xv++) {
430*4882a593Smuzhiyun         INT32 *valuators = &xv->valuator0;      // Treat all 6 vals as an array
431*4882a593Smuzhiyun         int j;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun         xv->type = DeviceValuator;
434*4882a593Smuzhiyun         xv->first_valuator = first_valuator + i;
435*4882a593Smuzhiyun         xv->num_valuators = ((num_valuators - i) > 6) ? 6 : (num_valuators - i);
436*4882a593Smuzhiyun         xv->deviceid = ev->deviceid;
437*4882a593Smuzhiyun         xv->device_state = state;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun         /* Unset valuators in masked valuator events have the proper data values
440*4882a593Smuzhiyun          * in the case of an absolute axis in between two set valuators. */
441*4882a593Smuzhiyun         for (j = 0; j < xv->num_valuators; j++)
442*4882a593Smuzhiyun             valuators[j] = ev->valuators.data[xv->first_valuator + j];
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun         if (i + 6 < num_valuators)
445*4882a593Smuzhiyun             xv->deviceid |= MORE_EVENTS;
446*4882a593Smuzhiyun     }
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun     return (num_valuators + 5) / 6;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun static int
appendKeyInfo(DeviceChangedEvent * dce,xXIKeyInfo * info)452*4882a593Smuzhiyun appendKeyInfo(DeviceChangedEvent *dce, xXIKeyInfo * info)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun     uint32_t *kc;
455*4882a593Smuzhiyun     int i;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun     info->type = XIKeyClass;
458*4882a593Smuzhiyun     info->num_keycodes = dce->keys.max_keycode - dce->keys.min_keycode + 1;
459*4882a593Smuzhiyun     info->length = sizeof(xXIKeyInfo) / 4 + info->num_keycodes;
460*4882a593Smuzhiyun     info->sourceid = dce->sourceid;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun     kc = (uint32_t *) &info[1];
463*4882a593Smuzhiyun     for (i = 0; i < info->num_keycodes; i++)
464*4882a593Smuzhiyun         *kc++ = i + dce->keys.min_keycode;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun     return info->length * 4;
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun static int
appendButtonInfo(DeviceChangedEvent * dce,xXIButtonInfo * info)470*4882a593Smuzhiyun appendButtonInfo(DeviceChangedEvent *dce, xXIButtonInfo * info)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun     unsigned char *bits;
473*4882a593Smuzhiyun     int mask_len;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun     mask_len = bytes_to_int32(bits_to_bytes(dce->buttons.num_buttons));
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun     info->type = XIButtonClass;
478*4882a593Smuzhiyun     info->num_buttons = dce->buttons.num_buttons;
479*4882a593Smuzhiyun     info->length = bytes_to_int32(sizeof(xXIButtonInfo)) +
480*4882a593Smuzhiyun         info->num_buttons + mask_len;
481*4882a593Smuzhiyun     info->sourceid = dce->sourceid;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun     bits = (unsigned char *) &info[1];
484*4882a593Smuzhiyun     memset(bits, 0, mask_len * 4);
485*4882a593Smuzhiyun     /* FIXME: is_down? */
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun     bits += mask_len * 4;
488*4882a593Smuzhiyun     memcpy(bits, dce->buttons.names, dce->buttons.num_buttons * sizeof(Atom));
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun     return info->length * 4;
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun static int
appendValuatorInfo(DeviceChangedEvent * dce,xXIValuatorInfo * info,int axisnumber)494*4882a593Smuzhiyun appendValuatorInfo(DeviceChangedEvent *dce, xXIValuatorInfo * info,
495*4882a593Smuzhiyun                    int axisnumber)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun     info->type = XIValuatorClass;
498*4882a593Smuzhiyun     info->length = sizeof(xXIValuatorInfo) / 4;
499*4882a593Smuzhiyun     info->label = dce->valuators[axisnumber].name;
500*4882a593Smuzhiyun     info->min.integral = dce->valuators[axisnumber].min;
501*4882a593Smuzhiyun     info->min.frac = 0;
502*4882a593Smuzhiyun     info->max.integral = dce->valuators[axisnumber].max;
503*4882a593Smuzhiyun     info->max.frac = 0;
504*4882a593Smuzhiyun     info->value = double_to_fp3232(dce->valuators[axisnumber].value);
505*4882a593Smuzhiyun     info->resolution = dce->valuators[axisnumber].resolution;
506*4882a593Smuzhiyun     info->number = axisnumber;
507*4882a593Smuzhiyun     info->mode = dce->valuators[axisnumber].mode;
508*4882a593Smuzhiyun     info->sourceid = dce->sourceid;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun     return info->length * 4;
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun static int
appendScrollInfo(DeviceChangedEvent * dce,xXIScrollInfo * info,int axisnumber)514*4882a593Smuzhiyun appendScrollInfo(DeviceChangedEvent *dce, xXIScrollInfo * info, int axisnumber)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun     if (dce->valuators[axisnumber].scroll.type == SCROLL_TYPE_NONE)
517*4882a593Smuzhiyun         return 0;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun     info->type = XIScrollClass;
520*4882a593Smuzhiyun     info->length = sizeof(xXIScrollInfo) / 4;
521*4882a593Smuzhiyun     info->number = axisnumber;
522*4882a593Smuzhiyun     switch (dce->valuators[axisnumber].scroll.type) {
523*4882a593Smuzhiyun     case SCROLL_TYPE_VERTICAL:
524*4882a593Smuzhiyun         info->scroll_type = XIScrollTypeVertical;
525*4882a593Smuzhiyun         break;
526*4882a593Smuzhiyun     case SCROLL_TYPE_HORIZONTAL:
527*4882a593Smuzhiyun         info->scroll_type = XIScrollTypeHorizontal;
528*4882a593Smuzhiyun         break;
529*4882a593Smuzhiyun     default:
530*4882a593Smuzhiyun         ErrorF("[Xi] Unknown scroll type %d. This is a bug.\n",
531*4882a593Smuzhiyun                dce->valuators[axisnumber].scroll.type);
532*4882a593Smuzhiyun         break;
533*4882a593Smuzhiyun     }
534*4882a593Smuzhiyun     info->increment =
535*4882a593Smuzhiyun         double_to_fp3232(dce->valuators[axisnumber].scroll.increment);
536*4882a593Smuzhiyun     info->sourceid = dce->sourceid;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun     info->flags = 0;
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun     if (dce->valuators[axisnumber].scroll.flags & SCROLL_FLAG_DONT_EMULATE)
541*4882a593Smuzhiyun         info->flags |= XIScrollFlagNoEmulation;
542*4882a593Smuzhiyun     if (dce->valuators[axisnumber].scroll.flags & SCROLL_FLAG_PREFERRED)
543*4882a593Smuzhiyun         info->flags |= XIScrollFlagPreferred;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun     return info->length * 4;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun static int
eventToDeviceChanged(DeviceChangedEvent * dce,xEvent ** xi)549*4882a593Smuzhiyun eventToDeviceChanged(DeviceChangedEvent *dce, xEvent **xi)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun     xXIDeviceChangedEvent *dcce;
552*4882a593Smuzhiyun     int len = sizeof(xXIDeviceChangedEvent);
553*4882a593Smuzhiyun     int nkeys;
554*4882a593Smuzhiyun     char *ptr;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun     if (dce->buttons.num_buttons) {
557*4882a593Smuzhiyun         len += sizeof(xXIButtonInfo);
558*4882a593Smuzhiyun         len += dce->buttons.num_buttons * sizeof(Atom); /* button names */
559*4882a593Smuzhiyun         len += pad_to_int32(bits_to_bytes(dce->buttons.num_buttons));
560*4882a593Smuzhiyun     }
561*4882a593Smuzhiyun     if (dce->num_valuators) {
562*4882a593Smuzhiyun         int i;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun         len += sizeof(xXIValuatorInfo) * dce->num_valuators;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun         for (i = 0; i < dce->num_valuators; i++)
567*4882a593Smuzhiyun             if (dce->valuators[i].scroll.type != SCROLL_TYPE_NONE)
568*4882a593Smuzhiyun                 len += sizeof(xXIScrollInfo);
569*4882a593Smuzhiyun     }
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun     nkeys = (dce->keys.max_keycode > 0) ?
572*4882a593Smuzhiyun         dce->keys.max_keycode - dce->keys.min_keycode + 1 : 0;
573*4882a593Smuzhiyun     if (nkeys > 0) {
574*4882a593Smuzhiyun         len += sizeof(xXIKeyInfo);
575*4882a593Smuzhiyun         len += sizeof(CARD32) * nkeys;  /* keycodes */
576*4882a593Smuzhiyun     }
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun     dcce = calloc(1, len);
579*4882a593Smuzhiyun     if (!dcce) {
580*4882a593Smuzhiyun         ErrorF("[Xi] BadAlloc in SendDeviceChangedEvent.\n");
581*4882a593Smuzhiyun         return BadAlloc;
582*4882a593Smuzhiyun     }
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun     dcce->type = GenericEvent;
585*4882a593Smuzhiyun     dcce->extension = IReqCode;
586*4882a593Smuzhiyun     dcce->evtype = XI_DeviceChanged;
587*4882a593Smuzhiyun     dcce->time = dce->time;
588*4882a593Smuzhiyun     dcce->deviceid = dce->deviceid;
589*4882a593Smuzhiyun     dcce->sourceid = dce->sourceid;
590*4882a593Smuzhiyun     dcce->reason =
591*4882a593Smuzhiyun         (dce->flags & DEVCHANGE_DEVICE_CHANGE) ? XIDeviceChange : XISlaveSwitch;
592*4882a593Smuzhiyun     dcce->num_classes = 0;
593*4882a593Smuzhiyun     dcce->length = bytes_to_int32(len - sizeof(xEvent));
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun     ptr = (char *) &dcce[1];
596*4882a593Smuzhiyun     if (dce->buttons.num_buttons) {
597*4882a593Smuzhiyun         dcce->num_classes++;
598*4882a593Smuzhiyun         ptr += appendButtonInfo(dce, (xXIButtonInfo *) ptr);
599*4882a593Smuzhiyun     }
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun     if (nkeys) {
602*4882a593Smuzhiyun         dcce->num_classes++;
603*4882a593Smuzhiyun         ptr += appendKeyInfo(dce, (xXIKeyInfo *) ptr);
604*4882a593Smuzhiyun     }
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun     if (dce->num_valuators) {
607*4882a593Smuzhiyun         int i;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun         dcce->num_classes += dce->num_valuators;
610*4882a593Smuzhiyun         for (i = 0; i < dce->num_valuators; i++)
611*4882a593Smuzhiyun             ptr += appendValuatorInfo(dce, (xXIValuatorInfo *) ptr, i);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun         for (i = 0; i < dce->num_valuators; i++) {
614*4882a593Smuzhiyun             if (dce->valuators[i].scroll.type != SCROLL_TYPE_NONE) {
615*4882a593Smuzhiyun                 dcce->num_classes++;
616*4882a593Smuzhiyun                 ptr += appendScrollInfo(dce, (xXIScrollInfo *) ptr, i);
617*4882a593Smuzhiyun             }
618*4882a593Smuzhiyun         }
619*4882a593Smuzhiyun     }
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun     *xi = (xEvent *) dcce;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun     return Success;
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun static int
count_bits(unsigned char * ptr,int len)627*4882a593Smuzhiyun count_bits(unsigned char *ptr, int len)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun     int bits = 0;
630*4882a593Smuzhiyun     unsigned int i;
631*4882a593Smuzhiyun     unsigned char x;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun     for (i = 0; i < len; i++) {
634*4882a593Smuzhiyun         x = ptr[i];
635*4882a593Smuzhiyun         while (x > 0) {
636*4882a593Smuzhiyun             bits += (x & 0x1);
637*4882a593Smuzhiyun             x >>= 1;
638*4882a593Smuzhiyun         }
639*4882a593Smuzhiyun     }
640*4882a593Smuzhiyun     return bits;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun static int
eventToDeviceEvent(DeviceEvent * ev,xEvent ** xi)644*4882a593Smuzhiyun eventToDeviceEvent(DeviceEvent *ev, xEvent **xi)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun     int len = sizeof(xXIDeviceEvent);
647*4882a593Smuzhiyun     xXIDeviceEvent *xde;
648*4882a593Smuzhiyun     int i, btlen, vallen;
649*4882a593Smuzhiyun     char *ptr;
650*4882a593Smuzhiyun     FP3232 *axisval;
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun     /* FIXME: this should just send the buttons we have, not MAX_BUTTONs. Same
653*4882a593Smuzhiyun      * with MAX_VALUATORS below */
654*4882a593Smuzhiyun     /* btlen is in 4 byte units */
655*4882a593Smuzhiyun     btlen = bytes_to_int32(bits_to_bytes(MAX_BUTTONS));
656*4882a593Smuzhiyun     len += btlen * 4;           /* buttonmask len */
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun     vallen = count_bits(ev->valuators.mask, ARRAY_SIZE(ev->valuators.mask));
659*4882a593Smuzhiyun     len += vallen * 2 * sizeof(uint32_t);       /* axisvalues */
660*4882a593Smuzhiyun     vallen = bytes_to_int32(bits_to_bytes(MAX_VALUATORS));
661*4882a593Smuzhiyun     len += vallen * 4;          /* valuators mask */
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun     *xi = calloc(1, len);
664*4882a593Smuzhiyun     xde = (xXIDeviceEvent *) * xi;
665*4882a593Smuzhiyun     xde->type = GenericEvent;
666*4882a593Smuzhiyun     xde->extension = IReqCode;
667*4882a593Smuzhiyun     xde->evtype = GetXI2Type(ev->type);
668*4882a593Smuzhiyun     xde->time = ev->time;
669*4882a593Smuzhiyun     xde->length = bytes_to_int32(len - sizeof(xEvent));
670*4882a593Smuzhiyun     if (IsTouchEvent((InternalEvent *) ev))
671*4882a593Smuzhiyun         xde->detail = ev->touchid;
672*4882a593Smuzhiyun     else
673*4882a593Smuzhiyun         xde->detail = ev->detail.button;
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun     xde->root = ev->root;
676*4882a593Smuzhiyun     xde->buttons_len = btlen;
677*4882a593Smuzhiyun     xde->valuators_len = vallen;
678*4882a593Smuzhiyun     xde->deviceid = ev->deviceid;
679*4882a593Smuzhiyun     xde->sourceid = ev->sourceid;
680*4882a593Smuzhiyun     xde->root_x = double_to_fp1616(ev->root_x + ev->root_x_frac);
681*4882a593Smuzhiyun     xde->root_y = double_to_fp1616(ev->root_y + ev->root_y_frac);
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun     if (IsTouchEvent((InternalEvent *)ev)) {
684*4882a593Smuzhiyun         if (ev->type == ET_TouchUpdate)
685*4882a593Smuzhiyun             xde->flags |= (ev->flags & TOUCH_PENDING_END) ? XITouchPendingEnd : 0;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun         if (ev->flags & TOUCH_POINTER_EMULATED)
688*4882a593Smuzhiyun             xde->flags |= XITouchEmulatingPointer;
689*4882a593Smuzhiyun     } else {
690*4882a593Smuzhiyun         xde->flags = ev->flags;
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun         if (ev->key_repeat)
693*4882a593Smuzhiyun             xde->flags |= XIKeyRepeat;
694*4882a593Smuzhiyun     }
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun     xde->mods.base_mods = ev->mods.base;
697*4882a593Smuzhiyun     xde->mods.latched_mods = ev->mods.latched;
698*4882a593Smuzhiyun     xde->mods.locked_mods = ev->mods.locked;
699*4882a593Smuzhiyun     xde->mods.effective_mods = ev->mods.effective;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun     xde->group.base_group = ev->group.base;
702*4882a593Smuzhiyun     xde->group.latched_group = ev->group.latched;
703*4882a593Smuzhiyun     xde->group.locked_group = ev->group.locked;
704*4882a593Smuzhiyun     xde->group.effective_group = ev->group.effective;
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun     ptr = (char *) &xde[1];
707*4882a593Smuzhiyun     for (i = 0; i < sizeof(ev->buttons) * 8; i++) {
708*4882a593Smuzhiyun         if (BitIsOn(ev->buttons, i))
709*4882a593Smuzhiyun             SetBit(ptr, i);
710*4882a593Smuzhiyun     }
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun     ptr += xde->buttons_len * 4;
713*4882a593Smuzhiyun     axisval = (FP3232 *) (ptr + xde->valuators_len * 4);
714*4882a593Smuzhiyun     for (i = 0; i < sizeof(ev->valuators.mask) * 8; i++) {
715*4882a593Smuzhiyun         if (BitIsOn(ev->valuators.mask, i)) {
716*4882a593Smuzhiyun             SetBit(ptr, i);
717*4882a593Smuzhiyun             *axisval = double_to_fp3232(ev->valuators.data[i]);
718*4882a593Smuzhiyun             axisval++;
719*4882a593Smuzhiyun         }
720*4882a593Smuzhiyun     }
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun     return Success;
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun static int
eventToTouchOwnershipEvent(TouchOwnershipEvent * ev,xEvent ** xi)726*4882a593Smuzhiyun eventToTouchOwnershipEvent(TouchOwnershipEvent *ev, xEvent **xi)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun     int len = sizeof(xXITouchOwnershipEvent);
729*4882a593Smuzhiyun     xXITouchOwnershipEvent *xtoe;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun     *xi = calloc(1, len);
732*4882a593Smuzhiyun     xtoe = (xXITouchOwnershipEvent *) * xi;
733*4882a593Smuzhiyun     xtoe->type = GenericEvent;
734*4882a593Smuzhiyun     xtoe->extension = IReqCode;
735*4882a593Smuzhiyun     xtoe->length = bytes_to_int32(len - sizeof(xEvent));
736*4882a593Smuzhiyun     xtoe->evtype = GetXI2Type(ev->type);
737*4882a593Smuzhiyun     xtoe->deviceid = ev->deviceid;
738*4882a593Smuzhiyun     xtoe->time = ev->time;
739*4882a593Smuzhiyun     xtoe->sourceid = ev->sourceid;
740*4882a593Smuzhiyun     xtoe->touchid = ev->touchid;
741*4882a593Smuzhiyun     xtoe->flags = 0;            /* we don't have wire flags for ownership yet */
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun     return Success;
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun static int
eventToRawEvent(RawDeviceEvent * ev,xEvent ** xi)747*4882a593Smuzhiyun eventToRawEvent(RawDeviceEvent *ev, xEvent **xi)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun     xXIRawEvent *raw;
750*4882a593Smuzhiyun     int vallen, nvals;
751*4882a593Smuzhiyun     int i, len = sizeof(xXIRawEvent);
752*4882a593Smuzhiyun     char *ptr;
753*4882a593Smuzhiyun     FP3232 *axisval, *axisval_raw;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun     nvals = count_bits(ev->valuators.mask, sizeof(ev->valuators.mask));
756*4882a593Smuzhiyun     len += nvals * sizeof(FP3232) * 2;  /* 8 byte per valuator, once
757*4882a593Smuzhiyun                                            raw, once processed */
758*4882a593Smuzhiyun     vallen = bytes_to_int32(bits_to_bytes(MAX_VALUATORS));
759*4882a593Smuzhiyun     len += vallen * 4;          /* valuators mask */
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun     *xi = calloc(1, len);
762*4882a593Smuzhiyun     raw = (xXIRawEvent *) * xi;
763*4882a593Smuzhiyun     raw->type = GenericEvent;
764*4882a593Smuzhiyun     raw->extension = IReqCode;
765*4882a593Smuzhiyun     raw->evtype = GetXI2Type(ev->type);
766*4882a593Smuzhiyun     raw->time = ev->time;
767*4882a593Smuzhiyun     raw->length = bytes_to_int32(len - sizeof(xEvent));
768*4882a593Smuzhiyun     raw->detail = ev->detail.button;
769*4882a593Smuzhiyun     raw->deviceid = ev->deviceid;
770*4882a593Smuzhiyun     raw->sourceid = ev->sourceid;
771*4882a593Smuzhiyun     raw->valuators_len = vallen;
772*4882a593Smuzhiyun     raw->flags = ev->flags;
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun     ptr = (char *) &raw[1];
775*4882a593Smuzhiyun     axisval = (FP3232 *) (ptr + raw->valuators_len * 4);
776*4882a593Smuzhiyun     axisval_raw = axisval + nvals;
777*4882a593Smuzhiyun     for (i = 0; i < sizeof(ev->valuators.mask) * 8; i++) {
778*4882a593Smuzhiyun         if (BitIsOn(ev->valuators.mask, i)) {
779*4882a593Smuzhiyun             SetBit(ptr, i);
780*4882a593Smuzhiyun             *axisval = double_to_fp3232(ev->valuators.data[i]);
781*4882a593Smuzhiyun             *axisval_raw = double_to_fp3232(ev->valuators.data_raw[i]);
782*4882a593Smuzhiyun             axisval++;
783*4882a593Smuzhiyun             axisval_raw++;
784*4882a593Smuzhiyun         }
785*4882a593Smuzhiyun     }
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun     return Success;
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun static int
eventToBarrierEvent(BarrierEvent * ev,xEvent ** xi)791*4882a593Smuzhiyun eventToBarrierEvent(BarrierEvent *ev, xEvent **xi)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun     xXIBarrierEvent *barrier;
794*4882a593Smuzhiyun     int len = sizeof(xXIBarrierEvent);
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun     *xi = calloc(1, len);
797*4882a593Smuzhiyun     barrier = (xXIBarrierEvent*) *xi;
798*4882a593Smuzhiyun     barrier->type = GenericEvent;
799*4882a593Smuzhiyun     barrier->extension = IReqCode;
800*4882a593Smuzhiyun     barrier->evtype = GetXI2Type(ev->type);
801*4882a593Smuzhiyun     barrier->length = bytes_to_int32(len - sizeof(xEvent));
802*4882a593Smuzhiyun     barrier->deviceid = ev->deviceid;
803*4882a593Smuzhiyun     barrier->sourceid = ev->sourceid;
804*4882a593Smuzhiyun     barrier->time = ev->time;
805*4882a593Smuzhiyun     barrier->event = ev->window;
806*4882a593Smuzhiyun     barrier->root = ev->root;
807*4882a593Smuzhiyun     barrier->dx = double_to_fp3232(ev->dx);
808*4882a593Smuzhiyun     barrier->dy = double_to_fp3232(ev->dy);
809*4882a593Smuzhiyun     barrier->dtime = ev->dt;
810*4882a593Smuzhiyun     barrier->flags = ev->flags;
811*4882a593Smuzhiyun     barrier->eventid = ev->event_id;
812*4882a593Smuzhiyun     barrier->barrier = ev->barrierid;
813*4882a593Smuzhiyun     barrier->root_x = double_to_fp1616(ev->root_x);
814*4882a593Smuzhiyun     barrier->root_y = double_to_fp1616(ev->root_y);
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun     return Success;
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun /**
820*4882a593Smuzhiyun  * Return the corresponding core type for the given event or 0 if no core
821*4882a593Smuzhiyun  * equivalent exists.
822*4882a593Smuzhiyun  */
823*4882a593Smuzhiyun int
GetCoreType(enum EventType type)824*4882a593Smuzhiyun GetCoreType(enum EventType type)
825*4882a593Smuzhiyun {
826*4882a593Smuzhiyun     int coretype = 0;
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun     switch (type) {
829*4882a593Smuzhiyun     case ET_Motion:
830*4882a593Smuzhiyun         coretype = MotionNotify;
831*4882a593Smuzhiyun         break;
832*4882a593Smuzhiyun     case ET_ButtonPress:
833*4882a593Smuzhiyun         coretype = ButtonPress;
834*4882a593Smuzhiyun         break;
835*4882a593Smuzhiyun     case ET_ButtonRelease:
836*4882a593Smuzhiyun         coretype = ButtonRelease;
837*4882a593Smuzhiyun         break;
838*4882a593Smuzhiyun     case ET_KeyPress:
839*4882a593Smuzhiyun         coretype = KeyPress;
840*4882a593Smuzhiyun         break;
841*4882a593Smuzhiyun     case ET_KeyRelease:
842*4882a593Smuzhiyun         coretype = KeyRelease;
843*4882a593Smuzhiyun         break;
844*4882a593Smuzhiyun     default:
845*4882a593Smuzhiyun         break;
846*4882a593Smuzhiyun     }
847*4882a593Smuzhiyun     return coretype;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun /**
851*4882a593Smuzhiyun  * Return the corresponding XI 1.x type for the given event or 0 if no
852*4882a593Smuzhiyun  * equivalent exists.
853*4882a593Smuzhiyun  */
854*4882a593Smuzhiyun int
GetXIType(enum EventType type)855*4882a593Smuzhiyun GetXIType(enum EventType type)
856*4882a593Smuzhiyun {
857*4882a593Smuzhiyun     int xitype = 0;
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun     switch (type) {
860*4882a593Smuzhiyun     case ET_Motion:
861*4882a593Smuzhiyun         xitype = DeviceMotionNotify;
862*4882a593Smuzhiyun         break;
863*4882a593Smuzhiyun     case ET_ButtonPress:
864*4882a593Smuzhiyun         xitype = DeviceButtonPress;
865*4882a593Smuzhiyun         break;
866*4882a593Smuzhiyun     case ET_ButtonRelease:
867*4882a593Smuzhiyun         xitype = DeviceButtonRelease;
868*4882a593Smuzhiyun         break;
869*4882a593Smuzhiyun     case ET_KeyPress:
870*4882a593Smuzhiyun         xitype = DeviceKeyPress;
871*4882a593Smuzhiyun         break;
872*4882a593Smuzhiyun     case ET_KeyRelease:
873*4882a593Smuzhiyun         xitype = DeviceKeyRelease;
874*4882a593Smuzhiyun         break;
875*4882a593Smuzhiyun     case ET_ProximityIn:
876*4882a593Smuzhiyun         xitype = ProximityIn;
877*4882a593Smuzhiyun         break;
878*4882a593Smuzhiyun     case ET_ProximityOut:
879*4882a593Smuzhiyun         xitype = ProximityOut;
880*4882a593Smuzhiyun         break;
881*4882a593Smuzhiyun     default:
882*4882a593Smuzhiyun         break;
883*4882a593Smuzhiyun     }
884*4882a593Smuzhiyun     return xitype;
885*4882a593Smuzhiyun }
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun /**
888*4882a593Smuzhiyun  * Return the corresponding XI 2.x type for the given event or 0 if no
889*4882a593Smuzhiyun  * equivalent exists.
890*4882a593Smuzhiyun  */
891*4882a593Smuzhiyun int
GetXI2Type(enum EventType type)892*4882a593Smuzhiyun GetXI2Type(enum EventType type)
893*4882a593Smuzhiyun {
894*4882a593Smuzhiyun     int xi2type = 0;
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun     switch (type) {
897*4882a593Smuzhiyun     case ET_Motion:
898*4882a593Smuzhiyun         xi2type = XI_Motion;
899*4882a593Smuzhiyun         break;
900*4882a593Smuzhiyun     case ET_ButtonPress:
901*4882a593Smuzhiyun         xi2type = XI_ButtonPress;
902*4882a593Smuzhiyun         break;
903*4882a593Smuzhiyun     case ET_ButtonRelease:
904*4882a593Smuzhiyun         xi2type = XI_ButtonRelease;
905*4882a593Smuzhiyun         break;
906*4882a593Smuzhiyun     case ET_KeyPress:
907*4882a593Smuzhiyun         xi2type = XI_KeyPress;
908*4882a593Smuzhiyun         break;
909*4882a593Smuzhiyun     case ET_KeyRelease:
910*4882a593Smuzhiyun         xi2type = XI_KeyRelease;
911*4882a593Smuzhiyun         break;
912*4882a593Smuzhiyun     case ET_Enter:
913*4882a593Smuzhiyun         xi2type = XI_Enter;
914*4882a593Smuzhiyun         break;
915*4882a593Smuzhiyun     case ET_Leave:
916*4882a593Smuzhiyun         xi2type = XI_Leave;
917*4882a593Smuzhiyun         break;
918*4882a593Smuzhiyun     case ET_Hierarchy:
919*4882a593Smuzhiyun         xi2type = XI_HierarchyChanged;
920*4882a593Smuzhiyun         break;
921*4882a593Smuzhiyun     case ET_DeviceChanged:
922*4882a593Smuzhiyun         xi2type = XI_DeviceChanged;
923*4882a593Smuzhiyun         break;
924*4882a593Smuzhiyun     case ET_RawKeyPress:
925*4882a593Smuzhiyun         xi2type = XI_RawKeyPress;
926*4882a593Smuzhiyun         break;
927*4882a593Smuzhiyun     case ET_RawKeyRelease:
928*4882a593Smuzhiyun         xi2type = XI_RawKeyRelease;
929*4882a593Smuzhiyun         break;
930*4882a593Smuzhiyun     case ET_RawButtonPress:
931*4882a593Smuzhiyun         xi2type = XI_RawButtonPress;
932*4882a593Smuzhiyun         break;
933*4882a593Smuzhiyun     case ET_RawButtonRelease:
934*4882a593Smuzhiyun         xi2type = XI_RawButtonRelease;
935*4882a593Smuzhiyun         break;
936*4882a593Smuzhiyun     case ET_RawMotion:
937*4882a593Smuzhiyun         xi2type = XI_RawMotion;
938*4882a593Smuzhiyun         break;
939*4882a593Smuzhiyun     case ET_RawTouchBegin:
940*4882a593Smuzhiyun         xi2type = XI_RawTouchBegin;
941*4882a593Smuzhiyun         break;
942*4882a593Smuzhiyun     case ET_RawTouchUpdate:
943*4882a593Smuzhiyun         xi2type = XI_RawTouchUpdate;
944*4882a593Smuzhiyun         break;
945*4882a593Smuzhiyun     case ET_RawTouchEnd:
946*4882a593Smuzhiyun         xi2type = XI_RawTouchEnd;
947*4882a593Smuzhiyun         break;
948*4882a593Smuzhiyun     case ET_FocusIn:
949*4882a593Smuzhiyun         xi2type = XI_FocusIn;
950*4882a593Smuzhiyun         break;
951*4882a593Smuzhiyun     case ET_FocusOut:
952*4882a593Smuzhiyun         xi2type = XI_FocusOut;
953*4882a593Smuzhiyun         break;
954*4882a593Smuzhiyun     case ET_TouchBegin:
955*4882a593Smuzhiyun         xi2type = XI_TouchBegin;
956*4882a593Smuzhiyun         break;
957*4882a593Smuzhiyun     case ET_TouchEnd:
958*4882a593Smuzhiyun         xi2type = XI_TouchEnd;
959*4882a593Smuzhiyun         break;
960*4882a593Smuzhiyun     case ET_TouchUpdate:
961*4882a593Smuzhiyun         xi2type = XI_TouchUpdate;
962*4882a593Smuzhiyun         break;
963*4882a593Smuzhiyun     case ET_TouchOwnership:
964*4882a593Smuzhiyun         xi2type = XI_TouchOwnership;
965*4882a593Smuzhiyun         break;
966*4882a593Smuzhiyun     case ET_BarrierHit:
967*4882a593Smuzhiyun         xi2type = XI_BarrierHit;
968*4882a593Smuzhiyun         break;
969*4882a593Smuzhiyun     case ET_BarrierLeave:
970*4882a593Smuzhiyun         xi2type = XI_BarrierLeave;
971*4882a593Smuzhiyun         break;
972*4882a593Smuzhiyun     default:
973*4882a593Smuzhiyun         break;
974*4882a593Smuzhiyun     }
975*4882a593Smuzhiyun     return xi2type;
976*4882a593Smuzhiyun }
977