* Boston, MA 02111-1307, USA.
*/
-/*
- * listener.c: test for accessibility implementation
- *
- */
+/* deviceeventcontroler.c: implement the DeviceEventController interface */
+
+#include <config.h>
#ifdef SPI_DEBUG
-#include <stdio.h>
+# include <stdio.h>
#endif
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>
-#include <config.h>
#include <gdk/gdkx.h> /* TODO: hide dependency (wrap in single porting file) */
#include <gdk/gdkwindow.h>
-#include <bonobo/Bonobo.h>
-#include <libspi/Accessibility.h>
-/*
- * This pulls the definition for the BonoboObject (GType)
- */
-#include "deviceeventcontroller.h"
+#include <libspi/deviceeventcontroller.h>
-/*
- * Our parent Gtk object type
- */
-#define PARENT_TYPE BONOBO_OBJECT_TYPE
+/* Our parent Gtk object type */
+#define PARENT_TYPE BONOBO_TYPE_OBJECT
-/*
- * A pointer to our parent object class
- */
+/* A pointer to our parent object class */
static GObjectClass *spi_device_event_controller_parent_class;
static gboolean kbd_registered = FALSE;
static Window root_window;
typedef enum {
- DEVICE_TYPE_KBD,
- DEVICE_TYPE_MOUSE,
- DEVICE_TYPE_LAST_DEFINED
-} DeviceTypeCategory;
+ SPI_DEVICE_TYPE_KBD,
+ SPI_DEVICE_TYPE_MOUSE,
+ SPI_DEVICE_TYPE_LAST_DEFINED
+} SpiDeviceTypeCategory;
+
+struct _DEControllerListener {
+ CORBA_Object object;
+ SpiDeviceTypeCategory type;
+};
+
+typedef struct _DEControllerListener DEControllerListener;
+
+struct _DEControllerKeyListener {
+ DEControllerListener listener;
+ Accessibility_KeySet *keys;
+ Accessibility_ControllerEventMask *mask;
+ Accessibility_KeyEventTypeSeq *typeseq;
+ gboolean is_system_global;
+};
+
+typedef struct _DEControllerKeyListener DEControllerKeyListener;
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);
+static void controller_register_device_listener (SpiDeviceEventController *controller,
+ DEControllerListener *l,
+ CORBA_Environment *ev);
/*
* Private methods
}
static gint
+_compare_listeners (gconstpointer p1, gconstpointer p2)
+{
+ DEControllerListener *l1 = (DEControllerListener *) p1;
+ DEControllerListener *l2 = (DEControllerListener *) p2;
+ return _compare_corba_objects (l1->object, l2->object);
+}
+
+static gint
_eventmask_compare_value (gconstpointer p1, gconstpointer p2)
{
long d;
return (gint) d;
}
+static DEControllerKeyListener *
+dec_key_listener_new (CORBA_Object l,
+ const Accessibility_KeySet *keys,
+ const Accessibility_ControllerEventMask *mask,
+ const Accessibility_KeyEventTypeSeq *typeseq,
+ const CORBA_boolean is_system_global,
+ CORBA_Environment *ev)
+{
+ DEControllerKeyListener *key_listener = g_new0 (DEControllerKeyListener, 1);
+ key_listener->listener.object = bonobo_object_dup_ref (l, ev);
+ key_listener->listener.type = SPI_DEVICE_TYPE_KBD;
+ key_listener->keys = ORBit_copy_value (keys, TC_Accessibility_KeySet);
+ key_listener->mask = ORBit_copy_value (mask, TC_Accessibility_ControllerEventMask);
+ key_listener->typeseq = ORBit_copy_value (typeseq, TC_Accessibility_KeyEventTypeSeq);
+ key_listener->is_system_global = is_system_global;
+
+#ifdef SPI_DEBUG
+ g_print ("new listener, with mask %x, is_global %d, keys %p\n",
+ (unsigned int) key_listener->mask->value,
+ (int) key_listener->is_system_global,
+ (void *) key_listener->keys);
+#endif
+ return key_listener;
+}
+
+static void
+dec_key_listener_free (DEControllerKeyListener *key_listener, CORBA_Environment *ev)
+{
+ bonobo_object_release_unref (key_listener->listener.object, ev);
+ CORBA_free (key_listener->typeseq);
+ CORBA_free (key_listener->mask);
+ CORBA_free (key_listener->keys);
+ g_free (key_listener);
+}
+
static void
-_controller_register_device_listener (SpiDeviceEventController *controller,
- const CORBA_Object l,
- const Accessibility_ControllerEventMask *mask,
- DeviceTypeCategory type,
- CORBA_Environment *ev)
+controller_register_device_listener (SpiDeviceEventController *controller,
+ DEControllerListener *listener,
+ CORBA_Environment *ev)
{
Accessibility_ControllerEventMask *mask_ptr = NULL;
+ DEControllerKeyListener *key_listener;
- 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,
+ 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->is_system_global)
+ {
+ mask_ptr = (Accessibility_ControllerEventMask *)
+ g_list_find_custom (controller->keymask_list, (gpointer) key_listener->mask,
_eventmask_compare_value);
- if (mask_ptr)
+ if (mask_ptr)
++(mask_ptr->refcount);
- else
- {
- if (mask->refcount != (CORBA_unsigned_short) 1)
+ else
+ {
+ if (key_listener->mask->refcount != (CORBA_unsigned_short) 1)
fprintf (stderr, "mask initial refcount is not 1!\n");
- if (mask->value > (CORBA_unsigned_long) 2048)
+ if (key_listener->mask->value > (CORBA_unsigned_long) 2048)
fprintf (stderr, "mask value looks invalid (%lu)\n",
- (unsigned long) mask->value);
+ (unsigned long) key_listener->mask->value);
else
fprintf (stderr, "appending mask with val=%lu\n",
- (unsigned long) mask->value);
+ (unsigned long) key_listener->mask->value);
mask_ptr = Accessibility_ControllerEventMask__alloc();
- mask_ptr->value = mask->value;
+ mask_ptr->value = key_listener->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:
+ case SPI_DEVICE_TYPE_MOUSE:
/* controller->mouse_listeners = g_list_append (controller->mouse_listeners,
CORBA_Object_duplicate (l, ev));*/
}
static void
-_controller_deregister_device_listener (SpiDeviceEventController *controller,
- const CORBA_Object l,
- const Accessibility_ControllerEventMask *mask,
- DeviceTypeCategory type,
- CORBA_Environment *ev)
+controller_deregister_device_listener (SpiDeviceEventController *controller,
+ DEControllerListener *listener,
+ CORBA_Environment *ev)
{
Accessibility_ControllerEventMask *mask_ptr;
+ DEControllerKeyListener *key_listener;
+ DEControllerListener *dec_listener;
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);
+ switch (listener->type) {
+ case SPI_DEVICE_TYPE_KBD:
+ key_listener = (DEControllerKeyListener *) listener;
+ /* first, deref matching event mask, if any */
list_ptr = (GList *)
- g_list_find_custom (controller->keymask_list, (gpointer) mask,
+ g_list_find_custom (controller->keymask_list, (gpointer) key_listener->mask,
_eventmask_compare_value);
if (list_ptr)
{
if (!mask_ptr->refcount)
{
controller->keymask_list =
- g_list_remove (controller->keymask_list, mask_ptr);
+ g_list_remove_link (controller->keymask_list, list_ptr);
; /* TODO: release any key grabs that are in place for this key mask */
}
}
+ /* now, remove this listener from the keylistener list */
+ list_ptr = g_list_find_custom (controller->key_listeners, listener, _compare_listeners);
+ if (list_ptr)
+ {
+ dec_listener = (DEControllerListener *) list_ptr->data;
+#ifdef SPI_DEBUG
+ g_print ("removing keylistener %p\n", dec_listener->object);
+#endif
+ controller->key_listeners = g_list_remove_link (controller->key_listeners,
+ list_ptr);
+ dec_key_listener_free (dec_listener, ev);
+ }
break;
- case DEVICE_TYPE_MOUSE:
+ case SPI_DEVICE_TYPE_MOUSE:
/* controller->mouse_listeners = g_list_append (controller->mouse_listeners,
CORBA_Object_duplicate (l, ev));*/
/* 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",
+ fprintf (stderr, "About to request events on window %ld of display %p\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"));
+ display = XOpenDisplay (g_getenv ("DISPLAY"));
root_window = DefaultRootWindow (display);
XSelectInput (display,
root_window,
}
static gboolean
+key_set_contains_key (Accessibility_KeySet *key_set, Accessibility_KeyStroke *key_event)
+{
+ gint i;
+ gint len;
+
+ /* g_assert (key_set); */
+ if (!key_set) { g_print ("null key set!"); return TRUE; }
+
+ len = key_set->_length;
+
+ if (len == 0) /* special case, means "all keys/any key" */
+ {
+ return TRUE;
+ }
+
+ for (i=0; i<len; ++i)
+ {
+#ifdef SPI_KEYEVENT_DEBUG
+ g_print ("key_set[%d] = %d; key_event %d, code %d\n",
+ i,
+ (int) key_set->_buffer[i],
+ (int) key_event->keyID,
+ (int) key_event->keycode);
+#endif
+ if (key_set->_buffer[i] == (CORBA_long) key_event->keyID) return TRUE;
+ if (key_set->_buffer[i] == (CORBA_long) -key_event->keycode) return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean
+key_eventtype_seq_contains_event (Accessibility_KeyEventTypeSeq *type_seq,
+ Accessibility_KeyStroke *key_event)
+{
+ gint i;
+ gint len;
+
+ /* g_assert (type_seq); */
+ 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)
+ {
+/* g_print ("type_seq[%d] = %d\n", i, (int) type_seq->_buffer[i]); */
+ if (type_seq->_buffer[i] == (CORBA_long) key_event->type) return TRUE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+key_event_matches_listener (Accessibility_KeyStroke *key_event,
+ DEControllerKeyListener *listener,
+ CORBA_boolean is_system_global)
+{
+ if ((key_event->modifiers == (CORBA_unsigned_short) (listener->mask->value & 0xFFFF)) &&
+ key_set_contains_key (listener->keys, key_event) &&
+ key_eventtype_seq_contains_event (listener->typeseq, key_event) &&
+ (is_system_global == listener->is_system_global))
+ {
+ return TRUE;
+ }
+ else
+ return FALSE;
+}
+
+static gboolean
+notify_keylisteners (GList *key_listeners,
+ Accessibility_KeyStroke *key_event,
+ CORBA_boolean is_system_global,
+ CORBA_Environment *ev)
+{
+ int i, n_listeners = g_list_length (key_listeners);
+ gboolean is_consumed = FALSE;
+
+ for (i=0; i<n_listeners && !is_consumed; ++i)
+ {
+ Accessibility_KeystrokeListener ls;
+ DEControllerKeyListener *key_listener = (DEControllerKeyListener *)
+ g_list_nth_data (key_listeners, i);
+ ls = (Accessibility_KeystrokeListener) key_listener->listener.object;
+ if (key_event_matches_listener (key_event, key_listener, is_system_global))
+ {
+ if (!CORBA_Object_is_nil(ls, ev))
+ {
+ is_consumed = Accessibility_KeystrokeListener_keyEvent (ls, key_event, ev);
+ }
+ }
+ else
+ {
+#ifdef SPI_KEYEVENT_DEBUG
+ g_print ("no match for listener %d\n", i);
+#endif
+ ;
+ }
+ }
+ return is_consumed;
+}
+
+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;
(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));
+ fprintf (stderr, "%s%c",
+ (x_key_event->state & Mod1Mask)?"Alt-":"",
+ ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
+ g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
#endif /* SPI_DEBUG */
}
else
#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);
- }
- }
+ is_consumed = notify_keylisteners (controller->key_listeners, &key_event, CORBA_TRUE, &ev);
+
if (is_consumed)
{
- XAllowEvents (display, SyncKeyboard, CurrentTime);
+ XAllowEvents (display, AsyncKeyboard, CurrentTime);
}
else
{
root_window,
True,
GrabModeAsync,
- GrabModeSync);
+ GrabModeAsync);
/* TODO: check call for errors and return FALSE if error occurs */
} else {
return FALSE; /* can't do control key yet */
#ifdef SPI_DEBUG
fprintf(stderr, "spi_device_event_controller_object_finalize called\n");
#endif
+ /* disconnect any special listeners, get rid of outstanding keygrabs */
+
spi_device_event_controller_parent_class->finalize (object);
}
/*
- * CORBA Accessibility::SpiDeviceEventController::registerKeystrokeListener
+ * CORBA Accessibility::DeviceEventController::registerKeystrokeListener
* method implementation
*/
static void
const Accessibility_KeySet *keys,
const Accessibility_ControllerEventMask *mask,
const Accessibility_KeyEventTypeSeq *type,
- const CORBA_boolean is_synchronous,
+ const CORBA_boolean is_system_global,
CORBA_Environment *ev)
{
SpiDeviceEventController *controller = SPI_DEVICE_EVENT_CONTROLLER (
bonobo_object_from_servant (servant));
+ DEControllerKeyListener *dec_listener;
#ifdef SPI_DEBUG
fprintf (stderr, "registering keystroke listener %p with maskVal %lu\n",
(void *) l, (unsigned long) mask->value);
#endif
- /* TODO: change this to an enum, indicating if event can be consumed */
- if (is_synchronous)
- _controller_register_device_listener(controller, l, mask, DEVICE_TYPE_KBD, ev);
- else
- ; /* register with toolkit instead */
+ dec_listener = dec_key_listener_new (l, keys, mask, type, is_system_global, ev);
+ controller_register_device_listener (controller, (DEControllerListener *) dec_listener, ev);
}
+
/*
- * CORBA Accessibility::SpiDeviceEventController::deregisterKeystrokeListener
+ * CORBA Accessibility::DeviceEventController::deregisterKeystrokeListener
* method implementation
*/
static void
const Accessibility_KeySet *keys,
const Accessibility_ControllerEventMask *mask,
const Accessibility_KeyEventTypeSeq *type,
- const CORBA_boolean is_synchronous,
+ const CORBA_boolean is_system_global,
CORBA_Environment *ev)
{
SpiDeviceEventController *controller = SPI_DEVICE_EVENT_CONTROLLER (
bonobo_object_from_servant (servant));
-#ifdef SPI_DEBUG
+ DEControllerKeyListener *key_listener = dec_key_listener_new (l,
+ keys,
+ mask,
+ type,
+ is_system_global,
+ ev);
+#ifdef SPI_DEREGISTER_DEBUG
fprintf (stderr, "deregistering keystroke listener %p with maskVal %lu\n",
(void *) l, (unsigned long) mask->value);
#endif
- _controller_deregister_device_listener(controller, l, mask, DEVICE_TYPE_KBD, ev);
+ controller_deregister_device_listener(controller,
+ (DEControllerListener *) key_listener,
+ ev);
+ dec_key_listener_free (key_listener, ev);
}
/*
- * CORBA Accessibility::SpiDeviceEventController::registerMouseListener
+ * CORBA Accessibility::DeviceEventController::registerMouseListener
* method implementation
*/
/*
#ifdef SPI_DEBUG
fprintf (stderr, "registering mouse listener %p\n", l);
#endif
- _controller_register_device_listener(controller, l, mask, DEVICE_TYPE_MOUSE, ev);
+ controller_register_device_listener(controller, DEVICE_TYPE_MOUSE, l, keys, mask, ev);
}
*/
}
/*
- * CORBA Accessibility::SpiDeviceEventController::registerKeystrokeListener
+ * CORBA Accessibility::DeviceEventController::registerKeystrokeListener
* method implementation
*/
static void
impl_generate_key_event (PortableServer_Servant servant,
const CORBA_long keycode,
- const CORBA_long synth_type,
+ const Accessibility_KeySynthType synth_type,
CORBA_Environment *ev)
{
long key_synth_code;
}
}
-/*
- * CORBA Accessibility::SpiDeviceEventController::generateMouseEvent
- * method implementation
- */
+/* Accessibility::DeviceEventController::generateMouseEvent */
static void
-impl_generate_mouse_event (PortableServer_Servant servant,
- const CORBA_long x,
- const CORBA_long y,
- const CORBA_char * eventName,
+impl_generate_mouse_event (PortableServer_Servant servant,
+ const CORBA_long x,
+ const CORBA_long y,
+ const CORBA_char *eventName,
+ CORBA_Environment *ev)
+{
+#ifdef SPI_DEBUG
+ fprintf (stderr, "generating mouse %s event at %ld, %ld\n", eventName, x, y);
+#endif
+}
+
+/* Accessibility::DeviceEventController::notifyListenersSync */
+static CORBA_boolean
+impl_notify_listeners_sync(PortableServer_Servant servant,
+ const Accessibility_DeviceEvent *event,
CORBA_Environment *ev)
{
+ SpiDeviceEventController *controller = SPI_DEVICE_EVENT_CONTROLLER (
+ bonobo_object_from_servant (servant));
+ Accessibility_KeyStroke *key_event = (Accessibility_KeyStroke *) event;
+#ifdef SPI_DEBUG
+ g_print ("notifying listeners synchronously: controller %x, event id %d\n",
+ (void *) controller, (int) event->eventID);
+#endif
+ return (notify_keylisteners (controller->key_listeners, key_event, CORBA_FALSE, ev) ?
+ CORBA_TRUE : CORBA_FALSE);
+}
+
+/* Accessibility::DeviceEventController::notifyListenersAsync */
+static void
+impl_notify_listeners_async (PortableServer_Servant servant,
+ const Accessibility_DeviceEvent *event,
+ CORBA_Environment *ev)
+{
+ SpiDeviceEventController *controller = SPI_DEVICE_EVENT_CONTROLLER(
+ bonobo_object_from_servant (servant));
+ Accessibility_KeyStroke *key_event = (Accessibility_KeyStroke *) event;
#ifdef SPI_DEBUG
- fprintf (stderr, "generating mouse %s event at %ld, %ld\n", eventName, x, y);
+ fprintf (stderr, "notifying listeners asynchronously\n");
#endif
+ notify_keylisteners (controller->key_listeners, key_event, CORBA_FALSE, ev);
}
static void
spi_device_event_controller_class_init (SpiDeviceEventControllerClass *klass)
{
GObjectClass * object_class = (GObjectClass *) klass;
- POA_Accessibility_SpiDeviceEventController__epv *epv = &klass->epv;
- spi_device_event_controller_parent_class = g_type_class_ref (BONOBO_OBJECT_TYPE);
+ POA_Accessibility_DeviceEventController__epv *epv = &klass->epv;
+ spi_device_event_controller_parent_class = g_type_class_peek_parent (klass);
object_class->finalize = spi_device_event_controller_object_finalize;
/* epv->registerMouseListener = impl_register_mouse_listener; */
epv->generateKeyEvent = impl_generate_key_event;
epv->generateMouseEvent = impl_generate_mouse_event;
+ epv->notifyListenersSync = impl_notify_listeners_sync;
+ epv->notifyListenersAsync = impl_notify_listeners_async;
klass->check_key_event = _check_key_event;
}
kbd_registered = _controller_register_with_devices (device_event_controller);
}
-gboolean spi_device_event_controller_check_key_event (SpiDeviceEventController *controller)
+gboolean
+spi_device_event_controller_check_key_event (SpiDeviceEventController *controller)
{
SpiDeviceEventControllerClass *klass = SPI_DEVICE_EVENT_CONTROLLER_GET_CLASS (controller);
if (klass->check_key_event)
return (klass->check_key_event) (controller);
+ return FALSE;
}
-GType
-spi_device_event_controller_get_type (void)
+SpiDeviceEventController *
+spi_device_event_controller_new (void *registryp)
{
- static GType type = 0;
-
- if (!type) {
- static const GTypeInfo tinfo = {
- sizeof (SpiDeviceEventControllerClass),
- (GBaseInitFunc) NULL,
- (GBaseFinalizeFunc) NULL,
- (GClassInitFunc) spi_device_event_controller_class_init,
- (GClassFinalizeFunc) NULL,
- NULL, /* class data */
- sizeof (SpiDeviceEventController),
- 0, /* n preallocs */
- (GInstanceInitFunc) spi_device_event_controller_init,
- NULL /* value table */
- };
- /*
- * Here we use bonobo_type_unique instead of
- * gtk_type_unique, this auto-generates a load of
- * CORBA structures for us. All derived types must
- * use bonobo_type_unique.
- */
- type = bonobo_type_unique (
- PARENT_TYPE,
- POA_Accessibility_SpiDeviceEventController__init,
- NULL,
- G_STRUCT_OFFSET (SpiDeviceEventControllerClass, epv),
- &tinfo,
- "SpiDeviceEventController");
- }
-
- return type;
+ BonoboObject *registry = (BonoboObject *) registryp;
+ SpiDeviceEventController *retval = g_object_new (
+ SPI_DEVICE_EVENT_CONTROLLER_TYPE, NULL);
+ retval->registry = registry;
+ bonobo_object_ref (registry);
+ return retval;
}
+BONOBO_TYPE_FUNC_FULL (SpiDeviceEventController,
+ Accessibility_DeviceEventController,
+ PARENT_TYPE,
+ spi_device_event_controller);
+