Code clean-ups
[platform/upstream/at-spi2-core.git] / atspi / atspi-misc.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  * Copyright 2010, 2011 Novell, Inc.
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 /*
26  *
27  * Basic SPI initialization and event loop function prototypes
28  *
29  */
30
31 #include "atspi-private.h"
32 #include "X11/Xlib.h"
33 #include "atspi-gmain.h"
34 #include <stdio.h>
35 #include <string.h>
36
37 static void handle_get_items (DBusPendingCall *pending, void *user_data);
38
39 static DBusConnection *bus = NULL;
40 static GHashTable *live_refs = NULL;
41 static gint method_call_timeout = 800;
42 static gint app_startup_time = 15000;
43
44 GMainLoop *atspi_main_loop;
45 gboolean atspi_no_cache;
46
47 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
48 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
49 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
50 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
51 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
52 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
53 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
54 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
55 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
56 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
57 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
58 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
59 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
60 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
61 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
62 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
63 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
64 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
65 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
66 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
67 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
68 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
69 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
70
71 static const char *interfaces[] =
72 {
73   ATSPI_DBUS_INTERFACE_ACCESSIBLE,
74   ATSPI_DBUS_INTERFACE_ACTION,
75   ATSPI_DBUS_INTERFACE_APPLICATION,
76   ATSPI_DBUS_INTERFACE_COLLECTION,
77   ATSPI_DBUS_INTERFACE_COMPONENT,
78   ATSPI_DBUS_INTERFACE_DOCUMENT,
79   ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
80   ATSPI_DBUS_INTERFACE_HYPERLINK,
81   ATSPI_DBUS_INTERFACE_HYPERTEXT,
82   ATSPI_DBUS_INTERFACE_IMAGE,
83   "org.a11y.atspi.LoginHelper",
84   ATSPI_DBUS_INTERFACE_SELECTION,
85   ATSPI_DBUS_INTERFACE_TABLE,
86   ATSPI_DBUS_INTERFACE_TEXT,
87   ATSPI_DBUS_INTERFACE_VALUE,
88   NULL
89 };
90
91 gint
92 _atspi_get_iface_num (const char *iface)
93 {
94   /* TODO: Use a binary search or hash to improve performance */
95   int i;
96
97   for (i = 0; interfaces[i]; i++)
98   {
99     if (!strcmp(iface, interfaces[i])) return i;
100   }
101   return -1;
102 }
103
104 static GHashTable *
105 get_live_refs (void)
106 {
107   if (!live_refs) 
108     {
109       live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
110     }
111   return live_refs;
112 }
113
114 /* TODO: Add an application parameter */
115 DBusConnection *
116 _atspi_bus ()
117 {
118   if (!bus)
119     atspi_init ();
120   if (!bus)
121     g_error ("AT-SPI: COuldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
122   return bus;
123 }
124
125 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
126
127 static void
128 cleanup ()
129 {
130   GHashTable *refs;
131
132   refs = live_refs;
133   live_refs = NULL;
134   if (refs)
135     {
136       g_hash_table_destroy (refs);
137     }
138 }
139
140 static gboolean atspi_inited = FALSE;
141
142 static GHashTable *app_hash = NULL;
143
144 static void
145 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
146 {
147   AtspiApplication *app = user_data;
148   DBusMessage *reply = dbus_pending_call_steal_reply (pending);
149   DBusMessage *message;
150   const char *address;
151   DBusPendingCall *new_pending;
152
153   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
154   {
155     if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
156                                DBUS_TYPE_INVALID))
157     {
158       DBusError error;
159       dbus_error_init (&error);
160       DBusConnection *bus = dbus_connection_open (address, &error);
161       if (bus)
162       {
163         if (app->bus)
164           dbus_connection_unref (app->bus);
165         app->bus = bus;
166       }
167     }
168   }
169   dbus_message_unref (reply);
170   dbus_pending_call_unref (pending);
171
172   if (!app->bus)
173     return; /* application has gone away / been disposed */
174
175   message = dbus_message_new_method_call (app->bus_name,
176                                           "/org/a11y/atspi/cache",
177                                           atspi_interface_cache, "GetItems");
178
179    dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
180   dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
181   dbus_message_unref (message);
182 }
183
184 static AtspiApplication *
185 get_application (const char *bus_name)
186 {
187   AtspiApplication *app = NULL;
188   char *bus_name_dup;
189   DBusMessage *message;
190   DBusError error;
191   DBusPendingCall *pending = NULL;
192
193   if (!app_hash)
194   {
195     app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
196     if (!app_hash) return NULL;
197   }
198   app = g_hash_table_lookup (app_hash, bus_name);
199   if (app) return app;
200   bus_name_dup = g_strdup (bus_name);
201   if (!bus_name_dup) return NULL;
202   // TODO: change below to something that will send state-change:defunct notification if necessary */
203   app = _atspi_application_new (bus_name);
204   if (!app) return NULL;
205   app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
206   app->bus = dbus_connection_ref (_atspi_bus ());
207   gettimeofday (&app->time_added, NULL);
208   app->cache = ATSPI_CACHE_UNDEFINED;
209   g_hash_table_insert (app_hash, bus_name_dup, app);
210   dbus_error_init (&error);
211   message = dbus_message_new_method_call (bus_name, atspi_path_root,
212                                           atspi_interface_application, "GetApplicationBusAddress");
213
214    dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
215   dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
216   dbus_message_unref (message);
217   return app;
218 }
219
220 static AtspiAccessible *
221 ref_accessible (const char *app_name, const char *path)
222 {
223   AtspiApplication *app;
224   AtspiAccessible *a;
225
226   if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
227     return NULL;
228
229   app = get_application (app_name);
230
231   if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
232   {
233     if (!app->root)
234     {
235       app->root = _atspi_accessible_new (app, atspi_path_root);
236       app->root->accessible_parent = atspi_get_desktop (0);
237     }
238     return g_object_ref (app->root);
239   }
240
241   a = g_hash_table_lookup (app->hash, path);
242   if (a)
243   {
244     return g_object_ref (a);
245   }
246   a = _atspi_accessible_new (app, path);
247   if (!a)
248     return NULL;
249   g_hash_table_insert (app->hash, g_strdup (a->parent.path), a);
250   g_object_ref (a);     /* for the hash */
251   return a;
252 }
253
254 static AtspiHyperlink *
255 ref_hyperlink (const char *app_name, const char *path)
256 {
257   AtspiApplication *app = get_application (app_name);
258   AtspiHyperlink *hyperlink;
259
260   if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
261     return NULL;
262
263   hyperlink = g_hash_table_lookup (app->hash, path);
264   if (hyperlink)
265   {
266     return g_object_ref (hyperlink);
267   }
268   hyperlink = _atspi_hyperlink_new (app, path);
269   g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
270   /* TODO: This should be a weak ref */
271   g_object_ref (hyperlink);     /* for the hash */
272   return hyperlink;
273 }
274
275 typedef struct
276 {
277   char *path;
278   char *parent;
279   GArray *children;
280   GArray *interfaces;
281   char *name;
282   dbus_uint32_t role;
283   char *description;
284   GArray *state_bitflags;
285 } CACHE_ADDITION;
286
287 static DBusHandlerResult
288 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
289 {
290   const char *sender = dbus_message_get_sender (message);
291   AtspiApplication *app;
292   const char *path;
293   DBusMessageIter iter, iter_struct;
294   const char *signature = dbus_message_get_signature (message);
295   AtspiAccessible *a;
296
297   if (strcmp (signature, "(so)") != 0)
298   {
299     g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
300     return DBUS_HANDLER_RESULT_HANDLED;
301   }
302
303   dbus_message_iter_init (message, &iter);
304   dbus_message_iter_recurse (&iter, &iter_struct);
305   dbus_message_iter_get_basic (&iter_struct, &sender);
306   dbus_message_iter_next (&iter_struct);
307   dbus_message_iter_get_basic (&iter_struct, &path);
308   app = get_application (sender);
309   a = ref_accessible (sender, path);
310   if (!a)
311     return DBUS_HANDLER_RESULT_HANDLED;
312   g_object_run_dispose (G_OBJECT (a));
313   g_hash_table_remove (app->hash, a->parent.path);
314   g_object_unref (a);   /* unref our own ref */
315   return DBUS_HANDLER_RESULT_HANDLED;
316 }
317
318 static gboolean
319 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
320 {
321   DBusError error;
322
323   dbus_error_init (&error);
324   AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
325   if (obj)
326   {
327     a->children = g_list_append (a->children, obj);
328     return TRUE;
329   }
330   else
331   {
332     g_warning ("AT-SPI: Error calling getRoot for %s: %s", bus_name, error.message);
333   }
334   return FALSE;
335 }
336
337 static void
338 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
339 {
340   AtspiEvent e;
341
342   memset (&e, 0, sizeof (e));
343   e.type = (add? "object:children-changed:add": "object:children-changed:remove");
344   e.source = parent;
345   e.detail1 = g_list_index (parent->children, child);
346   e.detail2 = 0;
347   _atspi_send_event (&e);
348 }
349
350 static void
351 unref_object_and_descendants (AtspiAccessible *obj)
352 {
353   GList *l;
354
355   for (l = obj->children; l; l = l->next)
356   {
357     unref_object_and_descendants (l->data);
358   }
359   g_object_unref (obj);
360 }
361
362 static gboolean
363 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
364 {
365   GList *l;
366   AtspiAccessible *child;
367
368   for (l = a->children; l; l = l->next)
369   {
370     child = l->data;
371     if (!strcmp (bus_name, child->parent.app->bus_name)) break;
372   }
373   if (!l)
374   {
375     return FALSE;
376   }
377   send_children_changed (a, child, FALSE);
378   a->children = g_list_remove (a->children, child);
379   unref_object_and_descendants (child);
380   return TRUE;
381 }
382
383 static AtspiAccessible *desktop;
384
385 void
386 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
387 {
388   DBusMessageIter iter_struct;
389
390   dbus_message_iter_recurse (iter, &iter_struct);
391   dbus_message_iter_get_basic (&iter_struct, app_name);
392   dbus_message_iter_next (&iter_struct);
393   dbus_message_iter_get_basic (&iter_struct, path);
394   dbus_message_iter_next (iter);
395 }
396
397 static void
398 add_accessible_from_iter (DBusMessageIter *iter)
399 {
400   DBusMessageIter iter_struct, iter_array;
401   const char *app_name, *path;
402   AtspiAccessible *accessible;
403   const char *name, *description;
404   dbus_uint32_t role;
405
406   dbus_message_iter_recurse (iter, &iter_struct);
407
408   /* get accessible */
409   get_reference_from_iter (&iter_struct, &app_name, &path);
410   accessible = ref_accessible (app_name, path);
411   if (!accessible)
412     return;
413
414   /* Get application: TODO */
415   dbus_message_iter_next (&iter_struct);
416
417   /* get parent */
418   get_reference_from_iter (&iter_struct, &app_name, &path);
419   if (accessible->accessible_parent)
420     g_object_unref (accessible->accessible_parent);
421   accessible->accessible_parent = ref_accessible (app_name, path);
422
423   /* Get children */
424   while (accessible->children)
425   {
426     g_object_unref (accessible->children->data);
427     accessible->children = g_list_remove (accessible->children, accessible->children->data);
428   }
429   dbus_message_iter_recurse (&iter_struct, &iter_array);
430   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
431   {
432     AtspiAccessible *child;
433     get_reference_from_iter (&iter_array, &app_name, &path);
434     child = ref_accessible (app_name, path);
435     accessible->children = g_list_append (accessible->children, child);
436   }
437
438   /* interfaces */
439   dbus_message_iter_next (&iter_struct);
440   _atspi_dbus_set_interfaces (accessible, &iter_struct);
441   dbus_message_iter_next (&iter_struct);
442
443   /* name */
444   if (accessible->name)
445     g_free (accessible->name);
446   dbus_message_iter_get_basic (&iter_struct, &name);
447   accessible->name = g_strdup (name);
448   dbus_message_iter_next (&iter_struct);
449
450   /* role */
451   dbus_message_iter_get_basic (&iter_struct, &role);
452   accessible->role = role;
453   dbus_message_iter_next (&iter_struct);
454
455   /* description */
456   if (accessible->description)
457     g_free (accessible->description);
458   dbus_message_iter_get_basic (&iter_struct, &description);
459   accessible->description = g_strdup (description);
460   dbus_message_iter_next (&iter_struct);
461
462   _atspi_dbus_set_state (accessible, &iter_struct);
463   dbus_message_iter_next (&iter_struct);
464
465   _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
466                                ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
467   if (!atspi_state_set_contains (accessible->states,
468                                        ATSPI_STATE_MANAGES_DESCENDANTS))
469     _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
470
471   /* This is a bit of a hack since the cache holds a ref, so we don't need
472    * the one provided for us anymore */
473   g_object_unref (accessible);
474 }
475
476 static void
477 handle_get_items (DBusPendingCall *pending, void *user_data)
478 {
479   DBusMessage *reply = dbus_pending_call_steal_reply (pending);
480   DBusMessageIter iter, iter_array;
481
482   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
483   {
484     const char *sender = dbus_message_get_sender (reply);
485     const char *error = NULL;
486     dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
487                            DBUS_TYPE_INVALID);
488     g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
489     dbus_message_unref (reply);
490     dbus_pending_call_unref (pending);
491     return;
492   }
493
494   dbus_message_iter_init (reply, &iter);
495   dbus_message_iter_recurse (&iter, &iter_array);
496   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
497   {
498     add_accessible_from_iter (&iter_array);
499     dbus_message_iter_next (&iter_array);
500   }
501   dbus_message_unref (reply);
502   dbus_pending_call_unref (pending);
503 }
504
505 /* TODO: Do we stil need this function? */
506 static AtspiAccessible *
507 ref_accessible_desktop (AtspiApplication *app)
508 {
509   DBusError error;
510   DBusMessage *message, *reply;
511   DBusMessageIter iter, iter_array;
512   gchar *bus_name_dup;
513
514   if (desktop)
515   {
516     g_object_ref (desktop);
517     return desktop;
518   }
519   desktop = _atspi_accessible_new (app, atspi_path_root);
520   if (!desktop)
521   {
522     return NULL;
523   }
524   g_hash_table_insert (app->hash, desktop->parent.path, desktop);
525   g_object_ref (desktop);       /* for the hash */
526   desktop->name = g_strdup ("main");
527   dbus_error_init (&error);
528   message = dbus_message_new_method_call (atspi_bus_registry,
529         atspi_path_root,
530         atspi_interface_accessible,
531         "GetChildren");
532   if (!message)
533     return NULL;
534   reply = _atspi_dbus_send_with_reply_and_block (message, NULL);
535   if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
536   {
537     g_warning ("Couldn't get application list: %s", error.message);
538     if (reply)
539       dbus_message_unref (reply);
540     return NULL;
541   }
542   dbus_message_iter_init (reply, &iter);
543   dbus_message_iter_recurse (&iter, &iter_array);
544   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
545   {
546     const char *app_name, *path;
547     get_reference_from_iter (&iter_array, &app_name, &path);
548     add_app_to_desktop (desktop, app_name);
549   }
550   dbus_message_unref (reply);
551
552   /* Record the alternate name as an alias for org.a11y.atspi.Registry */
553   bus_name_dup = g_strdup (dbus_message_get_sender (reply));
554   if (bus_name_dup)
555     g_hash_table_insert (app_hash, bus_name_dup, app);
556
557   return desktop;
558 }
559
560 AtspiAccessible *
561 _atspi_ref_accessible (const char *app, const char *path)
562 {
563   AtspiApplication *a = get_application (app);
564   if (!a) return NULL;
565   if ( APP_IS_REGISTRY(a))
566   {
567     return a->root = ref_accessible_desktop (a);
568   }
569   return ref_accessible (app, path);
570 }
571
572 AtspiAccessible *
573 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
574 {
575   DBusMessageIter iter;
576   AtspiAccessible *retval = NULL;
577   const char *signature;
578
579   if (!message)
580     return NULL;
581
582   signature = dbus_message_get_signature (message);
583   if (!strcmp (signature, "(so)"))
584   {
585     dbus_message_iter_init (message, &iter);
586     retval =  _atspi_dbus_return_accessible_from_iter (&iter);
587   }
588   else
589   {
590     g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
591   }
592   dbus_message_unref (message);
593   return retval;
594 }
595
596 AtspiAccessible *
597 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
598 {
599   const char *app_name, *path;
600
601   get_reference_from_iter (iter, &app_name, &path);
602   return ref_accessible (app_name, path);
603 }
604
605 AtspiHyperlink *
606 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
607 {
608   DBusMessageIter iter;
609   AtspiHyperlink *retval = NULL;
610   const char *signature;
611    
612   if (!message)
613     return NULL;
614
615   signature = dbus_message_get_signature (message);
616   if (!strcmp (signature, "(so)"))
617   {
618     dbus_message_iter_init (message, &iter);
619     retval =  _atspi_dbus_return_hyperlink_from_iter (&iter);
620   }
621   else
622   {
623     g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
624   }
625   dbus_message_unref (message);
626   return retval;
627 }
628
629 AtspiHyperlink *
630 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
631 {
632   const char *app_name, *path;
633
634   get_reference_from_iter (iter, &app_name, &path);
635   return ref_hyperlink (app_name, path);
636 }
637
638 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
639
640 static DBusHandlerResult
641 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
642 {
643   DBusMessageIter iter;
644   const char *sender = dbus_message_get_sender (message);
645
646   if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
647   {
648     g_warning ("AT-SPI: AddAccessible with unknown signature %s\n",
649                dbus_message_get_signature (message));
650     return DBUS_HANDLER_RESULT_HANDLED;
651   }
652
653   dbus_message_iter_init (message, &iter);
654   add_accessible_from_iter (&iter);
655   return DBUS_HANDLER_RESULT_HANDLED;
656 }
657
658 typedef struct
659 {
660   DBusConnection *bus;
661   DBusMessage *message;
662   void *data;
663 } BusDataClosure;
664
665 static guint process_deferred_messages_id = -1;
666
667 static void
668 process_deferred_message (BusDataClosure *closure)
669 {
670   int type = dbus_message_get_type (closure->message);
671   const char *interface = dbus_message_get_interface (closure->message);
672
673   if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
674       !strncmp (interface, "org.a11y.atspi.Event.", 21))
675   {
676     _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
677   }
678   if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
679   {
680     _atspi_dbus_handle_DeviceEvent (closure->bus,
681                                    closure->message, closure->data);
682   }
683   if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
684   {
685     handle_add_accessible (closure->bus, closure->message, closure->data);
686   }
687   if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
688   {
689     handle_remove_accessible (closure->bus, closure->message, closure->data);
690   }
691 }
692
693 static GQueue *deferred_messages = NULL;
694
695 gboolean
696 _atspi_process_deferred_messages (gpointer data)
697 {
698   static int in_process_deferred_messages = 0;
699   BusDataClosure *closure;
700
701   if (in_process_deferred_messages)
702     return TRUE;
703   in_process_deferred_messages = 1;
704   while (closure = g_queue_pop_head (deferred_messages))
705   {
706     process_deferred_message (closure);
707     dbus_message_unref (closure->message);
708     dbus_connection_unref (closure->bus);
709     g_free (closure);
710   }
711   /* If data is NULL, assume that we were called from GLib */
712   if (!data)
713     process_deferred_messages_id = -1;
714   in_process_deferred_messages = 0;
715   return FALSE;
716 }
717
718 static DBusHandlerResult
719 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
720 {
721   BusDataClosure *closure = g_new (BusDataClosure, 1);
722
723   closure->bus = dbus_connection_ref (bus);
724   closure->message = dbus_message_ref (message);
725   closure->data = user_data;
726
727   g_queue_push_tail (deferred_messages, closure);
728
729   if (process_deferred_messages_id == -1)
730     process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
731   return DBUS_HANDLER_RESULT_HANDLED;
732 }
733
734 static DBusHandlerResult
735 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
736 {
737   int type = dbus_message_get_type (message);
738   const char *interface = dbus_message_get_interface (message);
739
740   if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
741       !strncmp (interface, "org.a11y.atspi.Event.", 21))
742   {
743     return defer_message (bus, message, data);
744   }
745   if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
746   {
747     return defer_message (bus, message, data);
748   }
749   if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
750   {
751     return defer_message (bus, message, data);
752   }
753   if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
754   {
755     return defer_message (bus, message, data);
756   }
757   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
758 }
759
760 static const char *signal_interfaces[] =
761 {
762   "org.a11y.atspi.Event.Object",
763   "org.a11y.atspi.Event.Window",
764   "org.a11y.atspi.Event.Mouse",
765   "org.a11y.atspi.Event.Terminal",
766   "org.a11y.atspi.Event.Document",
767   "org.a11y.atspi.Event.Focus",
768   NULL
769 };
770
771 /*
772  * Returns a 'canonicalized' value for DISPLAY,
773  * with the screen number stripped off if present.
774  *
775  * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
776  */
777 static const gchar *
778 spi_display_name (void)
779 {
780   static const char *canonical_display_name = NULL;
781   if (!canonical_display_name)
782     {
783       const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
784       if (!display_env)
785         {
786           display_env = g_getenv ("DISPLAY");
787           if (!display_env || !display_env[0])
788             canonical_display_name = ":0";
789           else
790             {
791               gchar *display_p, *screen_p;
792               canonical_display_name = g_strdup (display_env);
793               display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
794               screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
795               if (screen_p && display_p && (screen_p > display_p))
796                 {
797                   *screen_p = '\0';
798                 }
799             }
800         }
801       else
802         {
803           canonical_display_name = display_env;
804         }
805     }
806   return canonical_display_name;
807 }
808
809 /**
810  * atspi_init:
811  *
812  * Connects to the accessibility registry and initializes the SPI.
813  *
814  * Returns: 0 on success, otherwise an integer error code.  
815  **/
816 int
817 atspi_init (void)
818 {
819   DBusError error;
820   char *match;
821   const gchar *no_cache;
822
823   if (atspi_inited)
824     {
825       return 1;
826     }
827
828   atspi_inited = TRUE;
829
830   g_type_init ();
831
832   get_live_refs();
833
834   dbus_error_init (&error);
835   bus = atspi_get_a11y_bus ();
836   if (!bus)
837     return 2;
838   dbus_bus_register (bus, &error);
839   atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
840   dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
841   match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
842   dbus_error_init (&error);
843   dbus_bus_add_match (bus, match, &error);
844   g_free (match);
845   match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
846   dbus_bus_add_match (bus, match, &error);
847   g_free (match);
848   match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
849   dbus_bus_add_match (bus, match, &error);
850   g_free (match);
851   match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
852   dbus_bus_add_match (bus, match, &error);
853   g_free (match);
854   match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
855   dbus_bus_add_match (bus, match, &error);
856   g_free (match);
857
858   no_cache = g_getenv ("ATSPI_NO_CACHE");
859   if (no_cache && g_strcmp0 (no_cache, "0") != 0)
860     atspi_no_cache = TRUE;
861
862   deferred_messages = g_queue_new ();
863
864   return 0;
865 }
866
867 /**
868  * atspi_event_main:
869  *
870  * Starts/enters the main event loop for the AT-SPI services.
871  *
872  * NOTE: This method does not return control; it is exited via a call to
873  * #atspi_event_quit from within an event handler.
874  *
875  **/
876 void
877 atspi_event_main (void)
878 {
879   atspi_main_loop = g_main_loop_new (NULL, FALSE);
880   g_main_loop_run (atspi_main_loop);
881   atspi_main_loop = NULL;
882 }
883
884 /**
885  * atspi_event_quit:
886  *
887  * Quits the last main event loop for the AT-SPI services,
888  * See: #atspi_event_main
889  **/
890 void
891 atspi_event_quit (void)
892 {
893   g_main_loop_quit (atspi_main_loop);
894 }
895
896 /**
897  * atspi_exit:
898  *
899  * Disconnects from #AtspiRegistry instances and releases 
900  * any floating resources. Call only once at exit.
901  *
902  * Returns: 0 if there were no leaks, otherwise other integer values.
903  **/
904 int
905 atspi_exit (void)
906 {
907   int leaked;
908
909   if (!atspi_inited)
910     {
911       return 0;
912     }
913
914   atspi_inited = FALSE;
915
916   if (live_refs)
917     {
918       leaked = g_hash_table_size (live_refs);
919     }
920   else
921     {
922       leaked = 0;
923     }
924
925   cleanup ();
926
927   return leaked;
928 }
929
930 static GSList *hung_processes;
931
932 static void
933 remove_hung_process (DBusPendingCall *pending, void *data)
934 {
935   gchar *bus_name = data;
936
937   hung_processes = g_slist_remove (hung_processes, data);
938   g_free (data);
939   dbus_pending_call_unref (pending);
940 }
941
942 static void
943 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
944 {
945   if (!message && error->name &&
946       !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
947   {
948     GSList *l;
949     DBusMessage *message;
950     gchar *bus_name_dup;
951     DBusPendingCall *pending = NULL;
952     for (l = hung_processes; l; l = l->next)
953       if (!strcmp (l->data, bus_name))
954         return;
955     message = dbus_message_new_method_call (bus_name, "/",
956                                             "org.freedesktop.DBus.Peer",
957                                             "Ping");
958     if (!message)
959       return;
960     dbus_connection_send_with_reply (bus, message, &pending, -1);
961     dbus_message_unref (message);
962     if (!pending)
963       return;
964     bus_name_dup = g_strdup (bus_name);
965     hung_processes = g_slist_append (hung_processes, bus_name_dup);
966     dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
967   }
968 }
969
970 static gboolean
971 connection_is_hung (const char *bus_name)
972 {
973   GSList *l;
974
975   for (l = hung_processes; l; l = l->next)
976     if (!strcmp (l->data, bus_name))
977       return TRUE;
978   return FALSE;
979 }
980
981 static gboolean
982 check_app (AtspiApplication *app, GError **error)
983 {
984   if (!app || !app->bus)
985   {
986     g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
987                           _("The application no longer exists"));
988     return FALSE;
989   }
990
991   if (atspi_main_loop && connection_is_hung (app->bus_name))
992   {
993       g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
994                            "The process appears to be hung.");
995     return FALSE;
996   }
997
998   return TRUE;
999 }
1000
1001 static void
1002 set_timeout (AtspiApplication *app)
1003 {
1004   struct timeval tv;
1005   int diff;
1006
1007   if (app && app_startup_time > 0)
1008   {
1009     gettimeofday (&tv, NULL);
1010     diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1011     dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1012   }
1013   else
1014     dbind_set_timeout (method_call_timeout);
1015 }
1016
1017 dbus_bool_t
1018 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1019 {
1020   va_list args;
1021   dbus_bool_t retval;
1022   DBusError err;
1023   AtspiObject *aobj = ATSPI_OBJECT (obj);
1024
1025   if (!check_app (aobj->app, error))
1026     return FALSE;
1027
1028   va_start (args, type);
1029   dbus_error_init (&err);
1030   set_timeout (aobj->app);
1031   retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1032                                            aobj->path, interface, method, &err,
1033                                            type, args);
1034   va_end (args);
1035   check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1036   _atspi_process_deferred_messages ((gpointer)TRUE);
1037   if (dbus_error_is_set (&err))
1038   {
1039     g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1040     dbus_error_free (&err);
1041   }
1042   return retval;
1043 }
1044
1045 DBusMessage *
1046 _atspi_dbus_call_partial (gpointer obj,
1047                           const char *interface,
1048                           const char *method,
1049                           GError **error,
1050                           const char *type, ...)
1051 {
1052   va_list args;
1053
1054   va_start (args, type);
1055   return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1056 }
1057
1058
1059 DBusMessage *
1060 _atspi_dbus_call_partial_va (gpointer obj,
1061                           const char *interface,
1062                           const char *method,
1063                           GError **error,
1064                           const char *type,
1065                           va_list args)
1066 {
1067   AtspiObject *aobj = ATSPI_OBJECT (obj);
1068   DBusError err;
1069     DBusMessage *msg = NULL, *reply = NULL;
1070     DBusMessageIter iter;
1071     const char *p;
1072
1073   dbus_error_init (&err);
1074
1075   if (!check_app (aobj->app, error))
1076     goto out;
1077
1078   msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1079   if (!msg)
1080     goto out;
1081
1082   p = type;
1083   dbus_message_iter_init_append (msg, &iter);
1084   dbind_any_marshal_va (&iter, &p, args);
1085
1086   set_timeout (aobj->app);
1087   reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1088   check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1089 out:
1090   va_end (args);
1091   if (msg)
1092     dbus_message_unref (msg);
1093   _atspi_process_deferred_messages ((gpointer)TRUE);
1094   if (dbus_error_is_set (&err))
1095   {
1096     /* TODO: Set gerror */
1097     dbus_error_free (&err);
1098   }
1099   return reply;
1100 }
1101
1102 dbus_bool_t
1103 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1104 {
1105   DBusMessage *message, *reply;
1106   DBusMessageIter iter, iter_variant;
1107   DBusError err;
1108   dbus_bool_t retval = FALSE;
1109   AtspiObject *aobj = ATSPI_OBJECT (obj);
1110   char expected_type = (type [0] == '(' ? 'r' : type [0]);
1111
1112   if (!aobj)
1113     return FALSE;
1114
1115   if (!check_app (aobj->app, error))
1116     return FALSE;
1117
1118   message = dbus_message_new_method_call (aobj->app->bus_name,
1119                                           aobj->path,
1120                                           "org.freedesktop.DBus.Properties",
1121                                           "Get");
1122   if (!message)
1123   {
1124     // TODO: throw exception
1125     return FALSE;
1126   }
1127   dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1128   dbus_error_init (&err);
1129   set_timeout (aobj->app);
1130   reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1131   check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1132   dbus_message_unref (message);
1133   _atspi_process_deferred_messages ((gpointer)TRUE);
1134   if (!reply)
1135   {
1136     // TODO: throw exception
1137     goto done;
1138   }
1139
1140   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1141   {
1142     const char *err = NULL;
1143     dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err, DBUS_TYPE_INVALID);
1144     if (err)
1145       g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err);
1146     goto done;
1147   }
1148
1149   dbus_message_iter_init (reply, &iter);
1150   if (dbus_message_iter_get_arg_type (&iter) != 'v')
1151   {
1152     g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1153     goto done;
1154   }
1155   dbus_message_iter_recurse (&iter, &iter_variant);
1156   if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1157   {
1158     g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1159     goto done;
1160   }
1161   if (!strcmp (type, "(so)"))
1162   {
1163     *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1164   }
1165   else
1166   {
1167     dbus_message_iter_get_basic (&iter_variant, data);
1168     if (type [0] == 's')
1169       *(char **)data = g_strdup (*(char **)data);
1170   }
1171   retval = TRUE;
1172 done:
1173   dbus_error_free (&err);
1174   if (reply)
1175     dbus_message_unref (reply);
1176   return retval;
1177 }
1178
1179 DBusMessage *
1180 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1181 {
1182   DBusMessage *reply;
1183   DBusError err;
1184   AtspiApplication *app;
1185   DBusConnection *bus;
1186
1187   app = get_application (dbus_message_get_destination (message));
1188
1189   if (app && !app->bus)
1190     return NULL;        /* will fail anyway; app has been disposed */
1191
1192   bus = (app ? app->bus : _atspi_bus());
1193   dbus_error_init (&err);
1194   set_timeout (app);
1195   reply = dbind_send_and_allow_reentry (bus, message, &err);
1196   _atspi_process_deferred_messages ((gpointer)TRUE);
1197   dbus_message_unref (message);
1198   if (err.message)
1199   {
1200     if (error)
1201       g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1202     dbus_error_free (&err);
1203   }
1204   return reply;
1205 }
1206
1207 GHashTable *
1208 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1209 {
1210   DBusMessageIter iter;
1211   GHashTable *ret;
1212
1213   if (!message)
1214     return NULL;
1215
1216   _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1217
1218   dbus_message_iter_init (message, &iter);
1219   ret = _atspi_dbus_hash_from_iter (&iter);
1220   dbus_message_unref (message);
1221   return ret;
1222 }
1223
1224 GHashTable *
1225 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1226 {
1227   GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1228                                             (GDestroyNotify) g_free,
1229                                             (GDestroyNotify) g_free);
1230   DBusMessageIter iter_array, iter_dict;
1231
1232   dbus_message_iter_recurse (iter, &iter_array);
1233   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1234   {
1235     const char *name, *value;
1236     dbus_message_iter_recurse (&iter_array, &iter_dict);
1237     dbus_message_iter_get_basic (&iter_dict, &name);
1238     dbus_message_iter_next (&iter_dict);
1239     dbus_message_iter_get_basic (&iter_dict, &value);
1240     g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1241     dbus_message_iter_next (&iter_array);
1242   }
1243   return hash;
1244 }
1245
1246 GArray *
1247 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1248 {
1249   DBusMessageIter iter;
1250   GArray *ret;
1251
1252   if (!message)
1253     return NULL;
1254
1255   _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1256
1257   dbus_message_iter_init (message, &iter);
1258
1259   ret = _atspi_dbus_attribute_array_from_iter (&iter);
1260   dbus_message_unref (message);
1261   return ret;
1262 }
1263
1264 GArray *
1265 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1266 {
1267   DBusMessageIter iter_array, iter_dict;
1268   GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1269
1270   dbus_message_iter_recurse (iter, &iter_array);
1271   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1272   {
1273     const char *name, *value;
1274     gchar *str;
1275     dbus_message_iter_recurse (&iter_array, &iter_dict);
1276     dbus_message_iter_get_basic (&iter_dict, &name);
1277     dbus_message_iter_next (&iter_dict);
1278     dbus_message_iter_get_basic (&iter_dict, &value);
1279     str = g_strdup_printf ("%s:%s", name, value);
1280     array = g_array_append_val (array, str);
1281     dbus_message_iter_next (&iter_array);;
1282   }
1283   return array;
1284 }
1285
1286 void
1287 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1288 {
1289   DBusMessageIter iter_array;
1290
1291   accessible->interfaces = 0;
1292   dbus_message_iter_recurse (iter, &iter_array);
1293   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1294   {
1295     const char *iface;
1296     gint n;
1297     dbus_message_iter_get_basic (&iter_array, &iface);
1298     if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1299     n = _atspi_get_iface_num (iface);
1300     if (n == -1)
1301     {
1302       g_warning ("AT-SPI: Unknown interface %s", iface);
1303     }
1304     else
1305       accessible->interfaces |= (1 << n);
1306     dbus_message_iter_next (&iter_array);
1307   }
1308   _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1309 }
1310
1311 void
1312 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1313 {
1314   DBusMessageIter iter_array;
1315   gint count;
1316   dbus_uint32_t *states;
1317
1318   dbus_message_iter_recurse (iter, &iter_array);
1319   dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1320   if (count != 2)
1321   {
1322     g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1323     if (!accessible->states)
1324       accessible->states = _atspi_state_set_new_internal (accessible, 0);
1325   }
1326   else
1327   {
1328     guint64 val = ((guint64)states [1]) << 32;
1329     val += states [0];
1330     if (!accessible->states)
1331       accessible->states = _atspi_state_set_new_internal (accessible, val);
1332     else
1333       accessible->states->states = val;
1334   }
1335   _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1336 }
1337
1338 GQuark
1339 _atspi_error_quark (void)
1340 {
1341   return g_quark_from_static_string ("atspi_error");
1342 }
1343
1344 /*
1345  * Gets the IOR from the XDisplay.
1346  */
1347 static char *
1348 get_accessibility_bus_address_x11 (void)
1349 {
1350   Atom AT_SPI_BUS;
1351   Atom actual_type;
1352   Display *bridge_display;
1353   int actual_format;
1354   char *data;
1355   unsigned char *data_x11 = NULL;
1356   unsigned long nitems;
1357   unsigned long leftover;
1358
1359   bridge_display = XOpenDisplay (spi_display_name ());
1360   if (!bridge_display)
1361     {
1362       g_warning ("Could not open X display");
1363       return NULL;
1364     }
1365       
1366   AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1367   XGetWindowProperty (bridge_display,
1368                       XDefaultRootWindow (bridge_display),
1369                       AT_SPI_BUS, 0L,
1370                       (long) BUFSIZ, False,
1371                       (Atom) 31, &actual_type, &actual_format,
1372                       &nitems, &leftover, &data_x11);
1373   XCloseDisplay (bridge_display);
1374
1375   data = g_strdup (data_x11);
1376   XFree (data_x11);
1377   return data;
1378 }
1379
1380 static char *
1381 get_accessibility_bus_address_dbus (void)
1382 {
1383   DBusConnection *session_bus = NULL;
1384   DBusMessage *message;
1385   DBusMessage *reply;
1386   DBusError error;
1387   char *address = NULL;
1388
1389   session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1390   if (!session_bus)
1391     return NULL;
1392
1393   message = dbus_message_new_method_call ("org.a11y.Bus",
1394                                           "/org/a11y/bus",
1395                                           "org.a11y.Bus",
1396                                           "GetAddress");
1397
1398   dbus_error_init (&error);
1399   reply = dbus_connection_send_with_reply_and_block (session_bus,
1400                                                      message,
1401                                                      -1,
1402                                                      &error);
1403   dbus_message_unref (message);
1404
1405   if (!reply)
1406   {
1407     g_warning ("Error retrieving accessibility bus address: %s: %s",
1408                error.name, error.message);
1409     dbus_error_free (&error);
1410     return NULL;
1411   }
1412   
1413   {
1414     const char *tmp_address;
1415     if (!dbus_message_get_args (reply,
1416                                 NULL,
1417                                 DBUS_TYPE_STRING,
1418                                 &tmp_address,
1419                                 DBUS_TYPE_INVALID))
1420       {
1421         dbus_message_unref (reply);
1422         return NULL;
1423       }
1424     address = g_strdup (tmp_address);
1425     dbus_message_unref (reply);
1426   }
1427   
1428   return address;
1429 }
1430
1431 DBusConnection *
1432 atspi_get_a11y_bus (void)
1433 {
1434   DBusConnection *bus = NULL;
1435   DBusError error;
1436   char *address;
1437
1438   address = get_accessibility_bus_address_x11 ();
1439   if (!address)
1440     address = get_accessibility_bus_address_dbus ();
1441   if (!address)
1442     return NULL;
1443
1444   dbus_error_init (&error);
1445   bus = dbus_connection_open (address, &error);
1446   g_free (address);
1447
1448   if (!bus)
1449     {
1450       g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1451       return NULL;
1452     }
1453   else
1454     {
1455       if (!dbus_bus_register (bus, &error))
1456         {
1457           g_warning ("Couldn't register with accessibility bus: %s", error.message);
1458           return NULL;
1459         }
1460     }
1461   
1462   return bus;
1463 }
1464
1465 /**
1466  *  Set the timeout used for method calls. If this is not set explicitly,
1467  *  a default of 0.8 ms is used.
1468  *  Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1469  *  sending a keyboard event notification. This means that, if an AT makes
1470  *  a call in response to the keyboard notification and the application
1471  *  being called does not respond before the timeout is reached,
1472  *  at-spi2-registryd will time out on the keyboard event notification and
1473  *  pass the key onto the application (ie, reply to indicate that the key
1474  *  was not consumed), so this may make it undesirable to set a timeout
1475  *  larger than 3 seconds.
1476  *
1477  *  @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1478  *  @startup_time: The amount of time, in milliseconds, to allow to pass
1479  *  before enforcing timeouts on an application. Can be used to prevent
1480  *  timeout exceptions if an application is likely to block for an extended
1481  *  period of time on initialization. -1 can be passed to disable this
1482  *  behavior.
1483  *
1484  * By default, the normal timeout is set to 800 ms, and the application startup
1485  * timeout is set to 15 seconds.
1486  */
1487 void
1488 atspi_set_timeout (gint val, gint startup_time)
1489 {
1490   method_call_timeout = val;
1491   app_startup_time = startup_time;
1492 }