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 KeySym ucs2keysym (long ucs);
69 long keysym2ucs(KeySym keysym);
71 static void spi_dec_x11_emit_modifier_event (SpiDEController *controller,
75 #define CHECK_RELEASE_DELAY 20
76 #define BIT(c, x) (c[x/8]&(1<<(x%8)))
77 static guint check_release_handler = 0;
78 static Accessibility_DeviceEvent pressed_event;
79 static void wait_for_release_event (XEvent *event, SpiDEController *controller);
81 static int spi_error_code = 0;
86 typedef struct _SpiPoint SpiPoint;
87 static SpiPoint last_mouse_pos_static = {0, 0};
88 static SpiPoint *last_mouse_pos = &last_mouse_pos_static;
89 static unsigned int mouse_mask_state = 0;
90 static unsigned int mouse_button_mask =
91 Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask;
92 static unsigned int key_modifier_mask =
93 Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask | ShiftMask | LockMask | ControlMask | SPI_KEYMASK_NUMLOCK;
94 static unsigned int _numlock_physical_mask = Mod2Mask; /* a guess, will be reset */
96 static XModifierKeymap* xmkeymap = NULL;
99 static int (*x_default_error_handler) (Display *display, XErrorEvent *error_event);
102 Display *xevie_display;
103 unsigned int last_press_keycode;
104 unsigned int last_release_keycode;
105 struct timeval last_press_time;
106 struct timeval last_release_time;
108 int xkb_major_extension_opcode;
109 int xkb_base_event_code;
110 int xkb_base_error_code;
111 unsigned int xkb_latch_mask;
112 unsigned int pending_xkb_mod_relatch_mask;
114 KeyCode reserved_keycode;
115 KeySym reserved_keysym;
116 guint reserved_reset_timeout;
117 } DEControllerPrivateData;
119 static void spi_controller_register_with_devices (SpiDEController *controller);
120 static gboolean spi_device_event_controller_forward_key_event (SpiDEController *controller,
121 const XEvent *event);
124 static SpiDEController *saved_controller;
127 keysym_mod_mask (KeySym keysym, KeyCode keycode)
129 /* we really should use XKB and look directly at the keymap */
130 /* this is very inelegant */
131 Display *display = spi_get_display ();
132 unsigned int mods_rtn = 0;
133 unsigned int retval = 0;
136 if (XkbLookupKeySym (display, keycode, 0, &mods_rtn, &sym_rtn) &&
137 (sym_rtn == keysym)) {
140 else if (XkbLookupKeySym (display, keycode, ShiftMask, &mods_rtn, &sym_rtn) &&
141 (sym_rtn == keysym)) {
144 else if (XkbLookupKeySym (display, keycode, Mod2Mask, &mods_rtn, &sym_rtn) &&
145 (sym_rtn == keysym)) {
148 else if (XkbLookupKeySym (display, keycode, Mod3Mask, &mods_rtn, &sym_rtn) &&
149 (sym_rtn == keysym)) {
152 else if (XkbLookupKeySym (display, keycode,
153 ShiftMask | Mod2Mask, &mods_rtn, &sym_rtn) &&
154 (sym_rtn == keysym)) {
155 retval = (Mod2Mask | ShiftMask);
157 else if (XkbLookupKeySym (display, keycode,
158 ShiftMask | Mod3Mask, &mods_rtn, &sym_rtn) &&
159 (sym_rtn == keysym)) {
160 retval = (Mod3Mask | ShiftMask);
162 else if (XkbLookupKeySym (display, keycode,
163 ShiftMask | Mod4Mask, &mods_rtn, &sym_rtn) &&
164 (sym_rtn == keysym)) {
165 retval = (Mod4Mask | ShiftMask);
173 replace_map_keysym (DEControllerPrivateData *priv, KeyCode keycode, KeySym keysym)
176 Display *dpy = spi_get_display ();
178 if (!(desc = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd)))
180 fprintf (stderr, "ERROR getting map\n");
184 if (desc && desc->map)
186 gint offset = desc->map->key_sym_map[keycode].offset;
187 desc->map->syms[offset] = keysym;
191 fprintf (stderr, "Error changing key map: empty server structure\n");
193 XkbSetMap (dpy, XkbAllMapComponentsMask, desc);
195 * FIXME: the use of XkbChangeMap, and the reuse of the priv->xkb_desc structure,
196 * would be far preferable.
197 * HOWEVER it does not seem to work using XFree 4.3.
199 /* XkbChangeMap (dpy, priv->xkb_desc, priv->changes); */
202 XkbFreeKeyboard (desc, 0, TRUE);
211 spi_dec_reset_reserved (gpointer data)
213 DEControllerPrivateData *priv = data;
214 replace_map_keysym (priv, priv->reserved_keycode, priv->reserved_keysym);
215 priv->reserved_reset_timeout = 0;
220 spi_dec_x11_get_keycode (SpiDEController *controller,
227 if (key_str && key_str[0])
228 keysym = XStringToKeysym(key_str);
229 keycode = XKeysymToKeycode (spi_get_display (), (KeySym) keysym);
232 DEControllerPrivateData *priv = controller->priv;
233 /* if there's no keycode available, fix it */
234 if (replace_map_keysym (priv, priv->reserved_keycode, keysym))
236 keycode = priv->reserved_keycode;
238 * queue a timer to restore the old keycode. Ugly, but required
239 * due to races / asynchronous X delivery.
240 * Long-term fix is to extend the X keymap here instead of replace entries.
242 priv->reserved_reset_timeout = g_timeout_add (500, spi_dec_reset_reserved, priv);
249 *modmask = keysym_mod_mask (keysym, keycode);
254 spi_dec_set_unlatch_pending (SpiDEController *controller, unsigned mask)
256 DEControllerPrivateData *priv = controller->priv;
258 if (priv->xkb_latch_mask) fprintf (stderr, "unlatch pending! %x\n",
259 priv->xkb_latch_mask);
261 priv->pending_xkb_mod_relatch_mask |= priv->xkb_latch_mask;
265 spi_dec_button_update_and_emit (SpiDEController *controller,
268 Accessibility_DeviceEvent mouse_e;
269 gchar event_detail[3];
270 gboolean is_consumed = FALSE;
272 if ((mask_return & mouse_button_mask) !=
273 (mouse_mask_state & mouse_button_mask))
275 int button_number = 0;
276 gboolean is_down = False;
278 if (!(mask_return & Button1Mask) &&
279 (mouse_mask_state & Button1Mask))
281 mouse_mask_state &= ~Button1Mask;
284 else if ((mask_return & Button1Mask) &&
285 !(mouse_mask_state & Button1Mask))
287 mouse_mask_state |= Button1Mask;
291 else if (!(mask_return & Button2Mask) &&
292 (mouse_mask_state & Button2Mask))
294 mouse_mask_state &= ~Button2Mask;
297 else if ((mask_return & Button2Mask) &&
298 !(mouse_mask_state & Button2Mask))
300 mouse_mask_state |= Button2Mask;
304 else if (!(mask_return & Button3Mask) &&
305 (mouse_mask_state & Button3Mask))
307 mouse_mask_state &= ~Button3Mask;
310 else if ((mask_return & Button3Mask) &&
311 !(mouse_mask_state & Button3Mask))
313 mouse_mask_state |= Button3Mask;
317 else if (!(mask_return & Button4Mask) &&
318 (mouse_mask_state & Button4Mask))
320 mouse_mask_state &= ~Button4Mask;
323 else if ((mask_return & Button4Mask) &&
324 !(mouse_mask_state & Button4Mask))
326 mouse_mask_state |= Button4Mask;
330 else if (!(mask_return & Button5Mask) &&
331 (mouse_mask_state & Button5Mask))
333 mouse_mask_state &= ~Button5Mask;
336 else if ((mask_return & Button5Mask) &&
337 !(mouse_mask_state & Button5Mask))
339 mouse_mask_state |= Button5Mask;
345 fprintf (stderr, "Button %d %s\n",
346 button_number, (is_down) ? "Pressed" : "Released");
348 snprintf (event_detail, 3, "%d%c", button_number,
349 (is_down) ? 'p' : 'r');
350 /* TODO: FIXME distinguish between physical and
353 mouse_e.type = (is_down) ?
354 Accessibility_BUTTON_PRESSED_EVENT :
355 Accessibility_BUTTON_RELEASED_EVENT;
356 mouse_e.id = button_number;
357 mouse_e.hw_code = button_number;
358 mouse_e.modifiers = (dbus_uint16_t) mouse_mask_state;
359 mouse_e.timestamp = 0;
360 mouse_e.event_string = "";
361 mouse_e.is_text = FALSE;
363 spi_controller_notify_mouselisteners (controller,
367 dbus_uint32_t x = last_mouse_pos->x, y = last_mouse_pos->y;
368 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Button", event_detail, x, y);
371 spi_dec_set_unlatch_pending (controller, mask_return);
382 spi_dec_x11_mouse_check (SpiDEController *controller,
383 int *x, int *y, gboolean *moved)
385 int win_x_return,win_y_return;
386 unsigned int mask_return;
387 Window root_return, child_return;
388 Display *display = spi_get_display ();
391 XQueryPointer(display, DefaultRootWindow (display),
392 &root_return, &child_return,
394 &win_x_return, &win_y_return, &mask_return);
396 * Since many clients grab the pointer, and X goes an automatic
397 * pointer grab on mouse-down, we often must detect mouse button events
398 * by polling rather than via a button grab.
399 * The while loop (rather than if) is used since it's possible that
400 * multiple buttons have changed state since we last checked.
402 if (mask_return != mouse_mask_state)
404 while (spi_dec_button_update_and_emit (controller, mask_return));
407 if (*x != last_mouse_pos->x || *y != last_mouse_pos->y)
409 // TODO: combine these two signals?
410 dbus_uint32_t ix = *x, iy = *y;
411 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Abs", "", ix, iy);
412 ix -= last_mouse_pos->x;
413 iy -= last_mouse_pos->y;
414 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Rel", "", ix, iy);
415 last_mouse_pos->x = *x;
416 last_mouse_pos->y = *y;
427 #ifdef WE_NEED_UGRAB_MOUSE
429 spi_dec_ungrab_mouse (gpointer data)
431 Display *display = spi_get_display ();
434 XUngrabButton (spi_get_display (), AnyButton, AnyModifier,
435 XDefaultRootWindow (spi_get_display ()));
442 spi_dec_init_mouse_listener (SpiDEController *dec)
445 Display *display = spi_get_display ();
449 if (XGrabButton (display, AnyButton, AnyModifier,
450 spi_get_root_window (),
451 True, ButtonPressMask | ButtonReleaseMask,
452 GrabModeSync, GrabModeAsync, None, None) != Success) {
454 fprintf (stderr, "WARNING: could not grab mouse buttons!\n");
458 XSync (display, False);
460 fprintf (stderr, "mouse buttons grabbed\n");
467 spi_dec_clear_unlatch_pending (SpiDEController *controller)
469 DEControllerPrivateData *priv = controller->priv;
470 priv->xkb_latch_mask = 0;
474 spi_device_event_controller_forward_mouse_event (SpiDEController *controller,
477 Accessibility_DeviceEvent mouse_e;
478 gchar event_detail[3];
479 gboolean is_consumed = FALSE;
480 gboolean xkb_mod_unlatch_occurred;
481 XButtonEvent *xbutton_event = (XButtonEvent *) xevent;
482 dbus_uint32_t ix, iy;
484 int button = xbutton_event->button;
486 unsigned int mouse_button_state = xbutton_event->state;
491 mouse_button_state |= Button1Mask;
494 mouse_button_state |= Button2Mask;
497 mouse_button_state |= Button3Mask;
500 mouse_button_state |= Button4Mask;
503 mouse_button_state |= Button5Mask;
507 last_mouse_pos->x = ((XButtonEvent *) xevent)->x_root;
508 last_mouse_pos->y = ((XButtonEvent *) xevent)->y_root;
511 fprintf (stderr, "mouse button %d %s (%x)\n",
512 xbutton_event->button,
513 (xevent->type == ButtonPress) ? "Press" : "Release",
516 snprintf (event_detail, 3, "%d%c", button,
517 (xevent->type == ButtonPress) ? 'p' : 'r');
519 /* TODO: FIXME distinguish between physical and logical buttons */
520 mouse_e.type = (xevent->type == ButtonPress) ?
521 Accessibility_BUTTON_PRESSED_EVENT :
522 Accessibility_BUTTON_RELEASED_EVENT;
524 mouse_e.hw_code = button;
525 mouse_e.modifiers = (dbus_uint16_t) xbutton_event->state;
526 mouse_e.timestamp = (dbus_uint32_t) xbutton_event->time;
527 mouse_e.event_string = "";
528 mouse_e.is_text = FALSE;
529 if ((mouse_button_state & mouse_button_mask) !=
530 (mouse_mask_state & mouse_button_mask))
532 if ((mouse_mask_state & key_modifier_mask) !=
533 (mouse_button_state & key_modifier_mask))
534 spi_dec_x11_emit_modifier_event (controller,
535 mouse_mask_state, mouse_button_state);
536 mouse_mask_state = mouse_button_state;
538 spi_controller_notify_mouselisteners (controller, &mouse_e);
539 ix = last_mouse_pos->x;
540 iy = last_mouse_pos->y;
541 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Button", event_detail, ix, iy);
544 xkb_mod_unlatch_occurred = (xevent->type == ButtonPress ||
545 xevent->type == ButtonRelease);
547 /* if client wants to consume this event, and XKB latch state was
548 * unset by this button event, we reset it
550 if (is_consumed && xkb_mod_unlatch_occurred)
551 spi_dec_set_unlatch_pending (controller, mouse_mask_state);
553 XAllowEvents (spi_get_display (),
554 (is_consumed) ? SyncPointer : ReplayPointer,
559 global_filter_fn (XEvent *xevent, void *data)
561 SpiDEController *controller;
562 DEControllerPrivateData *priv;
563 Display *display = spi_get_display ();
564 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
565 priv = controller->priv;
567 if (xevent->type == MappingNotify)
570 if (xevent->type == KeyPress || xevent->type == KeyRelease)
572 if (priv->xevie_display == NULL)
574 gboolean is_consumed;
577 spi_device_event_controller_forward_key_event (controller, xevent);
584 n_events = XPending (display);
586 #ifdef SPI_KEYEVENT_DEBUG
587 g_print ("Number of events pending: %d\n", n_events);
589 for (i = 0; i < n_events; i++)
591 XNextEvent (display, &next_event);
592 if (next_event.type != KeyPress &&
593 next_event.type != KeyRelease)
594 g_warning ("Unexpected event type %d in queue", next_event.type);
597 XAllowEvents (display, AsyncKeyboard, CurrentTime);
599 XUngrabKeyboard (display, CurrentTime);
603 if (xevent->type == KeyPress)
604 wait_for_release_event (xevent, controller);
605 XAllowEvents (display, ReplayKeyboard, CurrentTime);
611 if (xevent->type == ButtonPress || xevent->type == ButtonRelease)
613 spi_device_event_controller_forward_mouse_event (controller, xevent);
615 if (xevent->type == priv->xkb_base_event_code)
617 XkbAnyEvent * xkb_ev = (XkbAnyEvent *) xevent;
618 /* ugly but probably necessary...*/
619 XSynchronize (display, TRUE);
621 if (xkb_ev->xkb_type == XkbStateNotify)
623 XkbStateNotifyEvent *xkb_snev =
624 (XkbStateNotifyEvent *) xkb_ev;
625 /* check the mouse, to catch mouse events grabbed by
626 * another client; in case we should revert this XKB delatch
628 if (!priv->pending_xkb_mod_relatch_mask)
632 spi_dec_x11_mouse_check (controller, &x, &y, &moved);
634 /* we check again, since the previous call may have
636 if (priv->pending_xkb_mod_relatch_mask)
638 unsigned int feedback_mask;
640 fprintf (stderr, "relatching %x\n",
641 priv->pending_xkb_mod_relatch_mask);
643 /* temporarily turn off the latch bell, if it's on */
644 XkbGetControls (display,
645 XkbAccessXFeedbackMask,
647 feedback_mask = priv->xkb_desc->ctrls->ax_options;
648 if (feedback_mask & XkbAX_StickyKeysFBMask)
650 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
652 priv->xkb_desc->ctrls->ax_options
653 &= ~(XkbAX_StickyKeysFBMask);
654 XkbChangeControls (display, priv->xkb_desc, &changes);
656 /* TODO: account for lock as well as latch */
657 XkbLatchModifiers (display,
659 priv->pending_xkb_mod_relatch_mask,
660 priv->pending_xkb_mod_relatch_mask);
661 if (feedback_mask & XkbAX_StickyKeysFBMask)
663 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
665 priv->xkb_desc->ctrls->ax_options = feedback_mask;
666 XkbChangeControls (display, priv->xkb_desc, &changes);
669 fprintf (stderr, "relatched %x\n",
670 priv->pending_xkb_mod_relatch_mask);
672 priv->pending_xkb_mod_relatch_mask = 0;
676 priv->xkb_latch_mask = xkb_snev->latched_mods;
679 XSynchronize (display, FALSE);
686 _spi_controller_device_error_handler (Display *display, XErrorEvent *error)
688 if (error->error_code == BadAccess)
690 g_message ("Could not complete key grab: grab already in use.\n");
691 spi_error_code = BadAccess;
696 return (*x_default_error_handler) (display, error);
701 spi_controller_register_with_devices (SpiDEController *controller)
703 DEControllerPrivateData *priv;
704 int event_base, error_base, major_version, minor_version;
706 priv = controller->priv;
707 if (XTestQueryExtension (spi_get_display(), &event_base, &error_base, &major_version, &minor_version))
709 XTestGrabControl (spi_get_display (), True);
712 /* calls to device-specific implementations and routines go here */
713 /* register with: keyboard hardware code handler */
714 /* register with: (translated) keystroke handler */
716 priv->have_xkb = XkbQueryExtension (spi_get_display (),
717 &priv->xkb_major_extension_opcode,
718 &priv->xkb_base_event_code,
719 &priv->xkb_base_error_code, NULL, NULL);
723 guint64 reserved = 0;
724 priv->xkb_desc = XkbGetMap (spi_get_display (), XkbKeySymsMask, XkbUseCoreKbd);
725 XkbSelectEvents (spi_get_display (),
727 XkbStateNotifyMask, XkbStateNotifyMask);
728 _numlock_physical_mask = XkbKeysymToModifiers (spi_get_display (),
730 for (i = priv->xkb_desc->max_key_code; i >= priv->xkb_desc->min_key_code; --i)
732 if (priv->xkb_desc->map->key_sym_map[i].kt_index[0] == XkbOneLevelIndex)
734 if (XkbKeycodeToKeysym (spi_get_display (), i, 0, 0) != 0)
736 /* don't use this one if there's a grab client! */
738 /* Runtime errors are generated from these functions,
739 * that are then quashed. Equivalent to:
747 XGrabKey (spi_get_display (), i, 0,
748 spi_get_root_window (),
750 GrabModeSync, GrabModeSync);
751 XSync (spi_get_display (), TRUE);
752 XUngrabKey (spi_get_display (), i, 0,
753 spi_get_root_window ());
754 if (!spi_x_error_release ())
764 priv->reserved_keycode = reserved;
765 priv->reserved_keysym = XkbKeycodeToKeysym (spi_get_display (), reserved, 0, 0);
769 priv->reserved_keycode = XKeysymToKeycode (spi_get_display (), XK_numbersign);
770 priv->reserved_keysym = XK_numbersign;
772 #ifdef SPI_RESERVED_DEBUG
774 sym = XKeycodeToKeysym (spi_get_display (), reserved, 0);
775 fprintf (stderr, "%x\n", sym);
776 fprintf (stderr, "setting the reserved keycode to %d (%s)\n",
778 XKeysymToString (XKeycodeToKeysym (spi_get_display (),
783 spi_set_filter (global_filter_fn, controller);
784 spi_set_events (KeyPressMask | KeyReleaseMask);
786 x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
789 static Accessibility_DeviceEvent
790 spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
792 Accessibility_DeviceEvent key_event;
794 const int cbuf_bytes = 20;
798 nbytes = XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL);
799 key_event.id = (dbus_int32_t)(keysym);
800 key_event.hw_code = (dbus_int16_t) x_key_event->keycode;
801 if (((XEvent *) x_key_event)->type == KeyPress)
803 key_event.type = Accessibility_KEY_PRESSED_EVENT;
807 key_event.type = Accessibility_KEY_RELEASED_EVENT;
809 key_event.modifiers = (dbus_uint16_t)(x_key_event->state);
810 key_event.is_text = FALSE;
814 key_event.event_string = g_strdup ("space");
817 key_event.event_string = g_strdup ("Tab");
820 key_event.event_string = g_strdup ("Backspace");
823 key_event.event_string = g_strdup ("Return");
826 key_event.event_string = g_strdup ("Home");
829 key_event.event_string = g_strdup ("Page_Down");
832 key_event.event_string = g_strdup ("Page_Up");
835 key_event.event_string = g_strdup ("F1");
838 key_event.event_string = g_strdup ("F2");
841 key_event.event_string = g_strdup ("F3");
844 key_event.event_string = g_strdup ("F4");
847 key_event.event_string = g_strdup ("F5");
850 key_event.event_string = g_strdup ("F6");
853 key_event.event_string = g_strdup ("F7");
856 key_event.event_string = g_strdup ("F8");
859 key_event.event_string = g_strdup ("F9");
862 key_event.event_string = g_strdup ("F10");
865 key_event.event_string = g_strdup ("F11");
868 key_event.event_string = g_strdup ("F12");
871 key_event.event_string = g_strdup ("End");
874 key_event.event_string = g_strdup ("Escape");
877 key_event.event_string = g_strdup ("Up");
880 key_event.event_string = g_strdup ("Down");
883 key_event.event_string = g_strdup ("Left");
886 key_event.event_string = g_strdup ("Right");
892 cbuf[nbytes] = '\0'; /* OK since length is cbuf_bytes+1 */
893 key_event.event_string = g_strdup (cbuf);
894 c = keysym2ucs (keysym);
895 if (c > 0 && !g_unichar_iscntrl (c))
897 key_event.is_text = TRUE;
898 /* incorrect for some composed chars? */
903 key_event.event_string = g_strdup ("");
907 key_event.timestamp = (dbus_uint32_t) x_key_event->time;
908 #ifdef SPI_KEYEVENT_DEBUG
910 char *pressed_str = "pressed";
911 char *released_str = "released";
914 if (key_event.type == Accessibility_KEY_PRESSED_EVENT)
915 state_ptr = pressed_str;
917 state_ptr = released_str;
920 "Key %lu %s (%c), modifiers %d; string=%s [%x] %s\n",
921 (unsigned long) keysym,
923 keysym ? (int) keysym : '*',
924 (int) x_key_event->state,
925 key_event.event_string,
926 key_event.event_string[0],
927 (key_event.is_text == TRUE) ? "(text)" : "(not text)");
931 fprintf (stderr, "%s%c\n",
932 (x_key_event->state & Mod1Mask)?"Alt-":"",
933 ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
934 g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
935 fprintf (stderr, "serial: %x Time: %x\n", x_key_event->serial, x_key_event->time);
936 #endif /* SPI_DEBUG */
941 spi_dec_x11_grab_key (SpiDEController *controller,
943 Accessibility_ControllerEventMask mod_mask)
945 XGrabKey (spi_get_display (),
948 spi_get_root_window (),
952 XSync (spi_get_display (), False);
953 return spi_clear_error_state ();
957 spi_dec_x11_ungrab_key (SpiDEController *controller,
959 Accessibility_ControllerEventMask mod_mask)
961 XUngrabKey (spi_get_display (),
964 spi_get_root_window ());
968 xkb_get_slowkeys_delay (SpiDEController *controller)
970 unsigned int retval = 0;
971 DEControllerPrivateData *priv = controller->priv;
973 #ifdef XKB_HAS_GET_SLOW_KEYS_DELAY
974 retval = XkbGetSlowKeysDelay (spi_get_display (),
975 XkbUseCoreKbd, &bounce_delay);
977 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
979 Status s = XkbGetControls (spi_get_display (),
980 XkbAllControlsMask, priv->xkb_desc);
983 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbSlowKeysMask)
984 retval = priv->xkb_desc->ctrls->slow_keys_delay;
990 fprintf (stderr, "SlowKeys delay: %d\n", (int) retval);
996 xkb_get_bouncekeys_delay (SpiDEController *controller)
998 unsigned int retval = 0;
999 DEControllerPrivateData *priv = controller->priv;
1001 #ifdef XKB_HAS_GET_BOUNCE_KEYS_DELAY
1002 retval = XkbGetBounceKeysDelay (spi_get_display (),
1003 XkbUseCoreKbd, &bounce_delay);
1005 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
1007 Status s = XkbGetControls (spi_get_display (),
1008 XkbAllControlsMask, priv->xkb_desc);
1011 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbBounceKeysMask)
1012 retval = priv->xkb_desc->ctrls->debounce_delay;
1017 #ifdef SPI_XKB_DEBUG
1018 fprintf (stderr, "BounceKeys delay: %d\n", (int) retval);
1024 spi_dec_x11_synth_keycode_press (SpiDEController *controller,
1025 unsigned int keycode)
1027 unsigned int time = CurrentTime;
1028 unsigned int bounce_delay;
1029 unsigned int elapsed_msec;
1031 DEControllerPrivateData *priv = controller->priv;
1033 spi_x_error_trap ();
1034 if (keycode == priv->last_release_keycode)
1036 bounce_delay = xkb_get_bouncekeys_delay (controller);
1039 gettimeofday (&tv, NULL);
1041 (tv.tv_sec - priv->last_release_time.tv_sec) * 1000
1042 + (tv.tv_usec - priv->last_release_time.tv_usec) / 1000;
1043 #ifdef SPI_XKB_DEBUG
1044 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1045 (long) (tv.tv_usec - priv->last_release_time.tv_usec));
1047 #ifdef THIS_IS_BROKEN
1048 if (elapsed_msec < bounce_delay)
1049 time = bounce_delay - elapsed_msec + 1;
1051 time = bounce_delay + 10;
1052 /* fudge for broken XTest */
1054 #ifdef SPI_XKB_DEBUG
1055 fprintf (stderr, "waiting %d ms\n", time);
1059 XTestFakeKeyEvent (spi_get_display (), keycode, True, time);
1060 priv->last_press_keycode = keycode;
1061 XFlush (spi_get_display ());
1062 XSync (spi_get_display (), False);
1063 gettimeofday (&priv->last_press_time, NULL);
1068 spi_dec_x11_synth_keycode_release (SpiDEController *controller,
1069 unsigned int keycode)
1071 unsigned int time = CurrentTime;
1072 unsigned int slow_delay;
1073 unsigned int elapsed_msec;
1075 DEControllerPrivateData *priv = controller->priv;
1077 spi_x_error_trap ();
1078 if (keycode == priv->last_press_keycode)
1080 slow_delay = xkb_get_slowkeys_delay (controller);
1083 gettimeofday (&tv, NULL);
1085 (tv.tv_sec - priv->last_press_time.tv_sec) * 1000
1086 + (tv.tv_usec - priv->last_press_time.tv_usec) / 1000;
1087 #ifdef SPI_XKB_DEBUG
1088 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1089 (long) (tv.tv_usec - priv->last_press_time.tv_usec));
1091 #ifdef THIS_IS_BROKEN_DUNNO_WHY
1092 if (elapsed_msec < slow_delay)
1093 time = slow_delay - elapsed_msec + 1;
1095 time = slow_delay + 10;
1096 /* our XTest seems broken, we have to add slop as above */
1098 #ifdef SPI_XKB_DEBUG
1099 fprintf (stderr, "waiting %d ms\n", time);
1103 XTestFakeKeyEvent (spi_get_display (), keycode, False, time);
1104 priv->last_release_keycode = keycode;
1105 XSync (spi_get_display (), False);
1106 gettimeofday (&priv->last_release_time, NULL);
1111 spi_dec_x11_lock_modifiers (SpiDEController *controller, unsigned modifiers)
1113 DEControllerPrivateData *priv = controller->priv;
1115 if (priv->have_xkb) {
1116 return XkbLockModifiers (spi_get_display (), XkbUseCoreKbd,
1117 modifiers, modifiers);
1121 xmkeymap = XGetModifierMapping(spi_get_display ());
1122 for (mod_index=0;mod_index<8;mod_index++)
1123 if (modifiers & (1<<mod_index))
1124 spi_dec_x11_synth_keycode_press(controller, xmkeymap->modifiermap[mod_index]);
1130 spi_dec_x11_unlock_modifiers (SpiDEController *controller, unsigned modifiers)
1132 DEControllerPrivateData *priv = controller->priv;
1134 if (priv->have_xkb) {
1135 return XkbLockModifiers (spi_get_display (), XkbUseCoreKbd,
1140 xmkeymap = XGetModifierMapping(spi_get_display ());
1142 for (mod_index=0;mod_index<8;mod_index++)
1143 if (modifiers & (1<<mod_index))
1144 spi_dec_x11_synth_keycode_release(controller, xmkeymap->modifiermap[mod_index]);
1150 keysym_for_unichar (SpiDEController *controller, gunichar unichar)
1152 return ucs2keysym ((long) unichar);
1156 spi_dec_x11_synth_keystring (SpiDEController *controller, guint synth_type, gint keycode, const char *keystring)
1158 /* probably we need to create and inject an XIM handler eventually. */
1159 /* for now, try to match the string to existing
1160 * keycode+modifier states.
1164 gunichar unichar = 0;
1166 gboolean retval = TRUE;
1170 maxlen = strlen (keystring) + 1;
1171 keysyms = g_new0 (KeySym, maxlen);
1172 if (!(keystring && *keystring && g_utf8_validate (keystring, -1, &c))) {
1177 fprintf (stderr, "[keystring synthesis attempted on %s]\n", keystring);
1179 while (keystring && (unichar = g_utf8_get_char (keystring))) {
1183 mbytes = g_unichar_to_utf8 (unichar, bytes);
1184 bytes[mbytes] = '\0';
1186 fprintf (stderr, "[unichar %s]", bytes);
1188 keysym = keysym_for_unichar (controller, unichar);
1189 if (keysym == NoSymbol) {
1191 fprintf (stderr, "no keysym for %s", bytes);
1196 keysyms[i++] = keysym;
1197 keystring = g_utf8_next_char (keystring);
1200 XSynchronize (spi_get_display (), TRUE);
1201 for (i = 0; keysyms[i]; ++i) {
1202 if (!spi_dec_synth_keysym (controller, keysyms[i])) {
1204 fprintf (stderr, "could not synthesize %c\n",
1211 XSynchronize (spi_get_display (), FALSE);
1215 if (synth_type == Accessibility_KEY_SYM) {
1219 keysym = XkbKeycodeToKeysym (spi_get_display (), keycode, 0, 0);
1221 if (XkbKeysymToModifiers (spi_get_display (), keysym) == 0) {
1222 spi_dec_clear_unlatch_pending (controller);
1229 isEvent(Display *dpy, XEvent *event, char *arg)
1235 handle_io (GIOChannel *source,
1236 GIOCondition condition,
1239 SpiDEController *controller = (SpiDEController *) data;
1240 DEControllerPrivateData *priv = controller->priv;
1241 gboolean is_consumed = FALSE;
1244 while (XCheckIfEvent(priv->xevie_display, &ev, isEvent, NULL))
1246 if (ev.type == KeyPress || ev.type == KeyRelease)
1247 is_consumed = spi_device_event_controller_forward_key_event (controller, &ev);
1250 XevieSendEvent(priv->xevie_display, &ev, XEVIE_UNMODIFIED);
1255 #endif /* HAVE_XEVIE */
1258 spi_dec_x11_init (SpiDEController *controller)
1260 DEControllerPrivateData *priv = controller->priv;
1261 spi_events_init (spi_get_display());
1265 #endif /* HAVE_XEVIE */
1267 spi_events_init (spi_get_display ());
1269 priv->xevie_display = XOpenDisplay(NULL);
1271 if (XevieStart(priv->xevie_display) == TRUE)
1273 #ifdef SPI_KEYEVENT_DEBUG
1274 fprintf (stderr, "XevieStart() success \n");
1276 XevieSelectInput(priv->xevie_display, KeyPressMask | KeyReleaseMask);
1278 fd = ConnectionNumber(priv->xevie_display);
1279 ioc = g_io_channel_unix_new (fd);
1280 g_io_add_watch (ioc, G_IO_IN | G_IO_HUP, handle_io, controller);
1281 g_io_channel_unref (ioc);
1285 #ifdef SPI_KEYEVENT_DEBUG
1286 fprintf (stderr, "XevieStart() failed, only one client is allowed to do event int exception\n");
1289 #endif /* HAVE_XEVIE */
1291 gettimeofday (&priv->last_press_time, NULL);
1292 gettimeofday (&priv->last_release_time, NULL);
1293 spi_controller_register_with_devices (controller);
1295 spi_dec_init_mouse_listener (controller);
1297 saved_controller = controller;
1301 spi_dec_x11_finalize (SpiDEController *controller)
1303 DEControllerPrivateData *priv = controller->priv;
1304 /* disconnect any special listeners, get rid of outstanding keygrabs */
1305 XUngrabKey (spi_get_display (), AnyKey, AnyModifier, DefaultRootWindow (spi_get_display ()));
1308 if (priv->xevie_display != NULL)
1310 XevieEnd(priv->xevie_display);
1311 #ifdef SPI_KEYEVENT_DEBUG
1312 printf("XevieEnd(dpy) finished \n");
1318 XkbFreeKeyboard (priv->xkb_desc, 0, True);
1319 /* TODO: Should free the keymap */
1323 spi_device_event_controller_forward_key_event (SpiDEController *controller,
1324 const XEvent *event)
1326 DEControllerPrivateData *priv = controller->priv;
1327 Accessibility_DeviceEvent key_event;
1330 g_assert (event->type == KeyPress || event->type == KeyRelease);
1332 key_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1334 if (priv->xevie_display == NULL)
1335 spi_controller_update_key_grabs (controller, &key_event);
1337 /* relay to listeners, and decide whether to consume it or not */
1338 ret = spi_controller_notify_keylisteners (controller, &key_event, TRUE);
1339 g_free(key_event.event_string);
1345 is_key_released (long code)
1350 XQueryKeymap (spi_get_display (), keys);
1351 down = BIT (keys, code);
1356 check_release (gpointer data)
1359 Accessibility_DeviceEvent *event = (Accessibility_DeviceEvent *)data;
1360 KeyCode code = event->hw_code;
1362 released = is_key_released (code);
1366 check_release_handler = 0;
1367 event->type = Accessibility_KEY_RELEASED_EVENT;
1368 spi_controller_notify_keylisteners (saved_controller, event, TRUE);
1370 return (released == 0);
1374 wait_for_release_event (XEvent *event,
1375 SpiDEController *controller)
1377 pressed_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1378 check_release_handler = g_timeout_add (CHECK_RELEASE_DELAY, check_release, &pressed_event);
1382 spi_dec_x11_emit_modifier_event (SpiDEController *controller, guint prev_mask,
1385 dbus_uint32_t d1, d2;
1387 #ifdef SPI_XKB_DEBUG
1388 fprintf (stderr, "MODIFIER CHANGE EVENT! %x to %x\n",
1389 prev_mask, current_mask);
1392 /* set bits for the virtual modifiers like NUMLOCK */
1393 if (prev_mask & _numlock_physical_mask)
1394 prev_mask |= SPI_KEYMASK_NUMLOCK;
1395 if (current_mask & _numlock_physical_mask)
1396 current_mask |= SPI_KEYMASK_NUMLOCK;
1398 d1 = prev_mask & key_modifier_mask;
1399 d2 = current_mask & key_modifier_mask;
1400 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_KEYBOARD, "Modifiers", "", d1, d2);
1404 spi_dec_x11_generate_mouse_event (SpiDEController *controller,
1407 const char *eventName)
1410 gboolean err = FALSE;
1411 Display *display = spi_get_display ();
1413 switch (eventName[0])
1416 switch (eventName[1])
1418 /* TODO: check number of buttons before parsing */
1439 if (x != -1 && y != -1)
1441 XTestFakeMotionEvent (display, DefaultScreen (display),
1444 XTestFakeButtonEvent (display, button, !(eventName[2] == 'r'), 0);
1445 if (eventName[2] == 'c')
1446 XTestFakeButtonEvent (display, button, FALSE, 1);
1447 else if (eventName[2] == 'd')
1449 XTestFakeButtonEvent (display, button, FALSE, 1);
1450 XTestFakeButtonEvent (display, button, TRUE, 2);
1451 XTestFakeButtonEvent (display, button, FALSE, 3);
1455 case 'r': /* relative motion */
1456 XTestFakeRelativeMotionEvent (display, x, y, 0);
1458 case 'a': /* absolute motion */
1459 XTestFakeMotionEvent (display, DefaultScreen (display),
1466 spi_dec_setup_x11 (SpiDEControllerClass *klass)
1468 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1470 klass->plat.get_keycode = spi_dec_x11_get_keycode;
1471 klass->plat.mouse_check = spi_dec_x11_mouse_check;
1472 klass->plat.synth_keycode_press = spi_dec_x11_synth_keycode_press;
1473 klass->plat.synth_keycode_release = spi_dec_x11_synth_keycode_release;
1474 klass->plat.lock_modifiers = spi_dec_x11_lock_modifiers;
1475 klass->plat.unlock_modifiers = spi_dec_x11_unlock_modifiers;
1476 klass->plat.synth_keystring = spi_dec_x11_synth_keystring;
1477 klass->plat.grab_key = spi_dec_x11_grab_key;
1478 klass->plat.ungrab_key = spi_dec_x11_ungrab_key;
1479 klass->plat.emit_modifier_event = spi_dec_x11_emit_modifier_event;
1480 klass->plat.generate_mouse_event = spi_dec_x11_generate_mouse_event;
1482 klass->plat.init = spi_dec_x11_init;
1483 klass->plat.finalize = spi_dec_x11_finalize;
1485 g_type_class_add_private (object_class, sizeof (DEControllerPrivateData));