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