1 /* AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2003 Sun Microsystems Inc.,
6 * Copyright 2001, 2002 Ximian, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 /* deviceeventcontroller-x11.c: X-specific DeviceEventController support */
30 #undef SPI_KEYEVENT_DEBUG
38 #include <X11/Xutil.h>
39 #include <X11/extensions/XTest.h>
40 #include <X11/XKBlib.h>
44 #include <X11/keysymdef.h>
47 #include <X11/Xproto.h>
49 #include <X11/extensions/Xevie.h>
50 #endif /* HAVE_XEVIE */
54 #include <dbus/dbus.h>
59 #include "de-marshaller.h"
61 #include "event-source.h"
63 #include "deviceeventcontroller.h"
64 #include "reentrant-list.h"
66 #include "introspection.h"
68 static void spi_dec_x11_emit_modifier_event (SpiDEController *controller,
72 #define CHECK_RELEASE_DELAY 20
73 #define BIT(c, x) (c[x/8]&(1<<(x%8)))
74 static guint check_release_handler = 0;
75 static Accessibility_DeviceEvent pressed_event;
76 static void wait_for_release_event (XEvent *event, SpiDEController *controller);
78 static int spi_error_code = 0;
83 typedef struct _SpiPoint SpiPoint;
84 static SpiPoint last_mouse_pos_static = {0, 0};
85 static SpiPoint *last_mouse_pos = &last_mouse_pos_static;
86 static unsigned int mouse_mask_state = 0;
87 static unsigned int mouse_button_mask =
88 Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask;
89 static unsigned int key_modifier_mask =
90 Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask | ShiftMask | LockMask | ControlMask | SPI_KEYMASK_NUMLOCK;
91 static unsigned int _numlock_physical_mask = Mod2Mask; /* a guess, will be reset */
93 static XModifierKeymap* xmkeymap = NULL;
96 static int (*x_default_error_handler) (Display *display, XErrorEvent *error_event);
99 Display *xevie_display;
100 unsigned int last_press_keycode;
101 unsigned int last_release_keycode;
102 struct timeval last_press_time;
103 struct timeval last_release_time;
105 int xkb_major_extension_opcode;
106 int xkb_base_event_code;
107 int xkb_base_error_code;
108 unsigned int xkb_latch_mask;
109 unsigned int pending_xkb_mod_relatch_mask;
111 KeyCode reserved_keycode;
112 KeySym reserved_keysym;
113 guint reserved_reset_timeout;
114 } DEControllerPrivateData;
116 static void spi_controller_register_with_devices (SpiDEController *controller);
117 static gboolean spi_device_event_controller_forward_key_event (SpiDEController *controller,
118 const XEvent *event);
121 static SpiDEController *saved_controller;
124 keysym_mod_mask (KeySym keysym, KeyCode keycode)
126 /* we really should use XKB and look directly at the keymap */
127 /* this is very inelegant */
128 Display *display = spi_get_display ();
129 unsigned int mods_rtn = 0;
130 unsigned int retval = 0;
133 if (XkbLookupKeySym (display, keycode, 0, &mods_rtn, &sym_rtn) &&
134 (sym_rtn == keysym)) {
137 else if (XkbLookupKeySym (display, keycode, ShiftMask, &mods_rtn, &sym_rtn) &&
138 (sym_rtn == keysym)) {
141 else if (XkbLookupKeySym (display, keycode, Mod2Mask, &mods_rtn, &sym_rtn) &&
142 (sym_rtn == keysym)) {
145 else if (XkbLookupKeySym (display, keycode, Mod3Mask, &mods_rtn, &sym_rtn) &&
146 (sym_rtn == keysym)) {
149 else if (XkbLookupKeySym (display, keycode,
150 ShiftMask | Mod2Mask, &mods_rtn, &sym_rtn) &&
151 (sym_rtn == keysym)) {
152 retval = (Mod2Mask | ShiftMask);
154 else if (XkbLookupKeySym (display, keycode,
155 ShiftMask | Mod3Mask, &mods_rtn, &sym_rtn) &&
156 (sym_rtn == keysym)) {
157 retval = (Mod3Mask | ShiftMask);
159 else if (XkbLookupKeySym (display, keycode,
160 ShiftMask | Mod4Mask, &mods_rtn, &sym_rtn) &&
161 (sym_rtn == keysym)) {
162 retval = (Mod4Mask | ShiftMask);
170 replace_map_keysym (DEControllerPrivateData *priv, KeyCode keycode, KeySym keysym)
173 Display *dpy = spi_get_display ();
175 if (!(desc = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd)))
177 fprintf (stderr, "ERROR getting map\n");
181 if (desc && desc->map)
183 gint offset = desc->map->key_sym_map[keycode].offset;
184 desc->map->syms[offset] = keysym;
188 fprintf (stderr, "Error changing key map: empty server structure\n");
190 XkbSetMap (dpy, XkbAllMapComponentsMask, desc);
192 * FIXME: the use of XkbChangeMap, and the reuse of the priv->xkb_desc structure,
193 * would be far preferable.
194 * HOWEVER it does not seem to work using XFree 4.3.
196 /* XkbChangeMap (dpy, priv->xkb_desc, priv->changes); */
199 XkbFreeKeyboard (desc, 0, TRUE);
208 spi_dec_reset_reserved (gpointer data)
210 DEControllerPrivateData *priv = data;
211 replace_map_keysym (priv, priv->reserved_keycode, priv->reserved_keysym);
212 priv->reserved_reset_timeout = 0;
217 spi_dec_x11_get_keycode (SpiDEController *controller,
224 if (key_str && key_str[0])
225 keysym = XStringToKeysym(key_str);
226 keycode = XKeysymToKeycode (spi_get_display (), (KeySym) keysym);
229 DEControllerPrivateData *priv = controller->priv;
230 /* if there's no keycode available, fix it */
231 if (replace_map_keysym (priv, priv->reserved_keycode, keysym))
233 keycode = priv->reserved_keycode;
235 * queue a timer to restore the old keycode. Ugly, but required
236 * due to races / asynchronous X delivery.
237 * Long-term fix is to extend the X keymap here instead of replace entries.
239 priv->reserved_reset_timeout = g_timeout_add (500, spi_dec_reset_reserved, priv);
246 *modmask = keysym_mod_mask (keysym, keycode);
251 spi_dec_set_unlatch_pending (SpiDEController *controller, unsigned mask)
253 DEControllerPrivateData *priv = controller->priv;
255 if (priv->xkb_latch_mask) fprintf (stderr, "unlatch pending! %x\n",
256 priv->xkb_latch_mask);
258 priv->pending_xkb_mod_relatch_mask |= priv->xkb_latch_mask;
262 spi_dec_button_update_and_emit (SpiDEController *controller,
265 Accessibility_DeviceEvent mouse_e;
266 gchar event_detail[3];
267 gboolean is_consumed = FALSE;
269 if ((mask_return & mouse_button_mask) !=
270 (mouse_mask_state & mouse_button_mask))
272 int button_number = 0;
273 gboolean is_down = False;
275 if (!(mask_return & Button1Mask) &&
276 (mouse_mask_state & Button1Mask))
278 mouse_mask_state &= ~Button1Mask;
281 else if ((mask_return & Button1Mask) &&
282 !(mouse_mask_state & Button1Mask))
284 mouse_mask_state |= Button1Mask;
288 else if (!(mask_return & Button2Mask) &&
289 (mouse_mask_state & Button2Mask))
291 mouse_mask_state &= ~Button2Mask;
294 else if ((mask_return & Button2Mask) &&
295 !(mouse_mask_state & Button2Mask))
297 mouse_mask_state |= Button2Mask;
301 else if (!(mask_return & Button3Mask) &&
302 (mouse_mask_state & Button3Mask))
304 mouse_mask_state &= ~Button3Mask;
307 else if ((mask_return & Button3Mask) &&
308 !(mouse_mask_state & Button3Mask))
310 mouse_mask_state |= Button3Mask;
314 else if (!(mask_return & Button4Mask) &&
315 (mouse_mask_state & Button4Mask))
317 mouse_mask_state &= ~Button4Mask;
320 else if ((mask_return & Button4Mask) &&
321 !(mouse_mask_state & Button4Mask))
323 mouse_mask_state |= Button4Mask;
327 else if (!(mask_return & Button5Mask) &&
328 (mouse_mask_state & Button5Mask))
330 mouse_mask_state &= ~Button5Mask;
333 else if ((mask_return & Button5Mask) &&
334 !(mouse_mask_state & Button5Mask))
336 mouse_mask_state |= Button5Mask;
342 fprintf (stderr, "Button %d %s\n",
343 button_number, (is_down) ? "Pressed" : "Released");
345 snprintf (event_detail, 3, "%d%c", button_number,
346 (is_down) ? 'p' : 'r');
347 /* TODO: FIXME distinguish between physical and
350 mouse_e.type = (is_down) ?
351 Accessibility_BUTTON_PRESSED_EVENT :
352 Accessibility_BUTTON_RELEASED_EVENT;
353 mouse_e.id = button_number;
354 mouse_e.hw_code = button_number;
355 mouse_e.modifiers = (dbus_uint16_t) mouse_mask_state;
356 mouse_e.timestamp = 0;
357 mouse_e.event_string = "";
358 mouse_e.is_text = FALSE;
360 spi_controller_notify_mouselisteners (controller,
364 dbus_uint32_t x = last_mouse_pos->x, y = last_mouse_pos->y;
365 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Button", event_detail, x, y);
368 spi_dec_set_unlatch_pending (controller, mask_return);
379 spi_dec_x11_mouse_check (SpiDEController *controller,
380 int *x, int *y, gboolean *moved)
382 int win_x_return,win_y_return;
383 unsigned int mask_return;
384 Window root_return, child_return;
385 Display *display = spi_get_display ();
388 XQueryPointer(display, DefaultRootWindow (display),
389 &root_return, &child_return,
391 &win_x_return, &win_y_return, &mask_return);
393 * Since many clients grab the pointer, and X goes an automatic
394 * pointer grab on mouse-down, we often must detect mouse button events
395 * by polling rather than via a button grab.
396 * The while loop (rather than if) is used since it's possible that
397 * multiple buttons have changed state since we last checked.
399 if (mask_return != mouse_mask_state)
401 while (spi_dec_button_update_and_emit (controller, mask_return));
404 if (*x != last_mouse_pos->x || *y != last_mouse_pos->y)
406 // TODO: combine these two signals?
407 dbus_uint32_t ix = *x, iy = *y;
408 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Abs", "", ix, iy);
409 ix -= last_mouse_pos->x;
410 iy -= last_mouse_pos->y;
411 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Rel", "", ix, iy);
412 last_mouse_pos->x = *x;
413 last_mouse_pos->y = *y;
424 #ifdef WE_NEED_UGRAB_MOUSE
426 spi_dec_ungrab_mouse (gpointer data)
428 Display *display = spi_get_display ();
431 XUngrabButton (spi_get_display (), AnyButton, AnyModifier,
432 XDefaultRootWindow (spi_get_display ()));
439 spi_dec_init_mouse_listener (SpiDEController *dec)
442 Display *display = spi_get_display ();
446 if (XGrabButton (display, AnyButton, AnyModifier,
447 spi_get_root_window (),
448 True, ButtonPressMask | ButtonReleaseMask,
449 GrabModeSync, GrabModeAsync, None, None) != Success) {
451 fprintf (stderr, "WARNING: could not grab mouse buttons!\n");
455 XSync (display, False);
457 fprintf (stderr, "mouse buttons grabbed\n");
464 spi_dec_clear_unlatch_pending (SpiDEController *controller)
466 DEControllerPrivateData *priv = controller->priv;
467 priv->xkb_latch_mask = 0;
471 spi_device_event_controller_forward_mouse_event (SpiDEController *controller,
474 Accessibility_DeviceEvent mouse_e;
475 gchar event_detail[3];
476 gboolean is_consumed = FALSE;
477 gboolean xkb_mod_unlatch_occurred;
478 XButtonEvent *xbutton_event = (XButtonEvent *) xevent;
479 dbus_uint32_t ix, iy;
481 int button = xbutton_event->button;
483 unsigned int mouse_button_state = xbutton_event->state;
488 mouse_button_state |= Button1Mask;
491 mouse_button_state |= Button2Mask;
494 mouse_button_state |= Button3Mask;
497 mouse_button_state |= Button4Mask;
500 mouse_button_state |= Button5Mask;
504 last_mouse_pos->x = ((XButtonEvent *) xevent)->x_root;
505 last_mouse_pos->y = ((XButtonEvent *) xevent)->y_root;
508 fprintf (stderr, "mouse button %d %s (%x)\n",
509 xbutton_event->button,
510 (xevent->type == ButtonPress) ? "Press" : "Release",
513 snprintf (event_detail, 3, "%d%c", button,
514 (xevent->type == ButtonPress) ? 'p' : 'r');
516 /* TODO: FIXME distinguish between physical and logical buttons */
517 mouse_e.type = (xevent->type == ButtonPress) ?
518 Accessibility_BUTTON_PRESSED_EVENT :
519 Accessibility_BUTTON_RELEASED_EVENT;
521 mouse_e.hw_code = button;
522 mouse_e.modifiers = (dbus_uint16_t) xbutton_event->state;
523 mouse_e.timestamp = (dbus_uint32_t) xbutton_event->time;
524 mouse_e.event_string = "";
525 mouse_e.is_text = FALSE;
526 if ((mouse_button_state & mouse_button_mask) !=
527 (mouse_mask_state & mouse_button_mask))
529 if ((mouse_mask_state & key_modifier_mask) !=
530 (mouse_button_state & key_modifier_mask))
531 spi_dec_x11_emit_modifier_event (controller,
532 mouse_mask_state, mouse_button_state);
533 mouse_mask_state = mouse_button_state;
535 spi_controller_notify_mouselisteners (controller, &mouse_e);
536 ix = last_mouse_pos->x;
537 iy = last_mouse_pos->y;
538 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Button", event_detail, ix, iy);
541 xkb_mod_unlatch_occurred = (xevent->type == ButtonPress ||
542 xevent->type == ButtonRelease);
544 /* if client wants to consume this event, and XKB latch state was
545 * unset by this button event, we reset it
547 if (is_consumed && xkb_mod_unlatch_occurred)
548 spi_dec_set_unlatch_pending (controller, mouse_mask_state);
550 XAllowEvents (spi_get_display (),
551 (is_consumed) ? SyncPointer : ReplayPointer,
556 global_filter_fn (XEvent *xevent, void *data)
558 SpiDEController *controller;
559 DEControllerPrivateData *priv;
560 Display *display = spi_get_display ();
561 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
562 priv = controller->priv;
564 if (xevent->type == MappingNotify)
567 if (xevent->type == KeyPress || xevent->type == KeyRelease)
569 if (priv->xevie_display == NULL)
571 gboolean is_consumed;
574 spi_device_event_controller_forward_key_event (controller, xevent);
581 n_events = XPending (display);
583 #ifdef SPI_KEYEVENT_DEBUG
584 g_print ("Number of events pending: %d\n", n_events);
586 for (i = 0; i < n_events; i++)
588 XNextEvent (display, &next_event);
589 if (next_event.type != KeyPress &&
590 next_event.type != KeyRelease)
591 g_warning ("Unexpected event type %d in queue", next_event.type);
594 XAllowEvents (display, AsyncKeyboard, CurrentTime);
596 XUngrabKeyboard (display, CurrentTime);
600 if (xevent->type == KeyPress)
601 wait_for_release_event (xevent, controller);
602 XAllowEvents (display, ReplayKeyboard, CurrentTime);
608 if (xevent->type == ButtonPress || xevent->type == ButtonRelease)
610 spi_device_event_controller_forward_mouse_event (controller, xevent);
612 if (xevent->type == priv->xkb_base_event_code)
614 XkbAnyEvent * xkb_ev = (XkbAnyEvent *) xevent;
615 /* ugly but probably necessary...*/
616 XSynchronize (display, TRUE);
618 if (xkb_ev->xkb_type == XkbStateNotify)
620 XkbStateNotifyEvent *xkb_snev =
621 (XkbStateNotifyEvent *) xkb_ev;
622 /* check the mouse, to catch mouse events grabbed by
623 * another client; in case we should revert this XKB delatch
625 if (!priv->pending_xkb_mod_relatch_mask)
629 spi_dec_x11_mouse_check (controller, &x, &y, &moved);
631 /* we check again, since the previous call may have
633 if (priv->pending_xkb_mod_relatch_mask)
635 unsigned int feedback_mask;
637 fprintf (stderr, "relatching %x\n",
638 priv->pending_xkb_mod_relatch_mask);
640 /* temporarily turn off the latch bell, if it's on */
641 XkbGetControls (display,
642 XkbAccessXFeedbackMask,
644 feedback_mask = priv->xkb_desc->ctrls->ax_options;
645 if (feedback_mask & XkbAX_StickyKeysFBMask)
647 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
649 priv->xkb_desc->ctrls->ax_options
650 &= ~(XkbAX_StickyKeysFBMask);
651 XkbChangeControls (display, priv->xkb_desc, &changes);
653 /* TODO: account for lock as well as latch */
654 XkbLatchModifiers (display,
656 priv->pending_xkb_mod_relatch_mask,
657 priv->pending_xkb_mod_relatch_mask);
658 if (feedback_mask & XkbAX_StickyKeysFBMask)
660 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
662 priv->xkb_desc->ctrls->ax_options = feedback_mask;
663 XkbChangeControls (display, priv->xkb_desc, &changes);
666 fprintf (stderr, "relatched %x\n",
667 priv->pending_xkb_mod_relatch_mask);
669 priv->pending_xkb_mod_relatch_mask = 0;
673 priv->xkb_latch_mask = xkb_snev->latched_mods;
676 XSynchronize (display, FALSE);
683 _spi_controller_device_error_handler (Display *display, XErrorEvent *error)
685 if (error->error_code == BadAccess)
687 g_message ("Could not complete key grab: grab already in use.\n");
688 spi_error_code = BadAccess;
693 return (*x_default_error_handler) (display, error);
698 spi_controller_register_with_devices (SpiDEController *controller)
700 DEControllerPrivateData *priv;
701 int event_base, error_base, major_version, minor_version;
703 priv = controller->priv;
704 if (XTestQueryExtension (spi_get_display(), &event_base, &error_base, &major_version, &minor_version))
706 XTestGrabControl (spi_get_display (), True);
709 /* calls to device-specific implementations and routines go here */
710 /* register with: keyboard hardware code handler */
711 /* register with: (translated) keystroke handler */
713 priv->have_xkb = XkbQueryExtension (spi_get_display (),
714 &priv->xkb_major_extension_opcode,
715 &priv->xkb_base_event_code,
716 &priv->xkb_base_error_code, NULL, NULL);
720 guint64 reserved = 0;
721 priv->xkb_desc = XkbGetMap (spi_get_display (), XkbKeySymsMask, XkbUseCoreKbd);
722 XkbSelectEvents (spi_get_display (),
724 XkbStateNotifyMask, XkbStateNotifyMask);
725 _numlock_physical_mask = XkbKeysymToModifiers (spi_get_display (),
727 for (i = priv->xkb_desc->max_key_code; i >= priv->xkb_desc->min_key_code; --i)
729 if (priv->xkb_desc->map->key_sym_map[i].kt_index[0] == XkbOneLevelIndex)
731 if (XkbKeycodeToKeysym (spi_get_display (), i, 0, 0) != 0)
733 /* don't use this one if there's a grab client! */
735 /* Runtime errors are generated from these functions,
736 * that are then quashed. Equivalent to:
744 XGrabKey (spi_get_display (), i, 0,
745 spi_get_root_window (),
747 GrabModeSync, GrabModeSync);
748 XSync (spi_get_display (), TRUE);
749 XUngrabKey (spi_get_display (), i, 0,
750 spi_get_root_window ());
751 if (!spi_x_error_release ())
761 priv->reserved_keycode = reserved;
762 priv->reserved_keysym = XkbKeycodeToKeysym (spi_get_display (), reserved, 0, 0);
766 priv->reserved_keycode = XKeysymToKeycode (spi_get_display (), XK_numbersign);
767 priv->reserved_keysym = XK_numbersign;
769 #ifdef SPI_RESERVED_DEBUG
771 sym = XKeycodeToKeysym (spi_get_display (), reserved, 0);
772 fprintf (stderr, "%x\n", sym);
773 fprintf (stderr, "setting the reserved keycode to %d (%s)\n",
775 XKeysymToString (XKeycodeToKeysym (spi_get_display (),
780 spi_set_filter (global_filter_fn, controller);
781 spi_set_events (KeyPressMask | KeyReleaseMask);
783 x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
786 static Accessibility_DeviceEvent
787 spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
789 Accessibility_DeviceEvent key_event;
791 const int cbuf_bytes = 20;
795 nbytes = XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL);
796 key_event.id = (dbus_int32_t)(keysym);
797 key_event.hw_code = (dbus_int16_t) x_key_event->keycode;
798 if (((XEvent *) x_key_event)->type == KeyPress)
800 key_event.type = Accessibility_KEY_PRESSED_EVENT;
804 key_event.type = Accessibility_KEY_RELEASED_EVENT;
806 key_event.modifiers = (dbus_uint16_t)(x_key_event->state);
807 key_event.is_text = FALSE;
811 key_event.event_string = g_strdup ("space");
814 key_event.event_string = g_strdup ("Tab");
817 key_event.event_string = g_strdup ("Backspace");
820 key_event.event_string = g_strdup ("Return");
823 key_event.event_string = g_strdup ("Home");
826 key_event.event_string = g_strdup ("Page_Down");
829 key_event.event_string = g_strdup ("Page_Up");
832 key_event.event_string = g_strdup ("F1");
835 key_event.event_string = g_strdup ("F2");
838 key_event.event_string = g_strdup ("F3");
841 key_event.event_string = g_strdup ("F4");
844 key_event.event_string = g_strdup ("F5");
847 key_event.event_string = g_strdup ("F6");
850 key_event.event_string = g_strdup ("F7");
853 key_event.event_string = g_strdup ("F8");
856 key_event.event_string = g_strdup ("F9");
859 key_event.event_string = g_strdup ("F10");
862 key_event.event_string = g_strdup ("F11");
865 key_event.event_string = g_strdup ("F12");
868 key_event.event_string = g_strdup ("End");
871 key_event.event_string = g_strdup ("Escape");
874 key_event.event_string = g_strdup ("Up");
877 key_event.event_string = g_strdup ("Down");
880 key_event.event_string = g_strdup ("Left");
883 key_event.event_string = g_strdup ("Right");
889 cbuf[nbytes] = '\0'; /* OK since length is cbuf_bytes+1 */
890 key_event.event_string = g_strdup (cbuf);
891 c = keysym2ucs (keysym);
892 if (c > 0 && !g_unichar_iscntrl (c))
894 key_event.is_text = TRUE;
895 /* incorrect for some composed chars? */
900 key_event.event_string = g_strdup ("");
904 key_event.timestamp = (dbus_uint32_t) x_key_event->time;
905 #ifdef SPI_KEYEVENT_DEBUG
907 char *pressed_str = "pressed";
908 char *released_str = "released";
911 if (key_event.type == Accessibility_KEY_PRESSED_EVENT)
912 state_ptr = pressed_str;
914 state_ptr = released_str;
917 "Key %lu %s (%c), modifiers %d; string=%s [%x] %s\n",
918 (unsigned long) keysym,
920 keysym ? (int) keysym : '*',
921 (int) x_key_event->state,
922 key_event.event_string,
923 key_event.event_string[0],
924 (key_event.is_text == TRUE) ? "(text)" : "(not text)");
928 fprintf (stderr, "%s%c\n",
929 (x_key_event->state & Mod1Mask)?"Alt-":"",
930 ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
931 g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
932 fprintf (stderr, "serial: %x Time: %x\n", x_key_event->serial, x_key_event->time);
933 #endif /* SPI_DEBUG */
938 spi_dec_x11_grab_key (SpiDEController *controller,
940 Accessibility_ControllerEventMask mod_mask)
942 XGrabKey (spi_get_display (),
945 spi_get_root_window (),
949 XSync (spi_get_display (), False);
950 return spi_clear_error_state ();
954 spi_dec_x11_ungrab_key (SpiDEController *controller,
956 Accessibility_ControllerEventMask mod_mask)
958 XUngrabKey (spi_get_display (),
961 spi_get_root_window ());
965 xkb_get_slowkeys_delay (SpiDEController *controller)
967 unsigned int retval = 0;
968 DEControllerPrivateData *priv = controller->priv;
970 #ifdef XKB_HAS_GET_SLOW_KEYS_DELAY
971 retval = XkbGetSlowKeysDelay (spi_get_display (),
972 XkbUseCoreKbd, &bounce_delay);
974 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
976 Status s = XkbGetControls (spi_get_display (),
977 XkbAllControlsMask, priv->xkb_desc);
980 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbSlowKeysMask)
981 retval = priv->xkb_desc->ctrls->slow_keys_delay;
987 fprintf (stderr, "SlowKeys delay: %d\n", (int) retval);
993 xkb_get_bouncekeys_delay (SpiDEController *controller)
995 unsigned int retval = 0;
996 DEControllerPrivateData *priv = controller->priv;
998 #ifdef XKB_HAS_GET_BOUNCE_KEYS_DELAY
999 retval = XkbGetBounceKeysDelay (spi_get_display (),
1000 XkbUseCoreKbd, &bounce_delay);
1002 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
1004 Status s = XkbGetControls (spi_get_display (),
1005 XkbAllControlsMask, priv->xkb_desc);
1008 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbBounceKeysMask)
1009 retval = priv->xkb_desc->ctrls->debounce_delay;
1014 #ifdef SPI_XKB_DEBUG
1015 fprintf (stderr, "BounceKeys delay: %d\n", (int) retval);
1021 spi_dec_x11_synth_keycode_press (SpiDEController *controller,
1022 unsigned int keycode)
1024 unsigned int time = CurrentTime;
1025 unsigned int bounce_delay;
1026 unsigned int elapsed_msec;
1028 DEControllerPrivateData *priv = controller->priv;
1030 spi_x_error_trap ();
1031 if (keycode == priv->last_release_keycode)
1033 bounce_delay = xkb_get_bouncekeys_delay (controller);
1036 gettimeofday (&tv, NULL);
1038 (tv.tv_sec - priv->last_release_time.tv_sec) * 1000
1039 + (tv.tv_usec - priv->last_release_time.tv_usec) / 1000;
1040 #ifdef SPI_XKB_DEBUG
1041 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1042 (long) (tv.tv_usec - priv->last_release_time.tv_usec));
1044 #ifdef THIS_IS_BROKEN
1045 if (elapsed_msec < bounce_delay)
1046 time = bounce_delay - elapsed_msec + 1;
1048 time = bounce_delay + 10;
1049 /* fudge for broken XTest */
1051 #ifdef SPI_XKB_DEBUG
1052 fprintf (stderr, "waiting %d ms\n", time);
1056 XTestFakeKeyEvent (spi_get_display (), keycode, True, time);
1057 priv->last_press_keycode = keycode;
1058 XFlush (spi_get_display ());
1059 XSync (spi_get_display (), False);
1060 gettimeofday (&priv->last_press_time, NULL);
1065 spi_dec_x11_synth_keycode_release (SpiDEController *controller,
1066 unsigned int keycode)
1068 unsigned int time = CurrentTime;
1069 unsigned int slow_delay;
1070 unsigned int elapsed_msec;
1072 DEControllerPrivateData *priv = controller->priv;
1074 spi_x_error_trap ();
1075 if (keycode == priv->last_press_keycode)
1077 slow_delay = xkb_get_slowkeys_delay (controller);
1080 gettimeofday (&tv, NULL);
1082 (tv.tv_sec - priv->last_press_time.tv_sec) * 1000
1083 + (tv.tv_usec - priv->last_press_time.tv_usec) / 1000;
1084 #ifdef SPI_XKB_DEBUG
1085 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1086 (long) (tv.tv_usec - priv->last_press_time.tv_usec));
1088 #ifdef THIS_IS_BROKEN_DUNNO_WHY
1089 if (elapsed_msec < slow_delay)
1090 time = slow_delay - elapsed_msec + 1;
1092 time = slow_delay + 10;
1093 /* our XTest seems broken, we have to add slop as above */
1095 #ifdef SPI_XKB_DEBUG
1096 fprintf (stderr, "waiting %d ms\n", time);
1100 XTestFakeKeyEvent (spi_get_display (), keycode, False, time);
1101 priv->last_release_keycode = keycode;
1102 XSync (spi_get_display (), False);
1103 gettimeofday (&priv->last_release_time, NULL);
1108 spi_dec_x11_lock_modifiers (SpiDEController *controller, unsigned modifiers)
1110 DEControllerPrivateData *priv = controller->priv;
1112 if (priv->have_xkb) {
1113 return XkbLockModifiers (spi_get_display (), XkbUseCoreKbd,
1114 modifiers, modifiers);
1118 xmkeymap = XGetModifierMapping(spi_get_display ());
1119 for (mod_index=0;mod_index<8;mod_index++)
1120 if (modifiers & (1<<mod_index))
1121 spi_dec_x11_synth_keycode_press(controller, xmkeymap->modifiermap[mod_index]);
1127 spi_dec_x11_unlock_modifiers (SpiDEController *controller, unsigned modifiers)
1129 DEControllerPrivateData *priv = controller->priv;
1131 if (priv->have_xkb) {
1132 return XkbLockModifiers (spi_get_display (), XkbUseCoreKbd,
1137 xmkeymap = XGetModifierMapping(spi_get_display ());
1139 for (mod_index=0;mod_index<8;mod_index++)
1140 if (modifiers & (1<<mod_index))
1141 spi_dec_x11_synth_keycode_release(controller, xmkeymap->modifiermap[mod_index]);
1147 keysym_for_unichar (SpiDEController *controller, gunichar unichar)
1149 return (KeySym) ucs2keysym ((long) unichar);
1153 spi_dec_x11_synth_keystring (SpiDEController *controller, guint synth_type, gint keycode, const char *keystring)
1155 /* probably we need to create and inject an XIM handler eventually. */
1156 /* for now, try to match the string to existing
1157 * keycode+modifier states.
1161 gunichar unichar = 0;
1163 gboolean retval = TRUE;
1167 maxlen = strlen (keystring) + 1;
1168 keysyms = g_new0 (KeySym, maxlen);
1169 if (!(keystring && *keystring && g_utf8_validate (keystring, -1, &c))) {
1174 fprintf (stderr, "[keystring synthesis attempted on %s]\n", keystring);
1176 while (keystring && (unichar = g_utf8_get_char (keystring))) {
1180 mbytes = g_unichar_to_utf8 (unichar, bytes);
1181 bytes[mbytes] = '\0';
1183 fprintf (stderr, "[unichar %s]", bytes);
1185 keysym = keysym_for_unichar (controller, unichar);
1186 if (keysym == NoSymbol) {
1188 fprintf (stderr, "no keysym for %s", bytes);
1193 keysyms[i++] = keysym;
1194 keystring = g_utf8_next_char (keystring);
1197 XSynchronize (spi_get_display (), TRUE);
1198 for (i = 0; keysyms[i]; ++i) {
1199 if (!spi_dec_synth_keysym (controller, keysyms[i])) {
1201 fprintf (stderr, "could not synthesize %c\n",
1208 XSynchronize (spi_get_display (), FALSE);
1212 if (synth_type == Accessibility_KEY_SYM) {
1216 keysym = XkbKeycodeToKeysym (spi_get_display (), keycode, 0, 0);
1218 if (XkbKeysymToModifiers (spi_get_display (), keysym) == 0) {
1219 spi_dec_clear_unlatch_pending (controller);
1226 isEvent(Display *dpy, XEvent *event, char *arg)
1232 handle_io (GIOChannel *source,
1233 GIOCondition condition,
1236 SpiDEController *controller = (SpiDEController *) data;
1237 DEControllerPrivateData *priv = controller->priv;
1238 gboolean is_consumed = FALSE;
1241 while (XCheckIfEvent(priv->xevie_display, &ev, isEvent, NULL))
1243 if (ev.type == KeyPress || ev.type == KeyRelease)
1244 is_consumed = spi_device_event_controller_forward_key_event (controller, &ev);
1247 XevieSendEvent(priv->xevie_display, &ev, XEVIE_UNMODIFIED);
1252 #endif /* HAVE_XEVIE */
1255 spi_dec_x11_init (SpiDEController *controller)
1257 DEControllerPrivateData *priv = controller->priv;
1258 spi_events_init (spi_get_display());
1262 #endif /* HAVE_XEVIE */
1264 spi_events_init (spi_get_display ());
1266 priv->xevie_display = XOpenDisplay(NULL);
1268 if (XevieStart(priv->xevie_display) == TRUE)
1270 #ifdef SPI_KEYEVENT_DEBUG
1271 fprintf (stderr, "XevieStart() success \n");
1273 XevieSelectInput(priv->xevie_display, KeyPressMask | KeyReleaseMask);
1275 fd = ConnectionNumber(priv->xevie_display);
1276 ioc = g_io_channel_unix_new (fd);
1277 g_io_add_watch (ioc, G_IO_IN | G_IO_HUP, handle_io, controller);
1278 g_io_channel_unref (ioc);
1282 #ifdef SPI_KEYEVENT_DEBUG
1283 fprintf (stderr, "XevieStart() failed, only one client is allowed to do event int exception\n");
1286 #endif /* HAVE_XEVIE */
1288 gettimeofday (&priv->last_press_time, NULL);
1289 gettimeofday (&priv->last_release_time, NULL);
1290 spi_controller_register_with_devices (controller);
1292 spi_dec_init_mouse_listener (controller);
1294 saved_controller = controller;
1298 spi_dec_x11_finalize (SpiDEController *controller)
1300 DEControllerPrivateData *priv = controller->priv;
1301 /* disconnect any special listeners, get rid of outstanding keygrabs */
1302 XUngrabKey (spi_get_display (), AnyKey, AnyModifier, DefaultRootWindow (spi_get_display ()));
1305 if (priv->xevie_display != NULL)
1307 XevieEnd(priv->xevie_display);
1308 #ifdef SPI_KEYEVENT_DEBUG
1309 printf("XevieEnd(dpy) finished \n");
1315 XkbFreeKeyboard (priv->xkb_desc, 0, True);
1316 /* TODO: Should free the keymap */
1320 spi_device_event_controller_forward_key_event (SpiDEController *controller,
1321 const XEvent *event)
1323 DEControllerPrivateData *priv = controller->priv;
1324 Accessibility_DeviceEvent key_event;
1327 g_assert (event->type == KeyPress || event->type == KeyRelease);
1329 key_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1331 if (priv->xevie_display == NULL)
1332 spi_controller_update_key_grabs (controller, &key_event);
1334 /* relay to listeners, and decide whether to consume it or not */
1335 ret = spi_controller_notify_keylisteners (controller, &key_event, TRUE);
1336 g_free(key_event.event_string);
1342 is_key_released (long code)
1347 XQueryKeymap (spi_get_display (), keys);
1348 down = BIT (keys, code);
1353 check_release (gpointer data)
1356 Accessibility_DeviceEvent *event = (Accessibility_DeviceEvent *)data;
1357 KeyCode code = event->hw_code;
1359 released = is_key_released (code);
1363 check_release_handler = 0;
1364 event->type = Accessibility_KEY_RELEASED_EVENT;
1365 spi_controller_notify_keylisteners (saved_controller, event, TRUE);
1367 return (released == 0);
1371 wait_for_release_event (XEvent *event,
1372 SpiDEController *controller)
1374 pressed_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1375 check_release_handler = g_timeout_add (CHECK_RELEASE_DELAY, check_release, &pressed_event);
1379 spi_dec_x11_emit_modifier_event (SpiDEController *controller, guint prev_mask,
1382 dbus_uint32_t d1, d2;
1384 #ifdef SPI_XKB_DEBUG
1385 fprintf (stderr, "MODIFIER CHANGE EVENT! %x to %x\n",
1386 prev_mask, current_mask);
1389 /* set bits for the virtual modifiers like NUMLOCK */
1390 if (prev_mask & _numlock_physical_mask)
1391 prev_mask |= SPI_KEYMASK_NUMLOCK;
1392 if (current_mask & _numlock_physical_mask)
1393 current_mask |= SPI_KEYMASK_NUMLOCK;
1395 d1 = prev_mask & key_modifier_mask;
1396 d2 = current_mask & key_modifier_mask;
1397 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_KEYBOARD, "Modifiers", "", d1, d2);
1401 spi_dec_x11_generate_mouse_event (SpiDEController *controller,
1404 const char *eventName)
1407 gboolean err = FALSE;
1408 Display *display = spi_get_display ();
1410 switch (eventName[0])
1413 switch (eventName[1])
1415 /* TODO: check number of buttons before parsing */
1436 if (x != -1 && y != -1)
1438 XTestFakeMotionEvent (display, DefaultScreen (display),
1441 XTestFakeButtonEvent (display, button, !(eventName[2] == 'r'), 0);
1442 if (eventName[2] == 'c')
1443 XTestFakeButtonEvent (display, button, FALSE, 1);
1444 else if (eventName[2] == 'd')
1446 XTestFakeButtonEvent (display, button, FALSE, 1);
1447 XTestFakeButtonEvent (display, button, TRUE, 2);
1448 XTestFakeButtonEvent (display, button, FALSE, 3);
1452 case 'r': /* relative motion */
1453 XTestFakeRelativeMotionEvent (display, x, y, 0);
1455 case 'a': /* absolute motion */
1456 XTestFakeMotionEvent (display, DefaultScreen (display),
1463 spi_dec_setup_x11 (SpiDEControllerClass *klass)
1465 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1467 klass->plat.get_keycode = spi_dec_x11_get_keycode;
1468 klass->plat.mouse_check = spi_dec_x11_mouse_check;
1469 klass->plat.synth_keycode_press = spi_dec_x11_synth_keycode_press;
1470 klass->plat.synth_keycode_release = spi_dec_x11_synth_keycode_release;
1471 klass->plat.lock_modifiers = spi_dec_x11_lock_modifiers;
1472 klass->plat.unlock_modifiers = spi_dec_x11_unlock_modifiers;
1473 klass->plat.synth_keystring = spi_dec_x11_synth_keystring;
1474 klass->plat.grab_key = spi_dec_x11_grab_key;
1475 klass->plat.ungrab_key = spi_dec_x11_ungrab_key;
1476 klass->plat.emit_modifier_event = spi_dec_x11_emit_modifier_event;
1477 klass->plat.generate_mouse_event = spi_dec_x11_generate_mouse_event;
1479 klass->plat.init = spi_dec_x11_init;
1480 klass->plat.finalize = spi_dec_x11_finalize;
1482 g_type_class_add_private (object_class, sizeof (DEControllerPrivateData));