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