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