Fix atk-adaptor re-entry problem during register application.
[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 2008, 2009 Codethink Ltd.
6  * Copyright 2001, 2002, 2003 Sun Microsystems Inc.,
7  * Copyright 2001, 2002, 2003 Ximian, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "config.h"
26 #include "dbus/dbus-glib-lowlevel.h"
27
28 #include <X11/Xlib.h>
29 #include <X11/Xatom.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <atk/atk.h>
36
37 #include <droute/droute.h>
38
39 #include "bridge.h"
40 #include "event.h"
41 #include "adaptors.h"
42 #include "object.h"
43
44 #include "accessible-register.h"
45 #include "accessible-leasing.h"
46 #include "accessible-cache.h"
47
48 #include "common/spi-dbus.h"
49
50 /*---------------------------------------------------------------------------*/
51
52 SpiBridge *spi_global_app_data = NULL;
53
54 static const AtkMisc *atk_misc = NULL;
55
56 /*static Display *bridge_display = NULL;*/
57
58 /*---------------------------------------------------------------------------*/
59
60 /*
61  * Returns a 'canonicalized' value for DISPLAY,
62  * with the screen number stripped off if present.
63  *
64  */
65 static const gchar *
66 spi_display_name (void)
67 {
68   static const char *canonical_display_name = NULL;
69   if (!canonical_display_name)
70     {
71       const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
72       if (!display_env)
73         {
74           display_env = g_getenv ("DISPLAY");
75           if (!display_env || !display_env[0])
76             canonical_display_name = ":0";
77           else
78             {
79               gchar *display_p, *screen_p;
80               canonical_display_name = g_strdup (display_env);
81               display_p = strrchr (canonical_display_name, ':');
82               screen_p = strrchr (canonical_display_name, '.');
83               if (screen_p && display_p && (screen_p > display_p))
84                 {
85                   *screen_p = '\0';
86                 }
87             }
88         }
89       else
90         {
91           canonical_display_name = display_env;
92         }
93     }
94   return canonical_display_name;
95 }
96
97 /*---------------------------------------------------------------------------*/
98
99 /*
100  * Gets the IOR from the XDisplay.
101  * Not currently used in D-Bus version, but something similar
102  * may be employed in the future for accessing the registry daemon
103  * bus name.
104  */
105
106 static DBusConnection *
107 spi_atk_bridge_get_bus (void)
108 {
109   Atom AT_SPI_BUS;
110   Atom actual_type;
111   Display *bridge_display;
112   int actual_format;
113   unsigned char *data = NULL;
114   unsigned long nitems;
115   unsigned long leftover;
116
117   DBusConnection *bus = NULL;
118   DBusError error;
119
120   bridge_display = XOpenDisplay (spi_display_name ());
121   if (!bridge_display)
122     g_error ("AT_SPI: Could not get the display\n");
123
124   AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
125   XGetWindowProperty (bridge_display,
126                       XDefaultRootWindow (bridge_display),
127                       AT_SPI_BUS, 0L,
128                       (long) BUFSIZ, False,
129                       (Atom) 31, &actual_type, &actual_format,
130                       &nitems, &leftover, &data);
131
132   dbus_error_init (&error);
133
134   if (data == NULL)
135     {
136       g_warning
137         ("AT-SPI: Accessibility bus not found - Using session bus.\n");
138       bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
139       if (!bus)
140         g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
141     }
142   else
143     {
144       bus = dbus_connection_open (data, &error);
145       if (!bus)
146         {
147           g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
148         }
149       else
150         {
151           if (!dbus_bus_register (bus, &error))
152             g_error ("AT-SPI: Couldn't register with bus: %s\n", error.message);
153         }
154     }
155
156   return bus;
157 }
158
159 static void
160 set_reply (DBusPendingCall *pending, void *user_data)
161 {
162     void **replyptr = (void **)user_data;
163
164     *replyptr = dbus_pending_call_steal_reply (pending);
165 }
166
167 static DBusMessage *
168 send_and_allow_reentry (DBusConnection *bus, DBusMessage *message, DBusError *error)
169 {
170     DBusPendingCall *pending;
171     DBusMessage *reply = NULL;
172
173     if (!dbus_connection_send_with_reply (bus, message, &pending, -1))
174     {
175         return NULL;
176     }
177     dbus_pending_call_set_notify (pending, set_reply, (void *)&reply, NULL);
178     while (!reply)
179     {
180       if (!dbus_connection_read_write_dispatch (bus, -1)) return NULL;
181     }
182     return reply;
183 }
184 /*---------------------------------------------------------------------------*/
185
186 static gboolean
187 register_application (SpiBridge * app)
188 {
189   DBusMessage *message, *reply;
190   DBusMessageIter iter;
191   DBusError error;
192
193   dbus_error_init (&error);
194
195   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
196                                           SPI_DBUS_PATH_ROOT,
197                                           SPI_DBUS_INTERFACE_SOCKET,
198                                           "Embed");
199
200   dbus_message_iter_init_append (message, &iter);
201   spi_object_append_reference (&iter, app->root);
202   
203   reply = send_and_allow_reentry (app->bus, message, &error);
204
205   if (message)
206     dbus_message_unref (message);
207
208   if (reply)
209     {
210       DBusMessageIter iter, iter_struct;
211       gchar *app_name, *obj_path;
212
213       dbus_message_iter_init (reply, &iter);
214       dbus_message_iter_recurse (&iter, &iter_struct);
215       if (!(dbus_message_iter_get_arg_type (&iter_struct) == DBUS_TYPE_STRING))
216         {
217           g_warning ("AT-SPI: Could not obtain desktop path or name\n");
218           return FALSE;
219         }
220       dbus_message_iter_get_basic (&iter_struct, &app_name);
221       if (!dbus_message_iter_next (&iter_struct))
222         {
223           g_warning ("AT-SPI: Could not obtain desktop name");
224           return FALSE;
225         }
226       if (!(dbus_message_iter_get_arg_type (&iter_struct) == DBUS_TYPE_OBJECT_PATH))
227         {
228           g_warning ("AT-SPI: Could not obtain desktop path");
229           return FALSE;
230         }
231       dbus_message_iter_get_basic (&iter_struct, &obj_path);
232
233       app->desktop_name = g_strdup (app_name);
234       app->desktop_path = g_strdup (obj_path);
235     }
236   else
237     {
238       g_warning ("AT-SPI: Could not embed inside desktop: %s\n", error.message);
239       return FALSE;
240     }
241   return TRUE;
242 }
243
244 /*---------------------------------------------------------------------------*/
245
246 static void
247 deregister_application (SpiBridge * app)
248 {
249   DBusMessage *message;
250   DBusMessageIter iter;
251   DBusError error;
252   const char *uname;
253
254   dbus_error_init (&error);
255
256   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
257                                           SPI_DBUS_PATH_REGISTRY,
258                                           SPI_DBUS_INTERFACE_REGISTRY,
259                                           "DeregisterApplication");
260   dbus_message_set_no_reply (message, TRUE);
261
262   uname = dbus_bus_get_unique_name (app->bus);
263
264   dbus_message_iter_init_append (message, &iter);
265   dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
266   dbus_connection_send (app->bus, message, NULL);
267   if (message)
268     dbus_message_unref (message);
269 }
270
271 /*---------------------------------------------------------------------------*/
272
273 static void
274 exit_func (void)
275 {
276   if (!spi_global_app_data)
277     {
278       return;
279     }
280
281   spi_atk_tidy_windows ();
282   spi_atk_deregister_event_listeners ();
283   deregister_application (spi_global_app_data);
284
285   g_free (spi_global_app_data);
286   spi_global_app_data = NULL;
287
288   /* Not currently creating an XDisplay */
289 #if 0
290   if (bridge_display)
291     XCloseDisplay (bridge_display);
292 #endif
293 }
294
295 /*---------------------------------------------------------------------------*/
296
297 static AtkPlugClass *plug_class;
298 static AtkSocketClass *socket_class;
299
300 static gchar *
301 get_plug_id (AtkPlug * plug)
302 {
303   const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
304   gchar *path;
305   GString *str = g_string_new (NULL);
306
307   path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
308   g_string_printf (str, "%s:%s", uname, path);
309   g_free (path);
310   return g_string_free (str, FALSE);
311 }
312
313 static void
314 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
315 {
316   AtkObject *accessible = ATK_OBJECT(socket);
317   gchar *plug_name, *plug_path;
318
319   /* Force registration */
320   gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
321   /* Let the plug know that it has been embedded */
322   plug_name = g_strdup (plug_id);
323   if (!plug_name)
324     {
325       g_free (path);
326       return;
327     }
328   plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
329   if (plug_path)
330     {
331       DBusMessage *message;
332       *(plug_path++) = '\0';
333       message = dbus_message_new_method_call (plug_name, plug_path, SPI_DBUS_INTERFACE_SOCKET, "Embedded");
334       dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
335       dbus_connection_send (spi_global_app_data->bus, message, NULL);
336     }
337   g_free (plug_name);
338   g_free (path);
339 }
340
341 static void
342 install_plug_hooks ()
343 {
344   gpointer data;
345
346   data = g_type_class_ref (ATK_TYPE_PLUG);
347   plug_class = ATK_PLUG_CLASS (data);
348   data = g_type_class_ref (ATK_TYPE_SOCKET);
349   socket_class = ATK_SOCKET_CLASS (data);
350   plug_class->get_object_id = get_plug_id;
351   socket_class->embed = socket_embed_hook;
352 }
353
354 gchar *atspi_dbus_name = NULL;
355 static gboolean atspi_no_register = FALSE;
356
357 static GOptionEntry atspi_option_entries[] = {
358   {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
359    "D-Bus bus name to register as", NULL},
360   {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
361    "Do not register with Registry Daemon", NULL},
362   {NULL}
363 };
364
365 /*
366  * spi_app_init
367  *
368  * The following needs to be initialized.
369  *
370  * - DRoute for routing message to their accessible objects.
371  * - Event handlers for emmitting signals on specific ATK events.
372  * - Application registration with the AT-SPI registry.
373  *
374  */
375 static int
376 adaptor_init (gint * argc, gchar ** argv[])
377 {
378   GOptionContext *opt;
379   GError *err = NULL;
380   DBusError error;
381   DBusConnection *bus;
382   AtkObject *root;
383   gchar *introspection_directory;
384   static gboolean inited = FALSE;
385
386   if (inited)
387     return 0;
388
389   inited = TRUE;
390
391   DRoutePath *treepath, *accpath;
392
393   root = atk_get_root ();
394   g_return_val_if_fail (root, 0);
395
396   /* Parse command line options */
397   opt = g_option_context_new (NULL);
398   g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
399   g_option_context_set_ignore_unknown_options (opt, TRUE);
400   if (!g_option_context_parse (opt, argc, argv, &err))
401     g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
402
403   /* Allocate global data and do ATK initializations */
404   spi_global_app_data = g_new0 (SpiBridge, 1);
405   atk_misc = atk_misc_get_instance ();
406   spi_global_app_data->root = g_object_ref (root);
407
408   /* Set up D-Bus connection and register bus name */
409   dbus_error_init (&error);
410   spi_global_app_data->bus = spi_atk_bridge_get_bus ();
411   if (!spi_global_app_data->bus)
412     {
413       g_free (spi_global_app_data);
414       spi_global_app_data = NULL;
415       return 0;
416     }
417
418   if (atspi_dbus_name != NULL)
419     {
420       if (dbus_bus_request_name
421           (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
422         {
423           g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
424         }
425       else
426         {
427           g_print
428             ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
429              atspi_dbus_name);
430         }
431     }
432
433   dbus_connection_setup_with_g_main (spi_global_app_data->bus,
434                                      g_main_context_default ());
435
436   /* 
437    * Create the leasing, register and cache objects.
438    * The order is important here, the cache depends on the
439    * register object.
440    */
441   spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
442   spi_global_leasing  = g_object_new (SPI_LEASING_TYPE, NULL);
443   spi_global_cache    = g_object_new (SPI_CACHE_TYPE, NULL);
444
445   /* Register droute for routing AT-SPI messages */
446   spi_global_app_data->droute =
447     droute_new (spi_global_app_data->bus);
448
449   treepath = droute_add_one (spi_global_app_data->droute,
450                              "/org/at_spi/cache", spi_global_cache);
451
452   if (!treepath)
453     {
454       g_warning ("atk-bridge: Error in droute_add_one().  Already running?");
455       return 0;
456     }
457
458   accpath = droute_add_many (spi_global_app_data->droute,
459                              "/org/a11y/atspi/accessible",
460                              NULL,
461                              (DRouteGetDatumFunction)
462                              spi_global_register_path_to_object);
463
464
465   /* Register all interfaces with droute and set up application accessible db */
466   spi_initialize_cache (treepath);
467   spi_initialize_accessible (accpath);
468   spi_initialize_application (accpath);
469   spi_initialize_action (accpath);
470   spi_initialize_collection (accpath);
471   spi_initialize_component (accpath);
472   spi_initialize_document (accpath);
473   spi_initialize_editabletext (accpath);
474   spi_initialize_hyperlink (accpath);
475   spi_initialize_hypertext (accpath);
476   spi_initialize_image (accpath);
477   spi_initialize_selection (accpath);
478   spi_initialize_socket (accpath);
479   spi_initialize_table (accpath);
480   spi_initialize_text (accpath);
481   spi_initialize_value (accpath);
482
483   /* Register methods to send D-Bus signals on certain ATK events */
484   spi_atk_register_event_listeners ();
485
486   /* Hook our plug-and socket functions */
487   install_plug_hooks ();
488
489   /* Register this app by sending a signal out to AT-SPI registry daemon */
490   if (!atspi_no_register && (!root || !ATK_IS_PLUG (root)))
491     register_application (spi_global_app_data);
492
493   g_atexit (exit_func);
494
495   return 0;
496 }
497
498 /*---------------------------------------------------------------------------*/
499
500 int
501 gtk_module_init (gint * argc, gchar ** argv[])
502 {
503   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
504
505   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
506     {
507       return adaptor_init (argc, argv);
508     }
509   return 0;
510 }
511
512 void
513 gnome_accessibility_module_init (void)
514 {
515   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
516
517   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
518     {
519       adaptor_init (NULL, NULL);
520
521       if (g_getenv ("AT_SPI_DEBUG"))
522         {
523           g_print ("Atk Accessibility bridge initialized\n");
524         }
525     }
526 }
527
528 void
529 gnome_accessibility_module_shutdown (void)
530 {
531   spi_atk_deregister_event_listeners ();
532   exit_func ();
533 }
534
535 /*END------------------------------------------------------------------------*/