3f6c91734976ace97cf13763468cc03ab39b7e5b
[platform/upstream/at-spi2-core.git] / bus / at-spi-bus-launcher.c
1 /* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * 
3  * at-spi-bus-launcher: Manage the a11y bus as a child process 
4  *
5  * Copyright 2011 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "config.h"
24
25 #include <unistd.h>
26 #include <string.h>
27 #include <signal.h>
28 #include <sys/wait.h>
29 #include <errno.h>
30 #include <stdio.h>
31
32 #include <gio/gio.h>
33 #ifdef HAVE_X11
34 #include <X11/Xlib.h>
35 #include <X11/Xatom.h>
36 #endif
37
38 #define APP_CONTROL_OPERATION_SCREEN_READ "http://tizen.org/appcontrol/operation/read_screen"
39 #include <appsvc.h>
40 #include <vconf.h>
41
42 //uncomment if you want debug
43 //#ifndef TIZEN_ENGINEER_MODE
44 //#define TIZEN_ENGINEER_MODE
45 //#endif
46 #ifdef LOG_TAG
47 #undef LOG_TAG
48 #endif
49
50 #define LOG_TAG "ATSPI_BUS_LAUNCHER"
51
52 #include <dlog.h>
53 #include <aul.h>
54
55 //uncomment this if you want log suring startup
56 //seems like dlog is not working at startup time
57 #define ATSPI_BUS_LAUNCHER_LOG_TO_FILE
58
59 #ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
60 FILE *log_file;
61 #ifdef LOGD
62 #undef LOGD
63 #endif
64 #define LOGD(arg...) do {if (log_file) {fprintf(log_file, ##arg);fprintf(log_file, "\n"); fflush(log_file);}} while(0)
65 #endif
66
67
68 typedef enum {
69   A11Y_BUS_STATE_IDLE = 0,
70   A11Y_BUS_STATE_READING_ADDRESS,
71   A11Y_BUS_STATE_RUNNING,
72   A11Y_BUS_STATE_ERROR
73 } A11yBusState;
74
75 typedef struct {
76   GMainLoop *loop;
77   gboolean launch_immediately;
78   gboolean a11y_enabled;
79   gboolean screen_reader_enabled;
80   GDBusConnection *session_bus;
81   GSettings *a11y_schema;
82   GSettings *interface_schema;
83
84   int launch_screen_reader_repeats;
85   gboolean screen_reader_needed;
86   int pid;
87
88   A11yBusState state;
89
90   /* -1 == error, 0 == pending, > 0 == running */
91   int a11y_bus_pid;
92   char *a11y_bus_address;
93   int pipefd[2];
94   char *a11y_launch_error_message;
95 } A11yBusLauncher;
96
97 static A11yBusLauncher *_global_app = NULL;
98
99 static const gchar introspection_xml[] =
100   "<node>"
101   "  <interface name='org.a11y.Bus'>"
102   "    <method name='GetAddress'>"
103   "      <arg type='s' name='address' direction='out'/>"
104   "    </method>"
105   "  </interface>"
106   "<interface name='org.a11y.Status'>"
107   "<property name='IsEnabled' type='b' access='readwrite'/>"
108   "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
109   "</interface>"
110   "</node>";
111 static GDBusNodeInfo *introspection_data = NULL;
112
113 static void
114 setup_bus_child (gpointer data)
115 {
116   A11yBusLauncher *app = data;
117   (void) app;
118
119   close (app->pipefd[0]);
120   dup2 (app->pipefd[1], 3);
121   close (app->pipefd[1]);
122
123   /* On Linux, tell the bus process to exit if this process goes away */
124 #ifdef __linux
125 #include <sys/prctl.h>
126   prctl (PR_SET_PDEATHSIG, 15);
127 #endif  
128 }
129
130 /**
131  * unix_read_all_fd_to_string:
132  *
133  * Read all data from a file descriptor to a C string buffer.
134  */
135 static gboolean
136 unix_read_all_fd_to_string (int      fd,
137                             char    *buf,
138                             ssize_t  max_bytes)
139 {
140   ssize_t bytes_read;
141
142   while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
143     {
144       if (bytes_read < 0)
145         return FALSE;
146       buf += bytes_read;
147       max_bytes -= bytes_read;
148     }
149   *buf = '\0';
150   return TRUE;
151 }
152
153 static void
154 on_bus_exited (GPid     pid,
155                gint     status,
156                gpointer data)
157 {
158   A11yBusLauncher *app = data;
159   
160   app->a11y_bus_pid = -1;
161   app->state = A11Y_BUS_STATE_ERROR;
162   if (app->a11y_launch_error_message == NULL)
163     {
164       if (WIFEXITED (status))
165         app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
166       else if (WIFSIGNALED (status))
167         app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
168       else if (WIFSTOPPED (status))
169         app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
170     }
171   g_main_loop_quit (app->loop);
172
173
174 static gboolean
175 ensure_a11y_bus (A11yBusLauncher *app)
176 {
177   GPid pid;
178   char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
179   char addr_buf[2048];
180   GError *error = NULL;
181
182   if (app->a11y_bus_pid != 0)
183     return FALSE;
184   
185   argv[1] = g_strdup_printf ("--config-file=%s/at-spi2/accessibility.conf", SYSCONFDIR);
186
187   if (pipe (app->pipefd) < 0)
188     g_error ("Failed to create pipe: %s", strerror (errno));
189   
190   if (!g_spawn_async (NULL,
191                       argv,
192                       NULL,
193                       G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
194                       setup_bus_child,
195                       app,
196                       &pid,
197                       &error))
198     {
199       app->a11y_bus_pid = -1;
200       app->a11y_launch_error_message = g_strdup (error->message);
201       g_clear_error (&error);
202       goto error;
203     }
204
205   close (app->pipefd[1]);
206   app->pipefd[1] = -1;
207
208   g_child_watch_add (pid, on_bus_exited, app);
209
210   app->state = A11Y_BUS_STATE_READING_ADDRESS;
211   app->a11y_bus_pid = pid;
212   LOGD("Launched a11y bus, child is %ld", (long) pid);
213   if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
214     {
215       app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
216       kill (app->a11y_bus_pid, SIGTERM);
217       goto error;
218     }
219   close (app->pipefd[0]);
220   app->pipefd[0] = -1;
221   app->state = A11Y_BUS_STATE_RUNNING;
222
223   /* Trim the trailing newline */
224   app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
225   LOGD("a11y bus address: %s", app->a11y_bus_address);
226
227 #ifdef HAVE_X11
228   {
229     Display *display = XOpenDisplay (NULL);
230     if (display)
231       {
232         Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
233         XChangeProperty (display,
234                          XDefaultRootWindow (display),
235                          bus_address_atom,
236                          XA_STRING, 8, PropModeReplace,
237                          (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
238         XFlush (display);
239         XCloseDisplay (display);
240       }
241   }
242 #endif
243
244   return TRUE;
245   
246  error:
247   close (app->pipefd[0]);
248   close (app->pipefd[1]);
249   app->state = A11Y_BUS_STATE_ERROR;
250
251   return FALSE;
252 }
253
254 static void
255 handle_method_call (GDBusConnection       *connection,
256                     const gchar           *sender,
257                     const gchar           *object_path,
258                     const gchar           *interface_name,
259                     const gchar           *method_name,
260                     GVariant              *parameters,
261                     GDBusMethodInvocation *invocation,
262                     gpointer               user_data)
263 {
264   A11yBusLauncher *app = user_data;
265
266   if (g_strcmp0 (method_name, "GetAddress") == 0)
267     {
268       ensure_a11y_bus (app);
269       if (app->a11y_bus_pid > 0)
270         g_dbus_method_invocation_return_value (invocation,
271                                                g_variant_new ("(s)", app->a11y_bus_address));
272       else
273         g_dbus_method_invocation_return_dbus_error (invocation,
274                                                     "org.a11y.Bus.Error",
275                                                     app->a11y_launch_error_message);
276     }
277 }
278
279 static GVariant *
280 handle_get_property  (GDBusConnection       *connection,
281                       const gchar           *sender,
282                       const gchar           *object_path,
283                       const gchar           *interface_name,
284                       const gchar           *property_name,
285                     GError **error,
286                     gpointer               user_data)
287 {
288   A11yBusLauncher *app = user_data;
289
290   if (g_strcmp0 (property_name, "IsEnabled") == 0)
291     return g_variant_new ("b", app->a11y_enabled);
292   else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
293     return g_variant_new ("b", app->screen_reader_enabled);
294   else
295     return NULL;
296 }
297
298 static void
299 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
300                                gboolean notify_gsettings)
301 {
302   GVariantBuilder builder;
303   GVariantBuilder invalidated_builder;
304
305   if (enabled == app->a11y_enabled)
306     return;
307
308   app->a11y_enabled = enabled;
309
310   if (notify_gsettings && app->interface_schema)
311     {
312       g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
313                               enabled);
314       g_settings_sync ();
315     }
316
317   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
318   g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
319   g_variant_builder_add (&builder, "{sv}", "IsEnabled",
320                          g_variant_new_boolean (enabled));
321
322   g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
323                                  "org.freedesktop.DBus.Properties",
324                                  "PropertiesChanged",
325                                  g_variant_new ("(sa{sv}as)", "org.a11y.Status",
326                                                 &builder,
327                                                 &invalidated_builder),
328                                  NULL);
329 }
330
331 static void
332 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
333                                gboolean notify_gsettings)
334 {
335   GVariantBuilder builder;
336   GVariantBuilder invalidated_builder;
337
338   if (enabled == app->screen_reader_enabled)
339     return;
340
341   /* If the screen reader is being enabled, we should enable accessibility
342    * if it isn't enabled already */
343   if (enabled)
344     handle_a11y_enabled_change (app, enabled, notify_gsettings);
345
346   app->screen_reader_enabled = enabled;
347
348   if (notify_gsettings && app->a11y_schema)
349     {
350       g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
351                               enabled);
352       g_settings_sync ();
353     }
354
355   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
356   g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
357   g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
358                          g_variant_new_boolean (enabled));
359
360   g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
361                                  "org.freedesktop.DBus.Properties",
362                                  "PropertiesChanged",
363                                  g_variant_new ("(sa{sv}as)", "org.a11y.Status",
364                                                 &builder,
365                                                 &invalidated_builder),
366                                  NULL);
367 }
368
369 static gboolean
370 handle_set_property  (GDBusConnection       *connection,
371                       const gchar           *sender,
372                       const gchar           *object_path,
373                       const gchar           *interface_name,
374                       const gchar           *property_name,
375                       GVariant *value,
376                     GError **error,
377                     gpointer               user_data)
378 {
379   A11yBusLauncher *app = user_data;
380   const gchar *type = g_variant_get_type_string (value);
381   gboolean enabled;
382   
383   if (g_strcmp0 (type, "b") != 0)
384     {
385       g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
386                        "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
387       return FALSE;
388     }
389
390   enabled = g_variant_get_boolean (value);
391
392   if (g_strcmp0 (property_name, "IsEnabled") == 0)
393     {
394       handle_a11y_enabled_change (app, enabled, TRUE);
395       return TRUE;
396     }
397   else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
398     {
399       handle_screen_reader_enabled_change (app, enabled, TRUE);
400       return TRUE;
401     }
402   else
403     {
404       g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
405                        "Unknown property '%s'", property_name);
406       return FALSE;
407     }
408 }
409
410 static const GDBusInterfaceVTable bus_vtable =
411 {
412   handle_method_call,
413   NULL, /* handle_get_property, */
414   NULL  /* handle_set_property */
415 };
416
417 static const GDBusInterfaceVTable status_vtable =
418 {
419   NULL, /* handle_method_call */
420   handle_get_property,
421   handle_set_property
422 };
423
424 static void
425 on_bus_acquired (GDBusConnection *connection,
426                  const gchar     *name,
427                  gpointer         user_data)
428 {
429   A11yBusLauncher *app = user_data;
430   GError *error;
431   guint registration_id;
432   
433   if (connection == NULL)
434     {
435       g_main_loop_quit (app->loop);
436       return;
437     }
438   app->session_bus = connection;
439
440   if (app->launch_immediately)
441     {
442       ensure_a11y_bus (app);
443       if (app->state == A11Y_BUS_STATE_ERROR)
444         {
445           g_main_loop_quit (app->loop);
446           return;
447         }
448     }
449
450   error = NULL;
451   registration_id = g_dbus_connection_register_object (connection,
452                                                        "/org/a11y/bus",
453                                                        introspection_data->interfaces[0],
454                                                        &bus_vtable,
455                                                        _global_app,
456                                                        NULL,
457                                                        &error);
458   if (registration_id == 0)
459     {
460       g_error ("%s", error->message);
461       g_clear_error (&error);
462     }
463
464   g_dbus_connection_register_object (connection,
465                                      "/org/a11y/bus",
466                                      introspection_data->interfaces[1],
467                                      &status_vtable,
468                                      _global_app,
469                                      NULL,
470                                      NULL);
471 }
472
473 static void
474 on_name_lost (GDBusConnection *connection,
475               const gchar     *name,
476               gpointer         user_data)
477 {
478   A11yBusLauncher *app = user_data;
479   if (app->session_bus == NULL
480       && connection == NULL
481       && app->a11y_launch_error_message == NULL)
482     app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
483   g_main_loop_quit (app->loop);
484 }
485
486 static void
487 on_name_acquired (GDBusConnection *connection,
488                   const gchar     *name,
489                   gpointer         user_data)
490 {
491   A11yBusLauncher *app = user_data;
492   (void) app;
493 }
494
495 static int sigterm_pipefd[2];
496
497 static void
498 sigterm_handler (int signum)
499 {
500   write (sigterm_pipefd[1], "X", 1);
501 }
502
503 static gboolean
504 on_sigterm_pipe (GIOChannel  *channel,
505                  GIOCondition condition,
506                  gpointer     data)
507 {
508   A11yBusLauncher *app = data;
509   
510   g_main_loop_quit (app->loop);
511
512   return FALSE;
513 }
514
515 static void
516 init_sigterm_handling (A11yBusLauncher *app)
517 {
518   GIOChannel *sigterm_channel;
519
520   if (pipe (sigterm_pipefd) < 0)
521     g_error ("Failed to create pipe: %s", strerror (errno));
522   signal (SIGTERM, sigterm_handler);
523
524   sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
525   g_io_add_watch (sigterm_channel,
526                   G_IO_IN | G_IO_ERR | G_IO_HUP,
527                   on_sigterm_pipe,
528                   app);
529 }
530
531 static gboolean
532 already_running ()
533 {
534 #ifdef HAVE_X11
535   Atom AT_SPI_BUS;
536   Atom actual_type;
537   Display *bridge_display;
538   int actual_format;
539   unsigned char *data = NULL;
540   unsigned long nitems;
541   unsigned long leftover;
542   gboolean result = FALSE;
543
544   bridge_display = XOpenDisplay (NULL);
545   if (!bridge_display)
546               return FALSE;
547       
548   AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
549   XGetWindowProperty (bridge_display,
550                       XDefaultRootWindow (bridge_display),
551                       AT_SPI_BUS, 0L,
552                       (long) BUFSIZ, False,
553                       (Atom) 31, &actual_type, &actual_format,
554                       &nitems, &leftover, &data);
555
556   if (data)
557   {
558     GDBusConnection *bus;
559     bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
560                                                   NULL, NULL, NULL);
561     if (bus != NULL)
562       {
563         result = TRUE;
564         g_object_unref (bus);
565       }
566   }
567
568   XCloseDisplay (bridge_display);
569   return result;
570 #else
571   return FALSE;
572 #endif
573 }
574
575 static GSettings *
576 get_schema (const gchar *name)
577 {
578   const char * const *schemas = NULL;
579   gint i;
580
581   schemas = g_settings_list_schemas ();
582   for (i = 0; schemas[i]; i++)
583   {
584     if (!strcmp (schemas[i], name))
585       return g_settings_new (schemas[i]);
586   }
587
588   return NULL;
589 }
590
591 static void
592 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
593 {
594   gboolean new_val = g_settings_get_boolean (gsettings, key);
595   A11yBusLauncher *app = user_data;
596
597   if (!strcmp (key, "toolkit-accessibility"))
598     handle_a11y_enabled_change (_global_app, new_val, FALSE);
599   else if (!strcmp (key, "screen-reader-enabled"))
600     handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
601 }
602
603 static gboolean
604 _launch_screen_reader(gpointer user_data)
605 {
606    A11yBusLauncher *bl = user_data;
607    LOGD("Launching screen reader");
608
609    bundle *kb = NULL;
610    gboolean ret = FALSE;
611
612    kb = bundle_create();
613
614    if (kb == NULL)
615      {
616         LOGD("Can't create bundle");
617         return FALSE;
618      }
619    int operation_error = appsvc_set_operation(kb, APP_CONTROL_OPERATION_SCREEN_READ);
620    LOGD("appsvc_set_operation: %i", operation_error);
621
622    bl->pid = appsvc_run_service(kb, 0, NULL, NULL);
623
624    if (bl->pid > 0)
625      {
626         LOGD("Screen reader launched with pid: %i", bl->pid);
627         ret = TRUE;
628      }
629    else
630      {
631         LOGD("Can't start screen-reader - error code: %i", bl->pid);
632      }
633
634
635    bundle_free(kb);
636    return ret;
637 }
638
639 static gboolean
640 _launch_screen_reader_repeat_until_success(gpointer user_data) {
641     A11yBusLauncher *bl = user_data;
642
643     if (bl->launch_screen_reader_repeats > 100 || bl->pid > 0)
644       {
645          //do not try anymore
646          return FALSE;
647       }
648
649     gboolean ret = _launch_screen_reader(user_data);
650
651     if (ret)
652       {
653          //we managed to
654          bl->launch_screen_reader_repeats = 0;
655          return FALSE;
656       }
657     //try again
658     return TRUE;
659 }
660
661 static gboolean
662 _terminate_screen_reader(A11yBusLauncher *bl)
663 {
664    LOGD("Terminating screen reader");
665    int ret;
666    int ret_aul;
667    if (bl->pid <= 0)
668      return FALSE;
669
670
671    int status = aul_app_get_status_bypid(bl->pid);
672
673    if (status < 0)
674      {
675        LOGD("App with pid %d already terminated", bl->pid);
676        bl->pid = 0;
677        return TRUE;
678      }
679
680    LOGD("terminate process with pid %d", bl->pid);
681    ret_aul = aul_terminate_pid(bl->pid);
682    if (ret_aul >= 0)
683      {
684         LOGD("Terminating with aul_terminate_pid: return is %d", ret_aul);
685         bl->pid = 0;
686         return TRUE;
687      }
688    else
689      LOGD("aul_terminate_pid failed: return is %d", ret_aul);
690
691    LOGD("Unable to terminate process using aul api. Sending SIGTERM signal");
692    ret = kill(bl->pid, SIGTERM);
693    if (!ret)
694      {
695         bl->pid = 0;
696         return TRUE;
697      }
698
699    LOGD("Unable to terminate process: %d with api or signal.", bl->pid);
700    return FALSE;
701 }
702
703 void screen_reader_cb(keynode_t *node, void *user_data)
704 {
705    A11yBusLauncher *bl = user_data;
706    int ret;
707
708    ret = vconf_keynode_get_bool(node);
709    LOGD("vconf_keynode_get_bool(node): %i", ret);
710    if (ret < 0)
711      return;
712
713    //check if process really exists (e.g didn't crash)
714    if (bl->pid > 0)
715      {
716         int err = kill(bl->pid,0);
717         //process doesn't exist
718         if (err == ESRCH)
719           bl->pid = 0;
720      }
721
722    bl->screen_reader_needed = ret;
723    LOGD("bl->screen_reader_needed: %i, bl->pid: %i", ret, bl->pid);
724    if (!bl->screen_reader_needed && (bl->pid > 0))
725      _terminate_screen_reader(bl);
726    else if (bl->screen_reader_needed && (bl->pid <= 0))
727      _launch_screen_reader(bl);
728 }
729
730 int
731 main (int    argc,
732       char **argv)
733 {
734 #ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
735   log_file = fopen("/tmp/at-spi-bus-launcher.log", "a");
736 #endif
737
738   LOGD("Starting atspi bus launcher");
739   GError *error = NULL;
740   GMainLoop *loop;
741   GDBusConnection *session_bus;
742   int name_owner_id;
743   gboolean a11y_set = FALSE;
744   gboolean screen_reader_set = FALSE;
745   gint i;
746
747   if (already_running ())
748     {
749        LOGD("atspi bus launcher is already running");
750        return 0;
751     }
752
753   _global_app = g_slice_new0 (A11yBusLauncher);
754   _global_app->loop = g_main_loop_new (NULL, FALSE);
755   _global_app->launch_screen_reader_repeats = 0;
756
757   for (i = 1; i < argc; i++)
758     {
759       if (!strcmp (argv[i], "--launch-immediately"))
760         _global_app->launch_immediately = TRUE;
761       else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 2)
762         a11y_set = TRUE;
763       else if (sscanf (argv[i], "--screen-reader=%d",
764                        &_global_app->screen_reader_enabled) == 2)
765         screen_reader_set = TRUE;
766     else
767       g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
768     }
769
770   _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
771   _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
772
773   if (!a11y_set)
774     {
775       _global_app->a11y_enabled = _global_app->interface_schema
776                                   ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
777                                   : _global_app->launch_immediately;
778     }
779
780   if (!screen_reader_set)
781     {
782       _global_app->screen_reader_enabled = _global_app->a11y_schema
783                                   ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
784                                   : FALSE;
785     }
786
787   if (_global_app->interface_schema)
788     g_signal_connect (_global_app->interface_schema,
789                       "changed::toolkit-accessibility",
790                       G_CALLBACK (gsettings_key_changed), _global_app);
791
792   if (_global_app->a11y_schema)
793     g_signal_connect (_global_app->a11y_schema,
794                       "changed::screen-reader-enabled",
795                       G_CALLBACK (gsettings_key_changed), _global_app);
796
797   init_sigterm_handling (_global_app);
798
799   introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
800   g_assert (introspection_data != NULL);
801
802   name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
803                                   "org.a11y.Bus",
804                                   G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
805                                   on_bus_acquired,
806                                   on_name_acquired,
807                                   on_name_lost,
808                                   _global_app,
809                                   NULL);
810
811   int ret = vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &_global_app->screen_reader_needed);
812   if (ret != 0)
813     {
814       LOGD("Could not read VCONFKEY_SETAPPL_ACCESSIBILITY_TTS key value.\n");
815       return FALSE;
816     }
817   ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, screen_reader_cb, _global_app);
818   if(ret != 0)
819     {
820       LOGD("Could not add information level callback\n");
821       return FALSE;
822     }
823   if (_global_app->screen_reader_needed)
824     g_timeout_add_seconds(2,_launch_screen_reader_repeat_until_success, _global_app);
825
826   g_main_loop_run (_global_app->loop);
827
828   if (_global_app->a11y_bus_pid > 0)
829     kill (_global_app->a11y_bus_pid, SIGTERM);
830
831   /* Clear the X property if our bus is gone; in the case where e.g. 
832    * GDM is launching a login on an X server it was using before,
833    * we don't want early login processes to pick up the stale address.
834    */
835 #ifdef HAVE_X11
836   {
837     Display *display = XOpenDisplay (NULL);
838     if (display)
839       {
840         Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
841         XDeleteProperty (display,
842                          XDefaultRootWindow (display),
843                          bus_address_atom);
844
845         XFlush (display);
846         XCloseDisplay (display);
847       }
848   }
849 #endif
850
851   if (_global_app->a11y_launch_error_message)
852     {
853       g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);
854       return 1;
855     }
856   return 0;
857 }