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 setup_bus_child (gpointer data)
210 A11yBusLauncher *app = data;
213 close (app->pipefd[0]);
214 dup2 (app->pipefd[1], 3);
215 close (app->pipefd[1]);
217 /* On Linux, tell the bus process to exit if this process goes away */
219 #include <sys/prctl.h>
220 prctl (PR_SET_PDEATHSIG, 15);
225 * unix_read_all_fd_to_string:
227 * Read all data from a file descriptor to a C string buffer.
230 unix_read_all_fd_to_string (int fd,
236 while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
241 max_bytes -= bytes_read;
248 on_bus_exited (GPid pid,
252 A11yBusLauncher *app = data;
254 app->a11y_bus_pid = -1;
255 app->state = A11Y_BUS_STATE_ERROR;
256 if (app->a11y_launch_error_message == NULL)
258 if (WIFEXITED (status))
259 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
260 else if (WIFSIGNALED (status))
261 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
262 else if (WIFSTOPPED (status))
263 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
265 g_main_loop_quit (app->loop);
269 ensure_a11y_bus (A11yBusLauncher *app)
272 char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
274 GError *error = NULL;
275 const char *config_path = NULL;
277 if (app->a11y_bus_pid != 0)
280 if (g_file_test (SYSCONFDIR"/at-spi2/accessibility.conf", G_FILE_TEST_EXISTS))
281 config_path = "--config-file="SYSCONFDIR"/at-spi2/accessibility.conf";
283 config_path = "--config-file="DATADIR"/defaults/at-spi2/accessibility.conf";
285 argv[1] = config_path;
287 if (pipe (app->pipefd) < 0)
288 g_error ("Failed to create pipe: %s", strerror (errno));
290 if (!g_spawn_async (NULL,
293 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
299 app->a11y_bus_pid = -1;
300 app->a11y_launch_error_message = g_strdup (error->message);
301 g_clear_error (&error);
305 close (app->pipefd[1]);
308 g_child_watch_add (pid, on_bus_exited, app);
310 app->state = A11Y_BUS_STATE_READING_ADDRESS;
311 app->a11y_bus_pid = pid;
312 g_debug ("Launched a11y bus, child is %ld", (long) pid);
313 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
315 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
316 kill (app->a11y_bus_pid, SIGTERM);
319 close (app->pipefd[0]);
321 app->state = A11Y_BUS_STATE_RUNNING;
323 /* Trim the trailing newline */
324 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
325 g_debug ("a11y bus address: %s", app->a11y_bus_address);
329 Display *display = XOpenDisplay (NULL);
332 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
333 XChangeProperty (display,
334 XDefaultRootWindow (display),
336 XA_STRING, 8, PropModeReplace,
337 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
339 XCloseDisplay (display);
347 close (app->pipefd[0]);
348 close (app->pipefd[1]);
349 app->state = A11Y_BUS_STATE_ERROR;
355 handle_method_call (GDBusConnection *connection,
357 const gchar *object_path,
358 const gchar *interface_name,
359 const gchar *method_name,
360 GVariant *parameters,
361 GDBusMethodInvocation *invocation,
364 A11yBusLauncher *app = user_data;
366 if (g_strcmp0 (method_name, "GetAddress") == 0)
368 ensure_a11y_bus (app);
369 if (app->a11y_bus_pid > 0)
370 g_dbus_method_invocation_return_value (invocation,
371 g_variant_new ("(s)", app->a11y_bus_address));
373 g_dbus_method_invocation_return_dbus_error (invocation,
374 "org.a11y.Bus.Error",
375 app->a11y_launch_error_message);
380 handle_get_property (GDBusConnection *connection,
382 const gchar *object_path,
383 const gchar *interface_name,
384 const gchar *property_name,
388 A11yBusLauncher *app = user_data;
390 if (g_strcmp0 (property_name, "IsEnabled") == 0)
391 return g_variant_new ("b", app->a11y_enabled);
392 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
393 return g_variant_new ("b", app->screen_reader_enabled);
399 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
400 gboolean notify_gsettings)
402 GVariantBuilder builder;
403 GVariantBuilder invalidated_builder;
405 if (enabled == app->a11y_enabled)
408 app->a11y_enabled = enabled;
410 if (notify_gsettings && app->interface_schema)
412 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
417 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
418 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
419 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
420 g_variant_new_boolean (enabled));
422 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
423 "org.freedesktop.DBus.Properties",
425 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
427 &invalidated_builder),
432 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
433 gboolean notify_gsettings)
435 GVariantBuilder builder;
436 GVariantBuilder invalidated_builder;
438 if (enabled == app->screen_reader_enabled)
441 /* If the screen reader is being enabled, we should enable accessibility
442 * if it isn't enabled already */
444 handle_a11y_enabled_change (app, enabled, notify_gsettings);
446 app->screen_reader_enabled = enabled;
448 if (notify_gsettings && app->a11y_schema)
450 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
455 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
456 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
457 g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
458 g_variant_new_boolean (enabled));
460 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
461 "org.freedesktop.DBus.Properties",
463 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
465 &invalidated_builder),
470 handle_set_property (GDBusConnection *connection,
472 const gchar *object_path,
473 const gchar *interface_name,
474 const gchar *property_name,
479 A11yBusLauncher *app = user_data;
480 const gchar *type = g_variant_get_type_string (value);
483 if (g_strcmp0 (type, "b") != 0)
485 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
486 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
490 enabled = g_variant_get_boolean (value);
492 if (g_strcmp0 (property_name, "IsEnabled") == 0)
494 handle_a11y_enabled_change (app, enabled, TRUE);
497 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
499 handle_screen_reader_enabled_change (app, enabled, TRUE);
504 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
505 "Unknown property '%s'", property_name);
510 static const GDBusInterfaceVTable bus_vtable =
513 NULL, /* handle_get_property, */
514 NULL /* handle_set_property */
517 static const GDBusInterfaceVTable status_vtable =
519 NULL, /* handle_method_call */
525 on_bus_acquired (GDBusConnection *connection,
529 A11yBusLauncher *app = user_data;
531 guint registration_id;
533 if (connection == NULL)
535 g_main_loop_quit (app->loop);
538 app->session_bus = connection;
540 if (app->launch_immediately)
542 ensure_a11y_bus (app);
543 if (app->state == A11Y_BUS_STATE_ERROR)
545 g_main_loop_quit (app->loop);
551 registration_id = g_dbus_connection_register_object (connection,
553 introspection_data->interfaces[0],
558 if (registration_id == 0)
560 g_error ("%s", error->message);
561 g_clear_error (&error);
564 g_dbus_connection_register_object (connection,
566 introspection_data->interfaces[1],
574 on_name_lost (GDBusConnection *connection,
578 A11yBusLauncher *app = user_data;
579 if (app->session_bus == NULL
580 && connection == NULL
581 && app->a11y_launch_error_message == NULL)
582 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
583 g_main_loop_quit (app->loop);
587 on_name_acquired (GDBusConnection *connection,
591 A11yBusLauncher *app = user_data;
593 register_client (app);
596 static int sigterm_pipefd[2];
599 sigterm_handler (int signum)
601 write (sigterm_pipefd[1], "X", 1);
605 on_sigterm_pipe (GIOChannel *channel,
606 GIOCondition condition,
609 A11yBusLauncher *app = data;
611 g_main_loop_quit (app->loop);
617 init_sigterm_handling (A11yBusLauncher *app)
619 GIOChannel *sigterm_channel;
621 if (pipe (sigterm_pipefd) < 0)
622 g_error ("Failed to create pipe: %s", strerror (errno));
623 signal (SIGTERM, sigterm_handler);
625 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
626 g_io_add_watch (sigterm_channel,
627 G_IO_IN | G_IO_ERR | G_IO_HUP,
638 Display *bridge_display;
640 unsigned char *data = NULL;
641 unsigned long nitems;
642 unsigned long leftover;
643 gboolean result = FALSE;
645 bridge_display = XOpenDisplay (NULL);
649 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
650 XGetWindowProperty (bridge_display,
651 XDefaultRootWindow (bridge_display),
653 (long) BUFSIZ, False,
654 (Atom) 31, &actual_type, &actual_format,
655 &nitems, &leftover, &data);
659 GDBusConnection *bus;
660 bus = g_dbus_connection_new_for_address_sync ((const gchar *)data, 0,
665 g_object_unref (bus);
669 XCloseDisplay (bridge_display);
677 get_schema (const gchar *name)
679 const char * const *schemas = NULL;
682 schemas = g_settings_list_schemas ();
683 for (i = 0; schemas[i]; i++)
685 if (!strcmp (schemas[i], name))
686 return g_settings_new (schemas[i]);
693 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
695 gboolean new_val = g_settings_get_boolean (gsettings, key);
696 A11yBusLauncher *app = user_data;
698 if (!strcmp (key, "toolkit-accessibility"))
699 handle_a11y_enabled_change (_global_app, new_val, FALSE);
700 else if (!strcmp (key, "screen-reader-enabled"))
701 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
708 GError *error = NULL;
710 GDBusConnection *session_bus;
712 gboolean a11y_set = FALSE;
713 gboolean screen_reader_set = FALSE;
716 if (already_running ())
719 _global_app = g_slice_new0 (A11yBusLauncher);
720 _global_app->loop = g_main_loop_new (NULL, FALSE);
722 for (i = 1; i < argc; i++)
724 if (!strcmp (argv[i], "--launch-immediately"))
725 _global_app->launch_immediately = TRUE;
726 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 2)
728 else if (sscanf (argv[i], "--screen-reader=%d",
729 &_global_app->screen_reader_enabled) == 2)
730 screen_reader_set = TRUE;
732 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
735 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
736 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
740 _global_app->a11y_enabled = _global_app->interface_schema
741 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
742 : _global_app->launch_immediately;
745 if (!screen_reader_set)
747 _global_app->screen_reader_enabled = _global_app->a11y_schema
748 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
752 if (_global_app->interface_schema)
753 g_signal_connect (_global_app->interface_schema,
754 "changed::toolkit-accessibility",
755 G_CALLBACK (gsettings_key_changed), _global_app);
757 if (_global_app->a11y_schema)
758 g_signal_connect (_global_app->a11y_schema,
759 "changed::screen-reader-enabled",
760 G_CALLBACK (gsettings_key_changed), _global_app);
762 init_sigterm_handling (_global_app);
764 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
765 g_assert (introspection_data != NULL);
767 name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
769 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
776 g_main_loop_run (_global_app->loop);
778 if (_global_app->a11y_bus_pid > 0)
779 kill (_global_app->a11y_bus_pid, SIGTERM);
781 /* Clear the X property if our bus is gone; in the case where e.g.
782 * GDM is launching a login on an X server it was using before,
783 * we don't want early login processes to pick up the stale address.
787 Display *display = XOpenDisplay (NULL);
790 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
791 XDeleteProperty (display,
792 XDefaultRootWindow (display),
796 XCloseDisplay (display);
801 if (_global_app->a11y_launch_error_message)
803 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);