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 GDBusConnection *session_bus;
48 GSettings *desktop_schema;
51 /* -1 == error, 0 == pending, > 0 == running */
53 char *a11y_bus_address;
55 char *a11y_launch_error_message;
58 static A11yBusLauncher *_global_app = NULL;
60 static const gchar introspection_xml[] =
62 " <interface name='org.a11y.Bus'>"
63 " <method name='GetAddress'>"
64 " <arg type='s' name='address' direction='out'/>"
67 "<interface name='org.a11y.Status'>"
68 "<property name='IsEnabled' type='b' access='readwrite'/>"
71 static GDBusNodeInfo *introspection_data = NULL;
74 setup_bus_child (gpointer data)
76 A11yBusLauncher *app = data;
79 close (app->pipefd[0]);
80 dup2 (app->pipefd[1], 3);
81 close (app->pipefd[1]);
83 /* On Linux, tell the bus process to exit if this process goes away */
85 #include <sys/prctl.h>
86 prctl (PR_SET_PDEATHSIG, 15);
91 * unix_read_all_fd_to_string:
93 * Read all data from a file descriptor to a C string buffer.
96 unix_read_all_fd_to_string (int fd,
102 while (max_bytes > 1 && (bytes_read = read (fd, buf, MAX (4096, max_bytes - 1))))
107 max_bytes -= bytes_read;
114 on_bus_exited (GPid pid,
118 A11yBusLauncher *app = data;
120 app->a11y_bus_pid = -1;
121 app->state = A11Y_BUS_STATE_ERROR;
122 if (app->a11y_launch_error_message == NULL)
124 if (WIFEXITED (status))
125 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
126 else if (WIFSIGNALED (status))
127 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
128 else if (WIFSTOPPED (status))
129 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
131 g_main_loop_quit (app->loop);
135 ensure_a11y_bus (A11yBusLauncher *app)
138 char *argv[] = { DBUS_DAEMON, NULL, "--nofork", "--print-address", "3", NULL };
140 GError *error = NULL;
142 if (app->a11y_bus_pid != 0)
145 argv[1] = g_strdup_printf ("--config-file=%s/at-spi2/accessibility.conf", SYSCONFDIR);
147 if (pipe (app->pipefd) < 0)
148 g_error ("Failed to create pipe: %s", strerror (errno));
150 if (!g_spawn_async (NULL,
153 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
159 app->a11y_bus_pid = -1;
160 app->a11y_launch_error_message = g_strdup (error->message);
161 g_clear_error (&error);
165 close (app->pipefd[1]);
168 g_child_watch_add (pid, on_bus_exited, app);
170 app->state = A11Y_BUS_STATE_READING_ADDRESS;
171 app->a11y_bus_pid = pid;
172 g_debug ("Launched a11y bus, child is %ld", (long) pid);
173 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
175 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
176 kill (app->a11y_bus_pid, SIGTERM);
179 close (app->pipefd[0]);
181 app->state = A11Y_BUS_STATE_RUNNING;
183 /* Trim the trailing newline */
184 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
185 g_debug ("a11y bus address: %s", app->a11y_bus_address);
188 Display *display = XOpenDisplay (NULL);
191 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
192 XChangeProperty (display,
193 XDefaultRootWindow (display),
195 XA_STRING, 8, PropModeReplace,
196 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
198 XCloseDisplay (display);
205 close (app->pipefd[0]);
206 close (app->pipefd[1]);
207 app->state = A11Y_BUS_STATE_ERROR;
213 handle_method_call (GDBusConnection *connection,
215 const gchar *object_path,
216 const gchar *interface_name,
217 const gchar *method_name,
218 GVariant *parameters,
219 GDBusMethodInvocation *invocation,
222 A11yBusLauncher *app = user_data;
224 if (g_strcmp0 (method_name, "GetAddress") == 0)
226 ensure_a11y_bus (app);
227 if (app->a11y_bus_pid > 0)
228 g_dbus_method_invocation_return_value (invocation,
229 g_variant_new ("(s)", app->a11y_bus_address));
231 g_dbus_method_invocation_return_dbus_error (invocation,
232 "org.a11y.Bus.Error",
233 app->a11y_launch_error_message);
238 handle_get_property (GDBusConnection *connection,
240 const gchar *object_path,
241 const gchar *interface_name,
242 const gchar *property_name,
246 A11yBusLauncher *app = user_data;
248 if (g_strcmp0 (property_name, "IsEnabled") == 0)
250 return g_variant_new ("b", app->a11y_enabled);
257 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
258 gboolean notify_gsettings)
260 GVariantBuilder *builder;
261 GVariantBuilder *invalidated_builder;
263 if (enabled == app->a11y_enabled)
266 app->a11y_enabled = enabled;
268 if (notify_gsettings && app->desktop_schema)
269 g_settings_set_boolean (app->desktop_schema, "toolkit-accessibility",
272 builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
273 invalidated_builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
274 g_variant_builder_add (builder, "{sv}", "IsEnabled",
275 g_variant_new_boolean (enabled));
277 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
278 "org.freedesktop.DBus", "PropertiesChanged",
279 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
281 invalidated_builder),
286 handle_set_property (GDBusConnection *connection,
288 const gchar *object_path,
289 const gchar *interface_name,
290 const gchar *property_name,
295 A11yBusLauncher *app = user_data;
297 if (g_strcmp0 (property_name, "IsEnabled") == 0)
299 const gchar *type = g_variant_get_type_string (value);
301 if (g_strcmp0 (type, "b") != 0)
303 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
304 "org.a11y.Status.IsEnabled expects a boolean but got %s", type);
307 enabled = g_variant_get_boolean (value);
308 handle_a11y_enabled_change (app, enabled, TRUE);
313 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
314 "Unknown property '%s'", property_name);
319 static const GDBusInterfaceVTable bus_vtable =
322 NULL, /* handle_get_property, */
323 NULL /* handle_set_property */
326 static const GDBusInterfaceVTable status_vtable =
328 NULL, /* handle_method_call */
334 on_bus_acquired (GDBusConnection *connection,
338 A11yBusLauncher *app = user_data;
340 guint registration_id;
342 if (connection == NULL)
344 g_main_loop_quit (app->loop);
347 app->session_bus = connection;
349 if (app->launch_immediately)
351 ensure_a11y_bus (app);
352 if (app->state == A11Y_BUS_STATE_ERROR)
354 g_main_loop_quit (app->loop);
360 registration_id = g_dbus_connection_register_object (connection,
362 introspection_data->interfaces[0],
367 if (registration_id == 0)
368 g_error ("%s", error->message);
370 g_dbus_connection_register_object (connection,
372 introspection_data->interfaces[1],
380 on_name_lost (GDBusConnection *connection,
384 A11yBusLauncher *app = user_data;
385 if (app->session_bus == NULL
386 && connection == NULL
387 && app->a11y_launch_error_message == NULL)
388 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
389 g_main_loop_quit (app->loop);
393 on_name_acquired (GDBusConnection *connection,
397 A11yBusLauncher *app = user_data;
401 static int sigterm_pipefd[2];
404 sigterm_handler (int signum)
406 write (sigterm_pipefd[1], "X", 1);
410 on_sigterm_pipe (GIOChannel *channel,
411 GIOCondition condition,
414 A11yBusLauncher *app = data;
416 g_main_loop_quit (app->loop);
422 init_sigterm_handling (A11yBusLauncher *app)
424 GIOChannel *sigterm_channel;
426 if (pipe (sigterm_pipefd) < 0)
427 g_error ("Failed to create pipe: %s", strerror (errno));
428 signal (SIGTERM, sigterm_handler);
430 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
431 g_io_add_watch (sigterm_channel,
432 G_IO_IN | G_IO_ERR | G_IO_HUP,
442 Display *bridge_display;
444 unsigned char *data = NULL;
445 unsigned long nitems;
446 unsigned long leftover;
447 gboolean result = FALSE;
449 bridge_display = XOpenDisplay (NULL);
453 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
454 XGetWindowProperty (bridge_display,
455 XDefaultRootWindow (bridge_display),
457 (long) BUFSIZ, False,
458 (Atom) 31, &actual_type, &actual_format,
459 &nitems, &leftover, &data);
463 GDBusConnection *bus;
464 GError *error = NULL;
465 const gchar *old_session = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
466 /* TODO: Is there a better way to connect? This is really hacky */
467 g_setenv ("DBUS_SESSION_BUS_ADDRESS", data, TRUE);
468 bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
469 g_setenv ("DBUS_SESSION_BUS_ADDRESS", old_session, TRUE);
473 g_object_unref (bus);
477 XCloseDisplay (bridge_display);
482 get_desktop_schema ()
484 const char * const *schemas = NULL;
487 schemas = g_settings_list_schemas ();
488 for (i = 0; schemas[i]; i++)
490 if (!strcmp (schemas[i], "org.gnome.desktop.interface"))
491 return g_settings_new (schemas[i]);
498 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
500 gboolean new_val = g_settings_get_boolean (gsettings, key);
501 A11yBusLauncher *app = user_data;
503 if (strcmp (key, "toolkit-accessibility") != 0)
506 handle_a11y_enabled_change (_global_app, new_val, FALSE);
513 GError *error = NULL;
515 GDBusConnection *session_bus;
517 gboolean a11y_set = FALSE;
522 if (already_running ())
525 _global_app = g_slice_new0 (A11yBusLauncher);
526 _global_app->loop = g_main_loop_new (NULL, FALSE);
528 for (i = 1; i < argc; i++)
530 if (!strcmp (argv[i], "--launch-immediately"))
531 _global_app->launch_immediately = TRUE;
532 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 2)
535 g_error ("usage: %s [--launch-immediately] [--a11y=0|1]", argv[0]);
538 _global_app->desktop_schema = get_desktop_schema ();
542 _global_app->a11y_enabled = _global_app->desktop_schema
543 ? g_settings_get_boolean (_global_app->desktop_schema, "toolkit-accessibility")
544 : _global_app->launch_immediately;
547 if (_global_app->desktop_schema)
548 g_signal_connect (_global_app->desktop_schema,
549 "changed::toolkit-accessibility",
550 G_CALLBACK (gsettings_key_changed), _global_app);
552 init_sigterm_handling (_global_app);
554 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
555 g_assert (introspection_data != NULL);
557 name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
559 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
566 g_main_loop_run (_global_app->loop);
568 if (_global_app->a11y_bus_pid > 0)
569 kill (_global_app->a11y_bus_pid, SIGTERM);
571 /* Clear the X property if our bus is gone; in the case where e.g.
572 * GDM is launching a login on an X server it was using before,
573 * we don't want early login processes to pick up the stale address.
576 Display *display = XOpenDisplay (NULL);
579 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
580 XDeleteProperty (display,
581 XDefaultRootWindow (display),
585 XCloseDisplay (display);
589 if (_global_app->a11y_launch_error_message)
591 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);