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);
129 CORBA_exception_init (&ev);
133 Accessibility_Registry
136 if (!cspi_ping (registry))
138 registry = cspi_init ();
144 cspi_exception (void)
148 if (ev._major != CORBA_NO_EXCEPTION)
150 CORBA_exception_free (&ev);
162 * This method swallows the corba_object BonoboUnknown
163 * reference, and returns an Accessible associated with it.
164 * If the reference is loaned, it means it is only valid
165 * between a borrow / return pair.
168 cspi_object_get_ref (CORBA_Object corba_object, gboolean on_loan)
172 if (corba_object == CORBA_OBJECT_NIL)
176 else if (!cspi_check_ev ("pre method check: add"))
182 if ((ref = g_hash_table_lookup (cspi_get_live_refs (), corba_object)))
184 g_assert (ref->ref_count > 0);
188 if (ref->on_loan) /* Convert to a permanant ref */
190 ref->on_loan = FALSE;
194 cspi_release_unref (corba_object);
198 g_print ("returning cached %p => %p\n", ref, ref->objref);
203 ref = malloc (sizeof (Accessible));
204 ref->objref = corba_object;
206 ref->on_loan = on_loan;
208 g_print ("allocated %p => %p\n", ref, corba_object);
210 g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
218 cspi_object_add (CORBA_Object corba_object)
220 return cspi_object_get_ref (corba_object, FALSE);
224 cspi_object_borrow (CORBA_Object corba_object)
226 return cspi_object_get_ref (corba_object, TRUE);
230 cspi_object_return (Accessible *accessible)
232 g_return_if_fail (accessible != NULL);
234 if (!accessible->on_loan ||
235 accessible->ref_count == 1)
237 cspi_object_unref (accessible);
239 else /* Convert to a permanant ref */
241 accessible->on_loan = FALSE;
242 accessible->objref = cspi_dup_ref (accessible->objref);
243 accessible->ref_count--;
248 cspi_object_take (CORBA_Object corba_object)
250 Accessible *accessible;
251 accessible = cspi_object_borrow (corba_object);
253 cspi_object_ref (accessible);
255 * if the remote object is dead,
256 * cspi_object_return will throw an exception.
257 * FIXME: what clears that exception context ever ?
259 cspi_object_return (accessible);
260 if (cspi_exception ())
262 cspi_object_unref (accessible);
269 cspi_object_ref (Accessible *accessible)
271 g_return_if_fail (accessible != NULL);
273 accessible->ref_count++;
277 cspi_object_unref (Accessible *accessible)
279 if (accessible == NULL)
284 if (--accessible->ref_count == 0)
286 g_hash_table_remove (cspi_get_live_refs (), accessible->objref);
299 g_hash_table_destroy (refs);
302 if (registry != CORBA_OBJECT_NIL)
304 cspi_release_unref (registry);
305 registry = CORBA_OBJECT_NIL;
309 static gboolean SPI_inited = FALSE;
314 * Connects to the accessibility registry and initializes the SPI.
316 * Returns: 0 on success, otherwise an integer error code.
328 CORBA_exception_init (&ev);
330 registry = cspi_init ();
332 g_atexit (cspi_cleanup);
340 * Starts/enters the main event loop for the SPI services.
342 * (NOTE: This method does not return control, it is exited via a call to
343 * SPI_event_quit () from within an event handler).
347 SPI_event_main (void)
355 * Quits the last main event loop for the SPI services,
359 SPI_event_quit (void)
367 * Checks to see if an SPI event is waiting in the event queue.
368 * Used by clients that don't wish to use SPI_event_main().
370 * Not Yet Implemented.
372 * Returns: #TRUE if an event is waiting, otherwise #FALSE.
383 * @waitForEvent: a #SPIBoolean indicating whether to block or not.
385 * Gets the next event in the SPI event queue; blocks if no event
386 * is pending and @waitForEvent is #TRUE.
387 * Used by clients that don't wish to use SPI_event_main().
389 * Not Yet Implemented.
391 * Returns: the next #AccessibleEvent in the SPI event queue.
394 SPI_nextEvent (SPIBoolean waitForEvent)
400 report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
403 Accessible *a = (Accessible *) val;
405 name = Accessible_getName (a);
406 if (cspi_exception ())
411 role = Accessible_getRoleName (a);
412 if (cspi_exception ())
417 fprintf (stderr, "leaked %d references to object %s, role %s %p\n",
418 a->ref_count, name ? name : "<?>", role ? role : "<?>", a);
420 SPI_freeString (name);
427 * Disconnects from the Accessibility Registry and releases
428 * any floating resources. Call only once at exit.
430 * Returns: 0 if there were no leaks, otherwise non zero.
446 leaked = g_hash_table_size (live_refs);
449 g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
460 fprintf (stderr, "Leaked %d SPI handles\n", leaked);
466 fprintf (stderr, "bye-bye!\n");
473 * @s: a character string returned from another at-spi call.
475 * Free a character string returned from an at-spi call. Clients of
476 * at-spi should use this function instead of free () or g_free().
477 * A NULL string @s will be silently ignored.
478 * This API should not be used to free strings
479 * from other libraries or allocated by the client.
482 SPI_freeString (char *s)