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