Merge branch 'master' into mgorse
[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 <stdarg.h>
33 #include <atk/atk.h>
34
35 #include <droute/droute.h>
36
37 #include "bridge.h"
38 #include "event.h"
39 #include "accessible-register.h"
40 #include "adaptors.h"
41
42 #include "common/spi-dbus.h"
43
44 /*
45  * Provides the path for the introspection directory.
46  */
47 #if !defined ATSPI_INTROSPECTION_PATH
48     #error "No introspection XML directory defined"
49 #endif
50
51 /*---------------------------------------------------------------------------*/
52
53 SpiAppData *atk_adaptor_app_data = NULL;
54
55 static const AtkMisc *atk_misc = NULL;
56
57 /*static Display *bridge_display = NULL;*/
58
59 /*---------------------------------------------------------------------------*/
60
61 /*
62  * Returns a 'canonicalized' value for DISPLAY,
63  * with the screen number stripped off if present.
64  *
65  * Not currently used in D-Bus version but may be
66  * useful in future if we make use of XAtom.
67  */
68 #if 0
69 static const gchar*
70 spi_display_name (void)
71 {
72     static const char *canonical_display_name = NULL;
73     if (!canonical_display_name)
74       {
75         const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
76         if (!display_env)
77           {
78             display_env = g_getenv ("DISPLAY");
79             if (!display_env || !display_env[0]) 
80                 canonical_display_name = ":0";
81             else
82               {
83                 gchar *display_p, *screen_p;
84                 canonical_display_name = g_strdup (display_env);
85                 display_p = strrchr (canonical_display_name, ':');
86                 screen_p = strrchr (canonical_display_name, '.');
87                 if (screen_p && display_p && (screen_p > display_p))
88                   {
89                     *screen_p = '\0';
90                   }
91               }
92           }
93         else
94           {
95             canonical_display_name = display_env;
96           }
97       }
98     return canonical_display_name;
99 }
100 #endif
101
102 /*---------------------------------------------------------------------------*/
103
104 /*
105  * Gets the IOR from the XDisplay.
106  * Not currently used in D-Bus version, but something similar
107  * may be employed in the future for accessing the registry daemon
108  * bus name.
109  */
110 #if 0
111 static gchar *
112 spi_atk_bridge_get_registry_ior (void)
113 {
114      Atom AT_SPI_IOR;
115      Atom actual_type;
116      int actual_format;
117      unsigned char *data = NULL;  
118      unsigned long nitems;
119      unsigned long leftover;
120      if (!bridge_display) 
121        bridge_display = XOpenDisplay (spi_display_name ());
122
123      AT_SPI_IOR = XInternAtom (bridge_display, "AT_SPI_IOR", False); 
124      XGetWindowProperty(bridge_display, 
125                         XDefaultRootWindow (bridge_display),
126                         AT_SPI_IOR, 0L,
127                         (long)BUFSIZ, False,
128                         (Atom) 31, &actual_type, &actual_format,
129                         &nitems, &leftover, &data);
130      if (data == NULL)
131          g_warning (_("AT_SPI_REGISTRY was not started at session startup."));
132      return (gchar *) data;
133 }
134 #endif
135
136 /*---------------------------------------------------------------------------*/
137
138 static void
139 register_application (SpiAppData *app)
140 {
141   DBusMessage *message;
142   DBusMessageIter iter;
143   DBusError error;
144   const char *uname;
145
146   dbus_error_init (&error);
147
148   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
149                                           SPI_DBUS_PATH_REGISTRY,
150                                           SPI_DBUS_INTERFACE_REGISTRY,
151                                           "RegisterApplication");
152   dbus_message_set_no_reply (message, TRUE);
153
154   uname = dbus_bus_get_unique_name(app->bus);
155
156   dbus_message_iter_init_append(message, &iter);
157   dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uname);
158   dbus_connection_send (app->bus, message, NULL);
159   if (message) dbus_message_unref (message);
160 }
161
162 /*---------------------------------------------------------------------------*/
163
164 static void
165 deregister_application (SpiAppData *app)
166 {
167   DBusMessage *message;
168   DBusMessageIter iter;
169   DBusError error;
170   const char *uname;
171
172   dbus_error_init (&error);
173
174   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
175                                           SPI_DBUS_PATH_REGISTRY,
176                                           SPI_DBUS_INTERFACE_REGISTRY,
177                                           "DeregisterApplication");
178   dbus_message_set_no_reply (message, TRUE);
179
180   uname = dbus_bus_get_unique_name(app->bus);
181
182   dbus_message_iter_init_append(message, &iter);
183   dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uname);
184   dbus_connection_send (app->bus, message, NULL);
185   if (message) dbus_message_unref (message);
186 }
187
188 /*---------------------------------------------------------------------------*/
189
190 static void
191 exit_func (void)
192 {
193   if (!atk_adaptor_app_data)
194     {
195       return;
196     }
197
198   spi_atk_tidy_windows ();
199   spi_atk_deregister_event_listeners();
200   deregister_application (atk_adaptor_app_data);
201
202   g_free(atk_adaptor_app_data);
203   atk_adaptor_app_data = NULL;
204
205   /* Not currently creating an XDisplay */
206 #if 0
207   if (bridge_display)
208     XCloseDisplay (bridge_display);
209 #endif
210 }
211
212 /*---------------------------------------------------------------------------*/
213
214 static gchar *atspi_dbus_name;
215 static gboolean atspi_no_register;
216
217 static GOptionEntry atspi_option_entries[] =
218 {
219   {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name, "D-Bus bus name to register as", NULL},
220   {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register, "Do not register with Registry Daemon", NULL},
221   {NULL}
222 };
223
224 /*
225  * spi_app_init
226  *
227  * The following needs to be initialized.
228  *
229  * - DRoute for routing message to their accessible objects.
230  * - Event handlers for emmitting signals on specific ATK events.
231  * - Application registration with the AT-SPI registry.
232  *
233  */
234 static int
235 adaptor_init (gint *argc, gchar **argv[])
236 {
237   GOptionContext *opt;
238   GError *err = NULL;
239   DBusError error;
240   DBusConnection *bus;
241   AtkObject *root;
242   gchar *introspection_directory;
243   static gboolean inited = FALSE;
244
245   if (inited)
246     return 0;
247
248   inited = TRUE;
249
250   DRoutePath *treepath, *accpath;
251
252   root = atk_get_root ();
253   g_return_val_if_fail (root, 0);
254
255   /* Parse command line options */
256   opt = g_option_context_new(NULL);
257   g_option_context_add_main_entries(opt, atspi_option_entries, NULL);
258   g_option_context_set_ignore_unknown_options(opt, TRUE);
259   if (!g_option_context_parse(opt, argc, argv, &err))
260       g_warning("AT-SPI Option parsing failed: %s\n", err->message);
261
262   /* Allocate global data and do ATK initializations */
263   atk_adaptor_app_data = g_new0 (SpiAppData, 1);
264   atk_misc = atk_misc_get_instance ();
265   atk_adaptor_app_data->root = root;
266
267   /* Set up D-Bus connection and register bus name */
268   dbus_error_init (&error);
269   atk_adaptor_app_data->bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
270   if (!atk_adaptor_app_data->bus)
271   {
272     g_warning ("AT-SPI Couldn't connect to D-Bus: %s\n", error.message);
273     g_free(atk_adaptor_app_data);
274     atk_adaptor_app_data = NULL;
275     return 0;
276   }
277   if (atspi_dbus_name != NULL &&
278       dbus_bus_request_name(atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
279   {
280     g_print("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
281   }
282
283   dbus_connection_setup_with_g_main(atk_adaptor_app_data->bus, g_main_context_default());
284
285   /* Get D-Bus introspection directory */
286   introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
287   if (introspection_directory == NULL)
288       introspection_directory = ATSPI_INTROSPECTION_PATH;
289
290   /* Register droute for routing AT-SPI messages */
291   atk_adaptor_app_data->droute = droute_new (atk_adaptor_app_data->bus, introspection_directory);
292
293   treepath = droute_add_one (atk_adaptor_app_data->droute,
294                              "/org/freedesktop/atspi/tree",
295                              NULL);
296
297   accpath = droute_add_many (atk_adaptor_app_data->droute,
298                              "/org/freedesktop/atspi/accessible",
299                              NULL,
300                              (DRouteGetDatumFunction) atk_dbus_path_to_gobject);
301
302   /* Register all interfaces with droute and set up application accessible db */
303   spi_initialize_tree (treepath);
304
305   spi_initialize_accessible (accpath);
306   spi_initialize_application (accpath);
307   spi_initialize_action(accpath);
308   spi_initialize_collection (accpath);
309   spi_initialize_component (accpath);
310   spi_initialize_document (accpath);
311   spi_initialize_editabletext (accpath);
312   spi_initialize_hyperlink (accpath);
313   spi_initialize_hypertext (accpath);
314   spi_initialize_image (accpath);
315   spi_initialize_selection (accpath);
316   spi_initialize_table (accpath);
317   spi_initialize_text (accpath);
318   spi_initialize_value (accpath);
319
320   /* Initialize the AtkObject registration */
321   atk_dbus_initialize (atk_adaptor_app_data->root);
322
323   /* Register methods to send D-Bus signals on certain ATK events */
324   spi_atk_register_event_listeners ();
325
326   /* Register this app by sending a signal out to AT-SPI registry daemon */
327   register_application (atk_adaptor_app_data);
328
329   g_atexit (exit_func);
330
331   return 0;
332 }
333
334 /*---------------------------------------------------------------------------*/
335
336 int
337 gtk_module_init (gint *argc, gchar **argv[])
338 {
339   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
340
341   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
342     {
343         return adaptor_init (argc, argv);
344     }
345   return 0;
346 }
347
348 void
349 gnome_accessibility_module_init (void)
350 {
351   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
352
353   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
354     {
355       adaptor_init (NULL, NULL);
356
357       if (g_getenv ("AT_SPI_DEBUG"))
358         {
359             g_print("Atk Accessibility bridge initialized\n");
360         }
361     }
362 }
363
364 void
365 gnome_accessibility_module_shutdown (void)
366 {
367   spi_atk_deregister_event_listeners ();
368   exit_func ();
369 }
370 /*END------------------------------------------------------------------------*/