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