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>
35 #include "compositor.h"
38 struct weston_compositor *compositor;
40 struct termios terminal_attributes;
42 struct wl_event_source *input_source;
43 struct wl_event_source *enter_vt_source;
44 struct wl_event_source *leave_vt_source;
45 tty_vt_func_t vt_func;
48 static int on_enter_vt(int signal_number, void *data)
50 struct tty *tty = data;
52 ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
54 tty->vt_func(tty->compositor, TTY_ENTER_VT);
60 on_leave_vt(int signal_number, void *data)
62 struct tty *tty = data;
64 tty->vt_func(tty->compositor, TTY_LEAVE_VT);
66 ioctl(tty->fd, VT_RELDISP, 1);
72 on_tty_input(int fd, uint32_t mask, void *data)
74 struct tty *tty = data;
76 /* Ignore input to tty. We get keyboard events from evdev
78 tcflush(tty->fd, TCIFLUSH);
89 tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
91 fprintf(stderr, "could not open tty0: %m\n");
95 if (ioctl(tty0, VT_OPENQRY, &vt) < 0 || vt == -1) {
96 fprintf(stderr, "could not open tty0: %m\n");
102 snprintf(filename, sizeof filename, "/dev/tty%d", vt);
103 fprintf(stderr, "compositor: using new vt %s\n", filename);
104 fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
108 if (ioctl(fd, VT_ACTIVATE, vt) < 0 ||
109 ioctl(fd, VT_WAITACTIVE, vt) < 0) {
110 fprintf(stderr, "failed to swtich to new vt\n");
119 tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func,
122 struct termios raw_attributes;
123 struct vt_mode mode = { 0 };
126 struct wl_event_loop *loop;
130 tty = malloc(sizeof *tty);
134 memset(tty, 0, sizeof *tty);
135 tty->compositor = compositor;
136 tty->vt_func = vt_func;
138 snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
139 fprintf(stderr, "compositor: using %s\n", filename);
140 tty->fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
141 } else if (fstat(tty->fd, &buf) == 0 &&
142 major(buf.st_rdev) == TTY_MAJOR &&
143 minor(buf.st_rdev) > 0) {
144 tty->fd = fcntl(0, F_DUPFD_CLOEXEC, 0);
146 /* Fall back to try opening a new VT. This typically
148 tty->fd = try_open_vt();
152 fprintf(stderr, "failed to open tty: %m\n");
156 if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
157 fprintf(stderr, "could not get terminal attributes: %m\n");
161 /* Ignore control characters and disable echo */
162 raw_attributes = tty->terminal_attributes;
163 cfmakeraw(&raw_attributes);
165 /* Fix up line endings to be normal (cfmakeraw hoses them) */
166 raw_attributes.c_oflag |= OPOST | OCRNL;
168 if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
169 fprintf(stderr, "could not put terminal into raw mode: %m\n");
171 loop = wl_display_get_event_loop(compositor->wl_display);
173 wl_event_loop_add_fd(loop, tty->fd,
174 WL_EVENT_READABLE, on_tty_input, tty);
176 ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
178 fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n");
182 tty->compositor->focus = 1;
183 mode.mode = VT_PROCESS;
184 mode.relsig = SIGUSR1;
185 mode.acqsig = SIGUSR2;
186 if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
187 fprintf(stderr, "failed to take control of vt handling\n");
191 tty->leave_vt_source =
192 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, tty);
193 tty->enter_vt_source =
194 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, tty);
200 tty_destroy(struct tty *tty)
205 if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
207 "failed to set KD_TEXT mode on tty: %m\n");
209 if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
211 "could not restore terminal to canonical mode\n");