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