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 <sys/ioctl.h>
34 #include "compositor.h"
37 struct weston_compositor *compositor;
39 struct termios terminal_attributes;
41 struct wl_event_source *input_source;
42 struct wl_event_source *enter_vt_source;
43 struct wl_event_source *leave_vt_source;
44 tty_vt_func_t vt_func;
47 static int on_enter_vt(int signal_number, void *data)
49 struct tty *tty = data;
52 tty->vt_func(tty->compositor, TTY_ENTER_VT);
54 ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
55 ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
57 fprintf(stderr, "failed to set KD_GRAPHICS mode on console: %m\n");
63 on_leave_vt(int signal_number, void *data)
65 struct tty *tty = data;
68 ioctl (tty->fd, VT_RELDISP, 1);
69 ret = ioctl(tty->fd, KDSETMODE, KD_TEXT);
72 "failed to set KD_TEXT mode on console: %m\n");
74 tty->vt_func(tty->compositor, TTY_LEAVE_VT);
80 on_tty_input(int fd, uint32_t mask, void *data)
82 struct tty *tty = data;
84 /* Ignore input to tty. We get keyboard events from evdev
86 tcflush(tty->fd, TCIFLUSH);
92 tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func,
95 struct termios raw_attributes;
96 struct vt_mode mode = { 0 };
99 struct wl_event_loop *loop;
102 tty = malloc(sizeof *tty);
106 snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
107 fprintf(stderr, "compositor: using %s\n", filename);
109 memset(tty, 0, sizeof *tty);
110 tty->compositor = compositor;
111 tty->vt_func = vt_func;
112 tty->fd = open(filename, O_RDWR | O_NOCTTY);
114 fprintf(stderr, "failed to open active tty: %m\n");
118 if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
119 fprintf(stderr, "could not get terminal attributes: %m\n");
123 /* Ignore control characters and disable echo */
124 raw_attributes = tty->terminal_attributes;
125 cfmakeraw(&raw_attributes);
127 /* Fix up line endings to be normal (cfmakeraw hoses them) */
128 raw_attributes.c_oflag |= OPOST | OCRNL;
130 if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
131 fprintf(stderr, "could not put terminal into raw mode: %m\n");
133 loop = wl_display_get_event_loop(compositor->wl_display);
135 wl_event_loop_add_fd(loop, tty->fd,
136 WL_EVENT_READABLE, on_tty_input, tty);
138 ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
140 fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n");
144 tty->compositor->focus = 1;
145 mode.mode = VT_PROCESS;
146 mode.relsig = SIGUSR1;
147 mode.acqsig = SIGUSR2;
148 if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
149 fprintf(stderr, "failed to take control of vt handling\n");
153 tty->leave_vt_source =
154 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, tty);
155 tty->enter_vt_source =
156 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, tty);
162 tty_destroy(struct tty *tty)
167 if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
169 "failed to set KD_TEXT mode on tty: %m\n");
171 if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
173 "could not restore terminal to canonical mode\n");