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