Code style enforcement.
[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
139         ("AT-SPI: Accessibility bus not found - Using session bus.\n");
140       bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
141       if (!bus)
142         g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
143     }
144   else
145     {
146       bus = dbus_connection_open (data, &error);
147       if (!bus)
148         {
149           g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
150         }
151       else
152         {
153           if (!dbus_bus_register (bus, &error))
154             g_error ("AT-SPI: Couldn't register with bus: %s\n");
155         }
156     }
157
158   return bus;
159 }
160
161 /*---------------------------------------------------------------------------*/
162
163 static void
164 register_application (SpiAppData * app)
165 {
166   DBusMessage *message;
167   DBusMessageIter iter;
168   DBusError error;
169   const char *uname = NULL;
170
171   dbus_error_init (&error);
172
173   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
174                                           SPI_DBUS_PATH_REGISTRY,
175                                           SPI_DBUS_INTERFACE_REGISTRY,
176                                           "RegisterApplication");
177   dbus_message_set_no_reply (message, TRUE);
178
179   uname = dbus_bus_get_unique_name (app->bus);
180   if (!uname)
181     {
182       g_error ("AT-SPI: Couldn't get unique name for this connection");
183     }
184
185   dbus_message_iter_init_append (message, &iter);
186   dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
187   dbus_connection_send (app->bus, message, NULL);
188   if (message)
189     dbus_message_unref (message);
190 }
191
192 /*---------------------------------------------------------------------------*/
193
194 static void
195 deregister_application (SpiAppData * app)
196 {
197   DBusMessage *message;
198   DBusMessageIter iter;
199   DBusError error;
200   const char *uname;
201
202   dbus_error_init (&error);
203
204   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
205                                           SPI_DBUS_PATH_REGISTRY,
206                                           SPI_DBUS_INTERFACE_REGISTRY,
207                                           "DeregisterApplication");
208   dbus_message_set_no_reply (message, TRUE);
209
210   uname = dbus_bus_get_unique_name (app->bus);
211
212   dbus_message_iter_init_append (message, &iter);
213   dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
214   dbus_connection_send (app->bus, message, NULL);
215   if (message)
216     dbus_message_unref (message);
217 }
218
219 /*---------------------------------------------------------------------------*/
220
221 static void
222 exit_func (void)
223 {
224   if (!atk_adaptor_app_data)
225     {
226       return;
227     }
228
229   spi_atk_tidy_windows ();
230   spi_atk_deregister_event_listeners ();
231   deregister_application (atk_adaptor_app_data);
232
233   g_free (atk_adaptor_app_data);
234   atk_adaptor_app_data = NULL;
235
236   /* Not currently creating an XDisplay */
237 #if 0
238   if (bridge_display)
239     XCloseDisplay (bridge_display);
240 #endif
241 }
242
243 /*---------------------------------------------------------------------------*/
244
245 #ifdef __ATK_PLUG_H__
246 static AtkPlugClass *plug_class;
247 static AtkSocketClass *socket_class;
248
249 static gchar *
250 get_plug_id (AtkPlug * plug)
251 {
252   const char *uname = dbus_bus_get_unique_name (atk_adaptor_app_data->bus);
253   gchar *path;
254   GString *str = g_string_new (NULL);
255
256   path = atk_dbus_object_to_path (ATK_OBJECT (plug), TRUE);
257   g_string_printf (str, "%s:%s", uname, path);
258   g_free (path);
259   return g_string_free (str, FALSE);
260 }
261
262 static void
263 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
264 {
265   AtkObject *accessible = ATK_OBJECT(socket);
266   gchar *plug_name, *plug_path;
267
268   /* Force registration */
269   gchar *path = atk_dbus_object_to_path (accessible, TRUE);
270   spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
271   /* Let the plug know that it has been embedded */
272   plug_name = g_strdup (plug_id);
273   if (!plug_name)
274     {
275       g_free (path);
276       return;
277     }
278   plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
279   if (plug_path)
280     {
281       DBusMessage *message;
282       *(plug_path++) = '\0';
283       message = dbus_message_new_method_call (plug_name, plug_path, "org.freedesktop.atspi.Accessible", "Embedded");
284       dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
285       dbus_connection_send (atk_adaptor_app_data->bus, message, NULL);
286     }
287   g_free (plug_name);
288   g_free (path);
289 }
290
291 static void
292 install_plug_hooks ()
293 {
294   gpointer data;
295
296   data = g_type_class_ref (ATK_TYPE_PLUG);
297   plug_class = ATK_PLUG_CLASS (data);
298   data = g_type_class_ref (ATK_TYPE_SOCKET);
299   socket_class = ATK_SOCKET_CLASS (data);
300   plug_class->get_object_id = get_plug_id;
301   socket_class->embed = socket_embed_hook;
302 }
303 #endif
304
305 gchar *atspi_dbus_name = NULL;
306 static gboolean atspi_no_register = FALSE;
307
308 static GOptionEntry atspi_option_entries[] = {
309   {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
310    "D-Bus bus name to register as", NULL},
311   {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
312    "Do not register with Registry Daemon", NULL},
313   {NULL}
314 };
315
316 /*
317  * spi_app_init
318  *
319  * The following needs to be initialized.
320  *
321  * - DRoute for routing message to their accessible objects.
322  * - Event handlers for emmitting signals on specific ATK events.
323  * - Application registration with the AT-SPI registry.
324  *
325  */
326 static int
327 adaptor_init (gint * argc, gchar ** argv[])
328 {
329   GOptionContext *opt;
330   GError *err = NULL;
331   DBusError error;
332   DBusConnection *bus;
333   AtkObject *root;
334   gchar *introspection_directory;
335   static gboolean inited = FALSE;
336
337   if (inited)
338     return 0;
339
340   inited = TRUE;
341
342   DRoutePath *treepath, *accpath;
343
344   root = atk_get_root ();
345   g_return_val_if_fail (root, 0);
346
347   /* Parse command line options */
348   opt = g_option_context_new (NULL);
349   g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
350   g_option_context_set_ignore_unknown_options (opt, TRUE);
351   if (!g_option_context_parse (opt, argc, argv, &err))
352     g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
353
354   /* Allocate global data and do ATK initializations */
355   atk_adaptor_app_data = g_new0 (SpiAppData, 1);
356   atk_misc = atk_misc_get_instance ();
357   atk_adaptor_app_data->root = root;
358
359   /* Set up D-Bus connection and register bus name */
360   dbus_error_init (&error);
361   atk_adaptor_app_data->bus = spi_atk_bridge_get_bus ();
362   if (!atk_adaptor_app_data->bus)
363     {
364       g_free (atk_adaptor_app_data);
365       atk_adaptor_app_data = NULL;
366       return 0;
367     }
368
369   if (atspi_dbus_name != NULL)
370     {
371       if (dbus_bus_request_name
372           (atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
373         {
374           g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
375         }
376       else
377         {
378           g_print
379             ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
380              atspi_dbus_name);
381         }
382     }
383
384   dbus_connection_setup_with_g_main (atk_adaptor_app_data->bus,
385                                      g_main_context_default ());
386
387   /* Get D-Bus introspection directory */
388   introspection_directory = (char *) g_getenv ("ATSPI_INTROSPECTION_PATH");
389   if (introspection_directory == NULL)
390     introspection_directory = ATSPI_INTROSPECTION_PATH;
391
392   /* Register droute for routing AT-SPI messages */
393   atk_adaptor_app_data->droute =
394     droute_new (atk_adaptor_app_data->bus, introspection_directory);
395
396   treepath = droute_add_one (atk_adaptor_app_data->droute,
397                              "/org/freedesktop/atspi/tree", NULL);
398
399   accpath = droute_add_many (atk_adaptor_app_data->droute,
400                              "/org/freedesktop/atspi/accessible",
401                              NULL,
402                              (DRouteGetDatumFunction)
403                              atk_dbus_path_to_gobject);
404
405   /* Register all interfaces with droute and set up application accessible db */
406   spi_initialize_tree (treepath);
407
408   spi_initialize_accessible (accpath);
409   spi_initialize_application (accpath);
410   spi_initialize_action (accpath);
411   spi_initialize_collection (accpath);
412   spi_initialize_component (accpath);
413   spi_initialize_document (accpath);
414   spi_initialize_editabletext (accpath);
415   spi_initialize_hyperlink (accpath);
416   spi_initialize_hypertext (accpath);
417   spi_initialize_image (accpath);
418   spi_initialize_selection (accpath);
419   spi_initialize_table (accpath);
420   spi_initialize_text (accpath);
421   spi_initialize_value (accpath);
422
423   /* Initialize the AtkObject registration */
424   atk_dbus_initialize (atk_adaptor_app_data->root);
425
426   /* Register methods to send D-Bus signals on certain ATK events */
427   spi_atk_register_event_listeners ();
428
429 #ifdef __ATK_PLUG_H__
430   /* Hook our plug-and socket functions */
431   install_plug_hooks ();
432 #endif
433
434   /* Register this app by sending a signal out to AT-SPI registry daemon */
435   if (!atspi_no_register)
436     register_application (atk_adaptor_app_data);
437
438   g_atexit (exit_func);
439
440   return 0;
441 }
442
443 /*---------------------------------------------------------------------------*/
444
445 int
446 gtk_module_init (gint * argc, gchar ** argv[])
447 {
448   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
449
450   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
451     {
452       return adaptor_init (argc, argv);
453     }
454   return 0;
455 }
456
457 void
458 gnome_accessibility_module_init (void)
459 {
460   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
461
462   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
463     {
464       adaptor_init (NULL, NULL);
465
466       if (g_getenv ("AT_SPI_DEBUG"))
467         {
468           g_print ("Atk Accessibility bridge initialized\n");
469         }
470     }
471 }
472
473 void
474 gnome_accessibility_module_shutdown (void)
475 {
476   spi_atk_deregister_event_listeners ();
477   exit_func ();
478 }
479
480 /*END------------------------------------------------------------------------*/