]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-xlocal.c
merge with trunk
[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 G_DEFINE_TYPE (SeatXLocal, seat_xlocal, SEAT_TYPE);
22
23 static gboolean
24 seat_xlocal_get_start_local_sessions (Seat *seat)
25 {
26     return !seat_get_string_property (seat, "xdmcp-manager");
27 }
28
29 static void
30 seat_xlocal_setup (Seat *seat)
31 {
32     seat_set_can_switch (seat, TRUE);
33     seat_set_share_display_server (seat, seat_get_boolean_property (seat, "xserver-share"));
34     SEAT_CLASS (seat_xlocal_parent_class)->setup (seat);
35 }
36
37 static gboolean
38 seat_xlocal_start (Seat *seat)
39 {
40     return SEAT_CLASS (seat_xlocal_parent_class)->start (seat);
41 }
42
43 static void
44 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
45 {
46     /* Quit Plymouth */
47     plymouth_quit (TRUE);
48 }
49
50 static void
51 display_server_transition_plymouth_cb (DisplayServer *display_server, Seat *seat)
52 {
53     /* Quit Plymouth if we didn't do the transition */
54     if (plymouth_get_is_running ())
55         plymouth_quit (FALSE);
56
57     g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, display_server_transition_plymouth_cb, NULL);
58 }
59
60 static gint
61 get_vt (Seat *seat, DisplayServer *display_server)
62 {
63     gint vt = -1;
64     const gchar *xdg_seat = seat_get_string_property (seat, "xdg-seat");
65
66     /* If Plymouth is running, stop it */
67     if (plymouth_get_is_active () && plymouth_has_active_vt ())
68     {
69         gint active_vt = vt_get_active ();
70         if (active_vt >= vt_get_min ())
71         {
72             vt = active_vt;
73             g_signal_connect (display_server, "ready", G_CALLBACK (display_server_ready_cb), seat);
74             g_signal_connect (display_server, "stopped", G_CALLBACK (display_server_transition_plymouth_cb), seat);
75             plymouth_deactivate ();
76         }
77         else
78             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 ());
79     }
80     if (plymouth_get_is_active ())
81         plymouth_quit (FALSE);
82     if (vt < 0 && g_strcmp0 (xdg_seat, "seat0") == 0)
83         vt = vt_get_unused ();
84
85     return vt;
86 }
87
88 static DisplayServer *
89 create_x_server (Seat *seat)
90 {
91     XServerLocal *x_server;
92     const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL, *xdg_seat = NULL;
93     gboolean allow_tcp;
94     gint vt, port = 0;
95
96     x_server = x_server_local_new ();
97
98     vt = get_vt (seat, DISPLAY_SERVER (x_server));
99     if (vt >= 0)
100         x_server_local_set_vt (x_server, vt);
101
102     if (vt > 0)
103         l_debug (seat, "Starting local X display on VT %d", vt);
104     else
105         l_debug (seat, "Starting local X display");
106
107     /* If running inside an X server use Xephyr instead */
108     if (g_getenv ("DISPLAY"))
109         command = "Xephyr";
110     if (!command)
111         command = seat_get_string_property (seat, "xserver-command");
112     if (command)
113         x_server_local_set_command (x_server, command);
114
115     layout = seat_get_string_property (seat, "xserver-layout");
116     if (layout)
117         x_server_local_set_layout (x_server, layout);
118         
119     xdg_seat = seat_get_string_property (seat, "xdg-seat");
120     if (xdg_seat)
121         x_server_local_set_xdg_seat (x_server, xdg_seat);
122
123     config_file = seat_get_string_property (seat, "xserver-config");
124     if (config_file)
125         x_server_local_set_config (x_server, config_file);
126   
127     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
128     x_server_local_set_allow_tcp (x_server, allow_tcp);    
129
130     xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
131     if (xdmcp_manager)
132         x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
133
134     port = seat_get_integer_property (seat, "xdmcp-port");
135     if (port > 0)
136         x_server_local_set_xdmcp_port (x_server, port);
137
138     key_name = seat_get_string_property (seat, "xdmcp-key");
139     if (key_name)
140     {
141         gchar *dir, *path;
142         GKeyFile *keys;
143         gboolean result;
144         GError *error = NULL;
145
146         dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
147         path = g_build_filename (dir, "keys.conf", NULL);
148         g_free (dir);
149
150         keys = g_key_file_new ();
151         result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
152         if (error)
153             l_debug (seat, "Error getting key %s", error->message);
154         g_clear_error (&error);      
155
156         if (result)
157         {
158             gchar *key = NULL;
159
160             if (g_key_file_has_key (keys, "keyring", key_name, NULL))
161                 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
162             else
163                 l_debug (seat, "Key %s not defined", key_name);
164
165             if (key)
166                 x_server_local_set_xdmcp_key (x_server, key);
167             g_free (key);
168         }
169
170         g_free (path);
171         g_key_file_free (keys);
172     }
173
174     return DISPLAY_SERVER (x_server);
175 }
176
177 static DisplayServer *
178 create_unity_system_compositor (Seat *seat)
179 {
180     UnitySystemCompositor *compositor;
181     const gchar *command;
182     gchar *socket_name;
183     gint vt, timeout, i;
184
185     compositor = unity_system_compositor_new ();
186
187     command = seat_get_string_property (seat, "unity-compositor-command");
188     if (command)
189         unity_system_compositor_set_command (compositor, command);
190
191     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
192     if (timeout <= 0)
193         timeout = 60;
194     unity_system_compositor_set_timeout (compositor, timeout);
195
196     vt = get_vt (seat, DISPLAY_SERVER (compositor));
197     if (vt >= 0)
198         unity_system_compositor_set_vt (compositor, vt);
199   
200     for (i = 0; ; i++)
201     {
202         socket_name = g_strdup_printf ("/tmp/lightdm-mir-%d", i);
203         if (!g_file_test (socket_name, G_FILE_TEST_EXISTS))
204             break;
205     }
206     unity_system_compositor_set_socket (compositor, socket_name);
207     g_free (socket_name);
208
209     return DISPLAY_SERVER (compositor);
210 }
211
212 static DisplayServer *
213 seat_xlocal_create_display_server (Seat *seat, const gchar *session_type)
214 {
215     if (strcmp (session_type, "x") == 0)
216         return create_x_server (seat);
217     else if (strcmp (session_type, "mir") == 0)
218         return create_unity_system_compositor (seat);
219     else
220     {
221         l_warning (seat, "Can't create unsupported display server '%s'", session_type);
222         return NULL;
223     }
224 }
225
226 static Greeter *
227 seat_xlocal_create_greeter_session (Seat *seat)
228 {
229     Greeter *greeter_session;
230     const gchar *xdg_seat;
231
232     greeter_session = SEAT_CLASS (seat_xlocal_parent_class)->create_greeter_session (seat);
233     xdg_seat = seat_get_string_property (seat, "xdg-seat");
234     if (!xdg_seat)
235         xdg_seat = "seat0";
236     l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
237     session_set_env (SESSION (greeter_session), "XDG_SEAT", xdg_seat);
238
239     return greeter_session;
240 }
241
242 static Session *
243 seat_xlocal_create_session (Seat *seat)
244 {
245     Session *session;
246     const gchar *xdg_seat;
247
248     session = SEAT_CLASS (seat_xlocal_parent_class)->create_session (seat);
249     xdg_seat = seat_get_string_property (seat, "xdg-seat");
250     if (!xdg_seat)
251         xdg_seat = "seat0";
252     l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
253     session_set_env (SESSION (session), "XDG_SEAT", xdg_seat);
254
255     return session;
256 }
257
258 static void
259 seat_xlocal_set_active_session (Seat *seat, Session *session)
260 {
261     gint vt = display_server_get_vt (session_get_display_server (session));
262     if (vt >= 0)
263         vt_set_active (vt);
264
265     SEAT_CLASS (seat_xlocal_parent_class)->set_active_session (seat, session);
266 }
267
268 static Session *
269 seat_xlocal_get_active_session (Seat *seat)
270 {
271     gint vt;
272     GList *link;
273
274     vt = vt_get_active ();
275     if (vt < 0)
276         return NULL;
277
278     for (link = seat_get_sessions (seat); link; link = link->next)
279     {
280         Session *session = link->data;
281         DisplayServer *display_server;
282
283         display_server = session_get_display_server (session);
284         if (display_server && display_server_get_vt (display_server) == vt)
285             return session;
286     }
287
288     return NULL;
289 }
290
291 static void
292 seat_xlocal_run_script (Seat *seat, DisplayServer *display_server, Process *script)
293 {
294     const gchar *path;
295     XServerLocal *x_server;
296
297     x_server = X_SERVER_LOCAL (display_server);
298     path = x_server_local_get_authority_file_path (x_server);
299     process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
300     process_set_env (script, "XAUTHORITY", path);
301
302     SEAT_CLASS (seat_xlocal_parent_class)->run_script (seat, display_server, script);
303 }
304
305 static void
306 seat_xlocal_init (SeatXLocal *seat)
307 {
308 }
309
310 static void
311 seat_xlocal_class_init (SeatXLocalClass *klass)
312 {
313     SeatClass *seat_class = SEAT_CLASS (klass);
314
315     seat_class->get_start_local_sessions = seat_xlocal_get_start_local_sessions;
316     seat_class->setup = seat_xlocal_setup;
317     seat_class->start = seat_xlocal_start;
318     seat_class->create_display_server = seat_xlocal_create_display_server;
319     seat_class->create_greeter_session = seat_xlocal_create_greeter_session;
320     seat_class->create_session = seat_xlocal_create_session;
321     seat_class->set_active_session = seat_xlocal_set_active_session;
322     seat_class->get_active_session = seat_xlocal_get_active_session;
323     seat_class->run_script = seat_xlocal_run_script;
324 }