2009-01-09 Mark Doffman <mark.doffman@codethink.co.uk>
[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             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 <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <atk/atk.h>
36 #include <atk/atkobject.h>
37 #include <atk/atknoopobject.h>
38
39 #include <droute/droute.h>
40
41 #include "bridge.h"
42 #include "event.h"
43 #include "atk-dbus.h"
44
45 #include "spi-common/spi-dbus.h"
46
47 /*
48  * Provides the path for the introspection directory.
49  */
50 #if !defined ATSPI_INTROSPECTION_PATH
51     #error "No introspection XML directory defined"
52 #endif
53
54 /*---------------------------------------------------------------------------*/
55
56 SpiAppData *atk_adaptor_app_data = NULL;
57
58 static const AtkMisc *atk_misc = NULL;
59
60 /*static Display *bridge_display = NULL;*/
61
62 /*---------------------------------------------------------------------------*/
63
64 /*
65  * Returns a 'canonicalized' value for DISPLAY,
66  * with the screen number stripped off if present.
67  *
68  * Not currently used in D-Bus version but may be
69  * useful in future if we make use of XAtom.
70  */
71 #if 0
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 #endif
104
105 /*---------------------------------------------------------------------------*/
106
107 /*
108  * Gets the IOR from the XDisplay.
109  * Not currently used in D-Bus version, but something similar
110  * may be employed in the future for accessing the registry daemon
111  * bus name.
112  */
113 #if 0
114 static gchar *
115 spi_atk_bridge_get_registry_ior (void)
116 {
117      Atom AT_SPI_IOR;
118      Atom actual_type;
119      int actual_format;
120      unsigned char *data = NULL;  
121      unsigned long nitems;
122      unsigned long leftover;
123      if (!bridge_display) 
124        bridge_display = XOpenDisplay (spi_display_name ());
125
126      AT_SPI_IOR = XInternAtom (bridge_display, "AT_SPI_IOR", False); 
127      XGetWindowProperty(bridge_display, 
128                         XDefaultRootWindow (bridge_display),
129                         AT_SPI_IOR, 0L,
130                         (long)BUFSIZ, False,
131                         (Atom) 31, &actual_type, &actual_format,
132                         &nitems, &leftover, &data);
133      if (data == NULL)
134          g_warning (_("AT_SPI_REGISTRY was not started at session startup."));
135      return (gchar *) data;
136 }
137 #endif
138
139 /*---------------------------------------------------------------------------*/
140
141 static void
142 register_application (SpiAppData *app)
143 {
144   DBusMessage *message;
145   DBusMessageIter iter;
146   DBusError error;
147   const char *uname;
148
149   dbus_error_init (&error);
150
151   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
152                                           SPI_DBUS_PATH_REGISTRY,
153                                           SPI_DBUS_INTERFACE_REGISTRY,
154                                           "registerApplication");
155   dbus_message_set_no_reply (message, TRUE);
156
157   uname = dbus_bus_get_unique_name(app->bus);
158
159   dbus_message_iter_init_append(message, &iter);
160   dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uname);
161   dbus_connection_send (app->bus, message, NULL);
162   if (message) dbus_message_unref (message);
163 }
164
165 /*---------------------------------------------------------------------------*/
166
167 static void
168 deregister_application (SpiAppData *app)
169 {
170   DBusMessage *message;
171   DBusMessageIter iter;
172   DBusError error;
173   const char *uname;
174
175   dbus_error_init (&error);
176
177   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
178                                           SPI_DBUS_PATH_REGISTRY,
179                                           SPI_DBUS_INTERFACE_REGISTRY,
180                                           "deregisterApplication");
181   dbus_message_set_no_reply (message, TRUE);
182
183   uname = dbus_bus_get_unique_name(app->bus);
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) dbus_message_unref (message);
189 }
190
191 /*---------------------------------------------------------------------------*/
192
193 static void
194 exit_func (void)
195 {
196   if (!atk_adaptor_app_data)
197     {
198       return;
199     }
200
201   spi_atk_tidy_windows ();
202   spi_atk_deregister_event_listeners();
203   deregister_application (atk_adaptor_app_data);
204
205   g_free(atk_adaptor_app_data);
206   atk_adaptor_app_data = NULL;
207
208   /* Not currently creating an XDisplay */
209 #if 0
210   if (bridge_display)
211     XCloseDisplay (bridge_display);
212 #endif
213 }
214
215 /*---------------------------------------------------------------------------*/
216
217 static gchar *atspi_dbus_name;
218 static gboolean atspi_no_register;
219
220 static GOptionEntry atspi_option_entries[] =
221 {
222   {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name, "D-Bus bus name to register as", NULL},
223   {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register, "Do not register with Registry Daemon", NULL},
224   {NULL}
225 };
226
227 /*
228  * spi_app_init
229  *
230  * The following needs to be initialized.
231  *
232  * - DRoute for routing message to their accessible objects.
233  * - Event handlers for emmitting signals on specific ATK events.
234  * - Application registration with the AT-SPI registry.
235  *
236  */
237 static int
238 adaptor_init (gint *argc, gchar **argv[])
239 {
240   GOptionContext *opt;
241   GError *err = NULL;
242   DBusError error;
243   DBusConnection *bus;
244   gchar *introspection_directory;
245
246   DRoutePath *treepath, *accpath;
247
248   /* Parse command line options */
249   opt = g_option_context_new(NULL);
250   g_option_context_add_main_entries(opt, atspi_option_entries, NULL);
251   g_option_context_set_ignore_unknown_options(opt, TRUE);
252   if (!g_option_context_parse(opt, argc, argv, &err))
253       g_warning("AT-SPI Option parsing failed: %s\n", err->message);
254
255   /* Allocate global data and do ATK initializations */
256   atk_adaptor_app_data = g_new0 (SpiAppData, 1);
257   atk_misc = atk_misc_get_instance ();
258   atk_adaptor_app_data->root = atk_get_root();
259
260   /* Set up D-Bus connection and register bus name */
261   dbus_error_init (&error);
262   atk_adaptor_app_data->bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
263   if (!atk_adaptor_app_data->bus)
264   {
265     g_warning ("AT-SPI Couldn't connect to D-Bus: %s\n", error.message);
266     g_free(atk_adaptor_app_data);
267     atk_adaptor_app_data = NULL;
268     return 0;
269   }
270   if (atspi_dbus_name != NULL &&
271       dbus_bus_request_name(atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
272   {
273     g_print("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
274   }
275
276   dbus_connection_setup_with_g_main(atk_adaptor_app_data->bus, g_main_context_default());
277
278   /* Get D-Bus introspection directory */
279   introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
280   if (introspection_directory == NULL)
281       introspection_directory = ATSPI_INTROSPECTION_PATH;
282
283   /* Register droute for routing AT-SPI messages */
284   atk_adaptor_app_data->droute = droute_new (atk_adaptor_app_data->bus, introspection_directory);
285
286   treepath = droute_add_one (atk_adaptor_app_data->droute,
287                              "/org/freedesktop/atspi/tree",
288                              NULL);
289
290   accpath = droute_add_many (atk_adaptor_app_data->droute,
291                              "/org/freedesktop/atspi/accessible",
292                              NULL,
293                              (DRouteGetDatumFunction) atk_dbus_path_to_object);
294
295   /* Register all interfaces with droute and set up application accessible db */
296   spi_initialize_tree (treepath);
297
298   spi_initialize_accessible (accpath);
299   spi_initialize_action(accpath);
300   spi_initialize_collection (accpath);
301   spi_initialize_component (accpath);
302   spi_initialize_document (accpath);
303   spi_initialize_editabletext (accpath);
304   spi_initialize_hyperlink (accpath);
305   spi_initialize_hypertext (accpath);
306   spi_initialize_image (accpath);
307   spi_initialize_selection (accpath);
308   spi_initialize_table (accpath);
309   spi_initialize_text (accpath);
310   spi_initialize_value (accpath);
311
312   /* Register methods to send D-Bus signals on certain ATK events */
313   spi_atk_register_event_listeners ();
314
315   /* Initialize the AtkObject registration */
316   atk_dbus_initialize (atk_adaptor_app_data->root);
317
318   /* Register this app by sending a signal out to AT-SPI registry daemon */
319   register_application (atk_adaptor_app_data);
320
321   g_atexit (exit_func);
322
323   return 0;
324 }
325
326 /*---------------------------------------------------------------------------*/
327
328 int
329 gtk_module_init (gint *argc, gchar **argv[])
330 {
331   return adaptor_init (argc, argv);
332 }
333
334 /*END------------------------------------------------------------------------*/