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