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
28 #undef SPI_QUEUE_DEBUG
35 #include <bonobo/bonobo-exception.h>
36 #include "../libspi/spi-private.h"
39 /* Our parent GObject type */
40 #define PARENT_TYPE SPI_LISTENER_TYPE
42 /* A pointer to our parent object class */
43 static SpiListenerClass *spi_registry_parent_class;
45 static GQuark _deactivate_quark = 0;
46 static GQuark _activate_quark = 0;
47 static GQuark _state_quark = 0;
63 const char *event_name;
64 EventTypeCategory type_cat;
65 GQuark major; /* from string segment[1] */
66 GQuark minor; /* from string segment[1]+segment[2] */
67 GQuark detail; /* from string segment[3] (not concatenated) */
71 Accessibility_EventListener listener;
72 GQuark event_type_quark;
73 EventTypeCategory event_type_cat;
77 spi_registry_set_debug (const char *debug_flag_string)
79 if (debug_flag_string)
80 _dbg = (int) g_ascii_strtod (debug_flag_string, NULL);
84 spi_listener_struct_new (Accessibility_EventListener listener, CORBA_Environment *ev)
86 SpiListenerStruct *retval = g_malloc (sizeof (SpiListenerStruct));
87 retval->listener = bonobo_object_dup_ref (listener, ev);
93 spi_listener_struct_free (SpiListenerStruct *ls, CORBA_Environment *ev)
95 bonobo_object_release_unref (ls->listener, ev);
100 desktop_add_application (SpiDesktop *desktop,
101 guint index, gpointer data)
103 BonoboObject *registry = BONOBO_OBJECT (data);
104 Accessibility_Event e;
105 CORBA_Environment ev;
106 Accessibility_Accessible a;
108 CORBA_exception_init (&ev);
109 e.type = "object:children-changed:add";
110 e.source = BONOBO_OBJREF (desktop);
113 a = Accessibility_Accessible_getChildAtIndex (BONOBO_OBJREF (desktop),
116 spi_init_any_object (&e.any_data, a);
118 spi_init_any_nil (&e.any_data);
119 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
121 bonobo_object_release_unref (a, &ev);
122 CORBA_exception_free (&ev);
128 desktop_remove_application (SpiDesktop *desktop,
129 guint index, gpointer data)
131 BonoboObject *registry = BONOBO_OBJECT (data);
132 Accessibility_Event e;
133 Accessibility_Accessible a;
134 CORBA_Environment ev;
136 CORBA_exception_init (&ev);
138 e.type = "object:children-changed:remove";
139 e.source = BONOBO_OBJREF (desktop);
142 a = Accessibility_Accessible_getChildAtIndex (BONOBO_OBJREF (desktop),
145 spi_init_any_object (&e.any_data, a);
147 spi_init_any_nil (&e.any_data);
148 Accessibility_Accessible_unref (a, &ev);
149 Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
151 Accessibility_Desktop_unref (e.source, &ev);
152 CORBA_exception_free (&ev);
157 spi_registry_object_finalize (GObject *object)
159 DBG (1, g_warning ("spi_registry_object_finalize called\n"));
160 g_object_unref (SPI_REGISTRY (object)->de_controller);
162 G_OBJECT_CLASS (spi_registry_parent_class)->finalize (object);
166 _get_unique_id (void)
174 * registerApplication:
175 * @application: a reference to the requesting @Application
176 * return values: void
178 * Register a new application with the accessibility broker.
182 impl_accessibility_registry_register_application (PortableServer_Servant servant,
183 const Accessibility_Application application,
184 CORBA_Environment * ev)
186 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
189 fprintf (stderr, "registering app %p\n", application);
191 spi_desktop_add_application (registry->desktop, application);
193 Accessibility_Application__set_id (application, _get_unique_id (), ev);
196 * TODO: change the implementation below to a WM-aware one;
197 * e.g. don't add all apps to the SpiDesktop
201 #ifdef USE_A_HASH_IN_FUTURE
203 compare_corba_objects (gconstpointer p1, gconstpointer p2)
205 CORBA_Environment ev;
209 fprintf (stderr, "comparing %p to %p\n",
213 retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
219 register_with_toolkits (SpiRegistry *spi_registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
224 Accessibility_Desktop desktop;
225 Accessibility_Application app;
226 Accessibility_Registry registry;
227 registry = BONOBO_OBJREF (spi_registry_bonobo_object);
229 /* for each app in each desktop, call ...Application_registerToolkitEventListener */
231 n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
233 for (i=0; i<n_desktops; ++i)
235 desktop = Accessibility_Registry_getDesktop (registry, i, ev);
236 n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
237 for (j=0; j<n_apps; ++j)
239 app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
242 Accessibility_Application_registerToolkitEventListener (app,
244 CORBA_string_dup (etype->event_name),
250 #ifdef USE_A_HASH_IN_FUTURE
253 compare_listener_quarks (gconstpointer p1, gconstpointer p2)
255 return (((SpiListenerStruct *)p2)->event_type_quark !=
256 ((SpiListenerStruct *)p1)->event_type_quark);
260 compare_listener_corbaref (gconstpointer p1, gconstpointer p2)
262 return compare_corba_objects (((SpiListenerStruct *)p2)->listener,
263 ((SpiListenerStruct *)p1)->listener);
268 parse_event_type (EventTypeStruct *etype, const char *event_name)
270 gchar **split_string;
273 split_string = g_strsplit (event_name, ":", 4);
274 etype->event_name = event_name;
276 if (!g_ascii_strncasecmp (event_name, "focus:", 6))
278 etype->type_cat = ETYPE_FOCUS;
280 else if (!g_ascii_strncasecmp (event_name, "mouse:", 6))
282 etype->type_cat = ETYPE_MOUSE;
284 else if (!g_ascii_strncasecmp (event_name, "object:", 7))
286 etype->type_cat = ETYPE_OBJECT;
288 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
290 etype->type_cat = ETYPE_WINDOW;
292 else if (!g_ascii_strncasecmp (event_name, "keyboard:", 9))
294 etype->type_cat = ETYPE_KEYBOARD;
298 etype->type_cat = ETYPE_TOOLKIT;
303 etype->major = g_quark_from_string (split_string[1]);
306 etype->minor = g_quark_from_string (s = g_strconcat (split_string[1], split_string[2], NULL));
310 etype->detail = g_quark_from_string (split_string[3]);
314 etype->detail = g_quark_from_static_string ("");
319 etype->minor = etype->major;
320 etype->detail = g_quark_from_static_string (""); //etype->major;
325 etype->major = g_quark_from_static_string ("");
326 etype->minor = etype->major;
327 etype->detail = etype->major;
330 g_strfreev (split_string);
334 * deregisterApplication:
335 * @application: a reference to the @Application
336 * to be deregistered.
337 * return values: void
339 * De-register an application previously registered with the broker.
343 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
344 const Accessibility_Application application,
345 CORBA_Environment * ev)
347 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
349 spi_desktop_remove_application (registry->desktop, application);
352 fprintf (stderr, "de-registered app %p\n", application);
357 get_listener_list (SpiRegistry *registry,
358 EventTypeCategory cat)
368 ret = ®istry->object_listeners;
371 ret = ®istry->window_listeners;
375 ret = ®istry->toolkit_listeners;
385 * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
388 impl_accessibility_registry_register_global_event_listener (
389 PortableServer_Servant servant,
390 Accessibility_EventListener listener,
391 const CORBA_char *event_name,
392 CORBA_Environment *ev)
394 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
395 SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
396 EventTypeStruct etype;
399 #ifdef SPI_LISTENER_DEBUG
400 fprintf (stderr, "registering for events of type %s\n", event_name);
403 /* parse, check major event type and add listener accordingly */
404 parse_event_type (&etype, event_name);
405 ls->event_type_quark = etype.minor;
406 ls->event_type_cat = etype.type_cat;
408 list = get_listener_list (registry, etype.type_cat);
412 *list = g_list_prepend (*list, ls);
414 if (etype.type_cat == ETYPE_TOOLKIT)
416 register_with_toolkits (registry, &etype, ev);
421 spi_listener_struct_free (ls, ev);
425 static SpiReEntrantContinue
426 remove_listener_cb (GList * const *list, gpointer user_data)
428 SpiListenerStruct *ls = (SpiListenerStruct *) (*list)->data;
429 CORBA_Environment ev;
430 Accessibility_EventListener listener = user_data;
432 CORBA_exception_init (&ev);
434 if (CORBA_Object_is_equivalent (ls->listener, listener, &ev))
436 spi_re_entrant_list_delete_link (list);
437 spi_listener_struct_free (ls, &ev);
440 CORBA_exception_free (&ev);
442 return SPI_RE_ENTRANT_CONTINUE;
446 * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
449 impl_accessibility_registry_deregister_global_event_listener_all (
450 PortableServer_Servant servant,
451 Accessibility_EventListener listener,
452 CORBA_Environment *ev)
456 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
458 lists[0] = ®istry->object_listeners;
459 lists[1] = ®istry->window_listeners;
460 lists[2] = ®istry->toolkit_listeners;
462 for (i = 0; i < sizeof (lists) / sizeof (lists[0]); i++)
464 spi_re_entrant_list_foreach (lists [i], remove_listener_cb, listener);
470 * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
473 impl_accessibility_registry_deregister_global_event_listener (
474 PortableServer_Servant servant,
475 Accessibility_EventListener listener,
476 const CORBA_char *event_name,
477 CORBA_Environment *ev)
479 SpiRegistry *registry;
480 EventTypeStruct etype;
482 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
484 parse_event_type (&etype, (char *) event_name);
486 spi_re_entrant_list_foreach (get_listener_list (registry, etype.type_cat),
487 remove_listener_cb, listener);
493 * return values: a short integer indicating the current number of
496 * Get the current number of desktops.
500 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
501 CORBA_Environment * ev)
503 /* TODO: implement support for multiple virtual desktops */
504 CORBA_short n_desktops;
505 n_desktops = (CORBA_short) 1;
512 * @n: the index of the requested @Desktop.
513 * return values: a reference to the requested @Desktop.
515 * Get the nth accessible desktop.
518 static Accessibility_Desktop
519 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
521 CORBA_Environment * ev)
523 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
525 /* TODO: implement support for multiple virtual desktops */
528 return (Accessibility_Desktop)
529 bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
533 return (Accessibility_Desktop) CORBA_OBJECT_NIL;
540 * return values: a sequence containing references to
543 * Get a list of accessible desktops.
546 static Accessibility_DesktopSeq *
547 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
548 CORBA_Environment * ev)
550 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
551 Accessibility_DesktopSeq *desktops;
553 desktops = Accessibility_DesktopSeq__alloc ();
554 desktops->_length = desktops->_maximum = 1;
555 desktops->_buffer = Accessibility_DesktopSeq_allocbuf (desktops->_length);
556 desktops->_buffer [0] = bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
562 static Accessibility_DeviceEventController
563 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
564 CORBA_Environment *ev)
566 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
568 if (!registry->de_controller)
570 registry->de_controller = spi_device_event_controller_new (registry);
573 return bonobo_object_dup_ref (BONOBO_OBJREF (registry->de_controller), ev);
577 CORBA_Environment *ev;
578 Bonobo_Unknown source;
579 EventTypeStruct etype;
580 Accessibility_Event e_out;
583 static SpiReEntrantContinue
584 notify_listeners_cb (GList * const *list, gpointer user_data)
586 SpiListenerStruct *ls;
587 NotifyContext *ctx = user_data;
591 #ifdef SPI_LISTENER_DEBUG
592 fprintf (stderr, "event quarks: %lx %lx %lx\n", ls->event_type_quark, ctx->etype.major, ctx->etype.minor);
593 fprintf (stderr, "event name: %s\n", ctx->etype.event_name);
595 if ((ls->event_type_quark == ctx->etype.major) ||
596 (ls->event_type_quark == ctx->etype.minor))
600 fprintf (stderr, "notifying listener %d\n", 0);
601 /* g_list_index (list, l->data)); */
602 s = Accessibility_Accessible__get_name (ctx->source, ctx->ev);
603 fprintf (stderr, "event source name %s\n", s);
605 if (BONOBO_EX (ctx->ev))
607 CORBA_exception_free (ctx->ev);
608 return SPI_RE_ENTRANT_CONTINUE;
612 ctx->e_out.source = ctx->source;
614 if ((*list) && (*list)->data == ls)
616 Accessibility_EventListener_notifyEvent (
617 (Accessibility_EventListener) ls->listener, &ctx->e_out, ctx->ev);
618 if (ctx->ev->_major != CORBA_NO_EXCEPTION)
620 DBG (1, g_warning ("Accessibility app error: exception during "
621 "event notification: %s\n",
622 CORBA_exception_id (ctx->ev)));
623 CORBA_exception_free (ctx->ev);
624 /* FIXME: check that this item is removed from the list
625 * on system exception by a 'broken' listener */
630 return SPI_RE_ENTRANT_CONTINUE;
634 registry_emit_event (SpiRegistry *registry, NotifyContext *ctx)
636 GList **list = get_listener_list (registry, ctx->etype.type_cat);
641 spi_re_entrant_list_foreach (list, notify_listeners_cb, ctx);
645 static NotifyContext*
646 registry_clone_notify_context (NotifyContext *ctx)
648 NotifyContext *new_ctx = g_new0 (NotifyContext, 1);
651 new_ctx->source = bonobo_object_dup_ref (ctx->source, NULL);
652 new_ctx->etype.event_name = CORBA_string_dup (ctx->etype.event_name);
653 new_ctx->etype.type_cat = ctx->etype.type_cat;
654 new_ctx->etype.major = ctx->etype.major;
655 new_ctx->etype.minor = ctx->etype.minor;
656 new_ctx->etype.detail = ctx->etype.detail;
657 new_ctx->e_out.type = CORBA_string_dup (ctx->e_out.type);
658 new_ctx->e_out.source = ctx->e_out.source;
659 new_ctx->e_out.detail1 = ctx->e_out.detail1;
660 new_ctx->e_out.detail2 = ctx->e_out.detail2;
661 CORBA_any__copy (&(new_ctx->e_out.any_data), &(ctx->e_out.any_data));
666 registry_flush_event_queue (SpiRegistry *registry,
668 CORBA_Environment *ev)
670 NotifyContext *q_ctx;
671 while (!g_queue_is_empty (registry->deferred_event_queue)) {
672 q_ctx = g_queue_pop_tail (registry->deferred_event_queue);
673 #ifdef SPI_QUEUE_DEBUG
674 fprintf (stderr, "%s! %s [n=%d] %p\n", (discard ? "discard" : "pop"),
675 q_ctx->etype.event_name,
676 (int) registry->deferred_event_queue->length, q_ctx);
680 registry_emit_event (registry, q_ctx);
682 bonobo_object_release_unref (q_ctx->source, NULL);
683 CORBA_free ((void *)q_ctx->etype.event_name);
684 CORBA_free ((void *)q_ctx->e_out.type);
687 registry->is_queueing = FALSE;
691 registry_timeout_flush_queue (gpointer data)
693 SpiRegistry *registry = data;
694 CORBA_Environment ev;
695 #ifdef SPI_QUEUE_DEBUG
696 fprintf (stderr, "timeout! flushing queue...\n");
698 CORBA_exception_init (&ev);
699 registry_flush_event_queue (registry, FALSE, &ev);
704 registry_discard_on_event (SpiRegistry *registry, NotifyContext *ctx)
706 gboolean retval = FALSE;
707 NotifyContext *q_ctx = g_queue_peek_tail (registry->deferred_event_queue);
708 if ((q_ctx != NULL) &&
709 (ctx->etype.type_cat == ETYPE_WINDOW) &&
710 (ctx->etype.major == _activate_quark)) {
711 if (CORBA_Object_is_equivalent (ctx->source, q_ctx->source, NULL)) {
719 registry_reset_on_event (SpiRegistry *registry, NotifyContext *ctx)
721 return (ctx->etype.type_cat == ETYPE_WINDOW) ? TRUE : FALSE;
725 registry_start_queue (SpiRegistry *registry)
727 g_timeout_add_full (G_PRIORITY_HIGH_IDLE,
728 registry->exit_notify_timeout,
729 registry_timeout_flush_queue, registry,
731 registry->is_queueing = 1;
735 registry_defer_on_event (SpiRegistry *registry, NotifyContext *ctx)
737 gboolean defer = FALSE;
738 if ((ctx->etype.type_cat == ETYPE_WINDOW) &&
739 (ctx->etype.major == _deactivate_quark)) {
741 registry_start_queue (registry);
743 /* defer all object:state-change events after a window:deactivate */
744 else if ((ctx->etype.type_cat == ETYPE_FOCUS) ||
745 ((ctx->etype.type_cat == ETYPE_OBJECT) &&
746 (ctx->etype.major == _state_quark))) {
753 registry_queue_event (SpiRegistry *registry, NotifyContext *ctx)
755 NotifyContext *q_ctx = registry_clone_notify_context (ctx);
756 #ifdef SPI_QUEUE_DEBUG
757 if (q_ctx->etype.type_cat != ETYPE_MOUSE)
758 fprintf (stderr, "push! %s %p\n", q_ctx->etype.event_name, q_ctx);
760 g_queue_push_head (registry->deferred_event_queue, q_ctx);
764 * Dispose of event in one of several ways:
766 * 2) initiate queuing and push onto queue (below)
767 * 3) push on existing queue to either pop on timeout or on subsequent event
768 * 4) pass-through immediately
769 * 5) pass-through, discarding queued events
770 * 6) emit queued events and then pass through
773 registry_filter_event (SpiRegistry *registry, NotifyContext *ctx,
774 CORBA_Environment *ev)
776 gboolean queue_is_empty = FALSE;
777 g_assert (ctx != NULL);
779 /* case #1 is not yet used */
780 if (registry_defer_on_event (registry, ctx)) { /* #2, #3 */
781 if (registry->is_queueing) {
782 registry_queue_event (registry, ctx);
789 else if (registry_reset_on_event (registry, ctx)) { /* #5, #6 */
790 gboolean discard = registry_discard_on_event (registry, ctx);
791 #ifdef SPI_QUEUE_DEBUG
792 fprintf (stderr, "event %s caused reset, discard=%d\n",
793 ctx->etype.event_name, (int) discard);
795 registry_flush_event_queue (registry, discard, ev);
796 return (discard ? FALSE : TRUE);
804 impl_registry_notify_event (PortableServer_Servant servant,
805 const Accessibility_Event *e,
806 CORBA_Environment *ev)
808 SpiRegistry *registry;
810 static int level = 0;
813 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
815 parse_event_type (&ctx.etype, e->type);
819 ctx.source = e->source;
821 if (registry_filter_event (registry, &ctx, ev)) {
822 #ifdef SPI_QUEUE_DEBUG
823 if (ctx.etype.type_cat != ETYPE_MOUSE)
825 fprintf (stderr, "emit! %s level: %d\n", ctx.etype.event_name, level);
826 fprintf (stderr, "emit! %p %p\n", ctx.e_out, ctx.e_out.type);
829 registry_emit_event (registry, &ctx);
835 spi_registry_class_init (SpiRegistryClass *klass)
837 GObjectClass * object_class = (GObjectClass *) klass;
838 POA_Accessibility_Registry__epv *epv = &klass->epv;
840 spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
842 object_class->finalize = spi_registry_object_finalize;
844 klass->parent_class.epv.notifyEvent = impl_registry_notify_event;
846 epv->registerApplication = impl_accessibility_registry_register_application;
847 epv->deregisterApplication = impl_accessibility_registry_deregister_application;
848 epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
849 epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
850 epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
851 epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
852 epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
853 epv->getDesktop = impl_accessibility_registry_get_desktop;
854 epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
855 _deactivate_quark = g_quark_from_static_string ("deactivate");
856 _activate_quark = g_quark_from_static_string ("activate");
857 _state_quark = g_quark_from_static_string ("state-changed");
861 spi_registry_init (SpiRegistry *registry)
863 spi_registry_set_debug (g_getenv ("AT_SPI_DEBUG"));
865 * TODO: FIXME, this module makes the foolish assumptions that
866 * registryd uses the same display as the apps, and that the
867 * DISPLAY environment variable is set.
869 gdk_init (NULL, NULL);
871 registry->object_listeners = NULL;
872 registry->window_listeners = NULL;
873 registry->toolkit_listeners = NULL;
874 registry->deferred_event_queue = g_queue_new ();
875 registry->exit_notify_timeout = 100;
876 registry->desktop = spi_desktop_new ();
877 /* Register callback notification for application addition and removal */
878 g_signal_connect (G_OBJECT (registry->desktop),
880 G_CALLBACK (desktop_add_application),
883 g_signal_connect (G_OBJECT (registry->desktop),
884 "application_removed",
885 G_CALLBACK (desktop_remove_application),
888 registry->de_controller = spi_device_event_controller_new (registry);
891 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
892 Accessibility_Registry,
897 spi_registry_new (void)
899 SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
900 bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);