1
2
3
4
5
6#include "toys.h"
7
8int tty_fd(void)
9{
10 int i, j;
11
12 for (i = 0; i<3; i++) if (isatty(j = (i+1)%3)) return j;
13
14 return notstdio(open("/dev/tty", O_RDWR));
15}
16
17
18
19
20
21int terminal_size(unsigned *xx, unsigned *yy)
22{
23 struct winsize ws;
24 unsigned i, x = 0, y = 0;
25 char *s;
26
27
28 for (i=0; i<3; i++) {
29 memset(&ws, 0, sizeof(ws));
30 if (isatty(i) && !ioctl(i, TIOCGWINSZ, &ws)) {
31 if (ws.ws_col) x = ws.ws_col;
32 if (ws.ws_row) y = ws.ws_row;
33
34 break;
35 }
36 }
37 s = getenv("COLUMNS");
38 if (s) sscanf(s, "%u", &x);
39 s = getenv("LINES");
40 if (s) sscanf(s, "%u", &y);
41
42
43 if (xx && x) *xx = x;
44 if (yy && y) *yy = y;
45
46 return x || y;
47}
48
49
50
51
52
53int terminal_probesize(unsigned *xx, unsigned *yy)
54{
55 if (terminal_size(xx, yy) && (!xx || *xx) && (!yy || *yy)) return 1;
56
57
58
59 xprintf("\033[s\033[999C\033[999B\033[6n\033[u");
60
61 return 0;
62}
63
64
65int set_terminal(int fd, int raw, int speed, struct termios *old)
66{
67 struct termios termio;
68 int i = tcgetattr(fd, &termio);
69
70
71 if (i) return i;
72 if (old) *old = termio;
73
74
75
76
77
78
79 termio.c_iflag = IXANY|ICRNL|INLCR;
80 if (toys.which->flags & TOYFLAG_LOCALE) termio.c_iflag |= IUTF8;
81
82
83 termio.c_oflag = ONLCR|OPOST;
84
85
86
87
88
89
90
91
92
93 termio.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
94
95 if (raw) cfmakeraw(&termio);
96
97 if (speed) {
98 int i, speeds[] = {50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
99 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800,
100 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
101 2500000, 3000000, 3500000, 4000000};
102
103
104 for (i = 0; i < ARRAY_LEN(speeds); i++) if (speeds[i] == speed) break;
105 if (i == ARRAY_LEN(speeds)) error_exit("unknown speed: %d", speed);
106 cfsetspeed(&termio, i+1+4081*(i>15));
107 }
108
109 return tcsetattr(fd, TCSAFLUSH, &termio);
110}
111
112void xset_terminal(int fd, int raw, int speed, struct termios *old)
113{
114 if (-1 != set_terminal(fd, raw, speed, old)) return;
115
116 sprintf(libbuf, "/proc/self/fd/%d", fd);
117 libbuf[readlink0(libbuf, libbuf, sizeof(libbuf))] = 0;
118 perror_exit("tcsetattr %s", libbuf);
119}
120
121struct scan_key_list {
122 int key;
123 char *seq;
124} static const scan_key_list[] = {
125 {KEY_UP, "\033[A"}, {KEY_DOWN, "\033[B"},
126 {KEY_RIGHT, "\033[C"}, {KEY_LEFT, "\033[D"},
127
128 {KEY_UP|KEY_SHIFT, "\033[1;2A"}, {KEY_DOWN|KEY_SHIFT, "\033[1;2B"},
129 {KEY_RIGHT|KEY_SHIFT, "\033[1;2C"}, {KEY_LEFT|KEY_SHIFT, "\033[1;2D"},
130
131 {KEY_UP|KEY_ALT, "\033[1;3A"}, {KEY_DOWN|KEY_ALT, "\033[1;3B"},
132 {KEY_RIGHT|KEY_ALT, "\033[1;3C"}, {KEY_LEFT|KEY_ALT, "\033[1;3D"},
133
134 {KEY_UP|KEY_CTRL, "\033[1;5A"}, {KEY_DOWN|KEY_CTRL, "\033[1;5B"},
135 {KEY_RIGHT|KEY_CTRL, "\033[1;5C"}, {KEY_LEFT|KEY_CTRL, "\033[1;5D"},
136
137
138 {KEY_HOME, "\033[1~"},
139 {KEY_INSERT, "\033[2~"},
140 {KEY_DELETE, "\033[3~"},
141 {KEY_END, "\033[4~"},
142 {KEY_PGUP, "\033[5~"},
143 {KEY_PGDN, "\033[6~"},
144
145 {KEY_HOME, "\033OH"},
146 {KEY_END, "\033OF"},
147
148 {KEY_HOME, "\033[H"},
149 {KEY_END, "\033[F"},
150
151 {KEY_FN+1, "\033OP"}, {KEY_FN+2, "\033OQ"}, {KEY_FN+3, "\033OR"},
152 {KEY_FN+4, "\033OS"}, {KEY_FN+5, "\033[15~"}, {KEY_FN+6, "\033[17~"},
153 {KEY_FN+7, "\033[18~"}, {KEY_FN+8, "\033[19~"}, {KEY_FN+9, "\033[20~"},
154};
155
156
157
158
159
160
161
162int scan_key_getsize(char *scratch, int timeout_ms, unsigned *xx, unsigned *yy)
163{
164 struct pollfd pfd;
165 int maybe, i, j;
166 char *test;
167
168 for (;;) {
169 pfd.fd = 0;
170 pfd.events = POLLIN;
171 pfd.revents = 0;
172
173 maybe = 0;
174 if (*scratch) {
175 int pos[6];
176 unsigned x, y;
177
178
179 memset(pos, 0, 6*sizeof(int));
180 scratch[(1+*scratch)&15] = 0;
181 sscanf(scratch+1, "\033%n[%n%3u%n;%n%3u%nR%n", pos, pos+1, &y,
182 pos+2, pos+3, &x, pos+4, pos+5);
183 if (pos[5]) {
184
185 *scratch = 0;
186 if (xx) *xx = x;
187 if (yy) *yy = y;
188 return -3;
189 } else for (i=0; i<6; i++) if (pos[i]==*scratch) maybe = 1;
190
191
192 for (i = 0; i<ARRAY_LEN(scan_key_list); i++) {
193 test = scan_key_list[i].seq;
194 for (j = 0; j<*scratch; j++) if (scratch[j+1] != test[j]) break;
195 if (j == *scratch) {
196 maybe = 1;
197 if (!test[j]) {
198
199 *scratch = 0;
200 return 256+scan_key_list[i].key;
201 }
202 }
203 }
204
205
206 if (!maybe) break;
207 }
208
209
210
211
212 if (maybe || timeout_ms != -1)
213 if (!xpoll(&pfd, 1, maybe ? 30 : timeout_ms)) break;
214
215
216
217 if (toys.signal>0 || 1 != read(0, scratch+1+*scratch, 1))
218 return (toys.signal>0) ? -3 : -1;
219 ++*scratch;
220 }
221
222
223 if (!*scratch) return -2;
224 i = scratch[1];
225 if (--*scratch) memmove(scratch+1, scratch+2, *scratch);
226
227 return i;
228}
229
230
231
232int scan_key(char *scratch, int timeout_ms)
233{
234 return scan_key_getsize(scratch, timeout_ms, NULL, NULL);
235}
236
237void tty_esc(char *s)
238{
239 printf("\033[%s", s);
240}
241
242void tty_jump(int x, int y)
243{
244 char s[32];
245
246 sprintf(s, "%d;%dH", y+1, x+1);
247 tty_esc(s);
248}
249
250void tty_reset(void)
251{
252 set_terminal(0, 0, 0, 0);
253 tty_esc("?25h");
254 tty_esc("0m");
255 tty_jump(0, 999);
256 tty_esc("K");
257 fflush(0);
258}
259
260
261void tty_sigreset(int i)
262{
263 tty_reset();
264 _exit(i ? 128+i : 0);
265}
266
267void start_redraw(unsigned *width, unsigned *height)
268{
269
270 if (!toys.signal) {
271 *width = 80;
272 *height = 25;
273 set_terminal(0, 1, 0, 0);
274 sigatexit(tty_sigreset);
275 xsignal(SIGWINCH, generic_signal);
276 }
277 if (toys.signal != -1) {
278 toys.signal = -1;
279 terminal_probesize(width, height);
280 }
281 xprintf("\033[H\033[J");
282}
283