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