Plug/socket fixes, and remove conditional
[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 static AtkPlugClass *plug_class;
270 static AtkSocketClass *socket_class;
271
272 static gchar *
273 get_plug_id (AtkPlug * plug)
274 {
275   const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
276   gchar *path;
277   GString *str = g_string_new (NULL);
278
279   path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
280   g_string_printf (str, "%s:%s", uname, path);
281   g_free (path);
282   return g_string_free (str, FALSE);
283 }
284
285 static void
286 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
287 {
288   AtkObject *accessible = ATK_OBJECT(socket);
289   gchar *plug_name, *plug_path;
290
291   /* Force registration */
292   gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
293   /* Let the plug know that it has been embedded */
294   plug_name = g_strdup (plug_id);
295   if (!plug_name)
296     {
297       g_free (path);
298       return;
299     }
300   plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
301   if (plug_path)
302     {
303       DBusMessage *message;
304       *(plug_path++) = '\0';
305       message = dbus_message_new_method_call (plug_name, plug_path, "org.freedesktop.atspi.Accessible", "Embedded");
306       dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
307       dbus_connection_send (spi_global_app_data->bus, message, NULL);
308     }
309   g_free (plug_name);
310   g_free (path);
311 }
312
313 static void
314 install_plug_hooks ()
315 {
316   gpointer data;
317
318   data = g_type_class_ref (ATK_TYPE_PLUG);
319   plug_class = ATK_PLUG_CLASS (data);
320   data = g_type_class_ref (ATK_TYPE_SOCKET);
321   socket_class = ATK_SOCKET_CLASS (data);
322   plug_class->get_object_id = get_plug_id;
323   socket_class->embed = socket_embed_hook;
324 }
325
326 gchar *atspi_dbus_name = NULL;
327 static gboolean atspi_no_register = FALSE;
328
329 static GOptionEntry atspi_option_entries[] = {
330   {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
331    "D-Bus bus name to register as", NULL},
332   {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
333    "Do not register with Registry Daemon", NULL},
334   {NULL}
335 };
336
337 /*
338  * spi_app_init
339  *
340  * The following needs to be initialized.
341  *
342  * - DRoute for routing message to their accessible objects.
343  * - Event handlers for emmitting signals on specific ATK events.
344  * - Application registration with the AT-SPI registry.
345  *
346  */
347 static int
348 adaptor_init (gint * argc, gchar ** argv[])
349 {
350   GOptionContext *opt;
351   GError *err = NULL;
352   DBusError error;
353   DBusConnection *bus;
354   AtkObject *root;
355   gchar *introspection_directory;
356   static gboolean inited = FALSE;
357
358   if (inited)
359     return 0;
360
361   inited = TRUE;
362
363   DRoutePath *treepath, *accpath;
364
365   root = atk_get_root ();
366   g_return_val_if_fail (root, 0);
367
368   /* Parse command line options */
369   opt = g_option_context_new (NULL);
370   g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
371   g_option_context_set_ignore_unknown_options (opt, TRUE);
372   if (!g_option_context_parse (opt, argc, argv, &err))
373     g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
374
375   /* Allocate global data and do ATK initializations */
376   spi_global_app_data = g_new0 (SpiBridge, 1);
377   atk_misc = atk_misc_get_instance ();
378   spi_global_app_data->root = g_object_ref (root);
379
380   /* Set up D-Bus connection and register bus name */
381   dbus_error_init (&error);
382   spi_global_app_data->bus = spi_atk_bridge_get_bus ();
383   if (!spi_global_app_data->bus)
384     {
385       g_free (spi_global_app_data);
386       spi_global_app_data = NULL;
387       return 0;
388     }
389
390   if (atspi_dbus_name != NULL)
391     {
392       if (dbus_bus_request_name
393           (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
394         {
395           g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
396         }
397       else
398         {
399           g_print
400             ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
401              atspi_dbus_name);
402         }
403     }
404
405   dbus_connection_setup_with_g_main (spi_global_app_data->bus,
406                                      g_main_context_default ());
407
408   /* 
409    * Create the leasing, register and cache objects.
410    * The order is important here, the cache depends on the
411    * register object.
412    */
413   spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
414   spi_global_leasing  = g_object_new (SPI_LEASING_TYPE, NULL);
415   spi_global_cache    = g_object_new (SPI_CACHE_TYPE, NULL);
416
417   /* Get D-Bus introspection directory */
418   introspection_directory = (char *) g_getenv ("ATSPI_INTROSPECTION_PATH");
419   if (introspection_directory == NULL)
420     introspection_directory = ATSPI_INTROSPECTION_PATH;
421
422   /* Register droute for routing AT-SPI messages */
423   spi_global_app_data->droute =
424     droute_new (spi_global_app_data->bus, introspection_directory);
425
426   treepath = droute_add_one (spi_global_app_data->droute,
427                              "/org/at_spi/cache", spi_global_cache);
428
429   accpath = droute_add_many (spi_global_app_data->droute,
430                              "/org/freedesktop/atspi/accessible",
431                              NULL,
432                              (DRouteGetDatumFunction)
433                              spi_global_register_path_to_object);
434
435
436   /* Register all interfaces with droute and set up application accessible db */
437   spi_initialize_cache (treepath);
438   spi_initialize_accessible (accpath);
439   spi_initialize_application (accpath);
440   spi_initialize_action (accpath);
441   spi_initialize_collection (accpath);
442   spi_initialize_component (accpath);
443   spi_initialize_document (accpath);
444   spi_initialize_editabletext (accpath);
445   spi_initialize_hyperlink (accpath);
446   spi_initialize_hypertext (accpath);
447   spi_initialize_image (accpath);
448   spi_initialize_selection (accpath);
449   spi_initialize_table (accpath);
450   spi_initialize_text (accpath);
451   spi_initialize_value (accpath);
452
453   /* Register methods to send D-Bus signals on certain ATK events */
454   spi_atk_register_event_listeners ();
455
456   /* Hook our plug-and socket functions */
457   install_plug_hooks ();
458
459   /* Register this app by sending a signal out to AT-SPI registry daemon */
460   if (!atspi_no_register)
461     register_application (spi_global_app_data);
462
463   g_atexit (exit_func);
464
465   return 0;
466 }
467
468 /*---------------------------------------------------------------------------*/
469
470 int
471 gtk_module_init (gint * argc, gchar ** argv[])
472 {
473   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
474
475   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
476     {
477       return adaptor_init (argc, argv);
478     }
479   return 0;
480 }
481
482 void
483 gnome_accessibility_module_init (void)
484 {
485   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
486
487   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
488     {
489       adaptor_init (NULL, NULL);
490
491       if (g_getenv ("AT_SPI_DEBUG"))
492         {
493           g_print ("Atk Accessibility bridge initialized\n");
494         }
495     }
496 }
497
498 void
499 gnome_accessibility_module_shutdown (void)
500 {
501   spi_atk_deregister_event_listeners ();
502   exit_func ();
503 }
504
505 /*END------------------------------------------------------------------------*/