2 * Copyright © 2010 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 #include <linux/major.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
37 #include "compositor.h"
39 /* Introduced in 2.6.38 */
45 struct weston_compositor *compositor;
47 struct termios terminal_attributes;
49 struct wl_event_source *input_source;
50 struct wl_event_source *vt_source;
51 tty_vt_func_t vt_func;
52 int vt, starting_vt, has_vt;
56 static int vt_handler(int signal_number, void *data)
58 struct tty *tty = data;
61 tty->vt_func(tty->compositor, TTY_LEAVE_VT);
64 ioctl(tty->fd, VT_RELDISP, 1);
66 ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
68 tty->vt_func(tty->compositor, TTY_ENTER_VT);
76 on_tty_input(int fd, uint32_t mask, void *data)
78 struct tty *tty = data;
80 /* Ignore input to tty. We get keyboard events from evdev */
81 tcflush(tty->fd, TCIFLUSH);
87 try_open_vt(struct tty *tty)
92 tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
94 weston_log("could not open tty0: %m\n");
98 if (ioctl(tty0, VT_OPENQRY, &tty->vt) < 0 || tty->vt == -1) {
99 weston_log("could not open tty0: %m\n");
105 snprintf(filename, sizeof filename, "/dev/tty%d", tty->vt);
106 weston_log("compositor: using new vt %s\n", filename);
107 fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
115 tty_activate_vt(struct tty *tty, int vt)
117 return ioctl(tty->fd, VT_ACTIVATE, vt);
121 tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func,
124 struct termios raw_attributes;
125 struct vt_mode mode = { 0 };
128 struct wl_event_loop *loop;
133 tty = malloc(sizeof *tty);
137 memset(tty, 0, sizeof *tty);
138 tty->compositor = compositor;
139 tty->vt_func = vt_func;
141 tty->fd = weston_environment_get_fd("WESTON_TTY_FD");
143 tty->fd = STDIN_FILENO;
146 snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
147 weston_log("compositor: using %s\n", filename);
148 tty->fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
150 } else if (fstat(tty->fd, &buf) == 0 &&
151 major(buf.st_rdev) == TTY_MAJOR &&
152 minor(buf.st_rdev) > 0) {
153 if (tty->fd == STDIN_FILENO)
154 tty->fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
155 tty->vt = minor(buf.st_rdev);
157 /* Fall back to try opening a new VT. This typically
159 tty->fd = try_open_vt(tty);
163 weston_log("failed to open tty: %m\n");
168 if (ioctl(tty->fd, VT_GETSTATE, &vts) == 0)
169 tty->starting_vt = vts.v_active;
171 tty->starting_vt = tty->vt;
173 if (tty->starting_vt != tty->vt) {
174 if (ioctl(tty->fd, VT_ACTIVATE, tty->vt) < 0 ||
175 ioctl(tty->fd, VT_WAITACTIVE, tty->vt) < 0) {
176 weston_log("failed to swtich to new vt\n");
181 if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
182 weston_log("could not get terminal attributes: %m\n");
186 /* Ignore control characters and disable echo */
187 raw_attributes = tty->terminal_attributes;
188 cfmakeraw(&raw_attributes);
190 /* Fix up line endings to be normal (cfmakeraw hoses them) */
191 raw_attributes.c_oflag |= OPOST | OCRNL;
193 if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
194 weston_log("could not put terminal into raw mode: %m\n");
196 loop = wl_display_get_event_loop(compositor->wl_display);
198 ioctl(tty->fd, KDGKBMODE, &tty->kb_mode);
199 ret = ioctl(tty->fd, KDSKBMODE, K_OFF);
201 ret = ioctl(tty->fd, KDSKBMODE, K_RAW);
203 weston_log("failed to set keyboard mode on tty: %m\n");
207 tty->input_source = wl_event_loop_add_fd(loop, tty->fd,
210 if (!tty->input_source)
214 ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
216 weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
221 mode.mode = VT_PROCESS;
222 mode.relsig = SIGUSR1;
223 mode.acqsig = SIGUSR1;
224 if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
225 weston_log("failed to take control of vt handling\n");
230 wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, tty);
237 ioctl(tty->fd, VT_SETMODE, &mode);
240 ioctl(tty->fd, KDSETMODE, KD_TEXT);
243 if (tty->input_source)
244 wl_event_source_remove(tty->input_source);
245 ioctl(tty->fd, KDSKBMODE, tty->kb_mode);
248 tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes);
256 void tty_reset(struct tty *tty)
258 struct vt_mode mode = { 0 };
260 if (ioctl(tty->fd, KDSKBMODE, tty->kb_mode))
261 weston_log("failed to restore keyboard mode: %m\n");
263 if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
264 weston_log("failed to set KD_TEXT mode on tty: %m\n");
266 if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
267 weston_log("could not restore terminal to canonical mode\n");
270 if (ioctl(tty->fd, VT_SETMODE, &mode) < 0)
271 weston_log("could not reset vt handling\n");
273 if (tty->has_vt && tty->vt != tty->starting_vt) {
274 ioctl(tty->fd, VT_ACTIVATE, tty->starting_vt);
275 ioctl(tty->fd, VT_WAITACTIVE, tty->starting_vt);
280 tty_destroy(struct tty *tty)
282 if (tty->input_source)
283 wl_event_source_remove(tty->input_source);
285 wl_event_source_remove(tty->vt_source);