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