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 spi_registry_object_finalize (GObject *object)
88 printf ("spi_registry_object_finalize called\n");
90 /* TODO: unref deviceeventcontroller, which disconnects key listener */
91 G_OBJECT_CLASS (spi_registry_parent_class)->finalize (object);
103 * registerApplication:
104 * @application: a reference to the requesting @Application
105 * return values: void
107 * Register a new application with the accessibility broker.
111 impl_accessibility_registry_register_application (PortableServer_Servant servant,
112 const Accessibility_Application application,
113 CORBA_Environment * ev)
115 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
118 fprintf (stderr, "registering app %p\n", application);
120 spi_desktop_add_application (registry->desktop, application);
122 Accessibility_Application__set_id (application, _get_unique_id (), ev);
125 * TODO: change the implementation below to a WM-aware one;
126 * e.g. don't add all apps to the SpiDesktop
130 #ifdef USE_A_HASH_IN_FUTURE
132 compare_corba_objects (gconstpointer p1, gconstpointer p2)
134 CORBA_Environment ev;
138 fprintf (stderr, "comparing %p to %p\n",
142 retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
148 register_with_toolkits (SpiRegistry *spi_registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
153 Accessibility_Desktop desktop;
154 Accessibility_Application app;
155 Accessibility_Registry registry;
156 registry = BONOBO_OBJREF (spi_registry_bonobo_object);
158 /* for each app in each desktop, call ...Application_registerToolkitEventListener */
160 n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
162 for (i=0; i<n_desktops; ++i)
164 desktop = Accessibility_Registry_getDesktop (registry, i, ev);
165 n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
166 for (j=0; j<n_apps; ++j)
168 app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
171 Accessibility_Application_registerToolkitEventListener (app,
173 CORBA_string_dup (etype->event_name),
179 #ifdef USE_A_HASH_IN_FUTURE
182 compare_listener_quarks (gconstpointer p1, gconstpointer p2)
184 return (((SpiListenerStruct *)p2)->event_type_quark !=
185 ((SpiListenerStruct *)p1)->event_type_quark);
189 compare_listener_corbaref (gconstpointer p1, gconstpointer p2)
191 return compare_corba_objects (((SpiListenerStruct *)p2)->listener,
192 ((SpiListenerStruct *)p1)->listener);
197 parse_event_type (EventTypeStruct *etype, const char *event_name)
199 gchar **split_string;
202 split_string = g_strsplit (event_name, ":", 4);
203 etype->event_name = g_strdup (event_name);
205 if (!g_ascii_strncasecmp (event_name, "focus:", 6))
207 etype->type_cat = ETYPE_FOCUS;
209 else if (!g_ascii_strncasecmp (event_name, "object:", 7))
211 etype->type_cat = ETYPE_OBJECT;
213 else if (!g_ascii_strncasecmp (event_name, "window:", 7))
215 etype->type_cat = ETYPE_WINDOW;
219 etype->type_cat = ETYPE_TOOLKIT;
226 etype->minor = g_quark_from_string (s = g_strconcat (split_string[1], split_string[2], NULL));
230 etype->detail = g_quark_from_string (split_string[3]);
231 s = g_strconcat (split_string[1], split_string[2], split_string[3], NULL);
232 etype->major = g_quark_from_string (s);
237 etype->detail = g_quark_from_static_string ("");
238 s = g_strconcat (split_string[1], split_string[2], NULL);
239 etype->major = g_quark_from_string (s);
245 etype->major = g_quark_from_string (split_string[1]);
246 etype->minor = etype->major;
247 etype->detail = etype->major;
252 etype->major = g_quark_from_static_string ("");
253 etype->minor = etype->major;
254 etype->detail = etype->major;
257 g_strfreev (split_string);
261 * deregisterApplication:
262 * @application: a reference to the @Application
263 * to be deregistered.
264 * return values: void
266 * De-register an application previously registered with the broker.
270 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
271 const Accessibility_Application application,
272 CORBA_Environment * ev)
274 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
276 spi_desktop_remove_application (registry->desktop, application);
279 fprintf (stderr, "de-registered app %p\n", application);
284 get_listener_list (SpiRegistry *registry,
285 EventTypeCategory cat)
294 ret = ®istry->object_listeners;
297 ret = ®istry->window_listeners;
300 ret = ®istry->toolkit_listeners;
311 * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
314 impl_accessibility_registry_register_global_event_listener (
315 PortableServer_Servant servant,
316 Accessibility_EventListener listener,
317 const CORBA_char *event_name,
318 CORBA_Environment *ev)
320 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
321 SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
322 EventTypeStruct etype;
326 fprintf (stderr, "registering for events of type %s\n", event_name);
329 /* parse, check major event type and add listener accordingly */
330 parse_event_type (&etype, event_name);
331 ls->event_type_quark = etype.major;
332 ls->event_type_cat = etype.type_cat;
334 list = get_listener_list (registry, etype.type_cat);
338 *list = g_list_prepend (*list, ls);
340 if (etype.type_cat == ETYPE_TOOLKIT)
342 register_with_toolkits (registry, &etype, ev);
347 spi_listener_struct_free (ls, ev);
351 static SpiReEnterantContinue
352 remove_listener_cb (GList * const *list, gpointer user_data)
354 SpiListenerStruct *ls = (SpiListenerStruct *) (*list)->data;
355 CORBA_Environment ev;
356 Accessibility_EventListener listener = user_data;
358 CORBA_exception_init (&ev);
360 if (CORBA_Object_is_equivalent (ls->listener, listener, &ev))
362 spi_re_enterant_list_delete_link (list);
363 spi_listener_struct_free (ls, &ev);
366 CORBA_exception_free (&ev);
368 return SPI_RE_ENTERANT_CONTINUE;
372 * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
375 impl_accessibility_registry_deregister_global_event_listener_all (
376 PortableServer_Servant servant,
377 Accessibility_EventListener listener,
378 CORBA_Environment *ev)
382 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
384 lists[0] = ®istry->object_listeners;
385 lists[1] = ®istry->window_listeners;
386 lists[2] = ®istry->toolkit_listeners;
388 for (i = 0; i < sizeof (lists) / sizeof (lists[0]); i++)
390 spi_re_enterant_list_foreach (lists [i], remove_listener_cb, listener);
396 * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
399 impl_accessibility_registry_deregister_global_event_listener (
400 PortableServer_Servant servant,
401 Accessibility_EventListener listener,
402 const CORBA_char *event_name,
403 CORBA_Environment *ev)
405 SpiRegistry *registry;
406 EventTypeStruct etype;
408 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
410 parse_event_type (&etype, (char *) event_name);
412 spi_re_enterant_list_foreach (get_listener_list (registry, etype.type_cat),
413 remove_listener_cb, listener);
419 * return values: a short integer indicating the current number of
422 * Get the current number of desktops.
426 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
427 CORBA_Environment * ev)
429 /* TODO: implement support for multiple virtual desktops */
430 CORBA_short n_desktops;
431 n_desktops = (CORBA_short) 1;
438 * @n: the index of the requested @Desktop.
439 * return values: a reference to the requested @Desktop.
441 * Get the nth accessible desktop.
444 static Accessibility_Desktop
445 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
447 CORBA_Environment * ev)
449 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
451 /* TODO: implement support for multiple virtual desktops */
454 return (Accessibility_Desktop)
455 bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
459 return (Accessibility_Desktop) CORBA_OBJECT_NIL;
466 * return values: a sequence containing references to
469 * Get a list of accessible desktops.
472 static Accessibility_DesktopSeq *
473 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
474 CORBA_Environment * ev)
476 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
477 Accessibility_DesktopSeq *desktops;
479 desktops = Accessibility_DesktopSeq__alloc ();
480 desktops->_length = desktops->_maximum = 1;
481 desktops->_buffer = Accessibility_DesktopSeq_allocbuf (desktops->_length);
482 desktops->_buffer [0] = bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
488 static Accessibility_DeviceEventController
489 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
490 CORBA_Environment *ev)
492 SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
494 if (!registry->de_controller)
496 registry->de_controller = spi_device_event_controller_new (registry);
499 return bonobo_object_dup_ref (BONOBO_OBJREF (registry->de_controller), ev);
503 CORBA_Environment *ev;
504 Bonobo_Unknown source;
505 EventTypeStruct etype;
506 Accessibility_Event e_out;
509 static SpiReEnterantContinue
510 notify_listeners_cb (GList * const *list, gpointer user_data)
512 SpiListenerStruct *ls;
513 NotifyContext *ctx = user_data;
520 #ifdef SPI_LISTENER_DEBUG
521 fprintf (stderr, "event quarks: %lx %lx %lx\n", ls->event_type_quark, etype.major, etype.minor);
522 fprintf (stderr, "event name: %s\n", etype.event_name);
525 if ((ls->event_type_quark == ctx->etype.major) ||
526 (ls->event_type_quark == ctx->etype.minor))
529 fprintf (stderr, "notifying listener %d\n", g_list_index (listeners, l->data));
530 s = Accessibility_Accessible__get_name (ctx->source, ev);
531 fprintf (stderr, "event source name %s\n", s);
535 ctx->e_out.source = bonobo_object_dup_ref (ctx->source, ctx->ev);
536 if (BONOBO_EX (ctx->ev))
538 return SPI_RE_ENTERANT_CONTINUE;;
541 if ((*list) && (*list)->data == ls)
543 Accessibility_EventListener_notifyEvent (
544 (Accessibility_EventListener) ls->listener, &ctx->e_out, ctx->ev);
545 if (ctx->ev->_major != CORBA_NO_EXCEPTION)
547 g_warning ("Accessibility app error: exception during "
548 "event notification: %s\n",
549 CORBA_exception_id (ctx->ev));
552 else /* dup re-entered */
554 bonobo_object_release_unref (ctx->e_out.source, ctx->ev);
558 return SPI_RE_ENTERANT_CONTINUE;
562 impl_registry_notify_event (PortableServer_Servant servant,
563 const Accessibility_Event *e,
564 CORBA_Environment *ev)
566 SpiRegistry *registry;
570 registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
572 parse_event_type (&ctx.etype, e->type);
574 list = get_listener_list (registry, ctx.etype.type_cat);
580 ctx.source = e->source;
581 parse_event_type (&ctx.etype, e->type);
583 spi_re_enterant_list_foreach (list, notify_listeners_cb, &ctx);
586 if (e->source != CORBA_OBJECT_NIL)
588 Accessibility_Accessible_unref (e->source, ev);
594 spi_registry_class_init (SpiRegistryClass *klass)
596 GObjectClass * object_class = (GObjectClass *) klass;
597 POA_Accessibility_Registry__epv *epv = &klass->epv;
599 spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
601 object_class->finalize = spi_registry_object_finalize;
603 klass->parent_class.epv.notifyEvent = impl_registry_notify_event;
605 epv->registerApplication = impl_accessibility_registry_register_application;
606 epv->deregisterApplication = impl_accessibility_registry_deregister_application;
607 epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
608 epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
609 epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
610 epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
611 epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
612 epv->getDesktop = impl_accessibility_registry_get_desktop;
613 epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
617 spi_registry_init (SpiRegistry *registry)
619 registry->object_listeners = NULL;
620 registry->window_listeners = NULL;
621 registry->toolkit_listeners = NULL;
622 registry->desktop = spi_desktop_new ();
623 registry->de_controller = NULL;
626 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
627 Accessibility_Registry,
632 spi_registry_new (void)
634 SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
635 bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);