~ubuntu-branches/ubuntu/oneiric/gdm3/oneiric

« back to all changes in this revision

Viewing changes to gui/simple-greeter/greeter-main.c

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette
  • Date: 2010-03-25 20:02:20 UTC
  • Revision ID: james.westby@ubuntu.com-20100325200220-12cap62s6p304nuh
Tags: upstream-2.29.92
ImportĀ upstreamĀ versionĀ 2.29.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
 
 
22
#include <stdlib.h>
 
23
#include <locale.h>
 
24
#include <unistd.h>
 
25
#include <signal.h>
 
26
#include <string.h>
 
27
 
 
28
#include <glib.h>
 
29
#include <glib/gi18n.h>
 
30
#include <gdk/gdkx.h>
 
31
#include <gtk/gtk.h>
 
32
#include <gconf/gconf-client.h>
 
33
 
 
34
#include <dbus/dbus-glib.h>
 
35
 
 
36
#include "gdm-log.h"
 
37
#include "gdm-common.h"
 
38
#include "gdm-signal-handler.h"
 
39
#include "gdm-settings-client.h"
 
40
#include "gdm-settings-keys.h"
 
41
#include "gdm-profile.h"
 
42
 
 
43
#include "gdm-greeter-session.h"
 
44
 
 
45
#define SM_DBUS_NAME      "org.gnome.SessionManager"
 
46
#define SM_DBUS_PATH      "/org/gnome/SessionManager"
 
47
#define SM_DBUS_INTERFACE "org.gnome.SessionManager"
 
48
 
 
49
#define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
 
50
 
 
51
static DBusGConnection *bus_connection = NULL;
 
52
static DBusGProxy      *sm_proxy = NULL;
 
53
static char            *client_id = NULL;
 
54
static DBusGProxy      *client_proxy = NULL;
 
55
 
 
56
static gboolean
 
57
is_debug_set (void)
 
58
{
 
59
        gboolean debug = FALSE;
 
60
 
 
61
        /* enable debugging for unstable builds */
 
62
        if (gdm_is_version_unstable ()) {
 
63
                return TRUE;
 
64
        }
 
65
 
 
66
        gdm_settings_client_get_boolean (GDM_KEY_DEBUG, &debug);
 
67
        return debug;
 
68
}
 
69
 
 
70
 
 
71
static gboolean
 
72
signal_cb (int      signo,
 
73
           gpointer data)
 
74
{
 
75
        int ret;
 
76
 
 
77
        g_debug ("Got callback for signal %d", signo);
 
78
 
 
79
        ret = TRUE;
 
80
 
 
81
        switch (signo) {
 
82
        case SIGFPE:
 
83
        case SIGPIPE:
 
84
                /* let the fatal signals interrupt us */
 
85
                g_debug ("Caught signal %d, shutting down abnormally.", signo);
 
86
                ret = FALSE;
 
87
 
 
88
                break;
 
89
 
 
90
        case SIGINT:
 
91
        case SIGTERM:
 
92
                /* let the fatal signals interrupt us */
 
93
                g_debug ("Caught signal %d, shutting down normally.", signo);
 
94
                ret = FALSE;
 
95
 
 
96
                break;
 
97
 
 
98
        case SIGHUP:
 
99
                g_debug ("Got HUP signal");
 
100
                /* FIXME:
 
101
                 * Reread config stuff like system config files, VPN service files, etc
 
102
                 */
 
103
                ret = TRUE;
 
104
 
 
105
                break;
 
106
 
 
107
        case SIGUSR1:
 
108
                g_debug ("Got USR1 signal");
 
109
                /* FIXME:
 
110
                 * Play with log levels or something
 
111
                 */
 
112
                ret = TRUE;
 
113
 
 
114
                gdm_log_toggle_debug ();
 
115
 
 
116
                break;
 
117
 
 
118
        default:
 
119
                g_debug ("Caught unhandled signal %d", signo);
 
120
                ret = TRUE;
 
121
 
 
122
                break;
 
123
        }
 
124
 
 
125
        return ret;
 
126
}
 
