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