Added atk-bridge/bridge.c to POTFILES.skip, and commented configure.in,
[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   if (g_getenv ("AT_BRIDGE_SHUTDOWN"))
469     {
470         g_print("Atk Accessibility bridge initialized\n");
471     }
472 }
473
474 void
475 gnome_accessibility_module_shutdown (void)
476 {
477   BonoboObject *app = (BonoboObject *) this_app;
478   int     i;
479   GArray *ids = listener_ids;
480   
481   if (!atk_bridge_initialized)
482     {
483       return;
484     }
485   atk_bridge_initialized = FALSE;
486   this_app = NULL;
487
488   if (g_getenv ("AT_BRIDGE_SHUTDOWN"))
489     {
490         g_print("Atk Accessibility bridge shutdown\n");
491     }
492
493   listener_ids = NULL;
494   atk_remove_focus_tracker (atk_bridge_focus_tracker_id);
495   
496   for (i = 0; ids && i < ids->len; i++)
497   {
498           atk_remove_global_event_listener (g_array_index (ids, guint, i));
499   }
500   
501   atk_remove_key_event_listener (atk_bridge_key_event_listener_id);
502
503   deregister_application (app);
504 }
505
506 static void
507 spi_atk_bridge_focus_tracker (AtkObject *object)
508 {
509   SpiAccessible *source;
510   Accessibility_Event e;
511
512   source = spi_accessible_new (object);
513
514   e.type = "focus:";
515   e.source = BONOBO_OBJREF (source);
516   e.detail1 = 0;
517   e.detail2 = 0;
518   spi_init_any_nil (&e.any_data);
519
520   CORBA_exception_init (&ev);
521   Accessibility_Registry_notifyEvent (spi_atk_bridge_get_registry (), &e, &ev);
522   if (BONOBO_EX (&ev))
523     registry_died = TRUE;
524
525   bonobo_object_unref (source);
526   
527   CORBA_exception_free (&ev);
528 }
529
530 static void
531 spi_atk_emit_eventv (const GObject         *gobject,
532                      long                   detail1,
533                      long                   detail2,
534                      CORBA_any             *any,
535                      const char            *format, ...)
536 {
537   va_list             args;
538   Accessibility_Event e;
539   SpiAccessible      *source;
540   AtkObject          *aobject;
541 #ifdef SPI_BRIDGE_DEBUG
542   CORBA_string s;
543 #endif
544   
545   va_start (args, format);
546   
547   if (ATK_IS_IMPLEMENTOR (gobject))
548     {
549       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
550       source  = spi_accessible_new (aobject);
551       g_object_unref (G_OBJECT (aobject));
552     }
553   else if (ATK_IS_OBJECT (gobject))
554     {
555       aobject = ATK_OBJECT (gobject);
556       source  = spi_accessible_new (aobject);
557     }
558   else
559     {
560       aobject = NULL;
561       source  = NULL;
562       DBG (0, g_warning ("received property-change event from non-AtkImplementor"));
563     }
564
565   if (source) 
566     {
567       e.type = g_strdup_vprintf (format, args);
568       e.source = BONOBO_OBJREF (source);
569       e.detail1 = detail1;
570       e.detail2 = detail2;
571       if (any) e.any_data = *any;
572       else spi_init_any_nil (&e.any_data);
573
574 #ifdef SPI_BRIDGE_DEBUG
575       s = Accessibility_Accessible__get_name (BONOBO_OBJREF (source), &ev);
576       g_warning ("Emitting event '%s' (%lu, %lu) on %s",
577                  e.type, e.detail1, e.detail2, s);
578       CORBA_free (s);
579 #endif
580       CORBA_exception_init (&ev);
581       Accessibility_Registry_notifyEvent (spi_atk_bridge_get_registry (), 
582                                           &e, &ev);
583 #ifdef SPI_BRIDGE_DEBUG
584       if (ev._major != CORBA_NO_EXCEPTION)
585         g_warning ("error emitting event %s, (%d) %s",
586                    e.type,
587                    ev._major,
588                    CORBA_exception_id(&ev));
589 #endif        
590       if (BONOBO_EX (&ev)) registry_died = TRUE;
591       Accessibility_Accessible_unref (e.source, &ev);
592       
593       CORBA_exception_free (&ev);
594       
595       g_free (e.type);
596     }
597
598   va_end (args);
599
600 }
601
602 static gboolean
603 spi_atk_bridge_property_event_listener (GSignalInvocationHint *signal_hint,
604                                         guint n_param_values,
605                                         const GValue *param_values,
606                                         gpointer data)
607 {
608   AtkPropertyValues *values;
609   GObject *gobject;
610   const gchar *prop_name;
611   CORBA_any any;
612   const gchar *sp = NULL;
613   AtkObject *ao;
614   SpiAccessible *s_ao = NULL;
615   CORBA_Object c_obj;
616   gint i;
617
618 #ifdef SPI_BRIDGE_DEBUG
619   GSignalQuery signal_query;
620   const gchar *name;
621   const gchar *s, *s2;
622   
623   g_signal_query (signal_hint->signal_id, &signal_query);
624   name = signal_query.signal_name;
625
626   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
627   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
628   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
629   DBG (2, g_message ("Received (property) signal %s:%s:%s from object %s (gail %s)\n",
630            g_type_name (signal_query.itype), name, values->property_name, s, s2));
631   
632 #endif
633
634   gobject = g_value_get_object (param_values + 0);
635   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
636
637   prop_name = values->property_name;
638   if (strcmp (prop_name, "accessible-name") == 0)
639     {
640       sp = atk_object_get_name (ATK_OBJECT (gobject));
641       spi_init_any_string (&any, (gchar **)&sp);
642     }
643   else if (strcmp (prop_name, "accessible-description") == 0)
644     {
645       sp = atk_object_get_description (ATK_OBJECT (gobject));
646       spi_init_any_string (&any, (gchar **)&sp);
647     }
648   else if (strcmp (prop_name, "accessible-parent") == 0)
649     {
650       ao = atk_object_get_parent (ATK_OBJECT (gobject));
651       if (ao) 
652         {
653           s_ao = spi_accessible_new (ao);
654           c_obj = BONOBO_OBJREF (s_ao);
655           spi_init_any_object (&any, &c_obj);
656         }
657       else
658         {
659           spi_init_any_nil (&any);
660         }
661     }
662   else if (strcmp (prop_name, "accessible-table-summary") == 0)
663     {
664       ao = atk_table_get_summary (ATK_TABLE (gobject));
665       if (ao) 
666         {
667           s_ao = spi_accessible_new (ao);
668           c_obj = BONOBO_OBJREF (s_ao);
669           spi_init_any_object (&any, &c_obj);
670         }
671       else
672         {
673           spi_init_any_nil (&any);
674         }
675     }
676   else if (strcmp (prop_name, "accessible-table-column-header") == 0)
677     {
678       i = g_value_get_int (&(values->new_value));
679       ao = atk_table_get_column_header (ATK_TABLE (gobject), i);
680       if (ao) 
681         {
682           s_ao = spi_accessible_new (ao);
683           c_obj = BONOBO_OBJREF (s_ao);
684           spi_init_any_object (&any, &c_obj);
685         }
686       else
687         {
688           spi_init_any_nil (&any);
689         }
690     }
691   else if (strcmp (prop_name, "accessible-table-row-header") == 0)
692     {
693       i = g_value_get_int (&(values->new_value));
694       ao = atk_table_get_row_header (ATK_TABLE (gobject), i);
695       if (ao) 
696         {
697           s_ao = spi_accessible_new (ao);
698           c_obj = BONOBO_OBJREF (s_ao);
699           spi_init_any_object (&any, &c_obj);
700         }
701       else
702         {
703           spi_init_any_nil (&any);
704         }
705     }
706   else if (strcmp (prop_name, "accessible-table-row-description") == 0)
707     {
708       i = g_value_get_int (&(values->new_value));
709       sp = atk_table_get_row_description (ATK_TABLE (gobject), i);
710       spi_init_any_string (&any, (gchar **)&sp);
711     }
712   else if (strcmp (prop_name, "accessible-table-column-description") == 0)
713     {
714       i = g_value_get_int (&(values->new_value));
715       sp = atk_table_get_column_description (ATK_TABLE (gobject), i);
716       spi_init_any_string (&any, (gchar **)&sp);
717     }
718   else if (strcmp (prop_name, "accessible-table-caption-object") == 0)
719     {
720       ao = atk_table_get_caption (ATK_TABLE (gobject));
721       sp = atk_object_get_name (ao);
722       spi_init_any_string (&any, (gchar **)&sp);
723     }
724   else
725     {
726       spi_init_any_nil (&any);
727     }
728
729   spi_atk_emit_eventv (gobject, 0, 0, &any,
730                        "object:property-change:%s", prop_name);
731
732
733   return TRUE;
734 }
735
736 static gboolean
737 spi_atk_bridge_state_event_listener (GSignalInvocationHint *signal_hint,
738                                      guint n_param_values,
739                                      const GValue *param_values,
740                                      gpointer data)
741 {
742   GObject *gobject;
743   gchar *property_name;
744   gchar *type;
745   unsigned long detail1;
746 #ifdef SPI_BRIDGE_DEBUG
747   GSignalQuery signal_query;
748   const gchar *name;
749   
750   g_signal_query (signal_hint->signal_id, &signal_query);
751   name = signal_query.signal_name;
752   fprintf (stderr, "Received (state) signal %s:%s\n",
753            g_type_name (signal_query.itype), name);
754 #endif
755
756   gobject = g_value_get_object (param_values + 0);
757   property_name = g_strdup (g_value_get_string (param_values + 1));
758   detail1 = (g_value_get_boolean (param_values + 2))
759     ? 1 : 0;
760   type = g_strdup_printf ("object:state-changed:%s", property_name);
761   spi_atk_emit_eventv (gobject, 
762                        detail1,
763                        0,
764                        NULL,
765                        type);
766   g_free (property_name);
767   g_free (type);
768   return TRUE;
769 }
770
771 static void
772 spi_init_keystroke_from_atk_key_event (Accessibility_DeviceEvent  *keystroke,
773                                        AtkKeyEventStruct          *event)
774 {
775 #ifdef SPI_DEBUG
776   if (event)
777     {
778       g_print ("event %c (%d)\n", (int) event->keyval, (int) event->keycode);
779     }
780   else
781 #endif
782   if (!event)
783     { /* this doesn't really need translating */
784       g_print (_("WARNING: NULL key event reported."));
785     }
786   
787   keystroke->id        = (CORBA_long) event->keyval;
788   keystroke->hw_code   = (CORBA_short) event->keycode;
789   keystroke->timestamp = (CORBA_unsigned_long) event->timestamp;
790   keystroke->modifiers = (CORBA_unsigned_short) (event->state & 0xFFFF);
791   if (event->string)
792     {
793       gunichar c;
794
795       keystroke->event_string = CORBA_string_dup (event->string);
796       c = g_utf8_get_char_validated (event->string, -1);
797       if (c > 0 && g_unichar_isprint (c))
798         keystroke->is_text = CORBA_TRUE;
799       else
800         keystroke->is_text = CORBA_FALSE;
801     }
802   else
803     {
804       keystroke->event_string = CORBA_string_dup ("");
805       keystroke->is_text = CORBA_FALSE;
806     }
807   switch (event->type)
808     {
809     case (ATK_KEY_EVENT_PRESS):
810       keystroke->type = Accessibility_KEY_PRESSED_EVENT;
811       break;
812     case (ATK_KEY_EVENT_RELEASE):
813       keystroke->type = Accessibility_KEY_RELEASED_EVENT;
814       break;
815     default:
816       keystroke->type = 0;
817       break;
818     }
819 #if 0  
820   g_print ("key_event type %d; val=%d code=%d modifiers=%x name=%s is_text=%d, time=%lx\n",
821            (int) keystroke->type, (int) keystroke->id, (int) keystroke->hw_code,
822            (int) keystroke->modifiers,
823            keystroke->event_string, (int) keystroke->is_text, (unsigned long) keystroke->timestamp);
824 #endif
825 }
826
827 static gint
828 spi_atk_bridge_key_listener (AtkKeyEventStruct *event, gpointer data)
829 {
830   CORBA_boolean             result;
831   Accessibility_DeviceEvent key_event;
832
833   CORBA_exception_init (&ev);
834
835   spi_init_keystroke_from_atk_key_event (&key_event, event);
836
837   result = Accessibility_DeviceEventController_notifyListenersSync (
838           spi_atk_bridget_get_dec (), &key_event, &ev);
839
840   if (BONOBO_EX(&ev)) {
841       result = FALSE;
842       CORBA_exception_free (&ev);
843   }
844
845   return result;
846 }
847
848 static gboolean
849 spi_atk_bridge_signal_listener (GSignalInvocationHint *signal_hint,
850                                 guint n_param_values,
851                                 const GValue *param_values,
852                                 gpointer data)
853 {
854   GObject *gobject;
855   GSignalQuery signal_query;
856   const gchar *name;
857   const gchar *detail;
858   CORBA_any any;
859   CORBA_Object c_obj;
860   char *sp = NULL;
861   AtkObject *ao;
862   AtkText *text;
863   gint detail1 = 0, detail2 = 0;
864   SpiAccessible *s_ao = NULL;
865 #ifdef SPI_BRIDGE_DEBUG
866   const gchar *s, *s2;
867 #endif 
868   
869   g_signal_query (signal_hint->signal_id, &signal_query);
870
871   name = signal_query.signal_name;
872   if (signal_hint->detail)
873     detail = g_quark_to_string (signal_hint->detail);
874   else
875     detail = NULL;
876
877 #ifdef SPI_BRIDGE_DEBUG
878   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
879   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
880   fprintf (stderr, "Received signal %s:%s detail: %s from object %s (gail %s)\n",
881            g_type_name (signal_query.itype), name, 
882            detail ? detail : "<NULL>", s ? s : "<NULL>" , s2);
883 #endif
884   
885   gobject = g_value_get_object (param_values + 0);
886
887   if (signal_query.signal_id == atk_signal_active_descendant_changed)
888     {
889       gpointer child = g_value_get_pointer (param_values + 1);
890
891       g_return_val_if_fail (ATK_IS_OBJECT (child), TRUE);
892
893       ao = ATK_OBJECT (child);
894
895       detail1 = atk_object_get_index_in_parent (ao);
896       s_ao = spi_accessible_new (ao);
897       c_obj = BONOBO_OBJREF (s_ao);
898       spi_init_any_object (&any, &c_obj);
899     }
900   else if (signal_query.signal_id == atk_signal_link_selected)
901     {
902       if (G_VALUE_TYPE (param_values + 1) == G_TYPE_INT)
903         detail1 = g_value_get_int (param_values + 1);
904       spi_init_any_nil (&any);
905     }
906   else if ((signal_query.signal_id == atk_signal_children_changed) && gobject)
907     {
908       ao = atk_object_ref_accessible_child (ATK_OBJECT (gobject), 
909                                             detail1);
910       if (ao) 
911         {
912           s_ao = spi_accessible_new (ao);
913           c_obj = BONOBO_OBJREF (s_ao);
914           spi_init_any_object (&any, &c_obj);
915           g_object_unref (ao);
916         }
917       else
918         {
919           spi_init_any_nil (&any);
920         }
921     }
922   else
923     {
924       if (n_param_values >= 2)
925         {
926           if (G_VALUE_TYPE (param_values + 1) == G_TYPE_INT)
927             detail1 = g_value_get_int (param_values + 1);
928           if (n_param_values >= 3)
929             {
930               if (G_VALUE_TYPE (param_values + 2) == G_TYPE_INT)
931                 detail2 = g_value_get_int (param_values + 2);
932             }
933         }
934
935       if (signal_query.signal_id == atk_signal_text_changed)
936         {
937           sp = atk_text_get_text (ATK_TEXT (gobject),
938                                   detail1,
939                                   detail1+detail2);
940           spi_init_any_string (&any, &sp);
941         }
942       else if (signal_query.signal_id == atk_signal_text_selection_changed)
943         {
944           text = ATK_TEXT (gobject);
945  
946           /* Return NULL as the selected string */
947           spi_init_any_nil (&any);
948         }
949       else
950         {
951           spi_init_any_nil (&any);
952         }
953     }
954
955   if (detail)
956     spi_atk_emit_eventv (gobject, detail1, detail2, &any,
957                          "object:%s:%s", name, detail);
958   else
959     spi_atk_emit_eventv (gobject, detail1, detail2, &any,
960                          "object:%s", name);
961
962   if (sp)
963     g_free (sp);
964
965   return TRUE;
966 }
967
968 static gboolean
969 spi_atk_bridge_window_event_listener (GSignalInvocationHint *signal_hint,
970                                       guint n_param_values,
971                                       const GValue *param_values,
972                                       gpointer data)
973 {
974   GObject *gobject;
975   GSignalQuery signal_query;
976   CORBA_any any;
977   const gchar *name, *s;
978 #ifdef SPI_BRIDGE_DEBUG
979   const gchar *s2;
980 #endif
981   
982   g_signal_query (signal_hint->signal_id, &signal_query);
983
984   name = signal_query.signal_name;
985
986 #ifdef SPI_BRIDGE_DEBUG
987   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
988   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
989   fprintf (stderr, "Received signal %s:%s from object %s (gail %s)\n",
990            g_type_name (signal_query.itype), name, s ? s : "<NULL>" , s2);
991 #endif
992   
993   gobject = g_value_get_object (param_values + 0);
994
995   s = atk_object_get_name (ATK_OBJECT (gobject));
996   spi_init_any_string (&any, (char **) &s);
997   
998   spi_atk_emit_eventv (gobject, 0, 0, &any,
999                        "window:%s", name);
1000   return TRUE;
1001 }
1002
1003 static void
1004 spi_atk_tidy_windows (void)
1005 {
1006   AtkObject *root;
1007   gint n_children;
1008   gint i;
1009
1010   root = atk_get_root ();
1011   n_children = atk_object_get_n_accessible_children (root);
1012   for (i = 0; i < n_children; i++)
1013     {
1014       AtkObject *child;
1015       AtkStateSet *stateset;
1016       CORBA_any any;
1017       const gchar *name;
1018      
1019       child = atk_object_ref_accessible_child (root, i);
1020       stateset = atk_object_ref_state_set (child);
1021       
1022       name = atk_object_get_name (child);
1023       spi_init_any_string (&any, (char**) &name);
1024       if (atk_state_set_contains_state (stateset, ATK_STATE_ACTIVE))
1025         {
1026           spi_atk_emit_eventv (G_OBJECT (child), 0, 0, &any, "window:deactivate");
1027         }
1028       g_object_unref (stateset);
1029
1030       spi_atk_emit_eventv (G_OBJECT (child), 0, 0, &any, "window:destroy");
1031       g_object_unref (child);
1032     }
1033 }
1034
1035 static void
1036 reinit_register_vars (void)
1037 {
1038   registry = CORBA_OBJECT_NIL;
1039   device_event_controller = CORBA_OBJECT_NIL;
1040   this_app = NULL;
1041 }
1042