2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001 Sun Microsystems Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 /* deviceeventcontroler.c: implement the DeviceEventController interface */
28 #undef SPI_KEYEVENT_DEBUG
33 #include <bonobo/bonobo-exception.h>
36 #include <X11/extensions/XTest.h>
38 #include <X11/keysymdef.h>
40 #include <gdk/gdkx.h> /* TODO: hide dependency (wrap in single porting file) */
41 #include <gdk/gdkkeysyms.h>
42 #include <gdk/gdkwindow.h>
44 #include "../libspi/spi-private.h"
45 #include "deviceeventcontroller.h"
47 /* Our parent Gtk object type */
48 #define PARENT_TYPE BONOBO_TYPE_OBJECT
50 /* A pointer to our parent object class */
51 static GObjectClass *spi_device_event_controller_parent_class;
52 static int spi_error_code = 0;
54 int (*x_default_error_handler) (Display *display, XErrorEvent *error_event);
58 SPI_DEVICE_TYPE_MOUSE,
59 SPI_DEVICE_TYPE_LAST_DEFINED
60 } SpiDeviceTypeCategory;
64 guint pending_add : 1;
65 guint pending_remove : 1;
67 Accessibility_ControllerEventMask mod_mask;
68 CORBA_unsigned_long key_val; /* KeyCode */
69 } DEControllerGrabMask;
73 SpiDeviceTypeCategory type;
74 } DEControllerListener;
77 DEControllerListener listener;
79 Accessibility_KeySet *keys;
80 Accessibility_ControllerEventMask mask;
81 Accessibility_KeyEventTypeSeq *typeseq;
82 Accessibility_EventListenerMode *mode;
83 } DEControllerKeyListener;
85 static void spi_controller_register_with_devices (SpiDEController *controller);
86 static gboolean spi_controller_update_key_grabs (SpiDEController *controller,
87 Accessibility_DeviceEvent *recv);
88 static gboolean spi_controller_register_device_listener (SpiDEController *controller,
89 DEControllerListener *l,
90 CORBA_Environment *ev);
91 static void spi_device_event_controller_forward_key_event (SpiDEController *controller,
93 static gboolean spi_clear_error_state (void);
95 #define spi_get_display() GDK_DISPLAY()
100 keycode_for_keysym (long keysym)
102 return XKeysymToKeycode (spi_get_display (), (KeySym) keysym);
105 static DEControllerGrabMask *
106 spi_grab_mask_clone (DEControllerGrabMask *grab_mask)
108 DEControllerGrabMask *clone = g_new (DEControllerGrabMask, 1);
110 memcpy (clone, grab_mask, sizeof (DEControllerGrabMask));
112 clone->ref_count = 1;
113 clone->pending_add = TRUE;
114 clone->pending_remove = FALSE;
120 spi_grab_mask_free (DEControllerGrabMask *grab_mask)
126 spi_grab_mask_compare_values (gconstpointer p1, gconstpointer p2)
128 DEControllerGrabMask *l1 = (DEControllerGrabMask *) p1;
129 DEControllerGrabMask *l2 = (DEControllerGrabMask *) p2;
137 return ((l1->mod_mask != l2->mod_mask) || (l1->key_val != l2->key_val));
141 static DEControllerKeyListener *
142 spi_dec_key_listener_new (CORBA_Object l,
143 const Accessibility_KeySet *keys,
144 const Accessibility_ControllerEventMask mask,
145 const Accessibility_KeyEventTypeSeq *typeseq,
146 const Accessibility_EventListenerMode *mode,
147 CORBA_Environment *ev)
149 DEControllerKeyListener *key_listener = g_new0 (DEControllerKeyListener, 1);
150 key_listener->listener.object = bonobo_object_dup_ref (l, ev);
151 key_listener->listener.type = SPI_DEVICE_TYPE_KBD;
152 key_listener->keys = ORBit_copy_value (keys, TC_Accessibility_KeySet);
153 key_listener->mask = mask;
154 key_listener->typeseq = ORBit_copy_value (typeseq, TC_Accessibility_KeyEventTypeSeq);
156 key_listener->mode = ORBit_copy_value (mode, TC_Accessibility_EventListenerMode);
158 key_listener->mode = NULL;
161 g_print ("new listener, with mask %x, is_global %d, keys %p (%d)\n",
162 (unsigned int) key_listener->mask,
163 (int) (mode ? mode->global : 0),
164 (void *) key_listener->keys,
165 (int) (key_listener->keys ? key_listener->keys->_length : 0));
172 spi_dec_key_listener_free (DEControllerKeyListener *key_listener,
173 CORBA_Environment *ev)
175 bonobo_object_release_unref (key_listener->listener.object, ev);
176 CORBA_free (key_listener->typeseq);
177 CORBA_free (key_listener->keys);
178 g_free (key_listener);
182 _register_keygrab (SpiDEController *controller,
183 DEControllerGrabMask *grab_mask)
187 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
188 spi_grab_mask_compare_values);
191 DEControllerGrabMask *cur_mask = l->data;
193 cur_mask->ref_count++;
194 if (cur_mask->pending_remove)
196 cur_mask->pending_remove = FALSE;
201 controller->keygrabs_list =
202 g_list_prepend (controller->keygrabs_list,
203 spi_grab_mask_clone (grab_mask));
208 _deregister_keygrab (SpiDEController *controller,
209 DEControllerGrabMask *grab_mask)
213 l = g_list_find_custom (controller->keygrabs_list, grab_mask,
214 spi_grab_mask_compare_values);
218 DEControllerGrabMask *cur_mask = l->data;
220 cur_mask->ref_count--;
221 if (cur_mask->ref_count <= 0)
223 cur_mask->pending_remove = TRUE;
228 g_warning ("De-registering non-existant grab");
233 handle_keygrab (SpiDEController *controller,
234 DEControllerKeyListener *key_listener,
235 void (*process_cb) (SpiDEController *controller,
236 DEControllerGrabMask *grab_mask))
238 DEControllerGrabMask grab_mask = { 0 };
240 grab_mask.mod_mask = key_listener->mask;
241 if (key_listener->keys->_length == 0) /* special case means AnyKey/AllKeys */
243 grab_mask.key_val = AnyKey;
245 fprintf (stderr, "AnyKey grab!"); */
247 process_cb (controller, &grab_mask);
253 for (i = 0; i < key_listener->keys->_length; ++i)
255 Accessibility_KeyDefinition keydef = key_listener->keys->_buffer[i];
256 long int key_val = keydef.keysym;
257 /* X Grabs require keycodes, not keysyms */
258 if (keydef.keystring && keydef.keystring[0])
260 key_val = XStringToKeysym(keydef.keystring);
264 key_val = XKeysymToKeycode (spi_get_display (), (KeySym) key_val);
268 key_val = keydef.keycode;
270 grab_mask.key_val = key_val;
271 process_cb (controller, &grab_mask);
277 spi_controller_register_global_keygrabs (SpiDEController *controller,
278 DEControllerKeyListener *key_listener)
280 handle_keygrab (controller, key_listener, _register_keygrab);
281 return spi_controller_update_key_grabs (controller, NULL);
285 spi_controller_deregister_global_keygrabs (SpiDEController *controller,
286 DEControllerKeyListener *key_listener)
288 handle_keygrab (controller, key_listener, _deregister_keygrab);
289 spi_controller_update_key_grabs (controller, NULL);
293 spi_controller_register_device_listener (SpiDEController *controller,
294 DEControllerListener *listener,
295 CORBA_Environment *ev)
297 DEControllerKeyListener *key_listener;
299 switch (listener->type) {
300 case SPI_DEVICE_TYPE_KBD:
301 key_listener = (DEControllerKeyListener *) listener;
303 controller->key_listeners = g_list_prepend (controller->key_listeners,
305 if (key_listener->mode->global)
307 return spi_controller_register_global_keygrabs (controller, key_listener);
318 static GdkFilterReturn
319 global_filter_fn (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
321 XEvent *xevent = gdk_xevent;
322 SpiDEController *controller;
324 if (xevent->type != KeyPress && xevent->type != KeyRelease)
326 return GDK_FILTER_CONTINUE;
329 controller = SPI_DEVICE_EVENT_CONTROLLER (data);
331 spi_device_event_controller_forward_key_event (controller, xevent);
333 /* FIXME: is this right ? */
334 return GDK_FILTER_CONTINUE;
338 _spi_controller_device_error_handler (Display *display, XErrorEvent *error)
340 if (error->error_code == BadAccess)
342 g_message ("Could not complete key grab: grab already in use.\n");
343 spi_error_code = BadAccess;
348 return (*x_default_error_handler) (display, error);
353 spi_controller_register_with_devices (SpiDEController *controller)
355 /* calls to device-specific implementations and routines go here */
356 /* register with: keyboard hardware code handler */
357 /* register with: (translated) keystroke handler */
359 gdk_window_add_filter (NULL, global_filter_fn, controller);
361 gdk_window_set_events (gdk_get_default_root_window (),
362 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
364 x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
367 #define SPI_KEYEVENT_DEBUG
369 spi_key_set_contains_key (Accessibility_KeySet *key_set,
370 const Accessibility_DeviceEvent *key_event)
377 g_print ("null key set!");
381 len = key_set->_length;
383 if (len == 0) /* special case, means "all keys/any key" */
385 g_print ("anykey\n");
389 for (i = 0; i < len; ++i)
391 #ifdef SPI_KEYEVENT_DEBUG
392 g_print ("key_set[%d] = %d; key_event %d, code %d, string %s\n",
393 i, (int) key_set->_buffer[i].keycode,
394 (int) key_event->id, (int) key_event->hw_code,
395 key_event->event_string);
397 if (key_set->_buffer[i].keysym == (CORBA_long) key_event->id)
401 if (key_set->_buffer[i].keycode == (CORBA_long) key_event->hw_code)
405 if (key_event->event_string && key_event->event_string[0] &&
406 !strcmp (key_set->_buffer[i].keystring, key_event->event_string))
416 spi_key_eventtype_seq_contains_event (Accessibility_KeyEventTypeSeq *type_seq,
417 const Accessibility_DeviceEvent *key_event)
425 g_print ("null type seq!");
429 len = type_seq->_length;
431 if (len == 0) /* special case, means "all events/any event" */
436 for (i = 0; i < len; ++i)
439 g_print ("type_seq[%d] = %d; key event type = %d\n", i,
440 (int) type_seq->_buffer[i], (int) key_event->type);
442 if (type_seq->_buffer[i] == (CORBA_long) key_event->type)
452 spi_key_event_matches_listener (const Accessibility_DeviceEvent *key_event,
453 DEControllerKeyListener *listener,
454 CORBA_boolean is_system_global)
456 if ((key_event->modifiers == (CORBA_unsigned_short) (listener->mask & 0xFFFF)) &&
457 spi_key_set_contains_key (listener->keys, key_event) &&
458 spi_key_eventtype_seq_contains_event (listener->typeseq, key_event) &&
459 (is_system_global == listener->mode->global))
470 spi_notify_keylisteners (GList **key_listeners,
471 const Accessibility_DeviceEvent *key_event,
472 CORBA_boolean is_system_global,
473 CORBA_Environment *ev)
476 GSList *notify = NULL, *l2;
477 gboolean is_consumed;
484 for (l = *key_listeners; l; l = l->next)
486 DEControllerKeyListener *key_listener = l->data;
488 if (spi_key_event_matches_listener (key_event, key_listener, is_system_global))
490 Accessibility_DeviceEventListener ls = key_listener->listener.object;
492 if (ls != CORBA_OBJECT_NIL)
494 notify = g_slist_prepend (notify, CORBA_Object_duplicate (ls, ev));
499 #ifdef SPI_KEYEVENT_DEBUG
502 g_print ("no match for event\n");
507 for (l2 = notify; l2 && !is_consumed; l2 = l2->next)
509 Accessibility_DeviceEventListener ls = l2->data;
511 is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, key_event, ev);
516 CORBA_exception_free (ev);
519 CORBA_Object_release (ls, ev);
522 for (; l2; l2 = l2->next)
524 CORBA_Object_release (l2->data, ev);
527 g_slist_free (notify);
530 if (is_consumed) g_message ("consumed\n");
536 spi_clear_error_state ()
538 gboolean retval = spi_error_code != 0;
543 static Accessibility_DeviceEvent
544 spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
546 Accessibility_DeviceEvent key_event;
548 const int cbuf_bytes = 20;
549 char cbuf [cbuf_bytes];
551 keysym = XLookupKeysym (x_key_event, 0);
552 key_event.id = (CORBA_long)(keysym);
553 key_event.hw_code = (CORBA_short) x_key_event->keycode;
554 if (((XEvent *) x_key_event)->type == KeyPress)
556 key_event.type = Accessibility_KEY_PRESSED;
560 key_event.type = Accessibility_KEY_RELEASED;
562 key_event.modifiers = (CORBA_unsigned_short)(x_key_event->state);
563 key_event.is_text = CORBA_FALSE;
567 key_event.event_string = CORBA_string_dup ("space");
570 #ifdef SPI_KEYEVENT_DEBUG
571 fprintf(stderr, "Tab\n");
573 key_event.event_string = CORBA_string_dup ("Tab");
576 key_event.event_string = CORBA_string_dup ("Backspace");
579 key_event.event_string = CORBA_string_dup ("Return");
582 key_event.event_string = CORBA_string_dup ("Home");
585 key_event.event_string = CORBA_string_dup ("Page_Down");
588 key_event.event_string = CORBA_string_dup ("Page_Up");
591 key_event.event_string = CORBA_string_dup ("F1");
594 key_event.event_string = CORBA_string_dup ("F2");
597 key_event.event_string = CORBA_string_dup ("F3");
600 key_event.event_string = CORBA_string_dup ("F4");
603 key_event.event_string = CORBA_string_dup ("F5");
606 key_event.event_string = CORBA_string_dup ("F6");
609 key_event.event_string = CORBA_string_dup ("F7");
612 key_event.event_string = CORBA_string_dup ("F8");
615 key_event.event_string = CORBA_string_dup ("F9");
618 key_event.event_string = CORBA_string_dup ("F10");
621 key_event.event_string = CORBA_string_dup ("F11");
624 key_event.event_string = CORBA_string_dup ("F12");
627 key_event.event_string = CORBA_string_dup ("End");
630 key_event.event_string = CORBA_string_dup ("Escape");
633 key_event.event_string = CORBA_string_dup ("Up");
636 key_event.event_string = CORBA_string_dup ("Down");
639 key_event.event_string = CORBA_string_dup ("Left");
642 key_event.event_string = CORBA_string_dup ("Right");
645 if (XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL) > 0)
647 key_event.event_string = CORBA_string_dup (cbuf);
648 if (isgraph (keysym))
650 key_event.is_text = CORBA_TRUE; /* FIXME: incorrect for some composed chars? */
655 key_event.event_string = CORBA_string_dup ("");
659 key_event.timestamp = (CORBA_unsigned_long) x_key_event->time;
660 #ifdef SPI_KEYEVENT_DEBUG
662 "Key %lu pressed (%c), modifiers %d\n",
663 (unsigned long) keysym,
664 keysym ? (int) keysym : '*',
665 (int) x_key_event->state);
668 fprintf (stderr, "%s%c",
669 (x_key_event->state & Mod1Mask)?"Alt-":"",
670 ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
671 g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
672 #endif /* SPI_DEBUG */
677 spi_controller_update_key_grabs (SpiDEController *controller,
678 Accessibility_DeviceEvent *recv)
681 gboolean update_failed = FALSE;
683 g_return_val_if_fail (controller != NULL, FALSE);
686 * masks known to work with default RH 7.1+:
687 * 0 (no mods), LockMask, Mod1Mask, Mod2Mask, ShiftMask,
688 * ShiftMask|LockMask, Mod1Mask|LockMask, Mod2Mask|LockMask,
689 * ShiftMask|Mod1Mask, ShiftMask|Mod2Mask, Mod1Mask|Mod2Mask,
690 * ShiftMask|LockMask|Mod1Mask, ShiftMask|LockMask|Mod2Mask,
692 * ControlMask grabs are broken, must be in use already
694 for (l = controller->keygrabs_list; l; l = next)
697 gboolean re_issue_grab;
698 DEControllerGrabMask *grab_mask = l->data;
702 re_issue_grab = recv &&
703 /* (recv->type == Accessibility_KEY_RELEASED) && - (?) */
704 (recv->modifiers & grab_mask->mod_mask) &&
705 (grab_mask->key_val == keycode_for_keysym (recv->id));
708 fprintf (stderr, "mask=%lx %lx (%c%c) %s\n",
709 (long int) grab_mask->key_val,
710 (long int) grab_mask->mod_mask,
711 grab_mask->pending_add ? '+' : '.',
712 grab_mask->pending_remove ? '-' : '.',
713 re_issue_grab ? "re-issue": "");
718 if (grab_mask->pending_add && grab_mask->pending_remove)
722 else if (grab_mask->pending_remove)
725 fprintf (stderr, "ungrabbing, mask=%x\n", grab_mask->mod_mask);
727 XUngrabKey (spi_get_display (),
730 gdk_x11_get_default_root_xwindow ());
734 else if (grab_mask->pending_add || re_issue_grab)
738 fprintf (stderr, "grab with mask %x\n", grab_mask->mod_mask);
740 XGrabKey (spi_get_display (),
743 gdk_x11_get_default_root_xwindow (),
747 XSync (spi_get_display (), False);
748 update_failed = spi_clear_error_state ();
750 while (grab_mask->ref_count > 0) --grab_mask->ref_count;
755 grab_mask->pending_add = FALSE;
756 grab_mask->pending_remove = FALSE;
760 g_assert (grab_mask->ref_count <= 0);
762 controller->keygrabs_list = g_list_delete_link (
763 controller->keygrabs_list, l);
765 spi_grab_mask_free (grab_mask);
770 return ! update_failed;
774 * Implemented GObject::finalize
777 spi_device_event_controller_object_finalize (GObject *object)
779 SpiDEController *controller;
781 controller = SPI_DEVICE_EVENT_CONTROLLER (object);
784 fprintf(stderr, "spi_device_event_controller_object_finalize called\n");
786 /* disconnect any special listeners, get rid of outstanding keygrabs */
787 XUngrabKey (spi_get_display (), AnyKey, AnyModifier, DefaultRootWindow (spi_get_display ()));
789 spi_device_event_controller_parent_class->finalize (object);
793 * CORBA Accessibility::DEController::registerKeystrokeListener
794 * method implementation
797 impl_register_keystroke_listener (PortableServer_Servant servant,
798 const Accessibility_DeviceEventListener l,
799 const Accessibility_KeySet *keys,
800 const Accessibility_ControllerEventMask mask,
801 const Accessibility_KeyEventTypeSeq *type,
802 const Accessibility_EventListenerMode *mode,
803 CORBA_Environment *ev)
805 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
806 bonobo_object_from_servant (servant));
807 DEControllerKeyListener *dec_listener;
809 fprintf (stderr, "registering keystroke listener %p with maskVal %lu\n",
810 (void *) l, (unsigned long) mask);
812 dec_listener = spi_dec_key_listener_new (l, keys, mask, type, mode, ev);
813 return spi_controller_register_device_listener (
814 controller, (DEControllerListener *) dec_listener, ev);
819 CORBA_Environment *ev;
820 DEControllerKeyListener *key_listener;
821 } RemoveKeyListenerClosure;
823 static SpiReEntrantContinue
824 remove_key_listener_cb (GList * const *list,
827 DEControllerKeyListener *key_listener = (*list)->data;
828 RemoveKeyListenerClosure *ctx = user_data;
830 if (CORBA_Object_is_equivalent (ctx->key_listener->listener.object,
831 key_listener->listener.object, ctx->ev))
833 spi_re_entrant_list_delete_link (list);
834 spi_dec_key_listener_free (key_listener, ctx->ev);
837 return SPI_RE_ENTRANT_CONTINUE;
840 static SpiReEntrantContinue
841 copy_key_listener_cb (GList * const *list,
844 DEControllerKeyListener *key_listener = (*list)->data;
845 RemoveKeyListenerClosure *ctx = user_data;
847 if (CORBA_Object_is_equivalent (ctx->key_listener->listener.object,
848 key_listener->listener.object, ctx->ev))
850 /* TODO: FIXME aggregate keys in case the listener is registered twice */
851 CORBA_free (ctx->key_listener->keys);
852 ctx->key_listener->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
855 return SPI_RE_ENTRANT_CONTINUE;
859 * CORBA Accessibility::DEController::deregisterKeystrokeListener
860 * method implementation
863 impl_deregister_keystroke_listener (PortableServer_Servant servant,
864 const Accessibility_DeviceEventListener l,
865 const Accessibility_KeySet *keys,
866 const Accessibility_ControllerEventMask mask,
867 const Accessibility_KeyEventTypeSeq *type,
868 CORBA_Environment *ev)
870 DEControllerKeyListener *key_listener;
871 RemoveKeyListenerClosure ctx;
872 SpiDEController *controller;
874 controller = SPI_DEVICE_EVENT_CONTROLLER (bonobo_object (servant));
876 key_listener = spi_dec_key_listener_new (l, keys, mask, type, NULL, ev);
878 #ifdef SPI_DEREGISTER_DEBUG
879 fprintf (stderr, "deregistering keystroke listener %p with maskVal %lu\n",
880 (void *) l, (unsigned long) mask->value);
884 ctx.key_listener = key_listener;
886 /* special case, copy keyset from existing controller list entry */
887 if (keys->_length == 0)
889 spi_re_entrant_list_foreach (&controller->key_listeners,
890 copy_key_listener_cb, &ctx);
893 spi_controller_deregister_global_keygrabs (controller, key_listener);
895 spi_re_entrant_list_foreach (&controller->key_listeners,
896 remove_key_listener_cb, &ctx);
898 spi_dec_key_listener_free (key_listener, ev);
902 * CORBA Accessibility::DEController::registerKeystrokeListener
903 * method implementation
906 impl_generate_keyboard_event (PortableServer_Servant servant,
907 const CORBA_long keycode,
908 const CORBA_char *keystring,
909 const Accessibility_KeySynthType synth_type,
910 CORBA_Environment *ev)
915 fprintf (stderr, "synthesizing keystroke %ld, type %d\n",
916 (long) keycode, (int) synth_type);
918 /* TODO: hide/wrap/remove X dependency */
920 /* TODO: be accessX-savvy so that keyrelease occurs after sufficient timeout */
923 * TODO: when initializing, query for XTest extension before using,
924 * and fall back to XSendEvent() if XTest is not available.
927 /* TODO: implement keystring mode also */
928 gdk_error_trap_push ();
932 case Accessibility_KEY_PRESS:
933 XTestFakeKeyEvent (spi_get_display (), (unsigned int) keycode, True, CurrentTime);
935 case Accessibility_KEY_PRESSRELEASE:
936 XTestFakeKeyEvent (spi_get_display (), (unsigned int) keycode, True, CurrentTime);
937 case Accessibility_KEY_RELEASE:
938 XTestFakeKeyEvent (spi_get_display (), (unsigned int) keycode, False, CurrentTime);
940 case Accessibility_KEY_SYM:
941 key_synth_code = keycode_for_keysym (keycode);
942 XTestFakeKeyEvent (spi_get_display (), (unsigned int) key_synth_code, True, CurrentTime);
943 XTestFakeKeyEvent (spi_get_display (), (unsigned int) key_synth_code, False, CurrentTime);
945 case Accessibility_KEY_STRING:
946 fprintf (stderr, "Not yet implemented\n");
949 if (gdk_error_trap_pop ())
951 g_warning ("Error emitting keystroke");
955 /* Accessibility::DEController::generateMouseEvent */
957 impl_generate_mouse_event (PortableServer_Servant servant,
960 const CORBA_char *eventName,
961 CORBA_Environment *ev)
964 gboolean error = FALSE;
965 Display *display = spi_get_display ();
967 fprintf (stderr, "generating mouse %s event at %ld, %ld\n",
968 eventName, (long int) x, (long int) y);
970 g_message ("mouse event synthesis\n");
971 switch (eventName[0])
974 switch (eventName[1])
976 /* TODO: check number of buttons before parsing */
991 if (x != -1 && y != -1)
993 XTestFakeMotionEvent (display, DefaultScreen (display),
996 XTestFakeButtonEvent (display, button, !(eventName[2] == 'r'), 0);
997 if (eventName[2] == 'c')
998 XTestFakeButtonEvent (display, button, FALSE, 1);
999 else if (eventName[2] == 'd')
1001 XTestFakeButtonEvent (display, button, FALSE, 1);
1002 XTestFakeButtonEvent (display, button, TRUE, 2);
1003 XTestFakeButtonEvent (display, button, FALSE, 3);
1007 case 'r': /* relative motion */
1008 XTestFakeRelativeMotionEvent (display, x, y, 0);
1010 case 'a': /* absolute motion */
1011 XTestFakeMotionEvent (display, DefaultScreen (display),
1017 /* Accessibility::DEController::notifyListenersSync */
1018 static CORBA_boolean
1019 impl_notify_listeners_sync (PortableServer_Servant servant,
1020 const Accessibility_DeviceEvent *event,
1021 CORBA_Environment *ev)
1023 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1024 bonobo_object_from_servant (servant));
1026 g_print ("notifylistening listeners synchronously: controller %p, event id %d\n",
1027 controller, (int) event->id);
1029 return spi_notify_keylisteners (&controller->key_listeners, event, CORBA_FALSE, ev) ?
1030 CORBA_TRUE : CORBA_FALSE;
1033 /* Accessibility::DEController::notifyListenersAsync */
1035 impl_notify_listeners_async (PortableServer_Servant servant,
1036 const Accessibility_DeviceEvent *event,
1037 CORBA_Environment *ev)
1039 SpiDEController *controller = SPI_DEVICE_EVENT_CONTROLLER (
1040 bonobo_object_from_servant (servant));
1042 fprintf (stderr, "notifying listeners asynchronously\n");
1044 spi_notify_keylisteners (&controller->key_listeners, event, CORBA_FALSE, ev);
1048 spi_device_event_controller_class_init (SpiDEControllerClass *klass)
1050 GObjectClass * object_class = (GObjectClass *) klass;
1051 POA_Accessibility_DeviceEventController__epv *epv = &klass->epv;
1053 spi_device_event_controller_parent_class = g_type_class_peek_parent (klass);
1055 object_class->finalize = spi_device_event_controller_object_finalize;
1057 epv->registerKeystrokeListener = impl_register_keystroke_listener;
1058 epv->deregisterKeystrokeListener = impl_deregister_keystroke_listener;
1059 epv->generateKeyboardEvent = impl_generate_keyboard_event;
1060 epv->generateMouseEvent = impl_generate_mouse_event;
1061 epv->notifyListenersSync = impl_notify_listeners_sync;
1062 epv->notifyListenersAsync = impl_notify_listeners_async;
1066 spi_device_event_controller_init (SpiDEController *device_event_controller)
1068 device_event_controller->key_listeners = NULL;
1069 device_event_controller->mouse_listeners = NULL;
1070 device_event_controller->keygrabs_list = NULL;
1073 * TODO: fixme, this module makes the foolish assumptions that
1074 * registryd uses the same display as the apps, and that the
1075 * DISPLAY environment variable is set.
1077 gdk_init (NULL, NULL);
1079 spi_controller_register_with_devices (device_event_controller);
1083 spi_device_event_controller_forward_key_event (SpiDEController *controller,
1084 const XEvent *event)
1086 gboolean is_consumed = FALSE;
1087 CORBA_Environment ev;
1088 Accessibility_DeviceEvent key_event;
1090 g_assert (event->type == KeyPress || event->type == KeyRelease);
1092 CORBA_exception_init (&ev);
1094 key_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1096 spi_controller_update_key_grabs (controller, &key_event);
1098 /* relay to listeners, and decide whether to consume it or not */
1099 is_consumed = spi_notify_keylisteners (
1100 &controller->key_listeners, &key_event, CORBA_TRUE, &ev);
1102 CORBA_exception_free (&ev);
1106 XAllowEvents (spi_get_display (), AsyncKeyboard, CurrentTime);
1110 XAllowEvents (spi_get_display (), ReplayKeyboard, CurrentTime);
1115 spi_device_event_controller_new (SpiRegistry *registry)
1117 SpiDEController *retval = g_object_new (
1118 SPI_DEVICE_EVENT_CONTROLLER_TYPE, NULL);
1120 retval->registry = SPI_REGISTRY (bonobo_object_ref (
1121 BONOBO_OBJECT (registry)));
1126 BONOBO_TYPE_FUNC_FULL (SpiDEController,
1127 Accessibility_DeviceEventController,
1129 spi_device_event_controller);