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.
31 #include <dbus/dbus.h>
32 #include <dbus/dbus-glib.h>
36 #include "deviceeventcontroller.h"
38 #define CORBA_GCONF_KEY "/desktop/gnome/interface/at-spi-corba"
40 static gboolean need_to_quit ();
42 static GMainLoop *mainloop;
43 static gchar *dbus_name = NULL;
44 static gboolean use_gnome_session = FALSE;
46 static GOptionEntry optentries[] =
48 {"dbus-name", 0, 0, G_OPTION_ARG_STRING, &dbus_name, "Well-known name to register with D-Bus", NULL},
49 {"use-gnome-session", 0, 0, G_OPTION_ARG_NONE, &use_gnome_session, "Should register with gnome session manager", NULL},
53 static DBusGConnection *bus_connection = NULL;
54 static DBusGProxy *sm_proxy = NULL;
55 static char *client_id = NULL;
56 static DBusGProxy *client_proxy = NULL;
58 #define SM_DBUS_NAME "org.gnome.SessionManager"
59 #define SM_DBUS_PATH "/org/gnome/SessionManager"
60 #define SM_DBUS_INTERFACE "org.gnome.SessionManager"
62 #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
64 static void registry_session_init (const char *previous_client_id, const char *exe);
67 session_manager_connect (void)
70 if (bus_connection == NULL) {
74 bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
75 if (bus_connection == NULL) {
76 g_message ("Failed to connect to the session bus: %s",
83 sm_proxy = dbus_g_proxy_new_for_name (bus_connection,
87 return (sm_proxy != NULL);
91 kill_accessibility_bus ()
98 home = getenv ("HOME");
101 name = g_strconcat (home, "/", ".atspi-dbus-bus.pid");
105 fp = fopen (name, "r");
108 if (fscanf (fp, "%d", &pid) == 1)
110 kill (&pid, SIGTERM);
118 stop_cb (gpointer data)
120 kill_accessibility_bus ();
121 g_main_loop_quit (mainloop);
125 end_session_response (gboolean is_okay, const gchar *reason)
128 GError *error = NULL;
130 ret = dbus_g_proxy_call (client_proxy, "EndSessionResponse",
132 G_TYPE_BOOLEAN, is_okay,
133 G_TYPE_STRING, reason,
138 g_warning ("Failed to send session response %s", error->message);
139 g_error_free (error);
146 query_end_session_cb (guint flags, gpointer data)
148 end_session_response (TRUE, NULL);
152 end_session_cb (guint flags, gpointer data)
154 kill_accessibility_bus ();
155 end_session_response (TRUE, NULL);
156 g_main_loop_quit (mainloop);
159 register_client (void)
163 const char *startup_id;
166 startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
167 app_id = "at-spi-registryd.desktop";
170 res = dbus_g_proxy_call (sm_proxy,
173 G_TYPE_STRING, app_id,
174 G_TYPE_STRING, startup_id,
176 DBUS_TYPE_G_OBJECT_PATH, &client_id,
179 g_warning ("Failed to register client: %s", error->message);
180 g_error_free (error);
184 client_proxy = dbus_g_proxy_new_for_name (bus_connection,
187 SM_CLIENT_DBUS_INTERFACE);
189 dbus_g_proxy_add_signal (client_proxy, "Stop", G_TYPE_INVALID);
190 dbus_g_proxy_connect_signal (client_proxy, "Stop",
191 G_CALLBACK (stop_cb), NULL, NULL);
193 dbus_g_proxy_add_signal (client_proxy, "QueryEndSession", G_TYPE_UINT, G_TYPE_INVALID);
194 dbus_g_proxy_connect_signal (client_proxy, "QueryEndSession",
195 G_CALLBACK (query_end_session_cb), NULL, NULL);
197 dbus_g_proxy_add_signal (client_proxy, "EndSession", G_TYPE_UINT, G_TYPE_INVALID);
198 dbus_g_proxy_connect_signal (client_proxy, "EndSession",
199 G_CALLBACK (end_session_cb), NULL, NULL);
201 g_unsetenv ("DESKTOP_AUTOSTART_ID");
206 /*---------------------------------------------------------------------------*/
209 * Returns a 'canonicalized' value for DISPLAY,
210 * with the screen number stripped off if present.
214 spi_display_name (void)
216 static const char *canonical_display_name = NULL;
217 if (!canonical_display_name)
219 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
222 display_env = g_getenv ("DISPLAY");
223 if (!display_env || !display_env[0])
224 canonical_display_name = ":0";
227 gchar *display_p, *screen_p;
228 canonical_display_name = g_strdup (display_env);
229 display_p = strrchr (canonical_display_name, ':');
230 screen_p = strrchr (canonical_display_name, '.');
231 if (screen_p && display_p && (screen_p > display_p))
239 canonical_display_name = display_env;
242 return canonical_display_name;
245 /*---------------------------------------------------------------------------*/
248 * Gets the IOR from the XDisplay.
249 * Not currently used in D-Bus version, but something similar
250 * may be employed in the future for accessing the registry daemon
254 static DBusConnection *
259 Display *bridge_display;
261 unsigned char *data = NULL;
262 unsigned long nitems;
263 unsigned long leftover;
265 DBusConnection *bus = NULL;
268 bridge_display = XOpenDisplay (spi_display_name ());
270 g_error ("AT_SPI: Could not get the display");
272 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", FALSE);
273 XGetWindowProperty(bridge_display,
274 XDefaultRootWindow (bridge_display),
277 (Atom) 31, &actual_type, &actual_format,
278 &nitems, &leftover, &data);
280 dbus_error_init (&error);
284 g_warning ("AT-SPI: Accessibility bus bus not found - Using session bus.\n");
285 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
287 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
291 bus = dbus_connection_open (data, &error);
295 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
299 if (!dbus_bus_register (bus, &error))
300 g_error ("AT-SPI: Couldn't register with bus: %s\n", error.message);
304 XCloseDisplay (bridge_display);
308 /*---------------------------------------------------------------------------*/
310 typedef GObject *(*gconf_client_get_default_t) ();
311 typedef gboolean (*gconf_client_get_bool_t)(GObject *, const char *, void *);
314 main (int argc, char **argv)
316 SpiRegistry *registry;
317 SpiDEController *dec;
319 DBusConnection *bus = NULL;
332 /*Parse command options*/
333 opt = g_option_context_new(NULL);
334 g_option_context_add_main_entries(opt, optentries, NULL);
336 if (!g_option_context_parse(opt, &argc, &argv, &err))
337 g_error("Option parsing failed: %s\n", err->message);
339 if (dbus_name == NULL)
340 dbus_name = SPI_DBUS_NAME_REGISTRY;
342 dbus_error_init (&error);
343 bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
344 bus = spi_get_bus ();
350 mainloop = g_main_loop_new (NULL, FALSE);
351 dbus_connection_setup_with_g_main(bus, NULL);
353 ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
354 if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
356 g_error("Could not obtain D-Bus name - %s\n", dbus_name);
360 g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
363 registry = spi_registry_new (bus);
364 dec = spi_registry_dec_new (registry, bus);
366 if (use_gnome_session)
368 if (!session_manager_connect ())
369 g_warning ("Unable to connect to session manager");
371 if (!register_client ())
372 g_warning ("Unable to register client with session manager");
375 g_main_loop_run (mainloop);
383 gconf_client_get_default_t gconf_client_get_default = NULL;
384 gconf_client_get_bool_t gconf_client_get_bool = NULL;
385 GObject *gconf_client; /* really a GConfClient */
390 gconf = dlopen ("libgconf-2.so", RTLD_LAZY);
393 gconf_client_get_default = dlsym (gconf, "gconf_client_get_default");
394 gconf_client_get_bool = dlsym (gconf, "gconf_client_get_bool");
397 if (!gconf_client_get_default || !gconf_client_get_bool)
404 /* If we've been relocated, we will exit if the at-spi-corba gconf key
405 * has been set. If we have not been relocated, we will only run if the
406 * at-spi-dbus gconf key has been set.
408 gconf_client = gconf_client_get_default ();
409 ret = gconf_client_get_bool (gconf_client, CORBA_GCONF_KEY, NULL);
410 g_object_unref (gconf_client);