Only register for events when something is listening
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / event.c
index b973021..e882edf 100644 (file)
  */
 
 #include <string.h>
+#include <ctype.h>
 
 #include <atk/atk.h>
 #include <droute/droute.h>
+#include <atspi/atspi.h>
 
 #include "bridge.h"
 #include "accessible-register.h"
 
 #include "common/spi-dbus.h"
+#include "event.h"
+#include "object.h"
 
 static GArray *listener_ids = NULL;
 
@@ -53,11 +57,26 @@ typedef struct _SpiReentrantCallClosure
 } SpiReentrantCallClosure;
 
 static void
+switch_main_context (GMainContext *cnx)
+{
+  GList *list;
+
+#ifndef DISABLE_P2P
+  atspi_dbus_server_setup_with_g_main (spi_global_app_data->server, cnx);
+#endif
+  atspi_dbus_connection_setup_with_g_main (spi_global_app_data->bus, cnx);
+  for (list = spi_global_app_data->direct_connections; list; list = list->next)
+    atspi_dbus_connection_setup_with_g_main (list->data, cnx);
+}
+
+static void
 set_reply (DBusPendingCall * pending, void *user_data)
 {
   SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data; 
 
   closure->reply = dbus_pending_call_steal_reply (pending);
+  dbus_pending_call_unref (pending);
+  switch_main_context (NULL);
   g_main_loop_quit (closure->loop);
 }
 
@@ -66,26 +85,20 @@ send_and_allow_reentry (DBusConnection * bus, DBusMessage * message)
 {
   DBusPendingCall *pending;
   SpiReentrantCallClosure closure;
+  GMainContext *main_context;
 
-  if (!dbus_connection_send_with_reply (bus, message, &pending, -1))
-      return NULL;
-  dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL);
-  closure.loop = g_main_loop_new (NULL, FALSE);
+  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);
 
-  /* TODO: Remove old AT_SPI_CLIENT name */
-  if (getenv ("AT_SPI_CLIENT") || getenv ("AT_SPI_REENTER_G_MAIN_LOOP"))
-    {
-      g_main_loop_run  (closure.loop);
-    }
-  else
+  if (!dbus_connection_send_with_reply (bus, message, &pending, -1) || !pending)
     {
-      closure.reply = NULL;
-      while (!closure.reply)
-        {
-          if (!dbus_connection_read_write_dispatch (spi_global_app_data->bus, 1000))
-            return NULL;
-        }
+      switch_main_context (NULL);
+      return NULL;
     }
+  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;
@@ -215,7 +228,7 @@ convert_signal_name (const gchar * s)
 }
 
 static const void *
