]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-xlocal.c
Half baked code before 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 "xserver-local.h"
17 #include "xsession.h"
18 #include "mir-server.h"
19 #include "vt.h"
20
21 G_DEFINE_TYPE (SeatXLocal, seat_xlocal, SEAT_TYPE);
22
23 static void
24 seat_xlocal_setup (Seat *seat)
25 {
26     seat_set_can_switch (seat, TRUE);
27     seat_set_share_display_server (seat, seat_get_boolean_property (seat, "xserver-share"));
28     SEAT_CLASS (seat_xlocal_parent_class)->setup (seat);
29 }
30
31 static DisplayServer *
32 create_x_server (Seat *seat)
33 {
34     XServerLocal *xserver;
35     const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
36     gboolean allow_tcp;
37     gint port = 0;
38
39     g_debug ("Starting local X display");
40   
41     xserver = xserver_local_new ();
42
43     /* If running inside an X server use Xephyr instead */
44     if (g_getenv ("DISPLAY"))
45         command = "Xephyr";
46     if (!command)
47         command = seat_get_string_property (seat, "xserver-command");
48     if (command)
49         xserver_local_set_command (xserver, command);
50
51     layout = seat_get_string_property (seat, "xserver-layout");
52     if (layout)
53         xserver_local_set_layout (xserver, layout);
54
55     config_file = seat_get_string_property (seat, "xserver-config");
56     if (config_file)
57         xserver_local_set_config (xserver, config_file);
58   
59     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
60     xserver_local_set_allow_tcp (xserver, allow_tcp);    
61
62     xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
63     if (xdmcp_manager)
64         xserver_local_set_xdmcp_server (xserver, xdmcp_manager);
65
66     port = seat_get_integer_property (seat, "xdmcp-port");
67     if (port > 0)
68         xserver_local_set_xdmcp_port (xserver, port);
69
70     key_name = seat_get_string_property (seat, "xdmcp-key");
71     if (key_name)
72     {
73         gchar *dir, *path;
74         GKeyFile *keys;
75         gboolean result;
76         GError *error = NULL;
77
78         dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
79         path = g_build_filename (dir, "keys.conf", NULL);
80         g_free (dir);
81
82         keys = g_key_file_new ();
83         result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
84         if (error)
85             g_debug ("Error getting key %s", error->message);
86         g_clear_error (&error);      
87
88         if (result)
89         {
90             gchar *key = NULL;
91
92             if (g_key_file_has_key (keys, "keyring", key_name, NULL))
93                 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
94             else
95                 g_debug ("Key %s not defined", key_name);
96
97             if (key)
98                 xserver_local_set_xdmcp_key (xserver, key);
99             g_free (key);
100         }
101
102         g_free (path);
103         g_key_file_free (keys);
104     }
105
106     return DISPLAY_SERVER (xserver);
107 }
108
109 static DisplayServer *
110 create_mir_server (Seat *seat)
111 {
112     MirServer *mir_server;
113
114     mir_server = mir_server_new ();
115     // FIXME: Set VT
116
117     return DISPLAY_SERVER (mir_server);
118 }
119
120 static DisplayServer *
121 seat_xlocal_create_display_server (Seat *seat, const gchar *session_type)
122 {
123     if (strcmp (session_type, "x") == 0)
124         return create_x_server (seat);
125     else if (strcmp (session_type, "mir") == 0)
126         return create_mir_server (seat);
127     else
128     {
129         g_warning ("Can't create unsupported display server '%s'", session_type);
130         return NULL;
131     }
132 }
133
134 static Session *
135 seat_xlocal_create_session (Seat *seat, Display *display)
136 {
137     XServerLocal *xserver;
138     XSession *session;
139     gchar *t;
140
141     xserver = XSERVER_LOCAL (display_get_display_server (display));
142
143     session = xsession_new ();
144     t = g_strdup_printf ("/dev/tty%d", xserver_local_get_vt (xserver));
145     session_set_tty (SESSION (session), t);
146     g_free (t);
147
148     /* Set variables for logind */
149     session_set_env (SESSION (session), "XDG_SEAT", "seat0");
150     t = g_strdup_printf ("%d", xserver_local_get_vt (xserver));
151     session_set_env (SESSION (session), "XDG_VTNR", t);
152     g_free (t);
153
154     return SESSION (session);
155 }
156
157 static void
158 seat_xlocal_set_active_display (Seat *seat, Display *display)
159 {
160     gint vt = xserver_local_get_vt (XSERVER_LOCAL (display_get_display_server (display)));
161     if (vt >= 0)
162         vt_set_active (vt);
163
164     SEAT_CLASS (seat_xlocal_parent_class)->set_active_display (seat, display);
165 }
166
167 static Display *
168 seat_xlocal_get_active_display (Seat *seat)
169 {
170     gint vt;
171     GList *link;
172
173     vt = vt_get_active ();
174     if (vt < 0)
175         return NULL;
176
177     for (link = seat_get_displays (seat); link; link = link->next)
178     {
179         Display *display = link->data;
180         XServerLocal *xserver;
181
182         xserver = XSERVER_LOCAL (display_get_display_server (display));
183         if (xserver_local_get_vt (xserver) == vt)
184             return display;
185     }
186
187     return NULL;
188 }
189
190 static void
191 seat_xlocal_run_script (Seat *seat, Display *display, Process *script)
192 {
193     const gchar *path;
194     XServerLocal *xserver;
195
196     xserver = XSERVER_LOCAL (display_get_display_server (display));
197     path = xserver_local_get_authority_file_path (xserver);
198     process_set_env (script, "DISPLAY", xserver_get_address (XSERVER (xserver)));
199     process_set_env (script, "XAUTHORITY", path);
200
201     SEAT_CLASS (seat_xlocal_parent_class)->run_script (seat, display, script);
202 }
203
204 static void
205 seat_xlocal_init (SeatXLocal *seat)
206 {
207 }
208
209 static void
210 seat_xlocal_class_init (SeatXLocalClass *klass)
211 {
212     SeatClass *seat_class = SEAT_CLASS (klass);
213
214     seat_class->setup = seat_xlocal_setup;
215     seat_class->create_display_server = seat_xlocal_create_display_server;
216     seat_class->create_session = seat_xlocal_create_session;
217     seat_class->set_active_display = seat_xlocal_set_active_display;
218     seat_class->get_active_display = seat_xlocal_get_active_display;
219     seat_class->run_script = seat_xlocal_run_script;
220 }