2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002 Sun Microsystems Inc.,
6 * Copyright 2001, 2002 Ximian, Inc.
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.
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.
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.
26 * Basic SPI initialization and event loop function prototypes
33 #include <cspi/spi-private.h>
37 static CORBA_Environment ev = { 0 };
38 static Accessibility_Registry registry = CORBA_OBJECT_NIL;
39 static GHashTable *live_refs = NULL;
42 cspi_object_hash (gconstpointer key)
44 CORBA_Object object = (CORBA_Object) key;
46 return CORBA_Object_hash (object, 0, &ev);
50 cspi_object_equal (gconstpointer a, gconstpointer b)
52 CORBA_Object objecta = (CORBA_Object) a;
53 CORBA_Object objectb = (CORBA_Object) b;
55 return CORBA_Object_is_equivalent (objecta, objectb, &ev);
59 cspi_object_release (gpointer value)
61 Accessible *a = (Accessible *) value;
64 g_print ("releasing %p => %p\n", a, a->objref);
69 cspi_release_unref (a->objref);
72 memset (a, 0xaa, sizeof (Accessible));
81 cspi_accessible_is_a (Accessible *accessible,
82 const char *interface_name)
85 Bonobo_Unknown unknown;
87 if (accessible == NULL)
92 unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (accessible),
93 interface_name, cspi_ev ());
95 if (ev._major != CORBA_NO_EXCEPTION)
97 g_error ("Exception '%s' checking if is '%s'",
98 cspi_exception_get_text (),
102 if (unknown != CORBA_OBJECT_NIL)
105 cspi_release_unref (unknown);
116 cspi_get_live_refs (void)
120 live_refs = g_hash_table_new_full (cspi_object_hash,
123 cspi_object_release);
131 CORBA_exception_init (&ev);
135 Accessibility_Registry
138 if (!cspi_ping (registry))
140 registry = cspi_init ();
146 cspi_exception (void)
150 if (ev._major != CORBA_NO_EXCEPTION)
152 CORBA_exception_free (&ev);
164 * This method swallows the corba_object BonoboUnknown
165 * reference, and returns an Accessible associated with it.
166 * If the reference is loaned, it means it is only valid
167 * between a borrow / return pair.
170 cspi_object_get_ref (CORBA_Object corba_object, gboolean on_loan)
174 if (corba_object == CORBA_OBJECT_NIL)
178 else if (!cspi_check_ev ("pre method check: add"))
184 if ((ref = g_hash_table_lookup (cspi_get_live_refs (), corba_object)))
186 g_assert (ref->ref_count > 0);
190 if (ref->on_loan) /* Convert to a permanant ref */
192 ref->on_loan = FALSE;
196 cspi_release_unref (corba_object);
200 g_print ("returning cached %p => %p\n", ref, ref->objref);
205 ref = malloc (sizeof (Accessible));
206 ref->objref = corba_object;
208 ref->on_loan = on_loan;
210 g_print ("allocated %p => %p\n", ref, corba_object);
212 g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
220 cspi_object_add (CORBA_Object corba_object)
222 return cspi_object_get_ref (corba_object, FALSE);
226 cspi_object_borrow (CORBA_Object corba_object)
228 return cspi_object_get_ref (corba_object, TRUE);
232 cspi_object_return (Accessible *accessible)
234 g_return_if_fail (accessible != NULL);
236 if (!accessible->on_loan ||
237 accessible->ref_count == 1)
239 cspi_object_unref (accessible);
241 else /* Convert to a permanant ref */
243 accessible->on_loan = FALSE;
244 accessible->objref = cspi_dup_ref (accessible->objref);
245 accessible->ref_count--;
250 cspi_object_take (CORBA_Object corba_object)
252 Accessible *accessible;
253 accessible = cspi_object_borrow (corba_object);
255 cspi_object_ref (accessible);
257 * if the remote object is dead,
258 * cspi_object_return will throw an exception.
259 * FIXME: what clears that exception context ever ?
261 cspi_object_return (accessible);
262 if (cspi_exception ())
264 cspi_object_unref (accessible);
271 cspi_object_ref (Accessible *accessible)
273 g_return_if_fail (accessible != NULL);
275 accessible->ref_count++;
279 cspi_object_unref (Accessible *accessible)
281 if (accessible == NULL)
286 if (--accessible->ref_count == 0)
288 g_hash_table_remove (cspi_get_live_refs (), accessible->objref);
301 g_hash_table_destroy (refs);
304 if (registry != CORBA_OBJECT_NIL)
306 cspi_release_unref (registry);
307 registry = CORBA_OBJECT_NIL;
311 static gboolean SPI_inited = FALSE;
316 * Connects to the accessibility registry and initializes the SPI.
318 * Returns: 0 on success, otherwise an integer error code.
330 CORBA_exception_init (&ev);
332 registry = cspi_init ();
334 g_atexit (cspi_cleanup);
342 * Starts/enters the main event loop for the SPI services.
344 * (NOTE: This method does not return control, it is exited via a call to
345 * SPI_event_quit () from within an event handler).
349 SPI_event_main (void)
357 * Quits the last main event loop for the SPI services,
361 SPI_event_quit (void)
369 * Checks to see if an SPI event is waiting in the event queue.
370 * Used by clients that don't wish to use SPI_event_main().
372 * Not Yet Implemented.
374 * Returns: #TRUE if an event is waiting, otherwise #FALSE.
385 * @waitForEvent: a #SPIBoolean indicating whether to block or not.
387 * Gets the next event in the SPI event queue; blocks if no event
388 * is pending and @waitForEvent is #TRUE.
389 * Used by clients that don't wish to use SPI_event_main().
391 * Not Yet Implemented.
393 * Returns: the next #AccessibleEvent in the SPI event queue.
396 SPI_nextEvent (SPIBoolean waitForEvent)
402 report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
405 Accessible *a = (Accessible *) val;
407 name = Accessible_getName (a);
408 if (cspi_exception ())
413 role = Accessible_getRoleName (a);
414 if (cspi_exception ())
419 fprintf (stderr, "leaked %d references to object %s, role %s %p\n",
420 a->ref_count, name ? name : "<?>", role ? role : "<?>", a);
422 SPI_freeString (name);
429 * Disconnects from the Accessibility Registry and releases
430 * any floating resources. Call only once at exit.
432 * Returns: 0 if there were no leaks, otherwise non zero.
448 leaked = g_hash_table_size (live_refs);
451 g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
462 fprintf (stderr, "Leaked %d SPI handles\n", leaked);
468 fprintf (stderr, "bye-bye!\n");
475 * @s: a character string returned from another at-spi call.
477 * Free a character string returned from an at-spi call. Clients of
478 * at-spi should use this function instead of free () or g_free().
479 * A NULL string @s will be silently ignored.
480 * This API should not be used to free strings
481 * from other libraries or allocated by the client.
484 SPI_freeString (char *s)