]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-xlocal.c
Change log backup to enabled by default - this is probably more useful. We explicitly...
[sojka/lightdm.git] / src / seat-xlocal.c
1 /*
2  * Copyright (C) 2010-2011 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
14 #include "seat-xlocal.h"
15 #include "configuration.h"
16 #include "x-server-local.h"
17 #include "unity-system-compositor.h"
18 #include "wayland-session.h"
19 #include "plymouth.h"
20 #include "vt.h"
21
22 struct SeatXLocalPrivate
23 {
24     /* X server being used for XDMCP */
25     XServerLocal *xdmcp_x_server;
26 };
27
28 G_DEFINE_TYPE (SeatXLocal, seat_xlocal, SEAT_TYPE);
29
30 static XServerLocal *create_x_server (Seat *seat);
31
32 static void
33 seat_xlocal_setup (Seat *seat)
34 {
35     seat_set_supports_multi_session (seat, TRUE);
36     seat_set_share_display_server (seat, seat_get_boolean_property (seat, "xserver-share"));
37     SEAT_CLASS (seat_xlocal_parent_class)->setup (seat);
38 }
39
40 static void
41 check_stopped (SeatXLocal *seat)
42 {
43     if (!seat->priv->xdmcp_x_server)
44         SEAT_CLASS (seat_xlocal_parent_class)->stop (SEAT (seat));
45 }
46
47 static void
48 xdmcp_x_server_stopped_cb (DisplayServer *display_server, Seat *seat)
49 {
50     l_debug (seat, "XDMCP X server stopped");
51
52     g_signal_handlers_disconnect_matched (SEAT_XLOCAL (seat)->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
53     SEAT_XLOCAL (seat)->priv->xdmcp_x_server = NULL;
54     g_object_unref (display_server);
55
56     if (seat_get_is_stopping (seat))
57         check_stopped (SEAT_XLOCAL (seat));
58     else
59         seat_stop (seat);
60 }
61
62 static gboolean
63 seat_xlocal_start (Seat *seat)
64 {
65     const gchar *xdmcp_manager = NULL;
66
67     /* If running as an XDMCP client then just start an X server */
68     xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
69     if (xdmcp_manager)
70     {
71         SeatXLocal *s = SEAT_XLOCAL (seat);
72         const gchar *key_name = NULL;
73         gint port = 0;
74
75         s->priv->xdmcp_x_server = create_x_server (seat);
76         x_server_local_set_xdmcp_server (s->priv->xdmcp_x_server, xdmcp_manager);
77         port = seat_get_integer_property (seat, "xdmcp-port");
78         if (port > 0)
79             x_server_local_set_xdmcp_port (s->priv->xdmcp_x_server, port);
80         key_name = seat_get_string_property (seat, "xdmcp-key");
81         if (key_name)
82         {
83             gchar *path;
84             GKeyFile *keys;
85             gboolean result;
86             GError *error = NULL;
87
88             path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL);
89
90             keys = g_key_file_new ();
91             result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
92             if (error)
93                 l_debug (seat, "Error getting key %s", error->message);
94             g_clear_error (&error);
95
96             if (result)
97             {
98                 gchar *key = NULL;
99
100                 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
101                     key = g_key_file_get_string (keys, "keyring", key_name, NULL);
102                 else
103                     l_debug (seat, "Key %s not defined", key_name);
104
105                 if (key)
106                     x_server_local_set_xdmcp_key (s->priv->xdmcp_x_server, key);
107                 g_free (key);
108             }
109
110             g_free (path);
111             g_key_file_free (keys);
112         }
113
114         g_signal_connect (s->priv->xdmcp_x_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (xdmcp_x_server_stopped_cb), seat);
115         return display_server_start (DISPLAY_SERVER (s->priv->xdmcp_x_server));
116     }
117
118     return SEAT_CLASS (seat_xlocal_parent_class)->start (seat);
119 }
120
121 static void
122 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
123 {
124     /* Quit Plymouth */
125     plymouth_quit (TRUE);
126 }
127
128 static void
129 display_server_transition_plymouth_cb (DisplayServer *display_server, Seat *seat)
130 {
131     /* Quit Plymouth if we didn't do the transition */
132     if (plymouth_get_is_running ())
133         plymouth_quit (FALSE);
134
135     g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, display_server_transition_plymouth_cb, NULL);
136 }
137
138 static gint
139 get_vt (Seat *seat, DisplayServer *display_server)
140 {
141     gint vt = -1;
142     const gchar *xdg_seat = seat_get_name (seat);
143
144     if (strcmp (xdg_seat, "seat0") != 0)
145         return vt;
146
147     /* If Plymouth is running, stop it */
148     if (plymouth_get_is_active () && plymouth_has_active_vt ())
149     {
150         gint active_vt = vt_get_active ();
151         if (active_vt >= vt_get_min ())
152         {
153             vt = active_vt;
154             g_signal_connect (display_server, DISPLAY_SERVER_SIGNAL_READY, G_CALLBACK (display_server_ready_cb), seat);
155             g_signal_connect (display_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (display_server_transition_plymouth_cb), seat);
156             plymouth_deactivate ();
157         }
158         else
159             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 ());
160     }
161     if (plymouth_get_is_active ())
162         plymouth_quit (FALSE);
163     if (vt < 0)
164         vt = vt_get_unused ();
165
166     return vt;
167 }
168
169 static XServerLocal *
170 create_x_server (Seat *seat)
171 {
172     XServerLocal *x_server;
173     const gchar *command = NULL, *layout = NULL, *config_file = NULL;
174     gboolean allow_tcp;
175     gint vt;
176
177     x_server = x_server_local_new ();
178
179     vt = get_vt (seat, DISPLAY_SERVER (x_server));
180     if (vt >= 0)
181         x_server_local_set_vt (x_server, vt);
182
183     if (vt > 0)
184         l_debug (seat, "Starting local X display on VT %d", vt);
185     else
186         l_debug (seat, "Starting local X display");
187
188     /* If running inside an X server use Xephyr instead */
189     if (g_getenv ("DISPLAY"))
190         command = "Xephyr";
191     if (!command)
192         command = seat_get_string_property (seat, "xserver-command");
193     if (command)
194         x_server_local_set_command (x_server, command);
195
196     layout = seat_get_string_property (seat, "xserver-layout");
197     if (layout)
198         x_server_local_set_layout (x_server, layout);
199
200     x_server_local_set_xdg_seat (x_server, seat_get_name (seat));
201
202     config_file = seat_get_string_property (seat, "xserver-config");
203     if (config_file)
204         x_server_local_set_config (x_server, config_file);
205
206     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
207     x_server_local_set_allow_tcp (x_server, allow_tcp);
208
209     return x_server;
210 }
211
212 static DisplayServer *
213 create_unity_system_compositor (Seat *seat)
214 {
215     UnitySystemCompositor *compositor;
216     const gchar *command;
217     gchar *socket_name;
218     gint vt, timeout, i;
219
220     compositor = unity_system_compositor_new ();
221
222     command = seat_get_string_property (seat, "unity-compositor-command");
223     if (command)
224         unity_system_compositor_set_command (compositor, command);
225
226     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
227     if (timeout <= 0)
228         timeout = 60;
229     unity_system_compositor_set_timeout (compositor, timeout);
230
231     vt = get_vt (seat, DISPLAY_SERVER (compositor));
232     if (vt >= 0)
233         unity_system_compositor_set_vt (compositor, vt);
234
235     for (i = 0; ; i++)
236     {
237         socket_name = g_strdup_printf ("/run/lightdm-mir-%d", i);
238         if (!g_file_test (socket_name, G_FILE_TEST_EXISTS))
239             break;
240     }
241     unity_system_compositor_set_socket (compositor, socket_name);
242     g_free (socket_name);
243
244     unity_system_compositor_set_enable_hardware_cursor (compositor, TRUE);
245
246     return DISPLAY_SERVER (compositor);
247 }
248
249 static DisplayServer *
250 create_wayland_session (Seat *seat)
251 {
252     WaylandSession *session;
253     gint vt;
254
255     session = wayland_session_new ();
256
257     vt = get_vt (seat, DISPLAY_SERVER (session));
258     if (vt >= 0)
259         wayland_session_set_vt (session, vt);
260
261     return DISPLAY_SERVER (session);
262 }
263
264 static DisplayServer *
265 seat_xlocal_create_display_server (Seat *seat, Session *session)
266 {
267     const gchar *session_type;
268
269     session_type = session_get_session_type (session);
270     if (strcmp (session_type, "x") == 0)
271         return DISPLAY_SERVER (create_x_server (seat));
272     else if (strcmp (session_type, "mir") == 0)
273         return create_unity_system_compositor (seat);
274     else if (strcmp (session_type, "wayland") == 0)
275         return create_wayland_session (seat);
276     else if (strcmp (session_type, "mir-container") == 0)
277     {
278         DisplayServer *compositor;
279         const gchar *compositor_command;
280
281         compositor = create_unity_system_compositor (seat);
282         compositor_command = session_config_get_compositor_command (session_get_config (session));
283         if (compositor_command)
284             unity_system_compositor_set_command (UNITY_SYSTEM_COMPOSITOR (compositor), compositor_command);
285
286         return compositor;
287     }
288     else
289     {
290         l_warning (seat, "Can't create unsupported display server '%s'", session_type);
291         return NULL;
292     }
293 }
294
295 static Greeter *
296 seat_xlocal_create_greeter_session (Seat *seat)
297 {
298     Greeter *greeter_session;
299
300     greeter_session = SEAT_CLASS (seat_xlocal_parent_class)->create_greeter_session (seat);
301     session_set_env (SESSION (greeter_session), "XDG_SEAT", seat_get_name (seat));
302
303     return greeter_session;
304 }
305
306 static Session *
307 seat_xlocal_create_session (Seat *seat)
308 {
309     Session *session;
310
311     session = SEAT_CLASS (seat_xlocal_parent_class)->create_session (seat);
312     session_set_env (SESSION (session), "XDG_SEAT", seat_get_name (seat));
313
314     return session;
315 }
316
317 static void
318 seat_xlocal_set_active_session (Seat *seat, Session *session)
319 {
320     DisplayServer *display_server;
321
322     display_server = session_get_display_server (session);
323
324     gint vt = display_server_get_vt (display_server);
325     if (vt >= 0)
326         vt_set_active (vt);
327
328     if (IS_UNITY_SYSTEM_COMPOSITOR (display_server))
329         unity_system_compositor_set_active_session (UNITY_SYSTEM_COMPOSITOR (display_server), IS_GREETER (session) ? "greeter-0" : "session-0");
330
331     SEAT_CLASS (seat_xlocal_parent_class)->set_active_session (seat, session);
332 }
333
334 static Session *
335 seat_xlocal_get_active_session (Seat *seat)
336 {
337     gint vt;
338     GList *link;
339
340     vt = vt_get_active ();
341     if (vt < 0)
342         return NULL;
343
344     for (link = seat_get_sessions (seat); link; link = link->next)
345     {
346         Session *session = link->data;
347         DisplayServer *display_server;
348
349         display_server = session_get_display_server (session);
350         if (display_server && display_server_get_vt (display_server) == vt)
351             return session;
352     }
353
354     return NULL;
355 }
356
357 static void
358 seat_xlocal_run_script (Seat *seat, DisplayServer *display_server, Process *script)
359 {
360     if (IS_X_SERVER_LOCAL (display_server))
361     {
362         const gchar *path;
363         XServerLocal *x_server;
364
365         x_server = X_SERVER_LOCAL (display_server);
366         path = x_server_local_get_authority_file_path (x_server);
367         process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
368         process_set_env (script, "XAUTHORITY", path);
369     }
370
371     SEAT_CLASS (seat_xlocal_parent_class)->run_script (seat, display_server, script);
372 }
373
374 static void
375 seat_xlocal_stop (Seat *seat)
376 {
377     /* Stop the XDMCP X server first */
378     if (SEAT_XLOCAL (seat)->priv->xdmcp_x_server)
379         display_server_stop (DISPLAY_SERVER (SEAT_XLOCAL (seat)->priv->xdmcp_x_server));
380
381     check_stopped (SEAT_XLOCAL (seat));
382 }
383
384 static void
385 seat_xlocal_init (SeatXLocal *seat)
386 {
387     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_XLOCAL_TYPE, SeatXLocalPrivate);
388 }
389
390 static void
391 seat_xlocal_finalize (GObject *object)
392 {
393     SeatXLocal *seat = SEAT_XLOCAL (object);
394
395     if (seat->priv->xdmcp_x_server)
396     {
397         g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
398         g_object_unref (seat->priv->xdmcp_x_server);
399     }
400
401     G_OBJECT_CLASS (seat_xlocal_parent_class)->finalize (object);
402 }
403
404 static void
405 seat_xlocal_class_init (SeatXLocalClass *klass)
406 {
407     GObjectClass *object_class = G_OBJECT_CLASS (klass);
408     SeatClass *seat_class = SEAT_CLASS (klass);
409
410     object_class->finalize = seat_xlocal_finalize;
411
412     seat_class->setup = seat_xlocal_setup;
413     seat_class->start = seat_xlocal_start;
414     seat_class->create_display_server = seat_xlocal_create_display_server;
415     seat_class->create_greeter_session = seat_xlocal_create_greeter_session;
416     seat_class->create_session = seat_xlocal_create_session;
417     seat_class->set_active_session = seat_xlocal_set_active_session;
418     seat_class->get_active_session = seat_xlocal_get_active_session;
419     seat_class->run_script = seat_xlocal_run_script;
420     seat_class->stop = seat_xlocal_stop;
421
422     g_type_class_add_private (klass, sizeof (SeatXLocalPrivate));
423 }