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 *vt_source;
44 tty_vt_func_t vt_func;
45 int vt, starting_vt, has_vt;
48 static int vt_handler(int signal_number, void *data)
50 struct tty *tty = data;
53 tty->vt_func(tty->compositor, TTY_LEAVE_VT);
56 ioctl(tty->fd, VT_RELDISP, 1);
58 ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
60 tty->vt_func(tty->compositor, TTY_ENTER_VT);
68 on_tty_input(int fd, uint32_t mask, void *data)
70 struct tty *tty = data;
72 /* Ignore input to tty. We get keyboard events from evdev
74 tcflush(tty->fd, TCIFLUSH);
80 try_open_vt(struct tty *tty)
86 tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
88 fprintf(stderr, "could not open tty0: %m\n");
92 if (ioctl(tty0, VT_OPENQRY, &tty->vt) < 0 || tty->vt == -1) {
93 fprintf(stderr, "could not open tty0: %m\n");
99 snprintf(filename, sizeof filename, "/dev/tty%d", tty->vt);
100 fprintf(stderr, "compositor: using new vt %s\n", filename);
101 fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
105 if (ioctl(fd, VT_GETSTATE, &vts) == 0)
106 tty->starting_vt = vts.v_active;
108 tty->starting_vt = tty->vt;
110 if (ioctl(fd, VT_ACTIVATE, tty->vt) < 0 ||
111 ioctl(fd, VT_WAITACTIVE, tty->vt) < 0) {
112 fprintf(stderr, "failed to swtich to new vt\n");
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;
132 tty = malloc(sizeof *tty);
136 memset(tty, 0, sizeof *tty);
137 tty->compositor = compositor;
138 tty->vt_func = vt_func;
140 snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
141 fprintf(stderr, "compositor: using %s\n", filename);
142 tty->fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
143 } else if (fstat(tty->fd, &buf) == 0 &&
144 major(buf.st_rdev) == TTY_MAJOR &&
145 minor(buf.st_rdev) > 0) {
146 tty->fd = fcntl(0, F_DUPFD_CLOEXEC, 0);
148 /* Fall back to try opening a new VT. This typically
150 tty->fd = try_open_vt(tty);
154 fprintf(stderr, "failed to open tty: %m\n");
158 if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
159 fprintf(stderr, "could not get terminal attributes: %m\n");
163 /* Ignore control characters and disable echo */
164 raw_attributes = tty->terminal_attributes;
165 cfmakeraw(&raw_attributes);
167 /* Fix up line endings to be normal (cfmakeraw hoses them) */
168 raw_attributes.c_oflag |= OPOST | OCRNL;
170 if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
171 fprintf(stderr, "could not put terminal into raw mode: %m\n");
173 loop = wl_display_get_event_loop(compositor->wl_display);
175 wl_event_loop_add_fd(loop, tty->fd,
176 WL_EVENT_READABLE, on_tty_input, tty);
178 ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
180 fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n");
185 mode.mode = VT_PROCESS;
186 mode.relsig = SIGUSR1;
187 mode.acqsig = SIGUSR1;
188 if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
189 fprintf(stderr, "failed to take control of vt handling\n");
194 wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, tty);
200 tty_destroy(struct tty *tty)
202 struct vt_mode mode = { 0 };
207 if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
209 "failed to set KD_TEXT mode on tty: %m\n");
211 if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
213 "could not restore terminal to canonical mode\n");
216 if (ioctl(tty->fd, VT_SETMODE, &mode) < 0)
217 fprintf(stderr, "could not reset vt handling\n");
219 if (tty->has_vt && tty->vt != tty->starting_vt) {
220 ioctl(tty->fd, VT_ACTIVATE, tty->starting_vt);
221 ioctl(tty->fd, VT_WAITACTIVE, tty->starting_vt);
224 wl_event_source_remove(tty->input_source);
225 wl_event_source_remove(tty->vt_source);