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