+static DEControllerListener *
+spi_listener_clone (DEControllerListener *listener, CORBA_Environment *ev)
+{
+ DEControllerListener *clone = g_new0 (DEControllerListener, 1);
+ clone->object =
+ CORBA_Object_duplicate (listener->object, ev);
+ clone->type = listener->type;
+ clone->typeseq = ORBit_copy_value (listener->typeseq, TC_Accessibility_EventTypeSeq);
+ return clone;
+}
+
+static DEControllerKeyListener *
+spi_key_listener_clone (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
+{
+ DEControllerKeyListener *clone = g_new0 (DEControllerKeyListener, 1);
+ clone->listener.object =
+ CORBA_Object_duplicate (key_listener->listener.object, ev);
+ clone->listener.type = SPI_DEVICE_TYPE_KBD;
+ clone->keys = ORBit_copy_value (key_listener->keys, TC_Accessibility_KeySet);
+ clone->mask = key_listener->mask;
+ clone->listener.typeseq = ORBit_copy_value (key_listener->listener.typeseq, TC_Accessibility_EventTypeSeq);
+ if (key_listener->mode)
+ clone->mode = ORBit_copy_value (key_listener->mode, TC_Accessibility_EventListenerMode);
+ else
+ clone->mode = NULL;
+ return clone;
+}
+
+static void
+spi_key_listener_data_free (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
+{
+ CORBA_free (key_listener->listener.typeseq);
+ CORBA_free (key_listener->keys);
+ g_free (key_listener);
+}
+
+static void
+spi_key_listener_clone_free (DEControllerKeyListener *clone, CORBA_Environment *ev)
+{
+ CORBA_Object_release (clone->listener.object, ev);
+ spi_key_listener_data_free (clone, ev);
+}
+
+static void
+spi_listener_clone_free (DEControllerListener *clone, CORBA_Environment *ev)
+{
+ CORBA_Object_release (clone->object, ev);
+ CORBA_free (clone->typeseq);
+ g_free (clone);
+}
+
+static void
+spi_dec_listener_free (DEControllerListener *listener,
+ CORBA_Environment *ev)
+{
+ bonobo_object_release_unref (listener->object, ev);
+ if (listener->type == SPI_DEVICE_TYPE_KBD)
+ spi_key_listener_data_free ((DEControllerKeyListener *) listener, ev);
+}
+
+static void
+_register_keygrab (SpiDEController *controller,
+ DEControllerGrabMask *grab_mask)
+{
+ GList *l;
+
+ l = g_list_find_custom (controller->keygrabs_list, grab_mask,
+ spi_grab_mask_compare_values);
+ if (l)
+ {
+ DEControllerGrabMask *cur_mask = l->data;
+
+ cur_mask->ref_count++;
+ if (cur_mask->pending_remove)
+ {
+ cur_mask->pending_remove = FALSE;
+ }
+ }
+ else
+ {
+ controller->keygrabs_list =
+ g_list_prepend (controller->keygrabs_list,
+ spi_grab_mask_clone (grab_mask));
+ }
+}
+
+static void
+_deregister_keygrab (SpiDEController *controller,
+ DEControllerGrabMask *grab_mask)
+{
+ GList *l;
+
+ l = g_list_find_custom (controller->keygrabs_list, grab_mask,
+ spi_grab_mask_compare_values);
+
+ if (l)
+ {
+ DEControllerGrabMask *cur_mask = l->data;
+
+ cur_mask->ref_count--;
+ if (cur_mask->ref_count <= 0)
+ {
+ cur_mask->pending_remove = TRUE;
+ }
+ }
+ else
+ {
+ DBG (1, g_warning ("De-registering non-existant grab"));
+ }
+}
+
+static void
+handle_keygrab (SpiDEController *controller,
+ DEControllerKeyListener *key_listener,
+ void (*process_cb) (SpiDEController *controller,
+ DEControllerGrabMask *grab_mask))
+{
+ DEControllerGrabMask grab_mask = { 0 };
+
+ grab_mask.mod_mask = key_listener->mask;
+ if (key_listener->keys->_length == 0) /* special case means AnyKey/AllKeys */
+ {
+ grab_mask.key_val = AnyKey;
+#ifdef SPI_DEBUG
+ fprintf (stderr, "AnyKey grab!");
+#endif
+ process_cb (controller, &grab_mask);
+ }
+ else
+ {
+ int i;
+
+ for (i = 0; i < key_listener->keys->_length; ++i)
+ {
+ Accessibility_KeyDefinition keydef = key_listener->keys->_buffer[i];
+ long int key_val = keydef.keysym;
+ /* X Grabs require keycodes, not keysyms */
+ if (keydef.keystring && keydef.keystring[0])
+ {
+ key_val = XStringToKeysym(keydef.keystring);
+ }
+ if (key_val > 0)
+ {
+ key_val = XKeysymToKeycode (spi_get_display (), (KeySym) key_val);
+ }
+ else
+ {
+ key_val = keydef.keycode;
+ }
+ grab_mask.key_val = key_val;
+ process_cb (controller, &grab_mask);
+ }
+ }
+}
+
+static gboolean
+spi_controller_register_global_keygrabs (SpiDEController *controller,
+ DEControllerKeyListener *key_listener)
+{
+ handle_keygrab (controller, key_listener, _register_keygrab);
+ if (controller->xevie_display == NULL)
+ return spi_controller_update_key_grabs (controller, NULL);
+ else
+ return TRUE;
+}
+
+static void
+spi_controller_deregister_global_keygrabs (SpiDEController *controller,
+ DEControllerKeyListener *key_listener)
+{
+ handle_keygrab (controller, key_listener, _deregister_keygrab);
+ if (controller->xevie_display == NULL)
+ spi_controller_update_key_grabs (controller, NULL);
+}
+
+static gboolean
+spi_controller_register_device_listener (SpiDEController *controller,
+ DEControllerListener *listener,
+ CORBA_Environment *ev)
+{
+ DEControllerKeyListener *key_listener;
+
+ switch (listener->type) {
+ case SPI_DEVICE_TYPE_KBD:
+ key_listener = (DEControllerKeyListener *) listener;
+
+ controller->key_listeners = g_list_prepend (controller->key_listeners,
+ key_listener);
+ if (key_listener->mode->global)
+ {
+ return spi_controller_register_global_keygrabs (controller, key_listener);
+ }
+ else
+ return TRUE;
+ break;
+ case SPI_DEVICE_TYPE_MOUSE:
+ controller->mouse_listeners = g_list_prepend (controller->mouse_listeners, listener);
+ break;
+ default:
+ DBG (1, g_warning ("listener registration for unknown device type.\n"));
+ break;
+ }
+ return FALSE;
+}
+
+static gboolean
+spi_controller_notify_mouselisteners (SpiDEController *controller,
+ const Accessibility_DeviceEvent *event,
+ CORBA_Environment *ev)
+{
+ GList *l;
+ GSList *notify = NULL, *l2;
+ GList **listeners = &controller->mouse_listeners;
+ gboolean is_consumed;
+#ifdef SPI_KEYEVENT_DEBUG
+ gboolean found = FALSE;
+#endif
+ if (!listeners)
+ {
+ return FALSE;
+ }
+
+ for (l = *listeners; l; l = l->next)
+ {
+ DEControllerListener *listener = l->data;
+
+ if (spi_eventtype_seq_contains_event (listener->typeseq, event))
+ {
+ Accessibility_DeviceEventListener ls = listener->object;
+
+ if (ls != CORBA_OBJECT_NIL)
+ {
+ /* we clone (don't dup) the listener, to avoid refcount inc. */
+ notify = g_slist_prepend (notify,
+ spi_listener_clone (listener, ev));
+#ifdef SPI_KEYEVENT_DEBUG
+ found = TRUE;
+#endif
+ }
+ }
+ }
+
+#ifdef SPI_KEYEVENT_DEBUG
+ if (!found)
+ {
+ g_print ("no match for event\n");
+ }
+#endif
+
+ is_consumed = FALSE;
+ for (l2 = notify; l2 && !is_consumed; l2 = l2->next)
+ {
+ DEControllerListener *listener = l2->data;
+ Accessibility_DeviceEventListener ls = listener->object;
+
+ CORBA_exception_init (ev);
+ is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, event, ev);
+ if (BONOBO_EX (ev))
+ {
+ is_consumed = FALSE;
+ DBG (2, g_warning ("error notifying listener, removing it\n"));
+ spi_deregister_controller_device_listener (controller, listener,
+ ev);
+ CORBA_exception_free (ev);
+ }
+
+ spi_listener_clone_free ((DEControllerListener *) l2->data, ev);
+ }
+
+ for (; l2; l2 = l2->next)
+ {
+ DEControllerListener *listener = l2->data;
+ spi_listener_clone_free (listener, ev);
+ /* clone doesn't have its own ref, so don't use spi_device_listener_free */
+ }
+
+ g_slist_free (notify);
+
+#ifdef SPI_DEBUG
+ if (is_consumed) g_message ("consumed\n");
+#endif
+ return is_consumed;
+}
+
+static void
+spi_device_event_controller_forward_mouse_event (SpiDEController *controller,
+ XEvent *xevent)
+{
+ Accessibility_Event e;
+ Accessibility_DeviceEvent mouse_e;
+ CORBA_Environment ev;
+ gchar event_name[24];
+ gboolean is_consumed = FALSE;
+ gboolean xkb_mod_unlatch_occurred;
+ XButtonEvent *xbutton_event = (XButtonEvent *) xevent;
+
+ int button = xbutton_event->button;
+
+ unsigned int mouse_button_state = xbutton_event->state;
+
+ switch (button)
+ {
+ case 1:
+ mouse_button_state |= Button1Mask;
+ break;
+ case 2:
+ mouse_button_state |= Button2Mask;
+ break;
+ case 3:
+ mouse_button_state |= Button3Mask;
+ break;
+ case 4:
+ mouse_button_state |= Button4Mask;
+ break;
+ case 5:
+ mouse_button_state |= Button5Mask;
+ break;
+ }
+
+ last_mouse_pos->x = ((XButtonEvent *) xevent)->x_root;
+ last_mouse_pos->y = ((XButtonEvent *) xevent)->y_root;
+
+#ifdef SPI_DEBUG
+ fprintf (stderr, "mouse button %d %s (%x)\n",
+ xbutton_event->button,
+ (xevent->type == ButtonPress) ? "Press" : "Release",
+ mouse_button_state);
+#endif
+ snprintf (event_name, 22, "mouse:button:%d%c", button,
+ (xevent->type == ButtonPress) ? 'p' : 'r');
+
+ /* TODO: FIXME distinguish between physical and logical buttons */
+ mouse_e.type = (xevent->type == ButtonPress) ?
+ Accessibility_BUTTON_PRESSED_EVENT :
+ Accessibility_BUTTON_RELEASED_EVENT;
+ mouse_e.id = button;
+ mouse_e.hw_code = button;
+ mouse_e.modifiers = (CORBA_unsigned_short) xbutton_event->state;
+ mouse_e.timestamp = (CORBA_unsigned_long) xbutton_event->time;
+ mouse_e.event_string = "";
+ mouse_e.is_text = CORBA_FALSE;
+ if ((mouse_button_state & mouse_button_mask) !=
+ (mouse_mask_state & mouse_button_mask))
+ {
+ if ((mouse_mask_state & key_modifier_mask) !=
+ (mouse_button_state & key_modifier_mask))
+ spi_dec_emit_modifier_event (controller,
+ mouse_mask_state, mouse_button_state);
+ mouse_mask_state = mouse_button_state;
+ is_consumed =
+ spi_controller_notify_mouselisteners (controller, &mouse_e, &ev);
+ e.type = CORBA_string_dup (event_name);
+ e.source = BONOBO_OBJREF (controller->registry->desktop);
+ e.detail1 = last_mouse_pos->x;
+ e.detail2 = last_mouse_pos->y;
+ spi_init_any_nil (&e.any_data,
+ spi_accessible_new_return (atk_get_root (), FALSE, NULL),
+ Accessibility_ROLE_UNKNOWN,
+ "");
+ CORBA_exception_init (&ev);
+
+ Accessibility_Registry_notifyEvent (BONOBO_OBJREF (controller->registry),
+ &e,
+ &ev);
+ CORBA_free (e.any_data._value);
+ }
+
+ xkb_mod_unlatch_occurred = (xevent->type == ButtonPress ||
+ xevent->type == ButtonRelease);
+
+ /* if client wants to consume this event, and XKB latch state was
+ * unset by this button event, we reset it
+ */
+ if (is_consumed && xkb_mod_unlatch_occurred)
+ spi_dec_set_unlatch_pending (controller, mouse_mask_state);
+
+ XAllowEvents (spi_get_display (),
+ (is_consumed) ? SyncPointer : ReplayPointer,
+ CurrentTime);
+}
+
+static GdkFilterReturn
+global_filter_fn (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
+{
+ XEvent *xevent = gdk_xevent;
+ SpiDEController *controller;
+ DEControllerPrivateData *priv;
+ Display *display = spi_get_display ();
+ controller = SPI_DEVICE_EVENT_CONTROLLER (data);
+ priv = (DEControllerPrivateData *)
+ g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
+
+ if (xevent->type == MappingNotify)
+ xmkeymap = NULL;
+
+ if (xevent->type == KeyPress || xevent->type == KeyRelease)
+ {
+ if (controller->xevie_display == NULL)
+ {
+ gboolean is_consumed;
+
+ is_consumed =
+ spi_device_event_controller_forward_key_event (controller, xevent);
+
+ if (is_consumed)
+ {
+ int n_events;
+ int i;
+ XEvent next_event;
+ n_events = XPending (display);
+
+#ifdef SPI_KEYEVENT_DEBUG
+ g_print ("Number of events pending: %d\n", n_events);
+#endif
+ for (i = 0; i < n_events; i++)
+ {
+ XNextEvent (display, &next_event);
+ if (next_event.type != KeyPress &&
+ next_event.type != KeyRelease)
+ g_warning ("Unexpected event type %d in queue", next_event.type);
+ }
+
+ XAllowEvents (display, AsyncKeyboard, CurrentTime);
+ if (n_events)
+ XUngrabKeyboard (display, CurrentTime);
+ }
+ else
+ {
+ if (xevent->type == KeyPress)
+ wait_for_release_event (xevent, controller);
+ XAllowEvents (display, ReplayKeyboard, CurrentTime);
+ }
+ }
+
+ return GDK_FILTER_CONTINUE;
+ }
+ if (xevent->type == ButtonPress || xevent->type == ButtonRelease)
+ {
+ spi_device_event_controller_forward_mouse_event (controller, xevent);
+ }
+ if (xevent->type == priv->xkb_base_event_code)
+ {
+ XkbAnyEvent * xkb_ev = (XkbAnyEvent *) xevent;
+ /* ugly but probably necessary...*/
+ XSynchronize (display, TRUE);
+
+ if (xkb_ev->xkb_type == XkbStateNotify)
+ {
+ XkbStateNotifyEvent *xkb_snev =
+ (XkbStateNotifyEvent *) xkb_ev;
+ /* check the mouse, to catch mouse events grabbed by
+ * another client; in case we should revert this XKB delatch
+ */
+ if (!priv->pending_xkb_mod_relatch_mask)
+ {
+ int x, y;
+ gboolean moved;
+ spi_dec_mouse_check (controller, &x, &y, &moved);
+ }
+ /* we check again, since the previous call may have
+ changed this flag */
+ if (priv->pending_xkb_mod_relatch_mask)
+ {
+ unsigned int feedback_mask;
+#ifdef SPI_XKB_DEBUG
+ fprintf (stderr, "relatching %x\n",
+ priv->pending_xkb_mod_relatch_mask);
+#endif
+ /* temporarily turn off the latch bell, if it's on */
+ XkbGetControls (display,
+ XkbAccessXFeedbackMask,
+ priv->xkb_desc);
+ feedback_mask = priv->xkb_desc->ctrls->ax_options;
+ if (feedback_mask & XkbAX_StickyKeysFBMask)
+ {
+ XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
+ 0, False};
+ priv->xkb_desc->ctrls->ax_options
+ &= ~(XkbAX_StickyKeysFBMask);
+ XkbChangeControls (display, priv->xkb_desc, &changes);
+ }
+ /* TODO: account for lock as well as latch */
+ XkbLatchModifiers (display,
+ XkbUseCoreKbd,
+ priv->pending_xkb_mod_relatch_mask,
+ priv->pending_xkb_mod_relatch_mask);
+ if (feedback_mask & XkbAX_StickyKeysFBMask)
+ {
+ XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
+ 0, False};
+ priv->xkb_desc->ctrls->ax_options = feedback_mask;
+ XkbChangeControls (display, priv->xkb_desc, &changes);
+ }
+#ifdef SPI_XKB_DEBUG
+ fprintf (stderr, "relatched %x\n",
+ priv->pending_xkb_mod_relatch_mask);
+#endif
+ priv->pending_xkb_mod_relatch_mask = 0;
+ }
+ else
+ {
+ priv->xkb_latch_mask = xkb_snev->latched_mods;
+ }
+ }
+ else
+ DBG (2, g_warning ("XKB event %d\n", xkb_ev->xkb_type));
+ XSynchronize (display, FALSE);
+ }
+
+ return GDK_FILTER_CONTINUE;
+}
+
+static int
+_spi_controller_device_error_handler (Display *display, XErrorEvent *error)
+{
+ if (error->error_code == BadAccess)
+ {
+ g_message ("Could not complete key grab: grab already in use.\n");
+ spi_error_code = BadAccess;
+ return 0;
+ }
+ else
+ {
+ return (*x_default_error_handler) (display, error);
+ }
+}
+
+static void
+spi_controller_register_with_devices (SpiDEController *controller)
+{
+ DEControllerPrivateData *priv = (DEControllerPrivateData *)
+ g_object_get_qdata (G_OBJECT (controller), spi_dec_private_quark);
+ /* FIXME: should check for extension first! */
+ XTestGrabControl (spi_get_display (), True);
+
+ /* calls to device-specific implementations and routines go here */
+ /* register with: keyboard hardware code handler */
+ /* register with: (translated) keystroke handler */
+
+ priv->have_xkb = XkbQueryExtension (spi_get_display (),
+ &priv->xkb_major_extension_opcode,
+ &priv->xkb_base_event_code,
+ &priv->xkb_base_error_code, NULL, NULL);
+ if (priv->have_xkb)
+ {
+ gint i;
+ guint64 reserved = 0;
+ priv->xkb_desc = XkbGetMap (spi_get_display (), XkbKeySymsMask, XkbUseCoreKbd);
+ XkbSelectEvents (spi_get_display (),
+ XkbUseCoreKbd,
+ XkbStateNotifyMask, XkbStateNotifyMask);
+ _numlock_physical_mask = XkbKeysymToModifiers (spi_get_display (),
+ XK_Num_Lock);
+ for (i = priv->xkb_desc->max_key_code; i >= priv->xkb_desc->min_key_code; --i)
+ {
+ if (priv->xkb_desc->map->key_sym_map[i].kt_index[0] == XkbOneLevelIndex)
+ {
+ if (XKeycodeToKeysym (spi_get_display (), i, 0) != 0)
+ {
+ /* don't use this one if there's a grab client! */
+ gdk_error_trap_push ();
+ XGrabKey (spi_get_display (), i, 0,
+ gdk_x11_get_default_root_xwindow (),
+ TRUE,
+ GrabModeSync, GrabModeSync);
+ XSync (spi_get_display (), TRUE);
+ XUngrabKey (spi_get_display (), i, 0,
+ gdk_x11_get_default_root_xwindow ());
+ if (!gdk_error_trap_pop ())
+ {
+ reserved = i;
+ break;
+ }
+ }
+ }
+ }
+ if (reserved)
+ {
+ priv->reserved_keycode = reserved;
+ priv->reserved_keysym = XKeycodeToKeysym (spi_get_display (), reserved, 0);
+ }
+ else
+ {
+ priv->reserved_keycode = XKeysymToKeycode (spi_get_display (), XK_numbersign);
+ priv->reserved_keysym = XK_numbersign;
+ }
+#ifdef SPI_RESERVED_DEBUG
+ unsigned sym = 0;
+ sym = XKeycodeToKeysym (spi_get_display (), reserved, 0);
+ fprintf (stderr, "%x\n", sym);
+ fprintf (stderr, "setting the reserved keycode to %d (%s)\n",
+ reserved,
+ XKeysymToString (XKeycodeToKeysym (spi_get_display (),
+ reserved, 0)));
+#endif
+ }
+
+ gdk_window_add_filter (NULL, global_filter_fn, controller);
+
+ gdk_window_set_events (gdk_get_default_root_window (),
+ GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
+
+ x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
+}
+
+static gboolean
+spi_key_set_contains_key (Accessibility_KeySet *key_set,
+ const Accessibility_DeviceEvent *key_event)
+{
+ gint i;
+ gint len;
+
+ if (!key_set)
+ {
+ g_print ("null key set!");
+ return TRUE;
+ }
+
+ len = key_set->_length;
+
+ if (len == 0) /* special case, means "all keys/any key" */
+ {
+ g_print ("anykey\n");
+ return TRUE;
+ }
+
+ for (i = 0; i < len; ++i)
+ {
+#ifdef SPI_KEYEVENT_DEBUG
+ g_print ("key_set[%d] event = %d, code = %d; key_event %d, code %d, string %s\n",
+ i,
+ (int)key_set->_buffer[i].keysym,
+ (int) key_set->_buffer[i].keycode,
+ (int) key_event->id,
+ (int) key_event->hw_code,
+ key_event->event_string);
+#endif
+ if (key_set->_buffer[i].keysym == (CORBA_long) key_event->id)
+ {
+ return TRUE;
+ }
+ if (key_set->_buffer[i].keycode == (CORBA_long) key_event->hw_code)
+ {
+ return TRUE;
+ }
+ if (key_event->event_string && key_event->event_string[0] &&
+ !strcmp (key_set->_buffer[i].keystring, key_event->event_string))
+ {
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+static gboolean
+spi_eventtype_seq_contains_event (Accessibility_EventTypeSeq *type_seq,
+ const Accessibility_DeviceEvent *event)
+{
+ gint i;
+ gint len;
+
+
+ if (!type_seq)
+ {
+ g_print ("null type seq!");
+ return TRUE;
+ }
+
+ len = type_seq->_length;
+
+ if (len == 0) /* special case, means "all events/any event" */
+ {
+ return TRUE;
+ }
+
+ for (i = 0; i < len; ++i)
+ {
+#ifdef SPI_DEBUG
+ g_print ("type_seq[%d] = %d; event type = %d\n", i,
+ (int) type_seq->_buffer[i], (int) event->type);
+#endif
+ if (type_seq->_buffer[i] == (CORBA_long) event->type)
+ {
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+static gboolean
+spi_key_event_matches_listener (const Accessibility_DeviceEvent *key_event,
+ DEControllerKeyListener *listener,
+ CORBA_boolean is_system_global)
+{
+ if (((key_event->modifiers & 0xFF) == (CORBA_unsigned_short) (listener->mask & 0xFF)) &&
+ spi_key_set_contains_key (listener->keys, key_event) &&
+ spi_eventtype_seq_contains_event (listener->listener.typeseq, key_event) &&
+ (is_system_global == listener->mode->global))
+ {
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+static gboolean
+spi_controller_notify_keylisteners (SpiDEController *controller,
+ Accessibility_DeviceEvent *key_event,
+ CORBA_boolean is_system_global,
+ CORBA_Environment *ev)
+{
+ GList *l;
+ GSList *notify = NULL, *l2;
+ GList **key_listeners = &controller->key_listeners;
+ gboolean is_consumed;
+
+ if (!key_listeners)
+ {
+ return FALSE;
+ }
+
+ /* set the NUMLOCK event mask bit if appropriate: see bug #143702 */
+ if (key_event->modifiers & _numlock_physical_mask)
+ key_event->modifiers |= SPI_KEYMASK_NUMLOCK;
+
+ for (l = *key_listeners; l; l = l->next)
+ {
+ DEControllerKeyListener *key_listener = l->data;
+
+ if (spi_key_event_matches_listener (key_event, key_listener, is_system_global))
+ {
+ Accessibility_DeviceEventListener ls = key_listener->listener.object;
+
+ if (ls != CORBA_OBJECT_NIL)
+ {
+ /* we clone (don't dup) the listener, to avoid refcount inc. */
+ notify = g_slist_prepend (notify,
+ spi_key_listener_clone (key_listener, ev));
+ }
+ }
+ }
+
+#ifdef SPI_KEYEVENT_DEBUG
+ if (!notify)
+ {
+ g_print ("no match for event\n");
+ }
+#endif
+
+ is_consumed = FALSE;
+ for (l2 = notify; l2 && !is_consumed; l2 = l2->next)
+ {
+ DEControllerKeyListener *key_listener = l2->data;
+ Accessibility_DeviceEventListener ls = key_listener->listener.object;
+
+ is_consumed = Accessibility_DeviceEventListener_notifyEvent (ls, key_event, ev) &&
+ key_listener->mode->preemptive;
+
+ if (BONOBO_EX (ev))
+ {
+ is_consumed = FALSE;
+ spi_deregister_controller_key_listener (controller, key_listener,
+ ev);
+ CORBA_exception_free (ev);
+ }
+
+ spi_key_listener_clone_free (key_listener, ev);
+ }
+
+ for (; l2; l2 = l2->next)
+ {
+ DEControllerKeyListener *key_listener = l2->data;
+ spi_key_listener_clone_free (key_listener, ev);
+ /* clone doesn't have its own ref, so don't use spi_dec_listener_free */
+ }
+
+ g_slist_free (notify);
+
+#ifdef SPI_DEBUG
+ if (is_consumed) g_message ("consumed\n");
+#endif
+ return is_consumed;
+}
+
+static gboolean
+spi_clear_error_state (void)
+{
+ gboolean retval = spi_error_code != 0;
+ spi_error_code = 0;
+ return retval;
+}
+
+static Accessibility_DeviceEvent
+spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
+{
+ Accessibility_DeviceEvent key_event;
+ KeySym keysym;
+ const int cbuf_bytes = 20;
+ char cbuf [21];
+ int nbytes;
+
+ nbytes = XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL);
+ key_event.id = (CORBA_long)(keysym);
+ key_event.hw_code = (CORBA_short) x_key_event->keycode;
+ if (((XEvent *) x_key_event)->type == KeyPress)
+ {
+ key_event.type = Accessibility_KEY_PRESSED_EVENT;
+ }
+ else
+ {
+ key_event.type = Accessibility_KEY_RELEASED_EVENT;
+ }
+ key_event.modifiers = (CORBA_unsigned_short)(x_key_event->state);
+ key_event.is_text = CORBA_FALSE;
+ switch (keysym)
+ {
+ case ' ':
+ key_event.event_string = CORBA_string_dup ("space");
+ break;
+ case XK_Tab:
+ key_event.event_string = CORBA_string_dup ("Tab");
+ break;
+ case XK_BackSpace:
+ key_event.event_string = CORBA_string_dup ("Backspace");
+ break;
+ case XK_Return:
+ key_event.event_string = CORBA_string_dup ("Return");
+ break;
+ case XK_Home:
+ key_event.event_string = CORBA_string_dup ("Home");
+ break;
+ case XK_Page_Down:
+ key_event.event_string = CORBA_string_dup ("Page_Down");
+ break;
+ case XK_Page_Up:
+ key_event.event_string = CORBA_string_dup ("Page_Up");
+ break;
+ case XK_F1:
+ key_event.event_string = CORBA_string_dup ("F1");
+ break;
+ case XK_F2:
+ key_event.event_string = CORBA_string_dup ("F2");
+ break;
+ case XK_F3:
+ key_event.event_string = CORBA_string_dup ("F3");
+ break;
+ case XK_F4:
+ key_event.event_string = CORBA_string_dup ("F4");
+ break;
+ case XK_F5:
+ key_event.event_string = CORBA_string_dup ("F5");
+ break;
+ case XK_F6:
+ key_event.event_string = CORBA_string_dup ("F6");
+ break;
+ case XK_F7:
+ key_event.event_string = CORBA_string_dup ("F7");
+ break;
+ case XK_F8:
+ key_event.event_string = CORBA_string_dup ("F8");
+ break;
+ case XK_F9:
+ key_event.event_string = CORBA_string_dup ("F9");
+ break;
+ case XK_F10:
+ key_event.event_string = CORBA_string_dup ("F10");
+ break;
+ case XK_F11:
+ key_event.event_string = CORBA_string_dup ("F11");
+ break;
+ case XK_F12:
+ key_event.event_string = CORBA_string_dup ("F12");
+ break;
+ case XK_End:
+ key_event.event_string = CORBA_string_dup ("End");
+ break;
+ case XK_Escape:
+ key_event.event_string = CORBA_string_dup ("Escape");
+ break;
+ case XK_Up:
+ key_event.event_string = CORBA_string_dup ("Up");
+ break;
+ case XK_Down:
+ key_event.event_string = CORBA_string_dup ("Down");
+ break;
+ case XK_Left:
+ key_event.event_string = CORBA_string_dup ("Left");
+ break;
+ case XK_Right:
+ key_event.event_string = CORBA_string_dup ("Right");
+ break;
+ default:
+ if (nbytes > 0)
+ {
+ gunichar c;
+ cbuf[nbytes] = '\0'; /* OK since length is cbuf_bytes+1 */
+ key_event.event_string = CORBA_string_dup (cbuf);
+ c = keysym2ucs (keysym);
+ if (c > 0 && !g_unichar_iscntrl (c))
+ {
+ key_event.is_text = CORBA_TRUE;
+ /* incorrect for some composed chars? */
+ }
+ }
+ else
+ {
+ key_event.event_string = CORBA_string_dup ("");
+ }
+ }
+
+ key_event.timestamp = (CORBA_unsigned_long) x_key_event->time;
+#ifdef SPI_KEYEVENT_DEBUG
+ {
+ char *pressed_str = "pressed";
+ char *released_str = "released";
+ char *state_ptr;
+
+ if (key_event.type == Accessibility_KEY_PRESSED_EVENT)
+ state_ptr = pressed_str;
+ else
+ state_ptr = released_str;
+
+ fprintf (stderr,
+ "Key %lu %s (%c), modifiers %d; string=%s [%x] %s\n",
+ (unsigned long) keysym,
+ state_ptr,
+ keysym ? (int) keysym : '*',
+ (int) x_key_event->state,
+ key_event.event_string,
+ key_event.event_string[0],
+ (key_event.is_text == CORBA_TRUE) ? "(text)" : "(not text)");
+ }
+#endif
+#ifdef SPI_DEBUG
+ fprintf (stderr, "%s%c\n",
+ (x_key_event->state & Mod1Mask)?"Alt-":"",
+ ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
+ g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
+ fprintf (stderr, "serial: %x Time: %x\n", x_key_event->serial, x_key_event->time);
+#endif /* SPI_DEBUG */
+ return key_event;
+}
+
+static gboolean
+spi_controller_update_key_grabs (SpiDEController *controller,
+ Accessibility_DeviceEvent *recv)
+{
+ GList *l, *next;
+ gboolean update_failed = FALSE;
+ KeyCode keycode = 0;
+
+ g_return_val_if_fail (controller != NULL, FALSE);
+
+ /*
+ * masks known to work with default RH 7.1+:
+ * 0 (no mods), LockMask, Mod1Mask, Mod2Mask, ShiftMask,
+ * ShiftMask|LockMask, Mod1Mask|LockMask, Mod2Mask|LockMask,
+ * ShiftMask|Mod1Mask, ShiftMask|Mod2Mask, Mod1Mask|Mod2Mask,
+ * ShiftMask|LockMask|Mod1Mask, ShiftMask|LockMask|Mod2Mask,
+ *
+ * ControlMask grabs are broken, must be in use already
+ */
+ if (recv)
+ keycode = keycode_for_keysym (controller, recv->id, NULL);
+ for (l = controller->keygrabs_list; l; l = next)
+ {
+ gboolean do_remove;
+ gboolean re_issue_grab;
+ DEControllerGrabMask *grab_mask = l->data;
+
+ next = l->next;
+
+ re_issue_grab = recv &&
+ (recv->modifiers & grab_mask->mod_mask) &&
+ (grab_mask->key_val == keycode);
+
+#ifdef SPI_DEBUG
+ fprintf (stderr, "mask=%lx %lx (%c%c) %s\n",
+ (long int) grab_mask->key_val,
+ (long int) grab_mask->mod_mask,
+ grab_mask->pending_add ? '+' : '.',
+ grab_mask->pending_remove ? '-' : '.',
+ re_issue_grab ? "re-issue": "");
+#endif
+
+ do_remove = FALSE;
+
+ if (grab_mask->pending_add && grab_mask->pending_remove)
+ {
+ do_remove = TRUE;
+ }
+ else if (grab_mask->pending_remove)
+ {
+#ifdef SPI_DEBUG
+ fprintf (stderr, "ungrabbing, mask=%x\n", grab_mask->mod_mask);
+#endif
+ XUngrabKey (spi_get_display (),
+ grab_mask->key_val,
+ grab_mask->mod_mask,
+ gdk_x11_get_default_root_xwindow ());
+
+ do_remove = TRUE;
+ }
+ else if (grab_mask->pending_add || re_issue_grab)
+ {
+
+#ifdef SPI_DEBUG
+ fprintf (stderr, "grab %d with mask %x\n", grab_mask->key_val, grab_mask->mod_mask);
+#endif
+ XGrabKey (spi_get_display (),
+ grab_mask->key_val,
+ grab_mask->mod_mask,
+ gdk_x11_get_default_root_xwindow (),
+ True,
+ GrabModeSync,
+ GrabModeSync);
+ XSync (spi_get_display (), False);
+ update_failed = spi_clear_error_state ();
+ if (update_failed) {
+ while (grab_mask->ref_count > 0) --grab_mask->ref_count;
+ do_remove = TRUE;
+ }
+ }
+
+ grab_mask->pending_add = FALSE;
+ grab_mask->pending_remove = FALSE;
+
+ if (do_remove)
+ {
+ g_assert (grab_mask->ref_count <= 0);
+
+ controller->keygrabs_list = g_list_delete_link (
+ controller->keygrabs_list, l);
+
+ spi_grab_mask_free (grab_mask);
+ }
+
+ }
+
+ return ! update_failed;
+}
+
+/*
+ * Implemented GObject::finalize
+ */
+static void
+spi_device_event_controller_object_finalize (GObject *object)
+{
+ SpiDEController *controller;
+ DEControllerPrivateData *private;
+ controller = SPI_DEVICE_EVENT_CONTROLLER (object);
+
+#ifdef SPI_DEBUG
+ fprintf(stderr, "spi_device_event_controller_object_finalize called\n");
+#endif
+ /* disconnect any special listeners, get rid of outstanding keygrabs */
+ XUngrabKey (spi_get_display (), AnyKey, AnyModifier, DefaultRootWindow (spi_get_display ()));
+
+#ifdef HAVE_XEVIE
+ if (controller->xevie_display != NULL)
+ {
+ XevieEnd(controller->xevie_display);
+#ifdef SPI_KEYEVENT_DEBUG
+ printf("XevieEnd(dpy) finished \n");
+#endif
+ }
+#endif
+
+ private = g_object_get_data (G_OBJECT (controller), "spi-dec-private");
+ if (private->xkb_desc)
+ XkbFreeKeyboard (private->xkb_desc, 0, True);
+ g_free (private);
+ spi_device_event_controller_parent_class->finalize (object);
+}
+
+/*
+ * CORBA Accessibility::DEController::registerKeystrokeListener
+ * method implementation