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