2009-07-06 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / event.c
index cc03daf..2039b63 100644 (file)
@@ -2,7 +2,7 @@
  * AT-SPI - Assistive Technology Service Provider Interface
  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
  *
- * Copyright 2008, Codethink Ltd.
+ * Copyright 2008, 2009, Codethink Ltd.
  * Copyright 2001, 2002, 2003 Sun Microsystems Inc.,
  * Copyright 2001, 2002, 2003 Ximian, Inc.
  *
 #include <droute/droute.h>
 
 #include "bridge.h"
-#include "atk-dbus.h"
+#include "accessible-register.h"
 
-#include "spi-common/spi-dbus.h"
+#include "common/spi-dbus.h"
 
 static GArray *listener_ids = NULL;
 
 static gint atk_bridge_key_event_listener_id;
 static gint atk_bridge_focus_tracker_id;
 
-/* Amazingly the ATK event callbacks dont have a user
- * data parameter. Instead, with great sadness, we use
- * some global data. Data is declared and initialized
- * in bridge.c.
- */
-extern SpiAppData *atk_adaptor_app_data;
-
 /*---------------------------------------------------------------------------*/
 
 #define ITF_EVENT_OBJECT   "org.freedesktop.atspi.Event.Object"
@@ -53,21 +46,31 @@ extern SpiAppData *atk_adaptor_app_data;
 
 /*---------------------------------------------------------------------------*/
 
-/* When sending events it is safe to register an accessible object if
- * one does not already exist for a given AtkObject.
- * This is because the cache update signal should then be send before
- * the event signal is sent.
- */
-static gchar *
-get_object_path (AtkObject *accessible)
+static void
+set_reply (DBusPendingCall *pending, void *user_data)
 {
-    guint ref;
+    void **replyptr = (void **)user_data;
 
-    ref = atk_dbus_register_accessible (accessible);
-    return atk_dbus_ref_to_path (ref);
+    *replyptr = dbus_pending_call_steal_reply (pending);
 }
 
-/*---------------------------------------------------------------------------*/
+static DBusMessage *
+send_and_allow_reentry (DBusConnection *bus, DBusMessage *message)
+{
+    DBusPendingCall *pending;
+    DBusMessage *reply = NULL;
+
+    if (!dbus_connection_send_with_reply (bus, message, &pending, -1))
+    {
+        return NULL;
+    }
+    dbus_pending_call_set_notify (pending, set_reply, (void *)&reply, NULL);
+    while (!reply)
+    {
+      if (!dbus_connection_read_write_dispatch (bus, -1)) return NULL;
+    }
+    return reply;
+}
 
 static gboolean
 Accessibility_DeviceEventController_notifyListenersSync(const Accessibility_DeviceEvent *key_event)
