| 1 |
#include "vterm.h" |
| 2 |
#include "../src/vterm_internal.h" |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#define streq(a,b) (!strcmp(a,b)) |
| 8 |
#define strstartswith(a,b) (!strncmp(a,b,strlen(b))) |
| 9 |
|
| 10 |
static size_t inplace_hex2bytes(char *s) |
| 11 |
{ |
| 12 |
char *inpos = s, *outpos = s; |
| 13 |
|
| 14 |
while(*inpos) { |
| 15 |
unsigned int ch; |
| 16 |
sscanf(inpos, "%2x", &ch); |
| 17 |
*outpos = ch; |
| 18 |
outpos += 1; inpos += 2; |
| 19 |
} |
| 20 |
|
| 21 |
return outpos - s; |
| 22 |
} |
| 23 |
|
| 24 |
static VTermModifier strpe_modifiers(char **strp) |
| 25 |
{ |
| 26 |
VTermModifier state = 0; |
| 27 |
|
| 28 |
while((*strp)[0]) { |
| 29 |
switch(((*strp)++)[0]) { |
| 30 |
case 'S': state |= VTERM_MOD_SHIFT; break; |
| 31 |
case 'C': state |= VTERM_MOD_CTRL; break; |
| 32 |
case 'A': state |= VTERM_MOD_ALT; break; |
| 33 |
default: return state; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return state; |
| 38 |
} |
| 39 |
|
| 40 |
static VTermKey strp_key(char *str) |
| 41 |
{ |
| 42 |
static struct { |
| 43 |
char *name; |
| 44 |
VTermKey key; |
| 45 |
} keys[] = { |
| 46 |
{ "Up", VTERM_KEY_UP }, |
| 47 |
{ "Tab", VTERM_KEY_TAB }, |
| 48 |
{ "Enter", VTERM_KEY_ENTER }, |
| 49 |
{ "KP0", VTERM_KEY_KP_0 }, |
| 50 |
{ "F1", VTERM_KEY_FUNCTION(1) }, |
| 51 |
{ NULL, VTERM_KEY_NONE }, |
| 52 |
}; |
| 53 |
|
| 54 |
for(int i = 0; keys[i].name; i++) { |
| 55 |
if(streq(str, keys[i].name)) |
| 56 |
return keys[i].key; |
| 57 |
} |
| 58 |
|
| 59 |
return VTERM_KEY_NONE; |
| 60 |
} |
| 61 |
|
| 62 |
static void print_color(const VTermColor *col) |
| 63 |
{ |
| 64 |
if (VTERM_COLOR_IS_RGB(col)) { |
| 65 |
printf("rgb(%d,%d,%d", col->rgb.red, col->rgb.green, col->rgb.blue); |
| 66 |
} |
| 67 |
else if (VTERM_COLOR_IS_INDEXED(col)) { |
| 68 |
printf("idx(%d", col->indexed.idx); |
| 69 |
} |
| 70 |
else { |
| 71 |
printf("invalid(%d", col->type); |
| 72 |
} |
| 73 |
if (VTERM_COLOR_IS_DEFAULT_FG(col)) { |
| 74 |
printf(",is_default_fg"); |
| 75 |
} |
| 76 |
if (VTERM_COLOR_IS_DEFAULT_BG(col)) { |
| 77 |
printf(",is_default_bg"); |
| 78 |
} |
| 79 |
printf(")"); |
| 80 |
} |
| 81 |
|
| 82 |
static VTerm *vt; |
| 83 |
static VTermState *state; |
| 84 |
static VTermScreen *screen; |
| 85 |
|
| 86 |
static VTermEncodingInstance encoding; |
| 87 |
|
| 88 |
static void term_output(const char *s, size_t len, void *user) |
| 89 |
{ |
| 90 |
printf("output "); |
| 91 |
for(int i = 0; i < len; i++) |
| 92 |
printf("%x%s", (unsigned char)s[i], i < len-1 ? "," : "\n"); |
| 93 |
} |
| 94 |
|
| 95 |
static int parser_text(const char bytes[], size_t len, void *user) |
| 96 |
{ |
| 97 |
printf("text "); |
| 98 |
int i; |
| 99 |
for(i = 0; i < len; i++) { |
| 100 |
unsigned char b = bytes[i]; |
| 101 |
if(b < 0x20 || b == 0x7f || (b >= 0x80 && b < 0xa0)) |
| 102 |
break; |
| 103 |
printf(i ? ",%x" : "%x", b); |
| 104 |
} |
| 105 |
printf("\n"); |
| 106 |
|
| 107 |
return i; |
| 108 |
} |
| 109 |
|
| 110 |
static int parser_control(unsigned char control, void *user) |
| 111 |
{ |
| 112 |
printf("control %02x\n", control); |
| 113 |
|
| 114 |
return 1; |
| 115 |
} |
| 116 |
|
| 117 |
static int parser_escape(const char bytes[], size_t len, void *user) |
| 118 |
{ |
| 119 |
if(bytes[0] >= 0x20 && bytes[0] < 0x30) { |
| 120 |
if(len < 2) |
| 121 |
return -1; |
| 122 |
len = 2; |
| 123 |
} |
| 124 |
else { |
| 125 |
len = 1; |
| 126 |
} |
| 127 |
|
| 128 |
printf("escape "); |
| 129 |
for(int i = 0; i < len; i++) |
| 130 |
printf("%02x", bytes[i]); |
| 131 |
printf("\n"); |
| 132 |
|
| 133 |
return len; |
| 134 |
} |
| 135 |
|
| 136 |
static int parser_csi(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user) |
| 137 |
{ |
| 138 |
printf("csi %02x", command); |
| 139 |
|
| 140 |
if(leader && leader[0]) { |
| 141 |
printf(" L="); |
| 142 |
for(int i = 0; leader[i]; i++) |
| 143 |
printf("%02x", leader[i]); |
| 144 |
} |
| 145 |
|
| 146 |
for(int i = 0; i < argcount; i++) { |
| 147 |
char sep = i ? ',' : ' '; |
| 148 |
|
| 149 |
if(args[i] == CSI_ARG_MISSING) |
| 150 |
printf("%c*", sep); |
| 151 |
else |
| 152 |
printf("%c%ld%s", sep, CSI_ARG(args[i]), CSI_ARG_HAS_MORE(args[i]) ? "+" : ""); |
| 153 |
} |
| 154 |
|
| 155 |
if(intermed && intermed[0]) { |
| 156 |
printf(" I="); |
| 157 |
for(int i = 0; intermed[i]; i++) |
| 158 |
printf("%02x", intermed[i]); |
| 159 |
} |
| 160 |
|
| 161 |
printf("\n"); |
| 162 |
|
| 163 |
return 1; |
| 164 |
} |
| 165 |
|
| 166 |
static int parser_osc(const char *command, size_t cmdlen, void *user) |
| 167 |
{ |
| 168 |
printf("osc "); |
| 169 |
for(int i = 0; i < cmdlen; i++) |
| 170 |
printf("%02x", command[i]); |
| 171 |
printf("\n"); |
| 172 |
|
| 173 |
return 1; |
| 174 |
} |
| 175 |
|
| 176 |
static int parser_dcs(const char *command, size_t cmdlen, void *user) |
| 177 |
{ |
| 178 |
printf("dcs "); |
| 179 |
for(int i = 0; i < cmdlen; i++) |
| 180 |
printf("%02x", command[i]); |
| 181 |
printf("\n"); |
| 182 |
|
| 183 |
return 1; |
| 184 |
} |
| 185 |
|
| 186 |
static VTermParserCallbacks parser_cbs = { |
| 187 |
.text = parser_text, |
| 188 |
.control = parser_control, |
| 189 |
.escape = parser_escape, |
| 190 |
.csi = parser_csi, |
| 191 |
.osc = parser_osc, |
| 192 |
.dcs = parser_dcs, |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
static int want_movecursor = 0; |
| 198 |
static VTermPos state_pos; |
| 199 |
static int movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user) |
| 200 |
{ |
| 201 |
state_pos = pos; |
| 202 |
|
| 203 |
if(want_movecursor) |
| 204 |
printf("movecursor %d,%d\n", pos.row, pos.col); |
| 205 |
|
| 206 |
return 1; |
| 207 |
} |
| 208 |
|
| 209 |
static int want_scrollrect = 0; |
| 210 |
static int scrollrect(VTermRect rect, int downward, int rightward, void *user) |
| 211 |
{ |
| 212 |
if(!want_scrollrect) |
| 213 |
return 0; |
| 214 |
|
| 215 |
printf("scrollrect %d..%d,%d..%d => %+d,%+d\n", |
| 216 |
rect.start_row, rect.end_row, rect.start_col, rect.end_col, |
| 217 |
downward, rightward); |
| 218 |
|
| 219 |
return 1; |
| 220 |
} |
| 221 |
|
| 222 |
static int want_moverect = 0; |
| 223 |
static int moverect(VTermRect dest, VTermRect src, void *user) |
| 224 |
{ |
| 225 |
if(!want_moverect) |
| 226 |
return 0; |
| 227 |
|
| 228 |
printf("moverect %d..%d,%d..%d -> %d..%d,%d..%d\n", |
| 229 |
src.start_row, src.end_row, src.start_col, src.end_col, |
| 230 |
dest.start_row, dest.end_row, dest.start_col, dest.end_col); |
| 231 |
|
| 232 |
return 1; |
| 233 |
} |
| 234 |
|
| 235 |
static int want_settermprop = 0; |
| 236 |
static int settermprop(VTermProp prop, VTermValue *val, void *user) |
| 237 |
{ |
| 238 |
if(!want_settermprop) |
| 239 |
return 1; |
| 240 |
|
| 241 |
VTermValueType type = vterm_get_prop_type(prop); |
| 242 |
switch(type) { |
| 243 |
case VTERM_VALUETYPE_BOOL: |
| 244 |
printf("settermprop %d %s\n", prop, val->boolean ? "true" : "false"); |
| 245 |
return 1; |
| 246 |
case VTERM_VALUETYPE_INT: |
| 247 |
printf("settermprop %d %d\n", prop, val->number); |
| 248 |
return 1; |
| 249 |
case VTERM_VALUETYPE_STRING: |
| 250 |
printf("settermprop %d \"%s\"\n", prop, val->string); |
| 251 |
return 1; |
| 252 |
case VTERM_VALUETYPE_COLOR: |
| 253 |
printf("settermprop %d ", prop); |
| 254 |
print_color(&val->color); |
| 255 |
printf("\n"); |
| 256 |
return 1; |
| 257 |
|
| 258 |
case VTERM_N_VALUETYPES: |
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
static int want_state_putglyph = 0; |
| 268 |
static int state_putglyph(VTermGlyphInfo *info, VTermPos pos, void *user) |
| 269 |
{ |
| 270 |
if(!want_state_putglyph) |
| 271 |
return 1; |
| 272 |
|
| 273 |
printf("putglyph "); |
| 274 |
for(int i = 0; info->chars[i]; i++) |
| 275 |
printf(i ? ",%x" : "%x", info->chars[i]); |
| 276 |
printf(" %d %d,%d", info->width, pos.row, pos.col); |
| 277 |
if(info->protected_cell) |
| 278 |
printf(" prot"); |
| 279 |
if(info->dwl) |
| 280 |
printf(" dwl"); |
| 281 |
if(info->dhl) |
| 282 |
printf(" dhl-%s", info->dhl == 1 ? "top" : info->dhl == 2 ? "bottom" : "?" ); |
| 283 |
printf("\n"); |
| 284 |
|
| 285 |
return 1; |
| 286 |
} |
| 287 |
|
| 288 |
static int want_state_erase = 0; |
| 289 |
static int state_erase(VTermRect rect, int selective, void *user) |
| 290 |
{ |
| 291 |
if(!want_state_erase) |
| 292 |
return 1; |
| 293 |
|
| 294 |
printf("erase %d..%d,%d..%d%s\n", |
| 295 |
rect.start_row, rect.end_row, rect.start_col, rect.end_col, |
| 296 |
selective ? " selective" : ""); |
| 297 |
|
| 298 |
return 1; |
| 299 |
} |
| 300 |
|
| 301 |
static struct { |
| 302 |
int bold; |
| 303 |
int underline; |
| 304 |
int italic; |
| 305 |
int blink; |
| 306 |
int reverse; |
| 307 |
int strike; |
| 308 |
int font; |
| 309 |
VTermColor foreground; |
| 310 |
VTermColor background; |
| 311 |
} state_pen; |
| 312 |
static int state_setpenattr(VTermAttr attr, VTermValue *val, void *user) |
| 313 |
{ |
| 314 |
switch(attr) { |
| 315 |
case VTERM_ATTR_BOLD: |
| 316 |
state_pen.bold = val->boolean; |
| 317 |
break; |
| 318 |
case VTERM_ATTR_UNDERLINE: |
| 319 |
state_pen.underline = val->number; |
| 320 |
break; |
| 321 |
case VTERM_ATTR_ITALIC: |
| 322 |
state_pen.italic = val->boolean; |
| 323 |
break; |
| 324 |
case VTERM_ATTR_BLINK: |
| 325 |
state_pen.blink = val->boolean; |
| 326 |
break; |
| 327 |
case VTERM_ATTR_REVERSE: |
| 328 |
state_pen.reverse = val->boolean; |
| 329 |
break; |
| 330 |
case VTERM_ATTR_STRIKE: |
| 331 |
state_pen.strike = val->boolean; |
| 332 |
break; |
| 333 |
case VTERM_ATTR_FONT: |
| 334 |
state_pen.font = val->number; |
| 335 |
break; |
| 336 |
case VTERM_ATTR_FOREGROUND: |
| 337 |
state_pen.foreground = val->color; |
| 338 |
break; |
| 339 |
case VTERM_ATTR_BACKGROUND: |
| 340 |
state_pen.background = val->color; |
| 341 |
break; |
| 342 |
|
| 343 |
case VTERM_N_ATTRS: |
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
return 1; |
| 348 |
} |
| 349 |
|
| 350 |
static int state_setlineinfo(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user) |
| 351 |
{ |
| 352 |
return 1; |
| 353 |
} |
| 354 |
|
| 355 |
VTermStateCallbacks state_cbs = { |
| 356 |
.putglyph = state_putglyph, |
| 357 |
.movecursor = movecursor, |
| 358 |
.scrollrect = scrollrect, |
| 359 |
.moverect = moverect, |
| 360 |
.erase = state_erase, |
| 361 |
.setpenattr = state_setpenattr, |
| 362 |
.settermprop = settermprop, |
| 363 |
.setlineinfo = state_setlineinfo, |
| 364 |
}; |
| 365 |
|
| 366 |
static int want_screen_damage = 0; |
| 367 |
static int want_screen_damage_cells = 0; |
| 368 |
static int screen_damage(VTermRect rect, void *user) |
| 369 |
{ |
| 370 |
if(!want_screen_damage) |
| 371 |
return 1; |
| 372 |
|
| 373 |
printf("damage %d..%d,%d..%d", |
| 374 |
rect.start_row, rect.end_row, rect.start_col, rect.end_col); |
| 375 |
|
| 376 |
if(want_screen_damage_cells) { |
| 377 |
bool equals = false; |
| 378 |
|
| 379 |
for(int row = rect.start_row; row < rect.end_row; row++) { |
| 380 |
int eol = rect.end_col; |
| 381 |
while(eol > rect.start_col) { |
| 382 |
VTermScreenCell cell; |
| 383 |
vterm_screen_get_cell(screen, (VTermPos) { .row = row, .col = eol-1 }, &cell); |
| 384 |
if(cell.chars[0]) |
| 385 |
break; |
| 386 |
|
| 387 |
eol--; |
| 388 |
} |
| 389 |
|
| 390 |
if(eol == rect.start_col) |
| 391 |
break; |
| 392 |
|
| 393 |
if(!equals) |
| 394 |
printf(" ="), equals = true; |
| 395 |
|
| 396 |
printf(" %d<", row); |
| 397 |
for(int col = rect.start_col; col < eol; col++) { |
| 398 |
VTermScreenCell cell; |
| 399 |
vterm_screen_get_cell(screen, (VTermPos) { .row = row, .col = col }, &cell); |
| 400 |
printf(col == rect.start_col ? "%02X" : " %02X", cell.chars[0]); |
| 401 |
} |
| 402 |
printf(">"); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
printf("\n"); |
| 407 |
|
| 408 |
return 1; |
| 409 |
} |
| 410 |
|
| 411 |
static int want_screen_scrollback = 0; |
| 412 |
static int screen_sb_pushline(int cols, const VTermScreenCell *cells, void *user) |
| 413 |
{ |
| 414 |
if(!want_screen_scrollback) |
| 415 |
return 1; |
| 416 |
|
| 417 |
int eol = cols; |
| 418 |
while(eol && !cells[eol-1].chars[0]) |
| 419 |
eol--; |
| 420 |
|
| 421 |
printf("sb_pushline %d =", cols); |
| 422 |
for(int c = 0; c < eol; c++) |
| 423 |
printf(" %02X", cells[c].chars[0]); |
| 424 |
printf("\n"); |
| 425 |
|
| 426 |
return 1; |
| 427 |
} |
| 428 |
|
| 429 |
static int screen_sb_popline(int cols, VTermScreenCell *cells, void *user) |
| 430 |
{ |
| 431 |
if(!want_screen_scrollback) |
| 432 |
return 0; |
| 433 |
|
| 434 |
|
| 435 |
for(int col = 0; col < cols; col++) { |
| 436 |
if(col < 5) |
| 437 |
cells[col].chars[0] = 'A' + col; |
| 438 |
else |
| 439 |
cells[col].chars[0] = 0; |
| 440 |
|
| 441 |
cells[col].width = 1; |
| 442 |
} |
| 443 |
|
| 444 |
printf("sb_popline %d\n", cols); |
| 445 |
return 1; |
| 446 |
} |
| 447 |
|
| 448 |
VTermScreenCallbacks screen_cbs = { |
| 449 |
.damage = screen_damage, |
| 450 |
.moverect = moverect, |
| 451 |
.movecursor = movecursor, |
| 452 |
.settermprop = settermprop, |
| 453 |
.sb_pushline = screen_sb_pushline, |
| 454 |
.sb_popline = screen_sb_popline, |
| 455 |
}; |
| 456 |
|
| 457 |
int main(int argc, char **argv) |
| 458 |
{ |
| 459 |
char line[1024] = {0}; |
| 460 |
int flag; |
| 461 |
|
| 462 |
int err; |
| 463 |
|
| 464 |
setvbuf(stdout, NULL, _IONBF, 0); |
| 465 |
|
| 466 |
while(fgets(line, sizeof line, stdin)) { |
| 467 |
err = 0; |
| 468 |
|
| 469 |
char *nl; |
| 470 |
if((nl = strchr(line, '\n'))) |
| 471 |
*nl = '\0'; |
| 472 |
|
| 473 |
if(streq(line, "INIT")) { |
| 474 |
if(!vt) |
| 475 |
vt = vterm_new(25, 80); |
| 476 |
|
| 477 |
vterm_output_set_callback(vt, term_output, NULL); |
| 478 |
} |
| 479 |
|
| 480 |
else if(streq(line, "WANTPARSER")) { |
| 481 |
vterm_parser_set_callbacks(vt, &parser_cbs, NULL); |
| 482 |
} |
| 483 |
|
| 484 |
else if(strstartswith(line, "WANTSTATE") && (line[9] == '\0' || line[9] == ' ')) { |
| 485 |
if(!state) { |
| 486 |
state = vterm_obtain_state(vt); |
| 487 |
vterm_state_set_callbacks(state, &state_cbs, NULL); |
| 488 |
vterm_state_set_bold_highbright(state, 1); |
| 489 |
vterm_state_reset(state, 1); |
| 490 |
} |
| 491 |
|
| 492 |
int i = 9; |
| 493 |
int sense = 1; |
| 494 |
while(line[i] == ' ') |
| 495 |
i++; |
| 496 |
for( ; line[i]; i++) |
| 497 |
switch(line[i]) { |
| 498 |
case '+': |
| 499 |
sense = 1; |
| 500 |
break; |
| 501 |
case '-': |
| 502 |
sense = 0; |
| 503 |
break; |
| 504 |
case 'g': |
| 505 |
want_state_putglyph = sense; |
| 506 |
break; |
| 507 |
case 's': |
| 508 |
want_scrollrect = sense; |
| 509 |
break; |
| 510 |
case 'm': |
| 511 |
want_moverect = sense; |
| 512 |
break; |
| 513 |
case 'e': |
| 514 |
want_state_erase = sense; |
| 515 |
break; |
| 516 |
case 'p': |
| 517 |
want_settermprop = sense; |
| 518 |
break; |
| 519 |
case 'f': |
| 520 |
vterm_state_set_unrecognised_fallbacks(state, sense ? &parser_cbs : NULL, NULL); |
| 521 |
break; |
| 522 |
default: |
| 523 |
fprintf(stderr, "Unrecognised WANTSTATE flag '%c'\n", line[i]); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
else if(strstartswith(line, "WANTSCREEN") && (line[10] == '\0' || line[10] == ' ')) { |
| 528 |
if(!screen) |
| 529 |
screen = vterm_obtain_screen(vt); |
| 530 |
vterm_screen_enable_altscreen(screen, 1); |
| 531 |
vterm_screen_set_callbacks(screen, &screen_cbs, NULL); |
| 532 |
|
| 533 |
int i = 10; |
| 534 |
int sense = 1; |
| 535 |
while(line[i] == ' ') |
| 536 |
i++; |
| 537 |
for( ; line[i]; i++) |
| 538 |
switch(line[i]) { |
| 539 |
case '-': |
| 540 |
sense = 0; |
| 541 |
break; |
| 542 |
case 'd': |
| 543 |
want_screen_damage = sense; |
| 544 |
break; |
| 545 |
case 'D': |
| 546 |
want_screen_damage = sense; |
| 547 |
want_screen_damage_cells = sense; |
| 548 |
break; |
| 549 |
case 'm': |
| 550 |
want_moverect = sense; |
| 551 |
break; |
| 552 |
case 'c': |
| 553 |
want_movecursor = sense; |
| 554 |
break; |
| 555 |
case 'p': |
| 556 |
want_settermprop = 1; |
| 557 |
break; |
| 558 |
case 'b': |
| 559 |
want_screen_scrollback = sense; |
| 560 |
break; |
| 561 |
default: |
| 562 |
fprintf(stderr, "Unrecognised WANTSCREEN flag '%c'\n", line[i]); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
else if(sscanf(line, "UTF8 %d", &flag)) { |
| 567 |
vterm_set_utf8(vt, flag); |
| 568 |
} |
| 569 |
|
| 570 |
else if(streq(line, "RESET")) { |
| 571 |
if(state) { |
| 572 |
vterm_state_reset(state, 1); |
| 573 |
vterm_state_get_cursorpos(state, &state_pos); |
| 574 |
} |
| 575 |
if(screen) { |
| 576 |
vterm_screen_reset(screen, 1); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
else if(strstartswith(line, "RESIZE ")) { |
| 581 |
int rows, cols; |
| 582 |
char *linep = line + 7; |
| 583 |
while(linep[0] == ' ') |
| 584 |
linep++; |
| 585 |
sscanf(linep, "%d, %d", &rows, &cols); |
| 586 |
vterm_set_size(vt, rows, cols); |
| 587 |
} |
| 588 |
|
| 589 |
else if(strstartswith(line, "PUSH ")) { |
| 590 |
char *bytes = line + 5; |
| 591 |
size_t len = inplace_hex2bytes(bytes); |
| 592 |
size_t written = vterm_input_write(vt, bytes, len); |
| 593 |
if(written < len) |
| 594 |
fprintf(stderr, "! short write\n"); |
| 595 |
} |
| 596 |
|
| 597 |
else if(streq(line, "WANTENCODING")) { |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
encoding.enc = vterm_lookup_encoding(ENC_UTF8, 'u'); |
| 602 |
if(encoding.enc->init) |
| 603 |
(*encoding.enc->init)(encoding.enc, encoding.data); |
| 604 |
} |
| 605 |
|
| 606 |
else if(strstartswith(line, "ENCIN ")) { |
| 607 |
char *bytes = line + 6; |
| 608 |
size_t len = inplace_hex2bytes(bytes); |
| 609 |
|
| 610 |
uint32_t cp[len]; |
| 611 |
int cpi = 0; |
| 612 |
size_t pos = 0; |
| 613 |
|
| 614 |
(*encoding.enc->decode)(encoding.enc, encoding.data, |
| 615 |
cp, &cpi, len, bytes, &pos, len); |
| 616 |
|
| 617 |
if(cpi > 0) { |
| 618 |
printf("encout "); |
| 619 |
for(int i = 0; i < cpi; i++) { |
| 620 |
printf(i ? ",%x" : "%x", cp[i]); |
| 621 |
} |
| 622 |
printf("\n"); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
else if(strstartswith(line, "INCHAR ")) { |
| 627 |
char *linep = line + 7; |
| 628 |
unsigned int c = 0; |
| 629 |
while(linep[0] == ' ') |
| 630 |
linep++; |
| 631 |
VTermModifier mod = strpe_modifiers(&linep); |
| 632 |
sscanf(linep, " %x", &c); |
| 633 |
|
| 634 |
vterm_keyboard_unichar(vt, c, mod); |
| 635 |
} |
| 636 |
|
| 637 |
else if(strstartswith(line, "INKEY ")) { |
| 638 |
char *linep = line + 6; |
| 639 |
while(linep[0] == ' ') |
| 640 |
linep++; |
| 641 |
VTermModifier mod = strpe_modifiers(&linep); |
| 642 |
while(linep[0] == ' ') |
| 643 |
linep++; |
| 644 |
VTermKey key = strp_key(linep); |
| 645 |
|
| 646 |
vterm_keyboard_key(vt, key, mod); |
| 647 |
} |
| 648 |
|
| 649 |
else if(strstartswith(line, "PASTE ")) { |
| 650 |
char *linep = line + 6; |
| 651 |
if(streq(linep, "START")) |
| 652 |
vterm_keyboard_start_paste(vt); |
| 653 |
else if(streq(linep, "END")) |
| 654 |
vterm_keyboard_end_paste(vt); |
| 655 |
else |
| 656 |
goto abort_line; |
| 657 |
} |
| 658 |
|
| 659 |
else if(strstartswith(line, "FOCUS ")) { |
| 660 |
char *linep = line + 6; |
| 661 |
if(streq(linep, "IN")) |
| 662 |
vterm_state_focus_in(state); |
| 663 |
else if(streq(linep, "OUT")) |
| 664 |
vterm_state_focus_out(state); |
| 665 |
else |
| 666 |
goto abort_line; |
| 667 |
} |
| 668 |
|
| 669 |
else if(strstartswith(line, "MOUSEMOVE ")) { |
| 670 |
char *linep = line + 10; |
| 671 |
int row, col, len; |
| 672 |
while(linep[0] == ' ') |
| 673 |
linep++; |
| 674 |
sscanf(linep, "%d,%d%n", &row, &col, &len); |
| 675 |
linep += len; |
| 676 |
while(linep[0] == ' ') |
| 677 |
linep++; |
| 678 |
VTermModifier mod = strpe_modifiers(&linep); |
| 679 |
vterm_mouse_move(vt, row, col, mod); |
| 680 |
} |
| 681 |
|
| 682 |
else if(strstartswith(line, "MOUSEBTN ")) { |
| 683 |
char *linep = line + 9; |
| 684 |
char press; |
| 685 |
int button, len; |
| 686 |
while(linep[0] == ' ') |
| 687 |
linep++; |
| 688 |
sscanf(linep, "%c %d%n", &press, &button, &len); |
| 689 |
linep += len; |
| 690 |
while(linep[0] == ' ') |
| 691 |
linep++; |
| 692 |
VTermModifier mod = strpe_modifiers(&linep); |
| 693 |
vterm_mouse_button(vt, button, (press == 'd' || press == 'D'), mod); |
| 694 |
} |
| 695 |
|
| 696 |
else if(strstartswith(line, "DAMAGEMERGE ")) { |
| 697 |
char *linep = line + 12; |
| 698 |
while(linep[0] == ' ') |
| 699 |
linep++; |
| 700 |
if(streq(linep, "CELL")) |
| 701 |
vterm_screen_set_damage_merge(screen, VTERM_DAMAGE_CELL); |
| 702 |
else if(streq(linep, "ROW")) |
| 703 |
vterm_screen_set_damage_merge(screen, VTERM_DAMAGE_ROW); |
| 704 |
else if(streq(linep, "SCREEN")) |
| 705 |
vterm_screen_set_damage_merge(screen, VTERM_DAMAGE_SCREEN); |
| 706 |
else if(streq(linep, "SCROLL")) |
| 707 |
vterm_screen_set_damage_merge(screen, VTERM_DAMAGE_SCROLL); |
| 708 |
} |
| 709 |
|
| 710 |
else if(strstartswith(line, "DAMAGEFLUSH")) { |
| 711 |
vterm_screen_flush_damage(screen); |
| 712 |
} |
| 713 |
|
| 714 |
else if(line[0] == '?') { |
| 715 |
if(streq(line, "?cursor")) { |
| 716 |
VTermPos pos; |
| 717 |
vterm_state_get_cursorpos(state, &pos); |
| 718 |
if(pos.row != state_pos.row) |
| 719 |
printf("! row mismatch: state=%d,%d event=%d,%d\n", |
| 720 |
pos.row, pos.col, state_pos.row, state_pos.col); |
| 721 |
else if(pos.col != state_pos.col) |
| 722 |
printf("! col mismatch: state=%d,%d event=%d,%d\n", |
| 723 |
pos.row, pos.col, state_pos.row, state_pos.col); |
| 724 |
else |
| 725 |
printf("%d,%d\n", state_pos.row, state_pos.col); |
| 726 |
} |
| 727 |
else if(strstartswith(line, "?pen ")) { |
| 728 |
char *linep = line + 5; |
| 729 |
while(linep[0] == ' ') |
| 730 |
linep++; |
| 731 |
|
| 732 |
VTermValue val; |
| 733 |
#define BOOLSTR(v) ((v) ? "on" : "off") |
| 734 |
|
| 735 |
if(streq(linep, "bold")) { |
| 736 |
vterm_state_get_penattr(state, VTERM_ATTR_BOLD, &val); |
| 737 |
if(val.boolean != state_pen.bold) |
| 738 |
printf("! pen bold mismatch; state=%s, event=%s\n", |
| 739 |
BOOLSTR(val.boolean), BOOLSTR(state_pen.bold)); |
| 740 |
else |
| 741 |
printf("%s\n", BOOLSTR(state_pen.bold)); |
| 742 |
} |
| 743 |
else if(streq(linep, "underline")) { |
| 744 |
vterm_state_get_penattr(state, VTERM_ATTR_UNDERLINE, &val); |
| 745 |
if(val.boolean != state_pen.underline) |
| 746 |
printf("! pen underline mismatch; state=%d, event=%d\n", |
| 747 |
val.boolean, state_pen.underline); |
| 748 |
else |
| 749 |
printf("%d\n", state_pen.underline); |
| 750 |
} |
| 751 |
else if(streq(linep, "italic")) { |
| 752 |
vterm_state_get_penattr(state, VTERM_ATTR_ITALIC, &val); |
| 753 |
if(val.boolean != state_pen.italic) |
| 754 |
printf("! pen italic mismatch; state=%s, event=%s\n", |
| 755 |
BOOLSTR(val.boolean), BOOLSTR(state_pen.italic)); |
| 756 |
else |
| 757 |
printf("%s\n", BOOLSTR(state_pen.italic)); |
| 758 |
} |
| 759 |
else if(streq(linep, "blink")) { |
| 760 |
vterm_state_get_penattr(state, VTERM_ATTR_BLINK, &val); |
| 761 |
if(val.boolean != state_pen.blink) |
| 762 |
printf("! pen blink mismatch; state=%s, event=%s\n", |
| 763 |
BOOLSTR(val.boolean), BOOLSTR(state_pen.blink)); |
| 764 |
else |
| 765 |
printf("%s\n", BOOLSTR(state_pen.blink)); |
| 766 |
} |
| 767 |
else if(streq(linep, "reverse")) { |
| 768 |
vterm_state_get_penattr(state, VTERM_ATTR_REVERSE, &val); |
| 769 |
if(val.boolean != state_pen.reverse) |
| 770 |
printf("! pen reverse mismatch; state=%s, event=%s\n", |
| 771 |
BOOLSTR(val.boolean), BOOLSTR(state_pen.reverse)); |
| 772 |
else |
| 773 |
printf("%s\n", BOOLSTR(state_pen.reverse)); |
| 774 |
} |
| 775 |
else if(streq(linep, "font")) { |
| 776 |
vterm_state_get_penattr(state, VTERM_ATTR_FONT, &val); |
| 777 |
if(val.boolean != state_pen.font) |
| 778 |
printf("! pen font mismatch; state=%d, event=%d\n", |
| 779 |
val.boolean, state_pen.font); |
| 780 |
else |
| 781 |
printf("%d\n", state_pen.font); |
| 782 |
} |
| 783 |
else if(streq(linep, "foreground")) { |
| 784 |
print_color(&state_pen.foreground); |
| 785 |
printf("\n"); |
| 786 |
} |
| 787 |
else if(streq(linep, "background")) { |
| 788 |
print_color(&state_pen.background); |
| 789 |
printf("\n"); |
| 790 |
} |
| 791 |
else |
| 792 |
printf("?\n"); |
| 793 |
} |
| 794 |
else if(strstartswith(line, "?screen_chars ")) { |
| 795 |
char *linep = line + 13; |
| 796 |
VTermRect rect; |
| 797 |
size_t len; |
| 798 |
while(linep[0] == ' ') |
| 799 |
linep++; |
| 800 |
if(sscanf(linep, "%d,%d,%d,%d", &rect.start_row, &rect.start_col, &rect.end_row, &rect.end_col) < 4) { |
| 801 |
printf("! screen_chars unrecognised input\n"); |
| 802 |
goto abort_line; |
| 803 |
} |
| 804 |
len = vterm_screen_get_chars(screen, NULL, 0, rect); |
| 805 |
if(len == (size_t)-1) |
| 806 |
printf("! screen_chars error\n"); |
| 807 |
else if(len == 0) |
| 808 |
printf("\n"); |
| 809 |
else { |
| 810 |
uint32_t *chars = malloc(sizeof(uint32_t) * len); |
| 811 |
vterm_screen_get_chars(screen, chars, len, rect); |
| 812 |
for(size_t i = 0; i < len; i++) { |
| 813 |
printf("0x%02x%s", chars[i], i < len-1 ? "," : "\n"); |
| 814 |
} |
| 815 |
free(chars); |
| 816 |
} |
| 817 |
} |
| 818 |
else if(strstartswith(line, "?screen_text ")) { |
| 819 |
char *linep = line + 12; |
| 820 |
VTermRect rect; |
| 821 |
size_t len; |
| 822 |
while(linep[0] == ' ') |
| 823 |
linep++; |
| 824 |
if(sscanf(linep, "%d,%d,%d,%d", &rect.start_row, &rect.start_col, &rect.end_row, &rect.end_col) < 4) { |
| 825 |
printf("! screen_text unrecognised input\n"); |
| 826 |
goto abort_line; |
| 827 |
} |
| 828 |
len = vterm_screen_get_text(screen, NULL, 0, rect); |
| 829 |
if(len == (size_t)-1) |
| 830 |
printf("! screen_text error\n"); |
| 831 |
else if(len == 0) |
| 832 |
printf("\n"); |
| 833 |
else { |
| 834 |
|
| 835 |
unsigned char *buffer = malloc(len + 4); |
| 836 |
unsigned char *text = buffer + 2; |
| 837 |
text[-2] = 0x55; text[-1] = 0xAA; |
| 838 |
text[len] = 0x55; text[len+1] = 0xAA; |
| 839 |
|
| 840 |
vterm_screen_get_text(screen, (char *)text, len, rect); |
| 841 |
|
| 842 |
if(text[-2] != 0x55 || text[-1] != 0xAA) |
| 843 |
printf("! screen_get_text buffer overrun left [%02x,%02x]\n", text[-2], text[-1]); |
| 844 |
else if(text[len] != 0x55 || text[len+1] != 0xAA) |
| 845 |
printf("! screen_get_text buffer overrun right [%02x,%02x]\n", text[len], text[len+1]); |
| 846 |
else |
| 847 |
for(size_t i = 0; i < len; i++) { |
| 848 |
printf("0x%02x%s", text[i], i < len-1 ? "," : "\n"); |
| 849 |
} |
| 850 |
|
| 851 |
free(buffer); |
| 852 |
} |
| 853 |
} |
| 854 |
else if(strstartswith(line, "?screen_cell ")) { |
| 855 |
char *linep = line + 12; |
| 856 |
VTermPos pos; |
| 857 |
while(linep[0] == ' ') |
| 858 |
linep++; |
| 859 |
if(sscanf(linep, "%d,%d\n", &pos.row, &pos.col) < 2) { |
| 860 |
printf("! screen_cell unrecognised input\n"); |
| 861 |
goto abort_line; |
| 862 |
} |
| 863 |
VTermScreenCell cell; |
| 864 |
if(!vterm_screen_get_cell(screen, pos, &cell)) |
| 865 |
goto abort_line; |
| 866 |
printf("{"); |
| 867 |
for(int i = 0; i < VTERM_MAX_CHARS_PER_CELL && cell.chars[i]; i++) { |
| 868 |
printf("%s0x%x", i ? "," : "", cell.chars[i]); |
| 869 |
} |
| 870 |
printf("} width=%d attrs={", cell.width); |
| 871 |
if(cell.attrs.bold) printf("B"); |
| 872 |
if(cell.attrs.underline) printf("U%d", cell.attrs.underline); |
| 873 |
if(cell.attrs.italic) printf("I"); |
| 874 |
if(cell.attrs.blink) printf("K"); |
| 875 |
if(cell.attrs.reverse) printf("R"); |
| 876 |
if(cell.attrs.font) printf("F%d", cell.attrs.font); |
| 877 |
printf("} "); |
| 878 |
if(cell.attrs.dwl) printf("dwl "); |
| 879 |
if(cell.attrs.dhl) printf("dhl-%s ", cell.attrs.dhl == 2 ? "bottom" : "top"); |
| 880 |
printf("fg="); |
| 881 |
vterm_screen_convert_color_to_rgb(screen, &cell.fg); |
| 882 |
print_color(&cell.fg); |
| 883 |
printf(" bg="); |
| 884 |
vterm_screen_convert_color_to_rgb(screen, &cell.bg); |
| 885 |
print_color(&cell.bg); |
| 886 |
printf("\n"); |
| 887 |
} |
| 888 |
else if(strstartswith(line, "?screen_eol ")) { |
| 889 |
char *linep = line + 12; |
| 890 |
while(linep[0] == ' ') |
| 891 |
linep++; |
| 892 |
VTermPos pos; |
| 893 |
if(sscanf(linep, "%d,%d\n", &pos.row, &pos.col) < 2) { |
| 894 |
printf("! screen_eol unrecognised input\n"); |
| 895 |
goto abort_line; |
| 896 |
} |
| 897 |
printf("%d\n", vterm_screen_is_eol(screen, pos)); |
| 898 |
} |
| 899 |
else if(strstartswith(line, "?screen_attrs_extent ")) { |
| 900 |
char *linep = line + 21; |
| 901 |
while(linep[0] == ' ') |
| 902 |
linep++; |
| 903 |
VTermPos pos; |
| 904 |
if(sscanf(linep, "%d,%d\n", &pos.row, &pos.col) < 2) { |
| 905 |
printf("! screen_attrs_extent unrecognised input\n"); |
| 906 |
goto abort_line; |
| 907 |
} |
| 908 |
VTermRect rect = { |
| 909 |
.start_col = 0, |
| 910 |
.end_col = -1, |
| 911 |
}; |
| 912 |
if(!vterm_screen_get_attrs_extent(screen, &rect, pos, ~0)) { |
| 913 |
printf("! screen_attrs_extent failed\n"); |
| 914 |
goto abort_line; |
| 915 |
} |
| 916 |
printf("%d,%d-%d,%d\n", rect.start_row, rect.start_col, rect.end_row, rect.end_col); |
| 917 |
} |
| 918 |
else |
| 919 |
printf("?\n"); |
| 920 |
|
| 921 |
memset(line, 0, sizeof line); |
| 922 |
continue; |
| 923 |
} |
| 924 |
|
| 925 |
else |
| 926 |
abort_line: err = 1; |
| 927 |
|
| 928 |
size_t outlen = vterm_output_get_buffer_current(vt); |
| 929 |
if(outlen > 0) { |
| 930 |
char outbuff[outlen]; |
| 931 |
vterm_output_read(vt, outbuff, outlen); |
| 932 |
|
| 933 |
term_output(outbuff, outlen, NULL); |
| 934 |
} |
| 935 |
|
| 936 |
printf(err ? "?\n" : "DONE\n"); |
| 937 |
} |
| 938 |
|
| 939 |
vterm_free(vt); |
| 940 |
|
| 941 |
return 0; |
| 942 |
} |