Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)
1/* Copyright (C) 2022-2025 mintsuki and contributors. 2 * 3 * Redistribution and use in source and binary forms, with or without 4 * modification, are permitted provided that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain the above copyright notice, 7 * this list of conditions and the following disclaimer. 8 * 9 * 2. Redistributions in binary form must reproduce the above copyright notice, 10 * this list of conditions and the following disclaimer in the documentation 11 * and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 17 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 23 * POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include <stdint.h> 27#include <stddef.h> 28#include <stdbool.h> 29 30#define FLANTERM_IN_FLANTERM 31 32#include "flanterm.h" 33 34// Tries to implement this standard for terminfo 35// https://man7.org/linux/man-pages/man4/console_codes.4.html 36 37static const uint32_t col256[] = { 38 0x000000, 0x00005f, 0x000087, 0x0000af, 0x0000d7, 0x0000ff, 0x005f00, 0x005f5f, 39 0x005f87, 0x005faf, 0x005fd7, 0x005fff, 0x008700, 0x00875f, 0x008787, 0x0087af, 40 0x0087d7, 0x0087ff, 0x00af00, 0x00af5f, 0x00af87, 0x00afaf, 0x00afd7, 0x00afff, 41 0x00d700, 0x00d75f, 0x00d787, 0x00d7af, 0x00d7d7, 0x00d7ff, 0x00ff00, 0x00ff5f, 42 0x00ff87, 0x00ffaf, 0x00ffd7, 0x00ffff, 0x5f0000, 0x5f005f, 0x5f0087, 0x5f00af, 43 0x5f00d7, 0x5f00ff, 0x5f5f00, 0x5f5f5f, 0x5f5f87, 0x5f5faf, 0x5f5fd7, 0x5f5fff, 44 0x5f8700, 0x5f875f, 0x5f8787, 0x5f87af, 0x5f87d7, 0x5f87ff, 0x5faf00, 0x5faf5f, 45 0x5faf87, 0x5fafaf, 0x5fafd7, 0x5fafff, 0x5fd700, 0x5fd75f, 0x5fd787, 0x5fd7af, 46 0x5fd7d7, 0x5fd7ff, 0x5fff00, 0x5fff5f, 0x5fff87, 0x5fffaf, 0x5fffd7, 0x5fffff, 47 0x870000, 0x87005f, 0x870087, 0x8700af, 0x8700d7, 0x8700ff, 0x875f00, 0x875f5f, 48 0x875f87, 0x875faf, 0x875fd7, 0x875fff, 0x878700, 0x87875f, 0x878787, 0x8787af, 49 0x8787d7, 0x8787ff, 0x87af00, 0x87af5f, 0x87af87, 0x87afaf, 0x87afd7, 0x87afff, 50 0x87d700, 0x87d75f, 0x87d787, 0x87d7af, 0x87d7d7, 0x87d7ff, 0x87ff00, 0x87ff5f, 51 0x87ff87, 0x87ffaf, 0x87ffd7, 0x87ffff, 0xaf0000, 0xaf005f, 0xaf0087, 0xaf00af, 52 0xaf00d7, 0xaf00ff, 0xaf5f00, 0xaf5f5f, 0xaf5f87, 0xaf5faf, 0xaf5fd7, 0xaf5fff, 53 0xaf8700, 0xaf875f, 0xaf8787, 0xaf87af, 0xaf87d7, 0xaf87ff, 0xafaf00, 0xafaf5f, 54 0xafaf87, 0xafafaf, 0xafafd7, 0xafafff, 0xafd700, 0xafd75f, 0xafd787, 0xafd7af, 55 0xafd7d7, 0xafd7ff, 0xafff00, 0xafff5f, 0xafff87, 0xafffaf, 0xafffd7, 0xafffff, 56 0xd70000, 0xd7005f, 0xd70087, 0xd700af, 0xd700d7, 0xd700ff, 0xd75f00, 0xd75f5f, 57 0xd75f87, 0xd75faf, 0xd75fd7, 0xd75fff, 0xd78700, 0xd7875f, 0xd78787, 0xd787af, 58 0xd787d7, 0xd787ff, 0xd7af00, 0xd7af5f, 0xd7af87, 0xd7afaf, 0xd7afd7, 0xd7afff, 59 0xd7d700, 0xd7d75f, 0xd7d787, 0xd7d7af, 0xd7d7d7, 0xd7d7ff, 0xd7ff00, 0xd7ff5f, 60 0xd7ff87, 0xd7ffaf, 0xd7ffd7, 0xd7ffff, 0xff0000, 0xff005f, 0xff0087, 0xff00af, 61 0xff00d7, 0xff00ff, 0xff5f00, 0xff5f5f, 0xff5f87, 0xff5faf, 0xff5fd7, 0xff5fff, 62 0xff8700, 0xff875f, 0xff8787, 0xff87af, 0xff87d7, 0xff87ff, 0xffaf00, 0xffaf5f, 63 0xffaf87, 0xffafaf, 0xffafd7, 0xffafff, 0xffd700, 0xffd75f, 0xffd787, 0xffd7af, 64 0xffd7d7, 0xffd7ff, 0xffff00, 0xffff5f, 0xffff87, 0xffffaf, 0xffffd7, 0xffffff, 65 0x080808, 0x121212, 0x1c1c1c, 0x262626, 0x303030, 0x3a3a3a, 0x444444, 0x4e4e4e, 66 0x585858, 0x626262, 0x6c6c6c, 0x767676, 0x808080, 0x8a8a8a, 0x949494, 0x9e9e9e, 67 0xa8a8a8, 0xb2b2b2, 0xbcbcbc, 0xc6c6c6, 0xd0d0d0, 0xdadada, 0xe4e4e4, 0xeeeeee 68}; 69 70#define CHARSET_DEFAULT 0 71#define CHARSET_DEC_SPECIAL 1 72 73void flanterm_context_reinit(struct flanterm_context *ctx) { 74 ctx->tab_size = 8; 75 ctx->autoflush = true; 76 ctx->cursor_enabled = true; 77 ctx->scroll_enabled = true; 78 ctx->control_sequence = false; 79 ctx->escape = false; 80 ctx->osc = false; 81 ctx->osc_escape = false; 82 ctx->rrr = false; 83 ctx->discard_next = false; 84 ctx->bold = false; 85 ctx->bg_bold = false; 86 ctx->reverse_video = false; 87 ctx->dec_private = false; 88 ctx->insert_mode = false; 89 ctx->unicode_remaining = 0; 90 ctx->g_select = 0; 91 ctx->charsets[0] = CHARSET_DEFAULT; 92 ctx->charsets[1] = CHARSET_DEC_SPECIAL; 93 ctx->current_charset = 0; 94 ctx->escape_offset = 0; 95 ctx->esc_values_i = 0; 96 ctx->saved_cursor_x = 0; 97 ctx->saved_cursor_y = 0; 98 ctx->current_primary = (size_t)-1; 99 ctx->current_bg = (size_t)-1; 100 ctx->scroll_top_margin = 0; 101 ctx->scroll_bottom_margin = ctx->rows; 102 ctx->oob_output = FLANTERM_OOB_OUTPUT_ONLCR; 103} 104 105static void flanterm_putchar(struct flanterm_context *ctx, uint8_t c); 106 107void flanterm_write(struct flanterm_context *ctx, const char *buf, size_t count) { 108 for (size_t i = 0; i < count; i++) { 109 flanterm_putchar(ctx, buf[i]); 110 } 111 112 if (ctx->autoflush) { 113 ctx->double_buffer_flush(ctx); 114 } 115} 116 117static void sgr(struct flanterm_context *ctx) { 118 size_t i = 0; 119 120 if (!ctx->esc_values_i) 121 goto def; 122 123 for (; i < ctx->esc_values_i; i++) { 124 size_t offset; 125 126 if (ctx->esc_values[i] == 0) { 127def: 128 if (ctx->reverse_video) { 129 ctx->reverse_video = false; 130 ctx->swap_palette(ctx); 131 } 132 ctx->bold = false; 133 ctx->bg_bold = false; 134 ctx->current_primary = (size_t)-1; 135 ctx->current_bg = (size_t)-1; 136 ctx->set_text_bg_default(ctx); 137 ctx->set_text_fg_default(ctx); 138 continue; 139 } 140 141 else if (ctx->esc_values[i] == 1) { 142 ctx->bold = true; 143 if (ctx->current_primary != (size_t)-1) { 144 if (!ctx->reverse_video) { 145 ctx->set_text_fg_bright(ctx, ctx->current_primary); 146 } else { 147 ctx->set_text_bg_bright(ctx, ctx->current_primary); 148 } 149 } else { 150 if (!ctx->reverse_video) { 151 ctx->set_text_fg_default_bright(ctx); 152 } else { 153 ctx->set_text_bg_default_bright(ctx); 154 } 155 } 156 continue; 157 } 158 159 else if (ctx->esc_values[i] == 5) { 160 ctx->bg_bold = true; 161 if (ctx->current_bg != (size_t)-1) { 162 if (!ctx->reverse_video) { 163 ctx->set_text_bg_bright(ctx, ctx->current_bg); 164 } else { 165 ctx->set_text_fg_bright(ctx, ctx->current_bg); 166 } 167 } else { 168 if (!ctx->reverse_video) { 169 ctx->set_text_bg_default_bright(ctx); 170 } else { 171 ctx->set_text_fg_default_bright(ctx); 172 } 173 } 174 continue; 175 } 176 177 else if (ctx->esc_values[i] == 22) { 178 ctx->bold = false; 179 if (ctx->current_primary != (size_t)-1) { 180 if (!ctx->reverse_video) { 181 ctx->set_text_fg(ctx, ctx->current_primary); 182 } else { 183 ctx->set_text_bg(ctx, ctx->current_primary); 184 } 185 } else { 186 if (!ctx->reverse_video) { 187 ctx->set_text_fg_default(ctx); 188 } else { 189 ctx->set_text_bg_default(ctx); 190 } 191 } 192 continue; 193 } 194 195 else if (ctx->esc_values[i] == 25) { 196 ctx->bg_bold = false; 197 if (ctx->current_bg != (size_t)-1) { 198 if (!ctx->reverse_video) { 199 ctx->set_text_bg(ctx, ctx->current_bg); 200 } else { 201 ctx->set_text_fg(ctx, ctx->current_bg); 202 } 203 } else { 204 if (!ctx->reverse_video) { 205 ctx->set_text_bg_default(ctx); 206 } else { 207 ctx->set_text_fg_default(ctx); 208 } 209 } 210 continue; 211 } 212 213 else if (ctx->esc_values[i] >= 30 && ctx->esc_values[i] <= 37) { 214 offset = 30; 215 ctx->current_primary = ctx->esc_values[i] - offset; 216 217 if (ctx->reverse_video) { 218 goto set_bg; 219 } 220 221set_fg: 222 if ((ctx->bold && !ctx->reverse_video) 223 || (ctx->bg_bold && ctx->reverse_video)) { 224 ctx->set_text_fg_bright(ctx, ctx->esc_values[i] - offset); 225 } else { 226 ctx->set_text_fg(ctx, ctx->esc_values[i] - offset); 227 } 228 continue; 229 } 230 231 else if (ctx->esc_values[i] >= 40 && ctx->esc_values[i] <= 47) { 232 offset = 40; 233 ctx->current_bg = ctx->esc_values[i] - offset; 234 235 if (ctx->reverse_video) { 236 goto set_fg; 237 } 238 239set_bg: 240 if ((ctx->bold && ctx->reverse_video) 241 || (ctx->bg_bold && !ctx->reverse_video)) { 242 ctx->set_text_bg_bright(ctx, ctx->esc_values[i] - offset); 243 } else { 244 ctx->set_text_bg(ctx, ctx->esc_values[i] - offset); 245 } 246 continue; 247 } 248 249 else if (ctx->esc_values[i] >= 90 && ctx->esc_values[i] <= 97) { 250 offset = 90; 251 ctx->current_primary = ctx->esc_values[i] - offset; 252 253 if (ctx->reverse_video) { 254 goto set_bg_bright; 255 } 256 257set_fg_bright: 258 ctx->set_text_fg_bright(ctx, ctx->esc_values[i] - offset); 259 continue; 260 } 261 262 else if (ctx->esc_values[i] >= 100 && ctx->esc_values[i] <= 107) { 263 offset = 100; 264 ctx->current_bg = ctx->esc_values[i] - offset; 265 266 if (ctx->reverse_video) { 267 goto set_fg_bright; 268 } 269 270set_bg_bright: 271 ctx->set_text_bg_bright(ctx, ctx->esc_values[i] - offset); 272 continue; 273 } 274 275 else if (ctx->esc_values[i] == 39) { 276 ctx->current_primary = (size_t)-1; 277 278 if (ctx->reverse_video) { 279 ctx->swap_palette(ctx); 280 } 281 282 if (!ctx->bold) { 283 ctx->set_text_fg_default(ctx); 284 } else { 285 ctx->set_text_fg_default_bright(ctx); 286 } 287 288 if (ctx->reverse_video) { 289 ctx->swap_palette(ctx); 290 } 291 292 continue; 293 } 294 295 else if (ctx->esc_values[i] == 49) { 296 ctx->current_bg = (size_t)-1; 297 298 if (ctx->reverse_video) { 299 ctx->swap_palette(ctx); 300 } 301 302 if (!ctx->bg_bold) { 303 ctx->set_text_bg_default(ctx); 304 } else { 305 ctx->set_text_bg_default_bright(ctx); 306 } 307 308 if (ctx->reverse_video) { 309 ctx->swap_palette(ctx); 310 } 311 312 continue; 313 } 314 315 else if (ctx->esc_values[i] == 7) { 316 if (!ctx->reverse_video) { 317 ctx->reverse_video = true; 318 ctx->swap_palette(ctx); 319 } 320 continue; 321 } 322 323 else if (ctx->esc_values[i] == 27) { 324 if (ctx->reverse_video) { 325 ctx->reverse_video = false; 326 ctx->swap_palette(ctx); 327 } 328 continue; 329 } 330 331 // 256/RGB 332 else if (ctx->esc_values[i] == 38 || ctx->esc_values[i] == 48) { 333 bool fg = ctx->esc_values[i] == 38; 334 335 i++; 336 if (i >= ctx->esc_values_i) { 337 break; 338 } 339 340 switch (ctx->esc_values[i]) { 341 case 2: { // RGB 342 if (i + 3 >= ctx->esc_values_i) { 343 goto out; 344 } 345 346 uint32_t rgb_value = 0; 347 348 rgb_value |= ctx->esc_values[i + 1] << 16; 349 rgb_value |= ctx->esc_values[i + 2] << 8; 350 rgb_value |= ctx->esc_values[i + 3]; 351 352 i += 3; 353 354 (fg ? ctx->set_text_fg_rgb : ctx->set_text_bg_rgb)(ctx, rgb_value); 355 356 break; 357 } 358 case 5: { // 256 colors 359 if (i + 1 >= ctx->esc_values_i) { 360 goto out; 361 } 362 363 uint32_t col = ctx->esc_values[i + 1]; 364 365 i++; 366 367 if (col < 8) { 368 (fg ? ctx->set_text_fg : ctx->set_text_bg)(ctx, col); 369 } else if (col < 16) { 370 (fg ? ctx->set_text_fg_bright : ctx->set_text_bg_bright)(ctx, col - 8); 371 } else if (col < 256) { 372 uint32_t rgb_value = col256[col - 16]; 373 (fg ? ctx->set_text_fg_rgb : ctx->set_text_bg_rgb)(ctx, rgb_value); 374 } 375 376 break; 377 } 378 default: continue; 379 } 380 } 381 } 382 383out:; 384} 385 386static void dec_private_parse(struct flanterm_context *ctx, uint8_t c) { 387 ctx->dec_private = false; 388 389 if (ctx->esc_values_i == 0) { 390 return; 391 } 392 393 bool set; 394 395 switch (c) { 396 case 'h': 397 set = true; break; 398 case 'l': 399 set = false; break; 400 default: 401 return; 402 } 403 404 switch (ctx->esc_values[0]) { 405 case 25: { 406 if (set) { 407 ctx->cursor_enabled = true; 408 } else { 409 ctx->cursor_enabled = false; 410 } 411 return; 412 } 413 } 414 415 if (ctx->callback != NULL) { 416 ctx->callback(ctx, FLANTERM_CB_DEC, ctx->esc_values_i, (uintptr_t)ctx->esc_values, c); 417 } 418} 419 420static void linux_private_parse(struct flanterm_context *ctx) { 421 if (ctx->esc_values_i == 0) { 422 return; 423 } 424 425 if (ctx->callback != NULL) { 426 ctx->callback(ctx, FLANTERM_CB_LINUX, ctx->esc_values_i, (uintptr_t)ctx->esc_values, 0); 427 } 428} 429 430static void mode_toggle(struct flanterm_context *ctx, uint8_t c) { 431 if (ctx->esc_values_i == 0) { 432 return; 433 } 434 435 bool set; 436 437 switch (c) { 438 case 'h': 439 set = true; break; 440 case 'l': 441 set = false; break; 442 default: 443 return; 444 } 445 446 switch (ctx->esc_values[0]) { 447 case 4: 448 ctx->insert_mode = set; return; 449 } 450 451 if (ctx->callback != NULL) { 452 ctx->callback(ctx, FLANTERM_CB_MODE, ctx->esc_values_i, (uintptr_t)ctx->esc_values, c); 453 } 454} 455 456static void osc_parse(struct flanterm_context *ctx, uint8_t c) { 457 if (ctx->osc_escape && c == '\\') { 458 goto cleanup; 459 } 460 461 ctx->osc_escape = false; 462 463 switch (c) { 464 case 0x1b: 465 ctx->osc_escape = true; 466 return; 467 case '\a': 468 default: 469 break; 470 } 471 472cleanup: 473 ctx->osc_escape = false; 474 ctx->osc = false; 475 ctx->escape = false; 476} 477 478static void control_sequence_parse(struct flanterm_context *ctx, uint8_t c) { 479 if (ctx->escape_offset == 2) { 480 switch (c) { 481 case '[': 482 ctx->discard_next = true; 483 goto cleanup; 484 case '?': 485 ctx->dec_private = true; 486 return; 487 } 488 } 489 490 if (c >= '0' && c <= '9') { 491 if (ctx->esc_values_i == FLANTERM_MAX_ESC_VALUES) { 492 return; 493 } 494 ctx->rrr = true; 495 ctx->esc_values[ctx->esc_values_i] *= 10; 496 ctx->esc_values[ctx->esc_values_i] += c - '0'; 497 return; 498 } 499 500 if (ctx->rrr == true) { 501 ctx->esc_values_i++; 502 ctx->rrr = false; 503 if (c == ';') 504 return; 505 } else if (c == ';') { 506 if (ctx->esc_values_i == FLANTERM_MAX_ESC_VALUES) { 507 return; 508 } 509 ctx->esc_values[ctx->esc_values_i] = 0; 510 ctx->esc_values_i++; 511 return; 512 } 513 514 size_t esc_default; 515 switch (c) { 516 case 'J': case 'K': case 'q': 517 esc_default = 0; break; 518 default: 519 esc_default = 1; break; 520 } 521 522 for (size_t i = ctx->esc_values_i; i < FLANTERM_MAX_ESC_VALUES; i++) { 523 ctx->esc_values[i] = esc_default; 524 } 525 526 if (ctx->dec_private == true) { 527 dec_private_parse(ctx, c); 528 goto cleanup; 529 } 530 531 bool r = ctx->scroll_enabled; 532 ctx->scroll_enabled = false; 533 size_t x, y; 534 ctx->get_cursor_pos(ctx, &x, &y); 535 536 switch (c) { 537 case 'F': 538 x = 0; 539 // FALLTHRU 540 case 'A': { 541 if (ctx->esc_values[0] > y) 542 ctx->esc_values[0] = y; 543 size_t orig_y = y; 544 size_t dest_y = y - ctx->esc_values[0]; 545 bool will_be_in_scroll_region = false; 546 if ((ctx->scroll_top_margin >= dest_y && ctx->scroll_top_margin <= orig_y) 547 || (ctx->scroll_bottom_margin >= dest_y && ctx->scroll_bottom_margin <= orig_y)) { 548 will_be_in_scroll_region = true; 549 } 550 if (will_be_in_scroll_region && dest_y < ctx->scroll_top_margin) { 551 dest_y = ctx->scroll_top_margin; 552 } 553 ctx->set_cursor_pos(ctx, x, dest_y); 554 break; 555 } 556 case 'E': 557 x = 0; 558 // FALLTHRU 559 case 'e': 560 case 'B': { 561 if (y + ctx->esc_values[0] > ctx->rows - 1) 562 ctx->esc_values[0] = (ctx->rows - 1) - y; 563 size_t orig_y = y; 564 size_t dest_y = y + ctx->esc_values[0]; 565 bool will_be_in_scroll_region = false; 566 if ((ctx->scroll_top_margin >= orig_y && ctx->scroll_top_margin <= dest_y) 567 || (ctx->scroll_bottom_margin >= orig_y && ctx->scroll_bottom_margin <= dest_y)) { 568 will_be_in_scroll_region = true; 569 } 570 if (will_be_in_scroll_region && dest_y >= ctx->scroll_bottom_margin) { 571 dest_y = ctx->scroll_bottom_margin - 1; 572 } 573 ctx->set_cursor_pos(ctx, x, dest_y); 574 break; 575 } 576 case 'a': 577 case 'C': 578 if (x + ctx->esc_values[0] > ctx->cols - 1) 579 ctx->esc_values[0] = (ctx->cols - 1) - x; 580 ctx->set_cursor_pos(ctx, x + ctx->esc_values[0], y); 581 break; 582 case 'D': 583 if (ctx->esc_values[0] > x) 584 ctx->esc_values[0] = x; 585 ctx->set_cursor_pos(ctx, x - ctx->esc_values[0], y); 586 break; 587 case 'c': 588 if (ctx->callback != NULL) { 589 ctx->callback(ctx, FLANTERM_CB_PRIVATE_ID, 0, 0, 0); 590 } 591 break; 592 case 'd': 593 ctx->esc_values[0] -= 1; 594 if (ctx->esc_values[0] >= ctx->rows) 595 ctx->esc_values[0] = ctx->rows - 1; 596 ctx->set_cursor_pos(ctx, x, ctx->esc_values[0]); 597 break; 598 case 'G': 599 case '`': 600 ctx->esc_values[0] -= 1; 601 if (ctx->esc_values[0] >= ctx->cols) 602 ctx->esc_values[0] = ctx->cols - 1; 603 ctx->set_cursor_pos(ctx, ctx->esc_values[0], y); 604 break; 605 case 'H': 606 case 'f': 607 if (ctx->esc_values[0] != 0) { 608 ctx->esc_values[0]--; 609 } 610 if (ctx->esc_values[1] != 0) { 611 ctx->esc_values[1]--; 612 } 613 if (ctx->esc_values[1] >= ctx->cols) 614 ctx->esc_values[1] = ctx->cols - 1; 615 if (ctx->esc_values[0] >= ctx->rows) 616 ctx->esc_values[0] = ctx->rows - 1; 617 ctx->set_cursor_pos(ctx, ctx->esc_values[1], ctx->esc_values[0]); 618 break; 619 case 'M': { 620 size_t count = ctx->esc_values[0] > ctx->rows ? ctx->rows : ctx->esc_values[0]; 621 for (size_t i = 0; i < count; i++) { 622 ctx->scroll(ctx); 623 } 624 break; 625 } 626 case 'L': { 627 size_t old_scroll_top_margin = ctx->scroll_top_margin; 628 ctx->scroll_top_margin = y; 629 size_t count = ctx->esc_values[0] > ctx->rows ? ctx->rows : ctx->esc_values[0]; 630 for (size_t i = 0; i < count; i++) { 631 ctx->revscroll(ctx); 632 } 633 ctx->scroll_top_margin = old_scroll_top_margin; 634 break; 635 } 636 case 'n': 637 switch (ctx->esc_values[0]) { 638 case 5: 639 if (ctx->callback != NULL) { 640 ctx->callback(ctx, FLANTERM_CB_STATUS_REPORT, 0, 0, 0); 641 } 642 break; 643 case 6: 644 if (ctx->callback != NULL) { 645 ctx->callback(ctx, FLANTERM_CB_POS_REPORT, x + 1, y + 1, 0); 646 } 647 break; 648 } 649 break; 650 case 'q': 651 if (ctx->callback != NULL) { 652 ctx->callback(ctx, FLANTERM_CB_KBD_LEDS, ctx->esc_values[0], 0, 0); 653 } 654 break; 655 case 'J': 656 switch (ctx->esc_values[0]) { 657 case 0: { 658 size_t rows_remaining = ctx->rows - (y + 1); 659 size_t cols_diff = ctx->cols - (x + 1); 660 size_t to_clear = rows_remaining * ctx->cols + cols_diff + 1; 661 for (size_t i = 0; i < to_clear; i++) { 662 ctx->raw_putchar(ctx, ' '); 663 } 664 ctx->set_cursor_pos(ctx, x, y); 665 break; 666 } 667 case 1: { 668 ctx->set_cursor_pos(ctx, 0, 0); 669 bool b = false; 670 for (size_t yc = 0; yc < ctx->rows; yc++) { 671 for (size_t xc = 0; xc < ctx->cols; xc++) { 672 ctx->raw_putchar(ctx, ' '); 673 if (xc == x && yc == y) { 674 ctx->set_cursor_pos(ctx, x, y); 675 b = true; 676 break; 677 } 678 } 679 if (b == true) 680 break; 681 } 682 break; 683 } 684 case 2: 685 case 3: 686 ctx->clear(ctx, false); 687 break; 688 } 689 break; 690 case '@': 691 for (size_t i = ctx->cols - 1; ; i--) { 692 ctx->move_character(ctx, i + ctx->esc_values[0], y, i, y); 693 ctx->set_cursor_pos(ctx, i, y); 694 ctx->raw_putchar(ctx, ' '); 695 if (i == x) { 696 break; 697 } 698 } 699 ctx->set_cursor_pos(ctx, x, y); 700 break; 701 case 'P': 702 for (size_t i = x + ctx->esc_values[0]; i < ctx->cols; i++) 703 ctx->move_character(ctx, i - ctx->esc_values[0], y, i, y); 704 ctx->set_cursor_pos(ctx, ctx->cols - ctx->esc_values[0], y); 705 // FALLTHRU 706 case 'X': { 707 size_t count = ctx->esc_values[0] > ctx->cols ? ctx->cols : ctx->esc_values[0]; 708 for (size_t i = 0; i < count; i++) 709 ctx->raw_putchar(ctx, ' '); 710 ctx->set_cursor_pos(ctx, x, y); 711 break; 712 } 713 case 'm': 714 sgr(ctx); 715 break; 716 case 's': 717 ctx->get_cursor_pos(ctx, &ctx->saved_cursor_x, &ctx->saved_cursor_y); 718 break; 719 case 'u': 720 ctx->set_cursor_pos(ctx, ctx->saved_cursor_x, ctx->saved_cursor_y); 721 break; 722 case 'K': 723 switch (ctx->esc_values[0]) { 724 case 0: { 725 for (size_t i = x; i < ctx->cols; i++) 726 ctx->raw_putchar(ctx, ' '); 727 ctx->set_cursor_pos(ctx, x, y); 728 break; 729 } 730 case 1: { 731 ctx->set_cursor_pos(ctx, 0, y); 732 for (size_t i = 0; i < x; i++) 733 ctx->raw_putchar(ctx, ' '); 734 break; 735 } 736 case 2: { 737 ctx->set_cursor_pos(ctx, 0, y); 738 for (size_t i = 0; i < ctx->cols; i++) 739 ctx->raw_putchar(ctx, ' '); 740 ctx->set_cursor_pos(ctx, x, y); 741 break; 742 } 743 } 744 break; 745 case 'r': 746 if (ctx->esc_values[0] == 0) { 747 ctx->esc_values[0] = 1; 748 } 749 if (ctx->esc_values[1] == 0) { 750 ctx->esc_values[1] = 1; 751 } 752 ctx->scroll_top_margin = 0; 753 ctx->scroll_bottom_margin = ctx->rows; 754 if (ctx->esc_values_i > 0) { 755 ctx->scroll_top_margin = ctx->esc_values[0] - 1; 756 } 757 if (ctx->esc_values_i > 1) { 758 ctx->scroll_bottom_margin = ctx->esc_values[1]; 759 } 760 if (ctx->scroll_top_margin >= ctx->rows 761 || ctx->scroll_bottom_margin > ctx->rows 762 || ctx->scroll_top_margin >= (ctx->scroll_bottom_margin - 1)) { 763 ctx->scroll_top_margin = 0; 764 ctx->scroll_bottom_margin = ctx->rows; 765 } 766 ctx->set_cursor_pos(ctx, 0, 0); 767 break; 768 case 'l': 769 case 'h': 770 mode_toggle(ctx, c); 771 break; 772 case ']': 773 linux_private_parse(ctx); 774 break; 775 } 776 777 ctx->scroll_enabled = r; 778 779cleanup: 780 ctx->control_sequence = false; 781 ctx->escape = false; 782} 783 784static void restore_state(struct flanterm_context *ctx) { 785 ctx->bold = ctx->saved_state_bold; 786 ctx->bg_bold = ctx->saved_state_bg_bold; 787 ctx->reverse_video = ctx->saved_state_reverse_video; 788 ctx->current_charset = ctx->saved_state_current_charset; 789 ctx->current_primary = ctx->saved_state_current_primary; 790 ctx->current_bg = ctx->saved_state_current_bg; 791 792 ctx->restore_state(ctx); 793} 794 795static void save_state(struct flanterm_context *ctx) { 796 ctx->save_state(ctx); 797 798 ctx->saved_state_bold = ctx->bold; 799 ctx->saved_state_bg_bold = ctx->bg_bold; 800 ctx->saved_state_reverse_video = ctx->reverse_video; 801 ctx->saved_state_current_charset = ctx->current_charset; 802 ctx->saved_state_current_primary = ctx->current_primary; 803 ctx->saved_state_current_bg = ctx->current_bg; 804} 805 806static void escape_parse(struct flanterm_context *ctx, uint8_t c) { 807 ctx->escape_offset++; 808 809 if (ctx->osc == true) { 810 osc_parse(ctx, c); 811 return; 812 } 813 814 if (ctx->control_sequence == true) { 815 control_sequence_parse(ctx, c); 816 return; 817 } 818 819 size_t x, y; 820 ctx->get_cursor_pos(ctx, &x, &y); 821 822 switch (c) { 823 case ']': 824 ctx->osc_escape = false; 825 ctx->osc = true; 826 return; 827 case '[': 828 for (size_t i = 0; i < FLANTERM_MAX_ESC_VALUES; i++) 829 ctx->esc_values[i] = 0; 830 ctx->esc_values_i = 0; 831 ctx->rrr = false; 832 ctx->control_sequence = true; 833 return; 834 case '7': 835 save_state(ctx); 836 break; 837 case '8': 838 restore_state(ctx); 839 break; 840 case 'c': 841 flanterm_context_reinit(ctx); 842 ctx->clear(ctx, true); 843 break; 844 case 'D': 845 if (y == ctx->scroll_bottom_margin - 1) { 846 ctx->scroll(ctx); 847 ctx->set_cursor_pos(ctx, x, y); 848 } else { 849 ctx->set_cursor_pos(ctx, x, y + 1); 850 } 851 break; 852 case 'E': 853 if (y == ctx->scroll_bottom_margin - 1) { 854 ctx->scroll(ctx); 855 ctx->set_cursor_pos(ctx, 0, y); 856 } else { 857 ctx->set_cursor_pos(ctx, 0, y + 1); 858 } 859 break; 860 case 'M': 861 // "Reverse linefeed" 862 if (y == ctx->scroll_top_margin) { 863 ctx->revscroll(ctx); 864 ctx->set_cursor_pos(ctx, 0, y); 865 } else { 866 ctx->set_cursor_pos(ctx, 0, y - 1); 867 } 868 break; 869 case 'Z': 870 if (ctx->callback != NULL) { 871 ctx->callback(ctx, FLANTERM_CB_PRIVATE_ID, 0, 0, 0); 872 } 873 break; 874 case '(': 875 case ')': 876 ctx->g_select = c - '\''; 877 break; 878 } 879 880 ctx->escape = false; 881} 882 883static bool dec_special_print(struct flanterm_context *ctx, uint8_t c) { 884#define FLANTERM_DEC_SPCL_PRN(C) ctx->raw_putchar(ctx, (C)); return true; 885 switch (c) { 886 case '`': FLANTERM_DEC_SPCL_PRN(0x04) 887 case '0': FLANTERM_DEC_SPCL_PRN(0xdb) 888 case '-': FLANTERM_DEC_SPCL_PRN(0x18) 889 case ',': FLANTERM_DEC_SPCL_PRN(0x1b) 890 case '.': FLANTERM_DEC_SPCL_PRN(0x19) 891 case 'a': FLANTERM_DEC_SPCL_PRN(0xb1) 892 case 'f': FLANTERM_DEC_SPCL_PRN(0xf8) 893 case 'g': FLANTERM_DEC_SPCL_PRN(0xf1) 894 case 'h': FLANTERM_DEC_SPCL_PRN(0xb0) 895 case 'j': FLANTERM_DEC_SPCL_PRN(0xd9) 896 case 'k': FLANTERM_DEC_SPCL_PRN(0xbf) 897 case 'l': FLANTERM_DEC_SPCL_PRN(0xda) 898 case 'm': FLANTERM_DEC_SPCL_PRN(0xc0) 899 case 'n': FLANTERM_DEC_SPCL_PRN(0xc5) 900 case 'q': FLANTERM_DEC_SPCL_PRN(0xc4) 901 case 's': FLANTERM_DEC_SPCL_PRN(0x5f) 902 case 't': FLANTERM_DEC_SPCL_PRN(0xc3) 903 case 'u': FLANTERM_DEC_SPCL_PRN(0xb4) 904 case 'v': FLANTERM_DEC_SPCL_PRN(0xc1) 905 case 'w': FLANTERM_DEC_SPCL_PRN(0xc2) 906 case 'x': FLANTERM_DEC_SPCL_PRN(0xb3) 907 case 'y': FLANTERM_DEC_SPCL_PRN(0xf3) 908 case 'z': FLANTERM_DEC_SPCL_PRN(0xf2) 909 case '~': FLANTERM_DEC_SPCL_PRN(0xfa) 910 case '_': FLANTERM_DEC_SPCL_PRN(0xff) 911 case '+': FLANTERM_DEC_SPCL_PRN(0x1a) 912 case '{': FLANTERM_DEC_SPCL_PRN(0xe3) 913 case '}': FLANTERM_DEC_SPCL_PRN(0x9c) 914 } 915#undef FLANTERM_DEC_SPCL_PRN 916 917 return false; 918} 919 920// Following wcwidth related code inherited from: 921// https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c 922 923struct interval { 924 uint32_t first; 925 uint32_t last; 926}; 927 928/* auxiliary function for binary search in interval table */ 929static int bisearch(uint32_t ucs, const struct interval *table, int max) { 930 int min = 0; 931 int mid; 932 933 if (ucs < table[0].first || ucs > table[max].last) 934 return 0; 935 while (max >= min) { 936 mid = (min + max) / 2; 937 if (ucs > table[mid].last) 938 min = mid + 1; 939 else if (ucs < table[mid].first) 940 max = mid - 1; 941 else 942 return 1; 943 } 944 945 return 0; 946} 947 948int mk_wcwidth(uint32_t ucs) { 949 /* sorted list of non-overlapping intervals of non-spacing characters */ 950 /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */ 951 static const struct interval combining[] = { 952 { 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 }, 953 { 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 }, 954 { 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 }, 955 { 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 }, 956 { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED }, 957 { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A }, 958 { 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 }, 959 { 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D }, 960 { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 }, 961 { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, 962 { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C }, 963 { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, 964 { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, 965 { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, 966 { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C }, 967 { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D }, 968 { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 }, 969 { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 }, 970 { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC }, 971 { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD }, 972 { 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, 973 { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, 974 { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, 975 { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, 976 { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, 977 { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, 978 { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, 979 { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, 980 { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 }, 981 { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F }, 982 { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 }, 983 { 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD }, 984 { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD }, 985 { 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 }, 986 { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B }, 987 { 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 }, 988 { 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 }, 989 { 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF }, 990 { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 }, 991 { 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F }, 992 { 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B }, 993 { 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F }, 994 { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB }, 995 { 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F }, 996 { 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 }, 997 { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD }, 998 { 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F }, 999 { 0xE0100, 0xE01EF } 1000 }; 1001 1002 /* test for 8-bit control characters */ 1003 if (ucs == 0) 1004 return 0; 1005 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) 1006 return 1; 1007 1008 /* binary search in table of non-spacing characters */ 1009 if (bisearch(ucs, combining, 1010 sizeof(combining) / sizeof(struct interval) - 1)) 1011 return 0; 1012 1013 /* if we arrive here, ucs is not a combining or C0/C1 control character */ 1014 1015 return 1 + 1016 (ucs >= 0x1100 && 1017 (ucs <= 0x115f || /* Hangul Jamo init. consonants */ 1018 ucs == 0x2329 || ucs == 0x232a || 1019 (ucs >= 0x2e80 && ucs <= 0xa4cf && 1020 ucs != 0x303f) || /* CJK ... Yi */ 1021 (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */ 1022 (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */ 1023 (ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */ 1024 (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */ 1025 (ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */ 1026 (ucs >= 0xffe0 && ucs <= 0xffe6) || 1027 (ucs >= 0x20000 && ucs <= 0x2fffd) || 1028 (ucs >= 0x30000 && ucs <= 0x3fffd))); 1029} 1030 1031// End of https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c inherited code 1032 1033static int unicode_to_cp437(uint64_t code_point) { 1034 switch (code_point) { 1035 case 0x263a: return 1; 1036 case 0x263b: return 2; 1037 case 0x2665: return 3; 1038 case 0x2666: return 4; 1039 case 0x2663: return 5; 1040 case 0x2660: return 6; 1041 case 0x2022: return 7; 1042 case 0x25d8: return 8; 1043 case 0x25cb: return 9; 1044 case 0x25d9: return 10; 1045 case 0x2642: return 11; 1046 case 0x2640: return 12; 1047 case 0x266a: return 13; 1048 case 0x266b: return 14; 1049 case 0x263c: return 15; 1050 case 0x25ba: return 16; 1051 case 0x25c4: return 17; 1052 case 0x2195: return 18; 1053 case 0x203c: return 19; 1054 case 0x00b6: return 20; 1055 case 0x00a7: return 21; 1056 case 0x25ac: return 22; 1057 case 0x21a8: return 23; 1058 case 0x2191: return 24; 1059 case 0x2193: return 25; 1060 case 0x2192: return 26; 1061 case 0x2190: return 27; 1062 case 0x221f: return 28; 1063 case 0x2194: return 29; 1064 case 0x25b2: return 30; 1065 case 0x25bc: return 31; 1066 1067 case 0x2302: return 127; 1068 case 0x00c7: return 128; 1069 case 0x00fc: return 129; 1070 case 0x00e9: return 130; 1071 case 0x00e2: return 131; 1072 case 0x00e4: return 132; 1073 case 0x00e0: return 133; 1074 case 0x00e5: return 134; 1075 case 0x00e7: return 135; 1076 case 0x00ea: return 136; 1077 case 0x00eb: return 137; 1078 case 0x00e8: return 138; 1079 case 0x00ef: return 139; 1080 case 0x00ee: return 140; 1081 case 0x00ec: return 141; 1082 case 0x00c4: return 142; 1083 case 0x00c5: return 143; 1084 case 0x00c9: return 144; 1085 case 0x00e6: return 145; 1086 case 0x00c6: return 146; 1087 case 0x00f4: return 147; 1088 case 0x00f6: return 148; 1089 case 0x00f2: return 149; 1090 case 0x00fb: return 150; 1091 case 0x00f9: return 151; 1092 case 0x00ff: return 152; 1093 case 0x00d6: return 153; 1094 case 0x00dc: return 154; 1095 case 0x00a2: return 155; 1096 case 0x00a3: return 156; 1097 case 0x00a5: return 157; 1098 case 0x20a7: return 158; 1099 case 0x0192: return 159; 1100 case 0x00e1: return 160; 1101 case 0x00ed: return 161; 1102 case 0x00f3: return 162; 1103 case 0x00fa: return 163; 1104 case 0x00f1: return 164; 1105 case 0x00d1: return 165; 1106 case 0x00aa: return 166; 1107 case 0x00ba: return 167; 1108 case 0x00bf: return 168; 1109 case 0x2310: return 169; 1110 case 0x00ac: return 170; 1111 case 0x00bd: return 171; 1112 case 0x00bc: return 172; 1113 case 0x00a1: return 173; 1114 case 0x00ab: return 174; 1115 case 0x00bb: return 175; 1116 case 0x2591: return 176; 1117 case 0x2592: return 177; 1118 case 0x2593: return 178; 1119 case 0x2502: return 179; 1120 case 0x2524: return 180; 1121 case 0x2561: return 181; 1122 case 0x2562: return 182; 1123 case 0x2556: return 183; 1124 case 0x2555: return 184; 1125 case 0x2563: return 185; 1126 case 0x2551: return 186; 1127 case 0x2557: return 187; 1128 case 0x255d: return 188; 1129 case 0x255c: return 189; 1130 case 0x255b: return 190; 1131 case 0x2510: return 191; 1132 case 0x2514: return 192; 1133 case 0x2534: return 193; 1134 case 0x252c: return 194; 1135 case 0x251c: return 195; 1136 case 0x2500: return 196; 1137 case 0x253c: return 197; 1138 case 0x255e: return 198; 1139 case 0x255f: return 199; 1140 case 0x255a: return 200; 1141 case 0x2554: return 201; 1142 case 0x2569: return 202; 1143 case 0x2566: return 203; 1144 case 0x2560: return 204; 1145 case 0x2550: return 205; 1146 case 0x256c: return 206; 1147 case 0x2567: return 207; 1148 case 0x2568: return 208; 1149 case 0x2564: return 209; 1150 case 0x2565: return 210; 1151 case 0x2559: return 211; 1152 case 0x2558: return 212; 1153 case 0x2552: return 213; 1154 case 0x2553: return 214; 1155 case 0x256b: return 215; 1156 case 0x256a: return 216; 1157 case 0x2518: return 217; 1158 case 0x250c: return 218; 1159 case 0x2588: return 219; 1160 case 0x2584: return 220; 1161 case 0x258c: return 221; 1162 case 0x2590: return 222; 1163 case 0x2580: return 223; 1164 case 0x03b1: return 224; 1165 case 0x00df: return 225; 1166 case 0x0393: return 226; 1167 case 0x03c0: return 227; 1168 case 0x03a3: return 228; 1169 case 0x03c3: return 229; 1170 case 0x00b5: return 230; 1171 case 0x03c4: return 231; 1172 case 0x03a6: return 232; 1173 case 0x0398: return 233; 1174 case 0x03a9: return 234; 1175 case 0x03b4: return 235; 1176 case 0x221e: return 236; 1177 case 0x03c6: return 237; 1178 case 0x03b5: return 238; 1179 case 0x2229: return 239; 1180 case 0x2261: return 240; 1181 case 0x00b1: return 241; 1182 case 0x2265: return 242; 1183 case 0x2264: return 243; 1184 case 0x2320: return 244; 1185 case 0x2321: return 245; 1186 case 0x00f7: return 246; 1187 case 0x2248: return 247; 1188 case 0x00b0: return 248; 1189 case 0x2219: return 249; 1190 case 0x00b7: return 250; 1191 case 0x221a: return 251; 1192 case 0x207f: return 252; 1193 case 0x00b2: return 253; 1194 case 0x25a0: return 254; 1195 } 1196 1197 return -1; 1198} 1199 1200static void flanterm_putchar(struct flanterm_context *ctx, uint8_t c) { 1201 if (ctx->discard_next || (c == 0x18 || c == 0x1a)) { 1202 ctx->discard_next = false; 1203 ctx->escape = false; 1204 ctx->control_sequence = false; 1205 ctx->unicode_remaining = 0; 1206 ctx->osc = false; 1207 ctx->osc_escape = false; 1208 ctx->g_select = 0; 1209 return; 1210 } 1211 1212 if (ctx->unicode_remaining != 0) { 1213 if ((c & 0xc0) != 0x80) { 1214 ctx->unicode_remaining = 0; 1215 goto unicode_error; 1216 } 1217 1218 ctx->unicode_remaining--; 1219 ctx->code_point |= (uint64_t)(c & 0x3f) << (6 * ctx->unicode_remaining); 1220 if (ctx->unicode_remaining != 0) { 1221 return; 1222 } 1223 1224 int cc = unicode_to_cp437(ctx->code_point); 1225 1226 if (cc == -1) { 1227 size_t replacement_width = (size_t)mk_wcwidth(ctx->code_point); 1228 if (replacement_width > 0) { 1229 ctx->raw_putchar(ctx, 0xfe); 1230 } 1231 for (size_t i = 1; i < replacement_width; i++) { 1232 ctx->raw_putchar(ctx, ' '); 1233 } 1234 } else { 1235 ctx->raw_putchar(ctx, cc); 1236 } 1237 return; 1238 } 1239 1240unicode_error: 1241 if (c >= 0xc0 && c <= 0xf7) { 1242 if (c >= 0xc0 && c <= 0xdf) { 1243 ctx->unicode_remaining = 1; 1244 ctx->code_point = (uint64_t)(c & 0x1f) << 6; 1245 } else if (c >= 0xe0 && c <= 0xef) { 1246 ctx->unicode_remaining = 2; 1247 ctx->code_point = (uint64_t)(c & 0x0f) << (6 * 2); 1248 } else if (c >= 0xf0 && c <= 0xf7) { 1249 ctx->unicode_remaining = 3; 1250 ctx->code_point = (uint64_t)(c & 0x07) << (6 * 3); 1251 } 1252 return; 1253 } 1254 1255 if (ctx->escape == true) { 1256 escape_parse(ctx, c); 1257 return; 1258 } 1259 1260 if (ctx->g_select) { 1261 ctx->g_select--; 1262 switch (c) { 1263 case 'B': 1264 ctx->charsets[ctx->g_select] = CHARSET_DEFAULT; break; 1265 case '0': 1266 ctx->charsets[ctx->g_select] = CHARSET_DEC_SPECIAL; break; 1267 } 1268 ctx->g_select = 0; 1269 return; 1270 } 1271 1272 size_t x, y; 1273 ctx->get_cursor_pos(ctx, &x, &y); 1274 1275 switch (c) { 1276 case 0x00: 1277 case 0x7f: 1278 return; 1279 case 0x1b: 1280 ctx->escape_offset = 0; 1281 ctx->escape = true; 1282 return; 1283 case '\t': 1284 if ((x / ctx->tab_size + 1) >= ctx->cols) { 1285 ctx->set_cursor_pos(ctx, ctx->cols - 1, y); 1286 return; 1287 } 1288 ctx->set_cursor_pos(ctx, (x / ctx->tab_size + 1) * ctx->tab_size, y); 1289 return; 1290 case 0x0b: 1291 case 0x0c: 1292 case '\n': 1293 if (y == ctx->scroll_bottom_margin - 1) { 1294 ctx->scroll(ctx); 1295 ctx->set_cursor_pos(ctx, (ctx->oob_output & FLANTERM_OOB_OUTPUT_ONLCR) ? 0 : x, y); 1296 } else { 1297 ctx->set_cursor_pos(ctx, (ctx->oob_output & FLANTERM_OOB_OUTPUT_ONLCR) ? 0 : x, y + 1); 1298 } 1299 return; 1300 case '\b': 1301 ctx->set_cursor_pos(ctx, x - 1, y); 1302 return; 1303 case '\r': 1304 ctx->set_cursor_pos(ctx, 0, y); 1305 return; 1306 case '\a': 1307 // The bell is handled by the kernel 1308 if (ctx->callback != NULL) { 1309 ctx->callback(ctx, FLANTERM_CB_BELL, 0, 0, 0); 1310 } 1311 return; 1312 case 14: 1313 // Move to G1 set 1314 ctx->current_charset = 1; 1315 return; 1316 case 15: 1317 // Move to G0 set 1318 ctx->current_charset = 0; 1319 return; 1320 } 1321 1322 if (ctx->insert_mode == true) { 1323 for (size_t i = ctx->cols - 1; ; i--) { 1324 ctx->move_character(ctx, i + 1, y, i, y); 1325 if (i == x) { 1326 break; 1327 } 1328 } 1329 } 1330 1331 // Translate character set 1332 switch (ctx->charsets[ctx->current_charset]) { 1333 case CHARSET_DEFAULT: 1334 break; 1335 case CHARSET_DEC_SPECIAL: 1336 if (dec_special_print(ctx, c)) { 1337 return; 1338 } 1339 break; 1340 } 1341 1342 if (c >= 0x20 && c <= 0x7e) { 1343 ctx->raw_putchar(ctx, c); 1344 } else { 1345 ctx->raw_putchar(ctx, 0xfe); 1346 } 1347} 1348 1349void flanterm_full_refresh(struct flanterm_context *ctx) { 1350 ctx->full_refresh(ctx); 1351} 1352 1353void flanterm_deinit(struct flanterm_context *ctx, void (*_free)(void *, size_t)) { 1354 ctx->deinit(ctx, _free); 1355} 1356 1357void flanterm_get_dimensions(struct flanterm_context *ctx, size_t *cols, size_t *rows) { 1358 *cols = ctx->cols; 1359 *rows = ctx->rows; 1360} 1361 1362void flanterm_set_autoflush(struct flanterm_context *ctx, bool state) { 1363 ctx->autoflush = state; 1364} 1365 1366void flanterm_set_callback(struct flanterm_context *ctx, void (*callback)(struct flanterm_context *, uint64_t, uint64_t, uint64_t, uint64_t)) { 1367 ctx->callback = callback; 1368}