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 _atk_bridge_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);
442 /*---------------------------------------------------------------------------*/
447 if (!spi_global_app_data)
450 if (spi_global_app_data->app_bus_addr &&
451 !strncmp (spi_global_app_data->app_bus_addr, "unix:path=", 10))
453 unlink (spi_global_app_data->app_bus_addr + 10);
454 g_free (spi_global_app_data->app_bus_addr);
455 spi_global_app_data->app_bus_addr = NULL;
458 if (spi_global_app_data->app_tmp_dir)
460 rmdir (spi_global_app_data->app_tmp_dir);
461 g_free (spi_global_app_data->app_tmp_dir);
462 spi_global_app_data->app_tmp_dir = NULL;
467 deregister_application (SpiBridge * app)
469 DBusMessage *message;
470 DBusMessageIter iter;
473 if (spi_global_app_data->registration_pending)
475 g_source_remove (spi_global_app_data->registration_pending);
476 spi_global_app_data->registration_pending = 0;
480 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
481 ATSPI_DBUS_PATH_REGISTRY,
482 ATSPI_DBUS_INTERFACE_REGISTRY,
483 "DeregisterApplication");
484 dbus_message_set_no_reply (message, TRUE);
486 uname = dbus_bus_get_unique_name (app->bus);
488 dbus_message_iter_init_append (message, &iter);
489 dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
490 dbus_connection_send (app->bus, message, NULL);
492 dbus_message_unref (message);
496 g_free (app->desktop_name);
497 app->desktop_name = NULL;
498 g_free (app->desktop_path);
499 app->desktop_path = NULL;
502 /*---------------------------------------------------------------------------*/
504 /*---------------------------------------------------------------------------*/
506 static AtkPlugClass *plug_class;
507 static AtkSocketClass *socket_class;
510 get_plug_id (AtkPlug * plug)
512 const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
514 GString *str = g_string_new (NULL);
516 path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
517 g_string_printf (str, "%s:%s", uname, path);
519 return g_string_free (str, FALSE);
523 socket_ref_state_set (AtkObject *accessible)
525 char *child_name, *child_path;
526 AtkSocket *socket = ATK_SOCKET (accessible);
530 DBusMessage *message, *reply;
531 DBusMessageIter iter, iter_array;
534 set = atk_state_set_new ();
536 if (!socket->embedded_plug_id)
539 child_name = g_strdup (socket->embedded_plug_id);
542 child_path = g_utf8_strchr (child_name + 1, -1, ':');
548 *(child_path++) = '\0';
549 message = dbus_message_new_method_call (child_name, child_path, ATSPI_DBUS_INTERFACE_ACCESSIBLE, "GetState");
551 reply = dbus_connection_send_with_reply_and_block (spi_global_app_data->bus, message, 1, NULL);
552 dbus_message_unref (message);
555 if (strcmp (dbus_message_get_signature (reply), "au") != 0)
557 dbus_message_unref (reply);
561 dbus_message_iter_init (reply, &iter);
562 dbus_message_iter_recurse (&iter, &iter_array);
565 dbus_message_iter_get_basic (&iter_array, &v);
566 for (j = 0; j < 32; j++)
570 AtkState state = spi_atk_state_from_spi_state ((count << 5) + j);
571 atk_state_set_add_state (set, state);
576 while (dbus_message_iter_next (&iter_array));
577 dbus_message_unref (reply);
582 socket_embed_hook (AtkSocket * socket, const gchar * plug_id)
584 g_return_if_fail (spi_global_register != NULL);
586 AtkObject *accessible = ATK_OBJECT(socket);
587 gchar *plug_name, *plug_path;
588 AtkObjectClass *klass;
590 /* Force registration */
591 gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
592 /* Let the plug know that it has been embedded */
593 plug_name = g_strdup (plug_id);
599 plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
602 DBusMessage *message;
603 *(plug_path++) = '\0';
604 message = dbus_message_new_method_call (plug_name, plug_path, ATSPI_DBUS_INTERFACE_SOCKET, "Embedded");
605 dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
606 dbus_connection_send (spi_global_app_data->bus, message, NULL);
611 klass = ATK_OBJECT_GET_CLASS (accessible);
612 klass->ref_state_set = socket_ref_state_set;
616 install_plug_hooks ()
620 data = g_type_class_ref (ATK_TYPE_PLUG);
621 plug_class = ATK_PLUG_CLASS (data);
622 data = g_type_class_ref (ATK_TYPE_SOCKET);
623 socket_class = ATK_SOCKET_CLASS (data);
624 plug_class->get_object_id = get_plug_id;
625 socket_class->embed = socket_embed_hook;
629 get_ancestral_uid (guint pid)
635 gboolean got_ppid = 0;
636 gboolean got_uid = 0;
638 sprintf (buf, "/proc/%d/status", pid);
639 fp = fopen (buf, "r");
642 while ((!got_ppid || !got_uid) && fgets (buf, sizeof (buf), fp))
644 if (sscanf (buf, "PPid:\t%d", &ppid) == 1)
646 else if (sscanf (buf, "Uid:\t%d", &uid) == 1)
651 if (!got_ppid || !got_uid)
655 if (ppid == 0 || ppid == 1)
657 return get_ancestral_uid (ppid);
661 user_check (DBusConnection *bus, unsigned long uid, void *data)
663 if (uid == getuid () || uid == geteuid ())
667 guint ancestor = get_ancestral_uid (getpid ());
668 return (ancestor == uid || ancestor == 1 || ancestor == 0);
674 new_connection_cb (DBusServer *server, DBusConnection *con, void *data)
676 dbus_connection_set_unix_user_function (con, user_check, NULL, NULL);
677 dbus_connection_ref(con);
678 atspi_dbus_connection_setup_with_g_main(con, spi_context);
679 droute_intercept_dbus (con);
680 droute_context_register (spi_global_app_data->droute, con);
682 spi_global_app_data->direct_connections = g_list_append (spi_global_app_data->direct_connections, con);
686 gchar *atspi_dbus_name = NULL;
687 static gboolean atspi_no_register = FALSE;
689 static GOptionEntry atspi_option_entries[] = {
690 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
691 "D-Bus bus name to register as", NULL},
692 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
693 "Do not register with Registry Daemon", NULL},
698 introspect_children_cb (const char *path, void *data)
700 if (!strcmp (path, "/org/a11y/atspi/accessible"))
702 return g_strdup ("<node name=\"root\"/>\n");
703 /* TODO: Should we place the whole hierarchy here? */
709 handle_event_listener_registered (DBusConnection *bus, DBusMessage *message,
712 DBusMessageIter iter;
713 const char *signature = dbus_message_get_signature (message);
715 if (strcmp (signature, "ssas") != 0 &&
716 strcmp (signature, "ss") != 0)
718 g_warning ("got RegisterEvent with invalid signature '%s'", signature);
722 dbus_message_iter_init (message, &iter);
723 add_event_from_iter (&iter);
727 free_property_definition (void *data)
729 AtspiPropertyDefinition *pd = data;
736 remove_events (const char *bus_name, const char *event)
741 remove_data = g_strsplit (event, ":", 3);
747 for (list = spi_global_app_data->events; list;)
749 event_data *evdata = list->data;
750 if (!g_strcmp0 (evdata->bus_name, bus_name) &&
751 spi_event_is_subtype (evdata->data, remove_data))
754 GList *events = spi_global_app_data->events;
756 g_strfreev (evdata->data);
757 g_free (evdata->bus_name);
758 g_slist_free_full (evdata->properties, free_property_definition);
762 spi_global_app_data->events = g_list_delete_link (events, list);
771 g_strfreev (remove_data);
775 handle_event_listener_deregistered (DBusConnection *bus, DBusMessage *message,
781 if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
782 DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
785 remove_events (sender, name);
789 handle_device_listener_registered (DBusConnection *bus, DBusMessage *message,
793 DBusMessageIter iter, iter_struct;
795 if (strncmp (dbus_message_get_signature (message), "(s", 2) != 0)
797 g_warning ("atk-bridge: handle_device_listener_register: unknown signature");
801 dbus_message_iter_init (message, &iter);
802 dbus_message_iter_recurse (&iter, &iter_struct);
803 dbus_message_iter_get_basic (&iter_struct, &sender);
804 spi_atk_add_client (sender);
807 static DBusHandlerResult
808 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
810 const char *interface = dbus_message_get_interface (message);
811 const char *member = dbus_message_get_member (message);
812 DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
813 static gboolean registry_lost = FALSE;
815 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
816 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
818 if (!strcmp (interface, ATSPI_DBUS_INTERFACE_REGISTRY))
820 result = DBUS_HANDLER_RESULT_HANDLED;
821 if (!strcmp (member, "EventListenerRegistered"))
822 handle_event_listener_registered (bus, message, user_data);
823 else if (!strcmp (member, "EventListenerDeregistered"))
824 handle_event_listener_deregistered (bus, message, user_data);
826 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
828 else if (!strcmp (interface, ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER))
830 result = DBUS_HANDLER_RESULT_HANDLED;
831 if (!strcmp (member, "KeystrokeListenerRegistered"))
832 handle_device_listener_registered (bus, message, user_data);
833 else if (!strcmp (member, "DeviceListenerRegistered"))
834 handle_device_listener_registered (bus, message, user_data);
836 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
839 if (!g_strcmp0(interface, DBUS_INTERFACE_DBUS) &&
840 !g_strcmp0(member, "NameOwnerChanged"))
842 char *name, *old, *new;
843 if (dbus_message_get_args (message, NULL,
844 DBUS_TYPE_STRING, &name,
845 DBUS_TYPE_STRING, &old,
846 DBUS_TYPE_STRING, &new,
849 if (!strcmp (name, "org.a11y.atspi.Registry"))
851 if (registry_lost && !old[0])
853 _atk_bridge_register_application (spi_global_app_data);
854 registry_lost = FALSE;
857 registry_lost = TRUE;
859 else if (*old != '\0' && *new == '\0')
860 spi_atk_remove_client (old);
868 spi_atk_create_socket (SpiBridge *app)
873 const gchar *user_runtime_dir = g_get_user_runtime_dir ();
875 if (g_mkdir_with_parents (user_runtime_dir, 0700) != 0)
880 app->app_tmp_dir = g_build_filename (user_runtime_dir,
881 "at-spi2-XXXXXX", NULL);
882 if (!g_mkdtemp (app->app_tmp_dir))
884 g_free (app->app_tmp_dir);
885 app->app_tmp_dir = NULL;
890 if (app->app_tmp_dir)
891 app->app_bus_addr = g_strdup_printf ("unix:path=%s/socket", app->app_tmp_dir);
893 app->app_bus_addr = g_strdup_printf ("unix:path=%s/at-spi2-socket-%d",
894 user_runtime_dir, getpid ());
896 if (!spi_global_app_data->app_bus_addr)
899 dbus_error_init(&error);
900 server = dbus_server_listen(spi_global_app_data->app_bus_addr, &error);
903 g_warning ("atk-bridge: Couldn't listen on dbus server: %s", error.message);
904 dbus_error_free (&error);
905 spi_global_app_data->app_bus_addr [0] = '\0';
909 atspi_dbus_server_setup_with_g_main(server, spi_context);
910 dbus_server_set_new_connection_function(server, new_connection_cb, NULL, NULL);
912 spi_global_app_data->server = server;
919 * Checks the status of the environment variables
921 * At this moment it only checks NO_AT_BRIDGE
923 * Returns TRUE if there isn't anything on the environment preventing
924 * you to load the bridge, FALSE otherwise
931 envvar = g_getenv ("NO_AT_BRIDGE");
933 if (envvar && atoi (envvar) == 1)
942 DRoutePath *treepath;
944 spi_atk_register_event_listeners ();
945 if (!spi_global_cache)
947 spi_global_cache = g_object_new (SPI_CACHE_TYPE, NULL);
948 treepath = droute_add_one (spi_global_app_data->droute,
949 "/org/a11y/atspi/cache", spi_global_cache);
953 g_warning ("atk-bridge: Error in droute_add_one(). Already running?");
956 spi_initialize_cache (treepath);
957 if (spi_global_app_data->bus)
958 droute_path_register (treepath, spi_global_app_data->bus);
963 * atk_bridge_adaptor_init: initializes the atk bridge adaptor
965 * The following needs to be initialized.
967 * - DRoute for routing message to their accessible objects.
968 * - Event handlers for emmitting signals on specific ATK events.
969 * - setup the bus for p2p communication
970 * - Application registration with the AT-SPI registry.
972 * Returns: 0 if the bridge gets or was already initialized
973 * succesfully, -1 otherwise
976 atk_bridge_adaptor_init (gint * argc, gchar ** argv[])
982 gboolean load_bridge;
985 load_bridge = check_envvar ();
986 if (inited && !load_bridge)
987 g_warning ("ATK Bridge is disabled but a11y has already been enabled.");
996 root = atk_get_root ();
997 g_warn_if_fail (root);
1004 /* Parse command line options */
1005 opt = g_option_context_new (NULL);
1006 g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
1007 g_option_context_set_ignore_unknown_options (opt, TRUE);
1008 if (!g_option_context_parse (opt, argc, argv, &err))
1010 g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
1013 g_option_context_free (opt);
1015 /* Allocate global data and do ATK initializations */
1016 spi_global_app_data = g_new0 (SpiBridge, 1);
1017 spi_global_app_data->root = g_object_ref (root);
1018 spi_global_app_data->desktop_name = g_strdup (ATSPI_DBUS_NAME_REGISTRY);
1019 spi_global_app_data->desktop_path = g_strdup (ATSPI_DBUS_PATH_ROOT);
1021 /* Set up D-Bus connection and register bus name */
1022 dbus_error_init (&error);
1023 spi_global_app_data->bus = atspi_get_a11y_bus ();
1024 if (!spi_global_app_data->bus)
1026 g_free (spi_global_app_data);
1027 spi_global_app_data = NULL;
1032 if (atspi_dbus_name != NULL)
1034 if (dbus_bus_request_name
1035 (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
1037 g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
1042 ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
1047 spi_global_app_data->main_context = g_main_context_new ();
1049 atspi_dbus_connection_setup_with_g_main (spi_global_app_data->bus, NULL);
1051 /* Hook our plug-and socket functions */
1052 install_plug_hooks ();
1055 * Create the leasing, register and cache objects.
1056 * The order is important here, the cache depends on the
1059 spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
1060 spi_global_leasing = g_object_new (SPI_LEASING_TYPE, NULL);
1062 /* Register droute for routing AT-SPI messages */
1063 spi_global_app_data->droute =
1066 accpath = droute_add_many (spi_global_app_data->droute,
1067 "/org/a11y/atspi/accessible",
1069 introspect_children_cb,
1071 (DRouteGetDatumFunction)
1072 spi_global_register_path_to_object);
1075 /* Register all interfaces with droute and set up application accessible db */
1076 spi_initialize_accessible (accpath);
1077 spi_initialize_application (accpath);
1078 spi_initialize_action (accpath);
1079 spi_initialize_collection (accpath);
1080 spi_initialize_component (accpath);
1081 spi_initialize_document (accpath);
1082 spi_initialize_editabletext (accpath);
1083 spi_initialize_hyperlink (accpath);
1084 spi_initialize_hypertext (accpath);
1085 spi_initialize_image (accpath);
1086 spi_initialize_selection (accpath);
1087 spi_initialize_socket (accpath);
1088 spi_initialize_table (accpath);
1089 spi_initialize_table_cell (accpath);
1090 spi_initialize_text (accpath);
1091 spi_initialize_value (accpath);
1093 droute_context_register (spi_global_app_data->droute,
1094 spi_global_app_data->bus);
1096 /* Register methods to send D-Bus signals on certain ATK events */
1098 spi_atk_activate ();
1100 /* Set up filter and match rules to catch signals */
1101 dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.Registry', sender='org.a11y.atspi.Registry'", NULL);
1102 dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.DeviceEventListener', sender='org.a11y.atspi.Registry'", NULL);
1103 dbus_bus_add_match (spi_global_app_data->bus, "type='signal', arg0='org.a11y.atspi.Registry', interface='org.freedesktop.DBus', member='NameOwnerChanged'", NULL);
1104 dbus_connection_add_filter (spi_global_app_data->bus, signal_filter, NULL,
1107 /* Register this app by sending a signal out to AT-SPI registry daemon */
1108 if (!atspi_no_register && (!root || !ATK_IS_PLUG (root)) &&
1109 !spi_global_app_data->registration_pending)
1110 spi_global_app_data->registration_pending = spi_idle_add (_atk_bridge_register_application, spi_global_app_data);
1112 get_registered_event_listeners (spi_global_app_data);
1115 atexit (remove_socket);
1116 atexit_added = TRUE;
1118 dbus_error_free (&error);
1123 atk_bridge_adaptor_cleanup (void)
1131 if (!spi_global_app_data)
1134 spi_atk_tidy_windows ();
1135 spi_atk_deregister_event_listeners ();
1137 deregister_application (spi_global_app_data);
1139 if (spi_global_app_data->bus)
1141 dbus_connection_remove_filter (spi_global_app_data->bus, signal_filter, NULL);
1142 droute_context_unregister (spi_global_app_data->droute, spi_global_app_data->bus);
1143 dbus_connection_close (spi_global_app_data->bus);
1144 dbus_connection_unref (spi_global_app_data->bus);
1145 spi_global_app_data->bus = NULL;
1148 for (l = spi_global_app_data->direct_connections; l; l = l->next)
1150 DBusConnection *connection;
1152 connection = l->data;
1154 droute_context_unregister (spi_global_app_data->droute, connection);
1155 droute_unintercept_dbus (connection);
1156 dbus_connection_close (connection);
1157 dbus_connection_unref (connection);
1159 g_list_free (spi_global_app_data->direct_connections);
1160 spi_global_app_data->direct_connections = NULL;
1162 for (ls = clients; ls; ls = ls->next)
1164 g_slist_free (clients);
1167 g_clear_object (&spi_global_cache);
1168 g_clear_object (&spi_global_leasing);
1169 g_clear_object (&spi_global_register);
1171 if (spi_global_app_data->main_context)
1172 g_main_context_unref (spi_global_app_data->main_context);
1174 droute_free (spi_global_app_data->droute);
1176 g_free (spi_global_app_data);
1177 spi_global_app_data = NULL;
1182 /*---------------------------------------------------------------------------*/
1184 static gchar *name_match_tmpl =
1185 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged', arg0='%s'";
1188 spi_atk_add_client (const char *bus_name)
1193 for (l = clients; l; l = l->next)
1195 if (!g_strcmp0 (l->data, bus_name))
1199 spi_atk_activate ();
1200 clients = g_slist_append (clients, g_strdup (bus_name));
1201 match = g_strdup_printf (name_match_tmpl, bus_name);
1202 dbus_bus_add_match (spi_global_app_data->bus, match, NULL);
1207 spi_atk_remove_client (const char *bus_name)
1215 next_node = l->next;
1217 if (!g_strcmp0 (l->data, bus_name))
1219 gchar *match = g_strdup_printf (name_match_tmpl, l->data);
1220 dbus_bus_remove_match (spi_global_app_data->bus, match, NULL);
1223 clients = g_slist_delete_link (clients, l);
1225 spi_atk_deregister_event_listeners ();
1234 spi_atk_add_interface (DRoutePath *path,
1236 const char *introspect,
1237 const DRouteMethod *methods,
1238 const DRouteProperty *properties)
1240 droute_path_add_interface (path, name, introspect, methods, properties);
1244 if (!spi_global_app_data->property_hash)
1245 spi_global_app_data->property_hash = g_hash_table_new_full (g_str_hash,
1248 g_hash_table_insert (spi_global_app_data->property_hash, g_strdup (name),
1249 (gpointer) properties);
1252 /*END------------------------------------------------------------------------*/