Fixed issue: bus-launcher didn't start screen-reader after reader crash.
[platform/upstream/at-spi2-core.git] / bus / at-spi-bus-launcher.c
index ec506f5..73c8a73 100644 (file)
 #include <stdio.h>
 
 #include <gio/gio.h>
+#ifdef HAVE_X11
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
+#endif
+
+#define APP_CONTROL_OPERATION_SCREEN_READ "http://tizen.org/appcontrol/operation/read_screen"
+#include <appsvc.h>
+#include <vconf.h>
+
+//uncomment if you want debug
+//#ifndef TIZEN_ENGINEER_MODE
+//#define TIZEN_ENGINEER_MODE
+//#endif
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+
+#define LOG_TAG "ATSPI_BUS_LAUNCHER"
+
+#include <dlog.h>
+#include <aul.h>
+
+//uncomment this if you want log suring startup
+//seems like dlog is not working at startup time
+#define ATSPI_BUS_LAUNCHER_LOG_TO_FILE
+
+#ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
+FILE *log_file;
+#ifdef LOGD
+#undef LOGD
+#endif
+#define LOGD(arg...) do {if (log_file) {fprintf(log_file, ##arg);fprintf(log_file, "\n"); fflush(log_file);}} while(0)
+#endif
+
 
 typedef enum {
   A11Y_BUS_STATE_IDLE = 0,
@@ -49,7 +81,12 @@ typedef struct {
   GSettings *a11y_schema;
   GSettings *interface_schema;
 
+  int launch_screen_reader_repeats;
+  gboolean screen_reader_needed;
+  int pid;
+
   A11yBusState state;
+
   /* -1 == error, 0 == pending, > 0 == running */
   int a11y_bus_pid;
   char *a11y_bus_address;
@@ -172,7 +209,7 @@ ensure_a11y_bus (A11yBusLauncher *app)
 
   app->state = A11Y_BUS_STATE_READING_ADDRESS;
   app->a11y_bus_pid = pid;
-  g_debug ("Launched a11y bus, child is %ld", (long) pid);
+  LOGD("Launched a11y bus, child is %ld", (long) pid);
   if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
     {
       app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
@@ -185,8 +222,9 @@ ensure_a11y_bus (A11yBusLauncher *app)
 
   /* Trim the trailing newline */
   app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
-  g_debug ("a11y bus address: %s", app->a11y_bus_address);
+  LOGD("a11y bus address: %s", app->a11y_bus_address);
 
+#ifdef HAVE_X11
   {
     Display *display = XOpenDisplay (NULL);
     if (display)
@@ -201,6 +239,7 @@ ensure_a11y_bus (A11yBusLauncher *app)
         XCloseDisplay (display);
       }
   }
+#endif
 
   return TRUE;
   
@@ -492,6 +531,7 @@ init_sigterm_handling (A11yBusLauncher *app)
 static gboolean
 already_running ()
 {
+#ifdef HAVE_X11
   Atom AT_SPI_BUS;
   Atom actual_type;
   Display *bridge_display;
@@ -516,11 +556,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;
@@ -530,6 +567,9 @@ already_running ()
 
   XCloseDisplay (bridge_display);
   return result;
+#else
+  return FALSE;
+#endif
 }
 
 static GSettings *
@@ -560,10 +600,137 @@ 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(gpointer user_data)
+{
+   A11yBusLauncher *bl = user_data;
+   LOGD("Launching screen reader");
+
+   bundle *kb = NULL;
+   gboolean ret = FALSE;
+
+   kb = bundle_create();
+
+   if (kb == NULL)
+     {
+        LOGD("Can't create bundle");
+        return FALSE;
+     }
+   int operation_error = appsvc_set_operation(kb, APP_CONTROL_OPERATION_SCREEN_READ);
+   LOGD("appsvc_set_operation: %i", operation_error);
+
+   bl->pid = appsvc_run_service(kb, 0, NULL, NULL);
+
+   if (bl->pid > 0)
+     {
+        LOGD("Screen reader launched with pid: %i", bl->pid);
+        ret = TRUE;
+     }
+   else
+     {
+        LOGD("Can't start screen-reader - error code: %i", bl->pid);
+     }
+
+
+   bundle_free(kb);
+   return ret;
+}
+
+static gboolean
+_launch_screen_reader_repeat_until_success(gpointer user_data) {
+    A11yBusLauncher *bl = user_data;
+
+    if (bl->launch_screen_reader_repeats > 100 || bl->pid > 0)
+      {
+         //do not try anymore
+         return FALSE;
+      }
+
+    gboolean ret = _launch_screen_reader(user_data);
+
+    if (ret)
+      {
+         //we managed to
+         bl->launch_screen_reader_repeats = 0;
+         return FALSE;
+      }
+    //try again
+    return TRUE;
+}
+
+static gboolean
+_terminate_screen_reader(A11yBusLauncher *bl)
+{
+   LOGD("Terminating screen reader");
+   int ret;
+   if (bl->pid <= 0)
+     return FALSE;
+
+
+   int status = aul_app_get_status_bypid(bl->pid);
+
+   if (status < 0)
+     {
+       LOGD("App with pid %d already terminated", bl->pid);
+       bl->pid = 0;
+       return TRUE;
+     }
+
+   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);
+   LOGD("vconf_keynode_get_bool(node): %i", ret);
+   if (ret < 0)
+     return;
+
+   //check if process really exists (e.g didn't crash)
+   if (bl->pid > 0)
+     {
+        int err = kill(bl->pid,0);
+        //process doesn't exist
+        if (err == ESRCH)
+          bl->pid = 0;
+     }
+
+   bl->screen_reader_needed = ret;
+   LOGD("bl->screen_reader_needed: %i, bl->pid: %i", ret, bl->pid);
+   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)
 {
+#ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
+  log_file = fopen("/tmp/at-spi-bus-launcher.log", "a");
+#endif
+
+  LOGD("Starting atspi bus launcher");
   GError *error = NULL;
   GMainLoop *loop;
   GDBusConnection *session_bus;
@@ -572,13 +739,15 @@ main (int    argc,
   gboolean screen_reader_set = FALSE;
   gint i;
 
-  g_type_init ();
-
   if (already_running ())
-    return 0;
+    {
+       LOGD("atspi bus launcher is already running");
+       return 0;
+    }
 
   _global_app = g_slice_new0 (A11yBusLauncher);
   _global_app->loop = g_main_loop_new (NULL, FALSE);
+  _global_app->launch_screen_reader_repeats = 0;
 
   for (i = 1; i < argc; i++)
     {
@@ -634,6 +803,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)
+    g_timeout_add_seconds(2,_launch_screen_reader_repeat_until_success, _global_app);
+
   g_main_loop_run (_global_app->loop);
 
   if (_global_app->a11y_bus_pid > 0)
@@ -643,6 +827,7 @@ main (int    argc,
    * GDM is launching a login on an X server it was using before,
    * we don't want early login processes to pick up the stale address.
    */
+#ifdef HAVE_X11
   {
     Display *display = XOpenDisplay (NULL);
     if (display)
@@ -656,6 +841,7 @@ main (int    argc,
         XCloseDisplay (display);
       }
   }
+#endif
 
   if (_global_app->a11y_launch_error_message)
     {