+static gchar *
+introspect_children_cb (const char *path, void *data)
+{
+ if (!strcmp (path, "/org/a11y/atspi/accessible"))
+ {
+ return g_strdup ("<node name=\"root\"/>\n");
+ /* TODO: Should we place the whole hierarchy here? */
+ }
+ return NULL;
+}
+
+static void
+handle_event_listener_registered (DBusConnection *bus, DBusMessage *message,
+ void *user_data)
+{
+ const char *name;
+ char *sender;
+
+ if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
+ DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
+ return;
+
+ add_event (sender, name);
+}
+
+static void
+remove_events (const char *bus_name, const char *event)
+{
+ gchar **remove_data;
+ GList *list;
+
+ remove_data = g_strsplit (event, ":", 3);
+ if (!remove_data)
+ {
+ return;
+ }
+
+ for (list = spi_global_app_data->events; list;)
+ {
+ event_data *evdata = list->data;
+ if (!g_strcmp0 (evdata->bus_name, bus_name) &&
+ spi_event_is_subtype (evdata->data, remove_data))
+ {
+ GList *events = spi_global_app_data->events;
+ g_strfreev (evdata->data);
+ g_free (evdata->bus_name);
+ g_free (evdata);
+ if (list->prev)
+ {
+ GList *next = list->next;
+ list->prev = g_list_remove (list->prev, evdata);
+ list = next;
+ }
+ else
+ {
+ spi_global_app_data->events = g_list_remove (events, evdata);
+ list = spi_global_app_data->events;
+ }
+ }
+ else
+ {
+ list = list->next;
+ }
+ }
+
+ g_strfreev (remove_data);
+}
+
+static void
+handle_event_listener_deregistered (DBusConnection *bus, DBusMessage *message,
+ void *user_data)
+{
+ gchar *name;
+ char *sender;
+
+ if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
+ DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
+ return;
+
+ remove_events (sender, name);
+}
+
+static void
+handle_device_listener_registered (DBusConnection *bus, DBusMessage *message,
+ void *user_data)
+{
+ char *sender;
+ DBusMessageIter iter, iter_struct;
+
+ if (strncmp (dbus_message_get_signature (message), "(s", 2) != 0)
+ {
+ g_warning ("atk-bridge: handle_device_listener_register: unknown signature");
+ return;
+ }
+
+ dbus_message_iter_init (message, &iter);
+ dbus_message_iter_recurse (&iter, &iter_struct);
+ dbus_message_iter_get_basic (&iter_struct, &sender);
+ spi_atk_add_client (sender);
+}
+
+static DBusHandlerResult
+signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
+{
+ const char *interface = dbus_message_get_interface (message);
+ const char *member = dbus_message_get_member (message);
+ DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ static gboolean registry_lost = FALSE;
+
+ if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ if (!strcmp (interface, ATSPI_DBUS_INTERFACE_REGISTRY))
+ {
+ result = DBUS_HANDLER_RESULT_HANDLED;
+ if (!strcmp (member, "EventListenerRegistered"))
+ handle_event_listener_registered (bus, message, user_data);
+ else if (!strcmp (member, "EventListenerDeregistered"))
+ handle_event_listener_deregistered (bus, message, user_data);
+ else
+ result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ }
+ else if (!strcmp (interface, ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER))
+ {
+ result = DBUS_HANDLER_RESULT_HANDLED;
+ if (!strcmp (member, "KeystrokeListenerRegistered"))
+ handle_device_listener_registered (bus, message, user_data);
+ else if (!strcmp (member, "DeviceListenerRegistered"))
+ handle_device_listener_registered (bus, message, user_data);
+ else
+ result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ }
+
+ if (!g_strcmp0(interface, DBUS_INTERFACE_DBUS) &&
+ !g_strcmp0(member, "NameOwnerChanged"))
+ {
+ char *name, *old, *new;
+ if (dbus_message_get_args (message, NULL,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_STRING, &old,
+ DBUS_TYPE_STRING, &new,
+ DBUS_TYPE_INVALID))
+ {
+ if (!strcmp (name, "org.a11y.atspi.Registry"))
+ {
+ if (registry_lost && !old[0])
+ {
+ register_application (spi_global_app_data);
+ registry_lost = FALSE;
+ }
+ else if (!new[0])
+ registry_lost = TRUE;
+ }
+ else if (*old != '\0' && *new == '\0')
+ spi_atk_remove_client (old);
+ }
+ }
+
+ return result;
+}
+
+int
+spi_atk_create_socket (SpiBridge *app)
+{
+#ifndef DISABLE_P2P
+ DBusServer *server;
+ DBusError error;
+
+ if (getuid () != 0)
+ {
+ app->app_tmp_dir = g_build_filename (g_get_user_runtime_dir (),
+ "at-spi2-XXXXXX", NULL);
+ if (!g_mkdtemp (app->app_tmp_dir))
+ {
+ g_free (app->app_tmp_dir);
+ app->app_tmp_dir = NULL;
+ return FALSE;
+ }
+ }
+
+ if (app->app_tmp_dir)
+ app->app_bus_addr = g_strdup_printf ("unix:path=%s/socket", app->app_tmp_dir);
+ else
+ app->app_bus_addr = g_strdup_printf ("unix:path=%s/at-spi2-socket-%d",
+ g_get_user_runtime_dir (), getpid ());
+
+ if (!spi_global_app_data->app_bus_addr)
+ return -1;
+
+ dbus_error_init(&error);
+ server = dbus_server_listen(spi_global_app_data->app_bus_addr, &error);
+ if (server == NULL)
+ {
+ g_warning ("atk-bridge: Couldn't listen on dbus server: %s", error.message);
+ dbus_error_free (&error);
+ spi_global_app_data->app_bus_addr [0] = '\0';
+ g_main_context_unref (spi_global_app_data->main_context);
+ spi_global_app_data->main_context = NULL;
+ return -1;
+ }
+
+ atspi_dbus_server_setup_with_g_main(server, NULL);
+ dbus_server_set_new_connection_function(server, new_connection_cb, NULL, NULL);
+
+ spi_global_app_data->server = server;
+#endif
+
+ return 0;
+}
+
+/*
+ * Checks the status of the environment variables
+ *
+ * At this moment it only checks NO_AT_BRIDGE
+ *
+ * Returns TRUE if there isn't anything on the environment preventing
+ * you to load the bridge, FALSE otherwise
+ */
+static gboolean
+check_envvar (void)
+{
+ const gchar *envvar;
+
+ envvar = g_getenv ("NO_AT_BRIDGE");
+
+ if (envvar && atoi (envvar) == 1)
+ return FALSE;
+ else
+ return TRUE;
+}
+
+void
+spi_atk_activate ()
+{
+ DRoutePath *treepath;
+
+ spi_atk_register_event_listeners ();
+ if (!spi_global_cache)
+ {
+ spi_global_cache = g_object_new (SPI_CACHE_TYPE, NULL);
+ treepath = droute_add_one (spi_global_app_data->droute,
+ "/org/a11y/atspi/cache", spi_global_cache);
+
+ if (!treepath)
+ {
+ g_warning ("atk-bridge: Error in droute_add_one(). Already running?");
+ return;
+ }
+ spi_initialize_cache (treepath);
+ if (spi_global_app_data->bus)
+ droute_path_register (treepath, spi_global_app_data->bus);
+ }
+}
+