Updated ChangeLog, NEWS, and revved to 1.0.1. Branching for 2-0-0.
[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 #undef SPI_DEBUG
27
28 #include <config.h>
29 #ifdef SPI_DEBUG
30 #  include <stdio.h>
31 #endif
32
33 #include <bonobo/bonobo-exception.h>
34 #include "../libspi/spi-private.h"
35 #include "registry.h"
36
37 /* Our parent GObject type  */
38 #define PARENT_TYPE SPI_LISTENER_TYPE
39
40 /* A pointer to our parent object class */
41 static SpiListenerClass *spi_registry_parent_class;
42
43 typedef enum {
44   ETYPE_FOCUS,
45   ETYPE_OBJECT,
46   ETYPE_PROPERTY,
47   ETYPE_WINDOW,
48   ETYPE_TOOLKIT,
49   ETYPE_KEYBOARD,
50   
51   ETYPE_LAST_DEFINED
52 } EventTypeCategory;
53
54 typedef struct {
55   char *event_name;
56   EventTypeCategory type_cat;
57   GQuark major;  /* from string segment[1] */
58   GQuark minor;  /* from string segment[1]+segment[2] */
59   GQuark detail; /* from string segment[3] (not concatenated) */
60 } EventTypeStruct;
61
62 typedef struct {
63   Accessibility_EventListener listener;
64   GQuark            event_type_quark;
65   EventTypeCategory event_type_cat;
66 } SpiListenerStruct;
67
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
86 static void
87 desktop_add_application (SpiDesktop *desktop,
88                          guint index, gpointer data)
89 {
90   BonoboObject *registry = BONOBO_OBJECT (data);
91   Accessibility_Event e;
92   CORBA_Environment ev;
93   
94   e.type = g_strdup ("object:children-changed:add");
95   e.source = BONOBO_OBJREF (desktop);
96   e.detail1 = index;
97   e.detail2 = 0;
98   CORBA_exception_init (&ev);
99   Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
100                                       &e, &ev);
101   CORBA_exception_free (&ev);
102 }
103
104
105
106 static void
107 desktop_remove_application (SpiDesktop *desktop,
108                             guint index, gpointer data)
109 {
110   BonoboObject *registry = BONOBO_OBJECT (data);
111   Accessibility_Event e;
112   CORBA_Environment ev;
113   
114   e.type = g_strdup ("object:children-changed:remove");
115   e.source = BONOBO_OBJREF (desktop);
116   e.detail1 = index;
117   e.detail2 = 0;
118   CORBA_exception_init (&ev);
119   Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
120                                       &e, &ev);
121   CORBA_exception_free (&ev);
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");
361   fprintf (stderr, "registering for events of type %s\n", event_name);
362 #endif
363
364   /* parse, check major event type and add listener accordingly */
365   parse_event_type (&etype, event_name);
366   ls->event_type_quark = etype.major;
367   ls->event_type_cat = etype.type_cat;
368
369   list = get_listener_list (registry, etype.type_cat);
370
371   if (list)
372     {
373       *list = g_list_prepend (*list, ls);
374
375       if (etype.type_cat == ETYPE_TOOLKIT)
376         {
377           register_with_toolkits (registry, &etype, ev);
378         }
379     }
380   else
381     {
382       spi_listener_struct_free (ls, ev);
383     }
384 }
385
386 static SpiReEntrantContinue
387 remove_listener_cb (GList * const *list, gpointer user_data)
388 {
389   SpiListenerStruct *ls = (SpiListenerStruct *) (*list)->data;
390   CORBA_Environment  ev;
391   Accessibility_EventListener listener = user_data;
392
393   CORBA_exception_init (&ev);
394         
395   if (CORBA_Object_is_equivalent (ls->listener, listener, &ev))
396     {
397        spi_re_entrant_list_delete_link (list);
398        spi_listener_struct_free (ls, &ev);
399     }
400
401   CORBA_exception_free (&ev);
402
403   return SPI_RE_ENTRANT_CONTINUE;
404 }
405
406 /*
407  * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
408  */
409 static void
410 impl_accessibility_registry_deregister_global_event_listener_all (
411         PortableServer_Servant      servant,
412         Accessibility_EventListener listener,
413         CORBA_Environment          *ev)
414 {
415   int i;
416   GList **lists[2];
417   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
418
419   lists[0] = &registry->object_listeners;
420   lists[1] = &registry->window_listeners;
421   lists[2] = &registry->toolkit_listeners;
422
423   for (i = 0; i < sizeof (lists) / sizeof (lists[0]); i++)
424     {
425       spi_re_entrant_list_foreach (lists [i], remove_listener_cb, listener);
426     }
427 }
428
429
430 /*
431  * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
432  */
433 static void
434 impl_accessibility_registry_deregister_global_event_listener (
435         PortableServer_Servant      servant,
436         Accessibility_EventListener listener,
437         const CORBA_char           *event_name,
438         CORBA_Environment          *ev)
439 {
440   SpiRegistry    *registry;
441   EventTypeStruct etype;
442
443   registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
444
445   parse_event_type (&etype, (char *) event_name);
446
447   spi_re_entrant_list_foreach (get_listener_list (registry, etype.type_cat),
448                                 remove_listener_cb, listener);
449 }
450
451
452 /**
453  * getDesktopCount:
454  * return values: a short integer indicating the current number of
455  * @Desktops.
456  *
457  * Get the current number of desktops.
458  *
459  **/
460 static short
461 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
462                                                CORBA_Environment * ev)
463 {
464   /* TODO: implement support for multiple virtual desktops */
465   CORBA_short n_desktops;
466   n_desktops = (CORBA_short) 1;
467   return n_desktops;
468 }
469
470
471 /**
472  * getDesktop:
473  * @n: the index of the requested @Desktop.
474  * return values: a reference to the requested @Desktop.
475  *
476  * Get the nth accessible desktop.
477  *
478  **/
479 static Accessibility_Desktop
480 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
481                                          const CORBA_short n,
482                                          CORBA_Environment * ev)
483 {
484   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
485
486   /* TODO: implement support for multiple virtual desktops */
487   if (n == 0)
488     {
489       return (Accessibility_Desktop)
490         bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
491     }
492   else
493     {
494       return (Accessibility_Desktop) CORBA_OBJECT_NIL;
495     }
496 }
497
498
499 /**
500  * getDesktopList:
501  * return values: a sequence containing references to
502  * the @Desktops.
503  *
504  * Get a list of accessible desktops.
505  *
506  **/
507 static Accessibility_DesktopSeq *
508 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
509                                               CORBA_Environment * ev)
510 {
511   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
512   Accessibility_DesktopSeq *desktops;
513
514   desktops = Accessibility_DesktopSeq__alloc ();
515   desktops->_length = desktops->_maximum = 1;
516   desktops->_buffer = Accessibility_DesktopSeq_allocbuf (desktops->_length);
517   desktops->_buffer [0] = bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
518
519   return desktops;
520 }
521
522
523 static Accessibility_DeviceEventController
524 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
525                                                          CORBA_Environment     *ev)
526 {
527   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
528
529   if (!registry->de_controller)
530     {
531       registry->de_controller = spi_device_event_controller_new (registry);
532     }
533
534   return bonobo_object_dup_ref (BONOBO_OBJREF (registry->de_controller), ev);
535 }
536
537 typedef struct {
538   CORBA_Environment  *ev;
539   Bonobo_Unknown      source;
540   EventTypeStruct     etype;
541   Accessibility_Event e_out;
542 } NotifyContext;
543
544 static SpiReEntrantContinue
545 notify_listeners_cb (GList * const *list, gpointer user_data)
546 {
547   SpiListenerStruct *ls;
548   NotifyContext     *ctx = user_data;
549 #ifdef SPI_DEBUG
550   CORBA_string       s;
551 #endif
552
553   ls = (*list)->data;
554
555 #ifdef SPI_LISTENER_DEBUG
556   fprintf (stderr, "event quarks: %lx %lx %lx\n", ls->event_type_quark, ctx->etype.major, ctx->etype.minor);
557   fprintf (stderr, "event name: %s\n", ctx->etype.event_name);
558 #endif
559
560   if ((ls->event_type_quark == ctx->etype.major) ||
561       (ls->event_type_quark == ctx->etype.minor))
562     {
563 #ifdef SPI_DEBUG
564       fprintf (stderr, "notifying listener %d\n", 0);
565 /* g_list_index (list, l->data)); */
566       s = Accessibility_Accessible__get_name (ctx->source, ctx->ev);
567       fprintf (stderr, "event source name %s\n", s);
568       CORBA_free (s);
569 #endif
570       
571       ctx->e_out.source = bonobo_object_dup_ref (ctx->source, ctx->ev);
572       if (BONOBO_EX (ctx->ev))
573         {
574           return SPI_RE_ENTRANT_CONTINUE;;
575         }
576
577       if ((*list) && (*list)->data == ls)
578         {
579           Accessibility_EventListener_notifyEvent (
580             (Accessibility_EventListener) ls->listener, &ctx->e_out, ctx->ev);
581         if (ctx->ev->_major != CORBA_NO_EXCEPTION)
582           {
583             g_warning ("Accessibility app error: exception during "
584                        "event notification: %s\n",
585                        CORBA_exception_id (ctx->ev));
586           }
587         }
588       else /* dup re-entered */
589         {
590           bonobo_object_release_unref (ctx->e_out.source, ctx->ev);
591         }
592     }  
593
594   return SPI_RE_ENTRANT_CONTINUE;
595 }
596
597 static void
598 impl_registry_notify_event (PortableServer_Servant     servant,
599                             const Accessibility_Event *e,
600                             CORBA_Environment         *ev)
601 {
602   SpiRegistry  *registry;
603   GList       **list;
604   NotifyContext ctx;
605
606   registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
607
608   parse_event_type (&ctx.etype, e->type);
609
610   list = get_listener_list (registry, ctx.etype.type_cat);
611
612   if (list)
613     {
614       ctx.ev = ev;
615       ctx.e_out = *e;
616       ctx.source = e->source;
617       parse_event_type (&ctx.etype, e->type);
618
619       spi_re_entrant_list_foreach (list, notify_listeners_cb, &ctx);
620     }
621
622   if (e->source != CORBA_OBJECT_NIL)
623     {
624       Accessibility_Accessible_unref (e->source, ev);
625     }
626 }
627
628
629 static void
630 spi_registry_class_init (SpiRegistryClass *klass)
631 {
632   GObjectClass * object_class = (GObjectClass *) klass;
633   POA_Accessibility_Registry__epv *epv = &klass->epv;
634
635   spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
636   
637   object_class->finalize = spi_registry_object_finalize;
638
639   klass->parent_class.epv.notifyEvent   = impl_registry_notify_event;
640   
641   epv->registerApplication              = impl_accessibility_registry_register_application;
642   epv->deregisterApplication            = impl_accessibility_registry_deregister_application;
643   epv->registerGlobalEventListener      = impl_accessibility_registry_register_global_event_listener;
644   epv->deregisterGlobalEventListener    = impl_accessibility_registry_deregister_global_event_listener;
645   epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
646   epv->getDeviceEventController         = impl_accessibility_registry_get_device_event_controller;
647   epv->getDesktopCount                  = impl_accessibility_registry_get_desktop_count;
648   epv->getDesktop                       = impl_accessibility_registry_get_desktop;
649   epv->getDesktopList                   = impl_accessibility_registry_get_desktop_list;
650 }
651
652 static void
653 spi_registry_init (SpiRegistry *registry)
654 {
655   registry->object_listeners = NULL;
656   registry->window_listeners = NULL;
657   registry->toolkit_listeners = NULL;
658   registry->desktop = spi_desktop_new ();
659   /* Register callback notification for application addition and removal */
660   g_signal_connect (G_OBJECT (registry->desktop),
661                     "application_added",
662                     G_CALLBACK (desktop_add_application),
663                     registry);
664
665   g_signal_connect (G_OBJECT (registry->desktop),
666                     "application_removed",
667                     G_CALLBACK (desktop_remove_application),
668                     registry);
669
670   registry->de_controller = NULL;
671 }
672
673 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
674                        Accessibility_Registry,
675                        PARENT_TYPE,
676                        spi_registry);
677
678 SpiRegistry *
679 spi_registry_new (void)
680 {
681   SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
682   bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);
683   return retval;
684 }