2002-03-27 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / libspi / keystrokelistener.c
index 506eeb1..5fec6b1 100644 (file)
 #ifdef SPI_DEBUG
 #  include <stdio.h>
 #endif
+#include <libspi/listener.h>
 #include <libspi/keystrokelistener.h>
 
 /* Our parent Gtk object type  */
 #define PARENT_TYPE BONOBO_TYPE_OBJECT
 
-/* A pointer to our parent object class */
-static GObjectClass *keystroke_listener_parent_class;
+enum {
+       KEY_EVENT,
+       LAST_SIGNAL
+};
+static guint signals [LAST_SIGNAL];
 
 /*
- * Implemented GObject::finalize
+ * CORBA Accessibility::KeystrokeListener::keyEvent method implementation
  */
-static void
-keystroke_listener_object_finalize (GObject *object)
+static CORBA_boolean
+impl_key_event (PortableServer_Servant           servant,
+               const Accessibility_DeviceEvent *key,
+               CORBA_Environment               *ev)
 {
+  gboolean was_consumed = FALSE;
+  SpiKeystrokeListener *listener = SPI_KEYSTROKE_LISTENER (
+         bonobo_object_from_servant (servant));
 
-#ifdef SPI_DEBUG
-        fprintf(stderr, "keystroke_listener_object_finalize called\n");
-#endif
-        keystroke_listener_parent_class->finalize (object);
-}
+  g_signal_emit (G_OBJECT (listener), signals [KEY_EVENT], 0, key, &was_consumed);
 
-void   spi_keystroke_listener_add_callback (SpiKeystrokeListener *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
+  return was_consumed;
 }
 
-void   spi_keystroke_listener_remove_callback (SpiKeystrokeListener *listener,
-                                              BooleanKeystrokeListenerCB callback)
+static gboolean
+boolean_handled_accumulator (GSignalInvocationHint *ihint,
+                            GValue                *return_accu,
+                            const GValue          *handler_return,
+                            gpointer               dummy)
 {
-  listener->callbacks = g_list_remove (listener->callbacks, callback);
+  gboolean continue_emission;
+  gboolean signal_handled;
+  
+  signal_handled = g_value_get_boolean (handler_return);
+  g_value_set_boolean (return_accu, signal_handled);
+  continue_emission = !signal_handled;
+  
+  return continue_emission;
 }
 
-/*
- * CORBA Accessibility::KeystrokeListener::keyEvent method implementation
- */
-static CORBA_boolean
-impl_key_event (PortableServer_Servant     servant,
-               const Accessibility_KeyStroke *key,
-               CORBA_Environment         *ev)
+void
+marshal_BOOLEAN__POINTER (GClosure     *closure,
+                         GValue       *return_value,
+                         guint         n_param_values,
+                         const GValue *param_values,
+                         gpointer      invocation_hint,
+                         gpointer      marshal_data)
 {
-  SpiKeystrokeListener *listener = SPI_KEYSTROKE_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 & SPI_KEYMASK_ALT)?"Alt-":"",
-           ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
-           (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID));
-  }
-#endif
-  /* TODO: convert from the CORBA-based struct to a c-type-based one ? */
-#ifdef SPI_KEYSTROKE_DEBUG  
-    fprintf (stderr, "Key:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
-          (long) key->keyID,
-          (unsigned int) key->modifiers,
-          (int) key->keycode,
-          (long int) key->timestamp);
-#endif
-  while (callbacks)
-  {
-         BooleanKeystrokeListenerCB cb = (BooleanKeystrokeListenerCB) callbacks->data;
-         was_consumed = (*cb) (key) || was_consumed;
-         callbacks = g_list_next (callbacks);
-  }
-  return was_consumed;
+  typedef gboolean (*GMarshalFunc_BOOLEAN__POINTER) (gpointer     data1,
+                                                     gpointer     arg_1,
+                                                     gpointer     data2);
+  register GMarshalFunc_BOOLEAN__POINTER callback;
+  register GCClosure *cc = (GCClosure*) closure;
+  register gpointer data1, data2;
+  gboolean v_return;
+
+  g_return_if_fail (return_value != NULL);
+  g_return_if_fail (n_param_values == 2);
+
+  if (G_CCLOSURE_SWAP_DATA (closure))
+    {
+      data1 = closure->data;
+      data2 = g_value_peek_pointer (param_values + 0);
+    }
+  else
+    {
+      data1 = g_value_peek_pointer (param_values + 0);
+      data2 = closure->data;
+    }
+  callback = (GMarshalFunc_BOOLEAN__POINTER) (marshal_data ? marshal_data : cc->callback);
+
+  v_return = callback (data1,
+                       g_value_get_pointer (param_values + 1),
+                       data2);
+
+  g_value_set_boolean (return_value, v_return);
 }
 
 static void
 spi_keystroke_listener_class_init (SpiKeystrokeListenerClass *klass)
 {
-        GObjectClass * object_class = (GObjectClass *) klass;
-        POA_Accessibility_KeystrokeListener__epv *epv = &klass->epv;
-        keystroke_listener_parent_class = g_type_class_peek_parent (klass);
-
-        object_class->finalize = keystroke_listener_object_finalize;
-
-        epv->keyEvent = impl_key_event;
+  POA_Accessibility_DeviceEventListener__epv *epv = &klass->epv;
+  
+  signals [KEY_EVENT] = g_signal_new (
+    "key_event",
+    G_TYPE_FROM_CLASS (klass),
+    G_SIGNAL_RUN_LAST,
+    G_STRUCT_OFFSET (SpiKeystrokeListenerClass, key_event),
+    boolean_handled_accumulator, NULL,
+    marshal_BOOLEAN__POINTER,
+    G_TYPE_BOOLEAN, 1, G_TYPE_POINTER);
+  
+  epv->notifyEvent = impl_key_event;
 }
 
 static void
 spi_keystroke_listener_init (SpiKeystrokeListener *keystroke_listener)
 {
-       keystroke_listener->callbacks = NULL;
 }
 
 BONOBO_TYPE_FUNC_FULL (SpiKeystrokeListener,
-                      Accessibility_KeystrokeListener,
+                      Accessibility_DeviceEventListener,
                       BONOBO_TYPE_OBJECT,
                       spi_keystroke_listener);