127
 
 
128
static gboolean
 
129
session_manager_connect (void)
 
130
{
 
131
 
 
132
        if (bus_connection == NULL) {
 
133
                GError *error;
 
134
 
 
135
                error = NULL;
 
136
                bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 
137
                if (bus_connection == NULL) {
 
138
                        g_message ("Failed to connect to the session bus: %s",
 
139
                                   error->message);
 
140
                        g_error_free (error);
 
141
                        exit (1);
 
142
                }
 
143
        }
 
144
 
 
145
        sm_proxy = dbus_g_proxy_new_for_name (bus_connection,
 
146
                                              SM_DBUS_NAME,
 
147
                                              SM_DBUS_PATH,
 
148
                                              SM_DBUS_INTERFACE);
 
149
        return (sm_proxy != NULL);
 
150
}
 
151
 
 
152
static void
 
153
stop_cb (gpointer data)
 
154
{
 
155
        gtk_main_quit ();
 
156
}
 
157
 
 
158
static gboolean
 
159
end_session_response (gboolean is_okay, const gchar *reason)
 
160
{
 
161
        gboolean ret;
 
162
        GError *error = NULL;
 
163
 
 
164
        ret = dbus_g_proxy_call (client_proxy, "EndSessionResponse",
 
165
                                 &error,
 
166
                                 G_TYPE_BOOLEAN, is_okay,
 
167
                                 G_TYPE_STRING, reason,
 
168
                                 G_TYPE_INVALID,
 
169
                                 G_TYPE_INVALID);
 
170
 
 
171
        if (!ret) {
 
172
                g_warning ("Failed to send session response %s", error->message);
 
173
                g_error_free (error);
 
174
        }
 
175
 
 
176
        return ret;
 
177
}
 
178
 
 
179
static void
 
180
query_end_session_cb (guint flags, gpointer data)
 
181
{
 
182
        end_session_response (TRUE, NULL);
 
183
}
 
184
 
 
185
static void
 
186
end_session_cb (guint flags, gpointer data)
 
187
{
 
188
        end_session_response (TRUE, NULL);
 
189
        gtk_main_quit ();
 
190
}
 
191
 
 
192
static gboolean
 
193
register_client (void)
 
194
{
 
195
        GError     *error;
 
196
        gboolean    res;
 
197
        const char *startup_id;
 
198
        const char *app_id;
 
199
 
 
200
        startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
 
201
        app_id = "gdm-simple-greeter.desktop";
 
202
 
 
203
        error = NULL;
 
204
        res = dbus_g_proxy_call (sm_proxy,
 
205
                                 "RegisterClient",
 
206
                                 &error,
 
207
                                 G_TYPE_STRING, app_id,
 
208
                                 G_TYPE_STRING, startup_id,
 
209
                                 G_TYPE_INVALID,
 
210
                                 DBUS_TYPE_G_OBJECT_PATH, &client_id,
 
211
                                 G_TYPE_INVALID);
 
212
        if (! res) {
 
213
                g_warning ("Failed to register client: %s", error->message);
 
214
                g_error_free (error);
 
215
                return FALSE;
 
216
        }
 
217
 
 
218
        g_debug ("Client registered with session manager: %s", client_id);
 
219
        client_proxy = dbus_g_proxy_new_for_name (bus_connection,
 
220
                                                  SM_DBUS_NAME,
 
221
                                                  client_id,
 
222
                                                  SM_CLIENT_DBUS_INTERFACE);
 
223
 
 
224
        dbus_g_proxy_add_signal (client_proxy, "Stop", G_TYPE_INVALID);
 
225
        dbus_g_proxy_connect_signal (client_proxy, "Stop",
 
226
                                     G_CALLBACK (stop_cb), NULL, NULL);
 
227
 
 
228
        dbus_g_proxy_add_signal (client_proxy, "QueryEndSession", G_TYPE_UINT, G_TYPE_INVALID);
 
229
        dbus_g_proxy_connect_signal (client_proxy, "QueryEndSession",
 
230
                                     G_CALLBACK (query_end_session_cb), NULL, NULL);
 
231
 
 
232
        dbus_g_proxy_add_signal (client_proxy, "EndSession", G_TYPE_UINT, G_TYPE_INVALID);
 
233
        dbus_g_proxy_connect_signal (client_proxy, "EndSession",
 
234
                                     G_CALLBACK (end_session_cb), NULL, NULL);
 
235
 
 
236
        g_unsetenv ("DESKTOP_AUTOSTART_ID");
 
237
 
 
238
        return TRUE;
 
239
}
 
