db1db5c3be49359428b797a3aa09bd2da5d59c93
[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   if (argv[1]) g_free(argv[1]);
245
246   return TRUE;
247
248  error:
249   if (argv[1]) g_free(argv[1]);
250   close (app->pipefd[0]);
251   close (app->pipefd[1]);
252   app->state = A11Y_BUS_STATE_ERROR;
253
254   return FALSE;
255 }
256
257 static void
258 handle_method_call (GDBusConnection       *connection,
259                     const gchar           *sender,
260                     const gchar           *object_path,
261                     const gchar           *interface_name,
262                     const gchar           *method_name,
263                     GVariant              *parameters,
264                     GDBusMethodInvocation *invocation,
265                     gpointer               user_data)
266 {
267   A11yBusLauncher *app = user_data;
268
269   if (g_strcmp0 (method_name, "GetAddress") == 0)
270     {
271       ensure_a11y_bus (app);
272       if (app->a11y_bus_pid > 0)
273         g_dbus_method_invocation_return_value (invocation,
274                                                g_variant_new ("(s)", app->a11y_bus_address));
275       else
276         g_dbus_method_invocation_return_dbus_error (invocation,
277                                                     "org.a11y.Bus.Error",
278                                                     app->a11y_launch_error_message);
279     }
280 }
281
282 static GVariant *
283 handle_get_property  (GDBusConnection       *connection,
284                       const gchar           *sender,
285                       const gchar           *object_path,
286                       const gchar           *interface_name,
287                       const gchar           *property_name,
288                     GError **error,
289                     gpointer               user_data)
290 {
291   A11yBusLauncher *app = user_data;
292
293   if (g_strcmp0 (property_name, "IsEnabled") == 0)
294     return g_variant_new ("b", app->a11y_enabled);
295   else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
296     return g_variant_new ("b", app->screen_reader_enabled);
297   else
298     return NULL;
299 }
300
301 static void
302 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
303                                gboolean notify_gsettings)
304 {
305   GVariantBuilder builder;
306   GVariantBuilder invalidated_builder;
307
308   if (enabled == app->a11y_enabled)
309     return;
310
311   app->a11y_enabled = enabled;
312
313   if (notify_gsettings && app->interface_schema)
314     {
315       g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
316                               enabled);
317       g_settings_sync ();
318     }
319
320   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
321   g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
322   g_variant_builder_add (&builder, "{sv}", "IsEnabled",
323                          g_variant_new_boolean (enabled));
324
325   g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
326                                  "org.freedesktop.DBus.Properties",
327                                  "PropertiesChanged",
328                                  g_variant_new ("(sa{sv}as)", "org.a11y.Status",
329                                                 &builder,
330                                                 &invalidated_builder),
331                                  NULL);
332 }
333
334 static void
335 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
336                                gboolean notify_gsettings)
337 {
338   GVariantBuilder builder;
339   GVariantBuilder invalidated_builder;
340
341   if (enabled == app->screen_reader_enabled)
342     return;
343
344   /* If the screen reader is being enabled, we should enable accessibility
345    * if it isn't enabled already */
346   if (enabled)
347     handle_a11y_enabled_change (app, enabled, notify_gsettings);
348
349   app->screen_reader_enabled = enabled;
350
351   if (notify_gsettings && app->a11y_schema)
352     {
353       g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
354                               enabled);
355       g_settings_sync ();
356     }
357
358   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
359   g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
360   g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
361                          g_variant_new_boolean (enabled));
362
363   g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
364                                  "org.freedesktop.DBus.Properties",
365                                  "PropertiesChanged",
366                                  g_variant_new ("(sa{sv}as)", "org.a11y.Status",
367                                                 &builder,
368                                                 &invalidated_builder),
369                                  NULL);
370 }
371
372 static gboolean
373 handle_set_property  (GDBusConnection       *connection,
374                       const gchar           *sender,
375                       const gchar           *object_path,
376                       const gchar           *interface_name,
377                       const gchar           *property_name,
378                       GVariant *value,
379                     GError **error,
380                     gpointer               user_data)
381 {
382   A11yBusLauncher *app = user_data;
383   const gchar *type = g_variant_get_type_string (value);
384   gboolean enabled;
385
386   if (g_strcmp0 (type, "b") != 0)
387     {
388       g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
389                        "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
390       return FALSE;
391     }
392
393   enabled = g_variant_get_boolean (value);
394
395   if (g_strcmp0 (property_name, "IsEnabled") == 0)
396     {
397       handle_a11y_enabled_change (app, enabled, TRUE);
398       return TRUE;
399     }
400   else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
401     {
402       handle_screen_reader_enabled_change (app, enabled, TRUE);
403       return TRUE;
404     }
405   else
406     {
407       g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
408                        "Unknown property '%s'", property_name);
409       return FALSE;
410     }
411 }
412
413 static const GDBusInterfaceVTable bus_vtable =
414 {
415   handle_method_call,
416   NULL, /* handle_get_property, */
417   NULL  /* handle_set_property */
418 };
419
420 static const GDBusInterfaceVTable status_vtable =
421 {
422   NULL, /* handle_method_call */
423   handle_get_property,
424   handle_set_property
425 };
426
427 static void
428 on_bus_acquired (GDBusConnection *connection,
429                  const gchar     *name,
430                  gpointer         user_data)
431 {
432   A11yBusLauncher *app = user_data;
433   GError *error;
434   guint registration_id;
435
436   if (connection == NULL)
437     {
438       g_main_loop_quit (app->loop);
439       return;
440     }
441   app->session_bus = connection;
442
443   if (app->launch_immediately)
444     {
445       ensure_a11y_bus (app);
446       if (app->state == A11Y_BUS_STATE_ERROR)
447         {
448           g_main_loop_quit (app->loop);
449           return;
450         }
451     }
452
453   error = NULL;
454   registration_id = g_dbus_connection_register_object (connection,
455                                                        "/org/a11y/bus",
456                                                        introspection_data->interfaces[0],
457                                                        &bus_vtable,
458                                                        _global_app,
459                                                        NULL,
460                                                        &error);
461   if (registration_id == 0)
462     {
463       g_error ("%s", error->message);
464       g_clear_error (&error);
465     }
466
467   g_dbus_connection_register_object (connection,
468                                      "/org/a11y/bus",
469                                      introspection_data->interfaces[1],
470                                      &status_vtable,
471                                      _global_app,
472                                      NULL,
473                                      NULL);
474 }
475
476 static void
477 on_name_lost (GDBusConnection *connection,
478               const gchar     *name,
479               gpointer         user_data)
480 {
481   A11yBusLauncher *app = user_data;
482   if (app->session_bus == NULL
483       && connection == NULL
484       && app->a11y_launch_error_message == NULL)
485     app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
486   g_main_loop_quit (app->loop);
487 }
488
489 static void
490 on_name_acquired (GDBusConnection *connection,
491                   const gchar     *name,
492                   gpointer         user_data)
493 {
494   A11yBusLauncher *app = user_data;
495   (void) app;
496 }
497
498 static int sigterm_pipefd[2];
499
500 static void
501 sigterm_handler (int signum)
502 {
503   write (sigterm_pipefd[1], "X", 1);
504 }
505
506 static gboolean
507 on_sigterm_pipe (GIOChannel  *channel,
508                  GIOCondition condition,
509                  gpointer     data)
510 {
511   A11yBusLauncher *app = data;
512
513   g_main_loop_quit (app->loop);
514
515   return FALSE;
516 }
517
518 static void
519 init_sigterm_handling (A11yBusLauncher *app)
520 {
521   GIOChannel *sigterm_channel;
522
523   if (pipe (sigterm_pipefd) < 0)
524     g_error ("Failed to create pipe: %s", strerror (errno));
525   signal (SIGTERM, sigterm_handler);
526
527   sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
528   g_io_add_watch (sigterm_channel,
529                   G_IO_IN | G_IO_ERR | G_IO_HUP,
530                   on_sigterm_pipe,
531                   app);
532 }
533
534 static gboolean
535 already_running ()
536 {
537 #ifdef HAVE_X11
538   Atom AT_SPI_BUS;
539   Atom actual_type;
540   Display *bridge_display;
541   int actual_format;
542   unsigned char *data = NULL;
543   unsigned long nitems;
544   unsigned long leftover;
545   gboolean result = FALSE;
546
547   bridge_display = XOpenDisplay (NULL);
548   if (!bridge_display)
549               return FALSE;
550
551   AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
552   XGetWindowProperty (bridge_display,
553                       XDefaultRootWindow (bridge_display),
554                       AT_SPI_BUS, 0L,
555                       (long) BUFSIZ, False,
556                       (Atom) 31, &actual_type, &actual_format,
557                       &nitems, &leftover, &data);
558
559   if (data)
560   {
561     GDBusConnection *bus;
562     bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
563                                                   NULL, NULL, NULL);
564     if (bus != NULL)
565       {
566         result = TRUE;
567         g_object_unref (bus);
568       }
569   }
570
571   XCloseDisplay (bridge_display);
572   return result;
573 #else
574   return FALSE;
575 #endif
576 }
577
578 static GSettings *
579 get_schema (const gchar *name)
580 {
581   const char * const *schemas = NULL;
582   gint i;
583
584   schemas = g_settings_list_schemas ();
585   for (i = 0; schemas[i]; i++)
586   {
587     if (!strcmp (schemas[i], name))
588       return g_settings_new (schemas[i]);
589   }
590
591   return NULL;
592 }
593
594 static void
595 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
596 {
597   gboolean new_val = g_settings_get_boolean (gsettings, key);
598   A11yBusLauncher *app = user_data;
599
600   if (!strcmp (key, "toolkit-accessibility"))
601     handle_a11y_enabled_change (_global_app, new_val, FALSE);
602   else if (!strcmp (key, "screen-reader-enabled"))
603     handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
604 }
605
606 static gboolean
607 _launch_screen_reader(gpointer user_data)
608 {
609    A11yBusLauncher *bl = user_data;
610    LOGD("Launching screen reader");
611
612    bundle *kb = NULL;
613    gboolean ret = FALSE;
614
615    kb = bundle_create();
616
617    if (kb == NULL)
618      {
619         LOGD("Can't create bundle");
620         return FALSE;
621      }
622    int operation_error = appsvc_set_operation(kb, APP_CONTROL_OPERATION_SCREEN_READ);
623    LOGD("appsvc_set_operation: %i", operation_error);
624
625    bl->pid = appsvc_run_service(kb, 0, NULL, NULL);
626
627    if (bl->pid > 0)
628      {
629         LOGD("Screen reader launched with pid: %i", bl->pid);
630         ret = TRUE;
631      }
632    else
633      {
634         LOGD("Can't start screen-reader - error code: %i", bl->pid);
635      }
636
637
638    bundle_free(kb);
639    return ret;
640 }
641
642 static gboolean
643 _launch_screen_reader_repeat_until_success(gpointer user_data) {
644     A11yBusLauncher *bl = user_data;
645
646     if (bl->launch_screen_reader_repeats > 100 || bl->pid > 0)
647       {
648          //do not try anymore
649          return FALSE;
650       }
651
652     gboolean ret = _launch_screen_reader(user_data);
653
654     if (ret)
655       {
656          //we managed to
657          bl->launch_screen_reader_repeats = 0;
658          return FALSE;
659       }
660     //try again
661     return TRUE;
662 }
663
664 static gboolean
665 _terminate_screen_reader(A11yBusLauncher *bl)
666 {
667    LOGD("Terminating screen reader");
668    int ret;
669    int ret_aul;
670    if (bl->pid <= 0)
671      return FALSE;
672
673
674    int status = aul_app_get_status_bypid(bl->pid);
675
676    if (status < 0)
677      {
678        LOGD("App with pid %d already terminated", bl->pid);
679        bl->pid = 0;
680        return TRUE;
681      }
682
683    LOGD("terminate process with pid %d", bl->pid);
684    ret_aul = aul_terminate_pid(bl->pid);
685    if (ret_aul >= 0)
686      {
687         LOGD("Terminating with aul_terminate_pid: return is %d", ret_aul);
688         bl->pid = 0;
689         return TRUE;
690      }
691    else
692      LOGD("aul_terminate_pid failed: return is %d", ret_aul);
693
694    LOGD("Unable to terminate process using aul api. Sending SIGTERM signal");
695    ret = kill(bl->pid, SIGTERM);
696    if (!ret)
697      {
698         bl->pid = 0;
699         return TRUE;
700      }
701
702    LOGD("Unable to terminate process: %d with api or signal.", bl->pid);
703    return FALSE;
704 }
705
706 void screen_reader_cb(keynode_t *node, void *user_data)
707 {
708    A11yBusLauncher *bl = user_data;
709    int ret;
710
711    ret = vconf_keynode_get_bool(node);
712    LOGD("vconf_keynode_get_bool(node): %i", ret);
713    if (ret < 0)
714      return;
715
716    //check if process really exists (e.g didn't crash)
717    if (bl->pid > 0)
718      {
719         int err = kill(bl->pid,0);
720         //process doesn't exist
721         if (err == ESRCH)
722           bl->pid = 0;
723      }
724
725    bl->screen_reader_needed = ret;
726    LOGD("bl->screen_reader_needed: %i, bl->pid: %i", ret, bl->pid);
727    if (!bl->screen_reader_needed && (bl->pid > 0))
728      _terminate_screen_reader(bl);
729    else if (bl->screen_reader_needed && (bl->pid <= 0))
730      _launch_screen_reader(bl);
731 }
732
733 int
734 main (int    argc,
735       char **argv)
736 {
737 #ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
738   log_file = fopen("/tmp/at-spi-bus-launcher.log", "a");
739 #endif
740
741   LOGD("Starting atspi bus launcher");
742   GError *error = NULL;
743   GMainLoop *loop;
744   GDBusConnection *session_bus;
745   int name_owner_id;
746   gboolean a11y_set = FALSE;
747   gboolean screen_reader_set = FALSE;
748   gint i;
749
750   if (already_running ())
751     {
752        LOGD("atspi bus launcher is already running");
753        return 0;
754     }
755
756   _global_app = g_slice_new0 (A11yBusLauncher);
757   _global_app->loop = g_main_loop_new (NULL, FALSE);
758   _global_app->launch_screen_reader_repeats = 0;
759
760   for (i = 1; i < argc; i++)
761     {
762       if (!strcmp (argv[i], "--launch-immediately"))
763         _global_app->launch_immediately = TRUE;
764       else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 2)
765         a11y_set = TRUE;
766       else if (sscanf (argv[i], "--screen-reader=%d",
767                        &_global_app->screen_reader_enabled) == 2)
768         screen_reader_set = TRUE;
769     else
770       g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
771     }
772
773   _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
774   _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
775
776   if (!a11y_set)
777     {
778       _global_app->a11y_enabled = _global_app->interface_schema
779                                   ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
780                                   : _global_app->launch_immediately;
781     }
782
783   if (!screen_reader_set)
784     {
785       _global_app->screen_reader_enabled = _global_app->a11y_schema
786                                   ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
787                                   : FALSE;
788     }
789
790   if (_global_app->interface_schema)
791     g_signal_connect (_global_app->interface_schema,
792                       "changed::toolkit-accessibility",
793                       G_CALLBACK (gsettings_key_changed), _global_app);
794
795   if (_global_app->a11y_schema)
796     g_signal_connect (_global_app->a11y_schema,
797                       "changed::screen-reader-enabled",
798                       G_CALLBACK (gsettings_key_changed), _global_app);
799
800   init_sigterm_handling (_global_app);
801
802   introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
803   g_assert (introspection_data != NULL);
804
805   name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
806                                   "org.a11y.Bus",
807                                   G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
808                                   on_bus_acquired,
809                                   on_name_acquired,
810                                   on_name_lost,
811                                   _global_app,
812                                   NULL);
813
814   int ret = vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &_global_app->screen_reader_needed);
815   if (ret != 0)
816     {
817       LOGD("Could not read VCONFKEY_SETAPPL_ACCESSIBILITY_TTS key value.\n");
818       return FALSE;
819     }
820   ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, screen_reader_cb, _global_app);
821   if(ret != 0)
822     {
823       LOGD("Could not add information level callback\n");
824       return FALSE;
825     }
826   if (_global_app->screen_reader_needed)
827     g_timeout_add_seconds(2,_launch_screen_reader_repeat_until_success, _global_app);
828
829   g_main_loop_run (_global_app->loop);
830
831   if (_global_app->a11y_bus_pid > 0)
832     kill (_global_app->a11y_bus_pid, SIGTERM);
833
834   /* Clear the X property if our bus is gone; in the case where e.g.
835    * GDM is launching a login on an X server it was using before,
836    * we don't want early login processes to pick up the stale address.
837    */
838 #ifdef HAVE_X11
839   {
840     Display *display = XOpenDisplay (NULL);
841     if (display)
842       {
843         Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
844         XDeleteProperty (display,
845                          XDefaultRootWindow (display),
846                          bus_address_atom);
847
848         XFlush (display);
849         XCloseDisplay (display);
850       }
851   }
852 #endif
853
854   if (_global_app->a11y_launch_error_message)
855     {
856       g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);
857       return 1;
858     }
859   return 0;
860 }