Fixes for 98836, 98842, added slots to IDL for ABI freeze.
[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, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /* registry.c: the main accessibility service registry implementation */
25
26 #undef SPI_LISTENER_DEBUG
27 #undef SPI_DEBUG
28
29 #include <config.h>
30 #ifdef SPI_DEBUG
31 #  include <stdio.h>
32 #endif
33
34 #include <bonobo/bonobo-exception.h>
35 #include "../libspi/spi-private.h"
36 #include "registry.h"
37
38 /* Our parent GObject type  */
39 #define PARENT_TYPE SPI_LISTENER_TYPE
40
41 /* A pointer to our parent object class */
42 static SpiListenerClass *spi_registry_parent_class;
43
44 typedef enum {
45   ETYPE_FOCUS,
46   ETYPE_OBJECT,
47   ETYPE_PROPERTY,
48   ETYPE_WINDOW,
49   ETYPE_TOOLKIT,
50   ETYPE_KEYBOARD,
51   ETYPE_MOUSE,
52   ETYPE_LAST_DEFINED
53 } EventTypeCategory;
54
55 typedef struct {
56   const char *event_name;
57   EventTypeCategory type_cat;
58   GQuark major;  /* from string segment[1] */
59   GQuark minor;  /* from string segment[1]+segment[2] */
60   GQuark detail; /* from string segment[3] (not concatenated) */
61 } EventTypeStruct;
62
63 typedef struct {
64   Accessibility_EventListener listener;
65   GQuark            event_type_quark;
66   EventTypeCategory event_type_cat;
67 } SpiListenerStruct;
68
69 SpiListenerStruct *
70 spi_listener_struct_new (Accessibility_EventListener listener, CORBA_Environment *ev)
71 {
72   SpiListenerStruct *retval = g_malloc (sizeof (SpiListenerStruct));
73   retval->listener = bonobo_object_dup_ref (listener, ev);
74   return retval;
75 }
76
77
78 void
79 spi_listener_struct_free (SpiListenerStruct *ls, CORBA_Environment *ev)
80 {
81   bonobo_object_release_unref (ls->listener, ev);
82   g_free (ls);
83 }
84
85 static void
86 desktop_add_application (SpiDesktop *desktop,
87                          guint index, gpointer data)
88 {
89   BonoboObject *registry = BONOBO_OBJECT (data);
90   Accessibility_Event e;
91   CORBA_Environment ev;
92   Accessibility_Accessible a;
93   
94   CORBA_exception_init (&ev);
95   e.type = "object:children-changed:add";
96   e.source = BONOBO_OBJREF (desktop);
97   e.detail1 = index;
98   e.detail2 = 0;
99   a = Accessibility_Accessible_getChildAtIndex (BONOBO_OBJREF (desktop), 
100                                                 index, &ev);
101   /* FIXME
102   spi_init_any_object (&e.any_data, a);
103   */
104   spi_init_any_nil (&e.any_data);
105   Accessibility_Accessible_unref (a, &ev);
106   Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
107                                       &e, &ev);
108   Accessibility_Desktop_unref (e.source, &ev);
109   CORBA_exception_free (&ev);
110 }
111
112
113
114 static void
115 desktop_remove_application (SpiDesktop *desktop,
116                             guint index, gpointer data)
117 {
118   BonoboObject *registry = BONOBO_OBJECT (data);
119   Accessibility_Event e;
120   Accessibility_Accessible a;
121   CORBA_Environment ev;
122   
123   CORBA_exception_init (&ev);
124
125   e.type = "object:children-changed:remove";
126   e.source = BONOBO_OBJREF (desktop);
127   e.detail1 = index;
128   e.detail2 = 0;
129   a = Accessibility_Accessible_getChildAtIndex (BONOBO_OBJREF (desktop), 
130                                                 index, &ev);
131   /* FIXME
132   spi_init_any_object (&e.any_data, a);
133   */
134   spi_init_any_nil (&e.any_data);
135   Accessibility_Accessible_unref (a, &ev);
136   Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
137                                       &e, &ev);
138   Accessibility_Desktop_unref (e.source, &ev);
139   CORBA_exception_free (&ev);
140 }
141
142
143 static void
144 spi_registry_object_finalize (GObject *object)
145 {
146   fprintf (stderr, "spi_registry_object_finalize called\n");
147
148   /* TODO: unref deviceeventcontroller, which disconnects key listener */
149   G_OBJECT_CLASS (spi_registry_parent_class)->finalize (object);
150 }
151
152 static long
153 _get_unique_id (void)
154 {
155   static long id = 0;
156
157   return ++id;
158 }
159
160 /**
161  * registerApplication:
162  * @application: a reference to the requesting @Application
163  * return values: void
164  *
165  * Register a new application with the accessibility broker.
166  *
167  **/
168 static void
169 impl_accessibility_registry_register_application (PortableServer_Servant servant,
170                                                   const Accessibility_Application application,
171                                                   CORBA_Environment * ev)
172 {
173   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
174
175 #ifdef SPI_DEBUG
176   fprintf (stderr, "registering app %p\n", application);
177 #endif
178   spi_desktop_add_application (registry->desktop, application);
179
180   Accessibility_Application__set_id (application, _get_unique_id (), ev);
181
182   /*
183    * TODO: change the implementation below to a WM-aware one;
184    * e.g. don't add all apps to the SpiDesktop
185    */
186 }
187
188 #ifdef USE_A_HASH_IN_FUTURE
189 static gint
190 compare_corba_objects (gconstpointer p1, gconstpointer p2)
191 {
192   CORBA_Environment ev;
193   gint retval;
194
195 #ifdef SPI_DEBUG
196   fprintf (stderr, "comparing %p to %p\n",
197            p1, p2);
198 #endif
199   
200   retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
201   return retval;  
202 }
203 #endif
204
205 static void
206 register_with_toolkits (SpiRegistry *spi_registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
207 {
208   gint n_desktops;
209   gint n_apps;
210   gint i, j;
211   Accessibility_Desktop desktop;
212   Accessibility_Application app;
213   Accessibility_Registry registry;
214   registry  = BONOBO_OBJREF (spi_registry_bonobo_object);
215
216   /* for each app in each desktop, call ...Application_registerToolkitEventListener */
217
218   n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
219
220   for (i=0; i<n_desktops; ++i)
221     {
222       desktop = Accessibility_Registry_getDesktop (registry, i, ev);
223       n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
224       for (j=0; j<n_apps; ++j)
225         {
226           app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
227                                                                                    j,
228                                                                                    ev);
229           Accessibility_Application_registerToolkitEventListener (app,
230                                                                   registry,
231                                                                   CORBA_string_dup (etype->event_name),
232                                                                   ev);
233         }
234     }
235 }
236
237 #ifdef USE_A_HASH_IN_FUTURE
238
239 static gint
240 compare_listener_quarks (gconstpointer p1, gconstpointer p2)
241 {
242         return (((SpiListenerStruct *)p2)->event_type_quark !=
243                 ((SpiListenerStruct *)p1)->event_type_quark);
244 }
245
246 static gint
247 compare_listener_corbaref (gconstpointer p1, gconstpointer p2)
248 {
249   return compare_corba_objects (((SpiListenerStruct *)p2)->listener,
250                                 ((SpiListenerStruct *)p1)->listener);
251 }
252 #endif
253
254 static void
255 parse_event_type (EventTypeStruct *etype, const char *event_name)
256 {
257   gchar **split_string;
258   gchar *s;
259
260   split_string = g_strsplit (event_name, ":", 4);
261   etype->event_name = event_name;
262
263   if (!g_ascii_strncasecmp (event_name, "focus:", 6))
264     {
265       etype->type_cat = ETYPE_FOCUS;
266     }
267   else if (!g_ascii_strncasecmp (event_name, "mouse:", 6))
268     {
269       etype->type_cat = ETYPE_MOUSE;
270     }
271   else if (!g_ascii_strncasecmp (event_name, "object:", 7))
272     {
273       etype->type_cat = ETYPE_OBJECT;
274     }
275   else if (!g_ascii_strncasecmp (event_name, "window:", 7))
276     {
277       etype->type_cat = ETYPE_WINDOW;
278     }
279   else if (!g_ascii_strncasecmp (event_name, "keyboard:", 9))
280     {
281       etype->type_cat = ETYPE_KEYBOARD;
282     }
283   else
284     {
285       etype->type_cat = ETYPE_TOOLKIT;
286     }
287
288   if (split_string[1])
289     {
290       etype->major = g_quark_from_string (split_string[1]);
291       if (split_string[2])
292         {
293           etype->minor = g_quark_from_string (s = g_strconcat (split_string[1], split_string[2], NULL));
294           g_free (s);
295           if (split_string[3])
296             {
297               etype->detail = g_quark_from_string (split_string[3]);
298             }
299           else
300             {
301               etype->detail = g_quark_from_static_string ("");
302             }
303         }
304       else
305         {
306           etype->minor = etype->major;
307           etype->detail = g_quark_from_static_string (""); //etype->major;
308         }
309     }
310   else
311     {
312       etype->major = g_quark_from_static_string ("");
313       etype->minor = etype->major;
314       etype->detail = etype->major;
315     }
316
317   g_strfreev (split_string);
318 }
319
320 /**
321  * deregisterApplication:
322  * @application: a reference to the @Application
323  * to be deregistered.
324  * return values: void
325  *
326  * De-register an application previously registered with the broker.
327  *
328  **/
329 static void
330 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
331                                                     const Accessibility_Application application,
332                                                     CORBA_Environment * ev)
333 {
334   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
335
336   spi_desktop_remove_application (registry->desktop, application);
337
338 #ifdef SPI_DEBUG
339   fprintf (stderr, "de-registered app %p\n", application);
340 #endif
341 }
342
343 static GList **
344 get_listener_list (SpiRegistry      *registry,
345                    EventTypeCategory cat)
346 {
347   GList **ret;
348   
349   switch (cat)
350     {
351       case ETYPE_OBJECT:
352       case ETYPE_PROPERTY:
353       case ETYPE_FOCUS:
354         ret = &registry->object_listeners;
355         break;
356       case ETYPE_WINDOW:
357         ret = &registry->window_listeners;
358         break;
359       case ETYPE_MOUSE:
360       case ETYPE_TOOLKIT:
361         ret = &registry->toolkit_listeners;
362         break;
363       case ETYPE_KEYBOARD:
364       default:
365         ret = NULL;
366         break;
367     }
368   return ret;
369 }
370
371 /*
372  * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
373  */
374 static void
375 impl_accessibility_registry_register_global_event_listener (
376         PortableServer_Servant      servant,
377         Accessibility_EventListener listener,
378         const CORBA_char           *event_name,
379         CORBA_Environment          *ev)
380 {
381   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant)); 
382   SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
383   EventTypeStruct etype;
384   GList          **list;
385
386 #ifdef SPI_LISTENER_DEBUG
387   fprintf (stderr, "registering for events of type %s\n", event_name);
388 #endif
389
390   /* parse, check major event type and add listener accordingly */
391   parse_event_type (&etype, event_name);
392   ls->event_type_quark = etype.minor;
393   ls->event_type_cat = etype.type_cat;
394
395   list = get_listener_list (registry, etype.type_cat);
396
397   if (list)
398     {
399       *list = g_list_prepend (*list, ls);
400
401       if (etype.type_cat == ETYPE_TOOLKIT)
402         {
403           register_with_toolkits (registry, &etype, ev);
404         }
405     }
406   else
407     {
408       spi_listener_struct_free (ls, ev);
409     }
410 }
411
412 static SpiReEntrantContinue
413 remove_listener_cb (GList * const *list, gpointer user_data)
414 {
415   SpiListenerStruct *ls = (SpiListenerStruct *) (*list)->data;
416   CORBA_Environment  ev;
417   Accessibility_EventListener listener = user_data;
418
419   CORBA_exception_init (&ev);
420         
421   if (CORBA_Object_is_equivalent (ls->listener, listener, &ev))
422     {
423        spi_re_entrant_list_delete_link (list);
424        spi_listener_struct_free (ls, &ev);
425     }
426
427   CORBA_exception_free (&ev);
428
429   return SPI_RE_ENTRANT_CONTINUE;
430 }
431
432 /*
433  * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
434  */
435 static void
436 impl_accessibility_registry_deregister_global_event_listener_all (
437         PortableServer_Servant      servant,
438         Accessibility_EventListener listener,
439         CORBA_Environment          *ev)
440 {
441   int i;
442   GList **lists[3];
443   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
444
445   lists[0] = &registry->object_listeners;
446   lists[1] = &registry->window_listeners;
447   lists[2] = &registry->toolkit_listeners;
448
449   for (i = 0; i < sizeof (lists) / sizeof (lists[0]); i++)
450     {
451       spi_re_entrant_list_foreach (lists [i], remove_listener_cb, listener);
452     }
453 }
454
455
456 /*
457  * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
458  */
459 static void
460 impl_accessibility_registry_deregister_global_event_listener (
461         PortableServer_Servant      servant,
462         Accessibility_EventListener listener,
463         const CORBA_char           *event_name,
464         CORBA_Environment          *ev)
465 {
466   SpiRegistry    *registry;
467   EventTypeStruct etype;
468
469   registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
470
471   parse_event_type (&etype, (char *) event_name);
472
473   spi_re_entrant_list_foreach (get_listener_list (registry, etype.type_cat),
474                                 remove_listener_cb, listener);
475 }
476
477
478 /**
479  * getDesktopCount:
480  * return values: a short integer indicating the current number of
481  * @Desktops.
482  *
483  * Get the current number of desktops.
484  *
485  **/
486 static short
487 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
488                                                CORBA_Environment * ev)
489 {
490   /* TODO: implement support for multiple virtual desktops */
491   CORBA_short n_desktops;
492   n_desktops = (CORBA_short) 1;
493   return n_desktops;
494 }
495
496
497 /**
498  * getDesktop:
499  * @n: the index of the requested @Desktop.
500  * return values: a reference to the requested @Desktop.
501  *
502  * Get the nth accessible desktop.
503  *
504  **/
505 static Accessibility_Desktop
506 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
507                                          const CORBA_short n,
508                                          CORBA_Environment * ev)
509 {
510   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
511
512   /* TODO: implement support for multiple virtual desktops */
513   if (n == 0)
514     {
515       return (Accessibility_Desktop)
516         bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
517     }
518   else
519     {
520       return (Accessibility_Desktop) CORBA_OBJECT_NIL;
521     }
522 }
523
524
525 /**
526  * getDesktopList:
527  * return values: a sequence containing references to
528  * the @Desktops.
529  *
530  * Get a list of accessible desktops.
531  *
532  **/
533 static Accessibility_DesktopSeq *
534 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
535                                               CORBA_Environment * ev)
536 {
537   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
538   Accessibility_DesktopSeq *desktops;
539
540   desktops = Accessibility_DesktopSeq__alloc ();
541   desktops->_length = desktops->_maximum = 1;
542   desktops->_buffer = Accessibility_DesktopSeq_allocbuf (desktops->_length);
543   desktops->_buffer [0] = bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
544
545   return desktops;
546 }
547
548
549 static Accessibility_DeviceEventController
550 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
551                                                          CORBA_Environment     *ev)
552 {
553   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
554
555   if (!registry->de_controller)
556     {
557       registry->de_controller = spi_device_event_controller_new (registry);
558     }
559
560   return bonobo_object_dup_ref (BONOBO_OBJREF (registry->de_controller), ev);
561 }
562
563 typedef struct {
564   CORBA_Environment  *ev;
565   Bonobo_Unknown      source;
566   EventTypeStruct     etype;
567   Accessibility_Event e_out;
568 } NotifyContext;
569
570 static SpiReEntrantContinue
571 notify_listeners_cb (GList * const *list, gpointer user_data)
572 {
573   SpiListenerStruct *ls;
574   NotifyContext     *ctx = user_data;
575 #ifdef SPI_DEBUG
576   CORBA_string       s;
577 #endif
578
579   ls = (*list)->data;
580
581 #ifdef SPI_LISTENER_DEBUG
582   fprintf (stderr, "event quarks: %lx %lx %lx\n", ls->event_type_quark, ctx->etype.major, ctx->etype.minor);
583   fprintf (stderr, "event name: %s\n", ctx->etype.event_name);
584 #endif
585
586   if ((ls->event_type_quark == ctx->etype.major) ||
587       (ls->event_type_quark == ctx->etype.minor))
588     {
589 #ifdef SPI_DEBUG
590       fprintf (stderr, "notifying listener %d\n", 0);
591 /* g_list_index (list, l->data)); */
592       s = Accessibility_Accessible__get_name (ctx->source, ctx->ev);
593       fprintf (stderr, "event source name %s\n", s);
594       CORBA_free (s);
595 #endif
596       
597       ctx->e_out.source = CORBA_Object_duplicate (ctx->source, ctx->ev);
598       if (BONOBO_EX (ctx->ev))
599         {
600           return SPI_RE_ENTRANT_CONTINUE;
601         }
602       
603       if ((*list) && (*list)->data == ls)
604         {
605           Accessibility_EventListener_notifyEvent (
606             (Accessibility_EventListener) ls->listener, &ctx->e_out, ctx->ev);
607           if (ctx->ev->_major != CORBA_NO_EXCEPTION)
608             {
609               g_warning ("Accessibility app error: exception during "
610                         "event notification: %s\n",
611                         CORBA_exception_id (ctx->ev));
612               if (ctx->ev->_major == CORBA_SYSTEM_EXCEPTION)
613                       CORBA_exception_init (ctx->ev);
614               /* clear system exception on notify, it means listener is dead but
615                * that's no concern of the event source :-) */
616             }
617         }
618       else /* dup re-entered */
619         {
620           CORBA_Object_release (ctx->e_out.source, ctx->ev);
621         }
622     }  
623
624   return SPI_RE_ENTRANT_CONTINUE;
625 }
626
627 static void
628 impl_registry_notify_event (PortableServer_Servant     servant,
629                             const Accessibility_Event *e,
630                             CORBA_Environment         *ev)
631 {
632   SpiRegistry  *registry;
633   GList       **list;
634   NotifyContext ctx;
635
636   registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
637
638   parse_event_type (&ctx.etype, e->type);
639
640   list = get_listener_list (registry, ctx.etype.type_cat);
641
642   if (list && *list)
643     {
644       ctx.ev = ev;
645       ctx.e_out = *e;
646       CORBA_any__copy (&ctx.e_out.any_data, &e->any_data);
647       ctx.source = e->source;
648       spi_re_entrant_list_foreach (list, notify_listeners_cb, &ctx);
649     }
650 }
651
652 static void
653 spi_registry_class_init (SpiRegistryClass *klass)
654 {
655   GObjectClass * object_class = (GObjectClass *) klass;
656   POA_Accessibility_Registry__epv *epv = &klass->epv;
657
658   spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
659   
660   object_class->finalize = spi_registry_object_finalize;
661
662   klass->parent_class.epv.notifyEvent   = impl_registry_notify_event;
663   
664   epv->registerApplication              = impl_accessibility_registry_register_application;
665   epv->deregisterApplication            = impl_accessibility_registry_deregister_application;
666   epv->registerGlobalEventListener      = impl_accessibility_registry_register_global_event_listener;
667   epv->deregisterGlobalEventListener    = impl_accessibility_registry_deregister_global_event_listener;
668   epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
669   epv->getDeviceEventController         = impl_accessibility_registry_get_device_event_controller;
670   epv->getDesktopCount                  = impl_accessibility_registry_get_desktop_count;
671   epv->getDesktop                       = impl_accessibility_registry_get_desktop;
672   epv->getDesktopList                   = impl_accessibility_registry_get_desktop_list;
673 }
674
675 static void
676 spi_registry_init (SpiRegistry *registry)
677 {
678   fprintf (stderr, "REGISTRY INITIALIZED: toolkit list = %p\n",
679            &registry->toolkit_listeners);
680   registry->object_listeners = NULL;
681   registry->window_listeners = NULL;
682   registry->toolkit_listeners = NULL;
683   registry->desktop = spi_desktop_new ();
684   /* Register callback notification for application addition and removal */
685   g_signal_connect (G_OBJECT (registry->desktop),
686                     "application_added",
687                     G_CALLBACK (desktop_add_application),
688                     registry);
689
690   g_signal_connect (G_OBJECT (registry->desktop),
691                     "application_removed",
692                     G_CALLBACK (desktop_remove_application),
693                     registry);
694
695   registry->de_controller = spi_device_event_controller_new (registry);
696 }
697
698 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
699                        Accessibility_Registry,
700                        PARENT_TYPE,
701                        spi_registry);
702
703 SpiRegistry *
704 spi_registry_new (void)
705 {
706   SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
707   bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);
708   return retval;
709 }