Fix some missing prototypes
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / event.c
index 7dc9e45..2253ec0 100644 (file)
@@ -23,6 +23,7 @@
  */
 
 #include <string.h>
+#include <ctype.h>
 
 #include <atk/atk.h>
 #include <droute/droute.h>
@@ -31,6 +32,9 @@
 #include "accessible-register.h"
 
 #include "common/spi-dbus.h"
+#include "event.h"
+#include "object.h"
+#include "dbus/dbus-glib-lowlevel.h"
 
 static GArray *listener_ids = NULL;
 
@@ -39,38 +43,67 @@ static gint atk_bridge_focus_tracker_id;
 
 /*---------------------------------------------------------------------------*/
 
-#define ITF_EVENT_OBJECT   "org.freedesktop.atspi.Event.Object"
-#define ITF_EVENT_WINDOW   "org.freedesktop.atspi.Event.Window"
-#define ITF_EVENT_DOCUMENT "org.freedesktop.atspi.Event.Document"
-#define ITF_EVENT_FOCUS    "org.freedesktop.atspi.Event.Focus"
+#define ITF_EVENT_OBJECT   "org.a11y.atspi.Event.Object"
+#define ITF_EVENT_WINDOW   "org.a11y.atspi.Event.Window"
+#define ITF_EVENT_DOCUMENT "org.a11y.atspi.Event.Document"
+#define ITF_EVENT_FOCUS    "org.a11y.atspi.Event.Focus"
 
 /*---------------------------------------------------------------------------*/
 
