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 */
29 #undef SPI_KEYEVENT_DEBUG
34 #include <bonobo/bonobo-exception.h>
37 #include <X11/extensions/XTest.h>
39 #include <X11/keysymdef.h>
41 #include <gdk/gdkx.h> /* TODO: hide dependency (wrap in single porting file) */
42 #include <gdk/gdkkeysyms.h>
43 #include <gdk/gdkwindow.h>
45 #include "../libspi/spi-private.h"
46 #include "deviceeventcontroller.h"
48 /* Our parent Gtk object type */
49 #define PARENT_TYPE BONOBO_TYPE_OBJECT
51 /* A pointer to our parent object class */
52 static GObjectClass *spi_device_event_controller_parent_class;
53 static int spi_error_code = 0;
54 static GdkPoint *last_mouse_pos = NULL;
55 static unsigned int mouse_mask_state = 0;
56 static unsigned int mouse_button_mask =
57 Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask;
58 static unsigned int key_modifier_mask =
59 Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask | ShiftMask | LockMask | ControlMask;
61 int (*x_default_error_handler) (Display *display, XErrorEvent *error_event);
65 SPI_DEVICE_TYPE_MOUSE,
66 SPI_DEVICE_TYPE_LAST_DEFINED
67 } SpiDeviceTypeCategory;
71 guint pending_add : 1;
72 guint pending_remove : 1;
74 Accessibility_ControllerEventMask mod_mask;
75 CORBA_unsigned_long key_val; /* KeyCode */
76 } DEControllerGrabMask;
80 SpiDeviceTypeCategory type;
81 } DEControllerListener;
84 DEControllerListener listener;
86 Accessibility_KeySet *keys;
87 Accessibility_ControllerEventMask mask;
88 Accessibility_KeyEventTypeSeq *typeseq;
89 Accessibility_EventListenerMode *mode;
90 } DEControllerKeyListener;
92 static void spi_controller_register_with_devices (SpiDEController *controller);
93 static gboolean spi_controller_update_key_grabs (SpiDEController *controller,
94 Accessibility_DeviceEvent *recv);
95 static gboolean spi_controller_register_device_listener (SpiDEController *controller,
96 DEControllerListener *l,
97 CORBA_Environment *ev);
98 static void spi_device_event_controller_forward_key_event (SpiDEController *controller,
100 static void spi_deregister_controller_key_listener (SpiDEController *controller,
101 DEControllerKeyListener *key_listener,
102 CORBA_Environment *ev);
104 static gboolean spi_clear_error_state (void);
105 static gboolean spi_dec_poll_mouse_moved (gpointer data);
106 static gboolean spi_dec_poll_mouse_moving (gpointer data);
107 static gboolean spi_dec_poll_mouse_idle (gpointer data);
109 #define spi_get_display() GDK_DISPLAY()
111 /* Private methods */
114 keycode_for_keysym (long keysym)
116 return XKeysymToKeycode (spi_get_display (), (KeySym) keysym);
119 static DEControllerGrabMask *
120 spi_grab_mask_clone (DEControllerGrabMask *grab_mask)
122 DEControllerGrabMask *clone = g_new (DEControllerGrabMask, 1);
124 memcpy (clone, grab_mask, sizeof (DEControllerGrabMask));
126 clone->ref_count = 1;
127 clone->pending_add = TRUE;
128 clone->pending_remove = FALSE;
134 spi_grab_mask_free (DEControllerGrabMask *grab_mask)
140 spi_grab_mask_compare_values (gconstpointer p1, gconstpointer p2)
142 DEControllerGrabMask *l1 = (DEControllerGrabMask *) p1;
143 DEControllerGrabMask *l2 = (DEControllerGrabMask *) p2;
151 return ((l1->mod_mask != l2->mod_mask) || (l1->key_val != l2->key_val));
155 static gint poll_count = 0;
158 spi_dec_poll_mouse_moved (gpointer data)
160 SpiRegistry *registry = SPI_REGISTRY (data);
161 CORBA_Environment ev;
162 Accessibility_Event e;
163 Window root_return, child_return;
164 int win_x_return,win_y_return;
166 int poll_count_modulus = 10;
167 unsigned int mask_return;
168 gchar event_name[24];
169 Display *display = spi_get_display ();
172 XQueryPointer(display, DefaultRootWindow (display),
173 &root_return, &child_return,
175 &win_x_return, &win_y_return, &mask_return);
177 if (mask_return != mouse_mask_state) {
178 if ((mask_return & mouse_button_mask) !=
179 (mouse_mask_state & mouse_button_mask)) {
180 int button_number = 0;
181 if (!(mask_return & Button1Mask) &&
182 (mouse_mask_state & Button1Mask)) {
184 } else if (!(mask_return & Button2Mask) &&
185 (mouse_mask_state & Button2Mask)) {
187 } else if (!(mask_return & Button3Mask) &&
188 (mouse_mask_state & Button3Mask)) {
190 } else if (!(mask_return & Button4Mask) &&
191 (mouse_mask_state & Button1Mask)) {
193 } else if (!(mask_return & Button5Mask) &&
194 (mouse_mask_state & Button5Mask)) {
199 fprintf (stderr, "Button %d Released\n",
202 snprintf (event_name, 22, "mouse:button:%dr", button_number);
204 e.source = BONOBO_OBJREF (registry->desktop);
205 e.detail1 = last_mouse_pos->x;
206 e.detail2 = last_mouse_pos->y;
207 CORBA_exception_init (&ev);
208 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
213 if ((mask_return & key_modifier_mask) !=
214 (mouse_mask_state & key_modifier_mask)) {
215 fprintf (stderr, "MODIFIER CHANGE EVENT!\n");
216 e.type = "keyboard:modifiers";
217 e.source = BONOBO_OBJREF (registry->desktop);
218 e.detail1 = mouse_mask_state;
219 e.detail2 = mask_return;
220 CORBA_exception_init (&ev);
221 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
225 mouse_mask_state = mask_return;
227 if (last_mouse_pos == NULL) {
228 last_mouse_pos = g_new0 (GdkPoint, 1);
229 last_mouse_pos->x = 0;
230 last_mouse_pos->y = 0;
232 if (poll_count++ == poll_count_modulus) {
234 e.type = "mouse:abs";
235 e.source = BONOBO_OBJREF (registry->desktop);
238 CORBA_exception_init (&ev);
239 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
243 if (x != last_mouse_pos->x || y != last_mouse_pos->y) {
244 e.type = "mouse:rel";
245 e.source = BONOBO_OBJREF (registry->desktop);
246 e.detail1 = x - last_mouse_pos->x;
247 e.detail2 = y - last_mouse_pos->y;
248 CORBA_exception_init (&ev);
249 last_mouse_pos->x = x;
250 last_mouse_pos->y = y;
251 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
260 spi_dec_poll_mouse_idle (gpointer data)
262 if (! spi_dec_poll_mouse_moved (data))
266 g_timeout_add (20, spi_dec_poll_mouse_moving, data);
272 spi_dec_poll_mouse_moving (gpointer data)
274 if (spi_dec_poll_mouse_moved (data))
278 g_timeout_add (100, spi_dec_poll_mouse_idle, data);
284 spi_dec_ungrab_mouse (gpointer data)
286 Display *display = spi_get_display ();
287 fprintf (stderr, "mouse ungrab : display = %p\n", display);
290 XUngrabButton (spi_get_display (), AnyButton, AnyModifier,
291 XDefaultRootWindow (spi_get_display ()));
292 fprintf (stderr, "mouse grab released\n");
298 spi_dec_init_mouse_listener (SpiRegistry *registry)
300 Display *display = spi_get_display ();
301 g_timeout_add (100, spi_dec_poll_mouse_idle, registry);
305 XGrabButton (display, AnyButton, 0,
306 gdk_x11_get_default_root_xwindow (),
307 True, ButtonPressMask | ButtonReleaseMask,
308 GrabModeSync, GrabModeAsync, None, None);
309 XSync (display, False);
310 fprintf (stderr, "mouse buttons grabbed\n");
314 static DEControllerKeyListener *
315 spi_dec_key_listener_new (CORBA_Object l,
316 const Accessibility_KeySet *keys,
317 const Accessibility_ControllerEventMask mask,
318 const Accessibility_KeyEventTypeSeq *typeseq,
319 const Accessibility_EventListenerMode *mode,
320 CORBA_Environment *ev)
322 DEControllerKeyListener *key_listener = g_new0 (DEControllerKeyListener, 1);
323 key_listener->listener.object = bonobo_object_dup_ref (l, ev);
324 key_listener->listener.type = SPI_DEVICE_TYPE_KBD;
325 key_listener->keys = ORBit_copy_value (keys, TC_Accessibility_KeySet);
326 key_listener->mask = mask;
327 key_listener->typeseq = ORBit_copy_value (typeseq, TC_Accessibility_KeyEventTypeSeq);
329 key_listener->mode = ORBit_copy_value (mode, TC_Accessibility_EventListenerMode);
331 key_listener->mode = NULL;
334 g_print ("new listener, with mask %x, is_global %d, keys %p (%d)\n",
335 (unsigned int) key_listener->mask,
336 (int) (mode ? mode->global : 0),
337 (void *) key_listener->keys,
338 (int) (key_listener->keys ? key_listener->keys->_length : 0));
344 static DEControllerKeyListener *
345 spi_key_listener_clone (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
347 DEControllerKeyListener *clone = g_new0 (DEControllerKeyListener, 1);
348 clone->listener.object =
349 CORBA_Object_duplicate (key_listener->listener.object, ev);
350 clone->listener.type = SPI_DEVICE_TYPE_KBD;
351 clone->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
352 clone->mask = key_listener->mask;
353 clone->typeseq = ORBit_copy_value (key_listener->typeseq, TC_Accessibility_KeyEventTypeSeq);
354 if (key_listener->mode)
355 clone->mode = ORBit_copy_value (key_listener->mode, TC_Accessibility_EventListenerMode);
362 spi_key_listener_data_free (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
364 CORBA_free (key_listener->typeseq);
365 CORBA_free (key_listener->keys);
366 g_free (key_listener);
370 spi_key_listener_clone_free (DEControllerKeyListener *clone, CORBA_Environment *ev)
372 CORBA_Object_release (clone->listener.object, ev);
373 spi_key_listener_data_free (clone, ev);
377 spi_dec_key_listener_free (DEControllerKeyListener *key_listener,
378 CORBA_Environment *ev)
380 bonobo_object_release_unref (key_listener->listener.object, ev);
381 spi_key_listener_data_free (key_listener, ev);
385 _register_keygrab (SpiDEController *controller,
386 DEControllerGrabMask *grab_mask)
390 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
391 spi_grab_mask_compare_values);
394 DEControllerGrabMask *cur_mask = l->data;
396 cur_mask->ref_count++;
397 if (cur_mask->pending_remove)
399 cur_mask->pending_remove = FALSE;
404 controller->keygrabs_list =
405 g_list_prepend (controller->keygrabs_list,
406 spi_grab_mask_clone (grab_mask));
411 _deregister_keygrab (SpiDEController *controller,
412 DEControllerGrabMask *grab_mask)
416 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
417 spi_grab_mask_compare_values);
421 DEControllerGrabMask *cur_mask = l->data;
423 cur_mask->ref_count--;
424 if (cur_mask->ref_count <= 0)
426 cur_mask->pending_remove = TRUE;
431 g_warning ("De-registering non-existant grab");
436 handle_keygrab (SpiDEController *controller,
437 DEControllerKeyListener *key_listener,
438 void (*process_cb) (SpiDEController *controller,
439 DEControllerGrabMask *grab_mask))
441 DEControllerGrabMask grab_mask = { 0 };
443 grab_mask.mod_mask = key_listener->mask;
444 if (key_listener->keys->_length == 0) /* special case means AnyKey/AllKeys */
446 grab_mask.key_val = AnyKey;
448 fprintf (stderr, "AnyKey grab!");
450 process_cb (controller, &grab_mask);
456 for (i = 0; i < key_listener->keys->_length; ++i)
458 Accessibility_KeyDefinition keydef = key_listener->keys->_buffer[i];
459 long int key_val = keydef.keysym;
460 /* X Grabs require keycodes, not keysyms */
461 if (keydef.keystring && keydef.keystring[0])
463 key_val = XStringToKeysym(keydef.keystring);
467 key_val = XKeysymToKeycode (spi_get_display (), (KeySym) key_val);
471 key_val = keydef.keycode;
473 grab_mask.key_val = key_val;
474 process_cb (controller, &grab_mask);
480 spi_controller_register_global_keygrabs (SpiDEController *controller,
481 DEControllerKeyListener *key_listener)
483 handle_keygrab (controller, key_listener, _register_keygrab);
484 return spi_controller_update_key_grabs (controller, NULL);
488 spi_controller_deregister_global_keygrabs (SpiDEController *controller,
489 DEControllerKeyListener *key_listener)
491 handle_keygrab (controller, key_listener, _deregister_keygrab);
492 spi_controller_update_key_grabs (controller, NULL);
496 spi_controller_register_device_listener (SpiDEController *controller,
497 DEControllerListener *listener,
498 CORBA_Environment *ev)
500 DEControllerKeyListener *key_listener;
502 switch (listener->type) {
503 case SPI_DEVICE_TYPE_KBD:
504 key_listener = (DEControllerKeyListener *) listener;
506 controller->key_listeners = g_list_prepend (controller->key_listeners,
508 if (key_listener->mode->global)
510 return spi_controller_register_global_keygrabs (controller, key_listener);
522 spi_device_event_controller_forward_mouse_event (SpiDEController *controller,
525 Accessibility_Event e;
526 CORBA_Environment ev;
527 gchar event_name[24];
528 int button = ((XButtonEvent *) xevent)->button;
530 unsigned int mouse_button_state = ((XButtonEvent *) xevent)->state;
535 mouse_button_state |= Button1Mask;
538 mouse_button_state |= Button2Mask;
541 mouse_button_state |= Button3Mask;
544 mouse_button_state |= Button4Mask;
547 mouse_button_state |= Button5Mask;
550 last_mouse_pos->x = ((XButtonEvent *) xevent)->x_root;
551 last_mouse_pos->y = ((XButtonEvent *) xevent)->y_root;
554 fprintf (stderr, "mouse button %d %s (%x)\n",
555 ((XButtonEvent *) xevent)->button,
556 (xevent->type == ButtonPress) ? "Press" : "Release",
559 snprintf (event_name, 22, "mouse:button:%d%c", button,
560 (xevent->type == ButtonPress) ? 'p' : 'r');
562 e.type = CORBA_string_dup (event_name);
563 e.source = BONOBO_OBJREF (controller->registry->desktop);
564 e.detail1 = last_mouse_pos->x;
565 e.detail2 = last_mouse_pos->y;
566 CORBA_exception_init (&ev);
567 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
571 XAllowEvents (spi_get_display (), ReplayPointer, CurrentTime);
574 static GdkFilterReturn
575 global_filter_fn (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
577 XEvent *xevent = gdk_xevent;
578 SpiDEController *controller;
580 if (xevent->type == KeyPress || xevent->type == KeyRelease)
582 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
583 spi_device_event_controller_forward_key_event (controller, xevent);
584 /* FIXME: is this right ? */
585 return GDK_FILTER_CONTINUE;
587 if (xevent->type == ButtonPress || xevent->type == ButtonRelease)
589 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
590 spi_device_event_controller_forward_mouse_event (controller, xevent);
593 return GDK_FILTER_CONTINUE;
597 _spi_controller_device_error_handler (Display *display, XErrorEvent *error)
599 if (error->error_code == BadAccess)
601 g_message ("Could not complete key grab: grab already in use.\n");
602 spi_error_code = BadAccess;
607 return (*x_default_error_handler) (display, error);
612 spi_controller_register_with_devices (SpiDEController *controller)
614 /* calls to device-specific implementations and routines go here */
615 /* register with: keyboard hardware code handler */
616 /* register with: (translated) keystroke handler */
618 gdk_window_add_filter (NULL, global_filter_fn, controller);
620 gdk_window_set_events (gdk_get_default_root_window (),
621 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
623 x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
627 spi_key_set_contains_key (Accessibility_KeySet *key_set,
628 const Accessibility_DeviceEvent *key_event)
635 g_print ("null key set!");
639 len = key_set->_length;
641 if (len == 0) /* special case, means "all keys/any key" */
643 g_print ("anykey\n");
647 for (i = 0; i < len; ++i)
649 #ifdef SPI_KEYEVENT_DEBUG
650 g_print ("key_set[%d] = %d; key_event %d, code %d, string %s\n",
651 i, (int) key_set->_buffer[i].keycode,
652 (int) key_event->id, (int) key_event->hw_code,
653 key_event->event_string);
655 if (key_set->_buffer[i].keysym == (CORBA_long) key_event->id)
659 if (key_set->_buffer[i].keycode == (CORBA_long) key_event->hw_code)
663 if (key_event->event_string && key_event->event_string[0] &&
664 !strcmp (key_set->_buffer[i].keystring, key_event->event_string))
674 spi_key_eventtype_seq_contains_event (Accessibility_KeyEventTypeSeq *type_seq,
675 const Accessibility_DeviceEvent *key_event)
683 g_print ("null type seq!");
687 len = type_seq->_length;
689 if (len == 0) /* special case, means "all events/any event" */
694 for (i = 0; i < len; ++i)
697 g_print ("type_seq[%d] = %d; key event type = %d\n", i,
698 (int) type_seq->_buffer[i], (int) key_event->type);
700 if (type_seq->_buffer[i] == (CORBA_long) key_event->type)
710 spi_key_event_matches_listener (const Accessibility_DeviceEvent *key_event,
711 DEControllerKeyListener *listener,
712 CORBA_boolean is_system_global)
714 if ((key_event->modifiers == (CORBA_unsigned_short) (listener->mask & 0xFFFF)) &&
715 spi_key_set_contains_key (listener->keys, key_event) &&
716 spi_key_eventtype_seq_contains_event (listener->typeseq, key_event) &&
717 (is_system_global == listener->mode->global))
728 spi_controller_notify_keylisteners (SpiDEController *controller,
729 const Accessibility_DeviceEvent *key_event,
730 CORBA_boolean is_system_global,
731 CORBA_Environment *ev)
734 GSList *notify = NULL, *l2;
735 GList **key_listeners = &controller->key_listeners;
736 gboolean is_consumed;
743 for (l = *key_listeners; l; l = l->next)
745 DEControllerKeyListener *key_listener = l->data;
747 if (spi_key_event_matches_listener (key_event, key_listener, is_system_global))
749 Accessibility_DeviceEventListener ls = key_listener->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_key_listener_clone (key_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 DEControllerKeyListener *key_listener = l2->data;
771 Accessibility_DeviceEventListener ls = key_listener->listener.object;
773 is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, key_event, ev);
778 spi_deregister_controller_key_listener (controller, key_listener,
780 CORBA_exception_free (ev);
783 CORBA_Object_release (ls, ev);
786 for (; l2; l2 = l2->next)
788 DEControllerKeyListener *key_listener = l2->data;
789 spi_key_listener_clone_free (key_listener, ev);
790 /* clone doesn't have its own ref, so don't use spi_key_listener_free */
793 g_slist_free (notify);
796 if (is_consumed) g_message ("consumed\n");
802 spi_clear_error_state ()
804 gboolean retval = spi_error_code != 0;
809 static Accessibility_DeviceEvent
810 spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
812 Accessibility_DeviceEvent key_event;
814 const int cbuf_bytes = 20;
815 char cbuf [cbuf_bytes];
817 keysym = XLookupKeysym (x_key_event, 0);
818 key_event.id = (CORBA_long)(keysym);
819 key_event.hw_code = (CORBA_short) x_key_event->keycode;
820 if (((XEvent *) x_key_event)->type == KeyPress)
822 key_event.type = Accessibility_KEY_PRESSED;
826 key_event.type = Accessibility_KEY_RELEASED;
828 key_event.modifiers = (CORBA_unsigned_short)(x_key_event->state);
829 key_event.is_text = CORBA_FALSE;
833 key_event.event_string = CORBA_string_dup ("space");
836 #ifdef SPI_KEYEVENT_DEBUG
837 fprintf(stderr, "Tab\n");
839 key_event.event_string = CORBA_string_dup ("Tab");
842 key_event.event_string = CORBA_string_dup ("Backspace");
845 key_event.event_string = CORBA_string_dup ("Return");
848 key_event.event_string = CORBA_string_dup ("Home");
851 key_event.event_string = CORBA_string_dup ("Page_Down");
854 key_event.event_string = CORBA_string_dup ("Page_Up");
857 key_event.event_string = CORBA_string_dup ("F1");
860 key_event.event_string = CORBA_string_dup ("F2");
863 key_event.event_string = CORBA_string_dup ("F3");
866 key_event.event_string = CORBA_string_dup ("F4");
869 key_event.event_string = CORBA_string_dup ("F5");
872 key_event.event_string = CORBA_string_dup ("F6");
875 key_event.event_string = CORBA_string_dup ("F7");
878 key_event.event_string = CORBA_string_dup ("F8");
881 key_event.event_string = CORBA_string_dup ("F9");
884 key_event.event_string = CORBA_string_dup ("F10");
887 key_event.event_string = CORBA_string_dup ("F11");
890 key_event.event_string = CORBA_string_dup ("F12");
893 key_event.event_string = CORBA_string_dup ("End");
896 key_event.event_string = CORBA_string_dup ("Escape");
899 key_event.event_string = CORBA_string_dup ("Up");
902 key_event.event_string = CORBA_string_dup ("Down");
905 key_event.event_string = CORBA_string_dup ("Left");
908 key_event.event_string = CORBA_string_dup ("Right");
911 if (XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL) > 0)
913 key_event.event_string = CORBA_string_dup (cbuf);
914 if (isgraph (keysym))
916 key_event.is_text = CORBA_TRUE; /* FIXME: incorrect for some composed chars? */
921 key_event.event_string = CORBA_string_dup ("");
925 key_event.timestamp = (CORBA_unsigned_long) x_key_event->time;
926 #ifdef SPI_KEYEVENT_DEBUG
928 "Key %lu pressed (%c), modifiers %d\n",
929 (unsigned long) keysym,
930 keysym ? (int) keysym : '*',
931 (int) x_key_event->state);
934 fprintf (stderr, "%s%c",
935 (x_key_event->state & Mod1Mask)?"Alt-":"",
936 ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
937 g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
938 #endif /* SPI_DEBUG */
943 spi_controller_update_key_grabs (SpiDEController *controller,
944 Accessibility_DeviceEvent *recv)
947 gboolean update_failed = FALSE;
949 g_return_val_if_fail (controller != NULL, FALSE);
952 * masks known to work with default RH 7.1+:
953 * 0 (no mods), LockMask, Mod1Mask, Mod2Mask, ShiftMask,
954 * ShiftMask|LockMask, Mod1Mask|LockMask, Mod2Mask|LockMask,
955 * ShiftMask|Mod1Mask, ShiftMask|Mod2Mask, Mod1Mask|Mod2Mask,
956 * ShiftMask|LockMask|Mod1Mask, ShiftMask|LockMask|Mod2Mask,
958 * ControlMask grabs are broken, must be in use already
960 for (l = controller->keygrabs_list; l; l = next)
963 gboolean re_issue_grab;
964 DEControllerGrabMask *grab_mask = l->data;
968 re_issue_grab = recv &&
969 /* (recv->type == Accessibility_KEY_RELEASED) && - (?) */
970 (recv->modifiers & grab_mask->mod_mask) &&
971 (grab_mask->key_val == keycode_for_keysym (recv->id));
974 fprintf (stderr, "mask=%lx %lx (%c%c) %s\n",
975 (long int) grab_mask->key_val,
976 (long int) grab_mask->mod_mask,
977 grab_mask->pending_add ? '+' : '.',
978 grab_mask->pending_remove ? '-' : '.',
979 re_issue_grab ? "re-issue": "");
984 if (grab_mask->pending_add && grab_mask->pending_remove)
988 else if (grab_mask->pending_remove)
991 fprintf (stderr, "ungrabbing, mask=%x\n", grab_mask->mod_mask);
993 XUngrabKey (spi_get_display (),
996 gdk_x11_get_default_root_xwindow ());
1000 else if (grab_mask->pending_add || re_issue_grab)
1004 fprintf (stderr, "grab %d with mask %x\n", grab_mask->key_val, grab_mask->mod_mask);
1006 XGrabKey (spi_get_display (),
1008 grab_mask->mod_mask,
1009 gdk_x11_get_default_root_xwindow (),
1013 XSync (spi_get_display (), False);
1014 update_failed = spi_clear_error_state ();
1015 if (update_failed) {
1016 while (grab_mask->ref_count > 0) --grab_mask->ref_count;
1021 grab_mask->pending_add = FALSE;
1022 grab_mask->pending_remove = FALSE;
1026 g_assert (grab_mask->ref_count <= 0);
1028 controller->keygrabs_list = g_list_delete_link (
1029 controller->keygrabs_list, l);
1031 spi_grab_mask_free (grab_mask);
1036 return ! update_failed;
1040 * Implemented GObject::finalize
1043 spi_device_event_controller_object_finalize (GObject *object)
1045 SpiDEController *controller;
1047 controller = SPI_DEVICE_EVENT_CONTROLLER (object);
1050 fprintf(stderr, "spi_device_event_controller_object_finalize called\n");
1052 /* disconnect any special listeners, get rid of outstanding keygrabs */
1053 XUngrabKey (spi_get_display (), AnyKey, AnyModifier, DefaultRootWindow (spi_get_display ()));
1055 spi_device_event_controller_parent_class->finalize (object);
1059 * CORBA Accessibility::DEController::registerKeystrokeListener
1060 * method implementation
1062 static CORBA_boolean
1063 impl_register_keystroke_listener (PortableServer_Servant servant,
1064 const Accessibility_DeviceEventListener l,
1065 const Accessibility_KeySet *keys,
1066 const Accessibility_ControllerEventMask mask,
1067 const Accessibility_KeyEventTypeSeq *type,
1068 const Accessibility_EventListenerMode *mode,
1069 CORBA_Environment *ev)
1071 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1072 bonobo_object_from_servant (servant));
1073 DEControllerKeyListener *dec_listener;
1075 fprintf (stderr, "registering keystroke listener %p with maskVal %lu\n",
1076 (void *) l, (unsigned long) mask);
1078 dec_listener = spi_dec_key_listener_new (l, keys, mask, type, mode, ev);
1079 return spi_controller_register_device_listener (
1080 controller, (DEControllerListener *) dec_listener, ev);
1085 CORBA_Environment *ev;
1086 DEControllerKeyListener *key_listener;
1087 } RemoveKeyListenerClosure;
1089 static SpiReEntrantContinue
1090 remove_key_listener_cb (GList * const *list,
1093 DEControllerKeyListener *key_listener = (*list)->data;
1094 RemoveKeyListenerClosure *ctx = user_data;
1096 if (CORBA_Object_is_equivalent (ctx->key_listener->listener.object,
1097 key_listener->listener.object, ctx->ev))
1099 spi_re_entrant_list_delete_link (list);
1100 spi_dec_key_listener_free (key_listener, ctx->ev);
1103 return SPI_RE_ENTRANT_CONTINUE;
1106 static SpiReEntrantContinue
1107 copy_key_listener_cb (GList * const *list,
1110 DEControllerKeyListener *key_listener = (*list)->data;
1111 RemoveKeyListenerClosure *ctx = user_data;
1113 if (CORBA_Object_is_equivalent (ctx->key_listener->listener.object,
1114 key_listener->listener.object, ctx->ev))
1116 /* TODO: FIXME aggregate keys in case the listener is registered twice */
1117 CORBA_free (ctx->key_listener->keys);
1118 ctx->key_listener->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
1121 return SPI_RE_ENTRANT_CONTINUE;
1126 spi_deregister_controller_key_listener (SpiDEController *controller,
1127 DEControllerKeyListener *key_listener,
1128 CORBA_Environment *ev)
1130 RemoveKeyListenerClosure ctx;
1133 ctx.key_listener = key_listener;
1135 /* special case, copy keyset from existing controller list entry */
1136 if (key_listener->keys->_length == 0)
1138 spi_re_entrant_list_foreach (&controller->key_listeners,
1139 copy_key_listener_cb, &ctx);
1142 spi_controller_deregister_global_keygrabs (controller, key_listener);
1144 spi_re_entrant_list_foreach (&controller->key_listeners,
1145 remove_key_listener_cb, &ctx);
1150 * CORBA Accessibility::DEController::deregisterKeystrokeListener
1151 * method implementation
1154 impl_deregister_keystroke_listener (PortableServer_Servant servant,
1155 const Accessibility_DeviceEventListener l,
1156 const Accessibility_KeySet *keys,
1157 const Accessibility_ControllerEventMask mask,
1158 const Accessibility_KeyEventTypeSeq *type,
1159 CORBA_Environment *ev)
1161 DEControllerKeyListener *key_listener;
1162 SpiDEController *controller;
1164 controller = SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1166 key_listener = spi_dec_key_listener_new (l, keys, mask, type, NULL, ev);
1168 #ifdef SPI_DEREGISTER_DEBUG
1169 fprintf (stderr, "deregistering keystroke listener %p with maskVal %lu\n",
1170 (void *) l, (unsigned long) mask->value);
1173 spi_deregister_controller_key_listener (controller, key_listener, ev);
1175 spi_dec_key_listener_free (key_listener, ev);
1179 * CORBA Accessibility::DEController::registerKeystrokeListener
1180 * method implementation
1183 impl_generate_keyboard_event (PortableServer_Servant servant,
1184 const CORBA_long keycode,
1185 const CORBA_char *keystring,
1186 const Accessibility_KeySynthType synth_type,
1187 CORBA_Environment *ev)
1189 long key_synth_code;
1192 fprintf (stderr, "synthesizing keystroke %ld, type %d\n",
1193 (long) keycode, (int) synth_type);
1195 /* TODO: hide/wrap/remove X dependency */
1197 /* TODO: be accessX-savvy so that keyrelease occurs after sufficient timeout */
1200 * TODO: when initializing, query for XTest extension before using,
1201 * and fall back to XSendEvent() if XTest is not available.
1204 /* TODO: implement keystring mode also */
1205 gdk_error_trap_push ();
1209 case Accessibility_KEY_PRESS:
1210 XTestFakeKeyEvent (spi_get_display (), (unsigned int) keycode, True, CurrentTime);
1212 case Accessibility_KEY_PRESSRELEASE:
1213 XTestFakeKeyEvent (spi_get_display (), (unsigned int) keycode, True, CurrentTime);
1214 case Accessibility_KEY_RELEASE:
1215 XTestFakeKeyEvent (spi_get_display (), (unsigned int) keycode, False, CurrentTime);
1217 case Accessibility_KEY_SYM:
1218 key_synth_code = keycode_for_keysym (keycode);
1219 XTestFakeKeyEvent (spi_get_display (), (unsigned int) key_synth_code, True, CurrentTime);
1220 XTestFakeKeyEvent (spi_get_display (), (unsigned int) key_synth_code, False, CurrentTime);
1222 case Accessibility_KEY_STRING:
1223 fprintf (stderr, "Not yet implemented\n");
1226 if (gdk_error_trap_pop ())
1228 g_warning ("Error emitting keystroke");
1232 /* Accessibility::DEController::generateMouseEvent */
1234 impl_generate_mouse_event (PortableServer_Servant servant,
1237 const CORBA_char *eventName,
1238 CORBA_Environment *ev)
1241 gboolean error = FALSE;
1242 Display *display = spi_get_display ();
1244 fprintf (stderr, "generating mouse %s event at %ld, %ld\n",
1245 eventName, (long int) x, (long int) y);
1247 g_message ("mouse event synthesis\n");
1248 switch (eventName[0])
1251 switch (eventName[1])
1253 /* TODO: check number of buttons before parsing */
1268 if (x != -1 && y != -1)
1270 XTestFakeMotionEvent (display, DefaultScreen (display),
1273 XTestFakeButtonEvent (display, button, !(eventName[2] == 'r'), 0);
1274 if (eventName[2] == 'c')
1275 XTestFakeButtonEvent (display, button, FALSE, 1);
1276 else if (eventName[2] == 'd')
1278 XTestFakeButtonEvent (display, button, FALSE, 1);
1279 XTestFakeButtonEvent (display, button, TRUE, 2);
1280 XTestFakeButtonEvent (display, button, FALSE, 3);
1284 case 'r': /* relative motion */
1285 XTestFakeRelativeMotionEvent (display, x, y, 0);
1287 case 'a': /* absolute motion */
1288 XTestFakeMotionEvent (display, DefaultScreen (display),
1294 /* Accessibility::DEController::notifyListenersSync */
1295 static CORBA_boolean
1296 impl_notify_listeners_sync (PortableServer_Servant servant,
1297 const Accessibility_DeviceEvent *event,
1298 CORBA_Environment *ev)
1300 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1301 bonobo_object_from_servant (servant));
1303 g_print ("notifylistening listeners synchronously: controller %p, event id %d\n",
1304 controller, (int) event->id);
1306 return spi_controller_notify_keylisteners (controller, event, CORBA_FALSE, ev) ?
1307 CORBA_TRUE : CORBA_FALSE;
1310 /* Accessibility::DEController::notifyListenersAsync */
1312 impl_notify_listeners_async (PortableServer_Servant servant,
1313 const Accessibility_DeviceEvent *event,
1314 CORBA_Environment *ev)
1316 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1317 bonobo_object_from_servant (servant));
1319 fprintf (stderr, "notifying listeners asynchronously\n");
1321 spi_controller_notify_keylisteners (controller, event, CORBA_FALSE, ev);
1325 spi_device_event_controller_class_init (SpiDEControllerClass *klass)
1327 GObjectClass * object_class = (GObjectClass *) klass;
1328 POA_Accessibility_DeviceEventController__epv *epv = &klass->epv;
1330 spi_device_event_controller_parent_class = g_type_class_peek_parent (klass);
1332 object_class->finalize = spi_device_event_controller_object_finalize;
1334 epv->registerKeystrokeListener = impl_register_keystroke_listener;
1335 epv->deregisterKeystrokeListener = impl_deregister_keystroke_listener;
1336 epv->generateKeyboardEvent = impl_generate_keyboard_event;
1337 epv->generateMouseEvent = impl_generate_mouse_event;
1338 epv->notifyListenersSync = impl_notify_listeners_sync;
1339 epv->notifyListenersAsync = impl_notify_listeners_async;
1343 spi_device_event_controller_init (SpiDEController *device_event_controller)
1345 device_event_controller->key_listeners = NULL;
1346 device_event_controller->mouse_listeners = NULL;
1347 device_event_controller->keygrabs_list = NULL;
1350 * TODO: fixme, this module makes the foolish assumptions that
1351 * registryd uses the same display as the apps, and that the
1352 * DISPLAY environment variable is set.
1354 gdk_init (NULL, NULL);
1356 spi_controller_register_with_devices (device_event_controller);
1360 spi_device_event_controller_forward_key_event (SpiDEController *controller,
1361 const XEvent *event)
1363 gboolean is_consumed = FALSE;
1364 CORBA_Environment ev;
1365 Accessibility_DeviceEvent key_event;
1367 g_assert (event->type == KeyPress || event->type == KeyRelease);
1369 CORBA_exception_init (&ev);
1371 key_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1373 spi_controller_update_key_grabs (controller, &key_event);
1375 /* relay to listeners, and decide whether to consume it or not */
1376 is_consumed = spi_controller_notify_keylisteners (
1377 controller, &key_event, CORBA_TRUE, &ev);
1379 CORBA_exception_free (&ev);
1383 XAllowEvents (spi_get_display (), AsyncKeyboard, CurrentTime);
1387 XAllowEvents (spi_get_display (), ReplayKeyboard, CurrentTime);
1392 spi_device_event_controller_new (SpiRegistry *registry)
1394 SpiDEController *retval = g_object_new (
1395 SPI_DEVICE_EVENT_CONTROLLER_TYPE, NULL);
1397 retval->registry = SPI_REGISTRY (bonobo_object_ref (
1398 BONOBO_OBJECT (registry)));
1400 spi_dec_init_mouse_listener (registry);
1401 /* TODO: kill mouse listener on finalize */
1406 BONOBO_TYPE_FUNC_FULL (SpiDEController,
1407 Accessibility_DeviceEventController,
1409 spi_device_event_controller);