]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-local.c
Drop mir-container sessions - they were never used
[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->compositor && !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 get_unity_system_compositor (SeatLocal *seat)
191 {
192     const gchar *command;
193     gint timeout, vt;
194
195     if (seat->priv->compositor)
196         return seat->priv->compositor;
197
198     seat->priv->compositor = unity_system_compositor_new ();
199
200     command = seat_get_string_property (SEAT (seat), "unity-compositor-command");
201     if (command)
202         unity_system_compositor_set_command (seat->priv->compositor, command);
203
204     timeout = seat_get_integer_property (SEAT (seat), "unity-compositor-timeout");
205     if (timeout <= 0)
206         timeout = 60;
207     unity_system_compositor_set_timeout (seat->priv->compositor, timeout);
208
209     vt = get_vt (seat, DISPLAY_SERVER (seat->priv->compositor));
210     if (vt >= 0)
211         unity_system_compositor_set_vt (seat->priv->compositor, vt);
212
213     seat->priv->next_xmir_id = 0;
214     g_signal_connect (seat->priv->compositor, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (compositor_stopped_cb), seat);
215
216     return seat->priv->compositor;
217 }
218
219 static XServerLocal *
220 create_x_server (SeatLocal *seat)
221 {
222     const gchar *x_server_backend;
223     XServerLocal *x_server;
224     gchar *number;
225     XAuthority *cookie;
226     const gchar *layout = NULL, *config_file = NULL;
227     gboolean allow_tcp;
228     gint vt;
229
230     x_server_backend = seat_get_string_property (SEAT (seat), "xserver-backend");
231     if (g_strcmp0 (x_server_backend, "mir") == 0)
232     {
233         UnitySystemCompositor *compositor;
234         const gchar *command;
235         gchar *id;
236
237         compositor = get_unity_system_compositor (SEAT_LOCAL (seat));
238         x_server = X_SERVER_LOCAL (x_server_xmir_new (compositor));
239
240         command = seat_get_string_property (SEAT (seat), "xmir-command");
241         if (command)
242             x_server_local_set_command (x_server, command);
243
244         id = g_strdup_printf ("x-%d", seat->priv->next_xmir_id);
245         seat->priv->next_xmir_id++;
246         x_server_xmir_set_mir_id (X_SERVER_XMIR (x_server), id);
247         x_server_xmir_set_mir_socket (X_SERVER_XMIR (x_server), unity_system_compositor_get_socket (compositor));
248         g_free (id);
249     }
250     else
251     {
252         const gchar *command = NULL;
253
254         x_server = x_server_local_new ();
255
256         vt = get_vt (seat, DISPLAY_SERVER (x_server));
257         if (vt >= 0)
258             x_server_local_set_vt (x_server, vt);
259
260         if (vt > 0)
261             l_debug (seat, "Starting local X display on VT %d", vt);
262         else
263             l_debug (seat, "Starting local X display");
264
265         /* If running inside an X server use Xephyr instead */
266         if (g_getenv ("DISPLAY"))
267             command = "Xephyr";
268         if (!command)
269             command = seat_get_string_property (SEAT (seat), "xserver-command");
270         if (command)
271             x_server_local_set_command (x_server, command);
272     }
273
274     number = g_strdup_printf ("%d", x_server_get_display_number (X_SERVER (x_server)));
275     cookie = x_authority_new_local_cookie (number);
276     x_server_set_authority (X_SERVER (x_server), cookie);
277     g_free (number);
278     g_object_unref (cookie);
279
280     layout = seat_get_string_property (SEAT (seat), "xserver-layout");
281     if (layout)
282         x_server_local_set_layout (x_server, layout);
283
284     x_server_local_set_xdg_seat (x_server, seat_get_name (SEAT (seat)));
285
286     config_file = seat_get_string_property (SEAT (seat), "xserver-config");
287     if (config_file)
288         x_server_local_set_config (x_server, config_file);
289
290     allow_tcp = seat_get_boolean_property (SEAT (seat), "xserver-allow-tcp");
291     x_server_local_set_allow_tcp (x_server, allow_tcp);
292
293     return x_server;
294 }
295
296 static DisplayServer *
297 create_wayland_session (SeatLocal *seat)
298 {
299     WaylandSession *session;
300     gint vt;
301
302     session = wayland_session_new ();
303
304     vt = get_vt (seat, DISPLAY_SERVER (session));
305     if (vt >= 0)
306         wayland_session_set_vt (session, vt);
307
308     return DISPLAY_SERVER (session);
309 }
310
311 static DisplayServer *
312 seat_local_create_display_server (Seat *s, Session *session)
313 {
314     SeatLocal *seat = SEAT_LOCAL (s);
315     const gchar *session_type;
316
317     session_type = session_get_session_type (session);
318     if (strcmp (session_type, "x") == 0)
319         return DISPLAY_SERVER (create_x_server (seat));
320     else if (strcmp (session_type, "mir") == 0)
321         return g_object_ref (get_unity_system_compositor (seat));
322     else if (strcmp (session_type, "wayland") == 0)
323         return create_wayland_session (seat);
324     else
325     {
326         l_warning (seat, "Can't create unsupported display server '%s'", session_type);
327         return NULL;
328     }
329 }
330
331 static gboolean
332 seat_local_display_server_is_used (Seat *seat, DisplayServer *display_server)
333 {
334     if (display_server == DISPLAY_SERVER (SEAT_LOCAL (seat)->priv->compositor))
335         return TRUE;
336
337     return SEAT_CLASS (seat_local_parent_class)->display_server_is_used (seat, display_server);
338 }
339
340 static GreeterSession *
341 seat_local_create_greeter_session (Seat *seat)
342 {
343     GreeterSession *greeter_session;
344
345     greeter_session = SEAT_CLASS (seat_local_parent_class)->create_greeter_session (seat);
346     session_set_env (SESSION (greeter_session), "XDG_SEAT", seat_get_name (seat));
347
348     return greeter_session;
349 }
350
351 static Session *
352 seat_local_create_session (Seat *seat)
353 {
354     Session *session;
355
356     session = SEAT_CLASS (seat_local_parent_class)->create_session (seat);
357     session_set_env (SESSION (session), "XDG_SEAT", seat_get_name (seat));
358
359     return session;
360 }
361
362 static void
363 seat_local_set_active_session (Seat *s, Session *session)
364 {
365     SeatLocal *seat = SEAT_LOCAL (s);
366     DisplayServer *display_server;
367
368     display_server = session_get_display_server (session);
369
370     gint vt = display_server_get_vt (display_server);
371     if (vt >= 0)
372         vt_set_active (vt);
373
374     g_clear_object (&seat->priv->active_compositor_session);
375     if (IS_UNITY_SYSTEM_COMPOSITOR (display_server))
376     {
377         unity_system_compositor_set_active_session (UNITY_SYSTEM_COMPOSITOR (display_server), session_get_env (session, "MIR_SERVER_NAME"));
378         seat->priv->active_compositor_session = g_object_ref (session);
379     }
380     if (IS_X_SERVER_XMIR (display_server))
381     {
382         unity_system_compositor_set_active_session (seat->priv->compositor, x_server_xmir_get_mir_id (X_SERVER_XMIR (display_server)));
383         seat->priv->active_compositor_session = g_object_ref (session);
384     }
385
386     SEAT_CLASS (seat_local_parent_class)->set_active_session (s, session);
387 }
388
389 static Session *
390 seat_local_get_active_session (Seat *s)
391 {
392     SeatLocal *seat = SEAT_LOCAL (s);
393     gint vt;
394     GList *link;
395
396     vt = vt_get_active ();
397     if (vt < 0)
398         return NULL;
399
400     /* If the compositor is active return the session it is displaying */
401     if (seat->priv->compositor && display_server_get_vt (DISPLAY_SERVER (seat->priv->compositor)) == vt)
402         return seat->priv->active_compositor_session;
403
404     /* Otherwise find out which session is on this VT */
405     for (link = seat_get_sessions (s); link; link = link->next)
406     {
407         Session *session = link->data;
408         DisplayServer *display_server;
409
410         display_server = session_get_display_server (session);
411         if (display_server && display_server_get_vt (display_server) == vt)
412             return session;
413     }
414
415     return NULL;
416 }
417
418 static void
419 seat_local_set_next_session (Seat *seat, Session *session)
420 {
421     DisplayServer *display_server;
422     const gchar *id = NULL;
423
424     if (!session)
425         return;
426
427     display_server = session_get_display_server (session);
428
429     if (IS_X_SERVER_XMIR (display_server))
430         id = x_server_xmir_get_mir_id (X_SERVER_XMIR (display_server));
431     else
432         id = session_get_env (session, "MIR_SERVER_NAME");
433
434     if (id)
435     {
436         l_debug (seat, "Marking Mir session %s as the next session", id);
437         unity_system_compositor_set_next_session (SEAT_LOCAL (seat)->priv->compositor, id);
438     }
439     else
440         l_debug (seat, "Failed to work out session ID to mark");
441
442     SEAT_CLASS (seat_local_parent_class)->set_next_session (seat, session);
443 }
444
445 static void
446 seat_local_run_script (Seat *seat, DisplayServer *display_server, Process *script)
447 {
448     if (IS_X_SERVER_LOCAL (display_server))
449     {
450         const gchar *path;
451         XServerLocal *x_server;
452
453         x_server = X_SERVER_LOCAL (display_server);
454         path = x_server_local_get_authority_file_path (x_server);
455         process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
456         process_set_env (script, "XAUTHORITY", path);
457     }
458
459     SEAT_CLASS (seat_local_parent_class)->run_script (seat, display_server, script);
460 }
461
462 static void
463 seat_local_stop (Seat *s)
464 {
465     SeatLocal *seat = SEAT_LOCAL (s);
466
467     /* Stop the compositor */
468     if (seat->priv->compositor)
469         display_server_stop (DISPLAY_SERVER (seat->priv->compositor));
470
471     /* Stop the XDMCP X server */
472     if (seat->priv->xdmcp_x_server)
473         display_server_stop (DISPLAY_SERVER (seat->priv->xdmcp_x_server));
474
475     check_stopped (seat);
476 }
477
478 static void
479 seat_local_init (SeatLocal *seat)
480 {
481     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_LOCAL_TYPE, SeatLocalPrivate);
482 }
483
484 static void
485 seat_local_finalize (GObject *object)
486 {
487     SeatLocal *seat = SEAT_LOCAL (object);
488
489     g_clear_object (&seat->priv->compositor);
490     g_clear_object (&seat->priv->active_compositor_session);
491     if (seat->priv->xdmcp_x_server)
492     {
493         g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
494         g_object_unref (seat->priv->xdmcp_x_server);
495     }
496
497     G_OBJECT_CLASS (seat_local_parent_class)->finalize (object);
498 }
499
500 static void
501 seat_local_class_init (SeatLocalClass *klass)
502 {
503     GObjectClass *object_class = G_OBJECT_CLASS (klass);
504     SeatClass *seat_class = SEAT_CLASS (klass);
505
506     object_class->finalize = seat_local_finalize;
507
508     seat_class->setup = seat_local_setup;
509     seat_class->start = seat_local_start;
510     seat_class->create_display_server = seat_local_create_display_server;
511     seat_class->display_server_is_used = seat_local_display_server_is_used;
512     seat_class->create_greeter_session = seat_local_create_greeter_session;
513     seat_class->create_session = seat_local_create_session;
514     seat_class->set_active_session = seat_local_set_active_session;
515     seat_class->get_active_session = seat_local_get_active_session;
516     seat_class->set_next_session = seat_local_set_next_session;
517     seat_class->run_script = seat_local_run_script;
518     seat_class->stop = seat_local_stop;
519
520     g_type_class_add_private (klass, sizeof (SeatLocalPrivate));
521 }