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;
54 GDBusProxy *client_proxy;
57 /* -1 == error, 0 == pending, > 0 == running */
59 char *a11y_bus_address;
61 char *a11y_launch_error_message;
64 static A11yBusLauncher *_global_app = NULL;
66 static const gchar introspection_xml[] =
68 " <interface name='org.a11y.Bus'>"
69 " <method name='GetAddress'>"
70 " <arg type='s' name='address' direction='out'/>"
73 "<interface name='org.a11y.Status'>"
74 "<property name='IsEnabled' type='b' access='readwrite'/>"
75 "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
78 static GDBusNodeInfo *introspection_data = NULL;
81 respond_to_end_session (GDBusProxy *proxy)
85 parameters = g_variant_new ("(bs)", TRUE, "");
87 g_dbus_proxy_call (proxy,
88 "EndSessionResponse", parameters,
89 G_DBUS_CALL_FLAGS_NONE,
90 -1, NULL, NULL, NULL);
94 g_signal_cb (GDBusProxy *proxy,
100 A11yBusLauncher *app = user_data;
102 if (g_strcmp0 (signal_name, "QueryEndSession") == 0)
103 respond_to_end_session (proxy);
104 else if (g_strcmp0 (signal_name, "EndSession") == 0)
105 respond_to_end_session (proxy);
106 else if (g_strcmp0 (signal_name, "Stop") == 0)
107 g_main_loop_quit (app->loop);
111 client_proxy_ready_cb (GObject *source_object,
115 A11yBusLauncher *app = user_data;
116 GError *error = NULL;
118 app->client_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
122 g_warning ("Failed to get a client proxy: %s", error->message);
123 g_error_free (error);
128 g_signal_connect (app->client_proxy, "g-signal",
129 G_CALLBACK (g_signal_cb), app);
133 register_client (A11yBusLauncher *app)
135 GDBusProxyFlags flags;
136 GDBusProxy *sm_proxy;
139 const gchar *autostart_id;
140 gchar *client_startup_id;
141 GVariant *parameters;
145 flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
146 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS;
149 sm_proxy = g_dbus_proxy_new_sync (app->session_bus, flags, NULL,
150 "org.gnome.SessionManager",
151 "/org/gnome/SessionManager",
152 "org.gnome.SessionManager",
157 g_warning ("Failed to get session manager proxy: %s", error->message);
158 g_error_free (error);
163 app_id = "at-spi-bus-launcher";
164 autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
166 if (autostart_id != NULL)
168 client_startup_id = g_strdup (autostart_id);
169 g_unsetenv ("DESKTOP_AUTOSTART_ID");
173 client_startup_id = g_strdup ("");
176 parameters = g_variant_new ("(ss)", app_id, client_startup_id);
177 g_free (client_startup_id);
180 variant = g_dbus_proxy_call_sync (sm_proxy,
181 "RegisterClient", parameters,
182 G_DBUS_CALL_FLAGS_NONE,
185 g_object_unref (sm_proxy);
189 g_warning ("Failed to register client: %s", error->message);
190 g_error_free (error);
195 g_variant_get (variant, "(o)", &object_path);
196 g_variant_unref (variant);
198 flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
199 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, flags, NULL,
200 "org.gnome.SessionManager", object_path,
201 "org.gnome.SessionManager.ClientPrivate",
202 NULL, client_proxy_ready_cb, app);
204 g_free (object_path);
208 name_appeared_handler (GDBusConnection *connection,
210 const gchar *name_owner,
213 A11yBusLauncher *app = user_data;
215 register_client (app);
219 setup_bus_child (gpointer data)
221 A11yBusLauncher *app = data;
224 close (app->pipefd[0]);
225 dup2 (app->pipefd[1], 3);
226 close (app->pipefd[1]);
228 /* On Linux, tell the bus process to exit if this process goes away */
230 #include <sys/prctl.h>
231 prctl (PR_SET_PDEATHSIG, 15);
236 * unix_read_all_fd_to_string:
238 * Read all data from a file descriptor to a C string buffer.
241 unix_read_all_fd_to_string (int fd,
247 while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
252 max_bytes -= bytes_read;
259 on_bus_exited (GPid pid,
263 A11yBusLauncher *app = data;
265 app->a11y_bus_pid = -1;
266 app->state = A11Y_BUS_STATE_ERROR;
267 if (app->a11y_launch_error_message == NULL)
269 if (WIFEXITED (status))
270 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
271 else if (WIFSIGNALED (status))
272 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
273 else if (WIFSTOPPED (status))
274 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
276 g_main_loop_quit (app->loop);
280 ensure_a11y_bus (A11yBusLauncher *app)
283 char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
285 GError *error = NULL;
286 const char *config_path = NULL;
288 if (app->a11y_bus_pid != 0)
291 if (g_file_test (SYSCONFDIR"/at-spi2/accessibility.conf", G_FILE_TEST_EXISTS))
292 config_path = "--config-file="SYSCONFDIR"/at-spi2/accessibility.conf";
294 config_path = "--config-file="DATADIR"/defaults/at-spi2/accessibility.conf";
296 argv[1] = config_path;
298 if (pipe (app->pipefd) < 0)
299 g_error ("Failed to create pipe: %s", strerror (errno));
301 if (!g_spawn_async (NULL,
304 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
310 app->a11y_bus_pid = -1;
311 app->a11y_launch_error_message = g_strdup (error->message);
312 g_clear_error (&error);
316 close (app->pipefd[1]);
319 g_child_watch_add (pid, on_bus_exited, app);
321 app->state = A11Y_BUS_STATE_READING_ADDRESS;
322 app->a11y_bus_pid = pid;
323 g_debug ("Launched a11y bus, child is %ld", (long) pid);
324 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
326 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
327 kill (app->a11y_bus_pid, SIGTERM);
330 close (app->pipefd[0]);
332 app->state = A11Y_BUS_STATE_RUNNING;
334 /* Trim the trailing newline */
335 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
336 g_debug ("a11y bus address: %s", app->a11y_bus_address);
340 Display *display = XOpenDisplay (NULL);
343 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
344 XChangeProperty (display,
345 XDefaultRootWindow (display),
347 XA_STRING, 8, PropModeReplace,
348 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
350 XCloseDisplay (display);
358 close (app->pipefd[0]);
359 close (app->pipefd[1]);
360 app->state = A11Y_BUS_STATE_ERROR;
366 handle_method_call (GDBusConnection *connection,
368 const gchar *object_path,
369 const gchar *interface_name,
370 const gchar *method_name,
371 GVariant *parameters,
372 GDBusMethodInvocation *invocation,
375 A11yBusLauncher *app = user_data;
377 if (g_strcmp0 (method_name, "GetAddress") == 0)
379 ensure_a11y_bus (app);
380 if (app->a11y_bus_pid > 0)
381 g_dbus_method_invocation_return_value (invocation,
382 g_variant_new ("(s)", app->a11y_bus_address));
384 g_dbus_method_invocation_return_dbus_error (invocation,
385 "org.a11y.Bus.Error",
386 app->a11y_launch_error_message);
391 handle_get_property (GDBusConnection *connection,
393 const gchar *object_path,
394 const gchar *interface_name,
395 const gchar *property_name,
399 A11yBusLauncher *app = user_data;
401 if (g_strcmp0 (property_name, "IsEnabled") == 0)
402 return g_variant_new ("b", app->a11y_enabled);
403 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
404 return g_variant_new ("b", app->screen_reader_enabled);
410 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
411 gboolean notify_gsettings)
413 GVariantBuilder builder;
414 GVariantBuilder invalidated_builder;
416 if (enabled == app->a11y_enabled)
419 app->a11y_enabled = enabled;
421 if (notify_gsettings && app->interface_schema)
423 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
428 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
429 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
430 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
431 g_variant_new_boolean (enabled));
433 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
434 "org.freedesktop.DBus.Properties",
436 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
438 &invalidated_builder),
441 g_variant_builder_clear (&builder);
442 g_variant_builder_clear (&invalidated_builder);
446 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
447 gboolean notify_gsettings)
449 GVariantBuilder builder;
450 GVariantBuilder invalidated_builder;
452 if (enabled == app->screen_reader_enabled)
455 /* If the screen reader is being enabled, we should enable accessibility
456 * if it isn't enabled already */
458 handle_a11y_enabled_change (app, enabled, notify_gsettings);
460 app->screen_reader_enabled = enabled;
462 if (notify_gsettings && app->a11y_schema)
464 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
469 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
470 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
471 g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
472 g_variant_new_boolean (enabled));
474 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
475 "org.freedesktop.DBus.Properties",
477 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
479 &invalidated_builder),
482 g_variant_builder_clear (&builder);
483 g_variant_builder_clear (&invalidated_builder);
487 handle_set_property (GDBusConnection *connection,
489 const gchar *object_path,
490 const gchar *interface_name,
491 const gchar *property_name,
496 A11yBusLauncher *app = user_data;
497 const gchar *type = g_variant_get_type_string (value);
500 if (g_strcmp0 (type, "b") != 0)
502 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
503 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
507 enabled = g_variant_get_boolean (value);
509 if (g_strcmp0 (property_name, "IsEnabled") == 0)
511 handle_a11y_enabled_change (app, enabled, TRUE);
514 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
516 handle_screen_reader_enabled_change (app, enabled, TRUE);
521 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
522 "Unknown property '%s'", property_name);
527 static const GDBusInterfaceVTable bus_vtable =
530 NULL, /* handle_get_property, */
531 NULL /* handle_set_property */
534 static const GDBusInterfaceVTable status_vtable =
536 NULL, /* handle_method_call */
542 on_bus_acquired (GDBusConnection *connection,
546 A11yBusLauncher *app = user_data;
548 guint registration_id;
550 if (connection == NULL)
552 g_main_loop_quit (app->loop);
555 app->session_bus = connection;
557 if (app->launch_immediately)
559 ensure_a11y_bus (app);
560 if (app->state == A11Y_BUS_STATE_ERROR)
562 g_main_loop_quit (app->loop);
568 registration_id = g_dbus_connection_register_object (connection,
570 introspection_data->interfaces[0],
575 if (registration_id == 0)
577 g_error ("%s", error->message);
578 g_clear_error (&error);
581 g_dbus_connection_register_object (connection,
583 introspection_data->interfaces[1],
591 on_name_lost (GDBusConnection *connection,
595 A11yBusLauncher *app = user_data;
596 if (app->session_bus == NULL
597 && connection == NULL
598 && app->a11y_launch_error_message == NULL)
599 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
600 g_main_loop_quit (app->loop);
604 on_name_acquired (GDBusConnection *connection,
608 g_bus_watch_name (G_BUS_TYPE_SESSION,
609 "org.gnome.SessionManager",
610 G_BUS_NAME_WATCHER_FLAGS_NONE,
611 name_appeared_handler, NULL,
615 static int sigterm_pipefd[2];
618 sigterm_handler (int signum)
620 write (sigterm_pipefd[1], "X", 1);
624 on_sigterm_pipe (GIOChannel *channel,
625 GIOCondition condition,
628 A11yBusLauncher *app = data;
630 g_main_loop_quit (app->loop);
636 init_sigterm_handling (A11yBusLauncher *app)
638 GIOChannel *sigterm_channel;
640 if (pipe (sigterm_pipefd) < 0)
641 g_error ("Failed to create pipe: %s", strerror (errno));
642 signal (SIGTERM, sigterm_handler);
644 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
645 g_io_add_watch (sigterm_channel,
646 G_IO_IN | G_IO_ERR | G_IO_HUP,
657 Display *bridge_display;
659 unsigned char *data = NULL;
660 unsigned long nitems;
661 unsigned long leftover;
662 gboolean result = FALSE;
664 bridge_display = XOpenDisplay (NULL);
668 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
669 XGetWindowProperty (bridge_display,
670 XDefaultRootWindow (bridge_display),
672 (long) BUFSIZ, False,
673 (Atom) 31, &actual_type, &actual_format,
674 &nitems, &leftover, &data);
678 GDBusConnection *bus;
679 bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
684 g_object_unref (bus);
688 XCloseDisplay (bridge_display);
696 get_schema (const gchar *name)
698 const char * const *schemas = NULL;
701 schemas = g_settings_list_schemas ();
702 for (i = 0; schemas[i]; i++)
704 if (!strcmp (schemas[i], name))
705 return g_settings_new (schemas[i]);
712 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
714 gboolean new_val = g_settings_get_boolean (gsettings, key);
715 A11yBusLauncher *app = user_data;
717 if (!strcmp (key, "toolkit-accessibility"))
718 handle_a11y_enabled_change (_global_app, new_val, FALSE);
719 else if (!strcmp (key, "screen-reader-enabled"))
720 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
727 GError *error = NULL;
729 GDBusConnection *session_bus;
731 gboolean a11y_set = FALSE;
732 gboolean screen_reader_set = FALSE;
735 if (already_running ())
738 _global_app = g_slice_new0 (A11yBusLauncher);
739 _global_app->loop = g_main_loop_new (NULL, FALSE);
741 for (i = 1; i < argc; i++)
743 if (!strcmp (argv[i], "--launch-immediately"))
744 _global_app->launch_immediately = TRUE;
745 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 1)
747 else if (sscanf (argv[i], "--screen-reader=%d",
748 &_global_app->screen_reader_enabled) == 1)
749 screen_reader_set = TRUE;
751 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
754 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
755 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
759 _global_app->a11y_enabled = _global_app->interface_schema
760 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
761 : _global_app->launch_immediately;
764 if (!screen_reader_set)
766 _global_app->screen_reader_enabled = _global_app->a11y_schema
767 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
771 if (_global_app->interface_schema)
772 g_signal_connect (_global_app->interface_schema,
773 "changed::toolkit-accessibility",
774 G_CALLBACK (gsettings_key_changed), _global_app);
776 if (_global_app->a11y_schema)
777 g_signal_connect (_global_app->a11y_schema,
778 "changed::screen-reader-enabled",
779 G_CALLBACK (gsettings_key_changed), _global_app);
781 init_sigterm_handling (_global_app);
783 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
784 g_assert (introspection_data != NULL);
786 name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
788 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
795 g_main_loop_run (_global_app->loop);
797 if (_global_app->a11y_bus_pid > 0)
798 kill (_global_app->a11y_bus_pid, SIGTERM);
800 /* Clear the X property if our bus is gone; in the case where e.g.
801 * GDM is launching a login on an X server it was using before,
802 * we don't want early login processes to pick up the stale address.
806 Display *display = XOpenDisplay (NULL);
809 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
810 XDeleteProperty (display,
811 XDefaultRootWindow (display),
815 XCloseDisplay (display);
820 if (_global_app->a11y_launch_error_message)
822 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);