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>
39 A11Y_BUS_STATE_IDLE = 0,
40 A11Y_BUS_STATE_READING_ADDRESS,
41 A11Y_BUS_STATE_RUNNING,
47 gboolean launch_immediately;
48 gboolean a11y_enabled;
49 gboolean screen_reader_enabled;
50 GDBusConnection *session_bus;
51 GSettings *a11y_schema;
52 GSettings *interface_schema;
55 /* -1 == error, 0 == pending, > 0 == running */
57 char *a11y_bus_address;
59 char *a11y_launch_error_message;
62 static A11yBusLauncher *_global_app = NULL;
64 static const gchar introspection_xml[] =
66 " <interface name='org.a11y.Bus'>"
67 " <method name='GetAddress'>"
68 " <arg type='s' name='address' direction='out'/>"
71 "<interface name='org.a11y.Status'>"
72 "<property name='IsEnabled' type='b' access='readwrite'/>"
73 "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
76 static GDBusNodeInfo *introspection_data = NULL;
79 setup_bus_child (gpointer data)
81 A11yBusLauncher *app = data;
84 close (app->pipefd[0]);
85 dup2 (app->pipefd[1], 3);
86 close (app->pipefd[1]);
88 /* On Linux, tell the bus process to exit if this process goes away */
90 #include <sys/prctl.h>
91 prctl (PR_SET_PDEATHSIG, 15);
96 * unix_read_all_fd_to_string:
98 * Read all data from a file descriptor to a C string buffer.
101 unix_read_all_fd_to_string (int fd,
107 while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
112 max_bytes -= bytes_read;
119 on_bus_exited (GPid pid,
123 A11yBusLauncher *app = data;
125 app->a11y_bus_pid = -1;
126 app->state = A11Y_BUS_STATE_ERROR;
127 if (app->a11y_launch_error_message == NULL)
129 if (WIFEXITED (status))
130 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
131 else if (WIFSIGNALED (status))
132 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
133 else if (WIFSTOPPED (status))
134 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
136 g_main_loop_quit (app->loop);
140 ensure_a11y_bus (A11yBusLauncher *app)
143 char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
145 GError *error = NULL;
147 if (app->a11y_bus_pid != 0)
150 argv[1] = g_strdup_printf ("--config-file=%s/at-spi2/accessibility.conf", SYSCONFDIR);
152 if (pipe (app->pipefd) < 0)
153 g_error ("Failed to create pipe: %s", strerror (errno));
155 if (!g_spawn_async (NULL,
158 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
164 app->a11y_bus_pid = -1;
165 app->a11y_launch_error_message = g_strdup (error->message);
166 g_clear_error (&error);
170 close (app->pipefd[1]);
173 g_child_watch_add (pid, on_bus_exited, app);
175 app->state = A11Y_BUS_STATE_READING_ADDRESS;
176 app->a11y_bus_pid = pid;
177 g_debug ("Launched a11y bus, child is %ld", (long) pid);
178 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
180 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
181 kill (app->a11y_bus_pid, SIGTERM);
184 close (app->pipefd[0]);
186 app->state = A11Y_BUS_STATE_RUNNING;
188 /* Trim the trailing newline */
189 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
190 g_debug ("a11y bus address: %s", app->a11y_bus_address);
194 Display *display = XOpenDisplay (NULL);
197 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
198 XChangeProperty (display,
199 XDefaultRootWindow (display),
201 XA_STRING, 8, PropModeReplace,
202 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
204 XCloseDisplay (display);
212 close (app->pipefd[0]);
213 close (app->pipefd[1]);
214 app->state = A11Y_BUS_STATE_ERROR;
220 handle_method_call (GDBusConnection *connection,
222 const gchar *object_path,
223 const gchar *interface_name,
224 const gchar *method_name,
225 GVariant *parameters,
226 GDBusMethodInvocation *invocation,
229 A11yBusLauncher *app = user_data;
231 if (g_strcmp0 (method_name, "GetAddress") == 0)
233 ensure_a11y_bus (app);
234 if (app->a11y_bus_pid > 0)
235 g_dbus_method_invocation_return_value (invocation,
236 g_variant_new ("(s)", app->a11y_bus_address));
238 g_dbus_method_invocation_return_dbus_error (invocation,
239 "org.a11y.Bus.Error",
240 app->a11y_launch_error_message);
245 handle_get_property (GDBusConnection *connection,
247 const gchar *object_path,
248 const gchar *interface_name,
249 const gchar *property_name,
253 A11yBusLauncher *app = user_data;
255 if (g_strcmp0 (property_name, "IsEnabled") == 0)
256 return g_variant_new ("b", app->a11y_enabled);
257 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
258 return g_variant_new ("b", app->screen_reader_enabled);
264 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
265 gboolean notify_gsettings)
267 GVariantBuilder builder;
268 GVariantBuilder invalidated_builder;
270 if (enabled == app->a11y_enabled)
273 app->a11y_enabled = enabled;
275 if (notify_gsettings && app->interface_schema)
277 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
282 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
283 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
284 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
285 g_variant_new_boolean (enabled));
287 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
288 "org.freedesktop.DBus.Properties",
290 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
292 &invalidated_builder),
297 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
298 gboolean notify_gsettings)
300 GVariantBuilder builder;
301 GVariantBuilder invalidated_builder;
303 if (enabled == app->screen_reader_enabled)
306 /* If the screen reader is being enabled, we should enable accessibility
307 * if it isn't enabled already */
309 handle_a11y_enabled_change (app, enabled, notify_gsettings);
311 app->screen_reader_enabled = enabled;
313 if (notify_gsettings && app->a11y_schema)
315 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
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}", "ScreenReaderEnabled",
323 g_variant_new_boolean (enabled));
325 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
326 "org.freedesktop.DBus.Properties",
328 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
330 &invalidated_builder),
335 handle_set_property (GDBusConnection *connection,
337 const gchar *object_path,
338 const gchar *interface_name,
339 const gchar *property_name,
344 A11yBusLauncher *app = user_data;
345 const gchar *type = g_variant_get_type_string (value);
348 if (g_strcmp0 (type, "b") != 0)
350 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
351 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
355 enabled = g_variant_get_boolean (value);
357 if (g_strcmp0 (property_name, "IsEnabled") == 0)
359 handle_a11y_enabled_change (app, enabled, TRUE);
362 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
364 handle_screen_reader_enabled_change (app, enabled, TRUE);
369 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
370 "Unknown property '%s'", property_name);
375 static const GDBusInterfaceVTable bus_vtable =
378 NULL, /* handle_get_property, */
379 NULL /* handle_set_property */
382 static const GDBusInterfaceVTable status_vtable =
384 NULL, /* handle_method_call */
390 on_bus_acquired (GDBusConnection *connection,
394 A11yBusLauncher *app = user_data;
396 guint registration_id;
398 if (connection == NULL)
400 g_main_loop_quit (app->loop);
403 app->session_bus = connection;
405 if (app->launch_immediately)
407 ensure_a11y_bus (app);
408 if (app->state == A11Y_BUS_STATE_ERROR)
410 g_main_loop_quit (app->loop);
416 registration_id = g_dbus_connection_register_object (connection,
418 introspection_data->interfaces[0],
423 if (registration_id == 0)
425 g_error ("%s", error->message);
426 g_clear_error (&error);
429 g_dbus_connection_register_object (connection,
431 introspection_data->interfaces[1],
439 on_name_lost (GDBusConnection *connection,
443 A11yBusLauncher *app = user_data;
444 if (app->session_bus == NULL
445 && connection == NULL
446 && app->a11y_launch_error_message == NULL)
447 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
448 g_main_loop_quit (app->loop);
452 on_name_acquired (GDBusConnection *connection,
456 A11yBusLauncher *app = user_data;
460 static int sigterm_pipefd[2];
463 sigterm_handler (int signum)
465 write (sigterm_pipefd[1], "X", 1);
469 on_sigterm_pipe (GIOChannel *channel,
470 GIOCondition condition,
473 A11yBusLauncher *app = data;
475 g_main_loop_quit (app->loop);
481 init_sigterm_handling (A11yBusLauncher *app)
483 GIOChannel *sigterm_channel;
485 if (pipe (sigterm_pipefd) < 0)
486 g_error ("Failed to create pipe: %s", strerror (errno));
487 signal (SIGTERM, sigterm_handler);
489 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
490 g_io_add_watch (sigterm_channel,
491 G_IO_IN | G_IO_ERR | G_IO_HUP,
502 Display *bridge_display;
504 unsigned char *data = NULL;
505 unsigned long nitems;
506 unsigned long leftover;
507 gboolean result = FALSE;
509 bridge_display = XOpenDisplay (NULL);
513 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
514 XGetWindowProperty (bridge_display,
515 XDefaultRootWindow (bridge_display),
517 (long) BUFSIZ, False,
518 (Atom) 31, &actual_type, &actual_format,
519 &nitems, &leftover, &data);
523 GDBusConnection *bus;
524 const gchar *old_session = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
525 /* TODO: Is there a better way to connect? This is really hacky */
526 g_setenv ("DBUS_SESSION_BUS_ADDRESS", data, TRUE);
527 bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
528 g_setenv ("DBUS_SESSION_BUS_ADDRESS", old_session, TRUE);
532 g_object_unref (bus);
536 XCloseDisplay (bridge_display);
544 get_schema (const gchar *name)
546 const char * const *schemas = NULL;
549 schemas = g_settings_list_schemas ();
550 for (i = 0; schemas[i]; i++)
552 if (!strcmp (schemas[i], name))
553 return g_settings_new (schemas[i]);
560 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
562 gboolean new_val = g_settings_get_boolean (gsettings, key);
563 A11yBusLauncher *app = user_data;
565 if (!strcmp (key, "toolkit-accessibility"))
566 handle_a11y_enabled_change (_global_app, new_val, FALSE);
567 else if (!strcmp (key, "screen-reader-enabled"))
568 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
575 GError *error = NULL;
577 GDBusConnection *session_bus;
579 gboolean a11y_set = FALSE;
580 gboolean screen_reader_set = FALSE;
583 if (already_running ())
586 _global_app = g_slice_new0 (A11yBusLauncher);
587 _global_app->loop = g_main_loop_new (NULL, FALSE);
589 for (i = 1; i < argc; i++)
591 if (!strcmp (argv[i], "--launch-immediately"))
592 _global_app->launch_immediately = TRUE;
593 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 2)
595 else if (sscanf (argv[i], "--screen-reader=%d",
596 &_global_app->screen_reader_enabled) == 2)
597 screen_reader_set = TRUE;
599 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
602 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
603 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
607 _global_app->a11y_enabled = _global_app->interface_schema
608 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
609 : _global_app->launch_immediately;
612 if (!screen_reader_set)
614 _global_app->screen_reader_enabled = _global_app->a11y_schema
615 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
619 if (_global_app->interface_schema)
620 g_signal_connect (_global_app->interface_schema,
621 "changed::toolkit-accessibility",
622 G_CALLBACK (gsettings_key_changed), _global_app);
624 if (_global_app->a11y_schema)
625 g_signal_connect (_global_app->a11y_schema,
626 "changed::screen-reader-enabled",
627 G_CALLBACK (gsettings_key_changed), _global_app);
629 init_sigterm_handling (_global_app);
631 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
632 g_assert (introspection_data != NULL);
634 name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
636 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
643 g_main_loop_run (_global_app->loop);
645 if (_global_app->a11y_bus_pid > 0)
646 kill (_global_app->a11y_bus_pid, SIGTERM);
648 /* Clear the X property if our bus is gone; in the case where e.g.
649 * GDM is launching a login on an X server it was using before,
650 * we don't want early login processes to pick up the stale address.
654 Display *display = XOpenDisplay (NULL);
657 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
658 XDeleteProperty (display,
659 XDefaultRootWindow (display),
663 XCloseDisplay (display);
668 if (_global_app->a11y_launch_error_message)
670 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);