Merge branch 'master' of git+ssh://git.codethink.co.uk/git/atspi-dbus
[platform/core/uifw/at-spi2-atk.git] / cspi / spi-main.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 <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <cspi/spi-private.h>
34 #include "spi.h"
35
36 #undef DEBUG_OBJECTS
37
38 static DBusConnection *bus = NULL;
39 static GHashTable *apps = NULL;
40 static GHashTable *live_refs = NULL;
41 static GQueue *exception_handlers = NULL;
42 static DBusError exception;
43
44 static void
45 cspi_object_release (gpointer value)
46 {
47 }
48
49 gboolean
50 cspi_exception_throw (DBusError *error, const char *desc_prefix)
51 {
52   SPIExceptionHandler *handler = NULL;
53   SPIException ex;
54   if (exception_handlers) handler = g_queue_peek_head (exception_handlers);
55
56   ex.type = SPI_EXCEPTION_SOURCE_UNSPECIFIED;
57   ex.source = NULL;
58   ex.code = SPI_EXCEPTION_UNSPECIFIED;
59   ex.error = error;
60   // TODO: Fill in description
61   
62   if (handler)
63     return (*handler) (&ex, FALSE);
64   else
65     return FALSE; /* means exception was not handled */
66 }
67
68 const char *spi_path_dec = SPI_DBUS_PATH_DEC;
69 const char *spi_path_registry = SPI_DBUS_PATH_REGISTRY;
70 const char *spi_bus_registry = SPI_DBUS_NAME_REGISTRY;
71 const char *spi_path_desktop = SPI_DBUS_PATH_DESKTOP;
72 const char *spi_interface_accessible = SPI_DBUS_INTERFACE_ACCESSIBLE;
73 const char *spi_interface_action = SPI_DBUS_INTERFACE_ACTION;
74 const char *spi_interface_application = SPI_DBUS_INTERFACE_APPLICATION;
75 const char *spi_interface_component = SPI_DBUS_INTERFACE_COMPONENT;
76 const char *spi_interface_dec = SPI_DBUS_INTERFACE_DEC;
77 const char *spi_interface_desktop = SPI_DBUS_INTERFACE_DESKTOP;
78 const char *spi_interface_device_event_listener = SPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
79 const char *spi_interface_document = SPI_DBUS_INTERFACE_DOCUMENT;
80 const char *spi_interface_editable_text = SPI_DBUS_INTERFACE_EDITABLE_TEXT;
81 const char *spi_interface_hyperlink = SPI_DBUS_INTERFACE_HYPERLINK;
82 const char *spi_interface_hypertext = SPI_DBUS_INTERFACE_HYPERTEXT;
83 const char *spi_interface_image = SPI_DBUS_INTERFACE_IMAGE;
84 const char *spi_interface_registry = SPI_DBUS_INTERFACE_REGISTRY;
85 const char *spi_interface_selection = SPI_DBUS_INTERFACE_SELECTION;
86 const char *spi_interface_table = SPI_DBUS_INTERFACE_TABLE;
87 const char *spi_interface_text = SPI_DBUS_INTERFACE_TEXT;
88 const char *spi_interface_tree = SPI_DBUS_INTERFACE_TREE;
89 const char *spi_interface_value = SPI_DBUS_INTERFACE_VALUE;
90
91 static const char *interfaces[] =
92 {
93   SPI_DBUS_INTERFACE_ACCESSIBLE,
94   SPI_DBUS_INTERFACE_ACTION,
95   SPI_DBUS_INTERFACE_APPLICATION,
96   SPI_DBUS_INTERFACE_COMPONENT,
97   SPI_DBUS_INTERFACE_DOCUMENT,
98   SPI_DBUS_INTERFACE_EDITABLE_TEXT,
99   SPI_DBUS_INTERFACE_HYPERLINK,
100   SPI_DBUS_INTERFACE_HYPERTEXT,
101   SPI_DBUS_INTERFACE_IMAGE,
102   "org.freedesktop.atspi.LoginHelper",
103   SPI_DBUS_INTERFACE_SELECTION,
104   "org.freedesktop.atspi.Selector",
105   SPI_DBUS_INTERFACE_TABLE,
106   SPI_DBUS_INTERFACE_TEXT,
107   SPI_DBUS_INTERFACE_VALUE,
108   NULL
109 };
110
111 static gint get_iface_num (const char *iface)
112 {
113   // TODO: Use a binary search or hash to improve performance
114   int i;
115
116   for (i = 0; interfaces[i]; i++)
117   {
118     if (!strcmp(iface, interfaces[i])) return i;
119   }
120   return -1;
121 }
122
123 SPIBoolean
124 cspi_accessible_is_a (Accessible *accessible,
125                       const char *interface_name)
126 {
127   int n;
128
129   if (accessible == NULL)
130     {
131       return FALSE;
132     }
133
134   n = get_iface_num (interface_name);
135   if (n == -1) return FALSE;
136   return (SPIBoolean)((accessible->interfaces & (1 << n))? TRUE: FALSE);
137 }
138
139 static GHashTable *
140 cspi_get_live_refs (void)
141 {
142   if (!live_refs) 
143     {
144       live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
145     }
146   return live_refs;
147 }
148
149 DBusConnection *
150 SPI_bus (void)
151 {
152   return bus;
153 }
154
155 SPIBoolean
156 cspi_exception (void)
157 {
158   if (dbus_error_is_set (&exception))
159   {
160     dbus_error_free (&exception);
161     return TRUE;
162   }
163   return FALSE;
164 }
165
166 SPIBoolean
167 cspi_check_ev (const char *error_string)
168 {
169   if (dbus_error_is_set (&exception))
170   {
171     cspi_exception_throw (&exception, error_string);
172     return FALSE;
173   }
174   return TRUE;
175 }
176
177 Accessible *
178 cspi_object_add (Accessible *accessible)
179 {
180   if (accessible) cspi_object_ref (accessible);
181   return accessible;
182 }
183
184 void
185 cspi_object_ref (Accessible *accessible)
186 {
187   g_return_if_fail (accessible != NULL);
188
189   accessible->ref_count++;
190   g_hash_table_insert (live_refs, accessible, accessible);
191 }
192
193 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, spi_bus_registry))
194
195 static void
196 cspi_object_unref_internal (Accessible *accessible, gboolean defunct)
197 {
198   gboolean cached;
199
200   if (accessible == NULL)
201     {
202       return;
203     }
204
205   if (--accessible->ref_count == 0 || (accessible->ref_count == 1 && !defunct) && g_hash_table_lookup (live_refs, accessible))
206   {
207     AccessibleEvent e;
208     memset (&e, 0, sizeof(e));
209     e.type = "object:state-change:defunct";
210     e.source = accessible;
211     e.detail1 = 1;
212     cspi_dispatch_event (&e);
213     g_hash_table_remove (live_refs, accessible);
214   }
215   if (accessible->ref_count == 0)
216   {
217     if (APP_IS_REGISTRY (accessible->app))
218     {
219       g_free (accessible->v.path);
220     }
221     g_free(accessible);
222   }
223 }
224
225 void
226 cspi_object_unref (Accessible *accessible)
227 {
228   cspi_object_unref_internal (accessible, FALSE);
229 }
230
231 static void
232 cspi_cleanup (void)
233 {
234   GHashTable *refs;
235
236   cspi_streams_close_all ();
237
238   refs = live_refs;
239   live_refs = NULL;
240   if (refs)
241     {
242       g_hash_table_destroy (refs);
243     }
244 }
245
246 static gboolean SPI_inited = FALSE;
247
248 static GHashTable *app_hash = NULL;
249
250 static Accessible *
251 ref_accessible (CSpiApplication *app, const char *path)
252 {
253   int id;
254   guint *id_val;
255
256   if (sscanf (path, "/org/freedesktop/atspi/accessible/%d", &id) != 1)
257   {
258     return NULL;
259   }
260   Accessible *a = g_hash_table_lookup (app->hash, &id);
261   if (a)
262   {
263     cspi_object_ref (a);
264     return a;
265   }
266   id_val = g_new (guint, 1);
267   if (!id_val) return NULL;
268   *id_val = id;
269   a = g_new0 (Accessible, 1);
270   if (!a)
271   {
272     g_free (id_val);
273     return NULL;
274   }
275   g_hash_table_insert (app->hash, id_val, a);
276   a->app = app;
277   a->v.id = id;
278   a->ref_count = 2;     /* one for the caller, one for the hash */
279   return a;
280 }
281
282 static CSpiApplication *
283 cspi_get_application (const char *bus_name)
284 {
285   CSpiApplication *app = NULL;
286   char *bus_name_dup;
287
288   if (!app_hash)
289   {
290     app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_unref);
291     if (!app_hash) return;
292   }
293   app = g_hash_table_lookup (app_hash, bus_name);
294   if (app) return app;
295   bus_name_dup = g_strdup (bus_name);
296   if (!bus_name_dup) return NULL;
297   // TODO: change below to something that will send state-change:defunct notification if necessary */
298   app = g_new (CSpiApplication, 1);
299   if (!app) return NULL;
300   app->bus_name = bus_name_dup;
301   if (APP_IS_REGISTRY (app))
302   {
303     app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, cspi_object_unref);
304   }
305   else
306   {
307     app->hash = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, cspi_object_unref);
308   }
309   g_hash_table_insert (app_hash, bus_name_dup, app);
310   return app;
311 }
312
313 typedef struct
314 {
315   char *path;
316   char *parent;
317   GArray *children;
318   GArray *interfaces;
319   char *name;
320   dbus_uint32_t role;
321   char *description;
322 } CACHE_ADDITION;
323
324 /* Update the cache with added/modified objects and free the array */
325 static void
326 handle_additions (CSpiApplication*app, GArray *additions)
327 {
328   gint i, j;
329   GList *l, *new_list;
330
331   if (!additions)
332   {
333     return;
334   }
335   for (i = 0; i < additions->len; i++)
336   {
337     CACHE_ADDITION *ca = &g_array_index (additions, CACHE_ADDITION, i);
338     Accessible *a = ref_accessible (app, ca->path);
339       /* Note: children don't hold refs for their parents or vice versa */
340     a->parent = ref_accessible (app, ca->parent);
341     if (a->parent) cspi_object_unref (a->parent);
342     if (a->children)
343     {
344       g_list_free (a->children);
345       a->children = NULL;
346     }
347     for (j = 0; j < ca->children->len; j++)
348     {
349       const char *child_path = g_array_index (ca->children, const char *, j);
350       Accessible *child = ref_accessible (app, child_path);
351       new_list = g_list_append (a->children, child);
352       if (new_list) a->children = new_list;
353       cspi_object_unref (child);
354     }
355     a->interfaces = 0;
356     for (j = 0; j < ca->interfaces->len; j++)
357     {
358       const char *iface = g_array_index (ca->interfaces, const char *, j);
359       if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
360       gint n = get_iface_num (iface);
361       if (n == -1)
362       {
363         g_warning ("Unknown interface %s", iface);
364       }
365       else a->interfaces |= (1 << n);
366       g_free (iface);
367     }
368     if (a->name) g_free (a->name);
369     a->name = ca->name;
370     a->role = ca->role;
371     if (a->description) g_free (a->description);
372     a->description = ca->description;
373     g_array_free (ca->interfaces, TRUE);
374     g_array_free (ca->children, TRUE);
375     /* This is a bit of a hack since ref_accessible sets ref_count to 2
376      * for a new object, one of the refs being for the cache */
377     cspi_object_unref (a);
378   }
379   g_array_free (additions, TRUE);
380 }
381
382 static void
383 handle_removals (CSpiApplication *app, GArray *removals)
384 {
385   gint j;
386
387   if (!removals) return;
388   for (j = 0; j < removals->len; j++)
389   {
390     const char *path = g_array_index (removals, const char *, j);
391     Accessible *a = ref_accessible (app, path);
392     if (a->parent && g_list_find (a->parent->children, a))
393     {
394       a->parent->children = g_list_remove (a->parent->children, a);
395       /* Note: children don't hold refs for their parents or vice versa */
396     }
397     g_hash_table_remove (app->hash, &a->v.id);
398     cspi_object_unref_internal (a, TRUE);       /* unref our own ref */
399   }
400   g_array_free (removals, TRUE);
401 }
402
403 static gboolean
404 add_app_to_desktop (Accessible *a, const char *bus_name)
405 {
406   DBusError error;
407   char *root_path;
408
409   dbus_error_init (&error);
410   if (dbind_connection_method_call (bus, bus_name, "/org/freedesktop/atspi/tree", spi_interface_tree, "getRoot", &error, "=>o", &root_path))
411   {
412     Accessible *obj = cspi_ref_accessible (bus_name, root_path);
413     if (obj)
414     {
415       GList *new_list = g_list_append (a->children, obj);
416       if (new_list)
417       {
418         a->children = new_list;
419         return TRUE;
420       }
421     }
422     g_free (root_path);
423   }
424   else
425   {
426     g_warning ("Error calling getRoot for %s: %s", bus_name, error.message);
427   }
428   return FALSE;
429 }
430
431 static void
432 send_children_changed (Accessible *parent, Accessible *child, gboolean add)
433 {
434   AccessibleEvent e;
435
436   memset (&e, 0, sizeof(e));
437   e.type = (add? "object:children-changed:add": "object:children-changed:remove");
438   e.source = parent;
439   e.detail1 = g_list_index (parent->children, child);
440   cspi_dispatch_event (&e);
441 }
442
443 static void
444 unref_object_and_children (Accessible *obj)
445 {
446   GList *l;
447
448   for (l = obj->children; l; l = l->next)
449   {
450     unref_object_and_children (l->data);
451   }
452   cspi_object_unref_internal (obj, TRUE);
453 }
454
455 static gboolean
456 remove_app_from_desktop (Accessible *a, const char *bus_name)
457 {
458   GList *l;
459   Accessible *child;
460
461   for (l = a->children; l; l = l->next)
462   {
463     child = l->data;
464     if (!strcmp (bus_name, child->app->bus_name)) break;
465   }
466   if (!l)
467   {
468     g_warning ("Removing unregistered app %s; doing nothing\n", bus_name);
469     return FALSE;
470   }
471   send_children_changed (a, child, FALSE);
472   a->children = g_list_remove (a->children, child);
473   unref_object_and_children (child);
474   return TRUE;
475 }
476
477 static Accessible *desktop;
478
479 static Accessible *
480 ref_accessible_desktop (CSpiApplication *app)
481 {
482   DBusError error;
483   GArray *apps = NULL;
484   GArray *additions;
485   gint i;
486
487   if (desktop)
488   {
489     cspi_object_ref (desktop);
490     return desktop;
491   }
492   desktop = g_new0 (Accessible, 1);
493   if (!desktop)
494   {
495     return NULL;
496   }
497   g_hash_table_insert (app->hash, "", desktop);
498   desktop->app = app;
499   desktop->ref_count = 2;       /* one for the caller, one for the hash */
500   desktop->name = g_strdup ("");
501   dbus_error_init (&error);
502   if (!dbind_connection_method_call (bus, spi_bus_registry, spi_path_registry, spi_interface_registry, "getApplications", &error, "=>as", &apps))
503   {
504     g_error ("Couldn't get application list: %s", error.message);
505   }
506   for (i = 0; i < apps->len; i++)
507   {
508     const char *app_name = g_array_index (apps, char *, i);
509     if (app_name[0] == '\0')
510     {
511       g_warning ("Got empty app name");
512       continue;
513     }
514     CSpiApplication *app = cspi_get_application (app_name);
515     additions = NULL;
516     dbus_error_init (&error);
517     dbind_connection_method_call (bus, app_name, "/org/freedesktop/atspi/tree", spi_interface_tree, "getTree", &error, "=>a(ooaoassus)", &additions);
518     if (error.message)
519     {
520       g_warning ("getTree (%s): %s", app_name, error.message);
521     }
522     handle_additions (app, additions);
523     add_app_to_desktop (desktop, app_name);
524   }
525   g_array_free (apps, TRUE);
526   return desktop;
527 }
528
529 Accessible *
530 cspi_ref_accessible (const char *app, const char *path)
531 {
532   CSpiApplication *a = cspi_get_application (app);
533   if (!a) return NULL;
534   if ( APP_IS_REGISTRY(a))
535   {
536     return ref_accessible_desktop (a);
537   }
538   return ref_accessible (a, path);
539 }
540
541 Accessible *
542 cspi_ref_related_accessible (Accessible *obj, const char *path)
543 {
544   return ref_accessible (obj->app, path);
545 }
546
547 typedef struct
548 {
549   GArray *additions;
550   GArray *removals;
551 } CacheSignalData;
552
553 static const char *cacheSignalType = "a(ooaoassus)ao";
554
555 static DBusHandlerResult
556 cspi_dbus_handle_update_tree (DBusConnection *bus, DBusMessage *message, void *user_data)
557 {
558   DBusMessageIter iter;
559   CacheSignalData cd;
560   void *p = &cd;
561   const char *sender = dbus_message_get_sender (message);
562   CSpiApplication *app = cspi_get_application (sender);
563   const char *type = cacheSignalType;
564
565   if (!app)
566   {
567     g_warning ("UpdateTree from unknown app.  Should we add it?", sender);
568     return;
569   }
570   dbus_message_iter_init (message, &iter);
571   // TODO: Check signature
572   dbind_any_demarshal (&iter, &type, &p);       /* additions */
573   dbind_any_demarshal (&iter, &type, &p);       /* removals */
574   handle_additions (app, cd.additions);
575   handle_removals (app, cd.removals);
576 }
577
578 static DBusHandlerResult
579 cspi_dbus_handle_register_application (DBusConnection *bus, DBusMessage *message, void *user_data)
580 {
581   DBusError error;
582   dbus_uint32_t v;
583   Accessible *a;
584   char *bus_name;
585
586   dbus_error_init (&error);
587   a = cspi_ref_accessible (spi_bus_registry, spi_path_registry);
588   if (add_app_to_desktop (a, dbus_message_get_sender (message)))
589   {
590     send_children_changed (a, g_list_last (a->children)->data, TRUE);
591   }
592   cspi_object_unref (a);
593   return DBUS_HANDLER_RESULT_HANDLED;
594 }
595
596 static DBusHandlerResult
597 cspi_dbus_handle_deregister_application (DBusConnection *bus, DBusMessage *message, void *user_data)
598 {
599   Accessible *a;
600   DBusError error;
601   char *bus_name;
602  
603   dbus_error_init (&error);
604   if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &bus_name, DBUS_TYPE_INVALID))
605   {
606     g_warning ("Error processing %s: %s\n", dbus_message_get_member(message), error.message);
607     dbus_error_free (&error);
608     return DBUS_HANDLER_RESULT_HANDLED;
609   }
610
611   a = cspi_ref_accessible (spi_bus_registry, spi_path_registry);
612   remove_app_from_desktop (a, bus_name);
613   cspi_object_unref (a);
614   return DBUS_HANDLER_RESULT_HANDLED;
615 }
616
617
618 static DBusHandlerResult
619 cspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
620 {
621   int type = dbus_message_get_type (message);
622   const char *interface = dbus_message_get_interface (message);
623   const char *member = dbus_message_get_member (message); 
624   dbus_uint32_t v;
625   char *bus_name;
626
627   if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
628       !strcmp (interface, SPI_DBUS_INTERFACE_ACCESSIBLE))
629   {
630     return cspi_dbus_handle_event (bus, message, data);
631   }
632   if (dbus_message_is_method_call (message, spi_interface_device_event_listener, "notifyEvent"))
633   {
634     return cspi_dbus_handle_deviceEvent (bus, message, data);
635   }
636   if (dbus_message_is_signal (message, spi_interface_tree, "updateTree"))
637   {
638     return cspi_dbus_handle_update_tree (bus, message, data);
639   }
640   if (dbus_message_is_signal (message, spi_interface_tree, "registerApplication"))
641   {
642     return cspi_dbus_handle_register_application (bus, message, data);
643   }
644   if (dbus_message_is_signal (message, spi_interface_registry, "deregisterApplication"))
645   {
646     return cspi_dbus_handle_deregister_application (bus, message, data);
647   }
648   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
649 }
650
651 /**
652  * SPI_init:
653  *
654  * Connects to the accessibility registry and initializes the SPI.
655  *
656  * Returns: 0 on success, otherwise an integer error code.  
657  **/
658 int
659 SPI_init (void)
660 {
661   DBusError error;
662   char *match;
663
664   if (SPI_inited)
665     {
666       return 1;
667     }
668
669   SPI_inited = TRUE;
670
671   g_type_init ();
672
673   cspi_get_live_refs();
674   g_atexit (cspi_cleanup);
675
676   dbus_error_init (&error);
677   bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
678   if (!bus)
679   {
680     g_error ("Couldn't get session bus");
681     return 2;
682   }
683   dbus_bus_register (bus, &error);
684   dbus_connection_setup_with_g_main(bus, g_main_context_default());
685   dbus_connection_add_filter (bus, cspi_dbus_filter, NULL, NULL);
686   match = g_strdup_printf ("type='signal',interface='%s',member='updateTree'", spi_interface_tree);
687   dbus_error_init (&error);
688   dbus_bus_add_match (bus, match, &error);
689   g_free (match);
690   match = g_strdup_printf ("type='signal',interface='%s'", spi_interface_tree);
691   dbus_bus_add_match (bus, match, &error);
692   g_free (match);
693   return 0;
694 }
695
696   static GMainLoop *mainloop;
697
698 /**
699  * SPI_event_main:
700  *
701  * Starts/enters the main event loop for the SPI services.
702  *
703  * (NOTE: This method does not return control, it is exited via a call to
704  *  SPI_event_quit () from within an event handler).
705  *
706  **/
707 void
708 SPI_event_main (void)
709 {
710
711   mainloop = g_main_loop_new (NULL, FALSE);
712   g_main_loop_run (mainloop);
713 }
714
715 /**
716  * SPI_event_quit:
717  *
718  * Quits the last main event loop for the SPI services,
719  * see SPI_event_main
720  **/
721 void
722 SPI_event_quit (void)
723 {
724   g_main_loop_quit (mainloop);
725 }
726
727 /**
728  * SPI_eventIsReady:
729  *
730  * Checks to see if an SPI event is waiting in the event queue.
731  * Used by clients that don't wish to use SPI_event_main().
732  *
733  * Not Yet Implemented.
734  *
735  * Returns: #TRUE if an event is waiting, otherwise #FALSE.
736  *
737  **/
738 SPIBoolean
739 SPI_eventIsReady (void)
740 {
741   return FALSE;
742 }
743
744 /**
745  * SPI_nextEvent:
746  * @waitForEvent: a #SPIBoolean indicating whether to block or not.
747  *
748  * Gets the next event in the SPI event queue; blocks if no event
749  * is pending and @waitForEvent is #TRUE.
750  * Used by clients that don't wish to use SPI_event_main().
751  *
752  * Not Yet Implemented.
753  *
754  * Returns: the next #AccessibleEvent in the SPI event queue.
755  **/
756 AccessibleEvent *
757 SPI_nextEvent (SPIBoolean waitForEvent)
758 {
759   return NULL;
760 }
761
762 #ifdef PRINT_LEAKS
763 static void
764 report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
765 {
766   char *name, *role;
767   Accessible *a = (Accessible *) val;
768   
769   name = Accessible_getName (a);
770   if (cspi_exception ())
771     {
772       name = NULL;
773     }
774
775   role = Accessible_getRoleName (a);
776   if (cspi_exception ())
777     {
778       role = NULL;
779     }
780
781   fprintf (stderr, "leaked %d references to object %s, role %s %p\n",
782            a->ref_count, name ? name : "<?>", role ? role : "<?>", a);
783
784   SPI_freeString (name);
785 }
786 #endif
787
788 /**
789  * SPI_exit:
790  *
791  * Disconnects from the Accessibility Registry and releases 
792  * any floating resources. Call only once at exit.
793  *
794  * Returns: 0 if there were no leaks, otherwise non zero.
795  **/
796 int
797 SPI_exit (void)
798 {
799   int leaked;
800
801   if (!SPI_inited)
802     {
803       return 0;
804     }
805
806   SPI_inited = FALSE;
807
808   if (live_refs)
809     {
810       leaked = g_hash_table_size (live_refs);
811 #ifdef DEBUG_OBJECTS
812       fprintf (stderr, "Leaked %d SPI handles\n", leaked);
813
814 #define PRINT_LEAKS
815 #ifdef PRINT_LEAKS
816       g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
817 #endif
818
819 #endif
820     }
821   else
822     {
823       leaked = 0;
824     }
825
826   cspi_cleanup ();
827
828   return leaked;
829 }
830
831 /**
832  * SPI_freeString:
833  * @s: a character string returned from another at-spi call.
834  *
835  * Free a character string returned from an at-spi call.  Clients of
836  * at-spi should use this function instead of free () or g_free().
837  * A NULL string @s will be silently ignored.
838  * This API should not be used to free strings
839  * from other libraries or allocated by the client.
840  **/
841 void
842 SPI_freeString (char *s)
843 {
844   if (s)
845     {
846       g_free (s);
847     }
848 }
849
850 /**
851  * SPI_freeRect:
852  * @r: a pointer to an SPIRect returned from another at-spi call.
853  *
854  * Free a SPIRect structure returned from an at-spi call.  Clients of
855  * at-spi should use this function instead of free () or g_free().
856  * A NULL rect @r will be silently ignored.
857  * This API should not be used to free data
858  * from other libraries or allocated by the client.
859  *
860  * @Since: AT-SPI 1.6
861  **/
862 void
863 SPI_freeRect (SPIRect *r)
864 {
865   if (r)
866     {
867       /* err, okay, in this case the client _could_ 
868          have called g_free, but we don't want to guarantee it */
869       g_free (r);
870     }
871 }
872
873 /**
874  * SPI_dupString:
875  * @s: a UTF-8 string to be duplicated
876  * 
877  * @Since: AT-SPI 1.4
878  *
879  * Returns: a duplicate of the string passed as a parameter, which should
880  * be freed via SPI_freeString after use.
881  **/
882 char *
883 SPI_dupString (char *s)
884 {
885   if (s)
886     {
887       return g_strdup (s);
888     }
889   else 
890     return NULL;
891 }
892
893 /**
894  * SPI_exceptionHandlerPush:
895  * @handler: an #SPIExceptionHandler to install as the first code to deal with exceptions.
896  *
897  * Install a client-side handler for #SPIException instances, which can see and handle any
898  * exceptions before chaining them to the next exception handler in the stack.
899  *
900  * @Since: AT-SPI 1.4
901  *
902  * Returns %TRUE if the result succeeded, %FALSE if @hander could not be registered.
903  **/
904 SPIBoolean SPI_exceptionHandlerPush (SPIExceptionHandler *handler)
905 {
906   if (!exception_handlers)
907     exception_handlers = g_queue_new ();
908   g_queue_push_head (exception_handlers, handler);
909   return TRUE;
910 }
911
912 /**
913  * SPI_exceptionHandlerPop:
914  * 
915  * Remove/pop an #SPIExceptionHandler off the error handler stack and return the new handler.
916  *
917  * @Since: AT-SPI 1.4
918  *
919  * Returns the #SPIExceptionHandler which is now at the top of the error handler stack after the call.
920  **/
921 SPIExceptionHandler* SPI_exceptionHandlerPop (void)
922 {
923   return (SPIExceptionHandler *) g_queue_pop_head (exception_handlers);
924 }
925
926 /**
927  * SPIException_getSourceType:
928  * @err: the exception being queried
929  *
930  * Get the #SPIExceptionType of an exception which has been thrown.
931  *
932  * @Since: AT-SPI 1.4
933  *
934  * Returns: the #SPIExceptionType corresponding to exception @err.
935  **/
936 SPIExceptionType SPIException_getSourceType (SPIException *err)
937 {
938   if (err)
939     return err->type;
940   else
941     return SPI_EXCEPTION_SOURCE_UNSPECIFIED;
942 }
943
944 /**
945  * SPIException_getExceptionCode:
946  * @err: the #SPIException being queried.
947  *
948  * Get the #SPIExceptionCode telling what type of exception condition has occurred.
949  *
950  * @Since: AT-SPI 1.4
951  *
952  * Returns: the #SPIExceptionCode corresponding to exception @err.
953  **/
954 SPIExceptionCode SPIException_getExceptionCode (SPIException *err)
955 {  
956   return err->code;
957 }
958
959 /**
960  * SPIAccessibleException_getSource:
961  * @err: the #SPIException being queried.
962  *
963  * Get the identity of the object which threw an exception.
964  *
965  * @Since: AT-SPI 1.4
966  *
967  * Returns: a pointer to the #Accessible object which threw the exception.
968  **/
969 Accessible* SPIAccessibleException_getSource (SPIException *err)
970 {
971   if (err->type == SPI_EXCEPTION_SOURCE_ACCESSIBLE)
972     cspi_object_ref (err->source);
973     return err->source;
974   return NULL;
975 }
976
977 /**
978  * SPIException_getDescription:
979  * @err: the #SPIException being queried.
980  *
981  * Get a text description of the exception that has been thrown.
982  * Unfortunately these descriptions tend to be terse and limited in
983  * the detail which they can provide.
984  *
985  * Returns: a brief character string describing the exception.
986  **/
987 char* SPIException_getDescription (SPIException *err)
988 {
989   /* TODO: friendlier error messages? */
990   if (err->error)
991     return err->error->message;
992   return NULL;
993 }
994
995 static char *
996 get_path (Accessible *obj)
997 {
998   if (APP_IS_REGISTRY (obj->app))
999   {
1000     return g_strdup_printf (SPI_DBUS_PATH_REGISTRY);
1001   }
1002   return g_strdup_printf ("/org/freedesktop/atspi/accessible/%d", obj->v.id);
1003 }
1004
1005 dbus_bool_t
1006 cspi_dbus_call (Accessible *obj, const char *interface, const char *method, DBusError *error, const char *type, ...)
1007 {
1008   va_list args;
1009   char *path = get_path (obj);
1010   dbus_bool_t retval;
1011   DBusError err;
1012
1013   if (!error) error = &err;
1014   dbus_error_init (error);
1015   va_start (args, type);
1016   retval = dbind_connection_method_call_va (SPI_bus(), obj->app->bus_name, path, interface, method, error, type, args);
1017   va_end (args);
1018   g_free (path);
1019   if (dbus_error_is_set (error))
1020   {
1021     if (!dbus_error_is_set (&exception))
1022     {
1023       // TODO: Should we call cspi_exception_throw?
1024       dbus_move_error (error, &exception);
1025     }
1026     else if (error == &err) dbus_error_free (error);
1027   }
1028   return retval;
1029 }
1030
1031 dbus_bool_t
1032 cspi_dbus_get_property (Accessible *obj, const char *interface, const char *name, DBusError *error, const char *type, void *data)
1033 {
1034   DBusMessage *message, *reply;
1035   char *path = get_path (obj);
1036   DBusMessageIter iter, iter_variant;
1037   DBusError err;
1038   dbus_bool_t retval = FALSE;
1039
1040   message = dbus_message_new_method_call (obj->app->bus_name, path, "org.freedesktop.DBus.Properties", "Get");
1041   if (!message)
1042   {
1043     // TODO: throw exception
1044     goto done;
1045   }
1046   dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1047   if (!error) error = &err;
1048   dbus_error_init (error);
1049   reply = dbus_connection_send_with_reply_and_block (SPI_bus(), message, 1000, error);
1050   dbus_message_unref (message);
1051   if (!reply)
1052   {
1053     // TODO: throw exception
1054     goto done;
1055   }
1056   dbus_message_iter_init (reply, &iter);
1057   dbus_message_iter_recurse (&iter, &iter_variant);
1058   if (dbus_message_iter_get_arg_type (&iter_variant) != type[0])
1059   {
1060     g_warning ("cspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1061     goto done;
1062   }
1063   dbus_message_iter_get_basic (&iter_variant, data);
1064   dbus_message_unref (reply);
1065   if (type[0] == 's') *(char **)data = g_strdup (*(char **)data);
1066   retval = TRUE;
1067 done:
1068   g_free (path);
1069   return retval;
1070 }