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