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-2018 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.
29 #include <sys/prctl.h>
30 #include <sys/socket.h>
40 #include <X11/Xatom.h>
43 //TODO: move to vconf/vconf-internal-setting-keys.h?
44 #define VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE "db/setting/accessibility/universal-switch/configuration-service"
45 #define VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE "db/setting/accessibility/universal-switch/interaction-service"
47 #define MAX_NUMBER_OF_KEYS_PER_CLIENT 2
49 #define APP_CONTROL_OPERATION_SCREEN_READ "http://tizen.org/appcontrol/operation/read_screen"
50 #define APP_CONTROL_OPERATION_UNIVERSAL_SWITCH "http://tizen.org/appcontrol/operation/universal_switch"
54 //uncomment if you want debug
55 //#ifndef TIZEN_ENGINEER_MODE
56 //#define TIZEN_ENGINEER_MODE
62 #define LOG_TAG "ATSPI_BUS_LAUNCHER"
67 //uncomment this if you want log suring startup
68 //seems like dlog is not working at startup time
69 #define ATSPI_BUS_LAUNCHER_LOG_TO_FILE
71 #ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
76 #define LOGD(arg...) do {if (log_file) {fprintf(log_file, ##arg);fprintf(log_file, "\n"); fflush(log_file);}} while(0)
79 static gboolean _launch_process_repeat_until_success(gpointer user_data);
82 A11Y_BUS_STATE_IDLE = 0,
83 A11Y_BUS_STATE_READING_ADDRESS,
84 A11Y_BUS_STATE_RUNNING,
90 const char * app_control_operation;
91 const char * vconf_key[MAX_NUMBER_OF_KEYS_PER_CLIENT];
99 gboolean launch_immediately;
100 gboolean a11y_enabled;
101 gboolean screen_reader_enabled;
102 GHashTable *client_watcher_id;
103 GDBusConnection *session_bus;
104 GSettings *a11y_schema;
105 GSettings *interface_schema;
108 A11yBusClient screen_reader;
109 A11yBusClient universal_switch;
111 GDBusProxy *client_proxy;
115 /* -1 == error, 0 == pending, > 0 == running */
117 char *a11y_bus_address;
120 char *a11y_launch_error_message;
123 static A11yBusLauncher *_global_app = NULL;
125 static const gchar introspection_xml[] =
127 " <interface name='org.a11y.Bus'>"
128 " <method name='GetAddress'>"
129 " <arg type='s' name='address' direction='out'/>"
132 "<interface name='org.a11y.Status'>"
133 "<property name='IsEnabled' type='b' access='readwrite'/>"
134 "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
137 static GDBusNodeInfo *introspection_data = NULL;
140 respond_to_end_session (GDBusProxy *proxy)
142 GVariant *parameters;
144 parameters = g_variant_new ("(bs)", TRUE, "");
146 g_dbus_proxy_call (proxy,
147 "EndSessionResponse", parameters,
148 G_DBUS_CALL_FLAGS_NONE,
149 -1, NULL, NULL, NULL);
153 g_signal_cb (GDBusProxy *proxy,
156 GVariant *parameters,
159 A11yBusLauncher *app = user_data;
161 if (g_strcmp0 (signal_name, "QueryEndSession") == 0)
162 respond_to_end_session (proxy);
163 else if (g_strcmp0 (signal_name, "EndSession") == 0)
164 respond_to_end_session (proxy);
165 else if (g_strcmp0 (signal_name, "Stop") == 0)
166 g_main_loop_quit (app->loop);
170 client_proxy_ready_cb (GObject *source_object,
174 A11yBusLauncher *app = user_data;
175 GError *error = NULL;
177 app->client_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
181 g_warning ("Failed to get a client proxy: %s", error->message);
182 g_error_free (error);
187 g_signal_connect (app->client_proxy, "g-signal",
188 G_CALLBACK (g_signal_cb), app);
192 register_client (A11yBusLauncher *app)
194 GDBusProxyFlags flags;
195 GDBusProxy *sm_proxy;
198 const gchar *autostart_id;
199 gchar *client_startup_id;
200 GVariant *parameters;
204 flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
205 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS;
208 sm_proxy = g_dbus_proxy_new_sync (app->session_bus, flags, NULL,
209 "org.gnome.SessionManager",
210 "/org/gnome/SessionManager",
211 "org.gnome.SessionManager",
216 g_warning ("Failed to get session manager proxy: %s", error->message);
217 g_error_free (error);
222 app_id = "at-spi-bus-launcher";
223 autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
225 if (autostart_id != NULL)
227 client_startup_id = g_strdup (autostart_id);
228 g_unsetenv ("DESKTOP_AUTOSTART_ID");
232 client_startup_id = g_strdup ("");
235 parameters = g_variant_new ("(ss)", app_id, client_startup_id);
236 g_free (client_startup_id);
239 variant = g_dbus_proxy_call_sync (sm_proxy,
240 "RegisterClient", parameters,
241 G_DBUS_CALL_FLAGS_NONE,
244 g_object_unref (sm_proxy);
248 g_warning ("Failed to register client: %s", error->message);
249 g_error_free (error);
254 g_variant_get (variant, "(o)", &object_path);
255 g_variant_unref (variant);
257 flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
258 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, flags, NULL,
259 "org.gnome.SessionManager", object_path,
260 "org.gnome.SessionManager.ClientPrivate",
261 NULL, client_proxy_ready_cb, app);
263 g_free (object_path);
267 name_appeared_handler (GDBusConnection *connection,
269 const gchar *name_owner,
272 A11yBusLauncher *app = user_data;
274 register_client (app);
278 * unix_read_all_fd_to_string:
280 * Read all data from a file descriptor to a C string buffer.
283 unix_read_all_fd_to_string (int fd,
289 while (max_bytes > 1 && (bytes_read = read (fd, buf, MIN (4096, max_bytes - 1))))
294 max_bytes -= bytes_read;
301 on_bus_exited (GPid pid,
305 A11yBusLauncher *app = data;
307 app->a11y_bus_pid = -1;
308 app->state = A11Y_BUS_STATE_ERROR;
309 if (app->a11y_launch_error_message == NULL)
311 if (WIFEXITED (status))
312 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
313 else if (WIFSIGNALED (status))
314 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
315 else if (WIFSTOPPED (status))
316 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
318 g_main_loop_quit (app->loop);
323 setup_bus_child_daemon (gpointer data)
325 A11yBusLauncher *app = data;
328 close (app->pipefd[0]);
329 dup2 (app->pipefd[1], 3);
330 close (app->pipefd[1]);
332 /* On Linux, tell the bus process to exit if this process goes away */
334 prctl (PR_SET_PDEATHSIG, 15);
339 ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
341 char *argv[] = { DBUS_DAEMON, config_path, "--nofork", "--print-address", "3", NULL };
344 GError *error = NULL;
346 if (app->a11y_bus_pid != 0)
349 argv[1] = (char*)config_path;
351 if (pipe (app->pipefd) < 0)
352 g_error ("Failed to create pipe: %s", strerror (errno));
354 g_clear_pointer (&app->a11y_launch_error_message, g_free);
356 if (!g_spawn_async (NULL,
359 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
360 setup_bus_child_daemon,
365 app->a11y_bus_pid = -1;
366 app->a11y_launch_error_message = g_strdup (error->message);
367 g_clear_error (&error);
371 close (app->pipefd[1]);
374 g_child_watch_add (pid, on_bus_exited, app);
376 app->state = A11Y_BUS_STATE_READING_ADDRESS;
377 app->a11y_bus_pid = pid;
378 LOGD("Launched a11y bus, child is %ld", (long) pid);
379 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
381 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
382 kill (app->a11y_bus_pid, SIGTERM);
385 close (app->pipefd[0]);
387 app->state = A11Y_BUS_STATE_RUNNING;
389 /* Trim the trailing newline */
390 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
391 LOGD("a11y bus address: %s", app->a11y_bus_address);
396 close (app->pipefd[0]);
397 close (app->pipefd[1]);
398 app->state = A11Y_BUS_STATE_ERROR;
404 ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
412 setup_bus_child_broker (gpointer data)
414 A11yBusLauncher *app = data;
418 dup2 (app->listenfd, 3);
419 close (app->listenfd);
420 g_setenv("LISTEN_FDS", "1", TRUE);
422 pid_str = g_strdup_printf("%u", getpid());
423 g_setenv("LISTEN_PID", pid_str, TRUE);
426 /* Tell the bus process to exit if this process goes away */
427 prctl (PR_SET_PDEATHSIG, SIGTERM);
431 ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
433 char *argv[] = { DBUS_BROKER, config_path, "--scope", "user", NULL };
434 struct sockaddr_un addr = { .sun_family = AF_UNIX };
435 socklen_t addr_len = sizeof(addr);
437 GError *error = NULL;
439 if ((app->listenfd = socket (PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0)
440 g_error ("Failed to create listening socket: %s", strerror (errno));
442 if (bind (app->listenfd, (struct sockaddr *)&addr, sizeof(sa_family_t)) < 0)
443 g_error ("Failed to bind listening socket: %s", strerror (errno));
445 if (getsockname (app->listenfd, (struct sockaddr *)&addr, &addr_len) < 0)
446 g_error ("Failed to get socket name for listening socket: %s", strerror(errno));
448 if (listen (app->listenfd, 1024) < 0)
449 g_error ("Failed to listen on socket: %s", strerror(errno));
451 g_clear_pointer (&app->a11y_launch_error_message, g_free);
453 if (!g_spawn_async (NULL,
456 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
457 setup_bus_child_broker,
462 app->a11y_bus_pid = -1;
463 app->a11y_launch_error_message = g_strdup (error->message);
464 g_clear_error (&error);
468 close (app->listenfd);
471 g_child_watch_add (pid, on_bus_exited, app);
472 app->a11y_bus_pid = pid;
473 g_debug ("Launched a11y bus, child is %ld", (long) pid);
474 app->state = A11Y_BUS_STATE_RUNNING;
476 app->a11y_bus_address = g_strconcat("unix:abstract=", addr.sun_path + 1, NULL);
477 g_debug ("a11y bus address: %s", app->a11y_bus_address);
482 close (app->listenfd);
483 app->state = A11Y_BUS_STATE_ERROR;
489 ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
496 ensure_a11y_bus (A11yBusLauncher *app)
498 char *config_path = NULL;
499 gboolean success = FALSE;
501 if (app->a11y_bus_pid != 0)
504 if (g_file_test (SYSCONFDIR"/at-spi2/accessibility.conf", G_FILE_TEST_EXISTS))
505 config_path = "--config-file="SYSCONFDIR"/at-spi2/accessibility.conf";
507 config_path = "--config-file="DATADIR"/defaults/at-spi2/accessibility.conf";
509 #ifdef WANT_DBUS_BROKER
510 success = ensure_a11y_bus_broker (app, config_path);
513 if (!ensure_a11y_bus_daemon (app, config_path))
517 success = ensure_a11y_bus_daemon (app, config_path);
520 if (!ensure_a11y_bus_broker (app, config_path))
527 Display *display = XOpenDisplay (NULL);
530 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
531 XChangeProperty (display,
532 XDefaultRootWindow (display),
534 XA_STRING, 8, PropModeReplace,
535 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
537 XCloseDisplay (display);
546 handle_method_call (GDBusConnection *connection,
548 const gchar *object_path,
549 const gchar *interface_name,
550 const gchar *method_name,
551 GVariant *parameters,
552 GDBusMethodInvocation *invocation,
555 A11yBusLauncher *app = user_data;
557 if (g_strcmp0 (method_name, "GetAddress") == 0)
559 ensure_a11y_bus (app);
560 if (app->a11y_bus_pid > 0)
561 g_dbus_method_invocation_return_value (invocation,
562 g_variant_new ("(s)", app->a11y_bus_address));
564 g_dbus_method_invocation_return_dbus_error (invocation,
565 "org.a11y.Bus.Error",
566 app->a11y_launch_error_message);
571 handle_get_property (GDBusConnection *connection,
573 const gchar *object_path,
574 const gchar *interface_name,
575 const gchar *property_name,
579 A11yBusLauncher *app = user_data;
581 if (g_strcmp0 (property_name, "IsEnabled") == 0)
582 return g_variant_new ("b", app->a11y_enabled);
583 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
584 return g_variant_new ("b", app->screen_reader_enabled);
590 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
591 gboolean notify_gsettings)
593 GVariantBuilder builder;
594 GVariantBuilder invalidated_builder;
596 if (enabled == app->a11y_enabled)
599 app->a11y_enabled = enabled;
601 if (notify_gsettings && app->interface_schema)
603 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
608 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
609 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
610 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
611 g_variant_new_boolean (enabled));
613 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
614 "org.freedesktop.DBus.Properties",
616 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
618 &invalidated_builder),
621 g_variant_builder_clear (&builder);
622 g_variant_builder_clear (&invalidated_builder);
626 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
627 gboolean notify_gsettings)
629 GVariantBuilder builder;
630 GVariantBuilder invalidated_builder;
632 if (enabled == app->screen_reader_enabled)
635 app->screen_reader_enabled = enabled;
637 if (notify_gsettings && app->a11y_schema)
639 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
644 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
645 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
646 g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
647 g_variant_new_boolean (enabled));
649 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
650 "org.freedesktop.DBus.Properties",
652 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
654 &invalidated_builder),
656 g_variant_builder_clear (&builder);
657 g_variant_builder_clear (&invalidated_builder);
661 is_client_connected(A11yBusLauncher *app)
663 guint watchers = g_hash_table_size(app->client_watcher_id);
664 LOGD("clients connected: %d", watchers);
669 remove_client_watch(A11yBusLauncher *app,
672 LOGD("Remove client watcher for %s", sender);
673 guint watcher_id = GPOINTER_TO_UINT(g_hash_table_lookup(app->client_watcher_id, sender));
675 g_bus_unwatch_name(watcher_id);
677 g_hash_table_remove(app->client_watcher_id, sender);
678 if (!is_client_connected(app))
679 handle_a11y_enabled_change (app, FALSE, TRUE);
683 on_client_name_vanished (GDBusConnection *connection,
687 A11yBusLauncher *app = user_data;
688 remove_client_watch(app, name);
692 add_client_watch(A11yBusLauncher *app,
695 LOGD("Add client watcher for %s", sender);
697 if (g_hash_table_contains(app->client_watcher_id, sender))
699 LOGI("Watcher for %s already registered", sender);
703 guint watcher_id = g_bus_watch_name(G_BUS_TYPE_SESSION,
705 G_BUS_NAME_WATCHER_FLAGS_NONE,
707 on_client_name_vanished,
711 g_hash_table_insert(app->client_watcher_id, g_strdup(sender), GUINT_TO_POINTER(watcher_id));
712 handle_a11y_enabled_change (app, TRUE, TRUE);
716 handle_set_property (GDBusConnection *connection,
718 const gchar *object_path,
719 const gchar *interface_name,
720 const gchar *property_name,
725 A11yBusLauncher *app = user_data;
726 const gchar *type = g_variant_get_type_string (value);
729 if (g_strcmp0 (type, "b") != 0)
731 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
732 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
736 enabled = g_variant_get_boolean (value);
738 if (g_strcmp0 (property_name, "IsEnabled") == 0)
741 add_client_watch(app, sender);
743 remove_client_watch(app, sender);
746 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
748 handle_screen_reader_enabled_change (app, enabled, TRUE);
753 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
754 "Unknown property '%s'", property_name);
759 static const GDBusInterfaceVTable bus_vtable =
762 NULL, /* handle_get_property, */
763 NULL /* handle_set_property */
766 static const GDBusInterfaceVTable status_vtable =
768 NULL, /* handle_method_call */
774 on_bus_acquired (GDBusConnection *connection,
778 A11yBusLauncher *app = user_data;
780 guint registration_id;
782 if (connection == NULL)
784 g_main_loop_quit (app->loop);
787 app->session_bus = connection;
789 if (app->launch_immediately)
791 ensure_a11y_bus (app);
792 if (app->state == A11Y_BUS_STATE_ERROR)
794 g_main_loop_quit (app->loop);
800 registration_id = g_dbus_connection_register_object (connection,
802 introspection_data->interfaces[0],
807 if (registration_id == 0)
809 g_error ("%s", error->message);
810 g_clear_error (&error);
813 g_dbus_connection_register_object (connection,
815 introspection_data->interfaces[1],
823 on_name_lost (GDBusConnection *connection,
827 A11yBusLauncher *app = user_data;
828 if (app->session_bus == NULL
829 && connection == NULL
830 && app->a11y_launch_error_message == NULL)
831 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
832 g_main_loop_quit (app->loop);
836 on_name_acquired (GDBusConnection *connection,
840 g_bus_watch_name (G_BUS_TYPE_SESSION,
841 "org.gnome.SessionManager",
842 G_BUS_NAME_WATCHER_FLAGS_NONE,
843 name_appeared_handler, NULL,
847 static int sigterm_pipefd[2];
850 sigterm_handler (int signum)
852 write (sigterm_pipefd[1], "X", 1);
856 on_sigterm_pipe (GIOChannel *channel,
857 GIOCondition condition,
860 A11yBusLauncher *app = data;
862 g_main_loop_quit (app->loop);
868 init_sigterm_handling (A11yBusLauncher *app)
870 GIOChannel *sigterm_channel;
872 if (pipe (sigterm_pipefd) < 0)
873 g_error ("Failed to create pipe: %s", strerror (errno));
874 signal (SIGTERM, sigterm_handler);
876 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
877 g_io_add_watch (sigterm_channel,
878 G_IO_IN | G_IO_ERR | G_IO_HUP,
889 Display *bridge_display;
891 unsigned char *data = NULL;
892 unsigned long nitems;
893 unsigned long leftover;
894 gboolean result = FALSE;
896 bridge_display = XOpenDisplay (NULL);
900 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
901 XGetWindowProperty (bridge_display,
902 XDefaultRootWindow (bridge_display),
904 (long) BUFSIZ, False,
905 (Atom) 31, &actual_type, &actual_format,
906 &nitems, &leftover, &data);
910 GDBusConnection *bus;
911 bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
916 g_object_unref (bus);
920 XCloseDisplay (bridge_display);
928 get_schema (const gchar *name)
930 #if GLIB_CHECK_VERSION (2, 32, 0)
931 GSettingsSchemaSource *source = g_settings_schema_source_get_default ();
932 if (!source) return NULL;
934 GSettingsSchema *schema = g_settings_schema_source_lookup (source, name, FALSE);
939 return g_settings_new_full (schema, NULL, NULL);
941 const char * const *schemas = NULL;
944 schemas = g_settings_list_schemas ();
945 for (i = 0; schemas[i]; i++)
947 if (!strcmp (schemas[i], name))
948 return g_settings_new (schemas[i]);
956 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
958 gboolean new_val = g_settings_get_boolean (gsettings, key);
960 if (!strcmp (key, "toolkit-accessibility"))
961 handle_a11y_enabled_change (_global_app, new_val, FALSE);
962 else if (!strcmp (key, "screen-reader-enabled"))
963 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
967 _process_dead_tracker (int pid, void *data)
969 A11yBusLauncher *app = data;
971 if (app->screen_reader.pid > 0 && pid == app->screen_reader.pid)
973 LOGE("screen reader is dead, pid: %d, restarting", pid);
974 app->screen_reader.pid = 0;
975 g_timeout_add_seconds (2, _launch_process_repeat_until_success, &app->screen_reader);
978 if (app->universal_switch.pid > 0 && pid == app->universal_switch.pid)
980 LOGE("universal switch is dead, pid: %d, restarting", pid);
981 app->universal_switch.pid = 0;
982 g_timeout_add_seconds (2, _launch_process_repeat_until_success, &app->universal_switch);
988 _register_process_dead_tracker ()
990 if(_global_app->screen_reader.pid > 0 || _global_app->universal_switch.pid > 0) {
991 LOGD("registering process dead tracker");
992 aul_listen_app_dead_signal(_process_dead_tracker, _global_app);
994 LOGD("unregistering process dead tracker");
995 aul_listen_app_dead_signal(NULL, NULL);
1001 _launch_client(A11yBusClient *client, gboolean by_vconf_change)
1003 LOGD("Launching %s", client->name);
1006 gboolean ret = FALSE;
1008 kb = bundle_create();
1012 LOGD("Can't create bundle");
1016 if (by_vconf_change)
1018 if (bundle_add_str(kb, "by_vconf_change", "yes") != BUNDLE_ERROR_NONE)
1020 LOGD("Can't add information to bundle");
1024 int operation_error = appsvc_set_operation(kb, client->app_control_operation);
1025 LOGD("appsvc_set_operation: %i", operation_error);
1027 client->pid = appsvc_run_service(kb, 0, NULL, NULL);
1029 if (client->pid > 0)
1031 LOGD("Process launched with pid: %i", client->pid);
1032 _register_process_dead_tracker();
1037 LOGD("Can't start %s - error code: %i", client->name, client->pid);
1045 _launch_process_repeat_until_success(gpointer user_data) {
1046 A11yBusClient *client = user_data;
1048 if (client->launch_repeats > 100 || client->pid > 0)
1050 //do not try anymore
1054 gboolean ret = _launch_client(client, FALSE);
1059 client->launch_repeats = 0;
1062 client->launch_repeats++;
1068 _terminate_process(int pid)
1075 int status = aul_app_get_status_bypid(pid);
1079 LOGD("App with pid %d already terminated", pid);
1083 LOGD("terminate process with pid %d", pid);
1084 ret_aul = aul_terminate_pid(pid);
1087 LOGD("Terminating with aul_terminate_pid: return is %d", ret_aul);
1091 LOGD("aul_terminate_pid failed: return is %d", ret_aul);
1093 LOGD("Unable to terminate process using aul api. Sending SIGTERM signal");
1094 ret = kill(pid, SIGTERM);
1100 LOGD("Unable to terminate process: %d with api or signal.", pid);
1105 _terminate_client(A11yBusClient *client)
1107 LOGD("Terminating %s", client->name);
1108 int pid = client->pid;
1110 _register_process_dead_tracker();
1111 gboolean ret = _terminate_process(pid);
1115 void vconf_client_cb(keynode_t *node, void *user_data)
1117 A11yBusClient *client = user_data;
1119 gboolean client_needed = FALSE;
1121 for (i = 0; i < client->number_of_keys; i++) {
1123 int ret =vconf_get_bool(client->vconf_key[i], &status);
1126 LOGD("Could not read %s key value.\n", client->vconf_key[i]);
1129 LOGD("vconf_keynode_get_bool(node): %i", status);
1134 client_needed = TRUE;
1139 //check if process really exists (e.g didn't crash)
1140 if (client->pid > 0)
1142 int err = kill(client->pid,0);
1143 //process doesn't exist
1148 LOGD("client_needed: %i, client->pid: %i", client_needed, client->pid);
1149 if (!client_needed && (client->pid > 0))
1150 _terminate_client(client);
1151 else if (client_needed && (client->pid <= 0))
1152 _launch_client(client, TRUE);
1156 static gboolean register_executable(A11yBusClient *client)
1158 gboolean client_needed = FALSE;
1161 for (i = 0; i < client->number_of_keys; i++) {
1162 if (!client->vconf_key[i]) {
1163 LOGE("Vconf_key missing for client: %d \n", i);
1168 int ret = vconf_get_bool(client->vconf_key[i], &status);
1171 LOGD("Could not read %s key value.\n", client->vconf_key[i]);
1174 ret = vconf_notify_key_changed(client->vconf_key[i], vconf_client_cb, client);
1177 LOGD("Could not add information level callback\n");
1181 client_needed = TRUE;
1185 g_timeout_add_seconds(2,_launch_process_repeat_until_success, client);
1193 #ifdef ATSPI_BUS_LAUNCHER_LOG_TO_FILE
1194 log_file = fopen("/tmp/at-spi-bus-launcher.log", "a");
1197 LOGD("Starting atspi bus launcher");
1199 gboolean a11y_set = FALSE;
1200 gboolean screen_reader_set = FALSE;
1203 if (already_running ())
1205 LOGD("atspi bus launcher is already running");
1209 _global_app = g_slice_new0 (A11yBusLauncher);
1210 _global_app->loop = g_main_loop_new (NULL, FALSE);
1211 _global_app->client_watcher_id = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1213 _global_app->screen_reader.name = "screen-reader";
1214 _global_app->screen_reader.app_control_operation = APP_CONTROL_OPERATION_SCREEN_READ;
1215 _global_app->screen_reader.vconf_key[0] = VCONFKEY_SETAPPL_ACCESSIBILITY_TTS;
1216 _global_app->screen_reader.number_of_keys = 1;
1218 _global_app->universal_switch.name = "universal-switch";
1219 _global_app->universal_switch.app_control_operation = APP_CONTROL_OPERATION_UNIVERSAL_SWITCH;
1220 _global_app->universal_switch.vconf_key[0] = VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE;
1221 _global_app->universal_switch.vconf_key[1] = VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE;
1222 _global_app->universal_switch.number_of_keys = 2;
1224 for (i = 1; i < argc; i++)
1226 if (!strcmp (argv[i], "--launch-immediately"))
1227 _global_app->launch_immediately = TRUE;
1228 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 1)
1230 else if (sscanf (argv[i], "--screen-reader=%d",
1231 &_global_app->screen_reader_enabled) == 1)
1232 screen_reader_set = TRUE;
1234 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
1237 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
1238 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
1242 _global_app->a11y_enabled = _global_app->interface_schema
1243 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
1244 : _global_app->launch_immediately;
1247 if (!screen_reader_set)
1249 _global_app->screen_reader_enabled = _global_app->a11y_schema
1250 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
1254 if (_global_app->interface_schema)
1255 g_signal_connect (_global_app->interface_schema,
1256 "changed::toolkit-accessibility",
1257 G_CALLBACK (gsettings_key_changed), _global_app);
1259 if (_global_app->a11y_schema)
1260 g_signal_connect (_global_app->a11y_schema,
1261 "changed::screen-reader-enabled",
1262 G_CALLBACK (gsettings_key_changed), _global_app);
1264 init_sigterm_handling (_global_app);
1266 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
1267 g_assert (introspection_data != NULL);
1269 g_bus_own_name (G_BUS_TYPE_SESSION,
1271 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
1278 register_executable (&_global_app->screen_reader);
1279 register_executable (&_global_app->universal_switch);
1281 g_main_loop_run (_global_app->loop);
1283 if (_global_app->a11y_bus_pid > 0)
1284 kill (_global_app->a11y_bus_pid, SIGTERM);
1286 /* Clear the X property if our bus is gone; in the case where e.g.
1287 * GDM is launching a login on an X server it was using before,
1288 * we don't want early login processes to pick up the stale address.
1292 Display *display = XOpenDisplay (NULL);
1295 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
1296 XDeleteProperty (display,
1297 XDefaultRootWindow (display),
1301 XCloseDisplay (display);
1306 if (_global_app->a11y_launch_error_message)
1308 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);