1 /* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 *
3 * at-spi-bus-launcher: Manage the a11y bus as a child process
4 *
5 * Copyright 2011-2018 Red Hat, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
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 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include "config.h"
24
25 #include <unistd.h>
26 #include <string.h>
27 #include <signal.h>
28 #ifdef __linux__
29 #include <sys/prctl.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #endif
33 #include <sys/wait.h>
34 #include <errno.h>
35 #include <stdio.h>
36
37 #include <gio/gio.h>
38 #ifdef HAVE_X11
39 #include <X11/Xlib.h>
40 #include <X11/Xatom.h>
41 #endif
42
43 typedef enum {
44 A11Y_BUS_STATE_IDLE = 0,
45 A11Y_BUS_STATE_READING_ADDRESS,
46 A11Y_BUS_STATE_RUNNING,
47 A11Y_BUS_STATE_ERROR
48 } A11yBusState;
49
50 typedef struct {
51 GMainLoop *loop;
52 gboolean launch_immediately;
53 gboolean a11y_enabled;
54 gboolean screen_reader_enabled;
55 GDBusConnection *session_bus;
56 GSettings *a11y_schema;
57 GSettings *interface_schema;
58 int name_owner_id;
59
60 GDBusProxy *client_proxy;
61
62 A11yBusState state;
63 /* -1 == error, 0 == pending, > 0 == running */
64 int a11y_bus_pid;
65 char *a11y_bus_address;
66 #ifdef HAVE_X11
67 gboolean x11_prop_set;
68 #endif
69 int pipefd[2];
70 int listenfd;
71 char *a11y_launch_error_message;
72 GDBusProxy *sm_proxy;
73 } A11yBusLauncher;
74
75 static A11yBusLauncher *_global_app = NULL;
76
77 static const gchar introspection_xml[] =
78 "<node>"
79 " <interface name='org.a11y.Bus'>"
80 " <method name='GetAddress'>"
81 " <arg type='s' name='address' direction='out'/>"
82 " </method>"
83 " </interface>"
84 "<interface name='org.a11y.Status'>"
85 "<property name='IsEnabled' type='b' access='readwrite'/>"
86 "<property name='ScreenReaderEnabled' type='b' access='readwrite'/>"
87 "</interface>"
88 "</node>";
89 static GDBusNodeInfo *introspection_data = NULL;
90
91 static void
respond_to_end_session(GDBusProxy * proxy)92 respond_to_end_session (GDBusProxy *proxy)
93 {
94 GVariant *parameters;
95
96 parameters = g_variant_new ("(bs)", TRUE, "");
97
98 g_dbus_proxy_call (proxy,
99 "EndSessionResponse", parameters,
100 G_DBUS_CALL_FLAGS_NONE,
101 -1, NULL, NULL, NULL);
102 }
103
104 static void
g_signal_cb(GDBusProxy * proxy,gchar * sender_name,gchar * signal_name,GVariant * parameters,gpointer user_data)105 g_signal_cb (GDBusProxy *proxy,
106 gchar *sender_name,
107 gchar *signal_name,
108 GVariant *parameters,
109 gpointer user_data)
110 {
111 A11yBusLauncher *app = user_data;
112
113 if (g_strcmp0 (signal_name, "QueryEndSession") == 0)
114 respond_to_end_session (proxy);
115 else if (g_strcmp0 (signal_name, "EndSession") == 0)
116 respond_to_end_session (proxy);
117 else if (g_strcmp0 (signal_name, "Stop") == 0)
118 g_main_loop_quit (app->loop);
119 }
120
121 static void
client_proxy_ready_cb(GObject * source_object,GAsyncResult * res,gpointer user_data)122 client_proxy_ready_cb (GObject *source_object,
123 GAsyncResult *res,
124 gpointer user_data)
125 {
126 A11yBusLauncher *app = user_data;
127 GError *error = NULL;
128
129 app->client_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
130
131 if (error != NULL)
132 {
133 g_warning ("Failed to get a client proxy: %s", error->message);
134 g_error_free (error);
135
136 return;
137 }
138
139 g_signal_connect (app->client_proxy, "g-signal",
140 G_CALLBACK (g_signal_cb), app);
141 }
142
143 static void
client_registered(GObject * source,GAsyncResult * result,gpointer user_data)144 client_registered (GObject *source,
145 GAsyncResult *result,
146 gpointer user_data)
147 {
148 A11yBusLauncher *app = user_data;
149 GError *error = NULL;
150 GVariant *variant;
151 gchar *object_path;
152 GDBusProxyFlags flags;
153
154 variant = g_dbus_proxy_call_finish (app->sm_proxy, result, &error);
155 if (!variant)
156 {
157 if (error != NULL)
158 {
159 g_warning ("Failed to register client: %s", error->message);
160 g_error_free (error);
161 }
162 }
163 else
164 {
165 g_variant_get (variant, "(o)", &object_path);
166 g_variant_unref (variant);
167
168 flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
169 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, flags, NULL,
170 "org.gnome.SessionManager", object_path,
171 "org.gnome.SessionManager.ClientPrivate",
172 NULL, client_proxy_ready_cb, app);
173
174 g_free (object_path);
175 }
176 g_clear_object (&app->sm_proxy);
177 }
178
179 static void
register_client(A11yBusLauncher * app)180 register_client (A11yBusLauncher *app)
181 {
182 GDBusProxyFlags flags;
183 GError *error;
184 const gchar *app_id;
185 const gchar *autostart_id;
186 gchar *client_startup_id;
187 GVariant *parameters;
188
189 flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
190 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS;
191
192 error = NULL;
193 app->sm_proxy = g_dbus_proxy_new_sync (app->session_bus, flags, NULL,
194 "org.gnome.SessionManager",
195 "/org/gnome/SessionManager",
196 "org.gnome.SessionManager",
197 NULL, &error);
198
199 if (error != NULL)
200 {
201 g_warning ("Failed to get session manager proxy: %s", error->message);
202 g_error_free (error);
203
204 return;
205 }
206
207 app_id = "at-spi-bus-launcher";
208 autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
209
210 if (autostart_id != NULL)
211 {
212 client_startup_id = g_strdup (autostart_id);
213 g_unsetenv ("DESKTOP_AUTOSTART_ID");
214 }
215 else
216 {
217 client_startup_id = g_strdup ("");
218 }
219
220 parameters = g_variant_new ("(ss)", app_id, client_startup_id);
221 g_free (client_startup_id);
222
223 error = NULL;
224 g_dbus_proxy_call (app->sm_proxy,
225 "RegisterClient", parameters,
226 G_DBUS_CALL_FLAGS_NONE,
227 G_MAXINT, NULL, client_registered, app);
228
229 }
230
231 static void
name_appeared_handler(GDBusConnection * connection,const gchar * name,const gchar * name_owner,gpointer user_data)232 name_appeared_handler (GDBusConnection *connection,
233 const gchar *name,
234 const gchar *name_owner,
235 gpointer user_data)
236 {
237 A11yBusLauncher *app = user_data;
238
239 register_client (app);
240 }
241
242 /**
243 * unix_read_all_fd_to_string:
244 *
245 * Read all data from a file descriptor to a C string buffer.
246 */
247 static gboolean
unix_read_all_fd_to_string(int fd,char * buf,ssize_t max_bytes)248 unix_read_all_fd_to_string (int fd,
249 char *buf,
250 ssize_t max_bytes)
251 {
252 ssize_t bytes_read;
253
254 while (max_bytes > 1 && (bytes_read = read (fd, buf, MIN (4096, max_bytes - 1))))
255 {
256 if (bytes_read < 0)
257 return FALSE;
258 buf += bytes_read;
259 max_bytes -= bytes_read;
260 }
261 *buf = '\0';
262 return TRUE;
263 }
264
265 static void
on_bus_exited(GPid pid,gint status,gpointer data)266 on_bus_exited (GPid pid,
267 gint status,
268 gpointer data)
269 {
270 A11yBusLauncher *app = data;
271
272 app->a11y_bus_pid = -1;
273 app->state = A11Y_BUS_STATE_ERROR;
274 if (app->a11y_launch_error_message == NULL)
275 {
276 if (WIFEXITED (status))
277 app->a11y_launch_error_message = g_strdup_printf ("Bus exited with code %d", WEXITSTATUS (status));
278 else if (WIFSIGNALED (status))
279 app->a11y_launch_error_message = g_strdup_printf ("Bus killed by signal %d", WTERMSIG (status));
280 else if (WIFSTOPPED (status))
281 app->a11y_launch_error_message = g_strdup_printf ("Bus stopped by signal %d", WSTOPSIG (status));
282 }
283 g_main_loop_quit (app->loop);
284 }
285
286 #ifdef DBUS_DAEMON
287 static void
setup_bus_child_daemon(gpointer data)288 setup_bus_child_daemon (gpointer data)
289 {
290 A11yBusLauncher *app = data;
291 (void) app;
292
293 close (app->pipefd[0]);
294 dup2 (app->pipefd[1], 3);
295 close (app->pipefd[1]);
296
297 /* On Linux, tell the bus process to exit if this process goes away */
298 #ifdef __linux__
299 prctl (PR_SET_PDEATHSIG, 15);
300 #endif
301 }
302
303 static gboolean
ensure_a11y_bus_daemon(A11yBusLauncher * app,char * config_path)304 ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
305 {
306 char *argv[] = { DBUS_DAEMON, config_path, "--nofork", "--print-address", "3", NULL };
307 GPid pid;
308 char addr_buf[2048];
309 GError *error = NULL;
310
311 if (pipe (app->pipefd) < 0)
312 g_error ("Failed to create pipe: %s", strerror (errno));
313
314 g_clear_pointer (&app->a11y_launch_error_message, g_free);
315
316 if (!g_spawn_async (NULL,
317 argv,
318 NULL,
319 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
320 setup_bus_child_daemon,
321 app,
322 &pid,
323 &error))
324 {
325 app->a11y_bus_pid = -1;
326 app->a11y_launch_error_message = g_strdup (error->message);
327 g_clear_error (&error);
328 goto error;
329 }
330
331 close (app->pipefd[1]);
332 app->pipefd[1] = -1;
333
334 g_child_watch_add (pid, on_bus_exited, app);
335
336 app->state = A11Y_BUS_STATE_READING_ADDRESS;
337 app->a11y_bus_pid = pid;
338 g_debug ("Launched a11y bus, child is %ld", (long) pid);
339 if (!unix_read_all_fd_to_string (app->pipefd[0], addr_buf, sizeof (addr_buf)))
340 {
341 app->a11y_launch_error_message = g_strdup_printf ("Failed to read address: %s", strerror (errno));
342 kill (app->a11y_bus_pid, SIGTERM);
343 app->a11y_bus_pid = -1;
344 goto error;
345 }
346 close (app->pipefd[0]);
347 app->pipefd[0] = -1;
348 app->state = A11Y_BUS_STATE_RUNNING;
349
350 /* Trim the trailing newline */
351 app->a11y_bus_address = g_strchomp (g_strdup (addr_buf));
352 g_debug ("a11y bus address: %s", app->a11y_bus_address);
353
354 return TRUE;
355
356 error:
357 close (app->pipefd[0]);
358 close (app->pipefd[1]);
359 app->state = A11Y_BUS_STATE_ERROR;
360
361 return FALSE;
362 }
363 #else
364 static gboolean
ensure_a11y_bus_daemon(A11yBusLauncher * app,char * config_path)365 ensure_a11y_bus_daemon (A11yBusLauncher *app, char *config_path)
366 {
367 return FALSE;
368 }
369 #endif
370
371 #ifdef DBUS_BROKER
372 static void
setup_bus_child_broker(gpointer data)373 setup_bus_child_broker (gpointer data)
374 {
375 A11yBusLauncher *app = data;
376 gchar *pid_str;
377 (void) app;
378
379 dup2 (app->listenfd, 3);
380 close (app->listenfd);
381 g_setenv("LISTEN_FDS", "1", TRUE);
382
383 pid_str = g_strdup_printf("%u", getpid());
384 g_setenv("LISTEN_PID", pid_str, TRUE);
385 g_free(pid_str);
386
387 /* Tell the bus process to exit if this process goes away */
388 prctl (PR_SET_PDEATHSIG, SIGTERM);
389 }
390
391 static gboolean
ensure_a11y_bus_broker(A11yBusLauncher * app,char * config_path)392 ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
393 {
394 char *argv[] = { DBUS_BROKER, config_path, "--scope", "user", NULL };
395 struct sockaddr_un addr = { .sun_family = AF_UNIX };
396 socklen_t addr_len = sizeof(addr);
397 GPid pid;
398 GError *error = NULL;
399
400 if ((app->listenfd = socket (PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0)
401 g_error ("Failed to create listening socket: %s", strerror (errno));
402
403 if (bind (app->listenfd, (struct sockaddr *)&addr, sizeof(sa_family_t)) < 0)
404 g_error ("Failed to bind listening socket: %s", strerror (errno));
405
406 if (getsockname (app->listenfd, (struct sockaddr *)&addr, &addr_len) < 0)
407 g_error ("Failed to get socket name for listening socket: %s", strerror(errno));
408
409 if (listen (app->listenfd, 1024) < 0)
410 g_error ("Failed to listen on socket: %s", strerror(errno));
411
412 g_clear_pointer (&app->a11y_launch_error_message, g_free);
413
414 if (!g_spawn_async (NULL,
415 argv,
416 NULL,
417 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
418 setup_bus_child_broker,
419 app,
420 &pid,
421 &error))
422 {
423 app->a11y_bus_pid = -1;
424 app->a11y_launch_error_message = g_strdup (error->message);
425 g_clear_error (&error);
426 goto error;
427 }
428
429 close (app->listenfd);
430 app->listenfd = -1;
431
432 g_child_watch_add (pid, on_bus_exited, app);
433 app->a11y_bus_pid = pid;
434 g_debug ("Launched a11y bus, child is %ld", (long) pid);
435 app->state = A11Y_BUS_STATE_RUNNING;
436
437 app->a11y_bus_address = g_strconcat("unix:abstract=", addr.sun_path + 1, NULL);
438 g_debug ("a11y bus address: %s", app->a11y_bus_address);
439
440 return TRUE;
441
442 error:
443 close (app->listenfd);
444 app->state = A11Y_BUS_STATE_ERROR;
445
446 return FALSE;
447 }
448 #else
449 static gboolean
ensure_a11y_bus_broker(A11yBusLauncher * app,char * config_path)450 ensure_a11y_bus_broker (A11yBusLauncher *app, char *config_path)
451 {
452 return FALSE;
453 }
454 #endif
455
456 static gboolean
ensure_a11y_bus(A11yBusLauncher * app)457 ensure_a11y_bus (A11yBusLauncher *app)
458 {
459 char *config_path = NULL;
460 gboolean success = FALSE;
461
462 if (app->a11y_bus_pid != 0)
463 return FALSE;
464
465 if (g_file_test (SYSCONFDIR"/at-spi2/accessibility.conf", G_FILE_TEST_EXISTS))
466 config_path = "--config-file="SYSCONFDIR"/at-spi2/accessibility.conf";
467 else
468 config_path = "--config-file="DATADIR"/defaults/at-spi2/accessibility.conf";
469
470 #ifdef WANT_DBUS_BROKER
471 success = ensure_a11y_bus_broker (app, config_path);
472 if (!success)
473 {
474 if (!ensure_a11y_bus_daemon (app, config_path))
475 return FALSE;
476 }
477 #else
478 success = ensure_a11y_bus_daemon (app, config_path);
479 if (!success)
480 {
481 if (!ensure_a11y_bus_broker (app, config_path))
482 return FALSE;
483 }
484 #endif
485
486 #ifdef HAVE_X11
487 if (g_getenv ("DISPLAY") != NULL && g_getenv ("WAYLAND_DISPLAY") == NULL)
488 {
489 Display *display = XOpenDisplay (NULL);
490 if (display)
491 {
492 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
493 XChangeProperty (display,
494 XDefaultRootWindow (display),
495 bus_address_atom,
496 XA_STRING, 8, PropModeReplace,
497 (guchar *) app->a11y_bus_address, strlen (app->a11y_bus_address));
498 XFlush (display);
499 XCloseDisplay (display);
500 app->x11_prop_set = TRUE;
501 }
502 }
503 #endif
504
505 return TRUE;
506 }
507
508 static void
handle_method_call(GDBusConnection * connection,const gchar * sender,const gchar * object_path,const gchar * interface_name,const gchar * method_name,GVariant * parameters,GDBusMethodInvocation * invocation,gpointer user_data)509 handle_method_call (GDBusConnection *connection,
510 const gchar *sender,
511 const gchar *object_path,
512 const gchar *interface_name,
513 const gchar *method_name,
514 GVariant *parameters,
515 GDBusMethodInvocation *invocation,
516 gpointer user_data)
517 {
518 A11yBusLauncher *app = user_data;
519
520 if (g_strcmp0 (method_name, "GetAddress") == 0)
521 {
522 ensure_a11y_bus (app);
523 if (app->a11y_bus_pid > 0)
524 g_dbus_method_invocation_return_value (invocation,
525 g_variant_new ("(s)", app->a11y_bus_address));
526 else
527 g_dbus_method_invocation_return_dbus_error (invocation,
528 "org.a11y.Bus.Error",
529 app->a11y_launch_error_message);
530 }
531 }
532
533 static GVariant *
handle_get_property(GDBusConnection * connection,const gchar * sender,const gchar * object_path,const gchar * interface_name,const gchar * property_name,GError ** error,gpointer user_data)534 handle_get_property (GDBusConnection *connection,
535 const gchar *sender,
536 const gchar *object_path,
537 const gchar *interface_name,
538 const gchar *property_name,
539 GError **error,
540 gpointer user_data)
541 {
542 A11yBusLauncher *app = user_data;
543
544 if (g_strcmp0 (property_name, "IsEnabled") == 0)
545 return g_variant_new ("b", app->a11y_enabled);
546 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
547 return g_variant_new ("b", app->screen_reader_enabled);
548 else
549 return NULL;
550 }
551
552 static void
handle_a11y_enabled_change(A11yBusLauncher * app,gboolean enabled,gboolean notify_gsettings)553 handle_a11y_enabled_change (A11yBusLauncher *app, gboolean enabled,
554 gboolean notify_gsettings)
555 {
556 GVariantBuilder builder;
557 GVariantBuilder invalidated_builder;
558
559 if (enabled == app->a11y_enabled)
560 return;
561
562 app->a11y_enabled = enabled;
563
564 if (notify_gsettings && app->interface_schema)
565 {
566 g_settings_set_boolean (app->interface_schema, "toolkit-accessibility",
567 enabled);
568 g_settings_sync ();
569 }
570
571 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
572 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
573 g_variant_builder_add (&builder, "{sv}", "IsEnabled",
574 g_variant_new_boolean (enabled));
575
576 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
577 "org.freedesktop.DBus.Properties",
578 "PropertiesChanged",
579 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
580 &builder,
581 &invalidated_builder),
582 NULL);
583
584 g_variant_builder_clear (&builder);
585 g_variant_builder_clear (&invalidated_builder);
586 }
587
588 static void
handle_screen_reader_enabled_change(A11yBusLauncher * app,gboolean enabled,gboolean notify_gsettings)589 handle_screen_reader_enabled_change (A11yBusLauncher *app, gboolean enabled,
590 gboolean notify_gsettings)
591 {
592 GVariantBuilder builder;
593 GVariantBuilder invalidated_builder;
594
595 if (enabled == app->screen_reader_enabled)
596 return;
597
598 /* If the screen reader is being enabled, we should enable accessibility
599 * if it isn't enabled already */
600 if (enabled)
601 handle_a11y_enabled_change (app, enabled, notify_gsettings);
602
603 app->screen_reader_enabled = enabled;
604
605 if (notify_gsettings && app->a11y_schema)
606 {
607 g_settings_set_boolean (app->a11y_schema, "screen-reader-enabled",
608 enabled);
609 g_settings_sync ();
610 }
611
612 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
613 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
614 g_variant_builder_add (&builder, "{sv}", "ScreenReaderEnabled",
615 g_variant_new_boolean (enabled));
616
617 g_dbus_connection_emit_signal (app->session_bus, NULL, "/org/a11y/bus",
618 "org.freedesktop.DBus.Properties",
619 "PropertiesChanged",
620 g_variant_new ("(sa{sv}as)", "org.a11y.Status",
621 &builder,
622 &invalidated_builder),
623 NULL);
624
625 g_variant_builder_clear (&builder);
626 g_variant_builder_clear (&invalidated_builder);
627 }
628
629 static gboolean
handle_set_property(GDBusConnection * connection,const gchar * sender,const gchar * object_path,const gchar * interface_name,const gchar * property_name,GVariant * value,GError ** error,gpointer user_data)630 handle_set_property (GDBusConnection *connection,
631 const gchar *sender,
632 const gchar *object_path,
633 const gchar *interface_name,
634 const gchar *property_name,
635 GVariant *value,
636 GError **error,
637 gpointer user_data)
638 {
639 A11yBusLauncher *app = user_data;
640 const gchar *type = g_variant_get_type_string (value);
641 gboolean enabled;
642
643 if (g_strcmp0 (type, "b") != 0)
644 {
645 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
646 "org.a11y.Status.%s expects a boolean but got %s", property_name, type);
647 return FALSE;
648 }
649
650 enabled = g_variant_get_boolean (value);
651
652 if (g_strcmp0 (property_name, "IsEnabled") == 0)
653 {
654 handle_a11y_enabled_change (app, enabled, TRUE);
655 return TRUE;
656 }
657 else if (g_strcmp0 (property_name, "ScreenReaderEnabled") == 0)
658 {
659 handle_screen_reader_enabled_change (app, enabled, TRUE);
660 return TRUE;
661 }
662 else
663 {
664 g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
665 "Unknown property '%s'", property_name);
666 return FALSE;
667 }
668 }
669
670 static const GDBusInterfaceVTable bus_vtable =
671 {
672 handle_method_call,
673 NULL, /* handle_get_property, */
674 NULL /* handle_set_property */
675 };
676
677 static const GDBusInterfaceVTable status_vtable =
678 {
679 NULL, /* handle_method_call */
680 handle_get_property,
681 handle_set_property
682 };
683
684 static void
on_bus_acquired(GDBusConnection * connection,const gchar * name,gpointer user_data)685 on_bus_acquired (GDBusConnection *connection,
686 const gchar *name,
687 gpointer user_data)
688 {
689 A11yBusLauncher *app = user_data;
690 GError *error;
691 guint registration_id;
692
693 if (connection == NULL)
694 {
695 g_main_loop_quit (app->loop);
696 return;
697 }
698 app->session_bus = connection;
699
700 error = NULL;
701 registration_id = g_dbus_connection_register_object (connection,
702 "/org/a11y/bus",
703 introspection_data->interfaces[0],
704 &bus_vtable,
705 _global_app,
706 NULL,
707 &error);
708 if (registration_id == 0)
709 {
710 g_error ("%s", error->message);
711 g_clear_error (&error);
712 }
713
714 g_dbus_connection_register_object (connection,
715 "/org/a11y/bus",
716 introspection_data->interfaces[1],
717 &status_vtable,
718 _global_app,
719 NULL,
720 NULL);
721 }
722
723 static void
on_name_lost(GDBusConnection * connection,const gchar * name,gpointer user_data)724 on_name_lost (GDBusConnection *connection,
725 const gchar *name,
726 gpointer user_data)
727 {
728 A11yBusLauncher *app = user_data;
729 if (app->session_bus == NULL
730 && connection == NULL
731 && app->a11y_launch_error_message == NULL)
732 app->a11y_launch_error_message = g_strdup ("Failed to connect to session bus");
733 g_main_loop_quit (app->loop);
734 }
735
736 static void
on_name_acquired(GDBusConnection * connection,const gchar * name,gpointer user_data)737 on_name_acquired (GDBusConnection *connection,
738 const gchar *name,
739 gpointer user_data)
740 {
741 A11yBusLauncher *app = user_data;
742
743 if (app->launch_immediately)
744 {
745 ensure_a11y_bus (app);
746 if (app->state == A11Y_BUS_STATE_ERROR)
747 {
748 g_main_loop_quit (app->loop);
749 return;
750 }
751 }
752
753 g_bus_watch_name (G_BUS_TYPE_SESSION,
754 "org.gnome.SessionManager",
755 G_BUS_NAME_WATCHER_FLAGS_NONE,
756 name_appeared_handler, NULL,
757 user_data, NULL);
758 }
759
760 static int sigterm_pipefd[2];
761
762 static void
sigterm_handler(int signum)763 sigterm_handler (int signum)
764 {
765 write (sigterm_pipefd[1], "X", 1);
766 }
767
768 static gboolean
on_sigterm_pipe(GIOChannel * channel,GIOCondition condition,gpointer data)769 on_sigterm_pipe (GIOChannel *channel,
770 GIOCondition condition,
771 gpointer data)
772 {
773 A11yBusLauncher *app = data;
774
775 g_main_loop_quit (app->loop);
776
777 return FALSE;
778 }
779
780 static void
init_sigterm_handling(A11yBusLauncher * app)781 init_sigterm_handling (A11yBusLauncher *app)
782 {
783 GIOChannel *sigterm_channel;
784
785 if (pipe (sigterm_pipefd) < 0)
786 g_error ("Failed to create pipe: %s", strerror (errno));
787 signal (SIGTERM, sigterm_handler);
788
789 sigterm_channel = g_io_channel_unix_new (sigterm_pipefd[0]);
790 g_io_add_watch (sigterm_channel,
791 G_IO_IN | G_IO_ERR | G_IO_HUP,
792 on_sigterm_pipe,
793 app);
794 }
795
796 static GSettings *
get_schema(const gchar * name)797 get_schema (const gchar *name)
798 {
799 #if GLIB_CHECK_VERSION (2, 32, 0)
800 GSettingsSchemaSource *source = g_settings_schema_source_get_default ();
801 GSettingsSchema *schema = g_settings_schema_source_lookup (source, name, FALSE);
802
803 if (schema == NULL)
804 return NULL;
805
806 return g_settings_new_full (schema, NULL, NULL);
807 #else
808 const char * const *schemas = NULL;
809 gint i;
810
811 schemas = g_settings_list_schemas ();
812 for (i = 0; schemas[i]; i++)
813 {
814 if (!strcmp (schemas[i], name))
815 return g_settings_new (schemas[i]);
816 }
817
818 return NULL;
819 #endif
820 }
821
822 static void
gsettings_key_changed(GSettings * gsettings,const gchar * key,void * user_data)823 gsettings_key_changed (GSettings *gsettings, const gchar *key, void *user_data)
824 {
825 gboolean new_val = g_settings_get_boolean (gsettings, key);
826
827 if (!strcmp (key, "toolkit-accessibility"))
828 handle_a11y_enabled_change (_global_app, new_val, FALSE);
829 else if (!strcmp (key, "screen-reader-enabled"))
830 handle_screen_reader_enabled_change (_global_app, new_val, FALSE);
831 }
832
833 int
main(int argc,char ** argv)834 main (int argc,
835 char **argv)
836 {
837 gboolean a11y_set = FALSE;
838 gboolean screen_reader_set = FALSE;
839 gint i;
840
841 _global_app = g_slice_new0 (A11yBusLauncher);
842 _global_app->loop = g_main_loop_new (NULL, FALSE);
843
844 for (i = 1; i < argc; i++)
845 {
846 if (!strcmp (argv[i], "--launch-immediately"))
847 _global_app->launch_immediately = TRUE;
848 else if (sscanf (argv[i], "--a11y=%d", &_global_app->a11y_enabled) == 1)
849 a11y_set = TRUE;
850 else if (sscanf (argv[i], "--screen-reader=%d",
851 &_global_app->screen_reader_enabled) == 1)
852 screen_reader_set = TRUE;
853 else
854 g_error ("usage: %s [--launch-immediately] [--a11y=0|1] [--screen-reader=0|1]", argv[0]);
855 }
856
857 _global_app->interface_schema = get_schema ("org.gnome.desktop.interface");
858 _global_app->a11y_schema = get_schema ("org.gnome.desktop.a11y.applications");
859
860 if (!a11y_set)
861 {
862 _global_app->a11y_enabled = _global_app->interface_schema
863 ? g_settings_get_boolean (_global_app->interface_schema, "toolkit-accessibility")
864 : _global_app->launch_immediately;
865 }
866
867 if (!screen_reader_set)
868 {
869 _global_app->screen_reader_enabled = _global_app->a11y_schema
870 ? g_settings_get_boolean (_global_app->a11y_schema, "screen-reader-enabled")
871 : FALSE;
872 }
873
874 if (_global_app->interface_schema)
875 g_signal_connect (_global_app->interface_schema,
876 "changed::toolkit-accessibility",
877 G_CALLBACK (gsettings_key_changed), _global_app);
878
879 if (_global_app->a11y_schema)
880 g_signal_connect (_global_app->a11y_schema,
881 "changed::screen-reader-enabled",
882 G_CALLBACK (gsettings_key_changed), _global_app);
883
884 init_sigterm_handling (_global_app);
885
886 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
887 g_assert (introspection_data != NULL);
888
889 _global_app->name_owner_id =
890 g_bus_own_name (G_BUS_TYPE_SESSION,
891 "org.a11y.Bus",
892 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
893 on_bus_acquired,
894 on_name_acquired,
895 on_name_lost,
896 _global_app,
897 NULL);
898
899 g_main_loop_run (_global_app->loop);
900
901 if (_global_app->a11y_bus_pid > 0)
902 kill (_global_app->a11y_bus_pid, SIGTERM);
903
904 /* Clear the X property if our bus is gone; in the case where e.g.
905 * GDM is launching a login on an X server it was using before,
906 * we don't want early login processes to pick up the stale address.
907 */
908 #ifdef HAVE_X11
909 if (_global_app->x11_prop_set)
910 {
911 Display *display = XOpenDisplay (NULL);
912 if (display)
913 {
914 Atom bus_address_atom = XInternAtom (display, "AT_SPI_BUS", False);
915 XDeleteProperty (display,
916 XDefaultRootWindow (display),
917 bus_address_atom);
918
919 XFlush (display);
920 XCloseDisplay (display);
921 }
922 }
923 #endif
924
925 if (_global_app->a11y_launch_error_message)
926 {
927 g_printerr ("Failed to launch bus: %s", _global_app->a11y_launch_error_message);
928 return 1;
929 }
930 return 0;
931 }
932