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.
24 * registry.c: test for accessibility implementation
32 #include <bonobo/Bonobo.h>
35 * This pulls the CORBA definitions for the "Accessibility::Registry" server
37 #include <libspi/Accessibility.h>
40 * This pulls the definition for the BonoboObject (GType)
45 * Our parent GObject type
47 #define PARENT_TYPE LISTENER_TYPE
50 * A pointer to our parent object class
52 static ListenerClass *registry_parent_class;
63 EventTypeCategory type_cat;
71 Accessibility_EventListener listener;
72 guint event_type_hash;
73 EventTypeCategory event_type_cat;
76 /* static function prototypes */
77 static void registry_notify_listeners ( GList *listeners,
78 const Accessibility_Event *e,
79 CORBA_Environment *ev);
82 * Implemented GObject::finalize
85 registry_object_finalize (GObject *object)
87 /* Registry *registry = REGISTRY (object); */
88 GObjectClass *object_class = G_OBJECT_GET_CLASS( object);
90 printf("registry_object_finalize called\n");
92 object_class->finalize (object);
96 * registerApplication:
97 * @application: a reference to the requesting @Application
100 * Register a new application with the accessibility broker.
104 impl_accessibility_registry_register_application (PortableServer_Servant servant,
105 const Accessibility_Application application,
106 CORBA_Environment * ev)
108 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
111 fprintf (stderr, "registering app %p\n", application);
113 registry->desktop->applications = g_list_append (registry->desktop->applications,
114 CORBA_Object_duplicate (application, ev));
116 /* TODO: create unique string here (with libuuid call ?) */
117 Accessibility_Application__set_id (application, "test-some-unique-string", ev);
120 * TODO: change the implementation below to a WM-aware one;
121 * e.g. don't add all apps to the Desktop
126 compare_object_hash (gconstpointer p1, gconstpointer p2)
128 CORBA_Environment ev;
129 long long diff = ((CORBA_Object_hash ((CORBA_Object) p2, (CORBA_unsigned_long) 0, &ev)) -
130 (CORBA_Object_hash ((CORBA_Object) p1, (CORBA_unsigned_long) 0, &ev)));
131 return ((diff < 0) ? -1 : ((diff > 0) ? 1 : 0));
135 register_with_toolkits (Registry *registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
140 Accessibility_Desktop desktop;
141 Accessibility_Application app;
142 Accessibility_Registry registry;
143 registry = bonobo_object_corba_objref (bonobo_object (registry_bonobo_object));
145 /* for each app in each desktop, call ...Application_registerToolkitEventListener */
147 n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
149 for (i=0; i<n_desktops; ++i)
151 desktop = Accessibility_Registry_getDesktop (registry, i, ev);
152 n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
153 for (j=0; j<n_apps; ++j)
155 app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
158 /* TODO: should we be ref-ing the registry object before each call ? */
159 Accessibility_Application_registerToolkitEventListener (app,
161 CORBA_string_dup (etype->event_name),
168 compare_listener_hash (gconstpointer p1, gconstpointer p2)
170 return (((ListenerStruct *)p2)->event_type_hash - ((ListenerStruct *)p1)->event_type_hash);
174 parse_event_type (EventTypeStruct *etype, char *event_name)
177 gchar **split_string;
179 split_string = g_strsplit(event_name, ":", 4);
180 etype->event_name = g_strndup(event_name, 255);
182 if (!g_ascii_strncasecmp (event_name, "focus:", 6))
184 etype->type_cat = ETYPE_FOCUS;
186 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
188 etype->type_cat = ETYPE_WINDOW;
192 etype->type_cat = ETYPE_TOOLKIT;
197 etype->major = split_string[1];
200 etype->minor = split_string[2];
203 etype->detail = split_string[3];
204 etype->hash = g_str_hash ( g_strconcat (split_string[1], split_string[2], split_string[3], NULL));
208 etype->detail = g_strdup ("");
209 etype->hash = g_str_hash ( g_strconcat (split_string[1], split_string[2], NULL));
214 etype->minor = g_strdup ("");
215 etype->hash = g_str_hash ( split_string[1]);
220 etype->major = g_strdup ("");
221 etype->minor = g_strdup ("");
222 etype->detail = g_strdup ("");
223 etype->hash = g_str_hash ("");
226 /* TODO: don't forget to free the strings from caller when done ! */
230 * deregisterApplication:
231 * @application: a reference to the @Application
232 * to be deregistered.
233 * return values: void
235 * De-register an application previously registered with the broker.
239 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
240 const Accessibility_Application application,
241 CORBA_Environment * ev)
243 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
244 GList *list = g_list_find_custom (registry->applications, application, compare_object_hash);
247 fprintf (stderr, "deregistering application\n");
248 registry->applications = g_list_delete_link (registry->applications, list);
253 * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
256 impl_accessibility_registry_register_global_event_listener
257 (PortableServer_Servant servant,
258 Accessibility_EventListener listener,
259 const CORBA_char *event_name,
260 CORBA_Environment *ev)
262 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
263 ListenerStruct *ls = g_malloc (sizeof (ListenerStruct));
264 EventTypeStruct etype;
266 fprintf(stderr, "registering for events of type %s\n", event_name);
268 /* parse, check major event type and add listener accordingly */
269 parse_event_type (&etype, event_name);
270 ls->event_type_hash = etype.hash;
271 ls->event_type_cat = etype.type_cat;
273 switch (etype.type_cat)
276 ls->listener = CORBA_Object_duplicate (listener, ev);
277 registry->focus_listeners =
278 g_list_append (registry->focus_listeners, ls);
280 case (ETYPE_WINDOW) :
281 /* Support for Window Manager Events is not yet implemented */
283 case (ETYPE_TOOLKIT) :
284 ls->listener = CORBA_Object_duplicate (listener, ev);
285 registry->toolkit_listeners =
286 g_list_append (registry->toolkit_listeners, ls);
287 register_with_toolkits (registry, &etype, ev);
295 * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
298 impl_accessibility_registry_deregister_global_event_listener_all
299 (PortableServer_Servant servant,
300 Accessibility_EventListener listener,
301 CORBA_Environment *ev)
303 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
304 GList *list = g_list_find_custom (registry->focus_listeners, listener, compare_object_hash);
307 * TODO : de-register with toolkit if the last instance of a listener
308 * to a particular toolkit event type has been deregistered.
313 fprintf (stderr, "deregistering listener\n");
314 registry->focus_listeners = g_list_delete_link (registry->focus_listeners, list);
315 list = g_list_find_custom (registry->focus_listeners, listener, compare_object_hash);
317 list = g_list_find_custom (registry->toolkit_listeners, listener, compare_object_hash);
320 fprintf (stderr, "deregistering listener\n");
321 registry->toolkit_listeners = g_list_delete_link (registry->toolkit_listeners, list);
322 list = g_list_find_custom (registry->toolkit_listeners, listener, compare_object_hash);
327 * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
330 impl_accessibility_registry_deregister_global_event_listener
331 (PortableServer_Servant servant,
332 Accessibility_EventListener listener,
333 const CORBA_char * event_name,
334 CORBA_Environment *ev)
336 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
338 EventTypeStruct etype;
342 parse_event_type (&etype, event_name);
343 switch (etype.type_cat)
346 listeners = ®istry->focus_listeners;
348 case (ETYPE_WINDOW) :
349 /* Support for Window Manager Events is not yet implemented */
351 case (ETYPE_TOOLKIT) :
352 listeners = ®istry->toolkit_listeners;
358 ls.event_type_hash = etype.hash;
359 list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
363 fprintf (stderr, "deregistering listener\n");
364 *listeners = g_list_delete_link (*listeners, list);
365 list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
372 * return values: a short integer indicating the current number of
375 * Get the current number of desktops.
379 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
380 CORBA_Environment * ev)
382 /* TODO: implement support for multiple virtual desktops */
383 CORBA_short n_desktops;
384 n_desktops = (CORBA_short) 1;
390 * @n: the index of the requested @Desktop.
391 * return values: a reference to the requested @Desktop.
393 * Get the nth accessible desktop.
396 static Accessibility_Desktop
397 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
399 CORBA_Environment * ev)
401 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
403 /* TODO: implement support for multiple virtual desktops */
406 return (Accessibility_Desktop)
407 CORBA_Object_duplicate (
408 bonobo_object_corba_objref (bonobo_object (registry->desktop)), ev);
412 return (Accessibility_Desktop) CORBA_OBJECT_NIL;
418 * return values: a sequence containing references to
421 * Get a list of accessible desktops.
424 static Accessibility_DesktopSeq *
425 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
426 CORBA_Environment * ev)
428 /* TODO: implement support for multiple virtual desktops */
429 return (Accessibility_DesktopSeq *) NULL;
433 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
434 CORBA_Environment * ev)
436 /* TODO: not yet implemented! */
437 return CORBA_OBJECT_NIL;
441 impl_registry_notify_event (PortableServer_Servant servant,
442 const Accessibility_Event *e,
443 CORBA_Environment *ev)
445 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
446 EventTypeStruct etype;
448 parse_event_type (&etype, e->type);
450 switch (etype.type_cat)
453 registry_notify_listeners (registry->focus_listeners, e, ev);
455 case (ETYPE_WINDOW) :
456 registry_notify_listeners (registry->window_listeners, e, ev);
458 case (ETYPE_TOOLKIT) :
459 registry_notify_listeners (registry->toolkit_listeners, e, ev);
464 bonobo_object_release_unref (e->target, ev);
468 registry_notify_listeners ( GList *listeners,
469 const Accessibility_Event *e,
470 CORBA_Environment *ev)
475 EventTypeStruct etype;
477 parse_event_type (&etype, e->type);
478 minor_hash = g_str_hash (g_strconcat (etype.major, etype.minor, NULL));
479 len = g_list_length (listeners);
481 for (n=0; n<len; ++n)
483 ls = (ListenerStruct *) g_list_nth_data (listeners, n);
485 fprintf(stderr, "event hashes: %lx %lx %lx\n", ls->event_type_hash, etype.hash, minor_hash);
486 fprintf(stderr, "event name: %s\n", etype.event_name);
488 if ((ls->event_type_hash == etype.hash) || (ls->event_type_hash == minor_hash))
491 fprintf(stderr, "notifying listener #%d\n", n);
492 fprintf(stderr, "event name %s\n", Accessibility_Accessible__get_name(e->target, ev));
494 bonobo_object_dup_ref ( e->target, ev);
495 Accessibility_EventListener_notifyEvent ((Accessibility_EventListener) ls->listener,
498 if (ev->_major != CORBA_NO_EXCEPTION) {
500 ("Accessibility app error: exception during event notification: %s\n"),
501 CORBA_exception_id(ev));
509 registry_class_init (RegistryClass *klass)
511 GObjectClass * object_class = (GObjectClass *) klass;
512 POA_Accessibility_Registry__epv *epv = &klass->epv;
514 registry_parent_class = g_type_class_ref (LISTENER_TYPE);
516 object_class->finalize = registry_object_finalize;
518 epv->registerApplication = impl_accessibility_registry_register_application;
519 epv->deregisterApplication = impl_accessibility_registry_deregister_application;
520 epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
521 epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
522 epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
523 epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
524 epv->getDesktop = impl_accessibility_registry_get_desktop;
525 epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
526 epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
527 ((ListenerClass *) klass)->epv.notifyEvent = impl_registry_notify_event;
531 registry_init (Registry *registry)
533 registry->focus_listeners = NULL;
534 registry->window_listeners = NULL;
535 registry->toolkit_listeners = NULL;
536 registry->applications = NULL;
537 registry->desktop = desktop_new();
541 registry_get_type (void)
543 static GType type = 0;
546 static const GTypeInfo tinfo = {
547 sizeof (RegistryClass),
548 (GBaseInitFunc) NULL,
549 (GBaseFinalizeFunc) NULL,
550 (GClassInitFunc) registry_class_init,
551 (GClassFinalizeFunc) NULL,
552 NULL, /* class data */
555 (GInstanceInitFunc) registry_init,
556 NULL /* value table */
559 * Here we use bonobo_type_unique instead of
560 * gtk_type_unique, this auto-generates a load of
561 * CORBA structures for us. All derived types must
562 * use bonobo_type_unique.
564 type = bonobo_type_unique (
566 POA_Accessibility_Registry__init,
568 G_STRUCT_OFFSET (RegistryClass, epv),
580 REGISTRY (g_object_new (registry_get_type (), NULL));