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 */
25 #undef SPI_LISTENER_DEBUG
32 #include <bonobo/bonobo-exception.h>
33 #include "../libspi/spi-private.h"
36 /* Our parent GObject type */
37 #define PARENT_TYPE SPI_LISTENER_TYPE
39 /* A pointer to our parent object class */
40 static SpiListenerClass *spi_registry_parent_class;
55 EventTypeCategory type_cat;
56 GQuark major; /* from string segment[1] */
57 GQuark minor; /* from string segment[1]+segment[2] */
58 GQuark detail; /* from string segment[3] (not concatenated) */
62 Accessibility_EventListener listener;
63 GQuark event_type_quark;
64 EventTypeCategory event_type_cat;
69 spi_listener_struct_new (Accessibility_EventListener listener, CORBA_Environment *ev)
71 SpiListenerStruct *retval = g_malloc (sizeof (SpiListenerStruct));
72 retval->listener = bonobo_object_dup_ref (listener, ev);
78 spi_listener_struct_free (SpiListenerStruct *ls, CORBA_Environment *ev)
80 bonobo_object_release_unref (ls->listener, ev);
86 desktop_add_application (SpiDesktop *desktop,
87 guint index, gpointer data)
89 BonoboObject *registry = BONOBO_OBJECT (data);
90 Accessibility_Event e;
93 e.type = g_strdup ("object:children-changed::add");
94 e.source = BONOBO_OBJREF (desktop);
97 CORBA_exception_init (&ev);
98 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
100 CORBA_exception_free (&ev);
106 desktop_remove_application (SpiDesktop *desktop,
107 guint index, gpointer data)
109 BonoboObject *registry = BONOBO_OBJECT (data);
110 Accessibility_Event e;
111 CORBA_Environment ev;
113 e.type = g_strdup ("object:children-changed::remove");
114 e.source = BONOBO_OBJREF (desktop);
117 CORBA_exception_init (&ev);
118 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
120 CORBA_exception_free (&ev);
126 spi_registry_object_finalize (GObject *object)
128 printf ("spi_registry_object_finalize called\n");
130 /* TODO: unref deviceeventcontroller, which disconnects key listener */
131 G_OBJECT_CLASS (spi_registry_parent_class)->finalize (object);
135 _get_unique_id (void)
143 * registerApplication:
144 * @application: a reference to the requesting @Application
145 * return values: void
147 * Register a new application with the accessibility broker.
151 impl_accessibility_registry_register_application (PortableServer_Servant servant,
152 const Accessibility_Application application,
153 CORBA_Environment * ev)
155 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
158 fprintf (stderr, "registering app %p\n", application);
160 spi_desktop_add_application (registry->desktop, application);
162 Accessibility_Application__set_id (application, _get_unique_id (), ev);
165 * TODO: change the implementation below to a WM-aware one;
166 * e.g. don't add all apps to the SpiDesktop
170 #ifdef USE_A_HASH_IN_FUTURE
172 compare_corba_objects (gconstpointer p1, gconstpointer p2)
174 CORBA_Environment ev;
178 fprintf (stderr, "comparing %p to %p\n",
182 retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
188 register_with_toolkits (SpiRegistry *spi_registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
193 Accessibility_Desktop desktop;
194 Accessibility_Application app;
195 Accessibility_Registry registry;
196 registry = BONOBO_OBJREF (spi_registry_bonobo_object);
198 /* for each app in each desktop, call ...Application_registerToolkitEventListener */
200 n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
202 for (i=0; i<n_desktops; ++i)
204 desktop = Accessibility_Registry_getDesktop (registry, i, ev);
205 n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
206 for (j=0; j<n_apps; ++j)
208 app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
211 Accessibility_Application_registerToolkitEventListener (app,
213 CORBA_string_dup (etype->event_name),
219 #ifdef USE_A_HASH_IN_FUTURE
222 compare_listener_quarks (gconstpointer p1, gconstpointer p2)
224 return (((SpiListenerStruct *)p2)->event_type_quark !=
225 ((SpiListenerStruct *)p1)->event_type_quark);
229 compare_listener_corbaref (gconstpointer p1, gconstpointer p2)
231 return compare_corba_objects (((SpiListenerStruct *)p2)->listener,
232 ((SpiListenerStruct *)p1)->listener);
237 parse_event_type (EventTypeStruct *etype, const char *event_name)
239 gchar **split_string;
242 split_string = g_strsplit (event_name, ":", 4);
243 etype->event_name = g_strdup (event_name);
245 if (!g_ascii_strncasecmp (event_name, "focus:", 6))
247 etype->type_cat = ETYPE_FOCUS;
249 else if (!g_ascii_strncasecmp (event_name, "object:", 7))
251 etype->type_cat = ETYPE_OBJECT;
253 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
255 etype->type_cat = ETYPE_WINDOW;
259 etype->type_cat = ETYPE_TOOLKIT;
266 etype->minor = g_quark_from_string (s = g_strconcat (split_string[1], split_string[2], NULL));
270 etype->detail = g_quark_from_string (split_string[3]);
271 s = g_strconcat (split_string[1], split_string[2], split_string[3], NULL);
272 etype->major = g_quark_from_string (s);
277 etype->detail = g_quark_from_static_string ("");
278 s = g_strconcat (split_string[1], split_string[2], NULL);
279 etype->major = g_quark_from_string (s);
285 etype->major = g_quark_from_string (split_string[1]);
286 etype->minor = etype->major;
287 etype->detail = etype->major;
292 etype->major = g_quark_from_static_string ("");
293 etype->minor = etype->major;
294 etype->detail = etype->major;
297 g_strfreev (split_string);
301 * deregisterApplication:
302 * @application: a reference to the @Application
303 * to be deregistered.
304 * return values: void
306 * De-register an application previously registered with the broker.
310 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
311 const Accessibility_Application application,
312 CORBA_Environment * ev)
314 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
316 spi_desktop_remove_application (registry->desktop, application);
319 fprintf (stderr, "de-registered app %p\n", application);
324 get_listener_list (SpiRegistry *registry,
325 EventTypeCategory cat)
334 ret = ®istry->object_listeners;
337 ret = ®istry->window_listeners;
340 ret = ®istry->toolkit_listeners;
351 * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
354 impl_accessibility_registry_register_global_event_listener (
355 PortableServer_Servant servant,
356 Accessibility_EventListener listener,
357 const CORBA_char *event_name,
358 CORBA_Environment *ev)
360 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
361 SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
362 EventTypeStruct etype;
366 fprintf (stderr, "registering for events of type %s\n", event_name);
369 /* parse, check major event type and add listener accordingly */
370 parse_event_type (&etype, event_name);
371 ls->event_type_quark = etype.major;
372 ls->event_type_cat = etype.type_cat;
374 list = get_listener_list (registry, etype.type_cat);
378 *list = g_list_prepend (*list, ls);
380 if (etype.type_cat == ETYPE_TOOLKIT)
382 register_with_toolkits (registry, &etype, ev);
387 spi_listener_struct_free (ls, ev);
391 static SpiReEntrantContinue
392 remove_listener_cb (GList * const *list, gpointer user_data)
394 SpiListenerStruct *ls = (SpiListenerStruct *) (*list)->data;
395 CORBA_Environment ev;
396 Accessibility_EventListener listener = user_data;
398 CORBA_exception_init (&ev);
400 if (CORBA_Object_is_equivalent (ls->listener, listener, &ev))
402 spi_re_entrant_list_delete_link (list);
403 spi_listener_struct_free (ls, &ev);
406 CORBA_exception_free (&ev);
408 return SPI_RE_ENTRANT_CONTINUE;
412 * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
415 impl_accessibility_registry_deregister_global_event_listener_all (
416 PortableServer_Servant servant,
417 Accessibility_EventListener listener,
418 CORBA_Environment *ev)
422 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
424 lists[0] = ®istry->object_listeners;
425 lists[1] = ®istry->window_listeners;
426 lists[2] = ®istry->toolkit_listeners;
428 for (i = 0; i < sizeof (lists) / sizeof (lists[0]); i++)
430 spi_re_entrant_list_foreach (lists [i], remove_listener_cb, listener);
436 * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
439 impl_accessibility_registry_deregister_global_event_listener (
440 PortableServer_Servant servant,
441 Accessibility_EventListener listener,
442 const CORBA_char *event_name,
443 CORBA_Environment *ev)
445 SpiRegistry *registry;
446 EventTypeStruct etype;
448 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
450 parse_event_type (&etype, (char *) event_name);
452 spi_re_entrant_list_foreach (get_listener_list (registry, etype.type_cat),
453 remove_listener_cb, listener);
459 * return values: a short integer indicating the current number of
462 * Get the current number of desktops.
466 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
467 CORBA_Environment * ev)
469 /* TODO: implement support for multiple virtual desktops */
470 CORBA_short n_desktops;
471 n_desktops = (CORBA_short) 1;
478 * @n: the index of the requested @Desktop.
479 * return values: a reference to the requested @Desktop.
481 * Get the nth accessible desktop.
484 static Accessibility_Desktop
485 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
487 CORBA_Environment * ev)
489 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
491 /* TODO: implement support for multiple virtual desktops */
494 return (Accessibility_Desktop)
495 bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
499 return (Accessibility_Desktop) CORBA_OBJECT_NIL;
506 * return values: a sequence containing references to
509 * Get a list of accessible desktops.
512 static Accessibility_DesktopSeq *
513 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
514 CORBA_Environment * ev)
516 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
517 Accessibility_DesktopSeq *desktops;
519 desktops = Accessibility_DesktopSeq__alloc ();
520 desktops->_length = desktops->_maximum = 1;
521 desktops->_buffer = Accessibility_DesktopSeq_allocbuf (desktops->_length);
522 desktops->_buffer [0] = bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
528 static Accessibility_DeviceEventController
529 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
530 CORBA_Environment *ev)
532 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
534 if (!registry->de_controller)
536 registry->de_controller = spi_device_event_controller_new (registry);
539 return bonobo_object_dup_ref (BONOBO_OBJREF (registry->de_controller), ev);
543 CORBA_Environment *ev;
544 Bonobo_Unknown source;
545 EventTypeStruct etype;
546 Accessibility_Event e_out;
549 static SpiReEntrantContinue
550 notify_listeners_cb (GList * const *list, gpointer user_data)
552 SpiListenerStruct *ls;
553 NotifyContext *ctx = user_data;
560 #ifdef SPI_LISTENER_DEBUG
561 fprintf (stderr, "event quarks: %lx %lx %lx\n", ls->event_type_quark, etype.major, etype.minor);
562 fprintf (stderr, "event name: %s\n", etype.event_name);
565 if ((ls->event_type_quark == ctx->etype.major) ||
566 (ls->event_type_quark == ctx->etype.minor))
569 fprintf (stderr, "notifying listener %d\n", g_list_index (listeners, l->data));
570 s = Accessibility_Accessible__get_name (ctx->source, ev);
571 fprintf (stderr, "event source name %s\n", s);
575 ctx->e_out.source = bonobo_object_dup_ref (ctx->source, ctx->ev);
576 if (BONOBO_EX (ctx->ev))
578 return SPI_RE_ENTRANT_CONTINUE;;
581 if ((*list) && (*list)->data == ls)
583 Accessibility_EventListener_notifyEvent (
584 (Accessibility_EventListener) ls->listener, &ctx->e_out, ctx->ev);
585 if (ctx->ev->_major != CORBA_NO_EXCEPTION)
587 g_warning ("Accessibility app error: exception during "
588 "event notification: %s\n",
589 CORBA_exception_id (ctx->ev));
592 else /* dup re-entered */
594 bonobo_object_release_unref (ctx->e_out.source, ctx->ev);
598 return SPI_RE_ENTRANT_CONTINUE;
602 impl_registry_notify_event (PortableServer_Servant servant,
603 const Accessibility_Event *e,
604 CORBA_Environment *ev)
606 SpiRegistry *registry;
610 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
612 parse_event_type (&ctx.etype, e->type);
614 list = get_listener_list (registry, ctx.etype.type_cat);
620 ctx.source = e->source;
621 parse_event_type (&ctx.etype, e->type);
623 spi_re_entrant_list_foreach (list, notify_listeners_cb, &ctx);
626 if (e->source != CORBA_OBJECT_NIL)
628 Accessibility_Accessible_unref (e->source, ev);
634 spi_registry_class_init (SpiRegistryClass *klass)
636 GObjectClass * object_class = (GObjectClass *) klass;
637 POA_Accessibility_Registry__epv *epv = &klass->epv;
639 spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
641 object_class->finalize = spi_registry_object_finalize;
643 klass->parent_class.epv.notifyEvent = impl_registry_notify_event;
645 epv->registerApplication = impl_accessibility_registry_register_application;
646 epv->deregisterApplication = impl_accessibility_registry_deregister_application;
647 epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
648 epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
649 epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
650 epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
651 epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
652 epv->getDesktop = impl_accessibility_registry_get_desktop;
653 epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
657 spi_registry_init (SpiRegistry *registry)
659 registry->object_listeners = NULL;
660 registry->window_listeners = NULL;
661 registry->toolkit_listeners = NULL;
662 registry->desktop = spi_desktop_new ();
663 /* Register callback notification for application addition and removal */
664 g_signal_connect (G_OBJECT (registry->desktop),
666 G_CALLBACK (desktop_add_application),
669 g_signal_connect (G_OBJECT (registry->desktop),
670 "application_removed",
671 G_CALLBACK (desktop_remove_application),
674 registry->de_controller = NULL;
677 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
678 Accessibility_Registry,
683 spi_registry_new (void)
685 SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
686 bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);