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.
23 /* registry.c: the main accessibility service registry implementation */
31 * We'd like to replace the dependance on X-isms with a wrapper layer,
32 * to the extent that it can't be done with pure GDK.
33 * Anyone want to help?
39 #include <libspi/registry.h>
41 /* Our parent GObject type */
42 #define PARENT_TYPE SPI_LISTENER_TYPE
44 /* A pointer to our parent object class */
45 static SpiListenerClass *spi_registry_parent_class;
60 EventTypeCategory type_cat;
68 Accessibility_EventListener listener;
69 guint event_type_hash;
70 EventTypeCategory event_type_cat;
73 /* static function prototypes */
74 static void _registry_notify_listeners ( GList *listeners,
75 const Accessibility_Event *e,
76 CORBA_Environment *ev);
78 static long _get_unique_id();
80 static gboolean _device_event_controller_hook (gpointer source);
83 * Implemented GObject::finalize
86 spi_registry_object_finalize (GObject *object)
88 /* SpiRegistry *registry = SPI_REGISTRY (object); */
89 GObjectClass *object_class = G_OBJECT_GET_CLASS( object);
91 printf("spi_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 SpiRegistry *registry = SPI_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 bonobo_object_dup_ref (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 SpiDesktop
127 compare_corba_objects (gconstpointer p1, gconstpointer p2)
129 CORBA_Environment ev;
133 fprintf (stderr, "comparing %p to %p\n",
137 retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
142 register_with_toolkits (SpiRegistry *spi_registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
147 Accessibility_Desktop desktop;
148 Accessibility_Application app;
149 Accessibility_Registry registry;
150 registry = BONOBO_OBJREF (spi_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 Accessibility_Application_registerToolkitEventListener (app,
167 CORBA_string_dup (etype->event_name),
175 compare_listener_hash (gconstpointer p1, gconstpointer p2)
177 return (((SpiListenerStruct *)p2)->event_type_hash - ((SpiListenerStruct *)p1)->event_type_hash);
181 compare_listener_corbaref (gconstpointer p1, gconstpointer p2)
183 return compare_corba_objects (((SpiListenerStruct *)p2)->listener,
184 ((SpiListenerStruct *)p1)->listener);
188 parse_event_type (EventTypeStruct *etype, char *event_name)
190 gchar **split_string;
192 split_string = g_strsplit(event_name, ":", 4);
193 etype->event_name = g_strndup(event_name, 255);
195 if (!g_ascii_strncasecmp (event_name, "focus:", 6))
197 etype->type_cat = ETYPE_FOCUS;
199 else if (!g_ascii_strncasecmp (event_name, "object:", 7))
201 etype->type_cat = ETYPE_OBJECT;
203 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
205 etype->type_cat = ETYPE_WINDOW;
209 etype->type_cat = ETYPE_TOOLKIT;
214 etype->major = split_string[1];
217 etype->minor = split_string[2];
220 etype->detail = split_string[3];
221 etype->hash = g_str_hash ( g_strconcat (split_string[1], split_string[2], split_string[3], NULL));
225 etype->detail = g_strdup ("");
226 etype->hash = g_str_hash ( g_strconcat (split_string[1], split_string[2], NULL));
231 etype->minor = g_strdup ("");
232 etype->hash = g_str_hash ( split_string[1]);
237 etype->major = g_strdup ("");
238 etype->minor = g_strdup ("");
239 etype->detail = g_strdup ("");
240 etype->hash = g_str_hash ("");
243 /* TODO: don't forget to free the strings from caller when done ! */
247 * deregisterApplication:
248 * @application: a reference to the @Application
249 * to be deregistered.
250 * return values: void
252 * De-register an application previously registered with the broker.
256 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
257 const Accessibility_Application application,
258 CORBA_Environment * ev)
260 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
261 GList *list = g_list_find_custom (registry->desktop->applications, &application, compare_corba_objects);
270 fprintf (stderr, "deregistering application %p\n", application);
272 registry->desktop->applications = g_list_delete_link (registry->desktop->applications, list);
274 fprintf (stderr, "there are now %d apps registered.\n", g_list_length (registry->desktop->applications));
275 for (i = 0; i < g_list_length (registry->desktop->applications); ++i) {
276 fprintf (stderr, "getting application %d\n", i);
277 fprintf (stderr, "object address %p\n",
278 g_list_nth_data (registry->desktop->applications, i));
283 fprintf (stderr, "could not deregister application\n");
287 * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
290 impl_accessibility_registry_register_global_event_listener (
291 PortableServer_Servant servant,
292 Accessibility_EventListener listener,
293 const CORBA_char *event_name,
294 CORBA_Environment *ev)
296 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
297 SpiListenerStruct *ls = g_malloc (sizeof (SpiListenerStruct));
298 EventTypeStruct etype;
300 fprintf(stderr, "registering for events of type %s\n", event_name);
302 /* parse, check major event type and add listener accordingly */
303 parse_event_type (&etype, (char*) event_name);
304 ls->event_type_hash = etype.hash;
305 ls->event_type_cat = etype.type_cat;
307 switch (etype.type_cat)
310 case (ETYPE_OBJECT) :
311 case (ETYPE_PROPERTY) :
312 ls->listener = CORBA_Object_duplicate (listener, ev);
313 registry->object_listeners =
314 g_list_append (registry->object_listeners, ls);
316 case (ETYPE_WINDOW) :
317 /* Support for Window Manager Events is not yet implemented */
319 case (ETYPE_TOOLKIT) :
320 ls->listener = CORBA_Object_duplicate (listener, ev);
321 registry->toolkit_listeners =
322 g_list_append (registry->toolkit_listeners, ls);
323 register_with_toolkits (registry, &etype, ev);
331 * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
334 impl_accessibility_registry_deregister_global_event_listener_all (
335 PortableServer_Servant servant,
336 Accessibility_EventListener listener,
337 CORBA_Environment *ev)
339 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
340 SpiListenerStruct *ls = g_malloc (sizeof (SpiListenerStruct));
342 ls->listener = listener;
343 list = g_list_find_custom (registry->object_listeners, ls,
344 compare_listener_corbaref);
347 * TODO : de-register with toolkit if the last instance of a listener
348 * to a particular toolkit event type has been deregistered.
353 fprintf (stderr, "deregistering listener\n");
354 registry->object_listeners = g_list_delete_link (registry->object_listeners, list);
355 list = g_list_find_custom (registry->object_listeners, ls, compare_listener_corbaref);
357 list = g_list_find_custom (registry->toolkit_listeners, ls, compare_listener_corbaref);
360 fprintf (stderr, "deregistering listener\n");
361 registry->toolkit_listeners = g_list_delete_link (registry->toolkit_listeners, list);
362 list = g_list_find_custom (registry->toolkit_listeners, ls, compare_listener_corbaref);
367 * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
370 impl_accessibility_registry_deregister_global_event_listener (
371 PortableServer_Servant servant,
372 Accessibility_EventListener listener,
373 const CORBA_char * event_name,
374 CORBA_Environment *ev)
376 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
377 SpiListenerStruct ls;
378 EventTypeStruct etype;
382 parse_event_type (&etype, (char *) event_name);
383 switch (etype.type_cat)
385 case (ETYPE_OBJECT) :
386 case (ETYPE_PROPERTY) :
388 listeners = ®istry->object_listeners;
390 case (ETYPE_WINDOW) :
391 /* Support for Window Manager Events is not yet implemented */
393 case (ETYPE_TOOLKIT) :
394 listeners = ®istry->toolkit_listeners;
404 ls.event_type_hash = etype.hash;
405 list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
409 fprintf (stderr, "deregistering listener\n");
410 *listeners = g_list_delete_link (*listeners, list);
411 list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
418 * return values: a short integer indicating the current number of
421 * Get the current number of desktops.
425 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
426 CORBA_Environment * ev)
428 /* TODO: implement support for multiple virtual desktops */
429 CORBA_short n_desktops;
430 n_desktops = (CORBA_short) 1;
436 * @n: the index of the requested @Desktop.
437 * return values: a reference to the requested @Desktop.
439 * Get the nth accessible desktop.
442 static Accessibility_Desktop
443 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
445 CORBA_Environment * ev)
447 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
449 /* TODO: implement support for multiple virtual desktops */
452 return (Accessibility_Desktop)
453 CORBA_Object_duplicate (BONOBO_OBJREF (registry->desktop), ev);
457 return (Accessibility_Desktop) CORBA_OBJECT_NIL;
463 * return values: a sequence containing references to
466 * Get a list of accessible desktops.
469 static Accessibility_DesktopSeq *
470 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
471 CORBA_Environment * ev)
473 /* TODO: implement support for multiple virtual desktops */
474 return (Accessibility_DesktopSeq *) NULL;
477 static Accessibility_DeviceEventController
478 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
479 CORBA_Environment * ev)
481 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
482 if (!registry->device_event_controller)
483 registry->device_event_controller = spi_device_event_controller_new (registry);
485 return CORBA_Object_duplicate (BONOBO_OBJREF (registry->device_event_controller), ev);
489 impl_registry_notify_event (PortableServer_Servant servant,
490 const Accessibility_Event *e,
491 CORBA_Environment *ev)
493 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
494 EventTypeStruct etype;
496 parse_event_type (&etype, e->type);
498 switch (etype.type_cat)
500 case (ETYPE_OBJECT) :
501 case (ETYPE_PROPERTY) :
503 _registry_notify_listeners (registry->object_listeners, e, ev);
505 case (ETYPE_WINDOW) :
506 _registry_notify_listeners (registry->window_listeners, e, ev);
508 case (ETYPE_TOOLKIT) :
509 _registry_notify_listeners (registry->toolkit_listeners, e, ev);
511 case (ETYPE_KEYBOARD) :
515 /* Accessibility_Accessible_unref (e->source, ev);*/ /* This should be here! */
526 _registry_notify_listeners ( GList *listeners,
527 const Accessibility_Event *e,
528 CORBA_Environment *ev)
532 SpiListenerStruct *ls;
533 EventTypeStruct etype;
535 parse_event_type (&etype, e->type);
536 minor_hash = g_str_hash (g_strconcat (etype.major, etype.minor, NULL));
537 len = g_list_length (listeners);
539 for (n=0; n<len; ++n)
541 ls = (SpiListenerStruct *) g_list_nth_data (listeners, n);
542 #ifdef SPI_SPI_LISTENER_DEBUG
543 fprintf(stderr, "event hashes: %lx %lx %lx\n", ls->event_type_hash, etype.hash, minor_hash);
544 fprintf(stderr, "event name: %s\n", etype.event_name);
546 if ((ls->event_type_hash == etype.hash) || (ls->event_type_hash == minor_hash))
549 fprintf(stderr, "notifying listener #%d\n", n);
550 fprintf(stderr, "event source name %s\n", Accessibility_Accessible__get_name(e->source, ev));
552 e->source = CORBA_Object_duplicate (e->source, ev);
553 Accessibility_Accessible_ref ( e->source, ev);
554 Accessibility_EventListener_notifyEvent ((Accessibility_EventListener) ls->listener,
557 if (ev->_major != CORBA_NO_EXCEPTION) {
559 ("Accessibility app error: exception during event notification: %s\n"),
560 CORBA_exception_id(ev));
568 _device_event_controller_hook (gpointer p)
570 SpiRegistry *registry = (SpiRegistry *)p;
571 SpiDeviceEventController *controller = registry->device_event_controller;
573 spi_device_event_controller_check_key_event (controller);
578 spi_registry_class_init (SpiRegistryClass *klass)
580 GObjectClass * object_class = (GObjectClass *) klass;
581 POA_Accessibility_Registry__epv *epv = &klass->epv;
583 spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
585 object_class->finalize = spi_registry_object_finalize;
587 epv->registerApplication = impl_accessibility_registry_register_application;
588 epv->deregisterApplication = impl_accessibility_registry_deregister_application;
589 epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
590 epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
591 epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
592 epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
593 epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
594 epv->getDesktop = impl_accessibility_registry_get_desktop;
595 epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
597 ((SpiListenerClass *) klass)->epv.notifyEvent = impl_registry_notify_event;
601 spi_registry_init (SpiRegistry *registry)
603 registry->object_listeners = NULL;
604 registry->window_listeners = NULL;
605 registry->toolkit_listeners = NULL;
606 registry->applications = NULL;
607 registry->desktop = spi_desktop_new();
608 registry->device_event_controller = NULL;
609 registry->kbd_event_hook = _device_event_controller_hook;
612 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
613 Accessibility_Registry,
618 spi_registry_new (void)
620 SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);