]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-local.c
Introduce a seat object
[sojka/lightdm.git] / src / seat-local.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-local.h"
15 #include "configuration.h"
16 #include "xserver.h"
17 #include "vt.h"
18
19 struct SeatLocalPrivate
20 {
21     /* The section in the config for this seat */
22     gchar *config_section;
23 };
24
25 G_DEFINE_TYPE (SeatLocal, seat_local, SEAT_TYPE);
26
27 SeatLocal *
28 seat_local_new (const gchar *config_section)
29 {
30     SeatLocal *seat;
31
32     seat = g_object_new (SEAT_LOCAL_TYPE, NULL);
33     seat->priv->config_section = g_strdup (config_section);
34
35     return seat;
36 }
37
38 static gboolean
39 seat_local_get_can_switch (Seat *seat)
40 {
41     return TRUE;
42 }
43
44 static Display *
45 seat_local_add_display (Seat *seat)
46 {
47     XServer *xserver;
48     XAuthorization *authorization = NULL;
49     gchar *dir, *filename, *path, *command, *xserver_section = NULL;
50     gchar *number;
51     gchar hostname[1024];
52     Display *display;
53
54     g_debug ("Starting new display to switch user");
55
56     xserver = xserver_new (XSERVER_TYPE_LOCAL, NULL, xserver_get_free_display_number ());
57     number = g_strdup_printf ("%d", xserver_get_display_number (xserver));
58     gethostname (hostname, 1024);
59     authorization = xauth_new_cookie (XAUTH_FAMILY_LOCAL, hostname, number);
60     g_free (number);
61
62     xserver_set_vt (xserver, vt_get_unused ());
63
64     command = config_get_string (config_get_instance (), "LightDM", "default-xserver-command");
65     xserver_set_command (xserver, command);
66     g_free (command);
67
68     xserver_set_authorization (xserver, authorization);
69     g_object_unref (authorization);
70
71     filename = g_strdup_printf ("%s.log", xserver_get_address (xserver));
72     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
73     path = g_build_filename (dir, filename, NULL);
74     g_debug ("Logging to %s", path);
75     child_process_set_log_file (CHILD_PROCESS (xserver), path);
76     g_free (filename);
77     g_free (dir);
78     g_free (path);
79
80     /* Get the X server configuration */
81     if (SEAT_LOCAL (seat)->priv->config_section)
82         xserver_section = config_get_string (config_get_instance (), SEAT_LOCAL (seat)->priv->config_section, "xserver");
83     if (!xserver_section)
84         xserver_section = config_get_string (config_get_instance (), "LightDM", "xserver");
85
86     if (xserver_section)
87     {
88         gchar *xserver_command, *xserver_layout, *xserver_config_file;
89
90         g_debug ("Using X server configuration '%s' for display '%s'", xserver_section, SEAT_LOCAL (seat)->priv->config_section ? SEAT_LOCAL (seat)->priv->config_section : "<anonymous>");
91
92         xserver_command = config_get_string (config_get_instance (), xserver_section, "command");
93         if (xserver_command)
94             xserver_set_command (xserver, xserver_command);
95         g_free (xserver_command);
96
97         xserver_layout = config_get_string (config_get_instance (), xserver_section, "layout");
98         if (xserver_layout)
99             xserver_set_layout (xserver, xserver_layout);
100         g_free (xserver_layout);
101
102         xserver_config_file = config_get_string (config_get_instance (), xserver_section, "config-file");
103         if (xserver_config_file)
104             xserver_set_config_file (xserver, xserver_config_file);
105         g_free (xserver_config_file);
106
107         g_free (xserver_section);
108     }
109
110     if (config_get_boolean (config_get_instance (), "LightDM", "use-xephyr"))
111         xserver_set_command (xserver, "Xephyr");
112
113     display = display_new (xserver);
114     g_object_unref (xserver);
115
116     return display;
117 }
118
119 static void
120 seat_local_set_active_display (Seat *seat, Display *display)
121 {
122     gint number = xserver_get_vt (display_get_xserver (display));
123     if (number >= 0)
124         vt_set_active (number);
125 }
126
127 static void
128 seat_local_init (SeatLocal *seat)
129 {
130     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_LOCAL_TYPE, SeatLocalPrivate);
131 }
132
133 static void
134 seat_local_finalize (GObject *object)
135 {
136     SeatLocal *self;
137
138     self = SEAT_LOCAL (object);
139
140     g_free (self->priv->config_section);
141
142     G_OBJECT_CLASS (seat_local_parent_class)->finalize (object);
143 }
144
145 static void
146 seat_local_class_init (SeatLocalClass *klass)
147 {
148     GObjectClass *object_class = G_OBJECT_CLASS (klass);
149     SeatClass *seat_class = SEAT_CLASS (klass);
150
151     seat_class->get_can_switch = seat_local_get_can_switch;
152     seat_class->add_display = seat_local_add_display;
153     seat_class->set_active_display = seat_local_set_active_display;
154     object_class->finalize = seat_local_finalize;
155
156     g_type_class_add_private (klass, sizeof (SeatLocalPrivate));
157 }