1 /* $OpenBSD: screen-write.c,v 1.188 2020/11/09 08:42:43 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 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 static void screen_write_collect_clear(struct screen_write_ctx *, u_int, 27 u_int); 28 static void screen_write_collect_clear_end(struct screen_write_ctx *, u_int, 29 u_int); 30 static void screen_write_collect_clear_start(struct screen_write_ctx *, 31 u_int, u_int); 32 static void screen_write_collect_scroll(struct screen_write_ctx *); 33 static void screen_write_collect_flush(struct screen_write_ctx *, int, 34 const char *); 35 36 static int screen_write_overwrite(struct screen_write_ctx *, 37 struct grid_cell *, u_int); 38 static const struct grid_cell *screen_write_combine(struct screen_write_ctx *, 39 const struct utf8_data *, u_int *); 40 41 struct screen_write_collect_item { 42 u_int x; 43 int wrapped; 44 45 enum { TEXT, CLEAR_END, CLEAR_START } type; 46 u_int used; 47 u_int bg; 48 49 struct grid_cell gc; 50 51 TAILQ_ENTRY(screen_write_collect_item) entry; 52 }; 53 struct screen_write_collect_line { 54 u_int bg; 55 char *data; 56 TAILQ_HEAD(, screen_write_collect_item) items; 57 }; 58 59 static void 60 screen_write_offset_timer(__unused int fd, __unused short events, void *data) 61 { 62 struct window *w = data; 63 64 tty_update_window_offset(w); 65 } 66 67 /* Set cursor position. */ 68 static void 69 screen_write_set_cursor(struct screen_write_ctx *ctx, int cx, int cy) 70 { 71 struct window_pane *wp = ctx->wp; 72 struct window *w; 73 struct screen *s = ctx->s; 74 struct timeval tv = { .tv_usec = 10000 }; 75 76 if (cx != -1 && (u_int)cx == s->cx && cy != -1 && (u_int)cy == s->cy) 77 return; 78 79 if (cx != -1) { 80 if ((u_int)cx > screen_size_x(s)) /* allow last column */ 81 cx = screen_size_x(s) - 1; 82 s->cx = cx; 83 } 84 if (cy != -1) { 85 if ((u_int)cy > screen_size_y(s) - 1) 86 cy = screen_size_y(s) - 1; 87 s->cy = cy; 88 } 89 90 if (wp == NULL) 91 return; 92 w = wp->window; 93 94 if (!event_initialized(&w->offset_timer)) 95 evtimer_set(&w->offset_timer, screen_write_offset_timer, w); 96 if (!evtimer_pending(&w->offset_timer, NULL)) 97 evtimer_add(&w->offset_timer, &tv); 98 } 99 100 /* Do a full redraw. */ 101 static void 102 screen_write_redraw_cb(const struct tty_ctx *ttyctx) 103 { 104 struct window_pane *wp = ttyctx->arg; 105 106 if (wp != NULL) 107 wp->flags |= PANE_REDRAW; 108 } 109 110 /* Update context for client. */ 111 static int 112 screen_write_set_client_cb(struct tty_ctx *ttyctx, struct client *c) 113 { 114 struct window_pane *wp = ttyctx->arg; 115 116 if (c->session->curw->window != wp->window) 117 return (0); 118 if (wp->layout_cell == NULL) 119 return (0); 120 121 if (wp->flags & (PANE_REDRAW|PANE_DROP)) 122 return (-1); 123 if (c->flags & CLIENT_REDRAWPANES) { 124 /* 125 * Redraw is already deferred to redraw another pane - redraw 126 * this one also when that happens. 127 */ 128 log_debug("adding %%%u to deferred redraw", wp->id); 129 wp->flags |= PANE_REDRAW; 130 return (-1); 131 } 132 133 ttyctx->bigger = tty_window_offset(&c->tty, &ttyctx->wox, &ttyctx->woy, 134 &ttyctx->wsx, &ttyctx->wsy); 135 136 ttyctx->xoff = ttyctx->rxoff = wp->xoff; 137 ttyctx->yoff = ttyctx->ryoff = wp->yoff; 138 139 if (status_at_line(c) == 0) 140 ttyctx->yoff += status_line_size(c); 141 142 return (1); 143 } 144 145 /* Set up context for TTY command. */ 146 static void 147 screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx, 148 int sync) 149 { 150 struct screen *s = ctx->s; 151 152 memset(ttyctx, 0, sizeof *ttyctx); 153 154 if (ctx->wp != NULL) { 155 tty_default_colours(&ttyctx->defaults, ctx->wp); 156 ttyctx->palette = ctx->wp->palette; 157 } else { 158 memcpy(&ttyctx->defaults, &grid_default_cell, 159 sizeof ttyctx->defaults); 160 ttyctx->palette = NULL; 161 } 162 163 ttyctx->s = s; 164 ttyctx->sx = screen_size_x(s); 165 ttyctx->sy = screen_size_y(s); 166 167 ttyctx->ocx = s->cx; 168 ttyctx->ocy = s->cy; 169 ttyctx->orlower = s->rlower; 170 ttyctx->orupper = s->rupper; 171 172 if (ctx->init_ctx_cb != NULL) 173 ctx->init_ctx_cb(ctx, ttyctx); 174 else { 175 ttyctx->redraw_cb = screen_write_redraw_cb; 176 if (ctx->wp == NULL) 177 ttyctx->set_client_cb = NULL; 178 else 179 ttyctx->set_client_cb = screen_write_set_client_cb; 180 ttyctx->arg = ctx->wp; 181 } 182 183 if (ctx->wp != NULL && 184 (~ctx->flags & SCREEN_WRITE_SYNC) && 185 (sync || ctx->wp != ctx->wp->window->active)) { 186 tty_write(tty_cmd_syncstart, ttyctx); 187 ctx->flags |= SCREEN_WRITE_SYNC; 188 } 189 } 190 191 /* Make write list. */ 192 void 193 screen_write_make_list(struct screen *s) 194 { 195 u_int y; 196 197 s->write_list = xcalloc(screen_size_y(s), sizeof *s->write_list); 198 for (y = 0; y < screen_size_y(s); y++) 199 TAILQ_INIT(&s->write_list[y].items); 200 } 201 202 /* Free write list. */ 203 void 204 screen_write_free_list(struct screen *s) 205 { 206 u_int y; 207 208 for (y = 0; y < screen_size_y(s); y++) 209 free(s->write_list[y].data); 210 free(s->write_list); 211 } 212 213 /* Set up for writing. */ 214 static void 215 screen_write_init(struct screen_write_ctx *ctx, struct screen *s) 216 { 217 memset(ctx, 0, sizeof *ctx); 218 219 ctx->s = s; 220 221 if (ctx->s->write_list == NULL) 222 screen_write_make_list(ctx->s); 223 ctx->item = xcalloc(1, sizeof *ctx->item); 224 225 ctx->scrolled = 0; 226 ctx->bg = 8; 227 } 228 229 /* Initialize writing with a pane. */ 230 void 231 screen_write_start_pane(struct screen_write_ctx *ctx, struct window_pane *wp, 232 struct screen *s) 233 { 234 if (s == NULL) 235 s = wp->screen; 236 screen_write_init(ctx, s); 237 ctx->wp = wp; 238 239 if (log_get_level() != 0) { 240 log_debug("%s: size %ux%u, pane %%%u (at %u,%u)", 241 __func__, screen_size_x(ctx->s), screen_size_y(ctx->s), 242 wp->id, wp->xoff, wp->yoff); 243 } 244 } 245 246 /* Initialize writing with a callback. */ 247 void 248 screen_write_start_callback(struct screen_write_ctx *ctx, struct screen *s, 249 screen_write_init_ctx_cb cb, void *arg) 250 { 251 screen_write_init(ctx, s); 252 253 ctx->init_ctx_cb = cb; 254 ctx->arg = arg; 255 256 if (log_get_level() != 0) { 257 log_debug("%s: size %ux%u, with callback", __func__, 258 screen_size_x(ctx->s), screen_size_y(ctx->s)); 259 } 260 } 261 262 263 /* Initialize writing. */ 264 void 265 screen_write_start(struct screen_write_ctx *ctx, struct screen *s) 266 { 267 screen_write_init(ctx, s); 268 269 if (log_get_level() != 0) { 270 log_debug("%s: size %ux%u, no pane", __func__, 271 screen_size_x(ctx->s), screen_size_y(ctx->s)); 272 } 273 } 274 275 /* Finish writing. */ 276 void 277 screen_write_stop(struct screen_write_ctx *ctx) 278 { 279 screen_write_collect_end(ctx); 280 screen_write_collect_flush(ctx, 0, __func__); 281 282 log_debug("%s: %u cells (%u written, %u skipped)", __func__, 283 ctx->cells, ctx->written, ctx->skipped); 284 if (ctx->wp != NULL) { 285 ctx->wp->written += ctx->written; 286 ctx->wp->skipped += ctx->skipped; 287 } 288 289 free(ctx->item); 290 } 291 292 /* Reset screen state. */ 293 void 294 screen_write_reset(struct screen_write_ctx *ctx) 295 { 296 struct screen *s = ctx->s; 297 298 screen_reset_tabs(s); 299 screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1); 300 301 s->mode = MODE_CURSOR | MODE_WRAP; 302 303 screen_write_clearscreen(ctx, 8); 304 screen_write_set_cursor(ctx, 0, 0); 305 } 306 307 /* Write character. */ 308 void 309 screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp, 310 u_char ch) 311 { 312 struct grid_cell gc; 313 314 memcpy(&gc, gcp, sizeof gc); 315 316 utf8_set(&gc.data, ch); 317 screen_write_cell(ctx, &gc); 318 } 319 320 /* Calculate string length. */ 321 size_t 322 screen_write_strlen(const char *fmt, ...) 323 { 324 va_list ap; 325 char *msg; 326 struct utf8_data ud; 327 u_char *ptr; 328 size_t left, size = 0; 329 enum utf8_state more; 330 331 va_start(ap, fmt); 332 xvasprintf(&msg, fmt, ap); 333 va_end(ap); 334 335 ptr = msg; 336 while (*ptr != '\0') { 337 if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) { 338 ptr++; 339 340 left = strlen(ptr); 341 if (left < (size_t)ud.size - 1) 342 break; 343 while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE) 344 ptr++; 345 ptr++; 346 347 if (more == UTF8_DONE) 348 size += ud.width; 349 } else { 350 if (*ptr > 0x1f && *ptr < 0x7f) 351 size++; 352 ptr++; 353 } 354 } 355 356 free(msg); 357 return (size); 358 } 359 360 /* Write string wrapped over lines. */ 361 int 362 screen_write_text(struct screen_write_ctx *ctx, u_int cx, u_int width, 363 u_int lines, int more, const struct grid_cell *gcp, const char *fmt, ...) 364 { 365 struct screen *s = ctx->s; 366 va_list ap; 367 char *tmp; 368 u_int cy = s->cy, i, end, next, idx = 0, at, left; 369 struct utf8_data *text; 370 struct grid_cell gc; 371 372 memcpy(&gc, gcp, sizeof gc); 373 374 va_start(ap, fmt); 375 xvasprintf(&tmp, fmt, ap); 376 va_end(ap); 377 378 text = utf8_fromcstr(tmp); 379 free(tmp); 380 381 left = (cx + width) - s->cx; 382 for (;;) { 383 /* Find the end of what can fit on the line. */ 384 at = 0; 385 for (end = idx; text[end].size != 0; end++) { 386 if (text[end].size == 1 && text[end].data[0] == '\n') 387 break; 388 if (at + text[end].width > left) 389 break; 390 at += text[end].width; 391 } 392 393 /* 394 * If we're on a space, that's the end. If not, walk back to 395 * try and find one. 396 */ 397 if (text[end].size == 0) 398 next = end; 399 else if (text[end].size == 1 && text[end].data[0] == '\n') 400 next = end + 1; 401 else if (text[end].size == 1 && text[end].data[0] == ' ') 402 next = end + 1; 403 else { 404 for (i = end; i > idx; i--) { 405 if (text[i].size == 1 && text[i].data[0] == ' ') 406 break; 407 } 408 if (i != idx) { 409 next = i + 1; 410 end = i; 411 } else 412 next = end; 413 } 414 415 /* Print the line. */ 416 for (i = idx; i < end; i++) { 417 utf8_copy(&gc.data, &text[i]); 418 screen_write_cell(ctx, &gc); 419 } 420 421 /* If at the bottom, stop. */ 422 idx = next; 423 if (s->cy == cy + lines - 1 || text[idx].size == 0) 424 break; 425 426 screen_write_cursormove(ctx, cx, s->cy + 1, 0); 427 left = width; 428 } 429 430 /* 431 * Fail if on the last line and there is more to come or at the end, or 432 * if the text was not entirely consumed. 433 */ 434 if ((s->cy == cy + lines - 1 && (!more || s->cx == cx + width)) || 435 text[idx].size != 0) { 436 free(text); 437 return (0); 438 } 439 free(text); 440 441 /* 442 * If no more to come, move to the next line. Otherwise, leave on 443 * the same line (except if at the end). 444 */ 445 if (!more || s->cx == cx + width) 446 screen_write_cursormove(ctx, cx, s->cy + 1, 0); 447 return (1); 448 } 449 450 /* Write simple string (no maximum length). */ 451 void 452 screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp, 453 const char *fmt, ...) 454 { 455 va_list ap; 456 457 va_start(ap, fmt); 458 screen_write_vnputs(ctx, -1, gcp, fmt, ap); 459 va_end(ap); 460 } 461 462 /* Write string with length limit (-1 for unlimited). */ 463 void 464 screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen, 465 const struct grid_cell *gcp, const char *fmt, ...) 466 { 467 va_list ap; 468 469 va_start(ap, fmt); 470 screen_write_vnputs(ctx, maxlen, gcp, fmt, ap); 471 va_end(ap); 472 } 473 474 void 475 screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen, 476 const struct grid_cell *gcp, const char *fmt, va_list ap) 477 { 478 struct grid_cell gc; 479 struct utf8_data *ud = &gc.data; 480 char *msg; 481 u_char *ptr; 482 size_t left, size = 0; 483 enum utf8_state more; 484 485 memcpy(&gc, gcp, sizeof gc); 486 xvasprintf(&msg, fmt, ap); 487 488 ptr = msg; 489 while (*ptr != '\0') { 490 if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) { 491 ptr++; 492 493 left = strlen(ptr); 494 if (left < (size_t)ud->size - 1) 495 break; 496 while ((more = utf8_append(ud, *ptr)) == UTF8_MORE) 497 ptr++; 498 ptr++; 499 500 if (more != UTF8_DONE) 501 continue; 502 if (maxlen > 0 && size + ud->width > (size_t)maxlen) { 503 while (size < (size_t)maxlen) { 504 screen_write_putc(ctx, &gc, ' '); 505 size++; 506 } 507 break; 508 } 509 size += ud->width; 510 screen_write_cell(ctx, &gc); 511 } else { 512 if (maxlen > 0 && size + 1 > (size_t)maxlen) 513 break; 514 515 if (*ptr == '\001') 516 gc.attr ^= GRID_ATTR_CHARSET; 517 else if (*ptr == '\n') { 518 screen_write_linefeed(ctx, 0, 8); 519 screen_write_carriagereturn(ctx); 520 } else if (*ptr > 0x1f && *ptr < 0x7f) { 521 size++; 522 screen_write_putc(ctx, &gc, *ptr); 523 } 524 ptr++; 525 } 526 } 527 528 free(msg); 529 } 530 531 /* 532 * Copy from another screen but without the selection stuff. Assumes the target 533 * region is already big enough. 534 */ 535 void 536 screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src, 537 u_int px, u_int py, u_int nx, u_int ny) 538 { 539 struct screen *s = ctx->s; 540 struct grid *gd = src->grid; 541 struct grid_cell gc; 542 u_int xx, yy, cx, cy; 543 544 if (nx == 0 || ny == 0) 545 return; 546 547 cy = s->cy; 548 for (yy = py; yy < py + ny; yy++) { 549 if (yy >= gd->hsize + gd->sy) 550 break; 551 cx = s->cx; 552 for (xx = px; xx < px + nx; xx++) { 553 if (xx >= grid_get_line(gd, yy)->cellsize) 554 break; 555 grid_get_cell(gd, xx, yy, &gc); 556 if (xx + gc.data.width > px + nx) 557 break; 558 grid_view_set_cell(ctx->s->grid, cx, cy, &gc); 559 cx++; 560 } 561 cy++; 562 } 563 } 564 565 /* Draw a horizontal line on screen. */ 566 void 567 screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right) 568 { 569 struct screen *s = ctx->s; 570 struct grid_cell gc; 571 u_int cx, cy, i; 572 573 cx = s->cx; 574 cy = s->cy; 575 576 memcpy(&gc, &grid_default_cell, sizeof gc); 577 gc.attr |= GRID_ATTR_CHARSET; 578 579 screen_write_putc(ctx, &gc, left ? 't' : 'q'); 580 for (i = 1; i < nx - 1; i++) 581 screen_write_putc(ctx, &gc, 'q'); 582 screen_write_putc(ctx, &gc, right ? 'u' : 'q'); 583 584 screen_write_set_cursor(ctx, cx, cy); 585 } 586 587 /* Draw a horizontal line on screen. */ 588 void 589 screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom) 590 { 591 struct screen *s = ctx->s; 592 struct grid_cell gc; 593 u_int cx, cy, i; 594 595 cx = s->cx; 596 cy = s->cy; 597 598 memcpy(&gc, &grid_default_cell, sizeof gc); 599 gc.attr |= GRID_ATTR_CHARSET; 600 601 screen_write_putc(ctx, &gc, top ? 'w' : 'x'); 602 for (i = 1; i < ny - 1; i++) { 603 screen_write_set_cursor(ctx, cx, cy + i); 604 screen_write_putc(ctx, &gc, 'x'); 605 } 606 screen_write_set_cursor(ctx, cx, cy + ny - 1); 607 screen_write_putc(ctx, &gc, bottom ? 'v' : 'x'); 608 609 screen_write_set_cursor(ctx, cx, cy); 610 } 611 612 /* Draw a menu on screen. */ 613 void 614 screen_write_menu(struct screen_write_ctx *ctx, struct menu *menu, 615 int choice, const struct grid_cell *choice_gc) 616 { 617 struct screen *s = ctx->s; 618 struct grid_cell default_gc; 619 const struct grid_cell *gc = &default_gc; 620 u_int cx, cy, i, j; 621 const char *name; 622 623 cx = s->cx; 624 cy = s->cy; 625 626 memcpy(&default_gc, &grid_default_cell, sizeof default_gc); 627 628 screen_write_box(ctx, menu->width + 4, menu->count + 2); 629 screen_write_cursormove(ctx, cx + 2, cy, 0); 630 format_draw(ctx, &default_gc, menu->width, menu->title, NULL); 631 632 for (i = 0; i < menu->count; i++) { 633 name = menu->items[i].name; 634 if (name == NULL) { 635 screen_write_cursormove(ctx, cx, cy + 1 + i, 0); 636 screen_write_hline(ctx, menu->width + 4, 1, 1); 637 } else { 638 if (choice >= 0 && i == (u_int)choice && *name != '-') 639 gc = choice_gc; 640 screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0); 641 for (j = 0; j < menu->width; j++) 642 screen_write_putc(ctx, gc, ' '); 643 screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0); 644 if (*name == '-') { 645 name++; 646 default_gc.attr |= GRID_ATTR_DIM; 647 format_draw(ctx, gc, menu->width, name, NULL); 648 default_gc.attr &= ~GRID_ATTR_DIM; 649 } else 650 format_draw(ctx, gc, menu->width, name, NULL); 651 gc = &default_gc; 652 } 653 } 654 655 screen_write_set_cursor(ctx, cx, cy); 656 } 657 658 /* Draw a box on screen. */ 659 void 660 screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny) 661 { 662 struct screen *s = ctx->s; 663 struct grid_cell gc; 664 u_int cx, cy, i; 665 666 cx = s->cx; 667 cy = s->cy; 668 669 memcpy(&gc, &grid_default_cell, sizeof gc); 670 gc.attr |= GRID_ATTR_CHARSET; 671 672 screen_write_putc(ctx, &gc, 'l'); 673 for (i = 1; i < nx - 1; i++) 674 screen_write_putc(ctx, &gc, 'q'); 675 screen_write_putc(ctx, &gc, 'k'); 676 677 screen_write_set_cursor(ctx, cx, cy + ny - 1); 678 screen_write_putc(ctx, &gc, 'm'); 679 for (i = 1; i < nx - 1; i++) 680 screen_write_putc(ctx, &gc, 'q'); 681 screen_write_putc(ctx, &gc, 'j'); 682 683 for (i = 1; i < ny - 1; i++) { 684 screen_write_set_cursor(ctx, cx, cy + i); 685 screen_write_putc(ctx, &gc, 'x'); 686 } 687 for (i = 1; i < ny - 1; i++) { 688 screen_write_set_cursor(ctx, cx + nx - 1, cy + i); 689 screen_write_putc(ctx, &gc, 'x'); 690 } 691 692 screen_write_set_cursor(ctx, cx, cy); 693 } 694 695 /* 696 * Write a preview version of a window. Assumes target area is big enough and 697 * already cleared. 698 */ 699 void 700 screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx, 701 u_int ny) 702 { 703 struct screen *s = ctx->s; 704 struct grid_cell gc; 705 u_int cx, cy, px, py; 706 707 cx = s->cx; 708 cy = s->cy; 709 710 /* 711 * If the cursor is on, pick the area around the cursor, otherwise use 712 * the top left. 713 */ 714 if (src->mode & MODE_CURSOR) { 715 px = src->cx; 716 if (px < nx / 3) 717 px = 0; 718 else 719 px = px - nx / 3; 720 if (px + nx > screen_size_x(src)) { 721 if (nx > screen_size_x(src)) 722 px = 0; 723 else 724 px = screen_size_x(src) - nx; 725 } 726 py = src->cy; 727 if (py < ny / 3) 728 py = 0; 729 else 730 py = py - ny / 3; 731 if (py + ny > screen_size_y(src)) { 732 if (ny > screen_size_y(src)) 733 py = 0; 734 else 735 py = screen_size_y(src) - ny; 736 } 737 } else { 738 px = 0; 739 py = 0; 740 } 741 742 screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny); 743 744 if (src->mode & MODE_CURSOR) { 745 grid_view_get_cell(src->grid, src->cx, src->cy, &gc); 746 gc.attr |= GRID_ATTR_REVERSE; 747 screen_write_set_cursor(ctx, cx + (src->cx - px), 748 cy + (src->cy - py)); 749 screen_write_cell(ctx, &gc); 750 } 751 } 752 753 /* Set a mode. */ 754 void 755 screen_write_mode_set(struct screen_write_ctx *ctx, int mode) 756 { 757 struct screen *s = ctx->s; 758 759 s->mode |= mode; 760 } 761 762 /* Clear a mode. */ 763 void 764 screen_write_mode_clear(struct screen_write_ctx *ctx, int mode) 765 { 766 struct screen *s = ctx->s; 767 768 s->mode &= ~mode; 769 } 770 771 /* Cursor up by ny. */ 772 void 773 screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny) 774 { 775 struct screen *s = ctx->s; 776 u_int cx = s->cx, cy = s->cy; 777 778 if (ny == 0) 779 ny = 1; 780 781 if (cy < s->rupper) { 782 /* Above region. */ 783 if (ny > cy) 784 ny = cy; 785 } else { 786 /* Below region. */ 787 if (ny > cy - s->rupper) 788 ny = cy - s->rupper; 789 } 790 if (cx == screen_size_x(s)) 791 cx--; 792 793 cy -= ny; 794 795 screen_write_set_cursor(ctx, cx, cy); 796 } 797 798 /* Cursor down by ny. */ 799 void 800 screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny) 801 { 802 struct screen *s = ctx->s; 803 u_int cx = s->cx, cy = s->cy; 804 805 if (ny == 0) 806 ny = 1; 807 808 if (cy > s->rlower) { 809 /* Below region. */ 810 if (ny > screen_size_y(s) - 1 - cy) 811 ny = screen_size_y(s) - 1 - cy; 812 } else { 813 /* Above region. */ 814 if (ny > s->rlower - cy) 815 ny = s->rlower - cy; 816 } 817 if (cx == screen_size_x(s)) 818 cx--; 819 else if (ny == 0) 820 return; 821 822 cy += ny; 823 824 screen_write_set_cursor(ctx, cx, cy); 825 } 826 827 /* Cursor right by nx. */ 828 void 829 screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx) 830 { 831 struct screen *s = ctx->s; 832 u_int cx = s->cx, cy = s->cy; 833 834 if (nx == 0) 835 nx = 1; 836 837 if (nx > screen_size_x(s) - 1 - cx) 838 nx = screen_size_x(s) - 1 - cx; 839 if (nx == 0) 840 return; 841 842 cx += nx; 843 844 screen_write_set_cursor(ctx, cx, cy); 845 } 846 847 /* Cursor left by nx. */ 848 void 849 screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx) 850 { 851 struct screen *s = ctx->s; 852 u_int cx = s->cx, cy = s->cy; 853 854 if (nx == 0) 855 nx = 1; 856 857 if (nx > cx) 858 nx = cx; 859 if (nx == 0) 860 return; 861 862 cx -= nx; 863 864 screen_write_set_cursor(ctx, cx, cy); 865 } 866 867 /* Backspace; cursor left unless at start of wrapped line when can move up. */ 868 void 869 screen_write_backspace(struct screen_write_ctx *ctx) 870 { 871 struct screen *s = ctx->s; 872 struct grid_line *gl; 873 u_int cx = s->cx, cy = s->cy; 874 875 if (cx == 0) { 876 if (cy == 0) 877 return; 878 gl = grid_get_line(s->grid, s->grid->hsize + cy - 1); 879 if (gl->flags & GRID_LINE_WRAPPED) { 880 cy--; 881 cx = screen_size_x(s) - 1; 882 } 883 } else 884 cx--; 885 886 screen_write_set_cursor(ctx, cx, cy); 887 } 888 889 /* VT100 alignment test. */ 890 void 891 screen_write_alignmenttest(struct screen_write_ctx *ctx) 892 { 893 struct screen *s = ctx->s; 894 struct tty_ctx ttyctx; 895 struct grid_cell gc; 896 u_int xx, yy; 897 898 memcpy(&gc, &grid_default_cell, sizeof gc); 899 utf8_set(&gc.data, 'E'); 900 901 for (yy = 0; yy < screen_size_y(s); yy++) { 902 for (xx = 0; xx < screen_size_x(s); xx++) 903 grid_view_set_cell(s->grid, xx, yy, &gc); 904 } 905 906 screen_write_set_cursor(ctx, 0, 0); 907 908 s->rupper = 0; 909 s->rlower = screen_size_y(s) - 1; 910 911 screen_write_initctx(ctx, &ttyctx, 1); 912 913 screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1); 914 tty_write(tty_cmd_alignmenttest, &ttyctx); 915 } 916 917 /* Insert nx characters. */ 918 void 919 screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg) 920 { 921 struct screen *s = ctx->s; 922 struct tty_ctx ttyctx; 923 924 if (nx == 0) 925 nx = 1; 926 927 if (nx > screen_size_x(s) - s->cx) 928 nx = screen_size_x(s) - s->cx; 929 if (nx == 0) 930 return; 931 932 if (s->cx > screen_size_x(s) - 1) 933 return; 934 935 screen_write_initctx(ctx, &ttyctx, 0); 936 ttyctx.bg = bg; 937 938 grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg); 939 940 screen_write_collect_flush(ctx, 0, __func__); 941 ttyctx.num = nx; 942 tty_write(tty_cmd_insertcharacter, &ttyctx); 943 } 944 945 /* Delete nx characters. */ 946 void 947 screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg) 948 { 949 struct screen *s = ctx->s; 950 struct tty_ctx ttyctx; 951 952 if (nx == 0) 953 nx = 1; 954 955 if (nx > screen_size_x(s) - s->cx) 956 nx = screen_size_x(s) - s->cx; 957 if (nx == 0) 958 return; 959 960 if (s->cx > screen_size_x(s) - 1) 961 return; 962 963 screen_write_initctx(ctx, &ttyctx, 0); 964 ttyctx.bg = bg; 965 966 grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg); 967 968 screen_write_collect_flush(ctx, 0, __func__); 969 ttyctx.num = nx; 970 tty_write(tty_cmd_deletecharacter, &ttyctx); 971 } 972 973 /* Clear nx characters. */ 974 void 975 screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg) 976 { 977 struct screen *s = ctx->s; 978 struct tty_ctx ttyctx; 979 980 if (nx == 0) 981 nx = 1; 982 983 if (nx > screen_size_x(s) - s->cx) 984 nx = screen_size_x(s) - s->cx; 985 if (nx == 0) 986 return; 987 988 if (s->cx > screen_size_x(s) - 1) 989 return; 990 991 screen_write_initctx(ctx, &ttyctx, 0); 992 ttyctx.bg = bg; 993 994 grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg); 995 996 screen_write_collect_flush(ctx, 0, __func__); 997 ttyctx.num = nx; 998 tty_write(tty_cmd_clearcharacter, &ttyctx); 999 } 1000 1001 /* Insert ny lines. */ 1002 void 1003 screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg) 1004 { 1005 struct screen *s = ctx->s; 1006 struct grid *gd = s->grid; 1007 struct tty_ctx ttyctx; 1008 1009 if (ny == 0) 1010 ny = 1; 1011 1012 if (s->cy < s->rupper || s->cy > s->rlower) { 1013 if (ny > screen_size_y(s) - s->cy) 1014 ny = screen_size_y(s) - s->cy; 1015 if (ny == 0) 1016 return; 1017 1018 screen_write_initctx(ctx, &ttyctx, 1); 1019 ttyctx.bg = bg; 1020 1021 grid_view_insert_lines(gd, s->cy, ny, bg); 1022 1023 screen_write_collect_flush(ctx, 0, __func__); 1024 ttyctx.num = ny; 1025 tty_write(tty_cmd_insertline, &ttyctx); 1026 return; 1027 } 1028 1029 if (ny > s->rlower + 1 - s->cy) 1030 ny = s->rlower + 1 - s->cy; 1031 if (ny == 0) 1032 return; 1033 1034 screen_write_initctx(ctx, &ttyctx, 1); 1035 ttyctx.bg = bg; 1036 1037 if (s->cy < s->rupper || s->cy > s->rlower) 1038 grid_view_insert_lines(gd, s->cy, ny, bg); 1039 else 1040 grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg); 1041 1042 screen_write_collect_flush(ctx, 0, __func__); 1043 1044 ttyctx.num = ny; 1045 tty_write(tty_cmd_insertline, &ttyctx); 1046 } 1047 1048 /* Delete ny lines. */ 1049 void 1050 screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg) 1051 { 1052 struct screen *s = ctx->s; 1053 struct grid *gd = s->grid; 1054 struct tty_ctx ttyctx; 1055 1056 if (ny == 0) 1057 ny = 1; 1058 1059 if (s->cy < s->rupper || s->cy > s->rlower) { 1060 if (ny > screen_size_y(s) - s->cy) 1061 ny = screen_size_y(s) - s->cy; 1062 if (ny == 0) 1063 return; 1064 1065 screen_write_initctx(ctx, &ttyctx, 1); 1066 ttyctx.bg = bg; 1067 1068 grid_view_delete_lines(gd, s->cy, ny, bg); 1069 1070 screen_write_collect_flush(ctx, 0, __func__); 1071 ttyctx.num = ny; 1072 tty_write(tty_cmd_deleteline, &ttyctx); 1073 return; 1074 } 1075 1076 if (ny > s->rlower + 1 - s->cy) 1077 ny = s->rlower + 1 - s->cy; 1078 if (ny == 0) 1079 return; 1080 1081 screen_write_initctx(ctx, &ttyctx, 1); 1082 ttyctx.bg = bg; 1083 1084 if (s->cy < s->rupper || s->cy > s->rlower) 1085 grid_view_delete_lines(gd, s->cy, ny, bg); 1086 else 1087 grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg); 1088 1089 screen_write_collect_flush(ctx, 0, __func__); 1090 ttyctx.num = ny; 1091 tty_write(tty_cmd_deleteline, &ttyctx); 1092 } 1093 1094 /* Clear line at cursor. */ 1095 void 1096 screen_write_clearline(struct screen_write_ctx *ctx, u_int bg) 1097 { 1098 struct screen *s = ctx->s; 1099 struct grid_line *gl; 1100 u_int sx = screen_size_x(s); 1101 1102 gl = grid_get_line(s->grid, s->grid->hsize + s->cy); 1103 if (gl->cellsize == 0 && COLOUR_DEFAULT(bg)) 1104 return; 1105 1106 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg); 1107 1108 screen_write_collect_clear(ctx, s->cy, 1); 1109 ctx->s->write_list[s->cy].bg = 1 + bg; 1110 ctx->item->used = 0; 1111 } 1112 1113 /* Clear to end of line from cursor. */ 1114 void 1115 screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg) 1116 { 1117 struct screen *s = ctx->s; 1118 struct grid_line *gl; 1119 u_int sx = screen_size_x(s); 1120 struct screen_write_collect_item *ci = ctx->item; 1121 1122 if (s->cx == 0) { 1123 screen_write_clearline(ctx, bg); 1124 return; 1125 } 1126 1127 gl = grid_get_line(s->grid, s->grid->hsize + s->cy); 1128 if (s->cx > sx - 1 || (s->cx >= gl->cellsize && COLOUR_DEFAULT(bg))) 1129 return; 1130 1131 grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg); 1132 1133 screen_write_collect_clear_end(ctx, s->cy, s->cx); 1134 ci->x = s->cx; 1135 ci->type = CLEAR_END; 1136 ci->bg = bg; 1137 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry); 1138 ctx->item = xcalloc(1, sizeof *ctx->item); 1139 } 1140 1141 /* Clear to start of line from cursor. */ 1142 void 1143 screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg) 1144 { 1145 struct screen *s = ctx->s; 1146 u_int sx = screen_size_x(s); 1147 struct screen_write_collect_item *ci = ctx->item; 1148 1149 if (s->cx >= sx - 1) { 1150 screen_write_clearline(ctx, bg); 1151 return; 1152 } 1153 1154 if (s->cx > sx - 1) 1155 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg); 1156 else 1157 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg); 1158 1159 screen_write_collect_clear_start(ctx, s->cy, s->cx); 1160 ci->x = s->cx; 1161 ci->type = CLEAR_START; 1162 ci->bg = bg; 1163 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry); 1164 ctx->item = xcalloc(1, sizeof *ctx->item); 1165 } 1166 1167 /* Move cursor to px,py. */ 1168 void 1169 screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py, 1170 int origin) 1171 { 1172 struct screen *s = ctx->s; 1173 1174 if (origin && py != -1 && (s->mode & MODE_ORIGIN)) { 1175 if ((u_int)py > s->rlower - s->rupper) 1176 py = s->rlower; 1177 else 1178 py += s->rupper; 1179 } 1180 1181 if (px != -1 && (u_int)px > screen_size_x(s) - 1) 1182 px = screen_size_x(s) - 1; 1183 if (py != -1 && (u_int)py > screen_size_y(s) - 1) 1184 py = screen_size_y(s) - 1; 1185 1186 screen_write_set_cursor(ctx, px, py); 1187 } 1188 1189 /* Reverse index (up with scroll). */ 1190 void 1191 screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg) 1192 { 1193 struct screen *s = ctx->s; 1194 struct tty_ctx ttyctx; 1195 1196 if (s->cy == s->rupper) { 1197 grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg); 1198 screen_write_collect_flush(ctx, 0, __func__); 1199 1200 screen_write_initctx(ctx, &ttyctx, 1); 1201 ttyctx.bg = bg; 1202 1203 tty_write(tty_cmd_reverseindex, &ttyctx); 1204 } else if (s->cy > 0) 1205 screen_write_set_cursor(ctx, -1, s->cy - 1); 1206 1207 } 1208 1209 /* Set scroll region. */ 1210 void 1211 screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper, 1212 u_int rlower) 1213 { 1214 struct screen *s = ctx->s; 1215 1216 if (rupper > screen_size_y(s) - 1) 1217 rupper = screen_size_y(s) - 1; 1218 if (rlower > screen_size_y(s) - 1) 1219 rlower = screen_size_y(s) - 1; 1220 if (rupper >= rlower) /* cannot be one line */ 1221 return; 1222 1223 screen_write_collect_flush(ctx, 0, __func__); 1224 1225 /* Cursor moves to top-left. */ 1226 screen_write_set_cursor(ctx, 0, 0); 1227 1228 s->rupper = rupper; 1229 s->rlower = rlower; 1230 } 1231 1232 /* Line feed. */ 1233 void 1234 screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg) 1235 { 1236 struct screen *s = ctx->s; 1237 struct grid *gd = s->grid; 1238 struct grid_line *gl; 1239 1240 gl = grid_get_line(gd, gd->hsize + s->cy); 1241 if (wrapped) 1242 gl->flags |= GRID_LINE_WRAPPED; 1243 else 1244 gl->flags &= ~GRID_LINE_WRAPPED; 1245 1246 log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy, 1247 s->rupper, s->rlower); 1248 1249 if (bg != ctx->bg) { 1250 screen_write_collect_flush(ctx, 1, __func__); 1251 ctx->bg = bg; 1252 } 1253 1254 if (s->cy == s->rlower) { 1255 grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg); 1256 screen_write_collect_scroll(ctx); 1257 ctx->scrolled++; 1258 } else if (s->cy < screen_size_y(s) - 1) 1259 screen_write_set_cursor(ctx, -1, s->cy + 1); 1260 } 1261 1262 /* Scroll up. */ 1263 void 1264 screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg) 1265 { 1266 struct screen *s = ctx->s; 1267 struct grid *gd = s->grid; 1268 u_int i; 1269 1270 if (lines == 0) 1271 lines = 1; 1272 else if (lines > s->rlower - s->rupper + 1) 1273 lines = s->rlower - s->rupper + 1; 1274 1275 if (bg != ctx->bg) { 1276 screen_write_collect_flush(ctx, 1, __func__); 1277 ctx->bg = bg; 1278 } 1279 1280 for (i = 0; i < lines; i++) { 1281 grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg); 1282 screen_write_collect_scroll(ctx); 1283 } 1284 ctx->scrolled += lines; 1285 } 1286 1287 /* Scroll down. */ 1288 void 1289 screen_write_scrolldown(struct screen_write_ctx *ctx, u_int lines, u_int bg) 1290 { 1291 struct screen *s = ctx->s; 1292 struct grid *gd = s->grid; 1293 struct tty_ctx ttyctx; 1294 u_int i; 1295 1296 screen_write_initctx(ctx, &ttyctx, 1); 1297 ttyctx.bg = bg; 1298 1299 if (lines == 0) 1300 lines = 1; 1301 else if (lines > s->rlower - s->rupper + 1) 1302 lines = s->rlower - s->rupper + 1; 1303 1304 for (i = 0; i < lines; i++) 1305 grid_view_scroll_region_down(gd, s->rupper, s->rlower, bg); 1306 1307 screen_write_collect_flush(ctx, 0, __func__); 1308 ttyctx.num = lines; 1309 tty_write(tty_cmd_scrolldown, &ttyctx); 1310 } 1311 1312 /* Carriage return (cursor to start of line). */ 1313 void 1314 screen_write_carriagereturn(struct screen_write_ctx *ctx) 1315 { 1316 screen_write_set_cursor(ctx, 0, -1); 1317 } 1318 1319 /* Clear to end of screen from cursor. */ 1320 void 1321 screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg) 1322 { 1323 struct screen *s = ctx->s; 1324 struct grid *gd = s->grid; 1325 struct tty_ctx ttyctx; 1326 u_int sx = screen_size_x(s), sy = screen_size_y(s); 1327 1328 screen_write_initctx(ctx, &ttyctx, 1); 1329 ttyctx.bg = bg; 1330 1331 /* Scroll into history if it is enabled and clearing entire screen. */ 1332 if (s->cx == 0 && s->cy == 0 && (gd->flags & GRID_HISTORY)) 1333 grid_view_clear_history(gd, bg); 1334 else { 1335 if (s->cx <= sx - 1) 1336 grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg); 1337 grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg); 1338 } 1339 1340 screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1)); 1341 screen_write_collect_flush(ctx, 0, __func__); 1342 tty_write(tty_cmd_clearendofscreen, &ttyctx); 1343 } 1344 1345 /* Clear to start of screen. */ 1346 void 1347 screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg) 1348 { 1349 struct screen *s = ctx->s; 1350 struct tty_ctx ttyctx; 1351 u_int sx = screen_size_x(s); 1352 1353 screen_write_initctx(ctx, &ttyctx, 1); 1354 ttyctx.bg = bg; 1355 1356 if (s->cy > 0) 1357 grid_view_clear(s->grid, 0, 0, sx, s->cy, bg); 1358 if (s->cx > sx - 1) 1359 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg); 1360 else 1361 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg); 1362 1363 screen_write_collect_clear(ctx, 0, s->cy); 1364 screen_write_collect_flush(ctx, 0, __func__); 1365 tty_write(tty_cmd_clearstartofscreen, &ttyctx); 1366 } 1367 1368 /* Clear entire screen. */ 1369 void 1370 screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg) 1371 { 1372 struct screen *s = ctx->s; 1373 struct tty_ctx ttyctx; 1374 u_int sx = screen_size_x(s), sy = screen_size_y(s); 1375 1376 screen_write_initctx(ctx, &ttyctx, 1); 1377 ttyctx.bg = bg; 1378 1379 /* Scroll into history if it is enabled. */ 1380 if (s->grid->flags & GRID_HISTORY) 1381 grid_view_clear_history(s->grid, bg); 1382 else 1383 grid_view_clear(s->grid, 0, 0, sx, sy, bg); 1384 1385 screen_write_collect_clear(ctx, 0, sy); 1386 tty_write(tty_cmd_clearscreen, &ttyctx); 1387 } 1388 1389 /* Clear entire history. */ 1390 void 1391 screen_write_clearhistory(struct screen_write_ctx *ctx) 1392 { 1393 grid_clear_history(ctx->s->grid); 1394 } 1395 1396 /* Clear to start of a collected line. */ 1397 static void 1398 screen_write_collect_clear_start(struct screen_write_ctx *ctx, u_int y, u_int x) 1399 { 1400 struct screen_write_collect_item *ci, *tmp; 1401 size_t size = 0; 1402 u_int items = 0; 1403 1404 if (TAILQ_EMPTY(&ctx->s->write_list[y].items)) 1405 return; 1406 TAILQ_FOREACH_SAFE(ci, &ctx->s->write_list[y].items, entry, tmp) { 1407 switch (ci->type) { 1408 case CLEAR_START: 1409 break; 1410 case CLEAR_END: 1411 if (ci->x <= x) 1412 ci->x = x; 1413 continue; 1414 case TEXT: 1415 if (ci->x > x) 1416 continue; 1417 break; 1418 } 1419 items++; 1420 size += ci->used; 1421 TAILQ_REMOVE(&ctx->s->write_list[y].items, ci, entry); 1422 free(ci); 1423 } 1424 ctx->skipped += size; 1425 log_debug("%s: dropped %u items (%zu bytes) (line %u)", __func__, items, 1426 size, y); 1427 } 1428 1429 /* Clear to end of a collected line. */ 1430 static void 1431 screen_write_collect_clear_end(struct screen_write_ctx *ctx, u_int y, u_int x) 1432 { 1433 struct screen_write_collect_item *ci, *tmp; 1434 size_t size = 0; 1435 u_int items = 0; 1436 1437 if (TAILQ_EMPTY(&ctx->s->write_list[y].items)) 1438 return; 1439 TAILQ_FOREACH_SAFE(ci, &ctx->s->write_list[y].items, entry, tmp) { 1440 switch (ci->type) { 1441 case CLEAR_START: 1442 if (ci->x >= x) 1443 ci->x = x; 1444 continue; 1445 case CLEAR_END: 1446 break; 1447 case TEXT: 1448 if (ci->x < x) 1449 continue; 1450 break; 1451 } 1452 items++; 1453 size += ci->used; 1454 TAILQ_REMOVE(&ctx->s->write_list[y].items, ci, entry); 1455 free(ci); 1456 } 1457 ctx->skipped += size; 1458 log_debug("%s: dropped %u items (%zu bytes) (line %u)", __func__, items, 1459 size, y); 1460 } 1461 1462 /* Clear collected lines. */ 1463 static void 1464 screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n) 1465 { 1466 struct screen_write_collect_item *ci, *tmp; 1467 struct screen_write_collect_line *cl; 1468 u_int i, items; 1469 size_t size; 1470 1471 for (i = y; i < y + n; i++) { 1472 if (TAILQ_EMPTY(&ctx->s->write_list[i].items)) 1473 continue; 1474 items = 0; 1475 size = 0; 1476 cl = &ctx->s->write_list[i]; 1477 TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) { 1478 items++; 1479 size += ci->used; 1480 TAILQ_REMOVE(&cl->items, ci, entry); 1481 free(ci); 1482 } 1483 ctx->skipped += size; 1484 log_debug("%s: dropped %u items (%zu bytes) (line %u)", 1485 __func__, items, size, y); 1486 } 1487 } 1488 1489 /* Scroll collected lines up. */ 1490 static void 1491 screen_write_collect_scroll(struct screen_write_ctx *ctx) 1492 { 1493 struct screen *s = ctx->s; 1494 struct screen_write_collect_line *cl; 1495 u_int y; 1496 char *saved; 1497 1498 log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy, 1499 s->rupper, s->rlower); 1500 1501 screen_write_collect_clear(ctx, s->rupper, 1); 1502 saved = ctx->s->write_list[s->rupper].data; 1503 for (y = s->rupper; y < s->rlower; y++) { 1504 cl = &ctx->s->write_list[y + 1]; 1505 TAILQ_CONCAT(&ctx->s->write_list[y].items, &cl->items, entry); 1506 ctx->s->write_list[y].bg = cl->bg; 1507 ctx->s->write_list[y].data = cl->data; 1508 } 1509 ctx->s->write_list[s->rlower].bg = 1 + 8; 1510 ctx->s->write_list[s->rlower].data = saved; 1511 } 1512 1513 /* Flush collected lines. */ 1514 static void 1515 screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only, 1516 const char *from) 1517 { 1518 struct screen *s = ctx->s; 1519 struct screen_write_collect_item *ci, *tmp; 1520 struct screen_write_collect_line *cl; 1521 u_int y, cx, cy, items = 0; 1522 struct tty_ctx ttyctx; 1523 size_t written = 0; 1524 1525 if (ctx->scrolled != 0) { 1526 log_debug("%s: scrolled %u (region %u-%u)", __func__, 1527 ctx->scrolled, s->rupper, s->rlower); 1528 if (ctx->scrolled > s->rlower - s->rupper + 1) 1529 ctx->scrolled = s->rlower - s->rupper + 1; 1530 1531 screen_write_initctx(ctx, &ttyctx, 1); 1532 ttyctx.num = ctx->scrolled; 1533 ttyctx.bg = ctx->bg; 1534 tty_write(tty_cmd_scrollup, &ttyctx); 1535 } 1536 ctx->scrolled = 0; 1537 ctx->bg = 8; 1538 1539 if (scroll_only) 1540 return; 1541 1542 cx = s->cx; cy = s->cy; 1543 for (y = 0; y < screen_size_y(s); y++) { 1544 cl = &ctx->s->write_list[y]; 1545 if (cl->bg != 0) { 1546 screen_write_set_cursor(ctx, 0, y); 1547 screen_write_initctx(ctx, &ttyctx, 1); 1548 ttyctx.bg = cl->bg - 1; 1549 tty_write(tty_cmd_clearline, &ttyctx); 1550 } 1551 TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) { 1552 screen_write_set_cursor(ctx, ci->x, y); 1553 if (ci->type == CLEAR_END) { 1554 screen_write_initctx(ctx, &ttyctx, 1); 1555 ttyctx.bg = ci->bg; 1556 tty_write(tty_cmd_clearendofline, &ttyctx); 1557 } else if (ci->type == CLEAR_START) { 1558 screen_write_initctx(ctx, &ttyctx, 1); 1559 ttyctx.bg = ci->bg; 1560 tty_write(tty_cmd_clearstartofline, &ttyctx); 1561 } else { 1562 screen_write_initctx(ctx, &ttyctx, 0); 1563 ttyctx.cell = &ci->gc; 1564 ttyctx.wrapped = ci->wrapped; 1565 ttyctx.ptr = cl->data + ci->x; 1566 ttyctx.num = ci->used; 1567 tty_write(tty_cmd_cells, &ttyctx); 1568 } 1569 1570 items++; 1571 written += ci->used; 1572 1573 TAILQ_REMOVE(&cl->items, ci, entry); 1574 free(ci); 1575 } 1576 cl->bg = 0; 1577 } 1578 s->cx = cx; s->cy = cy; 1579 1580 log_debug("%s: flushed %u items (%zu bytes) (%s)", __func__, items, 1581 written, from); 1582 ctx->written += written; 1583 } 1584 1585 /* Finish and store collected cells. */ 1586 void 1587 screen_write_collect_end(struct screen_write_ctx *ctx) 1588 { 1589 struct screen *s = ctx->s; 1590 struct screen_write_collect_item *ci = ctx->item; 1591 struct screen_write_collect_line *cl = &s->write_list[s->cy]; 1592 struct grid_cell gc; 1593 u_int xx; 1594 1595 if (ci->used == 0) 1596 return; 1597 1598 ci->x = s->cx; 1599 TAILQ_INSERT_TAIL(&cl->items, ci, entry); 1600 ctx->item = xcalloc(1, sizeof *ctx->item); 1601 1602 log_debug("%s: %u %.*s (at %u,%u)", __func__, ci->used, 1603 (int)ci->used, cl->data + ci->x, s->cx, s->cy); 1604 1605 if (s->cx != 0) { 1606 for (xx = s->cx; xx > 0; xx--) { 1607 grid_view_get_cell(s->grid, xx, s->cy, &gc); 1608 if (~gc.flags & GRID_FLAG_PADDING) 1609 break; 1610 grid_view_set_cell(s->grid, xx, s->cy, 1611 &grid_default_cell); 1612 } 1613 if (gc.data.width > 1) { 1614 grid_view_set_cell(s->grid, xx, s->cy, 1615 &grid_default_cell); 1616 } 1617 } 1618 1619 grid_view_set_cells(s->grid, s->cx, s->cy, &ci->gc, cl->data + ci->x, 1620 ci->used); 1621 screen_write_set_cursor(ctx, s->cx + ci->used, -1); 1622 1623 for (xx = s->cx; xx < screen_size_x(s); xx++) { 1624 grid_view_get_cell(s->grid, xx, s->cy, &gc); 1625 if (~gc.flags & GRID_FLAG_PADDING) 1626 break; 1627 grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell); 1628 } 1629 } 1630 1631 /* Write cell data, collecting if necessary. */ 1632 void 1633 screen_write_collect_add(struct screen_write_ctx *ctx, 1634 const struct grid_cell *gc) 1635 { 1636 struct screen *s = ctx->s; 1637 struct screen_write_collect_item *ci; 1638 u_int sx = screen_size_x(s); 1639 int collect; 1640 1641 /* 1642 * Don't need to check that the attributes and whatnot are still the 1643 * same - input_parse will end the collection when anything that isn't 1644 * a plain character is encountered. 1645 */ 1646 1647 collect = 1; 1648 if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f) 1649 collect = 0; 1650 else if (gc->attr & GRID_ATTR_CHARSET) 1651 collect = 0; 1652 else if (~s->mode & MODE_WRAP) 1653 collect = 0; 1654 else if (s->mode & MODE_INSERT) 1655 collect = 0; 1656 else if (s->sel != NULL) 1657 collect = 0; 1658 if (!collect) { 1659 screen_write_collect_end(ctx); 1660 screen_write_collect_flush(ctx, 0, __func__); 1661 screen_write_cell(ctx, gc); 1662 return; 1663 } 1664 ctx->cells++; 1665 1666 if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx) 1667 screen_write_collect_end(ctx); 1668 ci = ctx->item; /* may have changed */ 1669 1670 if (s->cx > sx - 1) { 1671 log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy); 1672 ci->wrapped = 1; 1673 screen_write_linefeed(ctx, 1, 8); 1674 screen_write_set_cursor(ctx, 0, -1); 1675 } 1676 1677 if (ci->used == 0) 1678 memcpy(&ci->gc, gc, sizeof ci->gc); 1679 if (ctx->s->write_list[s->cy].data == NULL) 1680 ctx->s->write_list[s->cy].data = xmalloc(screen_size_x(ctx->s)); 1681 ctx->s->write_list[s->cy].data[s->cx + ci->used++] = gc->data.data[0]; 1682 } 1683 1684 /* Write cell data. */ 1685 void 1686 screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc) 1687 { 1688 struct screen *s = ctx->s; 1689 struct grid *gd = s->grid; 1690 struct grid_line *gl; 1691 struct grid_cell_entry *gce; 1692 struct grid_cell tmp_gc, now_gc; 1693 struct tty_ctx ttyctx; 1694 u_int sx = screen_size_x(s), sy = screen_size_y(s); 1695 u_int width = gc->data.width, xx, last, cx, cy; 1696 int selected, skip = 1; 1697 1698 /* Ignore padding cells. */ 1699 if (gc->flags & GRID_FLAG_PADDING) 1700 return; 1701 ctx->cells++; 1702 1703 /* If the width is zero, combine onto the previous character. */ 1704 if (width == 0) { 1705 screen_write_collect_flush(ctx, 0, __func__); 1706 if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) { 1707 cx = s->cx; cy = s->cy; 1708 screen_write_set_cursor(ctx, xx, s->cy); 1709 screen_write_initctx(ctx, &ttyctx, 0); 1710 ttyctx.cell = gc; 1711 tty_write(tty_cmd_cell, &ttyctx); 1712 s->cx = cx; s->cy = cy; 1713 } 1714 return; 1715 } 1716 1717 /* Flush any existing scrolling. */ 1718 screen_write_collect_flush(ctx, 1, __func__); 1719 1720 /* If this character doesn't fit, ignore it. */ 1721 if ((~s->mode & MODE_WRAP) && 1722 width > 1 && 1723 (width > sx || (s->cx != sx && s->cx > sx - width))) 1724 return; 1725 1726 /* If in insert mode, make space for the cells. */ 1727 if (s->mode & MODE_INSERT) { 1728 grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8); 1729 skip = 0; 1730 } 1731 1732 /* Check this will fit on the current line and wrap if not. */ 1733 if ((s->mode & MODE_WRAP) && s->cx > sx - width) { 1734 log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy); 1735 screen_write_linefeed(ctx, 1, 8); 1736 screen_write_set_cursor(ctx, 0, -1); 1737 screen_write_collect_flush(ctx, 1, __func__); 1738 } 1739 1740 /* Sanity check cursor position. */ 1741 if (s->cx > sx - width || s->cy > sy - 1) 1742 return; 1743 screen_write_initctx(ctx, &ttyctx, 0); 1744 1745 /* Handle overwriting of UTF-8 characters. */ 1746 gl = grid_get_line(s->grid, s->grid->hsize + s->cy); 1747 if (gl->flags & GRID_LINE_EXTENDED) { 1748 grid_view_get_cell(gd, s->cx, s->cy, &now_gc); 1749 if (screen_write_overwrite(ctx, &now_gc, width)) 1750 skip = 0; 1751 } 1752 1753 /* 1754 * If the new character is UTF-8 wide, fill in padding cells. Have 1755 * already ensured there is enough room. 1756 */ 1757 for (xx = s->cx + 1; xx < s->cx + width; xx++) { 1758 log_debug("%s: new padding at %u,%u", __func__, xx, s->cy); 1759 grid_view_set_padding(gd, xx, s->cy); 1760 skip = 0; 1761 } 1762 1763 /* If no change, do not draw. */ 1764 if (skip) { 1765 if (s->cx >= gl->cellsize) 1766 skip = grid_cells_equal(gc, &grid_default_cell); 1767 else { 1768 gce = &gl->celldata[s->cx]; 1769 if (gce->flags & GRID_FLAG_EXTENDED) 1770 skip = 0; 1771 else if (gc->flags != gce->flags) 1772 skip = 0; 1773 else if (gc->attr != gce->data.attr) 1774 skip = 0; 1775 else if (gc->fg != gce->data.fg) 1776 skip = 0; 1777 else if (gc->bg != gce->data.bg) 1778 skip = 0; 1779 else if (gc->data.width != 1) 1780 skip = 0; 1781 else if (gc->data.size != 1) 1782 skip = 0; 1783 else if (gce->data.data != gc->data.data[0]) 1784 skip = 0; 1785 } 1786 } 1787 1788 /* Update the selected flag and set the cell. */ 1789 selected = screen_check_selection(s, s->cx, s->cy); 1790 if (selected && (~gc->flags & GRID_FLAG_SELECTED)) { 1791 memcpy(&tmp_gc, gc, sizeof tmp_gc); 1792 tmp_gc.flags |= GRID_FLAG_SELECTED; 1793 grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc); 1794 } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) { 1795 memcpy(&tmp_gc, gc, sizeof tmp_gc); 1796 tmp_gc.flags &= ~GRID_FLAG_SELECTED; 1797 grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc); 1798 } else if (!skip) 1799 grid_view_set_cell(gd, s->cx, s->cy, gc); 1800 if (selected) 1801 skip = 0; 1802 1803 /* 1804 * Move the cursor. If not wrapping, stick at the last character and 1805 * replace it. 1806 */ 1807 last = !(s->mode & MODE_WRAP); 1808 if (s->cx <= sx - last - width) 1809 screen_write_set_cursor(ctx, s->cx + width, -1); 1810 else 1811 screen_write_set_cursor(ctx, sx - last, -1); 1812 1813 /* Create space for character in insert mode. */ 1814 if (s->mode & MODE_INSERT) { 1815 screen_write_collect_flush(ctx, 0, __func__); 1816 ttyctx.num = width; 1817 tty_write(tty_cmd_insertcharacter, &ttyctx); 1818 } 1819 1820 /* Write to the screen. */ 1821 if (!skip) { 1822 if (selected) { 1823 screen_select_cell(s, &tmp_gc, gc); 1824 ttyctx.cell = &tmp_gc; 1825 } else 1826 ttyctx.cell = gc; 1827 tty_write(tty_cmd_cell, &ttyctx); 1828 ctx->written++; 1829 } else 1830 ctx->skipped++; 1831 } 1832 1833 /* Combine a UTF-8 zero-width character onto the previous. */ 1834 static const struct grid_cell * 1835 screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud, 1836 u_int *xx) 1837 { 1838 struct screen *s = ctx->s; 1839 struct grid *gd = s->grid; 1840 static struct grid_cell gc; 1841 u_int n; 1842 1843 /* Can't combine if at 0. */ 1844 if (s->cx == 0) 1845 return (NULL); 1846 1847 /* Empty data is out. */ 1848 if (ud->size == 0) 1849 fatalx("UTF-8 data empty"); 1850 1851 /* Retrieve the previous cell. */ 1852 for (n = 1; n <= s->cx; n++) { 1853 grid_view_get_cell(gd, s->cx - n, s->cy, &gc); 1854 if (~gc.flags & GRID_FLAG_PADDING) 1855 break; 1856 } 1857 if (n > s->cx) 1858 return (NULL); 1859 *xx = s->cx - n; 1860 1861 /* Check there is enough space. */ 1862 if (gc.data.size + ud->size > sizeof gc.data.data) 1863 return (NULL); 1864 1865 log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size, 1866 ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy); 1867 1868 /* Append the data. */ 1869 memcpy(gc.data.data + gc.data.size, ud->data, ud->size); 1870 gc.data.size += ud->size; 1871 1872 /* Set the new cell. */ 1873 grid_view_set_cell(gd, *xx, s->cy, &gc); 1874 1875 return (&gc); 1876 } 1877 1878 /* 1879 * UTF-8 wide characters are a bit of an annoyance. They take up more than one 1880 * cell on the screen, so following cells must not be drawn by marking them as 1881 * padding. 1882 * 1883 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8 1884 * character, it is necessary to also overwrite any other cells which covered 1885 * by the same character. 1886 */ 1887 static int 1888 screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc, 1889 u_int width) 1890 { 1891 struct screen *s = ctx->s; 1892 struct grid *gd = s->grid; 1893 struct grid_cell tmp_gc; 1894 u_int xx; 1895 int done = 0; 1896 1897 if (gc->flags & GRID_FLAG_PADDING) { 1898 /* 1899 * A padding cell, so clear any following and leading padding 1900 * cells back to the character. Don't overwrite the current 1901 * cell as that happens later anyway. 1902 */ 1903 xx = s->cx + 1; 1904 while (--xx > 0) { 1905 grid_view_get_cell(gd, xx, s->cy, &tmp_gc); 1906 if (~tmp_gc.flags & GRID_FLAG_PADDING) 1907 break; 1908 log_debug("%s: padding at %u,%u", __func__, xx, s->cy); 1909 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell); 1910 } 1911 1912 /* Overwrite the character at the start of this padding. */ 1913 log_debug("%s: character at %u,%u", __func__, xx, s->cy); 1914 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell); 1915 done = 1; 1916 } 1917 1918 /* 1919 * Overwrite any padding cells that belong to any UTF-8 characters 1920 * we'll be overwriting with the current character. 1921 */ 1922 if (width != 1 || 1923 gc->data.width != 1 || 1924 gc->flags & GRID_FLAG_PADDING) { 1925 xx = s->cx + width - 1; 1926 while (++xx < screen_size_x(s)) { 1927 grid_view_get_cell(gd, xx, s->cy, &tmp_gc); 1928 if (~tmp_gc.flags & GRID_FLAG_PADDING) 1929 break; 1930 log_debug("%s: overwrite at %u,%u", __func__, xx, 1931 s->cy); 1932 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell); 1933 done = 1; 1934 } 1935 } 1936 1937 return (done); 1938 } 1939 1940 /* Set external clipboard. */ 1941 void 1942 screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len) 1943 { 1944 struct tty_ctx ttyctx; 1945 1946 screen_write_initctx(ctx, &ttyctx, 0); 1947 ttyctx.ptr = str; 1948 ttyctx.num = len; 1949 1950 tty_write(tty_cmd_setselection, &ttyctx); 1951 } 1952 1953 /* Write unmodified string. */ 1954 void 1955 screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len) 1956 { 1957 struct tty_ctx ttyctx; 1958 1959 screen_write_initctx(ctx, &ttyctx, 0); 1960 ttyctx.ptr = str; 1961 ttyctx.num = len; 1962 1963 tty_write(tty_cmd_rawstring, &ttyctx); 1964 } 1965 1966 /* Turn alternate screen on. */ 1967 void 1968 screen_write_alternateon(struct screen_write_ctx *ctx, struct grid_cell *gc, 1969 int cursor) 1970 { 1971 struct tty_ctx ttyctx; 1972 struct window_pane *wp = ctx->wp; 1973 1974 if (wp != NULL && !options_get_number(wp->options, "alternate-screen")) 1975 return; 1976 screen_alternate_on(ctx->s, gc, cursor); 1977 1978 screen_write_initctx(ctx, &ttyctx, 1); 1979 ttyctx.redraw_cb(&ttyctx); 1980 } 1981 1982 /* Turn alternate screen off. */ 1983 void 1984 screen_write_alternateoff(struct screen_write_ctx *ctx, struct grid_cell *gc, 1985 int cursor) 1986 { 1987 struct tty_ctx ttyctx; 1988 struct window_pane *wp = ctx->wp; 1989 1990 if (wp != NULL && !options_get_number(wp->options, "alternate-screen")) 1991 return; 1992 screen_alternate_off(ctx->s, gc, cursor); 1993 1994 screen_write_initctx(ctx, &ttyctx, 1); 1995 ttyctx.redraw_cb(&ttyctx); 1996 } 1997