1 /* $OpenBSD: grid.c,v 1.95 2019/05/26 17:34:45 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 21 #include <stdlib.h> 22 #include <string.h> 23 24 #include "tmux.h" 25 26 /* 27 * Grid data. This is the basic data structure that represents what is shown on 28 * screen. 29 * 30 * A grid is a grid of cells (struct grid_cell). Lines are not allocated until 31 * cells in that line are written to. The grid is split into history and 32 * viewable data with the history starting at row (line) 0 and extending to 33 * (hsize - 1); from hsize to hsize + (sy - 1) is the viewable data. All 34 * functions in this file work on absolute coordinates, grid-view.c has 35 * functions which work on the screen data. 36 */ 37 38 /* Default grid cell data. */ 39 const struct grid_cell grid_default_cell = { 40 0, 0, 8, 8, { { ' ' }, 0, 1, 1 } 41 }; 42 43 /* Cleared grid cell data. */ 44 const struct grid_cell grid_cleared_cell = { 45 GRID_FLAG_CLEARED, 0, 8, 8, { { ' ' }, 0, 1, 1 } 46 }; 47 static const struct grid_cell_entry grid_cleared_entry = { 48 GRID_FLAG_CLEARED, { .data = { 0, 8, 8, ' ' } } 49 }; 50 51 static void grid_empty_line(struct grid *, u_int, u_int); 52 53 /* Store cell in entry. */ 54 static void 55 grid_store_cell(struct grid_cell_entry *gce, const struct grid_cell *gc, 56 u_char c) 57 { 58 gce->flags = (gc->flags & ~GRID_FLAG_CLEARED); 59 60 gce->data.fg = gc->fg & 0xff; 61 if (gc->fg & COLOUR_FLAG_256) 62 gce->flags |= GRID_FLAG_FG256; 63 64 gce->data.bg = gc->bg & 0xff; 65 if (gc->bg & COLOUR_FLAG_256) 66 gce->flags |= GRID_FLAG_BG256; 67 68 gce->data.attr = gc->attr; 69 gce->data.data = c; 70 } 71 72 /* Check if a cell should be an extended cell. */ 73 static int 74 grid_need_extended_cell(const struct grid_cell_entry *gce, 75 const struct grid_cell *gc) 76 { 77 if (gce->flags & GRID_FLAG_EXTENDED) 78 return (1); 79 if (gc->attr > 0xff) 80 return (1); 81 if (gc->data.size != 1 || gc->data.width != 1) 82 return (1); 83 if ((gc->fg & COLOUR_FLAG_RGB) || (gc->bg & COLOUR_FLAG_RGB)) 84 return (1); 85 return (0); 86 } 87 88 /* Get an extended cell. */ 89 static void 90 grid_get_extended_cell(struct grid_line *gl, struct grid_cell_entry *gce, 91 int flags) 92 { 93 u_int at = gl->extdsize + 1; 94 95 gl->extddata = xreallocarray(gl->extddata, at, sizeof *gl->extddata); 96 gl->extdsize = at; 97 98 gce->offset = at - 1; 99 gce->flags = (flags | GRID_FLAG_EXTENDED); 100 } 101 102 /* Set cell as extended. */ 103 static struct grid_cell * 104 grid_extended_cell(struct grid_line *gl, struct grid_cell_entry *gce, 105 const struct grid_cell *gc) 106 { 107 struct grid_cell *gcp; 108 int flags = (gc->flags & ~GRID_FLAG_CLEARED); 109 110 if (~gce->flags & GRID_FLAG_EXTENDED) 111 grid_get_extended_cell(gl, gce, flags); 112 else if (gce->offset >= gl->extdsize) 113 fatalx("offset too big"); 114 gl->flags |= GRID_LINE_EXTENDED; 115 116 gcp = &gl->extddata[gce->offset]; 117 memcpy(gcp, gc, sizeof *gcp); 118 gcp->flags = flags; 119 return (gcp); 120 } 121 122 /* Free up unused extended cells. */ 123 static void 124 grid_compact_line(struct grid_line *gl) 125 { 126 int new_extdsize = 0; 127 struct grid_cell *new_extddata; 128 struct grid_cell_entry *gce; 129 struct grid_cell *gc; 130 u_int px, idx; 131 132 if (gl->extdsize == 0) 133 return; 134 135 for (px = 0; px < gl->cellsize; px++) { 136 gce = &gl->celldata[px]; 137 if (gce->flags & GRID_FLAG_EXTENDED) 138 new_extdsize++; 139 } 140 141 if (new_extdsize == 0) { 142 free(gl->extddata); 143 gl->extddata = NULL; 144 gl->extdsize = 0; 145 return; 146 } 147 new_extddata = xreallocarray(NULL, new_extdsize, sizeof *gl->extddata); 148 149 idx = 0; 150 for (px = 0; px < gl->cellsize; px++) { 151 gce = &gl->celldata[px]; 152 if (gce->flags & GRID_FLAG_EXTENDED) { 153 gc = &gl->extddata[gce->offset]; 154 memcpy(&new_extddata[idx], gc, sizeof *gc); 155 gce->offset = idx++; 156 } 157 } 158 159 free(gl->extddata); 160 gl->extddata = new_extddata; 161 gl->extdsize = new_extdsize; 162 } 163 164 /* Get line data. */ 165 struct grid_line * 166 grid_get_line(struct grid *gd, u_int line) 167 { 168 return (&gd->linedata[line]); 169 } 170 171 /* Adjust number of lines. */ 172 void 173 grid_adjust_lines(struct grid *gd, u_int lines) 174 { 175 gd->linedata = xreallocarray(gd->linedata, lines, sizeof *gd->linedata); 176 } 177 178 /* Copy default into a cell. */ 179 static void 180 grid_clear_cell(struct grid *gd, u_int px, u_int py, u_int bg) 181 { 182 struct grid_line *gl = &gd->linedata[py]; 183 struct grid_cell_entry *gce = &gl->celldata[px]; 184 struct grid_cell *gc; 185 186 memcpy(gce, &grid_cleared_entry, sizeof *gce); 187 if (bg & COLOUR_FLAG_RGB) { 188 grid_get_extended_cell(gl, gce, gce->flags); 189 gl->flags |= GRID_LINE_EXTENDED; 190 191 gc = &gl->extddata[gce->offset]; 192 memcpy(gc, &grid_cleared_cell, sizeof *gc); 193 gc->bg = bg; 194 } else { 195 if (bg & COLOUR_FLAG_256) 196 gce->flags |= GRID_FLAG_BG256; 197 gce->data.bg = bg; 198 } 199 } 200 201 /* Check grid y position. */ 202 static int 203 grid_check_y(struct grid *gd, const char* from, u_int py) 204 { 205 if (py >= gd->hsize + gd->sy) { 206 log_debug("%s: y out of range: %u", from, py); 207 return (-1); 208 } 209 return (0); 210 } 211 212 /* Compare grid cells. Return 1 if equal, 0 if not. */ 213 int 214 grid_cells_equal(const struct grid_cell *gca, const struct grid_cell *gcb) 215 { 216 if (gca->fg != gcb->fg || gca->bg != gcb->bg) 217 return (0); 218 if (gca->attr != gcb->attr || gca->flags != gcb->flags) 219 return (0); 220 if (gca->data.width != gcb->data.width) 221 return (0); 222 if (gca->data.size != gcb->data.size) 223 return (0); 224 return (memcmp(gca->data.data, gcb->data.data, gca->data.size) == 0); 225 } 226 227 /* Free one line. */ 228 static void 229 grid_free_line(struct grid *gd, u_int py) 230 { 231 free(gd->linedata[py].celldata); 232 gd->linedata[py].celldata = NULL; 233 free(gd->linedata[py].extddata); 234 gd->linedata[py].extddata = NULL; 235 } 236 237 /* Free several lines. */ 238 static void 239 grid_free_lines(struct grid *gd, u_int py, u_int ny) 240 { 241 u_int yy; 242 243 for (yy = py; yy < py + ny; yy++) 244 grid_free_line(gd, yy); 245 } 246 247 /* Create a new grid. */ 248 struct grid * 249 grid_create(u_int sx, u_int sy, u_int hlimit) 250 { 251 struct grid *gd; 252 253 gd = xmalloc(sizeof *gd); 254 gd->sx = sx; 255 gd->sy = sy; 256 257 gd->flags = GRID_HISTORY; 258 259 gd->hscrolled = 0; 260 gd->hsize = 0; 261 gd->hlimit = hlimit; 262 263 if (gd->sy != 0) 264 gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata); 265 else 266 gd->linedata = NULL; 267 268 return (gd); 269 } 270 271 /* Destroy grid. */ 272 void 273 grid_destroy(struct grid *gd) 274 { 275 grid_free_lines(gd, 0, gd->hsize + gd->sy); 276 277 free(gd->linedata); 278 279 free(gd); 280 } 281 282 /* Compare grids. */ 283 int 284 grid_compare(struct grid *ga, struct grid *gb) 285 { 286 struct grid_line *gla, *glb; 287 struct grid_cell gca, gcb; 288 u_int xx, yy; 289 290 if (ga->sx != gb->sx || ga->sy != gb->sy) 291 return (1); 292 293 for (yy = 0; yy < ga->sy; yy++) { 294 gla = &ga->linedata[yy]; 295 glb = &gb->linedata[yy]; 296 if (gla->cellsize != glb->cellsize) 297 return (1); 298 for (xx = 0; xx < gla->cellsize; xx++) { 299 grid_get_cell(ga, xx, yy, &gca); 300 grid_get_cell(gb, xx, yy, &gcb); 301 if (!grid_cells_equal(&gca, &gcb)) 302 return (1); 303 } 304 } 305 306 return (0); 307 } 308 309 /* Trim lines from the history. */ 310 static void 311 grid_trim_history(struct grid *gd, u_int ny) 312 { 313 grid_free_lines(gd, 0, ny); 314 memmove(&gd->linedata[0], &gd->linedata[ny], 315 (gd->hsize + gd->sy - ny) * (sizeof *gd->linedata)); 316 } 317 318 /* 319 * Collect lines from the history if at the limit. Free the top (oldest) 10% 320 * and shift up. 321 */ 322 void 323 grid_collect_history(struct grid *gd) 324 { 325 u_int ny; 326 327 if (gd->hsize == 0 || gd->hsize < gd->hlimit) 328 return; 329 330 ny = gd->hlimit / 10; 331 if (ny < 1) 332 ny = 1; 333 if (ny > gd->hsize) 334 ny = gd->hsize; 335 336 /* 337 * Free the lines from 0 to ny then move the remaining lines over 338 * them. 339 */ 340 grid_trim_history(gd, ny); 341 342 gd->hsize -= ny; 343 if (gd->hscrolled > gd->hsize) 344 gd->hscrolled = gd->hsize; 345 } 346 347 /* 348 * Scroll the entire visible screen, moving one line into the history. Just 349 * allocate a new line at the bottom and move the history size indicator. 350 */ 351 void 352 grid_scroll_history(struct grid *gd, u_int bg) 353 { 354 u_int yy; 355 356 yy = gd->hsize + gd->sy; 357 gd->linedata = xreallocarray(gd->linedata, yy + 1, 358 sizeof *gd->linedata); 359 grid_empty_line(gd, yy, bg); 360 361 gd->hscrolled++; 362 grid_compact_line(&gd->linedata[gd->hsize]); 363 gd->hsize++; 364 } 365 366 /* Clear the history. */ 367 void 368 grid_clear_history(struct grid *gd) 369 { 370 grid_trim_history(gd, gd->hsize); 371 372 gd->hscrolled = 0; 373 gd->hsize = 0; 374 375 gd->linedata = xreallocarray(gd->linedata, gd->sy, 376 sizeof *gd->linedata); 377 } 378 379 /* Scroll a region up, moving the top line into the history. */ 380 void 381 grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower, u_int bg) 382 { 383 struct grid_line *gl_history, *gl_upper; 384 u_int yy; 385 386 /* Create a space for a new line. */ 387 yy = gd->hsize + gd->sy; 388 gd->linedata = xreallocarray(gd->linedata, yy + 1, 389 sizeof *gd->linedata); 390 391 /* Move the entire screen down to free a space for this line. */ 392 gl_history = &gd->linedata[gd->hsize]; 393 memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history); 394 395 /* Adjust the region and find its start and end. */ 396 upper++; 397 gl_upper = &gd->linedata[upper]; 398 lower++; 399 400 /* Move the line into the history. */ 401 memcpy(gl_history, gl_upper, sizeof *gl_history); 402 403 /* Then move the region up and clear the bottom line. */ 404 memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper); 405 grid_empty_line(gd, lower, bg); 406 407 /* Move the history offset down over the line. */ 408 gd->hscrolled++; 409 gd->hsize++; 410 } 411 412 /* Expand line to fit to cell. */ 413 static void 414 grid_expand_line(struct grid *gd, u_int py, u_int sx, u_int bg) 415 { 416 struct grid_line *gl; 417 u_int xx; 418 419 gl = &gd->linedata[py]; 420 if (sx <= gl->cellsize) 421 return; 422 423 if (sx < gd->sx / 4) 424 sx = gd->sx / 4; 425 else if (sx < gd->sx / 2) 426 sx = gd->sx / 2; 427 else 428 sx = gd->sx; 429 430 gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata); 431 for (xx = gl->cellsize; xx < sx; xx++) 432 grid_clear_cell(gd, xx, py, bg); 433 gl->cellsize = sx; 434 } 435 436 /* Empty a line and set background colour if needed. */ 437 static void 438 grid_empty_line(struct grid *gd, u_int py, u_int bg) 439 { 440 memset(&gd->linedata[py], 0, sizeof gd->linedata[py]); 441 if (!COLOUR_DEFAULT(bg)) 442 grid_expand_line(gd, py, gd->sx, bg); 443 } 444 445 /* Peek at grid line. */ 446 const struct grid_line * 447 grid_peek_line(struct grid *gd, u_int py) 448 { 449 if (grid_check_y(gd, __func__, py) != 0) 450 return (NULL); 451 return (&gd->linedata[py]); 452 } 453 454 /* Get cell from line. */ 455 static void 456 grid_get_cell1(struct grid_line *gl, u_int px, struct grid_cell *gc) 457 { 458 struct grid_cell_entry *gce = &gl->celldata[px]; 459 460 if (gce->flags & GRID_FLAG_EXTENDED) { 461 if (gce->offset >= gl->extdsize) 462 memcpy(gc, &grid_default_cell, sizeof *gc); 463 else 464 memcpy(gc, &gl->extddata[gce->offset], sizeof *gc); 465 return; 466 } 467 468 gc->flags = gce->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256); 469 gc->attr = gce->data.attr; 470 gc->fg = gce->data.fg; 471 if (gce->flags & GRID_FLAG_FG256) 472 gc->fg |= COLOUR_FLAG_256; 473 gc->bg = gce->data.bg; 474 if (gce->flags & GRID_FLAG_BG256) 475 gc->bg |= COLOUR_FLAG_256; 476 utf8_set(&gc->data, gce->data.data); 477 } 478 479 /* Get cell for reading. */ 480 void 481 grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc) 482 { 483 if (grid_check_y(gd, __func__, py) != 0 || 484 px >= gd->linedata[py].cellsize) 485 memcpy(gc, &grid_default_cell, sizeof *gc); 486 else 487 grid_get_cell1(&gd->linedata[py], px, gc); 488 } 489 490 /* Set cell at relative position. */ 491 void 492 grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc) 493 { 494 struct grid_line *gl; 495 struct grid_cell_entry *gce; 496 497 if (grid_check_y(gd, __func__, py) != 0) 498 return; 499 500 grid_expand_line(gd, py, px + 1, 8); 501 502 gl = &gd->linedata[py]; 503 if (px + 1 > gl->cellused) 504 gl->cellused = px + 1; 505 506 gce = &gl->celldata[px]; 507 if (grid_need_extended_cell(gce, gc)) 508 grid_extended_cell(gl, gce, gc); 509 else 510 grid_store_cell(gce, gc, gc->data.data[0]); 511 } 512 513 /* Set cells at relative position. */ 514 void 515 grid_set_cells(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc, 516 const char *s, size_t slen) 517 { 518 struct grid_line *gl; 519 struct grid_cell_entry *gce; 520 struct grid_cell *gcp; 521 u_int i; 522 523 if (grid_check_y(gd, __func__, py) != 0) 524 return; 525 526 grid_expand_line(gd, py, px + slen, 8); 527 528 gl = &gd->linedata[py]; 529 if (px + slen > gl->cellused) 530 gl->cellused = px + slen; 531 532 for (i = 0; i < slen; i++) { 533 gce = &gl->celldata[px + i]; 534 if (grid_need_extended_cell(gce, gc)) { 535 gcp = grid_extended_cell(gl, gce, gc); 536 utf8_set(&gcp->data, s[i]); 537 } else 538 grid_store_cell(gce, gc, s[i]); 539 } 540 } 541 542 /* Clear area. */ 543 void 544 grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny, u_int bg) 545 { 546 struct grid_line *gl; 547 u_int xx, yy; 548 549 if (nx == 0 || ny == 0) 550 return; 551 552 if (px == 0 && nx == gd->sx) { 553 grid_clear_lines(gd, py, ny, bg); 554 return; 555 } 556 557 if (grid_check_y(gd, __func__, py) != 0) 558 return; 559 if (grid_check_y(gd, __func__, py + ny - 1) != 0) 560 return; 561 562 for (yy = py; yy < py + ny; yy++) { 563 gl = &gd->linedata[yy]; 564 if (px + nx >= gd->sx && px < gl->cellused) 565 gl->cellused = px; 566 if (px > gl->cellsize && COLOUR_DEFAULT(bg)) 567 continue; 568 if (px + nx >= gl->cellsize && COLOUR_DEFAULT(bg)) { 569 gl->cellsize = px; 570 continue; 571 } 572 grid_expand_line(gd, yy, px + nx, 8); /* default bg first */ 573 for (xx = px; xx < px + nx; xx++) 574 grid_clear_cell(gd, xx, yy, bg); 575 } 576 } 577 578 /* Clear lines. This just frees and truncates the lines. */ 579 void 580 grid_clear_lines(struct grid *gd, u_int py, u_int ny, u_int bg) 581 { 582 u_int yy; 583 584 if (ny == 0) 585 return; 586 587 if (grid_check_y(gd, __func__, py) != 0) 588 return; 589 if (grid_check_y(gd, __func__, py + ny - 1) != 0) 590 return; 591 592 for (yy = py; yy < py + ny; yy++) { 593 grid_free_line(gd, yy); 594 grid_empty_line(gd, yy, bg); 595 } 596 } 597 598 /* Move a group of lines. */ 599 void 600 grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny, u_int bg) 601 { 602 u_int yy; 603 604 if (ny == 0 || py == dy) 605 return; 606 607 if (grid_check_y(gd, __func__, py) != 0) 608 return; 609 if (grid_check_y(gd, __func__, py + ny - 1) != 0) 610 return; 611 if (grid_check_y(gd, __func__, dy) != 0) 612 return; 613 if (grid_check_y(gd, __func__, dy + ny - 1) != 0) 614 return; 615 616 /* Free any lines which are being replaced. */ 617 for (yy = dy; yy < dy + ny; yy++) { 618 if (yy >= py && yy < py + ny) 619 continue; 620 grid_free_line(gd, yy); 621 } 622 623 memmove(&gd->linedata[dy], &gd->linedata[py], 624 ny * (sizeof *gd->linedata)); 625 626 /* 627 * Wipe any lines that have been moved (without freeing them - they are 628 * still present). 629 */ 630 for (yy = py; yy < py + ny; yy++) { 631 if (yy < dy || yy >= dy + ny) 632 grid_empty_line(gd, yy, bg); 633 } 634 } 635 636 /* Move a group of cells. */ 637 void 638 grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx, 639 u_int bg) 640 { 641 struct grid_line *gl; 642 u_int xx; 643 644 if (nx == 0 || px == dx) 645 return; 646 647 if (grid_check_y(gd, __func__, py) != 0) 648 return; 649 gl = &gd->linedata[py]; 650 651 grid_expand_line(gd, py, px + nx, 8); 652 grid_expand_line(gd, py, dx + nx, 8); 653 memmove(&gl->celldata[dx], &gl->celldata[px], 654 nx * sizeof *gl->celldata); 655 if (dx + nx > gl->cellused) 656 gl->cellused = dx + nx; 657 658 /* Wipe any cells that have been moved. */ 659 for (xx = px; xx < px + nx; xx++) { 660 if (xx >= dx && xx < dx + nx) 661 continue; 662 grid_clear_cell(gd, xx, py, bg); 663 } 664 } 665 666 /* Get ANSI foreground sequence. */ 667 static size_t 668 grid_string_cells_fg(const struct grid_cell *gc, int *values) 669 { 670 size_t n; 671 u_char r, g, b; 672 673 n = 0; 674 if (gc->fg & COLOUR_FLAG_256) { 675 values[n++] = 38; 676 values[n++] = 5; 677 values[n++] = gc->fg & 0xff; 678 } else if (gc->fg & COLOUR_FLAG_RGB) { 679 values[n++] = 38; 680 values[n++] = 2; 681 colour_split_rgb(gc->fg, &r, &g, &b); 682 values[n++] = r; 683 values[n++] = g; 684 values[n++] = b; 685 } else { 686 switch (gc->fg) { 687 case 0: 688 case 1: 689 case 2: 690 case 3: 691 case 4: 692 case 5: 693 case 6: 694 case 7: 695 values[n++] = gc->fg + 30; 696 break; 697 case 8: 698 values[n++] = 39; 699 break; 700 case 90: 701 case 91: 702 case 92: 703 case 93: 704 case 94: 705 case 95: 706 case 96: 707 case 97: 708 values[n++] = gc->fg; 709 break; 710 } 711 } 712 return (n); 713 } 714 715 /* Get ANSI background sequence. */ 716 static size_t 717 grid_string_cells_bg(const struct grid_cell *gc, int *values) 718 { 719 size_t n; 720 u_char r, g, b; 721 722 n = 0; 723 if (gc->bg & COLOUR_FLAG_256) { 724 values[n++] = 48; 725 values[n++] = 5; 726 values[n++] = gc->bg & 0xff; 727 } else if (gc->bg & COLOUR_FLAG_RGB) { 728 values[n++] = 48; 729 values[n++] = 2; 730 colour_split_rgb(gc->bg, &r, &g, &b); 731 values[n++] = r; 732 values[n++] = g; 733 values[n++] = b; 734 } else { 735 switch (gc->bg) { 736 case 0: 737 case 1: 738 case 2: 739 case 3: 740 case 4: 741 case 5: 742 case 6: 743 case 7: 744 values[n++] = gc->bg + 40; 745 break; 746 case 8: 747 values[n++] = 49; 748 break; 749 case 100: 750 case 101: 751 case 102: 752 case 103: 753 case 104: 754 case 105: 755 case 106: 756 case 107: 757 values[n++] = gc->bg - 10; 758 break; 759 } 760 } 761 return (n); 762 } 763 764 /* 765 * Returns ANSI code to set particular attributes (colour, bold and so on) 766 * given a current state. 767 */ 768 static void 769 grid_string_cells_code(const struct grid_cell *lastgc, 770 const struct grid_cell *gc, char *buf, size_t len, int escape_c0) 771 { 772 int oldc[64], newc[64], s[128]; 773 size_t noldc, nnewc, n, i; 774 u_int attr = gc->attr, lastattr = lastgc->attr; 775 char tmp[64]; 776 777 struct { 778 u_int mask; 779 u_int code; 780 } attrs[] = { 781 { GRID_ATTR_BRIGHT, 1 }, 782 { GRID_ATTR_DIM, 2 }, 783 { GRID_ATTR_ITALICS, 3 }, 784 { GRID_ATTR_UNDERSCORE, 4 }, 785 { GRID_ATTR_BLINK, 5 }, 786 { GRID_ATTR_REVERSE, 7 }, 787 { GRID_ATTR_HIDDEN, 8 }, 788 { GRID_ATTR_STRIKETHROUGH, 9 }, 789 { GRID_ATTR_UNDERSCORE_2, 42 }, 790 { GRID_ATTR_UNDERSCORE_3, 43 }, 791 { GRID_ATTR_UNDERSCORE_4, 44 }, 792 { GRID_ATTR_UNDERSCORE_5, 45 }, 793 { GRID_ATTR_OVERLINE, 53 }, 794 }; 795 n = 0; 796 797 /* If any attribute is removed, begin with 0. */ 798 for (i = 0; i < nitems(attrs); i++) { 799 if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) { 800 s[n++] = 0; 801 lastattr &= GRID_ATTR_CHARSET; 802 break; 803 } 804 } 805 /* For each attribute that is newly set, add its code. */ 806 for (i = 0; i < nitems(attrs); i++) { 807 if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask)) 808 s[n++] = attrs[i].code; 809 } 810 811 /* Write the attributes. */ 812 *buf = '\0'; 813 if (n > 0) { 814 if (escape_c0) 815 strlcat(buf, "\\033[", len); 816 else 817 strlcat(buf, "\033[", len); 818 for (i = 0; i < n; i++) { 819 if (s[i] < 10) 820 xsnprintf(tmp, sizeof tmp, "%d", s[i]); 821 else { 822 xsnprintf(tmp, sizeof tmp, "%d:%d", s[i] / 10, 823 s[i] % 10); 824 } 825 strlcat(buf, tmp, len); 826 if (i + 1 < n) 827 strlcat(buf, ";", len); 828 } 829 strlcat(buf, "m", len); 830 } 831 832 /* If the foreground colour changed, write its parameters. */ 833 nnewc = grid_string_cells_fg(gc, newc); 834 noldc = grid_string_cells_fg(lastgc, oldc); 835 if (nnewc != noldc || 836 memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 || 837 (n != 0 && s[0] == 0)) { 838 if (escape_c0) 839 strlcat(buf, "\\033[", len); 840 else 841 strlcat(buf, "\033[", len); 842 for (i = 0; i < nnewc; i++) { 843 if (i + 1 < nnewc) 844 xsnprintf(tmp, sizeof tmp, "%d;", newc[i]); 845 else 846 xsnprintf(tmp, sizeof tmp, "%d", newc[i]); 847 strlcat(buf, tmp, len); 848 } 849 strlcat(buf, "m", len); 850 } 851 852 /* If the background colour changed, append its parameters. */ 853 nnewc = grid_string_cells_bg(gc, newc); 854 noldc = grid_string_cells_bg(lastgc, oldc); 855 if (nnewc != noldc || 856 memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 || 857 (n != 0 && s[0] == 0)) { 858 if (escape_c0) 859 strlcat(buf, "\\033[", len); 860 else 861 strlcat(buf, "\033[", len); 862 for (i = 0; i < nnewc; i++) { 863 if (i + 1 < nnewc) 864 xsnprintf(tmp, sizeof tmp, "%d;", newc[i]); 865 else 866 xsnprintf(tmp, sizeof tmp, "%d", newc[i]); 867 strlcat(buf, tmp, len); 868 } 869 strlcat(buf, "m", len); 870 } 871 872 /* Append shift in/shift out if needed. */ 873 if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) { 874 if (escape_c0) 875 strlcat(buf, "\\016", len); /* SO */ 876 else 877 strlcat(buf, "\016", len); /* SO */ 878 } 879 if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) { 880 if (escape_c0) 881 strlcat(buf, "\\017", len); /* SI */ 882 else 883 strlcat(buf, "\017", len); /* SI */ 884 } 885 } 886 887 /* Convert cells into a string. */ 888 char * 889 grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx, 890 struct grid_cell **lastgc, int with_codes, int escape_c0, int trim) 891 { 892 struct grid_cell gc; 893 static struct grid_cell lastgc1; 894 const char *data; 895 char *buf, code[128]; 896 size_t len, off, size, codelen; 897 u_int xx; 898 const struct grid_line *gl; 899 900 if (lastgc != NULL && *lastgc == NULL) { 901 memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1); 902 *lastgc = &lastgc1; 903 } 904 905 len = 128; 906 buf = xmalloc(len); 907 off = 0; 908 909 gl = grid_peek_line(gd, py); 910 for (xx = px; xx < px + nx; xx++) { 911 if (gl == NULL || xx >= gl->cellsize) 912 break; 913 grid_get_cell(gd, xx, py, &gc); 914 if (gc.flags & GRID_FLAG_PADDING) 915 continue; 916 917 if (with_codes) { 918 grid_string_cells_code(*lastgc, &gc, code, sizeof code, 919 escape_c0); 920 codelen = strlen(code); 921 memcpy(*lastgc, &gc, sizeof **lastgc); 922 } else 923 codelen = 0; 924 925 data = gc.data.data; 926 size = gc.data.size; 927 if (escape_c0 && size == 1 && *data == '\\') { 928 data = "\\\\"; 929 size = 2; 930 } 931 932 while (len < off + size + codelen + 1) { 933 buf = xreallocarray(buf, 2, len); 934 len *= 2; 935 } 936 937 if (codelen != 0) { 938 memcpy(buf + off, code, codelen); 939 off += codelen; 940 } 941 memcpy(buf + off, data, size); 942 off += size; 943 } 944 945 if (trim) { 946 while (off > 0 && buf[off - 1] == ' ') 947 off--; 948 } 949 buf[off] = '\0'; 950 951 return (buf); 952 } 953 954 /* 955 * Duplicate a set of lines between two grids. Both source and destination 956 * should be big enough. 957 */ 958 void 959 grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy, 960 u_int ny) 961 { 962 struct grid_line *dstl, *srcl; 963 u_int yy; 964 965 if (dy + ny > dst->hsize + dst->sy) 966 ny = dst->hsize + dst->sy - dy; 967 if (sy + ny > src->hsize + src->sy) 968 ny = src->hsize + src->sy - sy; 969 grid_free_lines(dst, dy, ny); 970 971 for (yy = 0; yy < ny; yy++) { 972 srcl = &src->linedata[sy]; 973 dstl = &dst->linedata[dy]; 974 975 memcpy(dstl, srcl, sizeof *dstl); 976 if (srcl->cellsize != 0) { 977 dstl->celldata = xreallocarray(NULL, 978 srcl->cellsize, sizeof *dstl->celldata); 979 memcpy(dstl->celldata, srcl->celldata, 980 srcl->cellsize * sizeof *dstl->celldata); 981 } else 982 dstl->celldata = NULL; 983 984 if (srcl->extdsize != 0) { 985 dstl->extdsize = srcl->extdsize; 986 dstl->extddata = xreallocarray(NULL, dstl->extdsize, 987 sizeof *dstl->extddata); 988 memcpy(dstl->extddata, srcl->extddata, dstl->extdsize * 989 sizeof *dstl->extddata); 990 } 991 992 sy++; 993 dy++; 994 } 995 } 996 997 /* Mark line as dead. */ 998 static void 999 grid_reflow_dead(struct grid_line *gl) 1000 { 1001 memset(gl, 0, sizeof *gl); 1002 gl->flags = GRID_LINE_DEAD; 1003 } 1004 1005 /* Add lines, return the first new one. */ 1006 static struct grid_line * 1007 grid_reflow_add(struct grid *gd, u_int n) 1008 { 1009 struct grid_line *gl; 1010 u_int sy = gd->sy + n; 1011 1012 gd->linedata = xreallocarray(gd->linedata, sy, sizeof *gd->linedata); 1013 gl = &gd->linedata[gd->sy]; 1014 memset(gl, 0, n * (sizeof *gl)); 1015 gd->sy = sy; 1016 return (gl); 1017 } 1018 1019 /* Move a line across. */ 1020 static struct grid_line * 1021 grid_reflow_move(struct grid *gd, struct grid_line *from) 1022 { 1023 struct grid_line *to; 1024 1025 to = grid_reflow_add(gd, 1); 1026 memcpy(to, from, sizeof *to); 1027 grid_reflow_dead(from); 1028 return (to); 1029 } 1030 1031 /* Join line below onto this one. */ 1032 static void 1033 grid_reflow_join(struct grid *target, struct grid *gd, u_int sx, u_int yy, 1034 u_int width, int already) 1035 { 1036 struct grid_line *gl, *from = NULL; 1037 struct grid_cell gc; 1038 u_int lines, left, i, to, line, want = 0; 1039 u_int at; 1040 int wrapped = 1; 1041 1042 /* 1043 * Add a new target line. 1044 */ 1045 if (!already) { 1046 to = target->sy; 1047 gl = grid_reflow_move(target, &gd->linedata[yy]); 1048 } else { 1049 to = target->sy - 1; 1050 gl = &target->linedata[to]; 1051 } 1052 at = gl->cellused; 1053 1054 /* 1055 * Loop until no more to consume or the target line is full. 1056 */ 1057 lines = 0; 1058 for (;;) { 1059 /* 1060 * If this is now the last line, there is nothing more to be 1061 * done. 1062 */ 1063 if (yy + 1 + lines == gd->hsize + gd->sy) 1064 break; 1065 line = yy + 1 + lines; 1066 1067 /* If the next line is empty, skip it. */ 1068 if (~gd->linedata[line].flags & GRID_LINE_WRAPPED) 1069 wrapped = 0; 1070 if (gd->linedata[line].cellused == 0) { 1071 if (!wrapped) 1072 break; 1073 lines++; 1074 continue; 1075 } 1076 1077 /* 1078 * Is the destination line now full? Copy the first character 1079 * separately because we need to leave "from" set to the last 1080 * line if this line is full. 1081 */ 1082 grid_get_cell1(&gd->linedata[line], 0, &gc); 1083 if (width + gc.data.width > sx) 1084 break; 1085 width += gc.data.width; 1086 grid_set_cell(target, at, to, &gc); 1087 at++; 1088 1089 /* Join as much more as possible onto the current line. */ 1090 from = &gd->linedata[line]; 1091 for (want = 1; want < from->cellused; want++) { 1092 grid_get_cell1(from, want, &gc); 1093 if (width + gc.data.width > sx) 1094 break; 1095 width += gc.data.width; 1096 1097 grid_set_cell(target, at, to, &gc); 1098 at++; 1099 } 1100 lines++; 1101 1102 /* 1103 * If this line wasn't wrapped or we didn't consume the entire 1104 * line, don't try to join any further lines. 1105 */ 1106 if (!wrapped || want != from->cellused || width == sx) 1107 break; 1108 } 1109 if (lines == 0) 1110 return; 1111 1112 /* 1113 * If we didn't consume the entire final line, then remove what we did 1114 * consume. If we consumed the entire line and it wasn't wrapped, 1115 * remove the wrap flag from this line. 1116 */ 1117 left = from->cellused - want; 1118 if (left != 0) { 1119 grid_move_cells(gd, 0, want, yy + lines, left, 8); 1120 from->cellsize = from->cellused = left; 1121 lines--; 1122 } else if (!wrapped) 1123 gl->flags &= ~GRID_LINE_WRAPPED; 1124 1125 /* Remove the lines that were completely consumed. */ 1126 for (i = yy + 1; i < yy + 1 + lines; i++) { 1127 free(gd->linedata[i].celldata); 1128 free(gd->linedata[i].extddata); 1129 grid_reflow_dead(&gd->linedata[i]); 1130 } 1131 1132 /* Adjust scroll position. */ 1133 if (gd->hscrolled > to + lines) 1134 gd->hscrolled -= lines; 1135 else if (gd->hscrolled > to) 1136 gd->hscrolled = to; 1137 } 1138 1139 /* Split this line into several new ones */ 1140 static void 1141 grid_reflow_split(struct grid *target, struct grid *gd, u_int sx, u_int yy, 1142 u_int at) 1143 { 1144 struct grid_line *gl = &gd->linedata[yy], *first; 1145 struct grid_cell gc; 1146 u_int line, lines, width, i, xx; 1147 u_int used = gl->cellused; 1148 int flags = gl->flags; 1149 1150 /* How many lines do we need to insert? We know we need at least two. */ 1151 if (~gl->flags & GRID_LINE_EXTENDED) 1152 lines = 1 + (gl->cellused - 1) / sx; 1153 else { 1154 lines = 2; 1155 width = 0; 1156 for (i = at; i < used; i++) { 1157 grid_get_cell1(gl, i, &gc); 1158 if (width + gc.data.width > sx) { 1159 lines++; 1160 width = 0; 1161 } 1162 width += gc.data.width; 1163 } 1164 } 1165 1166 /* Insert new lines. */ 1167 line = target->sy + 1; 1168 first = grid_reflow_add(target, lines); 1169 1170 /* Copy sections from the original line. */ 1171 width = 0; 1172 xx = 0; 1173 for (i = at; i < used; i++) { 1174 grid_get_cell1(gl, i, &gc); 1175 if (width + gc.data.width > sx) { 1176 target->linedata[line].flags |= GRID_LINE_WRAPPED; 1177 1178 line++; 1179 width = 0; 1180 xx = 0; 1181 } 1182 width += gc.data.width; 1183 grid_set_cell(target, xx, line, &gc); 1184 xx++; 1185 } 1186 if (flags & GRID_LINE_WRAPPED) 1187 target->linedata[line].flags |= GRID_LINE_WRAPPED; 1188 1189 /* Move the remainder of the original line. */ 1190 gl->cellsize = gl->cellused = at; 1191 gl->flags |= GRID_LINE_WRAPPED; 1192 memcpy(first, gl, sizeof *first); 1193 grid_reflow_dead(gl); 1194 1195 /* Adjust the scroll position. */ 1196 if (yy <= gd->hscrolled) 1197 gd->hscrolled += lines - 1; 1198 1199 /* 1200 * If the original line had the wrapped flag and there is still space 1201 * in the last new line, try to join with the next lines. 1202 */ 1203 if (width < sx && (flags & GRID_LINE_WRAPPED)) 1204 grid_reflow_join(target, gd, sx, yy, width, 1); 1205 } 1206 1207 /* Reflow lines on grid to new width. */ 1208 void 1209 grid_reflow(struct grid *gd, u_int sx) 1210 { 1211 struct grid *target; 1212 struct grid_line *gl; 1213 struct grid_cell gc; 1214 u_int yy, width, i, at, first; 1215 1216 /* 1217 * Create a destination grid. This is just used as a container for the 1218 * line data and may not be fully valid. 1219 */ 1220 target = grid_create(gd->sx, 0, 0); 1221 1222 /* 1223 * Loop over each source line. 1224 */ 1225 for (yy = 0; yy < gd->hsize + gd->sy; yy++) { 1226 gl = &gd->linedata[yy]; 1227 if (gl->flags & GRID_LINE_DEAD) 1228 continue; 1229 1230 /* 1231 * Work out the width of this line. first is the width of the 1232 * first character, at is the point at which the available 1233 * width is hit, and width is the full line width. 1234 */ 1235 first = at = width = 0; 1236 if (~gl->flags & GRID_LINE_EXTENDED) { 1237 first = 1; 1238 width = gl->cellused; 1239 if (width > sx) 1240 at = sx; 1241 else 1242 at = width; 1243 } else { 1244 for (i = 0; i < gl->cellused; i++) { 1245 grid_get_cell1(gl, i, &gc); 1246 if (i == 0) 1247 first = gc.data.width; 1248 if (at == 0 && width + gc.data.width > sx) 1249 at = i; 1250 width += gc.data.width; 1251 } 1252 } 1253 1254 /* 1255 * If the line is exactly right or the first character is wider 1256 * than the targe width, just move it across unchanged. 1257 */ 1258 if (width == sx || first > sx) { 1259 grid_reflow_move(target, gl); 1260 continue; 1261 } 1262 1263 /* 1264 * If the line is too big, it needs to be split, whether or not 1265 * it was previously wrapped. 1266 */ 1267 if (width > sx) { 1268 grid_reflow_split(target, gd, sx, yy, at); 1269 continue; 1270 } 1271 1272 /* 1273 * If the line was previously wrapped, join as much as possible 1274 * of the next line. 1275 */ 1276 if (gl->flags & GRID_LINE_WRAPPED) 1277 grid_reflow_join(target, gd, sx, yy, width, 0); 1278 else 1279 grid_reflow_move(target, gl); 1280 } 1281 1282 /* 1283 * Replace the old grid with the new. 1284 */ 1285 if (target->sy < gd->sy) 1286 grid_reflow_add(target, gd->sy - target->sy); 1287 gd->hsize = target->sy - gd->sy; 1288 if (gd->hscrolled > gd->hsize) 1289 gd->hscrolled = gd->hsize; 1290 free(gd->linedata); 1291 gd->linedata = target->linedata; 1292 free(target); 1293 } 1294 1295 /* Convert to position based on wrapped lines. */ 1296 void 1297 grid_wrap_position(struct grid *gd, u_int px, u_int py, u_int *wx, u_int *wy) 1298 { 1299 u_int ax = 0, ay = 0, yy; 1300 1301 for (yy = 0; yy < py; yy++) { 1302 if (gd->linedata[yy].flags & GRID_LINE_WRAPPED) 1303 ax += gd->linedata[yy].cellused; 1304 else { 1305 ax = 0; 1306 ay++; 1307 } 1308 } 1309 if (px >= gd->linedata[yy].cellused) 1310 ax = UINT_MAX; 1311 else 1312 ax += px; 1313 *wx = ax; 1314 *wy = ay; 1315 } 1316 1317 /* Convert position based on wrapped lines back. */ 1318 void 1319 grid_unwrap_position(struct grid *gd, u_int *px, u_int *py, u_int wx, u_int wy) 1320 { 1321 u_int yy, ax = 0, ay = 0; 1322 1323 for (yy = 0; yy < gd->hsize + gd->sy - 1; yy++) { 1324 if (ay == wy) 1325 break; 1326 if (gd->linedata[yy].flags & GRID_LINE_WRAPPED) 1327 ax += gd->linedata[yy].cellused; 1328 else { 1329 ax = 0; 1330 ay++; 1331 } 1332 } 1333 1334 /* 1335 * yy is now 0 on the unwrapped line which contains wx. Walk forwards 1336 * until we find the end or the line now containing wx. 1337 */ 1338 if (wx == UINT_MAX) { 1339 while (gd->linedata[yy].flags & GRID_LINE_WRAPPED) 1340 yy++; 1341 wx = gd->linedata[yy].cellused; 1342 } else { 1343 while (gd->linedata[yy].flags & GRID_LINE_WRAPPED) { 1344 if (wx < gd->linedata[yy].cellused) 1345 break; 1346 wx -= gd->linedata[yy].cellused; 1347 yy++; 1348 } 1349 } 1350 *px = wx; 1351 *py = yy; 1352 } 1353 1354 /* Get length of line. */ 1355 u_int 1356 grid_line_length(struct grid *gd, u_int py) 1357 { 1358 struct grid_cell gc; 1359 u_int px; 1360 1361 px = grid_get_line(gd, py)->cellsize; 1362 if (px > gd->sx) 1363 px = gd->sx; 1364 while (px > 0) { 1365 grid_get_cell(gd, px - 1, py, &gc); 1366 if (gc.data.size != 1 || *gc.data.data != ' ') 1367 break; 1368 px--; 1369 } 1370 return (px); 1371 } 1372