Prevent gnome-shell getting stuck on the login screen
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / event.c
index e97046b..586d196 100644 (file)
@@ -2,6 +2,7 @@
  * AT-SPI - Assistive Technology Service Provider Interface
  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
  *
+ * Copyright 2011, F123 Consulting & Mais Diferenças
  * Copyright 2008, 2009, Codethink Ltd.
  * Copyright 2001, 2002, 2003 Sun Microsystems Inc.,
  * Copyright 2001, 2002, 2003 Ximian, Inc.
  */
 
 #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 "spi-dbus.h"
+#include "event.h"
+#include "object.h"
 
 static GArray *listener_ids = NULL;
 
@@ -39,38 +44,87 @@ 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 
+{
+  DBusConnection *bus;
+  GMainLoop   *loop;
+  DBusMessage *reply;
+  guint timeout;
+} 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)
 {
-  void **replyptr = (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);
+}
+
+static gboolean
+timeout_reply (void *data)
+{
+  SpiReentrantCallClosure *closure = data;
 
-  *replyptr = dbus_pending_call_steal_reply (pending);
+  if (!dbus_connection_get_is_connected (closure->bus))
+    g_main_loop_quit (closure->loop);
+  closure->timeout = -1;
+  return FALSE;
 }
 
 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))
+  SpiReentrantCallClosure closure;
+  GMainContext *main_context;
+  GSource *source;
+
+  main_context = (g_getenv ("AT_SPI_CLIENT") ? NULL :
+                  spi_global_app_data->main_context);
+  closure.bus = bus;
+  closure.loop = g_main_loop_new (main_context, FALSE);
+  closure.reply = NULL;
+  switch_main_context (main_context);
+
+  if (!dbus_connection_send_with_reply (bus, message, &pending, 9000) || !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);
+  source = g_timeout_source_new (500);
+  g_source_set_callback (source, timeout_reply, &closure, NULL);
+  closure.timeout = g_source_attach (source, main_context);
+  g_source_unref (source);
+  g_main_loop_run  (closure.loop);
+  if (closure.timeout != -1)
+    g_source_destroy (source);
+  
+  g_main_loop_unref (closure.loop);
+  return closure.reply;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -83,7 +137,7 @@ send_and_allow_reentry (DBusConnection * bus, DBusMessage * message)
 
 static gboolean
 Accessibility_DeviceEventController_NotifyListenersSync (const
-                                                         Accessibility_DeviceEvent
+                                                         AtspiDeviceEvent
                                                          * key_event)
 {
   DBusMessage *message;
@@ -92,8 +146,8 @@ Accessibility_DeviceEventController_NotifyListenersSync (const
 
   message =
     dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
-                                  SPI_DBUS_PATH_DEC,
-                                  SPI_DBUS_INTERFACE_DEC,
+                                  ATSPI_DBUS_PATH_DEC,
+                                  ATSPI_DBUS_INTERFACE_DEC,
                                   "NotifyListenersSync");
 
   dbus_error_init (&error);
@@ -115,7 +169,7 @@ Accessibility_DeviceEventController_NotifyListenersSync (const
 }
 
 static void
-spi_init_keystroke_from_atk_key_event (Accessibility_DeviceEvent * keystroke,
+spi_init_keystroke_from_atk_key_event (AtspiDeviceEvent * keystroke,
                                        AtkKeyEventStruct * event)
 {
   keystroke->id = (dbus_int32_t) event->keyval;
@@ -141,10 +195,10 @@ spi_init_keystroke_from_atk_key_event (Accessibility_DeviceEvent * keystroke,
   switch (event->type)
     {
     case (ATK_KEY_EVENT_PRESS):
-      keystroke->type = Accessibility_KEY_PRESSED_EVENT;
+      keystroke->type = ATSPI_KEY_PRESSED;
       break;
     case (ATK_KEY_EVENT_RELEASE):
-      keystroke->type = Accessibility_KEY_RELEASED_EVENT;
+      keystroke->type = ATSPI_KEY_RELEASED;
       break;
     default:
       keystroke->type = 0;
@@ -164,7 +218,7 @@ static gint
 spi_atk_bridge_key_listener (AtkKeyEventStruct * event, gpointer data)
 {
   gboolean result;
-  Accessibility_DeviceEvent key_event;
+  AtspiDeviceEvent key_event;
 
   spi_init_keystroke_from_atk_key_event (&key_event, event);
 
@@ -197,7 +251,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)
@@ -206,6 +260,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:
@@ -222,7 +281,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);
@@ -258,6 +317,110 @@ 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: 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';
+  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;
+        }
+    }
+
+#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]);
+  return ret;
+}
+
 /*
  * Emits an AT-SPI event.
  * AT-SPI events names are split into three parts:
@@ -280,38 +443,46 @@ 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;
-  DBusMessageIter iter;
+  DBusMessageIter iter, iter_struct;
   
   if (!klass) klass = "";
   if (!major) major = "";
   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);
 
   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);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -329,7 +500,7 @@ focus_tracker (AtkObject * accessible)
 
 /*---------------------------------------------------------------------------*/
 
-#define PCHANGE "property-change"
+#define PCHANGE "PropertyChange"
 
 /* 
  * This handler handles the following ATK signals and
@@ -368,21 +539,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)
@@ -452,15 +629,15 @@ state_event_listener (GSignalInvocationHint * signal_hint,
   guint detail1;
 
   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
-  pname = g_strdup (g_value_get_string (&param_values[1]));
+  pname = g_value_get_string (&param_values[1]);
 
-  /* TODO - Possibly ignore a change to the 'defunct' state.
-   * This is because without reference counting defunct objects should be removed.
-   */
   detail1 = (g_value_get_boolean (&param_values[2])) ? 1 : 0;
   emit_event (accessible, ITF_EVENT_OBJECT, STATE_CHANGED, pname, detail1, 0,
               DBUS_TYPE_INT32_AS_STRING, 0, append_basic);
-  g_free (pname);
+
+  if (!g_strcmp0 (pname, "defunct"))
+    spi_register_deregister_object (spi_global_register, G_OBJECT (accessible),
+                                    TRUE);
   return TRUE;
 }
 
