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 GHashTable *client_watcher_id;
82 GDBusConnection *session_bus;
83 GSettings *a11y_schema;
84 GSettings *interface_schema;
86 int launch_screen_reader_repeats;
87 gboolean screen_reader_needed;
92 /* -1 == error, 0 == pending, > 0 == running */
94 char *a11y_bus_address;
96 char *a11y_launch_error_message;
99 static A11yBusLauncher *_global_app = NULL;
101 static const gchar introspection_xml[] =
103 " <interface name='org.a11y.Bus'>"
104 " <method name='GetAddress'>"
105 " <arg type='s' name='address' direction='out'/>"
108 "<interface name='org.a11y.Status'>"
109 "<property name='IsEnabled' type='b' access='readwrite'/>"
110 "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
113 static GDBusNodeInfo *introspection_data = NULL;
116 setup_bus_child (gpointer data)
118 A11yBusLauncher *app = data;
121 close (app->pipefd[0]);
122 dup2 (app->pipefd[1], 3);
123 close (app->pipefd[1]);
125 /* On Linux, tell the bus process to exit if this process goes away */
127 #include <sys/prctl.h>
128 prctl (PR_SET_PDEATHSIG, 15);
133 * unix_read_all_fd_to_string:
135 * Read all data from a file descriptor to a C string buffer.
138 unix_read_all_fd_to_string (int fd,
144 while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
149 max_bytes -= bytes_read;
156 on_bus_exited (GPid pid,
160 A11yBusLauncher *app = data;
162 app->a11y_bus_pid = -1;
163 app->state = A11Y_BUS_STATE_ERROR;
164 if (app->a11y_launch_error_message == NULL)
166 if (WIFEXITED (status))
167 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
168 else if (WIFSIGNALED (status))
169 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
170 else if (WIFSTOPPED (status))
171 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
173 g_main_loop_quit (app->loop);
177 ensure_a11y_bus (A11yBusLauncher *app)
180 char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
182 GError *error = NULL;
184 if (app->a11y_bus_pid != 0)
187 argv[1] = g_strdup_printf ("--config-file=%s/at-spi2/accessibility.conf", SYSCONFDIR);
189 if (pipe (app->pipefd) < 0)
190 g_error ("Failed to create pipe: %s", strerror (errno));
192 if (!g_spawn_async (NULL,
195 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
201 app->a11y_bus_pid = -1;
202 app->a11y_launch_error_message = g_strdup (error->message);
203 g_clear_error (&error);
207 close (app->pipefd[1]);
210 g_child_watch_add (pid, on_bus_exited, app);
212 app->state = A11Y_BUS_STATE_READING_ADDRESS;
213 app->a11y_bus_pid = pid;
214 LOGD("Launched a11y bus, child is %ld", (long) pid);
215 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
217 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
218 kill (app->a11y_bus_pid, SIGTERM);
221 close (app->pipefd[0]);
223 app->state = A11Y_BUS_STATE_RUNNING;
225 /* Trim the trailing newline */
226 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
227 LOGD("a11y bus address: %s", app->a11y_bus_address);
231 Display *display = XOpenDisplay (NULL);
234 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
235 XChangeProperty (display,
236 XDefaultRootWindow (display),
238 XA_STRING, 8, PropModeReplace,
239 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
241 XCloseDisplay (display);
246 if (argv[1]) g_free(argv[1]);
251 if (argv[1]) g_free(argv[1]);
252 close (app->pipefd[0]);
253 close (app->pipefd[1]);
254 app->state = A11Y_BUS_STATE_ERROR;
260 handle_method_call (GDBusConnection *connection,
262 const gchar *object_path,
263 const gchar *interface_name,
264 const gchar *method_name,
265 GVariant *parameters,
266 GDBusMethodInvocation *invocation,
269 A11yBusLauncher *app = user_data;
271 if (g_strcmp0 (method_name, "GetAddress") == 0)
273 ensure_a11y_bus (app);
274 if (app->a11y_bus_pid > 0)
275 g_dbus_method_invocation_return_value (invocation,
276 g_variant_new ("(s)", app->a11y_bus_address));
278 g_dbus_method_invocation_return_dbus_error (invocation,
279 "org.a11y.Bus.Error",
280 app->a11y_launch_error_message);
285 handle_get_property (GDBusConnection *connection,
287 const gchar *object_path,
288 const gchar *interface_name,
289 const gchar *property_name,
293 A11yBusLauncher *app = user_data;
295 if (g_strcmp0 (property_name, "IsEnabled") == 0)
296 return g_variant_new ("b", app->a11y_enabled);
297 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
298 return g_variant_new ("b", app->screen_reader_enabled);
304 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
305 gboolean notify_gsettings)
307 GVariantBuilder builder;
308 GVariantBuilder invalidated_builder;
310 if (enabled == app->a11y_enabled)
313 app->a11y_enabled = enabled;
315 if (notify_gsettings && app->interface_schema)
317 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
322 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
323 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
324 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
325 g_variant_new_boolean (enabled));
327 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
328 "org.freedesktop.DBus.Properties",
330 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
332 &invalidated_builder),
334 g_variant_builder_clear (&builder);
335 g_variant_builder_clear (&invalidated_builder);
339 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
340 gboolean notify_gsettings)
342 GVariantBuilder builder;
343 GVariantBuilder invalidated_builder;
345 if (enabled == app->screen_reader_enabled)
348 app->screen_reader_enabled = enabled;
350 if (notify_gsettings && app->a11y_schema)
352 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
357 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
358 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
359 g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
360 g_variant_new_boolean (enabled));
362 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
363 "org.freedesktop.DBus.Properties",
365 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
367 &invalidated_builder),
369 g_variant_builder_clear (&builder);
370 g_variant_builder_clear (&invalidated_builder);
374 is_client_connected(A11yBusLauncher *app)
376 guint watchers = g_hash_table_size(app->client_watcher_id);
377 LOGD("clients connected: %d", watchers);
382 remove_client_watch(A11yBusLauncher *app,
385 LOGD("Remove client watcher for %s", sender);
386 guint watcher_id = GPOINTER_TO_UINT(g_hash_table_lookup(app->client_watcher_id, sender));
388 g_bus_unwatch_name(watcher_id);
390 g_hash_table_remove(app->client_watcher_id, sender);
391 if (!is_client_connected(app))
392 handle_a11y_enabled_change (app, FALSE, TRUE);
396 on_client_name_vanished (GDBusConnection *connection,
400 A11yBusLauncher *app = user_data;
401 remove_client_watch(app, name);
405 add_client_watch(A11yBusLauncher *app,
408 LOGD("Add client watcher for %s", sender);
410 if (g_hash_table_contains(app->client_watcher_id, sender))
412 LOGI("Watcher for %s already registered", sender);
416 guint watcher_id = g_bus_watch_name(G_BUS_TYPE_SESSION,
418 G_BUS_NAME_WATCHER_FLAGS_NONE,
420 on_client_name_vanished,
424 g_hash_table_insert(app->client_watcher_id, g_strdup(sender), GUINT_TO_POINTER(watcher_id));
425 handle_a11y_enabled_change (app, TRUE, TRUE);
429 handle_set_property (GDBusConnection *connection,
431 const gchar *object_path,
432 const gchar *interface_name,
433 const gchar *property_name,
438 A11yBusLauncher *app = user_data;
439 const gchar *type = g_variant_get_type_string (value);
442 if (g_strcmp0 (type, "b") != 0)
444 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
445 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
449 enabled = g_variant_get_boolean (value);
451 if (g_strcmp0 (property_name, "IsEnabled") == 0)
454 add_client_watch(app, sender);
456 remove_client_watch(app, sender);
459 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
461 handle_screen_reader_enabled_change (app, enabled, TRUE);
466 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
467 "Unknown property '%s'", property_name);
472 static const GDBusInterfaceVTable bus_vtable =
475 NULL, /* handle_get_property, */
476 NULL /* handle_set_property */
479 static const GDBusInterfaceVTable status_vtable =
481 NULL, /* handle_method_call */
487 on_bus_acquired (GDBusConnection *connection,
491 A11yBusLauncher *app = user_data;
493 guint registration_id;
495 if (connection == NULL)
497 g_main_loop_quit (app->loop);
500 app->session_bus = connection;
502 if (app->launch_immediately)
504 ensure_a11y_bus (app);
505 if (app->state == A11Y_BUS_STATE_ERROR)
507 g_main_loop_quit (app->loop);
513 registration_id = g_dbus_connection_register_object (connection,
515 introspection_data->interfaces[0],
520 if (registration_id == 0)
522 g_error ("%s", error->message);
523 g_clear_error (&error);
526 g_dbus_connection_register_object (connection,
528 introspection_data->interfaces[1],
536 on_name_lost (GDBusConnection *connection,
540 A11yBusLauncher *app = user_data;
541 if (app->session_bus == NULL
542 && connection == NULL
543 && app->a11y_launch_error_message == NULL)
544 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
545 g_main_loop_quit (app->loop);
549 on_name_acquired (GDBusConnection *connection,
553 A11yBusLauncher *app = user_data;
557 static int sigterm_pipefd[2];
560 sigterm_handler (int signum)
562 write (sigterm_pipefd[1], "X", 1);
566 on_sigterm_pipe (GIOChannel *channel,
567 GIOCondition condition,
570 A11yBusLauncher *app = data;
572 g_main_loop_quit (app->loop);
578 init_sigterm_handling (A11yBusLauncher *app)
580 GIOChannel *sigterm_channel;
582 if (pipe (sigterm_pipefd) < 0)
583 g_error ("Failed to create pipe: %s", strerror (errno));
584 signal (SIGTERM, sigterm_handler);
586 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
587 g_io_add_watch (sigterm_channel,
588 G_IO_IN | G_IO_ERR | G_IO_HUP,
599 Display *bridge_display;
601 unsigned char *data = NULL;
602 unsigned long nitems;
603 unsigned long leftover;
604 gboolean result = FALSE;
606 bridge_display = XOpenDisplay (NULL);
610 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
611 XGetWindowProperty (bridge_display,
612 XDefaultRootWindow (bridge_display),
614 (long) BUFSIZ, False,
615 (Atom) 31, &actual_type, &actual_format,
616 &nitems, &leftover, &data);
620 GDBusConnection *bus;
621 bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
626 g_object_unref (bus);
630 XCloseDisplay (bridge_display);
638 get_schema (const gchar *name)
640 const char * const *schemas = NULL;
643 schemas = g_settings_list_schemas ();
644 for (i = 0; schemas[i]; i++)
646 if (!strcmp (schemas[i], name))
647 return g_settings_new (schemas[i]);
654 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
656 gboolean new_val = g_settings_get_boolean (gsettings, key);
658 if (!strcmp (key, "toolkit-accessibility"))
659 handle_a11y_enabled_change (_global_app, new_val, FALSE);
660 else if (!strcmp (key, "screen-reader-enabled"))
661 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
665 _screen_reader_dead_tracker (int pid, void *data)
667 A11yBusLauncher *app = data;
669 if (app->pid > 0 && pid == app->pid)
671 LOGE("screen reader is dead, pid: %d, restarting", pid);
673 g_timeout_add_seconds (2, _launch_screen_reader_repeat_until_success, app);
679 _launch_screen_reader(gpointer user_data, gboolean by_vconf_change)
681 A11yBusLauncher *bl = user_data;
682 LOGD("Launching screen reader");
685 gboolean ret = FALSE;
687 kb = bundle_create();
691 LOGD("Can't create bundle");
697 if (bundle_add_str(kb, "by_vconf_change", "yes") != BUNDLE_ERROR_NONE)
699 LOGD("Can't add information to bundle");
703 int operation_error = appsvc_set_operation(kb, APP_CONTROL_OPERATION_SCREEN_READ);
704 LOGD("appsvc_set_operation: %i", operation_error);
706 bl->pid = appsvc_run_service(kb, 0, NULL, NULL);
710 LOGD("Screen reader launched with pid: %i", bl->pid);
711 LOGD("registering screen reader dead tracker");
712 aul_listen_app_dead_signal(_screen_reader_dead_tracker, bl);
717 LOGD("Can't start screen-reader - error code: %i", bl->pid);
726 _launch_screen_reader_repeat_until_success(gpointer user_data) {
727 A11yBusLauncher *bl = user_data;
729 if (bl->launch_screen_reader_repeats > 100 || bl->pid > 0)
735 gboolean ret = _launch_screen_reader(user_data, FALSE);
740 bl->launch_screen_reader_repeats = 0;
748 _terminate_screen_reader(A11yBusLauncher *bl)
750 LOGD("Terminating screen reader");
757 LOGD("unregistering screen reader dead tracker");
758 aul_listen_app_dead_signal(NULL, NULL);
760 int status = aul_app_get_status_bypid(bl->pid);
764 LOGD("App with pid %d already terminated", bl->pid);
769 LOGD("terminate process with pid %d", bl->pid);
770 ret_aul = aul_terminate_pid(bl->pid);
773 LOGD("Terminating with aul_terminate_pid: return is %d", ret_aul);
778 LOGD("aul_terminate_pid failed: return is %d", ret_aul);
780 LOGD("Unable to terminate process using aul api. Sending SIGTERM signal");
781 ret = kill(bl->pid, SIGTERM);
788 LOGD("Unable to terminate process: %d with api or signal.", bl->pid);
792 void screen_reader_cb(keynode_t *node, void *user_data)
794 A11yBusLauncher *bl = user_data;
797 ret = vconf_keynode_get_bool(node);
798 LOGD("vconf_keynode_get_bool(node): %i", ret);
802 //check if process really exists (e.g didn't crash)
805 int err = kill(bl->pid,0);
806 //process doesn't exist
811 bl->screen_reader_needed = ret;
812 LOGD("bl->screen_reader_needed: %i, bl->pid: %i", ret, bl->pid);
813 if (!bl->screen_reader_needed && (bl->pid > 0))
814 _terminate_screen_reader(bl);
815 else if (bl->screen_reader_needed && (bl->pid <= 0))
816 _launch_screen_reader(bl, TRUE);
823 #ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
824 log_file = fopen("/tmp/at-spi-bus-launcher.log", "a");
827 LOGD("Starting atspi bus launcher");
829 gboolean a11y_set = FALSE;
830 gboolean screen_reader_set = FALSE;
833 if (already_running ())
835 LOGD("atspi bus launcher is already running");
839 _global_app = g_slice_new0 (A11yBusLauncher);
840 _global_app->loop = g_main_loop_new (NULL, FALSE);
841 _global_app->launch_screen_reader_repeats = 0;
842 _global_app->client_watcher_id = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
844 for (i = 1; i < argc; i++)
846 if (!strcmp (argv[i], "--launch-immediately"))
847 _global_app->launch_immediately = TRUE;
848 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 2)
850 else if (sscanf (argv[i], "--screen-reader=%d",
851 &_global_app->screen_reader_enabled) == 2)
852 screen_reader_set = TRUE;
854 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
857 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
858 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
862 _global_app->a11y_enabled = _global_app->interface_schema
863 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
864 : _global_app->launch_immediately;
867 if (!screen_reader_set)
869 _global_app->screen_reader_enabled = _global_app->a11y_schema
870 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
874 if (_global_app->interface_schema)
875 g_signal_connect (_global_app->interface_schema,
876 "changed::toolkit-accessibility",
877 G_CALLBACK (gsettings_key_changed), _global_app);
879 if (_global_app->a11y_schema)
880 g_signal_connect (_global_app->a11y_schema,
881 "changed::screen-reader-enabled",
882 G_CALLBACK (gsettings_key_changed), _global_app);
884 init_sigterm_handling (_global_app);
886 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
887 g_assert (introspection_data != NULL);
889 name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
891 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
898 int ret = vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &_global_app->screen_reader_needed);
901 LOGD("Could not read VCONFKEY_SETAPPL_ACCESSIBILITY_TTS key value.\n");
904 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, screen_reader_cb, _global_app);
907 LOGD("Could not add information level callback\n");
910 if (_global_app->screen_reader_needed)
911 g_timeout_add_seconds(2,_launch_screen_reader_repeat_until_success, _global_app);
913 g_main_loop_run (_global_app->loop);
915 if (_global_app->a11y_bus_pid > 0)
916 kill (_global_app->a11y_bus_pid, SIGTERM);
918 /* Clear the X property if our bus is gone; in the case where e.g.
919 * GDM is launching a login on an X server it was using before,
920 * we don't want early login processes to pick up the stale address.
924 Display *display = XOpenDisplay (NULL);
927 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
928 XDeleteProperty (display,
929 XDefaultRootWindow (display),
933 XCloseDisplay (display);
938 if (_global_app->a11y_launch_error_message)
940 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);