2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002 Sun Microsystems Inc.,
6 * Copyright 2001, 2002 Ximian, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
29 #include <dbus/dbus.h>
30 #include <dbus/dbus-glib.h>
34 #include "deviceeventcontroller.h"
36 #if !defined ATSPI_INTROSPECTION_PATH
37 #error "No introspection XML directory defined"
40 static GMainLoop *mainloop;
41 static gchar *dbus_name = NULL;
42 static gboolean use_gnome_session = FALSE;
44 static GOptionEntry optentries[] =
46 {"dbus-name", 0, 0, G_OPTION_ARG_STRING, &dbus_name, "Well-known name to register with D-Bus", NULL},
47 {"use-gnome-session", 0, 0, G_OPTION_ARG_NONE, &use_gnome_session, "Should register with gnome session manager", NULL},
51 static DBusGConnection *bus_connection = NULL;
52 static DBusGProxy *sm_proxy = NULL;
53 static char *client_id = NULL;
54 static DBusGProxy *client_proxy = NULL;
56 #define SM_DBUS_NAME "org.gnome.SessionManager"
57 #define SM_DBUS_PATH "/org/gnome/SessionManager"
58 #define SM_DBUS_INTERFACE "org.gnome.SessionManager"
60 #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
62 static void registry_session_init (const char *previous_client_id, const char *exe);
65 session_manager_connect (void)
68 if (bus_connection == NULL) {
72 bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
73 if (bus_connection == NULL) {
74 g_message ("Failed to connect to the session bus: %s",
81 sm_proxy = dbus_g_proxy_new_for_name (bus_connection,
85 return (sm_proxy != NULL);
89 stop_cb (gpointer data)
91 g_main_loop_quit (mainloop);
95 end_session_response (gboolean is_okay, const gchar *reason)
100 ret = dbus_g_proxy_call (client_proxy, "EndSessionResponse",
102 G_TYPE_BOOLEAN, is_okay,
103 G_TYPE_STRING, reason,
108 g_warning ("Failed to send session response %s", error->message);
109 g_error_free (error);
116 query_end_session_cb (guint flags, gpointer data)
118 end_session_response (TRUE, NULL);
122 end_session_cb (guint flags, gpointer data)
124 end_session_response (TRUE, NULL);
125 g_main_loop_quit (mainloop);
128 register_client (void)
132 const char *startup_id;
135 startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
136 app_id = "at-spi-registryd.desktop";
139 res = dbus_g_proxy_call (sm_proxy,
142 G_TYPE_STRING, app_id,
143 G_TYPE_STRING, startup_id,
145 DBUS_TYPE_G_OBJECT_PATH, &client_id,
148 g_warning ("Failed to register client: %s", error->message);
149 g_error_free (error);
153 client_proxy = dbus_g_proxy_new_for_name (bus_connection,
156 SM_CLIENT_DBUS_INTERFACE);
158 dbus_g_proxy_add_signal (client_proxy, "Stop", G_TYPE_INVALID);
159 dbus_g_proxy_connect_signal (client_proxy, "Stop",
160 G_CALLBACK (stop_cb), NULL, NULL);
162 dbus_g_proxy_add_signal (client_proxy, "QueryEndSession", G_TYPE_UINT, G_TYPE_INVALID);
163 dbus_g_proxy_connect_signal (client_proxy, "QueryEndSession",
164 G_CALLBACK (query_end_session_cb), NULL, NULL);
166 dbus_g_proxy_add_signal (client_proxy, "EndSession", G_TYPE_UINT, G_TYPE_INVALID);
167 dbus_g_proxy_connect_signal (client_proxy, "EndSession",
168 G_CALLBACK (end_session_cb), NULL, NULL);
170 g_unsetenv ("DESKTOP_AUTOSTART_ID");
175 /*---------------------------------------------------------------------------*/
178 * Returns a 'canonicalized' value for DISPLAY,
179 * with the screen number stripped off if present.
183 spi_display_name (void)
185 static const char *canonical_display_name = NULL;
186 if (!canonical_display_name)
188 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
191 display_env = g_getenv ("DISPLAY");
192 if (!display_env || !display_env[0])
193 canonical_display_name = ":0";
196 gchar *display_p, *screen_p;
197 canonical_display_name = g_strdup (display_env);
198 display_p = strrchr (canonical_display_name, ':');
199 screen_p = strrchr (canonical_display_name, '.');
200 if (screen_p && display_p && (screen_p > display_p))
208 canonical_display_name = display_env;
211 return canonical_display_name;
214 /*---------------------------------------------------------------------------*/
217 * Gets the IOR from the XDisplay.
218 * Not currently used in D-Bus version, but something similar
219 * may be employed in the future for accessing the registry daemon
223 static DBusConnection *
228 Display *bridge_display;
230 unsigned char *data = NULL;
231 unsigned long nitems;
232 unsigned long leftover;
234 DBusConnection *bus = NULL;
237 bridge_display = XOpenDisplay (spi_display_name ());
239 g_error ("AT_SPI: Could not get the display");
241 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", FALSE);
242 XGetWindowProperty(bridge_display,
243 XDefaultRootWindow (bridge_display),
246 (Atom) 31, &actual_type, &actual_format,
247 &nitems, &leftover, &data);
249 dbus_error_init (&error);
253 g_warning ("AT-SPI: Accessibility bus bus not found - Using session bus.\n");
254 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
256 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
260 bus = dbus_connection_open (data, &error);
263 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
267 if (!dbus_bus_register (bus, &error))
268 g_error ("AT-SPI: Couldn't register with bus: %s\n");
275 /*---------------------------------------------------------------------------*/
278 main (int argc, char **argv)
280 SpiRegistry *registry;
281 SpiDEController *dec;
282 gchar *introspection_directory;
284 DBusConnection *bus = NULL;
294 /* We depend on GDK as well as XLib for device event processing */
295 gdk_init(&argc, &argv);
297 /*Parse command options*/
298 opt = g_option_context_new(NULL);
299 g_option_context_add_main_entries(opt, optentries, NULL);
301 if (!g_option_context_parse(opt, &argc, &argv, &err))
302 g_error("Option parsing failed: %s\n", err->message);
304 if (dbus_name == NULL)
305 dbus_name = SPI_DBUS_NAME_REGISTRY;
307 dbus_error_init (&error);
308 bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
309 bus = spi_get_bus ();
315 mainloop = g_main_loop_new (NULL, FALSE);
316 dbus_connection_setup_with_g_main(bus, NULL);
318 ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
319 if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
321 g_error("Could not obtain D-Bus name - %s\n", dbus_name);
325 g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
328 /* Get D-Bus introspection directory */
329 introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
330 if (introspection_directory == NULL)
331 introspection_directory = ATSPI_INTROSPECTION_PATH;
333 registry = spi_registry_new (bus);
334 dec = spi_registry_dec_new (registry, bus);
336 if (use_gnome_session)
338 if (!session_manager_connect ())
339 g_warning ("Unable to connect to session manager");
341 if (!register_client ())
342 g_warning ("Unable to register client with session manager");
345 g_main_loop_run (mainloop);