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