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