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"
37 /* Introduced in 2.6.38 */
43 struct weston_compositor *compositor;
45 struct termios terminal_attributes;
47 struct wl_event_source *input_source;
48 struct wl_event_source *vt_source;
49 tty_vt_func_t vt_func;
50 int vt, starting_vt, has_vt;
54 static int vt_handler(int signal_number, void *data)
56 struct tty *tty = data;
59 tty->vt_func(tty->compositor, TTY_LEAVE_VT);
62 ioctl(tty->fd, VT_RELDISP, 1);
64 ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
66 tty->vt_func(tty->compositor, TTY_ENTER_VT);
74 on_tty_input(int fd, uint32_t mask, void *data)
76 struct tty *tty = data;
78 /* Ignore input to tty. We get keyboard events from evdev */
79 tcflush(tty->fd, TCIFLUSH);
85 try_open_vt(struct tty *tty)
90 tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
92 fprintf(stderr, "could not open tty0: %m\n");
96 if (ioctl(tty0, VT_OPENQRY, &tty->vt) < 0 || tty->vt == -1) {
97 fprintf(stderr, "could not open tty0: %m\n");
103 snprintf(filename, sizeof filename, "/dev/tty%d", tty->vt);
104 fprintf(stderr, "compositor: using new vt %s\n", filename);
105 fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
113 tty_activate_vt(struct tty *tty, int vt)
115 return ioctl(tty->fd, VT_ACTIVATE, vt);
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;
131 tty = malloc(sizeof *tty);
135 memset(tty, 0, sizeof *tty);
136 tty->compositor = compositor;
137 tty->vt_func = vt_func;
139 tty->fd = weston_environment_get_fd("WESTON_TTY_FD");
141 tty->fd = STDIN_FILENO;
144 snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
145 fprintf(stderr, "compositor: using %s\n", filename);
146 tty->fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
148 } else if (fstat(tty->fd, &buf) == 0 &&
149 major(buf.st_rdev) == TTY_MAJOR &&
150 minor(buf.st_rdev) > 0) {
151 if (tty->fd == STDIN_FILENO)
152 tty->fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
153 tty->vt = minor(buf.st_rdev);
155 /* Fall back to try opening a new VT. This typically
157 tty->fd = try_open_vt(tty);
161 fprintf(stderr, "failed to open tty: %m\n");
166 if (ioctl(tty->fd, VT_GETSTATE, &vts) == 0)
167 tty->starting_vt = vts.v_active;
169 tty->starting_vt = tty->vt;
171 if (tty->starting_vt != tty->vt) {
172 if (ioctl(tty->fd, VT_ACTIVATE, tty->vt) < 0 ||
173 ioctl(tty->fd, VT_WAITACTIVE, tty->vt) < 0) {
174 fprintf(stderr, "failed to swtich to new vt\n");
179 if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
180 fprintf(stderr, "could not get terminal attributes: %m\n");
184 /* Ignore control characters and disable echo */
185 raw_attributes = tty->terminal_attributes;
186 cfmakeraw(&raw_attributes);
188 /* Fix up line endings to be normal (cfmakeraw hoses them) */
189 raw_attributes.c_oflag |= OPOST | OCRNL;
191 if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
192 fprintf(stderr, "could not put terminal into raw mode: %m\n");
194 loop = wl_display_get_event_loop(compositor->wl_display);
196 ioctl(tty->fd, KDGKBMODE, &tty->kb_mode);
197 ret = ioctl(tty->fd, KDSKBMODE, K_OFF);
199 ret = ioctl(tty->fd, KDSKBMODE, K_RAW);
203 tty->input_source = wl_event_loop_add_fd(loop, tty->fd,
206 if (!tty->input_source)
211 fprintf(stderr, "failed to set K_OFF keyboard mode on tty: %m\n");
215 ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
217 fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n");
222 mode.mode = VT_PROCESS;
223 mode.relsig = SIGUSR1;
224 mode.acqsig = SIGUSR1;
225 if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
226 fprintf(stderr, "failed to take control of vt handling\n");
231 wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, tty);
238 ioctl(tty->fd, VT_SETMODE, &mode);
241 ioctl(tty->fd, KDSETMODE, KD_TEXT);
244 if (tty->input_source)
245 wl_event_source_remove(tty->input_source);
246 ioctl(tty->fd, KDSKBMODE, tty->kb_mode);
249 tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes);
258 tty_destroy(struct tty *tty)
260 struct vt_mode mode = { 0 };
262 if (tty->input_source)
263 wl_event_source_remove(tty->input_source);
265 if (ioctl(tty->fd, KDSKBMODE, tty->kb_mode))
266 fprintf(stderr, "failed to restore keyboard mode: %m\n");
268 if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
270 "failed to set KD_TEXT mode on tty: %m\n");
272 if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
274 "could not restore terminal to canonical mode\n");
277 if (ioctl(tty->fd, VT_SETMODE, &mode) < 0)
278 fprintf(stderr, "could not reset vt handling\n");
280 if (tty->has_vt && tty->vt != tty->starting_vt) {
281 ioctl(tty->fd, VT_ACTIVATE, tty->starting_vt);
282 ioctl(tty->fd, VT_WAITACTIVE, tty->starting_vt);
285 wl_event_source_remove(tty->vt_source);