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; |
| 43 | struct wl_event_source *enter_vt_source; |
| 44 | struct wl_event_source *leave_vt_source; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 45 | tty_vt_func_t vt_func; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 46 | }; |
| 47 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 48 | static int on_enter_vt(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 | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 52 | ioctl(tty->fd, VT_RELDISP, VT_ACKACQ); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 53 | |
Kristian Høgsberg | 353e57f | 2012-01-16 10:57:14 -0500 | [diff] [blame^] | 54 | tty->vt_func(tty->compositor, TTY_ENTER_VT); |
| 55 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 56 | return 1; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 59 | static int |
| 60 | on_leave_vt(int signal_number, void *data) |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 61 | { |
| 62 | struct tty *tty = data; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 63 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 64 | tty->vt_func(tty->compositor, TTY_LEAVE_VT); |
| 65 | |
Kristian Høgsberg | 353e57f | 2012-01-16 10:57:14 -0500 | [diff] [blame^] | 66 | ioctl(tty->fd, VT_RELDISP, 1); |
| 67 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 68 | return 1; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 71 | static int |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 72 | on_tty_input(int fd, uint32_t mask, void *data) |
| 73 | { |
| 74 | struct tty *tty = data; |
| 75 | |
| 76 | /* Ignore input to tty. We get keyboard events from evdev |
| 77 | */ |
| 78 | tcflush(tty->fd, TCIFLUSH); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 79 | |
| 80 | return 1; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | struct tty * |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 84 | tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func, |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 85 | int tty_nr) |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 86 | { |
| 87 | struct termios raw_attributes; |
| 88 | struct vt_mode mode = { 0 }; |
| 89 | int ret; |
| 90 | struct tty *tty; |
| 91 | struct wl_event_loop *loop; |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 92 | struct stat buf; |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 93 | char filename[16]; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 94 | |
| 95 | tty = malloc(sizeof *tty); |
| 96 | if (tty == NULL) |
| 97 | return NULL; |
| 98 | |
| 99 | memset(tty, 0, sizeof *tty); |
| 100 | tty->compositor = compositor; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 101 | tty->vt_func = vt_func; |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 102 | if (tty_nr > 0) { |
| 103 | snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr); |
| 104 | fprintf(stderr, "compositor: using %s\n", filename); |
| 105 | tty->fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC); |
| 106 | } else { |
| 107 | tty->fd = fcntl(0, F_DUPFD_CLOEXEC, 0); |
| 108 | } |
| 109 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 110 | if (tty->fd <= 0) { |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 111 | fprintf(stderr, "failed to open tty: %m\n"); |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | if (fstat(tty->fd, &buf) < 0 || |
| 116 | major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) { |
| 117 | fprintf(stderr, "stdin not a vt (%d, %d)\n", |
| 118 | major(buf.st_dev), minor(buf.st_dev)); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) { |
| 123 | fprintf(stderr, "could not get terminal attributes: %m\n"); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | /* Ignore control characters and disable echo */ |
| 128 | raw_attributes = tty->terminal_attributes; |
| 129 | cfmakeraw(&raw_attributes); |
| 130 | |
| 131 | /* Fix up line endings to be normal (cfmakeraw hoses them) */ |
| 132 | raw_attributes.c_oflag |= OPOST | OCRNL; |
| 133 | |
| 134 | if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0) |
| 135 | fprintf(stderr, "could not put terminal into raw mode: %m\n"); |
| 136 | |
| 137 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 138 | tty->input_source = |
| 139 | wl_event_loop_add_fd(loop, tty->fd, |
| 140 | WL_EVENT_READABLE, on_tty_input, tty); |
| 141 | |
| 142 | ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS); |
| 143 | if (ret) { |
| 144 | fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n"); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | tty->compositor->focus = 1; |
| 149 | mode.mode = VT_PROCESS; |
| 150 | mode.relsig = SIGUSR1; |
| 151 | mode.acqsig = SIGUSR2; |
David Herrmann | 7172d9e | 2011-12-02 16:16:40 +0100 | [diff] [blame] | 152 | if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) { |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 153 | fprintf(stderr, "failed to take control of vt handling\n"); |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | tty->leave_vt_source = |
| 158 | wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, tty); |
| 159 | tty->enter_vt_source = |
| 160 | wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, tty); |
| 161 | |
| 162 | return tty; |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | tty_destroy(struct tty *tty) |
| 167 | { |
Tim Wiederhake | 70af98c | 2011-01-25 12:01:00 +0100 | [diff] [blame] | 168 | if(!tty) |
| 169 | return; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 170 | |
Tim Wiederhake | 70af98c | 2011-01-25 12:01:00 +0100 | [diff] [blame] | 171 | if (ioctl(tty->fd, KDSETMODE, KD_TEXT)) |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 172 | fprintf(stderr, |
Tim Wiederhake | 70af98c | 2011-01-25 12:01:00 +0100 | [diff] [blame] | 173 | "failed to set KD_TEXT mode on tty: %m\n"); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 174 | |
| 175 | if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0) |
| 176 | fprintf(stderr, |
| 177 | "could not restore terminal to canonical mode\n"); |
| 178 | |
Kristian Høgsberg | 00513ab | 2012-01-15 15:20:53 -0500 | [diff] [blame] | 179 | close(tty->fd); |
| 180 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 181 | free(tty); |
| 182 | } |