+static GObjectClass *spi_device_event_controller_parent_class;
+
+static gboolean kbd_registered = FALSE;
+
+static Display *display;
+
+static Window root_window;
+
+typedef enum {
+ DEVICE_TYPE_KBD,
+ DEVICE_TYPE_MOUSE,
+ DEVICE_TYPE_LAST_DEFINED
+} DeviceTypeCategory;
+
+static gboolean _controller_register_with_devices (SpiDeviceEventController *controller);
+static gboolean _controller_grab_keyboard (SpiDeviceEventController *controller);
+
+static void _controller_register_device_listener (SpiDeviceEventController *controller,
+ const CORBA_Object l,
+ const Accessibility_ControllerEventMask *mask,
+ DeviceTypeCategory type,
+ CORBA_Environment *ev);
+
+/*
+ * Private methods
+ */
+
+static gint
+_compare_corba_objects (gconstpointer p1, gconstpointer p2)
+{
+ CORBA_Environment ev;
+ gint retval;
+ retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
+
+#ifdef SPI_DEBUG
+ fprintf (stderr, "comparing %p to %p; result %d\n",
+ p1, p2,
+ retval);
+#endif
+ return retval;
+}
+
+static gint
+_eventmask_compare_value (gconstpointer p1, gconstpointer p2)
+{
+ long d;
+ if (!p1 || !p2)
+ return (gint) (p1?1:(p2?-1:0));
+ else
+ d = ((long)((Accessibility_ControllerEventMask*)p2)->value) -
+ ((long)((Accessibility_ControllerEventMask*)p1)->value);
+ return (gint) d;
+}
+
+static void
+_controller_register_device_listener (SpiDeviceEventController *controller,
+ const CORBA_Object l,
+ const Accessibility_ControllerEventMask *mask,
+ DeviceTypeCategory type,
+ CORBA_Environment *ev)
+{
+ Accessibility_ControllerEventMask *mask_ptr = NULL;
+
+ switch (type) {
+ case DEVICE_TYPE_KBD:
+ controller->key_listeners = g_list_append (controller->key_listeners,
+ CORBA_Object_duplicate (l, ev));
+
+ mask_ptr = (Accessibility_ControllerEventMask *)
+ g_list_find_custom (controller->keymask_list, (gpointer) mask,
+ _eventmask_compare_value);
+ if (mask_ptr)
+ ++(mask_ptr->refcount);
+ else
+ {
+ if (mask->refcount != (CORBA_unsigned_short) 1)
+ fprintf (stderr, "mask initial refcount is not 1!\n");
+ if (mask->value > (CORBA_unsigned_long) 2048)
+ fprintf (stderr, "mask value looks invalid (%lu)\n",
+ (unsigned long) mask->value);
+ else
+ fprintf (stderr, "appending mask with val=%lu\n",
+ (unsigned long) mask->value);
+ mask_ptr = Accessibility_ControllerEventMask__alloc();
+ mask_ptr->value = mask->value;
+ mask_ptr->refcount = (CORBA_unsigned_short) 1;
+ controller->keymask_list = g_list_append (controller->keymask_list,
+ (gpointer) mask_ptr);
+ }
+ break;
+ case DEVICE_TYPE_MOUSE:
+/* controller->mouse_listeners = g_list_append (controller->mouse_listeners,
+ CORBA_Object_duplicate (l, ev));*/
+
+/* possibly this interface should NOT be used for mouse events ? */
+ break;
+ }
+}
+
+static void
+_controller_deregister_device_listener (SpiDeviceEventController *controller,
+ const CORBA_Object l,
+ const Accessibility_ControllerEventMask *mask,
+ DeviceTypeCategory type,
+ CORBA_Environment *ev)
+{
+ Accessibility_ControllerEventMask *mask_ptr;
+ GList *list_ptr;
+ switch (type) {
+ case DEVICE_TYPE_KBD:
+ list_ptr = g_list_find_custom (controller->key_listeners, l, _compare_corba_objects);
+ if (list_ptr)
+ controller->key_listeners = g_list_remove (controller->key_listeners, list_ptr);
+ list_ptr = (GList *)
+ g_list_find_custom (controller->keymask_list, (gpointer) mask,
+ _eventmask_compare_value);
+ if (list_ptr)
+ {
+ mask_ptr = (Accessibility_ControllerEventMask *) list_ptr->data;
+ if (mask_ptr)
+ --mask_ptr->refcount;
+ if (!mask_ptr->refcount)
+ {
+ controller->keymask_list =
+ g_list_remove (controller->keymask_list, mask_ptr);
+ ; /* TODO: release any key grabs that are in place for this key mask */
+ }
+ }
+ break;
+ case DEVICE_TYPE_MOUSE:
+/* controller->mouse_listeners = g_list_append (controller->mouse_listeners,
+ CORBA_Object_duplicate (l, ev));*/
+
+/* possibly this interface should NOT be used for mouse events ? */
+ break;
+ }
+}
+
+static gboolean
+_controller_register_with_devices (SpiDeviceEventController *controller)
+{
+ gboolean retval = FALSE;
+
+ /* calls to device-specific implementations and routines go here */
+ /* register with: keyboard hardware code handler */
+ /* register with: (translated) keystroke handler */
+#ifdef SPI_DEBUG
+ fprintf (stderr, "About to request events on window %ld of display %x\n",
+ (unsigned long) GDK_ROOT_WINDOW(), GDK_DISPLAY());
+#endif
+ /* We must open a new connection to the server to avoid clashing with the GDK event loop */
+ display = XOpenDisplay (getenv ("DISPLAY"));
+ root_window = DefaultRootWindow (display);
+ XSelectInput (display,
+ root_window,
+ KeyPressMask | KeyReleaseMask);
+ /* register with: mouse hardware device handler? */
+ /* register with: mouse event handler */
+ return retval;
+}
+
+static gboolean
+_check_key_event (SpiDeviceEventController *controller)
+{
+ static gboolean initialized = FALSE;
+ static gboolean is_active = FALSE;
+ XEvent *x_event = g_new0 (XEvent, 1);
+ XKeyEvent *x_key_event;
+ KeySym keysym;
+ gboolean is_consumed = FALSE;
+ char key_name[16];
+ int i;
+ int n_listeners = g_list_length (controller->key_listeners);
+ Accessibility_KeyStroke key_event;
+ static CORBA_Environment ev;
+
+ if (!initialized)
+ {
+ initialized = TRUE;
+ CORBA_exception_init (&ev);
+ }
+
+ while (XPending(display))
+ {
+ XNextEvent (display, x_event);
+ if (XFilterEvent (x_event, None)) continue;
+ if (x_event->type == KeyPress)
+ {
+ x_key_event = (XKeyEvent *)x_event;
+ keysym = XLookupKeysym (x_key_event, 0);
+ key_event.keyID = (CORBA_long)(keysym);
+ key_event.keycode = (CORBA_short) x_key_event->keycode;
+ key_event.type = Accessibility_KEY_PRESSED;
+ key_event.modifiers = (CORBA_unsigned_short)(x_key_event->state);
+#ifdef SPI_KEYEVENT_DEBUG
+ fprintf (stderr,
+ "Key %lu pressed (%c), modifiers %d\n",
+ (unsigned long) keysym,
+ keysym ? (int) keysym : '*',
+ (int) x_key_event->state);
+#endif
+#ifdef SPI_DEBUG
+ fprintf(stderr, "%s%c",
+ (x_key_event->state & Mod1Mask)?"Alt-":"",
+ ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
+ (char) toupper((int) keysym) : (char) tolower((int)keysym));
+#endif /* SPI_DEBUG */
+ }
+ else
+ {
+#ifdef SPI_KEYEVENT_DEBUG
+ fprintf (stderr, "other event, type %d\n", (int) x_event->type);
+#endif
+ }
+ /* relay to listeners, and decide whether to consume it or not */
+ for (i=0; i<n_listeners && !is_consumed; ++i)
+ {
+ Accessibility_KeystrokeListener ls;
+ ls = (Accessibility_KeystrokeListener)
+ g_list_nth_data (controller->key_listeners, i);
+ if (!CORBA_Object_is_nil(ls, &ev))
+ {
+ is_consumed = Accessibility_KeystrokeListener_keyEvent (ls, &key_event, &ev);
+ }
+ }
+ if (is_consumed)
+ {
+ XAllowEvents (display, SyncKeyboard, CurrentTime);
+ }
+ else
+ {
+ XAllowEvents (display, ReplayKeyboard, CurrentTime);
+ }
+ }
+ XUngrabKey (display, AnyKey, AnyModifier, root_window);
+ return _controller_grab_keyboard (controller);
+}
+
+static gboolean
+_controller_grab_keyboard (SpiDeviceEventController *controller)
+{
+ GList *maskList = controller->keymask_list;
+ int i;
+ int last_mask;
+ last_mask = g_list_length (maskList);
+
+/*
+ * 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
+ */
+
+ for (i=0; i < last_mask; ++i)
+ {
+ Accessibility_ControllerEventMask *mask
+ = (Accessibility_ControllerEventMask *)g_list_nth_data (maskList, i);
+ unsigned long maskVal = 0xFFFFFFFF;
+ if (mask) maskVal = (unsigned long) mask->value;
+#ifdef SPI_KEYEVENT_DEBUG
+ fprintf (stderr, "mask=%lx\n", maskVal);
+#endif
+ if (!(maskVal & ControlMask))
+ {
+ XGrabKey (display,
+ AnyKey,
+ maskVal,
+ root_window,
+ True,
+ GrabModeAsync,
+ GrabModeSync);
+ /* TODO: check call for errors and return FALSE if error occurs */
+ } else {
+ return FALSE; /* can't do control key yet */
+ }
+ }
+ return TRUE;
+}