bus: Rewrite a11y bus management, don't fall back to session bus
[platform/upstream/at-spi2-core.git] / registryd / registry-main.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include <stdlib.h>
25 #include <config.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <stdio.h>
29 #include <dlfcn.h>
30
31 #include <dbus/dbus.h>
32 #include <dbus/dbus-glib.h>
33
34 #include "paths.h"
35 #include "registry.h"
36 #include "deviceeventcontroller.h"
37 #include "libregistry-internals.h"
38
39 #define CORBA_GCONF_KEY  "/desktop/gnome/interface/at-spi-corba"
40
41 static gboolean need_to_quit ();
42
43 static GMainLoop *mainloop;
44 static gchar *dbus_name = NULL;
45 static gboolean use_gnome_session = FALSE;
46
47 static GOptionEntry optentries[] =
48 {
49   {"dbus-name", 0, 0, G_OPTION_ARG_STRING, &dbus_name, "Well-known name to register with D-Bus", NULL},
50   {"use-gnome-session", 0, 0, G_OPTION_ARG_NONE, &use_gnome_session, "Should register with gnome session manager", NULL},
51   {NULL}
52 };
53
54 static DBusGConnection *bus_connection = NULL;
55 static DBusGProxy      *sm_proxy = NULL;
56 static char            *client_id = NULL;
57 static DBusGProxy      *client_proxy = NULL;
58
59 #define SM_DBUS_NAME      "org.gnome.SessionManager"
60 #define SM_DBUS_PATH      "/org/gnome/SessionManager"
61 #define SM_DBUS_INTERFACE "org.gnome.SessionManager"
62
63 #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
64
65 static void registry_session_init (const char *previous_client_id, const char *exe);
66
67 static gboolean
68 session_manager_connect (void)
69 {
70
71         if (bus_connection == NULL) {
72                 GError *error;
73
74                 error = NULL;
75                 bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
76                 if (bus_connection == NULL) {
77                         g_message ("Failed to connect to the session bus: %s",
78                                    error->message);
79                         g_error_free (error);
80                         exit (1);
81                 }
82         }
83
84         sm_proxy = dbus_g_proxy_new_for_name (bus_connection,
85                                               SM_DBUS_NAME,
86                                               SM_DBUS_PATH,
87                                               SM_DBUS_INTERFACE);
88         return (sm_proxy != NULL);
89 }
90
91 static void
92 stop_cb (gpointer data)
93 {
94         g_main_loop_quit (mainloop);
95 }
96
97 static gboolean
98 end_session_response (gboolean is_okay, const gchar *reason)
99 {
100         gboolean ret;
101         GError *error = NULL;
102
103         ret = dbus_g_proxy_call (client_proxy, "EndSessionResponse",
104                                  &error,
105                                  G_TYPE_BOOLEAN, is_okay,
106                                  G_TYPE_STRING, reason,
107                                  G_TYPE_INVALID,
108                                  G_TYPE_INVALID);
109
110         if (!ret) {
111                 g_warning ("Failed to send session response %s", error->message);
112                 g_error_free (error);
113         }
114
115         return ret;
116 }
117
118 static void
119 query_end_session_cb (guint flags, gpointer data)
120 {
121         end_session_response (TRUE, NULL);
122 }
123
124 static void
125 end_session_cb (guint flags, gpointer data)
126 {
127         end_session_response (TRUE, NULL);
128         g_main_loop_quit (mainloop);
129 }
130 static gboolean
131 register_client (void)
132 {
133         GError     *error;
134         gboolean    res;
135         const char *startup_id;
136         const char *app_id;
137
138         startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
139         app_id = "at-spi-registryd.desktop";
140
141         error = NULL;
142         res = dbus_g_proxy_call (sm_proxy,
143                                  "RegisterClient",
144                                  &error,
145                                  G_TYPE_STRING, app_id,
146                                  G_TYPE_STRING, startup_id,
147                                  G_TYPE_INVALID,
148                                  DBUS_TYPE_G_OBJECT_PATH, &client_id,
149                                  G_TYPE_INVALID);
150         if (! res) {
151                 g_warning ("Failed to register client: %s", error->message);
152                 g_error_free (error);
153                 return FALSE;
154         }
155
156         client_proxy = dbus_g_proxy_new_for_name (bus_connection,
157                                                   SM_DBUS_NAME,
158                                                   client_id,
159                                                   SM_CLIENT_DBUS_INTERFACE);
160
161         dbus_g_proxy_add_signal (client_proxy, "Stop", G_TYPE_INVALID);
162         dbus_g_proxy_connect_signal (client_proxy, "Stop",
163                                      G_CALLBACK (stop_cb), NULL, NULL);
164
165         dbus_g_proxy_add_signal (client_proxy, "QueryEndSession", G_TYPE_UINT, G_TYPE_INVALID);
166         dbus_g_proxy_connect_signal (client_proxy, "QueryEndSession",
167                                      G_CALLBACK (query_end_session_cb), NULL, NULL);
168
169         dbus_g_proxy_add_signal (client_proxy, "EndSession", G_TYPE_UINT, G_TYPE_INVALID);
170         dbus_g_proxy_connect_signal (client_proxy, "EndSession",
171                                      G_CALLBACK (end_session_cb), NULL, NULL);
172
173         g_unsetenv ("DESKTOP_AUTOSTART_ID");
174
175         return TRUE;
176 }
177
178 /*---------------------------------------------------------------------------*/
179
180
181 /*---------------------------------------------------------------------------*/
182
183 typedef GObject *(*gconf_client_get_default_t) ();
184 typedef gboolean (*gconf_client_get_bool_t)(GObject *, const char *, void *);
185
186 int
187 main (int argc, char **argv)
188 {
189   SpiRegistry *registry;
190   SpiDEController *dec;
191
192   DBusConnection *bus = NULL;
193
194   GOptionContext *opt;
195
196   GError *err = NULL;
197   DBusError error;
198   int ret;
199
200   if (need_to_quit ())
201     return 0;
202
203   g_type_init();
204
205   /*Parse command options*/
206   opt = g_option_context_new(NULL);
207   g_option_context_add_main_entries(opt, optentries, NULL);
208
209   if (!g_option_context_parse(opt, &argc, &argv, &err))
210       g_error("Option parsing failed: %s\n", err->message);
211
212   if (dbus_name == NULL)
213       dbus_name = SPI_DBUS_NAME_REGISTRY;
214
215   dbus_error_init (&error);
216   bus = _libregistry_get_a11y_bus ();
217   if (!bus)
218   {
219     return 0;
220   }
221
222   mainloop = g_main_loop_new (NULL, FALSE);
223   dbus_connection_setup_with_g_main(bus, NULL);
224
225   ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
226   if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
227     {
228       g_error("Could not obtain D-Bus name - %s\n", dbus_name);
229     }
230   else
231     {
232       g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
233     }
234
235   registry = spi_registry_new (bus);
236   dec = spi_registry_dec_new (registry, bus);
237
238   if (use_gnome_session)
239     {
240       if (!session_manager_connect ())
241           g_warning ("Unable to connect to session manager");
242
243       if (!register_client ())
244           g_warning ("Unable to register client with session manager");
245     }
246
247   g_main_loop_run (mainloop);
248   return 0;
249 }
250
251 static gboolean
252 need_to_quit ()
253 {
254   void *gconf = NULL;
255   gconf_client_get_default_t gconf_client_get_default = NULL;
256   gconf_client_get_bool_t gconf_client_get_bool = NULL;
257   GObject *gconf_client;        /* really a GConfClient */
258   gboolean ret;
259
260   g_type_init ();
261
262   gconf = dlopen ("libgconf-2.so", RTLD_LAZY);
263   if (gconf)
264     {
265       gconf_client_get_default = dlsym (gconf, "gconf_client_get_default");
266       gconf_client_get_bool = dlsym (gconf, "gconf_client_get_bool");
267   }
268
269   if (!gconf_client_get_default || !gconf_client_get_bool)
270     {
271       if (gconf)
272         dlclose (gconf);
273       return FALSE;
274     }
275
276   /* If we've been relocated, we will exit if the at-spi-corba gconf key
277  * has been set.  If we have not been relocated, we will only run if the
278  * at-spi-dbus gconf key has been set.
279    */
280   gconf_client = gconf_client_get_default ();
281   ret = gconf_client_get_bool (gconf_client, CORBA_GCONF_KEY, NULL);
282   g_object_unref (gconf_client);
283
284   return ret;
285 }