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.
30 #include <dbus/dbus.h>
31 #include <dbus/dbus-glib.h>
35 #include "deviceeventcontroller.h"
37 #if !defined ATSPI_INTROSPECTION_PATH
38 #error "No introspection XML directory defined"
41 static GMainLoop *mainloop;
42 static gchar *dbus_name = NULL;
43 static gboolean use_gnome_session = FALSE;
45 static GOptionEntry optentries[] =
47 {"dbus-name", 0, 0, G_OPTION_ARG_STRING, &dbus_name, "Well-known name to register with D-Bus", NULL},
48 {"use-gnome-session", 0, 0, G_OPTION_ARG_NONE, &use_gnome_session, "Should register with gnome session manager", NULL},
52 static DBusGConnection *bus_connection = NULL;
53 static DBusGProxy *sm_proxy = NULL;
54 static char *client_id = NULL;
55 static DBusGProxy *client_proxy = NULL;
57 #define SM_DBUS_NAME "org.gnome.SessionManager"
58 #define SM_DBUS_PATH "/org/gnome/SessionManager"
59 #define SM_DBUS_INTERFACE "org.gnome.SessionManager"
61 #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
63 static void registry_session_init (const char *previous_client_id, const char *exe);
66 session_manager_connect (void)
69 if (bus_connection == NULL) {
73 bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
74 if (bus_connection == NULL) {
75 g_message ("Failed to connect to the session bus: %s",
82 sm_proxy = dbus_g_proxy_new_for_name (bus_connection,
86 return (sm_proxy != NULL);
90 stop_cb (gpointer data)
92 g_main_loop_quit (mainloop);
96 end_session_response (gboolean is_okay, const gchar *reason)
101 ret = dbus_g_proxy_call (client_proxy, "EndSessionResponse",
103 G_TYPE_BOOLEAN, is_okay,
104 G_TYPE_STRING, reason,
109 g_warning ("Failed to send session response %s", error->message);
110 g_error_free (error);
117 query_end_session_cb (guint flags, gpointer data)
119 end_session_response (TRUE, NULL);
123 end_session_cb (guint flags, gpointer data)
125 end_session_response (TRUE, NULL);
126 g_main_loop_quit (mainloop);
129 register_client (void)
133 const char *startup_id;
136 startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
137 app_id = "at-spi-registryd.desktop";
140 res = dbus_g_proxy_call (sm_proxy,
143 G_TYPE_STRING, app_id,
144 G_TYPE_STRING, startup_id,
146 DBUS_TYPE_G_OBJECT_PATH, &client_id,
149 g_warning ("Failed to register client: %s", error->message);
150 g_error_free (error);
154 client_proxy = dbus_g_proxy_new_for_name (bus_connection,
157 SM_CLIENT_DBUS_INTERFACE);
159 dbus_g_proxy_add_signal (client_proxy, "Stop", G_TYPE_INVALID);
160 dbus_g_proxy_connect_signal (client_proxy, "Stop",
161 G_CALLBACK (stop_cb), NULL, NULL);
163 dbus_g_proxy_add_signal (client_proxy, "QueryEndSession", G_TYPE_UINT, G_TYPE_INVALID);
164 dbus_g_proxy_connect_signal (client_proxy, "QueryEndSession",
165 G_CALLBACK (query_end_session_cb), NULL, NULL);
167 dbus_g_proxy_add_signal (client_proxy, "EndSession", G_TYPE_UINT, G_TYPE_INVALID);
168 dbus_g_proxy_connect_signal (client_proxy, "EndSession",
169 G_CALLBACK (end_session_cb), NULL, NULL);
171 g_unsetenv ("DESKTOP_AUTOSTART_ID");
176 /*---------------------------------------------------------------------------*/
179 * Returns a 'canonicalized' value for DISPLAY,
180 * with the screen number stripped off if present.
184 spi_display_name (void)
186 static const char *canonical_display_name = NULL;
187 if (!canonical_display_name)
189 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
192 display_env = g_getenv ("DISPLAY");
193 if (!display_env || !display_env[0])
194 canonical_display_name = ":0";
197 gchar *display_p, *screen_p;
198 canonical_display_name = g_strdup (display_env);
199 display_p = strrchr (canonical_display_name, ':');
200 screen_p = strrchr (canonical_display_name, '.');
201 if (screen_p && display_p && (screen_p > display_p))
209 canonical_display_name = display_env;
212 return canonical_display_name;
215 /*---------------------------------------------------------------------------*/
218 * Gets the IOR from the XDisplay.
219 * Not currently used in D-Bus version, but something similar
220 * may be employed in the future for accessing the registry daemon
224 static DBusConnection *
229 Display *bridge_display;
231 unsigned char *data = NULL;
232 unsigned long nitems;
233 unsigned long leftover;
235 DBusConnection *bus = NULL;
238 bridge_display = XOpenDisplay (spi_display_name ());
240 g_error ("AT_SPI: Could not get the display");
242 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", FALSE);
243 XGetWindowProperty(bridge_display,
244 XDefaultRootWindow (bridge_display),
247 (Atom) 31, &actual_type, &actual_format,
248 &nitems, &leftover, &data);
250 dbus_error_init (&error);
254 g_warning ("AT-SPI: Accessibility bus bus not found - Using session bus.\n");
255 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
257 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
261 bus = dbus_connection_open (data, &error);
264 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
268 if (!dbus_bus_register (bus, &error))
269 g_error ("AT-SPI: Couldn't register with bus: %s\n", error.message);
276 /*---------------------------------------------------------------------------*/
279 main (int argc, char **argv)
281 SpiRegistry *registry;
282 SpiDEController *dec;
283 gchar *introspection_directory;
285 DBusConnection *bus = NULL;
295 /*Parse command options*/
296 opt = g_option_context_new(NULL);
297 g_option_context_add_main_entries(opt, optentries, NULL);
299 if (!g_option_context_parse(opt, &argc, &argv, &err))
300 g_error("Option parsing failed: %s\n", err->message);
302 if (dbus_name == NULL)
303 dbus_name = SPI_DBUS_NAME_REGISTRY;
305 dbus_error_init (&error);
306 bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
307 bus = spi_get_bus ();
313 mainloop = g_main_loop_new (NULL, FALSE);
314 dbus_connection_setup_with_g_main(bus, NULL);
316 ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
317 if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
319 g_error("Could not obtain D-Bus name - %s\n", dbus_name);
323 g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
326 /* Get D-Bus introspection directory */
327 introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
328 if (introspection_directory == NULL)
329 introspection_directory = ATSPI_INTROSPECTION_PATH;
331 registry = spi_registry_new (bus);
332 dec = spi_registry_dec_new (registry, bus);
334 if (use_gnome_session)
336 if (!session_manager_connect ())
337 g_warning ("Unable to connect to session manager");
339 if (!register_client ())
340 g_warning ("Unable to register client with session manager");
343 g_main_loop_run (mainloop);