summaryrefslogtreecommitdiff
path: root/gui/simple-greeter/gdm-remote-login-window.c
blob: e673d91eed7327a0db27529d46dffa5c6fd266cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 William Jon McCann <jmccann@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <glib-object.h>

#include <gdk/gdkx.h>
#include <gtk/gtk.h>

#include "gdm-remote-login-window.h"
#include "gdm-common.h"

#define GDM_REMOTE_LOGIN_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_REMOTE_LOGIN_WINDOW, GdmRemoteLoginWindowPrivate))

struct GdmRemoteLoginWindowPrivate
{
        gboolean connected;
        char    *hostname;
        char    *display;
        GPid     xserver_pid;
        guint    xserver_watch_id;
};

enum {
        PROP_0,
};

enum {
        DISCONNECTED,
        LAST_SIGNAL
};

static guint signals [LAST_SIGNAL] = { 0, };

static void     gdm_remote_login_window_class_init   (GdmRemoteLoginWindowClass *klass);
static void     gdm_remote_login_window_init         (GdmRemoteLoginWindow      *remote_login_window);
static void     gdm_remote_login_window_finalize     (GObject                    *object);

G_DEFINE_TYPE (GdmRemoteLoginWindow, gdm_remote_login_window, GTK_TYPE_WINDOW)

static void
xserver_child_watch (GPid                  pid,
                     int                   status,
                     GdmRemoteLoginWindow *login_window)
{
        g_debug ("GdmRemoteLoginWindow: **** xserver (pid:%d) done (%s:%d)",
                 (int) pid,
                 WIFEXITED (status) ? "status"
                 : WIFSIGNALED (status) ? "signal"
                 : "unknown",
                 WIFEXITED (status) ? WEXITSTATUS (status)
                 : WIFSIGNALED (status) ? WTERMSIG (status)
                 : -1);

        g_spawn_close_pid (login_window->priv->xserver_pid);

        login_window->priv->xserver_pid = -1;
        login_window->priv->xserver_watch_id = 0;

        gtk_widget_destroy (GTK_WIDGET (login_window));
}

static gboolean
start_xephyr (GdmRemoteLoginWindow *login_window)
{
        GError     *local_error;
        char      **argv;
        gboolean    res;
        gboolean    ret;
        int         flags;
        char       *command;

        command = g_strdup_printf ("Xephyr -query %s -parent 0x%x -br -once %s",
                                   login_window->priv->hostname,
                                   (unsigned int)GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (login_window))),
                                   login_window->priv->display);
        g_debug ("GdmRemoteLoginWindow: Running: %s", command);

        ret = FALSE;

        argv = NULL;
        local_error = NULL;
        res = g_shell_parse_argv (command, NULL, &argv, &local_error);
        if (! res) {
                g_warning ("GdmRemoteLoginWindow: Unable to parse command: %s", local_error->message);
                g_error_free (local_error);
                goto out;
        }

        flags = G_SPAWN_SEARCH_PATH
                | G_SPAWN_DO_NOT_REAP_CHILD;

        local_error = NULL;
        res = g_spawn_async (NULL,
                             argv,
                             NULL,
                             flags,
                             NULL,
                             NULL,
                             &login_window->priv->xserver_pid,
                             &local_error);
        g_strfreev (argv);

        if (! res) {
                g_warning ("GdmRemoteLoginWindow: Unable to run command %s: %s",
                           command,
                           local_error->message);
                g_error_free (local_error);
                goto out;
        }

        g_debug ("GdmRemoteLoginWindow: Started: pid=%d command='%s'",
                 login_window->priv->xserver_pid,
                 command);

        login_window->priv->xserver_watch_id = g_child_watch_add (login_window->priv->xserver_pid,
                                                                  (GChildWatchFunc)xserver_child_watch,
                                                                  login_window);
        ret = TRUE;

 out:
        g_free (command);

        return ret;
}

