1 /* AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002 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.c: implement the DeviceEventController interface */
30 #define SPI_KEYEVENT_DEBUG
36 #include <bonobo/bonobo-exception.h>
39 #include <X11/extensions/XTest.h>
40 #include <X11/XKBlib.h>
42 #include <X11/keysymdef.h>
44 #include <gdk/gdkx.h> /* TODO: hide dependency (wrap in single porting file) */
45 #include <gdk/gdkkeysyms.h>
46 #include <gdk/gdkwindow.h>
48 #include "../libspi/spi-private.h"
49 #include "deviceeventcontroller.h"
51 /* Our parent Gtk object type */
52 #define PARENT_TYPE BONOBO_TYPE_OBJECT
54 /* A pointer to our parent object class */
55 static GObjectClass *spi_device_event_controller_parent_class;
56 static int spi_error_code = 0;
57 static GdkPoint last_mouse_pos_static = {0, 0};
58 static GdkPoint *last_mouse_pos = &last_mouse_pos_static;
59 static unsigned int mouse_mask_state = 0;
60 static unsigned int mouse_button_mask =
61 Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask;
62 static unsigned int key_modifier_mask =
63 Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask | ShiftMask | LockMask | ControlMask;
65 static GQuark spi_dec_private_quark = 0;
67 int (*x_default_error_handler) (Display *display, XErrorEvent *error_event);
71 SPI_DEVICE_TYPE_MOUSE,
72 SPI_DEVICE_TYPE_LAST_DEFINED
73 } SpiDeviceTypeCategory;
77 guint pending_add : 1;
78 guint pending_remove : 1;
80 Accessibility_ControllerEventMask mod_mask;
81 CORBA_unsigned_long key_val; /* KeyCode */
82 } DEControllerGrabMask;
86 SpiDeviceTypeCategory type;
87 Accessibility_EventTypeSeq *typeseq;
88 } DEControllerListener;
91 DEControllerListener listener;
93 Accessibility_KeySet *keys;
94 Accessibility_ControllerEventMask mask;
95 Accessibility_EventListenerMode *mode;
96 } DEControllerKeyListener;
99 unsigned int last_press_keycode;
100 unsigned int last_release_keycode;
101 struct timeval last_press_time;
102 struct timeval last_release_time;
104 int xkb_major_extension_opcode;
105 int xkb_base_event_code;
106 int xkb_base_error_code;
107 unsigned int xkb_latch_mask;
108 unsigned int pending_xkb_mod_relatch_mask;
110 } DEControllerPrivateData;
112 static void spi_controller_register_with_devices (SpiDEController *controller);
113 static gboolean spi_controller_update_key_grabs (SpiDEController *controller,
114 Accessibility_DeviceEvent *recv);
115 static gboolean spi_controller_register_device_listener (SpiDEController *controller,
116 DEControllerListener *l,
117 CORBA_Environment *ev);
118 static void spi_device_event_controller_forward_key_event (SpiDEController *controller,
119 const XEvent *event);
120 static void spi_deregister_controller_device_listener (SpiDEController *controller,
121 DEControllerListener *listener,
122 CORBA_Environment *ev);
123 static void spi_deregister_controller_key_listener (SpiDEController *controller,
124 DEControllerKeyListener *key_listener,
125 CORBA_Environment *ev);
126 static gboolean spi_controller_notify_mouselisteners (SpiDEController *controller,
127 const Accessibility_DeviceEvent *event,
128 CORBA_Environment *ev);
130 static gboolean spi_eventtype_seq_contains_event (Accessibility_EventTypeSeq *type_seq,
131 const Accessibility_DeviceEvent *event);
132 static gboolean spi_clear_error_state (void);
133 static gboolean spi_dec_poll_mouse_moved (gpointer data);
134 static gboolean spi_dec_poll_mouse_moving (gpointer data);
135 static gboolean spi_dec_poll_mouse_idle (gpointer data);
137 #define spi_get_display() GDK_DISPLAY()
139 /* Private methods */
142 keycode_for_keysym (long keysym)
144 return XKeysymToKeycode (spi_get_display (), (KeySym) keysym);
147 static DEControllerGrabMask *
148 spi_grab_mask_clone (DEControllerGrabMask *grab_mask)
150 DEControllerGrabMask *clone = g_new (DEControllerGrabMask, 1);
152 memcpy (clone, grab_mask, sizeof (DEControllerGrabMask));
154 clone->ref_count = 1;
155 clone->pending_add = TRUE;
156 clone->pending_remove = FALSE;
162 spi_grab_mask_free (DEControllerGrabMask *grab_mask)
168 spi_grab_mask_compare_values (gconstpointer p1, gconstpointer p2)
170 DEControllerGrabMask *l1 = (DEControllerGrabMask *) p1;
171 DEControllerGrabMask *l2 = (DEControllerGrabMask *) p2;
179 return ((l1->mod_mask != l2->mod_mask) || (l1->key_val != l2->key_val));
184 spi_dec_set_unlatch_pending (SpiDEController *controller, unsigned mask)
186 DEControllerPrivateData *priv =
187 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
189 if (priv->xkb_latch_mask) fprintf (stderr, "unlatch pending! %x\n", mask);
191 priv->pending_xkb_mod_relatch_mask |= priv->xkb_latch_mask;
195 spi_dec_button_update_and_emit (SpiDEController *controller,
198 CORBA_Environment ev;
199 Accessibility_Event e;
200 Accessibility_DeviceEvent mouse_e;
201 gchar event_name[24];
202 gboolean is_consumed = FALSE;
204 if ((mask_return & mouse_button_mask) !=
205 (mouse_mask_state & mouse_button_mask))
207 int button_number = 0;
208 gboolean is_down = False;
210 if (!(mask_return & Button1Mask) &&
211 (mouse_mask_state & Button1Mask))
213 mouse_mask_state &= ~Button1Mask;
216 else if ((mask_return & Button1Mask) &&
217 !(mouse_mask_state & Button1Mask))
219 mouse_mask_state |= Button1Mask;
223 else if (!(mask_return & Button2Mask) &&
224 (mouse_mask_state & Button2Mask))
226 mouse_mask_state &= ~Button2Mask;
229 else if ((mask_return & Button2Mask) &&
230 !(mouse_mask_state & Button2Mask))
232 mouse_mask_state |= Button2Mask;
236 else if (!(mask_return & Button3Mask) &&
237 (mouse_mask_state & Button3Mask))
239 mouse_mask_state &= ~Button3Mask;
242 else if ((mask_return & Button3Mask) &&
243 !(mouse_mask_state & Button3Mask))
245 mouse_mask_state |= Button3Mask;
249 else if (!(mask_return & Button4Mask) &&
250 (mouse_mask_state & Button4Mask))
252 mouse_mask_state &= ~Button4Mask;
255 else if ((mask_return & Button4Mask) &&
256 !(mouse_mask_state & Button4Mask))
258 mouse_mask_state |= Button4Mask;
262 else if (!(mask_return & Button5Mask) &&
263 (mouse_mask_state & Button5Mask))
265 mouse_mask_state &= ~Button5Mask;
268 else if ((mask_return & Button5Mask) &&
269 !(mouse_mask_state & Button5Mask))
271 mouse_mask_state |= Button5Mask;
277 fprintf (stderr, "Button %d %s\n",
278 button_number, (is_down) ? "Pressed" : "Released");
280 snprintf (event_name, 22, "mouse:button:%d%c", button_number,
281 (is_down) ? 'p' : 'r');
282 /* TODO: distinguish between physical and
285 mouse_e.type = (is_down) ?
286 Accessibility_BUTTON_PRESSED_EVENT :
287 Accessibility_BUTTON_RELEASED_EVENT;
288 mouse_e.id = button_number;
289 mouse_e.hw_code = button_number;
290 mouse_e.modifiers = (CORBA_unsigned_short) mouse_mask_state;
291 mouse_e.timestamp = 0;
292 mouse_e.event_string = "";
293 mouse_e.is_text = CORBA_FALSE;
295 spi_controller_notify_mouselisteners (controller,
299 e.source = BONOBO_OBJREF (controller->registry->desktop);
300 e.detail1 = last_mouse_pos->x;
301 e.detail2 = last_mouse_pos->y;
302 spi_init_any_nil (&e.any_data);
303 CORBA_exception_init (&ev);
306 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
311 spi_dec_set_unlatch_pending (controller, mask_return);
323 spi_dec_mouse_check (SpiDEController *controller,
324 int *x, int *y, gboolean *moved)
326 Accessibility_Event e;
327 CORBA_Environment ev;
328 int win_x_return,win_y_return;
329 unsigned int mask_return;
330 Window root_return, child_return;
331 Display *display = spi_get_display ();
334 XQueryPointer(display, DefaultRootWindow (display),
335 &root_return, &child_return,
337 &win_x_return, &win_y_return, &mask_return);
339 * Since many clients grab the pointer, and X goes an automatic
340 * pointer grab on mouse-down, we often must detect mouse button events
341 * by polling rather than via a button grab.
342 * The while loop (rather than if) is used since it's possible that
343 * multiple buttons have changed state since we last checked.
345 if (mask_return != mouse_mask_state)
347 while (spi_dec_button_update_and_emit (controller, mask_return));
350 if (*x != last_mouse_pos->x || *y != last_mouse_pos->y)
352 e.type = "mouse:abs";
353 e.source = BONOBO_OBJREF (controller->registry->desktop);
356 spi_init_any_nil (&e.any_data);
357 CORBA_exception_init (&ev);
358 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
361 e.type = "mouse:rel";
362 e.source = BONOBO_OBJREF (controller->registry->desktop);
363 e.detail1 = *x - last_mouse_pos->x;
364 e.detail2 = *y - last_mouse_pos->y;
365 spi_init_any_nil (&e.any_data);
366 CORBA_exception_init (&ev);
367 last_mouse_pos->x = *x;
368 last_mouse_pos->y = *y;
369 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
383 spi_dec_emit_modifier_event (SpiDEController *controller, guint prev_mask,
386 Accessibility_Event e;
387 CORBA_Environment ev;
390 fprintf (stderr, "MODIFIER CHANGE EVENT! %x to %x\n",
391 prev_mask, current_mask);
393 e.type = "keyboard:modifiers";
394 e.source = BONOBO_OBJREF (controller->registry->desktop);
395 e.detail1 = prev_mask & key_modifier_mask;
396 e.detail2 = current_mask & key_modifier_mask;
397 spi_init_any_nil (&e.any_data);
398 CORBA_exception_init (&ev);
399 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
405 spi_dec_poll_mouse_moved (gpointer data)
407 SpiRegistry *registry = SPI_REGISTRY (data);
408 SpiDEController *controller = registry->de_controller;
413 mask_return = spi_dec_mouse_check (controller, &x, &y, &moved);
415 if ((mask_return & key_modifier_mask) !=
416 (mouse_mask_state & key_modifier_mask))
418 spi_dec_emit_modifier_event (controller, mouse_mask_state, mask_return);
419 mouse_mask_state = mask_return;
426 spi_dec_poll_mouse_idle (gpointer data)
428 if (! spi_dec_poll_mouse_moved (data))
432 g_timeout_add (20, spi_dec_poll_mouse_moving, data);
438 spi_dec_poll_mouse_moving (gpointer data)
440 if (spi_dec_poll_mouse_moved (data))
444 g_timeout_add (100, spi_dec_poll_mouse_idle, data);
449 #ifdef WE_NEED_UGRAB_MOUSE
451 spi_dec_ungrab_mouse (gpointer data)
453 Display *display = spi_get_display ();
456 XUngrabButton (spi_get_display (), AnyButton, AnyModifier,
457 XDefaultRootWindow (spi_get_display ()));
464 spi_dec_init_mouse_listener (SpiRegistry *registry)
466 Display *display = spi_get_display ();
467 g_timeout_add (100, spi_dec_poll_mouse_idle, registry);
471 XGrabButton (display, AnyButton, AnyModifier,
472 gdk_x11_get_default_root_xwindow (),
473 True, ButtonPressMask | ButtonReleaseMask,
474 GrabModeSync, GrabModeAsync, None, None);
475 XSync (display, False);
477 fprintf (stderr, "mouse buttons grabbed\n");
482 static DEControllerKeyListener *
483 spi_dec_key_listener_new (CORBA_Object l,
484 const Accessibility_KeySet *keys,
485 const Accessibility_ControllerEventMask mask,
486 const Accessibility_EventTypeSeq *typeseq,
487 const Accessibility_EventListenerMode *mode,
488 CORBA_Environment *ev)
490 DEControllerKeyListener *key_listener = g_new0 (DEControllerKeyListener, 1);
491 key_listener->listener.object = bonobo_object_dup_ref (l, ev);
492 key_listener->listener.type = SPI_DEVICE_TYPE_KBD;
493 key_listener->keys = ORBit_copy_value (keys, TC_Accessibility_KeySet);
494 key_listener->mask = mask;
495 key_listener->listener.typeseq = ORBit_copy_value (typeseq, TC_Accessibility_EventTypeSeq);
497 key_listener->mode = ORBit_copy_value (mode, TC_Accessibility_EventListenerMode);
499 key_listener->mode = NULL;
502 g_print ("new listener, with mask %x, is_global %d, keys %p (%d)\n",
503 (unsigned int) key_listener->mask,
504 (int) (mode ? mode->global : 0),
505 (void *) key_listener->keys,
506 (int) (key_listener->keys ? key_listener->keys->_length : 0));
512 static DEControllerListener *
513 spi_dec_listener_new (CORBA_Object l,
514 const Accessibility_EventTypeSeq *typeseq,
515 CORBA_Environment *ev)
517 DEControllerListener *listener = g_new0 (DEControllerListener, 1);
518 listener->object = bonobo_object_dup_ref (l, ev);
519 listener->type = SPI_DEVICE_TYPE_MOUSE;
520 listener->typeseq = ORBit_copy_value (typeseq, TC_Accessibility_EventTypeSeq);
524 static DEControllerListener *
525 spi_listener_clone (DEControllerListener *listener, CORBA_Environment *ev)
527 DEControllerListener *clone = g_new0 (DEControllerListener, 1);
529 CORBA_Object_duplicate (listener->object, ev);
530 clone->type = listener->type;
531 clone->typeseq = ORBit_copy_value (listener->typeseq, TC_Accessibility_EventTypeSeq);
535 static DEControllerKeyListener *
536 spi_key_listener_clone (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
538 DEControllerKeyListener *clone = g_new0 (DEControllerKeyListener, 1);
539 clone->listener.object =
540 CORBA_Object_duplicate (key_listener->listener.object, ev);
541 clone->listener.type = SPI_DEVICE_TYPE_KBD;
542 clone->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
543 clone->mask = key_listener->mask;
544 clone->listener.typeseq = ORBit_copy_value (key_listener->listener.typeseq, TC_Accessibility_EventTypeSeq);
545 if (key_listener->mode)
546 clone->mode = ORBit_copy_value (key_listener->mode, TC_Accessibility_EventListenerMode);
553 spi_key_listener_data_free (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
555 CORBA_free (key_listener->listener.typeseq);
556 CORBA_free (key_listener->keys);
557 g_free (key_listener);
561 spi_key_listener_clone_free (DEControllerKeyListener *clone, CORBA_Environment *ev)
563 CORBA_Object_release (clone->listener.object, ev);
564 spi_key_listener_data_free (clone, ev);
568 spi_listener_clone_free (DEControllerListener *clone, CORBA_Environment *ev)
570 CORBA_Object_release (clone->object, ev);
571 CORBA_free (clone->typeseq);
576 spi_dec_listener_free (DEControllerListener *listener,
577 CORBA_Environment *ev)
579 bonobo_object_release_unref (listener->object, ev);
580 if (listener->type == SPI_DEVICE_TYPE_KBD)
581 spi_key_listener_data_free ((DEControllerKeyListener *) listener, ev);
585 _register_keygrab (SpiDEController *controller,
586 DEControllerGrabMask *grab_mask)
590 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
591 spi_grab_mask_compare_values);
594 DEControllerGrabMask *cur_mask = l->data;
596 cur_mask->ref_count++;
597 if (cur_mask->pending_remove)
599 cur_mask->pending_remove = FALSE;
604 controller->keygrabs_list =
605 g_list_prepend (controller->keygrabs_list,
606 spi_grab_mask_clone (grab_mask));
611 _deregister_keygrab (SpiDEController *controller,
612 DEControllerGrabMask *grab_mask)
616 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
617 spi_grab_mask_compare_values);
621 DEControllerGrabMask *cur_mask = l->data;
623 cur_mask->ref_count--;
624 if (cur_mask->ref_count <= 0)
626 cur_mask->pending_remove = TRUE;
631 DBG (1, g_warning ("De-registering non-existant grab"));
636 handle_keygrab (SpiDEController *controller,
637 DEControllerKeyListener *key_listener,
638 void (*process_cb) (SpiDEController *controller,
639 DEControllerGrabMask *grab_mask))
641 DEControllerGrabMask grab_mask = { 0 };
643 grab_mask.mod_mask = key_listener->mask;
644 if (key_listener->keys->_length == 0) /* special case means AnyKey/AllKeys */
646 grab_mask.key_val = AnyKey;
648 fprintf (stderr, "AnyKey grab!");
650 process_cb (controller, &grab_mask);
656 for (i = 0; i < key_listener->keys->_length; ++i)
658 Accessibility_KeyDefinition keydef = key_listener->keys->_buffer[i];
659 long int key_val = keydef.keysym;
660 /* X Grabs require keycodes, not keysyms */
661 if (keydef.keystring && keydef.keystring[0])
663 key_val = XStringToKeysym(keydef.keystring);
667 key_val = XKeysymToKeycode (spi_get_display (), (KeySym) key_val);
671 key_val = keydef.keycode;
673 grab_mask.key_val = key_val;
674 process_cb (controller, &grab_mask);
680 spi_controller_register_global_keygrabs (SpiDEController *controller,
681 DEControllerKeyListener *key_listener)
683 handle_keygrab (controller, key_listener, _register_keygrab);
684 return spi_controller_update_key_grabs (controller, NULL);
688 spi_controller_deregister_global_keygrabs (SpiDEController *controller,
689 DEControllerKeyListener *key_listener)
691 handle_keygrab (controller, key_listener, _deregister_keygrab);
692 spi_controller_update_key_grabs (controller, NULL);
696 spi_controller_register_device_listener (SpiDEController *controller,
697 DEControllerListener *listener,
698 CORBA_Environment *ev)
700 DEControllerKeyListener *key_listener;
702 switch (listener->type) {
703 case SPI_DEVICE_TYPE_KBD:
704 key_listener = (DEControllerKeyListener *) listener;
706 controller->key_listeners = g_list_prepend (controller->key_listeners,
708 if (key_listener->mode->global)
710 return spi_controller_register_global_keygrabs (controller, key_listener);
715 case SPI_DEVICE_TYPE_MOUSE:
716 controller->mouse_listeners = g_list_prepend (controller->mouse_listeners, listener);
719 DBG (1, g_warning ("listener registration for unknown device type.\n"));
726 spi_controller_notify_mouselisteners (SpiDEController *controller,
727 const Accessibility_DeviceEvent *event,
728 CORBA_Environment *ev)
731 GSList *notify = NULL, *l2;
732 GList **listeners = &controller->mouse_listeners;
733 gboolean is_consumed;
740 for (l = *listeners; l; l = l->next)
742 DEControllerListener *listener = l->data;
744 if (spi_eventtype_seq_contains_event (listener->typeseq, event))
746 Accessibility_DeviceEventListener ls = listener->object;
748 if (ls != CORBA_OBJECT_NIL)
750 /* we clone (don't dup) the listener, to avoid refcount inc. */
751 notify = g_slist_prepend (notify,
752 spi_listener_clone (listener, ev));
757 #ifdef SPI_KEYEVENT_DEBUG
760 g_print ("no match for event\n");
765 for (l2 = notify; l2 && !is_consumed; l2 = l2->next)
767 DEControllerListener *listener = l2->data;
768 Accessibility_DeviceEventListener ls = listener->object;
770 CORBA_exception_init (ev);
771 is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, event, ev);
775 DBG (2, g_warning ("error notifying listener, removing it\n"));
776 spi_deregister_controller_device_listener (controller, listener,
778 CORBA_exception_free (ev);
781 spi_listener_clone_free ((DEControllerListener *) l2->data, ev);
784 for (; l2; l2 = l2->next)
786 DEControllerListener *listener = l2->data;
787 spi_listener_clone_free (listener, ev);
788 /* clone doesn't have its own ref, so don't use spi_device_listener_free */
791 g_slist_free (notify);
794 if (is_consumed) g_message ("consumed\n");
800 spi_device_event_controller_forward_mouse_event (SpiDEController *controller,
803 Accessibility_Event e;
804 Accessibility_DeviceEvent mouse_e;
805 CORBA_Environment ev;
806 gchar event_name[24];
807 gboolean is_consumed = FALSE;
808 gboolean xkb_mod_unlatch_occurred;
809 XButtonEvent *xbutton_event = (XButtonEvent *) xevent;
811 int button = xbutton_event->button;
813 unsigned int mouse_button_state = xbutton_event->state;
818 mouse_button_state |= Button1Mask;
821 mouse_button_state |= Button2Mask;
824 mouse_button_state |= Button3Mask;
827 mouse_button_state |= Button4Mask;
830 mouse_button_state |= Button5Mask;
834 last_mouse_pos->x = ((XButtonEvent *) xevent)->x_root;
835 last_mouse_pos->y = ((XButtonEvent *) xevent)->y_root;
838 fprintf (stderr, "mouse button %d %s (%x)\n",
839 xbutton_event->button,
840 (xevent->type == ButtonPress) ? "Press" : "Release",
843 snprintf (event_name, 22, "mouse:button:%d%c", button,
844 (xevent->type == ButtonPress) ? 'p' : 'r');
846 /* TODO: distinguish between physical and logical buttons */
847 mouse_e.type = (xevent->type == ButtonPress) ?
848 Accessibility_BUTTON_PRESSED_EVENT :
849 Accessibility_BUTTON_RELEASED_EVENT;
851 mouse_e.hw_code = button;
852 mouse_e.modifiers = (CORBA_unsigned_short) xbutton_event->state;
853 mouse_e.timestamp = (CORBA_unsigned_long) xbutton_event->time;
854 mouse_e.event_string = "";
855 mouse_e.is_text = CORBA_FALSE;
856 if ((mouse_button_state & mouse_button_mask) !=
857 (mouse_mask_state & mouse_button_mask))
859 if ((mouse_mask_state & key_modifier_mask) !=
860 (mouse_button_state & key_modifier_mask))
861 spi_dec_emit_modifier_event (controller,
862 mouse_mask_state, mouse_button_state);
863 mouse_mask_state = mouse_button_state;
865 spi_controller_notify_mouselisteners (controller, &mouse_e, &ev);
866 e.type = CORBA_string_dup (event_name);
867 e.source = BONOBO_OBJREF (controller->registry->desktop);
868 e.detail1 = last_mouse_pos->x;
869 e.detail2 = last_mouse_pos->y;
870 spi_init_any_nil (&e.any_data);
871 CORBA_exception_init (&ev);
873 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
878 xkb_mod_unlatch_occurred = (xevent->type == ButtonPress ||
879 xevent->type == ButtonRelease);
881 /* if client wants to consume this event, and XKB latch state was
882 * unset by this button event, we reset it
884 if (is_consumed && (xkb_mod_unlatch_occurred))
885 spi_dec_set_unlatch_pending (controller, mouse_mask_state);
887 XAllowEvents (spi_get_display (),
888 (is_consumed) ? SyncPointer : ReplayPointer,
892 static GdkFilterReturn
893 global_filter_fn (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
895 XEvent *xevent = gdk_xevent;
896 SpiDEController *controller;
897 DEControllerPrivateData *priv;
898 Display *display = spi_get_display ();
899 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
900 priv = (DEControllerPrivateData *)
901 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
903 if (xevent->type == KeyPress || xevent->type == KeyRelease)
905 spi_device_event_controller_forward_key_event (controller, xevent);
906 return GDK_FILTER_CONTINUE;
908 if (xevent->type == ButtonPress || xevent->type == ButtonRelease)
910 spi_device_event_controller_forward_mouse_event (controller, xevent);
912 if (xevent->type == priv->xkb_base_event_code)
914 XkbAnyEvent * xkb_ev = (XkbAnyEvent *) xevent;
915 /* ugly but probably necessary...*/
916 XSynchronize (spi_get_display (), TRUE);
918 if (xkb_ev->xkb_type == XkbStateNotify)
920 XkbStateNotifyEvent *xkb_snev =
921 (XkbStateNotifyEvent *) xkb_ev;
922 /* check the mouse, to catch mouse events grabbed by
923 * another client; in case we should revert this XKB delatch
925 if (!priv->pending_xkb_mod_relatch_mask)
929 spi_dec_mouse_check (controller, &x, &y, &moved);
931 /* we check again, since the previous call may have
933 if (priv->pending_xkb_mod_relatch_mask)
935 unsigned int feedback_mask;
937 fprintf (stderr, "relatching %x\n",
938 priv->pending_xkb_mod_relatch_mask);
940 /* temporarily turn off the latch bell, if it's on */
941 XkbGetControls (display,
942 XkbAccessXFeedbackMask,
944 feedback_mask = priv->xkb_desc->ctrls->ax_options;
945 if (feedback_mask & XkbAX_StickyKeysFBMask)
947 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
949 priv->xkb_desc->ctrls->ax_options
950 &= ~(XkbAX_StickyKeysFBMask);
951 XkbChangeControls (display, priv->xkb_desc, &changes);
953 XkbLatchModifiers (display,
955 priv->pending_xkb_mod_relatch_mask,
956 priv->pending_xkb_mod_relatch_mask);
957 if (feedback_mask & XkbAX_StickyKeysFBMask)
959 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
961 priv->xkb_desc->ctrls->ax_options = feedback_mask;
962 XkbChangeControls (display, priv->xkb_desc, &changes);
965 fprintf (stderr, "relatched %x\n",
966 priv->pending_xkb_mod_relatch_mask);
968 priv->pending_xkb_mod_relatch_mask = 0;
972 priv->xkb_latch_mask = xkb_snev->latched_mods;
976 DBG (2, g_warning ("XKB event %d\n", xkb_ev->xkb_type));
977 XSynchronize (spi_get_display (), FALSE);
980 return GDK_FILTER_CONTINUE;
984 _spi_controller_device_error_handler (Display *display, XErrorEvent *error)
986 if (error->error_code == BadAccess)
988 g_message ("Could not complete key grab: grab already in use.\n");
989 spi_error_code = BadAccess;
994 return (*x_default_error_handler) (display, error);
999 spi_controller_register_with_devices (SpiDEController *controller)
1001 DEControllerPrivateData *priv = (DEControllerPrivateData *)
1002 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
1003 /* FIXME: should check for extension first! */
1004 XTestGrabControl (spi_get_display (), True);
1006 /* calls to device-specific implementations and routines go here */
1007 /* register with: keyboard hardware code handler */
1008 /* register with: (translated) keystroke handler */
1010 priv->have_xkb = XkbQueryExtension (spi_get_display (),
1011 &priv->xkb_major_extension_opcode,
1012 &priv->xkb_base_event_code,
1013 &priv->xkb_base_error_code, NULL, NULL);
1016 priv->xkb_desc = XkbGetMap (spi_get_display (), 0, XkbUseCoreKbd);
1017 XkbSelectEvents (spi_get_display (),
1019 XkbStateNotifyMask, XkbStateNotifyMask);
1022 gdk_window_add_filter (NULL, global_filter_fn, controller);
1024 gdk_window_set_events (gdk_get_default_root_window (),
1025 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
1027 x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
1031 spi_key_set_contains_key (Accessibility_KeySet *key_set,
1032 const Accessibility_DeviceEvent *key_event)
1039 g_print ("null key set!");
1043 len = key_set->_length;
1045 if (len == 0) /* special case, means "all keys/any key" */
1047 g_print ("anykey\n");
1051 for (i = 0; i < len; ++i)
1053 #ifdef SPI_KEYEVENT_DEBUG
1054 g_print ("key_set[%d] = %d; key_event %d, code %d, string %s\n",
1055 i, (int) key_set->_buffer[i].keycode,
1056 (int) key_event->id, (int) key_event->hw_code,
1057 key_event->event_string);
1059 if (key_set->_buffer[i].keysym == (CORBA_long) key_event->id)
1063 if (key_set->_buffer[i].keycode == (CORBA_long) key_event->hw_code)
1067 if (key_event->event_string && key_event->event_string[0] &&
1068 !strcmp (key_set->_buffer[i].keystring, key_event->event_string))
1078 spi_eventtype_seq_contains_event (Accessibility_EventTypeSeq *type_seq,
1079 const Accessibility_DeviceEvent *event)
1087 g_print ("null type seq!");
1091 len = type_seq->_length;
1093 if (len == 0) /* special case, means "all events/any event" */
1098 for (i = 0; i < len; ++i)
1101 g_print ("type_seq[%d] = %d; event type = %d\n", i,
1102 (int) type_seq->_buffer[i], (int) event->type);
1104 if (type_seq->_buffer[i] == (CORBA_long) event->type)
1114 spi_key_event_matches_listener (const Accessibility_DeviceEvent *key_event,
1115 DEControllerKeyListener *listener,
1116 CORBA_boolean is_system_global)
1118 if ((key_event->modifiers == (CORBA_unsigned_short) (listener->mask & 0xFFFF)) &&
1119 spi_key_set_contains_key (listener->keys, key_event) &&
1120 spi_eventtype_seq_contains_event (listener->listener.typeseq, key_event) &&
1121 (is_system_global == listener->mode->global))
1132 spi_controller_notify_keylisteners (SpiDEController *controller,
1133 const Accessibility_DeviceEvent *key_event,
1134 CORBA_boolean is_system_global,
1135 CORBA_Environment *ev)
1138 GSList *notify = NULL, *l2;
1139 GList **key_listeners = &controller->key_listeners;
1140 gboolean is_consumed;
1147 for (l = *key_listeners; l; l = l->next)
1149 DEControllerKeyListener *key_listener = l->data;
1151 if (spi_key_event_matches_listener (key_event, key_listener, is_system_global))
1153 Accessibility_DeviceEventListener ls = key_listener->listener.object;
1155 if (ls != CORBA_OBJECT_NIL)
1157 /* we clone (don't dup) the listener, to avoid refcount inc. */
1158 notify = g_slist_prepend (notify,
1159 spi_key_listener_clone (key_listener, ev));
1164 #ifdef SPI_KEYEVENT_DEBUG
1167 g_print ("no match for event\n");
1171 is_consumed = FALSE;
1172 for (l2 = notify; l2 && !is_consumed; l2 = l2->next)
1174 DEControllerKeyListener *key_listener = l2->data;
1175 Accessibility_DeviceEventListener ls = key_listener->listener.object;
1177 is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, key_event, ev);
1181 is_consumed = FALSE;
1182 spi_deregister_controller_key_listener (controller, key_listener,
1184 CORBA_exception_free (ev);
1187 spi_key_listener_clone_free (key_listener, ev);
1190 for (; l2; l2 = l2->next)
1192 DEControllerKeyListener *key_listener = l2->data;
1193 spi_key_listener_clone_free (key_listener, ev);
1194 /* clone doesn't have its own ref, so don't use spi_dec_listener_free */
1197 g_slist_free (notify);
1200 if (is_consumed) g_message ("consumed\n");
1206 spi_clear_error_state ()
1208 gboolean retval = spi_error_code != 0;
1213 static Accessibility_DeviceEvent
1214 spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
1216 Accessibility_DeviceEvent key_event;
1218 const int cbuf_bytes = 20;
1219 char cbuf [cbuf_bytes+1];
1222 nbytes = XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL);
1223 key_event.id = (CORBA_long)(keysym);
1224 key_event.hw_code = (CORBA_short) x_key_event->keycode;
1225 if (((XEvent *) x_key_event)->type == KeyPress)
1227 key_event.type = Accessibility_KEY_PRESSED;
1231 key_event.type = Accessibility_KEY_RELEASED;
1233 key_event.modifiers = (CORBA_unsigned_short)(x_key_event->state);
1234 key_event.is_text = CORBA_FALSE;
1238 key_event.event_string = CORBA_string_dup ("space");
1241 #ifdef SPI_KEYEVENT_DEBUG
1242 fprintf(stderr, "Tab\n");
1244 key_event.event_string = CORBA_string_dup ("Tab");
1247 key_event.event_string = CORBA_string_dup ("Backspace");
1250 key_event.event_string = CORBA_string_dup ("Return");
1253 key_event.event_string = CORBA_string_dup ("Home");
1256 key_event.event_string = CORBA_string_dup ("Page_Down");
1259 key_event.event_string = CORBA_string_dup ("Page_Up");
1262 key_event.event_string = CORBA_string_dup ("F1");
1265 key_event.event_string = CORBA_string_dup ("F2");
1268 key_event.event_string = CORBA_string_dup ("F3");
1271 key_event.event_string = CORBA_string_dup ("F4");
1274 key_event.event_string = CORBA_string_dup ("F5");
1277 key_event.event_string = CORBA_string_dup ("F6");
1280 key_event.event_string = CORBA_string_dup ("F7");
1283 key_event.event_string = CORBA_string_dup ("F8");
1286 key_event.event_string = CORBA_string_dup ("F9");
1289 key_event.event_string = CORBA_string_dup ("F10");
1292 key_event.event_string = CORBA_string_dup ("F11");
1295 key_event.event_string = CORBA_string_dup ("F12");
1298 key_event.event_string = CORBA_string_dup ("End");
1301 key_event.event_string = CORBA_string_dup ("Escape");
1304 key_event.event_string = CORBA_string_dup ("Up");
1307 key_event.event_string = CORBA_string_dup ("Down");
1310 key_event.event_string = CORBA_string_dup ("Left");
1313 key_event.event_string = CORBA_string_dup ("Right");
1318 cbuf[nbytes] = '\0'; /* OK since length is cbuf_bytes+1 */
1319 key_event.event_string = CORBA_string_dup (cbuf);
1320 if ((keysym && g_ascii_isprint (keysym)) || (nbytes == 1))
1322 key_event.is_text = CORBA_TRUE;
1323 /* FIXME: incorrect for some composed chars, unicode chars? */
1328 key_event.event_string = CORBA_string_dup ("");
1332 key_event.timestamp = (CORBA_unsigned_long) x_key_event->time;
1333 #ifdef SPI_KEYEVENT_DEBUG
1335 "Key %lu pressed (%c), modifiers %d\n",
1336 (unsigned long) keysym,
1337 keysym ? (int) keysym : '*',
1338 (int) x_key_event->state);
1341 fprintf (stderr, "%s%c",
1342 (x_key_event->state & Mod1Mask)?"Alt-":"",
1343 ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
1344 g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
1345 #endif /* SPI_DEBUG */
1350 spi_controller_update_key_grabs (SpiDEController *controller,
1351 Accessibility_DeviceEvent *recv)
1354 gboolean update_failed = FALSE;
1356 g_return_val_if_fail (controller != NULL, FALSE);
1359 * masks known to work with default RH 7.1+:
1360 * 0 (no mods), LockMask, Mod1Mask, Mod2Mask, ShiftMask,
1361 * ShiftMask|LockMask, Mod1Mask|LockMask, Mod2Mask|LockMask,
1362 * ShiftMask|Mod1Mask, ShiftMask|Mod2Mask, Mod1Mask|Mod2Mask,
1363 * ShiftMask|LockMask|Mod1Mask, ShiftMask|LockMask|Mod2Mask,
1365 * ControlMask grabs are broken, must be in use already
1367 for (l = controller->keygrabs_list; l; l = next)
1370 gboolean re_issue_grab;
1371 DEControllerGrabMask *grab_mask = l->data;
1375 re_issue_grab = recv &&
1376 /* (recv->type == Accessibility_KEY_RELEASED) && - (?) */
1377 (recv->modifiers & grab_mask->mod_mask) &&
1378 (grab_mask->key_val == keycode_for_keysym (recv->id));
1381 fprintf (stderr, "mask=%lx %lx (%c%c) %s\n",
1382 (long int) grab_mask->key_val,
1383 (long int) grab_mask->mod_mask,
1384 grab_mask->pending_add ? '+' : '.',
1385 grab_mask->pending_remove ? '-' : '.',
1386 re_issue_grab ? "re-issue": "");
1391 if (grab_mask->pending_add && grab_mask->pending_remove)
1395 else if (grab_mask->pending_remove)
1398 fprintf (stderr, "ungrabbing, mask=%x\n", grab_mask->mod_mask);
1400 XUngrabKey (spi_get_display (),
1402 grab_mask->mod_mask,
1403 gdk_x11_get_default_root_xwindow ());
1407 else if (grab_mask->pending_add || re_issue_grab)
1411 fprintf (stderr, "grab %d with mask %x\n", grab_mask->key_val, grab_mask->mod_mask);
1413 XGrabKey (spi_get_display (),
1415 grab_mask->mod_mask,
1416 gdk_x11_get_default_root_xwindow (),
1420 XSync (spi_get_display (), False);
1421 update_failed = spi_clear_error_state ();
1422 if (update_failed) {
1423 while (grab_mask->ref_count > 0) --grab_mask->ref_count;
1428 grab_mask->pending_add = FALSE;
1429 grab_mask->pending_remove = FALSE;
1433 g_assert (grab_mask->ref_count <= 0);
1435 controller->keygrabs_list = g_list_delete_link (
1436 controller->keygrabs_list, l);
1438 spi_grab_mask_free (grab_mask);
1443 return ! update_failed;
1447 * Implemented GObject::finalize
1450 spi_device_event_controller_object_finalize (GObject *object)
1452 SpiDEController *controller;
1453 DEControllerPrivateData *private;
1454 controller = SPI_DEVICE_EVENT_CONTROLLER (object);
1457 fprintf(stderr, "spi_device_event_controller_object_finalize called\n");
1459 /* disconnect any special listeners, get rid of outstanding keygrabs */
1460 XUngrabKey (spi_get_display (), AnyKey, AnyModifier, DefaultRootWindow (spi_get_display ()));
1462 private = g_object_get_data (G_OBJECT (controller), "spi-dec-private");
1463 if (private->xkb_desc)
1464 XkbFreeKeyboard (private->xkb_desc, 0, True);
1466 spi_device_event_controller_parent_class->finalize (object);
1470 * CORBA Accessibility::DEController::registerKeystrokeListener
1471 * method implementation
1473 static CORBA_boolean
1474 impl_register_keystroke_listener (PortableServer_Servant servant,
1475 const Accessibility_DeviceEventListener l,
1476 const Accessibility_KeySet *keys,
1477 const Accessibility_ControllerEventMask mask,
1478 const Accessibility_EventTypeSeq *type,
1479 const Accessibility_EventListenerMode *mode,
1480 CORBA_Environment *ev)
1482 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1483 bonobo_object_from_servant (servant));
1484 DEControllerKeyListener *dec_listener;
1486 fprintf (stderr, "registering keystroke listener %p with maskVal %lu\n",
1487 (void *) l, (unsigned long) mask);
1489 dec_listener = spi_dec_key_listener_new (l, keys, mask, type, mode, ev);
1490 return spi_controller_register_device_listener (
1491 controller, (DEControllerListener *) dec_listener, ev);
1496 * CORBA Accessibility::DEController::registerDeviceEventListener
1497 * method implementation
1499 static CORBA_boolean
1500 impl_register_device_listener (PortableServer_Servant servant,
1501 const Accessibility_DeviceEventListener l,
1502 const Accessibility_EventTypeSeq *event_types,
1503 CORBA_Environment *ev)
1505 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1506 bonobo_object_from_servant (servant));
1507 DEControllerListener *dec_listener;
1509 dec_listener = spi_dec_listener_new (l, event_types, ev);
1510 return spi_controller_register_device_listener (
1511 controller, (DEControllerListener *) dec_listener, ev);
1515 CORBA_Environment *ev;
1516 DEControllerListener *listener;
1517 } RemoveListenerClosure;
1519 static SpiReEntrantContinue
1520 remove_listener_cb (GList * const *list,
1523 DEControllerListener *listener = (*list)->data;
1524 RemoveListenerClosure *ctx = user_data;
1526 if (CORBA_Object_is_equivalent (ctx->listener->object,
1527 listener->object, ctx->ev))
1529 spi_re_entrant_list_delete_link (list);
1530 spi_dec_listener_free (listener, ctx->ev);
1533 return SPI_RE_ENTRANT_CONTINUE;
1536 static SpiReEntrantContinue
1537 copy_key_listener_cb (GList * const *list,
1540 DEControllerKeyListener *key_listener = (*list)->data;
1541 RemoveListenerClosure *ctx = user_data;
1543 if (CORBA_Object_is_equivalent (ctx->listener->object,
1544 key_listener->listener.object, ctx->ev))
1546 /* TODO: FIXME aggregate keys in case the listener is registered twice */
1547 DEControllerKeyListener *ctx_key_listener =
1548 (DEControllerKeyListener *) ctx->listener;
1549 CORBA_free (ctx_key_listener->keys);
1550 ctx_key_listener->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
1553 return SPI_RE_ENTRANT_CONTINUE;
1557 spi_deregister_controller_device_listener (SpiDEController *controller,
1558 DEControllerListener *listener,
1559 CORBA_Environment *ev)
1561 RemoveListenerClosure ctx;
1564 ctx.listener = listener;
1566 spi_re_entrant_list_foreach (&controller->mouse_listeners,
1567 remove_listener_cb, &ctx);
1571 spi_deregister_controller_key_listener (SpiDEController *controller,
1572 DEControllerKeyListener *key_listener,
1573 CORBA_Environment *ev)
1575 RemoveListenerClosure ctx;
1578 ctx.listener = (DEControllerListener *) key_listener;
1580 /* special case, copy keyset from existing controller list entry */
1581 if (key_listener->keys->_length == 0)
1583 spi_re_entrant_list_foreach (&controller->key_listeners,
1584 copy_key_listener_cb, &ctx);
1587 spi_controller_deregister_global_keygrabs (controller, key_listener);
1589 spi_re_entrant_list_foreach (&controller->key_listeners,
1590 remove_listener_cb, &ctx);
1595 * CORBA Accessibility::DEController::deregisterKeystrokeListener
1596 * method implementation
1599 impl_deregister_keystroke_listener (PortableServer_Servant servant,
1600 const Accessibility_DeviceEventListener l,
1601 const Accessibility_KeySet *keys,
1602 const Accessibility_ControllerEventMask mask,
1603 const Accessibility_EventTypeSeq *type,
1604 CORBA_Environment *ev)
1606 DEControllerKeyListener *key_listener;
1607 SpiDEController *controller;
1609 controller = SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1611 key_listener = spi_dec_key_listener_new (l, keys, mask, type, NULL, ev);
1613 #ifdef SPI_DEREGISTER_DEBUG
1614 fprintf (stderr, "deregistering keystroke listener %p with maskVal %lu\n",
1615 (void *) l, (unsigned long) mask->value);
1618 spi_deregister_controller_key_listener (controller, key_listener, ev);
1620 spi_dec_listener_free ((DEControllerListener *) key_listener, ev);
1624 * CORBA Accessibility::DEController::deregisterDeviceEventListener
1625 * method implementation
1628 impl_deregister_device_listener (PortableServer_Servant servant,
1629 const Accessibility_DeviceEventListener l,
1630 const Accessibility_EventTypeSeq *event_types,
1631 CORBA_Environment *ev)
1633 SpiDEController *controller;
1634 DEControllerListener *listener =
1635 spi_dec_listener_new (l, event_types, ev);
1637 controller = SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1639 spi_deregister_controller_device_listener (controller, listener, ev);
1641 spi_dec_listener_free (listener, ev);
1644 static unsigned int dec_xkb_get_slowkeys_delay (SpiDEController *controller)
1646 unsigned int retval = 0;
1647 DEControllerPrivateData *priv = (DEControllerPrivateData *)
1648 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
1650 #ifdef XKB_HAS_GET_SLOW_KEYS_DELAY
1651 retval = XkbGetSlowKeysDelay (spi_get_display (),
1652 XkbUseCoreKbd, &bounce_delay);
1654 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
1656 Status s = XkbGetControls (spi_get_display (),
1657 XkbAllControlsMask, priv->xkb_desc);
1660 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbSlowKeysMask)
1661 retval = priv->xkb_desc->ctrls->slow_keys_delay;
1666 #ifdef SPI_XKB_DEBUG
1667 fprintf (stderr, "SlowKeys delay: %d\n", (int) retval);
1672 static unsigned int dec_xkb_get_bouncekeys_delay (SpiDEController *controller)
1674 unsigned int retval = 0;
1675 DEControllerPrivateData *priv = (DEControllerPrivateData *)
1676 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
1678 #ifdef XKB_HAS_GET_BOUNCE_KEYS_DELAY
1679 retval = XkbGetBounceKeysDelay (spi_get_display (),
1680 XkbUseCoreKbd, &bounce_delay);
1682 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
1684 Status s = XkbGetControls (spi_get_display (),
1685 XkbAllControlsMask, priv->xkb_desc);
1688 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbBounceKeysMask)
1689 retval = priv->xkb_desc->ctrls->debounce_delay;
1694 #ifdef SPI_XKB_DEBUG
1695 fprintf (stderr, "BounceKeys delay: %d\n", (int) retval);
1701 dec_synth_keycode_press (SpiDEController *controller,
1702 unsigned int keycode)
1704 unsigned int time = CurrentTime;
1705 unsigned int bounce_delay;
1706 unsigned int elapsed_msec;
1708 DEControllerPrivateData *priv =
1709 (DEControllerPrivateData *) g_object_get_qdata (G_OBJECT (controller),
1710 spi_dec_private_quark);
1711 if (keycode == priv->last_release_keycode)
1713 bounce_delay = dec_xkb_get_bouncekeys_delay (controller);
1716 gettimeofday (&tv, NULL);
1718 (tv.tv_sec - priv->last_release_time.tv_sec) * 1000
1719 + (tv.tv_usec - priv->last_release_time.tv_usec) / 1000;
1720 #ifdef SPI_XKB_DEBUG
1721 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1722 (long) (tv.tv_usec - priv->last_release_time.tv_usec));
1724 #ifdef THIS_IS_BROKEN
1725 if (elapsed_msec < bounce_delay)
1726 time = bounce_delay - elapsed_msec + 1;
1728 time = bounce_delay + 10;
1729 /* fudge for broken XTest */
1731 #ifdef SPI_XKB_DEBUG
1732 fprintf (stderr, "waiting %d ms\n", time);
1736 XTestFakeKeyEvent (spi_get_display (), keycode, True, time);
1737 priv->last_press_keycode = keycode;
1738 XSync (spi_get_display (), False);
1739 gettimeofday (&priv->last_press_time, NULL);
1744 dec_synth_keycode_release (SpiDEController *controller,
1745 unsigned int keycode)
1747 unsigned int time = CurrentTime;
1748 unsigned int slow_delay;
1749 unsigned int elapsed_msec;
1751 DEControllerPrivateData *priv =
1752 (DEControllerPrivateData *) g_object_get_qdata (G_OBJECT (controller),
1753 spi_dec_private_quark);
1754 if (keycode == priv->last_press_keycode)
1756 slow_delay = dec_xkb_get_slowkeys_delay (controller);
1759 gettimeofday (&tv, NULL);
1761 (tv.tv_sec - priv->last_press_time.tv_sec) * 1000
1762 + (tv.tv_usec - priv->last_press_time.tv_usec) / 1000;
1763 #ifdef SPI_XKB_DEBUG
1764 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1765 (long) (tv.tv_usec - priv->last_press_time.tv_usec));
1767 #ifdef THIS_IS_BROKEN_DUNNO_WHY
1768 if (elapsed_msec < slow_delay)
1769 time = slow_delay - elapsed_msec + 1;
1771 time = slow_delay + 10;
1772 /* our XTest seems broken, we have to add slop as above */
1774 #ifdef SPI_XKB_DEBUG
1775 fprintf (stderr, "waiting %d ms\n", time);
1779 XTestFakeKeyEvent (spi_get_display (), keycode, False, time);
1780 priv->last_release_keycode = keycode;
1781 XSync (spi_get_display (), False);
1782 gettimeofday (&priv->last_release_time, NULL);
1787 * CORBA Accessibility::DEController::registerKeystrokeListener
1788 * method implementation
1791 impl_generate_keyboard_event (PortableServer_Servant servant,
1792 const CORBA_long keycode,
1793 const CORBA_char *keystring,
1794 const Accessibility_KeySynthType synth_type,
1795 CORBA_Environment *ev)
1797 SpiDEController *controller =
1798 SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1799 long key_synth_code;
1802 fprintf (stderr, "synthesizing keystroke %ld, type %d\n",
1803 (long) keycode, (int) synth_type);
1805 /* TODO: hide/wrap/remove X dependency */
1808 * TODO: when initializing, query for XTest extension before using,
1809 * and fall back to XSendEvent() if XTest is not available.
1812 /* TODO: implement keystring mode also */
1813 gdk_error_trap_push ();
1814 key_synth_code = keycode;
1818 case Accessibility_KEY_PRESS:
1819 dec_synth_keycode_press (controller, keycode);
1821 case Accessibility_KEY_PRESSRELEASE:
1822 dec_synth_keycode_press (controller, keycode);
1823 case Accessibility_KEY_RELEASE:
1824 dec_synth_keycode_release (controller, keycode);
1826 case Accessibility_KEY_SYM:
1827 #ifdef SPI_XKB_DEBUG
1828 fprintf (stderr, "KeySym synthesis\n");
1830 key_synth_code = keycode_for_keysym (keycode);
1831 dec_synth_keycode_press (controller, key_synth_code);
1832 dec_synth_keycode_release (controller, key_synth_code);
1834 case Accessibility_KEY_STRING:
1835 fprintf (stderr, "Not yet implemented\n");
1838 if (gdk_error_trap_pop ())
1840 DBG (-1, g_warning ("Error emitting keystroke"));
1844 /* Accessibility::DEController::generateMouseEvent */
1846 impl_generate_mouse_event (PortableServer_Servant servant,
1849 const CORBA_char *eventName,
1850 CORBA_Environment *ev)
1853 gboolean error = FALSE;
1854 Display *display = spi_get_display ();
1856 fprintf (stderr, "generating mouse %s event at %ld, %ld\n",
1857 eventName, (long int) x, (long int) y);
1859 switch (eventName[0])
1862 switch (eventName[1])
1864 /* TODO: check number of buttons before parsing */
1885 if (x != -1 && y != -1)
1887 XTestFakeMotionEvent (display, DefaultScreen (display),
1890 XTestFakeButtonEvent (display, button, !(eventName[2] == 'r'), 0);
1891 if (eventName[2] == 'c')
1892 XTestFakeButtonEvent (display, button, FALSE, 1);
1893 else if (eventName[2] == 'd')
1895 XTestFakeButtonEvent (display, button, FALSE, 1);
1896 XTestFakeButtonEvent (display, button, TRUE, 2);
1897 XTestFakeButtonEvent (display, button, FALSE, 3);
1901 case 'r': /* relative motion */
1902 XTestFakeRelativeMotionEvent (display, x, y, 0);
1904 case 'a': /* absolute motion */
1905 XTestFakeMotionEvent (display, DefaultScreen (display),
1911 /* Accessibility::DEController::notifyListenersSync */
1912 static CORBA_boolean
1913 impl_notify_listeners_sync (PortableServer_Servant servant,
1914 const Accessibility_DeviceEvent *event,
1915 CORBA_Environment *ev)
1917 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1918 bonobo_object_from_servant (servant));
1920 g_print ("notifylistening listeners synchronously: controller %p, event id %d\n",
1921 controller, (int) event->id);
1923 return spi_controller_notify_keylisteners (controller, event, CORBA_FALSE, ev) ?
1924 CORBA_TRUE : CORBA_FALSE;
1927 /* Accessibility::DEController::notifyListenersAsync */
1929 impl_notify_listeners_async (PortableServer_Servant servant,
1930 const Accessibility_DeviceEvent *event,
1931 CORBA_Environment *ev)
1933 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1934 bonobo_object_from_servant (servant));
1936 fprintf (stderr, "notifying listeners asynchronously\n");
1938 spi_controller_notify_keylisteners (controller, event, CORBA_FALSE, ev);
1942 spi_device_event_controller_class_init (SpiDEControllerClass *klass)
1944 GObjectClass * object_class = (GObjectClass *) klass;
1945 POA_Accessibility_DeviceEventController__epv *epv = &klass->epv;
1947 spi_device_event_controller_parent_class = g_type_class_peek_parent (klass);
1949 object_class->finalize = spi_device_event_controller_object_finalize;
1951 epv->registerKeystrokeListener = impl_register_keystroke_listener;
1952 epv->deregisterKeystrokeListener = impl_deregister_keystroke_listener;
1953 epv->registerDeviceEventListener = impl_register_device_listener;
1954 epv->deregisterDeviceEventListener = impl_deregister_device_listener;
1955 epv->generateKeyboardEvent = impl_generate_keyboard_event;
1956 epv->generateMouseEvent = impl_generate_mouse_event;
1957 epv->notifyListenersSync = impl_notify_listeners_sync;
1958 epv->notifyListenersAsync = impl_notify_listeners_async;
1960 if (!spi_dec_private_quark)
1961 spi_dec_private_quark = g_quark_from_static_string ("spi-dec-private");
1965 spi_device_event_controller_init (SpiDEController *device_event_controller)
1967 DEControllerPrivateData *private;
1968 device_event_controller->key_listeners = NULL;
1969 device_event_controller->mouse_listeners = NULL;
1970 device_event_controller->keygrabs_list = NULL;
1972 private = g_new0 (DEControllerPrivateData, 1);
1973 gettimeofday (&private->last_press_time, NULL);
1974 gettimeofday (&private->last_release_time, NULL);
1975 g_object_set_qdata (G_OBJECT (device_event_controller),
1976 spi_dec_private_quark,
1978 spi_controller_register_with_devices (device_event_controller);
1982 spi_device_event_controller_forward_key_event (SpiDEController *controller,
1983 const XEvent *event)
1985 gboolean is_consumed = FALSE;
1986 CORBA_Environment ev;
1987 Accessibility_DeviceEvent key_event;
1989 g_assert (event->type == KeyPress || event->type == KeyRelease);
1991 CORBA_exception_init (&ev);
1993 key_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1995 spi_controller_update_key_grabs (controller, &key_event);
1997 /* relay to listeners, and decide whether to consume it or not */
1998 is_consumed = spi_controller_notify_keylisteners (
1999 controller, &key_event, CORBA_TRUE, &ev);
2003 XAllowEvents (spi_get_display (), AsyncKeyboard, CurrentTime);
2007 XAllowEvents (spi_get_display (), ReplayKeyboard, CurrentTime);
2012 spi_device_event_controller_new (SpiRegistry *registry)
2014 SpiDEController *retval = g_object_new (
2015 SPI_DEVICE_EVENT_CONTROLLER_TYPE, NULL);
2017 retval->registry = SPI_REGISTRY (bonobo_object_ref (
2018 BONOBO_OBJECT (registry)));
2020 spi_dec_init_mouse_listener (registry);
2021 /* TODO: kill mouse listener on finalize */
2025 BONOBO_TYPE_FUNC_FULL (SpiDEController,
2026 Accessibility_DeviceEventController,
2028 spi_device_event_controller);