+#define PARENT_TYPE SPI_ACCESSIBLE_TYPE
+
+/*
+ * A pointer to our parent object class
+ */
+static SpiAccessibleClass *spi_application_parent_class;
+
+static SpiApplication *the_app;
+
+/* static methods */
+
+static void notify_listeners (GList *listeners,
+ Accessibility_Event *e,
+ CORBA_Environment *ev);
+
+static char* lookup_toolkit_event_for_name (char *generic_name);
+
+static char* reverse_lookup_name_for_toolkit_event (char *toolkit_name);
+
+/*
+ * Implemented GObject::finalize
+ */
+static void
+spi_accessible_application_finalize (GObject *object)
+{
+ /* TODO: any necessary cleanup */
+ (G_OBJECT_CLASS (spi_application_parent_class))->finalize (object);
+}
+
+static CORBA_string
+impl_accessibility_application_get_toolkit_name (PortableServer_Servant servant,
+ CORBA_Environment *ev)
+{
+ CORBA_char *retval;
+ SpiApplication *application = SPI_APPLICATION (bonobo_object_from_servant (servant));
+ retval = CORBA_string_dup (atk_get_toolkit_name ());
+ return retval;
+}
+
+static CORBA_string
+impl_accessibility_application_get_version (PortableServer_Servant servant,
+ CORBA_Environment *ev)
+{
+ CORBA_char *retval;
+ SpiApplication *application = SPI_APPLICATION (bonobo_object_from_servant (servant));
+ retval = CORBA_string_dup (atk_get_toolkit_version ());
+ return retval;
+}
+
+static CORBA_long
+impl_accessibility_application_get_id (PortableServer_Servant servant,
+ CORBA_Environment *ev)
+{
+ CORBA_long retval;
+ SpiApplication *application = SPI_APPLICATION (bonobo_object_from_servant (servant));
+ retval = (CORBA_long) application->id;
+ return retval;
+}
+
+static void
+impl_accessibility_application_set_id (PortableServer_Servant servant,
+ const CORBA_long id,
+ CORBA_Environment *ev)
+{
+ SpiApplication *application = SPI_APPLICATION (bonobo_object_from_servant (servant));
+ application->id = id;
+}
+
+#define APP_STATIC_BUFF_SZ 64
+
+static gboolean
+spi_application_object_event_listener (GSignalInvocationHint *signal_hint,
+ guint n_param_values,
+ const GValue *param_values,
+ gpointer data)
+{
+ Accessibility_Event *e = Accessibility_Event__alloc();
+ AtkObject *aobject;
+ GObject *gobject;
+ SpiAccessible *source;
+ CORBA_Environment ev;
+ GSignalQuery signal_query;
+ gchar *name;
+ char sbuf[APP_STATIC_BUFF_SZ];
+ char *generic_name;
+
+ g_signal_query (signal_hint->signal_id, &signal_query);
+ name = signal_query.signal_name;
+ fprintf (stderr, "Received (object) signal %s:%s\n",
+ g_type_name (signal_query.itype), name);
+
+ /* TODO: move GTK dependency out of app.c into bridge */
+ snprintf(sbuf, APP_STATIC_BUFF_SZ, "Gtk:%s:%s", g_type_name (signal_query.itype), name);
+
+ generic_name = reverse_lookup_name_for_toolkit_event (sbuf);
+ gobject = g_value_get_object (param_values + 0);
+
+ /* notify the actual listeners */
+ if (ATK_IS_IMPLEMENTOR (gobject))
+ {
+ aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
+ }
+ else if (ATK_IS_OBJECT (gobject))
+ {
+ aobject = ATK_OBJECT (gobject);
+ g_object_ref (G_OBJECT (aobject));
+ }
+ else
+ {
+ g_error("received event from non-AtkImplementor");
+ }
+
+ g_return_val_if_fail (generic_name, FALSE);
+ if (generic_name)
+ {
+ source = spi_accessible_new (aobject);
+ e->type = CORBA_string_dup (generic_name);
+ e->source = BONOBO_OBJREF (source);
+ /*
+ * no need to dup this ref, since it's inprocess
+ * and will be dup'ed by (inprocess) notify_listeners() call below
+ */
+ e->detail1 = 0;
+ e->detail2 = 0;
+ if (the_app) notify_listeners (the_app->toolkit_listeners, e, &ev);
+ /* unref because the in-process notify has called b_o_dup_ref (e->source) */
+ bonobo_object_release_unref (e->source, &ev);
+ }
+ /* and, decrement the refcount on atkobject, incremented moments ago:
+ * the call to spi_accessible_new() above should have added an extra ref */
+ g_object_unref (G_OBJECT (aobject));
+
+ return TRUE;
+}
+
+
+static gboolean
+spi_application_toolkit_event_listener (GSignalInvocationHint *signal_hint,
+ guint n_param_values,
+ const GValue *param_values,
+ gpointer data)
+{
+ Accessibility_Event *e = Accessibility_Event__alloc();
+ AtkObject *aobject;
+ GObject *gobject;
+ SpiAccessible *source;
+ CORBA_Environment ev;
+ GSignalQuery signal_query;
+ gchar *name;
+ char sbuf[APP_STATIC_BUFF_SZ];
+
+ g_signal_query (signal_hint->signal_id, &signal_query);
+ name = signal_query.signal_name;
+ fprintf (stderr, "Received signal %s:%s\n", g_type_name (signal_query.itype), name);
+
+ /* TODO: move GTK dependency out of app.c into bridge */
+ snprintf(sbuf, APP_STATIC_BUFF_SZ, "Gtk:%s:%s", g_type_name (signal_query.itype), name);
+
+ gobject = g_value_get_object (param_values + 0);
+ /* notify the actual listeners */
+ if (ATK_IS_IMPLEMENTOR (gobject))
+ {
+ aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
+ source = spi_accessible_new (aobject);
+ e->type = CORBA_string_dup (sbuf);
+ e->source = BONOBO_OBJREF (source);
+ e->detail1 = 0;
+ e->detail2 = 0;
+ if (the_app) notify_listeners (the_app->toolkit_listeners, e, &ev);
+ bonobo_object_unref (source);
+ g_object_unref (G_OBJECT (aobject));
+ }
+ return TRUE;
+}
+
+static void
+impl_accessibility_application_register_toolkit_event_listener (PortableServer_Servant servant,
+ Accessibility_EventListener listener,
+ const CORBA_char *event_name,
+ CORBA_Environment *ev)
+{
+ guint spi_listener_id;
+ spi_listener_id =
+ atk_add_global_event_listener (spi_application_toolkit_event_listener, event_name);
+ the_app->toolkit_listeners = g_list_append (the_app->toolkit_listeners,
+ CORBA_Object_duplicate (listener, ev));
+#ifdef SPI_DEBUG
+ fprintf (stderr, "registered %d for toolkit events named: %s\n",
+ spi_listener_id,
+ event_name);
+#endif
+}
+
+static void
+impl_accessibility_application_register_object_event_listener (PortableServer_Servant servant,
+ Accessibility_EventListener listener,
+ const CORBA_char *event_name,
+ CORBA_Environment *ev)
+{
+ guint spi_listener_id;
+ char *toolkit_specific_event_name = lookup_toolkit_event_for_name (event_name);
+ if (toolkit_specific_event_name)
+ {
+ spi_listener_id =
+ atk_add_global_event_listener (spi_application_object_event_listener,
+ CORBA_string_dup (toolkit_specific_event_name));
+ the_app->toolkit_listeners = g_list_append (the_app->toolkit_listeners,
+ CORBA_Object_duplicate (listener, ev));
+ }
+#ifdef SPI_DEBUG
+ fprintf (stderr, "registered %d for object events named: %s\n",
+ spi_listener_id,
+ event_name);
+#endif
+}