Several cache-related fixes
[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  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /*
25  *
26  * Basic SPI initialization and event loop function prototypes
27  *
28  */
29
30 #include "atspi-private.h"
31 #include "X11/Xlib.h"
32 #include <stdio.h>
33 #include <string.h>
34
35 static void handle_get_items (DBusPendingCall *pending, void *user_data);
36
37 static DBusConnection *bus = NULL;
38 static GHashTable *apps = NULL;
39 static GHashTable *live_refs = NULL;
40 static GQueue *exception_handlers = NULL;
41 static DBusError exception;
42
43 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
44 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
45 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
46 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
47 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
48 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
49 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
50 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
51 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
52 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
53 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
54 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
55 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
56 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
57 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
58 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
59 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
60 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
61 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
62 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
63 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
64 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
65 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
66
67 static const char *interfaces[] =
68 {
69   ATSPI_DBUS_INTERFACE_ACCESSIBLE,
70   ATSPI_DBUS_INTERFACE_ACTION,
71   ATSPI_DBUS_INTERFACE_APPLICATION,
72   ATSPI_DBUS_INTERFACE_COLLECTION,
73   ATSPI_DBUS_INTERFACE_COMPONENT,
74   ATSPI_DBUS_INTERFACE_DOCUMENT,
75   ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
76   ATSPI_DBUS_INTERFACE_HYPERLINK,
77   ATSPI_DBUS_INTERFACE_HYPERTEXT,
78   ATSPI_DBUS_INTERFACE_IMAGE,
79   "org.a11y.atspi.LoginHelper",
80   ATSPI_DBUS_INTERFACE_SELECTION,
81   ATSPI_DBUS_INTERFACE_TABLE,
82   ATSPI_DBUS_INTERFACE_TEXT,
83   ATSPI_DBUS_INTERFACE_VALUE,
84   NULL
85 };
86
87 gint
88 _atspi_get_iface_num (const char *iface)
89 {
90   /* TODO: Use a binary search or hash to improve performance */
91   int i;
92
93   for (i = 0; interfaces[i]; i++)
94   {
95     if (!strcmp(iface, interfaces[i])) return i;
96   }
97   return -1;
98 }
99
100 static GHashTable *
101 get_live_refs (void)
102 {
103   if (!live_refs) 
104     {
105       live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
106     }
107   return live_refs;
108 }
109
110 /* TODO: Add an application parameter */
111 DBusConnection *
112 _atspi_bus ()
113 {
114   if (!bus)
115     atspi_init ();
116   return bus;
117 }
118
119 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
120
121 static void
122 cleanup ()
123 {
124   GHashTable *refs;
125
126   refs = live_refs;
127   live_refs = NULL;
128   if (refs)
129     {
130       g_hash_table_destroy (refs);
131     }
132 }
133
134 static gboolean atspi_inited = FALSE;
135
136 static GHashTable *app_hash = NULL;
137
138 static AtspiApplication *
139 get_application (const char *bus_name)
140 {
141   AtspiApplication *app = NULL;
142   char *bus_name_dup;
143   DBusMessage *message;
144   DBusError error;
145   DBusPendingCall *pending = NULL;
146
147   if (!app_hash)
148   {
149     app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_unref);
150     if (!app_hash) return NULL;
151   }
152   app = g_hash_table_lookup (app_hash, bus_name);
153   if (app) return app;
154   bus_name_dup = g_strdup (bus_name);
155   if (!bus_name_dup) return NULL;
156   // TODO: change below to something that will send state-change:defunct notification if necessary */
157   app = _atspi_application_new (bus_name);
158   if (!app) return NULL;
159   app->bus_name = bus_name_dup;
160   app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
161   g_hash_table_insert (app_hash, bus_name_dup, app);
162   dbus_error_init (&error);
163   message = dbus_message_new_method_call (bus_name, "/org/a11y/atspi/cache",
164                                           atspi_interface_cache, "GetItems");
165
166    dbus_connection_send_with_reply (_atspi_bus (), message, &pending, 2000);
167   dbus_pending_call_set_notify (pending, handle_get_items, app, NULL);
168   return app;
169 }
170
171 static AtspiAccessible *
172 ref_accessible (const char *app_name, const char *path)
173 {
174   AtspiApplication *app;
175   AtspiAccessible *a;
176
177   if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
178     return NULL;
179
180   app = get_application (app_name);
181
182   if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
183   {
184     if (!app->root)
185     {
186       app->root = atspi_accessible_new (app, atspi_path_root);
187       app->root->accessible_parent = atspi_get_desktop (0);
188     }
189     return g_object_ref (app->root);
190   }
191
192   a = g_hash_table_lookup (app->hash, path);
193   if (a)
194   {
195     return g_object_ref (a);
196   }
197   a = atspi_accessible_new (app, path);
198   if (!a)
199     return NULL;
200   g_hash_table_insert (app->hash, a->parent.path, a);
201   g_object_ref (a);     /* for the hash */
202   return a;
203 }
204
205 static AtspiHyperlink *
206 ref_hyperlink (const char *app_name, const char *path)
207 {
208   AtspiApplication *app = get_application (app_name);
209   AtspiHyperlink *hyperlink;
210
211   if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
212     return NULL;
213
214   hyperlink = g_hash_table_lookup (app->hash, path);
215   if (hyperlink)
216   {
217     return g_object_ref (hyperlink);
218   }
219   hyperlink = atspi_hyperlink_new (app, path);
220   if (!hyperlink)
221     return NULL;
222   g_hash_table_insert (app->hash, hyperlink->parent.path, hyperlink);
223   /* TODO: This should be a weak ref */
224   g_object_ref (hyperlink);     /* for the hash */
225   return hyperlink;
226 }
227
228 typedef struct
229 {
230   char *path;
231   char *parent;
232   GArray *children;
233   GArray *interfaces;
234   char *name;
235   dbus_uint32_t role;
236   char *description;
237   GArray *state_bitflags;
238 } CACHE_ADDITION;
239
240 static DBusHandlerResult
241 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
242 {
243   const char *sender = dbus_message_get_sender (message);
244   AtspiApplication *app;
245   const char *path;
246   DBusMessageIter iter, iter_struct;
247   const char *signature = dbus_message_get_signature (message);
248   AtspiAccessible *a;
249   int id;
250
251   if (strcmp (signature, "(so)") != 0)
252   {
253     g_warning ("at-spi: Unknown signature %s for RemoveAccessible", signature);
254     return DBUS_HANDLER_RESULT_HANDLED;
255   }
256
257   dbus_message_iter_init (message, &iter);
258   dbus_message_iter_recurse (&iter, &iter_struct);
259   dbus_message_iter_get_basic (&iter_struct, &sender);
260   dbus_message_iter_get_basic (&iter_struct, &path);
261   app = get_application (sender);
262   a = ref_accessible (sender, path);
263   if (!a)
264     return DBUS_HANDLER_RESULT_HANDLED;
265   if (a->accessible_parent && g_list_find (a->accessible_parent->children, a))
266   {
267     a->accessible_parent->children = g_list_remove (a->accessible_parent->children, a);
268     g_object_unref (a);
269   }
270   g_hash_table_remove (app->hash, app->bus_name);
271   g_object_unref (a);   /* unref our own ref */
272   return DBUS_HANDLER_RESULT_HANDLED;
273 }
274
275 static gboolean
276 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
277 {
278   DBusError error;
279   char *root_path;
280
281   dbus_error_init (&error);
282   AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
283   if (obj)
284   {
285     GList *new_list = g_list_append (a->children, obj);
286     if (new_list)
287     {
288       a->children = new_list;
289       return TRUE;
290     }
291   }
292   else
293   {
294     g_warning ("Error calling getRoot for %s: %s", bus_name, error.message);
295   }
296   return FALSE;
297 }
298
299 static void
300 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
301 {
302   AtspiEvent e;
303
304   memset (&e, 0, sizeof (e));
305   e.type = (add? "object:children-changed:add": "object:children-changed:remove");
306   e.source = parent;
307   e.detail1 = g_list_index (parent->children, child);
308   e.detail2 = 0;
309   _atspi_send_event (&e);
310 }
311
312 static void
313 unref_object_and_descendants (AtspiAccessible *obj)
314 {
315   GList *l;
316
317   for (l = obj->children; l; l = l->next)
318   {
319     unref_object_and_descendants (l->data);
320   }
321   g_object_unref (obj);
322 }
323
324 static gboolean
325 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
326 {
327   GList *l;
328   AtspiAccessible *child;
329
330   for (l = a->children; l; l = l->next)
331   {
332     child = l->data;
333     if (!strcmp (bus_name, child->parent.app->bus_name)) break;
334   }
335   if (!l)
336   {
337     g_warning ("Removing unregistered app %s; doing nothing\n", bus_name);
338     return FALSE;
339   }
340   send_children_changed (a, child, FALSE);
341   a->children = g_list_remove (a->children, child);
342   unref_object_and_descendants (child);
343   return TRUE;
344 }
345
346 static AtspiAccessible *desktop;
347
348 void
349 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
350 {
351   DBusMessageIter iter_struct;
352
353   dbus_message_iter_recurse (iter, &iter_struct);
354   dbus_message_iter_get_basic (&iter_struct, app_name);
355   dbus_message_iter_next (&iter_struct);
356   dbus_message_iter_get_basic (&iter_struct, path);
357   dbus_message_iter_next (iter);
358 }
359
360 static void
361 add_accessible_from_iter (DBusMessageIter *iter)
362 {
363   gint i;
364   GList *new_list;
365   DBusMessageIter iter_struct, iter_array;
366   const char *app_name, *path;
367   AtspiApplication *app;
368   AtspiAccessible *accessible;
369   const char *name, *description;
370   dbus_uint32_t role;
371
372   dbus_message_iter_recurse (iter, &iter_struct);
373
374   /* get accessible */
375   get_reference_from_iter (&iter_struct, &app_name, &path);
376   accessible = ref_accessible (app_name, path);
377   if (!accessible)
378     return;
379
380   /* Get application: TODO */
381   dbus_message_iter_next (&iter_struct);
382
383   /* get parent */
384   get_reference_from_iter (&iter_struct, &app_name, &path);
385   if (accessible->accessible_parent)
386     g_object_unref (accessible->accessible_parent);
387   accessible->accessible_parent = ref_accessible (app_name, path);
388
389   /* Get children */
390   while (accessible->children)
391   {
392     g_object_unref (accessible->children->data);
393     accessible->children = g_list_remove (accessible->children, accessible->children->data);
394   }
395   dbus_message_iter_recurse (&iter_struct, &iter_array);
396   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
397   {
398     AtspiAccessible *child;
399     get_reference_from_iter (&iter_array, &app_name, &path);
400     child = ref_accessible (app_name, path);
401     new_list = g_list_append (accessible->children, child);
402     if (new_list) accessible->children = new_list;
403   }
404
405   /* interfaces */
406   dbus_message_iter_next (&iter_struct);
407   _atspi_dbus_set_interfaces (accessible, &iter_struct);
408   dbus_message_iter_next (&iter_struct);
409
410   /* name */
411   if (accessible->name)
412     g_free (accessible->name);
413   dbus_message_iter_get_basic (&iter_struct, &name);
414   accessible->name = g_strdup (name);
415   dbus_message_iter_next (&iter_struct);
416
417   /* role */
418   dbus_message_iter_get_basic (&iter_struct, &role);
419   accessible->role = role;
420   dbus_message_iter_next (&iter_struct);
421
422   /* description */
423   if (accessible->description)
424     g_free (accessible->description);
425   dbus_message_iter_get_basic (&iter_struct, &description);
426   accessible->description = g_strdup (description);
427   dbus_message_iter_next (&iter_struct);
428
429   _atspi_dbus_set_state (accessible, &iter_struct);
430   dbus_message_iter_next (&iter_struct);
431
432   accessible->cached_properties |= ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE | ATSPI_CACHE_PARENT;
433   if (!atspi_state_set_contains (accessible->states,
434                                        ATSPI_STATE_MANAGES_DESCENDANTS))
435     accessible->cached_properties |= ATSPI_CACHE_CHILDREN;
436
437   /* This is a bit of a hack since the cache holds a ref, so we don't need
438    * the one provided for us anymore */
439   g_object_unref (accessible);
440 }
441
442 static void
443 handle_get_items (DBusPendingCall *pending, void *user_data)
444 {
445   AtspiApplication *app = user_data;
446   DBusMessage *reply = dbus_pending_call_steal_reply (pending);
447   DBusMessageIter iter, iter_array;
448
449   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
450   {
451     dbus_message_unref (reply);
452     g_warning ("Atspi: Error in GetItems");
453     return;
454   }
455
456   dbus_message_iter_init (reply, &iter);
457   dbus_message_iter_recurse (&iter, &iter_array);
458   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
459   {
460     add_accessible_from_iter (&iter_array);
461     dbus_message_iter_next (&iter_array);
462   }
463   dbus_message_unref (reply);
464 }
465
466 /* TODO: Do we stil need this function? */
467 static AtspiAccessible *
468 ref_accessible_desktop (AtspiApplication *app)
469 {
470   DBusError error;
471   GArray *apps = NULL;
472   GArray *additions;
473   gint i;
474   DBusMessage *message, *reply;
475   DBusMessageIter iter, iter_array;
476
477   if (desktop)
478   {
479     g_object_ref (desktop);
480     return desktop;
481   }
482   desktop = atspi_accessible_new (app, atspi_path_root);
483   if (!desktop)
484   {
485     return NULL;
486   }
487   g_hash_table_insert (app->hash, desktop->parent.path, desktop);
488   g_object_ref (desktop);       /* for the hash */
489   desktop->name = g_strdup ("main");
490   dbus_error_init (&error);
491   message = dbus_message_new_method_call (atspi_bus_registry,
492         atspi_path_root,
493         atspi_interface_accessible,
494         "GetChildren");
495   if (!message)
496     return;
497   reply = _atspi_dbus_send_with_reply_and_block (message);
498   if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
499   {
500     g_error ("Couldn't get application list: %s", error.message);
501     if (reply)
502       dbus_message_unref (reply);
503     return;
504   }
505   dbus_message_iter_init (reply, &iter);
506   dbus_message_iter_recurse (&iter, &iter_array);
507   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
508   {
509     const char *app_name, *path;
510     get_reference_from_iter (&iter_array, &app_name, &path);
511     add_app_to_desktop (desktop, app_name);
512   }
513   dbus_message_unref (reply);
514   return desktop;
515 }
516
517 AtspiAccessible *
518 _atspi_ref_accessible (const char *app, const char *path)
519 {
520   AtspiApplication *a = get_application (app);
521   if (!a) return NULL;
522   if ( APP_IS_REGISTRY(a))
523   {
524     return ref_accessible_desktop (a);
525   }
526   return ref_accessible (app, path);
527 }
528
529 AtspiAccessible *
530 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
531 {
532   DBusMessageIter iter;
533   AtspiAccessible *retval = NULL;
534   const char *signature = dbus_message_get_signature (message);
535    
536   if (!strcmp (signature, "(so)"))
537   {
538     dbus_message_iter_init (message, &iter);
539     retval =  _atspi_dbus_return_accessible_from_iter (&iter);
540   }
541   else
542   {
543     g_warning ("Atspi: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
544   }
545   dbus_message_unref (message);
546   return retval;
547 }
548
549 AtspiAccessible *
550 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
551 {
552   const char *app_name, *path;
553
554   get_reference_from_iter (iter, &app_name, &path);
555   return ref_accessible (app_name, path);
556 }
557
558 AtspiHyperlink *
559 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
560 {
561   DBusMessageIter iter;
562   AtspiHyperlink *retval = NULL;
563   const char *signature = dbus_message_get_signature (message);
564    
565   if (!strcmp (signature, "(so)"))
566   {
567     dbus_message_iter_init (message, &iter);
568     retval =  _atspi_dbus_return_hyperlink_from_iter (&iter);
569   }
570   else
571   {
572     g_warning ("Atspi: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
573   }
574   dbus_message_unref (message);
575   return retval;
576 }
577
578 AtspiHyperlink *
579 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
580 {
581   const char *app_name, *path;
582
583   get_reference_from_iter (iter, &app_name, &path);
584   return ref_hyperlink (app_name, path);
585 }
586
587 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
588
589 static DBusHandlerResult
590 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
591 {
592   DBusMessageIter iter;
593   const char *sender = dbus_message_get_sender (message);
594   AtspiApplication *app = get_application (sender);
595   const char *type = cache_signal_type;
596
597   if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
598   {
599     g_warning ("atspi: AddAccessible with unknown signature %s\n", dbus_message_get_signature (message));
600     return;
601   }
602
603   dbus_message_iter_init (message, &iter);
604   add_accessible_from_iter (&iter);
605 }
606
607 typedef struct
608 {
609   DBusConnection *bus;
610   DBusMessage *message;
611   void *data;
612 } BusDataClosure;
613
614 static guint process_deferred_messages_id = -1;
615
616 static void
617 process_deferred_message (BusDataClosure *closure)
618 {
619   int type = dbus_message_get_type (closure->message);
620   const char *interface = dbus_message_get_interface (closure->message);
621   const char *member = dbus_message_get_member (closure->message); 
622   dbus_uint32_t v;
623   char *bus_name;
624
625   if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
626       !strncmp (interface, "org.a11y.atspi.Event.", 21))
627   {
628     atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
629   }
630   if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
631   {
632     atspi_dbus_handle_DeviceEvent (closure->bus,
633                                    closure->message, closure->data);
634   }
635   if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
636   {
637     handle_add_accessible (closure->bus, closure->message, closure->data);
638   }
639   if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
640   {
641     handle_remove_accessible (closure->bus, closure->message, closure->data);
642   }
643 }
644
645 static GList *deferred_messages = NULL;
646
647 gboolean
648 _atspi_process_deferred_messages (gpointer data)
649 {
650   static int in_process_deferred_messages = 0;
651
652   if (in_process_deferred_messages)
653     return;
654   in_process_deferred_messages = 1;
655   while (deferred_messages != NULL)
656   {
657     BusDataClosure *closure = deferred_messages->data;
658     process_deferred_message (closure);
659     deferred_messages = g_list_remove (deferred_messages, closure);
660     dbus_message_unref (closure->message);
661     dbus_connection_unref (closure->bus);
662     g_free (closure);
663   }
664   /* If data is NULL, assume that we were called from GLib */
665   if (!data)
666     process_deferred_messages_id = -1;
667   in_process_deferred_messages = 0;
668   return FALSE;
669 }
670
671 static DBusHandlerResult
672 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
673 {
674   BusDataClosure *closure = g_new (BusDataClosure, 1);
675   GList *new_list;
676
677   if (!closure)
678     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
679   closure->bus = dbus_connection_ref (bus);
680   closure->message = dbus_message_ref (message);
681   closure->data = user_data;
682
683   new_list = g_list_append (deferred_messages, closure);
684   if (new_list)
685     deferred_messages = new_list;
686
687   if (process_deferred_messages_id == -1)
688     process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
689   return DBUS_HANDLER_RESULT_HANDLED;
690 }
691
692 static DBusHandlerResult
693 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
694 {
695   int type = dbus_message_get_type (message);
696   const char *interface = dbus_message_get_interface (message);
697   const char *member = dbus_message_get_member (message); 
698   dbus_uint32_t v;
699   char *bus_name;
700
701   if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
702       !strncmp (interface, "org.a11y.atspi.Event.", 21))
703   {
704     return defer_message (bus, message, data);
705   }
706   if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
707   {
708     return defer_message (bus, message, data);
709   }
710   if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
711   {
712     return defer_message (bus, message, data);
713   }
714   if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
715   {
716     return defer_message (bus, message, data);
717   }
718   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
719 }
720
721 static const char *signal_interfaces[] =
722 {
723   "org.a11y.atspi.Event.Object",
724   "org.a11y.atspi.Event.Window",
725   "org.a11y.atspi.Event.Mouse",
726   "org.a11y.atspi.Event.Terminal",
727   "org.a11y.atspi.Event.Document",
728   "org.a11y.atspi.Event.Focus",
729   NULL
730 };
731
732 /*
733  * Returns a 'canonicalized' value for DISPLAY,
734  * with the screen number stripped off if present.
735  *
736  * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
737  */
738 static const gchar *
739 spi_display_name (void)
740 {
741   static const char *canonical_display_name = NULL;
742   if (!canonical_display_name)
743     {
744       const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
745       if (!display_env)
746         {
747           display_env = g_getenv ("DISPLAY");
748           if (!display_env || !display_env[0])
749             canonical_display_name = ":0";
750           else
751             {
752               gchar *display_p, *screen_p;
753               canonical_display_name = g_strdup (display_env);
754               display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
755               screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
756               if (screen_p && display_p && (screen_p > display_p))
757                 {
758                   *screen_p = '\0';
759                 }
760             }
761         }
762       else
763         {
764           canonical_display_name = display_env;
765         }
766     }
767   return canonical_display_name;
768 }
769
770 /* TODO: Avoid having duplicate functions for this here and in at-spi2-atk */
771 static DBusConnection *
772 get_accessibility_bus ()
773 {
774   Atom AT_SPI_BUS;
775   Atom actual_type;
776   Display *bridge_display;
777   int actual_format;
778   unsigned char *data = NULL;
779   unsigned long nitems;
780   unsigned long leftover;
781
782   DBusConnection *bus = NULL;
783   DBusError error;
784
785   bridge_display = XOpenDisplay (spi_display_name ());
786   if (!bridge_display)
787     {
788       g_warning ("AT_SPI: Could not get the display\n");
789       return NULL;
790     }
791
792   AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
793   XGetWindowProperty (bridge_display,
794                       XDefaultRootWindow (bridge_display),
795                       AT_SPI_BUS, 0L,
796                       (long) BUFSIZ, False,
797                       (Atom) 31, &actual_type, &actual_format,
798                       &nitems, &leftover, &data);
799
800   dbus_error_init (&error);
801
802   if (data == NULL)
803     {
804       g_warning
805         ("AT-SPI: Accessibility bus not found - Using session bus.\n");
806       bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
807       if (!bus)
808         {
809           g_warning ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
810           return NULL;
811         }
812     }
813   else
814     {
815       bus = dbus_connection_open (data, &error);
816       if (!bus)
817         {
818           g_warning ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
819           return NULL;
820         }
821       else
822         {
823           if (!dbus_bus_register (bus, &error))
824             {
825               g_warning ("AT-SPI: Couldn't register with bus: %s\n", error.message);
826               return NULL;
827             }
828         }
829     }
830
831   return bus;
832 }
833
834 /**
835  * atspi_init:
836  *
837  * Connects to the accessibility registry and initializes the SPI.
838  *
839  * Returns: 0 on success, otherwise an integer error code.  
840  **/
841 int
842 atspi_init (void)
843 {
844   DBusError error;
845   char *match;
846   int i;
847
848   if (atspi_inited)
849     {
850       return 1;
851     }
852
853   atspi_inited = TRUE;
854
855   g_type_init ();
856
857   get_live_refs();
858
859   dbus_error_init (&error);
860   bus = get_accessibility_bus ();
861   if (!bus)
862   {
863     g_error ("Couldn't get session bus");
864     return 2;
865   }
866   dbus_bus_register (bus, &error);
867   dbus_connection_setup_with_g_main(bus, g_main_context_default());
868   dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
869   dbind_set_timeout (1000);
870   match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
871   dbus_error_init (&error);
872   dbus_bus_add_match (bus, match, &error);
873   g_free (match);
874   match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
875   dbus_bus_add_match (bus, match, &error);
876   g_free (match);
877   match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
878   dbus_bus_add_match (bus, match, &error);
879   g_free (match);
880   match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
881   dbus_bus_add_match (bus, match, &error);
882   g_free (match);
883   match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
884   dbus_bus_add_match (bus, match, &error);
885   g_free (match);
886   return 0;
887 }
888
889   static GMainLoop *mainloop;
890
891 /**
892  * atspi_event_main:
893  *
894  * Starts/enters the main event loop for the AT-SPI services.
895  *
896  * (NOTE: This method does not return control, it is exited via a call to
897  *  atspi_event_quit () from within an event handler).
898  *
899  **/
900 void
901 atspi_event_main (void)
902 {
903   mainloop = g_main_loop_new (NULL, FALSE);
904   g_main_loop_run (mainloop);
905 }
906
907 /**
908  * atspi_event_quit:
909  *
910  * Quits the last main event loop for the SPI services,
911  * see atspi_event_main
912  **/
913 void
914 atspi_event_quit (void)
915 {
916   g_main_loop_quit (mainloop);
917 }
918
919 /**
920  * atspi_exit:
921  *
922  * Disconnects from the Accessibility Registry and releases 
923  * any floating resources. Call only once at exit.
924  *
925  * Returns: 0 if there were no leaks, otherwise non zero.
926  **/
927 int
928 atspi_exit (void)
929 {
930   int leaked;
931
932   if (!atspi_inited)
933     {
934       return 0;
935     }
936
937   atspi_inited = FALSE;
938
939   if (live_refs)
940     {
941       leaked = g_hash_table_size (live_refs);
942     }
943   else
944     {
945       leaked = 0;
946     }
947
948   cleanup ();
949
950   return leaked;
951 }
952
953 dbus_bool_t
954 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
955 {
956   va_list args;
957   dbus_bool_t retval;
958   DBusError err;
959   AtspiObject *aobj = ATSPI_OBJECT (obj);
960
961   va_start (args, type);
962   dbus_error_init (&err);
963   retval = dbind_method_call_reentrant_va (_atspi_bus(), aobj->app->bus_name, aobj->path, interface, method, &err, type, args);
964   va_end (args);
965   _atspi_process_deferred_messages ((gpointer)TRUE);
966   if (dbus_error_is_set (&err))
967   {
968     /* TODO: Set gerror */
969     dbus_error_free (&err);
970   }
971   return retval;
972 }
973
974 DBusMessage *
975 _atspi_dbus_call_partial (gpointer obj,
976                           const char *interface,
977                           const char *method,
978                           GError **error,
979                           const char *type, ...)
980 {
981   va_list args;
982
983   va_start (args, type);
984   return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
985 }
986
987 DBusMessage *
988 _atspi_dbus_call_partial_va (gpointer obj,
989                           const char *interface,
990                           const char *method,
991                           GError **error,
992                           const char *type,
993                           va_list args)
994 {
995   AtspiObject *aobj = ATSPI_OBJECT (obj);
996   dbus_bool_t retval;
997   DBusError err;
998     DBusMessage *msg = NULL, *reply = NULL;
999     DBusMessageIter iter;
1000     const char *p;
1001
1002   dbus_error_init (&err);
1003
1004     msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1005     if (!msg)
1006         goto out;
1007
1008     p = type;
1009     dbus_message_iter_init_append (msg, &iter);
1010     dbind_any_marshal_va (&iter, &p, args);
1011
1012     reply = dbind_send_and_allow_reentry (_atspi_bus(), msg, &err);
1013 out:
1014   va_end (args);
1015   _atspi_process_deferred_messages ((gpointer)TRUE);
1016   if (dbus_error_is_set (&err))
1017   {
1018     /* TODO: Set gerror */
1019     dbus_error_free (&err);
1020   }
1021   return reply;
1022 }
1023
1024 dbus_bool_t
1025 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1026 {
1027   DBusMessage *message, *reply;
1028   DBusMessageIter iter, iter_variant;
1029   DBusError err;
1030   dbus_bool_t retval = FALSE;
1031   AtspiObject *aobj = ATSPI_OBJECT (obj);
1032
1033   if (!aobj)
1034     return FALSE;
1035
1036   message = dbus_message_new_method_call (aobj->app->bus_name,
1037                                           aobj->path,
1038                                           "org.freedesktop.DBus.Properties",
1039                                           "Get");
1040   if (!message)
1041   {
1042     // TODO: throw exception
1043     goto done;
1044   }
1045   dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1046   dbus_error_init (&err);
1047   reply = dbind_send_and_allow_reentry (_atspi_bus(), message, &err);
1048   dbus_message_unref (message);
1049   _atspi_process_deferred_messages ((gpointer)TRUE);
1050   if (!reply)
1051   {
1052     // TODO: throw exception
1053     goto done;
1054   }
1055   dbus_message_iter_init (reply, &iter);
1056   if (dbus_message_iter_get_arg_type (&iter) != 'v')
1057   {
1058     g_warning ("at-spi: expected a variant when fetching %s from interface %s\n", name, interface);
1059     goto done;
1060   }
1061   dbus_message_iter_recurse (&iter, &iter_variant);
1062   if (dbus_message_iter_get_arg_type (&iter_variant) != type[0])
1063   {
1064     g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1065     goto done;
1066   }
1067   if (!strcmp (type, "(so)"))
1068   {
1069     *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1070   }
1071   else
1072   {
1073     dbus_message_iter_get_basic (&iter_variant, data);
1074     dbus_message_unref (reply);
1075     if (type [0] == 's')
1076       *(char **)data = g_strdup (*(char **)data);
1077   }
1078   retval = TRUE;
1079 done:
1080   return retval;
1081 }
1082
1083 DBusMessage *
1084 _atspi_dbus_send_with_reply_and_block (DBusMessage *message)
1085 {
1086   DBusMessage *reply;
1087   DBusError err;
1088
1089   dbus_error_init (&err);
1090   reply = dbind_send_and_allow_reentry (_atspi_bus(), message, &err);
1091   _atspi_process_deferred_messages ((gpointer)TRUE);
1092   dbus_message_unref (message);
1093   if (err.message)
1094     g_warning ("Atspi: Got error: %s\n", err.message);
1095   return reply;
1096 }
1097
1098 GHashTable *
1099 _atspi_dbus_hash_from_message (DBusMessage *message)
1100 {
1101   DBusMessageIter iter;
1102
1103   _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL);
1104
1105   dbus_message_iter_init (message, &iter);
1106   return _atspi_dbus_hash_from_iter (&iter);
1107 }
1108
1109 GHashTable *
1110 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1111 {
1112   GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
1113   DBusMessageIter iter_array, iter_dict;
1114
1115   dbus_message_iter_recurse (iter, &iter_array);
1116   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1117   {
1118     const char *name, *value;
1119     dbus_message_iter_recurse (&iter_array, &iter_dict);
1120     dbus_message_iter_get_basic (&iter_dict, &name);
1121     dbus_message_iter_next (&iter_dict);
1122     dbus_message_iter_get_basic (&iter_dict, &value);
1123     g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1124     dbus_message_iter_next (&iter_array);
1125   }
1126   return hash;
1127 }
1128
1129 GArray *
1130 _atspi_dbus_attribute_array_from_message (DBusMessage *message)
1131 {
1132   DBusMessageIter iter;
1133
1134   _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL);
1135
1136   dbus_message_iter_init (message, &iter);
1137
1138   return _atspi_dbus_attribute_array_from_iter (&iter);
1139 }
1140
1141 GArray *
1142 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1143 {
1144   DBusMessageIter iter_array, iter_dict;
1145   GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1146   gint count = 0;
1147
1148   dbus_message_iter_recurse (iter, &iter_array);
1149   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1150   {
1151     const char *name, *value;
1152     gchar *str;
1153     GArray *new_array;
1154     dbus_message_iter_recurse (&iter_array, &iter_dict);
1155     dbus_message_iter_get_basic (&iter_dict, &name);
1156     dbus_message_iter_next (&iter_dict);
1157     dbus_message_iter_get_basic (&iter_dict, &value);
1158     str = g_strdup_printf ("%s:%s", name, value);
1159     new_array = g_array_append_val (array, str);
1160     if (new_array)
1161       array = new_array;
1162     dbus_message_iter_next (&iter_array);;
1163   }
1164   return array;
1165 }
1166
1167 void
1168 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1169 {
1170   DBusMessageIter iter_array;
1171
1172   accessible->interfaces = 0;
1173   dbus_message_iter_recurse (iter, &iter_array);
1174   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1175   {
1176     const char *iface;
1177     gint n;
1178     dbus_message_iter_get_basic (&iter_array, &iface);
1179     if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1180     n = _atspi_get_iface_num (iface);
1181     if (n == -1)
1182     {
1183       g_warning ("at-spi: Unknown interface %s", iface);
1184     }
1185     else
1186       accessible->interfaces |= (1 << n);
1187     dbus_message_iter_next (&iter_array);
1188   }
1189   accessible->cached_properties |= ATSPI_CACHE_INTERFACES;
1190 }
1191
1192 void
1193 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1194 {
1195   DBusMessageIter iter_array;
1196   gint count;
1197   dbus_uint32_t *states;
1198
1199   dbus_message_iter_recurse (iter, &iter_array);
1200   dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1201   if (count != 2)
1202   {
1203     g_warning ("at-spi: expected 2 values in states array; got %d\n", count);
1204     if (!accessible->states)
1205       accessible->states = _atspi_state_set_new_internal (accessible, 0);
1206   }
1207   else
1208   {
1209     guint64 val = ((guint64)states [1]) << 32;
1210     val += states [0];
1211     if (!accessible->states)
1212       accessible->states = _atspi_state_set_new_internal (accessible, val);
1213     else
1214       accessible->states->states = val;
1215   }
1216   accessible->cached_properties |= ATSPI_CACHE_STATES;
1217 }