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