@@ -76,16 +79,16 @@ Accessibility_DeviceEventController_notifyListenersSync(const Accessibility_Devi
   DBusError error;
   dbus_bool_t consumed = FALSE;
 
-  message = 
+  message =
   dbus_message_new_method_call(SPI_DBUS_NAME_REGISTRY, 
-                              SPI_DBUS_PATH_REGISTRY,
-                              SPI_DBUS_INTERFACE_DEC,
-                              "notifyListenersSync");
+                               SPI_DBUS_PATH_DEC,
+                               SPI_DBUS_INTERFACE_DEC,
+                               "notifyListenersSync");
 
   dbus_error_init(&error);
   if (spi_dbus_marshal_deviceEvent(message, key_event))
   {
-    DBusMessage *reply = dbus_connection_send_with_reply_and_block(atk_adaptor_app_data->bus, message, 1000, &error);
+    DBusMessage *reply = send_and_allow_reentry (atk_adaptor_app_data->bus, message);
     if (reply)
     {
       DBusError error;
@@ -172,28 +175,6 @@ spi_atk_bridge_key_listener (AtkKeyEventStruct *event, gpointer data)
  * the AT-SPI event.
  */
 
-/*
- * This is a rather annoying function needed to replace
- * NULL values of strings with the empty string. Null string
- * values can be created by the atk_object_get_name or text selection
- */
-static const void *
-provide_defaults(const gint type,
-                const void *val)
-{
-  switch (type)
-    {
-      case DBUS_TYPE_STRING:
-      case DBUS_TYPE_OBJECT_PATH:
-          if (!val)
-             return "";
-          else
-             return val;
-      default:
-          return val;
-    }
-}
-
 static void 
 emit(AtkObject  *accessible,
      const char *klass,
@@ -204,54 +185,28 @@ emit(AtkObject  *accessible,
      const char *type,
      const void *val)
 {
-  DBusMessage *sig;
-  DBusMessageIter iter, sub;
-  gchar *path, *cname, *t;
+  gchar *path;
 
-  path = get_object_path (accessible);
+  /* TODO this is a hack, used becuase child-added events are not guaranteed.
+   * On recieving an event from a non-registered object we check if it can be safely 
+   * registered before sending the event.
+   */
+  path = atk_dbus_object_attempt_registration (accessible);
 
   /* Tough decision here
    * We won't send events from accessible
    * objects that have not yet been added to the accessible tree.
    */
   if (path == NULL)
+  {
+#ifdef SPI_ATK_DEBUG
+      g_debug ("AT-SPI: Event recieved from non-registered object");
+#endif
       return;
+  }
 
-  if (!klass) klass = "";
-  if (!major) major = "";
-  if (!minor) minor = "";
-
-  /*
-   * This is very annoying, but as '-' isn't a legal signal
-   * name in D-Bus (Why not??!?) The names need converting
-   * on this side, and again on the client side.
-   */
-  cname = g_strdup(major);
-  while ((t = strchr(cname, '-')) != NULL) *t = '_';
-
-  sig = dbus_message_new_signal(path, klass, cname);
-  g_free(cname);
+  spi_dbus_emit_signal (atk_adaptor_app_data->bus, path, klass, major, minor, detail1, detail2, type, val);
   g_free(path);
-
-  dbus_message_iter_init_append(sig, &iter);
-
-  dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &minor);
-  dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &detail1);
-  dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &detail2);
-
-  dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, type, &sub);
-  /*
-   * I need to convert the string signature to an integer type signature.
-   * DBUS_TYPE_INT32 is defined as 'i' whereas the string is "i".
-   * I should just be able to cast the first character of the string to an
-   * integer.
-   */
-  val = provide_defaults((int) *type, val);
-  dbus_message_iter_append_basic(&sub, (int) *type, &val);
-  dbus_message_iter_close_container(&iter, &sub);
-
-  dbus_connection_send(atk_adaptor_app_data->bus, sig, NULL);
-  dbus_message_unref(sig);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -272,7 +227,7 @@ emit_rect(AtkObject  *accessible,
   gchar *path, *cname, *t;
   dbus_int32_t dummy = 0;
 
-  path = get_object_path (accessible);
+  path = atk_dbus_object_to_path (accessible);
 
   /* Tough decision here
    * We won't send events from accessible
@@ -317,86 +272,6 @@ emit_rect(AtkObject  *accessible,
 /*---------------------------------------------------------------------------*/
 
 /*
- * The tree update listener handles the following Atk signals:
- *
- * Gtk:AtkObject:property-change
- * 
- * With the folowing property names:
- *
- * accessible-name
- * accessible-description
- * accessible-parent
- *
- * It updates the server side accessible-object database, which
- * will then syncronize with the client-side accessible cache.
- * 
- */
-static gboolean
-tree_update_listener (GSignalInvocationHint *signal_hint,
-                     guint                  n_param_values,
-                     const GValue          *param_values,
-                     gpointer               data)
-{
-  AtkObject *accessible;
-  AtkPropertyValues *values;
-  const gchar *pname = NULL;
-
-  accessible = g_value_get_object (&param_values[0]);
-  values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
-
-  pname = values[0].property_name;
-
-  if (strcmp (pname, "accessible-name") == 0 ||
-      strcmp (pname, "accessible-description") == 0 ||
-      strcmp (pname, "accessible-parent") == 0)
-    {
-      atk_dbus_update_accessible (accessible);
-    }
-  return TRUE;
-}
-
-/* 
- * Handles the ATK signal 'Gtk:AtkObject:children-changed'.
- *
- * It updates the server side accessible-object database, which
- * will then syncronize with the client-side accessible cache.
- *
- */
-static gboolean
-tree_update_children_listener (GSignalInvocationHint *signal_hint,
-                               guint                  n_param_values,
-                               const GValue          *param_values,
-                               gpointer               data)
-{
-  AtkObject *accessible;
-  const gchar *detail = NULL;
-  AtkObject *child;
-  gboolean child_needs_unref = FALSE;
-
-  if (signal_hint->detail)
-    detail = g_quark_to_string (signal_hint->detail);
-
-  accessible = g_value_get_object (&param_values[0]);
-  if (!strcmp (detail, "add"))
-    {
-      gpointer child;
-      int index = g_value_get_uint (param_values + 1);
-      child = g_value_get_pointer (param_values + 2);
-
-      if (ATK_IS_OBJECT (child))
-          g_object_ref (child);
-      else
-          child = atk_object_ref_accessible_child (accessible, index);
-
-      atk_dbus_register_accessible (child);
-      g_object_unref (child);
-    }
-  return TRUE;
-}
-
-/*---------------------------------------------------------------------------*/
-
-/*
  * The focus listener handles the ATK 'focus' signal and forwards it
  * as the AT-SPI event, 'focus:'
  */
@@ -432,11 +307,17 @@ property_event_listener (GSignalInvocationHint *signal_hint,
   AtkObject *otemp;
   const gchar *stemp;
   gint i;
-  
+
   accessible = g_value_get_object (&param_values[0]);
   values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
 
   pname = values[0].property_name;
+  if (strcmp (pname, "accessible-name") == 0 ||
+      strcmp (pname, "accessible-description") == 0 ||
+      strcmp (pname, "accessible-parent") == 0)
+  {
+      return TRUE;
+  }
 
   /* TODO Could improve this control statement by matching
    * on only the end of the signal names,
@@ -444,22 +325,25 @@ property_event_listener (GSignalInvocationHint *signal_hint,
   if (strcmp (pname, "accessible-table-summary") == 0)
     {
       otemp = atk_table_get_summary(ATK_TABLE (accessible));
-      stemp = get_object_path (otemp);
-      emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
+      stemp = atk_dbus_object_to_path (otemp);
+      if (stemp != NULL)
+          emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
     }
   else if (strcmp (pname, "accessible-table-column-header") == 0)
     {
       i = g_value_get_int (&(values->new_value));
       otemp = atk_table_get_column_header(ATK_TABLE (accessible), i);
-      stemp = get_object_path (otemp);
-      emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
+      stemp = atk_dbus_object_to_path (otemp);
+      if (stemp != NULL)
+          emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
     }
   else if (strcmp (pname, "accessible-table-row-header") == 0)
     {
       i = g_value_get_int (&(values->new_value));
       otemp = atk_table_get_row_header(ATK_TABLE (accessible), i);
-      stemp = get_object_path (otemp);
-      emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
+      stemp = atk_dbus_object_to_path (otemp);
+      if (stemp != NULL)
+          emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
     }
   else if (strcmp (pname, "accessible-table-row-description") == 0)
     {
@@ -638,7 +522,12 @@ active_descendant_event_listener (GSignalInvocationHint *signal_hint,
   minor = g_quark_to_string (signal_hint->detail);
 
   detail1 = atk_object_get_index_in_parent (child);
-  s = get_object_path (child);
+  s = atk_dbus_object_to_path (child);
+  if (s == NULL)
+    {
+      g_free (s);
+      return TRUE;
+    }
 
   emit(accessible, ITF_EVENT_OBJECT, name, "", detail1, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, s);
   g_free(s);
@@ -816,9 +705,6 @@ spi_atk_register_event_listeners (void)
 
   atk_bridge_focus_tracker_id = atk_add_focus_tracker (focus_tracker);
 
-  add_signal_listener (tree_update_listener,          "Gtk:AtkObject:property-change");
-  add_signal_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
-
   add_signal_listener (property_event_listener,               "Gtk:AtkObject:property-change");
   add_signal_listener (window_event_listener,                 "window:create");
   add_signal_listener (window_event_listener,                 "window:destroy");
@@ -830,7 +716,9 @@ spi_atk_register_event_listeners (void)
   add_signal_listener (document_event_listener,               "Gtk:AtkDocument:load-complete");
   add_signal_listener (document_event_listener,               "Gtk:AtkDocument:reload");
   add_signal_listener (document_event_listener,               "Gtk:AtkDocument:load-stopped");
+  /* TODO Fake this event on the client side */
   add_signal_listener (state_event_listener,                  "Gtk:AtkObject:state-change");
+  /* TODO */
   add_signal_listener (active_descendant_event_listener,      "Gtk:AtkObject:active-descendant-changed");
   add_signal_listener (bounds_event_listener,                 "Gtk:AtkComponent:bounds-changed");
   add_signal_listener (text_selection_changed_event_listener, "Gtk:AtkText:text-selection-changed");