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;
56 const char *event_name;
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;
92 Accessibility_Accessible a;
94 CORBA_exception_init (&ev);
95 e.type = "object:children-changed:add";
96 e.source = BONOBO_OBJREF (desktop);
99 a = Accessibility_Accessible_getChildAtIndex (BONOBO_OBJREF (desktop),
102 spi_init_any_object (&e.any_data, a);
104 spi_init_any_nil (&e.any_data);
105 Accessibility_Accessible_unref (a, &ev);
106 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
108 Accessibility_Desktop_unref (e.source, &ev);
109 CORBA_exception_free (&ev);
115 desktop_remove_application (SpiDesktop *desktop,
116 guint index, gpointer data)
118 BonoboObject *registry = BONOBO_OBJECT (data);
119 Accessibility_Event e;
120 Accessibility_Accessible a;
121 CORBA_Environment ev;
123 CORBA_exception_init (&ev);
125 e.type = "object:children-changed:remove";
126 e.source = BONOBO_OBJREF (desktop);
129 a = Accessibility_Accessible_getChildAtIndex (BONOBO_OBJREF (desktop),
132 spi_init_any_object (&e.any_data, a);
134 spi_init_any_nil (&e.any_data);
135 Accessibility_Accessible_unref (a, &ev);
136 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
138 Accessibility_Desktop_unref (e.source, &ev);
139 CORBA_exception_free (&ev);
144 spi_registry_object_finalize (GObject *object)
146 fprintf (stderr, "spi_registry_object_finalize called\n");
148 /* TODO: unref deviceeventcontroller, which disconnects key listener */
149 G_OBJECT_CLASS (spi_registry_parent_class)->finalize (object);
153 _get_unique_id (void)
161 * registerApplication:
162 * @application: a reference to the requesting @Application
163 * return values: void
165 * Register a new application with the accessibility broker.
169 impl_accessibility_registry_register_application (PortableServer_Servant servant,
170 const Accessibility_Application application,
171 CORBA_Environment * ev)
173 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
176 fprintf (stderr, "registering app %p\n", application);
178 spi_desktop_add_application (registry->desktop, application);
180 Accessibility_Application__set_id (application, _get_unique_id (), ev);
183 * TODO: change the implementation below to a WM-aware one;
184 * e.g. don't add all apps to the SpiDesktop
188 #ifdef USE_A_HASH_IN_FUTURE
190 compare_corba_objects (gconstpointer p1, gconstpointer p2)
192 CORBA_Environment ev;
196 fprintf (stderr, "comparing %p to %p\n",
200 retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
206 register_with_toolkits (SpiRegistry *spi_registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
211 Accessibility_Desktop desktop;
212 Accessibility_Application app;
213 Accessibility_Registry registry;
214 registry = BONOBO_OBJREF (spi_registry_bonobo_object);
216 /* for each app in each desktop, call ...Application_registerToolkitEventListener */
218 n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
220 for (i=0; i<n_desktops; ++i)
222 desktop = Accessibility_Registry_getDesktop (registry, i, ev);
223 n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
224 for (j=0; j<n_apps; ++j)
226 app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
229 Accessibility_Application_registerToolkitEventListener (app,
231 CORBA_string_dup (etype->event_name),
237 #ifdef USE_A_HASH_IN_FUTURE
240 compare_listener_quarks (gconstpointer p1, gconstpointer p2)
242 return (((SpiListenerStruct *)p2)->event_type_quark !=
243 ((SpiListenerStruct *)p1)->event_type_quark);
247 compare_listener_corbaref (gconstpointer p1, gconstpointer p2)
249 return compare_corba_objects (((SpiListenerStruct *)p2)->listener,
250 ((SpiListenerStruct *)p1)->listener);
255 parse_event_type (EventTypeStruct *etype, const char *event_name)
257 gchar **split_string;
260 split_string = g_strsplit (event_name, ":", 4);
261 etype->event_name = event_name;
263 if (!g_ascii_strncasecmp (event_name, "focus:", 6))
265 etype->type_cat = ETYPE_FOCUS;
267 else if (!g_ascii_strncasecmp (event_name, "mouse:", 6))
269 etype->type_cat = ETYPE_MOUSE;
271 else if (!g_ascii_strncasecmp (event_name, "object:", 7))
273 etype->type_cat = ETYPE_OBJECT;
275 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
277 etype->type_cat = ETYPE_WINDOW;
279 else if (!g_ascii_strncasecmp (event_name, "keyboard:", 9))
281 etype->type_cat = ETYPE_KEYBOARD;
285 etype->type_cat = ETYPE_TOOLKIT;
290 etype->major = g_quark_from_string (split_string[1]);
293 etype->minor = g_quark_from_string (s = g_strconcat (split_string[1], split_string[2], NULL));
297 etype->detail = g_quark_from_string (split_string[3]);
301 etype->detail = g_quark_from_static_string ("");
306 etype->minor = etype->major;
307 etype->detail = g_quark_from_static_string (""); //etype->major;
312 etype->major = g_quark_from_static_string ("");
313 etype->minor = etype->major;
314 etype->detail = etype->major;
317 g_strfreev (split_string);
321 * deregisterApplication:
322 * @application: a reference to the @Application
323 * to be deregistered.
324 * return values: void
326 * De-register an application previously registered with the broker.
330 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
331 const Accessibility_Application application,
332 CORBA_Environment * ev)
334 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
336 spi_desktop_remove_application (registry->desktop, application);
339 fprintf (stderr, "de-registered app %p\n", application);
344 get_listener_list (SpiRegistry *registry,
345 EventTypeCategory cat)
354 ret = ®istry->object_listeners;
357 ret = ®istry->window_listeners;
361 ret = ®istry->toolkit_listeners;
372 * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
375 impl_accessibility_registry_register_global_event_listener (
376 PortableServer_Servant servant,
377 Accessibility_EventListener listener,
378 const CORBA_char *event_name,
379 CORBA_Environment *ev)
381 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
382 SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
383 EventTypeStruct etype;
386 #ifdef SPI_LISTENER_DEBUG
387 fprintf (stderr, "registering for events of type %s\n", event_name);
390 /* parse, check major event type and add listener accordingly */
391 parse_event_type (&etype, event_name);
392 ls->event_type_quark = etype.minor;
393 ls->event_type_cat = etype.type_cat;
395 list = get_listener_list (registry, etype.type_cat);
399 *list = g_list_prepend (*list, ls);
401 if (etype.type_cat == ETYPE_TOOLKIT)
403 register_with_toolkits (registry, &etype, ev);
408 spi_listener_struct_free (ls, ev);
412 static SpiReEntrantContinue
413 remove_listener_cb (GList * const *list, gpointer user_data)
415 SpiListenerStruct *ls = (SpiListenerStruct *) (*list)->data;
416 CORBA_Environment ev;
417 Accessibility_EventListener listener = user_data;
419 CORBA_exception_init (&ev);
421 if (CORBA_Object_is_equivalent (ls->listener, listener, &ev))
423 spi_re_entrant_list_delete_link (list);
424 spi_listener_struct_free (ls, &ev);
427 CORBA_exception_free (&ev);
429 return SPI_RE_ENTRANT_CONTINUE;
433 * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
436 impl_accessibility_registry_deregister_global_event_listener_all (
437 PortableServer_Servant servant,
438 Accessibility_EventListener listener,
439 CORBA_Environment *ev)
443 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
445 lists[0] = ®istry->object_listeners;
446 lists[1] = ®istry->window_listeners;
447 lists[2] = ®istry->toolkit_listeners;
449 for (i = 0; i < sizeof (lists) / sizeof (lists[0]); i++)
451 spi_re_entrant_list_foreach (lists [i], remove_listener_cb, listener);
457 * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
460 impl_accessibility_registry_deregister_global_event_listener (
461 PortableServer_Servant servant,
462 Accessibility_EventListener listener,
463 const CORBA_char *event_name,
464 CORBA_Environment *ev)
466 SpiRegistry *registry;
467 EventTypeStruct etype;
469 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
471 parse_event_type (&etype, (char *) event_name);
473 spi_re_entrant_list_foreach (get_listener_list (registry, etype.type_cat),
474 remove_listener_cb, listener);
480 * return values: a short integer indicating the current number of
483 * Get the current number of desktops.
487 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
488 CORBA_Environment * ev)
490 /* TODO: implement support for multiple virtual desktops */
491 CORBA_short n_desktops;
492 n_desktops = (CORBA_short) 1;
499 * @n: the index of the requested @Desktop.
500 * return values: a reference to the requested @Desktop.
502 * Get the nth accessible desktop.
505 static Accessibility_Desktop
506 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
508 CORBA_Environment * ev)
510 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
512 /* TODO: implement support for multiple virtual desktops */
515 return (Accessibility_Desktop)
516 bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
520 return (Accessibility_Desktop) CORBA_OBJECT_NIL;
527 * return values: a sequence containing references to
530 * Get a list of accessible desktops.
533 static Accessibility_DesktopSeq *
534 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
535 CORBA_Environment * ev)
537 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
538 Accessibility_DesktopSeq *desktops;
540 desktops = Accessibility_DesktopSeq__alloc ();
541 desktops->_length = desktops->_maximum = 1;
542 desktops->_buffer = Accessibility_DesktopSeq_allocbuf (desktops->_length);
543 desktops->_buffer [0] = bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
549 static Accessibility_DeviceEventController
550 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
551 CORBA_Environment *ev)
553 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
555 if (!registry->de_controller)
557 registry->de_controller = spi_device_event_controller_new (registry);
560 return bonobo_object_dup_ref (BONOBO_OBJREF (registry->de_controller), ev);
564 CORBA_Environment *ev;
565 Bonobo_Unknown source;
566 EventTypeStruct etype;
567 Accessibility_Event e_out;
570 static SpiReEntrantContinue
571 notify_listeners_cb (GList * const *list, gpointer user_data)
573 SpiListenerStruct *ls;
574 NotifyContext *ctx = user_data;
581 #ifdef SPI_LISTENER_DEBUG
582 fprintf (stderr, "event quarks: %lx %lx %lx\n", ls->event_type_quark, ctx->etype.major, ctx->etype.minor);
583 fprintf (stderr, "event name: %s\n", ctx->etype.event_name);
586 if ((ls->event_type_quark == ctx->etype.major) ||
587 (ls->event_type_quark == ctx->etype.minor))
590 fprintf (stderr, "notifying listener %d\n", 0);
591 /* g_list_index (list, l->data)); */
592 s = Accessibility_Accessible__get_name (ctx->source, ctx->ev);
593 fprintf (stderr, "event source name %s\n", s);
597 ctx->e_out.source = CORBA_Object_duplicate (ctx->source, ctx->ev);
598 if (BONOBO_EX (ctx->ev))
600 return SPI_RE_ENTRANT_CONTINUE;
603 if ((*list) && (*list)->data == ls)
605 Accessibility_EventListener_notifyEvent (
606 (Accessibility_EventListener) ls->listener, &ctx->e_out, ctx->ev);
607 if (ctx->ev->_major != CORBA_NO_EXCEPTION)
609 g_warning ("Accessibility app error: exception during "
610 "event notification: %s\n",
611 CORBA_exception_id (ctx->ev));
612 if (ctx->ev->_major == CORBA_SYSTEM_EXCEPTION)
613 CORBA_exception_init (ctx->ev);
614 /* clear system exception on notify, it means listener is dead but
615 * that's no concern of the event source :-) */
618 else /* dup re-entered */
620 CORBA_Object_release (ctx->e_out.source, ctx->ev);
624 return SPI_RE_ENTRANT_CONTINUE;
628 impl_registry_notify_event (PortableServer_Servant servant,
629 const Accessibility_Event *e,
630 CORBA_Environment *ev)
632 SpiRegistry *registry;
636 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
638 parse_event_type (&ctx.etype, e->type);
640 list = get_listener_list (registry, ctx.etype.type_cat);
646 CORBA_any__copy (&ctx.e_out.any_data, &e->any_data);
647 ctx.source = e->source;
648 spi_re_entrant_list_foreach (list, notify_listeners_cb, &ctx);
653 spi_registry_class_init (SpiRegistryClass *klass)
655 GObjectClass * object_class = (GObjectClass *) klass;
656 POA_Accessibility_Registry__epv *epv = &klass->epv;
658 spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
660 object_class->finalize = spi_registry_object_finalize;
662 klass->parent_class.epv.notifyEvent = impl_registry_notify_event;
664 epv->registerApplication = impl_accessibility_registry_register_application;
665 epv->deregisterApplication = impl_accessibility_registry_deregister_application;
666 epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
667 epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
668 epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
669 epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
670 epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
671 epv->getDesktop = impl_accessibility_registry_get_desktop;
672 epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
676 spi_registry_init (SpiRegistry *registry)
678 fprintf (stderr, "REGISTRY INITIALIZED: toolkit list = %p\n",
679 ®istry->toolkit_listeners);
680 registry->object_listeners = NULL;
681 registry->window_listeners = NULL;
682 registry->toolkit_listeners = NULL;
683 registry->desktop = spi_desktop_new ();
684 /* Register callback notification for application addition and removal */
685 g_signal_connect (G_OBJECT (registry->desktop),
687 G_CALLBACK (desktop_add_application),
690 g_signal_connect (G_OBJECT (registry->desktop),
691 "application_removed",
692 G_CALLBACK (desktop_remove_application),
695 registry->de_controller = spi_device_event_controller_new (registry);
698 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
699 Accessibility_Registry,
704 spi_registry_new (void)
706 SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
707 bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);