blob: 9e555502766771086ba31d914f15a49708cc2bd3 [file] [log] [blame]
Kristian Høgsberge4762a62011-01-14 14:59:13 -05001/*
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 <sys/ioctl.h>
33
34#include "compositor.h"
35
36struct tty {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050037 struct weston_compositor *compositor;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050038 int fd;
39 struct termios terminal_attributes;
40
41 struct wl_event_source *input_source;
42 struct wl_event_source *enter_vt_source;
43 struct wl_event_source *leave_vt_source;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -040044 tty_vt_func_t vt_func;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050045};
46
Kristian Høgsbergb1868472011-04-22 12:27:57 -040047static int on_enter_vt(int signal_number, void *data)
Kristian Høgsberge4762a62011-01-14 14:59:13 -050048{
49 struct tty *tty = data;
50 int ret;
51
Kristian Høgsberg9396fc52011-05-06 15:15:37 -040052 tty->vt_func(tty->compositor, TTY_ENTER_VT);
Kristian Høgsberge4762a62011-01-14 14:59:13 -050053
54 ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
55 ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
56 if (ret)
57 fprintf(stderr, "failed to set KD_GRAPHICS mode on console: %m\n");
Kristian Høgsbergb1868472011-04-22 12:27:57 -040058
59 return 1;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050060}
61
Kristian Høgsbergb1868472011-04-22 12:27:57 -040062static int
63on_leave_vt(int signal_number, void *data)
Kristian Høgsberge4762a62011-01-14 14:59:13 -050064{
65 struct tty *tty = data;
66 int ret;
67
68 ioctl (tty->fd, VT_RELDISP, 1);
69 ret = ioctl(tty->fd, KDSETMODE, KD_TEXT);
70 if (ret)
71 fprintf(stderr,
72 "failed to set KD_TEXT mode on console: %m\n");
Kristian Høgsbergb1868472011-04-22 12:27:57 -040073
Kristian Høgsberg9396fc52011-05-06 15:15:37 -040074 tty->vt_func(tty->compositor, TTY_LEAVE_VT);
75
Kristian Høgsbergb1868472011-04-22 12:27:57 -040076 return 1;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050077}
78
Kristian Høgsbergb1868472011-04-22 12:27:57 -040079static int
Kristian Høgsberge4762a62011-01-14 14:59:13 -050080on_tty_input(int fd, uint32_t mask, void *data)
81{
82 struct tty *tty = data;
83
84 /* Ignore input to tty. We get keyboard events from evdev
85 */
86 tcflush(tty->fd, TCIFLUSH);
Kristian Høgsbergb1868472011-04-22 12:27:57 -040087
88 return 1;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050089}
90
91struct tty *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050092tty_create(struct weston_compositor *compositor, tty_vt_func_t vt_func,
Tiago Vignattifaee8012011-09-01 15:58:17 -040093 int tty_nr)
Kristian Høgsberge4762a62011-01-14 14:59:13 -050094{
95 struct termios raw_attributes;
96 struct vt_mode mode = { 0 };
97 int ret;
98 struct tty *tty;
99 struct wl_event_loop *loop;
Tiago Vignattifaee8012011-09-01 15:58:17 -0400100 char filename[16];
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500101
102 tty = malloc(sizeof *tty);
103 if (tty == NULL)
104 return NULL;
105
Tiago Vignatti280b8a62011-11-21 14:55:23 +0200106 snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
Tiago Vignattifaee8012011-09-01 15:58:17 -0400107 fprintf(stderr, "compositor: using %s\n", filename);
108
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500109 memset(tty, 0, sizeof *tty);
110 tty->compositor = compositor;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -0400111 tty->vt_func = vt_func;
Tiago Vignattifaee8012011-09-01 15:58:17 -0400112 tty->fd = open(filename, O_RDWR | O_NOCTTY);
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500113 if (tty->fd <= 0) {
114 fprintf(stderr, "failed to open active tty: %m\n");
115 return NULL;
116 }
117
118 if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
119 fprintf(stderr, "could not get terminal attributes: %m\n");
120 return NULL;
121 }
122
123 /* Ignore control characters and disable echo */
124 raw_attributes = tty->terminal_attributes;
125 cfmakeraw(&raw_attributes);
126
127 /* Fix up line endings to be normal (cfmakeraw hoses them) */
128 raw_attributes.c_oflag |= OPOST | OCRNL;
129
130 if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
131 fprintf(stderr, "could not put terminal into raw mode: %m\n");
132
133 loop = wl_display_get_event_loop(compositor->wl_display);
134 tty->input_source =
135 wl_event_loop_add_fd(loop, tty->fd,
136 WL_EVENT_READABLE, on_tty_input, tty);
137
138 ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
139 if (ret) {
140 fprintf(stderr, "failed to set KD_GRAPHICS mode on tty: %m\n");
141 return NULL;
142 }
143
144 tty->compositor->focus = 1;
145 mode.mode = VT_PROCESS;
146 mode.relsig = SIGUSR1;
147 mode.acqsig = SIGUSR2;
David Herrmann7172d9e2011-12-02 16:16:40 +0100148 if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500149 fprintf(stderr, "failed to take control of vt handling\n");
150 return NULL;
151 }
152
153 tty->leave_vt_source =
154 wl_event_loop_add_signal(loop, SIGUSR1, on_leave_vt, tty);
155 tty->enter_vt_source =
156 wl_event_loop_add_signal(loop, SIGUSR2, on_enter_vt, tty);
157
158 return tty;
159}
160
161void
162tty_destroy(struct tty *tty)
163{
Tim Wiederhake70af98c2011-01-25 12:01:00 +0100164 if(!tty)
165 return;
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500166
Tim Wiederhake70af98c2011-01-25 12:01:00 +0100167 if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500168 fprintf(stderr,
Tim Wiederhake70af98c2011-01-25 12:01:00 +0100169 "failed to set KD_TEXT mode on tty: %m\n");
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500170
171 if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
172 fprintf(stderr,
173 "could not restore terminal to canonical mode\n");
174
175 free(tty);
176}