blob: 90ef91c5c81ca6a3fce0b4fa6fdfd1567dbc1d51 [file] [log] [blame]
Pascal Bellarda8b594f2010-06-21 02:17:29 +02001/* vi: set sw=4 ts=4: */
2/*
3 * A text-mode VNC like program for Linux virtual terminals.
4 *
5 * pascal.bellard@ads-lu.com
6 *
7 * Based on Russell Stuart's conspy.c
8 * http://ace-host.stuart.id.au/russell/files/conspy.c
9 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Pascal Bellarda8b594f2010-06-21 02:17:29 +020011 */
12
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +010013//applet:IF_CONSPY(APPLET(conspy, BB_DIR_BIN, BB_SUID_DROP))
Pascal Bellarda8b594f2010-06-21 02:17:29 +020014
15//kbuild:lib-$(CONFIG_CONSPY) += conspy.o
16
17//config:config CONSPY
18//config: bool "conspy"
19//config: default n
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010020//config: select PLATFORM_LINUX
Pascal Bellarda8b594f2010-06-21 02:17:29 +020021//config: help
22//config: A text-mode VNC like program for Linux virtual terminals.
Denys Vlasenko1a3b0712010-06-27 20:42:17 +020023//config: example: conspy NUM shared access to console num
24//config: or conspy -nd NUM screenshot of console num
25//config: or conspy -cs NUM poor man's GNU screen like
Pascal Bellarda8b594f2010-06-21 02:17:29 +020026
27//usage:#define conspy_trivial_usage
Denys Vlasenko1a3b0712010-06-27 20:42:17 +020028//usage: "[-vcsndf] [-x COL] [-y LINE] [CONSOLE_NO]"
Pascal Bellarda8b594f2010-06-21 02:17:29 +020029//usage:#define conspy_full_usage "\n\n"
30//usage: "A text-mode VNC like program for Linux virtual consoles."
31//usage: "\nTo exit, quickly press ESC 3 times."
32//usage: "\n"
33//usage: "\nOptions:"
34//usage: "\n -v Don't send keystrokes to the console"
35//usage: "\n -c Create missing devices in /dev"
36//usage: "\n -s Open a SHELL session"
37//usage: "\n -n Black & white"
38//usage: "\n -d Dump console to stdout"
39//usage: "\n -f Follow cursor"
Denys Vlasenko1a3b0712010-06-27 20:42:17 +020040//usage: "\n -x COL Starting column"
Pascal Bellarda8b594f2010-06-21 02:17:29 +020041//usage: "\n -y LINE Starting line"
42
43#include "libbb.h"
44#include <sys/kd.h>
45
Marek Polacek7b181072010-10-28 21:34:56 +020046
47#define ESC "\033"
48
Pascal Bellarda8b594f2010-06-21 02:17:29 +020049struct screen_info {
Denys Vlasenko1a3b0712010-06-27 20:42:17 +020050 unsigned char lines, cols, cursor_x, cursor_y;
Pascal Bellarda8b594f2010-06-21 02:17:29 +020051};
52
Pascal Bellardfa5ea172010-06-30 07:05:31 +020053#define CHAR(x) (*(uint8_t*)(x))
54#define ATTR(x) (((uint8_t*)(x))[1])
55#define NEXT(x) ((x) += 2)
Denys Vlasenko51fa1472010-06-25 00:57:57 +020056#define DATA(x) (*(uint16_t*)(x))
Pascal Bellarda8b594f2010-06-21 02:17:29 +020057
58struct globals {
59 char* data;
60 int size;
61 int x, y;
62 int kbd_fd;
Pascal Bellard1b8e2b02010-06-23 20:25:00 +020063 int ioerror_count;
64 int key_count;
65 int escape_count;
66 int nokeys;
67 int current;
Pascal Bellardfa5ea172010-06-30 07:05:31 +020068 int first_line_offset;
69 int last_attr;
Pascal Bellardff377992010-06-28 15:50:22 +020070 // cached local tty parameters
71 unsigned width;
72 unsigned height;
73 unsigned col;
74 unsigned line;
Pascal Bellardfa5ea172010-06-30 07:05:31 +020075 smallint curoff; // unknown:0 cursor on:-1 cursor off:1
Marek Polacek7b181072010-10-28 21:34:56 +020076 char attrbuf[sizeof(ESC"[0;1;5;30;40m")];
Pascal Bellardff377992010-06-28 15:50:22 +020077 // remote console
Denys Vlasenko1a3b0712010-06-27 20:42:17 +020078 struct screen_info remote;
Pascal Bellardff377992010-06-28 15:50:22 +020079 // saved local tty terminfo
Pascal Bellarda8b594f2010-06-21 02:17:29 +020080 struct termios term_orig;
Pascal Bellardfa5ea172010-06-30 07:05:31 +020081 char vcsa_name[sizeof("/dev/vcsaNN")];
Pascal Bellarda8b594f2010-06-21 02:17:29 +020082};
83
84#define G (*ptr_to_globals)
85#define INIT_G() do { \
86 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Pascal Bellardff377992010-06-28 15:50:22 +020087 G.attrbuf[0] = '\033'; \
88 G.attrbuf[1] = '['; \
Pascal Bellardfa5ea172010-06-30 07:05:31 +020089 G.width = G.height = UINT_MAX; \
90 G.last_attr--; \
Pascal Bellarda8b594f2010-06-21 02:17:29 +020091} while (0)
92
Denys Vlasenko05a550b2010-06-21 04:00:16 +020093enum {
94 FLAG_v, // view only
95 FLAG_c, // create device if need
96 FLAG_s, // session
97 FLAG_n, // no colors
98 FLAG_d, // dump screen
99 FLAG_f, // follow cursor
100};
101#define FLAG(x) (1 << FLAG_##x)
102#define BW (option_mask32 & FLAG(n))
103
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200104static void clrscr(void)
105{
106 // Home, clear till end of screen
Marek Polacek7b181072010-10-28 21:34:56 +0200107 fputs(ESC"[1;1H" ESC"[J", stdout);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200108 G.col = G.line = 0;
109}
110
111static void set_cursor(int state)
112{
113 if (G.curoff != state) {
114 G.curoff = state;
Marek Polacek7b181072010-10-28 21:34:56 +0200115 fputs(ESC"[?25", stdout);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200116 bb_putchar("h?l"[1 + state]);
117 }
118}
119
120static void gotoxy(int col, int line)
121{
122 if (G.col != col || G.line != line) {
123 G.col = col;
124 G.line = line;
Marek Polacek7b181072010-10-28 21:34:56 +0200125 printf(ESC"[%u;%uH", line + 1, col + 1);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200126 }
127}
128
129static void cleanup(int code)
130{
131 set_cursor(-1); // cursor on
132 tcsetattr(G.kbd_fd, TCSANOW, &G.term_orig);
133 if (ENABLE_FEATURE_CLEAN_UP) {
134 close(G.kbd_fd);
135 }
136 // Reset attributes
137 if (!BW)
Marek Polacek7b181072010-10-28 21:34:56 +0200138 fputs(ESC"[0m", stdout);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200139 bb_putchar('\n');
140 if (code > 1)
141 kill_myself_with_sig(code);
142 exit(code);
143}
144
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200145static void screen_read_close(void)
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200146{
147 unsigned i, j;
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200148 int vcsa_fd;
149 char *data;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200150
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200151 // Close & re-open vcsa in case they have swapped virtual consoles
152 vcsa_fd = xopen(G.vcsa_name, O_RDONLY);
153 xread(vcsa_fd, &G.remote, 4);
154 i = G.remote.cols * 2;
155 G.first_line_offset = G.y * i;
156 i *= G.remote.lines;
157 if (G.data == NULL) {
158 G.size = i;
159 G.data = xzalloc(2 * i);
160 }
161 else if (G.size != i) {
162 cleanup(1);
163 }
164 data = G.data + G.current;
165 xread(vcsa_fd, data, G.size);
166 close(vcsa_fd);
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200167 for (i = 0; i < G.remote.lines; i++) {
168 for (j = 0; j < G.remote.cols; j++, NEXT(data)) {
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200169 unsigned x = j - G.x; // if will catch j < G.x too
170 unsigned y = i - G.y; // if will catch i < G.y too
171
172 if (CHAR(data) < ' ')
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200173 CHAR(data) = ' ';
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200174 if (y >= G.height || x >= G.width)
175 DATA(data) = 0;
176 }
177 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200178}
179
180static void screen_char(char *data)
181{
Pascal Bellardff377992010-06-28 15:50:22 +0200182 if (!BW) {
183 uint8_t attr = ATTR(data);
184 //uint8_t attr = ATTR(data) >> 1; // for framebuffer console
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200185 uint8_t attr_diff = G.last_attr ^ attr;
Denys Vlasenkoa54985b2010-06-24 17:50:00 +0200186
Pascal Bellardff377992010-06-28 15:50:22 +0200187 if (attr_diff) {
Denys Vlasenkoa54985b2010-06-24 17:50:00 +0200188// Attribute layout for VGA compatible text videobuffer:
189// blinking text
190// |red bkgd
191// ||green bkgd
192// |||blue bkgd
193// vvvv
194// 00000000 <- lsb bit on the right
195// bold text / text 8th bit
196// red text
197// green text
198// blue text
199// TODO: apparently framebuffer-based console uses different layout
200// (bug? attempt to get 8th text bit in better position?)
201// red bkgd
202// |green bkgd
203// ||blue bkgd
204// vvv
205// 00000000 <- lsb bit on the right
206// bold text
207// red text
208// green text
209// blue text
210// text 8th bit
Pascal Bellardff377992010-06-28 15:50:22 +0200211 // converting RGB color bit triad to BGR:
212 static const char color[8] = "04261537";
213 const uint8_t fg_mask = 0x07, bold_mask = 0x08;
214 const uint8_t bg_mask = 0x70, blink_mask = 0x80;
215 char *ptr;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200216
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200217 ptr = G.attrbuf + 2; // skip "ESC ["
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200218
Pascal Bellardff377992010-06-28 15:50:22 +0200219 // (G.last_attr & ~attr) has 1 only where
220 // G.last_attr has 1 but attr has 0.
221 // Here we check whether we have transition
222 // bold->non-bold or blink->non-blink:
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200223 if (G.last_attr < 0 // initial value
224 || ((G.last_attr & ~attr) & (bold_mask | blink_mask)) != 0
225 ) {
Pascal Bellardff377992010-06-28 15:50:22 +0200226 *ptr++ = '0'; // "reset all attrs"
227 *ptr++ = ';';
228 // must set fg & bg, maybe need to set bold or blink:
229 attr_diff = attr | ~(bold_mask | blink_mask);
230 }
Pascal Bellardff377992010-06-28 15:50:22 +0200231 G.last_attr = attr;
232 if (attr_diff & bold_mask) {
233 *ptr++ = '1';
234 *ptr++ = ';';
235 }
236 if (attr_diff & blink_mask) {
237 *ptr++ = '5';
238 *ptr++ = ';';
239 }
240 if (attr_diff & fg_mask) {
241 *ptr++ = '3';
242 *ptr++ = color[attr & fg_mask];
243 *ptr++ = ';';
244 }
245 if (attr_diff & bg_mask) {
246 *ptr++ = '4';
247 *ptr++ = color[(attr & bg_mask) >> 4];
248 *ptr++ = ';';
249 }
250 if (ptr != G.attrbuf + 2) {
251 ptr[-1] = 'm';
252 *ptr = '\0';
253 fputs(G.attrbuf, stdout);
254 }
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200255 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200256 }
Denys Vlasenkoa54985b2010-06-24 17:50:00 +0200257 putchar(CHAR(data));
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200258 G.col++;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200259}
260
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200261static void screen_dump(void)
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200262{
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200263 int linefeed_cnt;
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200264 int line, col;
265 int linecnt = G.remote.lines - G.y;
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200266 char *data = G.data + G.current + G.first_line_offset;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200267
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200268 linefeed_cnt = 0;
269 for (line = 0; line < linecnt && line < G.height; line++) {
270 int space_cnt = 0;
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200271 for (col = 0; col < G.remote.cols; col++, NEXT(data)) {
272 unsigned tty_col = col - G.x; // if will catch col < G.x too
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200273
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200274 if (tty_col >= G.width)
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200275 continue;
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200276 space_cnt++;
Denys Vlasenko51fa1472010-06-25 00:57:57 +0200277 if (BW && CHAR(data) == ' ')
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200278 continue;
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200279 while (linefeed_cnt != 0) {
Denys Vlasenko51fa1472010-06-25 00:57:57 +0200280 //bb_putchar('\r'); - tty driver does it for us
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200281 bb_putchar('\n');
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200282 linefeed_cnt--;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200283 }
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200284 while (--space_cnt)
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200285 bb_putchar(' ');
286 screen_char(data);
287 }
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200288 linefeed_cnt++;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200289 }
290}
291
292static void curmove(void)
293{
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200294 unsigned cx = G.remote.cursor_x - G.x;
295 unsigned cy = G.remote.cursor_y - G.y;
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200296 int cursor = 1;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200297
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200298 if (cx < G.width && cy < G.height) {
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200299 gotoxy(cx, cy);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200300 cursor = -1;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200301 }
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200302 set_cursor(cursor);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200303}
304
305static void create_cdev_if_doesnt_exist(const char* name, dev_t dev)
306{
307 int fd = open(name, O_RDONLY);
308 if (fd != -1)
309 close(fd);
310 else if (errno == ENOENT)
311 mknod(name, S_IFCHR | 0660, dev);
312}
313
314static NOINLINE void start_shell_in_child(const char* tty_name)
315{
Pascal Bellard926031b2010-07-04 15:32:38 +0200316 int pid = xvfork();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200317 if (pid == 0) {
318 struct termios termchild;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100319 const char *shell = get_shell_name();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200320
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200321 signal(SIGHUP, SIG_IGN);
322 // set tty as a controlling tty
323 setsid();
324 // make tty to be input, output, error
325 close(0);
326 xopen(tty_name, O_RDWR); // uses fd 0
327 xdup2(0, 1);
328 xdup2(0, 2);
329 ioctl(0, TIOCSCTTY, 1);
330 tcsetpgrp(0, getpid());
331 tcgetattr(0, &termchild);
332 termchild.c_lflag |= ECHO;
333 termchild.c_oflag |= ONLCR | XTABS;
334 termchild.c_iflag |= ICRNL;
335 termchild.c_iflag &= ~IXOFF;
336 tcsetattr_stdin_TCSANOW(&termchild);
337 execl(shell, shell, "-i", (char *) NULL);
338 bb_simple_perror_msg_and_die(shell);
339 }
340}
341
Denys Vlasenko51fa1472010-06-25 00:57:57 +0200342int conspy_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200343int conspy_main(int argc UNUSED_PARAM, char **argv)
344{
Denys Vlasenko51fa1472010-06-25 00:57:57 +0200345 char tty_name[sizeof("/dev/ttyNN")];
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200346#define keybuf bb_common_bufsiz1
347 struct termios termbuf;
348 unsigned opts;
349 unsigned ttynum;
350 int poll_timeout_ms;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200351#if ENABLE_LONG_OPTS
352 static const char getopt_longopts[] ALIGN1 =
353 "viewonly\0" No_argument "v"
354 "createdevice\0" No_argument "c"
355 "session\0" No_argument "s"
356 "nocolors\0" No_argument "n"
357 "dump\0" No_argument "d"
358 "follow\0" No_argument "f"
359 ;
360
361 applet_long_options = getopt_longopts;
362#endif
363 INIT_G();
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200364 strcpy(G.vcsa_name, "/dev/vcsa");
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200365
366 opt_complementary = "x+:y+"; // numeric params
367 opts = getopt32(argv, "vcsndfx:y:", &G.x, &G.y);
368 argv += optind;
369 ttynum = 0;
370 if (argv[0]) {
371 ttynum = xatou_range(argv[0], 0, 63);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200372 sprintf(G.vcsa_name + sizeof("/dev/vcsa")-1, "%u", ttynum);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200373 }
374 sprintf(tty_name, "%s%u", "/dev/tty", ttynum);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200375 if (opts & FLAG(c)) {
376 if ((opts & (FLAG(s)|FLAG(v))) != FLAG(v))
377 create_cdev_if_doesnt_exist(tty_name, makedev(4, ttynum));
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200378 create_cdev_if_doesnt_exist(G.vcsa_name, makedev(7, 128 + ttynum));
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200379 }
380 if ((opts & FLAG(s)) && ttynum) {
381 start_shell_in_child(tty_name);
382 }
383
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200384 screen_read_close();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200385 if (opts & FLAG(d)) {
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200386 screen_dump();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200387 bb_putchar('\n');
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200388 return 0;
389 }
390
391 bb_signals(BB_FATAL_SIGS, cleanup);
Denys Vlasenko918f4442010-06-25 14:40:25 +0200392
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200393 // All characters must be passed through to us unaltered
Denys Vlasenko918f4442010-06-25 14:40:25 +0200394 G.kbd_fd = xopen(CURRENT_TTY, O_RDONLY);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200395 tcgetattr(G.kbd_fd, &G.term_orig);
396 termbuf = G.term_orig;
397 termbuf.c_iflag &= ~(BRKINT|INLCR|ICRNL|IXON|IXOFF|IUCLC|IXANY|IMAXBEL);
Denys Vlasenko51fa1472010-06-25 00:57:57 +0200398 //termbuf.c_oflag &= ~(OPOST); - no, we still want \n -> \r\n
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200399 termbuf.c_lflag &= ~(ISIG|ICANON|ECHO);
400 termbuf.c_cc[VMIN] = 1;
401 termbuf.c_cc[VTIME] = 0;
402 tcsetattr(G.kbd_fd, TCSANOW, &termbuf);
Pascal Bellardff377992010-06-28 15:50:22 +0200403
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200404 poll_timeout_ms = 250;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200405 while (1) {
406 struct pollfd pfd;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200407 int bytes_read;
408 int i, j;
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200409 char *data, *old;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200410
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200411 // in the first loop G.width = G.height = 0: refresh
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200412 i = G.width;
413 j = G.height;
414 get_terminal_width_height(G.kbd_fd, &G.width, &G.height);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200415 if (option_mask32 & FLAG(f)) {
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200416 int nx = G.remote.cursor_x - G.width + 1;
417 int ny = G.remote.cursor_y - G.height + 1;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200418
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200419 if (G.remote.cursor_x < G.x) {
420 G.x = G.remote.cursor_x;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200421 i = 0; // force refresh
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200422 }
423 if (nx > G.x) {
424 G.x = nx;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200425 i = 0; // force refresh
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200426 }
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200427 if (G.remote.cursor_y < G.y) {
428 G.y = G.remote.cursor_y;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200429 i = 0; // force refresh
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200430 }
431 if (ny > G.y) {
432 G.y = ny;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200433 i = 0; // force refresh
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200434 }
435 }
436
437 // Scan console data and redraw our tty where needed
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200438 old = G.data + G.current;
439 G.current = G.size - G.current;
440 data = G.data + G.current;
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200441 screen_read_close();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200442 if (i != G.width || j != G.height) {
443 clrscr();
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200444 screen_dump();
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200445 } else {
446 // For each remote line
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200447 old += G.first_line_offset;
448 data += G.first_line_offset;
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200449 for (i = G.y; i < G.remote.lines; i++) {
450 char *first = NULL; // first char which needs updating
451 char *last = last; // last char which needs updating
452 unsigned iy = i - G.y;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200453
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200454 if (iy >= G.height)
455 break;
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200456 for (j = 0; j < G.remote.cols; j++, NEXT(old), NEXT(data)) {
457 unsigned jx = j - G.x; // if will catch j >= G.x too
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200458
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200459 if (jx < G.width && DATA(data) != DATA(old)) {
460 last = data;
461 if (!first) {
462 first = data;
463 gotoxy(jx, iy);
464 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200465 }
466 }
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200467 if (first) {
468 // Rewrite updated data on the local screen
469 for (; first <= last; NEXT(first))
470 screen_char(first);
471 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200472 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200473 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200474 curmove();
475
476 // Wait for local user keypresses
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200477 fflush_all();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200478 pfd.fd = G.kbd_fd;
479 pfd.events = POLLIN;
480 bytes_read = 0;
481 switch (poll(&pfd, 1, poll_timeout_ms)) {
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200482 char *k;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200483 case -1:
484 if (errno != EINTR)
485 cleanup(1);
486 break;
487 case 0:
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200488 if (++G.nokeys >= 4)
489 G.nokeys = G.escape_count = 0;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200490 break;
491 default:
492 // Read the keys pressed
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200493 k = keybuf + G.key_count;
494 bytes_read = read(G.kbd_fd, k, sizeof(keybuf) - G.key_count);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200495 if (bytes_read < 0)
496 cleanup(1);
497
498 // Do exit processing
499 for (i = 0; i < bytes_read; i++) {
Denys Vlasenko51fa1472010-06-25 00:57:57 +0200500 if (k[i] != '\033')
501 G.escape_count = 0;
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200502 else if (++G.escape_count >= 3)
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200503 cleanup(0);
504 }
505 }
506 poll_timeout_ms = 250;
Pascal Bellard6161cdb2011-04-11 03:52:53 +0200507 if (option_mask32 & FLAG(v)) continue;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200508
509 // Insert all keys pressed into the virtual console's input
510 // buffer. Don't do this if the virtual console is in scan
511 // code mode - giving ASCII characters to a program expecting
512 // scan codes will confuse it.
Pascal Bellard6161cdb2011-04-11 03:52:53 +0200513 G.key_count += bytes_read;
514 if (G.escape_count == 0) {
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200515 int handle, result;
516 long kbd_mode;
517
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200518 handle = xopen(tty_name, O_WRONLY);
519 result = ioctl(handle, KDGKBMODE, &kbd_mode);
Pascal Bellard81199672010-07-01 07:18:41 +0200520 if (result >= 0) {
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200521 char *p = keybuf;
Pascal Bellard81199672010-07-01 07:18:41 +0200522
523 if (kbd_mode != K_XLATE && kbd_mode != K_UNICODE) {
524 G.key_count = 0; // scan code mode
525 }
526 for (; G.key_count != 0; p++, G.key_count--) {
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200527 result = ioctl(handle, TIOCSTI, p);
Pascal Bellard81199672010-07-01 07:18:41 +0200528 if (result < 0) {
529 memmove(keybuf, p, G.key_count);
530 break;
531 }
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200532 // If there is an application on console which reacts
533 // to keypresses, we need to make our first sleep
534 // shorter to quickly redraw whatever it printed there.
535 poll_timeout_ms = 20;
536 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200537 }
538 // Close & re-open tty in case they have
539 // swapped virtual consoles
540 close(handle);
541
542 // We sometimes get spurious IO errors on the TTY
543 // as programs close and re-open it
Pascal Bellard81199672010-07-01 07:18:41 +0200544 if (result >= 0)
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200545 G.ioerror_count = 0;
546 else if (errno != EIO || ++G.ioerror_count > 4)
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200547 cleanup(1);
548 }
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200549 } /* while (1) */
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200550}