Modify the paths scheme.
[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;
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   dbus_message_set_no_reply (message, TRUE);
182
183   dbus_message_iter_init_append (message, &iter);
184   spi_object_append_reference (&iter, app->root);
185   dbus_connection_send (app->bus, message, NULL);
186   if (message)
187     dbus_message_unref (message);
188 }
189
190 /*---------------------------------------------------------------------------*/
191
192 static void
193 deregister_application (SpiBridge * app)
194 {
195   DBusMessage *message;
196   DBusMessageIter iter;
197   DBusError error;
198   const char *uname;
199
200   dbus_error_init (&error);
201
202   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
203                                           SPI_DBUS_PATH_REGISTRY,
204                                           SPI_DBUS_INTERFACE_REGISTRY,
205                                           "DeregisterApplication");
206   dbus_message_set_no_reply (message, TRUE);
207
208   uname = dbus_bus_get_unique_name (app->bus);
209
210   dbus_message_iter_init_append (message, &iter);
211   dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
212   dbus_connection_send (app->bus, message, NULL);
213   if (message)
214     dbus_message_unref (message);
215 }
216
217 /*---------------------------------------------------------------------------*/
218
219 static void
220 exit_func (void)
221 {
222   if (!spi_global_app_data)
223     {
224       return;
225     }
226
227   spi_atk_tidy_windows ();
228   spi_atk_deregister_event_listeners ();
229   deregister_application (spi_global_app_data);
230
231   g_free (spi_global_app_data);
232   spi_global_app_data = NULL;
233
234   /* Not currently creating an XDisplay */
235 #if 0
236   if (bridge_display)
237     XCloseDisplay (bridge_display);
238 #endif
239 }
240
241 /*---------------------------------------------------------------------------*/
242
243 #ifdef SPI_ATK_PLUG_SOCKET
244 static AtkPlugClass *plug_class;
245 static AtkSocketClass *socket_class;
246
247 static gchar *
248 get_plug_id (AtkPlug * plug)
249 {
250   const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
251   gchar *path;
252   GString *str = g_string_new (NULL);
253
254   path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
255   g_string_printf (str, "%s:%s", uname, path);
256   g_free (path);
257   return g_string_free (str, FALSE);
258 }
259
260 static void
261 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
262 {
263   AtkObject *accessible = ATK_OBJECT(socket);
264   gchar *plug_name, *plug_path;
265
266   /* Force registration */
267   gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
268   /* Let the plug know that it has been embedded */
269   plug_name = g_strdup (plug_id);
270   if (!plug_name)
271     {
272       g_free (path);
273       return;
274     }
275   plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
276   if (plug_path)
277     {
278       DBusMessage *message;
279       *(plug_path++) = '\0';
280       message = dbus_message_new_method_call (plug_name, plug_path, "org.freedesktop.atspi.Accessible", "Embedded");
281       dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
282       dbus_connection_send (spi_global_app_data->bus, message, NULL);
283     }
284   g_free (plug_name);
285   g_free (path);
286 }
287
288 static void
289 install_plug_hooks ()
290 {
291   gpointer data;
292
293   data = g_type_class_ref (ATK_TYPE_PLUG);
294   plug_class = ATK_PLUG_CLASS (data);
295   data = g_type_class_ref (ATK_TYPE_SOCKET);
296   socket_class = ATK_SOCKET_CLASS (data);
297   plug_class->get_object_id = get_plug_id;
298   socket_class->embed = socket_embed_hook;
299 }
300 #endif
301
302 gchar *atspi_dbus_name = NULL;
303 static gboolean atspi_no_register = FALSE;
304
305 static GOptionEntry atspi_option_entries[] = {
306   {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
307    "D-Bus bus name to register as", NULL},
308   {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
309    "Do not register with Registry Daemon", NULL},
310   {NULL}
311 };
312
313 /*
314  * spi_app_init
315  *
316  * The following needs to be initialized.
317  *
318  * - DRoute for routing message to their accessible objects.
319  * - Event handlers for emmitting signals on specific ATK events.
320  * - Application registration with the AT-SPI registry.
321  *
322  */
323 static int
324 adaptor_init (gint * argc, gchar ** argv[])
325 {
326   GOptionContext *opt;
327   GError *err = NULL;
328   DBusError error;
329   DBusConnection *bus;
330   AtkObject *root;
331   gchar *introspection_directory;
332   static gboolean inited = FALSE;
333
334   if (inited)
335     return 0;
336
337   inited = TRUE;
338
339   DRoutePath *treepath, *accpath;
340
341   root = atk_get_root ();
342   g_return_val_if_fail (root, 0);
343
344   /* Parse command line options */
345   opt = g_option_context_new (NULL);
346   g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
347   g_option_context_set_ignore_unknown_options (opt, TRUE);
348   if (!g_option_context_parse (opt, argc, argv, &err))
349     g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
350
351   /* Allocate global data and do ATK initializations */
352   spi_global_app_data = g_new0 (SpiBridge, 1);
353   atk_misc = atk_misc_get_instance ();
354   spi_global_app_data->root = g_object_ref (root);
355
356   /* Set up D-Bus connection and register bus name */
357   dbus_error_init (&error);
358   spi_global_app_data->bus = spi_atk_bridge_get_bus ();
359   if (!spi_global_app_data->bus)
360     {
361       g_free (spi_global_app_data);
362       spi_global_app_data = NULL;
363       return 0;
364     }
365
366   if (atspi_dbus_name != NULL)
367     {
368       if (dbus_bus_request_name
369           (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
370         {
371           g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
372         }
373       else
374         {
375           g_print
376             ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
377              atspi_dbus_name);
378         }
379     }
380
381   dbus_connection_setup_with_g_main (spi_global_app_data->bus,
382                                      g_main_context_default ());
383
384   /* 
385    * Create the leasing, register and cache objects.
386    * The order is important here, the cache depends on the
387    * register object.
388    */
389   spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
390   spi_global_leasing  = g_object_new (SPI_LEASING_TYPE, NULL);
391   spi_global_cache    = g_object_new (SPI_CACHE_TYPE, NULL);
392
393   /* Get D-Bus introspection directory */
394   introspection_directory = (char *) g_getenv ("ATSPI_INTROSPECTION_PATH");
395   if (introspection_directory == NULL)
396     introspection_directory = ATSPI_INTROSPECTION_PATH;
397
398   /* Register droute for routing AT-SPI messages */
399   spi_global_app_data->droute =
400     droute_new (spi_global_app_data->bus, introspection_directory);
401
402   treepath = droute_add_one (spi_global_app_data->droute,
403                              "/org/at_spi/cache", spi_global_cache);
404
405   accpath = droute_add_many (spi_global_app_data->droute,
406                              "/org/freedesktop/atspi/accessible",
407                              NULL,
408                              (DRouteGetDatumFunction)
409                              spi_global_register_path_to_object);
410
411
412   /* Register all interfaces with droute and set up application accessible db */
413   spi_initialize_cache (treepath);
414   spi_initialize_accessible (accpath);
415   spi_initialize_application (accpath);
416   spi_initialize_action (accpath);
417   spi_initialize_collection (accpath);
418   spi_initialize_component (accpath);
419   spi_initialize_document (accpath);
420   spi_initialize_editabletext (accpath);
421   spi_initialize_hyperlink (accpath);
422   spi_initialize_hypertext (accpath);
423   spi_initialize_image (accpath);
424   spi_initialize_selection (accpath);
425   spi_initialize_table (accpath);
426   spi_initialize_text (accpath);
427   spi_initialize_value (accpath);
428
429   /* Register methods to send D-Bus signals on certain ATK events */
430   spi_atk_register_event_listeners ();
431
432 #ifdef SPI_ATK_PLUG_SOCKET
433   /* Hook our plug-and socket functions */
434   install_plug_hooks ();
435 #endif
436
437   /* Register this app by sending a signal out to AT-SPI registry daemon */
438   if (!atspi_no_register)
439     register_application (spi_global_app_data);
440
441   g_atexit (exit_func);
442
443   return 0;
444 }
445
446 /*---------------------------------------------------------------------------*/
447
448 int
449 gtk_module_init (gint * argc, gchar ** argv[])
450 {
451   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
452
453   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
454     {
455       return adaptor_init (argc, argv);
456     }
457   return 0;
458 }
459
460 void
461 gnome_accessibility_module_init (void)
462 {
463   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
464
465   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
466     {
467       adaptor_init (NULL, NULL);
468
469       if (g_getenv ("AT_SPI_DEBUG"))
470         {
471           g_print ("Atk Accessibility bridge initialized\n");
472         }
473     }
474 }
475
476 void
477 gnome_accessibility_module_shutdown (void)
478 {
479   spi_atk_deregister_event_listeners ();
480   exit_func ();
481 }
482
483 /*END------------------------------------------------------------------------*/