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