-replace_null (const gint type,
+validate_for_dbus (const gint type,
               const void *val)
 {
   switch (type)
@@ -224,6 +237,11 @@ replace_null (const gint type,
       case DBUS_TYPE_OBJECT_PATH:
           if (!val)
              return "";
+          else if (!g_utf8_validate (val, -1, NULL))
+             {
+              g_warning ("atk-bridge: Received bad UTF-8 string when emitting event");
+              return "";
+               }
           else
              return val;
       default:
@@ -240,7 +258,7 @@ append_basic (DBusMessageIter *iter,
 
   dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, type, &sub);
 
-    val = replace_null ((int) *type, val);
+    val = validate_for_dbus ((int) *type, val);
     dbus_message_iter_append_basic(&sub, (int) *type, &val);
 
   dbus_message_iter_close_container(iter, &sub);
@@ -337,11 +355,27 @@ signal_is_needed (const gchar *klass, const gchar *major, const gchar *minor)
   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: Always pass events that update the cache.
+   * TODO: FOr 2.2, have at-spi2-core define a special "cache listener" for
+   * this instead, so that we don't send these if no one is listening */
+  if (!g_strcmp0 (data [1], "ChildrenChanged") ||
+      !g_strcmp0 (data [1], "PropertyChange") ||
+      !g_strcmp0 (data [1], "StateChanged"))
+  {
+    g_free (data [2]);
+    g_free (data [1]);
+    g_free (data [0]);
+    return TRUE;
+  }
+
   /* Hack: events such as "object::text-changed::insert:system" as
      generated by Gecko */
   data [2][strcspn (data [2], ":")] = '\0';
@@ -355,6 +389,9 @@ signal_is_needed (const gchar *klass, const gchar *major, const gchar *minor)
         }
     }
 
+#if 0
+  g_print("event: %s %s %s: %d\n", data[0], data[1], data[2], ret);
+#endif
   g_free (data [2]);
   g_free (data [1]);
   g_free (data [0]);
@@ -383,8 +420,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;
@@ -398,6 +434,8 @@ emit_event (AtkObject  *obj,
   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
@@ -405,7 +443,6 @@ emit_event (AtkObject  *obj,
    */
   cname = signal_name_to_dbus (major);
   sig = dbus_message_new_signal(path, klass, cname);
-  g_free(cname);
 
   dbus_message_iter_init_append(sig, &iter);
 
@@ -420,6 +457,9 @@ emit_event (AtkObject  *obj,
 
   if (g_strcmp0 (cname, "ChildrenChanged") != 0)
     spi_object_lease_if_needed (G_OBJECT (obj));
+
+  g_free(cname);
+  g_free (path);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -741,6 +781,8 @@ link_selected_event_listener (GSignalInvocationHint * signal_hint,
 /* 
  * Handles the ATK signal 'Gtk:AtkText:text-changed' and
  * converts it to the AT-SPI signal - 'object:text-changed'
+ * This signal is deprecated by Gtk:AtkText:text-insert
+ * and Gtk:AtkText:text-remove
  *
  */
 static gboolean
@@ -771,9 +813,109 @@ text_changed_event_listener (GSignalInvocationHint * signal_hint,
 
   emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
               DBUS_TYPE_STRING_AS_STRING, selected, append_basic);
+  g_free (selected);
+
+  return TRUE;
+}
+
+/* 
+ * Handles the ATK signal 'Gtk:AtkText:text-insert' and
+ * converts it to the AT-SPI signal - 'object:text-changed'
+ *
+ */
+static gboolean
+text_insert_event_listener (GSignalInvocationHint * signal_hint,
+                            guint n_param_values,
+                            const GValue * param_values, gpointer data)
+{
+  AtkObject *accessible;
+  guint text_changed_signal_id;
+  GSignalQuery signal_query;
+  const gchar *name;
+  gchar *minor, *text;
+  gint detail1, detail2;
+
+  accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
+  /* Get signal name for 'Gtk:AtkText:text-changed' so
+   * we convert it to the AT-SPI signal - 'object:text-changed'
+   */
+  text_changed_signal_id = g_signal_lookup ("text-changed", G_OBJECT_TYPE (accessible));
+  g_signal_query (text_changed_signal_id, &signal_query);
+  name = signal_query.signal_name;
+
+
+  /* Add the insert and keep any detail coming from atk */
+  minor = g_quark_to_string (signal_hint->detail);
+  if (minor)
+    minor = g_strconcat ("insert:", minor, NULL);
+  else
+    minor = g_strdup ("insert");
+
+  if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
+    detail1 = g_value_get_int (&param_values[1]);
+
+  if (G_VALUE_TYPE (&param_values[2]) == G_TYPE_INT)
+    detail2 = g_value_get_int (&param_values[2]);
+
+  if (G_VALUE_TYPE (&param_values[3]) == G_TYPE_STRING)
+    text = g_value_get_string (&param_values[3]);
+
+  emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
+              DBUS_TYPE_STRING_AS_STRING, text, append_basic);
+  g_free (minor);
   return TRUE;
 }
 
+/* 
+ * Handles the ATK signal 'Gtk:AtkText:text-remove' and
+ * converts it to the AT-SPI signal - 'object:text-changed'
+ *
+ */
+static gboolean
+text_remove_event_listener (GSignalInvocationHint * signal_hint,
+                            guint n_param_values,
+                            const GValue * param_values, gpointer data)
+{
+  AtkObject *accessible;
+  guint text_changed_signal_id;
+  GSignalQuery signal_query;
+  const gchar *name;
+  gchar *minor, *text;
+  gint detail1, detail2;
+
+  accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
+  /* Get signal name for 'Gtk:AtkText:text-changed' so
+   * we convert it to the AT-SPI signal - 'object:text-changed'
+   */
+  text_changed_signal_id = g_signal_lookup ("text-changed", G_OBJECT_TYPE (accessible));
+  g_signal_query (text_changed_signal_id, &signal_query);
+  name = signal_query.signal_name;
+
+  minor = g_quark_to_string (signal_hint->detail);
+
+  /* Add the delete and keep any detail coming from atk */
+  minor = g_quark_to_string (signal_hint->detail);
+  if (minor)
+    minor = g_strconcat ("delete:", minor, NULL);
+  else
+    minor = g_strdup ("delete");
+
+  if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
+    detail1 = g_value_get_int (&param_values[1]);
+
+  if (G_VALUE_TYPE (&param_values[2]) == G_TYPE_INT)
+    detail2 = g_value_get_int (&param_values[2]);
+
+  if (G_VALUE_TYPE (&param_values[3]) == G_TYPE_STRING)
+    text = g_value_get_string (&param_values[3]);
+
+  emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
+              DBUS_TYPE_STRING_AS_STRING, text, append_basic);
+  g_free (minor);
+  return TRUE;
+}
+
+
 /*---------------------------------------------------------------------------*/
 
 /* 
@@ -954,6 +1096,12 @@ spi_atk_register_event_listeners (void)
   g_object_unref (G_OBJECT (bo));
   g_object_unref (ao);
 
+  if (listener_ids)
+  {
+    g_warning ("atk_bridge: spi_atk-register_event_listeners called multiple times");
+    return;
+  }
+
   /* Register for focus event notifications, and register app with central registry  */
   listener_ids = g_array_sized_new (FALSE, TRUE, sizeof (guint), 16);
 
@@ -984,6 +1132,10 @@ spi_atk_register_event_listeners (void)
                        "Gtk:AtkText:text-selection-changed");
   add_signal_listener (text_changed_event_listener,
                        "Gtk:AtkText:text-changed");
+  add_signal_listener (text_insert_event_listener,
+                       "Gtk:AtkText:text-insert");
+  add_signal_listener (text_remove_event_listener,
+                       "Gtk:AtkText:text-remove");
   add_signal_listener (link_selected_event_listener,
                        "Gtk:AtkHypertext:link-selected");
   add_signal_listener (generic_event_listener,
@@ -1003,10 +1155,7 @@ spi_atk_register_event_listeners (void)
                        "Gtk:AtkTable:column-reordered");
   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");
+  add_signal_listener (children_changed_event_listener, "Gtk:AtkObject:children-changed");
 
 #if 0
   g_signal_connect (G_OBJECT (spi_global_app_data->root),
@@ -1041,15 +1190,22 @@ spi_atk_deregister_event_listeners (void)
   listener_ids = NULL;
 
   if (atk_bridge_focus_tracker_id)
+  {
     atk_remove_focus_tracker (atk_bridge_focus_tracker_id);
+    atk_bridge_focus_tracker_id = 0;
+  }
 
   for (i = 0; ids && i < ids->len; i++)
     {
       atk_remove_global_event_listener (g_array_index (ids, guint, i));
     }
+  g_array_free (ids, TRUE);
 
   if (atk_bridge_key_event_listener_id)
+  {
     atk_remove_key_event_listener (atk_bridge_key_event_listener_id);
+    atk_bridge_key_event_listener_id = 0;
+  }
 }
 
 /*---------------------------------------------------------------------------*/