2003-04-24 Padraig O'Briain <padraig.obriain@sun.com>
[platform/core/uifw/at-spi2-atk.git] / atk-bridge / bridge.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002, 2003 Sun Microsystems Inc.,
6  * Copyright 2001, 2002, 2003 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <libbonobo.h>
29 #include <orbit/orbit.h>
30 #include <atk/atk.h>
31 #include <atk/atkobject.h>
32 #include <atk/atknoopobject.h>
33 #include <libspi/Accessibility.h>
34 #include <libspi/spi-private.h>
35 #include "accessible.h"
36 #include "application.h"
37
38 #include <bonobo-activation/bonobo-activation-register.h>
39
40 #undef SPI_BRIDGE_DEBUG
41
42 #define DBG(a,b) if(_dbg>=(a))b
43
44 static int _dbg = 0;
45 static CORBA_Environment ev;
46 static Accessibility_Registry registry = CORBA_OBJECT_NIL;
47 static Accessibility_DeviceEventController device_event_controller = CORBA_OBJECT_NIL;
48 static SpiApplication *this_app = NULL;
49 static gboolean registry_died = FALSE;
50 static gboolean atk_listeners_registered = FALSE;
51 static guint toplevel_handler;
52
53 static guint atk_signal_text_changed;
54 static guint atk_signal_children_changed;
55 static guint atk_signal_active_descendant_changed;
56 static guint atk_signal_text_selection_changed;
57
58 /* NOT YET USED
59    static guint atk_signal_row_reordered;
60    static guint atk_signal_row_inserted;
61    static guint atk_signal_row_deleted;
62    static guint atk_signal_column_reordered;
63    static guint atk_signal_column_inserted;
64    static guint atk_signal_column_deleted;
65 */
66
67 static guint atk_signal_link_selected;
68
69 static Accessibility_Registry spi_atk_bridge_get_registry (void);
70 static void     spi_atk_bridge_do_registration         (void);
71 static void     spi_atk_bridge_toplevel_added          (AtkObject             *object,
72                                                         guint                 index,
73                                                         AtkObject             *child);
74
75 static void     spi_atk_bridge_exit_func               (void);
76 static void     spi_atk_register_event_listeners       (void);
77 static void     spi_atk_bridge_focus_tracker           (AtkObject             *object);
78 static void     spi_atk_bridge_register_application    (Accessibility_Registry registry);
79 static gboolean spi_atk_bridge_property_event_listener (GSignalInvocationHint *signal_hint,
80                                                         guint                  n_param_values,
81                                                         const GValue          *param_values,
82                                                         gpointer               data);
83 static gboolean
84 spi_atk_bridge_window_event_listener (GSignalInvocationHint *signal_hint,
85                                 guint n_param_values,
86                                 const GValue *param_values,
87                                 gpointer data);
88 static gboolean
89 spi_atk_bridge_state_event_listener (GSignalInvocationHint *signal_hint,
90                                      guint n_param_values,
91                                      const GValue *param_values,
92                                      gpointer data);
93 static gboolean spi_atk_bridge_signal_listener         (GSignalInvocationHint *signal_hint,
94                                                         guint                  n_param_values,
95                                                         const GValue          *param_values,
96                                                         gpointer               data);
97 static gint     spi_atk_bridge_key_listener            (AtkKeyEventStruct     *event,
98                                                         gpointer               data);
99
100 /* For automatic libgnome init */
101 extern void gnome_accessibility_module_init     (void);
102 extern void gnome_accessibility_module_shutdown (void);
103
104 static int     atk_bridge_initialized = FALSE;
105 static guint   atk_bridge_focus_tracker_id = 0;
106 static guint   atk_bridge_key_event_listener_id = 0;
107 static GArray *listener_ids = NULL;
108
109 /*
110  *   These exported symbols are hooked by gnome-program
111  * to provide automatic module initialization and shutdown.
112  */
113 extern void gnome_accessibility_module_init     (void);
114 extern void gnome_accessibility_module_shutdown (void);
115
116 static void
117 spi_atk_bridge_init_event_type_consts ()
118 {
119   atk_signal_children_changed = g_signal_lookup ("children_changed", 
120                                               ATK_TYPE_OBJECT);
121   atk_signal_text_changed = g_signal_lookup ("text_changed", 
122                                              ATK_TYPE_TEXT);
123   atk_signal_active_descendant_changed = 
124          g_signal_lookup ("active_descendant_changed", 
125                           ATK_TYPE_OBJECT);
126   atk_signal_link_selected = g_signal_lookup ("link_selected", 
127                                               ATK_TYPE_HYPERTEXT);
128   atk_signal_text_selection_changed = g_signal_lookup ("text_selection_changed", 
129                                               ATK_TYPE_TEXT);
130 }
131
132 static int
133 atk_bridge_init (gint *argc, gchar **argv[])
134 {
135   const char *debug_env_string = g_getenv ("AT_SPI_DEBUG");
136
137   if (atk_bridge_initialized)
138     {
139       return 0;
140     }
141   atk_bridge_initialized = TRUE;
142
143   if (debug_env_string)
144     _dbg = (int) g_ascii_strtod (debug_env_string, NULL);
145
146   if (!bonobo_init (argc, argv ? *argv : NULL))
147     {
148       g_error ("Could not initialize Bonobo");
149     }
150
151   /*
152    * We only want to enable the bridge for top level
153    * applications, we detect bonobo components by seeing
154    * if they were activated with the intention of extracting
155    * an impl. by IID - very solid.
156    */
157   if (bonobo_activation_iid_get ())
158     {
159       DBG (1, g_message ("Found Bonobo component\n"));
160       toplevel_handler = g_signal_connect (atk_get_root (), 
161                                            "children-changed::add",
162                                            (GCallback) spi_atk_bridge_toplevel_added, 
163                                            NULL);
164     }
165   else
166     {
167       spi_atk_bridge_do_registration ();
168     }
169  
170   spi_atk_bridge_init_event_type_consts ();
171
172   return 0;
173 }
174
175
176 static void
177 spi_atk_bridge_do_registration (void)
178 {
179   CORBA_Environment ev;
180
181   CORBA_exception_init(&ev);
182
183   if (spi_atk_bridge_get_registry () == CORBA_OBJECT_NIL)
184     {
185       g_error ("Could not locate registry");
186     }
187
188   bonobo_activate ();
189
190   /* Create the accessible application server object */
191
192   this_app = spi_application_new (atk_get_root ());
193
194   DBG (1, g_message ("About to register application\n"));
195
196   spi_atk_bridge_register_application (spi_atk_bridge_get_registry ());
197   
198   g_atexit (spi_atk_bridge_exit_func);
199
200   DBG (1, g_message ("Application registered & listening\n"));
201
202 }
203
204 static void
205 spi_atk_bridge_toplevel_added (AtkObject *object,
206                                guint     index,
207                                AtkObject *child)
208 {
209   g_signal_handler_disconnect (object, toplevel_handler);
210   spi_atk_bridge_do_registration ();
211 }
212
213 static void
214 spi_atk_bridge_register_application (Accessibility_Registry registry)
215 {
216   Accessibility_Registry_registerApplication (spi_atk_bridge_get_registry (),
217                                               BONOBO_OBJREF (this_app),
218                                               &ev);
219   spi_atk_register_event_listeners ();
220 }
221
222 static Accessibility_Registry
223 spi_atk_bridge_get_registry (void)
224 {
225   CORBA_Environment ev;
226
227   if (registry_died || (registry == NULL)) {
228           CORBA_exception_init (&ev);
229           if (registry_died) 
230             DBG (1, g_warning ("registry died! restarting..."));
231           registry = bonobo_activation_activate_from_id (
232                   "OAFIID:Accessibility_Registry:1.0", 0, NULL, &ev);
233           
234           if (ev._major != CORBA_NO_EXCEPTION)
235           {
236                   g_error ("Accessibility app error: exception during "
237                            "registry activation from id: %s\n",
238                            CORBA_exception_id (&ev));
239                   CORBA_exception_free (&ev);
240           }
241           
242           if (registry_died && registry) {
243                   registry_died = FALSE;
244                   spi_atk_bridge_register_application (registry);
245           }
246   }
247   return registry;
248 }
249
250 static Accessibility_DeviceEventController
251 spi_atk_bridget_get_dec (void)
252 {
253   CORBA_Environment ev;
254
255   if (device_event_controller != CORBA_OBJECT_NIL)
256     {
257       if (ORBit_small_get_connection_status (device_event_controller)
258           == ORBIT_CONNECTION_CONNECTED)
259         return device_event_controller;
260     }
261
262   CORBA_exception_init (&ev);
263
264   device_event_controller =
265     Accessibility_Registry_getDeviceEventController (
266             spi_atk_bridge_get_registry (), &ev);
267
268   if (BONOBO_EX (&ev))
269     {
270       g_warning ("failure: no deviceeventcontroller found\n");
271       registry_died = TRUE;
272       device_event_controller = CORBA_OBJECT_NIL;
273     }
274
275   return device_event_controller;
276 }
277
278 int
279 gtk_module_init (gint *argc, gchar **argv[])
280 {
281         return atk_bridge_init (argc, argv);
282 }
283
284 static void
285 add_signal_listener (const char *signal_name)
286 {
287   guint id;
288
289   id = atk_add_global_event_listener (
290     spi_atk_bridge_signal_listener, signal_name);
291
292   g_array_append_val (listener_ids, id);
293 }
294
295 static void
296 spi_atk_register_event_listeners (void)
297 {
298   /*
299    * kludge to make sure the Atk interface types are registered, otherwise
300    * the AtkText signal handlers below won't get registered
301    */
302   guint      id;
303   GObject   *ao = g_object_new (ATK_TYPE_OBJECT, NULL);
304   AtkObject *bo = atk_no_op_object_new (ao);
305
306
307   if (atk_listeners_registered) return;
308
309   atk_listeners_registered = TRUE;
310
311   /* Register for focus event notifications, and register app with central registry  */
312
313   listener_ids = g_array_sized_new (FALSE, TRUE, sizeof (guint), 16);
314
315   atk_bridge_focus_tracker_id = atk_add_focus_tracker (spi_atk_bridge_focus_tracker);
316
317   id = atk_add_global_event_listener (spi_atk_bridge_property_event_listener,
318                                       "Gtk:AtkObject:property-change");
319   g_array_append_val (listener_ids, id);
320   id = atk_add_global_event_listener (spi_atk_bridge_window_event_listener,
321                                       "window:create");
322   g_array_append_val (listener_ids, id);
323   id = atk_add_global_event_listener (spi_atk_bridge_window_event_listener,
324                                       "window:destroy");
325   g_array_append_val (listener_ids, id);
326   id = atk_add_global_event_listener (spi_atk_bridge_window_event_listener,
327                                       "window:minimize");
328   g_array_append_val (listener_ids, id);
329   id = atk_add_global_event_listener (spi_atk_bridge_window_event_listener,
330                                       "window:maximize");
331   g_array_append_val (listener_ids, id);
332   id = atk_add_global_event_listener (spi_atk_bridge_window_event_listener,
333                                       "window:restore");
334   g_array_append_val (listener_ids, id);
335   id = atk_add_global_event_listener (spi_atk_bridge_window_event_listener,
336                                       "window:activate");
337   g_array_append_val (listener_ids, id);
338   id = atk_add_global_event_listener (spi_atk_bridge_window_event_listener,
339                                       "window:deactivate");
340   g_array_append_val (listener_ids, id);
341   id = atk_add_global_event_listener (spi_atk_bridge_state_event_listener,
342                                       "Gtk:AtkObject:state-change");
343   g_array_append_val (listener_ids, id);
344
345   add_signal_listener ("Gtk:AtkObject:children-changed");
346   add_signal_listener ("Gtk:AtkObject:visible-data-changed");
347   add_signal_listener ("Gtk:AtkObject:active-descendant-changed");
348   add_signal_listener ("Gtk:AtkSelection:selection-changed");
349   add_signal_listener ("Gtk:AtkText:text-selection-changed");
350   add_signal_listener ("Gtk:AtkText:text-changed");
351   add_signal_listener ("Gtk:AtkText:text-caret-moved");
352   add_signal_listener ("Gtk:AtkTable:row-inserted");
353   add_signal_listener ("Gtk:AtkTable:row-reordered");
354   add_signal_listener ("Gtk:AtkTable:row-deleted");
355   add_signal_listener ("Gtk:AtkTable:column-inserted");
356   add_signal_listener ("Gtk:AtkTable:column-reordered");
357   add_signal_listener ("Gtk:AtkTable:column-deleted");
358   add_signal_listener ("Gtk:AtkTable:model-changed");
359   add_signal_listener ("Gtk:AtkHypertext:link-selected");
360 /*
361  * May add the following listeners to implement preemptive key listening for GTK+
362  *
363  * atk_add_global_event_listener (spi_atk_bridge_widgetkey_listener, "Gtk:GtkWidget:key-press-event");
364  * atk_add_global_event_listener (spi_atk_bridge_widgetkey_listener, "Gtk:GtkWidget:key-release-event");
365  */
366   atk_bridge_key_event_listener_id = atk_add_key_event_listener (
367     spi_atk_bridge_key_listener, NULL);
368   
369   g_object_unref (G_OBJECT (bo));
370   g_object_unref (ao);
371 }
372
373 static void
374 deregister_application (BonoboObject *app)
375 {
376   Accessibility_Registry registry = spi_atk_bridge_get_registry ();     
377   Accessibility_Registry_deregisterApplication (registry, BONOBO_OBJREF (app), &ev);
378
379   device_event_controller = bonobo_object_release_unref (device_event_controller, &ev);
380   registry = bonobo_object_release_unref (registry, &ev);
381   
382   app = bonobo_object_unref (app);
383 }
384
385 static void
386 spi_atk_bridge_exit_func (void)
387 {
388   BonoboObject *app = (BonoboObject *) this_app;
389
390   DBG (1, g_message ("exiting bridge\n"));
391
392   if (!app)
393     {
394       return;
395     }
396   this_app = NULL;
397
398   /*
399    *  FIXME: this may be incorrect for apps that do their own bonobo
400    *  shutdown, until we can explicitly shutdown to get the ordering
401    *  right.
402    */
403   if (!bonobo_is_initialized ())
404     {
405       DBG (1, g_warning ("Re-initializing bonobo\n"));
406       g_assert (bonobo_init (0, NULL));
407       g_assert (bonobo_activate ());
408     }
409   
410   deregister_application (app);
411
412   DBG (1, g_message ("bridge exit func complete.\n"));
413
414   if (g_getenv ("AT_BRIDGE_SHUTDOWN"))
415     {
416       g_assert (!bonobo_debug_shutdown ());
417     }
418 }
419
420 void
421 gnome_accessibility_module_init (void)
422 {
423   atk_bridge_init (NULL, NULL);
424
425   g_print("Atk Accessibilty bridge initialized\n");
426 }
427
428 void
429 gnome_accessibility_module_shutdown (void)
430 {
431   BonoboObject *app = (BonoboObject *) this_app;
432   int     i;
433   GArray *ids = listener_ids;
434   
435   if (!atk_bridge_initialized)
436     {
437       return;
438     }
439   atk_bridge_initialized = FALSE;
440   this_app = NULL;
441
442   g_print("Atk Accessibilty bridge shutdown\n");
443
444   listener_ids = NULL;
445   atk_remove_focus_tracker (atk_bridge_focus_tracker_id);
446   
447   for (i = 0; ids && i < ids->len; i++)
448   {
449           atk_remove_global_event_listener (g_array_index (ids, guint, i));
450   }
451   
452   atk_remove_key_event_listener (atk_bridge_key_event_listener_id);
453
454   deregister_application (app);
455 }
456
457 static void
458 spi_atk_bridge_focus_tracker (AtkObject *object)
459 {
460   SpiAccessible *source;
461   Accessibility_Event e;
462
463   source = spi_accessible_new (object);
464
465   e.type = "focus:";
466   e.source = BONOBO_OBJREF (source);
467   e.detail1 = 0;
468   e.detail2 = 0;
469   spi_init_any_nil (&e.any_data);
470
471   CORBA_exception_init (&ev);
472   Accessibility_Registry_notifyEvent (spi_atk_bridge_get_registry (), &e, &ev);
473   if (BONOBO_EX (&ev))
474     registry_died = TRUE;
475
476   bonobo_object_unref (source);
477   
478   CORBA_exception_free (&ev);
479 }
480
481 static void
482 spi_atk_emit_eventv (const GObject         *gobject,
483                      long                   detail1,
484                      long                   detail2,
485                      CORBA_any             *any,
486                      const char            *format, ...)
487 {
488   va_list             args;
489   Accessibility_Event e;
490   SpiAccessible      *source;
491   AtkObject          *aobject;
492 #ifdef SPI_BRIDGE_DEBUG
493   CORBA_string s;
494 #endif
495   
496   va_start (args, format);
497   
498   if (ATK_IS_IMPLEMENTOR (gobject))
499     {
500       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
501       source  = spi_accessible_new (aobject);
502       g_object_unref (G_OBJECT (aobject));
503     }
504   else if (ATK_IS_OBJECT (gobject))
505     {
506       aobject = ATK_OBJECT (gobject);
507       source  = spi_accessible_new (aobject);
508     }
509   else
510     {
511       aobject = NULL;
512       source  = NULL;
513       DBG (0, g_warning ("received property-change event from non-AtkImplementor"));
514     }
515
516   if (source) 
517     {
518       e.type = g_strdup_vprintf (format, args);
519       e.source = BONOBO_OBJREF (source);
520       e.detail1 = detail1;
521       e.detail2 = detail2;
522       if (any) e.any_data = *any;
523       else spi_init_any_nil (&e.any_data);
524
525 #ifdef SPI_BRIDGE_DEBUG
526       s = Accessibility_Accessible__get_name (BONOBO_OBJREF (source), &ev);
527       g_warning ("Emitting event '%s' (%lu, %lu) on %s",
528                  e.type, e.detail1, e.detail2, s);
529       CORBA_free (s);
530 #endif
531       CORBA_exception_init (&ev);
532       Accessibility_Registry_notifyEvent (spi_atk_bridge_get_registry (), 
533                                           &e, &ev);
534 #ifdef SPI_BRIDGE_DEBUG
535       if (ev._major != CORBA_NO_EXCEPTION)
536         g_warning ("error emitting event %s, (%d) %s",
537                    e.type,
538                    ev._major,
539                    CORBA_exception_id(&ev));
540 #endif        
541       if (BONOBO_EX (&ev)) registry_died = TRUE;
542       Accessibility_Accessible_unref (e.source, &ev);
543       
544       CORBA_exception_free (&ev);
545       
546       g_free (e.type);
547     }
548
549   va_end (args);
550
551 }
552
553 static gboolean
554 spi_atk_bridge_property_event_listener (GSignalInvocationHint *signal_hint,
555                                         guint n_param_values,
556                                         const GValue *param_values,
557                                         gpointer data)
558 {
559   AtkPropertyValues *values;
560   GObject *gobject;
561   const gchar *prop_name;
562   CORBA_any any;
563   const gchar *sp = NULL;
564   AtkObject *ao;
565   SpiAccessible *s_ao = NULL;
566   CORBA_Object c_obj;
567   gint i;
568
569 #ifdef SPI_BRIDGE_DEBUG
570   GSignalQuery signal_query;
571   const gchar *name;
572   const gchar *s, *s2;
573   
574   g_signal_query (signal_hint->signal_id, &signal_query);
575   name = signal_query.signal_name;
576
577   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
578   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
579   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
580   DBG (2, g_message ("Received (property) signal %s:%s:%s from object %s (gail %s)\n",
581            g_type_name (signal_query.itype), name, values->property_name, s, s2));
582   
583 #endif
584
585   gobject = g_value_get_object (param_values + 0);
586   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
587
588   prop_name = values->property_name;
589   if (strcmp (prop_name, "accessible-name") == 0)
590     {
591       sp = atk_object_get_name (ATK_OBJECT (gobject));
592       spi_init_any_string (&any, (gchar **)&sp);
593     }
594   else if (strcmp (prop_name, "accessible-description") == 0)
595     {
596       sp = atk_object_get_description (ATK_OBJECT (gobject));
597       spi_init_any_string (&any, (gchar **)&sp);
598     }
599   else if (strcmp (prop_name, "accessible-parent") == 0)
600     {
601       ao = atk_object_get_parent (ATK_OBJECT (gobject));
602       if (ao) 
603         {
604           s_ao = spi_accessible_new (ao);
605           c_obj = BONOBO_OBJREF (s_ao);
606           spi_init_any_object (&any, &c_obj);
607         }
608       else
609         {
610           spi_init_any_nil (&any);
611         }
612     }
613   else if (strcmp (prop_name, "accessible-table-summary") == 0)
614     {
615       ao = atk_table_get_summary (ATK_TABLE (gobject));
616       if (ao) 
617         {
618           s_ao = spi_accessible_new (ao);
619           c_obj = BONOBO_OBJREF (s_ao);
620           spi_init_any_object (&any, &c_obj);
621         }
622       else
623         {
624           spi_init_any_nil (&any);
625         }
626     }
627   else if (strcmp (prop_name, "accessible-table-column-header") == 0)
628     {
629       i = g_value_get_int (&(values->new_value));
630       ao = atk_table_get_column_header (ATK_TABLE (gobject), i);
631       if (ao) 
632         {
633           s_ao = spi_accessible_new (ao);
634           c_obj = BONOBO_OBJREF (s_ao);
635           spi_init_any_object (&any, &c_obj);
636         }
637       else
638         {
639           spi_init_any_nil (&any);
640         }
641     }
642   else if (strcmp (prop_name, "accessible-table-row-header") == 0)
643     {
644       i = g_value_get_int (&(values->new_value));
645       ao = atk_table_get_row_header (ATK_TABLE (gobject), i);
646       if (ao) 
647         {
648           s_ao = spi_accessible_new (ao);
649           c_obj = BONOBO_OBJREF (s_ao);
650           spi_init_any_object (&any, &c_obj);
651         }
652       else
653         {
654           spi_init_any_nil (&any);
655         }
656     }
657   else if (strcmp (prop_name, "accessible-table-row-description") == 0)
658     {
659       i = g_value_get_int (&(values->new_value));
660       sp = atk_table_get_row_description (ATK_TABLE (gobject), i);
661       spi_init_any_string (&any, (gchar **)&sp);
662     }
663   else if (strcmp (prop_name, "accessible-table-column-description") == 0)
664     {
665       i = g_value_get_int (&(values->new_value));
666       sp = atk_table_get_column_description (ATK_TABLE (gobject), i);
667       spi_init_any_string (&any, (gchar **)&sp);
668     }
669   else if (strcmp (prop_name, "accessible-table-caption-object") == 0)
670     {
671       ao = atk_table_get_caption (ATK_TABLE (gobject));
672       sp = atk_object_get_name (ao);
673       spi_init_any_string (&any, (gchar **)&sp);
674     }
675   else
676     {
677       spi_init_any_nil (&any);
678     }
679
680   spi_atk_emit_eventv (gobject, 0, 0, &any,
681                        "object:property-change:%s", prop_name);
682
683
684   return TRUE;
685 }
686
687 static gboolean
688 spi_atk_bridge_state_event_listener (GSignalInvocationHint *signal_hint,
689                                      guint n_param_values,
690                                      const GValue *param_values,
691                                      gpointer data)
692 {
693   GObject *gobject;
694   gchar *property_name;
695   gchar *type;
696   unsigned long detail1;
697 #ifdef SPI_BRIDGE_DEBUG
698   GSignalQuery signal_query;
699   const gchar *name;
700   
701   g_signal_query (signal_hint->signal_id, &signal_query);
702   name = signal_query.signal_name;
703   fprintf (stderr, "Received (state) signal %s:%s\n",
704            g_type_name (signal_query.itype), name);
705 #endif
706
707   gobject = g_value_get_object (param_values + 0);
708   property_name = g_strdup (g_value_get_string (param_values + 1));
709   detail1 = (g_value_get_boolean (param_values + 2))
710     ? 1 : 0;
711   type = g_strdup_printf ("object:state-changed:%s", property_name);
712   spi_atk_emit_eventv (gobject, 
713                        detail1,
714                        0,
715                        NULL,
716                        type);
717   g_free (property_name);
718   g_free (type);
719   return TRUE;
720 }
721
722 static void
723 spi_init_keystroke_from_atk_key_event (Accessibility_DeviceEvent  *keystroke,
724                                        AtkKeyEventStruct          *event)
725 {
726 #ifdef SPI_DEBUG
727   if (event)
728     {
729       g_print ("event %c (%d)\n", (int) event->keyval, (int) event->keycode);
730     }
731   else
732 #endif
733   if (!event)
734     {
735       g_print ("WARNING: NULL key event!");
736     }
737   
738   keystroke->id        = (CORBA_long) event->keyval;
739   keystroke->hw_code   = (CORBA_short) event->keycode;
740   keystroke->timestamp = (CORBA_unsigned_long) event->timestamp;
741   keystroke->modifiers = (CORBA_unsigned_short) (event->state & 0xFFFF);
742   if (event->string)
743     {
744       gunichar c;
745
746       keystroke->event_string = CORBA_string_dup (event->string);
747       c = g_utf8_get_char_validated (event->string, -1);
748       if (c > 0 && g_unichar_isprint (c))
749         keystroke->is_text = CORBA_TRUE;
750       else
751         keystroke->is_text = CORBA_FALSE;
752     }
753   else
754     {
755       keystroke->event_string = CORBA_string_dup ("");
756       keystroke->is_text = CORBA_FALSE;
757     }
758   switch (event->type)
759     {
760     case (ATK_KEY_EVENT_PRESS):
761       keystroke->type = Accessibility_KEY_PRESSED_EVENT;
762       break;
763     case (ATK_KEY_EVENT_RELEASE):
764       keystroke->type = Accessibility_KEY_RELEASED_EVENT;
765       break;
766     default:
767       keystroke->type = 0;
768       break;
769     }
770 #if 0  
771   g_print ("key_event type %d; val=%d code=%d modifiers=%x name=%s is_text=%d, time=%lx\n",
772            (int) keystroke->type, (int) keystroke->id, (int) keystroke->hw_code,
773            (int) keystroke->modifiers,
774            keystroke->event_string, (int) keystroke->is_text, (unsigned long) keystroke->timestamp);
775 #endif
776 }
777
778 static gint
779 spi_atk_bridge_key_listener (AtkKeyEventStruct *event, gpointer data)
780 {
781   CORBA_boolean             result;
782   Accessibility_DeviceEvent key_event;
783
784   CORBA_exception_init (&ev);
785
786   spi_init_keystroke_from_atk_key_event (&key_event, event);
787
788   result = Accessibility_DeviceEventController_notifyListenersSync (
789           spi_atk_bridget_get_dec (), &key_event, &ev);
790
791   CORBA_exception_free (&ev);
792
793   return result;
794 }
795
796 static gboolean
797 spi_atk_bridge_signal_listener (GSignalInvocationHint *signal_hint,
798                                 guint n_param_values,
799                                 const GValue *param_values,
800                                 gpointer data)
801 {
802   GObject *gobject;
803   GSignalQuery signal_query;
804   const gchar *name;
805   const gchar *detail;
806   CORBA_any any;
807   CORBA_Object c_obj;
808   char *sp = NULL;
809   AtkObject *ao;
810   AtkText *text;
811   gint detail1 = 0, detail2 = 0;
812   SpiAccessible *s_ao = NULL;
813 #ifdef SPI_BRIDGE_DEBUG
814   const gchar *s, *s2;
815 #endif 
816   
817   g_signal_query (signal_hint->signal_id, &signal_query);
818
819   name = signal_query.signal_name;
820   if (signal_hint->detail)
821     detail = g_quark_to_string (signal_hint->detail);
822   else
823     detail = NULL;
824
825 #ifdef SPI_BRIDGE_DEBUG
826   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
827   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
828   fprintf (stderr, "Received signal %s:%s detail: %s from object %s (gail %s)\n",
829            g_type_name (signal_query.itype), name, 
830            detail ? detail : "<NULL>", s ? s : "<NULL>" , s2);
831 #endif
832   
833   gobject = g_value_get_object (param_values + 0);
834
835   if (signal_query.signal_id == atk_signal_active_descendant_changed)
836     {
837       gpointer child = g_value_get_pointer (param_values + 1);
838
839       g_return_val_if_fail (ATK_IS_OBJECT (child), TRUE);
840
841       ao = ATK_OBJECT (child);
842
843       detail1 = atk_object_get_index_in_parent (ao);
844       s_ao = spi_accessible_new (ao);
845       c_obj = BONOBO_OBJREF (s_ao);
846       spi_init_any_object (&any, &c_obj);
847     }
848   else if (signal_query.signal_id == atk_signal_link_selected)
849     {
850       if (G_VALUE_TYPE (param_values + 1) == G_TYPE_INT)
851         detail1 = g_value_get_int (param_values + 1);
852       spi_init_any_nil (&any);
853     }
854   else if ((signal_query.signal_id == atk_signal_children_changed) && gobject)
855     {
856       ao = atk_object_ref_accessible_child (ATK_OBJECT (gobject), 
857                                             detail1);
858       if (ao) 
859         {
860           s_ao = spi_accessible_new (ao);
861           c_obj = BONOBO_OBJREF (s_ao);
862           spi_init_any_object (&any, &c_obj);
863           g_object_unref (ao);
864         }
865       else
866         {
867           spi_init_any_nil (&any);
868         }
869     }
870   else
871     {
872       if (n_param_values >= 2)
873         {
874           if (G_VALUE_TYPE (param_values + 1) == G_TYPE_INT)
875             detail1 = g_value_get_int (param_values + 1);
876           if (n_param_values >= 3)
877             {
878               if (G_VALUE_TYPE (param_values + 2) == G_TYPE_INT)
879                 detail2 = g_value_get_int (param_values + 2);
880             }
881         }
882
883       if (signal_query.signal_id == atk_signal_text_changed)
884         {
885           sp = atk_text_get_text (ATK_TEXT (gobject),
886                                   detail1,
887                                   detail1+detail2);
888           spi_init_any_string (&any, &sp);
889         }
890       else if (signal_query.signal_id == atk_signal_text_selection_changed)
891         {
892           text = ATK_TEXT (gobject);
893  
894           /* Return NULL as the selected string */
895           spi_init_any_nil (&any);
896         }
897       else
898         {
899           spi_init_any_nil (&any);
900         }
901     }
902
903   if (detail)
904     spi_atk_emit_eventv (gobject, detail1, detail2, &any,
905                          "object:%s:%s", name, detail);
906   else
907     spi_atk_emit_eventv (gobject, detail1, detail2, &any,
908                          "object:%s", name);
909
910   if (sp)
911     g_free (sp);
912
913   return TRUE;
914 }
915
916 static gboolean
917 spi_atk_bridge_window_event_listener (GSignalInvocationHint *signal_hint,
918                                       guint n_param_values,
919                                       const GValue *param_values,
920                                       gpointer data)
921 {
922   GObject *gobject;
923   GSignalQuery signal_query;
924   CORBA_any any;
925   const gchar *name, *s;
926 #ifdef SPI_BRIDGE_DEBUG
927   const gchar *s2;
928 #endif
929   
930   g_signal_query (signal_hint->signal_id, &signal_query);
931
932   name = signal_query.signal_name;
933
934 #ifdef SPI_BRIDGE_DEBUG
935   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
936   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
937   fprintf (stderr, "Received signal %s:%s from object %s (gail %s)\n",
938            g_type_name (signal_query.itype), name, s ? s : "<NULL>" , s2);
939 #endif
940   
941   gobject = g_value_get_object (param_values + 0);
942
943   s = atk_object_get_name (ATK_OBJECT (gobject));
944   spi_init_any_string (&any, (char **) &s);
945   
946   spi_atk_emit_eventv (gobject, 0, 0, &any,
947                        "window:%s", name);
948   return TRUE;
949 }