text: Split text and input-method protocols
[profile/ivi/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 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include "compositor.h"
38
39 /* Introduced in 2.6.38 */
40 #ifndef K_OFF
41 #define K_OFF 0x04
42 #endif
43
44 struct tty {
45         struct weston_compositor *compositor;
46         int fd;
47         struct termios terminal_attributes;
48
49         struct wl_event_source *input_source;
50         struct wl_event_source *vt_source;
51         tty_vt_func_t vt_func;
52         int vt, starting_vt, has_vt;
53         int kb_mode;
54 };
55
56 static int vt_handler(int signal_number, void *data)
57 {
58         struct tty *tty = data;
59
60         if (tty->has_vt) {
61                 tty->vt_func(tty->compositor, TTY_LEAVE_VT);
62                 tty->has_vt = 0;
63
64                 ioctl(tty->fd, VT_RELDISP, 1);
65         } else {
66                 ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
67
68                 tty->vt_func(tty->compositor, TTY_ENTER_VT);
69                 tty->has_vt = 1;
70         }
71
72         return 1;
73 }
74
75 static int
76 on_tty_input(int fd, uint32_t mask, void *data)
77 {
78         struct tty *tty = data;
79
80         /* Ignore input to tty.  We get keyboard events from evdev */
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
92         tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
93         if (tty0 < 0) {
94                 weston_log("could not open tty0: %m\n");
95                 return -1;
96         }
97
98         if (ioctl(tty0, VT_OPENQRY, &tty->vt) < 0 || tty->vt == -1) {
99                 weston_log("could not open tty0: %m\n");
100                 close(tty0);
101                 return -1;
102         }
103
104         close(tty0);
105         snprintf(filename, sizeof filename, "/dev/tty%d", tty->vt);
106         weston_log("compositor: using new vt %s\n", filename);
107         fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
108         if (fd < 0)
109                 return fd;
110
111         return fd;
112 }
113
114 int
115 tty_activate_vt(struct tty *tty, int vt)
116 {
117         return ioctl(tty->fd, VT_ACTIVATE, vt);
118 }
119
120 struct tty *
121 tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func,
122            int tty_nr)
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;
129         struct stat buf;
130         char filename[16];
131         struct vt_stat vts;
132
133         tty = malloc(sizeof *tty);
134         if (tty == NULL)
135                 return NULL;
136
137         memset(tty, 0, sizeof *tty);
138         tty->compositor = compositor;
139         tty->vt_func = vt_func;
140
141         tty->fd = weston_environment_get_fd("WESTON_TTY_FD");
142         if (tty->fd < 0)
143                 tty->fd = STDIN_FILENO;
144
145         if (tty_nr > 0) {
146                 snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
147                 weston_log("compositor: using %s\n", filename);
148                 tty->fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
149                 tty->vt = tty_nr;
150         } else if (fstat(tty->fd, &buf) == 0 &&
151                    major(buf.st_rdev) == TTY_MAJOR &&
152                    minor(buf.st_rdev) > 0) {
153                 if (tty->fd == STDIN_FILENO)
154                         tty->fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
155                 tty->vt = minor(buf.st_rdev);
156         } else {
157                 /* Fall back to try opening a new VT.  This typically
158                  * requires root. */
159                 tty->fd = try_open_vt(tty);
160         }
161
162         if (tty->fd <= 0) {
163                 weston_log("failed to open tty: %m\n");
164                 free(tty);
165                 return NULL;
166         }
167
168         if (ioctl(tty->fd, VT_GETSTATE, &vts) == 0)
169                 tty->starting_vt = vts.v_active;
170         else
171                 tty->starting_vt = tty->vt;
172
173         if (tty->starting_vt != tty->vt) {
174                 if (ioctl(tty->fd, VT_ACTIVATE, tty->vt) < 0 ||
175                     ioctl(tty->fd, VT_WAITACTIVE, tty->vt) < 0) {
176                         weston_log("failed to swtich to new vt\n");
177                         return NULL;
178                 }
179         }
180
181         if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
182                 weston_log("could not get terminal attributes: %m\n");
183                 goto err;
184         }
185
186         /* Ignore control characters and disable echo */
187         raw_attributes = tty->terminal_attributes;
188         cfmakeraw(&raw_attributes);
189
190         /* Fix up line endings to be normal (cfmakeraw hoses them) */
191         raw_attributes.c_oflag |= OPOST | OCRNL;
192
193         if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
194                 weston_log("could not put terminal into raw mode: %m\n");
195
196         loop = wl_display_get_event_loop(compositor->wl_display);
197
198         ioctl(tty->fd, KDGKBMODE, &tty->kb_mode);
199         ret = ioctl(tty->fd, KDSKBMODE, K_OFF);
200         if (ret) {
201                 ret = ioctl(tty->fd, KDSKBMODE, K_RAW);
202                 if (ret)
203                         goto err_attr;
204
205                 tty->input_source = wl_event_loop_add_fd(loop, tty->fd,
206                                                          WL_EVENT_READABLE,
207                                                          on_tty_input, tty);
208                 if (!tty->input_source)
209                         goto err_kdkbmode;
210         }
211
212         if (ret) {
213                 weston_log("failed to set K_OFF keyboard mode on tty: %m\n");
214                 goto err_attr;
215         }
216
217         ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
218         if (ret) {
219                 weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
220                 goto err_kdkbmode;
221         }
222
223         tty->has_vt = 1;
224         mode.mode = VT_PROCESS;
225         mode.relsig = SIGUSR1;
226         mode.acqsig = SIGUSR1;
227         if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
228                 weston_log("failed to take control of vt handling\n");
229                 goto err_kdmode;
230         }
231
232         tty->vt_source =
233                 wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, tty);
234         if (!tty->vt_source)
235                 goto err_vtmode;
236
237         return tty;
238
239 err_vtmode:
240         ioctl(tty->fd, VT_SETMODE, &mode);
241
242 err_kdmode:
243         ioctl(tty->fd, KDSETMODE, KD_TEXT);
244
245 err_kdkbmode:
246         if (tty->input_source)
247                 wl_event_source_remove(tty->input_source);
248         ioctl(tty->fd, KDSKBMODE, tty->kb_mode);
249
250 err_attr:
251         tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes);
252
253 err:
254         close(tty->fd);
255         free(tty);
256         return NULL;
257 }
258
259 void tty_reset(struct tty *tty)
260 {
261         struct vt_mode mode = { 0 };
262
263         if (ioctl(tty->fd, KDSKBMODE, tty->kb_mode))
264                 weston_log("failed to restore keyboard mode: %m\n");
265
266         if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
267                 weston_log("failed to set KD_TEXT mode on tty: %m\n");
268
269         if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
270                 weston_log("could not restore terminal to canonical mode\n");
271
272         mode.mode = VT_AUTO;
273         if (ioctl(tty->fd, VT_SETMODE, &mode) < 0)
274                 weston_log("could not reset vt handling\n");
275
276         if (tty->has_vt && tty->vt != tty->starting_vt) {
277                 ioctl(tty->fd, VT_ACTIVATE, tty->starting_vt);
278                 ioctl(tty->fd, VT_WAITACTIVE, tty->starting_vt);
279         }
280 }
281
282 void
283 tty_destroy(struct tty *tty)
284 {
285         if (tty->input_source)
286                 wl_event_source_remove(tty->input_source);
287
288         wl_event_source_remove(tty->vt_source);
289
290         tty_reset(tty);
291
292         close(tty->fd);
293
294         free(tty);
295 }