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