tty: If no tty option is given, use stdin and make sure it's a vt
[platform/upstream/weston.git] / src / tty.c
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>
32 #include <linux/major.h>
33 #include <sys/ioctl.h>
34
35 #include "compositor.h"
36
37 struct tty {
38         struct weston_compositor *compositor;
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;
45         tty_vt_func_t vt_func;
46 };
47
48 static int on_enter_vt(int signal_number, void *data)
49 {
50         struct tty *tty = data;
51         int ret;
52
53         tty->vt_func(tty->compositor, TTY_ENTER_VT);
54
55         ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
56         ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
57         if (ret)
58                 fprintf(stderr, "failed to set KD_GRAPHICS mode on console: %m\n");
59
60         return 1;
61 }
62
63 static int
64 on_leave_vt(int signal_number, void *data)
65 {
66         struct tty *tty = data;
67         int ret;
68
69         ioctl (tty->fd, VT_RELDISP, 1);
70         ret = ioctl(tty->fd, KDSETMODE, KD_TEXT);
71         if (ret)
72                 fprintf(stderr,
73                         "failed to set KD_TEXT mode on console: %m\n");
74
75         tty->vt_func(tty->compositor, TTY_LEAVE_VT);
76
77         return 1;
78 }
79
80 static int
81 on_tty_input(int fd, uint32_t mask, void *data)
82 {
83         struct tty *tty = data;
84
85         /* Ignore input to tty.  We get keyboard events from evdev
86          */
87         tcflush(tty->fd, TCIFLUSH);
88
89         return 1;
90 }
91
92 struct tty *
93 tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func,
94            int tty_nr)
95 {
96         struct termios raw_attributes;
97         struct vt_mode mode = { 0 };
98         int ret;
99         struct tty *tty;
100         struct wl_event_loop *loop;
101         struct stat buf;
102         char filename[16];
103
104         tty = malloc(sizeof *tty);
105         if (tty == NULL)
106                 return NULL;
107
108         memset(tty, 0, sizeof *tty);
109         tty->compositor = compositor;
110         tty->vt_func = vt_func;
111         if (tty_nr > 0) {
112                 snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
113                 fprintf(stderr, "compositor: using %s\n", filename);
114                 tty->fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
115         } else {
116                 tty->fd = fcntl(0, F_DUPFD_CLOEXEC, 0);
117         }
118
119         if (tty->fd <= 0) {
120                 fprintf(stderr, "failed to open tty: %m\n");
121                 return NULL;
122         }
123
124         if (fstat(tty->fd, &buf) < 0 ||
125             major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
126                 fprintf(stderr, "stdin not a vt (%d, %d)\n",
127                         major(buf.st_dev), minor(buf.st_dev));
128                 return NULL;
129         }
130
131         if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
132                 fprintf(stderr, "could not get terminal attributes: %m\n");
133                 return NULL;
134         }
135
136         /* Ignore control characters and disable echo */
137         raw_attributes = tty->terminal_attributes;
138         cfmakeraw(&raw_attributes);
139
140         /* Fix up line endings to be normal (cfmakeraw hoses them) */
141         raw_attributes.c_oflag |= OPOST | OCRNL;
142
143         if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
144                 fprintf(stderr, "could not put terminal into raw mode: %m\n");
145
146         loop = wl_display_get_event_loop(compositor->wl_display);
147         tty->input_source =
148                 wl_event_loop_add_fd(loop, tty->fd,
149                                      WL_EVENT_READABLE, on_tty_input, tty);
150
151         ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
152         if (ret) {
153                 fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n");
154                 return NULL;
155         }
156
157         tty->compositor->focus = 1;
158         mode.mode = VT_PROCESS;
159         mode.relsig = SIGUSR1;
160         mode.acqsig = SIGUSR2;
161         if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
162                 fprintf(stderr, "failed to take control of vt handling\n");
163                 return NULL;
164         }
165
166         tty->leave_vt_source =
167                 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, tty);
168         tty->enter_vt_source =
169                 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, tty);
170
171         return tty;
172 }
173
174 void
175 tty_destroy(struct tty *tty)
176 {
177         if(!tty)
178                 return;
179
180         if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
181                 fprintf(stderr,
182                         "failed to set KD_TEXT mode on tty: %m\n");
183
184         if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
185                 fprintf(stderr,
186                         "could not restore terminal to canonical mode\n");
187
188         free(tty);
189 }