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