registryd: Wait for session manager to start before registering with it
[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
34 #include "paths.h"
35 #include "registry.h"
36 #include "deviceeventcontroller.h"
37 #include "atspi/atspi.h"
38
39 static GMainLoop *mainloop;
40 static gchar *dbus_name = NULL;
41 static gboolean use_gnome_session = FALSE;
42
43 static GOptionEntry optentries[] =
44 {
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},
47   {NULL}
48 };
49
50 static GDBusProxy      *sm_proxy = NULL;
51 static char            *client_id = NULL;
52 static GDBusProxy      *client_proxy = NULL;
53
54 #define SM_DBUS_NAME      "org.gnome.SessionManager"
55 #define SM_DBUS_PATH      "/org/gnome/SessionManager"
56 #define SM_DBUS_INTERFACE "org.gnome.SessionManager"
57
58 #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
59
60 static gboolean register_client (void);
61
62 static void
63 on_session_signal (GDBusProxy *proxy,
64                    gchar      *sender_name,
65                    gchar      *signal_name,
66                    GVariant   *parameters,
67                    gpointer    user_data)
68 {
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");
74         }
75 }
76
77 static gboolean
78 session_manager_connect (void)
79 {
80
81         sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
82                                               SM_DBUS_NAME,
83                                               SM_DBUS_PATH,
84                                               SM_DBUS_INTERFACE, NULL, NULL);
85
86         g_signal_connect (G_OBJECT (sm_proxy), "g-signal",
87                           G_CALLBACK (on_session_signal), NULL);
88
89         return (sm_proxy != NULL);
90 }
91
92 static gboolean
93 end_session_response (gboolean is_okay, const gchar *reason)
94 {
95         GVariant *ret;
96         GError *error = NULL;
97
98         if (!reason)
99                 reason = "";
100
101         ret = g_dbus_proxy_call_sync (client_proxy, "EndSessionResponse",
102                                       g_variant_new ("(bs)", is_okay, reason),
103                                       0, 1000, NULL, &error);
104
105         if (!ret) {
106                 g_warning ("Failed to send session response %s", error->message);
107                 g_error_free (error);
108                 return FALSE;
109         }
110         else
111                 g_variant_unref (ret);
112
113         return TRUE;
114 }
115
116 static void
117 client_proxy_signal_cb (GDBusProxy *proxy,
118                         gchar *sender_name,
119                         gchar *signal_name,
120                         GVariant *parameters,
121                         gpointer user_data)
122 {
123         if (g_strcmp0 (signal_name, "QueryEndSession") == 0) {
124                 g_debug ("Got QueryEndSession signal");
125                 end_session_response (TRUE, NULL);
126         } else if (g_strcmp0 (signal_name, "EndSession") == 0) {
127                 g_debug ("Got EndSession signal");
128                 end_session_response (TRUE, NULL);
129                 g_main_loop_quit (mainloop);
130         } else if (g_strcmp0 (signal_name, "Stop") == 0) {
131                 g_debug ("Got Stop signal");
132                 g_main_loop_quit (mainloop);
133         }
134 }
135
136 static gboolean
137 register_client (void)
138 {
139         GError     *error;
140         GVariant *res;
141         const char *startup_id;
142         const char *app_id;
143
144         startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
145         if (!startup_id)
146                 startup_id = "";
147         app_id = "at-spi-registryd.desktop";
148
149         error = NULL;
150         res = g_dbus_proxy_call_sync (sm_proxy,
151                                  "RegisterClient",
152                                       g_variant_new ("(ss)", app_id,
153                                                      startup_id),
154                                       0, 1000, NULL, &error);
155         if (! res) {
156                 const char *message = (error && error->message ? error->message
157                                        : "no error");
158                 g_warning ("Failed to register client: %s", message);
159                 if (error)
160                   g_error_free (error);
161                 return FALSE;
162         }
163         g_variant_get (res, "(o)", &client_id);
164         g_variant_unref (res);
165
166         client_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
167                                                   SM_DBUS_NAME,
168                                                   client_id,
169                                                   SM_CLIENT_DBUS_INTERFACE,
170                                                   NULL, NULL);
171
172         g_signal_connect (client_proxy, "g-signal",
173                           G_CALLBACK (client_proxy_signal_cb), NULL);
174
175         g_unsetenv ("DESKTOP_AUTOSTART_ID");
176
177         return TRUE;
178 }
179
180 /*---------------------------------------------------------------------------*/
181
182
183 /*---------------------------------------------------------------------------*/
184
185 typedef GObject *(*gconf_client_get_default_t) ();
186 typedef gboolean (*gconf_client_get_bool_t)(GObject *, const char *, void *);
187
188 int
189 main (int argc, char **argv)
190 {
191   SpiRegistry *registry;
192   SpiDEController *dec;
193
194   DBusConnection *bus = NULL;
195
196   GOptionContext *opt;
197
198   GError *err = NULL;
199   int ret;
200
201   /*Parse command options*/
202   opt = g_option_context_new(NULL);
203   g_option_context_add_main_entries(opt, optentries, NULL);
204
205   if (!g_option_context_parse(opt, &argc, &argv, &err))
206     {
207       g_error("Option parsing failed: %s\n", err->message);
208       g_clear_error (&err);
209     }
210
211   if (dbus_name == NULL)
212       dbus_name = SPI_DBUS_NAME_REGISTRY;
213
214   bus = atspi_get_a11y_bus ();
215   if (!bus)
216   {
217     return 0;
218   }
219
220   mainloop = g_main_loop_new (NULL, FALSE);
221   atspi_dbus_connection_setup_with_g_main(bus, NULL);
222
223   ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, NULL);
224   if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
225     {
226       exit (0); /* most likely already running */
227     }
228   else
229     {
230       g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
231     }
232
233   registry = spi_registry_new (bus);
234   dec = spi_registry_dec_new (registry, bus);
235
236   if (use_gnome_session)
237     {
238       if (!session_manager_connect ())
239           g_warning ("Unable to connect to session manager");
240     }
241
242   g_main_loop_run (mainloop);
243
244   dbus_connection_close (bus);
245   dbus_connection_unref (bus);
246   g_object_unref (dec);
247   g_object_unref (registry);
248
249   return 0;
250 }