2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001 Sun Microsystems Inc.
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.
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.
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.
25 * Basic SPI initialization and event loop function prototypes
30 #include <cspi/spi-private.h>
34 static CORBA_Environment ev = { 0 };
35 static Accessibility_Registry registry = CORBA_OBJECT_NIL;
36 static GHashTable *live_refs = NULL;
39 cspi_object_hash (gconstpointer key)
41 CORBA_Object object = (CORBA_Object) key;
43 return CORBA_Object_hash (object, 0, &ev);
47 cspi_object_equal (gconstpointer a, gconstpointer b)
49 CORBA_Object objecta = (CORBA_Object) a;
50 CORBA_Object objectb = (CORBA_Object) b;
52 return CORBA_Object_is_equivalent (objecta, objectb, &ev);
56 cspi_object_release (gpointer value)
58 Accessible *a = (Accessible *) value;
61 g_print ("releasing %p => %p\n", a, a->objref);
66 cspi_release_unref (a->objref);
69 memset (a, 0xaa, sizeof (Accessible));
78 cspi_accessible_is_a (Accessible *accessible,
79 const char *interface_name)
82 Bonobo_Unknown unknown;
84 if (accessible == NULL)
89 unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (accessible),
90 interface_name, cspi_ev ());
92 if (ev._major != CORBA_NO_EXCEPTION)
94 g_error ("Exception '%s' checking if is '%s'",
95 cspi_exception_get_text (),
99 if (unknown != CORBA_OBJECT_NIL)
102 cspi_release_unref (unknown);
113 cspi_get_live_refs (void)
117 live_refs = g_hash_table_new_full (cspi_object_hash,
120 cspi_object_release);
131 Accessibility_Registry
134 if (!cspi_ping (registry))
136 registry = cspi_init ();
142 cspi_exception (void)
146 if (ev._major != CORBA_NO_EXCEPTION)
148 CORBA_exception_free (&ev);
160 * This method swallows the corba_object BonoboUnknown
161 * reference, and returns an Accessible associated with it.
162 * If the reference is loaned, it means it is only valid
163 * between a borrow / return pair.
166 cspi_object_get_ref (CORBA_Object corba_object, gboolean on_loan)
170 if (corba_object == CORBA_OBJECT_NIL)
174 else if (!cspi_check_ev ("pre method check: add"))
180 if ((ref = g_hash_table_lookup (cspi_get_live_refs (), corba_object)))
182 g_assert (ref->ref_count > 0);
186 if (ref->on_loan) /* Convert to a permanant ref */
188 ref->on_loan = FALSE;
192 cspi_release_unref (corba_object);
196 g_print ("returning cached %p => %p\n", ref, ref->objref);
201 ref = malloc (sizeof (Accessible));
202 ref->objref = corba_object;
204 ref->on_loan = on_loan;
206 g_print ("allocated %p => %p\n", ref, corba_object);
208 g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
216 cspi_object_add (CORBA_Object corba_object)
218 return cspi_object_get_ref (corba_object, FALSE);
222 cspi_object_borrow (CORBA_Object corba_object)
224 return cspi_object_get_ref (corba_object, TRUE);
228 cspi_object_return (Accessible *accessible)
230 g_return_if_fail (accessible != NULL);
232 if (!accessible->on_loan ||
233 accessible->ref_count == 1)
235 cspi_object_unref (accessible);
237 else /* Convert to a permanant ref */
239 accessible->on_loan = FALSE;
240 accessible->objref = cspi_dup_ref (accessible->objref);
241 accessible->ref_count--;
246 cspi_object_ref (Accessible *accessible)
248 g_return_if_fail (accessible != NULL);
250 accessible->ref_count++;
254 cspi_object_unref (Accessible *accessible)
256 if (accessible == NULL)
261 if (--accessible->ref_count == 0)
263 g_hash_table_remove (cspi_get_live_refs (), accessible->objref);
276 g_hash_table_destroy (refs);
279 if (registry != CORBA_OBJECT_NIL)
281 cspi_release_unref (registry);
282 registry = CORBA_OBJECT_NIL;
286 static gboolean SPI_inited = FALSE;
291 * Connects to the accessibility registry and initializes the SPI.
293 * Returns: 0 on success, otherwise an integer error code.
305 CORBA_exception_init (&ev);
307 registry = cspi_init ();
309 g_atexit (cspi_cleanup);
317 * Starts/enters the main event loop for the SPI services.
319 * (NOTE: This method does not return control, it is exited via a call to
320 * SPI_event_quit () from within an event handler).
324 SPI_event_main (void)
332 * Quits the last main event loop for the SPI services,
336 SPI_event_quit (void)
344 * Checks to see if an SPI event is waiting in the event queue.
345 * Used by clients that don't wish to use SPI_event_main().
347 * Not Yet Implemented.
349 * Returns: #TRUE if an event is waiting, otherwise #FALSE.
360 * @waitForEvent: a #SPIBoolean indicating whether to block or not.
362 * Gets the next event in the SPI event queue; blocks if no event
363 * is pending and @waitForEvent is #TRUE.
364 * Used by clients that don't wish to use SPI_event_main().
366 * Not Yet Implemented.
368 * Returns: the next #AccessibleEvent in the SPI event queue.
371 SPI_nextEvent (SPIBoolean waitForEvent)
377 report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
380 Accessible *a = (Accessible *) val;
382 name = Accessible_getName (a);
383 if (cspi_exception ())
388 role = Accessible_getRoleName (a);
389 if (cspi_exception ())
394 fprintf (stderr, "leaked %d references to object %s, role %s\n",
395 a->ref_count, name ? name : "<?>", role ? role : "<?>");
397 SPI_freeString (name);
404 * Disconnects from the Accessibility Registry and releases
405 * any floating resources. Call only once at exit.
407 * Returns: 0 if there were no leaks, otherwise non zero.
423 leaked = g_hash_table_size (live_refs);
426 g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
437 fprintf (stderr, "Leaked %d SPI handles\n", leaked);
443 fprintf (stderr, "bye-bye!\n");
450 * @s: a character string returned from another at-spi call.
452 * Free a character string returned from an at-spi call. Clients of
453 * at-spi should use this function instead of free () or g_free().
454 * A NULL string @s will be silently ignored.
455 * This API should not be used to free strings
456 * from other libraries or allocated by the client.
459 SPI_freeString (char *s)