]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-xlocal.c
Show source of configuration keys
[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     if (strcmp (xdg_seat, "seat0") != 0)
143         return vt;
144
145     /* If Plymouth is running, stop it */
146     if (plymouth_get_is_active () && plymouth_has_active_vt ())
147     {
148         gint active_vt = vt_get_active ();
149         if (active_vt >= vt_get_min ())
150         {
151             vt = active_vt;
152             g_signal_connect (display_server, "ready", G_CALLBACK (display_server_ready_cb), seat);
153             g_signal_connect (display_server, "stopped", G_CALLBACK (display_server_transition_plymouth_cb), seat);
154             plymouth_deactivate ();
155         }
156         else
157             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 ());
158     }
159     if (plymouth_get_is_active ())
160         plymouth_quit (FALSE);
161     if (vt < 0)
162         vt = vt_get_unused ();
163
164     return vt;
165 }
166
167 static XServerLocal *
168 create_x_server (Seat *seat)
169 {
170     XServerLocal *x_server;
171     const gchar *command = NULL, *layout = NULL, *config_file = NULL;
172     gboolean allow_tcp;
173     gint vt;
174
175     x_server = x_server_local_new ();
176
177     vt = get_vt (seat, DISPLAY_SERVER (x_server));
178     if (vt >= 0)
179         x_server_local_set_vt (x_server, vt);
180
181     if (vt > 0)
182         l_debug (seat, "Starting local X display on VT %d", vt);
183     else
184         l_debug (seat, "Starting local X display");
185
186     /* If running inside an X server use Xephyr instead */
187     if (g_getenv ("DISPLAY"))
188         command = "Xephyr";
189     if (!command)
190         command = seat_get_string_property (seat, "xserver-command");
191     if (command)
192         x_server_local_set_command (x_server, command);
193
194     layout = seat_get_string_property (seat, "xserver-layout");
195     if (layout)
196         x_server_local_set_layout (x_server, layout);
197
198     x_server_local_set_xdg_seat (x_server, seat_get_name (seat));
199
200     config_file = seat_get_string_property (seat, "xserver-config");
201     if (config_file)
202         x_server_local_set_config (x_server, config_file);
203   
204     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
205     x_server_local_set_allow_tcp (x_server, allow_tcp);    
206
207     return x_server;
208 }
209
210 static DisplayServer *
211 create_unity_system_compositor (Seat *seat)
212 {
213     UnitySystemCompositor *compositor;
214     const gchar *command;
215     gchar *socket_name;
216     gint vt, timeout, i;
217
218     compositor = unity_system_compositor_new ();
219
220     command = seat_get_string_property (seat, "unity-compositor-command");
221     if (command)
222         unity_system_compositor_set_command (compositor, command);
223
224     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
225     if (timeout <= 0)
226         timeout = 60;
227     unity_system_compositor_set_timeout (compositor, timeout);
228
229     vt = get_vt (seat, DISPLAY_SERVER (compositor));
230     if (vt >= 0)
231         unity_system_compositor_set_vt (compositor, vt);
232   
233     for (i = 0; ; i++)
234     {
235         socket_name = g_strdup_printf ("/run/lightdm-mir-%d", i);
236         if (!g_file_test (socket_name, G_FILE_TEST_EXISTS))
237             break;
238     }
239     unity_system_compositor_set_socket (compositor, socket_name);
240     g_free (socket_name);
241
242     unity_system_compositor_set_enable_hardware_cursor (compositor, TRUE);
243
244     return DISPLAY_SERVER (compositor);
245 }
246
247 static DisplayServer *
248 seat_xlocal_create_display_server (Seat *seat, Session *session)
249 {
250     const gchar *session_type;
251
252     session_type = session_get_session_type (session);
253     if (strcmp (session_type, "x") == 0)
254         return DISPLAY_SERVER (create_x_server (seat));
255     else if (strcmp (session_type, "mir") == 0)
256         return create_unity_system_compositor (seat);
257     else if (strcmp (session_type, "mir-container") == 0)
258     {
259         DisplayServer *compositor;
260         const gchar *compositor_command;
261
262         compositor = create_unity_system_compositor (seat);
263         compositor_command = session_config_get_compositor_command (session_get_config (session));
264         if (compositor_command)
265             unity_system_compositor_set_command (UNITY_SYSTEM_COMPOSITOR (compositor), compositor_command);
266       
267         return compositor;
268     }
269     else
270     {
271         l_warning (seat, "Can't create unsupported display server '%s'", session_type);
272         return NULL;
273     }
274 }
275
276 static Greeter *
277 seat_xlocal_create_greeter_session (Seat *seat)
278 {
279     Greeter *greeter_session;
280
281     greeter_session = SEAT_CLASS (seat_xlocal_parent_class)->create_greeter_session (seat);
282     session_set_env (SESSION (greeter_session), "XDG_SEAT", seat_get_name (seat));
283
284     return greeter_session;
285 }
286
287 static Session *
288 seat_xlocal_create_session (Seat *seat)
289 {
290     Session *session;
291
292     session = SEAT_CLASS (seat_xlocal_parent_class)->create_session (seat);
293     session_set_env (SESSION (session), "XDG_SEAT", seat_get_name (seat));
294
295     return session;
296 }
297
298 static void
299 seat_xlocal_set_active_session (Seat *seat, Session *session)
300 {
301     DisplayServer *display_server;
302
303     display_server = session_get_display_server (session);
304
305     gint vt = display_server_get_vt (display_server);
306     if (vt >= 0)
307         vt_set_active (vt);
308
309     if (IS_UNITY_SYSTEM_COMPOSITOR (display_server))
310         unity_system_compositor_set_active_session (UNITY_SYSTEM_COMPOSITOR (display_server), IS_GREETER (session) ? "greeter-0" : "session-0");
311
312     SEAT_CLASS (seat_xlocal_parent_class)->set_active_session (seat, session);
313 }
314
315 static Session *
316 seat_xlocal_get_active_session (Seat *seat)
317 {
318     gint vt;
319     GList *link;
320
321     vt = vt_get_active ();
322     if (vt < 0)
323         return NULL;
324
325     for (link = seat_get_sessions (seat); link; link = link->next)
326     {
327         Session *session = link->data;
328         DisplayServer *display_server;
329
330         display_server = session_get_display_server (session);
331         if (display_server && display_server_get_vt (display_server) == vt)
332             return session;
333     }
334
335     return NULL;
336 }
337
338 static void
339 seat_xlocal_run_script (Seat *seat, DisplayServer *display_server, Process *script)
340 {
341     const gchar *path;
342     XServerLocal *x_server;
343
344     x_server = X_SERVER_LOCAL (display_server);
345     path = x_server_local_get_authority_file_path (x_server);
346     process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
347     process_set_env (script, "XAUTHORITY", path);
348
349     SEAT_CLASS (seat_xlocal_parent_class)->run_script (seat, display_server, script);
350 }
351
352 static void
353 seat_xlocal_stop (Seat *seat)
354 {
355     /* Stop the XDMCP X server first */
356     if (SEAT_XLOCAL (seat)->priv->xdmcp_x_server)
357         display_server_stop (DISPLAY_SERVER (SEAT_XLOCAL (seat)->priv->xdmcp_x_server));
358
359     check_stopped (SEAT_XLOCAL (seat));
360 }
361
362 static void
363 seat_xlocal_init (SeatXLocal *seat)
364 {
365     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_XLOCAL_TYPE, SeatXLocalPrivate);
366 }
367
368 static void
369 seat_xlocal_finalize (GObject *object)
370 {
371     SeatXLocal *seat = SEAT_XLOCAL (object);
372
373     if (seat->priv->xdmcp_x_server)
374     {
375         g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
376         g_object_unref (seat->priv->xdmcp_x_server);
377     }
378
379     G_OBJECT_CLASS (seat_xlocal_parent_class)->finalize (object);
380 }
381
382 static void
383 seat_xlocal_class_init (SeatXLocalClass *klass)
384 {
385     GObjectClass *object_class = G_OBJECT_CLASS (klass);
386     SeatClass *seat_class = SEAT_CLASS (klass);
387
388     object_class->finalize = seat_xlocal_finalize;
389
390     seat_class->setup = seat_xlocal_setup;
391     seat_class->start = seat_xlocal_start;
392     seat_class->create_display_server = seat_xlocal_create_display_server;
393     seat_class->create_greeter_session = seat_xlocal_create_greeter_session;
394     seat_class->create_session = seat_xlocal_create_session;
395     seat_class->set_active_session = seat_xlocal_set_active_session;
396     seat_class->get_active_session = seat_xlocal_get_active_session;
397     seat_class->run_script = seat_xlocal_run_script;
398     seat_class->stop = seat_xlocal_stop;
399
400     g_type_class_add_private (klass, sizeof (SeatXLocalPrivate));
401 }