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