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