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