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