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