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 //TODO: move to vconf/vconf-internal-setting-keys.h?
39 #define VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH "db/setting/accessibility/universal-switch"
41 #define APP_CONTROL_OPERATION_SCREEN_READ "http://tizen.org/appcontrol/operation/read_screen"
42 #define APP_CONTROL_OPERATION_UNIVERSAL_SWITCH "http://tizen.org/appcontrol/operation/universal_switch"
46 //uncomment if you want debug
47 //#ifndef TIZEN_ENGINEER_MODE
48 //#define TIZEN_ENGINEER_MODE
54 #define LOG_TAG "ATSPI_BUS_LAUNCHER"
59 //uncomment this if you want log suring startup
60 //seems like dlog is not working at startup time
61 #define ATSPI_BUS_LAUNCHER_LOG_TO_FILE
63 #ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
68 #define LOGD(arg...) do {if (log_file) {fprintf(log_file, ##arg);fprintf(log_file, "\n"); fflush(log_file);}} while(0)
71 static gboolean _launch_process_repeat_until_success(gpointer user_data);
74 A11Y_BUS_STATE_IDLE = 0,
75 A11Y_BUS_STATE_READING_ADDRESS,
76 A11Y_BUS_STATE_RUNNING,
82 const char * app_control_operation;
83 const char * vconf_key;
90 gboolean launch_immediately;
91 gboolean a11y_enabled;
92 gboolean screen_reader_enabled;
93 GHashTable *client_watcher_id;
94 GDBusConnection *session_bus;
95 GSettings *a11y_schema;
96 GSettings *interface_schema;
98 A11yBusClient screen_reader;
99 A11yBusClient universal_switch;
103 /* -1 == error, 0 == pending, > 0 == running */
105 char *a11y_bus_address;
107 char *a11y_launch_error_message;
110 static A11yBusLauncher *_global_app = NULL;
112 static const gchar introspection_xml[] =
114 " <interface name='org.a11y.Bus'>"
115 " <method name='GetAddress'>"
116 " <arg type='s' name='address' direction='out'/>"
119 "<interface name='org.a11y.Status'>"
120 "<property name='IsEnabled' type='b' access='readwrite'/>"
121 "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
124 static GDBusNodeInfo *introspection_data = NULL;
127 setup_bus_child (gpointer data)
129 A11yBusLauncher *app = data;
132 close (app->pipefd[0]);
133 dup2 (app->pipefd[1], 3);
134 close (app->pipefd[1]);
136 /* On Linux, tell the bus process to exit if this process goes away */
138 #include <sys/prctl.h>
139 prctl (PR_SET_PDEATHSIG, 15);
144 * unix_read_all_fd_to_string:
146 * Read all data from a file descriptor to a C string buffer.
149 unix_read_all_fd_to_string (int fd,
155 while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
160 max_bytes -= bytes_read;
167 on_bus_exited (GPid pid,
171 A11yBusLauncher *app = data;
173 app->a11y_bus_pid = -1;
174 app->state = A11Y_BUS_STATE_ERROR;
175 if (app->a11y_launch_error_message == NULL)
177 if (WIFEXITED (status))
178 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
179 else if (WIFSIGNALED (status))
180 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
181 else if (WIFSTOPPED (status))
182 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
184 g_main_loop_quit (app->loop);
188 ensure_a11y_bus (A11yBusLauncher *app)
191 char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
193 GError *error = NULL;
195 if (app->a11y_bus_pid != 0)
198 argv[1] = g_strdup_printf ("--config-file=%s/at-spi2/accessibility.conf", SYSCONFDIR);
200 if (pipe (app->pipefd) < 0)
201 g_error ("Failed to create pipe: %s", strerror (errno));
203 if (!g_spawn_async (NULL,
206 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
212 app->a11y_bus_pid = -1;
213 app->a11y_launch_error_message = g_strdup (error->message);
214 g_clear_error (&error);
218 close (app->pipefd[1]);
221 g_child_watch_add (pid, on_bus_exited, app);
223 app->state = A11Y_BUS_STATE_READING_ADDRESS;
224 app->a11y_bus_pid = pid;
225 LOGD("Launched a11y bus, child is %ld", (long) pid);
226 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
228 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
229 kill (app->a11y_bus_pid, SIGTERM);
232 close (app->pipefd[0]);
234 app->state = A11Y_BUS_STATE_RUNNING;
236 /* Trim the trailing newline */
237 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
238 LOGD("a11y bus address: %s", app->a11y_bus_address);
242 Display *display = XOpenDisplay (NULL);
245 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
246 XChangeProperty (display,
247 XDefaultRootWindow (display),
249 XA_STRING, 8, PropModeReplace,
250 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
252 XCloseDisplay (display);
257 if (argv[1]) g_free(argv[1]);
262 if (argv[1]) g_free(argv[1]);
263 close (app->pipefd[0]);
264 close (app->pipefd[1]);
265 app->state = A11Y_BUS_STATE_ERROR;
271 handle_method_call (GDBusConnection *connection,
273 const gchar *object_path,
274 const gchar *interface_name,
275 const gchar *method_name,
276 GVariant *parameters,
277 GDBusMethodInvocation *invocation,
280 A11yBusLauncher *app = user_data;
282 if (g_strcmp0 (method_name, "GetAddress") == 0)
284 ensure_a11y_bus (app);
285 if (app->a11y_bus_pid > 0)
286 g_dbus_method_invocation_return_value (invocation,
287 g_variant_new ("(s)", app->a11y_bus_address));
289 g_dbus_method_invocation_return_dbus_error (invocation,
290 "org.a11y.Bus.Error",
291 app->a11y_launch_error_message);
296 handle_get_property (GDBusConnection *connection,
298 const gchar *object_path,
299 const gchar *interface_name,
300 const gchar *property_name,
304 A11yBusLauncher *app = user_data;
306 if (g_strcmp0 (property_name, "IsEnabled") == 0)
307 return g_variant_new ("b", app->a11y_enabled);
308 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
309 return g_variant_new ("b", app->screen_reader_enabled);
315 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
316 gboolean notify_gsettings)
318 GVariantBuilder builder;
319 GVariantBuilder invalidated_builder;
321 if (enabled == app->a11y_enabled)
324 app->a11y_enabled = enabled;
326 if (notify_gsettings && app->interface_schema)
328 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
333 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
334 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
335 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
336 g_variant_new_boolean (enabled));
338 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
339 "org.freedesktop.DBus.Properties",
341 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
343 &invalidated_builder),
345 g_variant_builder_clear (&builder);
346 g_variant_builder_clear (&invalidated_builder);
350 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
351 gboolean notify_gsettings)
353 GVariantBuilder builder;
354 GVariantBuilder invalidated_builder;
356 if (enabled == app->screen_reader_enabled)
359 app->screen_reader_enabled = enabled;
361 if (notify_gsettings && app->a11y_schema)
363 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
368 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
369 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
370 g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
371 g_variant_new_boolean (enabled));
373 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
374 "org.freedesktop.DBus.Properties",
376 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
378 &invalidated_builder),
380 g_variant_builder_clear (&builder);
381 g_variant_builder_clear (&invalidated_builder);
385 is_client_connected(A11yBusLauncher *app)
387 guint watchers = g_hash_table_size(app->client_watcher_id);
388 LOGD("clients connected: %d", watchers);
393 remove_client_watch(A11yBusLauncher *app,
396 LOGD("Remove client watcher for %s", sender);
397 guint watcher_id = GPOINTER_TO_UINT(g_hash_table_lookup(app->client_watcher_id, sender));
399 g_bus_unwatch_name(watcher_id);
401 g_hash_table_remove(app->client_watcher_id, sender);
402 if (!is_client_connected(app))
403 handle_a11y_enabled_change (app, FALSE, TRUE);
407 on_client_name_vanished (GDBusConnection *connection,
411 A11yBusLauncher *app = user_data;
412 remove_client_watch(app, name);
416 add_client_watch(A11yBusLauncher *app,
419 LOGD("Add client watcher for %s", sender);
421 if (g_hash_table_contains(app->client_watcher_id, sender))
423 LOGI("Watcher for %s already registered", sender);
427 guint watcher_id = g_bus_watch_name(G_BUS_TYPE_SESSION,
429 G_BUS_NAME_WATCHER_FLAGS_NONE,
431 on_client_name_vanished,
435 g_hash_table_insert(app->client_watcher_id, g_strdup(sender), GUINT_TO_POINTER(watcher_id));
436 handle_a11y_enabled_change (app, TRUE, TRUE);
440 handle_set_property (GDBusConnection *connection,
442 const gchar *object_path,
443 const gchar *interface_name,
444 const gchar *property_name,
449 A11yBusLauncher *app = user_data;
450 const gchar *type = g_variant_get_type_string (value);
453 if (g_strcmp0 (type, "b") != 0)
455 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
456 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
460 enabled = g_variant_get_boolean (value);
462 if (g_strcmp0 (property_name, "IsEnabled") == 0)
465 add_client_watch(app, sender);
467 remove_client_watch(app, sender);
470 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
472 handle_screen_reader_enabled_change (app, enabled, TRUE);
477 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
478 "Unknown property '%s'", property_name);
483 static const GDBusInterfaceVTable bus_vtable =
486 NULL, /* handle_get_property, */
487 NULL /* handle_set_property */
490 static const GDBusInterfaceVTable status_vtable =
492 NULL, /* handle_method_call */
498 on_bus_acquired (GDBusConnection *connection,
502 A11yBusLauncher *app = user_data;
504 guint registration_id;
506 if (connection == NULL)
508 g_main_loop_quit (app->loop);
511 app->session_bus = connection;
513 if (app->launch_immediately)
515 ensure_a11y_bus (app);
516 if (app->state == A11Y_BUS_STATE_ERROR)
518 g_main_loop_quit (app->loop);
524 registration_id = g_dbus_connection_register_object (connection,
526 introspection_data->interfaces[0],
531 if (registration_id == 0)
533 g_error ("%s", error->message);
534 g_clear_error (&error);
537 g_dbus_connection_register_object (connection,
539 introspection_data->interfaces[1],
547 on_name_lost (GDBusConnection *connection,
551 A11yBusLauncher *app = user_data;
552 if (app->session_bus == NULL
553 && connection == NULL
554 && app->a11y_launch_error_message == NULL)
555 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
556 g_main_loop_quit (app->loop);
560 on_name_acquired (GDBusConnection *connection,
564 A11yBusLauncher *app = user_data;
568 static int sigterm_pipefd[2];
571 sigterm_handler (int signum)
573 write (sigterm_pipefd[1], "X", 1);
577 on_sigterm_pipe (GIOChannel *channel,
578 GIOCondition condition,
581 A11yBusLauncher *app = data;
583 g_main_loop_quit (app->loop);
589 init_sigterm_handling (A11yBusLauncher *app)
591 GIOChannel *sigterm_channel;
593 if (pipe (sigterm_pipefd) < 0)
594 g_error ("Failed to create pipe: %s", strerror (errno));
595 signal (SIGTERM, sigterm_handler);
597 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
598 g_io_add_watch (sigterm_channel,
599 G_IO_IN | G_IO_ERR | G_IO_HUP,
610 Display *bridge_display;
612 unsigned char *data = NULL;
613 unsigned long nitems;
614 unsigned long leftover;
615 gboolean result = FALSE;
617 bridge_display = XOpenDisplay (NULL);
621 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
622 XGetWindowProperty (bridge_display,
623 XDefaultRootWindow (bridge_display),
625 (long) BUFSIZ, False,
626 (Atom) 31, &actual_type, &actual_format,
627 &nitems, &leftover, &data);
631 GDBusConnection *bus;
632 bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
637 g_object_unref (bus);
641 XCloseDisplay (bridge_display);
649 get_schema (const gchar *name)
651 const char * const *schemas = NULL;
654 schemas = g_settings_list_schemas ();
655 for (i = 0; schemas[i]; i++)
657 if (!strcmp (schemas[i], name))
658 return g_settings_new (schemas[i]);
665 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
667 gboolean new_val = g_settings_get_boolean (gsettings, key);
669 if (!strcmp (key, "toolkit-accessibility"))
670 handle_a11y_enabled_change (_global_app, new_val, FALSE);
671 else if (!strcmp (key, "screen-reader-enabled"))
672 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
676 _process_dead_tracker (int pid, void *data)
678 A11yBusLauncher *app = data;
680 if (app->screen_reader.pid > 0 && pid == app->screen_reader.pid)
682 LOGE("screen reader is dead, pid: %d, restarting", pid);
683 app->screen_reader.pid = 0;
684 g_timeout_add_seconds (2, _launch_process_repeat_until_success, &app->screen_reader);
687 if (app->universal_switch.pid > 0 && pid == app->universal_switch.pid)
689 LOGE("universal switch is dead, pid: %d, restarting", pid);
690 app->universal_switch.pid = 0;
691 g_timeout_add_seconds (2, _launch_process_repeat_until_success, &app->universal_switch);
697 _register_process_dead_tracker ()
699 if(_global_app->screen_reader.pid > 0 || _global_app->universal_switch.pid > 0) {
700 LOGD("registering process dead tracker");
701 aul_listen_app_dead_signal(_process_dead_tracker, _global_app);
703 LOGD("unregistering process dead tracker");
704 aul_listen_app_dead_signal(NULL, NULL);
710 _launch_client(A11yBusClient *client, gboolean by_vconf_change)
712 LOGD("Launching %s", client->name);
715 gboolean ret = FALSE;
717 kb = bundle_create();
721 LOGD("Can't create bundle");
727 if (bundle_add_str(kb, "by_vconf_change", "yes") != BUNDLE_ERROR_NONE)
729 LOGD("Can't add information to bundle");
733 int operation_error = appsvc_set_operation(kb, client->app_control_operation);
734 LOGD("appsvc_set_operation: %i", operation_error);
736 client->pid = appsvc_run_service(kb, 0, NULL, NULL);
740 LOGD("Process launched with pid: %i", client->pid);
741 _register_process_dead_tracker();
746 LOGD("Can't start %s - error code: %i", client->name, client->pid);
754 _launch_process_repeat_until_success(gpointer user_data) {
755 A11yBusClient *client = user_data;
757 if (client->launch_repeats > 100 || client->pid > 0)
763 gboolean ret = _launch_client(client, FALSE);
768 client->launch_repeats = 0;
771 client->launch_repeats++;
777 _terminate_process(int pid)
784 int status = aul_app_get_status_bypid(pid);
788 LOGD("App with pid %d already terminated", pid);
792 LOGD("terminate process with pid %d", pid);
793 ret_aul = aul_terminate_pid(pid);
796 LOGD("Terminating with aul_terminate_pid: return is %d", ret_aul);
800 LOGD("aul_terminate_pid failed: return is %d", ret_aul);
802 LOGD("Unable to terminate process using aul api. Sending SIGTERM signal");
803 ret = kill(pid, SIGTERM);
809 LOGD("Unable to terminate process: %d with api or signal.", pid);
814 _terminate_client(A11yBusClient *client)
816 LOGD("Terminating %s", client->name);
817 int pid = client->pid;
819 _register_process_dead_tracker();
820 gboolean ret = _terminate_process(pid);
824 void vconf_client_cb(keynode_t *node, void *user_data)
826 A11yBusClient *client = user_data;
827 int client_needed = vconf_keynode_get_bool(node);
828 LOGD("vconf_keynode_get_bool(node): %i", client_needed);
829 if (client_needed < 0)
832 //check if process really exists (e.g didn't crash)
835 int err = kill(client->pid,0);
836 //process doesn't exist
841 LOGD("client_needed: %i, client->pid: %i", client_needed, client->pid);
842 if (!client_needed && (client->pid > 0))
843 _terminate_client(client);
844 else if (client_needed && (client->pid <= 0))
845 _launch_client(client, TRUE);
849 static gboolean register_client(A11yBusClient *client)
851 gboolean client_needed = FALSE;
853 if(!client->vconf_key) {
854 LOGE("Vconf_key missing for client: %s \n", client->vconf_key);
858 int ret = vconf_get_bool(client->vconf_key, &client_needed);
861 LOGD("Could not read %s key value.\n", client->vconf_key);
864 ret = vconf_notify_key_changed(client->vconf_key, vconf_client_cb, client);
867 LOGD("Could not add information level callback\n");
872 g_timeout_add_seconds(2,_launch_process_repeat_until_success, client);
880 #ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
881 log_file = fopen("/tmp/at-spi-bus-launcher.log", "a");
884 LOGD("Starting atspi bus launcher");
885 gboolean a11y_set = FALSE;
886 gboolean screen_reader_set = FALSE;
889 if (already_running ())
891 LOGD("atspi bus launcher is already running");
895 _global_app = g_slice_new0 (A11yBusLauncher);
896 _global_app->loop = g_main_loop_new (NULL, FALSE);
897 _global_app->client_watcher_id = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
899 _global_app->screen_reader.name = "screen-reader";
900 _global_app->screen_reader.app_control_operation = APP_CONTROL_OPERATION_SCREEN_READ;
901 _global_app->screen_reader.vconf_key = VCONFKEY_SETAPPL_ACCESSIBILITY_TTS;
903 _global_app->universal_switch.name = "universal-switch";
904 _global_app->universal_switch.app_control_operation = APP_CONTROL_OPERATION_UNIVERSAL_SWITCH;
905 _global_app->universal_switch.vconf_key = VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH;
907 for (i = 1; i < argc; i++)
909 if (!strcmp (argv[i], "--launch-immediately"))
910 _global_app->launch_immediately = TRUE;
911 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 2)
913 else if (sscanf (argv[i], "--screen-reader=%d",
914 &_global_app->screen_reader_enabled) == 2)
915 screen_reader_set = TRUE;
917 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
920 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
921 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
925 _global_app->a11y_enabled = _global_app->interface_schema
926 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
927 : _global_app->launch_immediately;
930 if (!screen_reader_set)
932 _global_app->screen_reader_enabled = _global_app->a11y_schema
933 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
937 if (_global_app->interface_schema)
938 g_signal_connect (_global_app->interface_schema,
939 "changed::toolkit-accessibility",
940 G_CALLBACK (gsettings_key_changed), _global_app);
942 if (_global_app->a11y_schema)
943 g_signal_connect (_global_app->a11y_schema,
944 "changed::screen-reader-enabled",
945 G_CALLBACK (gsettings_key_changed), _global_app);
947 init_sigterm_handling (_global_app);
949 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
950 g_assert (introspection_data != NULL);
952 g_bus_own_name (G_BUS_TYPE_SESSION,
954 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
961 register_client (&_global_app->screen_reader);
962 register_client (&_global_app->universal_switch);
964 g_main_loop_run (_global_app->loop);
966 if (_global_app->a11y_bus_pid > 0)
967 kill (_global_app->a11y_bus_pid, SIGTERM);
969 /* Clear the X property if our bus is gone; in the case where e.g.
970 * GDM is launching a login on an X server it was using before,
971 * we don't want early login processes to pick up the stale address.
975 Display *display = XOpenDisplay (NULL);
978 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
979 XDeleteProperty (display,
980 XDefaultRootWindow (display),
984 XCloseDisplay (display);
989 if (_global_app->a11y_launch_error_message)
991 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);