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