2001-12-10 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_main.c
1 /*
2  *
3  * Basic SPI initialization and event loop function prototypes
4  *
5  */
6 #include <string.h>
7 #include <stdlib.h>
8 #include <cspi/spi-private.h>
9
10 #undef DEBUG_OBJECTS
11
12 static CORBA_Environment ev = { 0 };
13 static Accessibility_Registry registry = CORBA_OBJECT_NIL;
14 static GHashTable *live_refs = NULL;
15
16 static guint
17 spi_object_hash (gconstpointer key)
18 {
19   CORBA_Object object = (CORBA_Object) key;
20   guint        retval;
21   
22   retval = CORBA_Object_hash (object, 0, &ev);
23
24   return retval;
25 }
26
27 static gboolean
28 spi_object_equal (gconstpointer a, gconstpointer b)
29 {
30   CORBA_Object objecta = (CORBA_Object) a;
31   CORBA_Object objectb = (CORBA_Object) b;
32   gboolean     retval;
33
34   retval = CORBA_Object_is_equivalent (objecta, objectb, &ev);
35
36   return retval;
37 }
38
39 static void
40 spi_object_release (gpointer  value)
41 {
42   Accessible *a = (Accessible *) value;
43
44 #ifdef DEBUG_OBJECTS
45   g_print ("releasing %p => %p\n", a, a->objref);
46 #endif
47
48   bonobo_object_release_unref (a->objref, NULL);
49
50   memset (a, 0xaa, sizeof (Accessible));
51   a->ref_count = -1;
52
53 #ifndef DEBUG_OBJECTS
54   g_free (a);
55 #endif
56 }
57
58
59 SPIBoolean
60 cspi_accessible_is_a (Accessible *obj,
61                       const char *interface_name)
62 {
63   SPIBoolean        retval;
64   Bonobo_Unknown unknown;
65
66   if (obj == NULL)
67     {
68       return FALSE;
69     }
70
71   unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (obj),
72                                            interface_name, cspi_ev ());
73
74   if (BONOBO_EX (cspi_ev ()))
75     {
76       g_error ("Exception '%s' checking if is '%s'",
77                bonobo_exception_get_text (cspi_ev ()),
78                interface_name);
79     }
80
81   if (unknown != CORBA_OBJECT_NIL)
82     {
83       retval = TRUE;
84       bonobo_object_release_unref (unknown, NULL);
85     }
86   else
87     {
88       retval = FALSE;
89     }
90
91   return retval;
92 }
93
94 static GHashTable *
95 get_live_refs (void)
96 {
97   if (!live_refs) 
98     {
99       live_refs = g_hash_table_new_full (spi_object_hash,
100                                          spi_object_equal,
101                                          NULL,
102                                          spi_object_release);
103     }
104   return live_refs;
105 }
106
107 CORBA_Environment *
108 cspi_ev (void)
109 {
110   /* This method is an ugly hack */
111   return &ev;
112 }
113
114 Accessibility_Registry
115 cspi_registry (void)
116 {
117   return registry;
118 }
119
120 SPIBoolean
121 cspi_exception (void)
122 {
123   SPIBoolean retval;
124
125   if (BONOBO_EX (&ev))
126     {
127       CORBA_exception_free (&ev);
128       retval = TRUE;
129     }
130   else
131     {
132       retval = FALSE;
133     }
134
135   return retval;
136 }
137
138 Accessible *
139 cspi_object_add (CORBA_Object corba_object)
140 {
141   Accessible *ref;
142
143   if (corba_object == CORBA_OBJECT_NIL)
144     {
145       ref = NULL;
146     }
147   else if (!cspi_check_ev ("pre method check"))
148     {
149       ref = NULL;
150     }
151   else
152     {
153       if ((ref = g_hash_table_lookup (get_live_refs (), corba_object)))
154         {
155           g_assert (ref->ref_count > 0);
156           ref->ref_count++;
157           bonobo_object_release_unref (corba_object, NULL);
158 #ifdef DEBUG_OBJECTS
159           g_print ("returning cached %p => %p\n", ref, ref->objref);
160 #endif
161         }
162       else
163         {
164           ref = g_new (Accessible, 1);
165
166 #ifdef DEBUG_OBJECTS
167           g_print ("allocating %p => %p\n", ref, corba_object);
168 #endif
169
170           ref->objref = corba_object;
171           ref->ref_count = 1;
172
173           g_hash_table_insert (get_live_refs (), ref->objref, ref);
174         }
175     }
176
177   return ref;
178 }
179
180 void
181 cspi_object_ref (Accessible *accessible)
182 {
183   g_return_if_fail (accessible != NULL);
184
185   accessible->ref_count++;
186 }
187
188 void
189 cspi_object_unref (Accessible *accessible)
190 {
191   if (accessible == NULL)
192     {
193       return;
194     }
195
196   if (--accessible->ref_count == 0)
197     {
198       g_hash_table_remove (get_live_refs (), accessible->objref);
199     }
200 }
201
202 static void
203 cspi_cleanup (void)
204 {
205   GHashTable *refs;
206
207   refs = live_refs;
208   live_refs = NULL;
209   if (refs)
210     {
211       g_hash_table_destroy (refs);
212     }
213
214   if (registry != CORBA_OBJECT_NIL)
215     {
216       bonobo_object_release_unref (registry, NULL);
217       registry = CORBA_OBJECT_NIL;
218     }
219 }
220
221 static gboolean SPI_inited = FALSE;
222
223 /**
224  * SPI_init:
225  *
226  * Connects to the accessibility registry and initializes the SPI.
227  *
228  * Returns: 0 on success, otherwise an integer error code.
229  **/
230 int
231 SPI_init (void)
232 {
233   int argc = 0;
234   char *obj_id;
235
236   if (SPI_inited)
237     {
238       return 1;
239     }
240
241   SPI_inited = TRUE;
242
243   CORBA_exception_init (&ev);
244
245   if (!bonobo_init (&argc, NULL))
246     {
247       g_error ("Could not initialize Bonobo");
248     }
249
250   obj_id = "OAFIID:Accessibility_Registry:proto0.1";
251
252   registry = bonobo_activation_activate_from_id (
253           obj_id, 0, NULL, cspi_ev ());
254
255   if (ev._major != CORBA_NO_EXCEPTION)
256     {
257       g_error ("AT-SPI error: during registry activation: %s\n",
258                bonobo_exception_get_text (cspi_ev ()));
259     }
260
261   if (registry == CORBA_OBJECT_NIL)
262     {
263       g_error ("Could not locate registry");
264     }
265
266   bonobo_activate ();
267
268   g_atexit (cspi_cleanup);
269   
270   return 0;
271 }
272
273 /**
274  * SPI_event_main:
275  *
276  * Starts/enters the main event loop for the SPI services.
277  *
278  * (NOTE: This method does not return control, it is exited via a call to
279  *  SPI_event_quit () from within an event handler).
280  *
281  **/
282 void
283 SPI_event_main (void)
284 {
285   bonobo_main ();
286 }
287
288 /**
289  * SPI_event_quit:
290  *
291  * Quits the last main event loop for the SPI services,
292  * see SPI_event_main
293  **/
294 void
295 SPI_event_quit (void)
296 {
297   bonobo_main_quit ();
298 }
299
300 /**
301  * SPI_eventIsReady:
302  *
303  * Checks to see if an SPI event is waiting in the event queue.
304  * Used by clients that don't wish to use SPI_event_main().
305  *
306  * Not Yet Implemented.
307  *
308  * Returns: #TRUE if an event is waiting, otherwise #FALSE.
309  *
310  **/
311 SPIBoolean
312 SPI_eventIsReady ()
313 {
314   return FALSE;
315 }
316
317 /**
318  * SPI_nextEvent:
319  * @waitForEvent: a #SPIBoolean indicating whether to block or not.
320  *
321  * Gets the next event in the SPI event queue; blocks if no event
322  * is pending and @waitForEvent is #TRUE.
323  * Used by clients that don't wish to use SPI_event_main().
324  *
325  * Not Yet Implemented.
326  *
327  * Returns: the next #AccessibleEvent in the SPI event queue.
328  **/
329 AccessibleEvent *
330 SPI_nextEvent (SPIBoolean waitForEvent)
331 {
332   return NULL;
333 }
334
335 /**
336  * SPI_exit:
337  *
338  * Disconnects from the Accessibility Registry and releases 
339  * any floating resources. Call only once at exit.
340  *
341  * Returns: 0 if there were no leaks, otherwise non zero.
342  **/
343 int
344 SPI_exit (void)
345 {
346   int leaked;
347
348   if (!SPI_inited)
349     {
350       return 0;
351     }
352
353   SPI_inited = FALSE;
354
355   if (live_refs)
356     {
357       leaked = g_hash_table_size (live_refs);
358     }
359   else
360     {
361       leaked = 0;
362     }
363
364 #ifdef DEBUG_OBJECTS
365   if (leaked)
366     {
367       fprintf (stderr, "Leaked %d SPI handles\n", leaked);
368     }
369 #endif
370
371   cspi_cleanup ();
372
373   fprintf (stderr, "bye-bye!\n");
374
375   return leaked;
376 }