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.
32 #include <dbus/dbus.h>
36 #include "deviceeventcontroller.h"
37 #include "atspi/atspi.h"
39 static GMainLoop *mainloop;
40 static gchar *dbus_name = NULL;
41 static gboolean use_gnome_session = FALSE;
43 static GOptionEntry optentries[] =
45 {"dbus-name", 0, 0, G_OPTION_ARG_STRING, &dbus_name, "Well-known name to register with D-Bus", NULL},
46 {"use-gnome-session", 0, 0, G_OPTION_ARG_NONE, &use_gnome_session, "Should register with gnome session manager", NULL},
50 static GDBusProxy *sm_proxy = NULL;
51 static char *client_id = NULL;
52 static GDBusProxy *client_proxy = NULL;
54 #define SM_DBUS_NAME "org.gnome.SessionManager"
55 #define SM_DBUS_PATH "/org/gnome/SessionManager"
56 #define SM_DBUS_INTERFACE "org.gnome.SessionManager"
58 #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
60 static gboolean register_client (void);
63 on_session_signal (GDBusProxy *proxy,
69 if (g_strcmp0 (signal_name, "SessionOver") == 0) {
70 g_main_loop_quit (mainloop);
71 } else if (g_strcmp0 (signal_name, "SessionRunning") == 0) {
72 if (!register_client ())
73 g_warning ("Unable to register client with session manager");
78 session_manager_connect (void)
83 sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
86 SM_DBUS_INTERFACE, NULL, NULL);
88 res = g_dbus_proxy_call_sync (sm_proxy,
89 "IsSessionRunning", NULL,
93 g_variant_get (res, "(b)", &is_running);
94 g_variant_unref (res);
96 if (!register_client ())
97 g_warning ("Unable to register client with session manager");
101 g_signal_connect (G_OBJECT (sm_proxy), "g-signal",
102 G_CALLBACK (on_session_signal), NULL);
104 return (sm_proxy != NULL);
108 end_session_response (gboolean is_okay, const gchar *reason)
111 GError *error = NULL;
116 ret = g_dbus_proxy_call_sync (client_proxy, "EndSessionResponse",
117 g_variant_new ("(bs)", is_okay, reason),
118 0, 1000, NULL, &error);
121 g_warning ("Failed to send session response %s", error->message);
122 g_error_free (error);
126 g_variant_unref (ret);
132 client_proxy_signal_cb (GDBusProxy *proxy,
135 GVariant *parameters,
138 if (g_strcmp0 (signal_name, "QueryEndSession") == 0) {
139 g_debug ("Got QueryEndSession signal");
140 end_session_response (TRUE, NULL);
141 } else if (g_strcmp0 (signal_name, "EndSession") == 0) {
142 g_debug ("Got EndSession signal");
143 end_session_response (TRUE, NULL);
144 g_main_loop_quit (mainloop);
145 } else if (g_strcmp0 (signal_name, "Stop") == 0) {
146 g_debug ("Got Stop signal");
147 g_main_loop_quit (mainloop);
152 register_client (void)
156 const char *startup_id;
162 startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
165 app_id = "at-spi-registryd.desktop";
168 res = g_dbus_proxy_call_sync (sm_proxy,
170 g_variant_new ("(ss)", app_id,
172 0, 1000, NULL, &error);
174 const char *message = (error && error->message ? error->message
176 g_warning ("Failed to register client: %s", message);
178 g_error_free (error);
181 g_variant_get (res, "(o)", &client_id);
182 g_variant_unref (res);
184 client_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
187 SM_CLIENT_DBUS_INTERFACE,
190 g_signal_connect (client_proxy, "g-signal",
191 G_CALLBACK (client_proxy_signal_cb), NULL);
193 g_unsetenv ("DESKTOP_AUTOSTART_ID");
198 /*---------------------------------------------------------------------------*/
201 /*---------------------------------------------------------------------------*/
203 typedef GObject *(*gconf_client_get_default_t) ();
204 typedef gboolean (*gconf_client_get_bool_t)(GObject *, const char *, void *);
207 main (int argc, char **argv)
209 SpiRegistry *registry;
210 SpiDEController *dec;
212 DBusConnection *bus = NULL;
219 /*Parse command options*/
220 opt = g_option_context_new(NULL);
221 g_option_context_add_main_entries(opt, optentries, NULL);
223 if (!g_option_context_parse(opt, &argc, &argv, &err))
225 g_error("Option parsing failed: %s\n", err->message);
226 g_clear_error (&err);
229 if (dbus_name == NULL)
230 dbus_name = SPI_DBUS_NAME_REGISTRY;
232 bus = atspi_get_a11y_bus ();
238 mainloop = g_main_loop_new (NULL, FALSE);
239 atspi_dbus_connection_setup_with_g_main(bus, NULL);
241 ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, NULL);
242 if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
244 exit (0); /* most likely already running */
248 g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
251 registry = spi_registry_new (bus);
252 dec = spi_registry_dec_new (registry, bus);
254 if (use_gnome_session)
256 if (!session_manager_connect ())
257 g_warning ("Unable to connect to session manager");
260 g_main_loop_run (mainloop);
262 dbus_connection_close (bus);
263 dbus_connection_unref (bus);
264 g_object_unref (dec);
265 g_object_unref (registry);