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 "accessible-stateset.h"
49 #include "accessible-register.h"
50 #include "accessible-leasing.h"
51 #include "accessible-cache.h"
55 /*---------------------------------------------------------------------------*/
57 static DBusHandlerResult
58 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data);
60 SpiBridge *spi_global_app_data = NULL;
62 /*static Display *bridge_display = NULL;*/
64 /*---------------------------------------------------------------------------*/
67 * Returns a 'canonicalized' value for DISPLAY,
68 * with the screen number stripped off if present.
72 spi_display_name (void)
74 static const char *canonical_display_name = NULL;
75 if (!canonical_display_name)
77 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
80 display_env = g_getenv ("DISPLAY");
81 if (!display_env || !display_env[0])
82 canonical_display_name = ":0";
85 gchar *display_p, *screen_p;
86 canonical_display_name = g_strdup (display_env);
87 display_p = strrchr (canonical_display_name, ':');
88 screen_p = strrchr (canonical_display_name, '.');
89 if (screen_p && display_p && (screen_p > display_p))
97 canonical_display_name = display_env;
100 return canonical_display_name;
103 /*---------------------------------------------------------------------------*/
106 set_reply (DBusPendingCall *pending, void *user_data)
108 void **replyptr = (void **)user_data;
110 *replyptr = dbus_pending_call_steal_reply (pending);
111 dbus_pending_call_unref (pending);
114 /*---------------------------------------------------------------------------*/
117 add_event (const char *bus_name, const char *event)
123 spi_atk_add_client (bus_name);
124 evdata = (event_data *) g_malloc (sizeof (*evdata));
127 data = g_strsplit (event, ":", 3);
133 evdata->bus_name = g_strdup (bus_name);
135 new_list = g_list_append (spi_global_app_data->events, evdata);
137 spi_global_app_data->events = new_list;
140 static GSList *clients = NULL;
143 get_registered_event_listeners (SpiBridge *app)
145 DBusMessage *message, *reply;
146 DBusMessageIter iter, iter_array, iter_struct;
148 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
149 ATSPI_DBUS_PATH_REGISTRY,
150 ATSPI_DBUS_INTERFACE_REGISTRY,
151 "GetRegisteredEvents");
155 reply = dbus_connection_send_with_reply_and_block (app->bus, message, 5000, NULL);
156 dbus_message_unref (message);
159 spi_global_app_data->events_initialized = TRUE;
162 if (strcmp (dbus_message_get_signature (reply), "a(ss)") != 0)
164 g_warning ("atk-bridge: GetRegisteredEvents returned message with unknown signature");
165 dbus_message_unref (reply);
166 spi_global_app_data->events_initialized = TRUE;
169 dbus_message_iter_init (reply, &iter);
170 dbus_message_iter_recurse (&iter, &iter_array);
171 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
173 char *bus_name, *event;
174 dbus_message_iter_recurse (&iter_array, &iter_struct);
175 dbus_message_iter_get_basic (&iter_struct, &bus_name);
176 dbus_message_iter_next (&iter_struct);
177 dbus_message_iter_get_basic (&iter_struct, &event);
178 add_event (bus_name, event);
179 dbus_message_iter_next (&iter_array);
181 dbus_message_unref (reply);
184 spi_atk_deregister_event_listeners ();
185 spi_global_app_data->events_initialized = TRUE;
189 register_reply (DBusPendingCall *pending, void *user_data)
192 SpiBridge *app = user_data;
193 DBusMessage *message;
195 reply = dbus_pending_call_steal_reply (pending);
196 dbus_pending_call_unref (pending);
199 gchar *app_name, *obj_path;
201 if (strcmp (dbus_message_get_signature (reply), "(so)") != 0)
203 g_warning ("AT-SPI: Could not obtain desktop path or name\n");
207 DBusMessageIter iter, iter_struct;
208 dbus_message_iter_init (reply, &iter);
209 dbus_message_iter_recurse (&iter, &iter_struct);
210 dbus_message_iter_get_basic (&iter_struct, &app_name);
211 dbus_message_iter_next (&iter_struct);
212 dbus_message_iter_get_basic (&iter_struct, &obj_path);
214 app->desktop_name = g_strdup (app_name);
215 app->desktop_path = g_strdup (obj_path);
220 g_warning ("AT-SPI: Could not embed inside desktop");
223 dbus_message_unref (reply);
225 get_registered_event_listeners (spi_global_app_data);
229 register_application (SpiBridge * app)
231 DBusMessage *message, *reply;
232 DBusMessageIter iter;
234 DBusPendingCall *pending;
235 const int max_addr_length = 128; /* should be long enough */
237 dbus_error_init (&error);
239 /* These will be overridden when we get a reply, but in practice these
240 defaults should always be correct */
241 app->desktop_name = ATSPI_DBUS_NAME_REGISTRY;
242 app->desktop_path = ATSPI_DBUS_PATH_ROOT;
244 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
245 ATSPI_DBUS_PATH_ROOT,
246 ATSPI_DBUS_INTERFACE_SOCKET,
249 dbus_message_iter_init_append (message, &iter);
250 spi_object_append_reference (&iter, app->root);
252 if (!dbus_connection_send_with_reply (app->bus, message, &pending, -1)
258 dbus_pending_call_set_notify (pending, register_reply, app, NULL);
261 dbus_message_unref (message);
263 /* could this be better, we accept some amount of race in getting the temp name*/
264 /* make sure the directory exists */
265 mkdir ("/tmp/at-spi2/", S_IRWXU|S_IRWXG|S_IRWXO|S_ISVTX);
266 chmod ("/tmp/at-spi2/", S_IRWXU|S_IRWXG|S_IRWXO|S_ISVTX);
267 app->app_bus_addr = g_malloc(max_addr_length * sizeof(char));
269 sprintf (app->app_bus_addr, "unix:path=/tmp/at-spi2/socket-%d-%d", getpid(),
272 app->app_bus_addr [0] = '\0';
278 /*---------------------------------------------------------------------------*/
281 deregister_application (SpiBridge * app)
283 DBusMessage *message;
284 DBusMessageIter iter;
288 dbus_error_init (&error);
290 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
291 ATSPI_DBUS_PATH_REGISTRY,
292 ATSPI_DBUS_INTERFACE_REGISTRY,
293 "DeregisterApplication");
294 dbus_message_set_no_reply (message, TRUE);
296 uname = dbus_bus_get_unique_name (app->bus);
298 dbus_message_iter_init_append (message, &iter);
299 dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
300 dbus_connection_send (app->bus, message, NULL);
302 dbus_message_unref (message);
305 /*---------------------------------------------------------------------------*/
307 /*---------------------------------------------------------------------------*/
309 static AtkPlugClass *plug_class;
310 static AtkSocketClass *socket_class;
313 get_plug_id (AtkPlug * plug)
315 const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
317 GString *str = g_string_new (NULL);
319 path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
320 g_string_printf (str, "%s:%s", uname, path);
322 return g_string_free (str, FALSE);
326 socket_ref_state_set (AtkObject *accessible)
328 char *child_name, *child_path;
329 AtkSocket *socket = ATK_SOCKET (accessible);
333 DBusMessage *message, *reply;
334 DBusMessageIter iter, iter_array;
337 set = atk_state_set_new ();
339 if (!socket->embedded_plug_id)
342 child_name = g_strdup (socket->embedded_plug_id);
345 child_path = g_utf8_strchr (child_name + 1, -1, ':');
351 *(child_path++) = '\0';
352 message = dbus_message_new_method_call (child_name, child_path, ATSPI_DBUS_INTERFACE_ACCESSIBLE, "GetState");
354 reply = dbus_connection_send_with_reply_and_block (spi_global_app_data->bus, message, 1, NULL);
355 dbus_message_unref (message);
358 if (strcmp (dbus_message_get_signature (reply), "au") != 0)
360 dbus_message_unref (reply);
364 dbus_message_iter_init (reply, &iter);
365 dbus_message_iter_recurse (&iter, &iter_array);
368 dbus_message_iter_get_basic (&iter_array, &v);
369 for (j = 0; j < 32; j++)
373 AtkState state = spi_atk_state_from_spi_state ((count << 5) + j);
374 atk_state_set_add_state (set, state);
379 while (dbus_message_iter_next (&iter_array));
380 dbus_message_unref (reply);
385 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
387 AtkObject *accessible = ATK_OBJECT(socket);
388 gchar *plug_name, *plug_path;
389 AtkObjectClass *klass;
391 /* Force registration */
392 gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
393 /* Let the plug know that it has been embedded */
394 plug_name = g_strdup (plug_id);
400 plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
403 DBusMessage *message;
404 *(plug_path++) = '\0';
405 message = dbus_message_new_method_call (plug_name, plug_path, ATSPI_DBUS_INTERFACE_SOCKET, "Embedded");
406 dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
407 dbus_connection_send (spi_global_app_data->bus, message, NULL);
412 klass = ATK_OBJECT_GET_CLASS (accessible);
413 klass->ref_state_set = socket_ref_state_set;
417 install_plug_hooks ()
421 data = g_type_class_ref (ATK_TYPE_PLUG);
422 plug_class = ATK_PLUG_CLASS (data);
423 data = g_type_class_ref (ATK_TYPE_SOCKET);
424 socket_class = ATK_SOCKET_CLASS (data);
425 plug_class->get_object_id = get_plug_id;
426 socket_class->embed = socket_embed_hook;
430 get_ancestral_uid (uint pid)
436 gboolean got_ppid = 0;
437 gboolean got_uid = 0;
439 sprintf (buf, "/proc/%d/status", pid);
440 fp = fopen (buf, "r");
443 while ((!got_ppid || !got_uid) && fgets (buf, sizeof (buf), fp))
445 if (sscanf (buf, "PPid:\t%d", &ppid) == 1)
447 else if (sscanf (buf, "Uid:\t%d", &uid) == 1)
452 if (!got_ppid || !got_uid)
456 if (ppid == 0 || ppid == 1)
458 return get_ancestral_uid (ppid);
462 user_check (DBusConnection *bus, unsigned long uid)
464 if (uid == getuid () || uid == geteuid ())
467 return get_ancestral_uid (getpid ()) == uid;
472 new_connection_cb (DBusServer *server, DBusConnection *con, void *data)
476 dbus_connection_set_unix_user_function (con, user_check, NULL, NULL);
477 dbus_connection_ref(con);
478 atspi_dbus_connection_setup_with_g_main(con, NULL);
479 droute_intercept_dbus (con);
480 droute_context_register (spi_global_app_data->droute, con);
482 spi_global_app_data->direct_connections = g_list_append (spi_global_app_data->direct_connections, con);
492 dbus_error_init(&err);
493 server = dbus_server_listen(spi_global_app_data->app_bus_addr, &err);
496 g_warning (_("atk-bridge: Couldn't listen on dbus server: %s"), err.message);
497 dbus_error_init (&err);
498 spi_global_app_data->app_bus_addr [0] = '\0';
499 g_main_context_unref (spi_global_app_data->main_context);
500 spi_global_app_data->main_context = NULL;
504 atspi_dbus_server_setup_with_g_main(server, NULL);
505 dbus_server_set_new_connection_function(server, new_connection_cb, NULL, NULL);
507 spi_global_app_data->server = server;
514 gchar *atspi_dbus_name = NULL;
515 static gboolean atspi_no_register = FALSE;
517 static GOptionEntry atspi_option_entries[] = {
518 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
519 "D-Bus bus name to register as", NULL},
520 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
521 "Do not register with Registry Daemon", NULL},
526 introspect_children_cb (const char *path, void *data)
528 if (!strcmp (path, "/org/a11y/atspi/accessible"))
530 return g_strdup ("<node name=\"root\"/>\n");
531 /* TODO: Should we place the whole hierarchy here? */
537 handle_event_listener_registered (DBusConnection *bus, DBusMessage *message,
543 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
544 DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
547 add_event (sender, name);
551 remove_events (const char *bus_name, const char *event)
557 remove_data = g_strsplit (event, ":", 3);
563 for (list = spi_global_app_data->events; list;)
565 event_data *evdata = list->data;
566 if (!g_strcmp0 (evdata->bus_name, bus_name) &&
567 spi_event_is_subtype (evdata->data, remove_data))
569 GList *events = spi_global_app_data->events;
571 g_strfreev (evdata->data);
572 g_free (evdata->bus_name);
574 spi_global_app_data->events = g_list_remove (events, evdata);
582 g_strfreev (remove_data);
586 handle_event_listener_deregistered (DBusConnection *bus, DBusMessage *message,
589 const char *orig_name;
593 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
594 DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
597 remove_events (sender, name);
600 static DBusHandlerResult
601 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
603 const char *interface = dbus_message_get_interface (message);
604 const char *member = dbus_message_get_member (message);
605 DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
607 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
608 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
610 if (!strcmp (interface, ATSPI_DBUS_INTERFACE_REGISTRY))
612 result = DBUS_HANDLER_RESULT_HANDLED;
613 if (!strcmp (member, "EventListenerRegistered"))
614 handle_event_listener_registered (bus, message, user_data);
615 else if (!strcmp (member, "EventListenerDeregistered"))
616 handle_event_listener_deregistered (bus, message, user_data);
618 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
621 if (!g_strcmp0(interface, DBUS_INTERFACE_DBUS) &&
622 !g_strcmp0(member, "NameOwnerChanged"))
624 char *name, *old, *new;
625 result = DBUS_HANDLER_RESULT_HANDLED;
626 if (dbus_message_get_args (message, NULL,
627 DBUS_TYPE_STRING, &name,
628 DBUS_TYPE_STRING, &old,
629 DBUS_TYPE_STRING, &new,
632 if (*old != '\0' && *new == '\0')
633 spi_atk_remove_client (old);
643 * The following needs to be initialized.
645 * - DRoute for routing message to their accessible objects.
646 * - Event handlers for emmitting signals on specific ATK events.
647 * - setup the bus for p2p communication
648 * - Application registration with the AT-SPI registry.
652 adaptor_init (gint * argc, gchar ** argv[])
658 gchar *introspection_directory;
659 static gboolean inited = FALSE;
666 DRoutePath *treepath, *accpath;
668 root = atk_get_root ();
669 g_warn_if_fail (root);
676 /* Parse command line options */
677 opt = g_option_context_new (NULL);
678 g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
679 g_option_context_set_ignore_unknown_options (opt, TRUE);
680 if (!g_option_context_parse (opt, argc, argv, &err))
681 g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
682 g_option_context_free (opt);
684 /* Allocate global data and do ATK initializations */
685 spi_global_app_data = g_new0 (SpiBridge, 1);
686 spi_global_app_data->root = g_object_ref (root);
688 /* Set up D-Bus connection and register bus name */
689 dbus_error_init (&error);
690 spi_global_app_data->bus = atspi_get_a11y_bus ();
691 if (!spi_global_app_data->bus)
693 g_free (spi_global_app_data);
694 spi_global_app_data = NULL;
699 if (atspi_dbus_name != NULL)
701 if (dbus_bus_request_name
702 (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
704 g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
709 ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
714 spi_global_app_data->main_context = g_main_context_new ();
716 atspi_dbus_connection_setup_with_g_main (spi_global_app_data->bus, NULL);
718 /* Hook our plug-and socket functions */
719 install_plug_hooks ();
722 * Create the leasing, register and cache objects.
723 * The order is important here, the cache depends on the
726 spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
727 spi_global_leasing = g_object_new (SPI_LEASING_TYPE, NULL);
728 spi_global_cache = g_object_new (SPI_CACHE_TYPE, NULL);
730 /* Register droute for routing AT-SPI messages */
731 spi_global_app_data->droute =
734 treepath = droute_add_one (spi_global_app_data->droute,
735 "/org/a11y/atspi/cache", spi_global_cache);
739 g_warning ("atk-bridge: Error in droute_add_one(). Already running?");
743 accpath = droute_add_many (spi_global_app_data->droute,
744 "/org/a11y/atspi/accessible",
746 introspect_children_cb,
748 (DRouteGetDatumFunction)
749 spi_global_register_path_to_object);
752 /* Register all interfaces with droute and set up application accessible db */
753 spi_initialize_cache (treepath);
754 spi_initialize_accessible (accpath);
755 spi_initialize_application (accpath);
756 spi_initialize_action (accpath);
757 spi_initialize_collection (accpath);
758 spi_initialize_component (accpath);
759 spi_initialize_document (accpath);
760 spi_initialize_editabletext (accpath);
761 spi_initialize_hyperlink (accpath);
762 spi_initialize_hypertext (accpath);
763 spi_initialize_image (accpath);
764 spi_initialize_selection (accpath);
765 spi_initialize_socket (accpath);
766 spi_initialize_table (accpath);
767 spi_initialize_text (accpath);
768 spi_initialize_value (accpath);
770 droute_context_register (spi_global_app_data->droute,
771 spi_global_app_data->bus);
773 /* Register methods to send D-Bus signals on certain ATK events */
774 spi_atk_register_event_listeners ();
776 /* Set up filter and match rules to catch signals */
777 dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.Registry', sender='org.a11y.atspi.Registry'", NULL);
778 dbus_connection_add_filter (spi_global_app_data->bus, signal_filter, NULL,
781 /* Register this app by sending a signal out to AT-SPI registry daemon */
782 if (!atspi_no_register && (!root || !ATK_IS_PLUG (root)))
783 register_application (spi_global_app_data);
785 get_registered_event_listeners (spi_global_app_data);
792 /*---------------------------------------------------------------------------*/
795 gtk_module_init (gint * argc, gchar ** argv[])
797 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
799 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
801 return adaptor_init (argc, argv);
807 g_module_check_init (GModule *module)
809 g_module_make_resident (module);
815 gnome_accessibility_module_init (void)
817 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
819 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
821 adaptor_init (NULL, NULL);
823 if (g_getenv ("AT_SPI_DEBUG"))
825 g_print ("Atk Accessibility bridge initialized\n");
831 gnome_accessibility_module_shutdown (void)
835 if (!spi_global_app_data)
838 spi_atk_tidy_windows ();
839 spi_atk_deregister_event_listeners ();
841 deregister_application (spi_global_app_data);
843 if (spi_global_app_data->bus)
845 dbus_connection_remove_filter (spi_global_app_data->bus, signal_filter, NULL);
846 droute_context_unregister (spi_global_app_data->droute, spi_global_app_data->bus);
847 dbus_connection_unref (spi_global_app_data->bus);
850 for (l = spi_global_app_data->direct_connections; l; l = l->next)
852 droute_context_unregister (spi_global_app_data->droute, l->data);
853 droute_unintercept_dbus (l->data);
854 dbus_connection_unref (l);
856 g_list_free (spi_global_app_data->direct_connections);
858 for (l = clients; l; l = l->next)
860 g_list_free (clients);
863 g_object_unref (spi_global_cache);
864 g_object_unref (spi_global_leasing);
865 g_object_unref (spi_global_register);
867 if (spi_global_app_data->main_context)
868 g_main_context_unref (spi_global_app_data->main_context);
870 droute_free (spi_global_app_data->droute);
872 g_free (spi_global_app_data);
873 spi_global_app_data = NULL;
875 /* Not currently creating an XDisplay */
878 XCloseDisplay (bridge_display);
882 static gchar *name_match_tmpl =
883 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged', arg0='%s'";
886 spi_atk_add_client (const char *bus_name)
891 for (l = clients; l; l = l->next)
893 if (!g_strcmp0 (l->data, bus_name))
896 if (!clients && spi_global_app_data->events_initialized)
897 spi_atk_register_event_listeners ();
898 clients = g_slist_append (clients, g_strdup (bus_name));
899 match = g_strdup_printf (name_match_tmpl, bus_name);
900 dbus_bus_add_match (spi_global_app_data->bus, match, NULL);
905 spi_atk_remove_client (const char *bus_name)
909 for (l = clients; l; l = l->next)
911 if (!g_strcmp0 (l->data, bus_name))
913 gchar *match = g_strdup_printf (name_match_tmpl, l->data);
914 dbus_bus_remove_match (spi_global_app_data->bus, match, NULL);
917 clients = g_slist_remove_link (clients, l);
919 spi_atk_deregister_event_listeners ();
924 /*END------------------------------------------------------------------------*/