Completed de-registration fix.
[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 /*
24  * registry.c: the main accessibility service registry implementation
25  */
26
27 #ifdef SPI_DEBUG
28 #include <stdio.h>
29 #endif
30 #include <config.h>
31 #include <bonobo/Bonobo.h>
32
33 /*
34  * This pulls the CORBA definitions for the "Accessibility::Registry" server
35  */
36 #include <libspi/Accessibility.h>
37
38 /*
39  * This pulls the definition for the BonoboObject (GType)
40  */
41 #include "registry.h"
42
43 /*
44  * Our parent GObject type
45  */
46 #define PARENT_TYPE LISTENER_TYPE
47
48 /*
49  * A pointer to our parent object class
50  */
51 static ListenerClass *registry_parent_class;
52
53 typedef enum {
54   ETYPE_FOCUS,
55   ETYPE_WINDOW,
56   ETYPE_TOOLKIT,
57   ETYPE_LAST_DEFINED
58 } EventTypeCategory;
59
60 typedef struct {
61   char *event_name;
62   EventTypeCategory type_cat;
63   char * major;
64   char * minor;
65   char * detail;
66   guint hash;
67 } EventTypeStruct;
68
69 typedef struct {
70   Accessibility_EventListener listener;
71   guint event_type_hash;
72   EventTypeCategory event_type_cat;
73 } ListenerStruct;
74
75 /* static function prototypes */
76 static void _registry_notify_listeners ( GList *listeners,
77                                         const Accessibility_Event *e,
78                                         CORBA_Environment *ev);
79
80 static long _get_unique_id();
81
82 /*
83  * Implemented GObject::finalize
84  */
85 static void
86 registry_object_finalize (GObject *object)
87 {
88 /*        Registry *registry = REGISTRY (object); */
89         GObjectClass *object_class = G_OBJECT_GET_CLASS( object);
90
91         printf("registry_object_finalize called\n");
92
93         object_class->finalize (object);
94 }
95
96 /**
97  * registerApplication:
98  * @application: a reference to the requesting @Application
99  * return values: void
100  *
101  * Register a new application with the accessibility broker.
102  *
103  **/
104 static void
105 impl_accessibility_registry_register_application (PortableServer_Servant servant,
106                                                   const Accessibility_Application application,
107                                                   CORBA_Environment * ev)
108 {
109   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
110
111 #ifdef SPI_DEBUG
112   fprintf (stderr, "registering app %p\n", application);
113 #endif
114   registry->desktop->applications = g_list_append (registry->desktop->applications,
115                                                    CORBA_Object_duplicate (application, ev));
116
117   /* TODO: create unique string here (with libuuid call ?) and hash ? */
118   Accessibility_Application__set_id (application, _get_unique_id(), ev);
119
120   /*
121    * TODO: change the implementation below to a WM-aware one;
122    * e.g. don't add all apps to the Desktop
123    */
124 }
125
126 static gint
127 compare_object_hash (gconstpointer p1, gconstpointer p2)
128 {
129   CORBA_Environment ev;
130   gint retval;
131   retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
132
133 #ifdef SPI_DEBUG
134   fprintf (stderr, "comparing %p to %p; result %d\n",
135            p1, p2,
136            retval);
137 #endif
138   return retval;  
139 }
140
141 static void
142 register_with_toolkits (Registry *registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
143 {
144   gint n_desktops;
145   gint n_apps;
146   gint i, j;
147   Accessibility_Desktop desktop;
148   Accessibility_Application app;
149   Accessibility_Registry registry;
150   registry  = bonobo_object_corba_objref (bonobo_object (registry_bonobo_object));
151
152   /* for each app in each desktop, call ...Application_registerToolkitEventListener */
153
154   n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
155
156   for (i=0; i<n_desktops; ++i)
157     {
158       desktop = Accessibility_Registry_getDesktop (registry, i, ev);
159       n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
160       for (j=0; j<n_apps; ++j)
161         {
162           app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
163                                                                                    j,
164                                                                                    ev);
165           /* TODO: should we be ref-ing the registry object before each call ? */
166           Accessibility_Application_registerToolkitEventListener (app,
167                                                                   registry,
168                                                                   CORBA_string_dup (etype->event_name),
169                                                                   ev);
170         }
171     }
172 }
173
174 static gint
175 compare_listener_hash (gconstpointer p1, gconstpointer p2)
176 {
177   return (((ListenerStruct *)p2)->event_type_hash - ((ListenerStruct *)p1)->event_type_hash);
178 }
179
180 static void
181 parse_event_type (EventTypeStruct *etype, char *event_name)
182 {
183   guint nbytes = 0;
184   gchar **split_string;
185
186   split_string = g_strsplit(event_name, ":", 4);
187   etype->event_name = g_strndup(event_name, 255);
188
189   if (!g_ascii_strncasecmp (event_name, "focus:", 6))
190     {
191       etype->type_cat = ETYPE_FOCUS;
192     }
193   else if (!g_ascii_strncasecmp (event_name, "window:", 7))
194     {
195       etype->type_cat = ETYPE_WINDOW;
196     }
197   else
198     {
199       etype->type_cat = ETYPE_TOOLKIT;
200     }
201
202   if (split_string[1])
203     {
204       etype->major = split_string[1];
205       if (split_string[2])
206         {
207           etype->minor = split_string[2];
208           if (split_string[3])
209             {
210               etype->detail = split_string[3];
211               etype->hash = g_str_hash ( g_strconcat (split_string[1], split_string[2], split_string[3], NULL));
212             }
213           else
214             {
215               etype->detail = g_strdup ("");
216               etype->hash = g_str_hash ( g_strconcat (split_string[1], split_string[2], NULL));
217             }
218         }
219       else
220         {
221           etype->minor = g_strdup ("");
222           etype->hash = g_str_hash ( split_string[1]);
223         }
224     }
225   else
226     {
227       etype->major = g_strdup ("");
228       etype->minor = g_strdup ("");
229       etype->detail = g_strdup ("");
230       etype->hash = g_str_hash ("");
231     }
232
233   /* TODO: don't forget to free the strings from caller when done ! */
234 }
235
236 /**
237  * deregisterApplication:
238  * @application: a reference to the @Application
239  * to be deregistered.
240  * return values: void
241  *
242  * De-register an application previously registered with the broker.
243  *
244  **/
245 static void
246 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
247                                                     const Accessibility_Application application,
248                                                     CORBA_Environment * ev)
249 {
250   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
251   GList *list = g_list_find_custom (registry->desktop->applications, application, compare_object_hash);
252
253 #ifdef SPI_DEBUG
254   gint i;
255 #endif
256
257   if (list)
258     {
259 #ifdef SPI_DEBUG
260       fprintf (stderr, "deregistering application %p\n", application);
261 #endif
262       registry->desktop->applications = g_list_delete_link (registry->desktop->applications, list);
263 #ifdef SPI_DEBUG
264       fprintf (stderr, "there are now %d apps registered.\n", g_list_length (registry->desktop->applications));
265       for (i = 0; i < g_list_length (registry->desktop->applications); ++i) {
266           fprintf (stderr, "getting application %d\n", i);
267           fprintf (stderr, "object address %p\n",
268                g_list_nth_data (registry->desktop->applications, i));
269       }
270 #endif      
271     }
272   else
273     fprintf (stderr, "could not deregister application\n");
274 }
275
276 /*
277  * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
278  */
279 static void
280 impl_accessibility_registry_register_global_event_listener
281                                             (PortableServer_Servant  servant,
282                                              Accessibility_EventListener listener,
283                                              const CORBA_char *event_name,
284                                              CORBA_Environment      *ev)
285 {
286   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
287   ListenerStruct *ls = g_malloc (sizeof (ListenerStruct));
288   EventTypeStruct etype;
289
290   fprintf(stderr, "registering for events of type %s\n", event_name);
291
292   /* parse, check major event type and add listener accordingly */
293   parse_event_type (&etype, event_name);
294   ls->event_type_hash = etype.hash;
295   ls->event_type_cat = etype.type_cat;
296
297   switch (etype.type_cat)
298     {
299     case (ETYPE_FOCUS) :
300       ls->listener = CORBA_Object_duplicate (listener, ev);
301       registry->focus_listeners =
302         g_list_append (registry->focus_listeners, ls);
303       break;
304     case (ETYPE_WINDOW) :
305       /* Support for Window Manager Events is not yet implemented */
306       break;
307     case (ETYPE_TOOLKIT) :
308       ls->listener = CORBA_Object_duplicate (listener, ev);
309       registry->toolkit_listeners =
310         g_list_append (registry->toolkit_listeners, ls);
311       register_with_toolkits (registry, &etype, ev);
312       break;
313     default:
314       break;
315     }
316 }
317
318 /*
319  * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
320  */
321 static void
322 impl_accessibility_registry_deregister_global_event_listener_all
323                                                    (PortableServer_Servant  servant,
324                                                     Accessibility_EventListener listener,
325                                                     CORBA_Environment      *ev)
326 {
327   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
328   GList *list = g_list_find_custom (registry->focus_listeners, listener, compare_object_hash);
329
330   /*
331    * TODO : de-register with toolkit if the last instance of a listener
332    *        to a particular toolkit event type has been deregistered.
333    */
334
335   while (list)
336     {
337       fprintf (stderr, "deregistering listener\n");
338       registry->focus_listeners = g_list_delete_link (registry->focus_listeners, list);
339       list = g_list_find_custom (registry->focus_listeners, listener, compare_object_hash);
340     }
341   list = g_list_find_custom (registry->toolkit_listeners, listener, compare_object_hash);
342   while (list)
343     {
344       fprintf (stderr, "deregistering listener\n");
345       registry->toolkit_listeners = g_list_delete_link (registry->toolkit_listeners, list);
346       list = g_list_find_custom (registry->toolkit_listeners, listener, compare_object_hash);
347     }
348 }
349
350 /*
351  * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
352  */
353 static void
354 impl_accessibility_registry_deregister_global_event_listener
355                                                    (PortableServer_Servant  servant,
356                                                     Accessibility_EventListener listener,
357                                                     const CORBA_char * event_name,
358                                                     CORBA_Environment      *ev)
359 {
360   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
361   ListenerStruct ls;
362   EventTypeStruct etype;
363   GList *list;
364   GList **listeners;
365
366   parse_event_type (&etype, event_name);
367   switch (etype.type_cat)
368     {
369     case (ETYPE_FOCUS) :
370       listeners = &registry->focus_listeners;
371       break;
372     case (ETYPE_WINDOW) :
373       /* Support for Window Manager Events is not yet implemented */
374       break;
375     case (ETYPE_TOOLKIT) :
376       listeners = &registry->toolkit_listeners;
377       break;
378     default:
379       break;
380     }
381
382   ls.event_type_hash = etype.hash;
383   list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
384
385   while (list)
386     {
387       fprintf (stderr, "deregistering listener\n");
388       *listeners = g_list_delete_link (*listeners, list);
389       list = g_list_find_custom (*listeners, &ls, compare_listener_hash);
390     }
391 }
392
393
394 /**
395  * getDesktopCount:
396  * return values: a short integer indicating the current number of
397  * @Desktops.
398  *
399  * Get the current number of desktops.
400  *
401  **/
402 static short
403 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
404                                                CORBA_Environment * ev)
405 {
406   /* TODO: implement support for multiple virtual desktops */
407   CORBA_short n_desktops;
408   n_desktops = (CORBA_short) 1;
409   return n_desktops;
410 }
411
412 /**
413  * getDesktop:
414  * @n: the index of the requested @Desktop.
415  * return values: a reference to the requested @Desktop.
416  *
417  * Get the nth accessible desktop.
418  *
419  **/
420 static Accessibility_Desktop
421 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
422                                          const CORBA_short n,
423                                          CORBA_Environment * ev)
424 {
425   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
426
427   /* TODO: implement support for multiple virtual desktops */
428   if (n == 0)
429     {
430       return (Accessibility_Desktop)
431         CORBA_Object_duplicate (
432              bonobo_object_corba_objref (bonobo_object (registry->desktop)), ev);
433     }
434   else
435     {
436       return (Accessibility_Desktop) CORBA_OBJECT_NIL;
437     }
438 }
439
440 /**
441  * getDesktopList:
442  * return values: a sequence containing references to
443  * the @Desktops.
444  *
445  * Get a list of accessible desktops.
446  *
447  **/
448 static Accessibility_DesktopSeq *
449 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
450                                               CORBA_Environment * ev)
451 {
452   /* TODO: implement support for multiple virtual desktops */
453   return (Accessibility_DesktopSeq *) NULL;
454 }
455
456 static CORBA_Object
457 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
458                                                          CORBA_Environment * ev)
459 {
460   /* TODO: not yet implemented! */
461   return CORBA_OBJECT_NIL;
462 }
463
464 static void
465 impl_registry_notify_event (PortableServer_Servant servant,
466                             const Accessibility_Event *e,
467                             CORBA_Environment *ev)
468 {
469   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
470   EventTypeStruct etype;
471
472   parse_event_type (&etype, e->type);
473
474   switch (etype.type_cat)
475     {
476     case (ETYPE_FOCUS) :
477       _registry_notify_listeners (registry->focus_listeners, e, ev);
478       break;
479     case (ETYPE_WINDOW) :
480       _registry_notify_listeners (registry->window_listeners, e, ev);
481       break;
482     case (ETYPE_TOOLKIT) :
483       _registry_notify_listeners (registry->toolkit_listeners, e, ev);
484       break;
485     default:
486       break;
487     }
488  Accessibility_Accessible_unref (e->source, ev);
489 }
490
491 static long
492 _get_unique_id ()
493 {
494   static long id = 0;
495   return ++id;
496 }
497
498 static void
499 _registry_notify_listeners ( GList *listeners,
500                             const Accessibility_Event *e,
501                             CORBA_Environment *ev)
502 {
503   int n;
504   int len;
505   ListenerStruct *ls;
506   EventTypeStruct etype;
507   guint minor_hash;
508   parse_event_type (&etype, e->type);
509   minor_hash = g_str_hash (g_strconcat (etype.major, etype.minor, NULL));
510   len = g_list_length (listeners);
511
512   for (n=0; n<len; ++n)
513     {
514       ls =  (ListenerStruct *) g_list_nth_data (listeners, n);
515 #ifdef SPI_DEBUG
516       fprintf(stderr, "event hashes: %lx %lx %lx\n", ls->event_type_hash, etype.hash, minor_hash);
517       fprintf(stderr, "event name: %s\n", etype.event_name);
518 #endif
519       if ((ls->event_type_hash == etype.hash) || (ls->event_type_hash == minor_hash))
520         {
521 #ifdef SPI_DEBUG
522           fprintf(stderr, "notifying listener #%d\n", n);
523           fprintf(stderr, "event name %s\n", Accessibility_Accessible__get_name(e->source, ev));
524 #endif
525           Accessibility_Accessible_ref ( e->source, ev);
526           Accessibility_EventListener_notifyEvent ((Accessibility_EventListener) ls->listener,
527                                                    e,
528                                                    ev);
529           if (ev->_major != CORBA_NO_EXCEPTION) {
530                 fprintf(stderr,
531                 ("Accessibility app error: exception during event notification: %s\n"),
532                         CORBA_exception_id(ev));
533                 exit(-1);
534           }
535         }
536     }
537 }
538
539 static void
540 registry_class_init (RegistryClass *klass)
541 {
542         GObjectClass * object_class = (GObjectClass *) klass;
543         POA_Accessibility_Registry__epv *epv = &klass->epv;
544
545         registry_parent_class = g_type_class_ref (LISTENER_TYPE);
546
547         object_class->finalize = registry_object_finalize;
548
549         epv->registerApplication = impl_accessibility_registry_register_application;
550         epv->deregisterApplication = impl_accessibility_registry_deregister_application;
551         epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
552         epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
553         epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
554         epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
555         epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
556         epv->getDesktop = impl_accessibility_registry_get_desktop;
557         epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
558
559         ((ListenerClass *) klass)->epv.notifyEvent = impl_registry_notify_event;
560 }
561
562 static void
563 registry_init (Registry *registry)
564 {
565   registry->focus_listeners = NULL;
566   registry->window_listeners = NULL;
567   registry->toolkit_listeners = NULL;
568   registry->applications = NULL;
569   registry->desktop = desktop_new();
570 }
571
572 GType
573 registry_get_type (void)
574 {
575         static GType type = 0;
576
577         if (!type) {
578                 static const GTypeInfo tinfo = {
579                         sizeof (RegistryClass),
580                         (GBaseInitFunc) NULL,
581                         (GBaseFinalizeFunc) NULL,
582                         (GClassInitFunc) registry_class_init,
583                         (GClassFinalizeFunc) NULL,
584                         NULL, /* class data */
585                         sizeof (Registry),
586                         0, /* n preallocs */
587                         (GInstanceInitFunc) registry_init,
588                         NULL /* value table */
589                 };
590                 /*
591                  *   Here we use bonobo_type_unique instead of
592                  * gtk_type_unique, this auto-generates a load of
593                  * CORBA structures for us. All derived types must
594                  * use bonobo_type_unique.
595                  */
596                 type = bonobo_type_unique (
597                         PARENT_TYPE,
598                         POA_Accessibility_Registry__init,
599                         NULL,
600                         G_STRUCT_OFFSET (RegistryClass, epv),
601                         &tinfo,
602                         "Registry");
603         }
604
605         return type;
606 }
607
608 Registry *
609 registry_new (void)
610 {
611     Registry *retval =
612                REGISTRY (g_object_new (registry_get_type (), NULL));
613     return retval;
614 }