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