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 */
32 /* Our parent GObject type */
33 #define PARENT_TYPE SPI_LISTENER_TYPE
35 /* A pointer to our parent object class */
36 static SpiListenerClass *spi_registry_parent_class;
51 EventTypeCategory type_cat;
59 Accessibility_EventListener listener;
60 guint event_type_hash;
61 EventTypeCategory event_type_cat;
64 /* static function prototypes */
65 static void _registry_notify_listeners (GList *listeners,
66 const Accessibility_Event *e,
67 CORBA_Environment *ev);
69 static long _get_unique_id();
71 static gboolean _device_event_controller_hook (gpointer source);
74 spi_listener_struct_new (Accessibility_EventListener listener, CORBA_Environment *ev)
76 SpiListenerStruct *retval = g_malloc (sizeof (SpiListenerStruct));
77 retval->listener = bonobo_object_dup_ref (listener, ev);
82 spi_listener_struct_free (SpiListenerStruct *ls, CORBA_Environment *ev)
84 bonobo_object_release_unref (ls->listener, ev);
88 /* GObject::finalize */
90 spi_registry_object_finalize (GObject *object)
92 SpiRegistry *registry = SPI_REGISTRY (object);
94 printf("spi_registry_object_finalize called\n");
95 /* TODO: unref deviceeventcontroller, which disconnects key listener */
96 G_OBJECT_CLASS (spi_registry_parent_class)->finalize (object);
100 * registerApplication:
101 * @application: a reference to the requesting @Application
102 * return values: void
104 * Register a new application with the accessibility broker.
108 impl_accessibility_registry_register_application (PortableServer_Servant servant,
109 const Accessibility_Application application,
110 CORBA_Environment * ev)
112 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
115 fprintf (stderr, "registering app %p\n", application);
117 spi_desktop_add_application (registry->desktop, application);
119 Accessibility_Application__set_id (application, _get_unique_id(), ev);
122 * TODO: change the implementation below to a WM-aware one;
123 * e.g. don't add all apps to the SpiDesktop
128 compare_corba_objects (gconstpointer p1, gconstpointer p2)
130 CORBA_Environment ev;
134 fprintf (stderr, "comparing %p to %p\n",
138 retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
143 register_with_toolkits (SpiRegistry *spi_registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
148 Accessibility_Desktop desktop;
149 Accessibility_Application app;
150 Accessibility_Registry registry;
151 registry = BONOBO_OBJREF (spi_registry_bonobo_object);
153 /* for each app in each desktop, call ...Application_registerToolkitEventListener */
155 n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
157 for (i=0; i<n_desktops; ++i)
159 desktop = Accessibility_Registry_getDesktop (registry, i, ev);
160 n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
161 for (j=0; j<n_apps; ++j)
163 app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
166 Accessibility_Application_registerToolkitEventListener (app,
168 CORBA_string_dup (etype->event_name),
176 compare_listener_hash (gconstpointer p1, gconstpointer p2)
178 return (((SpiListenerStruct *)p2)->event_type_hash - ((SpiListenerStruct *)p1)->event_type_hash);
182 compare_listener_corbaref (gconstpointer p1, gconstpointer p2)
184 return compare_corba_objects (((SpiListenerStruct *)p2)->listener,
185 ((SpiListenerStruct *)p1)->listener);
189 parse_event_type (EventTypeStruct *etype, char *event_name)
191 gchar **split_string;
194 split_string = g_strsplit(event_name, ":", 4);
195 etype->event_name = g_strndup(event_name, 255);
197 if (!g_ascii_strncasecmp (event_name, "focus:", 6))
199 etype->type_cat = ETYPE_FOCUS;
201 else if (!g_ascii_strncasecmp (event_name, "object:", 7))
203 etype->type_cat = ETYPE_OBJECT;
205 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
207 etype->type_cat = ETYPE_WINDOW;
211 etype->type_cat = ETYPE_TOOLKIT;
216 etype->major = g_strdup (split_string[1]);
219 etype->minor = g_strdup (split_string[2]);
222 etype->detail = g_strdup (split_string[3]);
223 s = g_strconcat (split_string[1], split_string[2], split_string[3], NULL);
224 etype->hash = g_str_hash (s);
229 etype->detail = g_strdup ("");
230 s = g_strconcat (split_string[1], split_string[2], NULL);
231 etype->hash = g_str_hash (s);
237 etype->minor = g_strdup ("");
238 etype->hash = g_str_hash ( split_string[1]);
243 etype->major = g_strdup ("");
244 etype->minor = g_strdup ("");
245 etype->detail = g_strdup ("");
246 etype->hash = g_str_hash ("");
249 g_strfreev (split_string);
253 * deregisterApplication:
254 * @application: a reference to the @Application
255 * to be deregistered.
256 * return values: void
258 * De-register an application previously registered with the broker.
262 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
263 const Accessibility_Application application,
264 CORBA_Environment * ev)
266 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
268 spi_desktop_remove_application (registry->desktop, application);
271 fprintf (stderr, "de-registered app %p\n", application);
276 * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
279 impl_accessibility_registry_register_global_event_listener (
280 PortableServer_Servant servant,
281 Accessibility_EventListener listener,
282 const CORBA_char *event_name,
283 CORBA_Environment *ev)
285 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
286 SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
287 EventTypeStruct etype;
289 fprintf(stderr, "registering for events of type %s\n", event_name);
291 /* parse, check major event type and add listener accordingly */
292 parse_event_type (&etype, (char*) event_name);
293 ls->event_type_hash = etype.hash;
294 ls->event_type_cat = etype.type_cat;
296 switch (etype.type_cat)
299 case (ETYPE_OBJECT) :
300 case (ETYPE_PROPERTY) :
301 registry->object_listeners =
302 g_list_append (registry->object_listeners, ls);
304 case (ETYPE_WINDOW) :
305 /* Support for Window Manager Events is not yet implemented */
306 spi_listener_struct_free (ls, ev);
308 case (ETYPE_TOOLKIT) :
309 registry->toolkit_listeners =
310 g_list_append (registry->toolkit_listeners, ls);
311 register_with_toolkits (registry, &etype, ev);
314 spi_listener_struct_free (ls, ev);
320 * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
323 impl_accessibility_registry_deregister_global_event_listener_all (
324 PortableServer_Servant servant,
325 Accessibility_EventListener listener,
326 CORBA_Environment *ev)
328 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
329 SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
331 list = g_list_find_custom (registry->object_listeners, ls,
332 compare_listener_corbaref);
335 * TODO : de-register with toolkit if the last instance of a listener
336 * to a particular toolkit event type has been deregistered.
341 spi_listener_struct_free ((SpiListenerStruct *) list->data, ev);
342 registry->object_listeners = g_list_delete_link (registry->object_listeners, list);
343 list = g_list_find_custom (registry->object_listeners, ls, compare_listener_corbaref);
345 list = g_list_find_custom (registry->toolkit_listeners, ls, compare_listener_corbaref);
348 spi_listener_struct_free ((SpiListenerStruct *) list->data, ev);
349 registry->toolkit_listeners = g_list_delete_link (registry->toolkit_listeners, list);
350 list = g_list_find_custom (registry->toolkit_listeners, ls, compare_listener_corbaref);
352 spi_listener_struct_free (ls, ev);
356 * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
359 impl_accessibility_registry_deregister_global_event_listener (
360 PortableServer_Servant servant,
361 Accessibility_EventListener listener,
362 const CORBA_char * event_name,
363 CORBA_Environment *ev)
365 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
366 SpiListenerStruct ls;
367 EventTypeStruct etype;
371 parse_event_type (&etype, (char *) event_name);
372 switch (etype.type_cat)
374 case (ETYPE_OBJECT) :
375 case (ETYPE_PROPERTY) :
377 listeners = ®istry->object_listeners;
379 case (ETYPE_WINDOW) :
380 /* Support for Window Manager Events is not yet implemented */
383 case (ETYPE_TOOLKIT) :
384 listeners = ®istry->toolkit_listeners;
394 ls.event_type_hash = etype.hash;
395 list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
399 spi_listener_struct_free ((SpiListenerStruct *) list->data, ev);
400 *listeners = g_list_delete_link (*listeners, list);
401 list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
408 * return values: a short integer indicating the current number of
411 * Get the current number of desktops.
415 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
416 CORBA_Environment * ev)
418 /* TODO: implement support for multiple virtual desktops */
419 CORBA_short n_desktops;
420 n_desktops = (CORBA_short) 1;
426 * @n: the index of the requested @Desktop.
427 * return values: a reference to the requested @Desktop.
429 * Get the nth accessible desktop.
432 static Accessibility_Desktop
433 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
435 CORBA_Environment * ev)
437 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
439 /* TODO: implement support for multiple virtual desktops */
442 return (Accessibility_Desktop)
443 bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
447 return (Accessibility_Desktop) CORBA_OBJECT_NIL;
453 * return values: a sequence containing references to
456 * Get a list of accessible desktops.
459 static Accessibility_DesktopSeq *
460 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
461 CORBA_Environment * ev)
463 /* TODO: implement support for multiple virtual desktops */
464 return (Accessibility_DesktopSeq *) NULL;
467 static Accessibility_DeviceEventController
468 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
469 CORBA_Environment * ev)
471 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
472 if (!registry->device_event_controller)
473 registry->device_event_controller = spi_device_event_controller_new (registry);
475 return bonobo_object_dup_ref (BONOBO_OBJREF (registry->device_event_controller), ev);
479 impl_registry_notify_event (PortableServer_Servant servant,
480 const Accessibility_Event *e,
481 CORBA_Environment *ev)
483 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
484 EventTypeStruct etype;
486 parse_event_type (&etype, e->type);
488 switch (etype.type_cat)
490 case (ETYPE_OBJECT) :
491 case (ETYPE_PROPERTY) :
493 _registry_notify_listeners (registry->object_listeners, e, ev);
495 case (ETYPE_WINDOW) :
496 _registry_notify_listeners (registry->window_listeners, e, ev);
498 case (ETYPE_TOOLKIT) :
499 _registry_notify_listeners (registry->toolkit_listeners, e, ev);
501 case (ETYPE_KEYBOARD) :
505 if (e->source != CORBA_OBJECT_NIL)
507 Accessibility_Accessible_unref (e->source, ev);
519 _registry_notify_listeners (GList *listeners,
520 const Accessibility_Event *e_in,
521 CORBA_Environment *ev)
524 Accessibility_Event e_out;
525 SpiListenerStruct *ls;
526 EventTypeStruct etype;
531 parse_event_type (&etype, e_in->type);
533 s = g_strconcat (etype.major, etype.minor, NULL);
534 minor_hash = g_str_hash (s);
537 for (l = listeners; l; l = l->next)
539 ls = (SpiListenerStruct *) l->data;
541 #ifdef SPI_SPI_LISTENER_DEBUG
542 fprintf (stderr, "event hashes: %lx %lx %lx\n", ls->event_type_hash, etype.hash, minor_hash);
543 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", g_list_index (listeners, l->data));
550 s = Accessibility_Accessible__get_name (e_in->source, ev);
551 fprintf (stderr, "event source name %s\n", s);
554 e_out.source = bonobo_object_dup_ref (e_in->source, ev);
555 Accessibility_EventListener_notifyEvent ((Accessibility_EventListener) ls->listener,
558 if (ev->_major != CORBA_NO_EXCEPTION)
560 g_warning ("Accessibility app error: exception during event notification: %s\n",
561 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->desktop = spi_desktop_new();
607 registry->device_event_controller = NULL;
608 registry->kbd_event_hook = _device_event_controller_hook;
611 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
612 Accessibility_Registry,
617 spi_registry_new (void)
619 SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
620 bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);