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.
34 #include <X11/Xatom.h>
37 A11Y_BUS_STATE_IDLE = 0,
38 A11Y_BUS_STATE_READING_ADDRESS,
39 A11Y_BUS_STATE_RUNNING,
45 gboolean launch_immediately;
46 gboolean a11y_enabled;
47 gboolean screen_reader_enabled;
48 GDBusConnection *session_bus;
49 GSettings *a11y_schema;
50 GSettings *interface_schema;
53 /* -1 == error, 0 == pending, > 0 == running */
55 char *a11y_bus_address;
57 char *a11y_launch_error_message;
60 static A11yBusLauncher *_global_app = NULL;
62 static const gchar introspection_xml[] =
64 " <interface name='org.a11y.Bus'>"
65 " <method name='GetAddress'>"
66 " <arg type='s' name='address' direction='out'/>"
69 "<interface name='org.a11y.Status'>"
70 "<property name='IsEnabled' type='b' access='readwrite'/>"
71 "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
74 static GDBusNodeInfo *introspection_data = NULL;
77 setup_bus_child (gpointer data)
79 A11yBusLauncher *app = data;
82 close (app->pipefd[0]);
83 dup2 (app->pipefd[1], 3);
84 close (app->pipefd[1]);
86 /* On Linux, tell the bus process to exit if this process goes away */
88 #include <sys/prctl.h>
89 prctl (PR_SET_PDEATHSIG, 15);
94 * unix_read_all_fd_to_string:
96 * Read all data from a file descriptor to a C string buffer.
99 unix_read_all_fd_to_string (int fd,
105 while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
110 max_bytes -= bytes_read;
117 on_bus_exited (GPid pid,
121 A11yBusLauncher *app = data;
123 app->a11y_bus_pid = -1;
124 app->state = A11Y_BUS_STATE_ERROR;
125 if (app->a11y_launch_error_message == NULL)
127 if (WIFEXITED (status))
128 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
129 else if (WIFSIGNALED (status))
130 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
131 else if (WIFSTOPPED (status))
132 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
134 g_main_loop_quit (app->loop);
138 ensure_a11y_bus (A11yBusLauncher *app)
141 char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
143 GError *error = NULL;
145 if (app->a11y_bus_pid != 0)
148 argv[1] = g_strdup_printf ("--config-file=%s/at-spi2/accessibility.conf", SYSCONFDIR);
150 if (pipe (app->pipefd) < 0)
151 g_error ("Failed to create pipe: %s", strerror (errno));
153 if (!g_spawn_async (NULL,
156 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
162 app->a11y_bus_pid = -1;
163 app->a11y_launch_error_message = g_strdup (error->message);
164 g_clear_error (&error);
168 close (app->pipefd[1]);
171 g_child_watch_add (pid, on_bus_exited, app);
173 app->state = A11Y_BUS_STATE_READING_ADDRESS;
174 app->a11y_bus_pid = pid;
175 g_debug ("Launched a11y bus, child is %ld", (long) pid);
176 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
178 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
179 kill (app->a11y_bus_pid, SIGTERM);
182 close (app->pipefd[0]);
184 app->state = A11Y_BUS_STATE_RUNNING;
186 /* Trim the trailing newline */
187 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
188 g_debug ("a11y bus address: %s", app->a11y_bus_address);
191 Display *display = XOpenDisplay (NULL);
194 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
195 XChangeProperty (display,
196 XDefaultRootWindow (display),
198 XA_STRING, 8, PropModeReplace,
199 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
201 XCloseDisplay (display);
208 close (app->pipefd[0]);
209 close (app->pipefd[1]);
210 app->state = A11Y_BUS_STATE_ERROR;
216 handle_method_call (GDBusConnection *connection,
218 const gchar *object_path,
219 const gchar *interface_name,
220 const gchar *method_name,
221 GVariant *parameters,
222 GDBusMethodInvocation *invocation,
225 A11yBusLauncher *app = user_data;
227 if (g_strcmp0 (method_name, "GetAddress") == 0)
229 ensure_a11y_bus (app);
230 if (app->a11y_bus_pid > 0)
231 g_dbus_method_invocation_return_value (invocation,
232 g_variant_new ("(s)", app->a11y_bus_address));
234 g_dbus_method_invocation_return_dbus_error (invocation,
235 "org.a11y.Bus.Error",
236 app->a11y_launch_error_message);
241 handle_get_property (GDBusConnection *connection,
243 const gchar *object_path,
244 const gchar *interface_name,
245 const gchar *property_name,
249 A11yBusLauncher *app = user_data;
251 if (g_strcmp0 (property_name, "IsEnabled") == 0)
252 return g_variant_new ("b", app->a11y_enabled);
253 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
254 return g_variant_new ("b", app->screen_reader_enabled);
260 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
261 gboolean notify_gsettings)
263 GVariantBuilder builder;
264 GVariantBuilder invalidated_builder;
266 if (enabled == app->a11y_enabled)
269 app->a11y_enabled = enabled;
271 if (notify_gsettings && app->interface_schema)
273 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
278 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
279 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
280 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
281 g_variant_new_boolean (enabled));
283 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
284 "org.freedesktop.DBus.Properties",
286 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
288 &invalidated_builder),
293 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
294 gboolean notify_gsettings)
296 GVariantBuilder builder;
297 GVariantBuilder invalidated_builder;
299 if (enabled == app->screen_reader_enabled)
302 /* If the screen reader is being enabled, we should enable accessibility
303 * if it isn't enabled already */
305 handle_a11y_enabled_change (app, enabled, notify_gsettings);
307 app->screen_reader_enabled = enabled;
309 if (notify_gsettings && app->a11y_schema)
311 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
316 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
317 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
318 g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
319 g_variant_new_boolean (enabled));
321 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
322 "org.freedesktop.DBus.Properties",
324 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
326 &invalidated_builder),
331 handle_set_property (GDBusConnection *connection,
333 const gchar *object_path,
334 const gchar *interface_name,
335 const gchar *property_name,
340 A11yBusLauncher *app = user_data;
341 const gchar *type = g_variant_get_type_string (value);
344 if (g_strcmp0 (type, "b") != 0)
346 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
347 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
351 enabled = g_variant_get_boolean (value);
353 if (g_strcmp0 (property_name, "IsEnabled") == 0)
355 handle_a11y_enabled_change (app, enabled, TRUE);
358 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
360 handle_screen_reader_enabled_change (app, enabled, TRUE);
365 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
366 "Unknown property '%s'", property_name);
371 static const GDBusInterfaceVTable bus_vtable =
374 NULL, /* handle_get_property, */
375 NULL /* handle_set_property */
378 static const GDBusInterfaceVTable status_vtable =
380 NULL, /* handle_method_call */
386 on_bus_acquired (GDBusConnection *connection,
390 A11yBusLauncher *app = user_data;
392 guint registration_id;
394 if (connection == NULL)
396 g_main_loop_quit (app->loop);
399 app->session_bus = connection;
401 if (app->launch_immediately)
403 ensure_a11y_bus (app);
404 if (app->state == A11Y_BUS_STATE_ERROR)
406 g_main_loop_quit (app->loop);
412 registration_id = g_dbus_connection_register_object (connection,
414 introspection_data->interfaces[0],
419 if (registration_id == 0)
420 g_error ("%s", error->message);
422 g_dbus_connection_register_object (connection,
424 introspection_data->interfaces[1],
432 on_name_lost (GDBusConnection *connection,
436 A11yBusLauncher *app = user_data;
437 if (app->session_bus == NULL
438 && connection == NULL
439 && app->a11y_launch_error_message == NULL)
440 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
441 g_main_loop_quit (app->loop);
445 on_name_acquired (GDBusConnection *connection,
449 A11yBusLauncher *app = user_data;
453 static int sigterm_pipefd[2];
456 sigterm_handler (int signum)
458 write (sigterm_pipefd[1], "X", 1);
462 on_sigterm_pipe (GIOChannel *channel,
463 GIOCondition condition,
466 A11yBusLauncher *app = data;
468 g_main_loop_quit (app->loop);
474 init_sigterm_handling (A11yBusLauncher *app)
476 GIOChannel *sigterm_channel;
478 if (pipe (sigterm_pipefd) < 0)
479 g_error ("Failed to create pipe: %s", strerror (errno));
480 signal (SIGTERM, sigterm_handler);
482 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
483 g_io_add_watch (sigterm_channel,
484 G_IO_IN | G_IO_ERR | G_IO_HUP,
494 Display *bridge_display;
496 unsigned char *data = NULL;
497 unsigned long nitems;
498 unsigned long leftover;
499 gboolean result = FALSE;
501 bridge_display = XOpenDisplay (NULL);
505 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
506 XGetWindowProperty (bridge_display,
507 XDefaultRootWindow (bridge_display),
509 (long) BUFSIZ, False,
510 (Atom) 31, &actual_type, &actual_format,
511 &nitems, &leftover, &data);
515 GDBusConnection *bus;
516 GError *error = NULL;
517 const gchar *old_session = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
518 /* TODO: Is there a better way to connect? This is really hacky */
519 g_setenv ("DBUS_SESSION_BUS_ADDRESS", data, TRUE);
520 bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
521 g_setenv ("DBUS_SESSION_BUS_ADDRESS", old_session, TRUE);
525 g_object_unref (bus);
529 XCloseDisplay (bridge_display);
534 get_schema (const gchar *name)
536 const char * const *schemas = NULL;
539 schemas = g_settings_list_schemas ();
540 for (i = 0; schemas[i]; i++)
542 if (!strcmp (schemas[i], name))
543 return g_settings_new (schemas[i]);
550 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
552 gboolean new_val = g_settings_get_boolean (gsettings, key);
553 A11yBusLauncher *app = user_data;
555 if (!strcmp (key, "toolkit-accessibility"))
556 handle_a11y_enabled_change (_global_app, new_val, FALSE);
557 else if (!strcmp (key, "screen-reader-enabled"))
558 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
565 GError *error = NULL;
567 GDBusConnection *session_bus;
569 gboolean a11y_set = FALSE;
570 gboolean screen_reader_set = FALSE;
575 if (already_running ())
578 _global_app = g_slice_new0 (A11yBusLauncher);
579 _global_app->loop = g_main_loop_new (NULL, FALSE);
581 for (i = 1; i < argc; i++)
583 if (!strcmp (argv[i], "--launch-immediately"))
584 _global_app->launch_immediately = TRUE;
585 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 2)
587 else if (sscanf (argv[i], "--screen-reader=%d",
588 &_global_app->screen_reader_enabled) == 2)
589 screen_reader_set = TRUE;
591 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
594 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
595 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
599 _global_app->a11y_enabled = _global_app->interface_schema
600 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
601 : _global_app->launch_immediately;
604 if (!screen_reader_set)
606 _global_app->screen_reader_enabled = _global_app->a11y_schema
607 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
611 if (_global_app->interface_schema)
612 g_signal_connect (_global_app->interface_schema,
613 "changed::toolkit-accessibility",
614 G_CALLBACK (gsettings_key_changed), _global_app);
616 if (_global_app->a11y_schema)
617 g_signal_connect (_global_app->a11y_schema,
618 "changed::screen-reader-enabled",
619 G_CALLBACK (gsettings_key_changed), _global_app);
621 init_sigterm_handling (_global_app);
623 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
624 g_assert (introspection_data != NULL);
626 name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
628 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
635 g_main_loop_run (_global_app->loop);
637 if (_global_app->a11y_bus_pid > 0)
638 kill (_global_app->a11y_bus_pid, SIGTERM);
640 /* Clear the X property if our bus is gone; in the case where e.g.
641 * GDM is launching a login on an X server it was using before,
642 * we don't want early login processes to pick up the stale address.
645 Display *display = XOpenDisplay (NULL);
648 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
649 XDeleteProperty (display,
650 XDefaultRootWindow (display),
654 XCloseDisplay (display);
658 if (_global_app->a11y_launch_error_message)
660 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);