Support many at-spi clients
[platform/upstream/at-spi2-core.git] / bus / at-spi-bus-launcher.c
index 87b2da7..bb6f9f9 100644 (file)
@@ -78,6 +78,7 @@ typedef struct {
   gboolean launch_immediately;
   gboolean a11y_enabled;
   gboolean screen_reader_enabled;
+  GHashTable *client_watcher_id;
   GDBusConnection *session_bus;
   GSettings *a11y_schema;
   GSettings *interface_schema;
@@ -344,11 +345,6 @@ handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
   if (enabled == app->screen_reader_enabled)
     return;
 
-  /* If the screen reader is being enabled, we should enable accessibility
-   * if it isn't enabled already */
-  if (enabled)
-    handle_a11y_enabled_change (app, enabled, notify_gsettings);
-
   app->screen_reader_enabled = enabled;
 
   if (notify_gsettings && app->a11y_schema)
@@ -375,6 +371,61 @@ handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
 }
 
 static gboolean
+is_client_connected(A11yBusLauncher *app)
+{
+  guint watchers = g_hash_table_size(app->client_watcher_id);
+  LOGD("clients connected: %d", watchers);
+  return watchers > 0;
+}
+
+static void
+remove_client_watch(A11yBusLauncher *app,
+                                  const gchar     *sender)
+{
+  LOGD("Remove client watcher for %s", sender);
+  guint watcher_id = GPOINTER_TO_UINT(g_hash_table_lookup(app->client_watcher_id, sender));
+  if (watcher_id)
+    g_bus_unwatch_name(watcher_id);
+
+  g_hash_table_remove(app->client_watcher_id, sender);
+  if (!is_client_connected(app))
+    handle_a11y_enabled_change (app, FALSE, TRUE);
+}
+
+static void
+on_client_name_vanished (GDBusConnection *connection,
+                                       const gchar     *name,
+                                       gpointer         user_data)
+{
+  A11yBusLauncher *app = user_data;
+  remove_client_watch(app, name);
+}
+
+static void
+add_client_watch(A11yBusLauncher *app,
+                               const gchar     *sender)
+{
+  LOGD("Add client watcher for %s", sender);
+
+  if (g_hash_table_contains(app->client_watcher_id, sender))
+    {
+      LOGI("Watcher for %s already registered", sender);
+      return;
+    }
+
+  guint watcher_id = g_bus_watch_name(G_BUS_TYPE_SESSION,
+                     sender,
+                     G_BUS_NAME_WATCHER_FLAGS_NONE,
+                     NULL,
+                     on_client_name_vanished,
+                     app,
+                     NULL);
+
+  g_hash_table_insert(app->client_watcher_id, g_strdup(sender), GUINT_TO_POINTER(watcher_id));
+  handle_a11y_enabled_change (app, TRUE, TRUE);
+}
+
+static gboolean
 handle_set_property  (GDBusConnection       *connection,
                       const gchar           *sender,
                       const gchar           *object_path,
@@ -399,7 +450,10 @@ handle_set_property  (GDBusConnection       *connection,
 
   if (g_strcmp0 (property_name, "IsEnabled") == 0)
     {
-      handle_a11y_enabled_change (app, enabled, TRUE);
+      if (enabled)
+        add_client_watch(app, sender);
+      else
+        remove_client_watch(app, sender);
       return TRUE;
     }
   else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
@@ -600,7 +654,6 @@ static void
 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
 {
   gboolean new_val = g_settings_get_boolean (gsettings, key);
-  A11yBusLauncher *app = user_data;
 
   if (!strcmp (key, "toolkit-accessibility"))
     handle_a11y_enabled_change (_global_app, new_val, FALSE);
@@ -772,9 +825,6 @@ main (int    argc,
 #endif
 
   LOGD("Starting atspi bus launcher");
-  GError *error = NULL;
-  GMainLoop *loop;
-  GDBusConnection *session_bus;
   int name_owner_id;
   gboolean a11y_set = FALSE;
   gboolean screen_reader_set = FALSE;
@@ -789,6 +839,7 @@ main (int    argc,
   _global_app = g_slice_new0 (A11yBusLauncher);
   _global_app->loop = g_main_loop_new (NULL, FALSE);
   _global_app->launch_screen_reader_repeats = 0;
+  _global_app->client_watcher_id = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
 
   for (i = 1; i < argc; i++)
     {