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
31 #include <cspi/spi-private.h>
35 static CORBA_Environment ev = { 0 };
36 static Accessibility_Registry registry = CORBA_OBJECT_NIL;
37 static GHashTable *live_refs = NULL;
40 cspi_object_hash (gconstpointer key)
42 CORBA_Object object = (CORBA_Object) key;
44 return CORBA_Object_hash (object, 0, &ev);
48 cspi_object_equal (gconstpointer a, gconstpointer b)
50 CORBA_Object objecta = (CORBA_Object) a;
51 CORBA_Object objectb = (CORBA_Object) b;
53 return CORBA_Object_is_equivalent (objecta, objectb, &ev);
57 cspi_object_release (gpointer value)
59 Accessible *a = (Accessible *) value;
62 g_print ("releasing %p => %p\n", a, a->objref);
67 cspi_release_unref (a->objref);
70 memset (a, 0xaa, sizeof (Accessible));
79 cspi_accessible_is_a (Accessible *accessible,
80 const char *interface_name)
83 Bonobo_Unknown unknown;
85 if (accessible == NULL)
90 unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (accessible),
91 interface_name, cspi_ev ());
93 if (ev._major != CORBA_NO_EXCEPTION)
95 g_error ("Exception '%s' checking if is '%s'",
96 cspi_exception_get_text (),
100 if (unknown != CORBA_OBJECT_NIL)
103 cspi_release_unref (unknown);
114 cspi_get_live_refs (void)
118 live_refs = g_hash_table_new_full (cspi_object_hash,
121 cspi_object_release);
132 Accessibility_Registry
135 if (!cspi_ping (registry))
137 registry = cspi_init ();
143 cspi_exception (void)
147 if (ev._major != CORBA_NO_EXCEPTION)
149 CORBA_exception_free (&ev);
161 * This method swallows the corba_object BonoboUnknown
162 * reference, and returns an Accessible associated with it.
163 * If the reference is loaned, it means it is only valid
164 * between a borrow / return pair.
167 cspi_object_get_ref (CORBA_Object corba_object, gboolean on_loan)
171 if (corba_object == CORBA_OBJECT_NIL)
175 else if (!cspi_check_ev ("pre method check: add"))
181 if ((ref = g_hash_table_lookup (cspi_get_live_refs (), corba_object)))
183 g_assert (ref->ref_count > 0);
187 if (ref->on_loan) /* Convert to a permanant ref */
189 ref->on_loan = FALSE;
193 cspi_release_unref (corba_object);
197 g_print ("returning cached %p => %p\n", ref, ref->objref);
202 ref = malloc (sizeof (Accessible));
203 ref->objref = corba_object;
205 ref->on_loan = on_loan;
207 g_print ("allocated %p => %p\n", ref, corba_object);
209 g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
217 cspi_object_add (CORBA_Object corba_object)
219 return cspi_object_get_ref (corba_object, FALSE);
223 cspi_object_borrow (CORBA_Object corba_object)
225 return cspi_object_get_ref (corba_object, TRUE);
229 cspi_object_return (Accessible *accessible)
231 g_return_if_fail (accessible != NULL);
233 if (!accessible->on_loan ||
234 accessible->ref_count == 1)
236 cspi_object_unref (accessible);
238 else /* Convert to a permanant ref */
240 accessible->on_loan = FALSE;
241 accessible->objref = cspi_dup_ref (accessible->objref);
242 accessible->ref_count--;
247 cspi_object_ref (Accessible *accessible)
249 g_return_if_fail (accessible != NULL);
251 accessible->ref_count++;
255 cspi_object_unref (Accessible *accessible)
257 if (accessible == NULL)
262 if (--accessible->ref_count == 0)
264 g_hash_table_remove (cspi_get_live_refs (), accessible->objref);
277 g_hash_table_destroy (refs);
280 if (registry != CORBA_OBJECT_NIL)
282 cspi_release_unref (registry);
283 registry = CORBA_OBJECT_NIL;
287 static gboolean SPI_inited = FALSE;
292 * Connects to the accessibility registry and initializes the SPI.
294 * Returns: 0 on success, otherwise an integer error code.
306 CORBA_exception_init (&ev);
308 registry = cspi_init ();
310 g_atexit (cspi_cleanup);
318 * Starts/enters the main event loop for the SPI services.
320 * (NOTE: This method does not return control, it is exited via a call to
321 * SPI_event_quit () from within an event handler).
325 SPI_event_main (void)
333 * Quits the last main event loop for the SPI services,
337 SPI_event_quit (void)
345 * Checks to see if an SPI event is waiting in the event queue.
346 * Used by clients that don't wish to use SPI_event_main().
348 * Not Yet Implemented.
350 * Returns: #TRUE if an event is waiting, otherwise #FALSE.
361 * @waitForEvent: a #SPIBoolean indicating whether to block or not.
363 * Gets the next event in the SPI event queue; blocks if no event
364 * is pending and @waitForEvent is #TRUE.
365 * Used by clients that don't wish to use SPI_event_main().
367 * Not Yet Implemented.
369 * Returns: the next #AccessibleEvent in the SPI event queue.
372 SPI_nextEvent (SPIBoolean waitForEvent)
378 report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
381 Accessible *a = (Accessible *) val;
383 name = Accessible_getName (a);
384 if (cspi_exception ())
389 role = Accessible_getRoleName (a);
390 if (cspi_exception ())
395 fprintf (stderr, "leaked %d references to object %s, role %s\n",
396 a->ref_count, name ? name : "<?>", role ? role : "<?>");
398 SPI_freeString (name);
405 * Disconnects from the Accessibility Registry and releases
406 * any floating resources. Call only once at exit.
408 * Returns: 0 if there were no leaks, otherwise non zero.
424 leaked = g_hash_table_size (live_refs);
427 g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
438 fprintf (stderr, "Leaked %d SPI handles\n", leaked);
444 fprintf (stderr, "bye-bye!\n");
451 * @s: a character string returned from another at-spi call.
453 * Free a character string returned from an at-spi call. Clients of
454 * at-spi should use this function instead of free () or g_free().
455 * A NULL string @s will be silently ignored.
456 * This API should not be used to free strings
457 * from other libraries or allocated by the client.
460 SPI_freeString (char *s)