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
33 #include <bonobo/bonobo-exception.h>
34 #include "../libspi/spi-private.h"
37 /* Our parent GObject type */
38 #define PARENT_TYPE SPI_LISTENER_TYPE
40 /* A pointer to our parent object class */
41 static SpiListenerClass *spi_registry_parent_class;
56 EventTypeCategory type_cat;
57 GQuark major; /* from string segment[1] */
58 GQuark minor; /* from string segment[1]+segment[2] */
59 GQuark detail; /* from string segment[3] (not concatenated) */
63 Accessibility_EventListener listener;
64 GQuark event_type_quark;
65 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);
85 desktop_add_application (SpiDesktop *desktop,
86 guint index, gpointer data)
88 BonoboObject *registry = BONOBO_OBJECT (data);
89 Accessibility_Event e;
92 e.type = g_strdup ("object:children-changed:add");
93 e.source = BONOBO_OBJREF (desktop);
96 CORBA_exception_init (&ev);
97 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
99 Accessibility_Desktop_unref (e.source, &ev);
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 Accessibility_Desktop_unref (e.source, &ev);
121 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, "mouse:", 6))
251 etype->type_cat = ETYPE_MOUSE;
253 else if (!g_ascii_strncasecmp (event_name, "object:", 7))
255 etype->type_cat = ETYPE_OBJECT;
257 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
259 etype->type_cat = ETYPE_WINDOW;
263 etype->type_cat = ETYPE_TOOLKIT;
268 etype->major = g_quark_from_string (split_string[1]);
271 etype->minor = g_quark_from_string (s = g_strconcat (split_string[1], split_string[2], NULL));
275 etype->detail = g_quark_from_string (split_string[3]);
279 etype->detail = g_quark_from_static_string ("");
284 etype->minor = etype->major;
285 etype->detail = g_quark_from_static_string (""); //etype->major;
290 etype->major = g_quark_from_static_string ("");
291 etype->minor = etype->major;
292 etype->detail = etype->major;
295 g_strfreev (split_string);
299 * deregisterApplication:
300 * @application: a reference to the @Application
301 * to be deregistered.
302 * return values: void
304 * De-register an application previously registered with the broker.
308 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
309 const Accessibility_Application application,
310 CORBA_Environment * ev)
312 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
314 spi_desktop_remove_application (registry->desktop, application);
317 fprintf (stderr, "de-registered app %p\n", application);
322 get_listener_list (SpiRegistry *registry,
323 EventTypeCategory cat)
332 ret = ®istry->object_listeners;
335 ret = ®istry->window_listeners;
339 ret = ®istry->toolkit_listeners;
350 * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
353 impl_accessibility_registry_register_global_event_listener (
354 PortableServer_Servant servant,
355 Accessibility_EventListener listener,
356 const CORBA_char *event_name,
357 CORBA_Environment *ev)
359 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
360 SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
361 EventTypeStruct etype;
364 #ifdef SPI_LISTENER_DEBUG
365 fprintf (stderr, "registering");
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.minor;
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, ctx->etype.major, ctx->etype.minor);
562 fprintf (stderr, "event name: %s\n", ctx->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", 0);
570 /* g_list_index (list, l->data)); */
571 s = Accessibility_Accessible__get_name (ctx->source, ctx->ev);
572 fprintf (stderr, "event source name %s\n", s);
576 ctx->e_out.source = CORBA_Object_duplicate (ctx->source, ctx->ev);
578 if (BONOBO_EX (ctx->ev))
580 return SPI_RE_ENTRANT_CONTINUE;;
583 if ((*list) && (*list)->data == ls)
585 Accessibility_EventListener_notifyEvent (
586 (Accessibility_EventListener) ls->listener, &ctx->e_out, ctx->ev);
587 if (ctx->ev->_major != CORBA_NO_EXCEPTION)
589 g_warning ("Accessibility app error: exception during "
590 "event notification: %s\n",
591 CORBA_exception_id (ctx->ev));
592 if (ctx->ev->_major == CORBA_SYSTEM_EXCEPTION)
593 CORBA_exception_init (ctx->ev);
594 /* clear system exception on notify, it means listener is dead but
595 * that's no concern of the event source :-) */
598 else /* dup re-entered */
600 CORBA_Object_release (ctx->e_out.source, ctx->ev);
604 return SPI_RE_ENTRANT_CONTINUE;
608 impl_registry_notify_event (PortableServer_Servant servant,
609 const Accessibility_Event *e,
610 CORBA_Environment *ev)
612 SpiRegistry *registry;
616 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
618 parse_event_type (&ctx.etype, e->type);
620 list = get_listener_list (registry, ctx.etype.type_cat);
626 ctx.source = e->source;
627 parse_event_type (&ctx.etype, e->type);
629 spi_re_entrant_list_foreach (list, notify_listeners_cb, &ctx);
636 spi_registry_class_init (SpiRegistryClass *klass)
638 GObjectClass * object_class = (GObjectClass *) klass;
639 POA_Accessibility_Registry__epv *epv = &klass->epv;
641 spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
643 object_class->finalize = spi_registry_object_finalize;
645 klass->parent_class.epv.notifyEvent = impl_registry_notify_event;
647 epv->registerApplication = impl_accessibility_registry_register_application;
648 epv->deregisterApplication = impl_accessibility_registry_deregister_application;
649 epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
650 epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
651 epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
652 epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
653 epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
654 epv->getDesktop = impl_accessibility_registry_get_desktop;
655 epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
659 spi_registry_init (SpiRegistry *registry)
661 registry->object_listeners = NULL;
662 registry->window_listeners = NULL;
663 registry->toolkit_listeners = NULL;
664 registry->desktop = spi_desktop_new ();
665 /* Register callback notification for application addition and removal */
666 g_signal_connect (G_OBJECT (registry->desktop),
668 G_CALLBACK (desktop_add_application),
671 g_signal_connect (G_OBJECT (registry->desktop),
672 "application_removed",
673 G_CALLBACK (desktop_remove_application),
676 registry->de_controller = spi_device_event_controller_new (registry);
679 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
680 Accessibility_Registry,
685 spi_registry_new (void)
687 SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
688 bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);