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