-
Notifications
You must be signed in to change notification settings - Fork 85
/
lt_screen.c
382 lines (347 loc) · 8.13 KB
/
lt_screen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include "lt_types.h"
#include "wchar.h"
static const char version[] = "lt_screen|v=1";
int usage() {
fprintf(stderr, "usage: lt_screen\n");
return 0;
}
// ------------------------------------------------------------------
#define MAX_PARAMS 3
typedef struct ScreenChar {
wchar ch;
Attr attr;
Color fg_color;
Color bg_color;
} ScreenChar;
typedef struct ScreenState {
ScreenChar* chars;
int w;
int h;
int cx;
int cy;
Attr curr_attr;
Color curr_fg_color;
Color curr_bg_color;
int param_top;
int params[MAX_PARAMS+1];
int in_esc;
} ScreenState;
static ScreenState screen;
static int ttyin; // input text and control sequences
static int ttyout; // output for screen dump
static int quiet = 0;
static int verbose = 0;
// ------------------------------------------------------------------
void screen_init() {
screen.w = 80;
screen.h = 24;
screen.cx = 0;
screen.cy = 0;
screen.in_esc = 0;
screen.curr_attr = 0;
screen.curr_fg_color = screen.curr_bg_color = 0;
screen.param_top = -1;
screen.params[0] = 0;
}
void param_print() {
int i;
fprintf(stderr, "(");
for (i = 0; i <= screen.param_top; ++i)
fprintf(stderr, "%d ", screen.params[i]);
fprintf(stderr, ")");
}
void param_push(int v) {
if (screen.param_top >= (int) countof(screen.params)-1)
return;
screen.params[++screen.param_top] = v;
}
int param_pop(){
if (screen.param_top < 0)
return 0; // missing param is assumed to be 0
return screen.params[screen.param_top--];
}
int screen_x(int x) {
if (x < 0) x = 0;
if (x >= screen.w) x = screen.w-1;
return x;
}
int screen_y(int y) {
if (y < 0) y = 0;
if (y >= screen.h) y = screen.h-1;
return y;
}
ScreenChar* screen_char(int x, int y) {
x = screen_x(x);
y = screen_x(y);
return &screen.chars[y * screen.w + x];
}
int screen_incr(int* px, int* py) {
if (++(*px) >= screen.w) {
*px = 0;
if (++(*py) >= screen.h) {
*py = 0;
return 0;
}
}
return 1;
}
void screen_char_set(int x, int y, wchar ch, Attr attr, Color fg_color, Color bg_color) {
ScreenChar* sc = screen_char(x, y);
sc->ch = ch;
sc->attr = attr;
sc->fg_color = fg_color;
sc->bg_color = bg_color;
}
int screen_clear(int x, int y, int count) {
while (count-- > 0) {
screen_char_set(x, y, '_', 0, 0, 0);
screen_incr(&x, &y);
}
return 1;
}
int screen_busy() {
write(ttyout, "*BUSY*\n", 7);
return 1;
}
int screen_read(int x, int y, int count) {
//write(ttyout, "$|", 2);
int attr = 0;
int fg_color = 0;
int bg_color = 0;
while (count-- > 0) {
byte buf[32];
byte* bufp = buf;
ScreenChar* sc = screen_char(x, y);
if (sc->attr != attr) {
attr = sc->attr;
*bufp++ = '@';
*bufp++ = attr;
}
if (sc->fg_color != fg_color || sc->bg_color != bg_color) {
fg_color = sc->fg_color;
bg_color = sc->bg_color;
*bufp++ = '$';
*bufp++ = fg_color;
*bufp++ = bg_color;
}
if (x == screen.cx && y == screen.cy)
*bufp++ = '#';
if (sc->ch == '@' || sc->ch == '$' || sc->ch == '\\' || sc->ch == '#')
*bufp++ = '\\';
store_wchar(&bufp, sc->ch);
write(ttyout, buf, bufp-buf);
screen_incr(&x, &y);
}
write(ttyout, "\n", 1);
return 1;
}
int screen_move(int x, int y) {
screen.cx = x;
screen.cy = y;
return 1;
}
int screen_cr() {
screen.cx = 0;
return 1;
}
int screen_bs() {
if (screen.cx <= 0) return 0;
--screen.cx;
return 1;
}
int screen_scroll() {
int len = screen.w * (screen.h-1);
memmove(screen_char(0,0), screen_char(0,1), len * sizeof(ScreenChar));
screen_clear(0, screen.h-1, screen.w);
return 1;
}
int screen_rscroll() {
int len = screen.w * (screen.h-1);
memmove(screen_char(0,1), screen_char(0,0), len * sizeof(ScreenChar));
screen_clear(0, 0, screen.w);
return 1;
}
int screen_set_attr(int attr) {
screen.curr_attr |= attr;
return 0;
}
int screen_clear_attr(int attr) {
screen.curr_attr &= ~attr;
return 0;
}
// ------------------------------------------------------------------
void beep() {
if (!quiet)
fprintf(stderr, "\7");
}
int exec_esc(wchar ch) {
int x, y, count;
if (verbose) {
fprintf(stderr, "exec ESC-%c ", (char)ch);
param_print();
fprintf(stderr, "\n");
}
switch (ch) {
case 'A': // clear all
return screen_clear(0, 0, screen.w * screen.h);
case 'L': // clear from cursor to end of line
return screen_clear(screen.cx, screen.cy, screen.w - screen.cx);
case 'S': // clear from cursor to end of screen
return screen_clear(screen.cx, screen.cy,
(screen.w - screen.cx) + (screen.h - screen.cy -1) * screen.w);
case 'R': // read N3 chars starting at (N1,N2)
count = param_pop();
y = param_pop();
x = param_pop();
return screen_read(x, y, count);
case 'j': // jump cursor to (N1,N2)
y = param_pop();
x = param_pop();
return screen_move(x, y);
case 'g': // visual bell
return 0;
case 'h': // cursor home
return screen_move(0, 0);
case 'l': // cursor lower left
return screen_move(0, screen.h-1);
case 'r': // reverse scroll
return screen_rscroll();
case '<': // cursor left to start of line
return screen_cr();
case 's': // enter standout
return screen_set_attr(ATTR_STANDOUT);
case 't': // exit standout
return screen_clear_attr(ATTR_STANDOUT);
case 'u': // enter underline
return screen_set_attr(ATTR_UNDERLINE);
case 'v': // exit underline
return screen_clear_attr(ATTR_UNDERLINE);
case 'd': // enter bold
return screen_set_attr(ATTR_BOLD);
case 'e': // exit bold
return screen_clear_attr(ATTR_BOLD);
case 'b': // enter blink
return screen_set_attr(ATTR_BLINK);
case 'c': // exit blink
return screen_clear_attr(ATTR_BLINK);
case '?': // print version string
write(ttyout, version, strlen(version));
return 1;
default:
return 0;
}
}
int add_char(wchar ch) {
if (verbose) fprintf(stderr, "add %lx at %d,%d\n", ch, screen.cx, screen.cy);
screen_char_set(screen.cx, screen.cy, ch, screen.curr_attr, screen.curr_fg_color, screen.curr_bg_color);
int fits = screen_incr(&screen.cx, &screen.cy);
if (fits && is_wide_char(ch)) {
screen_char_set(screen.cx, screen.cy, 0, 0, 0, 0);
fits = screen_incr(&screen.cx, &screen.cy);
}
if (!fits) { // Wrap at bottom of screen = scroll
screen.cx = 0;
screen.cy = screen.h-1;
return screen_scroll();
}
return 1;
}
int process_char(wchar ch) {
int ok = 1;
if (screen.in_esc) {
if (ch >= '0' && ch <= '9') {
param_push(10 * param_pop() + ch - '0');
} else if (ch == ';') {
param_push(0);
} else {
screen.in_esc = 0;
ok = exec_esc(ch);
}
} else if (ch == ESC) {
screen.in_esc = 1;
} else if (ch == '\r') {
screen_cr();
} else if (ch == '\b') {
screen_bs();
} else if (ch == '\n') {
if (screen.cy < screen.h-1)
++screen.cy;
else
screen_scroll();
screen.cx = 0; // auto CR
} else if (ch == '\7') {
beep();
} else if (ch == '\t') {
ok = add_char(' ');
} else if (ch >= '\40') {
ok = add_char(ch);
}
return ok;
}
void screen_dump_handler(int signum) {
// (signum == LTSIG_READ_SCREEN)
if (verbose) fprintf(stderr, "screen: rcv dump signal\n");
(void) screen_read(0, 0, screen.w * screen.h);
}
// ------------------------------------------------------------------
int setup(int argc, char** argv) {
int ch;
int ready_pid = 0;
screen_init();
while ((ch = getopt(argc, argv, "h:qr:vw:")) != -1) {
switch (ch) {
case 'h':
screen.h = atoi(optarg);
break;
case 'q':
quiet = 1;
break;
case 'r':
ready_pid = atoi(optarg);
break;
case 'v':
++verbose;
break;
case 'w':
screen.w = atoi(optarg);
break;
default:
return usage();
}
}
int len = screen.w * screen.h;
screen.chars = malloc(len * sizeof(ScreenChar));
screen_clear(0, 0, len);
if (optind >= argc) {
ttyin = 0;
ttyout = 1;
} else {
ttyin = ttyout = open(argv[optind], O_RDWR);
if (ttyin < 0) {
fprintf(stderr, "cannot open %s\n", argv[optind]);
return 0;
}
}
signal(LTSIG_SCREEN_DUMP, screen_dump_handler);
if (ready_pid != 0)
kill(ready_pid, LTSIG_SCREEN_READY);
return 1;
}
int main(int argc, char** argv) {
if (!setup(argc, argv))
return RUN_ERR;
for (;;) {
wchar ch = read_wchar(ttyin);
if (verbose) fprintf(stderr, "screen read %c (%lx)\n", pr_ascii(ch), ch);
if (ch == 0)
break;
if (!process_char(ch))
beep();
}
return RUN_OK;
}