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_warning ("Exception '%s' checking if is '%s'",
98 cspi_exception_get_text (),
103 else if (unknown != CORBA_OBJECT_NIL)
106 cspi_release_unref (unknown);
117 cspi_get_live_refs (void)
121 live_refs = g_hash_table_new_full (cspi_object_hash,
124 cspi_object_release);
132 CORBA_exception_init (&ev);
136 Accessibility_Registry
139 if (!cspi_ping (registry))
141 registry = cspi_init ();
147 cspi_exception (void)
151 if (ev._major != CORBA_NO_EXCEPTION)
153 CORBA_exception_free (&ev);
165 * This method swallows the corba_object BonoboUnknown
166 * reference, and returns an Accessible associated with it.
167 * If the reference is loaned, it means it is only valid
168 * between a borrow / return pair.
171 cspi_object_get_ref (CORBA_Object corba_object, gboolean on_loan)
175 if (corba_object == CORBA_OBJECT_NIL)
179 else if (!cspi_check_ev ("pre method check: add"))
185 if ((ref = g_hash_table_lookup (cspi_get_live_refs (), corba_object)))
187 g_assert (ref->ref_count > 0);
191 if (ref->on_loan) /* Convert to a permanant ref */
193 ref->on_loan = FALSE;
197 cspi_release_unref (corba_object);
201 g_print ("returning cached %p => %p\n", ref, ref->objref);
206 ref = malloc (sizeof (Accessible));
207 ref->objref = corba_object;
209 ref->on_loan = on_loan;
211 g_print ("allocated %p => %p\n", ref, corba_object);
213 g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
221 cspi_object_add (CORBA_Object corba_object)
223 return cspi_object_get_ref (corba_object, FALSE);
227 cspi_object_borrow (CORBA_Object corba_object)
229 return cspi_object_get_ref (corba_object, TRUE);
233 cspi_object_return (Accessible *accessible)
235 g_return_if_fail (accessible != NULL);
237 if (!accessible->on_loan ||
238 accessible->ref_count == 1)
240 cspi_object_unref (accessible);
242 else /* Convert to a permanant ref */
244 accessible->on_loan = FALSE;
245 accessible->objref = cspi_dup_ref (accessible->objref);
246 accessible->ref_count--;
251 cspi_object_take (CORBA_Object corba_object)
253 Accessible *accessible;
254 accessible = cspi_object_borrow (corba_object);
256 cspi_object_ref (accessible);
258 * if the remote object is dead,
259 * cspi_object_return will throw an exception.
260 * FIXME: what clears that exception context ever ?
262 cspi_object_return (accessible);
263 if (cspi_exception ())
265 cspi_object_unref (accessible);
272 cspi_object_ref (Accessible *accessible)
274 g_return_if_fail (accessible != NULL);
276 accessible->ref_count++;
280 cspi_object_unref (Accessible *accessible)
282 if (accessible == NULL)
287 if (--accessible->ref_count == 0)
289 g_hash_table_remove (cspi_get_live_refs (), accessible->objref);
302 g_hash_table_destroy (refs);
305 if (registry != CORBA_OBJECT_NIL)
307 cspi_release_unref (registry);
308 registry = CORBA_OBJECT_NIL;
312 static gboolean SPI_inited = FALSE;
317 * Connects to the accessibility registry and initializes the SPI.
319 * Returns: 0 on success, otherwise an integer error code.
331 CORBA_exception_init (&ev);
333 registry = cspi_init ();
335 g_atexit (cspi_cleanup);
343 * Starts/enters the main event loop for the SPI services.
345 * (NOTE: This method does not return control, it is exited via a call to
346 * SPI_event_quit () from within an event handler).
350 SPI_event_main (void)
358 * Quits the last main event loop for the SPI services,
362 SPI_event_quit (void)
370 * Checks to see if an SPI event is waiting in the event queue.
371 * Used by clients that don't wish to use SPI_event_main().
373 * Not Yet Implemented.
375 * Returns: #TRUE if an event is waiting, otherwise #FALSE.
386 * @waitForEvent: a #SPIBoolean indicating whether to block or not.
388 * Gets the next event in the SPI event queue; blocks if no event
389 * is pending and @waitForEvent is #TRUE.
390 * Used by clients that don't wish to use SPI_event_main().
392 * Not Yet Implemented.
394 * Returns: the next #AccessibleEvent in the SPI event queue.
397 SPI_nextEvent (SPIBoolean waitForEvent)
403 report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
406 Accessible *a = (Accessible *) val;
408 name = Accessible_getName (a);
409 if (cspi_exception ())
414 role = Accessible_getRoleName (a);
415 if (cspi_exception ())
420 fprintf (stderr, "leaked %d references to object %s, role %s %p\n",
421 a->ref_count, name ? name : "<?>", role ? role : "<?>", a);
423 SPI_freeString (name);
430 * Disconnects from the Accessibility Registry and releases
431 * any floating resources. Call only once at exit.
433 * Returns: 0 if there were no leaks, otherwise non zero.
449 leaked = g_hash_table_size (live_refs);
452 g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
463 fprintf (stderr, "Leaked %d SPI handles\n", leaked);
469 fprintf (stderr, "bye-bye!\n");
476 * @s: a character string returned from another at-spi call.
478 * Free a character string returned from an at-spi call. Clients of
479 * at-spi should use this function instead of free () or g_free().
480 * A NULL string @s will be silently ignored.
481 * This API should not be used to free strings
482 * from other libraries or allocated by the client.
485 SPI_freeString (char *s)