]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-xlocal.c
Move VT allocation and Plymouth interaction to the seat level, instead of in the...
[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 "plymouth.h"
18 #include "vt.h"
19
20 G_DEFINE_TYPE (SeatXLocal, seat_xlocal, SEAT_TYPE);
21
22 static void
23 seat_xlocal_setup (Seat *seat)
24 {
25     seat_set_can_switch (seat, TRUE);
26     seat_set_share_display_server (seat, seat_get_boolean_property (seat, "xserver-share"));
27     SEAT_CLASS (seat_xlocal_parent_class)->setup (seat);
28 }
29
30 static gboolean
31 seat_xlocal_start (Seat *seat)
32 {
33    
34     return SEAT_CLASS (seat_xlocal_parent_class)->start (seat);
35 }
36
37 static void
38 x_server_ready_cb (XServerLocal *x_server, Seat *seat)
39 {
40     /* Quit Plymouth */
41     plymouth_quit (TRUE);
42 }
43
44 static void
45 x_server_transition_plymouth_cb (XServerLocal *x_server, Seat *seat)
46 {
47     /* Quit Plymouth if we didn't do the transition */
48     if (plymouth_get_is_running ())
49         plymouth_quit (FALSE);
50
51     g_signal_handlers_disconnect_matched (x_server, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, x_server_transition_plymouth_cb, NULL);
52 }
53
54 static void
55 x_server_stopped_cb (XServerLocal *x_server, Seat *seat)
56 {
57     gint vt;
58
59     /* Can re-use the VT */
60     vt = display_server_get_vt (DISPLAY_SERVER (x_server));
61     if (vt > 0)
62         vt_unref (vt);
63
64     g_signal_handlers_disconnect_matched (x_server, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, x_server_stopped_cb, NULL);
65 }
66
67 static DisplayServer *
68 seat_xlocal_create_display_server (Seat *seat)
69 {
70     XServerLocal *x_server;
71     const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
72     gboolean allow_tcp;
73     gint vt = -1, port = 0;
74
75     if (vt > 0)
76         g_debug ("Starting local X display on VT %d", vt);
77     else
78         g_debug ("Starting local X display");
79   
80     x_server = x_server_local_new ();
81
82     /* If running inside an X server use Xephyr instead */
83     if (g_getenv ("DISPLAY"))
84         command = "Xephyr";
85     if (!command)
86         command = seat_get_string_property (seat, "xserver-command");
87     if (command)
88         x_server_local_set_command (x_server, command);
89
90     /* If Plymouth is running, stop it */
91     if (plymouth_get_is_active () && plymouth_has_active_vt ())
92     {
93         gint active_vt = vt_get_active ();
94         if (active_vt >= vt_get_min ())
95         {
96             vt = active_vt;
97             g_signal_connect (x_server, "ready", G_CALLBACK (x_server_ready_cb), seat);
98             g_signal_connect (x_server, "stopped", G_CALLBACK (x_server_transition_plymouth_cb), seat);
99             plymouth_deactivate ();
100         }
101         else
102             g_debug ("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 ());
103     }
104     if (plymouth_get_is_active ())
105         plymouth_quit (FALSE);
106     if (vt < 0)
107         vt = vt_get_unused ();
108     if (vt >= 0)
109     {
110         vt_ref (vt);
111         x_server_local_set_vt (x_server, vt);
112         g_signal_connect (x_server, "stopped", G_CALLBACK (x_server_stopped_cb), seat);
113     }
114
115     layout = seat_get_string_property (seat, "xserver-layout");
116     if (layout)
117         x_server_local_set_layout (x_server, layout);
118
119     config_file = seat_get_string_property (seat, "xserver-config");
120     if (config_file)
121         x_server_local_set_config (x_server, config_file);
122   
123     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
124     x_server_local_set_allow_tcp (x_server, allow_tcp);    
125
126     xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
127     if (xdmcp_manager)
128         x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
129
130     port = seat_get_integer_property (seat, "xdmcp-port");
131     if (port > 0)
132         x_server_local_set_xdmcp_port (x_server, port);
133
134     key_name = seat_get_string_property (seat, "xdmcp-key");
135     if (key_name)
136     {
137         gchar *dir, *path;
138         GKeyFile *keys;
139         gboolean result;
140         GError *error = NULL;
141
142         dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
143         path = g_build_filename (dir, "keys.conf", NULL);
144         g_free (dir);
145
146         keys = g_key_file_new ();
147         result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
148         if (error)
149             g_debug ("Error getting key %s", error->message);
150         g_clear_error (&error);      
151
152         if (result)
153         {
154             gchar *key = NULL;
155
156             if (g_key_file_has_key (keys, "keyring", key_name, NULL))
157                 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
158             else
159                 g_debug ("Key %s not defined", key_name);
160
161             if (key)
162                 x_server_local_set_xdmcp_key (x_server, key);
163             g_free (key);
164         }
165
166         g_free (path);
167         g_key_file_free (keys);
168     }
169
170     return DISPLAY_SERVER (x_server);
171 }
172
173 static Greeter *
174 seat_xlocal_create_greeter_session (Seat *seat)
175 {
176     Greeter *greeter_session;
177
178     greeter_session = SEAT_CLASS (seat_xlocal_parent_class)->create_greeter_session (seat);
179     session_set_env (SESSION (greeter_session), "XDG_SEAT", "seat0");
180
181     return greeter_session;
182 }
183
184 static Session *
185 seat_xlocal_create_session (Seat *seat)
186 {
187     Session *session;
188
189     session = SEAT_CLASS (seat_xlocal_parent_class)->create_session (seat);
190     session_set_env (SESSION (session), "XDG_SEAT", "seat0");
191
192     return session;
193 }
194
195 static void
196 seat_xlocal_set_active_session (Seat *seat, Session *session)
197 {
198     gint vt = display_server_get_vt (session_get_display_server (session));
199     if (vt >= 0)
200         vt_set_active (vt);
201
202     SEAT_CLASS (seat_xlocal_parent_class)->set_active_session (seat, session);
203 }
204
205 static Session *
206 seat_xlocal_get_active_session (Seat *seat)
207 {
208     gint vt;
209     GList *link;
210
211     vt = vt_get_active ();
212     if (vt < 0)
213         return NULL;
214
215     for (link = seat_get_sessions (seat); link; link = link->next)
216     {
217         Session *session = link->data;
218         if (display_server_get_vt (session_get_display_server (session)) == vt)
219             return session;
220     }
221
222     return NULL;
223 }
224
225 static void
226 seat_xlocal_run_script (Seat *seat, DisplayServer *display_server, Process *script)
227 {
228     const gchar *path;
229     XServerLocal *x_server;
230
231     x_server = X_SERVER_LOCAL (display_server);
232     path = x_server_local_get_authority_file_path (x_server);
233     process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
234     process_set_env (script, "XAUTHORITY", path);
235
236     SEAT_CLASS (seat_xlocal_parent_class)->run_script (seat, display_server, script);
237 }
238
239 static void
240 seat_xlocal_init (SeatXLocal *seat)
241 {
242 }
243
244 static void
245 seat_xlocal_class_init (SeatXLocalClass *klass)
246 {
247     SeatClass *seat_class = SEAT_CLASS (klass);
248
249     seat_class->setup = seat_xlocal_setup;
250     seat_class->start = seat_xlocal_start;
251     seat_class->create_display_server = seat_xlocal_create_display_server;
252     seat_class->create_greeter_session = seat_xlocal_create_greeter_session;
253     seat_class->create_session = seat_xlocal_create_session;
254     seat_class->set_active_session = seat_xlocal_set_active_session;
255     seat_class->get_active_session = seat_xlocal_get_active_session;
256     seat_class->run_script = seat_xlocal_run_script;
257 }