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);
240 g_source_set_name_by_id (priv->reserved_reset_timeout, "[at-spi2-core] spi_dec_reset_reserved");
247 *modmask = keysym_mod_mask (keysym, keycode);
252 spi_dec_set_unlatch_pending (SpiDEController *controller, unsigned mask)
254 DEControllerPrivateData *priv = controller->priv;
256 if (priv->xkb_latch_mask) fprintf (stderr, "unlatch pending! %x\n",
257 priv->xkb_latch_mask);
259 priv->pending_xkb_mod_relatch_mask |= priv->xkb_latch_mask;
263 spi_dec_button_update_and_emit (SpiDEController *controller,
266 Accessibility_DeviceEvent mouse_e;
267 gchar event_detail[3];
268 gboolean is_consumed = FALSE;
270 if ((mask_return & mouse_button_mask) !=
271 (mouse_mask_state & mouse_button_mask))
273 int button_number = 0;
274 gboolean is_down = False;
276 if (!(mask_return & Button1Mask) &&
277 (mouse_mask_state & Button1Mask))
279 mouse_mask_state &= ~Button1Mask;
282 else if ((mask_return & Button1Mask) &&
283 !(mouse_mask_state & Button1Mask))
285 mouse_mask_state |= Button1Mask;
289 else if (!(mask_return & Button2Mask) &&
290 (mouse_mask_state & Button2Mask))
292 mouse_mask_state &= ~Button2Mask;
295 else if ((mask_return & Button2Mask) &&
296 !(mouse_mask_state & Button2Mask))
298 mouse_mask_state |= Button2Mask;
302 else if (!(mask_return & Button3Mask) &&
303 (mouse_mask_state & Button3Mask))
305 mouse_mask_state &= ~Button3Mask;
308 else if ((mask_return & Button3Mask) &&
309 !(mouse_mask_state & Button3Mask))
311 mouse_mask_state |= Button3Mask;
315 else if (!(mask_return & Button4Mask) &&
316 (mouse_mask_state & Button4Mask))
318 mouse_mask_state &= ~Button4Mask;
321 else if ((mask_return & Button4Mask) &&
322 !(mouse_mask_state & Button4Mask))
324 mouse_mask_state |= Button4Mask;
328 else if (!(mask_return & Button5Mask) &&
329 (mouse_mask_state & Button5Mask))
331 mouse_mask_state &= ~Button5Mask;
334 else if ((mask_return & Button5Mask) &&
335 !(mouse_mask_state & Button5Mask))
337 mouse_mask_state |= Button5Mask;
343 fprintf (stderr, "Button %d %s\n",
344 button_number, (is_down) ? "Pressed" : "Released");
346 snprintf (event_detail, 3, "%d%c", button_number,
347 (is_down) ? 'p' : 'r');
348 /* TODO: FIXME distinguish between physical and
351 mouse_e.type = (is_down) ?
352 Accessibility_BUTTON_PRESSED_EVENT :
353 Accessibility_BUTTON_RELEASED_EVENT;
354 mouse_e.id = button_number;
355 mouse_e.hw_code = button_number;
356 mouse_e.modifiers = (dbus_uint16_t) mouse_mask_state;
357 mouse_e.timestamp = 0;
358 mouse_e.event_string = "";
359 mouse_e.is_text = FALSE;
361 spi_controller_notify_mouselisteners (controller,
365 dbus_uint32_t x = last_mouse_pos->x, y = last_mouse_pos->y;
366 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Button", event_detail, x, y);
369 spi_dec_set_unlatch_pending (controller, mask_return);
380 spi_dec_x11_mouse_check (SpiDEController *controller,
381 int *x, int *y, gboolean *moved)
383 int win_x_return,win_y_return;
384 unsigned int mask_return;
385 Window root_return, child_return;
386 Display *display = spi_get_display ();
389 XQueryPointer(display, DefaultRootWindow (display),
390 &root_return, &child_return,
392 &win_x_return, &win_y_return, &mask_return);
394 * Since many clients grab the pointer, and X goes an automatic
395 * pointer grab on mouse-down, we often must detect mouse button events
396 * by polling rather than via a button grab.
397 * The while loop (rather than if) is used since it's possible that
398 * multiple buttons have changed state since we last checked.
400 if (mask_return != mouse_mask_state)
402 while (spi_dec_button_update_and_emit (controller, mask_return));
405 if (*x != last_mouse_pos->x || *y != last_mouse_pos->y)
407 // TODO: combine these two signals?
408 dbus_uint32_t ix = *x, iy = *y;
409 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Abs", "", ix, iy);
410 ix -= last_mouse_pos->x;
411 iy -= last_mouse_pos->y;
412 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Rel", "", ix, iy);
413 last_mouse_pos->x = *x;
414 last_mouse_pos->y = *y;
425 #ifdef WE_NEED_UGRAB_MOUSE
427 spi_dec_ungrab_mouse (gpointer data)
429 Display *display = spi_get_display ();
432 XUngrabButton (spi_get_display (), AnyButton, AnyModifier,
433 XDefaultRootWindow (spi_get_display ()));
440 spi_dec_init_mouse_listener (SpiDEController *dec)
443 Display *display = spi_get_display ();
447 if (XGrabButton (display, AnyButton, AnyModifier,
448 spi_get_root_window (),
449 True, ButtonPressMask | ButtonReleaseMask,
450 GrabModeSync, GrabModeAsync, None, None) != Success) {
452 fprintf (stderr, "WARNING: could not grab mouse buttons!\n");
456 XSync (display, False);
458 fprintf (stderr, "mouse buttons grabbed\n");
465 spi_dec_clear_unlatch_pending (SpiDEController *controller)
467 DEControllerPrivateData *priv = controller->priv;
468 priv->xkb_latch_mask = 0;
472 spi_device_event_controller_forward_mouse_event (SpiDEController *controller,
475 Accessibility_DeviceEvent mouse_e;
476 gchar event_detail[3];
477 gboolean is_consumed = FALSE;
478 gboolean xkb_mod_unlatch_occurred;
479 XButtonEvent *xbutton_event = (XButtonEvent *) xevent;
480 dbus_uint32_t ix, iy;
482 int button = xbutton_event->button;
484 unsigned int mouse_button_state = xbutton_event->state;
489 mouse_button_state |= Button1Mask;
492 mouse_button_state |= Button2Mask;
495 mouse_button_state |= Button3Mask;
498 mouse_button_state |= Button4Mask;
501 mouse_button_state |= Button5Mask;
505 last_mouse_pos->x = ((XButtonEvent *) xevent)->x_root;
506 last_mouse_pos->y = ((XButtonEvent *) xevent)->y_root;
509 fprintf (stderr, "mouse button %d %s (%x)\n",
510 xbutton_event->button,
511 (xevent->type == ButtonPress) ? "Press" : "Release",
514 snprintf (event_detail, 3, "%d%c", button,
515 (xevent->type == ButtonPress) ? 'p' : 'r');
517 /* TODO: FIXME distinguish between physical and logical buttons */
518 mouse_e.type = (xevent->type == ButtonPress) ?
519 Accessibility_BUTTON_PRESSED_EVENT :
520 Accessibility_BUTTON_RELEASED_EVENT;
522 mouse_e.hw_code = button;
523 mouse_e.modifiers = (dbus_uint16_t) xbutton_event->state;
524 mouse_e.timestamp = (dbus_uint32_t) xbutton_event->time;
525 mouse_e.event_string = "";
526 mouse_e.is_text = FALSE;
527 if ((mouse_button_state & mouse_button_mask) !=
528 (mouse_mask_state & mouse_button_mask))
530 if ((mouse_mask_state & key_modifier_mask) !=
531 (mouse_button_state & key_modifier_mask))
532 spi_dec_x11_emit_modifier_event (controller,
533 mouse_mask_state, mouse_button_state);
534 mouse_mask_state = mouse_button_state;
536 spi_controller_notify_mouselisteners (controller, &mouse_e);
537 ix = last_mouse_pos->x;
538 iy = last_mouse_pos->y;
539 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Button", event_detail, ix, iy);
542 xkb_mod_unlatch_occurred = (xevent->type == ButtonPress ||
543 xevent->type == ButtonRelease);
545 /* if client wants to consume this event, and XKB latch state was
546 * unset by this button event, we reset it
548 if (is_consumed && xkb_mod_unlatch_occurred)
549 spi_dec_set_unlatch_pending (controller, mouse_mask_state);
551 XAllowEvents (spi_get_display (),
552 (is_consumed) ? SyncPointer : ReplayPointer,
557 global_filter_fn (XEvent *xevent, void *data)
559 SpiDEController *controller;
560 DEControllerPrivateData *priv;
561 Display *display = spi_get_display ();
562 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
563 priv = controller->priv;
565 if (xevent->type == MappingNotify)
568 if (xevent->type == KeyPress || xevent->type == KeyRelease)
570 if (priv->xevie_display == NULL)
572 gboolean is_consumed;
575 spi_device_event_controller_forward_key_event (controller, xevent);
582 n_events = XPending (display);
584 #ifdef SPI_KEYEVENT_DEBUG
585 g_print ("Number of events pending: %d\n", n_events);
587 for (i = 0; i < n_events; i++)
589 XNextEvent (display, &next_event);
590 if (next_event.type != KeyPress &&
591 next_event.type != KeyRelease)
592 g_warning ("Unexpected event type %d in queue", next_event.type);
595 XAllowEvents (display, AsyncKeyboard, CurrentTime);
597 XUngrabKeyboard (display, CurrentTime);
601 if (xevent->type == KeyPress)
602 wait_for_release_event (xevent, controller);
603 XAllowEvents (display, ReplayKeyboard, CurrentTime);
609 if (xevent->type == ButtonPress || xevent->type == ButtonRelease)
611 spi_device_event_controller_forward_mouse_event (controller, xevent);
613 if (xevent->type == priv->xkb_base_event_code)
615 XkbAnyEvent * xkb_ev = (XkbAnyEvent *) xevent;
616 /* ugly but probably necessary...*/
617 XSynchronize (display, TRUE);
619 if (xkb_ev->xkb_type == XkbStateNotify)
621 XkbStateNotifyEvent *xkb_snev =
622 (XkbStateNotifyEvent *) xkb_ev;
623 /* check the mouse, to catch mouse events grabbed by
624 * another client; in case we should revert this XKB delatch
626 if (!priv->pending_xkb_mod_relatch_mask)
630 spi_dec_x11_mouse_check (controller, &x, &y, &moved);
632 /* we check again, since the previous call may have
634 if (priv->pending_xkb_mod_relatch_mask)
636 unsigned int feedback_mask;
638 fprintf (stderr, "relatching %x\n",
639 priv->pending_xkb_mod_relatch_mask);
641 /* temporarily turn off the latch bell, if it's on */
642 XkbGetControls (display,
643 XkbAccessXFeedbackMask,
645 feedback_mask = priv->xkb_desc->ctrls->ax_options;
646 if (feedback_mask & XkbAX_StickyKeysFBMask)
648 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
650 priv->xkb_desc->ctrls->ax_options
651 &= ~(XkbAX_StickyKeysFBMask);
652 XkbChangeControls (display, priv->xkb_desc, &changes);
654 /* TODO: account for lock as well as latch */
655 XkbLatchModifiers (display,
657 priv->pending_xkb_mod_relatch_mask,
658 priv->pending_xkb_mod_relatch_mask);
659 if (feedback_mask & XkbAX_StickyKeysFBMask)
661 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
663 priv->xkb_desc->ctrls->ax_options = feedback_mask;
664 XkbChangeControls (display, priv->xkb_desc, &changes);
667 fprintf (stderr, "relatched %x\n",
668 priv->pending_xkb_mod_relatch_mask);
670 priv->pending_xkb_mod_relatch_mask = 0;
674 priv->xkb_latch_mask = xkb_snev->latched_mods;
677 XSynchronize (display, FALSE);
684 _spi_controller_device_error_handler (Display *display, XErrorEvent *error)
686 if (error->error_code == BadAccess)
688 g_message ("Could not complete key grab: grab already in use.\n");
689 spi_error_code = BadAccess;
694 return (*x_default_error_handler) (display, error);
699 spi_controller_register_with_devices (SpiDEController *controller)
701 DEControllerPrivateData *priv;
702 int event_base, error_base, major_version, minor_version;
704 priv = controller->priv;
705 if (XTestQueryExtension (spi_get_display(), &event_base, &error_base, &major_version, &minor_version))
707 XTestGrabControl (spi_get_display (), True);
710 /* calls to device-specific implementations and routines go here */
711 /* register with: keyboard hardware code handler */
712 /* register with: (translated) keystroke handler */
714 priv->have_xkb = XkbQueryExtension (spi_get_display (),
715 &priv->xkb_major_extension_opcode,
716 &priv->xkb_base_event_code,
717 &priv->xkb_base_error_code, NULL, NULL);
721 guint64 reserved = 0;
722 priv->xkb_desc = XkbGetMap (spi_get_display (), XkbKeySymsMask, XkbUseCoreKbd);
723 XkbSelectEvents (spi_get_display (),
725 XkbStateNotifyMask, XkbStateNotifyMask);
726 _numlock_physical_mask = XkbKeysymToModifiers (spi_get_display (),
728 for (i = priv->xkb_desc->max_key_code; i >= priv->xkb_desc->min_key_code; --i)
730 if (priv->xkb_desc->map->key_sym_map[i].kt_index[0] == XkbOneLevelIndex)
732 if (XkbKeycodeToKeysym (spi_get_display (), i, 0, 0) != 0)
734 /* don't use this one if there's a grab client! */
736 /* Runtime errors are generated from these functions,
737 * that are then quashed. Equivalent to:
745 XGrabKey (spi_get_display (), i, 0,
746 spi_get_root_window (),
748 GrabModeSync, GrabModeSync);
749 XSync (spi_get_display (), TRUE);
750 XUngrabKey (spi_get_display (), i, 0,
751 spi_get_root_window ());
752 if (!spi_x_error_release ())
762 priv->reserved_keycode = reserved;
763 priv->reserved_keysym = XkbKeycodeToKeysym (spi_get_display (), reserved, 0, 0);
767 priv->reserved_keycode = XKeysymToKeycode (spi_get_display (), XK_numbersign);
768 priv->reserved_keysym = XK_numbersign;
770 #ifdef SPI_RESERVED_DEBUG
772 sym = XKeycodeToKeysym (spi_get_display (), reserved, 0);
773 fprintf (stderr, "%x\n", sym);
774 fprintf (stderr, "setting the reserved keycode to %d (%s)\n",
776 XKeysymToString (XKeycodeToKeysym (spi_get_display (),
781 spi_set_filter (global_filter_fn, controller);
782 spi_set_events (KeyPressMask | KeyReleaseMask);
784 x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
787 static Accessibility_DeviceEvent
788 spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
790 Accessibility_DeviceEvent key_event;
792 const int cbuf_bytes = 20;
796 nbytes = XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL);
797 key_event.id = (dbus_int32_t)(keysym);
798 key_event.hw_code = (dbus_int16_t) x_key_event->keycode;
799 if (((XEvent *) x_key_event)->type == KeyPress)
801 key_event.type = Accessibility_KEY_PRESSED_EVENT;
805 key_event.type = Accessibility_KEY_RELEASED_EVENT;
807 key_event.modifiers = (dbus_uint16_t)(x_key_event->state);
808 key_event.is_text = FALSE;
812 key_event.event_string = g_strdup ("space");
815 key_event.event_string = g_strdup ("Tab");
818 key_event.event_string = g_strdup ("Backspace");
821 key_event.event_string = g_strdup ("Return");
824 key_event.event_string = g_strdup ("Home");
827 key_event.event_string = g_strdup ("Page_Down");
830 key_event.event_string = g_strdup ("Page_Up");
833 key_event.event_string = g_strdup ("F1");
836 key_event.event_string = g_strdup ("F2");
839 key_event.event_string = g_strdup ("F3");
842 key_event.event_string = g_strdup ("F4");
845 key_event.event_string = g_strdup ("F5");
848 key_event.event_string = g_strdup ("F6");
851 key_event.event_string = g_strdup ("F7");
854 key_event.event_string = g_strdup ("F8");
857 key_event.event_string = g_strdup ("F9");
860 key_event.event_string = g_strdup ("F10");
863 key_event.event_string = g_strdup ("F11");
866 key_event.event_string = g_strdup ("F12");
869 key_event.event_string = g_strdup ("End");
872 key_event.event_string = g_strdup ("Escape");
875 key_event.event_string = g_strdup ("Up");
878 key_event.event_string = g_strdup ("Down");
881 key_event.event_string = g_strdup ("Left");
884 key_event.event_string = g_strdup ("Right");
890 cbuf[nbytes] = '\0'; /* OK since length is cbuf_bytes+1 */
891 key_event.event_string = g_strdup (cbuf);
892 c = keysym2ucs (keysym);
893 if (c > 0 && !g_unichar_iscntrl (c))
895 key_event.is_text = TRUE;
896 /* incorrect for some composed chars? */
901 key_event.event_string = g_strdup ("");
905 key_event.timestamp = (dbus_uint32_t) x_key_event->time;
906 #ifdef SPI_KEYEVENT_DEBUG
908 char *pressed_str = "pressed";
909 char *released_str = "released";
912 if (key_event.type == Accessibility_KEY_PRESSED_EVENT)
913 state_ptr = pressed_str;
915 state_ptr = released_str;
918 "Key %lu %s (%c), modifiers %d; string=%s [%x] %s\n",
919 (unsigned long) keysym,
921 keysym ? (int) keysym : '*',
922 (int) x_key_event->state,
923 key_event.event_string,
924 key_event.event_string[0],
925 (key_event.is_text == TRUE) ? "(text)" : "(not text)");
929 fprintf (stderr, "%s%c\n",
930 (x_key_event->state & Mod1Mask)?"Alt-":"",
931 ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
932 g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
933 fprintf (stderr, "serial: %x Time: %x\n", x_key_event->serial, x_key_event->time);
934 #endif /* SPI_DEBUG */
939 spi_dec_x11_grab_key (SpiDEController *controller,
941 Accessibility_ControllerEventMask mod_mask)
943 XGrabKey (spi_get_display (),
946 spi_get_root_window (),
950 XSync (spi_get_display (), False);
951 return spi_clear_error_state ();
955 spi_dec_x11_ungrab_key (SpiDEController *controller,
957 Accessibility_ControllerEventMask mod_mask)
959 XUngrabKey (spi_get_display (),
962 spi_get_root_window ());
966 xkb_get_slowkeys_delay (SpiDEController *controller)
968 unsigned int retval = 0;
969 DEControllerPrivateData *priv = controller->priv;
971 #ifdef XKB_HAS_GET_SLOW_KEYS_DELAY
972 retval = XkbGetSlowKeysDelay (spi_get_display (),
973 XkbUseCoreKbd, &bounce_delay);
975 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
977 Status s = XkbGetControls (spi_get_display (),
978 XkbAllControlsMask, priv->xkb_desc);
981 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbSlowKeysMask)
982 retval = priv->xkb_desc->ctrls->slow_keys_delay;
988 fprintf (stderr, "SlowKeys delay: %d\n", (int) retval);
994 xkb_get_bouncekeys_delay (SpiDEController *controller)
996 unsigned int retval = 0;
997 DEControllerPrivateData *priv = controller->priv;
999 #ifdef XKB_HAS_GET_BOUNCE_KEYS_DELAY
1000 retval = XkbGetBounceKeysDelay (spi_get_display (),
1001 XkbUseCoreKbd, &bounce_delay);
1003 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
1005 Status s = XkbGetControls (spi_get_display (),
1006 XkbAllControlsMask, priv->xkb_desc);
1009 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbBounceKeysMask)
1010 retval = priv->xkb_desc->ctrls->debounce_delay;
1015 #ifdef SPI_XKB_DEBUG
1016 fprintf (stderr, "BounceKeys delay: %d\n", (int) retval);
1022 spi_dec_x11_synth_keycode_press (SpiDEController *controller,
1023 unsigned int keycode)
1025 unsigned int time = CurrentTime;
1026 unsigned int bounce_delay;
1027 unsigned int elapsed_msec;
1029 DEControllerPrivateData *priv = controller->priv;
1031 spi_x_error_trap ();
1032 if (keycode == priv->last_release_keycode)
1034 bounce_delay = xkb_get_bouncekeys_delay (controller);
1037 gettimeofday (&tv, NULL);
1039 (tv.tv_sec - priv->last_release_time.tv_sec) * 1000
1040 + (tv.tv_usec - priv->last_release_time.tv_usec) / 1000;
1041 #ifdef SPI_XKB_DEBUG
1042 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1043 (long) (tv.tv_usec - priv->last_release_time.tv_usec));
1045 #ifdef THIS_IS_BROKEN
1046 if (elapsed_msec < bounce_delay)
1047 time = bounce_delay - elapsed_msec + 1;
1049 time = bounce_delay + 10;
1050 /* fudge for broken XTest */
1052 #ifdef SPI_XKB_DEBUG
1053 fprintf (stderr, "waiting %d ms\n", time);
1057 XTestFakeKeyEvent (spi_get_display (), keycode, True, time);
1058 priv->last_press_keycode = keycode;
1059 XFlush (spi_get_display ());
1060 XSync (spi_get_display (), False);
1061 gettimeofday (&priv->last_press_time, NULL);
1066 spi_dec_x11_synth_keycode_release (SpiDEController *controller,
1067 unsigned int keycode)
1069 unsigned int time = CurrentTime;
1070 unsigned int slow_delay;
1071 unsigned int elapsed_msec;
1073 DEControllerPrivateData *priv = controller->priv;
1075 spi_x_error_trap ();
1076 if (keycode == priv->last_press_keycode)
1078 slow_delay = xkb_get_slowkeys_delay (controller);
1081 gettimeofday (&tv, NULL);
1083 (tv.tv_sec - priv->last_press_time.tv_sec) * 1000
1084 + (tv.tv_usec - priv->last_press_time.tv_usec) / 1000;
1085 #ifdef SPI_XKB_DEBUG
1086 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1087 (long) (tv.tv_usec - priv->last_press_time.tv_usec));
1089 #ifdef THIS_IS_BROKEN_DUNNO_WHY
1090 if (elapsed_msec < slow_delay)
1091 time = slow_delay - elapsed_msec + 1;
1093 time = slow_delay + 10;
1094 /* our XTest seems broken, we have to add slop as above */
1096 #ifdef SPI_XKB_DEBUG
1097 fprintf (stderr, "waiting %d ms\n", time);
1101 XTestFakeKeyEvent (spi_get_display (), keycode, False, time);
1102 priv->last_release_keycode = keycode;
1103 XSync (spi_get_display (), False);
1104 gettimeofday (&priv->last_release_time, NULL);
1109 spi_dec_x11_lock_modifiers (SpiDEController *controller, unsigned modifiers)
1111 DEControllerPrivateData *priv = controller->priv;
1113 if (priv->have_xkb) {
1114 return XkbLockModifiers (spi_get_display (), XkbUseCoreKbd,
1115 modifiers, modifiers);
1119 xmkeymap = XGetModifierMapping(spi_get_display ());
1120 for (mod_index=0;mod_index<8;mod_index++)
1121 if (modifiers & (1<<mod_index))
1122 spi_dec_x11_synth_keycode_press(controller, xmkeymap->modifiermap[mod_index]);
1128 spi_dec_x11_unlock_modifiers (SpiDEController *controller, unsigned modifiers)
1130 DEControllerPrivateData *priv = controller->priv;
1132 if (priv->have_xkb) {
1133 return XkbLockModifiers (spi_get_display (), XkbUseCoreKbd,
1138 xmkeymap = XGetModifierMapping(spi_get_display ());
1140 for (mod_index=0;mod_index<8;mod_index++)
1141 if (modifiers & (1<<mod_index))
1142 spi_dec_x11_synth_keycode_release(controller, xmkeymap->modifiermap[mod_index]);
1148 keysym_for_unichar (SpiDEController *controller, gunichar unichar)
1150 return (KeySym) ucs2keysym ((long) unichar);
1154 spi_dec_x11_synth_keystring (SpiDEController *controller, guint synth_type, gint keycode, const char *keystring)
1156 /* probably we need to create and inject an XIM handler eventually. */
1157 /* for now, try to match the string to existing
1158 * keycode+modifier states.
1162 gunichar unichar = 0;
1164 gboolean retval = TRUE;
1168 if (!(keystring && *keystring && g_utf8_validate (keystring, -1, &c))) {
1173 fprintf (stderr, "[keystring synthesis attempted on %s]\n", keystring);
1175 maxlen = strlen (keystring) + 1;
1176 keysyms = g_new0 (KeySym, maxlen);
1178 while (keystring && (unichar = g_utf8_get_char (keystring))) {
1182 mbytes = g_unichar_to_utf8 (unichar, bytes);
1183 bytes[mbytes] = '\0';
1185 fprintf (stderr, "[unichar %s]", bytes);
1187 keysym = keysym_for_unichar (controller, unichar);
1188 if (keysym == NoSymbol) {
1190 fprintf (stderr, "no keysym for %s", bytes);
1195 keysyms[i++] = keysym;
1196 keystring = g_utf8_next_char (keystring);
1199 XSynchronize (spi_get_display (), TRUE);
1200 for (i = 0; keysyms[i]; ++i) {
1201 if (!spi_dec_synth_keysym (controller, keysyms[i])) {
1203 fprintf (stderr, "could not synthesize %c\n",
1210 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);
1379 g_source_set_name_by_id (check_release_handler, "[at-spi2-core] check_release");
1383 spi_dec_x11_emit_modifier_event (SpiDEController *controller, guint prev_mask,
1386 dbus_uint32_t d1, d2;
1388 #ifdef SPI_XKB_DEBUG
1389 fprintf (stderr, "MODIFIER CHANGE EVENT! %x to %x\n",
1390 prev_mask, current_mask);
1393 /* set bits for the virtual modifiers like NUMLOCK */
1394 if (prev_mask & _numlock_physical_mask)
1395 prev_mask |= SPI_KEYMASK_NUMLOCK;
1396 if (current_mask & _numlock_physical_mask)
1397 current_mask |= SPI_KEYMASK_NUMLOCK;
1399 d1 = prev_mask & key_modifier_mask;
1400 d2 = current_mask & key_modifier_mask;
1401 spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_KEYBOARD, "Modifiers", "", d1, d2);
1405 spi_dec_x11_generate_mouse_event (SpiDEController *controller,
1408 const char *eventName)
1411 gboolean err = FALSE;
1412 Display *display = spi_get_display ();
1414 switch (eventName[0])
1417 switch (eventName[1])
1419 /* TODO: check number of buttons before parsing */
1440 if (x != -1 && y != -1)
1442 XTestFakeMotionEvent (display, DefaultScreen (display),
1445 XTestFakeButtonEvent (display, button, !(eventName[2] == 'r'), 0);
1446 if (eventName[2] == 'c')
1447 XTestFakeButtonEvent (display, button, FALSE, 1);
1448 else if (eventName[2] == 'd')
1450 XTestFakeButtonEvent (display, button, FALSE, 1);
1451 XTestFakeButtonEvent (display, button, TRUE, 2);
1452 XTestFakeButtonEvent (display, button, FALSE, 3);
1456 case 'r': /* relative motion */
1457 XTestFakeRelativeMotionEvent (display, x, y, 0);
1459 case 'a': /* absolute motion */
1460 XTestFakeMotionEvent (display, DefaultScreen (display),
1467 spi_dec_setup_x11 (SpiDEControllerClass *klass)
1469 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1471 klass->plat.get_keycode = spi_dec_x11_get_keycode;
1472 klass->plat.mouse_check = spi_dec_x11_mouse_check;
1473 klass->plat.synth_keycode_press = spi_dec_x11_synth_keycode_press;
1474 klass->plat.synth_keycode_release = spi_dec_x11_synth_keycode_release;
1475 klass->plat.lock_modifiers = spi_dec_x11_lock_modifiers;
1476 klass->plat.unlock_modifiers = spi_dec_x11_unlock_modifiers;
1477 klass->plat.synth_keystring = spi_dec_x11_synth_keystring;
1478 klass->plat.grab_key = spi_dec_x11_grab_key;
1479 klass->plat.ungrab_key = spi_dec_x11_ungrab_key;
1480 klass->plat.emit_modifier_event = spi_dec_x11_emit_modifier_event;
1481 klass->plat.generate_mouse_event = spi_dec_x11_generate_mouse_event;
1483 klass->plat.init = spi_dec_x11_init;
1484 klass->plat.finalize = spi_dec_x11_finalize;
1486 g_type_class_add_private (object_class, sizeof (DEControllerPrivateData));