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