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