tty: Switch back to launch tty if we switched away
[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         int vt, starting_vt, has_vt;
47 };
48
49 static int on_enter_vt(int signal_number, void *data)
50 {
51         struct tty *tty = data;
52
53         ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
54
55         tty->vt_func(tty->compositor, TTY_ENTER_VT);
56         tty->has_vt = 1;
57
58         return 1;
59 }
60
61 static int
62 on_leave_vt(int signal_number, void *data)
63 {
64         struct tty *tty = data;
65
66         tty->vt_func(tty->compositor, TTY_LEAVE_VT);
67         tty->has_vt = 0;
68
69         ioctl(tty->fd, VT_RELDISP, 1);
70
71         return 1;
72 }
73
74 static int
75 on_tty_input(int fd, uint32_t mask, void *data)
76 {
77         struct tty *tty = data;
78
79         /* Ignore input to tty.  We get keyboard events from evdev
80          */
81         tcflush(tty->fd, TCIFLUSH);
82
83         return 1;
84 }
85
86 static int
87 try_open_vt(struct tty *tty)
88 {
89         int tty0, fd;
90         char filename[16];
91         struct vt_stat vts;
92
93         tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
94         if (tty0 < 0) {
95                 fprintf(stderr, "could not open tty0: %m\n");
96                 return -1;
97         }
98
99         if (ioctl(tty0, VT_OPENQRY, &tty->vt) < 0 || tty->vt == -1) {
100                 fprintf(stderr, "could not open tty0: %m\n");
101                 close(tty0);
102                 return -1;
103         }
104
105         close(tty0);
106         snprintf(filename, sizeof filename, "/dev/tty%d", tty->vt);
107         fprintf(stderr, "compositor: using new vt %s\n", filename);
108         fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
109         if (fd < 0)
110                 return fd;
111
112         if (ioctl(fd, VT_GETSTATE, &vts) == 0)
113                 tty->starting_vt = vts.v_active;
114         else
115                 tty->starting_vt = tty->vt;
116
117         if (ioctl(fd, VT_ACTIVATE, tty->vt) < 0 ||
118             ioctl(fd, VT_WAITACTIVE, tty->vt) < 0) {
119                 fprintf(stderr, "failed to swtich to new vt\n");
120                 close(fd);
121                 return -1;
122         }
123
124         return fd;
125 }
126
127 struct tty *
128 tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func,
129            int tty_nr)
130 {
131         struct termios raw_attributes;
132         struct vt_mode mode = { 0 };
133         int ret;
134         struct tty *tty;
135         struct wl_event_loop *loop;
136         struct stat buf;
137         char filename[16];
138
139         tty = malloc(sizeof *tty);
140         if (tty == NULL)
141                 return NULL;
142
143         memset(tty, 0, sizeof *tty);
144         tty->compositor = compositor;
145         tty->vt_func = vt_func;
146         if (tty_nr > 0) {
147                 snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
148                 fprintf(stderr, "compositor: using %s\n", filename);
149                 tty->fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
150         } else if (fstat(tty->fd, &buf) == 0 &&
151                    major(buf.st_rdev) == TTY_MAJOR &&
152                    minor(buf.st_rdev) > 0) {
153                 tty->fd = fcntl(0, F_DUPFD_CLOEXEC, 0);
154         } else {
155                 /* Fall back to try opening a new VT.  This typically
156                  * requires root. */
157                 tty->fd = try_open_vt(tty);
158         }
159
160         if (tty->fd <= 0) {
161                 fprintf(stderr, "failed to open tty: %m\n");
162                 return NULL;
163         }
164
165         if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
166                 fprintf(stderr, "could not get terminal attributes: %m\n");
167                 return NULL;
168         }
169
170         /* Ignore control characters and disable echo */
171         raw_attributes = tty->terminal_attributes;
172         cfmakeraw(&raw_attributes);
173
174         /* Fix up line endings to be normal (cfmakeraw hoses them) */
175         raw_attributes.c_oflag |= OPOST | OCRNL;
176
177         if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
178                 fprintf(stderr, "could not put terminal into raw mode: %m\n");
179
180         loop = wl_display_get_event_loop(compositor->wl_display);
181         tty->input_source =
182                 wl_event_loop_add_fd(loop, tty->fd,
183                                      WL_EVENT_READABLE, on_tty_input, tty);
184
185         ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
186         if (ret) {
187                 fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n");
188                 return NULL;
189         }
190
191         tty->has_vt = 1;
192         mode.mode = VT_PROCESS;
193         mode.relsig = SIGUSR1;
194         mode.acqsig = SIGUSR2;
195         if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
196                 fprintf(stderr, "failed to take control of vt handling\n");
197                 return NULL;
198         }
199
200         tty->leave_vt_source =
201                 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, tty);
202         tty->enter_vt_source =
203                 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, tty);
204
205         return tty;
206 }
207
208 void
209 tty_destroy(struct tty *tty)
210 {
211         struct vt_mode mode = { 0 };
212
213         if(!tty)
214                 return;
215
216         if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
217                 fprintf(stderr,
218                         "failed to set KD_TEXT mode on tty: %m\n");
219
220         if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
221                 fprintf(stderr,
222                         "could not restore terminal to canonical mode\n");
223
224         mode.mode = VT_AUTO;
225         if (ioctl(tty->fd, VT_SETMODE, &mode) < 0)
226                 fprintf(stderr, "could not reset vt handling\n");
227
228         if (tty->has_vt && tty->vt != tty->starting_vt) {
229                 ioctl(tty->fd, VT_ACTIVATE, tty->starting_vt);
230                 ioctl(tty->fd, VT_WAITACTIVE, tty->starting_vt);
231         }
232
233         close(tty->fd);
234
235         free(tty);
236 }