Suppress SEGVs due to improper traveral of leaked-objects list;
[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 CORBA_Environment ev = { 0 };
39 static Accessibility_Registry registry = CORBA_OBJECT_NIL;
40 static GHashTable *live_refs = NULL;
41 static GQueue *exception_handlers = NULL;
42
43 static guint
44 cspi_object_hash (gconstpointer key)
45 {
46   CORBA_Object object = (CORBA_Object) key;
47
48   return CORBA_Object_hash (object, 0, &ev);
49 }
50
51 static gboolean
52 cspi_object_equal (gconstpointer a, gconstpointer b)
53 {
54   CORBA_Object objecta = (CORBA_Object) a;
55   CORBA_Object objectb = (CORBA_Object) b;
56
57   return CORBA_Object_is_equivalent (objecta, objectb, cspi_ev ());
58 }
59
60 static void
61 cspi_object_release (gpointer value)
62 {
63   Accessible *a = (Accessible *) value;
64
65 #ifdef DEBUG_OBJECTS
66   g_print ("releasing %p => %p\n", a, a->objref);
67 #endif
68
69   if (!a->on_loan)
70     {
71       cspi_release_unref (a->objref);
72     }
73
74   memset (a, 0xaa, sizeof (Accessible));
75   a->ref_count = -1;
76
77 #ifndef DEBUG_OBJECTS
78   free (a);
79 #endif
80 }
81
82 gboolean
83 _cspi_exception_throw (CORBA_Environment *ev, char *desc_prefix)
84 {
85   SPIExceptionHandler *handler = NULL;
86   SPIException ex;
87   if (exception_handlers) handler = g_queue_peek_head (exception_handlers);
88
89   ex.type = SPI_EXCEPTION_SOURCE_UNSPECIFIED;
90   ex.source = CORBA_OBJECT_NIL; /* can we get this from here? */
91   ex.ev = CORBA_exception__copy (ev);
92   switch (ev->_major) {
93   case CORBA_SYSTEM_EXCEPTION:
94     ex.code = SPI_EXCEPTION_UNSPECIFIED;
95     break;
96   case CORBA_USER_EXCEPTION: /* help! how to interpret this? */
97     ex.code = SPI_EXCEPTION_UNSPECIFIED;
98     break;
99   default:
100     ex.code = SPI_EXCEPTION_UNSPECIFIED;
101     break;
102   }
103   
104   if (handler)
105     return (*handler) (&ex, FALSE);
106   else
107     return FALSE; /* means exception was not handled */
108 }
109
110 SPIBoolean
111 cspi_accessible_is_a (Accessible *accessible,
112                       const char *interface_name)
113 {
114   SPIBoolean        retval;
115   Bonobo_Unknown unknown;
116
117   if (accessible == NULL)
118     {
119       return FALSE;
120     }
121
122   unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (accessible),
123                                            interface_name, cspi_ev ());
124
125   if (ev._major != CORBA_NO_EXCEPTION)
126     {
127       g_warning ("Exception '%s' checking if is '%s'",
128                  cspi_exception_get_text (),
129                  interface_name);
130       retval = FALSE;
131     }
132
133   else if (unknown != CORBA_OBJECT_NIL)
134     {
135       retval = TRUE;
136       cspi_release_unref (unknown);
137     }
138   else
139     {
140       retval = FALSE;
141     }
142
143   return retval;
144 }
145
146 static GHashTable *
147 cspi_get_live_refs (void)
148 {
149   if (!live_refs) 
150     {
151       live_refs = g_hash_table_new_full (cspi_object_hash,
152                                          cspi_object_equal,
153                                          NULL,
154                                          cspi_object_release);
155     }
156   return live_refs;
157 }
158
159 CORBA_Environment *
160 cspi_peek_ev (void)
161 {
162   return &ev;
163 }
164
165 CORBA_Environment *
166 cspi_ev (void)
167 {
168   CORBA_exception_init (&ev);
169   return &ev;
170 }
171
172 Accessibility_Registry
173 cspi_registry (void)
174 {
175   if (!cspi_ping (registry))
176     {
177       registry = cspi_init ();
178     }
179   return registry;
180 }
181
182 SPIBoolean
183 cspi_exception (void)
184 {
185   SPIBoolean retval;
186
187   if (ev._major != CORBA_NO_EXCEPTION)
188     {
189       CORBA_exception_free (&ev);
190       retval = TRUE;
191     }
192   else
193     {
194       retval = FALSE;
195     }
196
197   return retval;
198 }
199
200 /*
201  *   This method swallows the corba_object BonoboUnknown
202  * reference, and returns an Accessible associated with it.
203  * If the reference is loaned, it means it is only valid
204  * between a borrow / return pair.
205  */
206 static Accessible *
207 cspi_object_get_ref (CORBA_Object corba_object, gboolean on_loan)
208 {
209   Accessible *ref;
210
211   if (corba_object == CORBA_OBJECT_NIL)
212     {
213       ref = NULL;
214     }
215   else if (!cspi_check_ev ("pre method check: add"))
216     {
217       ref = NULL;
218     }
219   else
220     {
221       if ((ref = g_hash_table_lookup (cspi_get_live_refs (), corba_object)))
222         {
223           g_assert (ref->ref_count > 0);
224           ref->ref_count++;
225           if (!on_loan)
226             {
227               if (ref->on_loan) /* Convert to a permanant ref */
228                 {
229                   ref->on_loan = FALSE;
230                 }
231               else
232                 {
233                   cspi_release_unref (corba_object);
234                 }
235             }
236 #ifdef DEBUG_OBJECTS
237           g_print ("returning cached %p => %p\n", ref, ref->objref);
238 #endif
239         }
240       else
241         {
242           ref = malloc (sizeof (Accessible));
243           ref->objref = corba_object;
244           ref->ref_count = 1;
245           ref->on_loan = on_loan;
246 #ifdef DEBUG_OBJECTS
247           g_print ("allocated %p => %p\n", ref, corba_object);
248 #endif
249           g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
250         }
251     }
252
253   return ref;
254 }
255
256 Accessible *
257 cspi_object_add (CORBA_Object corba_object)
258 {
259   return cspi_object_get_ref (corba_object, FALSE);
260 }
261
262 Accessible *
263 cspi_object_borrow (CORBA_Object corba_object)
264 {
265   return cspi_object_get_ref (corba_object, TRUE);
266 }
267
268 void
269 cspi_object_return (Accessible *accessible)
270 {
271   int old_ref_count;
272   g_return_if_fail (accessible != NULL);
273
274   if (!accessible->on_loan ||
275       accessible->ref_count == 1)
276     {
277       cspi_object_unref (accessible);
278     }
279   else /* Convert to a permanant ref */
280     {
281       accessible->on_loan = FALSE;
282       old_ref_count = accessible->ref_count;
283       accessible->objref = cspi_dup_ref (accessible->objref);
284       if (old_ref_count != accessible->ref_count &&
285           accessible->ref_count == 1)
286         {
287             cspi_object_unref (accessible);
288         }
289       else    
290         {
291           accessible->ref_count--;
292         }
293     }
294 }
295
296 Accessible *
297 cspi_object_take (CORBA_Object corba_object)
298 {
299   Accessible *accessible;
300   accessible = cspi_object_borrow (corba_object);
301
302   cspi_object_ref (accessible);
303   /* 
304    * if the remote object is dead, 
305    * cspi_object_return will throw an exception. 
306    * FIXME: what clears that exception context ever ?
307    */
308   cspi_object_return (accessible);
309   if (cspi_exception ()) 
310     {
311       cspi_object_unref (accessible);
312       accessible = NULL;
313     }
314   return accessible;
315 }
316
317 void
318 cspi_object_ref (Accessible *accessible)
319 {
320   g_return_if_fail (accessible != NULL);
321
322   accessible->ref_count++;
323 }
324
325 void
326 cspi_object_unref (Accessible *accessible)
327 {
328   if (accessible == NULL)
329     {
330       return;
331     }
332
333   if (--accessible->ref_count == 0)
334     {
335       g_hash_table_remove (cspi_get_live_refs (), accessible->objref);
336     }
337 }
338
339 static void
340 cspi_cleanup (void)
341 {
342   GHashTable *refs;
343
344   cspi_streams_close_all ();
345
346   refs = live_refs;
347   live_refs = NULL;
348   if (refs)
349     {
350       g_hash_table_destroy (refs);
351     }
352
353   if (registry != CORBA_OBJECT_NIL)
354     {
355       cspi_release_unref (registry);
356       registry = CORBA_OBJECT_NIL;
357     }
358 }
359
360 static gboolean SPI_inited = FALSE;
361
362 /**
363  * SPI_init:
364  *
365  * Connects to the accessibility registry and initializes the SPI.
366  *
367  * Returns: 0 on success, otherwise an integer error code.
368  **/
369 int
370 SPI_init (void)
371 {
372   if (SPI_inited)
373     {
374       return 1;
375     }
376
377   SPI_inited = TRUE;
378
379   CORBA_exception_init (&ev);
380
381   registry = cspi_init ();
382
383   g_atexit (cspi_cleanup);
384   
385   return 0;
386 }
387
388 /**
389  * SPI_event_main:
390  *
391  * Starts/enters the main event loop for the SPI services.
392  *
393  * (NOTE: This method does not return control, it is exited via a call to
394  *  SPI_event_quit () from within an event handler).
395  *
396  **/
397 void
398 SPI_event_main (void)
399 {
400   cspi_main ();
401 }
402
403 /**
404  * SPI_event_quit:
405  *
406  * Quits the last main event loop for the SPI services,
407  * see SPI_event_main
408  **/
409 void
410 SPI_event_quit (void)
411 {
412   cspi_main_quit ();
413 }
414
415 /**
416  * SPI_eventIsReady:
417  *
418  * Checks to see if an SPI event is waiting in the event queue.
419  * Used by clients that don't wish to use SPI_event_main().
420  *
421  * Not Yet Implemented.
422  *
423  * Returns: #TRUE if an event is waiting, otherwise #FALSE.
424  *
425  **/
426 SPIBoolean
427 SPI_eventIsReady ()
428 {
429   return FALSE;
430 }
431
432 /**
433  * SPI_nextEvent:
434  * @waitForEvent: a #SPIBoolean indicating whether to block or not.
435  *
436  * Gets the next event in the SPI event queue; blocks if no event
437  * is pending and @waitForEvent is #TRUE.
438  * Used by clients that don't wish to use SPI_event_main().
439  *
440  * Not Yet Implemented.
441  *
442  * Returns: the next #AccessibleEvent in the SPI event queue.
443  **/
444 AccessibleEvent *
445 SPI_nextEvent (SPIBoolean waitForEvent)
446 {
447   return NULL;
448 }
449
450 static void
451 report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
452 {
453   char *name, *role;
454   Accessible *a = (Accessible *) val;
455   
456   name = Accessible_getName (a);
457   if (cspi_exception ())
458     {
459       name = NULL;
460     }
461
462   role = Accessible_getRoleName (a);
463   if (cspi_exception ())
464     {
465       role = NULL;
466     }
467
468   fprintf (stderr, "leaked %d references to object %s, role %s %p\n",
469            a->ref_count, name ? name : "<?>", role ? role : "<?>", a);
470
471   SPI_freeString (name);
472 }
473
474
475 /**
476  * SPI_exit:
477  *
478  * Disconnects from the Accessibility Registry and releases 
479  * any floating resources. Call only once at exit.
480  *
481  * Returns: 0 if there were no leaks, otherwise non zero.
482  **/
483 int
484 SPI_exit (void)
485 {
486   int leaked;
487
488   if (!SPI_inited)
489     {
490       return 0;
491     }
492
493   SPI_inited = FALSE;
494
495   if (live_refs)
496     {
497       leaked = g_hash_table_size (live_refs);
498 #ifdef DEBUG_OBJECTS
499       fprintf (stderr, "Leaked %d SPI handles\n", leaked);
500
501 #define PRINT_LEAKS
502 #ifdef PRINT_LEAKS
503       g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
504 #endif
505
506 #endif
507     }
508   else
509     {
510       leaked = 0;
511     }
512
513   cspi_cleanup ();
514
515   return leaked;
516 }
517
518 /**
519  * SPI_freeString:
520  * @s: a character string returned from another at-spi call.
521  *
522  * Free a character string returned from an at-spi call.  Clients of
523  * at-spi should use this function instead of free () or g_free().
524  * A NULL string @s will be silently ignored.
525  * This API should not be used to free strings
526  * from other libraries or allocated by the client.
527  **/
528 void
529 SPI_freeString (char *s)
530 {
531   if (s)
532     {
533       CORBA_free (s);
534     }
535 }
536
537 /**
538  * DOCUMENT_ME!
539  **/
540 char *
541 SPI_dupString (char *s)
542 {
543   if (s)
544     {
545       return CORBA_string_dup (s);
546     }
547   else 
548     return NULL;
549 }
550
551 /**
552  * DOCUMENT_ME!
553  **/
554 SPIBoolean SPI_exceptionHandlerPush (SPIExceptionHandler *handler)
555 {
556   if (!exception_handlers)
557     exception_handlers = g_queue_new ();
558   g_queue_push_head (exception_handlers, handler);
559   return TRUE;
560 }
561
562 /**
563  * DOCUMENT_ME!
564  **/
565 SPIExceptionHandler* SPI_exceptionHandlerPop (void)
566 {
567   return (SPIExceptionHandler *) g_queue_pop_head (exception_handlers);
568 }
569
570 /**
571  * DOCUMENT_ME!
572  **/
573 SPIExceptionType SPIException_getSourceType (SPIException *err)
574 {
575   if (err)
576     return err->type;
577   else
578     return SPI_EXCEPTION_SOURCE_UNSPECIFIED;
579 }
580
581 /**
582  * DOCUMENT_ME!
583  **/
584 SPIExceptionCode SPIException_getExceptionCode (SPIException *err)
585 {  
586   return err->code;
587 }
588
589 /**
590  * DOCUMENT_ME!
591  **/
592 Accessible* SPIAccessibleException_getSource (SPIException *err)
593 {
594   if (err->type == SPI_EXCEPTION_SOURCE_ACCESSIBLE)
595     return cspi_object_get_ref (err->source, FALSE);
596   return NULL;
597 }
598
599 /**
600  * DOCUMENT_ME!
601  **/
602 char* SPIException_getDescription (SPIException *err)
603 {
604   /* TODO: friendlier error messages? */
605   if (err->ev)
606     return CORBA_exception_id (err->ev);
607   return NULL;
608 }