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