2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002 Sun Microsystems Inc.,
6 * Copyright 2001, 2002 Ximian, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 /* registry.c: the main accessibility service registry implementation */
26 #undef SPI_LISTENER_DEBUG
34 #include <bonobo/bonobo-exception.h>
35 #include "../libspi/spi-private.h"
38 /* Our parent GObject type */
39 #define PARENT_TYPE SPI_LISTENER_TYPE
41 /* A pointer to our parent object class */
42 static SpiListenerClass *spi_registry_parent_class;
57 EventTypeCategory type_cat;
58 GQuark major; /* from string segment[1] */
59 GQuark minor; /* from string segment[1]+segment[2] */
60 GQuark detail; /* from string segment[3] (not concatenated) */
64 Accessibility_EventListener listener;
65 GQuark event_type_quark;
66 EventTypeCategory event_type_cat;
70 spi_listener_struct_new (Accessibility_EventListener listener, CORBA_Environment *ev)
72 SpiListenerStruct *retval = g_malloc (sizeof (SpiListenerStruct));
73 retval->listener = bonobo_object_dup_ref (listener, ev);
79 spi_listener_struct_free (SpiListenerStruct *ls, CORBA_Environment *ev)
81 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 Accessibility_Desktop_unref (e.source, &ev);
101 CORBA_exception_free (&ev);
107 desktop_remove_application (SpiDesktop *desktop,
108 guint index, gpointer data)
110 BonoboObject *registry = BONOBO_OBJECT (data);
111 Accessibility_Event e;
112 CORBA_Environment ev;
114 e.type = g_strdup ("object:children-changed:remove");
115 e.source = BONOBO_OBJREF (desktop);
118 CORBA_exception_init (&ev);
119 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
121 Accessibility_Desktop_unref (e.source, &ev);
122 CORBA_exception_free (&ev);
127 spi_registry_object_finalize (GObject *object)
129 printf ("spi_registry_object_finalize called\n");
131 /* TODO: unref deviceeventcontroller, which disconnects key listener */
132 G_OBJECT_CLASS (spi_registry_parent_class)->finalize (object);
136 _get_unique_id (void)
144 * registerApplication:
145 * @application: a reference to the requesting @Application
146 * return values: void
148 * Register a new application with the accessibility broker.
152 impl_accessibility_registry_register_application (PortableServer_Servant servant,
153 const Accessibility_Application application,
154 CORBA_Environment * ev)
156 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
159 fprintf (stderr, "registering app %p\n", application);
161 spi_desktop_add_application (registry->desktop, application);
163 Accessibility_Application__set_id (application, _get_unique_id (), ev);
166 * TODO: change the implementation below to a WM-aware one;
167 * e.g. don't add all apps to the SpiDesktop
171 #ifdef USE_A_HASH_IN_FUTURE
173 compare_corba_objects (gconstpointer p1, gconstpointer p2)
175 CORBA_Environment ev;
179 fprintf (stderr, "comparing %p to %p\n",
183 retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
189 register_with_toolkits (SpiRegistry *spi_registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
194 Accessibility_Desktop desktop;
195 Accessibility_Application app;
196 Accessibility_Registry registry;
197 registry = BONOBO_OBJREF (spi_registry_bonobo_object);
199 /* for each app in each desktop, call ...Application_registerToolkitEventListener */
201 n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
203 for (i=0; i<n_desktops; ++i)
205 desktop = Accessibility_Registry_getDesktop (registry, i, ev);
206 n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
207 for (j=0; j<n_apps; ++j)
209 app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
212 Accessibility_Application_registerToolkitEventListener (app,
214 CORBA_string_dup (etype->event_name),
220 #ifdef USE_A_HASH_IN_FUTURE
223 compare_listener_quarks (gconstpointer p1, gconstpointer p2)
225 return (((SpiListenerStruct *)p2)->event_type_quark !=
226 ((SpiListenerStruct *)p1)->event_type_quark);
230 compare_listener_corbaref (gconstpointer p1, gconstpointer p2)
232 return compare_corba_objects (((SpiListenerStruct *)p2)->listener,
233 ((SpiListenerStruct *)p1)->listener);
238 parse_event_type (EventTypeStruct *etype, const char *event_name)
240 gchar **split_string;
243 split_string = g_strsplit (event_name, ":", 4);
244 etype->event_name = g_strdup (event_name);
246 if (!g_ascii_strncasecmp (event_name, "focus:", 6))
248 etype->type_cat = ETYPE_FOCUS;
250 else if (!g_ascii_strncasecmp (event_name, "mouse:", 6))
252 etype->type_cat = ETYPE_MOUSE;
254 else if (!g_ascii_strncasecmp (event_name, "object:", 7))
256 etype->type_cat = ETYPE_OBJECT;
258 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
260 etype->type_cat = ETYPE_WINDOW;
264 etype->type_cat = ETYPE_TOOLKIT;
269 etype->major = g_quark_from_string (split_string[1]);
272 etype->minor = g_quark_from_string (s = g_strconcat (split_string[1], split_string[2], NULL));
276 etype->detail = g_quark_from_string (split_string[3]);
280 etype->detail = g_quark_from_static_string ("");
285 etype->minor = etype->major;
286 etype->detail = g_quark_from_static_string (""); //etype->major;
291 etype->major = g_quark_from_static_string ("");
292 etype->minor = etype->major;
293 etype->detail = etype->major;
296 g_strfreev (split_string);
300 * deregisterApplication:
301 * @application: a reference to the @Application
302 * to be deregistered.
303 * return values: void
305 * De-register an application previously registered with the broker.
309 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
310 const Accessibility_Application application,
311 CORBA_Environment * ev)
313 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
315 spi_desktop_remove_application (registry->desktop, application);
318 fprintf (stderr, "de-registered app %p\n", application);
323 get_listener_list (SpiRegistry *registry,
324 EventTypeCategory cat)
333 ret = ®istry->object_listeners;
336 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;
365 #ifdef SPI_LISTENER_DEBUG
366 fprintf (stderr, "registering");
367 fprintf (stderr, "registering for events of type %s\n", event_name);
370 /* parse, check major event type and add listener accordingly */
371 parse_event_type (&etype, event_name);
372 ls->event_type_quark = etype.minor;
373 ls->event_type_cat = etype.type_cat;
375 list = get_listener_list (registry, etype.type_cat);
379 *list = g_list_prepend (*list, ls);
381 if (etype.type_cat == ETYPE_TOOLKIT)
383 register_with_toolkits (registry, &etype, ev);
388 spi_listener_struct_free (ls, ev);
392 static SpiReEntrantContinue
393 remove_listener_cb (GList * const *list, gpointer user_data)
395 SpiListenerStruct *ls = (SpiListenerStruct *) (*list)->data;
396 CORBA_Environment ev;
397 Accessibility_EventListener listener = user_data;
399 CORBA_exception_init (&ev);
401 if (CORBA_Object_is_equivalent (ls->listener, listener, &ev))
403 spi_re_entrant_list_delete_link (list);
404 spi_listener_struct_free (ls, &ev);
407 CORBA_exception_free (&ev);
409 return SPI_RE_ENTRANT_CONTINUE;
413 * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
416 impl_accessibility_registry_deregister_global_event_listener_all (
417 PortableServer_Servant servant,
418 Accessibility_EventListener listener,
419 CORBA_Environment *ev)
423 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
425 lists[0] = ®istry->object_listeners;
426 lists[1] = ®istry->window_listeners;
427 lists[2] = ®istry->toolkit_listeners;
429 for (i = 0; i < sizeof (lists) / sizeof (lists[0]); i++)
431 spi_re_entrant_list_foreach (lists [i], remove_listener_cb, listener);
437 * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
440 impl_accessibility_registry_deregister_global_event_listener (
441 PortableServer_Servant servant,
442 Accessibility_EventListener listener,
443 const CORBA_char *event_name,
444 CORBA_Environment *ev)
446 SpiRegistry *registry;
447 EventTypeStruct etype;
449 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
451 parse_event_type (&etype, (char *) event_name);
453 spi_re_entrant_list_foreach (get_listener_list (registry, etype.type_cat),
454 remove_listener_cb, listener);
460 * return values: a short integer indicating the current number of
463 * Get the current number of desktops.
467 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
468 CORBA_Environment * ev)
470 /* TODO: implement support for multiple virtual desktops */
471 CORBA_short n_desktops;
472 n_desktops = (CORBA_short) 1;
479 * @n: the index of the requested @Desktop.
480 * return values: a reference to the requested @Desktop.
482 * Get the nth accessible desktop.
485 static Accessibility_Desktop
486 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
488 CORBA_Environment * ev)
490 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
492 /* TODO: implement support for multiple virtual desktops */
495 return (Accessibility_Desktop)
496 bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
500 return (Accessibility_Desktop) CORBA_OBJECT_NIL;
507 * return values: a sequence containing references to
510 * Get a list of accessible desktops.
513 static Accessibility_DesktopSeq *
514 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
515 CORBA_Environment * ev)
517 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
518 Accessibility_DesktopSeq *desktops;
520 desktops = Accessibility_DesktopSeq__alloc ();
521 desktops->_length = desktops->_maximum = 1;
522 desktops->_buffer = Accessibility_DesktopSeq_allocbuf (desktops->_length);
523 desktops->_buffer [0] = bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
529 static Accessibility_DeviceEventController
530 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
531 CORBA_Environment *ev)
533 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
535 if (!registry->de_controller)
537 registry->de_controller = spi_device_event_controller_new (registry);
540 return bonobo_object_dup_ref (BONOBO_OBJREF (registry->de_controller), ev);
544 CORBA_Environment *ev;
545 Bonobo_Unknown source;
546 EventTypeStruct etype;
547 Accessibility_Event e_out;
550 static SpiReEntrantContinue
551 notify_listeners_cb (GList * const *list, gpointer user_data)
553 SpiListenerStruct *ls;
554 NotifyContext *ctx = user_data;
561 #ifdef SPI_LISTENER_DEBUG
562 fprintf (stderr, "event quarks: %lx %lx %lx\n", ls->event_type_quark, ctx->etype.major, ctx->etype.minor);
563 fprintf (stderr, "event name: %s\n", ctx->etype.event_name);
566 if ((ls->event_type_quark == ctx->etype.major) ||
567 (ls->event_type_quark == ctx->etype.minor))
570 fprintf (stderr, "notifying listener %d\n", 0);
571 /* g_list_index (list, l->data)); */
572 s = Accessibility_Accessible__get_name (ctx->source, ctx->ev);
573 fprintf (stderr, "event source name %s\n", s);
577 ctx->e_out.source = CORBA_Object_duplicate (ctx->source, ctx->ev);
579 if (BONOBO_EX (ctx->ev))
581 return SPI_RE_ENTRANT_CONTINUE;;
584 if ((*list) && (*list)->data == ls)
586 Accessibility_EventListener_notifyEvent (
587 (Accessibility_EventListener) ls->listener, &ctx->e_out, ctx->ev);
588 if (ctx->ev->_major != CORBA_NO_EXCEPTION)
590 g_warning ("Accessibility app error: exception during "
591 "event notification: %s\n",
592 CORBA_exception_id (ctx->ev));
593 if (ctx->ev->_major == CORBA_SYSTEM_EXCEPTION)
594 CORBA_exception_init (ctx->ev);
595 /* clear system exception on notify, it means listener is dead but
596 * that's no concern of the event source :-) */
599 else /* dup re-entered */
601 CORBA_Object_release (ctx->e_out.source, ctx->ev);
605 return SPI_RE_ENTRANT_CONTINUE;
609 impl_registry_notify_event (PortableServer_Servant servant,
610 const Accessibility_Event *e,
611 CORBA_Environment *ev)
613 SpiRegistry *registry;
617 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
619 parse_event_type (&ctx.etype, e->type);
621 list = get_listener_list (registry, ctx.etype.type_cat);
627 ctx.source = e->source;
628 parse_event_type (&ctx.etype, e->type);
630 spi_re_entrant_list_foreach (list, notify_listeners_cb, &ctx);
637 spi_registry_class_init (SpiRegistryClass *klass)
639 GObjectClass * object_class = (GObjectClass *) klass;
640 POA_Accessibility_Registry__epv *epv = &klass->epv;
642 spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
644 object_class->finalize = spi_registry_object_finalize;
646 klass->parent_class.epv.notifyEvent = impl_registry_notify_event;
648 epv->registerApplication = impl_accessibility_registry_register_application;
649 epv->deregisterApplication = impl_accessibility_registry_deregister_application;
650 epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
651 epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
652 epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
653 epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
654 epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
655 epv->getDesktop = impl_accessibility_registry_get_desktop;
656 epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
660 spi_registry_init (SpiRegistry *registry)
662 registry->object_listeners = NULL;
663 registry->window_listeners = NULL;
664 registry->toolkit_listeners = NULL;
665 registry->desktop = spi_desktop_new ();
666 /* Register callback notification for application addition and removal */
667 g_signal_connect (G_OBJECT (registry->desktop),
669 G_CALLBACK (desktop_add_application),
672 g_signal_connect (G_OBJECT (registry->desktop),
673 "application_removed",
674 G_CALLBACK (desktop_remove_application),
677 registry->de_controller = spi_device_event_controller_new (registry);
680 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
681 Accessibility_Registry,
686 spi_registry_new (void)
688 SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
689 bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);