1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
|
#include "vterm_internal.h"
#include <stdio.h>
#include <string.h>
#define strneq(a,b,n) (strncmp(a,b,n)==0)
#if defined(DEBUG) && DEBUG > 1
# define DEBUG_GLYPH_COMBINE
#endif
/* Some convenient wrappers to make callback functions easier */
static void putglyph(VTermState *state, const uint32_t chars[], int width, VTermPos pos)
{
VTermGlyphInfo info = {
.chars = chars,
.width = width,
.protected_cell = state->protected_cell,
.dwl = state->lineinfo[pos.row].doublewidth,
.dhl = state->lineinfo[pos.row].doubleheight,
};
if(state->callbacks && state->callbacks->putglyph)
if((*state->callbacks->putglyph)(&info, pos, state->cbdata))
return;
DEBUG_LOG("libvterm: Unhandled putglyph U+%04x at (%d,%d)\n", chars[0], pos.col, pos.row);
}
static void updatecursor(VTermState *state, VTermPos *oldpos, int cancel_phantom)
{
if(state->pos.col == oldpos->col && state->pos.row == oldpos->row)
return;
if(cancel_phantom)
state->at_phantom = 0;
if(state->callbacks && state->callbacks->movecursor)
if((*state->callbacks->movecursor)(state->pos, *oldpos, state->mode.cursor_visible, state->cbdata))
return;
}
static void erase(VTermState *state, VTermRect rect, int selective)
{
if(state->callbacks && state->callbacks->erase)
if((*state->callbacks->erase)(rect, selective, state->cbdata))
return;
}
static VTermState *vterm_state_new(VTerm *vt)
{
VTermState *state = vterm_allocator_malloc(vt, sizeof(VTermState));
state->vt = vt;
state->rows = vt->rows;
state->cols = vt->cols;
state->mouse_col = 0;
state->mouse_row = 0;
state->mouse_buttons = 0;
state->mouse_protocol = MOUSE_X10;
state->callbacks = NULL;
state->cbdata = NULL;
vterm_state_newpen(state);
state->bold_is_highbright = 0;
return state;
}
INTERNAL void vterm_state_free(VTermState *state)
{
vterm_allocator_free(state->vt, state->tabstops);
vterm_allocator_free(state->vt, state->lineinfo);
vterm_allocator_free(state->vt, state->combine_chars);
vterm_allocator_free(state->vt, state);
}
static void scroll(VTermState *state, VTermRect rect, int downward, int rightward)
{
if(!downward && !rightward)
return;
int rows = rect.end_row - rect.start_row;
if(downward > rows)
downward = rows;
else if(downward < -rows)
downward = -rows;
int cols = rect.end_col - rect.start_col;
if(rightward > cols)
rightward = cols;
else if(rightward < -cols)
rightward = -cols;
// Update lineinfo if full line
if(rect.start_col == 0 && rect.end_col == state->cols && rightward == 0) {
int height = rect.end_row - rect.start_row - abs(downward);
if(downward > 0)
memmove(state->lineinfo + rect.start_row,
state->lineinfo + rect.start_row + downward,
height * sizeof(state->lineinfo[0]));
else
memmove(state->lineinfo + rect.start_row - downward,
state->lineinfo + rect.start_row,
height * sizeof(state->lineinfo[0]));
}
if(state->callbacks && state->callbacks->scrollrect)
if((*state->callbacks->scrollrect)(rect, downward, rightward, state->cbdata))
return;
if(state->callbacks)
vterm_scroll_rect(rect, downward, rightward,
state->callbacks->moverect, state->callbacks->erase, state->cbdata);
}
static void linefeed(VTermState *state)
{
if(state->pos.row == SCROLLREGION_BOTTOM(state) - 1) {
VTermRect rect = {
.start_row = state->scrollregion_top,
.end_row = SCROLLREGION_BOTTOM(state),
.start_col = SCROLLREGION_LEFT(state),
.end_col = SCROLLREGION_RIGHT(state),
};
scroll(state, rect, 1, 0);
}
else if(state->pos.row < state->rows-1)
state->pos.row++;
}
static void grow_combine_buffer(VTermState *state)
{
size_t new_size = state->combine_chars_size * 2;
uint32_t *new_chars = vterm_allocator_malloc(state->vt, new_size * sizeof(new_chars[0]));
memcpy(new_chars, state->combine_chars, state->combine_chars_size * sizeof(new_chars[0]));
vterm_allocator_free(state->vt, state->combine_chars);
state->combine_chars = new_chars;
state->combine_chars_size = new_size;
}
static void set_col_tabstop(VTermState *state, int col)
{
unsigned char mask = 1 << (col & 7);
state->tabstops[col >> 3] |= mask;
}
static void clear_col_tabstop(VTermState *state, int col)
{
unsigned char mask = 1 << (col & 7);
state->tabstops[col >> 3] &= ~mask;
}
static int is_col_tabstop(VTermState *state, int col)
{
unsigned char mask = 1 << (col & 7);
return state->tabstops[col >> 3] & mask;
}
static int is_cursor_in_scrollregion(const VTermState *state)
{
if(state->pos.row < state->scrollregion_top ||
state->pos.row >= SCROLLREGION_BOTTOM(state))
return 0;
if(state->pos.col < SCROLLREGION_LEFT(state) ||
state->pos.col >= SCROLLREGION_RIGHT(state))
return 0;
return 1;
}
static void tab(VTermState *state, int count, int direction)
{
while(count > 0) {
if(direction > 0) {
if(state->pos.col >= THISROWWIDTH(state)-1)
return;
state->pos.col++;
}
else if(direction < 0) {
if(state->pos.col < 1)
return;
state->pos.col--;
}
if(is_col_tabstop(state, state->pos.col))
count--;
}
}
#define NO_FORCE 0
#define FORCE 1
#define DWL_OFF 0
#define DWL_ON 1
#define DHL_OFF 0
#define DHL_TOP 1
#define DHL_BOTTOM 2
static void set_lineinfo(VTermState *state, int row, int force, int dwl, int dhl)
{
VTermLineInfo info = state->lineinfo[row];
if(dwl == DWL_OFF)
info.doublewidth = DWL_OFF;
else if(dwl == DWL_ON)
info.doublewidth = DWL_ON;
// else -1 to ignore
if(dhl == DHL_OFF)
info.doubleheight = DHL_OFF;
else if(dhl == DHL_TOP)
info.doubleheight = DHL_TOP;
else if(dhl == DHL_BOTTOM)
info.doubleheight = DHL_BOTTOM;
if((state->callbacks &&
state->callbacks->setlineinfo &&
(*state->callbacks->setlineinfo)(row, &info, state->lineinfo + row, state->cbdata))
|| force)
state->lineinfo[row] = info;
}
static int on_text(const char bytes[], size_t len, void *user)
{
VTermState *state = user;
VTermPos oldpos = state->pos;
// We'll have at most len codepoints
uint32_t codepoints[len];
int npoints = 0;
size_t eaten = 0;
VTermEncodingInstance *encoding =
state->gsingle_set ? &state->encoding[state->gsingle_set] :
!(bytes[eaten] & 0x80) ? &state->encoding[state->gl_set] :
state->vt->mode.utf8 ? &state->encoding_utf8 :
&state->encoding[state->gr_set];
(*encoding->enc->decode)(encoding->enc, encoding->data,
codepoints, &npoints, state->gsingle_set ? 1 : len,
bytes, &eaten, len);
/* There's a chance an encoding (e.g. UTF-8) hasn't found enough bytes yet
* for even a single codepoint
*/
if(!npoints)
return eaten;
if(state->gsingle_set && npoints)
state->gsingle_set = 0;
int i = 0;
/* This is a combining char. that needs to be merged with the previous
* glyph output */
if(vterm_unicode_is_combining(codepoints[i])) {
/* See if the cursor has moved since */
if(state->pos.row == state->combine_pos.row && state->pos.col == state->combine_pos.col + state->combine_width) {
#ifdef DEBUG_GLYPH_COMBINE
int printpos;
printf("DEBUG: COMBINING SPLIT GLYPH of chars {");
for(printpos = 0; state->combine_chars[printpos]; printpos++)
printf("U+%04x ", state->combine_chars[printpos]);
printf("} + {");
#endif
/* Find where we need to append these combining chars */
int saved_i = 0;
while(state->combine_chars[saved_i])
saved_i++;
/* Add extra ones */
while(i < npoints && vterm_unicode_is_combining(codepoints[i])) {
if(saved_i >= state->combine_chars_size)
grow_combine_buffer(state);
state->combine_chars[saved_i++] = codepoints[i++];
}
if(saved_i >= state->combine_chars_size)
grow_combine_buffer(state);
state->combine_chars[saved_i] = 0;
#ifdef DEBUG_GLYPH_COMBINE
for(; state->combine_chars[printpos]; printpos++)
printf("U+%04x ", state->combine_chars[printpos]);
printf("}\n");
#endif
/* Now render it */
putglyph(state, state->combine_chars, state->combine_width, state->combine_pos);
}
else {
DEBUG_LOG("libvterm: TODO: Skip over split char+combining\n");
}
}
for(; i < npoints; i++) {
// Try to find combining characters following this
int glyph_starts = i;
int glyph_ends;
for(glyph_ends = i + 1; glyph_ends < npoints; glyph_ends++)
if(!vterm_unicode_is_combining(codepoints[glyph_ends]))
break;
int width = 0;
uint32_t chars[glyph_ends - glyph_starts + 1];
for( ; i < glyph_ends; i++) {
chars[i - glyph_starts] = codepoints[i];
int this_width = vterm_unicode_width(codepoints[i]);
#ifdef DEBUG
if(this_width < 0) {
fprintf(stderr, "Text with negative-width codepoint U+%04x\n", codepoints[i]);
abort();
}
#endif
width += this_width;
}
chars[glyph_ends - glyph_starts] = 0;
i--;
#ifdef DEBUG_GLYPH_COMBINE
int printpos;
printf("DEBUG: COMBINED GLYPH of %d chars {", glyph_ends - glyph_starts);
for(printpos = 0; printpos < glyph_ends - glyph_starts; printpos++)
printf("U+%04x ", chars[printpos]);
printf("}, onscreen width %d\n", width);
#endif
if(state->at_phantom || state->pos.col + width > THISROWWIDTH(state)) {
linefeed(state);
state->pos.col = 0;
state->at_phantom = 0;
}
if(state->mode.insert) {
/* TODO: This will be a little inefficient for large bodies of text, as
* it'll have to 'ICH' effectively before every glyph. We should scan
* ahead and ICH as many times as required
*/
VTermRect rect = {
.start_row = state->pos.row,
.end_row = state->pos.row + 1,
.start_col = state->pos.col,
.end_col = THISROWWIDTH(state),
};
scroll(state, rect, 0, -1);
}
putglyph(state, chars, width, state->pos);
if(i == npoints - 1) {
/* End of the buffer. Save the chars in case we have to combine with
* more on the next call */
int save_i;
for(save_i = 0; chars[save_i]; save_i++) {
if(save_i >= state->combine_chars_size)
grow_combine_buffer(state);
state->combine_chars[save_i] = chars[save_i];
}
if(save_i >= state->combine_chars_size)
grow_combine_buffer(state);
state->combine_chars[save_i] = 0;
state->combine_width = width;
state->combine_pos = state->pos;
}
if(state->pos.col + width >= THISROWWIDTH(state)) {
if(state->mode.autowrap)
state->at_phantom = 1;
}
else {
state->pos.col += width;
}
}
updatecursor(state, &oldpos, 0);
#ifdef DEBUG
if(state->pos.row < 0 || state->pos.row >= state->rows ||
state->pos.col < 0 || state->pos.col >= state->cols) {
fprintf(stderr, "Position out of bounds after text: (%d,%d)\n",
state->pos.row, state->pos.col);
abort();
}
#endif
return eaten;
}
static int on_control(unsigned char control, void *user)
{
VTermState *state = user;
VTermPos oldpos = state->pos;
switch(control) {
case 0x07: // BEL - ECMA-48 8.3.3
if(state->callbacks && state->callbacks->bell)
(*state->callbacks->bell)(state->cbdata);
break;
case 0x08: // BS - ECMA-48 8.3.5
if(state->pos.col > 0)
state->pos.col--;
break;
case 0x09: // HT - ECMA-48 8.3.60
tab(state, 1, +1);
break;
case 0x0a: // LF - ECMA-48 8.3.74
case 0x0b: // VT
case 0x0c: // FF
linefeed(state);
if(state->mode.newline)
state->pos.col = 0;
break;
case 0x0d: // CR - ECMA-48 8.3.15
state->pos.col = 0;
break;
case 0x0e: // LS1 - ECMA-48 8.3.76
state->gl_set = 1;
break;
case 0x0f: // LS0 - ECMA-48 8.3.75
state->gl_set = 0;
break;
case 0x84: // IND - DEPRECATED but implemented for completeness
linefeed(state);
break;
case 0x85: // NEL - ECMA-48 8.3.86
linefeed(state);
state->pos.col = 0;
break;
case 0x88: // HTS - ECMA-48 8.3.62
set_col_tabstop(state, state->pos.col);
break;
case 0x8d: // RI - ECMA-48 8.3.104
if(state->pos.row == state->scrollregion_top) {
VTermRect rect = {
.start_row = state->scrollregion_top,
.end_row = SCROLLREGION_BOTTOM(state),
.start_col = SCROLLREGION_LEFT(state),
.end_col = SCROLLREGION_RIGHT(state),
};
scroll(state, rect, -1, 0);
}
else if(state->pos.row > 0)
state->pos.row--;
break;
case 0x8e: // SS2 - ECMA-48 8.3.141
state->gsingle_set = 2;
break;
case 0x8f: // SS3 - ECMA-48 8.3.142
state->gsingle_set = 3;
break;
default:
if(state->fallbacks && state->fallbacks->control)
if((*state->fallbacks->control)(control, state->fbdata))
return 1;
return 0;
}
updatecursor(state, &oldpos, 1);
#ifdef DEBUG
if(state->pos.row < 0 || state->pos.row >= state->rows ||
state->pos.col < 0 || state->pos.col >= state->cols) {
fprintf(stderr, "Position out of bounds after Ctrl %02x: (%d,%d)\n",
control, state->pos.row, state->pos.col);
abort();
}
#endif
return 1;
}
static int settermprop_bool(VTermState *state, VTermProp prop, int v)
{
VTermValue val = { .boolean = v };
return vterm_state_set_termprop(state, prop, &val);
}
static int settermprop_int(VTermState *state, VTermProp prop, int v)
{
VTermValue val = { .number = v };
return vterm_state_set_termprop(state, prop, &val);
}
static int settermprop_string(VTermState *state, VTermProp prop, const char *str, size_t len)
{
char strvalue[len+1];
strncpy(strvalue, str, len);
strvalue[len] = 0;
VTermValue val = { .string = strvalue };
return vterm_state_set_termprop(state, prop, &val);
}
static void savecursor(VTermState *state, int save)
{
if(save) {
state->saved.pos = state->pos;
state->saved.mode.cursor_visible = state->mode.cursor_visible;
state->saved.mode.cursor_blink = state->mode.cursor_blink;
state->saved.mode.cursor_shape = state->mode.cursor_shape;
vterm_state_savepen(state, 1);
}
else {
VTermPos oldpos = state->pos;
state->pos = state->saved.pos;
settermprop_bool(state, VTERM_PROP_CURSORVISIBLE, state->saved.mode.cursor_visible);
settermprop_bool(state, VTERM_PROP_CURSORBLINK, state->saved.mode.cursor_blink);
settermprop_int (state, VTERM_PROP_CURSORSHAPE, state->saved.mode.cursor_shape);
vterm_state_savepen(state, 0);
updatecursor(state, &oldpos, 1);
}
}
static int on_escape(const char *bytes, size_t len, void *user)
{
VTermState *state = user;
/* Easier to decode this from the first byte, even though the final
* byte terminates it
*/
switch(bytes[0]) {
case ' ':
if(len != 2)
return 0;
switch(bytes[1]) {
case 'F': // S7C1T
state->vt->mode.ctrl8bit = 0;
break;
case 'G': // S8C1T
state->vt->mode.ctrl8bit = 1;
break;
default:
return 0;
}
return 2;
case '#':
if(len != 2)
return 0;
switch(bytes[1]) {
case '3': // DECDHL top
if(state->mode.leftrightmargin)
break;
set_lineinfo(state, state->pos.row, NO_FORCE, DWL_ON, DHL_TOP);
break;
case '4': // DECDHL bottom
if(state->mode.leftrightmargin)
break;
set_lineinfo(state, state->pos.row, NO_FORCE, DWL_ON, DHL_BOTTOM);
break;
case '5': // DECSWL
if(state->mode.leftrightmargin)
break;
set_lineinfo(state, state->pos.row, NO_FORCE, DWL_OFF, DHL_OFF);
break;
case '6': // DECDWL
if(state->mode.leftrightmargin)
break;
set_lineinfo(state, state->pos.row, NO_FORCE, DWL_ON, DHL_OFF);
break;
case '8': // DECALN
{
VTermPos pos;
uint32_t E[] = { 'E', 0 };
for(pos.row = 0; pos.row < state->rows; pos.row++)
for(pos.col = 0; pos.col < ROWWIDTH(state, pos.row); pos.col++)
putglyph(state, E, 1, pos);
break;
}
default:
return 0;
}
return 2;
case '(': case ')': case '*': case '+': // SCS
if(len != 2)
return 0;
{
int setnum = bytes[0] - 0x28;
VTermEncoding *newenc = vterm_lookup_encoding(ENC_SINGLE_94, bytes[1]);
if(newenc) {
state->encoding[setnum].enc = newenc;
if(newenc->init)
(*newenc->init)(newenc, state->encoding[setnum].data);
}
}
return 2;
case '7': // DECSC
savecursor(state, 1);
return 1;
case '8': // DECRC
savecursor(state, 0);
return 1;
case '<': // Ignored by VT100. Used in VT52 mode to switch up to VT100
return 1;
case '=': // DECKPAM
state->mode.keypad = 1;
return 1;
case '>': // DECKPNM
state->mode.keypad = 0;
return 1;
case 'c': // RIS - ECMA-48 8.3.105
{
VTermPos oldpos = state->pos;
vterm_state_reset(state, 1);
if(state->callbacks && state->callbacks->movecursor)
(*state->callbacks->movecursor)(state->pos, oldpos, state->mode.cursor_visible, state->cbdata);
return 1;
}
case 'n': // LS2 - ECMA-48 8.3.78
state->gl_set = 2;
return 1;
case 'o': // LS3 - ECMA-48 8.3.80
state->gl_set = 3;
return 1;
case '~': // LS1R - ECMA-48 8.3.77
state->gr_set = 1;
return 1;
case '}': // LS2R - ECMA-48 8.3.79
state->gr_set = 2;
return 1;
case '|': // LS3R - ECMA-48 8.3.81
state->gr_set = 3;
return 1;
default:
return 0;
}
}
static void set_mode(VTermState *state, int num, int val)
{
switch(num) {
case 4: // IRM - ECMA-48 7.2.10
state->mode.insert = val;
break;
case 20: // LNM - ANSI X3.4-1977
state->mode.newline = val;
break;
default:
DEBUG_LOG("libvterm: Unknown mode %d\n", num);
return;
}
}
static void set_dec_mode(VTermState *state, int num, int val)
{
switch(num) {
case 1:
state->mode.cursor = val;
break;
case 5: // DECSCNM - screen mode
settermprop_bool(state, VTERM_PROP_REVERSE, val);
break;
case 6: // DECOM - origin mode
{
VTermPos oldpos = state->pos;
state->mode.origin = val;
state->pos.row = state->mode.origin ? state->scrollregion_top : 0;
state->pos.col = state->mode.origin ? SCROLLREGION_LEFT(state) : 0;
updatecursor(state, &oldpos, 1);
}
break;
case 7:
state->mode.autowrap = val;
break;
case 12:
settermprop_bool(state, VTERM_PROP_CURSORBLINK, val);
break;
case 25:
settermprop_bool(state, VTERM_PROP_CURSORVISIBLE, val);
break;
case 69: // DECVSSM - vertical split screen mode
// DECLRMM - left/right margin mode
state->mode.leftrightmargin = val;
if(val) {
// Setting DECVSSM must clear doublewidth/doubleheight state of every line
for(int row = 0; row < state->rows; row++)
set_lineinfo(state, row, FORCE, DWL_OFF, DHL_OFF);
}
break;
case 1000:
case 1002:
case 1003:
settermprop_int(state, VTERM_PROP_MOUSE,
!val ? VTERM_PROP_MOUSE_NONE :
(num == 1000) ? VTERM_PROP_MOUSE_CLICK :
(num == 1002) ? VTERM_PROP_MOUSE_DRAG :
VTERM_PROP_MOUSE_MOVE);
break;
case 1004:
state->mode.report_focus = val;
break;
case 1005:
state->mouse_protocol = val ? MOUSE_UTF8 : MOUSE_X10;
break;
case 1006:
state->mouse_protocol = val ? MOUSE_SGR : MOUSE_X10;
break;
case 1015:
state->mouse_protocol = val ? MOUSE_RXVT : MOUSE_X10;
break;
case 1047:
settermprop_bool(state, VTERM_PROP_ALTSCREEN, val);
break;
case 1048:
savecursor(state, val);
break;
case 1049:
settermprop_bool(state, VTERM_PROP_ALTSCREEN, val);
savecursor(state, val);
break;
case 2004:
state->mode.bracketpaste = val;
break;
default:
DEBUG_LOG("libvterm: Unknown DEC mode %d\n", num);
return;
}
}
static void request_dec_mode(VTermState *state, int num)
{
int reply;
switch(num) {
case 1:
reply = state->mode.cursor;
break;
case 5:
reply = state->mode.screen;
break;
case 6:
reply = state->mode.origin;
break;
case 7:
reply = state->mode.autowrap;
break;
case 12:
reply = state->mode.cursor_blink;
break;
case 25:
reply = state->mode.cursor_visible;
break;
case 69:
reply = state->mode.leftrightmargin;
break;
case 1000:
reply = state->mouse_flags == MOUSE_WANT_CLICK;
break;
case 1002:
reply = state->mouse_flags == (MOUSE_WANT_CLICK|MOUSE_WANT_DRAG);
break;
case 1003:
reply = state->mouse_flags == (MOUSE_WANT_CLICK|MOUSE_WANT_MOVE);
break;
case 1004:
reply = state->mode.report_focus;
break;
case 1005:
reply = state->mouse_protocol == MOUSE_UTF8;
break;
case 1006:
reply = state->mouse_protocol == MOUSE_SGR;
break;
case 1015:
reply = state->mouse_protocol == MOUSE_RXVT;
break;
case 1047:
reply = state->mode.alt_screen;
break;
case 2004:
reply = state->mode.bracketpaste;
break;
default:
vterm_push_output_sprintf_ctrl(state->vt, C1_CSI, "?%d;%d$y", num, 0);
return;
}
vterm_push_output_sprintf_ctrl(state->vt, C1_CSI, "?%d;%d$y", num, reply ? 1 : 2);
}
static int on_csi(const char *leader, const long args[], int argcount, const char *intermed, char command, void *user)
{
VTermState *state = user;
int leader_byte = 0;
int intermed_byte = 0;
int cancel_phantom = 1;
if(leader && leader[0]) {
if(leader[1]) // longer than 1 char
return 0;
switch(leader[0]) {
case '?':
case '>':
leader_byte = leader[0];
break;
default:
return 0;
}
}
if(intermed && intermed[0]) {
if(intermed[1]) // longer than 1 char
return 0;
switch(intermed[0]) {
case ' ':
case '"':
case '$':
case '\'':
intermed_byte = intermed[0];
break;
default:
return 0;
}
}
VTermPos oldpos = state->pos;
// Some temporaries for later code
int count, val;
int row, col;
VTermRect rect;
int selective;
#define LBOUND(v,min) if((v) < (min)) (v) = (min)
#define UBOUND(v,max) if((v) > (max)) (v) = (max)
#define LEADER(l,b) ((l << 8) | b)
#define INTERMED(i,b) ((i << 16) | b)
switch(intermed_byte << 16 | leader_byte << 8 | command) {
case 0x40: // ICH - ECMA-48 8.3.64
count = CSI_ARG_COUNT(args[0]);
if(!is_cursor_in_scrollregion(state))
break;
rect.start_row = state->pos.row;
rect.end_row = state->pos.row + 1;
rect.start_col = state->pos.col;
if(state->mode.leftrightmargin)
rect.end_col = SCROLLREGION_RIGHT(state);
else
rect.end_col = THISROWWIDTH(state);
scroll(state, rect, 0, -count);
break;
case 0x41: // CUU - ECMA-48 8.3.22
count = CSI_ARG_COUNT(args[0]);
state->pos.row -= count;
state->at_phantom = 0;
break;
case 0x42: // CUD - ECMA-48 8.3.19
count = CSI_ARG_COUNT(args[0]);
state->pos.row += count;
state->at_phantom = 0;
break;
case 0x43: // CUF - ECMA-48 8.3.20
count = CSI_ARG_COUNT(args[0]);
state->pos.col += count;
state->at_phantom = 0;
break;
case 0x44: // CUB - ECMA-48 8.3.18
count = CSI_ARG_COUNT(args[0]);
state->pos.col -= count;
state->at_phantom = 0;
break;
case 0x45: // CNL - ECMA-48 8.3.12
count = CSI_ARG_COUNT(args[0]);
state->pos.col = 0;
state->pos.row += count;
state->at_phantom = 0;
break;
case 0x46: // CPL - ECMA-48 8.3.13
count = CSI_ARG_COUNT(args[0]);
state->pos.col = 0;
state->pos.row -= count;
state->at_phantom = 0;
break;
case 0x47: // CHA - ECMA-48 8.3.9
val = CSI_ARG_OR(args[0], 1);
state->pos.col = val-1;
state->at_phantom = 0;
break;
case 0x48: // CUP - ECMA-48 8.3.21
row = CSI_ARG_OR(args[0], 1);
col = argcount < 2 || CSI_ARG_IS_MISSING(args[1]) ? 1 : CSI_ARG(args[1]);
// zero-based
state->pos.row = row-1;
state->pos.col = col-1;
if(state->mode.origin) {
state->pos.row += state->scrollregion_top;
state->pos.col += SCROLLREGION_LEFT(state);
}
state->at_phantom = 0;
break;
case 0x49: // CHT - ECMA-48 8.3.10
count = CSI_ARG_COUNT(args[0]);
tab(state, count, +1);
break;
case 0x4a: // ED - ECMA-48 8.3.39
case LEADER('?', 0x4a): // DECSED - Selective Erase in Display
selective = (leader_byte == '?');
switch(CSI_ARG(args[0])) {
case CSI_ARG_MISSING:
case 0:
rect.start_row = state->pos.row; rect.end_row = state->pos.row + 1;
rect.start_col = state->pos.col; rect.end_col = state->cols;
if(rect.end_col > rect.start_col)
erase(state, rect, selective);
rect.start_row = state->pos.row + 1; rect.end_row = state->rows;
rect.start_col = 0;
for(int row = rect.start_row; row < rect.end_row; row++)
set_lineinfo(state, row, FORCE, DWL_OFF, DHL_OFF);
if(rect.end_row > rect.start_row)
erase(state, rect, selective);
break;
case 1:
rect.start_row = 0; rect.end_row = state->pos.row;
rect.start_col = 0; rect.end_col = state->cols;
for(int row = rect.start_row; row < rect.end_row; row++)
set_lineinfo(state, row, FORCE, DWL_OFF, DHL_OFF);
if(rect.end_col > rect.start_col)
erase(state, rect, selective);
rect.start_row = state->pos.row; rect.end_row = state->pos.row + 1;
rect.end_col = state->pos.col + 1;
if(rect.end_row > rect.start_row)
erase(state, rect, selective);
break;
case 2:
rect.start_row = 0; rect.end_row = state->rows;
rect.start_col = 0; rect.end_col = state->cols;
for(int row = rect.start_row; row < rect.end_row; row++)
set_lineinfo(state, row, FORCE, DWL_OFF, DHL_OFF);
erase(state, rect, selective);
break;
}
break;
case 0x4b: // EL - ECMA-48 8.3.41
case LEADER('?', 0x4b): // DECSEL - Selective Erase in Line
selective = (leader_byte == '?');
rect.start_row = state->pos.row;
rect.end_row = state->pos.row + 1;
switch(CSI_ARG(args[0])) {
case CSI_ARG_MISSING:
case 0:
rect.start_col = state->pos.col; rect.end_col = THISROWWIDTH(state); break;
case 1:
rect.start_col = 0; rect.end_col = state->pos.col + 1; break;
case 2:
rect.start_col = 0; rect.end_col = THISROWWIDTH(state); break;
default:
return 0;
}
if(rect.end_col > rect.start_col)
erase(state, rect, selective);
break;
case 0x4c: // IL - ECMA-48 8.3.67
count = CSI_ARG_COUNT(args[0]);
if(!is_cursor_in_scrollregion(state))
break;
rect.start_row = state->pos.row;
rect.end_row = SCROLLREGION_BOTTOM(state);
rect.start_col = SCROLLREGION_LEFT(state);
rect.end_col = SCROLLREGION_RIGHT(state);
scroll(state, rect, -count, 0);
break;
case 0x4d: // DL - ECMA-48 8.3.32
count = CSI_ARG_COUNT(args[0]);
if(!is_cursor_in_scrollregion(state))
break;
rect.start_row = state->pos.row;
rect.end_row = SCROLLREGION_BOTTOM(state);
rect.start_col = SCROLLREGION_LEFT(state);
rect.end_col = SCROLLREGION_RIGHT(state);
scroll(state, rect, count, 0);
break;
case 0x50: // DCH - ECMA-48 8.3.26
count = CSI_ARG_COUNT(args[0]);
if(!is_cursor_in_scrollregion(state))
break;
rect.start_row = state->pos.row;
rect.end_row = state->pos.row + 1;
rect.start_col = state->pos.col;
if(state->mode.leftrightmargin)
rect.end_col = SCROLLREGION_RIGHT(state);
else
rect.end_col = THISROWWIDTH(state);
scroll(state, rect, 0, count);
break;
case 0x53: // SU - ECMA-48 8.3.147
count = CSI_ARG_COUNT(args[0]);
rect.start_row = state->scrollregion_top;
rect.end_row = SCROLLREGION_BOTTOM(state);
rect.start_col = SCROLLREGION_LEFT(state);
rect.end_col = SCROLLREGION_RIGHT(state);
scroll(state, rect, count, 0);
break;
case 0x54: // SD - ECMA-48 8.3.113
count = CSI_ARG_COUNT(args[0]);
rect.start_row = state->scrollregion_top;
rect.end_row = SCROLLREGION_BOTTOM(state);
rect.start_col = SCROLLREGION_LEFT(state);
rect.end_col = SCROLLREGION_RIGHT(state);
scroll(state, rect, -count, 0);
break;
case 0x58: // ECH - ECMA-48 8.3.38
count = CSI_ARG_COUNT(args[0]);
rect.start_row = state->pos.row;
rect.end_row = state->pos.row + 1;
rect.start_col = state->pos.col;
rect.end_col = state->pos.col + count;
UBOUND(rect.end_col, THISROWWIDTH(state));
erase(state, rect, 0);
break;
case 0x5a: // CBT - ECMA-48 8.3.7
count = CSI_ARG_COUNT(args[0]);
tab(state, count, -1);
break;
case 0x60: // HPA - ECMA-48 8.3.57
col = CSI_ARG_OR(args[0], 1);
state->pos.col = col-1;
state->at_phantom = 0;
break;
case 0x61: // HPR - ECMA-48 8.3.59
count = CSI_ARG_COUNT(args[0]);
state->pos.col += count;
state->at_phantom = 0;
break;
case 0x62: { // REP - ECMA-48 8.3.103
const int row_width = THISROWWIDTH(state);
count = CSI_ARG_COUNT(args[0]);
col = state->pos.col + count;
UBOUND(col, row_width);
while (state->pos.col < col) {
putglyph(state, state->combine_chars, state->combine_width, state->pos);
state->pos.col += state->combine_width;
}
if (state->pos.col + state->combine_width >= row_width) {
if (state->mode.autowrap) {
state->at_phantom = 1;
cancel_phantom = 0;
}
}
break;
}
case 0x63: // DA - ECMA-48 8.3.24
val = CSI_ARG_OR(args[0], 0);
if(val == 0)
// DEC VT100 response
vterm_push_output_sprintf_ctrl(state->vt, C1_CSI, "?1;2c");
break;
case LEADER('>', 0x63): // DEC secondary Device Attributes
vterm_push_output_sprintf_ctrl(state->vt, C1_CSI, ">%d;%d;%dc", 0, 100, 0);
break;
case 0x64: // VPA - ECMA-48 8.3.158
row = CSI_ARG_OR(args[0], 1);
state->pos.row = row-1;
if(state->mode.origin)
state->pos.row += state->scrollregion_top;
state->at_phantom = 0;
break;
case 0x65: // VPR - ECMA-48 8.3.160
count = CSI_ARG_COUNT(args[0]);
state->pos.row += count;
state->at_phantom = 0;
break;
case 0x66: // HVP - ECMA-48 8.3.63
row = CSI_ARG_OR(args[0], 1);
col = argcount < 2 || CSI_ARG_IS_MISSING(args[1]) ? 1 : CSI_ARG(args[1]);
// zero-based
state->pos.row = row-1;
state->pos.col = col-1;
if(state->mode.origin) {
state->pos.row += state->scrollregion_top;
state->pos.col += SCROLLREGION_LEFT(state);
}
state->at_phantom = 0;
break;
case 0x67: // TBC - ECMA-48 8.3.154
val = CSI_ARG_OR(args[0], 0);
switch(val) {
case 0:
clear_col_tabstop(state, state->pos.col);
break;
case 3:
case 5:
for(col = 0; col < state->cols; col++)
clear_col_tabstop(state, col);
break;
case 1:
case 2:
case 4:
break;
/* TODO: 1, 2 and 4 aren't meaningful yet without line tab stops */
default:
return 0;
}
break;
case 0x68: // SM - ECMA-48 8.3.125
if(!CSI_ARG_IS_MISSING(args[0]))
set_mode(state, CSI_ARG(args[0]), 1);
break;
case LEADER('?', 0x68): // DEC private mode set
if(!CSI_ARG_IS_MISSING(args[0]))
set_dec_mode(state, CSI_ARG(args[0]), 1);
break;
case 0x6a: // HPB - ECMA-48 8.3.58
count = CSI_ARG_COUNT(args[0]);
state->pos.col -= count;
state->at_phantom = 0;
break;
case 0x6b: // VPB - ECMA-48 8.3.159
count = CSI_ARG_COUNT(args[0]);
state->pos.row -= count;
state->at_phantom = 0;
break;
case 0x6c: // RM - ECMA-48 8.3.106
if(!CSI_ARG_IS_MISSING(args[0]))
set_mode(state, CSI_ARG(args[0]), 0);
break;
case LEADER('?', 0x6c): // DEC private mode reset
if(!CSI_ARG_IS_MISSING(args[0]))
set_dec_mode(state, CSI_ARG(args[0]), 0);
break;
case 0x6d: // SGR - ECMA-48 8.3.117
vterm_state_setpen(state, args, argcount);
break;
case 0x6e: // DSR - ECMA-48 8.3.35
case LEADER('?', 0x6e): // DECDSR
val = CSI_ARG_OR(args[0], 0);
{
char *qmark = (leader_byte == '?') ? "?" : "";
switch(val) {
case 0: case 1: case 2: case 3: case 4:
// ignore - these are replies
break;
case 5:
vterm_push_output_sprintf_ctrl(state->vt, C1_CSI, "%s0n", qmark);
break;
case 6: // CPR - cursor position report
vterm_push_output_sprintf_ctrl(state->vt, C1_CSI, "%s%d;%dR", qmark, state->pos.row + 1, state->pos.col + 1);
break;
}
}
break;
case LEADER('!', 0x70): // DECSTR - DEC soft terminal reset
vterm_state_reset(state, 0);
break;
case LEADER('?', INTERMED('$', 0x70)):
request_dec_mode(state, CSI_ARG(args[0]));
break;
case INTERMED(' ', 0x71): // DECSCUSR - DEC set cursor shape
val = CSI_ARG_OR(args[0], 1);
switch(val) {
case 0: case 1:
settermprop_bool(state, VTERM_PROP_CURSORBLINK, 1);
settermprop_int (state, VTERM_PROP_CURSORSHAPE, VTERM_PROP_CURSORSHAPE_BLOCK);
break;
case 2:
settermprop_bool(state, VTERM_PROP_CURSORBLINK, 0);
settermprop_int (state, VTERM_PROP_CURSORSHAPE, VTERM_PROP_CURSORSHAPE_BLOCK);
break;
case 3:
settermprop_bool(state, VTERM_PROP_CURSORBLINK, 1);
settermprop_int (state, VTERM_PROP_CURSORSHAPE, VTERM_PROP_CURSORSHAPE_UNDERLINE);
break;
case 4:
settermprop_bool(state, VTERM_PROP_CURSORBLINK, 0);
settermprop_int (state, VTERM_PROP_CURSORSHAPE, VTERM_PROP_CURSORSHAPE_UNDERLINE);
break;
case 5:
settermprop_bool(state, VTERM_PROP_CURSORBLINK, 1);
settermprop_int (state, VTERM_PROP_CURSORSHAPE, VTERM_PROP_CURSORSHAPE_BAR_LEFT);
break;
case 6:
settermprop_bool(state, VTERM_PROP_CURSORBLINK, 0);
settermprop_int (state, VTERM_PROP_CURSORSHAPE, VTERM_PROP_CURSORSHAPE_BAR_LEFT);
break;
}
break;
case INTERMED('"', 0x71): // DECSCA - DEC select character protection attribute
val = CSI_ARG_OR(args[0], 0);
switch(val) {
case 0: case 2:
state->protected_cell = 0;
break;
case 1:
state->protected_cell = 1;
break;
}
break;
case 0x72: // DECSTBM - DEC custom
state->scrollregion_top = CSI_ARG_OR(args[0], 1) - 1;
state->scrollregion_bottom = argcount < 2 || CSI_ARG_IS_MISSING(args[1]) ? -1 : CSI_ARG(args[1]);
LBOUND(state->scrollregion_top, 0);
UBOUND(state->scrollregion_top, state->rows);
LBOUND(state->scrollregion_bottom, -1);
if(state->scrollregion_top == 0 && state->scrollregion_bottom == state->rows)
state->scrollregion_bottom = -1;
else
UBOUND(state->scrollregion_bottom, state->rows);
if(SCROLLREGION_BOTTOM(state) <= state->scrollregion_top) {
// Invalid
state->scrollregion_top = 0;
state->scrollregion_bottom = -1;
}
break;
case 0x73: // DECSLRM - DEC custom
// Always allow setting these margins, just they won't take effect without DECVSSM
state->scrollregion_left = CSI_ARG_OR(args[0], 1) - 1;
state->scrollregion_right = argcount < 2 || CSI_ARG_IS_MISSING(args[1]) ? -1 : CSI_ARG(args[1]);
LBOUND(state->scrollregion_left, 0);
UBOUND(state->scrollregion_left, state->cols);
LBOUND(state->scrollregion_right, -1);
if(state->scrollregion_left == 0 && state->scrollregion_right == state->cols)
state->scrollregion_right = -1;
else
UBOUND(state->scrollregion_right, state->cols);
if(state->scrollregion_right > -1 &&
state->scrollregion_right <= state->scrollregion_left) {
// Invalid
state->scrollregion_left = 0;
state->scrollregion_right = -1;
}
break;
case INTERMED('\'', 0x7D): // DECIC
count = CSI_ARG_COUNT(args[0]);
if(!is_cursor_in_scrollregion(state))
break;
rect.start_row = state->scrollregion_top;
rect.end_row = SCROLLREGION_BOTTOM(state);
rect.start_col = state->pos.col;
rect.end_col = SCROLLREGION_RIGHT(state);
scroll(state, rect, 0, -count);
break;
case INTERMED('\'', 0x7E): // DECDC
count = CSI_ARG_COUNT(args[0]);
if(!is_cursor_in_scrollregion(state))
break;
rect.start_row = state->scrollregion_top;
rect.end_row = SCROLLREGION_BOTTOM(state);
rect.start_col = state->pos.col;
rect.end_col = SCROLLREGION_RIGHT(state);
scroll(state, rect, 0, count);
break;
default:
if(state->fallbacks && state->fallbacks->csi)
if((*state->fallbacks->csi)(leader, args, argcount, intermed, command, state->fbdata))
return 1;
return 0;
}
if(state->mode.origin) {
LBOUND(state->pos.row, state->scrollregion_top);
UBOUND(state->pos.row, SCROLLREGION_BOTTOM(state)-1);
LBOUND(state->pos.col, SCROLLREGION_LEFT(state));
UBOUND(state->pos.col, SCROLLREGION_RIGHT(state)-1);
}
else {
LBOUND(state->pos.row, 0);
UBOUND(state->pos.row, state->rows-1);
LBOUND(state->pos.col, 0);
UBOUND(state->pos.col, THISROWWIDTH(state)-1);
}
updatecursor(state, &oldpos, cancel_phantom);
#ifdef DEBUG
if(state->pos.row < 0 || state->pos.row >= state->rows ||
state->pos.col < 0 || state->pos.col >= state->cols) {
fprintf(stderr, "Position out of bounds after CSI %c: (%d,%d)\n",
command, state->pos.row, state->pos.col);
abort();
}
if(SCROLLREGION_BOTTOM(state) <= state->scrollregion_top) {
fprintf(stderr, "Scroll region height out of bounds after CSI %c: %d <= %d\n",
command, SCROLLREGION_BOTTOM(state), state->scrollregion_top);
abort();
}
if(SCROLLREGION_RIGHT(state) <= SCROLLREGION_LEFT(state)) {
fprintf(stderr, "Scroll region width out of bounds after CSI %c: %d <= %d\n",
command, SCROLLREGION_RIGHT(state), SCROLLREGION_LEFT(state));
abort();
}
#endif
return 1;
}
static int on_osc(const char *command, size_t cmdlen, void *user)
{
VTermState *state = user;
if(cmdlen < 2)
return 0;
if(strneq(command, "0;", 2)) {
settermprop_string(state, VTERM_PROP_ICONNAME, command + 2, cmdlen - 2);
settermprop_string(state, VTERM_PROP_TITLE, command + 2, cmdlen - 2);
return 1;
}
else if(strneq(command, "1;", 2)) {
settermprop_string(state, VTERM_PROP_ICONNAME, command + 2, cmdlen - 2);
return 1;
}
else if(strneq(command, "2;", 2)) {
settermprop_string(state, VTERM_PROP_TITLE, command + 2, cmdlen - 2);
return 1;
}
else if(state->fallbacks && state->fallbacks->osc)
if((*state->fallbacks->osc)(command, cmdlen, state->fbdata))
return 1;
return 0;
}
static void request_status_string(VTermState *state, const char *command, size_t cmdlen)
{
VTerm *vt = state->vt;
if(cmdlen == 1)
switch(command[0]) {
case 'm': // Query SGR
{
long args[20];
int argc = vterm_state_getpen(state, args, sizeof(args)/sizeof(args[0]));
size_t cur = 0;
cur += snprintf(vt->tmpbuffer + cur, vt->tmpbuffer_len - cur,
vt->mode.ctrl8bit ? "\x90" "1$r" : ESC_S "P" "1$r"); // DCS 1$r ...
if(cur >= vt->tmpbuffer_len)
return;
for(int argi = 0; argi < argc; argi++) {
cur += snprintf(vt->tmpbuffer + cur, vt->tmpbuffer_len - cur,
argi == argc - 1 ? "%ld" :
CSI_ARG_HAS_MORE(args[argi]) ? "%ld:" :
"%ld;",
CSI_ARG(args[argi]));
if(cur >= vt->tmpbuffer_len)
return;
}
cur += snprintf(vt->tmpbuffer + cur, vt->tmpbuffer_len - cur,
vt->mode.ctrl8bit ? "m" "\x9C" : "m" ESC_S "\\"); // ... m ST
if(cur >= vt->tmpbuffer_len)
return;
vterm_push_output_bytes(vt, vt->tmpbuffer, cur);
}
return;
case 'r': // Query DECSTBM
vterm_push_output_sprintf_dcs(vt, "1$r%d;%dr", state->scrollregion_top+1, SCROLLREGION_BOTTOM(state));
return;
case 's': // Query DECSLRM
vterm_push_output_sprintf_dcs(vt, "1$r%d;%ds", SCROLLREGION_LEFT(state)+1, SCROLLREGION_RIGHT(state));
return;
}
if(cmdlen == 2) {
if(strneq(command, " q", 2)) {
int reply;
switch(state->mode.cursor_shape) {
case VTERM_PROP_CURSORSHAPE_BLOCK: reply = 2; break;
case VTERM_PROP_CURSORSHAPE_UNDERLINE: reply = 4; break;
case VTERM_PROP_CURSORSHAPE_BAR_LEFT: reply = 6; break;
}
if(state->mode.cursor_blink)
reply--;
vterm_push_output_sprintf_dcs(vt, "1$r%d q", reply);
return;
}
else if(strneq(command, "\"q", 2)) {
vterm_push_output_sprintf_dcs(vt, "1$r%d\"q", state->protected_cell ? 1 : 2);
return;
}
}
vterm_push_output_sprintf_dcs(state->vt, "0$r%.s", (int)cmdlen, command);
}
static int on_dcs(const char *command, size_t cmdlen, void *user)
{
VTermState *state = user;
if(cmdlen >= 2 && strneq(command, "$q", 2)) {
request_status_string(state, command+2, cmdlen-2);
return 1;
}
else if(state->fallbacks && state->fallbacks->dcs)
if((*state->fallbacks->dcs)(command, cmdlen, state->fbdata))
return 1;
return 0;
}
static int on_resize(int rows, int cols, void *user)
{
VTermState *state = user;
VTermPos oldpos = state->pos;
if(cols != state->cols) {
unsigned char *newtabstops = vterm_allocator_malloc(state->vt, (cols + 7) / 8);
/* TODO: This can all be done much more efficiently bytewise */
int col;
for(col = 0; col < state->cols && col < cols; col++) {
unsigned char mask = 1 << (col & 7);
if(state->tabstops[col >> 3] & mask)
newtabstops[col >> 3] |= mask;
else
newtabstops[col >> 3] &= ~mask;
}
for( ; col < cols; col++) {
unsigned char mask = 1 << (col & 7);
if(col % 8 == 0)
newtabstops[col >> 3] |= mask;
else
newtabstops[col >> 3] &= ~mask;
}
vterm_allocator_free(state->vt, state->tabstops);
state->tabstops = newtabstops;
}
if(rows != state->rows) {
VTermLineInfo *newlineinfo = vterm_allocator_malloc(state->vt, rows * sizeof(VTermLineInfo));
int row;
for(row = 0; row < state->rows && row < rows; row++) {
newlineinfo[row] = state->lineinfo[row];
}
for( ; row < rows; row++) {
newlineinfo[row] = (VTermLineInfo){
.doublewidth = 0,
};
}
vterm_allocator_free(state->vt, state->lineinfo);
state->lineinfo = newlineinfo;
}
state->rows = rows;
state->cols = cols;
if(state->scrollregion_bottom > -1)
UBOUND(state->scrollregion_bottom, state->rows);
if(state->scrollregion_right > -1)
UBOUND(state->scrollregion_right, state->cols);
VTermPos delta = { 0, 0 };
if(state->callbacks && state->callbacks->resize)
(*state->callbacks->resize)(rows, cols, &delta, state->cbdata);
if(state->at_phantom && state->pos.col < cols-1) {
state->at_phantom = 0;
state->pos.col++;
}
state->pos.row += delta.row;
state->pos.col += delta.col;
if(state->pos.row >= rows)
state->pos.row = rows - 1;
if(state->pos.col >= cols)
state->pos.col = cols - 1;
updatecursor(state, &oldpos, 1);
return 1;
}
static const VTermParserCallbacks parser_callbacks = {
.text = on_text,
.control = on_control,
.escape = on_escape,
.csi = on_csi,
.osc = on_osc,
.dcs = on_dcs,
.resize = on_resize,
};
VTermState *vterm_obtain_state(VTerm *vt)
{
if(vt->state)
return vt->state;
VTermState *state = vterm_state_new(vt);
vt->state = state;
state->combine_chars_size = 16;
state->combine_chars = vterm_allocator_malloc(state->vt, state->combine_chars_size * sizeof(state->combine_chars[0]));
state->tabstops = vterm_allocator_malloc(state->vt, (state->cols + 7) / 8);
state->lineinfo = vterm_allocator_malloc(state->vt, state->rows * sizeof(VTermLineInfo));
state->encoding_utf8.enc = vterm_lookup_encoding(ENC_UTF8, 'u');
if(*state->encoding_utf8.enc->init)
(*state->encoding_utf8.enc->init)(state->encoding_utf8.enc, state->encoding_utf8.data);
vterm_parser_set_callbacks(vt, &parser_callbacks, state);
return state;
}
void vterm_state_reset(VTermState *state, int hard)
{
state->scrollregion_top = 0;
state->scrollregion_bottom = -1;
state->scrollregion_left = 0;
state->scrollregion_right = -1;
state->mode.keypad = 0;
state->mode.cursor = 0;
state->mode.autowrap = 1;
state->mode.insert = 0;
state->mode.newline = 0;
state->mode.alt_screen = 0;
state->mode.origin = 0;
state->mode.leftrightmargin = 0;
state->mode.bracketpaste = 0;
state->mode.report_focus = 0;
state->vt->mode.ctrl8bit = 0;
for(int col = 0; col < state->cols; col++)
if(col % 8 == 0)
set_col_tabstop(state, col);
else
clear_col_tabstop(state, col);
for(int row = 0; row < state->rows; row++)
set_lineinfo(state, row, FORCE, DWL_OFF, DHL_OFF);
if(state->callbacks && state->callbacks->initpen)
(*state->callbacks->initpen)(state->cbdata);
vterm_state_resetpen(state);
VTermEncoding *default_enc = state->vt->mode.utf8 ?
vterm_lookup_encoding(ENC_UTF8, 'u') :
vterm_lookup_encoding(ENC_SINGLE_94, 'B');
for(int i = 0; i < 4; i++) {
state->encoding[i].enc = default_enc;
if(default_enc->init)
(*default_enc->init)(default_enc, state->encoding[i].data);
}
state->gl_set = 0;
state->gr_set = 1;
state->gsingle_set = 0;
state->protected_cell = 0;
// Initialise the props
settermprop_bool(state, VTERM_PROP_CURSORVISIBLE, 1);
settermprop_bool(state, VTERM_PROP_CURSORBLINK, 1);
settermprop_int (state, VTERM_PROP_CURSORSHAPE, VTERM_PROP_CURSORSHAPE_BLOCK);
if(hard) {
state->pos.row = 0;
state->pos.col = 0;
state->at_phantom = 0;
VTermRect rect = { 0, state->rows, 0, state->cols };
erase(state, rect, 0);
}
}
void vterm_state_get_cursorpos(const VTermState *state, VTermPos *cursorpos)
{
*cursorpos = state->pos;
}
void vterm_state_set_callbacks(VTermState *state, const VTermStateCallbacks *callbacks, void *user)
{
if(callbacks) {
state->callbacks = callbacks;
state->cbdata = user;
if(state->callbacks && state->callbacks->initpen)
(*state->callbacks->initpen)(state->cbdata);
}
else {
state->callbacks = NULL;
state->cbdata = NULL;
}
}
void *vterm_state_get_cbdata(VTermState *state)
{
return state->cbdata;
}
void vterm_state_set_unrecognised_fallbacks(VTermState *state, const VTermParserCallbacks *fallbacks, void *user)
{
if(fallbacks) {
state->fallbacks = fallbacks;
state->fbdata = user;
}
else {
state->fallbacks = NULL;
state->fbdata = NULL;
}
}
void *vterm_state_get_unrecognised_fbdata(VTermState *state)
{
return state->fbdata;
}
int vterm_state_set_termprop(VTermState *state, VTermProp prop, VTermValue *val)
{
/* Only store the new value of the property if usercode said it was happy.
* This is especially important for altscreen switching */
if(state->callbacks && state->callbacks->settermprop)
if(!(*state->callbacks->settermprop)(prop, val, state->cbdata))
return 0;
switch(prop) {
case VTERM_PROP_TITLE:
case VTERM_PROP_ICONNAME:
// we don't store these, just transparently pass through
return 1;
case VTERM_PROP_CURSORVISIBLE:
state->mode.cursor_visible = val->boolean;
return 1;
case VTERM_PROP_CURSORBLINK:
state->mode.cursor_blink = val->boolean;
return 1;
case VTERM_PROP_CURSORSHAPE:
state->mode.cursor_shape = val->number;
return 1;
case VTERM_PROP_REVERSE:
state->mode.screen = val->boolean;
return 1;
case VTERM_PROP_ALTSCREEN:
state->mode.alt_screen = val->boolean;
if(state->mode.alt_screen) {
VTermRect rect = {
.start_row = 0,
.start_col = 0,
.end_row = state->rows,
.end_col = state->cols,
};
erase(state, rect, 0);
}
return 1;
case VTERM_PROP_MOUSE:
state->mouse_flags = 0;
if(val->number)
state->mouse_flags |= MOUSE_WANT_CLICK;
if(val->number == VTERM_PROP_MOUSE_DRAG)
state->mouse_flags |= MOUSE_WANT_DRAG;
if(val->number == VTERM_PROP_MOUSE_MOVE)
state->mouse_flags |= MOUSE_WANT_MOVE;
return 1;
case VTERM_N_PROPS:
return 0;
}
return 0;
}
void vterm_state_focus_in(VTermState *state)
{
if(state->mode.report_focus)
vterm_push_output_sprintf_ctrl(state->vt, C1_CSI, "I");
}
void vterm_state_focus_out(VTermState *state)
{
if(state->mode.report_focus)
vterm_push_output_sprintf_ctrl(state->vt, C1_CSI, "O");
}
const VTermLineInfo *vterm_state_get_lineinfo(const VTermState *state, int row)
{
return state->lineinfo + row;
}
|