Using proper vconf key - VCONFKEY_SETAPPL_ACCESSIBILITY_TTS
[platform/upstream/at-spi2-core.git] / bus / at-spi-bus-launcher.c
index f7832dd..98a0098 100644 (file)
 #include <X11/Xatom.h>
 #endif
 
+#define APP_CONTROL_OPERATION_SCREEN_READ "http://tizen.org/appcontrol/operation/read_screen"
+#include <appsvc.h>
+#include <vconf.h>
+#include <dlog.h>
+#include <aul.h>
+
 typedef enum {
   A11Y_BUS_STATE_IDLE = 0,
   A11Y_BUS_STATE_READING_ADDRESS,
@@ -51,7 +57,11 @@ typedef struct {
   GSettings *a11y_schema;
   GSettings *interface_schema;
 
+  gboolean screen_reader_needed;
+  int pid;
+
   A11yBusState state;
+
   /* -1 == error, 0 == pending, > 0 == running */
   int a11y_bus_pid;
   char *a11y_bus_address;
@@ -521,11 +531,8 @@ already_running ()
   if (data)
   {
     GDBusConnection *bus;
-    const gchar *old_session = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
-    /* TODO: Is there a better way to connect? This is really hacky */
-    g_setenv ("DBUS_SESSION_BUS_ADDRESS", data, TRUE);
-    bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
-    g_setenv ("DBUS_SESSION_BUS_ADDRESS", old_session, TRUE);
+    bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
+                                                  NULL, NULL, NULL);
     if (bus != NULL)
       {
         result = TRUE;
@@ -568,6 +575,70 @@ gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
     handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
 }
 
+static gboolean
+_launch_screen_reader(A11yBusLauncher *bl)
+{
+   bundle *kb = NULL;
+   gboolean ret = FALSE;
+
+   kb = bundle_create();
+   if (kb == NULL)
+     return FALSE;
+
+   appsvc_set_operation(kb, APP_CONTROL_OPERATION_SCREEN_READ);
+
+   bl->pid = appsvc_usr_run_service(kb, 0, NULL, NULL, getuid());
+   if (bl->pid >= 0) {
+       ret = TRUE;
+   }
+
+   bundle_free(kb);
+   return ret;
+}
+
+static gboolean
+_terminate_screen_reader(A11yBusLauncher *bl)
+{
+   int ret;
+   if (bl->pid <= 0)
+     return FALSE;
+
+   LOGD("terminate process with pid %d", bl->pid);
+   if (!aul_terminate_pid(bl->pid))
+     {
+        bl->pid = 0;
+        return TRUE;
+     }
+
+   LOGD("Unable to terminate process using aul api. Sending SIGTERM signal");
+   ret = kill(bl->pid, SIGTERM);
+   if (!ret)
+     {
+        bl->pid = 0;
+        return TRUE;
+     }
+
+   LOGD("Unable to terminate process: %d with api or signal.", bl->pid);
+   return FALSE;
+}
+
+void screen_reader_cb(keynode_t *node, void *user_data)
+{
+   A11yBusLauncher *bl = user_data;
+   int ret;
+
+   ret = vconf_keynode_get_bool(node);
+   if (ret < 0)
+     return;
+
+   bl->screen_reader_needed = ret;
+
+   if (!bl->screen_reader_needed && (bl->pid > 0))
+     _terminate_screen_reader(bl);
+   else if (bl->screen_reader_needed && (bl->pid <= 0))
+     _launch_screen_reader(bl);
+}
+
 int
 main (int    argc,
       char **argv)
@@ -580,8 +651,6 @@ main (int    argc,
   gboolean screen_reader_set = FALSE;
   gint i;
 
-  g_type_init ();
-
   if (already_running ())
     return 0;
 
@@ -642,6 +711,21 @@ main (int    argc,
                                   _global_app,
                                   NULL);
 
+  int ret = vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &_global_app->screen_reader_needed);
+  if (ret != 0)
+    {
+      LOGD("Could not read VCONFKEY_SETAPPL_ACCESSIBILITY_TTS key value.\n");
+      return FALSE;
+    }
+  ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, screen_reader_cb, _global_app);
+  if(ret != 0)
+    {
+      LOGD("Could not add information level callback\n");
+      return FALSE;
+    }
+  if (_global_app->screen_reader_needed)
+    _launch_screen_reader(_global_app);
+
   g_main_loop_run (_global_app->loop);
 
   if (_global_app->a11y_bus_pid > 0)