]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-local.c
43fd21ded2bdc83beddf79ca87840c4431997290
[sojka/lightdm.git] / src / seat-local.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-local.h"
15 #include "configuration.h"
16 #include "x-server-local.h"
17 #include "x-server-xmir.h"
18 #include "unity-system-compositor.h"
19 #include "wayland-session.h"
20 #include "plymouth.h"
21 #include "vt.h"
22
23 struct SeatLocalPrivate
24 {
25     /* System compositor being used for Mir sessions */
26     UnitySystemCompositor *compositor;
27
28     /* Session currently active on compositor */
29     Session *active_compositor_session;
30
31     /* Counter for Mir IDs to use */
32     int next_xmir_id;
33
34     /* X server being used for XDMCP */
35     XServerLocal *xdmcp_x_server;
36 };
37
38 G_DEFINE_TYPE (SeatLocal, seat_local, SEAT_TYPE);
39
40 static XServerLocal *create_x_server (SeatLocal *seat);
41
42 static void
43 seat_local_setup (Seat *seat)
44 {
45     seat_set_supports_multi_session (seat, TRUE);
46     seat_set_share_display_server (seat, seat_get_boolean_property (seat, "xserver-share"));
47     SEAT_CLASS (seat_local_parent_class)->setup (seat);
48 }
49
50 static void
51 check_stopped (SeatLocal *seat)
52 {
53     if (!seat->priv->xdmcp_x_server)
54         SEAT_CLASS (seat_local_parent_class)->stop (SEAT (seat));
55 }
56
57 static void
58 xdmcp_x_server_stopped_cb (DisplayServer *display_server, SeatLocal *seat)
59 {
60     l_debug (seat, "XDMCP X server stopped");
61
62     g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
63     g_clear_object (&seat->priv->xdmcp_x_server);
64
65     if (seat_get_is_stopping (SEAT (seat)))
66         check_stopped (seat);
67     else
68         seat_stop (SEAT (seat));
69 }
70
71 static void
72 compositor_stopped_cb (UnitySystemCompositor *compositor, SeatLocal *seat)
73 {
74     l_debug (seat, "Compositor stopped");
75
76     g_clear_object (&seat->priv->compositor);
77
78     if (seat_get_is_stopping (SEAT (seat)))
79         check_stopped (seat);
80 }  
81
82 static gboolean
83 seat_local_start (Seat *seat)
84 {
85     const gchar *xdmcp_manager = NULL;
86
87     /* If running as an XDMCP client then just start an X server */
88     xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
89     if (xdmcp_manager)
90     {
91         SeatLocal *s = SEAT_LOCAL (seat);
92         const gchar *key_name = NULL;
93         gint port = 0;
94
95         s->priv->xdmcp_x_server = create_x_server (s);
96         x_server_local_set_xdmcp_server (s->priv->xdmcp_x_server, xdmcp_manager);
97         port = seat_get_integer_property (seat, "xdmcp-port");
98         if (port > 0)
99             x_server_local_set_xdmcp_port (s->priv->xdmcp_x_server, port);
100         key_name = seat_get_string_property (seat, "xdmcp-key");
101         if (key_name)
102         {
103             gchar *path;
104             GKeyFile *keys;
105             gboolean result;
106             GError *error = NULL;
107
108             path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL);
109
110             keys = g_key_file_new ();
111             result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
112             if (error)
113                 l_debug (seat, "Error getting key %s", error->message);
114             g_clear_error (&error);
115
116             if (result)
117             {
118                 gchar *key = NULL;
119
120                 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
121                     key = g_key_file_get_string (keys, "keyring", key_name, NULL);
122                 else
123                     l_debug (seat, "Key %s not defined", key_name);
124
125                 if (key)
126                     x_server_local_set_xdmcp_key (s->priv->xdmcp_x_server, key);
127                 g_free (key);
128             }
129
130             g_free (path);
131             g_key_file_free (keys);
132         }
133
134         g_signal_connect (s->priv->xdmcp_x_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (xdmcp_x_server_stopped_cb), seat);
135         return display_server_start (DISPLAY_SERVER (s->priv->xdmcp_x_server));
136     }
137
138     return SEAT_CLASS (seat_local_parent_class)->start (seat);
139 }
140
141 static void
142 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
143 {
144     /* Quit Plymouth */
145     plymouth_quit (TRUE);
146 }
147
148 static void
149 display_server_transition_plymouth_cb (DisplayServer *display_server, Seat *seat)
150 {
151     /* Quit Plymouth if we didn't do the transition */
152     if (plymouth_get_is_running ())
153         plymouth_quit (FALSE);
154
155     g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, display_server_transition_plymouth_cb, NULL);
156 }
157
158 static gint
159 get_vt (SeatLocal *seat, DisplayServer *display_server)
160 {
161     gint vt = -1;
162     const gchar *xdg_seat = seat_get_name (SEAT (seat));
163
164     if (strcmp (xdg_seat, "seat0") != 0)
165         return vt;
166
167     /* If Plymouth is running, stop it */
168     if (plymouth_get_is_active () && plymouth_has_active_vt ())
169     {
170         gint active_vt = vt_get_active ();
171         if (active_vt >= vt_get_min ())
172         {
173             vt = active_vt;
174             g_signal_connect (display_server, DISPLAY_SERVER_SIGNAL_READY, G_CALLBACK (display_server_ready_cb), seat);
175             g_signal_connect (display_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (display_server_transition_plymouth_cb), seat);
176             plymouth_deactivate ();
177         }
178         else
179             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 ());
180     }
181     if (plymouth_get_is_active ())
182         plymouth_quit (FALSE);
183     if (vt < 0)
184         vt = vt_get_unused ();
185
186     return vt;
187 }
188
189 static UnitySystemCompositor *
190 create_unity_system_compositor (SeatLocal *seat)
191 {
192     UnitySystemCompositor *compositor;
193     const gchar *command;
194     gint timeout, vt;
195
196     compositor = unity_system_compositor_new ();
197
198     command = seat_get_string_property (SEAT (seat), "unity-compositor-command");
199     if (command)
200         unity_system_compositor_set_command (compositor, command);
201
202     timeout = seat_get_integer_property (SEAT (seat), "unity-compositor-timeout");
203     if (timeout <= 0)
204         timeout = 60;
205     unity_system_compositor_set_timeout (compositor, timeout);
206
207     vt = get_vt (seat, DISPLAY_SERVER (compositor));
208     if (vt >= 0)
209         unity_system_compositor_set_vt (compositor, vt);
210
211     return compositor;
212 }
213
214 static UnitySystemCompositor *
215 get_unity_system_compositor (SeatLocal *seat)
216 {
217     if (seat->priv->compositor)
218         return seat->priv->compositor;
219
220     seat->priv->compositor = create_unity_system_compositor (seat);
221     seat->priv->next_xmir_id = 0;
222     g_signal_connect (seat->priv->compositor, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (compositor_stopped_cb), seat);
223
224     return seat->priv->compositor;
225 }
226
227 static XServerLocal *
228 create_x_server (SeatLocal *seat)
229 {
230     const gchar *x_server_backend;
231     XServerLocal *x_server;
232     gchar *number;
233     XAuthority *cookie;
234     const gchar *layout = NULL, *config_file = NULL;
235     gboolean allow_tcp;
236     gint vt;
237
238     x_server_backend = seat_get_string_property (SEAT (seat), "xserver-backend");
239     if (g_strcmp0 (x_server_backend, "mir") == 0)
240     {
241         UnitySystemCompositor *compositor;
242         const gchar *command;
243         gchar *id;
244
245         compositor = get_unity_system_compositor (SEAT_LOCAL (seat));
246         x_server = X_SERVER_LOCAL (x_server_xmir_new (compositor));
247
248         command = seat_get_string_property (SEAT (seat), "xmir-command");
249         if (command)
250             x_server_local_set_command (x_server, command);
251
252         id = g_strdup_printf ("x-%d", seat->priv->next_xmir_id);
253         seat->priv->next_xmir_id++;
254         x_server_xmir_set_mir_id (X_SERVER_XMIR (x_server), id);
255         x_server_xmir_set_mir_socket (X_SERVER_XMIR (x_server), unity_system_compositor_get_socket (compositor));
256         g_free (id);
257     }
258     else
259     {
260         const gchar *command = NULL;
261
262         x_server = x_server_local_new ();
263
264         vt = get_vt (seat, DISPLAY_SERVER (x_server));
265         if (vt >= 0)
266             x_server_local_set_vt (x_server, vt);
267
268         if (vt > 0)
269             l_debug (seat, "Starting local X display on VT %d", vt);
270         else
271             l_debug (seat, "Starting local X display");
272
273         /* If running inside an X server use Xephyr instead */
274         if (g_getenv ("DISPLAY"))
275             command = "Xephyr";
276         if (!command)
277             command = seat_get_string_property (SEAT (seat), "xserver-command");
278         if (command)
279             x_server_local_set_command (x_server, command);
280     }
281
282     number = g_strdup_printf ("%d", x_server_get_display_number (X_SERVER (x_server)));
283     cookie = x_authority_new_local_cookie (number);
284     x_server_set_authority (X_SERVER (x_server), cookie);
285     g_free (number);
286     g_object_unref (cookie);
287
288     layout = seat_get_string_property (SEAT (seat), "xserver-layout");
289     if (layout)
290         x_server_local_set_layout (x_server, layout);
291
292     x_server_local_set_xdg_seat (x_server, seat_get_name (SEAT (seat)));
293
294     config_file = seat_get_string_property (SEAT (seat), "xserver-config");
295     if (config_file)
296         x_server_local_set_config (x_server, config_file);
297
298     allow_tcp = seat_get_boolean_property (SEAT (seat), "xserver-allow-tcp");
299     x_server_local_set_allow_tcp (x_server, allow_tcp);
300
301     return x_server;
302 }
303
304 static DisplayServer *
305 create_wayland_session (SeatLocal *seat)
306 {
307     WaylandSession *session;
308     gint vt;
309
310     session = wayland_session_new ();
311
312     vt = get_vt (seat, DISPLAY_SERVER (session));
313     if (vt >= 0)
314         wayland_session_set_vt (session, vt);
315
316     return DISPLAY_SERVER (session);
317 }
318
319 static DisplayServer *
320 seat_local_create_display_server (Seat *s, Session *session)
321 {
322     SeatLocal *seat = SEAT_LOCAL (s);
323     const gchar *session_type;
324
325     session_type = session_get_session_type (session);
326     if (strcmp (session_type, "x") == 0)
327         return DISPLAY_SERVER (create_x_server (seat));
328     else if (strcmp (session_type, "mir") == 0)
329         return g_object_ref (get_unity_system_compositor (seat));
330     else if (strcmp (session_type, "wayland") == 0)
331         return create_wayland_session (seat);
332     else if (strcmp (session_type, "mir-container") == 0)
333     {
334         UnitySystemCompositor *compositor;
335         const gchar *compositor_command;
336
337         compositor = create_unity_system_compositor (seat);
338         compositor_command = session_config_get_compositor_command (session_get_config (session));
339         if (compositor_command)
340             unity_system_compositor_set_command (compositor, compositor_command);
341
342         return DISPLAY_SERVER (compositor);
343     }
344     else
345     {
346         l_warning (seat, "Can't create unsupported display server '%s'", session_type);
347         return NULL;
348     }
349 }
350
351 static gboolean
352 seat_local_display_server_is_used (Seat *seat, DisplayServer *display_server)
353 {
354     if (display_server == DISPLAY_SERVER (SEAT_LOCAL (seat)->priv->compositor))
355         return TRUE;
356
357     return SEAT_CLASS (seat_local_parent_class)->display_server_is_used (seat, display_server);
358 }
359
360 static GreeterSession *
361 seat_local_create_greeter_session (Seat *seat)
362 {
363     GreeterSession *greeter_session;
364
365     greeter_session = SEAT_CLASS (seat_local_parent_class)->create_greeter_session (seat);
366     session_set_env (SESSION (greeter_session), "XDG_SEAT", seat_get_name (seat));
367
368     return greeter_session;
369 }
370
371 static Session *
372 seat_local_create_session (Seat *seat)
373 {
374     Session *session;
375
376     session = SEAT_CLASS (seat_local_parent_class)->create_session (seat);
377     session_set_env (SESSION (session), "XDG_SEAT", seat_get_name (seat));
378
379     return session;
380 }
381
382 static void
383 seat_local_set_active_session (Seat *s, Session *session)
384 {
385     SeatLocal *seat = SEAT_LOCAL (s);
386     DisplayServer *display_server;
387
388     display_server = session_get_display_server (session);
389
390     gint vt = display_server_get_vt (display_server);
391     if (vt >= 0)
392         vt_set_active (vt);
393
394     g_clear_object (&seat->priv->active_compositor_session);
395     if (IS_UNITY_SYSTEM_COMPOSITOR (display_server))
396     {
397         unity_system_compositor_set_active_session (UNITY_SYSTEM_COMPOSITOR (display_server), session_get_env (session, "MIR_SERVER_NAME"));
398         seat->priv->active_compositor_session = g_object_ref (session);
399     }
400     if (IS_X_SERVER_XMIR (display_server))
401     {
402         unity_system_compositor_set_active_session (seat->priv->compositor, x_server_xmir_get_mir_id (X_SERVER_XMIR (display_server)));
403         seat->priv->active_compositor_session = g_object_ref (session);
404     }
405
406     SEAT_CLASS (seat_local_parent_class)->set_active_session (s, session);
407 }
408
409 static Session *
410 seat_local_get_active_session (Seat *s)
411 {
412     SeatLocal *seat = SEAT_LOCAL (s);
413     gint vt;
414     GList *link;
415
416     vt = vt_get_active ();
417     if (vt < 0)
418         return NULL;
419
420     /* If the compositor is active return the session it is displaying */
421     if (seat->priv->compositor && display_server_get_vt (DISPLAY_SERVER (seat->priv->compositor)) == vt)
422         return seat->priv->active_compositor_session;
423
424     /* Otherwise find out which session is on this VT */
425     for (link = seat_get_sessions (s); link; link = link->next)
426     {
427         Session *session = link->data;
428         DisplayServer *display_server;
429
430         display_server = session_get_display_server (session);
431         if (display_server && display_server_get_vt (display_server) == vt)
432             return session;
433     }
434
435     return NULL;
436 }
437
438 static void
439 seat_local_set_next_session (Seat *seat, Session *session)
440 {
441     DisplayServer *display_server;
442     const gchar *id = NULL;
443
444     if (!session)
445         return;
446
447     display_server = session_get_display_server (session);
448
449     if (IS_X_SERVER_XMIR (display_server))
450         id = x_server_xmir_get_mir_id (X_SERVER_XMIR (display_server));
451     else
452         id = session_get_env (session, "MIR_SERVER_NAME");
453
454     if (id)
455     {
456         l_debug (seat, "Marking Mir session %s as the next session", id);
457         unity_system_compositor_set_next_session (SEAT_LOCAL (seat)->priv->compositor, id);
458     }
459     else
460         l_debug (seat, "Failed to work out session ID to mark");
461
462     SEAT_CLASS (seat_local_parent_class)->set_next_session (seat, session);
463 }
464
465 static void
466 seat_local_run_script (Seat *seat, DisplayServer *display_server, Process *script)
467 {
468     if (IS_X_SERVER_LOCAL (display_server))
469     {
470         const gchar *path;
471         XServerLocal *x_server;
472
473         x_server = X_SERVER_LOCAL (display_server);
474         path = x_server_local_get_authority_file_path (x_server);
475         process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
476         process_set_env (script, "XAUTHORITY", path);
477     }
478
479     SEAT_CLASS (seat_local_parent_class)->run_script (seat, display_server, script);
480 }
481
482 static void
483 seat_local_stop (Seat *s)
484 {
485     SeatLocal *seat = SEAT_LOCAL (s);
486
487     /* Stop the compositor */
488     if (seat->priv->compositor)
489         display_server_stop (DISPLAY_SERVER (seat->priv->compositor));
490
491     /* Stop the XDMCP X server */
492     if (seat->priv->xdmcp_x_server)
493         display_server_stop (DISPLAY_SERVER (seat->priv->xdmcp_x_server));
494
495     check_stopped (seat);
496 }
497
498 static void
499 seat_local_init (SeatLocal *seat)
500 {
501     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_LOCAL_TYPE, SeatLocalPrivate);
502 }
503
504 static void
505 seat_local_finalize (GObject *object)
506 {
507     SeatLocal *seat = SEAT_LOCAL (object);
508
509     g_clear_object (&seat->priv->compositor);
510     g_clear_object (&seat->priv->active_compositor_session);
511     if (seat->priv->xdmcp_x_server)
512     {
513         g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
514         g_object_unref (seat->priv->xdmcp_x_server);
515     }
516
517     G_OBJECT_CLASS (seat_local_parent_class)->finalize (object);
518 }
519
520 static void
521 seat_local_class_init (SeatLocalClass *klass)
522 {
523     GObjectClass *object_class = G_OBJECT_CLASS (klass);
524     SeatClass *seat_class = SEAT_CLASS (klass);
525
526     object_class->finalize = seat_local_finalize;
527
528     seat_class->setup = seat_local_setup;
529     seat_class->start = seat_local_start;
530     seat_class->create_display_server = seat_local_create_display_server;
531     seat_class->display_server_is_used = seat_local_display_server_is_used;
532     seat_class->create_greeter_session = seat_local_create_greeter_session;
533     seat_class->create_session = seat_local_create_session;
534     seat_class->set_active_session = seat_local_set_active_session;
535     seat_class->get_active_session = seat_local_get_active_session;
536     seat_class->set_next_session = seat_local_set_next_session;
537     seat_class->run_script = seat_local_run_script;
538     seat_class->stop = seat_local_stop;
539
540     g_type_class_add_private (klass, sizeof (SeatLocalPrivate));
541 }