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