static gboolean
start_xdmx (GdmRemoteLoginWindow *login_window)
{
        char    *cmd;
        gboolean res;
        GError  *error;

        cmd = g_strdup_printf ("Xdmx -query %s -br -once %s",
                               login_window->priv->hostname,
                               login_window->priv->display);
        g_debug ("Running: %s", cmd);

        error = NULL;
        res = g_spawn_command_line_async (cmd, &error);

        g_free (cmd);

        if (! res) {
                g_warning ("Could not start Xdmx X server: %s", error->message);
                g_error_free (error);
                return FALSE;
        }

        return TRUE;
}

gboolean
gdm_remote_login_window_connect (GdmRemoteLoginWindow *login_window,
                                 const char           *hostname)
{
        gboolean res;
        char    *title;

        title = g_strdup_printf (_("Remote Login (Connecting to %s…)"), hostname);

        gtk_window_set_title (GTK_WINDOW (login_window), title);

        login_window->priv->hostname = g_strdup (hostname);
        login_window->priv->display = g_strdup (":300");

        if (0) {
                res = start_xdmx (login_window);
        } else {
                res = start_xephyr (login_window);
        }

        if (res) {
                title = g_strdup_printf (_("Remote Login (Connected to %s)"), hostname);
                gtk_window_set_title (GTK_WINDOW (login_window), title);
                g_free (title);
        }

        return res;
}

static void
gdm_remote_login_window_set_property (GObject      *object,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
        switch (prop_id) {
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
gdm_remote_login_window_get_property (GObject    *object,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
        switch (prop_id) {
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static GObject *
gdm_remote_login_window_constructor (GType                  type,
                                     guint                  n_construct_properties,
                                     GObjectConstructParam *construct_properties)
{
        GdmRemoteLoginWindow      *login_window;

        login_window = GDM_REMOTE_LOGIN_WINDOW (G_OBJECT_CLASS (gdm_remote_login_window_parent_class)->constructor (type,
                                                                                                                      n_construct_properties,
                                                                                                                      construct_properties));


        return G_OBJECT (login_window);
}

static void
gdm_remote_login_window_class_init (GdmRemoteLoginWindowClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->get_property = gdm_remote_login_window_get_property;
        object_class->set_property = gdm_remote_login_window_set_property;
        object_class->constructor = gdm_remote_login_window_constructor;
        object_class->finalize = gdm_remote_login_window_finalize;

        signals [DISCONNECTED] =
                g_signal_new ("disconnected",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (GdmRemoteLoginWindowClass, disconnected),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE,
                              0);

        g_type_class_add_private (klass, sizeof (GdmRemoteLoginWindowPrivate));
}

static void
gdm_remote_login_window_init (GdmRemoteLoginWindow *login_window)
{
        login_window->priv = GDM_REMOTE_LOGIN_WINDOW_GET_PRIVATE (login_window);

        gtk_window_set_position (GTK_WINDOW (login_window), GTK_WIN_POS_CENTER_ALWAYS);
        gtk_window_set_title (GTK_WINDOW (login_window), _("Remote Login"));
        gtk_window_set_decorated (GTK_WINDOW (login_window), FALSE);
        gtk_window_set_skip_taskbar_hint (GTK_WINDOW (login_window), TRUE);
        gtk_window_set_skip_pager_hint (GTK_WINDOW (login_window), TRUE);
        gtk_window_stick (GTK_WINDOW (login_window));
        gtk_window_maximize (GTK_WINDOW (login_window));
        gtk_window_set_icon_name (GTK_WINDOW (login_window), "computer");
}

static void
gdm_remote_login_window_finalize (GObject *object)
{
        GdmRemoteLoginWindow *login_window;

        g_return_if_fail (object != NULL);
        g_return_if_fail (GDM_IS_REMOTE_LOGIN_WINDOW (object));

        login_window = GDM_REMOTE_LOGIN_WINDOW (object);

        g_return_if_fail (login_window->priv != NULL);

        G_OBJECT_CLASS (gdm_remote_login_window_parent_class)->finalize (object);
}

GtkWidget *
gdm_remote_login_window_new (gboolean is_local)
{
        GObject *object;

        object = g_object_new (GDM_TYPE_REMOTE_LOGIN_WINDOW,
                               NULL);

        return GTK_WIDGET (object);
}