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 Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 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 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
36 #include <droute/droute.h>
37 #include <atspi/atspi.h>
38 #include <atk-bridge.h>
44 #include "accessible-stateset.h"
46 #include "accessible-register.h"
47 #include "accessible-leasing.h"
48 #include "accessible-cache.h"
52 /*---------------------------------------------------------------------------*/
54 static DBusHandlerResult
55 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data);
57 SpiBridge *spi_global_app_data = NULL;
59 static gboolean inited = FALSE;
60 static gboolean atexit_added = FALSE;
62 /*---------------------------------------------------------------------------*/
65 add_event (const char *bus_name, const char *event)
70 spi_atk_add_client (bus_name);
71 evdata = g_new0 (event_data, 1);
72 data = g_strsplit (event, ":", 3);
78 evdata->bus_name = g_strdup (bus_name);
80 spi_global_app_data->events = g_list_append (spi_global_app_data->events, evdata);
84 static GSList *clients = NULL;
89 static int replies_received = 0;
91 if (!spi_global_app_data)
95 if (replies_received == 3)
98 spi_atk_deregister_event_listeners ();
99 spi_global_app_data->events_initialized = TRUE;
104 _atk_bridge_type_from_iface (const char *iface)
106 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_ACCESSIBLE))
107 return ATK_TYPE_OBJECT;
108 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_ACTION))
109 return ATK_TYPE_ACTION;
110 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_COMPONENT))
111 return ATK_TYPE_COMPONENT;
112 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_DOCUMENT))
113 return ATK_TYPE_DOCUMENT;
114 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_HYPERTEXT))
115 return ATK_TYPE_HYPERTEXT;
116 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_HYPERLINK))
117 return ATK_TYPE_HYPERLINK;
118 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_IMAGE))
119 return ATK_TYPE_IMAGE;
120 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_SELECTION))
121 return ATK_TYPE_SELECTION;
122 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_TABLE))
123 return ATK_TYPE_TABLE;
124 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_TEXT))
125 return ATK_TYPE_TEXT;
126 if (!strcmp (iface, ATSPI_DBUS_INTERFACE_VALUE))
127 return ATK_TYPE_VALUE;
131 DRoutePropertyFunction
132 _atk_bridge_find_property_func (const char *property, GType *type)
138 if (!strncasecmp (property, "action.", 7))
140 iface = ATSPI_DBUS_INTERFACE_ACTION;
141 member = property + 7;
143 else if (!strncasecmp (property, "component.", 10))
145 iface = ATSPI_DBUS_INTERFACE_COMPONENT;
146 member = property + 10;
148 else if (!strncasecmp (property, "selection.", 10))
150 iface = ATSPI_DBUS_INTERFACE_SELECTION;
151 member = property + 10;
153 else if (!strncasecmp (property, "table.", 6))
155 iface = ATSPI_DBUS_INTERFACE_TABLE;
156 member = property + 6;
158 else if (!strncasecmp (property, "text.", 5))
160 iface = ATSPI_DBUS_INTERFACE_TEXT;
161 member = property + 5;
163 else if (!strncasecmp (property, "value.", 6))
165 iface = ATSPI_DBUS_INTERFACE_VALUE;
166 member = property + 6;
170 iface = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
174 *type = _atk_bridge_type_from_iface (iface);
176 dp = g_hash_table_lookup (spi_global_app_data->property_hash, iface);
181 for (;dp->name; dp++)
183 if (!strcasecmp (dp->name, member))
192 add_property_to_event (event_data *evdata, const char *property)
194 AtspiPropertyDefinition *prop = g_new0 (AtspiPropertyDefinition, 1);
195 prop->func = _atk_bridge_find_property_func (property, &prop->type);
198 g_warning ("atk-bridge: Request for unknown property '%s'", property);
203 prop->name = g_strdup (property);
204 evdata->properties = g_slist_append (evdata->properties, prop);
208 add_event_from_iter (DBusMessageIter *iter)
210 const char *bus_name, *event;
213 dbus_message_iter_get_basic (iter, &bus_name);
214 dbus_message_iter_next (iter);
215 dbus_message_iter_get_basic (iter, &event);
216 dbus_message_iter_next (iter);
217 evdata = add_event (bus_name, event);
218 if (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY)
220 DBusMessageIter iter_sub_array;
221 dbus_message_iter_recurse (iter, &iter_sub_array);
222 while (dbus_message_iter_get_arg_type (&iter_sub_array) != DBUS_TYPE_INVALID)
224 const char *property;
225 dbus_message_iter_get_basic (&iter_sub_array, &property);
226 add_property_to_event (evdata, property);
227 dbus_message_iter_next (&iter_sub_array);
233 get_events_reply (DBusPendingCall *pending, void *user_data)
235 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
236 DBusMessageIter iter, iter_array, iter_struct;
238 if (!reply || !spi_global_app_data)
241 if (strcmp (dbus_message_get_signature (reply), "a(ss)") != 0 &&
242 strcmp (dbus_message_get_signature (reply), "a(ssas)") != 0)
244 g_warning ("atk-bridge: GetRegisteredEvents returned message with unknown signature");
248 dbus_message_iter_init (reply, &iter);
249 dbus_message_iter_recurse (&iter, &iter_array);
250 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
252 dbus_message_iter_recurse (&iter_array, &iter_struct);
253 add_event_from_iter (&iter_struct);
254 dbus_message_iter_next (&iter_array);
259 dbus_message_unref (reply);
261 dbus_pending_call_unref (pending);
263 tally_event_reply ();
267 get_device_events_reply (DBusPendingCall *pending, void *user_data)
269 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
270 DBusMessageIter iter, iter_array, iter_struct;
275 if (strncmp (dbus_message_get_signature (reply), "a(s", 3) != 0)
277 g_warning ("atk-bridge: get_device_events_reply: unknown signature");
281 dbus_message_iter_init (reply, &iter);
282 dbus_message_iter_recurse (&iter, &iter_array);
283 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
286 dbus_message_iter_recurse (&iter_array, &iter_struct);
287 dbus_message_iter_get_basic (&iter_struct, &bus_name);
288 spi_atk_add_client (bus_name);
289 dbus_message_iter_next (&iter_array);
294 dbus_message_unref (reply);
296 dbus_pending_call_unref (pending);
298 tally_event_reply ();
302 get_registered_event_listeners (SpiBridge *app)
304 DBusMessage *message;
305 DBusPendingCall *pending = NULL;
307 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
308 ATSPI_DBUS_PATH_REGISTRY,
309 ATSPI_DBUS_INTERFACE_REGISTRY,
310 "GetRegisteredEvents");
314 dbus_connection_send_with_reply (app->bus, message, &pending, -1);
315 dbus_message_unref (message);
318 spi_global_app_data->events_initialized = TRUE;
321 dbus_pending_call_set_notify (pending, get_events_reply, NULL, NULL);
323 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
325 ATSPI_DBUS_INTERFACE_DEC,
326 "GetKeystrokeListeners");
330 dbus_connection_send_with_reply (app->bus, message, &pending, -1);
331 dbus_message_unref (message);
334 spi_global_app_data->events_initialized = TRUE;
337 dbus_pending_call_set_notify (pending, get_device_events_reply, NULL, NULL);
339 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
341 ATSPI_DBUS_INTERFACE_DEC,
342 "GetDeviceEventListeners");
346 dbus_connection_send_with_reply (app->bus, message, &pending, -1);
347 dbus_message_unref (message);
350 spi_global_app_data->events_initialized = TRUE;
353 dbus_pending_call_set_notify (pending, get_device_events_reply, NULL, NULL);
357 register_reply (DBusPendingCall *pending, void *user_data)
360 SpiBridge *app = user_data;
362 reply = dbus_pending_call_steal_reply (pending);
363 dbus_pending_call_unref (pending);
365 if (!spi_global_app_data)
368 dbus_message_unref (reply);
374 gchar *app_name, *obj_path;
376 if (strcmp (dbus_message_get_signature (reply), "(so)") != 0)
378 g_warning ("AT-SPI: Could not obtain desktop path or name\n");
382 DBusMessageIter iter, iter_struct;
383 dbus_message_iter_init (reply, &iter);
384 dbus_message_iter_recurse (&iter, &iter_struct);
385 dbus_message_iter_get_basic (&iter_struct, &app_name);
386 dbus_message_iter_next (&iter_struct);
387 dbus_message_iter_get_basic (&iter_struct, &obj_path);
389 g_free (app->desktop_name);
390 app->desktop_name = g_strdup (app_name);
391 g_free (app->desktop_path);
392 app->desktop_path = g_strdup (obj_path);
397 g_warning ("AT-SPI: Could not embed inside desktop");
400 dbus_message_unref (reply);
402 if (!spi_global_app_data->events_initialized)
403 get_registered_event_listeners (spi_global_app_data);
407 register_application (gpointer data)
409 SpiBridge * app = data;
410 DBusMessage *message;
411 DBusMessageIter iter;
412 DBusPendingCall *pending;
414 spi_global_app_data->registration_pending = 0;
416 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
417 ATSPI_DBUS_PATH_ROOT,
418 ATSPI_DBUS_INTERFACE_SOCKET,
421 dbus_message_iter_init_append (message, &iter);
422 spi_object_append_reference (&iter, app->root);
424 if (!dbus_connection_send_with_reply (app->bus, message, &pending, -1)
428 dbus_pending_call_unref (pending);
430 dbus_message_unref (message);
434 dbus_pending_call_set_notify (pending, register_reply, app, NULL);
437 dbus_message_unref (message);
443 _atk_bridge_schedule_application_registration (SpiBridge *app)
445 /* We need the callback to be called first thing, before any other of ours
446 * (and possibly of client apps), so use a high priority and a short timeout
447 * to try and be called first by the main loop. */
448 if (!app->registration_pending)
449 app->registration_pending = spi_timeout_add_full (G_PRIORITY_HIGH, 0,
450 register_application,
455 _atk_bridge_remove_pending_application_registration (SpiBridge *app)
457 if (app->registration_pending)
459 g_source_remove (app->registration_pending);
460 app->registration_pending = 0;
467 /*---------------------------------------------------------------------------*/
472 if (!spi_global_app_data)
475 if (spi_global_app_data->app_bus_addr &&
476 !strncmp (spi_global_app_data->app_bus_addr, "unix:path=", 10))
478 unlink (spi_global_app_data->app_bus_addr + 10);
479 g_free (spi_global_app_data->app_bus_addr);
480 spi_global_app_data->app_bus_addr = NULL;
483 if (spi_global_app_data->app_tmp_dir)
485 rmdir (spi_global_app_data->app_tmp_dir);
486 g_free (spi_global_app_data->app_tmp_dir);
487 spi_global_app_data->app_tmp_dir = NULL;
492 deregister_application (SpiBridge * app)
494 DBusMessage *message;
495 DBusMessageIter iter;
498 if (_atk_bridge_remove_pending_application_registration (spi_global_app_data))
501 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
502 ATSPI_DBUS_PATH_REGISTRY,
503 ATSPI_DBUS_INTERFACE_REGISTRY,
504 "DeregisterApplication");
505 dbus_message_set_no_reply (message, TRUE);
507 uname = dbus_bus_get_unique_name (app->bus);
509 dbus_message_iter_init_append (message, &iter);
510 dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
511 dbus_connection_send (app->bus, message, NULL);
513 dbus_message_unref (message);
517 g_free (app->desktop_name);
518 app->desktop_name = NULL;
519 g_free (app->desktop_path);
520 app->desktop_path = NULL;
523 /*---------------------------------------------------------------------------*/
525 /*---------------------------------------------------------------------------*/
527 static AtkPlugClass *plug_class;
528 static AtkSocketClass *socket_class;
531 get_plug_id (AtkPlug * plug)
533 const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
535 GString *str = g_string_new (NULL);
537 path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
538 g_string_printf (str, "%s:%s", uname, path);
540 return g_string_free (str, FALSE);
544 socket_ref_state_set (AtkObject *accessible)
546 char *child_name, *child_path;
547 AtkSocket *socket = ATK_SOCKET (accessible);
551 DBusMessage *message, *reply;
552 DBusMessageIter iter, iter_array;
555 set = atk_state_set_new ();
557 if (!socket->embedded_plug_id)
560 child_name = g_strdup (socket->embedded_plug_id);
563 child_path = g_utf8_strchr (child_name + 1, -1, ':');
569 *(child_path++) = '\0';
570 message = dbus_message_new_method_call (child_name, child_path, ATSPI_DBUS_INTERFACE_ACCESSIBLE, "GetState");
572 reply = dbus_connection_send_with_reply_and_block (spi_global_app_data->bus, message, 1, NULL);
573 dbus_message_unref (message);
576 if (strcmp (dbus_message_get_signature (reply), "au") != 0)
578 dbus_message_unref (reply);
582 dbus_message_iter_init (reply, &iter);
583 dbus_message_iter_recurse (&iter, &iter_array);
586 dbus_message_iter_get_basic (&iter_array, &v);
587 for (j = 0; j < 32; j++)
591 AtkState state = spi_atk_state_from_spi_state ((count << 5) + j);
592 atk_state_set_add_state (set, state);
597 while (dbus_message_iter_next (&iter_array));
598 dbus_message_unref (reply);
603 socket_embed_hook (AtkSocket * socket, const gchar * plug_id)
605 g_return_if_fail (spi_global_register != NULL);
607 AtkObject *accessible = ATK_OBJECT(socket);
608 gchar *plug_name, *plug_path;
609 AtkObjectClass *klass;
611 /* Force registration */
612 gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
613 /* Let the plug know that it has been embedded */
614 plug_name = g_strdup (plug_id);
620 plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
623 DBusMessage *message;
624 *(plug_path++) = '\0';
625 message = dbus_message_new_method_call (plug_name, plug_path, ATSPI_DBUS_INTERFACE_SOCKET, "Embedded");
626 dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
627 dbus_connection_send (spi_global_app_data->bus, message, NULL);
632 klass = ATK_OBJECT_GET_CLASS (accessible);
633 klass->ref_state_set = socket_ref_state_set;
637 install_plug_hooks ()
641 data = g_type_class_ref (ATK_TYPE_PLUG);
642 plug_class = ATK_PLUG_CLASS (data);
643 data = g_type_class_ref (ATK_TYPE_SOCKET);
644 socket_class = ATK_SOCKET_CLASS (data);
645 plug_class->get_object_id = get_plug_id;
646 socket_class->embed = socket_embed_hook;
650 get_ancestral_uid (guint pid)
656 gboolean got_ppid = 0;
657 gboolean got_uid = 0;
659 sprintf (buf, "/proc/%d/status", pid);
660 fp = fopen (buf, "r");
663 while ((!got_ppid || !got_uid) && fgets (buf, sizeof (buf), fp))
665 if (sscanf (buf, "PPid:\t%d", &ppid) == 1)
667 else if (sscanf (buf, "Uid:\t%d", &uid) == 1)
672 if (!got_ppid || !got_uid)
676 if (ppid == 0 || ppid == 1)
678 return get_ancestral_uid (ppid);
682 user_check (DBusConnection *bus, unsigned long uid, void *data)
684 if (uid == getuid () || uid == geteuid ())
688 guint ancestor = get_ancestral_uid (getpid ());
689 return (ancestor == uid || ancestor == 1 || ancestor == 0);
695 new_connection_cb (DBusServer *server, DBusConnection *con, void *data)
697 dbus_connection_set_unix_user_function (con, user_check, NULL, NULL);
698 dbus_connection_ref(con);
699 atspi_dbus_connection_setup_with_g_main(con, spi_context);
700 droute_intercept_dbus (con);
701 droute_context_register (spi_global_app_data->droute, con);
703 spi_global_app_data->direct_connections = g_list_append (spi_global_app_data->direct_connections, con);
707 gchar *atspi_dbus_name = NULL;
708 static gboolean atspi_no_register = FALSE;
710 static GOptionEntry atspi_option_entries[] = {
711 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
712 "D-Bus bus name to register as", NULL},
713 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
714 "Do not register with Registry Daemon", NULL},
719 introspect_children_cb (const char *path, void *data)
721 if (!strcmp (path, "/org/a11y/atspi/accessible"))
723 return g_strdup ("<node name=\"root\"/>\n");
724 /* TODO: Should we place the whole hierarchy here? */
730 handle_event_listener_registered (DBusConnection *bus, DBusMessage *message,
733 DBusMessageIter iter;
734 const char *signature = dbus_message_get_signature (message);
736 if (strcmp (signature, "ssas") != 0 &&
737 strcmp (signature, "ss") != 0)
739 g_warning ("got RegisterEvent with invalid signature '%s'", signature);
743 dbus_message_iter_init (message, &iter);
744 add_event_from_iter (&iter);
748 free_property_definition (void *data)
750 AtspiPropertyDefinition *pd = data;
757 remove_events (const char *bus_name, const char *event)
762 remove_data = g_strsplit (event, ":", 3);
768 for (list = spi_global_app_data->events; list;)
770 event_data *evdata = list->data;
771 if (!g_strcmp0 (evdata->bus_name, bus_name) &&
772 spi_event_is_subtype (evdata->data, remove_data))
775 GList *events = spi_global_app_data->events;
777 g_strfreev (evdata->data);
778 g_free (evdata->bus_name);
779 g_slist_free_full (evdata->properties, free_property_definition);
783 spi_global_app_data->events = g_list_delete_link (events, list);
792 g_strfreev (remove_data);
796 handle_event_listener_deregistered (DBusConnection *bus, DBusMessage *message,
802 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
803 DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
806 remove_events (sender, name);
810 handle_device_listener_registered (DBusConnection *bus, DBusMessage *message,
814 DBusMessageIter iter, iter_struct;
816 if (strncmp (dbus_message_get_signature (message), "(s", 2) != 0)
818 g_warning ("atk-bridge: handle_device_listener_register: unknown signature");
822 dbus_message_iter_init (message, &iter);
823 dbus_message_iter_recurse (&iter, &iter_struct);
824 dbus_message_iter_get_basic (&iter_struct, &sender);
825 spi_atk_add_client (sender);
828 static DBusHandlerResult
829 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
831 const char *interface = dbus_message_get_interface (message);
832 const char *member = dbus_message_get_member (message);
833 DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
834 static gboolean registry_lost = FALSE;
836 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
837 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
839 if (!strcmp (interface, ATSPI_DBUS_INTERFACE_REGISTRY))
841 result = DBUS_HANDLER_RESULT_HANDLED;
842 if (!strcmp (member, "EventListenerRegistered"))
843 handle_event_listener_registered (bus, message, user_data);
844 else if (!strcmp (member, "EventListenerDeregistered"))
845 handle_event_listener_deregistered (bus, message, user_data);
847 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
849 else if (!strcmp (interface, ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER))
851 result = DBUS_HANDLER_RESULT_HANDLED;
852 if (!strcmp (member, "KeystrokeListenerRegistered"))
853 handle_device_listener_registered (bus, message, user_data);
854 else if (!strcmp (member, "DeviceListenerRegistered"))
855 handle_device_listener_registered (bus, message, user_data);
857 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
860 if (!g_strcmp0(interface, DBUS_INTERFACE_DBUS) &&
861 !g_strcmp0(member, "NameOwnerChanged"))
863 char *name, *old, *new;
864 if (dbus_message_get_args (message, NULL,
865 DBUS_TYPE_STRING, &name,
866 DBUS_TYPE_STRING, &old,
867 DBUS_TYPE_STRING, &new,
870 if (!strcmp (name, "org.a11y.atspi.Registry"))
872 if (registry_lost && !old[0])
874 register_application (spi_global_app_data);
875 registry_lost = FALSE;
878 registry_lost = TRUE;
880 else if (*old != '\0' && *new == '\0')
881 spi_atk_remove_client (old);
889 spi_atk_create_socket (SpiBridge *app)
894 const gchar *user_runtime_dir = g_get_user_runtime_dir ();
896 if (g_mkdir_with_parents (user_runtime_dir, 0700) != 0)
901 app->app_tmp_dir = g_build_filename (user_runtime_dir,
902 "at-spi2-XXXXXX", NULL);
903 if (!g_mkdtemp (app->app_tmp_dir))
905 g_free (app->app_tmp_dir);
906 app->app_tmp_dir = NULL;
911 if (app->app_tmp_dir)
912 app->app_bus_addr = g_strdup_printf ("unix:path=%s/socket", app->app_tmp_dir);
914 app->app_bus_addr = g_strdup_printf ("unix:path=%s/at-spi2-socket-%d",
915 user_runtime_dir, getpid ());
917 if (!spi_global_app_data->app_bus_addr)
920 dbus_error_init(&error);
921 server = dbus_server_listen(spi_global_app_data->app_bus_addr, &error);
924 g_warning ("atk-bridge: Couldn't listen on dbus server: %s", error.message);
925 dbus_error_free (&error);
926 spi_global_app_data->app_bus_addr [0] = '\0';
930 atspi_dbus_server_setup_with_g_main(server, spi_context);
931 dbus_server_set_new_connection_function(server, new_connection_cb, NULL, NULL);
933 spi_global_app_data->server = server;
940 * Checks the status of the environment variables
942 * At this moment it only checks NO_AT_BRIDGE
944 * Returns TRUE if there isn't anything on the environment preventing
945 * you to load the bridge, FALSE otherwise
952 envvar = g_getenv ("NO_AT_BRIDGE");
954 if (envvar && atoi (envvar) == 1)
963 DRoutePath *treepath;
965 spi_atk_register_event_listeners ();
966 if (!spi_global_cache)
968 spi_global_cache = g_object_new (SPI_CACHE_TYPE, NULL);
969 treepath = droute_add_one (spi_global_app_data->droute,
970 "/org/a11y/atspi/cache", spi_global_cache);
974 g_warning ("atk-bridge: Error in droute_add_one(). Already running?");
977 spi_initialize_cache (treepath);
978 if (spi_global_app_data->bus)
979 droute_path_register (treepath, spi_global_app_data->bus);
984 * atk_bridge_adaptor_init: initializes the atk bridge adaptor
986 * The following needs to be initialized.
988 * - DRoute for routing message to their accessible objects.
989 * - Event handlers for emmitting signals on specific ATK events.
990 * - setup the bus for p2p communication
991 * - Application registration with the AT-SPI registry.
993 * Returns: 0 if the bridge gets or was already initialized
994 * succesfully, -1 otherwise
997 atk_bridge_adaptor_init (gint * argc, gchar ** argv[])
1003 gboolean load_bridge;
1004 DRoutePath *accpath;
1006 load_bridge = check_envvar ();
1007 if (inited && !load_bridge)
1008 g_warning ("ATK Bridge is disabled but a11y has already been enabled.");
1017 root = atk_get_root ();
1018 g_warn_if_fail (root);
1025 /* Parse command line options */
1026 opt = g_option_context_new (NULL);
1027 g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
1028 g_option_context_set_ignore_unknown_options (opt, TRUE);
1029 if (!g_option_context_parse (opt, argc, argv, &err))
1031 g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
1034 g_option_context_free (opt);
1036 /* Allocate global data and do ATK initializations */
1037 spi_global_app_data = g_new0 (SpiBridge, 1);
1038 spi_global_app_data->root = g_object_ref (root);
1039 spi_global_app_data->desktop_name = g_strdup (ATSPI_DBUS_NAME_REGISTRY);
1040 spi_global_app_data->desktop_path = g_strdup (ATSPI_DBUS_PATH_ROOT);
1042 /* Set up D-Bus connection and register bus name */
1043 dbus_error_init (&error);
1044 spi_global_app_data->bus = atspi_get_a11y_bus ();
1045 if (!spi_global_app_data->bus)
1047 g_free (spi_global_app_data);
1048 spi_global_app_data = NULL;
1053 if (atspi_dbus_name != NULL)
1055 if (dbus_bus_request_name
1056 (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
1058 g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
1063 ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
1068 spi_global_app_data->main_context = g_main_context_new ();
1070 atspi_dbus_connection_setup_with_g_main (spi_global_app_data->bus, NULL);
1072 /* Hook our plug-and socket functions */
1073 install_plug_hooks ();
1076 * Create the leasing, register and cache objects.
1077 * The order is important here, the cache depends on the
1080 spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
1081 spi_global_leasing = g_object_new (SPI_LEASING_TYPE, NULL);
1083 /* Register droute for routing AT-SPI messages */
1084 spi_global_app_data->droute =
1087 accpath = droute_add_many (spi_global_app_data->droute,
1088 "/org/a11y/atspi/accessible",
1090 introspect_children_cb,
1092 (DRouteGetDatumFunction)
1093 spi_global_register_path_to_object);
1096 /* Register all interfaces with droute and set up application accessible db */
1097 spi_initialize_accessible (accpath);
1098 spi_initialize_application (accpath);
1099 spi_initialize_action (accpath);
1100 spi_initialize_collection (accpath);
1101 spi_initialize_component (accpath);
1102 spi_initialize_document (accpath);
1103 spi_initialize_editabletext (accpath);
1104 spi_initialize_hyperlink (accpath);
1105 spi_initialize_hypertext (accpath);
1106 spi_initialize_image (accpath);
1107 spi_initialize_selection (accpath);
1108 spi_initialize_socket (accpath);
1109 spi_initialize_table (accpath);
1110 spi_initialize_table_cell (accpath);
1111 spi_initialize_text (accpath);
1112 spi_initialize_value (accpath);
1114 droute_context_register (spi_global_app_data->droute,
1115 spi_global_app_data->bus);
1117 /* Register methods to send D-Bus signals on certain ATK events */
1119 spi_atk_activate ();
1121 /* Set up filter and match rules to catch signals */
1122 dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.Registry', sender='org.a11y.atspi.Registry'", NULL);
1123 dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.DeviceEventListener', sender='org.a11y.atspi.Registry'", NULL);
1124 dbus_bus_add_match (spi_global_app_data->bus, "type='signal', arg0='org.a11y.atspi.Registry', interface='org.freedesktop.DBus', member='NameOwnerChanged'", NULL);
1125 dbus_connection_add_filter (spi_global_app_data->bus, signal_filter, NULL,
1128 /* Register this app by sending a signal out to AT-SPI registry daemon */
1129 if (!atspi_no_register && (!root || !ATK_IS_PLUG (root)))
1130 _atk_bridge_schedule_application_registration (spi_global_app_data);
1132 get_registered_event_listeners (spi_global_app_data);
1135 atexit (remove_socket);
1136 atexit_added = TRUE;
1138 dbus_error_free (&error);
1143 atk_bridge_adaptor_cleanup (void)
1151 if (!spi_global_app_data)
1154 spi_atk_tidy_windows ();
1155 spi_atk_deregister_event_listeners ();
1157 deregister_application (spi_global_app_data);
1159 if (spi_global_app_data->bus)
1161 dbus_connection_remove_filter (spi_global_app_data->bus, signal_filter, NULL);
1162 droute_context_unregister (spi_global_app_data->droute, spi_global_app_data->bus);
1163 dbus_connection_close (spi_global_app_data->bus);
1164 dbus_connection_unref (spi_global_app_data->bus);
1165 spi_global_app_data->bus = NULL;
1168 for (l = spi_global_app_data->direct_connections; l; l = l->next)
1170 DBusConnection *connection;
1172 connection = l->data;
1174 droute_context_unregister (spi_global_app_data->droute, connection);
1175 droute_unintercept_dbus (connection);
1176 dbus_connection_close (connection);
1177 dbus_connection_unref (connection);
1179 g_list_free (spi_global_app_data->direct_connections);
1180 spi_global_app_data->direct_connections = NULL;
1182 for (ls = clients; ls; ls = ls->next)
1184 g_slist_free (clients);
1187 g_clear_object (&spi_global_cache);
1188 g_clear_object (&spi_global_leasing);
1189 g_clear_object (&spi_global_register);
1191 if (spi_global_app_data->main_context)
1192 g_main_context_unref (spi_global_app_data->main_context);
1194 droute_free (spi_global_app_data->droute);
1196 g_free (spi_global_app_data);
1197 spi_global_app_data = NULL;
1202 /*---------------------------------------------------------------------------*/
1204 static gchar *name_match_tmpl =
1205 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged', arg0='%s'";
1208 spi_atk_add_client (const char *bus_name)
1213 for (l = clients; l; l = l->next)
1215 if (!g_strcmp0 (l->data, bus_name))
1219 spi_atk_activate ();
1220 clients = g_slist_append (clients, g_strdup (bus_name));
1221 match = g_strdup_printf (name_match_tmpl, bus_name);
1222 dbus_bus_add_match (spi_global_app_data->bus, match, NULL);
1227 spi_atk_remove_client (const char *bus_name)
1235 next_node = l->next;
1237 if (!g_strcmp0 (l->data, bus_name))
1239 gchar *match = g_strdup_printf (name_match_tmpl, l->data);
1240 dbus_bus_remove_match (spi_global_app_data->bus, match, NULL);
1243 clients = g_slist_delete_link (clients, l);
1245 spi_atk_deregister_event_listeners ();
1254 spi_atk_add_interface (DRoutePath *path,
1256 const char *introspect,
1257 const DRouteMethod *methods,
1258 const DRouteProperty *properties)
1260 droute_path_add_interface (path, name, introspect, methods, properties);
1264 if (!spi_global_app_data->property_hash)
1265 spi_global_app_data->property_hash = g_hash_table_new_full (g_str_hash,
1268 g_hash_table_insert (spi_global_app_data->property_hash, g_strdup (name),
1269 (gpointer) properties);
1272 /*END------------------------------------------------------------------------*/