* registryd/Makefile: Add CLEANFILES so that .server file is removed
[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   
93   e.type = "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   Accessibility_Desktop_unref (e.source, &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 = "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   Accessibility_Desktop_unref (e.source, &ev);
122   CORBA_exception_free (&ev);
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 = 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, "mouse:", 6))
251     {
252       etype->type_cat = ETYPE_MOUSE;
253     }
254   else if (!g_ascii_strncasecmp (event_name, "object:", 7))
255     {
256       etype->type_cat = ETYPE_OBJECT;
257     }
258   else if (!g_ascii_strncasecmp (event_name, "window:", 7))
259     {
260       etype->type_cat = ETYPE_WINDOW;
261     }
262   else
263     {
264       etype->type_cat = ETYPE_TOOLKIT;
265     }
266
267   if (split_string[1])
268     {
269       etype->major = g_quark_from_string (split_string[1]);
270       if (split_string[2])
271         {
272           etype->minor = g_quark_from_string (s = g_strconcat (split_string[1], split_string[2], NULL));
273           g_free (s);
274           if (split_string[3])
275             {
276               etype->detail = g_quark_from_string (split_string[3]);
277             }
278           else
279             {
280               etype->detail = g_quark_from_static_string ("");
281             }
282         }
283       else
284         {
285           etype->minor = etype->major;
286           etype->detail = g_quark_from_static_string (""); //etype->major;
287         }
288     }
289   else
290     {
291       etype->major = g_quark_from_static_string ("");
292       etype->minor = etype->major;
293       etype->detail = etype->major;
294     }
295
296   g_strfreev (split_string);
297 }
298
299 /**
300  * deregisterApplication:
301  * @application: a reference to the @Application
302  * to be deregistered.
303  * return values: void
304  *
305  * De-register an application previously registered with the broker.
306  *
307  **/
308 static void
309 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
310                                                     const Accessibility_Application application,
311                                                     CORBA_Environment * ev)
312 {
313   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
314
315   spi_desktop_remove_application (registry->desktop, application);
316
317 #ifdef SPI_DEBUG
318   fprintf (stderr, "de-registered app %p\n", application);
319 #endif
320 }
321
322 static GList **
323 get_listener_list (SpiRegistry      *registry,
324                    EventTypeCategory cat)
325 {
326   GList **ret;
327   
328   switch (cat)
329     {
330       case ETYPE_OBJECT:
331       case ETYPE_PROPERTY:
332       case ETYPE_FOCUS:
333         ret = &registry->object_listeners;
334         break;
335       case ETYPE_WINDOW:
336         ret = &registry->window_listeners;
337         break;
338       case ETYPE_MOUSE:
339       case ETYPE_TOOLKIT:
340         ret = &registry->toolkit_listeners;
341         break;
342       case ETYPE_KEYBOARD:
343       default:
344         ret = NULL;
345         break;
346     }
347   return ret;
348 }
349
350 /*
351  * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
352  */
353 static void
354 impl_accessibility_registry_register_global_event_listener (
355         PortableServer_Servant      servant,
356         Accessibility_EventListener listener,
357         const CORBA_char           *event_name,
358         CORBA_Environment          *ev)
359 {
360   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant)); 
361   SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
362   EventTypeStruct etype;
363   GList          **list;
364
365 #ifdef SPI_LISTENER_DEBUG
366   fprintf (stderr, "registering");
367   fprintf (stderr, "registering for events of type %s\n", event_name);
368 #endif
369
370   /* parse, check major event type and add listener accordingly */
371   parse_event_type (&etype, event_name);
372   ls->event_type_quark = etype.minor;
373   ls->event_type_cat = etype.type_cat;
374
375   list = get_listener_list (registry, etype.type_cat);
376
377   if (list)
378     {
379       *list = g_list_prepend (*list, ls);
380
381       if (etype.type_cat == ETYPE_TOOLKIT)
382         {
383           register_with_toolkits (registry, &etype, ev);
384         }
385     }
386   else
387     {
388       spi_listener_struct_free (ls, ev);
389     }
390 }
391
392 static SpiReEntrantContinue
393 remove_listener_cb (GList * const *list, gpointer user_data)
394 {
395   SpiListenerStruct *ls = (SpiListenerStruct *) (*list)->data;
396   CORBA_Environment  ev;
397   Accessibility_EventListener listener = user_data;
398
399   CORBA_exception_init (&ev);
400         
401   if (CORBA_Object_is_equivalent (ls->listener, listener, &ev))
402     {
403        spi_re_entrant_list_delete_link (list);
404        spi_listener_struct_free (ls, &ev);
405     }
406
407   CORBA_exception_free (&ev);
408
409   return SPI_RE_ENTRANT_CONTINUE;
410 }
411
412 /*
413  * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
414  */
415 static void
416 impl_accessibility_registry_deregister_global_event_listener_all (
417         PortableServer_Servant      servant,
418         Accessibility_EventListener listener,
419         CORBA_Environment          *ev)
420 {
421   int i;
422   GList **lists[2];
423   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
424
425   lists[0] = &registry->object_listeners;
426   lists[1] = &registry->window_listeners;
427   lists[2] = &registry->toolkit_listeners;
428
429   for (i = 0; i < sizeof (lists) / sizeof (lists[0]); i++)
430     {
431       spi_re_entrant_list_foreach (lists [i], remove_listener_cb, listener);
432     }
433 }
434
435
436 /*
437  * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
438  */
439 static void
440 impl_accessibility_registry_deregister_global_event_listener (
441         PortableServer_Servant      servant,
442         Accessibility_EventListener listener,
443         const CORBA_char           *event_name,
444         CORBA_Environment          *ev)
445 {
446   SpiRegistry    *registry;
447   EventTypeStruct etype;
448
449   registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
450
451   parse_event_type (&etype, (char *) event_name);
452
453   spi_re_entrant_list_foreach (get_listener_list (registry, etype.type_cat),
454                                 remove_listener_cb, listener);
455 }
456
457
458 /**
459  * getDesktopCount:
460  * return values: a short integer indicating the current number of
461  * @Desktops.
462  *
463  * Get the current number of desktops.
464  *
465  **/
466 static short
467 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
468                                                CORBA_Environment * ev)
469 {
470   /* TODO: implement support for multiple virtual desktops */
471   CORBA_short n_desktops;
472   n_desktops = (CORBA_short) 1;
473   return n_desktops;
474 }
475
476
477 /**
478  * getDesktop:
479  * @n: the index of the requested @Desktop.
480  * return values: a reference to the requested @Desktop.
481  *
482  * Get the nth accessible desktop.
483  *
484  **/
485 static Accessibility_Desktop
486 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
487                                          const CORBA_short n,
488                                          CORBA_Environment * ev)
489 {
490   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
491
492   /* TODO: implement support for multiple virtual desktops */
493   if (n == 0)
494     {
495       return (Accessibility_Desktop)
496         bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
497     }
498   else
499     {
500       return (Accessibility_Desktop) CORBA_OBJECT_NIL;
501     }
502 }
503
504
505 /**
506  * getDesktopList:
507  * return values: a sequence containing references to
508  * the @Desktops.
509  *
510  * Get a list of accessible desktops.
511  *
512  **/
513 static Accessibility_DesktopSeq *
514 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
515                                               CORBA_Environment * ev)
516 {
517   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
518   Accessibility_DesktopSeq *desktops;
519
520   desktops = Accessibility_DesktopSeq__alloc ();
521   desktops->_length = desktops->_maximum = 1;
522   desktops->_buffer = Accessibility_DesktopSeq_allocbuf (desktops->_length);
523   desktops->_buffer [0] = bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
524
525   return desktops;
526 }
527
528
529 static Accessibility_DeviceEventController
530 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
531                                                          CORBA_Environment     *ev)
532 {
533   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
534
535   if (!registry->de_controller)
536     {
537       registry->de_controller = spi_device_event_controller_new (registry);
538     }
539
540   return bonobo_object_dup_ref (BONOBO_OBJREF (registry->de_controller), ev);
541 }
542
543 typedef struct {
544   CORBA_Environment  *ev;
545   Bonobo_Unknown      source;
546   EventTypeStruct     etype;
547   Accessibility_Event e_out;
548 } NotifyContext;
549
550 static SpiReEntrantContinue
551 notify_listeners_cb (GList * const *list, gpointer user_data)
552 {
553   SpiListenerStruct *ls;
554   NotifyContext     *ctx = user_data;
555 #ifdef SPI_DEBUG
556   CORBA_string       s;
557 #endif
558
559   ls = (*list)->data;
560
561 #ifdef SPI_LISTENER_DEBUG
562   fprintf (stderr, "event quarks: %lx %lx %lx\n", ls->event_type_quark, ctx->etype.major, ctx->etype.minor);
563   fprintf (stderr, "event name: %s\n", ctx->etype.event_name);
564 #endif
565
566   if ((ls->event_type_quark == ctx->etype.major) ||
567       (ls->event_type_quark == ctx->etype.minor))
568     {
569 #ifdef SPI_DEBUG
570       fprintf (stderr, "notifying listener %d\n", 0);
571 /* g_list_index (list, l->data)); */
572       s = Accessibility_Accessible__get_name (ctx->source, ctx->ev);
573       fprintf (stderr, "event source name %s\n", s);
574       CORBA_free (s);
575 #endif
576       
577       ctx->e_out.source = CORBA_Object_duplicate (ctx->source, ctx->ev);
578
579       if (BONOBO_EX (ctx->ev))
580         {
581           return SPI_RE_ENTRANT_CONTINUE;
582         }
583       
584       if ((*list) && (*list)->data == ls)
585         {
586           Accessibility_EventListener_notifyEvent (
587             (Accessibility_EventListener) ls->listener, &ctx->e_out, ctx->ev);
588           if (ctx->ev->_major != CORBA_NO_EXCEPTION)
589             {
590               g_warning ("Accessibility app error: exception during "
591                         "event notification: %s\n",
592                         CORBA_exception_id (ctx->ev));
593               if (ctx->ev->_major == CORBA_SYSTEM_EXCEPTION)
594                       CORBA_exception_init (ctx->ev);
595               /* clear system exception on notify, it means listener is dead but
596                * that's no concern of the event source :-) */
597             }
598         }
599       else /* dup re-entered */
600         {
601           CORBA_Object_release (ctx->e_out.source, ctx->ev);
602         }
603     }  
604
605   return SPI_RE_ENTRANT_CONTINUE;
606 }
607
608 static void
609 impl_registry_notify_event (PortableServer_Servant     servant,
610                             const Accessibility_Event *e,
611                             CORBA_Environment         *ev)
612 {
613   SpiRegistry  *registry;
614   GList       **list;
615   NotifyContext ctx;
616
617   registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
618
619   parse_event_type (&ctx.etype, e->type);
620
621   list = get_listener_list (registry, ctx.etype.type_cat);
622
623   if (list)
624     {
625       ctx.ev = ev;
626       ctx.e_out = *e;
627       ctx.source = e->source;
628
629       spi_re_entrant_list_foreach (list, notify_listeners_cb, &ctx);
630     }
631
632 }
633
634
635 static void
636 spi_registry_class_init (SpiRegistryClass *klass)
637 {
638   GObjectClass * object_class = (GObjectClass *) klass;
639   POA_Accessibility_Registry__epv *epv = &klass->epv;
640
641   spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
642   
643   object_class->finalize = spi_registry_object_finalize;
644
645   klass->parent_class.epv.notifyEvent   = impl_registry_notify_event;
646   
647   epv->registerApplication              = impl_accessibility_registry_register_application;
648   epv->deregisterApplication            = impl_accessibility_registry_deregister_application;
649   epv->registerGlobalEventListener      = impl_accessibility_registry_register_global_event_listener;
650   epv->deregisterGlobalEventListener    = impl_accessibility_registry_deregister_global_event_listener;
651   epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
652   epv->getDeviceEventController         = impl_accessibility_registry_get_device_event_controller;
653   epv->getDesktopCount                  = impl_accessibility_registry_get_desktop_count;
654   epv->getDesktop                       = impl_accessibility_registry_get_desktop;
655   epv->getDesktopList                   = impl_accessibility_registry_get_desktop_list;
656 }
657
658 static void
659 spi_registry_init (SpiRegistry *registry)
660 {
661   registry->object_listeners = NULL;
662   registry->window_listeners = NULL;
663   registry->toolkit_listeners = NULL;
664   registry->desktop = spi_desktop_new ();
665   /* Register callback notification for application addition and removal */
666   g_signal_connect (G_OBJECT (registry->desktop),
667                     "application_added",
668                     G_CALLBACK (desktop_add_application),
669                     registry);
670
671   g_signal_connect (G_OBJECT (registry->desktop),
672                     "application_removed",
673                     G_CALLBACK (desktop_remove_application),
674                     registry);
675
676   registry->de_controller = spi_device_event_controller_new (registry);
677 }
678
679 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
680                        Accessibility_Registry,
681                        PARENT_TYPE,
682                        spi_registry);
683
684 SpiRegistry *
685 spi_registry_new (void)
686 {
687   SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
688   bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);
689   return retval;
690 }