eb2fa759b9447f3c4566b67718ed91b366d167f1
[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 "spi-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
244   DRoutePath *treepath, *accpath;
245
246   root = atk_get_root ();
247   g_return_val_if_fail (root, 0);
248
249   /* Parse command line options */
250   opt = g_option_context_new(NULL);
251   g_option_context_add_main_entries(opt, atspi_option_entries, NULL);
252   g_option_context_set_ignore_unknown_options(opt, TRUE);
253   if (!g_option_context_parse(opt, argc, argv, &err))
254       g_warning("AT-SPI Option parsing failed: %s\n", err->message);
255
256   /* Allocate global data and do ATK initializations */
257   atk_adaptor_app_data = g_new0 (SpiAppData, 1);
258   atk_misc = atk_misc_get_instance ();
259   atk_adaptor_app_data->root = root;
260
261   /* Set up D-Bus connection and register bus name */
262   dbus_error_init (&error);
263   atk_adaptor_app_data->bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
264   if (!atk_adaptor_app_data->bus)
265   {
266     g_warning ("AT-SPI Couldn't connect to D-Bus: %s\n", error.message);
267     g_free(atk_adaptor_app_data);
268     atk_adaptor_app_data = NULL;
269     return 0;
270   }
271   if (atspi_dbus_name != NULL &&
272       dbus_bus_request_name(atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
273   {
274     g_print("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
275   }
276
277   dbus_connection_setup_with_g_main(atk_adaptor_app_data->bus, g_main_context_default());
278
279   /* Get D-Bus introspection directory */
280   introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
281   if (introspection_directory == NULL)
282       introspection_directory = ATSPI_INTROSPECTION_PATH;
283
284   /* Register droute for routing AT-SPI messages */
285   atk_adaptor_app_data->droute = droute_new (atk_adaptor_app_data->bus, introspection_directory);
286
287   treepath = droute_add_one (atk_adaptor_app_data->droute,
288                              "/org/freedesktop/atspi/tree",
289                              NULL);
290
291   accpath = droute_add_many (atk_adaptor_app_data->droute,
292                              "/org/freedesktop/atspi/accessible",
293                              NULL,
294                              (DRouteGetDatumFunction) atk_dbus_path_to_object);
295
296   /* Register all interfaces with droute and set up application accessible db */
297   spi_initialize_tree (treepath);
298
299   spi_initialize_accessible (accpath);
300   spi_initialize_application (accpath);
301   spi_initialize_action(accpath);
302   spi_initialize_collection (accpath);
303   spi_initialize_component (accpath);
304   spi_initialize_document (accpath);
305   spi_initialize_editabletext (accpath);
306   spi_initialize_hyperlink (accpath);
307   spi_initialize_hypertext (accpath);
308   spi_initialize_image (accpath);
309   spi_initialize_selection (accpath);
310   spi_initialize_table (accpath);
311   spi_initialize_text (accpath);
312   spi_initialize_value (accpath);
313
314   /* Initialize the AtkObject registration */
315   atk_dbus_initialize (atk_adaptor_app_data->root);
316
317   /* Register methods to send D-Bus signals on certain ATK events */
318   spi_atk_register_event_listeners ();
319
320   /* Register this app by sending a signal out to AT-SPI registry daemon */
321   register_application (atk_adaptor_app_data);
322
323   g_atexit (exit_func);
324
325   return 0;
326 }
327
328 /*---------------------------------------------------------------------------*/
329
330 int
331 gtk_module_init (gint *argc, gchar **argv[])
332 {
333   return adaptor_init (argc, argv);
334 }
335
336 /*END------------------------------------------------------------------------*/