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