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;
130 long long diff = ((CORBA_Object_hash ((CORBA_Object) p2, (CORBA_unsigned_long) 0, &ev)) -
131 (CORBA_Object_hash ((CORBA_Object) p1, (CORBA_unsigned_long) 0, &ev)));
134 fprintf (stderr, "comparing %p to %p, via hashes %ld and %ld; diff %ld\n",
136 CORBA_Object_hash ((CORBA_Object) p1, (CORBA_unsigned_long) 0, &ev),
137 CORBA_Object_hash ((CORBA_Object) p2, (CORBA_unsigned_long) 0, &ev),
141 return ((diff < 0) ? -1 : ((diff > 0) ? 1 : 0));
145 register_with_toolkits (Registry *registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
150 Accessibility_Desktop desktop;
151 Accessibility_Application app;
152 Accessibility_Registry registry;
153 registry = bonobo_object_corba_objref (bonobo_object (registry_bonobo_object));
155 /* for each app in each desktop, call ...Application_registerToolkitEventListener */
157 n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
159 for (i=0; i<n_desktops; ++i)
161 desktop = Accessibility_Registry_getDesktop (registry, i, ev);
162 n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
163 for (j=0; j<n_apps; ++j)
165 app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
168 /* TODO: should we be ref-ing the registry object before each call ? */
169 Accessibility_Application_registerToolkitEventListener (app,
171 CORBA_string_dup (etype->event_name),
178 compare_listener_hash (gconstpointer p1, gconstpointer p2)
180 return (((ListenerStruct *)p2)->event_type_hash - ((ListenerStruct *)p1)->event_type_hash);
184 parse_event_type (EventTypeStruct *etype, char *event_name)
187 gchar **split_string;
189 split_string = g_strsplit(event_name, ":", 4);
190 etype->event_name = g_strndup(event_name, 255);
192 if (!g_ascii_strncasecmp (event_name, "focus:", 6))
194 etype->type_cat = ETYPE_FOCUS;
196 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
198 etype->type_cat = ETYPE_WINDOW;
202 etype->type_cat = ETYPE_TOOLKIT;
207 etype->major = split_string[1];
210 etype->minor = split_string[2];
213 etype->detail = split_string[3];
214 etype->hash = g_str_hash ( g_strconcat (split_string[1], split_string[2], split_string[3], NULL));
218 etype->detail = g_strdup ("");
219 etype->hash = g_str_hash ( g_strconcat (split_string[1], split_string[2], NULL));
224 etype->minor = g_strdup ("");
225 etype->hash = g_str_hash ( split_string[1]);
230 etype->major = g_strdup ("");
231 etype->minor = g_strdup ("");
232 etype->detail = g_strdup ("");
233 etype->hash = g_str_hash ("");
236 /* TODO: don't forget to free the strings from caller when done ! */
240 * deregisterApplication:
241 * @application: a reference to the @Application
242 * to be deregistered.
243 * return values: void
245 * De-register an application previously registered with the broker.
249 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
250 const Accessibility_Application application,
251 CORBA_Environment * ev)
253 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
254 GList *list = g_list_find_custom (registry->desktop->applications, application, compare_object_hash);
263 fprintf (stderr, "deregistering application %p\n", application);
265 registry->desktop->applications = g_list_delete_link (registry->desktop->applications, list);
267 fprintf (stderr, "there are now %d apps registered.\n", g_list_length (registry->desktop->applications));
268 for (i = 0; i < g_list_length (registry->desktop->applications); ++i) {
269 fprintf (stderr, "getting application %d\n", i);
270 fprintf (stderr, "object address %p\n",
271 g_list_nth_data (registry->desktop->applications, i));
276 fprintf (stderr, "could not deregister application\n");
280 * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
283 impl_accessibility_registry_register_global_event_listener
284 (PortableServer_Servant servant,
285 Accessibility_EventListener listener,
286 const CORBA_char *event_name,
287 CORBA_Environment *ev)
289 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
290 ListenerStruct *ls = g_malloc (sizeof (ListenerStruct));
291 EventTypeStruct etype;
293 fprintf(stderr, "registering for events of type %s\n", event_name);
295 /* parse, check major event type and add listener accordingly */
296 parse_event_type (&etype, event_name);
297 ls->event_type_hash = etype.hash;
298 ls->event_type_cat = etype.type_cat;
300 switch (etype.type_cat)
303 ls->listener = CORBA_Object_duplicate (listener, ev);
304 registry->focus_listeners =
305 g_list_append (registry->focus_listeners, ls);
307 case (ETYPE_WINDOW) :
308 /* Support for Window Manager Events is not yet implemented */
310 case (ETYPE_TOOLKIT) :
311 ls->listener = CORBA_Object_duplicate (listener, ev);
312 registry->toolkit_listeners =
313 g_list_append (registry->toolkit_listeners, ls);
314 register_with_toolkits (registry, &etype, ev);
322 * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
325 impl_accessibility_registry_deregister_global_event_listener_all
326 (PortableServer_Servant servant,
327 Accessibility_EventListener listener,
328 CORBA_Environment *ev)
330 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
331 GList *list = g_list_find_custom (registry->focus_listeners, listener, compare_object_hash);
334 * TODO : de-register with toolkit if the last instance of a listener
335 * to a particular toolkit event type has been deregistered.
340 fprintf (stderr, "deregistering listener\n");
341 registry->focus_listeners = g_list_delete_link (registry->focus_listeners, list);
342 list = g_list_find_custom (registry->focus_listeners, listener, compare_object_hash);
344 list = g_list_find_custom (registry->toolkit_listeners, listener, compare_object_hash);
347 fprintf (stderr, "deregistering listener\n");
348 registry->toolkit_listeners = g_list_delete_link (registry->toolkit_listeners, list);
349 list = g_list_find_custom (registry->toolkit_listeners, listener, compare_object_hash);
354 * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
357 impl_accessibility_registry_deregister_global_event_listener
358 (PortableServer_Servant servant,
359 Accessibility_EventListener listener,
360 const CORBA_char * event_name,
361 CORBA_Environment *ev)
363 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
365 EventTypeStruct etype;
369 parse_event_type (&etype, event_name);
370 switch (etype.type_cat)
373 listeners = ®istry->focus_listeners;
375 case (ETYPE_WINDOW) :
376 /* Support for Window Manager Events is not yet implemented */
378 case (ETYPE_TOOLKIT) :
379 listeners = ®istry->toolkit_listeners;
385 ls.event_type_hash = etype.hash;
386 list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
390 fprintf (stderr, "deregistering listener\n");
391 *listeners = g_list_delete_link (*listeners, list);
392 list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
399 * return values: a short integer indicating the current number of
402 * Get the current number of desktops.
406 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
407 CORBA_Environment * ev)
409 /* TODO: implement support for multiple virtual desktops */
410 CORBA_short n_desktops;
411 n_desktops = (CORBA_short) 1;
417 * @n: the index of the requested @Desktop.
418 * return values: a reference to the requested @Desktop.
420 * Get the nth accessible desktop.
423 static Accessibility_Desktop
424 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
426 CORBA_Environment * ev)
428 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
430 /* TODO: implement support for multiple virtual desktops */
433 return (Accessibility_Desktop)
434 CORBA_Object_duplicate (
435 bonobo_object_corba_objref (bonobo_object (registry->desktop)), ev);
439 return (Accessibility_Desktop) CORBA_OBJECT_NIL;
445 * return values: a sequence containing references to
448 * Get a list of accessible desktops.
451 static Accessibility_DesktopSeq *
452 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
453 CORBA_Environment * ev)
455 /* TODO: implement support for multiple virtual desktops */
456 return (Accessibility_DesktopSeq *) NULL;
460 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
461 CORBA_Environment * ev)
463 /* TODO: not yet implemented! */
464 return CORBA_OBJECT_NIL;
468 impl_registry_notify_event (PortableServer_Servant servant,
469 const Accessibility_Event *e,
470 CORBA_Environment *ev)
472 Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
473 EventTypeStruct etype;
475 parse_event_type (&etype, e->type);
477 switch (etype.type_cat)
480 _registry_notify_listeners (registry->focus_listeners, e, ev);
482 case (ETYPE_WINDOW) :
483 _registry_notify_listeners (registry->window_listeners, e, ev);
485 case (ETYPE_TOOLKIT) :
486 _registry_notify_listeners (registry->toolkit_listeners, e, ev);
491 Accessibility_Accessible_unref (e->source, ev);
502 _registry_notify_listeners ( GList *listeners,
503 const Accessibility_Event *e,
504 CORBA_Environment *ev)
509 EventTypeStruct etype;
511 parse_event_type (&etype, e->type);
512 minor_hash = g_str_hash (g_strconcat (etype.major, etype.minor, NULL));
513 len = g_list_length (listeners);
515 for (n=0; n<len; ++n)
517 ls = (ListenerStruct *) g_list_nth_data (listeners, n);
519 fprintf(stderr, "event hashes: %lx %lx %lx\n", ls->event_type_hash, etype.hash, minor_hash);
520 fprintf(stderr, "event name: %s\n", etype.event_name);
522 if ((ls->event_type_hash == etype.hash) || (ls->event_type_hash == minor_hash))
525 fprintf(stderr, "notifying listener #%d\n", n);
526 fprintf(stderr, "event name %s\n", Accessibility_Accessible__get_name(e->source, ev));
528 Accessibility_Accessible_ref ( e->source, ev);
529 Accessibility_EventListener_notifyEvent ((Accessibility_EventListener) ls->listener,
532 if (ev->_major != CORBA_NO_EXCEPTION) {
534 ("Accessibility app error: exception during event notification: %s\n"),
535 CORBA_exception_id(ev));
543 registry_class_init (RegistryClass *klass)
545 GObjectClass * object_class = (GObjectClass *) klass;
546 POA_Accessibility_Registry__epv *epv = &klass->epv;
548 registry_parent_class = g_type_class_ref (LISTENER_TYPE);
550 object_class->finalize = registry_object_finalize;
552 epv->registerApplication = impl_accessibility_registry_register_application;
553 epv->deregisterApplication = impl_accessibility_registry_deregister_application;
554 epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
555 epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
556 epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
557 epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
558 epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
559 epv->getDesktop = impl_accessibility_registry_get_desktop;
560 epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
562 ((ListenerClass *) klass)->epv.notifyEvent = impl_registry_notify_event;
566 registry_init (Registry *registry)
568 registry->focus_listeners = NULL;
569 registry->window_listeners = NULL;
570 registry->toolkit_listeners = NULL;
571 registry->applications = NULL;
572 registry->desktop = desktop_new();
576 registry_get_type (void)
578 static GType type = 0;
581 static const GTypeInfo tinfo = {
582 sizeof (RegistryClass),
583 (GBaseInitFunc) NULL,
584 (GBaseFinalizeFunc) NULL,
585 (GClassInitFunc) registry_class_init,
586 (GClassFinalizeFunc) NULL,
587 NULL, /* class data */
590 (GInstanceInitFunc) registry_init,
591 NULL /* value table */
594 * Here we use bonobo_type_unique instead of
595 * gtk_type_unique, this auto-generates a load of
596 * CORBA structures for us. All derived types must
597 * use bonobo_type_unique.
599 type = bonobo_type_unique (
601 POA_Accessibility_Registry__init,
603 G_STRUCT_OFFSET (RegistryClass, epv),
615 REGISTRY (g_object_new (registry_get_type (), NULL));