+typedef struct _SpiReentrantCallClosure 
+{
+  GMainLoop   *loop;
+  DBusMessage *reply;
+} SpiReentrantCallClosure;
+
+static void
+switch_main_context (GMainContext *cnx)
+{
+/* This code won't work on dbus-glib earlier than 0.9.0 because of FDO#30574 */
+  if (spi_global_app_data->app_bus_addr [0] == '\0')
+    return;
+
+  GList *list;
+
+  dbus_server_setup_with_g_main (spi_global_app_data->server, cnx);
+  dbus_connection_setup_with_g_main (spi_global_app_data->bus, cnx);
+  for (list = spi_global_app_data->direct_connections; list; list = list->next)
+    dbus_connection_setup_with_g_main (list->data, cnx);
+}
+
 static void
 set_reply (DBusPendingCall * pending, void *user_data)
 {
-  void **replyptr = (void **) user_data;
+  SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data; 
 
-  *replyptr = dbus_pending_call_steal_reply (pending);
+  closure->reply = dbus_pending_call_steal_reply (pending);
+  dbus_pending_call_unref (pending);
+  switch_main_context (NULL);
+  g_main_loop_quit (closure->loop);
 }
 
 static DBusMessage *
 send_and_allow_reentry (DBusConnection * bus, DBusMessage * message)
 {
   DBusPendingCall *pending;
-  DBusMessage *reply = NULL;
+  SpiReentrantCallClosure closure;
+  GMainContext *main_context;
 
-  if (!dbus_connection_send_with_reply (bus, message, &pending, -1))
+  main_context = (g_getenv ("AT_SPI_CLIENT") ? NULL :
+                  spi_global_app_data->main_context);
+  closure.loop = g_main_loop_new (main_context, FALSE);
+  switch_main_context (main_context);
+
+  if (!dbus_connection_send_with_reply (bus, message, &pending, -1) || !pending)
     {
+      switch_main_context (NULL);
       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;
+  dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL);
+  g_main_loop_run  (closure.loop);
+  
+  g_main_loop_unref (closure.loop);
+  return closure.reply;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -258,6 +291,94 @@ append_object (DBusMessageIter *iter,
   spi_object_append_v_reference (iter, ATK_OBJECT (val));
 }
 
+static gchar *
+signal_name_to_dbus (const gchar *s)
+{
+  gchar *ret = g_strdup (s);
+  gchar *t;
+
+  if (!ret)
+    return NULL;
+  ret [0] = toupper (ret [0]);
+  while ((t = strchr (ret, '-')) != NULL)
+  {
+    memmove (t, t + 1, strlen (t));
+    *t = toupper (*t);
+  }
+  return ret;
+}
+
+/*
+ * Converts names of the form "active-descendant-changed" to
+ * "ActiveDescendantChanged"
+ */
+static gchar *
+ensure_proper_format (const char *name)
+{
+  gchar *ret = (gchar *) g_malloc (strlen (name) * 2 + 2);
+  gchar *p = ret;
+  gboolean need_upper = TRUE;
+
+  if (!ret)
+    return NULL;
+  while (*name)
+    {
+      if (need_upper)
+        {
+          *p++ = toupper (*name);
+          need_upper = FALSE;
+        }
+      else if (*name == '-')
+        need_upper = TRUE;
+      else if (*name == ':')
+        {
+          need_upper = TRUE;
+          *p++ = *name;
+        }
+      else
+        *p++ = *name;
+      name++;
+    }
+  *p = '\0';
+  return ret;
+}
+
+static gboolean
+signal_is_needed (const gchar *klass, const gchar *major, const gchar *minor)
+{
+  gchar *data [4];
+  GList *iter;
+  event_data *evdata;
+  gboolean ret = FALSE;
+  GList *list;
+
+  if (!spi_global_app_data->events_initialized)
+    return TRUE;
+
+  data [0] = ensure_proper_format (klass + 21);
+  data [1] = ensure_proper_format (major);
+  data [2] = ensure_proper_format (minor);
+  data [3] = NULL;
+
+  /* Hack: events such as "object::text-changed::insert:system" as
+     generated by Gecko */
+  data [2][strcspn (data [2], ":")] = '\0';
+  for (list = spi_global_app_data->events; list; list = list->next)
+    {
+      evdata = list->data;
+      if (spi_event_is_subtype (data, evdata->data))
+        {
+          ret = TRUE;
+          break;
+        }
+    }
+
+  g_free (data [2]);
+  g_free (data [1]);
+  g_free (data [0]);
+  return ret;
+}
+
 /*
  * Emits an AT-SPI event.
  * AT-SPI events names are split into three parts:
@@ -280,8 +401,7 @@ emit_event (AtkObject  *obj,
             void (*append_variant) (DBusMessageIter *, const char *, const void *))
 {
   DBusConnection *bus = spi_global_app_data->bus;
-  const char *path =  spi_register_object_to_path (spi_global_register,
-                                                   G_OBJECT (obj));
+  const char *path;
 
   gchar *cname, *t;
   DBusMessage *sig;
@@ -292,27 +412,35 @@ emit_event (AtkObject  *obj,
   if (!minor) minor = "";
   if (!type) type = "u";
 
+  if (!signal_is_needed (klass, major, minor))
+    return;
+
+  path =  spi_register_object_to_path (spi_global_register, G_OBJECT (obj));
+
   /*
    * 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 = '_';
+  cname = signal_name_to_dbus (major);
   sig = dbus_message_new_signal(path, klass, cname);
-  g_free(cname);
 
   dbus_message_iter_init_append(sig, &iter);
 
-  spi_object_append_reference (&iter, spi_global_app_data->root);
   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);
-
   append_variant (&iter, type, val);
+  spi_object_append_reference (&iter, spi_global_app_data->root);
 
   dbus_connection_send(bus, sig, NULL);
   dbus_message_unref(sig);
+
+  if (g_strcmp0 (cname, "ChildrenChanged") != 0)
+    spi_object_lease_if_needed (G_OBJECT (obj));
+
+  g_free(cname);
+  g_free (path);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -330,7 +458,7 @@ focus_tracker (AtkObject * accessible)
 
 /*---------------------------------------------------------------------------*/
 
-#define PCHANGE "property-change"
+#define PCHANGE "PropertyChange"
 
 /* 
  * This handler handles the following ATK signals and
@@ -369,21 +497,27 @@ property_event_listener (GSignalInvocationHint * signal_hint,
         emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
                     DBUS_TYPE_STRING_AS_STRING, s1, append_basic);
     }
-  if (strcmp (pname, "accessible-description") == 0)
+  else if (strcmp (pname, "accessible-description") == 0)
     {
       s1 = atk_object_get_description (accessible);
       if (s1 != NULL)
         emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
                     DBUS_TYPE_STRING_AS_STRING, s1, append_basic);
     }
-  if (strcmp (pname, "accessible-parent") == 0)
+  else if (strcmp (pname, "accessible-parent") == 0)
     {
       otemp = atk_object_get_parent (accessible);
       if (otemp != NULL)
         emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
                     "(so)", otemp, append_object);
     }
-  if (strcmp (pname, "accessible-table-summary") == 0)
+  else if (strcmp (pname, "accessible-role") == 0)
+    {
+      i = atk_object_get_role (accessible);
+      emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
+                    DBUS_TYPE_UINT32_AS_STRING, GINT_TO_POINTER(i), append_basic);
+    }
+  else if (strcmp (pname, "accessible-table-summary") == 0)
     {
       otemp = atk_table_get_summary (ATK_TABLE (accessible));
       if (otemp != NULL)
@@ -575,7 +709,6 @@ active_descendant_event_listener (GSignalInvocationHint * signal_hint,
   AtkObject *child;
   GSignalQuery signal_query;
   const gchar *name, *minor;
-  gchar *s;
   gint detail1;
 
   g_signal_query (signal_hint->signal_id, &signal_query);
@@ -590,7 +723,6 @@ active_descendant_event_listener (GSignalInvocationHint * signal_hint,
 
   emit_event (accessible, ITF_EVENT_OBJECT, name, "", detail1, 0,
               "(so)", child, append_object);
-  g_free (s);
   return TRUE;
 }
 
@@ -701,9 +833,82 @@ text_selection_changed_event_listener (GSignalInvocationHint * signal_hint,
 /*---------------------------------------------------------------------------*/
 
 /*
+ * Children changed signal converter and forwarder.
+ *
+ * Klass (Interface) org.a11y.atspi.Event.Object
+ * Major is the signal name.
+ * Minor is 'add' or 'remove'
+ * detail1 is the index.
+ * detail2 is 0.
+ * any_data is the child reference.
+ */
+static gboolean
+children_changed_event_listener (GSignalInvocationHint * signal_hint,
+                                 guint n_param_values,
+                                 const GValue * param_values, gpointer data)
+{
+  GSignalQuery signal_query;
+  const gchar *name, *minor;
+  gint detail1, detail2 = 0;
+
+  AtkObject *accessible, *ao=NULL;
+  gpointer child;
+
+  g_signal_query (signal_hint->signal_id, &signal_query);
+  name = signal_query.signal_name;
+
+  accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
+  minor = g_quark_to_string (signal_hint->detail);
+
+  detail1 = g_value_get_uint (param_values + 1);
+  child = g_value_get_pointer (param_values + 2);
+
+  if (ATK_IS_OBJECT (child))
+    {
+      ao = ATK_OBJECT (child);
+      emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
+                  "(so)", ao, append_object);
+    }
+  else if ((minor != NULL) && (strcmp (minor, "add") == 0))
+    {
+      ao = atk_object_ref_accessible_child (accessible, 
+                                            detail1);
+      emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
+                  "(so)", ao, append_object);
+    }
+  else
+    {
+      emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
+                  "(so)", ao, append_object);
+    }
+  return TRUE;
+}
+
+/*---------------------------------------------------------------------------*/
+
+static void
+toplevel_added_event_listener (AtkObject * accessible,
+                               guint index, AtkObject * child)
+{
+  emit_event (accessible, ITF_EVENT_OBJECT, "children-changed", "add", index, 0,
+              "(so)", child, append_object);
+}
+
+static void
+toplevel_removed_event_listener (AtkObject * accessible,
+                                 guint index, AtkObject * child)
+{
+  emit_event (accessible, ITF_EVENT_OBJECT, "children-changed", "remove", index, 0,
+              "(so)", child, append_object);
+}
+
+/*---------------------------------------------------------------------------*/
+
+/*
  * Generic signal converter and forwarder.
  *
- * Klass (Interface) org.freedesktop.atspi.Event.Object
+ * Klass (Interface) org.a11y.atspi.Event.Object
  * Major is the signal name.
  * Minor is NULL.
  * detail1 is 0.
@@ -820,6 +1025,20 @@ spi_atk_register_event_listeners (void)
   add_signal_listener (generic_event_listener, "Gtk:AtkTable:column-deleted");
   add_signal_listener (generic_event_listener, "Gtk:AtkTable:model-changed");
 
+  /* Children signal listeners */
+  atk_add_global_event_listener (children_changed_event_listener,
+                                 "Gtk:AtkObject:children-changed");
+
+#if 0
+  g_signal_connect (G_OBJECT (spi_global_app_data->root),
+                    "children-changed::add",
+                    (GCallback) toplevel_added_event_listener, NULL);
+
+  g_signal_connect (G_OBJECT (spi_global_app_data->root),
+                    "children-changed::remove",
+                    (GCallback) toplevel_removed_event_listener, NULL);
+#endif
+
   /*
    * May add the following listeners to implement preemptive key listening for GTK+
    *
@@ -895,4 +1114,17 @@ spi_atk_tidy_windows (void)
     }
 }
 
+gboolean
+spi_event_is_subtype (gchar **needle, gchar **haystack)
+{
+  while (*haystack && **haystack)
+    {
+      if (g_strcmp0 (*needle, *haystack))
+        return FALSE;
+      needle++;
+      haystack++;
+    }
+  return TRUE;
+}
+
 /*END------------------------------------------------------------------------*/