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