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