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