2 * Copyright © 2009 Red Hat, Inc.
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:
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
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.
26 * @file eventconvert.c
27 * This file contains event conversion routines from InternalEvent to the
28 * matching protocol events.
31 #ifdef HAVE_DIX_CONFIG_H
32 #include <dix-config.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>
46 #include "exglobals.h"
47 #include "eventconvert.h"
48 #include "xiquerydevice.h"
52 static int countValuators(DeviceEvent *ev, int *first);
53 static int getValuatorEvents(DeviceEvent *ev, deviceValuator *xv);
54 static int eventToKeyButtonPointer(DeviceEvent *ev, xEvent **xi, int *count);
55 static int eventToDeviceChanged(DeviceChangedEvent *ev, xEvent **dcce);
56 static int eventToDeviceEvent(DeviceEvent *ev, xEvent **xi);
57 static int eventToRawEvent(RawDeviceEvent *ev, xEvent **xi);
59 /* Do not use, read comments below */
60 BOOL EventIsKeyRepeat(xEvent *event);
63 * Hack to allow detectable autorepeat for core and XI1 events.
64 * The sequence number is unused until we send to the client and can be
65 * misused to store data. More or less, anyway.
67 * Do not use this. It may change any time without warning, eat your babies
68 * and piss on your cat.
71 EventSetKeyRepeatFlag(xEvent *event, BOOL on)
73 event->u.u.sequenceNumber = on;
77 * Check if the event was marked as a repeat event before.
78 * NOTE: This is a nasty hack and should NOT be used by anyone else but
82 EventIsKeyRepeat(xEvent *event)
84 return !!event->u.u.sequenceNumber;
88 * Convert the given event to the respective core event.
91 * Success ... core contains the matching core event.
92 * BadValue .. One or more values in the internal event are invalid.
93 * BadMatch .. The event has no core equivalent.
95 * @param[in] event The event to convert into a core event.
96 * @param[in] core The memory location to store the core event at.
97 * @return Success or the matching error code.
100 EventToCore(InternalEvent *event, xEvent *core)
102 switch(event->any.type)
106 DeviceEvent *e = &event->device_event;
107 /* Don't create core motion event if neither x nor y are
109 if (!BitIsOn(e->valuators.mask, 0) &&
110 !BitIsOn(e->valuators.mask, 1))
115 case ET_ButtonRelease:
119 DeviceEvent *e = &event->device_event;
121 if (e->detail.key > 0xFF)
124 memset(core, 0, sizeof(xEvent));
125 core->u.u.type = e->type - ET_KeyPress + KeyPress;
126 core->u.u.detail = e->detail.key & 0xFF;
127 core->u.keyButtonPointer.time = e->time;
128 core->u.keyButtonPointer.rootX = e->root_x;
129 core->u.keyButtonPointer.rootY = e->root_y;
130 core->u.keyButtonPointer.state = e->corestate;
131 core->u.keyButtonPointer.root = e->root;
132 EventSetKeyRepeatFlag(core, (e->type == ET_KeyPress && e->key_repeat));
136 case ET_ProximityOut:
138 case ET_RawKeyRelease:
139 case ET_RawButtonPress:
140 case ET_RawButtonRelease:
145 ErrorF("[dix] EventToCore: Not implemented yet \n");
146 return BadImplementation;
152 * Convert the given event to the respective XI 1.x event and store it in
153 * xi. xi is allocated on demand and must be freed by the caller.
154 * count returns the number of events in xi. If count is 1, and the type of
155 * xi is GenericEvent, then xi may be larger than 32 bytes.
158 * Success ... core contains the matching core event.
159 * BadValue .. One or more values in the internal event are invalid.
160 * BadMatch .. The event has no XI equivalent.
162 * @param[in] ev The event to convert into an XI 1 event.
163 * @param[out] xi Future memory location for the XI event.
164 * @param[out] count Number of elements in xi.
166 * @return Success or the error code.
169 EventToXI(InternalEvent *ev, xEvent **xi, int *count)
171 switch (ev->any.type)
175 case ET_ButtonRelease:
179 case ET_ProximityOut:
180 return eventToKeyButtonPointer(&ev->device_event, xi, count);
181 case ET_DeviceChanged:
183 case ET_RawKeyRelease:
184 case ET_RawButtonPress:
185 case ET_RawButtonRelease:
194 ErrorF("[dix] EventToXI: Not implemented for %d \n", ev->any.type);
195 return BadImplementation;
199 * Convert the given event to the respective XI 2.x event and store it in xi.
200 * xi is allocated on demand and must be freed by the caller.
203 * Success ... core contains the matching core event.
204 * BadValue .. One or more values in the internal event are invalid.
205 * BadMatch .. The event has no XI2 equivalent.
207 * @param[in] ev The event to convert into an XI2 event
208 * @param[out] xi Future memory location for the XI2 event.
210 * @return Success or the error code.
213 EventToXI2(InternalEvent *ev, xEvent **xi)
215 switch (ev->any.type)
217 /* Enter/FocusIn are for grabs. We don't need an actual event, since
218 * the real events delivered are triggered elsewhere */
225 case ET_ButtonRelease:
228 return eventToDeviceEvent(&ev->device_event, xi);
230 case ET_ProximityOut:
233 case ET_DeviceChanged:
234 return eventToDeviceChanged(&ev->changed_event, xi);
236 case ET_RawKeyRelease:
237 case ET_RawButtonPress:
238 case ET_RawButtonRelease:
240 return eventToRawEvent(&ev->raw_event, xi);
245 ErrorF("[dix] EventToXI2: Not implemented for %d \n", ev->any.type);
246 return BadImplementation;
250 eventToKeyButtonPointer(DeviceEvent *ev, xEvent **xi, int *count)
253 int first; /* dummy */
254 deviceKeyButtonPointer *kbp;
256 /* Sorry, XI 1.x protocol restrictions. */
257 if (ev->detail.button > 0xFF || ev->deviceid >= 0x80)
263 num_events = (countValuators(ev, &first) + 5)/6; /* valuator ev */
264 num_events++; /* the actual event event */
266 *xi = calloc(num_events, sizeof(xEvent));
272 kbp = (deviceKeyButtonPointer*)(*xi);
273 kbp->detail = ev->detail.button;
274 kbp->time = ev->time;
275 kbp->root = ev->root;
276 kbp->root_x = ev->root_x;
277 kbp->root_y = ev->root_y;
278 kbp->deviceid = ev->deviceid;
279 kbp->state = ev->corestate;
280 EventSetKeyRepeatFlag((xEvent*)kbp,
281 (ev->type == ET_KeyPress && ev->key_repeat));
284 kbp->deviceid |= MORE_EVENTS;
288 case ET_Motion: kbp->type = DeviceMotionNotify; break;
289 case ET_ButtonPress: kbp->type = DeviceButtonPress; break;
290 case ET_ButtonRelease: kbp->type = DeviceButtonRelease; break;
291 case ET_KeyPress: kbp->type = DeviceKeyPress; break;
292 case ET_KeyRelease: kbp->type = DeviceKeyRelease; break;
293 case ET_ProximityIn: kbp->type = ProximityIn; break;
294 case ET_ProximityOut: kbp->type = ProximityOut; break;
301 getValuatorEvents(ev, (deviceValuator*)(kbp + 1));
310 * Set first to the first valuator in the event ev and return the number of
311 * valuators from first to the last set valuator.
314 countValuators(DeviceEvent *ev, int *first)
316 int first_valuator = -1, last_valuator = -1, num_valuators = 0;
319 for (i = 0; i < sizeof(ev->valuators.mask) * 8; i++)
321 if (BitIsOn(ev->valuators.mask, i))
323 if (first_valuator == -1)
329 if (first_valuator != -1)
331 num_valuators = last_valuator - first_valuator + 1;
332 *first = first_valuator;
335 return num_valuators;
339 getValuatorEvents(DeviceEvent *ev, deviceValuator *xv)
343 int first_valuator, num_valuators;
346 num_valuators = countValuators(ev, &first_valuator);
347 if (num_valuators > 0)
349 DeviceIntPtr dev = NULL;
350 dixLookupDevice(&dev, ev->deviceid, serverClient, DixUseAccess);
351 /* State needs to be assembled BEFORE the device is updated. */
352 state = (dev && dev->key) ? XkbStateFieldFromRec(&dev->key->xkbInfo->state) : 0;
353 state |= (dev && dev->button) ? (dev->button->state) : 0;
356 /* FIXME: non-continuous valuator data in internal events*/
357 for (i = 0; i < num_valuators; i += 6, xv++) {
358 xv->type = DeviceValuator;
359 xv->first_valuator = first_valuator + i;
360 xv->num_valuators = ((num_valuators - i) > 6) ? 6 : (num_valuators - i);
361 xv->deviceid = ev->deviceid;
362 xv->device_state = state;
363 switch (xv->num_valuators) {
365 xv->valuator5 = ev->valuators.data[xv->first_valuator + 5];
367 xv->valuator4 = ev->valuators.data[xv->first_valuator + 4];
369 xv->valuator3 = ev->valuators.data[xv->first_valuator + 3];
371 xv->valuator2 = ev->valuators.data[xv->first_valuator + 2];
373 xv->valuator1 = ev->valuators.data[xv->first_valuator + 1];
375 xv->valuator0 = ev->valuators.data[xv->first_valuator + 0];
378 if (i + 6 < num_valuators)
379 xv->deviceid |= MORE_EVENTS;
382 return (num_valuators + 5) / 6;
387 appendKeyInfo(DeviceChangedEvent *dce, xXIKeyInfo* info)
392 info->type = XIKeyClass;
393 info->num_keycodes = dce->keys.max_keycode - dce->keys.min_keycode + 1;
394 info->length = sizeof(xXIKeyInfo)/4 + info->num_keycodes;
395 info->sourceid = dce->sourceid;
397 kc = (uint32_t*)&info[1];
398 for (i = 0; i < info->num_keycodes; i++)
399 *kc++ = i + dce->keys.min_keycode;
401 return info->length * 4;
405 appendButtonInfo(DeviceChangedEvent *dce, xXIButtonInfo *info)
410 mask_len = bytes_to_int32(bits_to_bytes(dce->buttons.num_buttons));
412 info->type = XIButtonClass;
413 info->num_buttons = dce->buttons.num_buttons;
414 info->length = bytes_to_int32(sizeof(xXIButtonInfo)) +
415 info->num_buttons + mask_len;
416 info->sourceid = dce->sourceid;
418 bits = (unsigned char*)&info[1];
419 memset(bits, 0, mask_len * 4);
420 /* FIXME: is_down? */
422 bits += mask_len * 4;
423 memcpy(bits, dce->buttons.names, dce->buttons.num_buttons * sizeof(Atom));
425 return info->length * 4;
429 appendValuatorInfo(DeviceChangedEvent *dce, xXIValuatorInfo *info, int axisnumber)
431 info->type = XIValuatorClass;
432 info->length = sizeof(xXIValuatorInfo)/4;
433 info->label = dce->valuators[axisnumber].name;
434 info->min.integral = dce->valuators[axisnumber].min;
436 info->max.integral = dce->valuators[axisnumber].max;
439 info->value.integral = 0;
440 info->value.frac = 0;
441 info->resolution = dce->valuators[axisnumber].resolution;
442 info->number = axisnumber;
443 info->mode = dce->valuators[axisnumber].mode; /* Server doesn't have per-axis mode yet */
444 info->sourceid = dce->sourceid;
446 return info->length * 4;
450 eventToDeviceChanged(DeviceChangedEvent *dce, xEvent **xi)
452 xXIDeviceChangedEvent *dcce;
453 int len = sizeof(xXIDeviceChangedEvent);
457 if (dce->buttons.num_buttons)
459 len += sizeof(xXIButtonInfo);
460 len += dce->buttons.num_buttons * sizeof(Atom); /* button names */
461 len += pad_to_int32(bits_to_bytes(dce->buttons.num_buttons));
463 if (dce->num_valuators)
464 len += sizeof(xXIValuatorInfo) * dce->num_valuators;
466 nkeys = (dce->keys.max_keycode > 0) ?
467 dce->keys.max_keycode - dce->keys.min_keycode + 1 : 0;
470 len += sizeof(xXIKeyInfo);
471 len += sizeof(CARD32) * nkeys; /* keycodes */
474 dcce = calloc(1, len);
477 ErrorF("[Xi] BadAlloc in SendDeviceChangedEvent.\n");
481 dcce->type = GenericEvent;
482 dcce->extension = IReqCode;
483 dcce->evtype = XI_DeviceChanged;
484 dcce->time = dce->time;
485 dcce->deviceid = dce->deviceid;
486 dcce->sourceid = dce->sourceid;
487 dcce->reason = (dce->flags & DEVCHANGE_DEVICE_CHANGE) ? XIDeviceChange : XISlaveSwitch;
488 dcce->num_classes = 0;
489 dcce->length = bytes_to_int32(len - sizeof(xEvent));
491 ptr = (char*)&dcce[1];
492 if (dce->buttons.num_buttons)
495 ptr += appendButtonInfo(dce, (xXIButtonInfo*)ptr);
501 ptr += appendKeyInfo(dce, (xXIKeyInfo*)ptr);
504 if (dce->num_valuators)
508 dcce->num_classes += dce->num_valuators;
509 for (i = 0; i < dce->num_valuators; i++)
510 ptr += appendValuatorInfo(dce, (xXIValuatorInfo*)ptr, i);
518 static int count_bits(unsigned char* ptr, int len)
524 for (i = 0; i < len; i++)
537 eventToDeviceEvent(DeviceEvent *ev, xEvent **xi)
539 int len = sizeof(xXIDeviceEvent);
541 int i, btlen, vallen;
545 /* FIXME: this should just send the buttons we have, not MAX_BUTTONs. Same
546 * with MAX_VALUATORS below */
547 /* btlen is in 4 byte units */
548 btlen = bytes_to_int32(bits_to_bytes(MAX_BUTTONS));
549 len += btlen * 4; /* buttonmask len */
552 vallen = count_bits(ev->valuators.mask, sizeof(ev->valuators.mask)/sizeof(ev->valuators.mask[0]));
553 len += vallen * 2 * sizeof(uint32_t); /* axisvalues */
554 vallen = bytes_to_int32(bits_to_bytes(MAX_VALUATORS));
555 len += vallen * 4; /* valuators mask */
557 *xi = calloc(1, len);
558 xde = (xXIDeviceEvent*)*xi;
559 xde->type = GenericEvent;
560 xde->extension = IReqCode;
561 xde->evtype = GetXI2Type((InternalEvent*)ev);
562 xde->time = ev->time;
563 xde->length = bytes_to_int32(len - sizeof(xEvent));
564 xde->detail = ev->detail.button;
565 xde->root = ev->root;
566 xde->buttons_len = btlen;
567 xde->valuators_len = vallen;
568 xde->deviceid = ev->deviceid;
569 xde->sourceid = ev->sourceid;
570 xde->root_x = FP1616(ev->root_x, ev->root_x_frac);
571 xde->root_y = FP1616(ev->root_y, ev->root_y_frac);
574 xde->flags |= XIKeyRepeat;
576 xde->mods.base_mods = ev->mods.base;
577 xde->mods.latched_mods = ev->mods.latched;
578 xde->mods.locked_mods = ev->mods.locked;
579 xde->mods.effective_mods = ev->mods.effective;
581 xde->group.base_group = ev->group.base;
582 xde->group.latched_group = ev->group.latched;
583 xde->group.locked_group = ev->group.locked;
584 xde->group.effective_group = ev->group.effective;
586 ptr = (char*)&xde[1];
587 for (i = 0; i < sizeof(ev->buttons) * 8; i++)
589 if (BitIsOn(ev->buttons, i))
593 ptr += xde->buttons_len * 4;
594 axisval = (FP3232*)(ptr + xde->valuators_len * 4);
595 for (i = 0; i < sizeof(ev->valuators.mask) * 8; i++)
597 if (BitIsOn(ev->valuators.mask, i))
600 axisval->integral = ev->valuators.data[i];
601 axisval->frac = ev->valuators.data_frac[i];
610 eventToRawEvent(RawDeviceEvent *ev, xEvent **xi)
614 int i, len = sizeof(xXIRawEvent);
618 nvals = count_bits(ev->valuators.mask, sizeof(ev->valuators.mask));
619 len += nvals * sizeof(FP3232) * 2; /* 8 byte per valuator, once
620 raw, once processed */
621 vallen = bytes_to_int32(bits_to_bytes(MAX_VALUATORS));
622 len += vallen * 4; /* valuators mask */
624 *xi = calloc(1, len);
625 raw = (xXIRawEvent*)*xi;
626 raw->type = GenericEvent;
627 raw->extension = IReqCode;
628 raw->evtype = GetXI2Type((InternalEvent*)ev);
629 raw->time = ev->time;
630 raw->length = bytes_to_int32(len - sizeof(xEvent));
631 raw->detail = ev->detail.button;
632 raw->deviceid = ev->deviceid;
633 raw->valuators_len = vallen;
635 ptr = (char*)&raw[1];
636 axisval = (FP3232*)(ptr + raw->valuators_len * 4);
637 for (i = 0; i < sizeof(ev->valuators.mask) * 8; i++)
639 if (BitIsOn(ev->valuators.mask, i))
642 axisval->integral = ev->valuators.data[i];
643 axisval->frac = ev->valuators.data_frac[i];
644 (axisval + nvals)->integral = ev->valuators.data_raw[i];
645 (axisval + nvals)->frac = ev->valuators.data_raw_frac[i];
654 * Return the corresponding core type for the given event or 0 if no core
658 GetCoreType(InternalEvent *event)
661 switch(event->any.type)
663 case ET_Motion: coretype = MotionNotify; break;
664 case ET_ButtonPress: coretype = ButtonPress; break;
665 case ET_ButtonRelease: coretype = ButtonRelease; break;
666 case ET_KeyPress: coretype = KeyPress; break;
667 case ET_KeyRelease: coretype = KeyRelease; break;
675 * Return the corresponding XI 1.x type for the given event or 0 if no
679 GetXIType(InternalEvent *event)
682 switch(event->any.type)
684 case ET_Motion: xitype = DeviceMotionNotify; break;
685 case ET_ButtonPress: xitype = DeviceButtonPress; break;
686 case ET_ButtonRelease: xitype = DeviceButtonRelease; break;
687 case ET_KeyPress: xitype = DeviceKeyPress; break;
688 case ET_KeyRelease: xitype = DeviceKeyRelease; break;
689 case ET_ProximityIn: xitype = ProximityIn; break;
690 case ET_ProximityOut: xitype = ProximityOut; break;
698 * Return the corresponding XI 2.x type for the given event or 0 if no
702 GetXI2Type(InternalEvent *event)
706 switch(event->any.type)
708 case ET_Motion: xi2type = XI_Motion; break;
709 case ET_ButtonPress: xi2type = XI_ButtonPress; break;
710 case ET_ButtonRelease: xi2type = XI_ButtonRelease; break;
711 case ET_KeyPress: xi2type = XI_KeyPress; break;
712 case ET_KeyRelease: xi2type = XI_KeyRelease; break;
713 case ET_Enter: xi2type = XI_Enter; break;
714 case ET_Leave: xi2type = XI_Leave; break;
715 case ET_Hierarchy: xi2type = XI_HierarchyChanged; break;
716 case ET_DeviceChanged: xi2type = XI_DeviceChanged; break;
717 case ET_RawKeyPress: xi2type = XI_RawKeyPress; break;
718 case ET_RawKeyRelease: xi2type = XI_RawKeyRelease; break;
719 case ET_RawButtonPress: xi2type = XI_RawButtonPress; break;
720 case ET_RawButtonRelease: xi2type = XI_RawButtonRelease; break;
721 case ET_RawMotion: xi2type = XI_RawMotion; break;
722 case ET_FocusIn: xi2type = XI_FocusIn; break;
723 case ET_FocusOut: xi2type = XI_FocusOut; break;