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_button_state = 0;
56 static unsigned int mouse_button_mask =
57 Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask;
59 int (*x_default_error_handler) (Display *display, XErrorEvent *error_event);
63 SPI_DEVICE_TYPE_MOUSE,
64 SPI_DEVICE_TYPE_LAST_DEFINED
65 } SpiDeviceTypeCategory;
69 guint pending_add : 1;
70 guint pending_remove : 1;
72 Accessibility_ControllerEventMask mod_mask;
73 CORBA_unsigned_long key_val; /* KeyCode */
74 } DEControllerGrabMask;
78 SpiDeviceTypeCategory type;
79 } DEControllerListener;
82 DEControllerListener listener;
84 Accessibility_KeySet *keys;
85 Accessibility_ControllerEventMask mask;
86 Accessibility_KeyEventTypeSeq *typeseq;
87 Accessibility_EventListenerMode *mode;
88 } DEControllerKeyListener;
90 static void spi_controller_register_with_devices (SpiDEController *controller);
91 static gboolean spi_controller_update_key_grabs (SpiDEController *controller,
92 Accessibility_DeviceEvent *recv);
93 static gboolean spi_controller_register_device_listener (SpiDEController *controller,
94 DEControllerListener *l,
95 CORBA_Environment *ev);
96 static void spi_device_event_controller_forward_key_event (SpiDEController *controller,
98 static void spi_deregister_controller_key_listener (SpiDEController *controller,
99 DEControllerKeyListener *key_listener,
100 CORBA_Environment *ev);
102 static gboolean spi_clear_error_state (void);
103 static gboolean spi_dec_poll_mouse_moved (gpointer data);
104 static gboolean spi_dec_poll_mouse_moving (gpointer data);
105 static gboolean spi_dec_poll_mouse_idle (gpointer data);
107 #define spi_get_display() GDK_DISPLAY()
109 /* Private methods */
112 keycode_for_keysym (long keysym)
114 return XKeysymToKeycode (spi_get_display (), (KeySym) keysym);
117 static DEControllerGrabMask *
118 spi_grab_mask_clone (DEControllerGrabMask *grab_mask)
120 DEControllerGrabMask *clone = g_new (DEControllerGrabMask, 1);
122 memcpy (clone, grab_mask, sizeof (DEControllerGrabMask));
124 clone->ref_count = 1;
125 clone->pending_add = TRUE;
126 clone->pending_remove = FALSE;
132 spi_grab_mask_free (DEControllerGrabMask *grab_mask)
138 spi_grab_mask_compare_values (gconstpointer p1, gconstpointer p2)
140 DEControllerGrabMask *l1 = (DEControllerGrabMask *) p1;
141 DEControllerGrabMask *l2 = (DEControllerGrabMask *) p2;
149 return ((l1->mod_mask != l2->mod_mask) || (l1->key_val != l2->key_val));
154 spi_dec_poll_mouse_moved (gpointer data)
156 SpiRegistry *registry = SPI_REGISTRY (data);
157 CORBA_Environment ev;
158 Accessibility_Event e;
159 Window root_return, child_return;
160 int win_x_return,win_y_return;
162 unsigned int mask_return;
163 gchar event_name[24];
164 Display *display = spi_get_display ();
167 XQueryPointer(display, DefaultRootWindow (display),
168 &root_return, &child_return,
170 &win_x_return, &win_y_return, &mask_return);
172 if ((mask_return & mouse_button_mask) != mouse_button_state) {
173 int button_number = 0;
174 if (!(mask_return & Button1Mask) &&
175 (mouse_button_state & Button1Mask)) {
177 } else if (!(mask_return & Button2Mask) &&
178 (mouse_button_state & Button2Mask)) {
180 } else if (!(mask_return & Button3Mask) &&
181 (mouse_button_state & Button3Mask)) {
183 } else if (!(mask_return & Button4Mask) &&
184 (mouse_button_state & Button1Mask)) {
186 } else if (!(mask_return & Button5Mask) &&
187 (mouse_button_state & Button5Mask)) {
192 fprintf (stderr, "Button %d Released\n",
195 snprintf (event_name, 22, "mouse:button:%dr", button_number);
197 e.source = BONOBO_OBJREF (registry->desktop);
198 e.detail1 = last_mouse_pos->x;
199 e.detail2 = last_mouse_pos->y;
200 CORBA_exception_init (&ev);
201 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
205 mouse_button_state = mask_return & mouse_button_mask;
207 if (last_mouse_pos == NULL) {
208 last_mouse_pos = g_new0 (GdkPoint, 1);
209 last_mouse_pos->x = 0;
210 last_mouse_pos->y = 0;
211 e.type = "mouse:abs";
213 e.type = "mouse:rel";
215 if (x != last_mouse_pos->x || y != last_mouse_pos->y) {
216 e.source = BONOBO_OBJREF (registry->desktop);
217 e.detail1 = x - last_mouse_pos->x;
218 e.detail2 = y - last_mouse_pos->y;
219 CORBA_exception_init (&ev);
220 last_mouse_pos->x = x;
221 last_mouse_pos->y = y;
222 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
231 spi_dec_poll_mouse_idle (gpointer data)
233 if (! spi_dec_poll_mouse_moved (data))
237 g_timeout_add (20, spi_dec_poll_mouse_moving, data);
243 spi_dec_poll_mouse_moving (gpointer data)
245 if (spi_dec_poll_mouse_moved (data))
249 g_timeout_add (100, spi_dec_poll_mouse_idle, data);
255 spi_dec_ungrab_mouse (gpointer data)
257 Display *display = spi_get_display ();
258 fprintf (stderr, "mouse ungrab : display = %p\n", display);
261 XUngrabButton (spi_get_display (), AnyButton, AnyModifier,
262 XDefaultRootWindow (spi_get_display ()));
263 fprintf (stderr, "mouse grab released\n");
269 spi_dec_init_mouse_listener (SpiRegistry *registry)
271 Display *display = spi_get_display ();
272 g_timeout_add (100, spi_dec_poll_mouse_idle, registry);
276 XGrabButton (display, AnyButton, 0,
277 gdk_x11_get_default_root_xwindow (),
278 True, ButtonPressMask | ButtonReleaseMask,
279 GrabModeSync, GrabModeAsync, None, None);
280 XSync (display, False);
281 fprintf (stderr, "mouse buttons grabbed\n");
285 static DEControllerKeyListener *
286 spi_dec_key_listener_new (CORBA_Object l,
287 const Accessibility_KeySet *keys,
288 const Accessibility_ControllerEventMask mask,
289 const Accessibility_KeyEventTypeSeq *typeseq,
290 const Accessibility_EventListenerMode *mode,
291 CORBA_Environment *ev)
293 DEControllerKeyListener *key_listener = g_new0 (DEControllerKeyListener, 1);
294 key_listener->listener.object = bonobo_object_dup_ref (l, ev);
295 key_listener->listener.type = SPI_DEVICE_TYPE_KBD;
296 key_listener->keys = ORBit_copy_value (keys, TC_Accessibility_KeySet);
297 key_listener->mask = mask;
298 key_listener->typeseq = ORBit_copy_value (typeseq, TC_Accessibility_KeyEventTypeSeq);
300 key_listener->mode = ORBit_copy_value (mode, TC_Accessibility_EventListenerMode);
302 key_listener->mode = NULL;
305 g_print ("new listener, with mask %x, is_global %d, keys %p (%d)\n",
306 (unsigned int) key_listener->mask,
307 (int) (mode ? mode->global : 0),
308 (void *) key_listener->keys,
309 (int) (key_listener->keys ? key_listener->keys->_length : 0));
315 static DEControllerKeyListener *
316 spi_key_listener_clone (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
318 DEControllerKeyListener *clone = g_new0 (DEControllerKeyListener, 1);
319 clone->listener.object =
320 CORBA_Object_duplicate (key_listener->listener.object, ev);
321 clone->listener.type = SPI_DEVICE_TYPE_KBD;
322 clone->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
323 clone->mask = key_listener->mask;
324 clone->typeseq = ORBit_copy_value (key_listener->typeseq, TC_Accessibility_KeyEventTypeSeq);
325 if (key_listener->mode)
326 clone->mode = ORBit_copy_value (key_listener->mode, TC_Accessibility_EventListenerMode);
333 spi_key_listener_data_free (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
335 CORBA_free (key_listener->typeseq);
336 CORBA_free (key_listener->keys);
337 g_free (key_listener);
341 spi_key_listener_clone_free (DEControllerKeyListener *clone, CORBA_Environment *ev)
343 CORBA_Object_release (clone->listener.object, ev);
344 spi_key_listener_data_free (clone, ev);
348 spi_dec_key_listener_free (DEControllerKeyListener *key_listener,
349 CORBA_Environment *ev)
351 bonobo_object_release_unref (key_listener->listener.object, ev);
352 spi_key_listener_data_free (key_listener, ev);
356 _register_keygrab (SpiDEController *controller,
357 DEControllerGrabMask *grab_mask)
361 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
362 spi_grab_mask_compare_values);
365 DEControllerGrabMask *cur_mask = l->data;
367 cur_mask->ref_count++;
368 if (cur_mask->pending_remove)
370 cur_mask->pending_remove = FALSE;
375 controller->keygrabs_list =
376 g_list_prepend (controller->keygrabs_list,
377 spi_grab_mask_clone (grab_mask));
382 _deregister_keygrab (SpiDEController *controller,
383 DEControllerGrabMask *grab_mask)
387 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
388 spi_grab_mask_compare_values);
392 DEControllerGrabMask *cur_mask = l->data;
394 cur_mask->ref_count--;
395 if (cur_mask->ref_count <= 0)
397 cur_mask->pending_remove = TRUE;
402 g_warning ("De-registering non-existant grab");
407 handle_keygrab (SpiDEController *controller,
408 DEControllerKeyListener *key_listener,
409 void (*process_cb) (SpiDEController *controller,
410 DEControllerGrabMask *grab_mask))
412 DEControllerGrabMask grab_mask = { 0 };
414 grab_mask.mod_mask = key_listener->mask;
415 if (key_listener->keys->_length == 0) /* special case means AnyKey/AllKeys */
417 grab_mask.key_val = AnyKey;
419 fprintf (stderr, "AnyKey grab!"); */
421 process_cb (controller, &grab_mask);
427 for (i = 0; i < key_listener->keys->_length; ++i)
429 Accessibility_KeyDefinition keydef = key_listener->keys->_buffer[i];
430 long int key_val = keydef.keysym;
431 /* X Grabs require keycodes, not keysyms */
432 if (keydef.keystring && keydef.keystring[0])
434 key_val = XStringToKeysym(keydef.keystring);
438 key_val = XKeysymToKeycode (spi_get_display (), (KeySym) key_val);
442 key_val = keydef.keycode;
444 grab_mask.key_val = key_val;
445 process_cb (controller, &grab_mask);
451 spi_controller_register_global_keygrabs (SpiDEController *controller,
452 DEControllerKeyListener *key_listener)
454 handle_keygrab (controller, key_listener, _register_keygrab);
455 return spi_controller_update_key_grabs (controller, NULL);
459 spi_controller_deregister_global_keygrabs (SpiDEController *controller,
460 DEControllerKeyListener *key_listener)
462 handle_keygrab (controller, key_listener, _deregister_keygrab);
463 spi_controller_update_key_grabs (controller, NULL);
467 spi_controller_register_device_listener (SpiDEController *controller,
468 DEControllerListener *listener,
469 CORBA_Environment *ev)
471 DEControllerKeyListener *key_listener;
473 switch (listener->type) {
474 case SPI_DEVICE_TYPE_KBD:
475 key_listener = (DEControllerKeyListener *) listener;
477 controller->key_listeners = g_list_prepend (controller->key_listeners,
479 if (key_listener->mode->global)
481 return spi_controller_register_global_keygrabs (controller, key_listener);
493 spi_device_event_controller_forward_mouse_event (SpiDEController *controller,
496 Accessibility_Event e;
497 CORBA_Environment ev;
498 gchar event_name[24];
499 int button = ((XButtonEvent *) xevent)->button;
501 mouse_button_state = ((XButtonEvent *) xevent)->state;
506 mouse_button_state |= Button1Mask;
509 mouse_button_state |= Button2Mask;
512 mouse_button_state |= Button3Mask;
515 mouse_button_state |= Button4Mask;
518 mouse_button_state |= Button5Mask;
521 last_mouse_pos->x = ((XButtonEvent *) xevent)->x_root;
522 last_mouse_pos->y = ((XButtonEvent *) xevent)->y_root;
525 fprintf (stderr, "mouse button %d %s (%x)\n",
526 ((XButtonEvent *) xevent)->button,
527 (xevent->type == ButtonPress) ? "Press" : "Release",
530 snprintf (event_name, 22, "mouse:button:%d%c", button,
531 (xevent->type == ButtonPress) ? 'p' : 'r');
533 e.type = CORBA_string_dup (event_name);
534 e.source = BONOBO_OBJREF (controller->registry->desktop);
535 e.detail1 = last_mouse_pos->x;
536 e.detail2 = last_mouse_pos->y;
537 CORBA_exception_init (&ev);
538 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
542 XAllowEvents (spi_get_display (), ReplayPointer, CurrentTime);
545 static GdkFilterReturn
546 global_filter_fn (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
548 XEvent *xevent = gdk_xevent;
549 SpiDEController *controller;
551 if (xevent->type == KeyPress && xevent->type == KeyRelease)
553 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
554 spi_device_event_controller_forward_key_event (controller, xevent);
555 /* FIXME: is this right ? */
556 return GDK_FILTER_CONTINUE;
558 if (xevent->type == ButtonPress || xevent->type == ButtonRelease)
560 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
561 spi_device_event_controller_forward_mouse_event (controller, xevent);
564 return GDK_FILTER_CONTINUE;
568 _spi_controller_device_error_handler (Display *display, XErrorEvent *error)
570 if (error->error_code == BadAccess)
572 g_message ("Could not complete key grab: grab already in use.\n");
573 spi_error_code = BadAccess;
578 return (*x_default_error_handler) (display, error);
583 spi_controller_register_with_devices (SpiDEController *controller)
585 /* calls to device-specific implementations and routines go here */
586 /* register with: keyboard hardware code handler */
587 /* register with: (translated) keystroke handler */
589 gdk_window_add_filter (NULL, global_filter_fn, controller);
591 gdk_window_set_events (gdk_get_default_root_window (),
592 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
594 x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
597 #define SPI_KEYEVENT_DEBUG
599 spi_key_set_contains_key (Accessibility_KeySet *key_set,
600 const Accessibility_DeviceEvent *key_event)
607 g_print ("null key set!");
611 len = key_set->_length;
613 if (len == 0) /* special case, means "all keys/any key" */
615 g_print ("anykey\n");
619 for (i = 0; i < len; ++i)
621 #ifdef SPI_KEYEVENT_DEBUG
622 g_print ("key_set[%d] = %d; key_event %d, code %d, string %s\n",
623 i, (int) key_set->_buffer[i].keycode,
624 (int) key_event->id, (int) key_event->hw_code,
625 key_event->event_string);
627 if (key_set->_buffer[i].keysym == (CORBA_long) key_event->id)
631 if (key_set->_buffer[i].keycode == (CORBA_long) key_event->hw_code)
635 if (key_event->event_string && key_event->event_string[0] &&
636 !strcmp (key_set->_buffer[i].keystring, key_event->event_string))
646 spi_key_eventtype_seq_contains_event (Accessibility_KeyEventTypeSeq *type_seq,
647 const Accessibility_DeviceEvent *key_event)
655 g_print ("null type seq!");
659 len = type_seq->_length;
661 if (len == 0) /* special case, means "all events/any event" */
666 for (i = 0; i < len; ++i)
669 g_print ("type_seq[%d] = %d; key event type = %d\n", i,
670 (int) type_seq->_buffer[i], (int) key_event->type);
672 if (type_seq->_buffer[i] == (CORBA_long) key_event->type)
682 spi_key_event_matches_listener (const Accessibility_DeviceEvent *key_event,
683 DEControllerKeyListener *listener,
684 CORBA_boolean is_system_global)
686 if ((key_event->modifiers == (CORBA_unsigned_short) (listener->mask & 0xFFFF)) &&
687 spi_key_set_contains_key (listener->keys, key_event) &&
688 spi_key_eventtype_seq_contains_event (listener->typeseq, key_event) &&
689 (is_system_global == listener->mode->global))
700 spi_controller_notify_keylisteners (SpiDEController *controller,
701 const Accessibility_DeviceEvent *key_event,
702 CORBA_boolean is_system_global,
703 CORBA_Environment *ev)
706 GSList *notify = NULL, *l2;
707 GList **key_listeners = &controller->key_listeners;
708 gboolean is_consumed;
715 for (l = *key_listeners; l; l = l->next)
717 DEControllerKeyListener *key_listener = l->data;
719 if (spi_key_event_matches_listener (key_event, key_listener, is_system_global))
721 Accessibility_DeviceEventListener ls = key_listener->listener.object;
723 if (ls != CORBA_OBJECT_NIL)
725 /* we clone (don't dup) the listener, to avoid refcount inc. */
726 notify = g_slist_prepend (notify,
727 spi_key_listener_clone (key_listener, ev));
732 #ifdef SPI_KEYEVENT_DEBUG
735 g_print ("no match for event\n");
740 for (l2 = notify; l2 && !is_consumed; l2 = l2->next)
742 DEControllerKeyListener *key_listener = l2->data;
743 Accessibility_DeviceEventListener ls = key_listener->listener.object;
745 is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, key_event, ev);
750 spi_deregister_controller_key_listener (controller, key_listener,
752 CORBA_exception_free (ev);
755 CORBA_Object_release (ls, ev);
758 for (; l2; l2 = l2->next)
760 DEControllerKeyListener *key_listener = l2->data;
761 spi_key_listener_clone_free (key_listener, ev);
762 /* clone doesn't have its own ref, so don't use spi_key_listener_free */
765 g_slist_free (notify);
768 if (is_consumed) g_message ("consumed\n");
774 spi_clear_error_state ()
776 gboolean retval = spi_error_code != 0;
781 static Accessibility_DeviceEvent
782 spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
784 Accessibility_DeviceEvent key_event;
786 const int cbuf_bytes = 20;
787 char cbuf [cbuf_bytes];
789 keysym = XLookupKeysym (x_key_event, 0);
790 key_event.id = (CORBA_long)(keysym);
791 key_event.hw_code = (CORBA_short) x_key_event->keycode;
792 if (((XEvent *) x_key_event)->type == KeyPress)
794 key_event.type = Accessibility_KEY_PRESSED;
798 key_event.type = Accessibility_KEY_RELEASED;
800 key_event.modifiers = (CORBA_unsigned_short)(x_key_event->state);
801 key_event.is_text = CORBA_FALSE;
805 key_event.event_string = CORBA_string_dup ("space");
808 #ifdef SPI_KEYEVENT_DEBUG
809 fprintf(stderr, "Tab\n");
811 key_event.event_string = CORBA_string_dup ("Tab");
814 key_event.event_string = CORBA_string_dup ("Backspace");
817 key_event.event_string = CORBA_string_dup ("Return");
820 key_event.event_string = CORBA_string_dup ("Home");
823 key_event.event_string = CORBA_string_dup ("Page_Down");
826 key_event.event_string = CORBA_string_dup ("Page_Up");
829 key_event.event_string = CORBA_string_dup ("F1");
832 key_event.event_string = CORBA_string_dup ("F2");
835 key_event.event_string = CORBA_string_dup ("F3");
838 key_event.event_string = CORBA_string_dup ("F4");
841 key_event.event_string = CORBA_string_dup ("F5");
844 key_event.event_string = CORBA_string_dup ("F6");
847 key_event.event_string = CORBA_string_dup ("F7");
850 key_event.event_string = CORBA_string_dup ("F8");
853 key_event.event_string = CORBA_string_dup ("F9");
856 key_event.event_string = CORBA_string_dup ("F10");
859 key_event.event_string = CORBA_string_dup ("F11");
862 key_event.event_string = CORBA_string_dup ("F12");
865 key_event.event_string = CORBA_string_dup ("End");
868 key_event.event_string = CORBA_string_dup ("Escape");
871 key_event.event_string = CORBA_string_dup ("Up");
874 key_event.event_string = CORBA_string_dup ("Down");
877 key_event.event_string = CORBA_string_dup ("Left");
880 key_event.event_string = CORBA_string_dup ("Right");
883 if (XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL) > 0)
885 key_event.event_string = CORBA_string_dup (cbuf);
886 if (isgraph (keysym))
888 key_event.is_text = CORBA_TRUE; /* FIXME: incorrect for some composed chars? */
893 key_event.event_string = CORBA_string_dup ("");
897 key_event.timestamp = (CORBA_unsigned_long) x_key_event->time;
898 #ifdef SPI_KEYEVENT_DEBUG
900 "Key %lu pressed (%c), modifiers %d\n",
901 (unsigned long) keysym,
902 keysym ? (int) keysym : '*',
903 (int) x_key_event->state);
906 fprintf (stderr, "%s%c",
907 (x_key_event->state & Mod1Mask)?"Alt-":"",
908 ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
909 g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
910 #endif /* SPI_DEBUG */
915 spi_controller_update_key_grabs (SpiDEController *controller,
916 Accessibility_DeviceEvent *recv)
919 gboolean update_failed = FALSE;
921 g_return_val_if_fail (controller != NULL, FALSE);
924 * masks known to work with default RH 7.1+:
925 * 0 (no mods), LockMask, Mod1Mask, Mod2Mask, ShiftMask,
926 * ShiftMask|LockMask, Mod1Mask|LockMask, Mod2Mask|LockMask,
927 * ShiftMask|Mod1Mask, ShiftMask|Mod2Mask, Mod1Mask|Mod2Mask,
928 * ShiftMask|LockMask|Mod1Mask, ShiftMask|LockMask|Mod2Mask,
930 * ControlMask grabs are broken, must be in use already
932 for (l = controller->keygrabs_list; l; l = next)
935 gboolean re_issue_grab;
936 DEControllerGrabMask *grab_mask = l->data;
940 re_issue_grab = recv &&
941 /* (recv->type == Accessibility_KEY_RELEASED) && - (?) */
942 (recv->modifiers & grab_mask->mod_mask) &&
943 (grab_mask->key_val == keycode_for_keysym (recv->id));
946 fprintf (stderr, "mask=%lx %lx (%c%c) %s\n",
947 (long int) grab_mask->key_val,
948 (long int) grab_mask->mod_mask,
949 grab_mask->pending_add ? '+' : '.',
950 grab_mask->pending_remove ? '-' : '.',
951 re_issue_grab ? "re-issue": "");
956 if (grab_mask->pending_add && grab_mask->pending_remove)
960 else if (grab_mask->pending_remove)
963 fprintf (stderr, "ungrabbing, mask=%x\n", grab_mask->mod_mask);
965 XUngrabKey (spi_get_display (),
968 gdk_x11_get_default_root_xwindow ());
972 else if (grab_mask->pending_add || re_issue_grab)
976 fprintf (stderr, "grab with mask %x\n", grab_mask->mod_mask);
978 XGrabKey (spi_get_display (),
981 gdk_x11_get_default_root_xwindow (),
985 XSync (spi_get_display (), False);
986 update_failed = spi_clear_error_state ();
988 while (grab_mask->ref_count > 0) --grab_mask->ref_count;
993 grab_mask->pending_add = FALSE;
994 grab_mask->pending_remove = FALSE;
998 g_assert (grab_mask->ref_count <= 0);
1000 controller->keygrabs_list = g_list_delete_link (
1001 controller->keygrabs_list, l);
1003 spi_grab_mask_free (grab_mask);
1008 return ! update_failed;
1012 * Implemented GObject::finalize
1015 spi_device_event_controller_object_finalize (GObject *object)
1017 SpiDEController *controller;
1019 controller = SPI_DEVICE_EVENT_CONTROLLER (object);
1022 fprintf(stderr, "spi_device_event_controller_object_finalize called\n");
1024 /* disconnect any special listeners, get rid of outstanding keygrabs */
1025 XUngrabKey (spi_get_display (), AnyKey, AnyModifier, DefaultRootWindow (spi_get_display ()));
1027 spi_device_event_controller_parent_class->finalize (object);
1031 * CORBA Accessibility::DEController::registerKeystrokeListener
1032 * method implementation
1034 static CORBA_boolean
1035 impl_register_keystroke_listener (PortableServer_Servant servant,
1036 const Accessibility_DeviceEventListener l,
1037 const Accessibility_KeySet *keys,
1038 const Accessibility_ControllerEventMask mask,
1039 const Accessibility_KeyEventTypeSeq *type,
1040 const Accessibility_EventListenerMode *mode,
1041 CORBA_Environment *ev)
1043 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1044 bonobo_object_from_servant (servant));
1045 DEControllerKeyListener *dec_listener;
1047 fprintf (stderr, "registering keystroke listener %p with maskVal %lu\n",
1048 (void *) l, (unsigned long) mask);
1050 dec_listener = spi_dec_key_listener_new (l, keys, mask, type, mode, ev);
1051 return spi_controller_register_device_listener (
1052 controller, (DEControllerListener *) dec_listener, ev);
1057 CORBA_Environment *ev;
1058 DEControllerKeyListener *key_listener;
1059 } RemoveKeyListenerClosure;
1061 static SpiReEntrantContinue
1062 remove_key_listener_cb (GList * const *list,
1065 DEControllerKeyListener *key_listener = (*list)->data;
1066 RemoveKeyListenerClosure *ctx = user_data;
1068 if (CORBA_Object_is_equivalent (ctx->key_listener->listener.object,
1069 key_listener->listener.object, ctx->ev))
1071 spi_re_entrant_list_delete_link (list);
1072 spi_dec_key_listener_free (key_listener, ctx->ev);
1075 return SPI_RE_ENTRANT_CONTINUE;
1078 static SpiReEntrantContinue
1079 copy_key_listener_cb (GList * const *list,
1082 DEControllerKeyListener *key_listener = (*list)->data;
1083 RemoveKeyListenerClosure *ctx = user_data;
1085 if (CORBA_Object_is_equivalent (ctx->key_listener->listener.object,
1086 key_listener->listener.object, ctx->ev))
1088 /* TODO: FIXME aggregate keys in case the listener is registered twice */
1089 CORBA_free (ctx->key_listener->keys);
1090 ctx->key_listener->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
1093 return SPI_RE_ENTRANT_CONTINUE;
1098 spi_deregister_controller_key_listener (SpiDEController *controller,
1099 DEControllerKeyListener *key_listener,
1100 CORBA_Environment *ev)
1102 RemoveKeyListenerClosure ctx;
1105 ctx.key_listener = key_listener;
1107 /* special case, copy keyset from existing controller list entry */
1108 if (key_listener->keys->_length == 0)
1110 spi_re_entrant_list_foreach (&controller->key_listeners,
1111 copy_key_listener_cb, &ctx);
1114 spi_controller_deregister_global_keygrabs (controller, key_listener);
1116 spi_re_entrant_list_foreach (&controller->key_listeners,
1117 remove_key_listener_cb, &ctx);
1122 * CORBA Accessibility::DEController::deregisterKeystrokeListener
1123 * method implementation
1126 impl_deregister_keystroke_listener (PortableServer_Servant servant,
1127 const Accessibility_DeviceEventListener l,
1128 const Accessibility_KeySet *keys,
1129 const Accessibility_ControllerEventMask mask,
1130 const Accessibility_KeyEventTypeSeq *type,
1131 CORBA_Environment *ev)
1133 DEControllerKeyListener *key_listener;
1134 SpiDEController *controller;
1136 controller = SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
1138 key_listener = spi_dec_key_listener_new (l, keys, mask, type, NULL, ev);
1140 #ifdef SPI_DEREGISTER_DEBUG
1141 fprintf (stderr, "deregistering keystroke listener %p with maskVal %lu\n",
1142 (void *) l, (unsigned long) mask->value);
1145 spi_deregister_controller_key_listener (controller, key_listener, ev);
1147 spi_dec_key_listener_free (key_listener, ev);
1151 * CORBA Accessibility::DEController::registerKeystrokeListener
1152 * method implementation
1155 impl_generate_keyboard_event (PortableServer_Servant servant,
1156 const CORBA_long keycode,
1157 const CORBA_char *keystring,
1158 const Accessibility_KeySynthType synth_type,
1159 CORBA_Environment *ev)
1161 long key_synth_code;
1164 fprintf (stderr, "synthesizing keystroke %ld, type %d\n",
1165 (long) keycode, (int) synth_type);
1167 /* TODO: hide/wrap/remove X dependency */
1169 /* TODO: be accessX-savvy so that keyrelease occurs after sufficient timeout */
1172 * TODO: when initializing, query for XTest extension before using,
1173 * and fall back to XSendEvent() if XTest is not available.
1176 /* TODO: implement keystring mode also */
1177 gdk_error_trap_push ();
1181 case Accessibility_KEY_PRESS:
1182 XTestFakeKeyEvent (spi_get_display (), (unsigned int) keycode, True, CurrentTime);
1184 case Accessibility_KEY_PRESSRELEASE:
1185 XTestFakeKeyEvent (spi_get_display (), (unsigned int) keycode, True, CurrentTime);
1186 case Accessibility_KEY_RELEASE:
1187 XTestFakeKeyEvent (spi_get_display (), (unsigned int) keycode, False, CurrentTime);
1189 case Accessibility_KEY_SYM:
1190 key_synth_code = keycode_for_keysym (keycode);
1191 XTestFakeKeyEvent (spi_get_display (), (unsigned int) key_synth_code, True, CurrentTime);
1192 XTestFakeKeyEvent (spi_get_display (), (unsigned int) key_synth_code, False, CurrentTime);
1194 case Accessibility_KEY_STRING:
1195 fprintf (stderr, "Not yet implemented\n");
1198 if (gdk_error_trap_pop ())
1200 g_warning ("Error emitting keystroke");
1204 /* Accessibility::DEController::generateMouseEvent */
1206 impl_generate_mouse_event (PortableServer_Servant servant,
1209 const CORBA_char *eventName,
1210 CORBA_Environment *ev)
1213 gboolean error = FALSE;
1214 Display *display = spi_get_display ();
1216 fprintf (stderr, "generating mouse %s event at %ld, %ld\n",
1217 eventName, (long int) x, (long int) y);
1219 g_message ("mouse event synthesis\n");
1220 switch (eventName[0])
1223 switch (eventName[1])
1225 /* TODO: check number of buttons before parsing */
1240 if (x != -1 && y != -1)
1242 XTestFakeMotionEvent (display, DefaultScreen (display),
1245 XTestFakeButtonEvent (display, button, !(eventName[2] == 'r'), 0);
1246 if (eventName[2] == 'c')
1247 XTestFakeButtonEvent (display, button, FALSE, 1);
1248 else if (eventName[2] == 'd')
1250 XTestFakeButtonEvent (display, button, FALSE, 1);
1251 XTestFakeButtonEvent (display, button, TRUE, 2);
1252 XTestFakeButtonEvent (display, button, FALSE, 3);
1256 case 'r': /* relative motion */
1257 XTestFakeRelativeMotionEvent (display, x, y, 0);
1259 case 'a': /* absolute motion */
1260 XTestFakeMotionEvent (display, DefaultScreen (display),
1266 /* Accessibility::DEController::notifyListenersSync */
1267 static CORBA_boolean
1268 impl_notify_listeners_sync (PortableServer_Servant servant,
1269 const Accessibility_DeviceEvent *event,
1270 CORBA_Environment *ev)
1272 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1273 bonobo_object_from_servant (servant));
1275 g_print ("notifylistening listeners synchronously: controller %p, event id %d\n",
1276 controller, (int) event->id);
1278 return spi_controller_notify_keylisteners (controller, event, CORBA_FALSE, ev) ?
1279 CORBA_TRUE : CORBA_FALSE;
1282 /* Accessibility::DEController::notifyListenersAsync */
1284 impl_notify_listeners_async (PortableServer_Servant servant,
1285 const Accessibility_DeviceEvent *event,
1286 CORBA_Environment *ev)
1288 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1289 bonobo_object_from_servant (servant));
1291 fprintf (stderr, "notifying listeners asynchronously\n");
1293 spi_controller_notify_keylisteners (controller, event, CORBA_FALSE, ev);
1297 spi_device_event_controller_class_init (SpiDEControllerClass *klass)
1299 GObjectClass * object_class = (GObjectClass *) klass;
1300 POA_Accessibility_DeviceEventController__epv *epv = &klass->epv;
1302 spi_device_event_controller_parent_class = g_type_class_peek_parent (klass);
1304 object_class->finalize = spi_device_event_controller_object_finalize;
1306 epv->registerKeystrokeListener = impl_register_keystroke_listener;
1307 epv->deregisterKeystrokeListener = impl_deregister_keystroke_listener;
1308 epv->generateKeyboardEvent = impl_generate_keyboard_event;
1309 epv->generateMouseEvent = impl_generate_mouse_event;
1310 epv->notifyListenersSync = impl_notify_listeners_sync;
1311 epv->notifyListenersAsync = impl_notify_listeners_async;
1315 spi_device_event_controller_init (SpiDEController *device_event_controller)
1317 device_event_controller->key_listeners = NULL;
1318 device_event_controller->mouse_listeners = NULL;
1319 device_event_controller->keygrabs_list = NULL;
1322 * TODO: fixme, this module makes the foolish assumptions that
1323 * registryd uses the same display as the apps, and that the
1324 * DISPLAY environment variable is set.
1326 gdk_init (NULL, NULL);
1328 spi_controller_register_with_devices (device_event_controller);
1332 spi_device_event_controller_forward_key_event (SpiDEController *controller,
1333 const XEvent *event)
1335 gboolean is_consumed = FALSE;
1336 CORBA_Environment ev;
1337 Accessibility_DeviceEvent key_event;
1339 g_assert (event->type == KeyPress || event->type == KeyRelease);
1341 CORBA_exception_init (&ev);
1343 key_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1345 spi_controller_update_key_grabs (controller, &key_event);
1347 /* relay to listeners, and decide whether to consume it or not */
1348 is_consumed = spi_controller_notify_keylisteners (
1349 controller, &key_event, CORBA_TRUE, &ev);
1351 CORBA_exception_free (&ev);
1355 XAllowEvents (spi_get_display (), AsyncKeyboard, CurrentTime);
1359 XAllowEvents (spi_get_display (), ReplayKeyboard, CurrentTime);
1364 spi_device_event_controller_new (SpiRegistry *registry)
1366 SpiDEController *retval = g_object_new (
1367 SPI_DEVICE_EVENT_CONTROLLER_TYPE, NULL);
1369 retval->registry = SPI_REGISTRY (bonobo_object_ref (
1370 BONOBO_OBJECT (registry)));
1372 spi_dec_init_mouse_listener (registry);
1373 /* TODO: kill mouse listener on finalize */
1378 BONOBO_TYPE_FUNC_FULL (SpiDEController,
1379 Accessibility_DeviceEventController,
1381 spi_device_event_controller);