eventconvert.c revision f2346221
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
54static int countValuators(DeviceEvent *ev, int *first);
55static int getValuatorEvents(DeviceEvent *ev, deviceValuator * xv);
56static int eventToKeyButtonPointer(DeviceEvent *ev, xEvent **xi, int *count);
57static int eventToDeviceChanged(DeviceChangedEvent *ev, xEvent **dcce);
58static int eventToDeviceEvent(DeviceEvent *ev, xEvent **xi);
59static int eventToRawEvent(RawDeviceEvent *ev, xEvent **xi);
60static int eventToBarrierEvent(BarrierEvent *ev, xEvent **xi);
61static int eventToTouchOwnershipEvent(TouchOwnershipEvent *ev, xEvent **xi);
62static int eventToGestureSwipeEvent(GestureEvent *ev, xEvent **xi);
63static int eventToGesturePinchEvent(GestureEvent *ev, xEvent **xi);
64
65/* Do not use, read comments below */
66BOOL 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 */
76static void
77EventSetKeyRepeatFlag(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 */
87BOOL
88EventIsKeyRepeat(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 */
105int
106EventToCore(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 */
205int
206EventToXI(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 */
263int
264EventToXI2(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
318static int
319eventToKeyButtonPointer(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 */
410static int
411countValuators(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
432static int
433getValuatorEvents(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
473static int
474appendKeyInfo(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
491static int
492appendButtonInfo(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
515static int
516appendValuatorInfo(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
535static int
536appendScrollInfo(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
570static int
571eventToDeviceChanged(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
648static int
649count_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
665static int
666eventToDeviceEvent(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    if (*xi == NULL)
687        return BadAlloc;
688    xde = (xXIDeviceEvent *) * xi;
689    xde->type = GenericEvent;
690    xde->extension = IReqCode;
691    xde->evtype = GetXI2Type(ev->type);
692    xde->time = ev->time;
693    xde->length = bytes_to_int32(len - sizeof(xEvent));
694    if (IsTouchEvent((InternalEvent *) ev))
695        xde->detail = ev->touchid;
696    else
697        xde->detail = ev->detail.button;
698
699    xde->root = ev->root;
700    xde->buttons_len = btlen;
701    xde->valuators_len = vallen;
702    xde->deviceid = ev->deviceid;
703    xde->sourceid = ev->sourceid;
704    xde->root_x = double_to_fp1616(ev->root_x + ev->root_x_frac);
705    xde->root_y = double_to_fp1616(ev->root_y + ev->root_y_frac);
706
707    if (IsTouchEvent((InternalEvent *)ev)) {
708        if (ev->type == ET_TouchUpdate)
709            xde->flags |= (ev->flags & TOUCH_PENDING_END) ? XITouchPendingEnd : 0;
710
711        if (ev->flags & TOUCH_POINTER_EMULATED)
712            xde->flags |= XITouchEmulatingPointer;
713    } else {
714        xde->flags = ev->flags;
715
716        if (ev->key_repeat)
717            xde->flags |= XIKeyRepeat;
718    }
719
720    xde->mods.base_mods = ev->mods.base;
721    xde->mods.latched_mods = ev->mods.latched;
722    xde->mods.locked_mods = ev->mods.locked;
723    xde->mods.effective_mods = ev->mods.effective;
724
725    xde->group.base_group = ev->group.base;
726    xde->group.latched_group = ev->group.latched;
727    xde->group.locked_group = ev->group.locked;
728    xde->group.effective_group = ev->group.effective;
729
730    ptr = (char *) &xde[1];
731    for (i = 0; i < sizeof(ev->buttons) * 8; i++) {
732        if (BitIsOn(ev->buttons, i))
733            SetBit(ptr, i);
734    }
735
736    ptr += xde->buttons_len * 4;
737    axisval = (FP3232 *) (ptr + xde->valuators_len * 4);
738    for (i = 0; i < MAX_VALUATORS; i++) {
739        if (BitIsOn(ev->valuators.mask, i)) {
740            SetBit(ptr, i);
741            *axisval = double_to_fp3232(ev->valuators.data[i]);
742            axisval++;
743        }
744    }
745
746    return Success;
747}
748
749static int
750eventToTouchOwnershipEvent(TouchOwnershipEvent *ev, xEvent **xi)
751{
752    int len = sizeof(xXITouchOwnershipEvent);
753    xXITouchOwnershipEvent *xtoe;
754
755    *xi = calloc(1, len);
756    if (*xi == NULL)
757        return BadAlloc;
758    xtoe = (xXITouchOwnershipEvent *) * xi;
759    xtoe->type = GenericEvent;
760    xtoe->extension = IReqCode;
761    xtoe->length = bytes_to_int32(len - sizeof(xEvent));
762    xtoe->evtype = GetXI2Type(ev->type);
763    xtoe->deviceid = ev->deviceid;
764    xtoe->time = ev->time;
765    xtoe->sourceid = ev->sourceid;
766    xtoe->touchid = ev->touchid;
767    xtoe->flags = 0;            /* we don't have wire flags for ownership yet */
768
769    return Success;
770}
771
772static int
773eventToRawEvent(RawDeviceEvent *ev, xEvent **xi)
774{
775    xXIRawEvent *raw;
776    int vallen, nvals;
777    int i, len = sizeof(xXIRawEvent);
778    char *ptr;
779    FP3232 *axisval, *axisval_raw;
780
781    nvals = count_bits(ev->valuators.mask, sizeof(ev->valuators.mask));
782    len += nvals * sizeof(FP3232) * 2;  /* 8 byte per valuator, once
783                                           raw, once processed */
784    vallen = bytes_to_int32(bits_to_bytes(MAX_VALUATORS));
785    len += vallen * 4;          /* valuators mask */
786
787    *xi = calloc(1, len);
788    if (*xi == NULL)
789        return BadAlloc;
790    raw = (xXIRawEvent *) * xi;
791    raw->type = GenericEvent;
792    raw->extension = IReqCode;
793    raw->evtype = GetXI2Type(ev->type);
794    raw->time = ev->time;
795    raw->length = bytes_to_int32(len - sizeof(xEvent));
796    raw->detail = ev->detail.button;
797    raw->deviceid = ev->deviceid;
798    raw->sourceid = ev->sourceid;
799    raw->valuators_len = vallen;
800    raw->flags = ev->flags;
801
802    ptr = (char *) &raw[1];
803    axisval = (FP3232 *) (ptr + raw->valuators_len * 4);
804    axisval_raw = axisval + nvals;
805    for (i = 0; i < MAX_VALUATORS; i++) {
806        if (BitIsOn(ev->valuators.mask, i)) {
807            SetBit(ptr, i);
808            *axisval = double_to_fp3232(ev->valuators.data[i]);
809            *axisval_raw = double_to_fp3232(ev->valuators.data_raw[i]);
810            axisval++;
811            axisval_raw++;
812        }
813    }
814
815    return Success;
816}
817
818static int
819eventToBarrierEvent(BarrierEvent *ev, xEvent **xi)
820{
821    xXIBarrierEvent *barrier;
822    int len = sizeof(xXIBarrierEvent);
823
824    *xi = calloc(1, len);
825    if (*xi == NULL)
826        return BadAlloc;
827    barrier = (xXIBarrierEvent*) *xi;
828    barrier->type = GenericEvent;
829    barrier->extension = IReqCode;
830    barrier->evtype = GetXI2Type(ev->type);
831    barrier->length = bytes_to_int32(len - sizeof(xEvent));
832    barrier->deviceid = ev->deviceid;
833    barrier->sourceid = ev->sourceid;
834    barrier->time = ev->time;
835    barrier->event = ev->window;
836    barrier->root = ev->root;
837    barrier->dx = double_to_fp3232(ev->dx);
838    barrier->dy = double_to_fp3232(ev->dy);
839    barrier->dtime = ev->dt;
840    barrier->flags = ev->flags;
841    barrier->eventid = ev->event_id;
842    barrier->barrier = ev->barrierid;
843    barrier->root_x = double_to_fp1616(ev->root_x);
844    barrier->root_y = double_to_fp1616(ev->root_y);
845
846    return Success;
847}
848
849int
850eventToGesturePinchEvent(GestureEvent *ev, xEvent **xi)
851{
852    int len = sizeof(xXIGesturePinchEvent);
853    xXIGesturePinchEvent *xpe;
854
855    *xi = calloc(1, len);
856    if (*xi == NULL)
857        return BadAlloc;
858    xpe = (xXIGesturePinchEvent *) * xi;
859    xpe->type = GenericEvent;
860    xpe->extension = IReqCode;
861    xpe->evtype = GetXI2Type(ev->type);
862    xpe->time = ev->time;
863    xpe->length = bytes_to_int32(len - sizeof(xEvent));
864    xpe->detail = ev->num_touches;
865
866    xpe->root = ev->root;
867    xpe->deviceid = ev->deviceid;
868    xpe->sourceid = ev->sourceid;
869    xpe->root_x = double_to_fp1616(ev->root_x);
870    xpe->root_y = double_to_fp1616(ev->root_y);
871    xpe->flags |= (ev->flags & GESTURE_CANCELLED) ? XIGesturePinchEventCancelled : 0;
872
873    xpe->delta_x = double_to_fp1616(ev->delta_x);
874    xpe->delta_y = double_to_fp1616(ev->delta_y);
875    xpe->delta_unaccel_x = double_to_fp1616(ev->delta_unaccel_x);
876    xpe->delta_unaccel_y = double_to_fp1616(ev->delta_unaccel_y);
877    xpe->scale = double_to_fp1616(ev->scale);
878    xpe->delta_angle = double_to_fp1616(ev->delta_angle);
879
880    xpe->mods.base_mods = ev->mods.base;
881    xpe->mods.latched_mods = ev->mods.latched;
882    xpe->mods.locked_mods = ev->mods.locked;
883    xpe->mods.effective_mods = ev->mods.effective;
884
885    xpe->group.base_group = ev->group.base;
886    xpe->group.latched_group = ev->group.latched;
887    xpe->group.locked_group = ev->group.locked;
888    xpe->group.effective_group = ev->group.effective;
889
890    return Success;
891}
892
893int
894eventToGestureSwipeEvent(GestureEvent *ev, xEvent **xi)
895{
896    int len = sizeof(xXIGestureSwipeEvent);
897    xXIGestureSwipeEvent *xde;
898
899    *xi = calloc(1, len);
900    if (*xi == NULL)
901        return BadAlloc;
902    xde = (xXIGestureSwipeEvent *) * xi;
903    xde->type = GenericEvent;
904    xde->extension = IReqCode;
905    xde->evtype = GetXI2Type(ev->type);
906    xde->time = ev->time;
907    xde->length = bytes_to_int32(len - sizeof(xEvent));
908    xde->detail = ev->num_touches;
909
910    xde->root = ev->root;
911    xde->deviceid = ev->deviceid;
912    xde->sourceid = ev->sourceid;
913    xde->root_x = double_to_fp1616(ev->root_x);
914    xde->root_y = double_to_fp1616(ev->root_y);
915    xde->flags |= (ev->flags & GESTURE_CANCELLED) ? XIGestureSwipeEventCancelled : 0;
916
917    xde->delta_x = double_to_fp1616(ev->delta_x);
918    xde->delta_y = double_to_fp1616(ev->delta_y);
919    xde->delta_unaccel_x = double_to_fp1616(ev->delta_unaccel_x);
920    xde->delta_unaccel_y = double_to_fp1616(ev->delta_unaccel_y);
921
922    xde->mods.base_mods = ev->mods.base;
923    xde->mods.latched_mods = ev->mods.latched;
924    xde->mods.locked_mods = ev->mods.locked;
925    xde->mods.effective_mods = ev->mods.effective;
926
927    xde->group.base_group = ev->group.base;
928    xde->group.latched_group = ev->group.latched;
929    xde->group.locked_group = ev->group.locked;
930    xde->group.effective_group = ev->group.effective;
931
932    return Success;
933}
934
935/**
936 * Return the corresponding core type for the given event or 0 if no core
937 * equivalent exists.
938 */
939int
940GetCoreType(enum EventType type)
941{
942    int coretype = 0;
943
944    switch (type) {
945    case ET_Motion:
946        coretype = MotionNotify;
947        break;
948    case ET_ButtonPress:
949        coretype = ButtonPress;
950        break;
951    case ET_ButtonRelease:
952        coretype = ButtonRelease;
953        break;
954    case ET_KeyPress:
955        coretype = KeyPress;
956        break;
957    case ET_KeyRelease:
958        coretype = KeyRelease;
959        break;
960    default:
961        break;
962    }
963    return coretype;
964}
965
966/**
967 * Return the corresponding XI 1.x type for the given event or 0 if no
968 * equivalent exists.
969 */
970int
971GetXIType(enum EventType type)
972{
973    int xitype = 0;
974
975    switch (type) {
976    case ET_Motion:
977        xitype = DeviceMotionNotify;
978        break;
979    case ET_ButtonPress:
980        xitype = DeviceButtonPress;
981        break;
982    case ET_ButtonRelease:
983        xitype = DeviceButtonRelease;
984        break;
985    case ET_KeyPress:
986        xitype = DeviceKeyPress;
987        break;
988    case ET_KeyRelease:
989        xitype = DeviceKeyRelease;
990        break;
991    case ET_ProximityIn:
992        xitype = ProximityIn;
993        break;
994    case ET_ProximityOut:
995        xitype = ProximityOut;
996        break;
997    default:
998        break;
999    }
1000    return xitype;
1001}
1002
1003/**
1004 * Return the corresponding XI 2.x type for the given event or 0 if no
1005 * equivalent exists.
1006 */
1007int
1008GetXI2Type(enum EventType type)
1009{
1010    int xi2type = 0;
1011
1012    switch (type) {
1013    case ET_Motion:
1014        xi2type = XI_Motion;
1015        break;
1016    case ET_ButtonPress:
1017        xi2type = XI_ButtonPress;
1018        break;
1019    case ET_ButtonRelease:
1020        xi2type = XI_ButtonRelease;
1021        break;
1022    case ET_KeyPress:
1023        xi2type = XI_KeyPress;
1024        break;
1025    case ET_KeyRelease:
1026        xi2type = XI_KeyRelease;
1027        break;
1028    case ET_Enter:
1029        xi2type = XI_Enter;
1030        break;
1031    case ET_Leave:
1032        xi2type = XI_Leave;
1033        break;
1034    case ET_Hierarchy:
1035        xi2type = XI_HierarchyChanged;
1036        break;
1037    case ET_DeviceChanged:
1038        xi2type = XI_DeviceChanged;
1039        break;
1040    case ET_RawKeyPress:
1041        xi2type = XI_RawKeyPress;
1042        break;
1043    case ET_RawKeyRelease:
1044        xi2type = XI_RawKeyRelease;
1045        break;
1046    case ET_RawButtonPress:
1047        xi2type = XI_RawButtonPress;
1048        break;
1049    case ET_RawButtonRelease:
1050        xi2type = XI_RawButtonRelease;
1051        break;
1052    case ET_RawMotion:
1053        xi2type = XI_RawMotion;
1054        break;
1055    case ET_RawTouchBegin:
1056        xi2type = XI_RawTouchBegin;
1057        break;
1058    case ET_RawTouchUpdate:
1059        xi2type = XI_RawTouchUpdate;
1060        break;
1061    case ET_RawTouchEnd:
1062        xi2type = XI_RawTouchEnd;
1063        break;
1064    case ET_FocusIn:
1065        xi2type = XI_FocusIn;
1066        break;
1067    case ET_FocusOut:
1068        xi2type = XI_FocusOut;
1069        break;
1070    case ET_TouchBegin:
1071        xi2type = XI_TouchBegin;
1072        break;
1073    case ET_TouchEnd:
1074        xi2type = XI_TouchEnd;
1075        break;
1076    case ET_TouchUpdate:
1077        xi2type = XI_TouchUpdate;
1078        break;
1079    case ET_TouchOwnership:
1080        xi2type = XI_TouchOwnership;
1081        break;
1082    case ET_BarrierHit:
1083        xi2type = XI_BarrierHit;
1084        break;
1085    case ET_BarrierLeave:
1086        xi2type = XI_BarrierLeave;
1087        break;
1088    case ET_GesturePinchBegin:
1089        xi2type = XI_GesturePinchBegin;
1090        break;
1091    case ET_GesturePinchUpdate:
1092        xi2type = XI_GesturePinchUpdate;
1093        break;
1094    case ET_GesturePinchEnd:
1095        xi2type = XI_GesturePinchEnd;
1096        break;
1097    case ET_GestureSwipeBegin:
1098        xi2type = XI_GestureSwipeBegin;
1099        break;
1100    case ET_GestureSwipeUpdate:
1101        xi2type = XI_GestureSwipeUpdate;
1102        break;
1103    case ET_GestureSwipeEnd:
1104        xi2type = XI_GestureSwipeEnd;
1105        break;
1106    default:
1107        break;
1108    }
1109    return xi2type;
1110}
1111
1112/**
1113 * Converts a gesture type to corresponding Gesture{Pinch,Swipe}Begin.
1114 * Returns 0 if the input type is not a gesture.
1115 */
1116enum EventType
1117GestureTypeToBegin(enum EventType type)
1118{
1119    switch (type) {
1120    case ET_GesturePinchBegin:
1121    case ET_GesturePinchUpdate:
1122    case ET_GesturePinchEnd:
1123        return ET_GesturePinchBegin;
1124    case ET_GestureSwipeBegin:
1125    case ET_GestureSwipeUpdate:
1126    case ET_GestureSwipeEnd:
1127        return ET_GestureSwipeBegin;
1128    default:
1129        return 0;
1130    }
1131}
1132
1133/**
1134 * Converts a gesture type to corresponding Gesture{Pinch,Swipe}End.
1135 * Returns 0 if the input type is not a gesture.
1136 */
1137enum EventType
1138GestureTypeToEnd(enum EventType type)
1139{
1140    switch (type) {
1141    case ET_GesturePinchBegin:
1142    case ET_GesturePinchUpdate:
1143    case ET_GesturePinchEnd:
1144        return ET_GesturePinchEnd;
1145    case ET_GestureSwipeBegin:
1146    case ET_GestureSwipeUpdate:
1147    case ET_GestureSwipeEnd:
1148        return ET_GestureSwipeEnd;
1149    default:
1150        return 0;
1151    }
1152}
1153