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