240
 
 
241
int
 
242
main (int argc, char *argv[])
 
243
{
 
244
        GError            *error;
 
245
        GdmGreeterSession *session;
 
246
        gboolean           res;
 
247
        GdmSignalHandler  *signal_handler;
 
248
 
 
249
        bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
 
250
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
251
        textdomain (GETTEXT_PACKAGE);
 
252
 
 
253
        setlocale (LC_ALL, "");
 
254
 
 
255
        gdm_set_fatal_warnings_if_unstable ();
 
256
 
 
257
        g_type_init ();
 
258
 
 
259
        gdm_profile_start ("Initializing settings client");
 
260
        if (! gdm_settings_client_init (GDMCONFDIR "/gdm.schemas", "/")) {
 
261
                g_critical ("Unable to initialize settings client");
 
262
                exit (1);
 
263
        }
 
264
        gdm_profile_end ("Initializing settings client");
 
265
 
 
266
        g_debug ("Greeter session pid=%d display=%s xauthority=%s",
 
267
                 (int)getpid (),
 
268
                 g_getenv ("DISPLAY"),
 
269
                 g_getenv ("XAUTHORITY"));
 
270
 
 
271
        /* FIXME: For testing to make it easier to attach gdb */
 
272
        /*sleep (15);*/
 
273
 
 
274
        gdm_log_init ();
 
275
        gdm_log_set_debug (is_debug_set ());
 
276
 
 
277
        gtk_init (&argc, &argv);
 
278
 
 
279
        signal_handler = gdm_signal_handler_new ();
 
280
        gdm_signal_handler_add_fatal (signal_handler);
 
281
        gdm_signal_handler_add (signal_handler, SIGTERM, signal_cb, NULL);
 
282
        gdm_signal_handler_add (signal_handler, SIGINT, signal_cb, NULL);
 
283
        gdm_signal_handler_add (signal_handler, SIGFPE, signal_cb, NULL);
 
284
        gdm_signal_handler_add (signal_handler, SIGHUP, signal_cb, NULL);
 
285
        gdm_signal_handler_add (signal_handler, SIGUSR1, signal_cb, NULL);
 
286
 
 
287
        gdm_profile_start ("Creating new greeter session");
 
288
        session = gdm_greeter_session_new ();
 
289
        if (session == NULL) {
 
290
                g_critical ("Unable to create greeter session");
 
291
                exit (1);
 
292
        }
 
293
        gdm_profile_end ("Creating new greeter session");
 
294
 
 
295
        error = NULL;
 
296
        res = gdm_greeter_session_start (session, &error);
 
297
        if (! res) {
 
298
                g_warning ("Unable to start greeter session: %s", error->message);
 
299
                g_error_free (error);
 
300
                exit (1);
 
301
        }
 
302
 
 
303
        res = session_manager_connect ();
 
304
        if (! res) {
 
305
                g_warning ("Unable to connect to session manager");
 
306
                exit (1);
 
307
        }
 
308
 
 
309
        res = register_client ();
 
310
        if (! res) {
 
311
                g_warning ("Unable to register client with session manager");
 
312
        }
 
313
 
 
314
        gtk_main ();
 
315
 
 
316
        if (session != NULL) {
 
317
                g_object_unref (session);
 
318
        }
 
319
 
 
320
        return 0;
 
321
}