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