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: the main accessibility service registry implementation
31 #include <bonobo/Bonobo.h>
34 * This pulls the CORBA definitions for the "Accessibility::Registry" server
36 #include <libspi/Accessibility.h>
39 * This pulls the definition for the BonoboObject (GType)
44 * Our parent GObject type
46 #define PARENT_TYPE LISTENER_TYPE
49 * A pointer to our parent object class
51 static ListenerClass *registry_parent_class;
62 EventTypeCategory type_cat;
70 Accessibility_EventListener listener;
71 guint event_type_hash;
72 EventTypeCategory event_type_cat;
75 /* static function prototypes */
76 static void _registry_notify_listeners ( GList *listeners,
77 const Accessibility_Event *e,
78 CORBA_Environment *ev);
80 static long _get_unique_id();
83 * Implemented GObject::finalize
86 registry_object_finalize (GObject *object)
88 /* Registry *registry = REGISTRY (object); */
89 GObjectClass *object_class = G_OBJECT_GET_CLASS( object);
91 printf("registry_object_finalize called\n");
93 object_class->finalize (object);
97 * registerApplication:
98 * @application: a reference to the requesting @Application
101 * Register a new application with the accessibility broker.
105 impl_accessibility_registry_register_application (PortableServer_Servant servant,
106 const Accessibility_Application application,
107 CORBA_Environment * ev)
109 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
112 fprintf (stderr, "registering app %p\n", application);
114 registry->desktop->applications = g_list_append (registry->desktop->applications,
115 CORBA_Object_duplicate (application, ev));
117 /* TODO: create unique string here (with libuuid call ?) and hash ? */
118 Accessibility_Application__set_id (application, _get_unique_id(), ev);
121 * TODO: change the implementation below to a WM-aware one;
122 * e.g. don't add all apps to the Desktop
127 compare_object_hash (gconstpointer p1, gconstpointer p2)
129 CORBA_Environment ev;
131 retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
134 fprintf (stderr, "comparing %p to %p; result %d\n",
142 register_with_toolkits (Registry *registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
147 Accessibility_Desktop desktop;
148 Accessibility_Application app;
149 Accessibility_Registry registry;
150 registry = bonobo_object_corba_objref (bonobo_object (registry_bonobo_object));
152 /* for each app in each desktop, call ...Application_registerToolkitEventListener */
154 n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
156 for (i=0; i<n_desktops; ++i)
158 desktop = Accessibility_Registry_getDesktop (registry, i, ev);
159 n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
160 for (j=0; j<n_apps; ++j)
162 app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
165 /* TODO: should we be ref-ing the registry object before each call ? */
166 Accessibility_Application_registerToolkitEventListener (app,
168 CORBA_string_dup (etype->event_name),
175 compare_listener_hash (gconstpointer p1, gconstpointer p2)
177 return (((ListenerStruct *)p2)->event_type_hash - ((ListenerStruct *)p1)->event_type_hash);
181 parse_event_type (EventTypeStruct *etype, char *event_name)
184 gchar **split_string;
186 split_string = g_strsplit(event_name, ":", 4);
187 etype->event_name = g_strndup(event_name, 255);
189 if (!g_ascii_strncasecmp (event_name, "focus:", 6))
191 etype->type_cat = ETYPE_FOCUS;
193 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
195 etype->type_cat = ETYPE_WINDOW;
199 etype->type_cat = ETYPE_TOOLKIT;
204 etype->major = split_string[1];
207 etype->minor = split_string[2];
210 etype->detail = split_string[3];
211 etype->hash = g_str_hash ( g_strconcat (split_string[1], split_string[2], split_string[3], NULL));
215 etype->detail = g_strdup ("");
216 etype->hash = g_str_hash ( g_strconcat (split_string[1], split_string[2], NULL));
221 etype->minor = g_strdup ("");
222 etype->hash = g_str_hash ( split_string[1]);
227 etype->major = g_strdup ("");
228 etype->minor = g_strdup ("");
229 etype->detail = g_strdup ("");
230 etype->hash = g_str_hash ("");
233 /* TODO: don't forget to free the strings from caller when done ! */
237 * deregisterApplication:
238 * @application: a reference to the @Application
239 * to be deregistered.
240 * return values: void
242 * De-register an application previously registered with the broker.
246 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
247 const Accessibility_Application application,
248 CORBA_Environment * ev)
250 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
251 GList *list = g_list_find_custom (registry->desktop->applications, application, compare_object_hash);
260 fprintf (stderr, "deregistering application %p\n", application);
262 registry->desktop->applications = g_list_delete_link (registry->desktop->applications, list);
264 fprintf (stderr, "there are now %d apps registered.\n", g_list_length (registry->desktop->applications));
265 for (i = 0; i < g_list_length (registry->desktop->applications); ++i) {
266 fprintf (stderr, "getting application %d\n", i);
267 fprintf (stderr, "object address %p\n",
268 g_list_nth_data (registry->desktop->applications, i));
273 fprintf (stderr, "could not deregister application\n");
277 * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
280 impl_accessibility_registry_register_global_event_listener
281 (PortableServer_Servant servant,
282 Accessibility_EventListener listener,
283 const CORBA_char *event_name,
284 CORBA_Environment *ev)
286 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
287 ListenerStruct *ls = g_malloc (sizeof (ListenerStruct));
288 EventTypeStruct etype;
290 fprintf(stderr, "registering for events of type %s\n", event_name);
292 /* parse, check major event type and add listener accordingly */
293 parse_event_type (&etype, event_name);
294 ls->event_type_hash = etype.hash;
295 ls->event_type_cat = etype.type_cat;
297 switch (etype.type_cat)
300 ls->listener = CORBA_Object_duplicate (listener, ev);
301 registry->focus_listeners =
302 g_list_append (registry->focus_listeners, ls);
304 case (ETYPE_WINDOW) :
305 /* Support for Window Manager Events is not yet implemented */
307 case (ETYPE_TOOLKIT) :
308 ls->listener = CORBA_Object_duplicate (listener, ev);
309 registry->toolkit_listeners =
310 g_list_append (registry->toolkit_listeners, ls);
311 register_with_toolkits (registry, &etype, ev);
319 * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
322 impl_accessibility_registry_deregister_global_event_listener_all
323 (PortableServer_Servant servant,
324 Accessibility_EventListener listener,
325 CORBA_Environment *ev)
327 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
328 GList *list = g_list_find_custom (registry->focus_listeners, listener, compare_object_hash);
331 * TODO : de-register with toolkit if the last instance of a listener
332 * to a particular toolkit event type has been deregistered.
337 fprintf (stderr, "deregistering listener\n");
338 registry->focus_listeners = g_list_delete_link (registry->focus_listeners, list);
339 list = g_list_find_custom (registry->focus_listeners, listener, compare_object_hash);
341 list = g_list_find_custom (registry->toolkit_listeners, listener, compare_object_hash);
344 fprintf (stderr, "deregistering listener\n");
345 registry->toolkit_listeners = g_list_delete_link (registry->toolkit_listeners, list);
346 list = g_list_find_custom (registry->toolkit_listeners, listener, compare_object_hash);
351 * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
354 impl_accessibility_registry_deregister_global_event_listener
355 (PortableServer_Servant servant,
356 Accessibility_EventListener listener,
357 const CORBA_char * event_name,
358 CORBA_Environment *ev)
360 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
362 EventTypeStruct etype;
366 parse_event_type (&etype, event_name);
367 switch (etype.type_cat)
370 listeners = ®istry->focus_listeners;
372 case (ETYPE_WINDOW) :
373 /* Support for Window Manager Events is not yet implemented */
375 case (ETYPE_TOOLKIT) :
376 listeners = ®istry->toolkit_listeners;
382 ls.event_type_hash = etype.hash;
383 list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
387 fprintf (stderr, "deregistering listener\n");
388 *listeners = g_list_delete_link (*listeners, list);
389 list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
396 * return values: a short integer indicating the current number of
399 * Get the current number of desktops.
403 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
404 CORBA_Environment * ev)
406 /* TODO: implement support for multiple virtual desktops */
407 CORBA_short n_desktops;
408 n_desktops = (CORBA_short) 1;
414 * @n: the index of the requested @Desktop.
415 * return values: a reference to the requested @Desktop.
417 * Get the nth accessible desktop.
420 static Accessibility_Desktop
421 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
423 CORBA_Environment * ev)
425 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
427 /* TODO: implement support for multiple virtual desktops */
430 return (Accessibility_Desktop)
431 CORBA_Object_duplicate (
432 bonobo_object_corba_objref (bonobo_object (registry->desktop)), ev);
436 return (Accessibility_Desktop) CORBA_OBJECT_NIL;
442 * return values: a sequence containing references to
445 * Get a list of accessible desktops.
448 static Accessibility_DesktopSeq *
449 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
450 CORBA_Environment * ev)
452 /* TODO: implement support for multiple virtual desktops */
453 return (Accessibility_DesktopSeq *) NULL;
457 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
458 CORBA_Environment * ev)
460 /* TODO: not yet implemented! */
461 return CORBA_OBJECT_NIL;
465 impl_registry_notify_event (PortableServer_Servant servant,
466 const Accessibility_Event *e,
467 CORBA_Environment *ev)
469 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
470 EventTypeStruct etype;
472 parse_event_type (&etype, e->type);
474 switch (etype.type_cat)
477 _registry_notify_listeners (registry->focus_listeners, e, ev);
479 case (ETYPE_WINDOW) :
480 _registry_notify_listeners (registry->window_listeners, e, ev);
482 case (ETYPE_TOOLKIT) :
483 _registry_notify_listeners (registry->toolkit_listeners, e, ev);
488 Accessibility_Accessible_unref (e->source, ev);
499 _registry_notify_listeners ( GList *listeners,
500 const Accessibility_Event *e,
501 CORBA_Environment *ev)
506 EventTypeStruct etype;
508 parse_event_type (&etype, e->type);
509 minor_hash = g_str_hash (g_strconcat (etype.major, etype.minor, NULL));
510 len = g_list_length (listeners);
512 for (n=0; n<len; ++n)
514 ls = (ListenerStruct *) g_list_nth_data (listeners, n);
516 fprintf(stderr, "event hashes: %lx %lx %lx\n", ls->event_type_hash, etype.hash, minor_hash);
517 fprintf(stderr, "event name: %s\n", etype.event_name);
519 if ((ls->event_type_hash == etype.hash) || (ls->event_type_hash == minor_hash))
522 fprintf(stderr, "notifying listener #%d\n", n);
523 fprintf(stderr, "event name %s\n", Accessibility_Accessible__get_name(e->source, ev));
525 Accessibility_Accessible_ref ( e->source, ev);
526 Accessibility_EventListener_notifyEvent ((Accessibility_EventListener) ls->listener,
529 if (ev->_major != CORBA_NO_EXCEPTION) {
531 ("Accessibility app error: exception during event notification: %s\n"),
532 CORBA_exception_id(ev));
540 registry_class_init (RegistryClass *klass)
542 GObjectClass * object_class = (GObjectClass *) klass;
543 POA_Accessibility_Registry__epv *epv = &klass->epv;
545 registry_parent_class = g_type_class_ref (LISTENER_TYPE);
547 object_class->finalize = registry_object_finalize;
549 epv->registerApplication = impl_accessibility_registry_register_application;
550 epv->deregisterApplication = impl_accessibility_registry_deregister_application;
551 epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
552 epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
553 epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
554 epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
555 epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
556 epv->getDesktop = impl_accessibility_registry_get_desktop;
557 epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
559 ((ListenerClass *) klass)->epv.notifyEvent = impl_registry_notify_event;
563 registry_init (Registry *registry)
565 registry->focus_listeners = NULL;
566 registry->window_listeners = NULL;
567 registry->toolkit_listeners = NULL;
568 registry->applications = NULL;
569 registry->desktop = desktop_new();
573 registry_get_type (void)
575 static GType type = 0;
578 static const GTypeInfo tinfo = {
579 sizeof (RegistryClass),
580 (GBaseInitFunc) NULL,
581 (GBaseFinalizeFunc) NULL,
582 (GClassInitFunc) registry_class_init,
583 (GClassFinalizeFunc) NULL,
584 NULL, /* class data */
587 (GInstanceInitFunc) registry_init,
588 NULL /* value table */
591 * Here we use bonobo_type_unique instead of
592 * gtk_type_unique, this auto-generates a load of
593 * CORBA structures for us. All derived types must
594 * use bonobo_type_unique.
596 type = bonobo_type_unique (
598 POA_Accessibility_Registry__init,
600 G_STRUCT_OFFSET (RegistryClass, epv),
612 REGISTRY (g_object_new (registry_get_type (), NULL));