2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2008, 2009 Codethink Ltd.
6 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.,
7 * Copyright 2001, 2002, 2003 Ximian, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
29 #include <X11/Xatom.h>
38 #include <droute/droute.h>
40 #include <glib/gi18n.h>
41 #include <atspi/atspi.h>
47 #include "common/spi-stateset.h"
49 #include "accessible-register.h"
50 #include "accessible-leasing.h"
51 #include "accessible-cache.h"
53 #include "common/spi-dbus.h"
55 /*---------------------------------------------------------------------------*/
57 SpiBridge *spi_global_app_data = NULL;
59 /*static Display *bridge_display = NULL;*/
61 /*---------------------------------------------------------------------------*/
64 * Returns a 'canonicalized' value for DISPLAY,
65 * with the screen number stripped off if present.
69 spi_display_name (void)
71 static const char *canonical_display_name = NULL;
72 if (!canonical_display_name)
74 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
77 display_env = g_getenv ("DISPLAY");
78 if (!display_env || !display_env[0])
79 canonical_display_name = ":0";
82 gchar *display_p, *screen_p;
83 canonical_display_name = g_strdup (display_env);
84 display_p = strrchr (canonical_display_name, ':');
85 screen_p = strrchr (canonical_display_name, '.');
86 if (screen_p && display_p && (screen_p > display_p))
94 canonical_display_name = display_env;
97 return canonical_display_name;
100 /*---------------------------------------------------------------------------*/
103 set_reply (DBusPendingCall *pending, void *user_data)
105 void **replyptr = (void **)user_data;
107 *replyptr = dbus_pending_call_steal_reply (pending);
108 dbus_pending_call_unref (pending);
111 /*---------------------------------------------------------------------------*/
114 add_event (const char *bus_name, const char *event)
120 spi_atk_add_client (bus_name);
121 evdata = (event_data *) g_malloc (sizeof (*evdata));
124 data = g_strsplit (event, ":", 3);
130 evdata->bus_name = g_strdup (bus_name);
132 new_list = g_list_append (spi_global_app_data->events, evdata);
134 spi_global_app_data->events = new_list;
137 static GSList *clients = NULL;
140 get_registered_event_listeners (SpiBridge *app)
142 DBusMessage *message, *reply;
143 DBusMessageIter iter, iter_array, iter_struct;
145 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
146 SPI_DBUS_PATH_REGISTRY,
147 SPI_DBUS_INTERFACE_REGISTRY,
148 "GetRegisteredEvents");
152 reply = dbus_connection_send_with_reply_and_block (app->bus, message, 5000, NULL);
153 dbus_message_unref (message);
156 spi_global_app_data->events_initialized = TRUE;
159 if (strcmp (dbus_message_get_signature (reply), "a(ss)") != 0)
161 g_warning ("atk-bridge: GetRegisteredEvents returned message with unknown signature");
162 dbus_message_unref (reply);
163 spi_global_app_data->events_initialized = TRUE;
166 dbus_message_iter_init (reply, &iter);
167 dbus_message_iter_recurse (&iter, &iter_array);
168 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
170 char *bus_name, *event;
171 dbus_message_iter_recurse (&iter_array, &iter_struct);
172 dbus_message_iter_get_basic (&iter_struct, &bus_name);
173 dbus_message_iter_next (&iter_struct);
174 dbus_message_iter_get_basic (&iter_struct, &event);
175 add_event (bus_name, event);
176 dbus_message_iter_next (&iter_array);
178 dbus_message_unref (reply);
181 spi_atk_deregister_event_listeners ();
182 spi_global_app_data->events_initialized = TRUE;
186 register_reply (DBusPendingCall *pending, void *user_data)
189 SpiBridge *app = user_data;
190 DBusMessage *message;
192 reply = dbus_pending_call_steal_reply (pending);
193 dbus_pending_call_unref (pending);
196 gchar *app_name, *obj_path;
198 if (strcmp (dbus_message_get_signature (reply), "(so)") != 0)
200 g_warning ("AT-SPI: Could not obtain desktop path or name\n");
204 DBusMessageIter iter, iter_struct;
205 dbus_message_iter_init (reply, &iter);
206 dbus_message_iter_recurse (&iter, &iter_struct);
207 dbus_message_iter_get_basic (&iter_struct, &app_name);
208 dbus_message_iter_next (&iter_struct);
209 dbus_message_iter_get_basic (&iter_struct, &obj_path);
211 app->desktop_name = g_strdup (app_name);
212 app->desktop_path = g_strdup (obj_path);
217 g_warning ("AT-SPI: Could not embed inside desktop");
220 dbus_message_unref (reply);
222 get_registered_event_listeners (spi_global_app_data);
226 register_application (SpiBridge * app)
228 DBusMessage *message, *reply;
229 DBusMessageIter iter;
231 DBusPendingCall *pending;
232 const int max_addr_length = 128; /* should be long enough */
234 dbus_error_init (&error);
236 /* These will be overridden when we get a reply, but in practice these
237 defaults should always be correct */
238 app->desktop_name = SPI_DBUS_NAME_REGISTRY;
239 app->desktop_path = SPI_DBUS_PATH_ROOT;
241 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
243 SPI_DBUS_INTERFACE_SOCKET,
246 dbus_message_iter_init_append (message, &iter);
247 spi_object_append_reference (&iter, app->root);
249 if (!dbus_connection_send_with_reply (app->bus, message, &pending, -1)
255 dbus_pending_call_set_notify (pending, register_reply, app, NULL);
258 dbus_message_unref (message);
260 /* could this be better, we accept some amount of race in getting the temp name*/
261 /* make sure the directory exists */
262 mkdir ("/tmp/at-spi2/", S_IRWXU|S_IRWXG|S_IRWXO|S_ISVTX);
263 chmod ("/tmp/at-spi2/", S_IRWXU|S_IRWXG|S_IRWXO|S_ISVTX);
264 app->app_bus_addr = g_malloc(max_addr_length * sizeof(char));
266 sprintf (app->app_bus_addr, "unix:path=/tmp/at-spi2/socket-%d-%d", getpid(),
269 app->app_bus_addr [0] = '\0';
275 /*---------------------------------------------------------------------------*/
278 deregister_application (SpiBridge * app)
280 DBusMessage *message;
281 DBusMessageIter iter;
285 dbus_error_init (&error);
287 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
288 SPI_DBUS_PATH_REGISTRY,
289 SPI_DBUS_INTERFACE_REGISTRY,
290 "DeregisterApplication");
291 dbus_message_set_no_reply (message, TRUE);
293 uname = dbus_bus_get_unique_name (app->bus);
295 dbus_message_iter_init_append (message, &iter);
296 dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
297 dbus_connection_send (app->bus, message, NULL);
299 dbus_message_unref (message);
302 /*---------------------------------------------------------------------------*/
307 if (!spi_global_app_data)
312 spi_atk_tidy_windows ();
313 spi_atk_deregister_event_listeners ();
314 deregister_application (spi_global_app_data);
316 g_free (spi_global_app_data);
317 spi_global_app_data = NULL;
319 /* Not currently creating an XDisplay */
322 XCloseDisplay (bridge_display);
326 /*---------------------------------------------------------------------------*/
328 static AtkPlugClass *plug_class;
329 static AtkSocketClass *socket_class;
332 get_plug_id (AtkPlug * plug)
334 const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
336 GString *str = g_string_new (NULL);
338 path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
339 g_string_printf (str, "%s:%s", uname, path);
341 return g_string_free (str, FALSE);
345 socket_ref_state_set (AtkObject *accessible)
347 char *child_name, *child_path;
348 AtkSocket *socket = ATK_SOCKET (accessible);
352 DBusMessage *message, *reply;
353 DBusMessageIter iter, iter_array;
356 if (!socket->embedded_plug_id)
359 child_name = g_strdup (socket->embedded_plug_id);
362 child_path = g_utf8_strchr (child_name + 1, -1, ':');
368 *(child_path++) = '\0';
369 message = dbus_message_new_method_call (child_name, child_path, SPI_DBUS_INTERFACE_ACCESSIBLE, "GetState");
371 reply = dbus_connection_send_with_reply_and_block (spi_global_app_data->bus, message, 1, NULL);
372 dbus_message_unref (message);
375 if (strcmp (dbus_message_get_signature (reply), "au") != 0)
377 dbus_message_unref (reply);
380 set = atk_state_set_new ();
383 dbus_message_iter_init (reply, &iter);
384 dbus_message_iter_recurse (&iter, &iter_array);
387 dbus_message_iter_get_basic (&iter_array, &v);
388 for (j = 0; j < 32; j++)
392 AtkState state = spi_atk_state_from_spi_state ((count << 5) + j);
393 atk_state_set_add_state (set, state);
398 while (dbus_message_iter_next (&iter_array));
399 dbus_message_unref (reply);
404 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
406 AtkObject *accessible = ATK_OBJECT(socket);
407 gchar *plug_name, *plug_path;
408 AtkObjectClass *klass;
410 /* Force registration */
411 gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
412 /* Let the plug know that it has been embedded */
413 plug_name = g_strdup (plug_id);
419 plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
422 DBusMessage *message;
423 *(plug_path++) = '\0';
424 message = dbus_message_new_method_call (plug_name, plug_path, SPI_DBUS_INTERFACE_SOCKET, "Embedded");
425 dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
426 dbus_connection_send (spi_global_app_data->bus, message, NULL);
431 klass = ATK_OBJECT_GET_CLASS (accessible);
432 klass->ref_state_set = socket_ref_state_set;
436 install_plug_hooks ()
440 data = g_type_class_ref (ATK_TYPE_PLUG);
441 plug_class = ATK_PLUG_CLASS (data);
442 data = g_type_class_ref (ATK_TYPE_SOCKET);
443 socket_class = ATK_SOCKET_CLASS (data);
444 plug_class->get_object_id = get_plug_id;
445 socket_class->embed = socket_embed_hook;
449 get_ancestral_uid (uint pid)
455 gboolean got_ppid = 0;
456 gboolean got_uid = 0;
458 sprintf (buf, "/proc/%d/status", pid);
459 fp = fopen (buf, "r");
462 while ((!got_ppid || !got_uid) && fgets (buf, sizeof (buf), fp))
464 if (sscanf (buf, "PPid:\t%d", &ppid) == 1)
466 else if (sscanf (buf, "Uid:\t%d", &uid) == 1)
471 if (!got_ppid || !got_uid)
475 if (ppid == 0 || ppid == 1)
477 return get_ancestral_uid (ppid);
481 user_check (DBusConnection *bus, unsigned long uid)
483 if (uid == getuid () || uid == geteuid ())
486 return get_ancestral_uid (getpid ()) == uid;
491 new_connection_cb (DBusServer *server, DBusConnection *con, void *data)
495 dbus_connection_set_unix_user_function (con, user_check, NULL, NULL);
496 dbus_connection_ref(con);
497 atspi_dbus_connection_setup_with_g_main(con, NULL);
498 droute_intercept_dbus (con);
499 droute_context_register (spi_global_app_data->droute, con);
501 new_list = g_list_append (spi_global_app_data->direct_connections, con);
503 spi_global_app_data->direct_connections = new_list;
513 dbus_error_init(&err);
514 server = dbus_server_listen(spi_global_app_data->app_bus_addr, &err);
517 g_warning (_("atk-bridge: Couldn't listen on dbus server: %s"), err.message);
518 dbus_error_init (&err);
519 spi_global_app_data->app_bus_addr [0] = '\0';
520 g_main_context_unref (spi_global_app_data->main_context);
521 spi_global_app_data->main_context = NULL;
525 atspi_dbus_server_setup_with_g_main(server, NULL);
526 dbus_server_set_new_connection_function(server, new_connection_cb, NULL, NULL);
528 spi_global_app_data->server = server;
535 gchar *atspi_dbus_name = NULL;
536 static gboolean atspi_no_register = FALSE;
538 static GOptionEntry atspi_option_entries[] = {
539 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
540 "D-Bus bus name to register as", NULL},
541 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
542 "Do not register with Registry Daemon", NULL},
547 introspect_children_cb (const char *path, void *data)
549 if (!strcmp (path, "/org/a11y/atspi/accessible"))
551 return g_strdup ("<node name=\"root\"/>\n");
552 /* TODO: Should we place the whole hierarchy here? */
558 handle_event_listener_registered (DBusConnection *bus, DBusMessage *message,
564 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
565 DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
568 add_event (sender, name);
572 remove_events (const char *bus_name, const char *event)
578 remove_data = g_strsplit (event, ":", 3);
584 for (list = spi_global_app_data->events; list;)
586 event_data *evdata = list->data;
587 if (!g_strcmp0 (evdata->bus_name, bus_name) &&
588 spi_event_is_subtype (evdata->data, remove_data))
590 GList *events = spi_global_app_data->events;
592 g_strfreev (evdata->data);
593 g_free (evdata->bus_name);
595 spi_global_app_data->events = g_list_remove (events, evdata);
603 g_strfreev (remove_data);
607 handle_event_listener_deregistered (DBusConnection *bus, DBusMessage *message,
610 const char *orig_name;
614 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
615 DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
618 remove_events (sender, name);
621 static DBusHandlerResult
622 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
624 const char *interface = dbus_message_get_interface (message);
625 const char *member = dbus_message_get_member (message);
626 DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
628 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
629 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
631 if (!strcmp (interface, SPI_DBUS_INTERFACE_REGISTRY))
633 result = DBUS_HANDLER_RESULT_HANDLED;
634 if (!strcmp (member, "EventListenerRegistered"))
635 handle_event_listener_registered (bus, message, user_data);
636 else if (!strcmp (member, "EventListenerDeregistered"))
637 handle_event_listener_deregistered (bus, message, user_data);
639 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
642 if (!g_strcmp0(interface, DBUS_INTERFACE_DBUS) &&
643 !g_strcmp0(member, "NameOwnerChanged"))
645 char *name, *old, *new;
646 result = DBUS_HANDLER_RESULT_HANDLED;
647 if (dbus_message_get_args (message, NULL,
648 DBUS_TYPE_STRING, &name,
649 DBUS_TYPE_STRING, &old,
650 DBUS_TYPE_STRING, &new,
653 if (*old != '\0' && *new == '\0')
654 spi_atk_remove_client (old);
664 * The following needs to be initialized.
666 * - DRoute for routing message to their accessible objects.
667 * - Event handlers for emmitting signals on specific ATK events.
668 * - setup the bus for p2p communication
669 * - Application registration with the AT-SPI registry.
673 adaptor_init (gint * argc, gchar ** argv[])
679 gchar *introspection_directory;
680 static gboolean inited = FALSE;
687 DRoutePath *treepath, *accpath;
689 root = atk_get_root ();
690 g_warn_if_fail (root);
697 /* Parse command line options */
698 opt = g_option_context_new (NULL);
699 g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
700 g_option_context_set_ignore_unknown_options (opt, TRUE);
701 if (!g_option_context_parse (opt, argc, argv, &err))
702 g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
703 g_option_context_free (opt);
705 /* Allocate global data and do ATK initializations */
706 spi_global_app_data = g_new0 (SpiBridge, 1);
707 spi_global_app_data->root = g_object_ref (root);
709 /* Set up D-Bus connection and register bus name */
710 dbus_error_init (&error);
711 spi_global_app_data->bus = atspi_get_a11y_bus ();
712 if (!spi_global_app_data->bus)
714 g_free (spi_global_app_data);
715 spi_global_app_data = NULL;
720 if (atspi_dbus_name != NULL)
722 if (dbus_bus_request_name
723 (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
725 g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
730 ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
735 spi_global_app_data->main_context = g_main_context_new ();
737 atspi_dbus_connection_setup_with_g_main (spi_global_app_data->bus, NULL);
739 /* Hook our plug-and socket functions */
740 install_plug_hooks ();
743 * Create the leasing, register and cache objects.
744 * The order is important here, the cache depends on the
747 spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
748 spi_global_leasing = g_object_new (SPI_LEASING_TYPE, NULL);
749 spi_global_cache = g_object_new (SPI_CACHE_TYPE, NULL);
751 /* Register droute for routing AT-SPI messages */
752 spi_global_app_data->droute =
755 treepath = droute_add_one (spi_global_app_data->droute,
756 "/org/a11y/atspi/cache", spi_global_cache);
760 g_warning ("atk-bridge: Error in droute_add_one(). Already running?");
764 accpath = droute_add_many (spi_global_app_data->droute,
765 "/org/a11y/atspi/accessible",
767 introspect_children_cb,
769 (DRouteGetDatumFunction)
770 spi_global_register_path_to_object);
773 /* Register all interfaces with droute and set up application accessible db */
774 spi_initialize_cache (treepath);
775 spi_initialize_accessible (accpath);
776 spi_initialize_application (accpath);
777 spi_initialize_action (accpath);
778 spi_initialize_collection (accpath);
779 spi_initialize_component (accpath);
780 spi_initialize_document (accpath);
781 spi_initialize_editabletext (accpath);
782 spi_initialize_hyperlink (accpath);
783 spi_initialize_hypertext (accpath);
784 spi_initialize_image (accpath);
785 spi_initialize_selection (accpath);
786 spi_initialize_socket (accpath);
787 spi_initialize_table (accpath);
788 spi_initialize_text (accpath);
789 spi_initialize_value (accpath);
791 droute_context_register (spi_global_app_data->droute,
792 spi_global_app_data->bus);
794 /* Register methods to send D-Bus signals on certain ATK events */
795 spi_atk_register_event_listeners ();
797 /* Set up filter and match rules to catch signals */
798 dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.Registry', sender='org.a11y.atspi.Registry'", NULL);
799 dbus_connection_add_filter (spi_global_app_data->bus, signal_filter, NULL,
802 /* Register this app by sending a signal out to AT-SPI registry daemon */
803 if (!atspi_no_register && (!root || !ATK_IS_PLUG (root)))
804 register_application (spi_global_app_data);
806 get_registered_event_listeners (spi_global_app_data);
813 /*---------------------------------------------------------------------------*/
816 gtk_module_init (gint * argc, gchar ** argv[])
818 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
820 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
822 return adaptor_init (argc, argv);
828 g_module_check_init (GModule *module)
830 g_module_make_resident (module);
836 gnome_accessibility_module_init (void)
838 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
840 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
842 adaptor_init (NULL, NULL);
844 if (g_getenv ("AT_SPI_DEBUG"))
846 g_print ("Atk Accessibility bridge initialized\n");
852 gnome_accessibility_module_shutdown (void)
854 spi_atk_deregister_event_listeners ();
858 static gchar *name_match_tmpl =
859 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged', arg0='%s'";
862 spi_atk_add_client (const char *bus_name)
867 for (l = clients; l; l = l->next)
869 if (!g_strcmp0 (l->data, bus_name))
872 if (!clients && spi_global_app_data->events_initialized)
873 spi_atk_register_event_listeners ();
874 clients = g_slist_append (clients, g_strdup (bus_name));
875 match = g_strdup_printf (name_match_tmpl, bus_name);
876 dbus_bus_add_match (spi_global_app_data->bus, match, NULL);
881 spi_atk_remove_client (const char *bus_name)
885 for (l = clients; l; l = l->next)
887 if (!g_strcmp0 (l->data, bus_name))
889 gchar *match = g_strdup_printf (name_match_tmpl, l->data);
890 dbus_bus_remove_match (spi_global_app_data->bus, match, NULL);
893 clients = g_slist_remove_link (clients, l);
895 spi_atk_deregister_event_listeners ();
900 /*END------------------------------------------------------------------------*/