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 #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_static = {0, 0};
57 static GdkPoint *last_mouse_pos = &last_mouse_pos_static;
58 static unsigned int mouse_mask_state = 0;
59 static unsigned int mouse_button_mask =
60 Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask;
61 static unsigned int key_modifier_mask =
62 Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask | ShiftMask | LockMask | ControlMask;
64 static GQuark spi_dec_private_quark = 0;
66 int (*x_default_error_handler) (Display *display, XErrorEvent *error_event);
70 SPI_DEVICE_TYPE_MOUSE,
71 SPI_DEVICE_TYPE_LAST_DEFINED
72 } SpiDeviceTypeCategory;
76 guint pending_add : 1;
77 guint pending_remove : 1;
79 Accessibility_ControllerEventMask mod_mask;
80 CORBA_unsigned_long key_val; /* KeyCode */
81 } DEControllerGrabMask;
85 SpiDeviceTypeCategory type;
86 Accessibility_EventTypeSeq *typeseq;
87 } DEControllerListener;
90 DEControllerListener listener;
92 Accessibility_KeySet *keys;
93 Accessibility_ControllerEventMask mask;
94 Accessibility_EventListenerMode *mode;
95 } DEControllerKeyListener;
98 unsigned int last_press_keycode;
99 unsigned int last_release_keycode;
100 struct timeval last_press_time;
101 struct timeval last_release_time;
103 int xkb_major_extension_opcode;
104 int xkb_base_event_code;
105 int xkb_base_error_code;
106 unsigned int xkb_latch_mask;
107 unsigned int pending_xkb_mod_relatch_mask;
109 } DEControllerPrivateData;
111 static void spi_controller_register_with_devices (SpiDEController *controller);
112 static gboolean spi_controller_update_key_grabs (SpiDEController *controller,
113 Accessibility_DeviceEvent *recv);
114 static gboolean spi_controller_register_device_listener (SpiDEController *controller,
115 DEControllerListener *l,
116 CORBA_Environment *ev);
117 static void spi_device_event_controller_forward_key_event (SpiDEController *controller,
118 const XEvent *event);
119 static void spi_deregister_controller_device_listener (SpiDEController *controller,
120 DEControllerListener *listener,
121 CORBA_Environment *ev);
122 static void spi_deregister_controller_key_listener (SpiDEController *controller,
123 DEControllerKeyListener *key_listener,
124 CORBA_Environment *ev);
125 static gboolean spi_controller_notify_mouselisteners (SpiDEController *controller,
126 const Accessibility_DeviceEvent *event,
127 CORBA_Environment *ev);
129 static gboolean spi_eventtype_seq_contains_event (Accessibility_EventTypeSeq *type_seq,
130 const Accessibility_DeviceEvent *event);
131 static gboolean spi_clear_error_state (void);
132 static gboolean spi_dec_poll_mouse_moved (gpointer data);
133 static gboolean spi_dec_poll_mouse_moving (gpointer data);
134 static gboolean spi_dec_poll_mouse_idle (gpointer data);
136 #define spi_get_display() GDK_DISPLAY()
138 /* Private methods */
141 keycode_for_keysym (long keysym)
143 return XKeysymToKeycode (spi_get_display (), (KeySym) keysym);
146 static DEControllerGrabMask *
147 spi_grab_mask_clone (DEControllerGrabMask *grab_mask)
149 DEControllerGrabMask *clone = g_new (DEControllerGrabMask, 1);
151 memcpy (clone, grab_mask, sizeof (DEControllerGrabMask));
153 clone->ref_count = 1;
154 clone->pending_add = TRUE;
155 clone->pending_remove = FALSE;
161 spi_grab_mask_free (DEControllerGrabMask *grab_mask)
167 spi_grab_mask_compare_values (gconstpointer p1, gconstpointer p2)
169 DEControllerGrabMask *l1 = (DEControllerGrabMask *) p1;
170 DEControllerGrabMask *l2 = (DEControllerGrabMask *) p2;
178 return ((l1->mod_mask != l2->mod_mask) || (l1->key_val != l2->key_val));
183 spi_dec_set_unlatch_pending (SpiDEController *controller, unsigned mask)
185 DEControllerPrivateData *priv =
186 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
188 if (priv->xkb_latch_mask) fprintf (stderr, "unlatch pending! %x\n", mask);
190 priv->pending_xkb_mod_relatch_mask |= priv->xkb_latch_mask;
193 static gint poll_count = 0;
196 spi_dec_button_update_and_emit (SpiDEController *controller,
199 CORBA_Environment ev;
200 Accessibility_Event e;
201 Accessibility_DeviceEvent mouse_e;
202 gchar event_name[24];
203 gboolean is_consumed = FALSE;
205 if ((mask_return & mouse_button_mask) !=
206 (mouse_mask_state & mouse_button_mask))
208 int button_number = 0;
209 gboolean is_down = False;
211 if (!(mask_return & Button1Mask) &&
212 (mouse_mask_state & Button1Mask))
214 mouse_mask_state &= ~Button1Mask;
217 else if ((mask_return & Button1Mask) &&
218 !(mouse_mask_state & Button1Mask))
220 mouse_mask_state |= Button1Mask;
224 else if (!(mask_return & Button2Mask) &&
225 (mouse_mask_state & Button2Mask))
227 mouse_mask_state &= ~Button2Mask;
230 else if ((mask_return & Button2Mask) &&
231 !(mouse_mask_state & Button2Mask))
233 mouse_mask_state |= Button2Mask;
237 else if (!(mask_return & Button3Mask) &&
238 (mouse_mask_state & Button3Mask))
240 mouse_mask_state &= ~Button3Mask;
243 else if ((mask_return & Button3Mask) &&
244 !(mouse_mask_state & Button3Mask))
246 mouse_mask_state |= Button3Mask;
250 else if (!(mask_return & Button4Mask) &&
251 (mouse_mask_state & Button1Mask))
253 mouse_mask_state &= ~Button4Mask;
256 else if ((mask_return & Button4Mask) &&
257 !(mouse_mask_state & Button1Mask))
259 mouse_mask_state |= Button4Mask;
263 else if (!(mask_return & Button5Mask) &&
264 (mouse_mask_state & Button5Mask))
266 mouse_mask_state &= ~Button5Mask;
269 else if ((mask_return & Button5Mask) &&
270 !(mouse_mask_state & Button5Mask))
272 mouse_mask_state |= Button5Mask;
278 fprintf (stderr, "Button %d %s\n",
279 button_number, (is_down) ? "Pressed" : "Released");
281 snprintf (event_name, 22, "mouse:button:%d%c", button_number,
282 (is_down) ? 'p' : 'r');
283 /* TODO: distinguish between physical and
286 mouse_e.type = (is_down) ?
287 Accessibility_BUTTON_PRESSED_EVENT :
288 Accessibility_BUTTON_RELEASED_EVENT;
289 mouse_e.id = button_number;
290 mouse_e.hw_code = button_number;
291 mouse_e.modifiers = (CORBA_unsigned_short) mouse_mask_state;
292 mouse_e.timestamp = 0;
293 mouse_e.event_string = "";
294 mouse_e.is_text = CORBA_FALSE;
296 spi_controller_notify_mouselisteners (controller,
300 e.source = BONOBO_OBJREF (controller->registry->desktop);
301 e.detail1 = last_mouse_pos->x;
302 e.detail2 = last_mouse_pos->y;
303 spi_init_any_nil (&e.any_data);
304 CORBA_exception_init (&ev);
307 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
312 spi_dec_set_unlatch_pending (controller, mask_return);
324 spi_dec_mouse_check (SpiDEController *controller,
325 int *x, int *y, gboolean *moved)
327 Accessibility_Event e;
328 CORBA_Environment ev;
329 int win_x_return,win_y_return;
330 int poll_count_modulus = 10;
331 unsigned int mask_return;
332 Window root_return, child_return;
333 Display *display = spi_get_display ();
336 XQueryPointer(display, DefaultRootWindow (display),
337 &root_return, &child_return,
339 &win_x_return, &win_y_return, &mask_return);
341 * Since many clients grab the pointer, and X goes an automatic
342 * pointer grab on mouse-down, we often must detect mouse button events
343 * by polling rather than via a button grab.
344 * The while loop (rather than if) is used since it's possible that
345 * multiple buttons have changed state since we last checked.
347 if (mask_return != mouse_mask_state)
349 while (spi_dec_button_update_and_emit (controller, mask_return));
352 if (poll_count++ == poll_count_modulus) {
354 e.type = "mouse:abs";
355 e.source = BONOBO_OBJREF (controller->registry->desktop);
358 spi_init_any_nil (&e.any_data);
359 CORBA_exception_init (&ev);
360 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
364 if (*x != last_mouse_pos->x || *y != last_mouse_pos->y)
366 e.type = "mouse:rel";
367 e.source = BONOBO_OBJREF (controller->registry->desktop);
368 e.detail1 = *x - last_mouse_pos->x;
369 e.detail2 = *y - last_mouse_pos->y;
370 spi_init_any_nil (&e.any_data);
371 CORBA_exception_init (&ev);
372 last_mouse_pos->x = *x;
373 last_mouse_pos->y = *y;
374 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
388 spi_dec_emit_modifier_event (SpiDEController *controller, guint prev_mask,
391 Accessibility_Event e;
392 CORBA_Environment ev;
395 fprintf (stderr, "MODIFIER CHANGE EVENT! %x to %x\n",
396 prev_mask, current_mask);
398 e.type = "keyboard:modifiers";
399 e.source = BONOBO_OBJREF (controller->registry->desktop);
400 e.detail1 = prev_mask & key_modifier_mask;
401 e.detail2 = current_mask & key_modifier_mask;
402 spi_init_any_nil (&e.any_data);
403 CORBA_exception_init (&ev);
404 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
410 spi_dec_poll_mouse_moved (gpointer data)
412 SpiRegistry *registry = SPI_REGISTRY (data);
413 SpiDEController *controller = registry->de_controller;
418 mask_return = spi_dec_mouse_check (controller, &x, &y, &moved);
420 if ((mask_return & key_modifier_mask) !=
421 (mouse_mask_state & key_modifier_mask))
423 spi_dec_emit_modifier_event (controller, mouse_mask_state, mask_return);
424 mouse_mask_state = mask_return;
431 spi_dec_poll_mouse_idle (gpointer data)
433 if (! spi_dec_poll_mouse_moved (data))
437 g_timeout_add (20, spi_dec_poll_mouse_moving, data);
443 spi_dec_poll_mouse_moving (gpointer data)
445 if (spi_dec_poll_mouse_moved (data))
449 g_timeout_add (100, spi_dec_poll_mouse_idle, data);
455 spi_dec_ungrab_mouse (gpointer data)
457 Display *display = spi_get_display ();
460 XUngrabButton (spi_get_display (), AnyButton, AnyModifier,
461 XDefaultRootWindow (spi_get_display ()));
467 spi_dec_init_mouse_listener (SpiRegistry *registry)
469 Display *display = spi_get_display ();
470 g_timeout_add (100, spi_dec_poll_mouse_idle, registry);
474 XGrabButton (display, AnyButton, AnyModifier,
475 gdk_x11_get_default_root_xwindow (),
476 True, ButtonPressMask | ButtonReleaseMask,
477 GrabModeSync, GrabModeAsync, None, None);
478 XSync (display, False);
480 fprintf (stderr, "mouse buttons grabbed\n");
485 static DEControllerKeyListener *
486 spi_dec_key_listener_new (CORBA_Object l,
487 const Accessibility_KeySet *keys,
488 const Accessibility_ControllerEventMask mask,
489 const Accessibility_EventTypeSeq *typeseq,
490 const Accessibility_EventListenerMode *mode,
491 CORBA_Environment *ev)
493 DEControllerKeyListener *key_listener = g_new0 (DEControllerKeyListener, 1);
494 key_listener->listener.object = bonobo_object_dup_ref (l, ev);
495 key_listener->listener.type = SPI_DEVICE_TYPE_KBD;
496 key_listener->keys = ORBit_copy_value (keys, TC_Accessibility_KeySet);
497 key_listener->mask = mask;
498 key_listener->listener.typeseq = ORBit_copy_value (typeseq, TC_Accessibility_EventTypeSeq);
500 key_listener->mode = ORBit_copy_value (mode, TC_Accessibility_EventListenerMode);
502 key_listener->mode = NULL;
505 g_print ("new listener, with mask %x, is_global %d, keys %p (%d)\n",
506 (unsigned int) key_listener->mask,
507 (int) (mode ? mode->global : 0),
508 (void *) key_listener->keys,
509 (int) (key_listener->keys ? key_listener->keys->_length : 0));
515 static DEControllerListener *
516 spi_dec_listener_new (CORBA_Object l,
517 const Accessibility_EventTypeSeq *typeseq,
518 CORBA_Environment *ev)
520 DEControllerListener *listener = g_new0 (DEControllerListener, 1);
521 listener->object = bonobo_object_dup_ref (l, ev);
522 listener->type = SPI_DEVICE_TYPE_MOUSE;
523 listener->typeseq = ORBit_copy_value (typeseq, TC_Accessibility_EventTypeSeq);
527 static DEControllerListener *
528 spi_listener_clone (DEControllerListener *listener, CORBA_Environment *ev)
530 DEControllerListener *clone = g_new0 (DEControllerListener, 1);
532 CORBA_Object_duplicate (listener->object, ev);
533 clone->type = listener->type;
534 clone->typeseq = ORBit_copy_value (listener->typeseq, TC_Accessibility_EventTypeSeq);
538 static DEControllerKeyListener *
539 spi_key_listener_clone (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
541 DEControllerKeyListener *clone = g_new0 (DEControllerKeyListener, 1);
542 clone->listener.object =
543 CORBA_Object_duplicate (key_listener->listener.object, ev);
544 clone->listener.type = SPI_DEVICE_TYPE_KBD;
545 clone->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
546 clone->mask = key_listener->mask;
547 clone->listener.typeseq = ORBit_copy_value (key_listener->listener.typeseq, TC_Accessibility_EventTypeSeq);
548 if (key_listener->mode)
549 clone->mode = ORBit_copy_value (key_listener->mode, TC_Accessibility_EventListenerMode);
556 spi_key_listener_data_free (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
558 CORBA_free (key_listener->listener.typeseq);
559 CORBA_free (key_listener->keys);
560 g_free (key_listener);
564 spi_key_listener_clone_free (DEControllerKeyListener *clone, CORBA_Environment *ev)
566 CORBA_Object_release (clone->listener.object, ev);
567 spi_key_listener_data_free (clone, ev);
571 spi_listener_clone_free (DEControllerListener *clone, CORBA_Environment *ev)
573 CORBA_Object_release (clone->object, ev);
574 CORBA_free (clone->typeseq);
579 spi_dec_listener_free (DEControllerListener *listener,
580 CORBA_Environment *ev)
582 bonobo_object_release_unref (listener->object, ev);
583 if (listener->type == SPI_DEVICE_TYPE_KBD)
584 spi_key_listener_data_free ((DEControllerKeyListener *) listener, ev);
588 _register_keygrab (SpiDEController *controller,
589 DEControllerGrabMask *grab_mask)
593 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
594 spi_grab_mask_compare_values);
597 DEControllerGrabMask *cur_mask = l->data;
599 cur_mask->ref_count++;
600 if (cur_mask->pending_remove)
602 cur_mask->pending_remove = FALSE;
607 controller->keygrabs_list =
608 g_list_prepend (controller->keygrabs_list,
609 spi_grab_mask_clone (grab_mask));
614 _deregister_keygrab (SpiDEController *controller,
615 DEControllerGrabMask *grab_mask)
619 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
620 spi_grab_mask_compare_values);
624 DEControllerGrabMask *cur_mask = l->data;
626 cur_mask->ref_count--;
627 if (cur_mask->ref_count <= 0)
629 cur_mask->pending_remove = TRUE;
634 DBG (1, g_warning ("De-registering non-existant grab"));
639 handle_keygrab (SpiDEController *controller,
640 DEControllerKeyListener *key_listener,
641 void (*process_cb) (SpiDEController *controller,
642 DEControllerGrabMask *grab_mask))
644 DEControllerGrabMask grab_mask = { 0 };
646 grab_mask.mod_mask = key_listener->mask;
647 if (key_listener->keys->_length == 0) /* special case means AnyKey/AllKeys */
649 grab_mask.key_val = AnyKey;
651 fprintf (stderr, "AnyKey grab!");
653 process_cb (controller, &grab_mask);
659 for (i = 0; i < key_listener->keys->_length; ++i)
661 Accessibility_KeyDefinition keydef = key_listener->keys->_buffer[i];
662 long int key_val = keydef.keysym;
663 /* X Grabs require keycodes, not keysyms */
664 if (keydef.keystring && keydef.keystring[0])
666 key_val = XStringToKeysym(keydef.keystring);
670 key_val = XKeysymToKeycode (spi_get_display (), (KeySym) key_val);
674 key_val = keydef.keycode;
676 grab_mask.key_val = key_val;
677 process_cb (controller, &grab_mask);
683 spi_controller_register_global_keygrabs (SpiDEController *controller,
684 DEControllerKeyListener *key_listener)
686 handle_keygrab (controller, key_listener, _register_keygrab);
687 return spi_controller_update_key_grabs (controller, NULL);
691 spi_controller_deregister_global_keygrabs (SpiDEController *controller,
692 DEControllerKeyListener *key_listener)
694 handle_keygrab (controller, key_listener, _deregister_keygrab);
695 spi_controller_update_key_grabs (controller, NULL);
699 spi_controller_register_device_listener (SpiDEController *controller,
700 DEControllerListener *listener,
701 CORBA_Environment *ev)
703 DEControllerKeyListener *key_listener;
705 switch (listener->type) {
706 case SPI_DEVICE_TYPE_KBD:
707 key_listener = (DEControllerKeyListener *) listener;
709 controller->key_listeners = g_list_prepend (controller->key_listeners,
711 if (key_listener->mode->global)
713 return spi_controller_register_global_keygrabs (controller, key_listener);
718 case SPI_DEVICE_TYPE_MOUSE:
719 controller->mouse_listeners = g_list_prepend (controller->mouse_listeners, listener);
722 DBG (1, g_warning ("listener registration for unknown device type.\n"));
729 spi_controller_notify_mouselisteners (SpiDEController *controller,
730 const Accessibility_DeviceEvent *event,
731 CORBA_Environment *ev)
734 GSList *notify = NULL, *l2;
735 GList **listeners = &controller->mouse_listeners;
736 gboolean is_consumed;
743 for (l = *listeners; l; l = l->next)
745 DEControllerListener *listener = l->data;
747 if (spi_eventtype_seq_contains_event (listener->typeseq, event))
749 Accessibility_DeviceEventListener ls = listener->object;
751 if (ls != CORBA_OBJECT_NIL)
753 /* we clone (don't dup) the listener, to avoid refcount inc. */
754 notify = g_slist_prepend (notify,
755 spi_listener_clone (listener, ev));
760 #ifdef SPI_KEYEVENT_DEBUG
763 g_print ("no match for event\n");
768 for (l2 = notify; l2 && !is_consumed; l2 = l2->next)
770 DEControllerListener *listener = l2->data;
771 Accessibility_DeviceEventListener ls = listener->object;
773 CORBA_exception_init (ev);
774 is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, event, ev);
778 DBG (2, g_warning ("error notifying listener, removing it\n"));
779 spi_deregister_controller_device_listener (controller, listener,
781 CORBA_exception_free (ev);
784 spi_listener_clone_free ((DEControllerListener *) l2->data, ev);
787 for (; l2; l2 = l2->next)
789 DEControllerListener *listener = l2->data;
790 spi_listener_clone_free (listener, ev);
791 /* clone doesn't have its own ref, so don't use spi_device_listener_free */
794 g_slist_free (notify);
797 if (is_consumed) g_message ("consumed\n");
803 spi_device_event_controller_forward_mouse_event (SpiDEController *controller,
806 Accessibility_Event e;
807 Accessibility_DeviceEvent mouse_e;
808 CORBA_Environment ev;
809 gchar event_name[24];
810 gboolean is_consumed = FALSE;
811 gboolean xkb_mod_unlatch_occurred;
812 XButtonEvent *xbutton_event = (XButtonEvent *) xevent;
814 int button = xbutton_event->button;
816 unsigned int mouse_button_state = xbutton_event->state;
821 mouse_button_state |= Button1Mask;
824 mouse_button_state |= Button2Mask;
827 mouse_button_state |= Button3Mask;
830 mouse_button_state |= Button4Mask;
833 mouse_button_state |= Button5Mask;
837 last_mouse_pos->x = ((XButtonEvent *) xevent)->x_root;
838 last_mouse_pos->y = ((XButtonEvent *) xevent)->y_root;
841 fprintf (stderr, "mouse button %d %s (%x)\n",
842 xbutton_event->button,
843 (xevent->type == ButtonPress) ? "Press" : "Release",
846 snprintf (event_name, 22, "mouse:button:%d%c", button,
847 (xevent->type == ButtonPress) ? 'p' : 'r');
849 /* TODO: distinguish between physical and logical buttons */
850 mouse_e.type = (xevent->type == ButtonPress) ?
851 Accessibility_BUTTON_PRESSED_EVENT :
852 Accessibility_BUTTON_RELEASED_EVENT;
854 mouse_e.hw_code = button;
855 mouse_e.modifiers = (CORBA_unsigned_short) xbutton_event->state;
856 mouse_e.timestamp = (CORBA_unsigned_long) xbutton_event->time;
857 mouse_e.event_string = "";
858 mouse_e.is_text = CORBA_FALSE;
859 if ((mouse_button_state & mouse_button_mask) !=
860 (mouse_mask_state & mouse_button_mask))
862 if ((mouse_mask_state & key_modifier_mask) !=
863 (mouse_button_state & key_modifier_mask))
864 spi_dec_emit_modifier_event (controller,
865 mouse_mask_state, mouse_button_state);
866 mouse_mask_state = mouse_button_state;
868 spi_controller_notify_mouselisteners (controller, &mouse_e, &ev);
869 e.type = CORBA_string_dup (event_name);
870 e.source = BONOBO_OBJREF (controller->registry->desktop);
871 e.detail1 = last_mouse_pos->x;
872 e.detail2 = last_mouse_pos->y;
873 spi_init_any_nil (&e.any_data);
874 CORBA_exception_init (&ev);
876 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
881 xkb_mod_unlatch_occurred = (xevent->type == ButtonPress ||
882 xevent->type == ButtonRelease);
884 /* if client wants to consume this event, and XKB latch state was
885 * unset by this button event, we reset it
887 if (is_consumed && (xkb_mod_unlatch_occurred))
888 spi_dec_set_unlatch_pending (controller, mouse_mask_state);
890 XAllowEvents (spi_get_display (),
891 (is_consumed) ? SyncPointer : ReplayPointer,
895 static GdkFilterReturn
896 global_filter_fn (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
898 XEvent *xevent = gdk_xevent;
899 SpiDEController *controller;
900 DEControllerPrivateData *priv;
901 Display *display = spi_get_display ();
902 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
903 priv = (DEControllerPrivateData *)
904 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
906 if (xevent->type == KeyPress || xevent->type == KeyRelease)
908 spi_device_event_controller_forward_key_event (controller, xevent);
909 return GDK_FILTER_CONTINUE;
911 if (xevent->type == ButtonPress || xevent->type == ButtonRelease)
913 spi_device_event_controller_forward_mouse_event (controller, xevent);
915 if (xevent->type == priv->xkb_base_event_code)
917 XkbAnyEvent * xkb_ev = (XkbAnyEvent *) xevent;
918 /* ugly but probably necessary...*/
919 XSynchronize (spi_get_display (), TRUE);
921 if (xkb_ev->xkb_type == XkbStateNotify)
923 XkbStateNotifyEvent *xkb_snev =
924 (XkbStateNotifyEvent *) xkb_ev;
925 /* check the mouse, to catch mouse events grabbed by
926 * another client; in case we should revert this XKB delatch
928 if (!priv->pending_xkb_mod_relatch_mask)
932 spi_dec_mouse_check (controller, &x, &y, &moved);
934 /* we check again, since the previous call may have
936 if (priv->pending_xkb_mod_relatch_mask)
938 unsigned int feedback_mask;
940 fprintf (stderr, "relatching %x\n",
941 priv->pending_xkb_mod_relatch_mask);
943 /* temporarily turn off the latch bell, if it's on */
944 XkbGetControls (display,
945 XkbAccessXFeedbackMask,
947 feedback_mask = priv->xkb_desc->ctrls->ax_options;
948 if (feedback_mask & XkbAX_StickyKeysFBMask)
950 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
952 priv->xkb_desc->ctrls->ax_options
953 &= ~(XkbAX_StickyKeysFBMask);
954 XkbChangeControls (display, priv->xkb_desc, &changes);
956 XkbLatchModifiers (display,
958 priv->pending_xkb_mod_relatch_mask,
959 priv->pending_xkb_mod_relatch_mask);
960 if (feedback_mask & XkbAX_StickyKeysFBMask)
962 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
964 priv->xkb_desc->ctrls->ax_options = feedback_mask;
965 XkbChangeControls (display, priv->xkb_desc, &changes);
968 fprintf (stderr, "relatched %x\n",
969 priv->pending_xkb_mod_relatch_mask);
971 priv->pending_xkb_mod_relatch_mask = 0;
975 priv->xkb_latch_mask = xkb_snev->latched_mods;
979 DBG (2, g_warning ("XKB event %d\n", xkb_ev->xkb_type));
980 XSynchronize (spi_get_display (), FALSE);
983 return GDK_FILTER_CONTINUE;
987 _spi_controller_device_error_handler (Display *display, XErrorEvent *error)
989 if (error->error_code == BadAccess)
991 g_message ("Could not complete key grab: grab already in use.\n");
992 spi_error_code = BadAccess;
997 return (*x_default_error_handler) (display, error);
1002 spi_controller_register_with_devices (SpiDEController *controller)
1004 DEControllerPrivateData *priv = (DEControllerPrivateData *)
1005 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
1006 /* FIXME: should check for extension first! */
1007 XTestGrabControl (spi_get_display (), True);
1008 priv->xkb_desc = XkbGetMap (spi_get_display (), 0, XkbUseCoreKbd);
1010 /* calls to device-specific implementations and routines go here */
1011 /* register with: keyboard hardware code handler */
1012 /* register with: (translated) keystroke handler */
1014 priv->have_xkb = XkbQueryExtension (spi_get_display (),
1015 &priv->xkb_major_extension_opcode,
1016 &priv->xkb_base_event_code,
1017 &priv->xkb_base_error_code, NULL, NULL);
1020 XkbSelectEvents (spi_get_display (),
1022 XkbStateNotifyMask, XkbStateNotifyMask);
1024 gdk_window_add_filter (NULL, global_filter_fn, controller);
1026 gdk_window_set_events (gdk_get_default_root_window (),
1027 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
1029 x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
1033 spi_key_set_contains_key (Accessibility_KeySet *key_set,
1034 const Accessibility_DeviceEvent *key_event)
1041 g_print ("null key set!");
1045 len = key_set->_length;
1047 if (len == 0) /* special case, means "all keys/any key" */
1049 g_print ("anykey\n");
1053 for (i = 0; i < len; ++i)
1055 #ifdef SPI_KEYEVENT_DEBUG
1056 g_print ("key_set[%d] = %d; key_event %d, code %d, string %s\n",
1057 i, (int) key_set->_buffer[i].keycode,
1058 (int) key_event->id, (int) key_event->hw_code,
1059 key_event->event_string);
1061 if (key_set->_buffer[i].keysym == (CORBA_long) key_event->id)
1065 if (key_set->_buffer[i].keycode == (CORBA_long) key_event->hw_code)
1069 if (key_event->event_string && key_event->event_string[0] &&
1070 !strcmp (key_set->_buffer[i].keystring, key_event->event_string))
1080 spi_eventtype_seq_contains_event (Accessibility_EventTypeSeq *type_seq,
1081 const Accessibility_DeviceEvent *event)
1089 g_print ("null type seq!");
1093 len = type_seq->_length;
1095 if (len == 0) /* special case, means "all events/any event" */
1100 for (i = 0; i < len; ++i)
1103 g_print ("type_seq[%d] = %d; event type = %d\n", i,
1104 (int) type_seq->_buffer[i], (int) event->type);
1106 if (type_seq->_buffer[i] == (CORBA_long) event->type)
1116 spi_key_event_matches_listener (const Accessibility_DeviceEvent *key_event,
1117 DEControllerKeyListener *listener,
1118 CORBA_boolean is_system_global)
1120 if ((key_event->modifiers == (CORBA_unsigned_short) (listener->mask & 0xFFFF)) &&
1121 spi_key_set_contains_key (listener->keys, key_event) &&
1122 spi_eventtype_seq_contains_event (listener->listener.typeseq, key_event) &&
1123 (is_system_global == listener->mode->global))
1134 spi_controller_notify_keylisteners (SpiDEController *controller,
1135 const Accessibility_DeviceEvent *key_event,
1136 CORBA_boolean is_system_global,
1137 CORBA_Environment *ev)
1140 GSList *notify = NULL, *l2;
1141 GList **key_listeners = &controller->key_listeners;
1142 gboolean is_consumed;
1149 for (l = *key_listeners; l; l = l->next)
1151 DEControllerKeyListener *key_listener = l->data;
1153 if (spi_key_event_matches_listener (key_event, key_listener, is_system_global))
1155 Accessibility_DeviceEventListener ls = key_listener->listener.object;
1157 if (ls != CORBA_OBJECT_NIL)
1159 /* we clone (don't dup) the listener, to avoid refcount inc. */
1160 notify = g_slist_prepend (notify,
1161 spi_key_listener_clone (key_listener, ev));
1166 #ifdef SPI_KEYEVENT_DEBUG
1169 g_print ("no match for event\n");
1173 is_consumed = FALSE;
1174 for (l2 = notify; l2 && !is_consumed; l2 = l2->next)
1176 DEControllerKeyListener *key_listener = l2->data;
1177 Accessibility_DeviceEventListener ls = key_listener->listener.object;
1179 is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, key_event, ev);
1183 is_consumed = FALSE;
1184 spi_deregister_controller_key_listener (controller, key_listener,
1186 CORBA_exception_free (ev);
1189 spi_key_listener_clone_free (key_listener, ev);
1192 for (; l2; l2 = l2->next)
1194 DEControllerKeyListener *key_listener = l2->data;
1195 spi_key_listener_clone_free (key_listener, ev);
1196 /* clone doesn't have its own ref, so don't use spi_dec_listener_free */
1199 g_slist_free (notify);
1202 if (is_consumed) g_message ("consumed\n");
1208 spi_clear_error_state ()
1210 gboolean retval = spi_error_code != 0;
1215 static Accessibility_DeviceEvent
1216 spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
1218 Accessibility_DeviceEvent key_event;
1220 const int cbuf_bytes = 20;
1221 char cbuf [cbuf_bytes];
1223 keysym = XLookupKeysym (x_key_event, 0);
1224 key_event.id = (CORBA_long)(keysym);
1225 key_event.hw_code = (CORBA_short) x_key_event->keycode;
1226 if (((XEvent *) x_key_event)->type == KeyPress)
1228 key_event.type = Accessibility_KEY_PRESSED;
1232 key_event.type = Accessibility_KEY_RELEASED;
1234 key_event.modifiers = (CORBA_unsigned_short)(x_key_event->state);
1235 key_event.is_text = CORBA_FALSE;
1239 key_event.event_string = CORBA_string_dup ("space");
1242 #ifdef SPI_KEYEVENT_DEBUG
1243 fprintf(stderr, "Tab\n");
1245 key_event.event_string = CORBA_string_dup ("Tab");
1248 key_event.event_string = CORBA_string_dup ("Backspace");
1251 key_event.event_string = CORBA_string_dup ("Return");
1254 key_event.event_string = CORBA_string_dup ("Home");
1257 key_event.event_string = CORBA_string_dup ("Page_Down");
1260 key_event.event_string = CORBA_string_dup ("Page_Up");
1263 key_event.event_string = CORBA_string_dup ("F1");
1266 key_event.event_string = CORBA_string_dup ("F2");
1269 key_event.event_string = CORBA_string_dup ("F3");
1272 key_event.event_string = CORBA_string_dup ("F4");
1275 key_event.event_string = CORBA_string_dup ("F5");
1278 key_event.event_string = CORBA_string_dup ("F6");
1281 key_event.event_string = CORBA_string_dup ("F7");
1284 key_event.event_string = CORBA_string_dup ("F8");
1287 key_event.event_string = CORBA_string_dup ("F9");
1290 key_event.event_string = CORBA_string_dup ("F10");
1293 key_event.event_string = CORBA_string_dup ("F11");
1296 key_event.event_string = CORBA_string_dup ("F12");
1299 key_event.event_string = CORBA_string_dup ("End");
1302 key_event.event_string = CORBA_string_dup ("Escape");
1305 key_event.event_string = CORBA_string_dup ("Up");
1308 key_event.event_string = CORBA_string_dup ("Down");
1311 key_event.event_string = CORBA_string_dup ("Left");
1314 key_event.event_string = CORBA_string_dup ("Right");
1317 if (XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL) > 0)
1319 key_event.event_string = CORBA_string_dup (cbuf);
1320 if (isgraph (keysym))
1322 key_event.is_text = CORBA_TRUE; /* FIXME: incorrect for some composed chars? */
1327 key_event.event_string = CORBA_string_dup ("");
1331 key_event.timestamp = (CORBA_unsigned_long) x_key_event->time;
1332 #ifdef SPI_KEYEVENT_DEBUG
1334 "Key %lu pressed (%c), modifiers %d\n",
1335 (unsigned long) keysym,
1336 keysym ? (int) keysym : '*',
1337 (int) x_key_event->state);
1340 fprintf (stderr, "%s%c",
1341 (x_key_event->state & Mod1Mask)?"Alt-":"",
1342 ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
1343 g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
1344 #endif /* SPI_DEBUG */
1349 spi_controller_update_key_grabs (SpiDEController *controller,
1350 Accessibility_DeviceEvent *recv)
1353 gboolean update_failed = FALSE;
1355 g_return_val_if_fail (controller != NULL, FALSE);
1358 * masks known to work with default RH 7.1+:
1359 * 0 (no mods), LockMask, Mod1Mask, Mod2Mask, ShiftMask,
1360 * ShiftMask|LockMask, Mod1Mask|LockMask, Mod2Mask|LockMask,
1361 * ShiftMask|Mod1Mask, ShiftMask|Mod2Mask, Mod1Mask|Mod2Mask,
1362 * ShiftMask|LockMask|Mod1Mask, ShiftMask|LockMask|Mod2Mask,
1364 * ControlMask grabs are broken, must be in use already
1366 for (l = controller->keygrabs_list; l; l = next)
1369 gboolean re_issue_grab;
1370 DEControllerGrabMask *grab_mask = l->data;
1374 re_issue_grab = recv &&
1375 /* (recv->type == Accessibility_KEY_RELEASED) && - (?) */
1376 (recv->modifiers & grab_mask->mod_mask) &&
1377 (grab_mask->key_val == keycode_for_keysym (recv->id));
1380 fprintf (stderr, "mask=%lx %lx (%c%c) %s\n",
1381 (long int) grab_mask->key_val,
1382 (long int) grab_mask->mod_mask,
1383 grab_mask->pending_add ? '+' : '.',
1384 grab_mask->pending_remove ? '-' : '.',
1385 re_issue_grab ? "re-issue": "");
1390 if (grab_mask->pending_add && grab_mask->pending_remove)
1394 else if (grab_mask->pending_remove)
1397 fprintf (stderr, "ungrabbing, mask=%x\n", grab_mask->mod_mask);
1399 XUngrabKey (spi_get_display (),
1401 grab_mask->mod_mask,
1402 gdk_x11_get_default_root_xwindow ());
1406 else if (grab_mask->pending_add || re_issue_grab)
1410 fprintf (stderr, "grab %d with mask %x\n", grab_mask->key_val, grab_mask->mod_mask);
1412 XGrabKey (spi_get_display (),
1414 grab_mask->mod_mask,
1415 gdk_x11_get_default_root_xwindow (),
1419 XSync (spi_get_display (), False);
1420 update_failed = spi_clear_error_state ();
1421 if (update_failed) {
1422 while (grab_mask->ref_count > 0) --grab_mask->ref_count;
1427 grab_mask->pending_add = FALSE;
1428 grab_mask->pending_remove = FALSE;
1432 g_assert (grab_mask->ref_count <= 0);
1434 controller->keygrabs_list = g_list_delete_link (
1435 controller->keygrabs_list, l);
1437 spi_grab_mask_free (grab_mask);
1442 return ! update_failed;
1446 * Implemented GObject::finalize
1449 spi_device_event_controller_object_finalize (GObject *object)
1451 SpiDEController *controller;
1452 DEControllerPrivateData *private;
1453 controller = SPI_DEVICE_EVENT_CONTROLLER (object);
1456 fprintf(stderr, "spi_device_event_controller_object_finalize called\n");
1458 /* disconnect any special listeners, get rid of outstanding keygrabs */
1459 XUngrabKey (spi_get_display (), AnyKey, AnyModifier, DefaultRootWindow (spi_get_display ()));
1461 private = g_object_get_data (G_OBJECT (controller), "spi-dec-private");
1462 if (private->xkb_desc)
1463 XkbFreeKeyboard (private->xkb_desc, 0, True);
1465 spi_device_event_controller_parent_class->finalize (object);
1469 * CORBA Accessibility::DEController::registerKeystrokeListener
1470 * method implementation
1472 static CORBA_boolean
1473 impl_register_keystroke_listener (PortableServer_Servant servant,
1474 const Accessibility_DeviceEventListener l,
1475 const Accessibility_KeySet *keys,
1476 const Accessibility_ControllerEventMask mask,
1477 const Accessibility_EventTypeSeq *type,
1478 const Accessibility_EventListenerMode *mode,
1479 CORBA_Environment *ev)
1481 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1482 bonobo_object_from_servant (servant));
1483 DEControllerKeyListener *dec_listener;
1485 fprintf (stderr, "registering keystroke listener %p with maskVal %lu\n",
1486 (void *) l, (unsigned long) mask);
1488 dec_listener = spi_dec_key_listener_new (l, keys, mask, type, mode, ev);
1489 return spi_controller_register_device_listener (
1490 controller, (DEControllerListener *) dec_listener, ev);
1495 * CORBA Accessibility::DEController::registerDeviceEventListener
1496 * method implementation
1498 static CORBA_boolean
1499 impl_register_device_listener (PortableServer_Servant servant,
1500 const Accessibility_DeviceEventListener l,
1501 const Accessibility_EventTypeSeq *event_types,
1502 CORBA_Environment *ev)
1504 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1505 bonobo_object_from_servant (servant));
1506 DEControllerListener *dec_listener;
1508 dec_listener = spi_dec_listener_new (l, event_types, ev);
1509 return spi_controller_register_device_listener (
1510 controller, (DEControllerListener *) dec_listener, ev);
1514 CORBA_Environment *ev;
1515 DEControllerListener *listener;
1516 } RemoveListenerClosure;
1518 static SpiReEntrantContinue
1519 remove_listener_cb (GList * const *list,
1522 DEControllerListener *listener = (*list)->data;
1523 RemoveListenerClosure *ctx = user_data;
1525 if (CORBA_Object_is_equivalent (ctx->listener->object,
1526 listener->object, ctx->ev))
1528 spi_re_entrant_list_delete_link (list);
1529 spi_dec_listener_free (listener, ctx->ev);
1532 return SPI_RE_ENTRANT_CONTINUE;
1535 static SpiReEntrantContinue
1536 copy_key_listener_cb (GList * const *list,
1539 DEControllerKeyListener *key_listener = (*list)->data;
1540 RemoveListenerClosure *ctx = user_data;
1542 if (CORBA_Object_is_equivalent (ctx->listener->object,
1543 key_listener->listener.object, ctx->ev))
1545 /* TODO: FIXME aggregate keys in case the listener is registered twice */
1546 DEControllerKeyListener *ctx_key_listener =
1547 (DEControllerKeyListener *) ctx->listener;
1548 CORBA_free (ctx_key_listener->keys);
1549 ctx_key_listener->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
1552 return SPI_RE_ENTRANT_CONTINUE;
1556 spi_deregister_controller_device_listener (SpiDEController *controller,
1557 DEControllerListener *listener,
1558 CORBA_Environment *ev)
1560 RemoveListenerClosure ctx;
1563 ctx.listener = listener;
1565 spi_re_entrant_list_foreach (&controller->mouse_listeners,
1566 remove_listener_cb, &ctx);
1570 spi_deregister_controller_key_listener (SpiDEController *controller,
1571 DEControllerKeyListener *key_listener,
1572 CORBA_Environment *ev)
1574 RemoveListenerClosure ctx;
1577 ctx.listener = (DEControllerListener *) key_listener;
1579 /* special case, copy keyset from existing controller list entry */
1580 if (key_listener->keys->_length == 0)
1582 spi_re_entrant_list_foreach (&controller->key_listeners,
1583 copy_key_listener_cb, &ctx);
1586 spi_controller_deregister_global_keygrabs (controller, key_listener);
1588 spi_re_entrant_list_foreach (&controller->key_listeners,
1589 remove_listener_cb, &ctx);
1594 * CORBA Accessibility::DEController::deregisterKeystrokeListener
1595 * method implementation
1598 impl_deregister_keystroke_listener (PortableServer_Servant servant,
1599 const Accessibility_DeviceEventListener l,
1600 const Accessibility_KeySet *keys,
1601 const Accessibility_ControllerEventMask mask,
1602 const Accessibility_EventTypeSeq *type,
1603 CORBA_Environment *ev)
1605 DEControllerKeyListener *key_listener;
1606 SpiDEController *controller;
1608 controller = SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1610 key_listener = spi_dec_key_listener_new (l, keys, mask, type, NULL, ev);
1612 #ifdef SPI_DEREGISTER_DEBUG
1613 fprintf (stderr, "deregistering keystroke listener %p with maskVal %lu\n",
1614 (void *) l, (unsigned long) mask->value);
1617 spi_deregister_controller_key_listener (controller, key_listener, ev);
1619 spi_dec_listener_free ((DEControllerListener *) key_listener, ev);
1623 * CORBA Accessibility::DEController::deregisterDeviceEventListener
1624 * method implementation
1627 impl_deregister_device_listener (PortableServer_Servant servant,
1628 const Accessibility_DeviceEventListener l,
1629 const Accessibility_EventTypeSeq *event_types,
1630 CORBA_Environment *ev)
1632 SpiDEController *controller;
1633 DEControllerListener *listener =
1634 spi_dec_listener_new (l, event_types, ev);
1636 controller = SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1638 spi_deregister_controller_device_listener (controller, listener, ev);
1640 spi_dec_listener_free (listener, ev);
1643 static unsigned int dec_xkb_get_slowkeys_delay (SpiDEController *controller)
1645 unsigned int retval = 0;
1646 DEControllerPrivateData *priv = (DEControllerPrivateData *)
1647 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
1649 #ifdef XKB_HAS_GET_SLOW_KEYS_DELAY
1650 retval = XkbGetSlowKeysDelay (spi_get_display (),
1651 XkbUseCoreKbd, &bounce_delay);
1653 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
1655 Status s = XkbGetControls (spi_get_display (),
1656 XkbAllControlsMask, priv->xkb_desc);
1659 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbSlowKeysMask)
1660 retval = priv->xkb_desc->ctrls->slow_keys_delay;
1665 #ifdef SPI_XKB_DEBUG
1666 fprintf (stderr, "SlowKeys delay: %d\n", (int) retval);
1671 static unsigned int dec_xkb_get_bouncekeys_delay (SpiDEController *controller)
1673 unsigned int retval = 0;
1674 DEControllerPrivateData *priv = (DEControllerPrivateData *)
1675 g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
1677 #ifdef XKB_HAS_GET_BOUNCE_KEYS_DELAY
1678 retval = XkbGetBounceKeysDelay (spi_get_display (),
1679 XkbUseCoreKbd, &bounce_delay);
1681 if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
1683 Status s = XkbGetControls (spi_get_display (),
1684 XkbAllControlsMask, priv->xkb_desc);
1687 if (priv->xkb_desc->ctrls->enabled_ctrls & XkbBounceKeysMask)
1688 retval = priv->xkb_desc->ctrls->debounce_delay;
1693 #ifdef SPI_XKB_DEBUG
1694 fprintf (stderr, "BounceKeys delay: %d\n", (int) retval);
1700 dec_synth_keycode_press (SpiDEController *controller,
1701 unsigned int keycode)
1703 unsigned int time = CurrentTime;
1704 unsigned int bounce_delay;
1705 unsigned int elapsed_msec;
1707 DEControllerPrivateData *priv =
1708 (DEControllerPrivateData *) g_object_get_qdata (G_OBJECT (controller),
1709 spi_dec_private_quark);
1710 if (keycode == priv->last_release_keycode)
1712 bounce_delay = dec_xkb_get_bouncekeys_delay (controller);
1715 gettimeofday (&tv, NULL);
1717 (tv.tv_sec - priv->last_release_time.tv_sec) * 1000
1718 + (tv.tv_usec - priv->last_release_time.tv_usec) / 1000;
1719 #ifdef SPI_XKB_DEBUG
1720 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1721 (long) (tv.tv_usec - priv->last_release_time.tv_usec));
1723 #ifdef THIS_IS_BROKEN
1724 if (elapsed_msec < bounce_delay)
1725 time = bounce_delay - elapsed_msec + 1;
1727 time = bounce_delay + 10;
1728 /* fudge for broken XTest */
1730 #ifdef SPI_XKB_DEBUG
1731 fprintf (stderr, "waiting %d ms\n", time);
1735 XTestFakeKeyEvent (spi_get_display (), keycode, True, time);
1736 priv->last_press_keycode = keycode;
1737 XSync (spi_get_display (), False);
1738 gettimeofday (&priv->last_press_time, NULL);
1743 dec_synth_keycode_release (SpiDEController *controller,
1744 unsigned int keycode)
1746 unsigned int time = CurrentTime;
1747 unsigned int slow_delay;
1748 unsigned int elapsed_msec;
1750 DEControllerPrivateData *priv =
1751 (DEControllerPrivateData *) g_object_get_qdata (G_OBJECT (controller),
1752 spi_dec_private_quark);
1753 if (keycode == priv->last_press_keycode)
1755 slow_delay = dec_xkb_get_slowkeys_delay (controller);
1758 gettimeofday (&tv, NULL);
1760 (tv.tv_sec - priv->last_press_time.tv_sec) * 1000
1761 + (tv.tv_usec - priv->last_press_time.tv_usec) / 1000;
1762 #ifdef SPI_XKB_DEBUG
1763 fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1764 (long) (tv.tv_usec - priv->last_press_time.tv_usec));
1766 #ifdef THIS_IS_BROKEN_DUNNO_WHY
1767 if (elapsed_msec < slow_delay)
1768 time = slow_delay - elapsed_msec + 1;
1770 time = slow_delay + 10;
1771 /* our XTest seems broken, we have to add slop as above */
1773 #ifdef SPI_XKB_DEBUG
1774 fprintf (stderr, "waiting %d ms\n", time);
1778 XTestFakeKeyEvent (spi_get_display (), keycode, False, time);
1779 priv->last_release_keycode = keycode;
1780 XSync (spi_get_display (), False);
1781 gettimeofday (&priv->last_release_time, NULL);
1786 * CORBA Accessibility::DEController::registerKeystrokeListener
1787 * method implementation
1790 impl_generate_keyboard_event (PortableServer_Servant servant,
1791 const CORBA_long keycode,
1792 const CORBA_char *keystring,
1793 const Accessibility_KeySynthType synth_type,
1794 CORBA_Environment *ev)
1796 SpiDEController *controller =
1797 SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1798 DEControllerPrivateData *priv;
1799 long key_synth_code;
1800 unsigned int slow_keys_delay;
1801 unsigned int press_time;
1802 unsigned int release_time;
1805 fprintf (stderr, "synthesizing keystroke %ld, type %d\n",
1806 (long) keycode, (int) synth_type);
1808 /* TODO: hide/wrap/remove X dependency */
1811 * TODO: when initializing, query for XTest extension before using,
1812 * and fall back to XSendEvent() if XTest is not available.
1815 /* TODO: implement keystring mode also */
1816 gdk_error_trap_push ();
1817 key_synth_code = keycode;
1821 case Accessibility_KEY_PRESS:
1822 dec_synth_keycode_press (controller, keycode);
1824 case Accessibility_KEY_PRESSRELEASE:
1825 dec_synth_keycode_press (controller, keycode);
1826 case Accessibility_KEY_RELEASE:
1827 dec_synth_keycode_release (controller, keycode);
1829 case Accessibility_KEY_SYM:
1830 #ifdef SPI_XKB_DEBUG
1831 fprintf (stderr, "KeySym synthesis\n");
1833 key_synth_code = keycode_for_keysym (keycode);
1834 dec_synth_keycode_press (controller, key_synth_code);
1835 dec_synth_keycode_release (controller, key_synth_code);
1837 case Accessibility_KEY_STRING:
1838 fprintf (stderr, "Not yet implemented\n");
1841 if (gdk_error_trap_pop ())
1843 DBG (-1, g_warning ("Error emitting keystroke"));
1847 /* Accessibility::DEController::generateMouseEvent */
1849 impl_generate_mouse_event (PortableServer_Servant servant,
1852 const CORBA_char *eventName,
1853 CORBA_Environment *ev)
1856 gboolean error = FALSE;
1857 Display *display = spi_get_display ();
1859 fprintf (stderr, "generating mouse %s event at %ld, %ld\n",
1860 eventName, (long int) x, (long int) y);
1862 switch (eventName[0])
1865 switch (eventName[1])
1867 /* TODO: check number of buttons before parsing */
1882 if (x != -1 && y != -1)
1884 XTestFakeMotionEvent (display, DefaultScreen (display),
1887 XTestFakeButtonEvent (display, button, !(eventName[2] == 'r'), 0);
1888 if (eventName[2] == 'c')
1889 XTestFakeButtonEvent (display, button, FALSE, 1);
1890 else if (eventName[2] == 'd')
1892 XTestFakeButtonEvent (display, button, FALSE, 1);
1893 XTestFakeButtonEvent (display, button, TRUE, 2);
1894 XTestFakeButtonEvent (display, button, FALSE, 3);
1898 case 'r': /* relative motion */
1899 XTestFakeRelativeMotionEvent (display, x, y, 0);
1901 case 'a': /* absolute motion */
1902 XTestFakeMotionEvent (display, DefaultScreen (display),
1908 /* Accessibility::DEController::notifyListenersSync */
1909 static CORBA_boolean
1910 impl_notify_listeners_sync (PortableServer_Servant servant,
1911 const Accessibility_DeviceEvent *event,
1912 CORBA_Environment *ev)
1914 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1915 bonobo_object_from_servant (servant));
1917 g_print ("notifylistening listeners synchronously: controller %p, event id %d\n",
1918 controller, (int) event->id);
1920 return spi_controller_notify_keylisteners (controller, event, CORBA_FALSE, ev) ?
1921 CORBA_TRUE : CORBA_FALSE;
1924 /* Accessibility::DEController::notifyListenersAsync */
1926 impl_notify_listeners_async (PortableServer_Servant servant,
1927 const Accessibility_DeviceEvent *event,
1928 CORBA_Environment *ev)
1930 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1931 bonobo_object_from_servant (servant));
1933 fprintf (stderr, "notifying listeners asynchronously\n");
1935 spi_controller_notify_keylisteners (controller, event, CORBA_FALSE, ev);
1939 spi_device_event_controller_class_init (SpiDEControllerClass *klass)
1941 GObjectClass * object_class = (GObjectClass *) klass;
1942 POA_Accessibility_DeviceEventController__epv *epv = &klass->epv;
1944 spi_device_event_controller_parent_class = g_type_class_peek_parent (klass);
1946 object_class->finalize = spi_device_event_controller_object_finalize;
1948 epv->registerKeystrokeListener = impl_register_keystroke_listener;
1949 epv->deregisterKeystrokeListener = impl_deregister_keystroke_listener;
1950 epv->registerDeviceEventListener = impl_register_device_listener;
1951 epv->deregisterDeviceEventListener = impl_deregister_device_listener;
1952 epv->generateKeyboardEvent = impl_generate_keyboard_event;
1953 epv->generateMouseEvent = impl_generate_mouse_event;
1954 epv->notifyListenersSync = impl_notify_listeners_sync;
1955 epv->notifyListenersAsync = impl_notify_listeners_async;
1957 if (!spi_dec_private_quark)
1958 spi_dec_private_quark = g_quark_from_static_string ("spi-dec-private");
1962 spi_device_event_controller_init (SpiDEController *device_event_controller)
1964 DEControllerPrivateData *private;
1965 device_event_controller->key_listeners = NULL;
1966 device_event_controller->mouse_listeners = NULL;
1967 device_event_controller->keygrabs_list = NULL;
1970 * TODO: fixme, this module makes the foolish assumptions that
1971 * registryd uses the same display as the apps, and that the
1972 * DISPLAY environment variable is set.
1974 gdk_init (NULL, NULL);
1976 private = g_new0 (DEControllerPrivateData, 1);
1977 gettimeofday (&private->last_press_time, NULL);
1978 gettimeofday (&private->last_release_time, NULL);
1979 g_object_set_qdata (G_OBJECT (device_event_controller),
1980 spi_dec_private_quark,
1982 spi_controller_register_with_devices (device_event_controller);
1986 spi_device_event_controller_forward_key_event (SpiDEController *controller,
1987 const XEvent *event)
1989 gboolean is_consumed = FALSE;
1990 CORBA_Environment ev;
1991 Accessibility_DeviceEvent key_event;
1993 g_assert (event->type == KeyPress || event->type == KeyRelease);
1995 CORBA_exception_init (&ev);
1997 key_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1999 spi_controller_update_key_grabs (controller, &key_event);
2001 /* relay to listeners, and decide whether to consume it or not */
2002 is_consumed = spi_controller_notify_keylisteners (
2003 controller, &key_event, CORBA_TRUE, &ev);
2007 XAllowEvents (spi_get_display (), AsyncKeyboard, CurrentTime);
2011 XAllowEvents (spi_get_display (), ReplayKeyboard, CurrentTime);
2016 spi_device_event_controller_new (SpiRegistry *registry)
2018 SpiDEController *retval = g_object_new (
2019 SPI_DEVICE_EVENT_CONTROLLER_TYPE, NULL);
2020 DEControllerPrivateData *private;
2022 retval->registry = SPI_REGISTRY (bonobo_object_ref (
2023 BONOBO_OBJECT (registry)));
2025 spi_dec_init_mouse_listener (registry);
2026 /* TODO: kill mouse listener on finalize */
2030 BONOBO_TYPE_FUNC_FULL (SpiDEController,
2031 Accessibility_DeviceEventController,
2033 spi_device_event_controller);