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