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