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