2 * 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 #undef SPI_KEYEVENT_DEBUG
35 #include <bonobo/bonobo-exception.h>
38 #include <X11/extensions/XTest.h>
39 #include <X11/XKBlib.h>
41 #include <X11/keysymdef.h>
43 #include <gdk/gdkx.h> /* TODO: hide dependency (wrap in single porting file) */
44 #include <gdk/gdkkeysyms.h>
45 #include <gdk/gdkwindow.h>
47 #include "../libspi/spi-private.h"
48 #include "deviceeventcontroller.h"
50 /* Our parent Gtk object type */
51 #define PARENT_TYPE BONOBO_TYPE_OBJECT
53 /* A pointer to our parent object class */
54 static GObjectClass *spi_device_event_controller_parent_class;
55 static int spi_error_code = 0;
56 static GdkPoint *last_mouse_pos = NULL;
57 static unsigned int mouse_mask_state = 0;
58 static unsigned int mouse_button_mask =
59 Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask;
60 static unsigned int key_modifier_mask =
61 Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask | ShiftMask | LockMask | ControlMask;
63 static GQuark spi_dec_private_quark = 0;
65 int (*x_default_error_handler) (Display *display, XErrorEvent *error_event);
69 SPI_DEVICE_TYPE_MOUSE,
70 SPI_DEVICE_TYPE_LAST_DEFINED
71 } SpiDeviceTypeCategory;
75 guint pending_add : 1;
76 guint pending_remove : 1;
78 Accessibility_ControllerEventMask mod_mask;
79 CORBA_unsigned_long key_val; /* KeyCode */
80 } DEControllerGrabMask;
84 SpiDeviceTypeCategory type;
85 Accessibility_EventTypeSeq *typeseq;
86 } DEControllerListener;
89 DEControllerListener listener;
91 Accessibility_KeySet *keys;
92 Accessibility_ControllerEventMask mask;
93 Accessibility_EventListenerMode *mode;
94 } DEControllerKeyListener;
97 unsigned int last_press_keycode;
98 unsigned int last_release_keycode;
99 struct timeval last_press_time;
100 struct timeval last_release_time;
102 int xkb_major_extension_opcode;
103 int xkb_base_event_code;
104 int xkb_base_error_code;
105 unsigned int xkb_latch_mask;
106 unsigned int pending_xkb_mod_relatch_mask;
108 } DEControllerPrivateData;
110 static void spi_controller_register_with_devices (SpiDEController *controller);
111 static gboolean spi_controller_update_key_grabs (SpiDEController *controller,
112 Accessibility_DeviceEvent *recv);
113 static gboolean spi_controller_register_device_listener (SpiDEController *controller,
114 DEControllerListener *l,
115 CORBA_Environment *ev);
116 static void spi_device_event_controller_forward_key_event (SpiDEController *controller,
117 const XEvent *event);
118 static void spi_deregister_controller_device_listener (SpiDEController *controller,
119 DEControllerListener *listener,
120 CORBA_Environment *ev);
121 static void spi_deregister_controller_key_listener (SpiDEController *controller,
122 DEControllerKeyListener *key_listener,
123 CORBA_Environment *ev);
124 static gboolean spi_controller_notify_mouselisteners (SpiDEController *controller,
125 const Accessibility_DeviceEvent *event,
126 CORBA_Environment *ev);
128 static gboolean spi_eventtype_seq_contains_event (Accessibility_EventTypeSeq *type_seq,
129 const Accessibility_DeviceEvent *event);
130 static gboolean spi_clear_error_state (void);
131 static gboolean spi_dec_poll_mouse_moved (gpointer data);
132 static gboolean spi_dec_poll_mouse_moving (gpointer data);
133 static gboolean spi_dec_poll_mouse_idle (gpointer data);
135 #define spi_get_display() GDK_DISPLAY()
137 /* Private methods */
140 keycode_for_keysym (long keysym)
142 return XKeysymToKeycode (spi_get_display (), (KeySym) keysym);
145 static DEControllerGrabMask *
146 spi_grab_mask_clone (DEControllerGrabMask *grab_mask)
148 DEControllerGrabMask *clone = g_new (DEControllerGrabMask, 1);
150 memcpy (clone, grab_mask, sizeof (DEControllerGrabMask));
152 clone->ref_count = 1;
153 clone->pending_add = TRUE;
154 clone->pending_remove = FALSE;
160 spi_grab_mask_free (DEControllerGrabMask *grab_mask)
166 spi_grab_mask_compare_values (gconstpointer p1, gconstpointer p2)
168 DEControllerGrabMask *l1 = (DEControllerGrabMask *) p1;
169 DEControllerGrabMask *l2 = (DEControllerGrabMask *) p2;
177 return ((l1->mod_mask != l2->mod_mask) || (l1->key_val != l2->key_val));
181 static gint poll_count = 0;
184 spi_dec_poll_mouse_moved (gpointer data)
186 SpiRegistry *registry = SPI_REGISTRY (data);
187 SpiDEController *controller = registry->de_controller;
188 CORBA_Environment ev;
189 Accessibility_Event e;
190 Accessibility_DeviceEvent mouse_e;
191 Window root_return, child_return;
192 int win_x_return,win_y_return;
194 int poll_count_modulus = 10;
195 unsigned int mask_return;
196 gchar event_name[24];
197 gboolean is_consumed;
198 Display *display = spi_get_display ();
201 XQueryPointer(display, DefaultRootWindow (display),
202 &root_return, &child_return,
204 &win_x_return, &win_y_return, &mask_return);
206 if (mask_return != mouse_mask_state) {
207 if ((mask_return & mouse_button_mask) !=
208 (mouse_mask_state & mouse_button_mask)) {
209 int button_number = 0;
210 if (!(mask_return & Button1Mask) &&
211 (mouse_mask_state & Button1Mask)) {
213 } else if (!(mask_return & Button2Mask) &&
214 (mouse_mask_state & Button2Mask)) {
216 } else if (!(mask_return & Button3Mask) &&
217 (mouse_mask_state & Button3Mask)) {
219 } else if (!(mask_return & Button4Mask) &&
220 (mouse_mask_state & Button1Mask)) {
222 } else if (!(mask_return & Button5Mask) &&
223 (mouse_mask_state & Button5Mask)) {
228 fprintf (stderr, "Button %d Released\n",
231 snprintf (event_name, 22, "mouse:button:%dr", button_number);
232 /* TODO: distinguish between physical and
235 mouse_e.type = Accessibility_BUTTON_RELEASED_EVENT;
236 mouse_e.id = button_number;
237 mouse_e.hw_code = button_number;
238 mouse_e.modifiers = (CORBA_unsigned_short)
240 mouse_e.timestamp = 0;
241 mouse_e.event_string = CORBA_string_dup ("");
242 mouse_e.is_text = CORBA_FALSE;
244 spi_controller_notify_mouselisteners (controller,
247 CORBA_free (mouse_e.event_string);
250 e.source = BONOBO_OBJREF (registry->desktop);
251 e.detail1 = last_mouse_pos->x;
252 e.detail2 = last_mouse_pos->y;
253 CORBA_exception_init (&ev);
255 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
260 if ((mask_return & key_modifier_mask) !=
261 (mouse_mask_state & key_modifier_mask)) {
263 fprintf (stderr, "MODIFIER CHANGE EVENT!\n");
265 e.type = "keyboard:modifiers";
266 e.source = BONOBO_OBJREF (registry->desktop);
267 e.detail1 = mouse_mask_state;
268 e.detail2 = mask_return;
269 CORBA_exception_init (&ev);
270 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
274 mouse_mask_state = mask_return;
276 if (last_mouse_pos == NULL) {
277 last_mouse_pos = g_new0 (GdkPoint, 1);
278 last_mouse_pos->x = 0;
279 last_mouse_pos->y = 0;
281 if (poll_count++ == poll_count_modulus) {
283 e.type = "mouse:abs";
284 e.source = BONOBO_OBJREF (registry->desktop);
287 CORBA_exception_init (&ev);
288 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
292 if (x != last_mouse_pos->x || y != last_mouse_pos->y) {
293 e.type = "mouse:rel";
294 e.source = BONOBO_OBJREF (registry->desktop);
295 e.detail1 = x - last_mouse_pos->x;
296 e.detail2 = y - last_mouse_pos->y;
297 CORBA_exception_init (&ev);
298 last_mouse_pos->x = x;
299 last_mouse_pos->y = y;
300 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
309 spi_dec_poll_mouse_idle (gpointer data)
311 if (! spi_dec_poll_mouse_moved (data))
315 g_timeout_add (20, spi_dec_poll_mouse_moving, data);
321 spi_dec_poll_mouse_moving (gpointer data)
323 if (spi_dec_poll_mouse_moved (data))
327 g_timeout_add (100, spi_dec_poll_mouse_idle, data);
333 spi_dec_ungrab_mouse (gpointer data)
335 Display *display = spi_get_display ();
336 fprintf (stderr, "mouse ungrab : display = %p\n", display);
339 XUngrabButton (spi_get_display (), AnyButton, AnyModifier,
340 XDefaultRootWindow (spi_get_display ()));
341 fprintf (stderr, "mouse grab released\n");
347 spi_dec_init_mouse_listener (SpiRegistry *registry)
349 Display *display = spi_get_display ();
350 g_timeout_add (100, spi_dec_poll_mouse_idle, registry);
354 XGrabButton (display, AnyButton, AnyModifier,
355 gdk_x11_get_default_root_xwindow (),
356 True, ButtonPressMask | ButtonReleaseMask,
357 GrabModeSync, GrabModeAsync, None, None);
358 XSync (display, False);
360 fprintf (stderr, "mouse buttons grabbed\n");
365 static DEControllerKeyListener *
366 spi_dec_key_listener_new (CORBA_Object l,
367 const Accessibility_KeySet *keys,
368 const Accessibility_ControllerEventMask mask,
369 const Accessibility_EventTypeSeq *typeseq,
370 const Accessibility_EventListenerMode *mode,
371 CORBA_Environment *ev)
373 DEControllerKeyListener *key_listener = g_new0 (DEControllerKeyListener, 1);
374 key_listener->listener.object = bonobo_object_dup_ref (l, ev);
375 key_listener->listener.type = SPI_DEVICE_TYPE_KBD;
376 key_listener->keys = ORBit_copy_value (keys, TC_Accessibility_KeySet);
377 key_listener->mask = mask;
378 key_listener->listener.typeseq = ORBit_copy_value (typeseq, TC_Accessibility_EventTypeSeq);
380 key_listener->mode = ORBit_copy_value (mode, TC_Accessibility_EventListenerMode);
382 key_listener->mode = NULL;
385 g_print ("new listener, with mask %x, is_global %d, keys %p (%d)\n",
386 (unsigned int) key_listener->mask,
387 (int) (mode ? mode->global : 0),
388 (void *) key_listener->keys,
389 (int) (key_listener->keys ? key_listener->keys->_length : 0));
395 static DEControllerListener *
396 spi_dec_listener_new (CORBA_Object l,
397 const Accessibility_EventTypeSeq *typeseq,
398 CORBA_Environment *ev)
400 DEControllerListener *listener = g_new0 (DEControllerListener, 1);
401 listener->object = bonobo_object_dup_ref (l, ev);
402 listener->type = SPI_DEVICE_TYPE_MOUSE;
403 listener->typeseq = ORBit_copy_value (typeseq, TC_Accessibility_EventTypeSeq);
407 static DEControllerListener *
408 spi_listener_clone (DEControllerListener *listener, CORBA_Environment *ev)
410 DEControllerListener *clone = g_new0 (DEControllerListener, 1);
412 CORBA_Object_duplicate (listener->object, ev);
413 clone->type = listener->type;
414 clone->typeseq = ORBit_copy_value (listener->typeseq, TC_Accessibility_EventTypeSeq);
418 static DEControllerKeyListener *
419 spi_key_listener_clone (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
421 DEControllerKeyListener *clone = g_new0 (DEControllerKeyListener, 1);
422 clone->listener.object =
423 CORBA_Object_duplicate (key_listener->listener.object, ev);
424 clone->listener.type = SPI_DEVICE_TYPE_KBD;
425 clone->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
426 clone->mask = key_listener->mask;
427 clone->listener.typeseq = ORBit_copy_value (key_listener->listener.typeseq, TC_Accessibility_EventTypeSeq);
428 if (key_listener->mode)
429 clone->mode = ORBit_copy_value (key_listener->mode, TC_Accessibility_EventListenerMode);
436 spi_key_listener_data_free (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
438 CORBA_free (key_listener->listener.typeseq);
439 CORBA_free (key_listener->keys);
440 g_free (key_listener);
444 spi_key_listener_clone_free (DEControllerKeyListener *clone, CORBA_Environment *ev)
446 CORBA_Object_release (clone->listener.object, ev);
447 spi_key_listener_data_free (clone, ev);
451 spi_listener_clone_free (DEControllerListener *clone, CORBA_Environment *ev)
453 CORBA_Object_release (clone->object, ev);
454 CORBA_free (clone->typeseq);
459 spi_dec_listener_free (DEControllerListener *listener,
460 CORBA_Environment *ev)
462 bonobo_object_release_unref (listener->object, ev);
463 if (listener->type == SPI_DEVICE_TYPE_KBD)
464 spi_key_listener_data_free ((DEControllerKeyListener *) listener, ev);
468 _register_keygrab (SpiDEController *controller,
469 DEControllerGrabMask *grab_mask)
473 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
474 spi_grab_mask_compare_values);
477 DEControllerGrabMask *cur_mask = l->data;
479 cur_mask->ref_count++;
480 if (cur_mask->pending_remove)
482 cur_mask->pending_remove = FALSE;
487 controller->keygrabs_list =
488 g_list_prepend (controller->keygrabs_list,
489 spi_grab_mask_clone (grab_mask));
494 _deregister_keygrab (SpiDEController *controller,
495 DEControllerGrabMask *grab_mask)
499 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
500 spi_grab_mask_compare_values);
504 DEControllerGrabMask *cur_mask = l->data;
506 cur_mask->ref_count--;
507 if (cur_mask->ref_count <= 0)
509 cur_mask->pending_remove = TRUE;
514 g_warning ("De-registering non-existant grab");
519 handle_keygrab (SpiDEController *controller,
520 DEControllerKeyListener *key_listener,
521 void (*process_cb) (SpiDEController *controller,
522 DEControllerGrabMask *grab_mask))
524 DEControllerGrabMask grab_mask = { 0 };
526 grab_mask.mod_mask = key_listener->mask;
527 if (key_listener->keys->_length == 0) /* special case means AnyKey/AllKeys */
529 grab_mask.key_val = AnyKey;
531 fprintf (stderr, "AnyKey grab!");
533 process_cb (controller, &grab_mask);
539 for (i = 0; i < key_listener->keys->_length; ++i)
541 Accessibility_KeyDefinition keydef = key_listener->keys->_buffer[i];
542 long int key_val = keydef.keysym;
543 /* X Grabs require keycodes, not keysyms */
544 if (keydef.keystring && keydef.keystring[0])
546 key_val = XStringToKeysym(keydef.keystring);
550 key_val = XKeysymToKeycode (spi_get_display (), (KeySym) key_val);
554 key_val = keydef.keycode;
556 grab_mask.key_val = key_val;
557 process_cb (controller, &grab_mask);
563 spi_controller_register_global_keygrabs (SpiDEController *controller,
564 DEControllerKeyListener *key_listener)
566 handle_keygrab (controller, key_listener, _register_keygrab);
567 return spi_controller_update_key_grabs (controller, NULL);
571 spi_controller_deregister_global_keygrabs (SpiDEController *controller,
572 DEControllerKeyListener *key_listener)
574 handle_keygrab (controller, key_listener, _deregister_keygrab);
575 spi_controller_update_key_grabs (controller, NULL);
579 spi_controller_register_device_listener (SpiDEController *controller,
580 DEControllerListener *listener,
581 CORBA_Environment *ev)
583 DEControllerKeyListener *key_listener;
585 switch (listener->type) {
586 case SPI_DEVICE_TYPE_KBD:
587 key_listener = (DEControllerKeyListener *) listener;
589 controller->key_listeners = g_list_prepend (controller->key_listeners,
591 if (key_listener->mode->global)
593 return spi_controller_register_global_keygrabs (controller, key_listener);
598 case SPI_DEVICE_TYPE_MOUSE:
599 controller->mouse_listeners = g_list_prepend (controller->mouse_listeners, listener);
602 fprintf (stderr, "WARNING: listener registration for unknown device type.\n");
609 spi_controller_notify_mouselisteners (SpiDEController *controller,
610 const Accessibility_DeviceEvent *event,
611 CORBA_Environment *ev)
614 GSList *notify = NULL, *l2;
615 GList **listeners = &controller->mouse_listeners;
616 gboolean is_consumed;
623 for (l = *listeners; l; l = l->next)
625 DEControllerListener *listener = l->data;
627 if (spi_eventtype_seq_contains_event (listener->typeseq, event))
629 Accessibility_DeviceEventListener ls = listener->object;
631 if (ls != CORBA_OBJECT_NIL)
633 /* we clone (don't dup) the listener, to avoid refcount inc. */
634 notify = g_slist_prepend (notify,
635 spi_listener_clone (listener, ev));
640 #ifdef SPI_KEYEVENT_DEBUG
643 g_print ("no match for event\n");
648 for (l2 = notify; l2 && !is_consumed; l2 = l2->next)
650 DEControllerListener *listener = l2->data;
651 Accessibility_DeviceEventListener ls = listener->object;
653 fprintf (stderr, "notifying mouse listener\n");
654 CORBA_exception_init (ev);
655 is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, event, ev);
656 fprintf (stderr, "%sconsumed\n", is_consumed ? "" : "not ");
661 fprintf (stderr, "error notifying listener, removing it\n");
662 spi_deregister_controller_device_listener (controller, listener,
664 CORBA_exception_free (ev);
667 spi_listener_clone_free ((DEControllerListener *) l2->data, ev);
670 for (; l2; l2 = l2->next)
672 DEControllerListener *listener = l2->data;
673 spi_listener_clone_free (listener, ev);
674 /* clone doesn't have its own ref, so don't use spi_device_listener_free */
677 g_slist_free (notify);
680 if (is_consumed) g_message ("consumed\n");
686 spi_device_event_controller_forward_mouse_event (SpiDEController *controller,
689 Accessibility_Event e;
690 Accessibility_DeviceEvent mouse_e;
691 CORBA_Environment ev;
692 gchar event_name[24];
693 gboolean is_consumed = FALSE;
694 gboolean xkb_mod_unlatch_occurred;
695 DEControllerPrivateData *priv;
696 XButtonEvent *xbutton_event = (XButtonEvent *) xevent;
698 int button = xbutton_event->button;
700 unsigned int mouse_button_state = xbutton_event->state;
705 mouse_button_state |= Button1Mask;
708 mouse_button_state |= Button2Mask;
711 mouse_button_state |= Button3Mask;
714 mouse_button_state |= Button4Mask;
717 mouse_button_state |= Button5Mask;
720 last_mouse_pos->x = ((XButtonEvent *) xevent)->x_root;
721 last_mouse_pos->y = ((XButtonEvent *) xevent)->y_root;
724 fprintf (stderr, "mouse button %d %s (%x)\n",
725 xbutton_event->button,
726 (xevent->type == ButtonPress) ? "Press" : "Release",
729 snprintf (event_name, 22, "mouse:button:%d%c", button,
730 (xevent->type == ButtonPress) ? 'p' : 'r');
732 /* TODO: distinguish between physical and logical buttons */
733 mouse_e.type = (xevent->type == ButtonPress) ?
734 Accessibility_BUTTON_PRESSED_EVENT :
735 Accessibility_BUTTON_RELEASED_EVENT;
737 mouse_e.hw_code = button;
738 mouse_e.modifiers = (CORBA_unsigned_short) xbutton_event->state;
739 mouse_e.timestamp = (CORBA_unsigned_long) xbutton_event->time;
740 mouse_e.event_string = CORBA_string_dup ("");
741 mouse_e.is_text = CORBA_FALSE;
742 is_consumed = spi_controller_notify_mouselisteners (controller, &mouse_e, &ev);
743 CORBA_free (mouse_e.event_string);
745 e.type = CORBA_string_dup (event_name);
746 e.source = BONOBO_OBJREF (controller->registry->desktop);
747 e.detail1 = last_mouse_pos->x;
748 e.detail2 = last_mouse_pos->y;
749 CORBA_exception_init (&ev);
751 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
755 xkb_mod_unlatch_occurred = (xevent->type == ButtonPress ||
756 xevent->type == ButtonRelease);
758 /* if client wants to consume this event, and XKB latch state was
759 * unset by this button event, we reset it
761 if (is_consumed && xkb_mod_unlatch_occurred)
763 priv = g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
764 priv->pending_xkb_mod_relatch_mask |= priv->xkb_latch_mask;
767 XAllowEvents (spi_get_display (),
768 (is_consumed) ? SyncPointer : ReplayPointer,
772 static GdkFilterReturn
773 global_filter_fn (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
775 XEvent *xevent = gdk_xevent;
776 SpiDEController *controller;
777 DEControllerPrivateData *priv;
778 Display *display = spi_get_display ();
779 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
780 priv = (DEControllerPrivateData *)
781 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
783 if (xevent->type == KeyPress || xevent->type == KeyRelease)
785 spi_device_event_controller_forward_key_event (controller, xevent);
786 return GDK_FILTER_CONTINUE;
788 if (xevent->type == ButtonPress || xevent->type == ButtonRelease)
790 spi_device_event_controller_forward_mouse_event (controller, xevent);
792 if (xevent->type == priv->xkb_base_event_code)
794 XkbAnyEvent * xkb_ev = (XkbAnyEvent *) xevent;
796 if (xkb_ev->xkb_type == XkbStateNotify)
798 XkbStateNotifyEvent *xkb_snev =
799 (XkbStateNotifyEvent *) xkb_ev;
800 priv->xkb_latch_mask = xkb_snev->latched_mods;
801 if (priv->pending_xkb_mod_relatch_mask)
803 unsigned int feedback_mask;
804 fprintf (stderr, "relatching %x\n",
805 priv->pending_xkb_mod_relatch_mask);
806 /* temporarily turn off the latch bell, if it's on */
807 XkbGetControls (display,
808 XkbAccessXFeedbackMask,
810 feedback_mask = priv->xkb_desc->ctrls->ax_options;
811 if (feedback_mask & XkbAX_StickyKeysFBMask)
813 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
815 priv->xkb_desc->ctrls->ax_options
816 &= ~(XkbAX_StickyKeysFBMask);
817 XkbChangeControls (display, priv->xkb_desc, &changes);
819 XkbLatchModifiers (display,
821 priv->pending_xkb_mod_relatch_mask,
822 priv->pending_xkb_mod_relatch_mask);
823 if (feedback_mask & XkbAX_StickyKeysFBMask)
825 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
827 priv->xkb_desc->ctrls->ax_options = feedback_mask;
828 XkbChangeControls (display, priv->xkb_desc, &changes);
830 fprintf (stderr, "relatched %x\n",
831 priv->pending_xkb_mod_relatch_mask);
832 priv->pending_xkb_mod_relatch_mask = 0;
836 fprintf (stderr, "XKB event %d\n", xkb_ev->xkb_type);
839 return GDK_FILTER_CONTINUE;
843 _spi_controller_device_error_handler (Display *display, XErrorEvent *error)
845 if (error->error_code == BadAccess)
847 g_message ("Could not complete key grab: grab already in use.\n");
848 spi_error_code = BadAccess;
853 return (*x_default_error_handler) (display, error);
858 spi_controller_register_with_devices (SpiDEController *controller)
860 DEControllerPrivateData *priv = (DEControllerPrivateData *)
861 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
863 priv->xkb_desc = XkbGetMap (spi_get_display (), 0, XkbUseCoreKbd);
865 /* calls to device-specific implementations and routines go here */
866 /* register with: keyboard hardware code handler */
867 /* register with: (translated) keystroke handler */
869 priv->have_xkb = XkbQueryExtension (spi_get_display (),
870 &priv->xkb_major_extension_opcode,
871 &priv->xkb_base_event_code,
872 &priv->xkb_base_error_code, NULL, NULL);
875 XkbSelectEvents (spi_get_display (),
877 XkbStateNotifyMask, XkbStateNotifyMask);
879 gdk_window_add_filter (NULL, global_filter_fn, controller);
881 gdk_window_set_events (gdk_get_default_root_window (),
882 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
884 x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
888 spi_key_set_contains_key (Accessibility_KeySet *key_set,
889 const Accessibility_DeviceEvent *key_event)
896 g_print ("null key set!");
900 len = key_set->_length;
902 if (len == 0) /* special case, means "all keys/any key" */
904 g_print ("anykey\n");
908 for (i = 0; i < len; ++i)
910 #ifdef SPI_KEYEVENT_DEBUG
911 g_print ("key_set[%d] = %d; key_event %d, code %d, string %s\n",
912 i, (int) key_set->_buffer[i].keycode,
913 (int) key_event->id, (int) key_event->hw_code,
914 key_event->event_string);
916 if (key_set->_buffer[i].keysym == (CORBA_long) key_event->id)
920 if (key_set->_buffer[i].keycode == (CORBA_long) key_event->hw_code)
924 if (key_event->event_string && key_event->event_string[0] &&
925 !strcmp (key_set->_buffer[i].keystring, key_event->event_string))
935 spi_eventtype_seq_contains_event (Accessibility_EventTypeSeq *type_seq,
936 const Accessibility_DeviceEvent *event)
944 g_print ("null type seq!");
948 len = type_seq->_length;
950 if (len == 0) /* special case, means "all events/any event" */
955 for (i = 0; i < len; ++i)
958 g_print ("type_seq[%d] = %d; event type = %d\n", i,
959 (int) type_seq->_buffer[i], (int) event->type);
961 if (type_seq->_buffer[i] == (CORBA_long) event->type)
971 spi_key_event_matches_listener (const Accessibility_DeviceEvent *key_event,
972 DEControllerKeyListener *listener,
973 CORBA_boolean is_system_global)
975 if ((key_event->modifiers == (CORBA_unsigned_short) (listener->mask & 0xFFFF)) &&
976 spi_key_set_contains_key (listener->keys, key_event) &&
977 spi_eventtype_seq_contains_event (listener->listener.typeseq, key_event) &&
978 (is_system_global == listener->mode->global))
989 spi_controller_notify_keylisteners (SpiDEController *controller,
990 const Accessibility_DeviceEvent *key_event,
991 CORBA_boolean is_system_global,
992 CORBA_Environment *ev)
995 GSList *notify = NULL, *l2;
996 GList **key_listeners = &controller->key_listeners;
997 gboolean is_consumed;
1004 for (l = *key_listeners; l; l = l->next)
1006 DEControllerKeyListener *key_listener = l->data;
1008 if (spi_key_event_matches_listener (key_event, key_listener, is_system_global))
1010 Accessibility_DeviceEventListener ls = key_listener->listener.object;
1012 if (ls != CORBA_OBJECT_NIL)
1014 /* we clone (don't dup) the listener, to avoid refcount inc. */
1015 notify = g_slist_prepend (notify,
1016 spi_key_listener_clone (key_listener, ev));
1021 #ifdef SPI_KEYEVENT_DEBUG
1024 g_print ("no match for event\n");
1028 is_consumed = FALSE;
1029 for (l2 = notify; l2 && !is_consumed; l2 = l2->next)
1031 DEControllerKeyListener *key_listener = l2->data;
1032 Accessibility_DeviceEventListener ls = key_listener->listener.object;
1034 is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, key_event, ev);
1038 is_consumed = FALSE;
1039 spi_deregister_controller_key_listener (controller, key_listener,
1041 CORBA_exception_free (ev);
1044 spi_key_listener_clone_free (key_listener, ev);
1047 for (; l2; l2 = l2->next)
1049 DEControllerKeyListener *key_listener = l2->data;
1050 spi_key_listener_clone_free (key_listener, ev);
1051 /* clone doesn't have its own ref, so don't use spi_dec_listener_free */
1054 g_slist_free (notify);
1057 if (is_consumed) g_message ("consumed\n");
1063 spi_clear_error_state ()
1065 gboolean retval = spi_error_code != 0;
1070 static Accessibility_DeviceEvent
1071 spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
1073 Accessibility_DeviceEvent key_event;
1075 const int cbuf_bytes = 20;
1076 char cbuf [cbuf_bytes];
1078 keysym = XLookupKeysym (x_key_event, 0);
1079 key_event.id = (CORBA_long)(keysym);
1080 key_event.hw_code = (CORBA_short) x_key_event->keycode;
1081 if (((XEvent *) x_key_event)->type == KeyPress)
1083 key_event.type = Accessibility_KEY_PRESSED;
1087 key_event.type = Accessibility_KEY_RELEASED;
1089 key_event.modifiers = (CORBA_unsigned_short)(x_key_event->state);
1090 key_event.is_text = CORBA_FALSE;
1094 key_event.event_string = CORBA_string_dup ("space");
1097 #ifdef SPI_KEYEVENT_DEBUG
1098 fprintf(stderr, "Tab\n");
1100 key_event.event_string = CORBA_string_dup ("Tab");
1103 key_event.event_string = CORBA_string_dup ("Backspace");
1106 key_event.event_string = CORBA_string_dup ("Return");
1109 key_event.event_string = CORBA_string_dup ("Home");
1112 key_event.event_string = CORBA_string_dup ("Page_Down");
1115 key_event.event_string = CORBA_string_dup ("Page_Up");
1118 key_event.event_string = CORBA_string_dup ("F1");
1121 key_event.event_string = CORBA_string_dup ("F2");
1124 key_event.event_string = CORBA_string_dup ("F3");
1127 key_event.event_string = CORBA_string_dup ("F4");
1130 key_event.event_string = CORBA_string_dup ("F5");
1133 key_event.event_string = CORBA_string_dup ("F6");
1136 key_event.event_string = CORBA_string_dup ("F7");
1139 key_event.event_string = CORBA_string_dup ("F8");
1142 key_event.event_string = CORBA_string_dup ("F9");
1145 key_event.event_string = CORBA_string_dup ("F10");
1148 key_event.event_string = CORBA_string_dup ("F11");
1151 key_event.event_string = CORBA_string_dup ("F12");
1154 key_event.event_string = CORBA_string_dup ("End");
1157 key_event.event_string = CORBA_string_dup ("Escape");
1160 key_event.event_string = CORBA_string_dup ("Up");
1163 key_event.event_string = CORBA_string_dup ("Down");
1166 key_event.event_string = CORBA_string_dup ("Left");
1169 key_event.event_string = CORBA_string_dup ("Right");
1172 if (XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL) > 0)
1174 key_event.event_string = CORBA_string_dup (cbuf);
1175 if (isgraph (keysym))
1177 key_event.is_text = CORBA_TRUE; /* FIXME: incorrect for some composed chars? */
1182 key_event.event_string = CORBA_string_dup ("");
1186 key_event.timestamp = (CORBA_unsigned_long) x_key_event->time;
1187 #ifdef SPI_KEYEVENT_DEBUG
1189 "Key %lu pressed (%c), modifiers %d\n",
1190 (unsigned long) keysym,
1191 keysym ? (int) keysym : '*',
1192 (int) x_key_event->state);
1195 fprintf (stderr, "%s%c",
1196 (x_key_event->state & Mod1Mask)?"Alt-":"",
1197 ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
1198 g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
1199 #endif /* SPI_DEBUG */
1204 spi_controller_update_key_grabs (SpiDEController *controller,
1205 Accessibility_DeviceEvent *recv)
1208 gboolean update_failed = FALSE;
1210 g_return_val_if_fail (controller != NULL, FALSE);
1213 * masks known to work with default RH 7.1+:
1214 * 0 (no mods), LockMask, Mod1Mask, Mod2Mask, ShiftMask,
1215 * ShiftMask|LockMask, Mod1Mask|LockMask, Mod2Mask|LockMask,
1216 * ShiftMask|Mod1Mask, ShiftMask|Mod2Mask, Mod1Mask|Mod2Mask,
1217 * ShiftMask|LockMask|Mod1Mask, ShiftMask|LockMask|Mod2Mask,
1219 * ControlMask grabs are broken, must be in use already
1221 for (l = controller->keygrabs_list; l; l = next)
1224 gboolean re_issue_grab;
1225 DEControllerGrabMask *grab_mask = l->data;
1229 re_issue_grab = recv &&
1230 /* (recv->type == Accessibility_KEY_RELEASED) && - (?) */
1231 (recv->modifiers & grab_mask->mod_mask) &&
1232 (grab_mask->key_val == keycode_for_keysym (recv->id));
1235 fprintf (stderr, "mask=%lx %lx (%c%c) %s\n",
1236 (long int) grab_mask->key_val,
1237 (long int) grab_mask->mod_mask,
1238 grab_mask->pending_add ? '+' : '.',
1239 grab_mask->pending_remove ? '-' : '.',
1240 re_issue_grab ? "re-issue": "");
1245 if (grab_mask->pending_add && grab_mask->pending_remove)
1249 else if (grab_mask->pending_remove)
1252 fprintf (stderr, "ungrabbing, mask=%x\n", grab_mask->mod_mask);
1254 XUngrabKey (spi_get_display (),
1256 grab_mask->mod_mask,
1257 gdk_x11_get_default_root_xwindow ());
1261 else if (grab_mask->pending_add || re_issue_grab)
1265 fprintf (stderr, "grab %d with mask %x\n", grab_mask->key_val, grab_mask->mod_mask);
1267 XGrabKey (spi_get_display (),
1269 grab_mask->mod_mask,
1270 gdk_x11_get_default_root_xwindow (),
1274 XSync (spi_get_display (), False);
1275 update_failed = spi_clear_error_state ();
1276 if (update_failed) {
1277 while (grab_mask->ref_count > 0) --grab_mask->ref_count;
1282 grab_mask->pending_add = FALSE;
1283 grab_mask->pending_remove = FALSE;
1287 g_assert (grab_mask->ref_count <= 0);
1289 controller->keygrabs_list = g_list_delete_link (
1290 controller->keygrabs_list, l);
1292 spi_grab_mask_free (grab_mask);
1297 return ! update_failed;
1301 * Implemented GObject::finalize
1304 spi_device_event_controller_object_finalize (GObject *object)
1306 SpiDEController *controller;
1307 DEControllerPrivateData *private;
1308 controller = SPI_DEVICE_EVENT_CONTROLLER (object);
1311 fprintf(stderr, "spi_device_event_controller_object_finalize called\n");
1313 /* disconnect any special listeners, get rid of outstanding keygrabs */
1314 XUngrabKey (spi_get_display (), AnyKey, AnyModifier, DefaultRootWindow (spi_get_display ()));
1316 private = g_object_get_data (G_OBJECT (controller), "spi-dec-private");
1317 if (private->xkb_desc)
1318 XkbFreeKeyboard (private->xkb_desc, 0, True);
1320 spi_device_event_controller_parent_class->finalize (object);
1324 * CORBA Accessibility::DEController::registerKeystrokeListener
1325 * method implementation
1327 static CORBA_boolean
1328 impl_register_keystroke_listener (PortableServer_Servant servant,
1329 const Accessibility_DeviceEventListener l,
1330 const Accessibility_KeySet *keys,
1331 const Accessibility_ControllerEventMask mask,
1332 const Accessibility_EventTypeSeq *type,
1333 const Accessibility_EventListenerMode *mode,
1334 CORBA_Environment *ev)
1336 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1337 bonobo_object_from_servant (servant));
1338 DEControllerKeyListener *dec_listener;
1340 fprintf (stderr, "registering keystroke listener %p with maskVal %lu\n",
1341 (void *) l, (unsigned long) mask);
1343 dec_listener = spi_dec_key_listener_new (l, keys, mask, type, mode, ev);
1344 return spi_controller_register_device_listener (
1345 controller, (DEControllerListener *) dec_listener, ev);
1350 * CORBA Accessibility::DEController::registerDeviceEventListener
1351 * method implementation
1353 static CORBA_boolean
1354 impl_register_device_listener (PortableServer_Servant servant,
1355 const Accessibility_DeviceEventListener l,
1356 const Accessibility_EventTypeSeq *event_types,
1357 CORBA_Environment *ev)
1359 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1360 bonobo_object_from_servant (servant));
1361 DEControllerListener *dec_listener;
1363 dec_listener = spi_dec_listener_new (l, event_types, ev);
1364 return spi_controller_register_device_listener (
1365 controller, (DEControllerListener *) dec_listener, ev);
1369 CORBA_Environment *ev;
1370 DEControllerListener *listener;
1371 } RemoveListenerClosure;
1373 static SpiReEntrantContinue
1374 remove_listener_cb (GList * const *list,
1377 DEControllerListener *listener = (*list)->data;
1378 RemoveListenerClosure *ctx = user_data;
1380 if (CORBA_Object_is_equivalent (ctx->listener->object,
1381 listener->object, ctx->ev))
1383 spi_re_entrant_list_delete_link (list);
1384 spi_dec_listener_free (listener, ctx->ev);
1387 return SPI_RE_ENTRANT_CONTINUE;
1390 static SpiReEntrantContinue
1391 copy_key_listener_cb (GList * const *list,
1394 DEControllerKeyListener *key_listener = (*list)->data;
1395 RemoveListenerClosure *ctx = user_data;
1397 if (CORBA_Object_is_equivalent (ctx->listener->object,
1398 key_listener->listener.object, ctx->ev))
1400 /* TODO: FIXME aggregate keys in case the listener is registered twice */
1401 DEControllerKeyListener *ctx_key_listener =
1402 (DEControllerKeyListener *) ctx->listener;
1403 CORBA_free (ctx_key_listener->keys);
1404 ctx_key_listener->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
1407 return SPI_RE_ENTRANT_CONTINUE;
1411 spi_deregister_controller_device_listener (SpiDEController *controller,
1412 DEControllerListener *listener,
1413 CORBA_Environment *ev)
1415 spi_re_entrant_list_foreach (&controller->mouse_listeners,
1416 remove_listener_cb, listener);
1420 spi_deregister_controller_key_listener (SpiDEController *controller,
1421 DEControllerKeyListener *key_listener,
1422 CORBA_Environment *ev)
1424 RemoveListenerClosure ctx;
1427 ctx.listener = (DEControllerListener *) key_listener;
1429 /* special case, copy keyset from existing controller list entry */
1430 if (key_listener->keys->_length == 0)
1432 spi_re_entrant_list_foreach (&controller->key_listeners,
1433 copy_key_listener_cb, &ctx);
1436 spi_controller_deregister_global_keygrabs (controller, key_listener);
1438 spi_re_entrant_list_foreach (&controller->key_listeners,
1439 remove_listener_cb, &ctx);
1444 * CORBA Accessibility::DEController::deregisterKeystrokeListener
1445 * method implementation
1448 impl_deregister_keystroke_listener (PortableServer_Servant servant,
1449 const Accessibility_DeviceEventListener l,
1450 const Accessibility_KeySet *keys,
1451 const Accessibility_ControllerEventMask mask,
1452 const Accessibility_EventTypeSeq *type,
1453 CORBA_Environment *ev)
1455 DEControllerKeyListener *key_listener;
1456 SpiDEController *controller;
1458 controller = SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1460 key_listener = spi_dec_key_listener_new (l, keys, mask, type, NULL, ev);
1462 #ifdef SPI_DEREGISTER_DEBUG
1463 fprintf (stderr, "deregistering keystroke listener %p with maskVal %lu\n",
1464 (void *) l, (unsigned long) mask->value);
1467 spi_deregister_controller_key_listener (controller, key_listener, ev);
1469 spi_dec_listener_free ((DEControllerListener *) key_listener, ev);
1473 * CORBA Accessibility::DEController::deregisterDeviceEventListener
1474 * method implementation
1477 impl_deregister_device_listener (PortableServer_Servant servant,
1478 const Accessibility_DeviceEventListener l,
1479 const Accessibility_EventTypeSeq *event_types,
1480 CORBA_Environment *ev)
1482 SpiDEController *controller;
1483 DEControllerListener *listener =
1484 spi_dec_listener_new (l, event_types, ev);
1486 controller = SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1488 spi_deregister_controller_device_listener (controller, listener, ev);
1490 spi_dec_listener_free (listener, ev);
1493 static unsigned int dec_xkb_get_slowkeys_delay (SpiDEController *controller)
1495 unsigned int retval = 0;
1496 DEControllerPrivateData *priv = (DEControllerPrivateData *)
1497 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
1499 #ifdef XKB_HAS_GET_SLOW_KEYS_DELAY
1500 retval = XkbGetSlowKeysDelay (spi_get_display (),
1501 XkbUseCoreKbd, &bounce_delay);
1503 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
1505 Status s = XkbGetControls (spi_get_display (),
1506 XkbAllControlsMask, priv->xkb_desc);
1509 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbSlowKeysMask)
1510 retval = priv->xkb_desc->ctrls->slow_keys_delay;
1515 #ifdef SPI_XKB_DEBUG
1516 fprintf (stderr, "SlowKeys delay: %d\n", (int) retval);
1521 static unsigned int dec_xkb_get_bouncekeys_delay (SpiDEController *controller)
1523 unsigned int retval = 0;
1524 DEControllerPrivateData *priv = (DEControllerPrivateData *)
1525 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
1527 #ifdef XKB_HAS_GET_BOUNCE_KEYS_DELAY
1528 retval = XkbGetBounceKeysDelay (spi_get_display (),
1529 XkbUseCoreKbd, &bounce_delay);
1531 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
1533 Status s = XkbGetControls (spi_get_display (),
1534 XkbAllControlsMask, priv->xkb_desc);
1537 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbBounceKeysMask)
1538 retval = priv->xkb_desc->ctrls->debounce_delay;
1543 #ifdef SPI_XKB_DEBUG
1544 fprintf (stderr, "BounceKeys delay: %d\n", (int) retval);
1550 dec_synth_keycode_press (SpiDEController *controller,
1551 unsigned int keycode)
1553 unsigned int time = CurrentTime;
1554 unsigned int bounce_delay;
1555 unsigned int elapsed_msec;
1557 DEControllerPrivateData *priv =
1558 (DEControllerPrivateData *) g_object_get_qdata (G_OBJECT (controller),
1559 spi_dec_private_quark);
1560 if (keycode == priv->last_release_keycode)
1562 bounce_delay = dec_xkb_get_bouncekeys_delay (controller);
1565 gettimeofday (&tv, NULL);
1567 (tv.tv_sec - priv->last_release_time.tv_sec) * 1000
1568 + (tv.tv_usec - priv->last_release_time.tv_usec) / 1000;
1569 #ifdef SPI_XKB_DEBUG
1570 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1571 (long) (tv.tv_usec - priv->last_release_time.tv_usec));
1573 #ifdef THIS_IS_BROKEN
1574 if (elapsed_msec < bounce_delay)
1575 time = bounce_delay - elapsed_msec + 1;
1577 time = bounce_delay + 10;
1578 /* fudge for broken XTest */
1580 #ifdef SPI_XKB_DEBUG
1581 fprintf (stderr, "waiting %d ms\n", time);
1585 fprintf (stderr, "press %d\n", (int) keycode);
1586 XTestFakeKeyEvent (spi_get_display (), keycode, True, time);
1587 priv->last_press_keycode = keycode;
1588 XSync (spi_get_display (), False);
1589 gettimeofday (&priv->last_press_time, NULL);
1594 dec_synth_keycode_release (SpiDEController *controller,
1595 unsigned int keycode)
1597 unsigned int time = CurrentTime;
1598 unsigned int slow_delay;
1599 unsigned int elapsed_msec;
1601 DEControllerPrivateData *priv =
1602 (DEControllerPrivateData *) g_object_get_qdata (G_OBJECT (controller),
1603 spi_dec_private_quark);
1604 if (keycode == priv->last_press_keycode)
1606 slow_delay = dec_xkb_get_slowkeys_delay (controller);
1609 gettimeofday (&tv, NULL);
1611 (tv.tv_sec - priv->last_press_time.tv_sec) * 1000
1612 + (tv.tv_usec - priv->last_press_time.tv_usec) / 1000;
1613 #ifdef SPI_XKB_DEBUG
1614 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1615 (long) (tv.tv_usec - priv->last_press_time.tv_usec));
1617 #ifdef THIS_IS_BROKEN_DUNNO_WHY
1618 if (elapsed_msec < slow_delay)
1619 time = slow_delay - elapsed_msec + 1;
1621 time = slow_delay + 10;
1622 /* our XTest seems broken, we have to add slop as above */
1624 #ifdef SPI_XKB_DEBUG
1625 fprintf (stderr, "waiting %d ms\n", time);
1629 fprintf (stderr, "release %d\n", (int) keycode);
1630 XTestFakeKeyEvent (spi_get_display (), keycode, False, time);
1631 priv->last_release_keycode = keycode;
1632 XSync (spi_get_display (), False);
1633 gettimeofday (&priv->last_release_time, NULL);
1638 * CORBA Accessibility::DEController::registerKeystrokeListener
1639 * method implementation
1642 impl_generate_keyboard_event (PortableServer_Servant servant,
1643 const CORBA_long keycode,
1644 const CORBA_char *keystring,
1645 const Accessibility_KeySynthType synth_type,
1646 CORBA_Environment *ev)
1648 SpiDEController *controller =
1649 SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1650 long key_synth_code;
1651 unsigned int slow_keys_delay;
1652 unsigned int press_time;
1653 unsigned int release_time;
1656 fprintf (stderr, "synthesizing keystroke %ld, type %d\n",
1657 (long) keycode, (int) synth_type);
1659 /* TODO: hide/wrap/remove X dependency */
1662 * TODO: when initializing, query for XTest extension before using,
1663 * and fall back to XSendEvent() if XTest is not available.
1666 /* TODO: implement keystring mode also */
1667 gdk_error_trap_push ();
1671 case Accessibility_KEY_PRESS:
1672 dec_synth_keycode_press (controller, keycode);
1674 case Accessibility_KEY_PRESSRELEASE:
1675 dec_synth_keycode_press (controller, keycode);
1676 case Accessibility_KEY_RELEASE:
1677 dec_synth_keycode_release (controller, keycode);
1679 case Accessibility_KEY_SYM:
1680 #ifdef SPI_XKB_DEBUG
1681 fprintf (stderr, "KeySym synthesis\n");
1683 key_synth_code = keycode_for_keysym (keycode);
1684 dec_synth_keycode_press (controller, key_synth_code);
1685 dec_synth_keycode_release (controller, key_synth_code);
1687 case Accessibility_KEY_STRING:
1688 fprintf (stderr, "Not yet implemented\n");
1691 if (gdk_error_trap_pop ())
1693 g_warning ("Error emitting keystroke");
1697 /* Accessibility::DEController::generateMouseEvent */
1699 impl_generate_mouse_event (PortableServer_Servant servant,
1702 const CORBA_char *eventName,
1703 CORBA_Environment *ev)
1706 gboolean error = FALSE;
1707 Display *display = spi_get_display ();
1709 fprintf (stderr, "generating mouse %s event at %ld, %ld\n",
1710 eventName, (long int) x, (long int) y);
1712 g_message ("mouse event synthesis\n");
1713 switch (eventName[0])
1716 switch (eventName[1])
1718 /* TODO: check number of buttons before parsing */
1733 if (x != -1 && y != -1)
1735 XTestFakeMotionEvent (display, DefaultScreen (display),
1738 XTestFakeButtonEvent (display, button, !(eventName[2] == 'r'), 0);
1739 if (eventName[2] == 'c')
1740 XTestFakeButtonEvent (display, button, FALSE, 1);
1741 else if (eventName[2] == 'd')
1743 XTestFakeButtonEvent (display, button, FALSE, 1);
1744 XTestFakeButtonEvent (display, button, TRUE, 2);
1745 XTestFakeButtonEvent (display, button, FALSE, 3);
1749 case 'r': /* relative motion */
1750 XTestFakeRelativeMotionEvent (display, x, y, 0);
1752 case 'a': /* absolute motion */
1753 XTestFakeMotionEvent (display, DefaultScreen (display),
1759 /* Accessibility::DEController::notifyListenersSync */
1760 static CORBA_boolean
1761 impl_notify_listeners_sync (PortableServer_Servant servant,
1762 const Accessibility_DeviceEvent *event,
1763 CORBA_Environment *ev)
1765 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1766 bonobo_object_from_servant (servant));
1768 g_print ("notifylistening listeners synchronously: controller %p, event id %d\n",
1769 controller, (int) event->id);
1771 return spi_controller_notify_keylisteners (controller, event, CORBA_FALSE, ev) ?
1772 CORBA_TRUE : CORBA_FALSE;
1775 /* Accessibility::DEController::notifyListenersAsync */
1777 impl_notify_listeners_async (PortableServer_Servant servant,
1778 const Accessibility_DeviceEvent *event,
1779 CORBA_Environment *ev)
1781 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1782 bonobo_object_from_servant (servant));
1784 fprintf (stderr, "notifying listeners asynchronously\n");
1786 spi_controller_notify_keylisteners (controller, event, CORBA_FALSE, ev);
1790 spi_device_event_controller_class_init (SpiDEControllerClass *klass)
1792 GObjectClass * object_class = (GObjectClass *) klass;
1793 POA_Accessibility_DeviceEventController__epv *epv = &klass->epv;
1795 spi_device_event_controller_parent_class = g_type_class_peek_parent (klass);
1797 object_class->finalize = spi_device_event_controller_object_finalize;
1799 epv->registerKeystrokeListener = impl_register_keystroke_listener;
1800 epv->deregisterKeystrokeListener = impl_deregister_keystroke_listener;
1801 epv->registerDeviceEventListener = impl_register_device_listener;
1802 epv->deregisterDeviceEventListener = impl_deregister_device_listener;
1803 epv->generateKeyboardEvent = impl_generate_keyboard_event;
1804 epv->generateMouseEvent = impl_generate_mouse_event;
1805 epv->notifyListenersSync = impl_notify_listeners_sync;
1806 epv->notifyListenersAsync = impl_notify_listeners_async;
1808 if (!spi_dec_private_quark)
1809 spi_dec_private_quark = g_quark_from_static_string ("spi-dec-private");
1813 spi_device_event_controller_init (SpiDEController *device_event_controller)
1815 DEControllerPrivateData *private;
1816 device_event_controller->key_listeners = NULL;
1817 device_event_controller->mouse_listeners = NULL;
1818 device_event_controller->keygrabs_list = NULL;
1821 * TODO: fixme, this module makes the foolish assumptions that
1822 * registryd uses the same display as the apps, and that the
1823 * DISPLAY environment variable is set.
1825 gdk_init (NULL, NULL);
1827 private = g_new0 (DEControllerPrivateData, 1);
1828 gettimeofday (&private->last_press_time, NULL);
1829 gettimeofday (&private->last_release_time, NULL);
1830 g_object_set_qdata (G_OBJECT (device_event_controller),
1831 spi_dec_private_quark,
1834 spi_controller_register_with_devices (device_event_controller);
1838 spi_device_event_controller_forward_key_event (SpiDEController *controller,
1839 const XEvent *event)
1841 gboolean is_consumed = FALSE;
1842 CORBA_Environment ev;
1843 Accessibility_DeviceEvent key_event;
1845 g_assert (event->type == KeyPress || event->type == KeyRelease);
1847 CORBA_exception_init (&ev);
1849 key_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1851 spi_controller_update_key_grabs (controller, &key_event);
1853 /* relay to listeners, and decide whether to consume it or not */
1854 is_consumed = spi_controller_notify_keylisteners (
1855 controller, &key_event, CORBA_TRUE, &ev);
1859 XAllowEvents (spi_get_display (), AsyncKeyboard, CurrentTime);
1863 XAllowEvents (spi_get_display (), ReplayKeyboard, CurrentTime);
1868 spi_device_event_controller_new (SpiRegistry *registry)
1870 SpiDEController *retval = g_object_new (
1871 SPI_DEVICE_EVENT_CONTROLLER_TYPE, NULL);
1872 DEControllerPrivateData *private;
1874 retval->registry = SPI_REGISTRY (bonobo_object_ref (
1875 BONOBO_OBJECT (registry)));
1877 spi_dec_init_mouse_listener (registry);
1878 /* TODO: kill mouse listener on finalize */
1882 BONOBO_TYPE_FUNC_FULL (SpiDEController,
1883 Accessibility_DeviceEventController,
1885 spi_device_event_controller);