Changes to event notification, to fix bugs associated with the use of oneways.
[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 Sun Microsystems Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /*
24  *
25  * Basic SPI initialization and event loop function prototypes
26  *
27  */
28 #include <string.h>
29 #include <stdlib.h>
30 #include <cspi/spi-private.h>
31
32 #undef DEBUG_OBJECTS
33
34 static CORBA_Environment ev = { 0 };
35 static Accessibility_Registry registry = CORBA_OBJECT_NIL;
36 static GHashTable *live_refs = NULL;
37
38 static guint
39 cspi_object_hash (gconstpointer key)
40 {
41   CORBA_Object object = (CORBA_Object) key;
42   guint        retval;
43   
44   retval = CORBA_Object_hash (object, 0, &ev);
45
46   return retval;
47 }
48
49 static gboolean
50 cspi_object_equal (gconstpointer a, gconstpointer b)
51 {
52   CORBA_Object objecta = (CORBA_Object) a;
53   CORBA_Object objectb = (CORBA_Object) b;
54   gboolean     retval;
55
56   retval = CORBA_Object_is_equivalent (objecta, objectb, &ev);
57
58   return retval;
59 }
60
61 static void
62 cspi_object_release (gpointer  value)
63 {
64   Accessible *a = (Accessible *) value;
65
66 #ifdef DEBUG_OBJECTS
67   g_print ("releasing %p => %p\n", a, a->objref);
68 #endif
69
70   cspi_release_unref (a->objref);
71
72   memset (a, 0xaa, sizeof (Accessible));
73   a->ref_count = -1;
74
75 #ifndef DEBUG_OBJECTS
76   free (a);
77 #endif
78 }
79
80 SPIBoolean
81 cspi_accessible_is_a (Accessible *obj,
82                       const char *interface_name)
83 {
84   SPIBoolean        retval;
85   Bonobo_Unknown unknown;
86
87   if (obj == NULL)
88     {
89       return FALSE;
90     }
91
92   unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (obj),
93                                            interface_name, cspi_ev ());
94
95   if (ev._major != CORBA_NO_EXCEPTION)
96     {
97       g_error ("Exception '%s' checking if is '%s'",
98                cspi_exception_get_text (),
99                interface_name);
100     }
101
102   if (unknown != CORBA_OBJECT_NIL)
103     {
104       retval = TRUE;
105       cspi_release_unref (unknown);
106     }
107   else
108     {
109       retval = FALSE;
110     }
111
112   return retval;
113 }
114
115 static GHashTable *
116 cspi_get_live_refs (void)
117 {
118   if (!live_refs) 
119     {
120       live_refs = g_hash_table_new_full (cspi_object_hash,
121                                          cspi_object_equal,
122                                          NULL,
123                                          cspi_object_release);
124     }
125   return live_refs;
126 }
127
128 CORBA_Environment *
129 cspi_ev (void)
130 {
131   /* This method is an ugly hack */
132   return &ev;
133 }
134
135 Accessibility_Registry
136 cspi_registry (void)
137 {
138   return registry;
139 }
140
141 SPIBoolean
142 cspi_exception (void)
143 {
144   SPIBoolean retval;
145
146   if (ev._major != CORBA_NO_EXCEPTION)
147     {
148       CORBA_exception_free (&ev);
149       retval = TRUE;
150     }
151   else
152     {
153       retval = FALSE;
154     }
155
156   return retval;
157 }
158
159 Accessible *
160 cspi_object_new (CORBA_Object corba_object)
161 {
162   Accessible *ref;
163
164   if (corba_object == CORBA_OBJECT_NIL)
165     {
166       ref = NULL;
167     }
168   else if (!cspi_check_ev ("pre method check: add"))
169     {
170       ref = NULL;
171     }
172   else
173     {
174       ref = malloc (sizeof (Accessible));
175 //    ref->objref = CORBA_Object_duplicate (corba_object, cspi_ev ());
176       ref->objref = corba_object;
177       ref->ref_count = 1;
178     }
179
180   return ref;
181 }
182
183 Accessible *
184 cspi_object_add (CORBA_Object corba_object)
185 {
186         return cspi_object_add_ref (corba_object, FALSE);
187 }
188
189 Accessible *
190 cspi_object_add_ref (CORBA_Object corba_object, gboolean do_ref)
191 {
192   Accessible *ref;
193
194   if (corba_object == CORBA_OBJECT_NIL)
195     {
196       ref = NULL;
197     }
198   else if (!cspi_check_ev ("pre method check: add"))
199     {
200       ref = NULL;
201     }
202   else
203     {
204       if ((ref = g_hash_table_lookup (cspi_get_live_refs (), corba_object)))
205         {
206           g_assert (ref->ref_count > 0);
207           ref->ref_count++;
208           if (!do_ref) 
209                 cspi_release_unref (corba_object);
210 #ifdef DEBUG_OBJECTS
211           g_print ("returning cached %p => %p\n", ref, ref->objref);
212 #endif
213         }
214       else
215         {
216           ref = cspi_object_new (corba_object);
217
218 #ifdef DEBUG_OBJECTS
219           g_print ("allocated %p => %p\n", ref, corba_object);
220 #endif
221           if (do_ref) {
222 #ifdef JAVA_BRIDGE_BUG_IS_FIXED
223                   g_assert (CORBA_Object_is_a (corba_object,
224                                                 "IDL:Bonobo/Unknown:1.0", cspi_ev ()));
225 #endif
226                   Bonobo_Unknown_ref (corba_object, cspi_ev ());
227           }
228           g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
229         }
230     }
231
232   return ref;
233 }
234
235 void
236 cspi_object_ref (Accessible *accessible)
237 {
238   g_return_if_fail (accessible != NULL);
239
240   if (g_hash_table_lookup (cspi_get_live_refs (), accessible->objref) == NULL) {
241           accessible->objref = bonobo_object_dup_ref (accessible->objref, cspi_ev ());
242           g_hash_table_insert (cspi_get_live_refs (), accessible->objref, accessible);    
243   } else {
244           accessible->ref_count++;
245   }
246 }
247
248 void
249 cspi_object_unref (Accessible *accessible)
250 {
251   if (accessible == NULL)
252     {
253       return;
254     }
255
256   if (--accessible->ref_count == 0)
257     {
258       g_hash_table_remove (cspi_get_live_refs (), accessible->objref);
259     }
260 }
261
262 static void
263 cspi_cleanup (void)
264 {
265   GHashTable *refs;
266
267   refs = live_refs;
268   live_refs = NULL;
269   if (refs)
270     {
271       g_hash_table_destroy (refs);
272     }
273
274   if (registry != CORBA_OBJECT_NIL)
275     {
276       cspi_release_unref (registry);
277       registry = CORBA_OBJECT_NIL;
278     }
279 }
280
281 static gboolean SPI_inited = FALSE;
282
283 /**
284  * SPI_init:
285  *
286  * Connects to the accessibility registry and initializes the SPI.
287  *
288  * Returns: 0 on success, otherwise an integer error code.
289  **/
290 int
291 SPI_init (void)
292 {
293   if (SPI_inited)
294     {
295       return 1;
296     }
297
298   SPI_inited = TRUE;
299
300   CORBA_exception_init (&ev);
301
302   registry = cspi_init ();
303
304   g_atexit (cspi_cleanup);
305   
306   return 0;
307 }
308
309 /**
310  * SPI_event_main:
311  *
312  * Starts/enters the main event loop for the SPI services.
313  *
314  * (NOTE: This method does not return control, it is exited via a call to
315  *  SPI_event_quit () from within an event handler).
316  *
317  **/
318 void
319 SPI_event_main (void)
320 {
321   cspi_main ();
322 }
323
324 /**
325  * SPI_event_quit:
326  *
327  * Quits the last main event loop for the SPI services,
328  * see SPI_event_main
329  **/
330 void
331 SPI_event_quit (void)
332 {
333   cspi_main_quit ();
334 }
335
336 /**
337  * SPI_eventIsReady:
338  *
339  * Checks to see if an SPI event is waiting in the event queue.
340  * Used by clients that don't wish to use SPI_event_main().
341  *
342  * Not Yet Implemented.
343  *
344  * Returns: #TRUE if an event is waiting, otherwise #FALSE.
345  *
346  **/
347 SPIBoolean
348 SPI_eventIsReady ()
349 {
350   return FALSE;
351 }
352
353 /**
354  * SPI_nextEvent:
355  * @waitForEvent: a #SPIBoolean indicating whether to block or not.
356  *
357  * Gets the next event in the SPI event queue; blocks if no event
358  * is pending and @waitForEvent is #TRUE.
359  * Used by clients that don't wish to use SPI_event_main().
360  *
361  * Not Yet Implemented.
362  *
363  * Returns: the next #AccessibleEvent in the SPI event queue.
364  **/
365 AccessibleEvent *
366 SPI_nextEvent (SPIBoolean waitForEvent)
367 {
368   return NULL;
369 }
370
371 static void
372 report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
373 {
374         Accessible *a = (Accessible *) val;
375         char *name, *role;
376         name = Accessible_getName (a);
377         if (cspi_exception ()) name = NULL;
378         role = Accessible_getRoleName (a);
379         if (cspi_exception ()) role = NULL;
380         fprintf (stderr, "leaked object %s, role %s\n", (name) ? name : "<?>",
381                  (role) ? role : "<?>");
382         if (name) SPI_freeString (name);
383 }
384
385
386 /**
387  * SPI_exit:
388  *
389  * Disconnects from the Accessibility Registry and releases 
390  * any floating resources. Call only once at exit.
391  *
392  * Returns: 0 if there were no leaks, otherwise non zero.
393  **/
394 int
395 SPI_exit (void)
396 {
397   int leaked;
398
399   if (!SPI_inited)
400     {
401       return 0;
402     }
403
404   SPI_inited = FALSE;
405
406   if (live_refs)
407     {
408       leaked = g_hash_table_size (live_refs);
409 #define PRINT_LEAKS
410 #ifdef PRINT_LEAKS
411       g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
412 #endif
413     }
414   else
415     {
416       leaked = 0;
417     }
418
419 #ifdef DEBUG_OBJECTS
420   if (leaked)
421     {
422       fprintf (stderr, "Leaked %d SPI handles\n", leaked);
423     }
424 #endif
425
426   cspi_cleanup ();
427
428   fprintf (stderr, "bye-bye!\n");
429
430   return leaked;
431 }
432
433 /**
434  * SPI_freeString:
435  * @s: a character string returned from another at-spi call.
436  *
437  * Free a character string returned from an at-spi call.  Clients of
438  * at-spi should use this function instead of free () or g_free().
439  * This API should not be used to free strings
440  * from other libraries or allocated by the client.
441  **/
442 void
443 SPI_freeString (char *s)
444 {
445   CORBA_free (s);
446 }