2001-12-10 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / registryd / registry.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /* registry.c: the main accessibility service registry implementation */
24
25 #include <config.h>
26 #ifdef SPI_DEBUG
27 #  include <stdio.h>
28 #endif
29
30 #include <libspi/registry.h>
31
32 /* Our parent GObject type  */
33 #define PARENT_TYPE SPI_LISTENER_TYPE
34
35 /* A pointer to our parent object class */
36 static SpiListenerClass *spi_registry_parent_class;
37
38 typedef enum {
39   ETYPE_FOCUS,
40   ETYPE_OBJECT,
41   ETYPE_PROPERTY,
42   ETYPE_WINDOW,
43   ETYPE_TOOLKIT,
44   ETYPE_KEYBOARD,
45   
46   ETYPE_LAST_DEFINED
47 } EventTypeCategory;
48
49 typedef struct {
50   char *event_name;
51   EventTypeCategory type_cat;
52   char * major;
53   char * minor;
54   char * detail;
55   guint hash;
56 } EventTypeStruct;
57
58 typedef struct {
59   Accessibility_EventListener listener;
60   guint event_type_hash;
61   EventTypeCategory event_type_cat;
62 } SpiListenerStruct;
63
64 /* static function prototypes */
65 static void _registry_notify_listeners (GList *listeners,
66                                         const Accessibility_Event *e,
67                                         CORBA_Environment *ev);
68
69 static long _get_unique_id();
70
71 static gboolean _device_event_controller_hook (gpointer source);
72
73 SpiListenerStruct *
74 spi_listener_struct_new (Accessibility_EventListener listener, CORBA_Environment *ev)
75 {
76   SpiListenerStruct *retval = g_malloc (sizeof (SpiListenerStruct));
77   retval->listener = bonobo_object_dup_ref (listener, ev);
78   return retval;
79 }
80
81 void
82 spi_listener_struct_free (SpiListenerStruct *ls, CORBA_Environment *ev)
83 {
84   bonobo_object_release_unref (ls->listener, ev);
85   g_free (ls);
86 }
87
88 /* GObject::finalize */
89 static void
90 spi_registry_object_finalize (GObject *object)
91 {
92         SpiRegistry *registry = SPI_REGISTRY (object);
93
94         printf("spi_registry_object_finalize called\n");
95         /* TODO: unref deviceeventcontroller, which disconnects key listener */
96         G_OBJECT_CLASS (spi_registry_parent_class)->finalize (object);
97 }
98
99 /**
100  * registerApplication:
101  * @application: a reference to the requesting @Application
102  * return values: void
103  *
104  * Register a new application with the accessibility broker.
105  *
106  **/
107 static void
108 impl_accessibility_registry_register_application (PortableServer_Servant servant,
109                                                   const Accessibility_Application application,
110                                                   CORBA_Environment * ev)
111 {
112   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
113
114 #ifdef SPI_DEBUG
115   fprintf (stderr, "registering app %p\n", application);
116 #endif
117   spi_desktop_add_application (registry->desktop, application);
118
119   Accessibility_Application__set_id (application, _get_unique_id(), ev);
120
121   /*
122    * TODO: change the implementation below to a WM-aware one;
123    * e.g. don't add all apps to the SpiDesktop
124    */
125 }
126
127 static gint
128 compare_corba_objects (gconstpointer p1, gconstpointer p2)
129 {
130   CORBA_Environment ev;
131   gint retval;
132
133 #ifdef SPI_DEBUG
134   fprintf (stderr, "comparing %p to %p\n",
135            p1, p2);
136 #endif
137   
138   retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
139   return retval;  
140 }
141
142 static void
143 register_with_toolkits (SpiRegistry *spi_registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
144 {
145   gint n_desktops;
146   gint n_apps;
147   gint i, j;
148   Accessibility_Desktop desktop;
149   Accessibility_Application app;
150   Accessibility_Registry registry;
151   registry  = BONOBO_OBJREF (spi_registry_bonobo_object);
152
153   /* for each app in each desktop, call ...Application_registerToolkitEventListener */
154
155   n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
156
157   for (i=0; i<n_desktops; ++i)
158     {
159       desktop = Accessibility_Registry_getDesktop (registry, i, ev);
160       n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
161       for (j=0; j<n_apps; ++j)
162         {
163           app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
164                                                                                    j,
165                                                                                    ev);
166           Accessibility_Application_registerToolkitEventListener (app,
167                                                                   registry,
168                                                                   CORBA_string_dup (etype->event_name),
169                                                                   
170                                                                   ev);
171         }
172     }
173 }
174
175 static gint
176 compare_listener_hash (gconstpointer p1, gconstpointer p2)
177 {
178   return (((SpiListenerStruct *)p2)->event_type_hash - ((SpiListenerStruct *)p1)->event_type_hash);
179 }
180
181 static gint
182 compare_listener_corbaref (gconstpointer p1, gconstpointer p2)
183 {
184   return compare_corba_objects (((SpiListenerStruct *)p2)->listener,
185                                 ((SpiListenerStruct *)p1)->listener);
186 }
187
188 static void
189 parse_event_type (EventTypeStruct *etype, char *event_name)
190 {
191   gchar **split_string;
192   gchar *s;
193
194   split_string = g_strsplit(event_name, ":", 4);
195   etype->event_name = g_strndup(event_name, 255);
196
197   if (!g_ascii_strncasecmp (event_name, "focus:", 6))
198     {
199       etype->type_cat = ETYPE_FOCUS;
200     }
201   else if (!g_ascii_strncasecmp (event_name, "object:", 7))
202     {
203       etype->type_cat = ETYPE_OBJECT;
204     }
205   else if (!g_ascii_strncasecmp (event_name, "window:", 7))
206     {
207       etype->type_cat = ETYPE_WINDOW;
208     }
209   else
210     {
211       etype->type_cat = ETYPE_TOOLKIT;
212     }
213
214   if (split_string[1])
215     {
216       etype->major = g_strdup (split_string[1]);
217       if (split_string[2])
218         {
219           etype->minor = g_strdup (split_string[2]);
220           if (split_string[3])
221             {
222               etype->detail = g_strdup (split_string[3]);
223               s = g_strconcat (split_string[1], split_string[2], split_string[3], NULL);
224               etype->hash = g_str_hash (s);
225               g_free (s);
226             }
227           else
228             {
229               etype->detail = g_strdup ("");
230               s = g_strconcat (split_string[1], split_string[2], NULL);
231               etype->hash = g_str_hash (s);
232               g_free (s);
233             }
234         }
235       else
236         {
237           etype->minor = g_strdup ("");
238           etype->hash = g_str_hash ( split_string[1]);
239         }
240     }
241   else
242     {
243       etype->major = g_strdup ("");
244       etype->minor = g_strdup ("");
245       etype->detail = g_strdup ("");
246       etype->hash = g_str_hash ("");
247     }
248
249   g_strfreev (split_string);
250 }
251
252 /**
253  * deregisterApplication:
254  * @application: a reference to the @Application
255  * to be deregistered.
256  * return values: void
257  *
258  * De-register an application previously registered with the broker.
259  *
260  **/
261 static void
262 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
263                                                     const Accessibility_Application application,
264                                                     CORBA_Environment * ev)
265 {
266   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
267
268   spi_desktop_remove_application (registry->desktop, application);
269
270 #ifdef SPI_DEBUG
271   fprintf (stderr, "de-registered app %p\n", application);
272 #endif
273 }
274
275 /*
276  * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
277  */
278 static void
279 impl_accessibility_registry_register_global_event_listener (
280                                              PortableServer_Servant  servant,
281                                              Accessibility_EventListener listener,
282                                              const CORBA_char *event_name,
283                                              CORBA_Environment      *ev)
284 {
285   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
286   SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
287   EventTypeStruct etype;
288
289   fprintf(stderr, "registering for events of type %s\n", event_name);
290
291   /* parse, check major event type and add listener accordingly */
292   parse_event_type (&etype, (char*) event_name);
293   ls->event_type_hash = etype.hash;
294   ls->event_type_cat = etype.type_cat;
295
296   switch (etype.type_cat)
297     {
298     case (ETYPE_FOCUS) :
299     case (ETYPE_OBJECT) :
300     case (ETYPE_PROPERTY) :
301       registry->object_listeners =
302         g_list_append (registry->object_listeners, ls);
303       break;
304     case (ETYPE_WINDOW) :
305       /* Support for Window Manager Events is not yet implemented */
306       spi_listener_struct_free (ls, ev);
307       break;
308     case (ETYPE_TOOLKIT) :
309       registry->toolkit_listeners =
310         g_list_append (registry->toolkit_listeners, ls);
311       register_with_toolkits (registry, &etype, ev);
312       break;
313     default:
314       spi_listener_struct_free (ls, ev);
315       break;
316     }
317 }
318
319 /*
320  * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
321  */
322 static void
323 impl_accessibility_registry_deregister_global_event_listener_all (
324                                                     PortableServer_Servant  servant,
325                                                     Accessibility_EventListener listener,
326                                                     CORBA_Environment      *ev)
327 {
328   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
329   SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
330   GList *list;
331   list = g_list_find_custom (registry->object_listeners, ls,
332                              compare_listener_corbaref);
333
334   /*
335    * TODO : de-register with toolkit if the last instance of a listener
336    *        to a particular toolkit event type has been deregistered.
337    */
338
339   while (list)
340     {
341       spi_listener_struct_free ((SpiListenerStruct *) list->data, ev);
342       registry->object_listeners = g_list_delete_link (registry->object_listeners, list);
343       list = g_list_find_custom (registry->object_listeners, ls, compare_listener_corbaref);
344     }
345   list = g_list_find_custom (registry->toolkit_listeners, ls, compare_listener_corbaref);
346   while (list)
347     {
348       spi_listener_struct_free ((SpiListenerStruct *) list->data, ev);
349       registry->toolkit_listeners = g_list_delete_link (registry->toolkit_listeners, list);
350       list = g_list_find_custom (registry->toolkit_listeners, ls, compare_listener_corbaref);
351     }
352   spi_listener_struct_free (ls, ev);
353 }
354
355 /*
356  * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
357  */
358 static void
359 impl_accessibility_registry_deregister_global_event_listener (
360                                                     PortableServer_Servant  servant,
361                                                     Accessibility_EventListener listener,
362                                                     const CORBA_char * event_name,
363                                                     CORBA_Environment      *ev)
364 {
365   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
366   SpiListenerStruct ls;
367   EventTypeStruct etype;
368   GList *list;
369   GList **listeners;
370
371   parse_event_type (&etype, (char *) event_name);
372   switch (etype.type_cat)
373     {
374     case (ETYPE_OBJECT) :
375     case (ETYPE_PROPERTY) :
376     case (ETYPE_FOCUS) :
377       listeners = &registry->object_listeners;
378       break;
379     case (ETYPE_WINDOW) :
380       /* Support for Window Manager Events is not yet implemented */
381       listeners = NULL;
382       break;
383     case (ETYPE_TOOLKIT) :
384       listeners = &registry->toolkit_listeners;
385       break;
386     default:
387       listeners = NULL;
388       break;
389     }
390
391   if (!listeners)
392           return;
393
394   ls.event_type_hash = etype.hash;
395   list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
396
397   while (list)
398     {
399       spi_listener_struct_free ((SpiListenerStruct *) list->data, ev);
400       *listeners = g_list_delete_link (*listeners, list);
401       list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
402     }
403 }
404
405
406 /**
407  * getDesktopCount:
408  * return values: a short integer indicating the current number of
409  * @Desktops.
410  *
411  * Get the current number of desktops.
412  *
413  **/
414 static short
415 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
416                                                CORBA_Environment * ev)
417 {
418   /* TODO: implement support for multiple virtual desktops */
419   CORBA_short n_desktops;
420   n_desktops = (CORBA_short) 1;
421   return n_desktops;
422 }
423
424 /**
425  * getDesktop:
426  * @n: the index of the requested @Desktop.
427  * return values: a reference to the requested @Desktop.
428  *
429  * Get the nth accessible desktop.
430  *
431  **/
432 static Accessibility_Desktop
433 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
434                                          const CORBA_short n,
435                                          CORBA_Environment * ev)
436 {
437   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
438
439   /* TODO: implement support for multiple virtual desktops */
440   if (n == 0)
441     {
442       return (Accessibility_Desktop)
443         bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
444     }
445   else
446     {
447       return (Accessibility_Desktop) CORBA_OBJECT_NIL;
448     }
449 }
450
451 /**
452  * getDesktopList:
453  * return values: a sequence containing references to
454  * the @Desktops.
455  *
456  * Get a list of accessible desktops.
457  *
458  **/
459 static Accessibility_DesktopSeq *
460 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
461                                               CORBA_Environment * ev)
462 {
463   /* TODO: implement support for multiple virtual desktops */
464   return (Accessibility_DesktopSeq *) NULL;
465 }
466
467 static Accessibility_DeviceEventController
468 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
469                                                          CORBA_Environment * ev)
470 {
471   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
472   if (!registry->device_event_controller)
473     registry->device_event_controller = spi_device_event_controller_new (registry);
474
475   return bonobo_object_dup_ref (BONOBO_OBJREF (registry->device_event_controller), ev);
476 }
477
478 static void
479 impl_registry_notify_event (PortableServer_Servant servant,
480                             const Accessibility_Event *e,
481                             CORBA_Environment *ev)
482 {
483   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
484   EventTypeStruct etype;
485
486   parse_event_type (&etype, e->type);
487
488   switch (etype.type_cat)
489     {
490     case (ETYPE_OBJECT) :
491     case (ETYPE_PROPERTY) :
492     case (ETYPE_FOCUS) :
493       _registry_notify_listeners (registry->object_listeners, e, ev); 
494       break;
495     case (ETYPE_WINDOW) :
496       _registry_notify_listeners (registry->window_listeners, e, ev);
497       break;
498     case (ETYPE_TOOLKIT) :
499       _registry_notify_listeners (registry->toolkit_listeners, e, ev); 
500       break;
501     case (ETYPE_KEYBOARD) :
502     default:
503       break;
504     }
505   if (e->source != CORBA_OBJECT_NIL)
506     {
507       Accessibility_Accessible_unref (e->source, ev);
508     }
509 }
510
511 static long
512 _get_unique_id ()
513 {
514   static long id = 0;
515   return ++id;
516 }
517
518 static void
519 _registry_notify_listeners (GList *listeners,
520                             const Accessibility_Event *e_in,
521                             CORBA_Environment *ev)
522 {
523   GList              *l;
524   Accessibility_Event e_out;
525   SpiListenerStruct  *ls;
526   EventTypeStruct     etype;
527   guint               minor_hash;
528   CORBA_string        s;
529
530   e_out = *e_in;
531   parse_event_type (&etype, e_in->type);
532
533   s = g_strconcat (etype.major, etype.minor, NULL);
534   minor_hash = g_str_hash (s);
535   g_free (s);
536
537   for (l = listeners; l; l = l->next)
538     {
539       ls = (SpiListenerStruct *) l->data;
540
541 #ifdef SPI_SPI_LISTENER_DEBUG
542       fprintf (stderr, "event hashes: %lx %lx %lx\n", ls->event_type_hash, etype.hash, minor_hash);
543       fprintf (stderr, "event name: %s\n", etype.event_name);
544 #endif
545
546       if ((ls->event_type_hash == etype.hash) || (ls->event_type_hash == minor_hash))
547         {
548 #ifdef SPI_DEBUG
549           fprintf (stderr, "notifying listener %d\n", g_list_index (listeners, l->data));
550           s = Accessibility_Accessible__get_name (e_in->source, ev);
551           fprintf (stderr, "event source name %s\n", s);
552           CORBA_free (s);
553 #endif
554           e_out.source = bonobo_object_dup_ref (e_in->source, ev);
555           Accessibility_EventListener_notifyEvent ((Accessibility_EventListener) ls->listener,
556                                                    &e_out,
557                                                    ev);
558           if (ev->_major != CORBA_NO_EXCEPTION)
559             {
560               g_warning ("Accessibility app error: exception during event notification: %s\n",
561                        CORBA_exception_id (ev));
562             }
563         }
564     }
565 }
566
567 static gboolean
568 _device_event_controller_hook (gpointer p)
569 {
570     SpiRegistry *registry = (SpiRegistry *)p;
571     SpiDeviceEventController *controller = registry->device_event_controller;
572     if (controller)
573         spi_device_event_controller_check_key_event (controller);
574     return TRUE;
575 }
576
577 static void
578 spi_registry_class_init (SpiRegistryClass *klass)
579 {
580         GObjectClass * object_class = (GObjectClass *) klass;
581         POA_Accessibility_Registry__epv *epv = &klass->epv;
582
583         spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
584
585         object_class->finalize = spi_registry_object_finalize;
586
587         epv->registerApplication = impl_accessibility_registry_register_application;
588         epv->deregisterApplication = impl_accessibility_registry_deregister_application;
589         epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
590         epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
591         epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
592         epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
593         epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
594         epv->getDesktop = impl_accessibility_registry_get_desktop;
595         epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
596
597         ((SpiListenerClass *) klass)->epv.notifyEvent = impl_registry_notify_event;
598 }
599
600 static void
601 spi_registry_init (SpiRegistry *registry)
602 {
603   registry->object_listeners = NULL;
604   registry->window_listeners = NULL;
605   registry->toolkit_listeners = NULL;
606   registry->desktop = spi_desktop_new();
607   registry->device_event_controller = NULL;
608   registry->kbd_event_hook = _device_event_controller_hook;
609 }
610
611 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
612                        Accessibility_Registry,
613                        PARENT_TYPE,
614                        spi_registry);
615
616 SpiRegistry *
617 spi_registry_new (void)
618 {
619     SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
620     bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);
621     return retval;
622 }