1 /* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
3 * at-spi-bus-launcher: Manage the a11y bus as a child process
5 * Copyright 2011 Red Hat, Inc.
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.
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.
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.
35 #include <X11/Xatom.h>
38 #define APP_CONTROL_OPERATION_SCREEN_READ "http://tizen.org/appcontrol/operation/read_screen"
42 //uncomment if you want debug
43 //#ifndef TIZEN_ENGINEER_MODE
44 //#define TIZEN_ENGINEER_MODE
50 #define LOG_TAG "ATSPI_BUS_LAUNCHER"
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
59 #ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
64 #define LOGD(arg...) do {if (log_file) {fprintf(log_file, ##arg);fprintf(log_file, "\n"); fflush(log_file);}} while(0)
67 static gboolean _launch_screen_reader_repeat_until_success(gpointer user_data);
70 A11Y_BUS_STATE_IDLE = 0,
71 A11Y_BUS_STATE_READING_ADDRESS,
72 A11Y_BUS_STATE_RUNNING,
78 gboolean launch_immediately;
79 gboolean a11y_enabled;
80 gboolean screen_reader_enabled;
81 GDBusConnection *session_bus;
82 GSettings *a11y_schema;
83 GSettings *interface_schema;
85 int launch_screen_reader_repeats;
86 gboolean screen_reader_needed;
91 /* -1 == error, 0 == pending, > 0 == running */
93 char *a11y_bus_address;
95 char *a11y_launch_error_message;
98 static A11yBusLauncher *_global_app = NULL;
100 static const gchar introspection_xml[] =
102 " <interface name='org.a11y.Bus'>"
103 " <method name='GetAddress'>"
104 " <arg type='s' name='address' direction='out'/>"
107 "<interface name='org.a11y.Status'>"
108 "<property name='IsEnabled' type='b' access='readwrite'/>"
109 "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
112 static GDBusNodeInfo *introspection_data = NULL;
115 setup_bus_child (gpointer data)
117 A11yBusLauncher *app = data;
120 close (app->pipefd[0]);
121 dup2 (app->pipefd[1], 3);
122 close (app->pipefd[1]);
124 /* On Linux, tell the bus process to exit if this process goes away */
126 #include <sys/prctl.h>
127 prctl (PR_SET_PDEATHSIG, 15);
132 * unix_read_all_fd_to_string:
134 * Read all data from a file descriptor to a C string buffer.
137 unix_read_all_fd_to_string (int fd,
143 while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
148 max_bytes -= bytes_read;
155 on_bus_exited (GPid pid,
159 A11yBusLauncher *app = data;
161 app->a11y_bus_pid = -1;
162 app->state = A11Y_BUS_STATE_ERROR;
163 if (app->a11y_launch_error_message == NULL)
165 if (WIFEXITED (status))
166 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
167 else if (WIFSIGNALED (status))
168 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
169 else if (WIFSTOPPED (status))
170 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
172 g_main_loop_quit (app->loop);
176 ensure_a11y_bus (A11yBusLauncher *app)
179 char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
181 GError *error = NULL;
183 if (app->a11y_bus_pid != 0)
186 argv[1] = g_strdup_printf ("--config-file=%s/at-spi2/accessibility.conf", SYSCONFDIR);
188 if (pipe (app->pipefd) < 0)
189 g_error ("Failed to create pipe: %s", strerror (errno));
191 if (!g_spawn_async (NULL,
194 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
200 app->a11y_bus_pid = -1;
201 app->a11y_launch_error_message = g_strdup (error->message);
202 g_clear_error (&error);
206 close (app->pipefd[1]);
209 g_child_watch_add (pid, on_bus_exited, app);
211 app->state = A11Y_BUS_STATE_READING_ADDRESS;
212 app->a11y_bus_pid = pid;
213 LOGD("Launched a11y bus, child is %ld", (long) pid);
214 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
216 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
217 kill (app->a11y_bus_pid, SIGTERM);
220 close (app->pipefd[0]);
222 app->state = A11Y_BUS_STATE_RUNNING;
224 /* Trim the trailing newline */
225 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
226 LOGD("a11y bus address: %s", app->a11y_bus_address);
230 Display *display = XOpenDisplay (NULL);
233 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
234 XChangeProperty (display,
235 XDefaultRootWindow (display),
237 XA_STRING, 8, PropModeReplace,
238 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
240 XCloseDisplay (display);
245 if (argv[1]) g_free(argv[1]);
250 if (argv[1]) g_free(argv[1]);
251 close (app->pipefd[0]);
252 close (app->pipefd[1]);
253 app->state = A11Y_BUS_STATE_ERROR;
259 handle_method_call (GDBusConnection *connection,
261 const gchar *object_path,
262 const gchar *interface_name,
263 const gchar *method_name,
264 GVariant *parameters,
265 GDBusMethodInvocation *invocation,
268 A11yBusLauncher *app = user_data;
270 if (g_strcmp0 (method_name, "GetAddress") == 0)
272 ensure_a11y_bus (app);
273 if (app->a11y_bus_pid > 0)
274 g_dbus_method_invocation_return_value (invocation,
275 g_variant_new ("(s)", app->a11y_bus_address));
277 g_dbus_method_invocation_return_dbus_error (invocation,
278 "org.a11y.Bus.Error",
279 app->a11y_launch_error_message);
284 handle_get_property (GDBusConnection *connection,
286 const gchar *object_path,
287 const gchar *interface_name,
288 const gchar *property_name,
292 A11yBusLauncher *app = user_data;
294 if (g_strcmp0 (property_name, "IsEnabled") == 0)
295 return g_variant_new ("b", app->a11y_enabled);
296 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
297 return g_variant_new ("b", app->screen_reader_enabled);
303 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
304 gboolean notify_gsettings)
306 GVariantBuilder builder;
307 GVariantBuilder invalidated_builder;
309 if (enabled == app->a11y_enabled)
312 app->a11y_enabled = enabled;
314 if (notify_gsettings && app->interface_schema)
316 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
321 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
322 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
323 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
324 g_variant_new_boolean (enabled));
326 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
327 "org.freedesktop.DBus.Properties",
329 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
331 &invalidated_builder),
333 g_variant_builder_clear (&builder);
334 g_variant_builder_clear (&invalidated_builder);
338 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
339 gboolean notify_gsettings)
341 GVariantBuilder builder;
342 GVariantBuilder invalidated_builder;
344 if (enabled == app->screen_reader_enabled)
347 /* If the screen reader is being enabled, we should enable accessibility
348 * if it isn't enabled already */
350 handle_a11y_enabled_change (app, enabled, notify_gsettings);
352 app->screen_reader_enabled = enabled;
354 if (notify_gsettings && app->a11y_schema)
356 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
361 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
362 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
363 g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
364 g_variant_new_boolean (enabled));
366 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
367 "org.freedesktop.DBus.Properties",
369 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
371 &invalidated_builder),
373 g_variant_builder_clear (&builder);
374 g_variant_builder_clear (&invalidated_builder);
378 handle_set_property (GDBusConnection *connection,
380 const gchar *object_path,
381 const gchar *interface_name,
382 const gchar *property_name,
387 A11yBusLauncher *app = user_data;
388 const gchar *type = g_variant_get_type_string (value);
391 if (g_strcmp0 (type, "b") != 0)
393 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
394 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
398 enabled = g_variant_get_boolean (value);
400 if (g_strcmp0 (property_name, "IsEnabled") == 0)
402 handle_a11y_enabled_change (app, enabled, TRUE);
405 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
407 handle_screen_reader_enabled_change (app, enabled, TRUE);
412 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
413 "Unknown property '%s'", property_name);
418 static const GDBusInterfaceVTable bus_vtable =
421 NULL, /* handle_get_property, */
422 NULL /* handle_set_property */
425 static const GDBusInterfaceVTable status_vtable =
427 NULL, /* handle_method_call */
433 on_bus_acquired (GDBusConnection *connection,
437 A11yBusLauncher *app = user_data;
439 guint registration_id;
441 if (connection == NULL)
443 g_main_loop_quit (app->loop);
446 app->session_bus = connection;
448 if (app->launch_immediately)
450 ensure_a11y_bus (app);
451 if (app->state == A11Y_BUS_STATE_ERROR)
453 g_main_loop_quit (app->loop);
459 registration_id = g_dbus_connection_register_object (connection,
461 introspection_data->interfaces[0],
466 if (registration_id == 0)
468 g_error ("%s", error->message);
469 g_clear_error (&error);
472 g_dbus_connection_register_object (connection,
474 introspection_data->interfaces[1],
482 on_name_lost (GDBusConnection *connection,
486 A11yBusLauncher *app = user_data;
487 if (app->session_bus == NULL
488 && connection == NULL
489 && app->a11y_launch_error_message == NULL)
490 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
491 g_main_loop_quit (app->loop);
495 on_name_acquired (GDBusConnection *connection,
499 A11yBusLauncher *app = user_data;
503 static int sigterm_pipefd[2];
506 sigterm_handler (int signum)
508 write (sigterm_pipefd[1], "X", 1);
512 on_sigterm_pipe (GIOChannel *channel,
513 GIOCondition condition,
516 A11yBusLauncher *app = data;
518 g_main_loop_quit (app->loop);
524 init_sigterm_handling (A11yBusLauncher *app)
526 GIOChannel *sigterm_channel;
528 if (pipe (sigterm_pipefd) < 0)
529 g_error ("Failed to create pipe: %s", strerror (errno));
530 signal (SIGTERM, sigterm_handler);
532 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
533 g_io_add_watch (sigterm_channel,
534 G_IO_IN | G_IO_ERR | G_IO_HUP,
545 Display *bridge_display;
547 unsigned char *data = NULL;
548 unsigned long nitems;
549 unsigned long leftover;
550 gboolean result = FALSE;
552 bridge_display = XOpenDisplay (NULL);
556 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
557 XGetWindowProperty (bridge_display,
558 XDefaultRootWindow (bridge_display),
560 (long) BUFSIZ, False,
561 (Atom) 31, &actual_type, &actual_format,
562 &nitems, &leftover, &data);
566 GDBusConnection *bus;
567 bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
572 g_object_unref (bus);
576 XCloseDisplay (bridge_display);
584 get_schema (const gchar *name)
586 const char * const *schemas = NULL;
589 schemas = g_settings_list_schemas ();
590 for (i = 0; schemas[i]; i++)
592 if (!strcmp (schemas[i], name))
593 return g_settings_new (schemas[i]);
600 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
602 gboolean new_val = g_settings_get_boolean (gsettings, key);
603 A11yBusLauncher *app = user_data;
605 if (!strcmp (key, "toolkit-accessibility"))
606 handle_a11y_enabled_change (_global_app, new_val, FALSE);
607 else if (!strcmp (key, "screen-reader-enabled"))
608 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
612 _screen_reader_dead_tracker (int pid, void *data)
614 A11yBusLauncher *app = data;
616 if (app->pid > 0 && pid == app->pid)
618 LOGE("screen reader is dead, pid: %d, restarting", pid);
620 g_timeout_add_seconds (2, _launch_screen_reader_repeat_until_success, app);
626 _launch_screen_reader(gpointer user_data, gboolean by_vconf_change)
628 A11yBusLauncher *bl = user_data;
629 LOGD("Launching screen reader");
632 gboolean ret = FALSE;
634 kb = bundle_create();
638 LOGD("Can't create bundle");
644 if (bundle_add_str(kb, "by_vconf_change", "yes") != BUNDLE_ERROR_NONE)
646 LOGD("Can't add information to bundle");
650 int operation_error = appsvc_set_operation(kb, APP_CONTROL_OPERATION_SCREEN_READ);
651 LOGD("appsvc_set_operation: %i", operation_error);
653 bl->pid = appsvc_run_service(kb, 0, NULL, NULL);
657 LOGD("Screen reader launched with pid: %i", bl->pid);
658 LOGD("registering screen reader dead tracker");
659 aul_listen_app_dead_signal(_screen_reader_dead_tracker, bl);
664 LOGD("Can't start screen-reader - error code: %i", bl->pid);
673 _launch_screen_reader_repeat_until_success(gpointer user_data) {
674 A11yBusLauncher *bl = user_data;
676 if (bl->launch_screen_reader_repeats > 100 || bl->pid > 0)
682 gboolean ret = _launch_screen_reader(user_data, FALSE);
687 bl->launch_screen_reader_repeats = 0;
695 _terminate_screen_reader(A11yBusLauncher *bl)
697 LOGD("Terminating screen reader");
704 LOGD("unregistering screen reader dead tracker");
705 aul_listen_app_dead_signal(NULL, NULL);
707 int status = aul_app_get_status_bypid(bl->pid);
711 LOGD("App with pid %d already terminated", bl->pid);
716 LOGD("terminate process with pid %d", bl->pid);
717 ret_aul = aul_terminate_pid(bl->pid);
720 LOGD("Terminating with aul_terminate_pid: return is %d", ret_aul);
725 LOGD("aul_terminate_pid failed: return is %d", ret_aul);
727 LOGD("Unable to terminate process using aul api. Sending SIGTERM signal");
728 ret = kill(bl->pid, SIGTERM);
735 LOGD("Unable to terminate process: %d with api or signal.", bl->pid);
739 void screen_reader_cb(keynode_t *node, void *user_data)
741 A11yBusLauncher *bl = user_data;
744 ret = vconf_keynode_get_bool(node);
745 LOGD("vconf_keynode_get_bool(node): %i", ret);
749 //check if process really exists (e.g didn't crash)
752 int err = kill(bl->pid,0);
753 //process doesn't exist
758 bl->screen_reader_needed = ret;
759 LOGD("bl->screen_reader_needed: %i, bl->pid: %i", ret, bl->pid);
760 if (!bl->screen_reader_needed && (bl->pid > 0))
761 _terminate_screen_reader(bl);
762 else if (bl->screen_reader_needed && (bl->pid <= 0))
763 _launch_screen_reader(bl, TRUE);
770 #ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
771 log_file = fopen("/tmp/at-spi-bus-launcher.log", "a");
774 LOGD("Starting atspi bus launcher");
775 GError *error = NULL;
777 GDBusConnection *session_bus;
779 gboolean a11y_set = FALSE;
780 gboolean screen_reader_set = FALSE;
783 if (already_running ())
785 LOGD("atspi bus launcher is already running");
789 _global_app = g_slice_new0 (A11yBusLauncher);
790 _global_app->loop = g_main_loop_new (NULL, FALSE);
791 _global_app->launch_screen_reader_repeats = 0;
793 for (i = 1; i < argc; i++)
795 if (!strcmp (argv[i], "--launch-immediately"))
796 _global_app->launch_immediately = TRUE;
797 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 2)
799 else if (sscanf (argv[i], "--screen-reader=%d",
800 &_global_app->screen_reader_enabled) == 2)
801 screen_reader_set = TRUE;
803 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
806 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
807 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
811 _global_app->a11y_enabled = _global_app->interface_schema
812 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
813 : _global_app->launch_immediately;
816 if (!screen_reader_set)
818 _global_app->screen_reader_enabled = _global_app->a11y_schema
819 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
823 if (_global_app->interface_schema)
824 g_signal_connect (_global_app->interface_schema,
825 "changed::toolkit-accessibility",
826 G_CALLBACK (gsettings_key_changed), _global_app);
828 if (_global_app->a11y_schema)
829 g_signal_connect (_global_app->a11y_schema,
830 "changed::screen-reader-enabled",
831 G_CALLBACK (gsettings_key_changed), _global_app);
833 init_sigterm_handling (_global_app);
835 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
836 g_assert (introspection_data != NULL);
838 name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
840 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
847 int ret = vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &_global_app->screen_reader_needed);
850 LOGD("Could not read VCONFKEY_SETAPPL_ACCESSIBILITY_TTS key value.\n");
853 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, screen_reader_cb, _global_app);
856 LOGD("Could not add information level callback\n");
859 if (_global_app->screen_reader_needed)
860 g_timeout_add_seconds(2,_launch_screen_reader_repeat_until_success, _global_app);
862 g_main_loop_run (_global_app->loop);
864 if (_global_app->a11y_bus_pid > 0)
865 kill (_global_app->a11y_bus_pid, SIGTERM);
867 /* Clear the X property if our bus is gone; in the case where e.g.
868 * GDM is launching a login on an X server it was using before,
869 * we don't want early login processes to pick up the stale address.
873 Display *display = XOpenDisplay (NULL);
876 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
877 XDeleteProperty (display,
878 XDefaultRootWindow (display),
882 XCloseDisplay (display);
887 if (_global_app->a11y_launch_error_message)
889 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);