1 /* $OpenBSD: screen-write.c,v 1.187 2020/07/21 05:24:33 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 log_debug("XXX %u %u", ci->x, ci->bg); 1555 screen_write_initctx(ctx, &ttyctx, 1); 1556 ttyctx.bg = ci->bg; 1557 tty_write(tty_cmd_clearendofline, &ttyctx); 1558 } else if (ci->type == CLEAR_START) { 1559 screen_write_initctx(ctx, &ttyctx, 1); 1560 ttyctx.bg = ci->bg; 1561 tty_write(tty_cmd_clearstartofline, &ttyctx); 1562 } else { 1563 screen_write_initctx(ctx, &ttyctx, 0); 1564 ttyctx.cell = &ci->gc; 1565 ttyctx.wrapped = ci->wrapped; 1566 ttyctx.ptr = cl->data + ci->x; 1567 ttyctx.num = ci->used; 1568 tty_write(tty_cmd_cells, &ttyctx); 1569 } 1570 1571 items++; 1572 written += ci->used; 1573 1574 TAILQ_REMOVE(&cl->items, ci, entry); 1575 free(ci); 1576 } 1577 cl->bg = 0; 1578 } 1579 s->cx = cx; s->cy = cy; 1580 1581 log_debug("%s: flushed %u items (%zu bytes) (%s)", __func__, items, 1582 written, from); 1583 ctx->written += written; 1584 } 1585 1586 /* Finish and store collected cells. */ 1587 void 1588 screen_write_collect_end(struct screen_write_ctx *ctx) 1589 { 1590 struct screen *s = ctx->s; 1591 struct screen_write_collect_item *ci = ctx->item; 1592 struct screen_write_collect_line *cl = &s->write_list[s->cy]; 1593 struct grid_cell gc; 1594 u_int xx; 1595 1596 if (ci->used == 0) 1597 return; 1598 1599 ci->x = s->cx; 1600 TAILQ_INSERT_TAIL(&cl->items, ci, entry); 1601 ctx->item = xcalloc(1, sizeof *ctx->item); 1602 1603 log_debug("%s: %u %.*s (at %u,%u)", __func__, ci->used, 1604 (int)ci->used, cl->data + ci->x, s->cx, s->cy); 1605 1606 if (s->cx != 0) { 1607 for (xx = s->cx; xx > 0; xx--) { 1608 grid_view_get_cell(s->grid, xx, s->cy, &gc); 1609 if (~gc.flags & GRID_FLAG_PADDING) 1610 break; 1611 grid_view_set_cell(s->grid, xx, s->cy, 1612 &grid_default_cell); 1613 } 1614 if (gc.data.width > 1) { 1615 grid_view_set_cell(s->grid, xx, s->cy, 1616 &grid_default_cell); 1617 } 1618 } 1619 1620 grid_view_set_cells(s->grid, s->cx, s->cy, &ci->gc, cl->data + ci->x, 1621 ci->used); 1622 screen_write_set_cursor(ctx, s->cx + ci->used, -1); 1623 1624 for (xx = s->cx; xx < screen_size_x(s); xx++) { 1625 grid_view_get_cell(s->grid, xx, s->cy, &gc); 1626 if (~gc.flags & GRID_FLAG_PADDING) 1627 break; 1628 grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell); 1629 } 1630 } 1631 1632 /* Write cell data, collecting if necessary. */ 1633 void 1634 screen_write_collect_add(struct screen_write_ctx *ctx, 1635 const struct grid_cell *gc) 1636 { 1637 struct screen *s = ctx->s; 1638 struct screen_write_collect_item *ci; 1639 u_int sx = screen_size_x(s); 1640 int collect; 1641 1642 /* 1643 * Don't need to check that the attributes and whatnot are still the 1644 * same - input_parse will end the collection when anything that isn't 1645 * a plain character is encountered. 1646 */ 1647 1648 collect = 1; 1649 if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f) 1650 collect = 0; 1651 else if (gc->attr & GRID_ATTR_CHARSET) 1652 collect = 0; 1653 else if (~s->mode & MODE_WRAP) 1654 collect = 0; 1655 else if (s->mode & MODE_INSERT) 1656 collect = 0; 1657 else if (s->sel != NULL) 1658 collect = 0; 1659 if (!collect) { 1660 screen_write_collect_end(ctx); 1661 screen_write_collect_flush(ctx, 0, __func__); 1662 screen_write_cell(ctx, gc); 1663 return; 1664 } 1665 ctx->cells++; 1666 1667 if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx) 1668 screen_write_collect_end(ctx); 1669 ci = ctx->item; /* may have changed */ 1670 1671 if (s->cx > sx - 1) { 1672 log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy); 1673 ci->wrapped = 1; 1674 screen_write_linefeed(ctx, 1, 8); 1675 screen_write_set_cursor(ctx, 0, -1); 1676 } 1677 1678 if (ci->used == 0) 1679 memcpy(&ci->gc, gc, sizeof ci->gc); 1680 if (ctx->s->write_list[s->cy].data == NULL) 1681 ctx->s->write_list[s->cy].data = xmalloc(screen_size_x(ctx->s)); 1682 ctx->s->write_list[s->cy].data[s->cx + ci->used++] = gc->data.data[0]; 1683 } 1684 1685 /* Write cell data. */ 1686 void 1687 screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc) 1688 { 1689 struct screen *s = ctx->s; 1690 struct grid *gd = s->grid; 1691 struct grid_line *gl; 1692 struct grid_cell_entry *gce; 1693 struct grid_cell tmp_gc, now_gc; 1694 struct tty_ctx ttyctx; 1695 u_int sx = screen_size_x(s), sy = screen_size_y(s); 1696 u_int width = gc->data.width, xx, last, cx, cy; 1697 int selected, skip = 1; 1698 1699 /* Ignore padding cells. */ 1700 if (gc->flags & GRID_FLAG_PADDING) 1701 return; 1702 ctx->cells++; 1703 1704 /* If the width is zero, combine onto the previous character. */ 1705 if (width == 0) { 1706 screen_write_collect_flush(ctx, 0, __func__); 1707 if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) { 1708 cx = s->cx; cy = s->cy; 1709 screen_write_set_cursor(ctx, xx, s->cy); 1710 screen_write_initctx(ctx, &ttyctx, 0); 1711 ttyctx.cell = gc; 1712 tty_write(tty_cmd_cell, &ttyctx); 1713 s->cx = cx; s->cy = cy; 1714 } 1715 return; 1716 } 1717 1718 /* Flush any existing scrolling. */ 1719 screen_write_collect_flush(ctx, 1, __func__); 1720 1721 /* If this character doesn't fit, ignore it. */ 1722 if ((~s->mode & MODE_WRAP) && 1723 width > 1 && 1724 (width > sx || (s->cx != sx && s->cx > sx - width))) 1725 return; 1726 1727 /* If in insert mode, make space for the cells. */ 1728 if (s->mode & MODE_INSERT) { 1729 grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8); 1730 skip = 0; 1731 } 1732 1733 /* Check this will fit on the current line and wrap if not. */ 1734 if ((s->mode & MODE_WRAP) && s->cx > sx - width) { 1735 log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy); 1736 screen_write_linefeed(ctx, 1, 8); 1737 screen_write_set_cursor(ctx, 0, -1); 1738 screen_write_collect_flush(ctx, 1, __func__); 1739 } 1740 1741 /* Sanity check cursor position. */ 1742 if (s->cx > sx - width || s->cy > sy - 1) 1743 return; 1744 screen_write_initctx(ctx, &ttyctx, 0); 1745 1746 /* Handle overwriting of UTF-8 characters. */ 1747 gl = grid_get_line(s->grid, s->grid->hsize + s->cy); 1748 if (gl->flags & GRID_LINE_EXTENDED) { 1749 grid_view_get_cell(gd, s->cx, s->cy, &now_gc); 1750 if (screen_write_overwrite(ctx, &now_gc, width)) 1751 skip = 0; 1752 } 1753 1754 /* 1755 * If the new character is UTF-8 wide, fill in padding cells. Have 1756 * already ensured there is enough room. 1757 */ 1758 for (xx = s->cx + 1; xx < s->cx + width; xx++) { 1759 log_debug("%s: new padding at %u,%u", __func__, xx, s->cy); 1760 grid_view_set_padding(gd, xx, s->cy); 1761 skip = 0; 1762 } 1763 1764 /* If no change, do not draw. */ 1765 if (skip) { 1766 if (s->cx >= gl->cellsize) 1767 skip = grid_cells_equal(gc, &grid_default_cell); 1768 else { 1769 gce = &gl->celldata[s->cx]; 1770 if (gce->flags & GRID_FLAG_EXTENDED) 1771 skip = 0; 1772 else if (gc->flags != gce->flags) 1773 skip = 0; 1774 else if (gc->attr != gce->data.attr) 1775 skip = 0; 1776 else if (gc->fg != gce->data.fg) 1777 skip = 0; 1778 else if (gc->bg != gce->data.bg) 1779 skip = 0; 1780 else if (gc->data.width != 1) 1781 skip = 0; 1782 else if (gc->data.size != 1) 1783 skip = 0; 1784 else if (gce->data.data != gc->data.data[0]) 1785 skip = 0; 1786 } 1787 } 1788 1789 /* Update the selected flag and set the cell. */ 1790 selected = screen_check_selection(s, s->cx, s->cy); 1791 if (selected && (~gc->flags & GRID_FLAG_SELECTED)) { 1792 memcpy(&tmp_gc, gc, sizeof tmp_gc); 1793 tmp_gc.flags |= GRID_FLAG_SELECTED; 1794 grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc); 1795 } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) { 1796 memcpy(&tmp_gc, gc, sizeof tmp_gc); 1797 tmp_gc.flags &= ~GRID_FLAG_SELECTED; 1798 grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc); 1799 } else if (!skip) 1800 grid_view_set_cell(gd, s->cx, s->cy, gc); 1801 if (selected) 1802 skip = 0; 1803 1804 /* 1805 * Move the cursor. If not wrapping, stick at the last character and 1806 * replace it. 1807 */ 1808 last = !(s->mode & MODE_WRAP); 1809 if (s->cx <= sx - last - width) 1810 screen_write_set_cursor(ctx, s->cx + width, -1); 1811 else 1812 screen_write_set_cursor(ctx, sx - last, -1); 1813 1814 /* Create space for character in insert mode. */ 1815 if (s->mode & MODE_INSERT) { 1816 screen_write_collect_flush(ctx, 0, __func__); 1817 ttyctx.num = width; 1818 tty_write(tty_cmd_insertcharacter, &ttyctx); 1819 } 1820 1821 /* Write to the screen. */ 1822 if (!skip) { 1823 if (selected) { 1824 screen_select_cell(s, &tmp_gc, gc); 1825 ttyctx.cell = &tmp_gc; 1826 } else 1827 ttyctx.cell = gc; 1828 tty_write(tty_cmd_cell, &ttyctx); 1829 ctx->written++; 1830 } else 1831 ctx->skipped++; 1832 } 1833 1834 /* Combine a UTF-8 zero-width character onto the previous. */ 1835 static const struct grid_cell * 1836 screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud, 1837 u_int *xx) 1838 { 1839 struct screen *s = ctx->s; 1840 struct grid *gd = s->grid; 1841 static struct grid_cell gc; 1842 u_int n; 1843 1844 /* Can't combine if at 0. */ 1845 if (s->cx == 0) 1846 return (NULL); 1847 1848 /* Empty data is out. */ 1849 if (ud->size == 0) 1850 fatalx("UTF-8 data empty"); 1851 1852 /* Retrieve the previous cell. */ 1853 for (n = 1; n <= s->cx; n++) { 1854 grid_view_get_cell(gd, s->cx - n, s->cy, &gc); 1855 if (~gc.flags & GRID_FLAG_PADDING) 1856 break; 1857 } 1858 if (n > s->cx) 1859 return (NULL); 1860 *xx = s->cx - n; 1861 1862 /* Check there is enough space. */ 1863 if (gc.data.size + ud->size > sizeof gc.data.data) 1864 return (NULL); 1865 1866 log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size, 1867 ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy); 1868 1869 /* Append the data. */ 1870 memcpy(gc.data.data + gc.data.size, ud->data, ud->size); 1871 gc.data.size += ud->size; 1872 1873 /* Set the new cell. */ 1874 grid_view_set_cell(gd, *xx, s->cy, &gc); 1875 1876 return (&gc); 1877 } 1878 1879 /* 1880 * UTF-8 wide characters are a bit of an annoyance. They take up more than one 1881 * cell on the screen, so following cells must not be drawn by marking them as 1882 * padding. 1883 * 1884 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8 1885 * character, it is necessary to also overwrite any other cells which covered 1886 * by the same character. 1887 */ 1888 static int 1889 screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc, 1890 u_int width) 1891 { 1892 struct screen *s = ctx->s; 1893 struct grid *gd = s->grid; 1894 struct grid_cell tmp_gc; 1895 u_int xx; 1896 int done = 0; 1897 1898 if (gc->flags & GRID_FLAG_PADDING) { 1899 /* 1900 * A padding cell, so clear any following and leading padding 1901 * cells back to the character. Don't overwrite the current 1902 * cell as that happens later anyway. 1903 */ 1904 xx = s->cx + 1; 1905 while (--xx > 0) { 1906 grid_view_get_cell(gd, xx, s->cy, &tmp_gc); 1907 if (~tmp_gc.flags & GRID_FLAG_PADDING) 1908 break; 1909 log_debug("%s: padding at %u,%u", __func__, xx, s->cy); 1910 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell); 1911 } 1912 1913 /* Overwrite the character at the start of this padding. */ 1914 log_debug("%s: character at %u,%u", __func__, xx, s->cy); 1915 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell); 1916 done = 1; 1917 } 1918 1919 /* 1920 * Overwrite any padding cells that belong to any UTF-8 characters 1921 * we'll be overwriting with the current character. 1922 */ 1923 if (width != 1 || 1924 gc->data.width != 1 || 1925 gc->flags & GRID_FLAG_PADDING) { 1926 xx = s->cx + width - 1; 1927 while (++xx < screen_size_x(s)) { 1928 grid_view_get_cell(gd, xx, s->cy, &tmp_gc); 1929 if (~tmp_gc.flags & GRID_FLAG_PADDING) 1930 break; 1931 log_debug("%s: overwrite at %u,%u", __func__, xx, 1932 s->cy); 1933 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell); 1934 done = 1; 1935 } 1936 } 1937 1938 return (done); 1939 } 1940 1941 /* Set external clipboard. */ 1942 void 1943 screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len) 1944 { 1945 struct tty_ctx ttyctx; 1946 1947 screen_write_initctx(ctx, &ttyctx, 0); 1948 ttyctx.ptr = str; 1949 ttyctx.num = len; 1950 1951 tty_write(tty_cmd_setselection, &ttyctx); 1952 } 1953 1954 /* Write unmodified string. */ 1955 void 1956 screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len) 1957 { 1958 struct tty_ctx ttyctx; 1959 1960 screen_write_initctx(ctx, &ttyctx, 0); 1961 ttyctx.ptr = str; 1962 ttyctx.num = len; 1963 1964 tty_write(tty_cmd_rawstring, &ttyctx); 1965 } 1966 1967 /* Turn alternate screen on. */ 1968 void 1969 screen_write_alternateon(struct screen_write_ctx *ctx, struct grid_cell *gc, 1970 int cursor) 1971 { 1972 struct tty_ctx ttyctx; 1973 struct window_pane *wp = ctx->wp; 1974 1975 if (wp != NULL && !options_get_number(wp->options, "alternate-screen")) 1976 return; 1977 screen_alternate_on(ctx->s, gc, cursor); 1978 1979 screen_write_initctx(ctx, &ttyctx, 1); 1980 ttyctx.redraw_cb(&ttyctx); 1981 } 1982 1983 /* Turn alternate screen off. */ 1984 void 1985 screen_write_alternateoff(struct screen_write_ctx *ctx, struct grid_cell *gc, 1986 int cursor) 1987 { 1988 struct tty_ctx ttyctx; 1989 struct window_pane *wp = ctx->wp; 1990 1991 if (wp != NULL && !options_get_number(wp->options, "alternate-screen")) 1992 return; 1993 screen_alternate_off(ctx->s, gc, cursor); 1994 1995 screen_write_initctx(ctx, &ttyctx, 1); 1996 ttyctx.redraw_cb(&ttyctx); 1997 } 1998