Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 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. |
| 13 | * |
| 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. |
| 21 | */ |
| 22 | |
| 23 | #include <termios.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <signal.h> |
| 30 | #include <linux/kd.h> |
| 31 | #include <linux/vt.h> |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 32 | #include <linux/major.h> |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 33 | #include <sys/ioctl.h> |
| 34 | |
| 35 | #include "compositor.h" |
| 36 | |
| 37 | struct tty { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 38 | struct weston_compositor *compositor; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 39 | int fd; |
| 40 | struct termios terminal_attributes; |
| 41 | |
| 42 | struct wl_event_source *input_source; |
Kristian Høgsberg | 08fcbf0 | 2012-01-18 12:42:16 -0500 | [diff] [blame] | 43 | struct wl_event_source *vt_source; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 44 | tty_vt_func_t vt_func; |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 45 | int vt, starting_vt, has_vt; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 46 | }; |
| 47 | |
Kristian Høgsberg | 08fcbf0 | 2012-01-18 12:42:16 -0500 | [diff] [blame] | 48 | static int vt_handler(int signal_number, void *data) |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 49 | { |
| 50 | struct tty *tty = data; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 51 | |
Kristian Høgsberg | 08fcbf0 | 2012-01-18 12:42:16 -0500 | [diff] [blame] | 52 | if (tty->has_vt) { |
| 53 | tty->vt_func(tty->compositor, TTY_LEAVE_VT); |
| 54 | tty->has_vt = 0; |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 55 | |
Kristian Høgsberg | 08fcbf0 | 2012-01-18 12:42:16 -0500 | [diff] [blame] | 56 | ioctl(tty->fd, VT_RELDISP, 1); |
| 57 | } else { |
| 58 | ioctl(tty->fd, VT_RELDISP, VT_ACKACQ); |
Kristian Høgsberg | 353e57f | 2012-01-16 10:57:14 -0500 | [diff] [blame] | 59 | |
Kristian Høgsberg | 08fcbf0 | 2012-01-18 12:42:16 -0500 | [diff] [blame] | 60 | tty->vt_func(tty->compositor, TTY_ENTER_VT); |
| 61 | tty->has_vt = 1; |
| 62 | } |
Kristian Høgsberg | 353e57f | 2012-01-16 10:57:14 -0500 | [diff] [blame] | 63 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 64 | return 1; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 67 | static int |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 68 | on_tty_input(int fd, uint32_t mask, void *data) |
| 69 | { |
| 70 | struct tty *tty = data; |
| 71 | |
| 72 | /* Ignore input to tty. We get keyboard events from evdev |
| 73 | */ |
| 74 | tcflush(tty->fd, TCIFLUSH); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 75 | |
| 76 | return 1; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Kristian Høgsberg | 39d908e | 2012-01-16 22:42:22 -0500 | [diff] [blame] | 79 | static int |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 80 | try_open_vt(struct tty *tty) |
Kristian Høgsberg | 39d908e | 2012-01-16 22:42:22 -0500 | [diff] [blame] | 81 | { |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 82 | int tty0, fd; |
Kristian Høgsberg | 39d908e | 2012-01-16 22:42:22 -0500 | [diff] [blame] | 83 | char filename[16]; |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 84 | struct vt_stat vts; |
Kristian Høgsberg | 39d908e | 2012-01-16 22:42:22 -0500 | [diff] [blame] | 85 | |
| 86 | tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC); |
| 87 | if (tty0 < 0) { |
| 88 | fprintf(stderr, "could not open tty0: %m\n"); |
| 89 | return -1; |
| 90 | } |
| 91 | |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 92 | if (ioctl(tty0, VT_OPENQRY, &tty->vt) < 0 || tty->vt == -1) { |
Kristian Høgsberg | 39d908e | 2012-01-16 22:42:22 -0500 | [diff] [blame] | 93 | fprintf(stderr, "could not open tty0: %m\n"); |
| 94 | close(tty0); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | close(tty0); |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 99 | snprintf(filename, sizeof filename, "/dev/tty%d", tty->vt); |
Kristian Høgsberg | 39d908e | 2012-01-16 22:42:22 -0500 | [diff] [blame] | 100 | fprintf(stderr, "compositor: using new vt %s\n", filename); |
| 101 | fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC); |
| 102 | if (fd < 0) |
| 103 | return fd; |
| 104 | |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 105 | if (ioctl(fd, VT_GETSTATE, &vts) == 0) |
| 106 | tty->starting_vt = vts.v_active; |
| 107 | else |
| 108 | tty->starting_vt = tty->vt; |
| 109 | |
| 110 | if (ioctl(fd, VT_ACTIVATE, tty->vt) < 0 || |
| 111 | ioctl(fd, VT_WAITACTIVE, tty->vt) < 0) { |
Kristian Høgsberg | 39d908e | 2012-01-16 22:42:22 -0500 | [diff] [blame] | 112 | fprintf(stderr, "failed to swtich to new vt\n"); |
| 113 | close(fd); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | return fd; |
| 118 | } |
| 119 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 120 | struct tty * |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 121 | tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func, |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 122 | int tty_nr) |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 123 | { |
| 124 | struct termios raw_attributes; |
| 125 | struct vt_mode mode = { 0 }; |
| 126 | int ret; |
| 127 | struct tty *tty; |
| 128 | struct wl_event_loop *loop; |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 129 | struct stat buf; |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 130 | char filename[16]; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 131 | |
| 132 | tty = malloc(sizeof *tty); |
| 133 | if (tty == NULL) |
| 134 | return NULL; |
| 135 | |
| 136 | memset(tty, 0, sizeof *tty); |
| 137 | tty->compositor = compositor; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 138 | tty->vt_func = vt_func; |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 139 | if (tty_nr > 0) { |
| 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); |
Kristian Høgsberg | 39d908e | 2012-01-16 22:42:22 -0500 | [diff] [blame] | 143 | } else if (fstat(tty->fd, &buf) == 0 && |
| 144 | major(buf.st_rdev) == TTY_MAJOR && |
| 145 | minor(buf.st_rdev) > 0) { |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 146 | tty->fd = fcntl(0, F_DUPFD_CLOEXEC, 0); |
Kristian Høgsberg | 39d908e | 2012-01-16 22:42:22 -0500 | [diff] [blame] | 147 | } else { |
| 148 | /* Fall back to try opening a new VT. This typically |
| 149 | * requires root. */ |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 150 | tty->fd = try_open_vt(tty); |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 153 | if (tty->fd <= 0) { |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 154 | fprintf(stderr, "failed to open tty: %m\n"); |
| 155 | return NULL; |
| 156 | } |
| 157 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 158 | if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) { |
| 159 | fprintf(stderr, "could not get terminal attributes: %m\n"); |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | /* Ignore control characters and disable echo */ |
| 164 | raw_attributes = tty->terminal_attributes; |
| 165 | cfmakeraw(&raw_attributes); |
| 166 | |
| 167 | /* Fix up line endings to be normal (cfmakeraw hoses them) */ |
| 168 | raw_attributes.c_oflag |= OPOST | OCRNL; |
| 169 | |
| 170 | if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0) |
| 171 | fprintf(stderr, "could not put terminal into raw mode: %m\n"); |
| 172 | |
| 173 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 174 | tty->input_source = |
| 175 | wl_event_loop_add_fd(loop, tty->fd, |
| 176 | WL_EVENT_READABLE, on_tty_input, tty); |
| 177 | |
| 178 | ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS); |
| 179 | if (ret) { |
| 180 | fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n"); |
| 181 | return NULL; |
| 182 | } |
| 183 | |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 184 | tty->has_vt = 1; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 185 | mode.mode = VT_PROCESS; |
| 186 | mode.relsig = SIGUSR1; |
Kristian Høgsberg | 08fcbf0 | 2012-01-18 12:42:16 -0500 | [diff] [blame] | 187 | mode.acqsig = SIGUSR1; |
David Herrmann | 7172d9e | 2011-12-02 16:16:40 +0100 | [diff] [blame] | 188 | if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) { |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 189 | fprintf(stderr, "failed to take control of vt handling\n"); |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Kristian Høgsberg | 08fcbf0 | 2012-01-18 12:42:16 -0500 | [diff] [blame] | 193 | tty->vt_source = |
| 194 | wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, tty); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 195 | |
| 196 | return tty; |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | tty_destroy(struct tty *tty) |
| 201 | { |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 202 | struct vt_mode mode = { 0 }; |
| 203 | |
Tim Wiederhake | 70af98c | 2011-01-25 12:01:00 +0100 | [diff] [blame] | 204 | if(!tty) |
| 205 | return; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 206 | |
Tim Wiederhake | 70af98c | 2011-01-25 12:01:00 +0100 | [diff] [blame] | 207 | if (ioctl(tty->fd, KDSETMODE, KD_TEXT)) |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 208 | fprintf(stderr, |
Tim Wiederhake | 70af98c | 2011-01-25 12:01:00 +0100 | [diff] [blame] | 209 | "failed to set KD_TEXT mode on tty: %m\n"); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 210 | |
| 211 | if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0) |
| 212 | fprintf(stderr, |
| 213 | "could not restore terminal to canonical mode\n"); |
| 214 | |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 215 | mode.mode = VT_AUTO; |
| 216 | if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) |
| 217 | fprintf(stderr, "could not reset vt handling\n"); |
| 218 | |
| 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); |
| 222 | } |
| 223 | |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame^] | 224 | wl_event_source_remove(tty->input_source); |
| 225 | wl_event_source_remove(tty->vt_source); |
| 226 | |
Kristian Høgsberg | 00513ab | 2012-01-15 15:20:53 -0500 | [diff] [blame] | 227 | close(tty->fd); |
| 228 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 229 | free(tty); |
| 230 | } |