@@ -574,7 +751,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);
@@ -589,7 +765,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;
 }
 
@@ -608,7 +783,7 @@ link_selected_event_listener (GSignalInvocationHint * signal_hint,
   AtkObject *accessible;
   GSignalQuery signal_query;
   const gchar *name, *minor;
-  gint detail1;
+  gint detail1 = 0;
 
   g_signal_query (signal_hint->signal_id, &signal_query);
   name = signal_query.signal_name;
@@ -629,6 +804,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
@@ -640,7 +817,7 @@ text_changed_event_listener (GSignalInvocationHint * signal_hint,
   GSignalQuery signal_query;
   const gchar *name, *minor;
   gchar *selected;
-  gint detail1, detail2;
+  gint detail1 = 0, detail2 = 0;
 
   g_signal_query (signal_hint->signal_id, &signal_query);
   name = signal_query.signal_name;
@@ -659,9 +836,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 = 0, detail2 = 0;
+
+  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 = 0, detail2 = 0;
+
+  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;
 }
 
+
 /*---------------------------------------------------------------------------*/
 
 /* 
@@ -678,7 +955,7 @@ text_selection_changed_event_listener (GSignalInvocationHint * signal_hint,
   AtkObject *accessible;
   GSignalQuery signal_query;
   const gchar *name, *minor;
-  gint detail1, detail2;
+  gint detail1 = 0, detail2 = 0;
 
   g_signal_query (signal_hint->signal_id, &signal_query);
   name = signal_query.signal_name;
@@ -700,9 +977,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 = 0, 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.
@@ -742,13 +1092,17 @@ generic_event_listener (GSignalInvocationHint * signal_hint,
  * and stores the signal id returned so that the function may be
  * de-registered later.
  */
-static void
+static guint
 add_signal_listener (GSignalEmissionHook listener, const char *signal_name)
 {
   guint id;
 
   id = atk_add_global_event_listener (listener, signal_name);
-  g_array_append_val (listener_ids, id);
+
+  if (id > 0) /* id == 0 is a failure */
+    g_array_append_val (listener_ids, id);
+
+  return id;
 }
 
 /*
@@ -765,10 +1119,17 @@ spi_atk_register_event_listeners (void)
    */
   GObject *ao = g_object_new (ATK_TYPE_OBJECT, NULL);
   AtkObject *bo = atk_no_op_object_new (ao);
+  guint id = 0;
 
   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);
 
@@ -776,13 +1137,37 @@ spi_atk_register_event_listeners (void)
 
   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");
-  add_signal_listener (window_event_listener, "window:minimize");
-  add_signal_listener (window_event_listener, "window:maximize");
-  add_signal_listener (window_event_listener, "window:restore");
-  add_signal_listener (window_event_listener, "window:activate");
-  add_signal_listener (window_event_listener, "window:deactivate");
+
+  /* window events: we tentative try to register using the old format */
+  id = add_signal_listener (window_event_listener, "window:create");
+
+  if (id != 0)
+    {
+      /* If we are able to register using the old format, we assume
+       * that the ATK implementor is managing window events without
+       * AtkWindow. We can't use the opposite test because after
+       * including AtkWindow on ATK you would be able to register to
+       * that event, although the ATK implementor could or not use it.
+       */
+
+      add_signal_listener (window_event_listener, "window:destroy");
+      add_signal_listener (window_event_listener, "window:minimize");
+      add_signal_listener (window_event_listener, "window:maximize");
+      add_signal_listener (window_event_listener, "window:restore");
+      add_signal_listener (window_event_listener, "window:activate");
+      add_signal_listener (window_event_listener, "window:deactivate");
+    }
+  else
+    {
+      add_signal_listener (window_event_listener, "Atk:AtkWindow:create");
+      add_signal_listener (window_event_listener, "Atk:AtkWindow:destroy");
+      add_signal_listener (window_event_listener, "Atk:AtkWindow:minimize");
+      add_signal_listener (window_event_listener, "Atk:AtkWindow:maximize");
+      add_signal_listener (window_event_listener, "Atk:AtkWindow:restore");
+      add_signal_listener (window_event_listener, "Atk:AtkWindow:activate");
+      add_signal_listener (window_event_listener, "Atk:AtkWindow:deactivate");
+    }
+
   add_signal_listener (document_event_listener,
                        "Gtk:AtkDocument:load-complete");
   add_signal_listener (document_event_listener, "Gtk:AtkDocument:reload");
@@ -799,6 +1184,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,
@@ -818,6 +1207,17 @@ 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");
+  add_signal_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+
@@ -842,15 +1242,25 @@ 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++)
+  if (ids)
     {
-      atk_remove_global_event_listener (g_array_index (ids, guint, i));
+      for (i = 0; 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;
+  }
 }
 
 /*---------------------------------------------------------------------------*/
@@ -894,4 +1304,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------------------------------------------------------------------------*/