]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-unity.c
Don't reconnect a display server if already connected to it
[sojka/lightdm.git] / src / seat-unity.c
1 /*
2  * Copyright (C) 2012-2013 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  *
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later
8  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9  * license.
10  */
11
12 #include <string.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <glib/gstdio.h>
16
17 #include "seat-unity.h"
18 #include "configuration.h"
19 #include "unity-system-compositor.h"
20 #include "x-server-local.h"
21 #include "mir-server.h"
22 #include "vt.h"
23 #include "plymouth.h"
24
25 struct SeatUnityPrivate
26 {
27     /* System compositor */
28     UnitySystemCompositor *compositor;
29
30     /* X server being used for XDMCP */
31     XServerLocal *xdmcp_x_server;
32
33     /* Next Mir ID to use for a Mir sessions, X server and greeters */
34     gint next_session_id;
35     gint next_x_server_id;
36     gint next_greeter_id;
37
38     /* The currently visible session */
39     Session *active_session;
40     DisplayServer *active_display_server;
41 };
42
43 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
44
45 static XServerLocal *create_x_server (Seat *seat);
46
47 static void
48 seat_unity_setup (Seat *seat)
49 {
50     seat_set_supports_multi_session (seat, TRUE);
51     SEAT_CLASS (seat_unity_parent_class)->setup (seat);
52 }
53
54 static void
55 check_stopped (SeatUnity *seat)
56 {
57     if (!seat->priv->compositor && !seat->priv->xdmcp_x_server)
58         SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
59 }
60
61 static void
62 xdmcp_x_server_stopped_cb (DisplayServer *display_server, Seat *seat)
63 {
64     l_debug (seat, "XDMCP X server stopped");
65
66     g_signal_handlers_disconnect_matched (SEAT_UNITY (seat)->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
67     SEAT_UNITY (seat)->priv->xdmcp_x_server = NULL;
68
69     g_object_unref (display_server);
70
71     if (seat_get_is_stopping (seat))
72         check_stopped (SEAT_UNITY (seat));
73     else
74         seat_stop (seat);
75 }
76
77 static void
78 compositor_ready_cb (UnitySystemCompositor *compositor, SeatUnity *seat)
79 {
80     const gchar *xdmcp_manager = NULL;
81
82     l_debug (seat, "Compositor ready");
83
84     /* If running as an XDMCP client then just start an X server */
85     xdmcp_manager = seat_get_string_property (SEAT (seat), "xdmcp-manager");
86     if (xdmcp_manager)
87     {
88         const gchar *key_name = NULL;
89         gint port = 0;
90
91         seat->priv->xdmcp_x_server = create_x_server (SEAT (seat));
92         x_server_local_set_xdmcp_server (seat->priv->xdmcp_x_server, xdmcp_manager);
93         port = seat_get_integer_property (SEAT (seat), "xdmcp-port");
94         if (port > 0)
95             x_server_local_set_xdmcp_port (seat->priv->xdmcp_x_server, port);
96         key_name = seat_get_string_property (SEAT (seat), "xdmcp-key");
97         if (key_name)
98         {
99             gchar *path;
100             GKeyFile *keys;
101             gboolean result;
102             GError *error = NULL;
103
104             path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL);
105
106             keys = g_key_file_new ();
107             result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
108             if (error)
109                 l_debug (seat, "Error getting key %s", error->message);
110             g_clear_error (&error);
111
112             if (result)
113             {
114                 gchar *key = NULL;
115
116                 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
117                     key = g_key_file_get_string (keys, "keyring", key_name, NULL);
118                 else
119                     l_debug (seat, "Key %s not defined", key_name);
120
121                 if (key)
122                     x_server_local_set_xdmcp_key (seat->priv->xdmcp_x_server, key);
123                 g_free (key);
124             }
125
126             g_free (path);
127             g_key_file_free (keys);
128         }
129
130         g_signal_connect (seat->priv->xdmcp_x_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (xdmcp_x_server_stopped_cb), seat);
131         if (!display_server_start (DISPLAY_SERVER (seat->priv->xdmcp_x_server)))
132             seat_stop (SEAT (seat));
133     }
134
135     SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
136 }
137
138 static void
139 compositor_stopped_cb (UnitySystemCompositor *compositor, SeatUnity *seat)
140 {
141     l_debug (seat, "Compositor stopped");
142
143     g_clear_object (&seat->priv->compositor);
144
145     if (seat_get_is_stopping (SEAT (seat)))
146         check_stopped (seat);
147     else
148         seat_stop (SEAT (seat));
149 }
150
151 static gboolean
152 seat_unity_start (Seat *seat)
153 {
154     gint vt = -1;
155     int timeout;
156
157     /* Replace Plymouth if it is running */
158     if (plymouth_get_is_active () && plymouth_has_active_vt ())
159     {
160         gint active_vt = vt_get_active ();
161         if (active_vt >= vt_get_min ())
162         {
163             vt = active_vt;
164             plymouth_quit (TRUE);
165         }
166         else
167             l_debug (seat, "Plymouth is running on VT %d, but this is less than the configured minimum of %d so not replacing it", active_vt, vt_get_min ());
168     }
169     if (plymouth_get_is_active ())
170         plymouth_quit (FALSE);
171     if (vt < 0)
172         vt = vt_can_multi_seat () ? vt_get_unused () : 0;
173     if (vt < 0)
174     {
175         l_debug (seat, "Failed to get a VT to run on");
176         return FALSE;
177     }
178
179     timeout = seat_get_integer_property (SEAT (seat), "unity-compositor-timeout");
180     if (timeout <= 0)
181         timeout = 60;
182
183     SEAT_UNITY (seat)->priv->compositor = unity_system_compositor_new ();
184     g_signal_connect (SEAT_UNITY (seat)->priv->compositor, DISPLAY_SERVER_SIGNAL_READY, G_CALLBACK (compositor_ready_cb), seat);
185     g_signal_connect (SEAT_UNITY (seat)->priv->compositor, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (compositor_stopped_cb), seat);
186     unity_system_compositor_set_command (SEAT_UNITY (seat)->priv->compositor, seat_get_string_property (SEAT (seat), "unity-compositor-command"));
187     unity_system_compositor_set_vt (SEAT_UNITY (seat)->priv->compositor, vt);
188     unity_system_compositor_set_timeout (SEAT_UNITY (seat)->priv->compositor, timeout);
189
190     return display_server_start (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->compositor));
191 }
192
193 static XServerLocal *
194 create_x_server (Seat *seat)
195 {
196     XServerLocal *x_server;
197     gchar *number;
198     XAuthority *cookie;
199     const gchar *command = NULL, *layout = NULL, *config_file = NULL;
200     gboolean allow_tcp;
201     gchar *id;
202
203     l_debug (seat, "Starting X server on Unity compositor");
204
205     x_server = x_server_local_new ();
206
207     command = seat_get_string_property (seat, "xmir-command");
208     x_server_local_set_command (x_server, command);
209
210     id = g_strdup_printf ("x-%d", SEAT_UNITY (seat)->priv->next_x_server_id);
211     SEAT_UNITY (seat)->priv->next_x_server_id++;
212     x_server_local_set_mir_id (x_server, id);
213     x_server_local_set_mir_socket (x_server, unity_system_compositor_get_socket (SEAT_UNITY (seat)->priv->compositor));
214     g_free (id);
215
216     number = g_strdup_printf ("%d", x_server_get_display_number (X_SERVER (x_server)));
217     cookie = x_authority_new_local_cookie (number);
218     x_server_set_authority (X_SERVER (x_server), cookie);
219     g_free (number);
220     g_object_unref (cookie);
221
222     layout = seat_get_string_property (seat, "xserver-layout");
223     if (layout)
224         x_server_local_set_layout (x_server, layout);
225
226     x_server_local_set_xdg_seat (x_server, seat_get_name (seat));
227
228     config_file = seat_get_string_property (seat, "xserver-config");
229     if (config_file)
230         x_server_local_set_config (x_server, config_file);
231
232     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
233     x_server_local_set_allow_tcp (x_server, allow_tcp);
234
235     return x_server;
236 }
237
238 static DisplayServer *
239 create_mir_server (Seat *seat)
240 {
241     MirServer *mir_server;
242
243     mir_server = mir_server_new ();
244     mir_server_set_parent_socket (mir_server, unity_system_compositor_get_socket (SEAT_UNITY (seat)->priv->compositor));
245
246     return DISPLAY_SERVER (mir_server);
247 }
248
249 static DisplayServer *
250 seat_unity_create_display_server (Seat *seat, Session *session)
251 {
252     const gchar *session_type;
253
254     session_type = session_get_session_type (session);
255     if (strcmp (session_type, "x") == 0)
256         return DISPLAY_SERVER (create_x_server (seat));
257     else if (strcmp (session_type, "mir") == 0)
258         return create_mir_server (seat);
259     else
260     {
261         l_warning (seat, "Can't create unsupported display server '%s'", session_type);
262         return NULL;
263     }
264 }
265
266 static GreeterSession *
267 seat_unity_create_greeter_session (Seat *seat)
268 {
269     GreeterSession *greeter_session;
270     gchar *id;
271     gint vt;
272
273     greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
274     session_set_env (SESSION (greeter_session), "XDG_SEAT", seat_get_name (seat));
275
276     id = g_strdup_printf ("greeter-%d", SEAT_UNITY (seat)->priv->next_greeter_id);
277     SEAT_UNITY (seat)->priv->next_greeter_id++;
278     session_set_env (SESSION (greeter_session), "MIR_SERVER_NAME", id);
279     g_free (id);
280
281     vt = display_server_get_vt (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->compositor));
282     if (vt >= 0)
283     {
284         gchar *value = g_strdup_printf ("%d", vt);
285         session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
286         g_free (value);
287     }
288
289     return greeter_session;
290 }
291
292 static Session *
293 seat_unity_create_session (Seat *seat)
294 {
295     Session *session;
296     gchar *id;
297     gint vt;
298
299     session = SEAT_CLASS (seat_unity_parent_class)->create_session (seat);
300     session_set_env (session, "XDG_SEAT", seat_get_name (seat));
301
302     id = g_strdup_printf ("session-%d", SEAT_UNITY (seat)->priv->next_session_id);
303     SEAT_UNITY (seat)->priv->next_session_id++;
304     session_set_env (session, "MIR_SERVER_NAME", id);
305     g_free (id);
306
307     vt = display_server_get_vt (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->compositor));
308     if (vt >= 0)
309     {
310         gchar *value = g_strdup_printf ("%d", vt);
311         session_set_env (SESSION (session), "XDG_VTNR", value);
312         g_free (value);
313     }
314
315     return session;
316 }
317
318 static void
319 seat_unity_set_active_session (Seat *seat, Session *session)
320 {
321     DisplayServer *display_server;
322
323     if (session == SEAT_UNITY (seat)->priv->active_session)
324         return;
325     SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
326
327     display_server = session_get_display_server (session);
328     if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
329     {
330         const gchar *id = NULL;
331
332         SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
333
334         if (IS_X_SERVER_LOCAL (display_server))
335             id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
336         else
337             id = session_get_env (session, "MIR_SERVER_NAME");
338
339         if (id)
340         {
341             l_debug (seat, "Switching to Mir session %s", id);
342             unity_system_compositor_set_active_session (SEAT_UNITY (seat)->priv->compositor, id);
343         }
344         else
345             l_warning (seat, "Failed to work out session ID");
346     }
347
348     SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
349 }
350
351 static Session *
352 seat_unity_get_active_session (Seat *seat)
353 {
354     return SEAT_UNITY (seat)->priv->active_session;
355 }
356
357 static void
358 seat_unity_set_next_session (Seat *seat, Session *session)
359 {
360     DisplayServer *display_server;
361     const gchar *id = NULL;
362
363     if (!session)
364         return;
365
366     display_server = session_get_display_server (session);
367
368     if (IS_X_SERVER_LOCAL (display_server))
369         id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
370     else
371         id = session_get_env (session, "MIR_SERVER_NAME");
372
373     if (id)
374     {
375         l_debug (seat, "Marking Mir session %s as the next session", id);
376         unity_system_compositor_set_next_session (SEAT_UNITY (seat)->priv->compositor, id);
377     }
378     else
379     {
380         l_debug (seat, "Failed to work out session ID to mark");
381     }
382
383     SEAT_CLASS (seat_unity_parent_class)->set_next_session (seat, session);
384 }
385
386 static void
387 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
388 {
389     if (IS_X_SERVER_LOCAL (display_server))
390     {
391         XServerLocal *x_server;
392         const gchar *path;
393
394         x_server = X_SERVER_LOCAL (display_server);
395         path = x_server_local_get_authority_file_path (x_server);
396         process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
397         process_set_env (script, "XAUTHORITY", path);
398     }
399
400     SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
401 }
402
403 static void
404 seat_unity_stop (Seat *seat)
405 {
406     /* Stop the compositor first */
407     if (SEAT_UNITY (seat)->priv->compositor)
408         display_server_stop (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->compositor));
409
410     /* Stop the XDMCP X server first */
411     if (SEAT_UNITY (seat)->priv->xdmcp_x_server)
412         display_server_stop (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->xdmcp_x_server));
413
414     check_stopped (SEAT_UNITY (seat));
415 }
416
417 static void
418 seat_unity_init (SeatUnity *seat)
419 {
420     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
421 }
422
423 static void
424 seat_unity_finalize (GObject *object)
425 {
426     SeatUnity *seat = SEAT_UNITY (object);
427
428     g_clear_object (&seat->priv->compositor);
429     if (seat->priv->xdmcp_x_server)
430     {
431         g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
432         g_object_unref (seat->priv->xdmcp_x_server);
433     }
434     g_clear_object (&seat->priv->active_session);
435     g_clear_object (&seat->priv->active_display_server);
436
437     G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
438 }
439
440 static void
441 seat_unity_class_init (SeatUnityClass *klass)
442 {
443     GObjectClass *object_class = G_OBJECT_CLASS (klass);
444     SeatClass *seat_class = SEAT_CLASS (klass);
445
446     object_class->finalize = seat_unity_finalize;
447     seat_class->setup = seat_unity_setup;
448     seat_class->start = seat_unity_start;
449     seat_class->create_display_server = seat_unity_create_display_server;
450     seat_class->create_greeter_session = seat_unity_create_greeter_session;
451     seat_class->create_session = seat_unity_create_session;
452     seat_class->set_active_session = seat_unity_set_active_session;
453     seat_class->get_active_session = seat_unity_get_active_session;
454     seat_class->set_next_session = seat_unity_set_next_session;
455     seat_class->run_script = seat_unity_run_script;
456     seat_class->stop = seat_unity_stop;
457
458     g_type_class_add_private (klass, sizeof (SeatUnityPrivate));
459 }