2001-11-13 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / libspi / keystrokelistener.c
index 61f883c..f59b00b 100644 (file)
@@ -54,7 +54,7 @@ static GObjectClass *keystroke_listener_parent_class;
 static void
 keystroke_listener_object_finalize (GObject *object)
 {
-/*        KeystrokeListener *keystroke_listener = KEYSTROKE_LISTENER (object); */
+/*        KeystrokeListener *keystroke_listener = KEYSTROKE_SPI_LISTENER (object); */
 
 #ifdef SPI_DEBUG
         fprintf(stderr, "keystroke_listener_object_finalize called\n");
@@ -62,23 +62,54 @@ keystroke_listener_object_finalize (GObject *object)
         keystroke_listener_parent_class->finalize (object);
 }
 
+void   keystroke_listener_add_callback (KeystrokeListener *listener,
+                                       BooleanKeystrokeListenerCB callback)
+{
+  listener->callbacks = g_list_append (listener->callbacks, callback);
+#ifdef SPI_DEBUG
+        fprintf(stderr, "keystroke_listener_add_callback (%p) called\n",
+               (gpointer) callback);
+#endif
+}
+
+void   keystroke_listener_remove_callback (KeystrokeListener *listener,
+                                          BooleanKeystrokeListenerCB callback)
+{
+  listener->callbacks = g_list_remove (listener->callbacks, callback);
+}
+
 /*
  * CORBA Accessibility::KeystrokeListener::keyEvent method implementation
  */
-
-static void
+static CORBA_boolean
 impl_key_event (PortableServer_Servant     servant,
                const Accessibility_KeyStroke *key,
                CORBA_Environment         *ev)
 {
-#ifdef SPI_DEBUG
+  KeystrokeListener *listener = KEYSTROKE_SPI_LISTENER (bonobo_object_from_servant (servant));
+  GList *callbacks = listener->callbacks;
+  gboolean was_consumed = FALSE;
+#ifdef SPI_KEYEVENT_DEBUG
   if (ev->_major != CORBA_NO_EXCEPTION) {
     fprintf(stderr,
             ("Accessibility app error: exception during keystroke notification: %s\n"),
             CORBA_exception_id(ev));
     exit(-1);
   }
+  else {
+    fprintf(stderr, "%s%c",
+           (key->modifiers & KEYMASK_ALT)?"Alt-":"",
+           ((key->modifiers & KEYMASK_SHIFT)^(key->modifiers & KEYMASK_SHIFTLOCK))?
+           (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID));
+  }
 #endif
+  while (callbacks)
+  {
+         BooleanKeystrokeListenerCB cb = (BooleanKeystrokeListenerCB) callbacks->data;
+         was_consumed = (*cb) (key) || was_consumed;
+         callbacks = g_list_next (callbacks);
+  }
+  return was_consumed;
 }
 
 static void
@@ -96,6 +127,7 @@ keystroke_listener_class_init (KeystrokeListenerClass *klass)
 static void
 keystroke_listener_init (KeystrokeListener *keystroke_listener)
 {
+       keystroke_listener->callbacks = NULL;
 }
 
 GType
@@ -138,6 +170,6 @@ KeystrokeListener *
 keystroke_listener_new (void)
 {
     KeystrokeListener *retval =
-               KEYSTROKE_LISTENER (g_object_new (keystroke_listener_get_type (), NULL));
+               KEYSTROKE_SPI_LISTENER (g_object_new (keystroke_listener_get_type (), NULL));
     return retval;
 }