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