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;
44 retval = 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;
56 retval = CORBA_Object_is_equivalent (objecta, objectb, &ev);
62 cspi_object_release (gpointer value)
64 Accessible *a = (Accessible *) value;
67 g_print ("releasing %p => %p\n", a, a->objref);
70 cspi_release_unref (a->objref);
72 memset (a, 0xaa, sizeof (Accessible));
81 cspi_accessible_is_a (Accessible *obj,
82 const char *interface_name)
85 Bonobo_Unknown unknown;
92 unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (obj),
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);
134 Accessibility_Registry
137 if (!cspi_ping (registry))
138 registry = cspi_init ();
143 cspi_exception (void)
147 if (ev._major != CORBA_NO_EXCEPTION)
149 CORBA_exception_free (&ev);
161 cspi_object_new (CORBA_Object corba_object)
165 if (corba_object == CORBA_OBJECT_NIL)
169 else if (!cspi_check_ev ("pre method check: add"))
175 ref = malloc (sizeof (Accessible));
176 // ref->objref = CORBA_Object_duplicate (corba_object, cspi_ev ());
177 ref->objref = corba_object;
185 cspi_object_add (CORBA_Object corba_object)
187 return cspi_object_add_ref (corba_object, FALSE);
191 cspi_object_add_ref (CORBA_Object corba_object, gboolean do_ref)
195 if (corba_object == CORBA_OBJECT_NIL)
199 else if (!cspi_check_ev ("pre method check: add"))
205 if ((ref = g_hash_table_lookup (cspi_get_live_refs (), corba_object)))
207 g_assert (ref->ref_count > 0);
210 cspi_release_unref (corba_object);
212 g_print ("returning cached %p => %p\n", ref, ref->objref);
217 ref = cspi_object_new (corba_object);
220 g_print ("allocated %p => %p\n", ref, corba_object);
223 #ifdef JAVA_BRIDGE_BUG_IS_FIXED
224 g_assert (CORBA_Object_is_a (corba_object,
225 "IDL:Bonobo/Unknown:1.0", cspi_ev ()));
227 Bonobo_Unknown_ref (corba_object, cspi_ev ());
229 g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
237 cspi_object_ref (Accessible *accessible)
239 g_return_if_fail (accessible != NULL);
241 if (g_hash_table_lookup (cspi_get_live_refs (), accessible->objref) == NULL) {
242 accessible->objref = cspi_dup_ref (accessible->objref);
243 g_hash_table_insert (cspi_get_live_refs (), accessible->objref, accessible);
245 accessible->ref_count++;
250 cspi_object_unref (Accessible *accessible)
252 if (accessible == NULL)
257 if (--accessible->ref_count == 0)
259 g_hash_table_remove (cspi_get_live_refs (), accessible->objref);
272 g_hash_table_destroy (refs);
275 if (registry != CORBA_OBJECT_NIL)
277 cspi_release_unref (registry);
278 registry = CORBA_OBJECT_NIL;
282 static gboolean SPI_inited = FALSE;
287 * Connects to the accessibility registry and initializes the SPI.
289 * Returns: 0 on success, otherwise an integer error code.
301 CORBA_exception_init (&ev);
303 registry = cspi_init ();
305 g_atexit (cspi_cleanup);
313 * Starts/enters the main event loop for the SPI services.
315 * (NOTE: This method does not return control, it is exited via a call to
316 * SPI_event_quit () from within an event handler).
320 SPI_event_main (void)
328 * Quits the last main event loop for the SPI services,
332 SPI_event_quit (void)
340 * Checks to see if an SPI event is waiting in the event queue.
341 * Used by clients that don't wish to use SPI_event_main().
343 * Not Yet Implemented.
345 * Returns: #TRUE if an event is waiting, otherwise #FALSE.
356 * @waitForEvent: a #SPIBoolean indicating whether to block or not.
358 * Gets the next event in the SPI event queue; blocks if no event
359 * is pending and @waitForEvent is #TRUE.
360 * Used by clients that don't wish to use SPI_event_main().
362 * Not Yet Implemented.
364 * Returns: the next #AccessibleEvent in the SPI event queue.
367 SPI_nextEvent (SPIBoolean waitForEvent)
373 report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
375 Accessible *a = (Accessible *) val;
377 name = Accessible_getName (a);
378 if (cspi_exception ()) name = NULL;
379 role = Accessible_getRoleName (a);
380 if (cspi_exception ()) role = NULL;
381 fprintf (stderr, "leaked object %s, role %s\n", (name) ? name : "<?>",
382 (role) ? role : "<?>");
383 if (name) SPI_freeString (name);
390 * Disconnects from the Accessibility Registry and releases
391 * any floating resources. Call only once at exit.
393 * Returns: 0 if there were no leaks, otherwise non zero.
409 leaked = g_hash_table_size (live_refs);
412 g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
423 fprintf (stderr, "Leaked %d SPI handles\n", leaked);
429 fprintf (stderr, "bye-bye!\n");
436 * @s: a character string returned from another at-spi call.
438 * Free a character string returned from an at-spi call. Clients of
439 * at-spi should use this function instead of free () or g_free().
440 * This API should not be used to free strings
441 * from other libraries or allocated by the client.
444 SPI_freeString (char *s)