Merge branch 'mgorse' into socketplug
[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 #ifdef __ATK_PLUG_H__
215 static AtkPlugClass *plug_class;
216 static AtkSocketClass *socket_class;
217
218 static gchar *
219 get_plug_id (AtkPlug *plug)
220 {
221   const char *uname = dbus_bus_get_unique_name(atk_adaptor_app_data->bus);
222   gchar *path;
223   GString *str = g_string_new (NULL);
224
225   path = atk_dbus_object_to_path (ATK_OBJECT(plug), TRUE);
226   g_string_printf (str, "%s:%s", uname, path);
227   g_free (path);
228   return g_string_free (str, FALSE);
229 }
230
231 static void
232 socket_embed_hook (AtkSocket *socket, gchar *plug_id)
233 {
234   AtkObject *accessible = ATK_OBJECT(socket);
235   /* Force registration */
236   gchar *path = atk_dbus_object_to_path (accessible, TRUE);
237   spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
238   g_free (path);
239 }
240
241 static void
242 install_plug_hooks ()
243 {
244   gpointer data;
245   
246   data = g_type_class_ref (ATK_TYPE_PLUG);
247   plug_class = ATK_PLUG_CLASS (data);
248   data = g_type_class_ref (ATK_TYPE_SOCKET);
249   socket_class = ATK_SOCKET_CLASS (data);
250   plug_class->get_object_id = get_plug_id;
251   socket_class->embed = socket_embed_hook;
252 }
253 #endif
254
255 static gchar *atspi_dbus_name;
256 static gboolean atspi_no_register;
257
258 static GOptionEntry atspi_option_entries[] =
259 {
260   {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name, "D-Bus bus name to register as", NULL},
261   {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register, "Do not register with Registry Daemon", NULL},
262   {NULL}
263 };
264
265 /*
266  * spi_app_init
267  *
268  * The following needs to be initialized.
269  *
270  * - DRoute for routing message to their accessible objects.
271  * - Event handlers for emmitting signals on specific ATK events.
272  * - Application registration with the AT-SPI registry.
273  *
274  */
275 static int
276 adaptor_init (gint *argc, gchar **argv[])
277 {
278   GOptionContext *opt;
279   GError *err = NULL;
280   DBusError error;
281   DBusConnection *bus;
282   AtkObject *root;
283   gchar *introspection_directory;
284   static gboolean inited = FALSE;
285
286   if (inited)
287     return 0;
288
289   inited = TRUE;
290
291   DRoutePath *treepath, *accpath;
292
293   root = atk_get_root ();
294   g_return_val_if_fail (root, 0);
295
296   /* Parse command line options */
297   opt = g_option_context_new(NULL);
298   g_option_context_add_main_entries(opt, atspi_option_entries, NULL);
299   g_option_context_set_ignore_unknown_options(opt, TRUE);
300   if (!g_option_context_parse(opt, argc, argv, &err))
301       g_warning("AT-SPI Option parsing failed: %s\n", err->message);
302
303   /* Allocate global data and do ATK initializations */
304   atk_adaptor_app_data = g_new0 (SpiAppData, 1);
305   atk_misc = atk_misc_get_instance ();
306   atk_adaptor_app_data->root = root;
307
308   /* Set up D-Bus connection and register bus name */
309   dbus_error_init (&error);
310   atk_adaptor_app_data->bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
311   if (!atk_adaptor_app_data->bus)
312   {
313     g_warning ("AT-SPI Couldn't connect to D-Bus: %s\n", error.message);
314     g_free(atk_adaptor_app_data);
315     atk_adaptor_app_data = NULL;
316     return 0;
317   }
318   if (atspi_dbus_name != NULL &&
319       dbus_bus_request_name(atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
320   {
321     g_print("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
322   }
323
324   dbus_connection_setup_with_g_main(atk_adaptor_app_data->bus, g_main_context_default());
325
326   /* Get D-Bus introspection directory */
327   introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
328   if (introspection_directory == NULL)
329       introspection_directory = ATSPI_INTROSPECTION_PATH;
330
331   /* Register droute for routing AT-SPI messages */
332   atk_adaptor_app_data->droute = droute_new (atk_adaptor_app_data->bus, introspection_directory);
333
334   treepath = droute_add_one (atk_adaptor_app_data->droute,
335                              "/org/freedesktop/atspi/tree",
336                              NULL);
337
338   accpath = droute_add_many (atk_adaptor_app_data->droute,
339                              "/org/freedesktop/atspi/accessible",
340                              NULL,
341                              (DRouteGetDatumFunction) atk_dbus_path_to_gobject);
342
343   /* Register all interfaces with droute and set up application accessible db */
344   spi_initialize_tree (treepath);
345
346   spi_initialize_accessible (accpath);
347   spi_initialize_application (accpath);
348   spi_initialize_action(accpath);
349   spi_initialize_collection (accpath);
350   spi_initialize_component (accpath);
351   spi_initialize_document (accpath);
352   spi_initialize_editabletext (accpath);
353   spi_initialize_hyperlink (accpath);
354   spi_initialize_hypertext (accpath);
355   spi_initialize_image (accpath);
356   spi_initialize_selection (accpath);
357   spi_initialize_table (accpath);
358   spi_initialize_text (accpath);
359   spi_initialize_value (accpath);
360
361   /* Initialize the AtkObject registration */
362   atk_dbus_initialize (atk_adaptor_app_data->root);
363
364   /* Register methods to send D-Bus signals on certain ATK events */
365   spi_atk_register_event_listeners ();
366
367 #ifdef __ATK_PLUG_H__
368   /* Hook our plug-and socket functions */
369   install_plug_hooks ();
370 #endif
371
372   /* Register this app by sending a signal out to AT-SPI registry daemon */
373   register_application (atk_adaptor_app_data);
374
375   g_atexit (exit_func);
376
377   return 0;
378 }
379
380 /*---------------------------------------------------------------------------*/
381
382 int
383 gtk_module_init (gint *argc, gchar **argv[])
384 {
385   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
386
387   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
388     {
389         return adaptor_init (argc, argv);
390     }
391   return 0;
392 }
393
394 void
395 gnome_accessibility_module_init (void)
396 {
397   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
398
399   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
400     {
401       adaptor_init (NULL, NULL);
402
403       if (g_getenv ("AT_SPI_DEBUG"))
404         {
405             g_print("Atk Accessibility bridge initialized\n");
406         }
407     }
408 }
409
410 void
411 gnome_accessibility_module_shutdown (void)
412 {
413   spi_atk_deregister_event_listeners ();
414   exit_func ();
415 }
416 /*END------------------------------------------------------------------------*/