1 /* $OpenBSD: screen.c,v 1.86 2024/08/21 04:17:09 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 #include <unistd.h> 24 #include <vis.h> 25 26 #include "tmux.h" 27 28 /* Selected area in screen. */ 29 struct screen_sel { 30 int hidden; 31 int rectangle; 32 int modekeys; 33 34 u_int sx; 35 u_int sy; 36 37 u_int ex; 38 u_int ey; 39 40 struct grid_cell cell; 41 }; 42 43 /* Entry on title stack. */ 44 struct screen_title_entry { 45 char *text; 46 47 TAILQ_ENTRY(screen_title_entry) entry; 48 }; 49 TAILQ_HEAD(screen_titles, screen_title_entry); 50 51 static void screen_resize_y(struct screen *, u_int, int, u_int *); 52 static void screen_reflow(struct screen *, u_int, u_int *, u_int *, int); 53 54 /* Free titles stack. */ 55 static void 56 screen_free_titles(struct screen *s) 57 { 58 struct screen_title_entry *title_entry; 59 60 if (s->titles == NULL) 61 return; 62 63 while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) { 64 TAILQ_REMOVE(s->titles, title_entry, entry); 65 free(title_entry->text); 66 free(title_entry); 67 } 68 69 free(s->titles); 70 s->titles = NULL; 71 } 72 73 /* Create a new screen. */ 74 void 75 screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit) 76 { 77 s->grid = grid_create(sx, sy, hlimit); 78 s->saved_grid = NULL; 79 80 s->title = xstrdup(""); 81 s->titles = NULL; 82 s->path = NULL; 83 84 s->cstyle = SCREEN_CURSOR_DEFAULT; 85 s->default_cstyle = SCREEN_CURSOR_DEFAULT; 86 s->mode = MODE_CURSOR; 87 s->default_mode = 0; 88 s->ccolour = -1; 89 s->default_ccolour = -1; 90 s->tabs = NULL; 91 s->sel = NULL; 92 93 s->write_list = NULL; 94 s->hyperlinks = NULL; 95 96 screen_reinit(s); 97 } 98 99 /* Reinitialise screen. */ 100 void 101 screen_reinit(struct screen *s) 102 { 103 s->cx = 0; 104 s->cy = 0; 105 106 s->rupper = 0; 107 s->rlower = screen_size_y(s) - 1; 108 109 s->mode = MODE_CURSOR|MODE_WRAP|(s->mode & MODE_CRLF); 110 111 if (options_get_number(global_options, "extended-keys") == 2) 112 s->mode = (s->mode & ~EXTENDED_KEY_MODES)|MODE_KEYS_EXTENDED; 113 114 if (s->saved_grid != NULL) 115 screen_alternate_off(s, NULL, 0); 116 s->saved_cx = UINT_MAX; 117 s->saved_cy = UINT_MAX; 118 119 screen_reset_tabs(s); 120 121 grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8); 122 123 screen_clear_selection(s); 124 screen_free_titles(s); 125 screen_reset_hyperlinks(s); 126 } 127 128 /* Reset hyperlinks of a screen. */ 129 void 130 screen_reset_hyperlinks(struct screen *s) 131 { 132 if (s->hyperlinks == NULL) 133 s->hyperlinks = hyperlinks_init(); 134 else 135 hyperlinks_reset(s->hyperlinks); 136 } 137 138 /* Destroy a screen. */ 139 void 140 screen_free(struct screen *s) 141 { 142 free(s->sel); 143 free(s->tabs); 144 free(s->path); 145 free(s->title); 146 147 if (s->write_list != NULL) 148 screen_write_free_list(s); 149 150 if (s->saved_grid != NULL) 151 grid_destroy(s->saved_grid); 152 grid_destroy(s->grid); 153 154 if (s->hyperlinks != NULL) 155 hyperlinks_free(s->hyperlinks); 156 screen_free_titles(s); 157 } 158 159 /* Reset tabs to default, eight spaces apart. */ 160 void 161 screen_reset_tabs(struct screen *s) 162 { 163 u_int i; 164 165 free(s->tabs); 166 167 if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL) 168 fatal("bit_alloc failed"); 169 for (i = 8; i < screen_size_x(s); i += 8) 170 bit_set(s->tabs, i); 171 } 172 173 /* Set screen cursor style and mode. */ 174 void 175 screen_set_cursor_style(u_int style, enum screen_cursor_style *cstyle, 176 int *mode) 177 { 178 switch (style) { 179 case 0: 180 *cstyle = SCREEN_CURSOR_DEFAULT; 181 break; 182 case 1: 183 *cstyle = SCREEN_CURSOR_BLOCK; 184 *mode |= MODE_CURSOR_BLINKING; 185 break; 186 case 2: 187 *cstyle = SCREEN_CURSOR_BLOCK; 188 *mode &= ~MODE_CURSOR_BLINKING; 189 break; 190 case 3: 191 *cstyle = SCREEN_CURSOR_UNDERLINE; 192 *mode |= MODE_CURSOR_BLINKING; 193 break; 194 case 4: 195 *cstyle = SCREEN_CURSOR_UNDERLINE; 196 *mode &= ~MODE_CURSOR_BLINKING; 197 break; 198 case 5: 199 *cstyle = SCREEN_CURSOR_BAR; 200 *mode |= MODE_CURSOR_BLINKING; 201 break; 202 case 6: 203 *cstyle = SCREEN_CURSOR_BAR; 204 *mode &= ~MODE_CURSOR_BLINKING; 205 break; 206 } 207 } 208 209 /* Set screen cursor colour. */ 210 void 211 screen_set_cursor_colour(struct screen *s, int colour) 212 { 213 s->ccolour = colour; 214 } 215 216 /* Set screen title. */ 217 int 218 screen_set_title(struct screen *s, const char *title) 219 { 220 if (!utf8_isvalid(title)) 221 return (0); 222 free(s->title); 223 s->title = xstrdup(title); 224 return (1); 225 } 226 227 /* Set screen path. */ 228 void 229 screen_set_path(struct screen *s, const char *path) 230 { 231 free(s->path); 232 utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL); 233 } 234 235 /* Push the current title onto the stack. */ 236 void 237 screen_push_title(struct screen *s) 238 { 239 struct screen_title_entry *title_entry; 240 241 if (s->titles == NULL) { 242 s->titles = xmalloc(sizeof *s->titles); 243 TAILQ_INIT(s->titles); 244 } 245 title_entry = xmalloc(sizeof *title_entry); 246 title_entry->text = xstrdup(s->title); 247 TAILQ_INSERT_HEAD(s->titles, title_entry, entry); 248 } 249 250 /* 251 * Pop a title from the stack and set it as the screen title. If the stack is 252 * empty, do nothing. 253 */ 254 void 255 screen_pop_title(struct screen *s) 256 { 257 struct screen_title_entry *title_entry; 258 259 if (s->titles == NULL) 260 return; 261 262 title_entry = TAILQ_FIRST(s->titles); 263 if (title_entry != NULL) { 264 screen_set_title(s, title_entry->text); 265 266 TAILQ_REMOVE(s->titles, title_entry, entry); 267 free(title_entry->text); 268 free(title_entry); 269 } 270 } 271 272 /* Resize screen with options. */ 273 void 274 screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow, 275 int eat_empty, int cursor) 276 { 277 u_int cx = s->cx, cy = s->grid->hsize + s->cy; 278 279 if (s->write_list != NULL) 280 screen_write_free_list(s); 281 282 log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)", 283 __func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy, 284 cx, cy); 285 286 if (sx < 1) 287 sx = 1; 288 if (sy < 1) 289 sy = 1; 290 291 if (sx != screen_size_x(s)) { 292 s->grid->sx = sx; 293 screen_reset_tabs(s); 294 } else 295 reflow = 0; 296 297 if (sy != screen_size_y(s)) 298 screen_resize_y(s, sy, eat_empty, &cy); 299 300 if (reflow) 301 screen_reflow(s, sx, &cx, &cy, cursor); 302 303 if (cy >= s->grid->hsize) { 304 s->cx = cx; 305 s->cy = cy - s->grid->hsize; 306 } else { 307 s->cx = 0; 308 s->cy = 0; 309 } 310 311 log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx, 312 s->cy, cx, cy); 313 314 if (s->write_list != NULL) 315 screen_write_make_list(s); 316 } 317 318 /* Resize screen. */ 319 void 320 screen_resize(struct screen *s, u_int sx, u_int sy, int reflow) 321 { 322 screen_resize_cursor(s, sx, sy, reflow, 1, 1); 323 } 324 325 static void 326 screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy) 327 { 328 struct grid *gd = s->grid; 329 u_int needed, available, oldy, i; 330 331 if (sy == 0) 332 fatalx("zero size"); 333 oldy = screen_size_y(s); 334 335 /* 336 * When resizing: 337 * 338 * If the height is decreasing, delete lines from the bottom until 339 * hitting the cursor, then push lines from the top into the history. 340 * 341 * When increasing, pull as many lines as possible from scrolled 342 * history (not explicitly cleared from view) to the top, then fill the 343 * remaining with blanks at the bottom. 344 */ 345 346 /* Size decreasing. */ 347 if (sy < oldy) { 348 needed = oldy - sy; 349 350 /* Delete as many lines as possible from the bottom. */ 351 if (eat_empty) { 352 available = oldy - 1 - s->cy; 353 if (available > 0) { 354 if (available > needed) 355 available = needed; 356 grid_view_delete_lines(gd, oldy - available, 357 available, 8); 358 } 359 needed -= available; 360 } 361 362 /* 363 * Now just increase the history size, if possible, to take 364 * over the lines which are left. If history is off, delete 365 * lines from the top. 366 */ 367 available = s->cy; 368 if (gd->flags & GRID_HISTORY) { 369 gd->hscrolled += needed; 370 gd->hsize += needed; 371 } else if (needed > 0 && available > 0) { 372 if (available > needed) 373 available = needed; 374 grid_view_delete_lines(gd, 0, available, 8); 375 (*cy) -= available; 376 } 377 } 378 379 /* Resize line array. */ 380 grid_adjust_lines(gd, gd->hsize + sy); 381 382 /* Size increasing. */ 383 if (sy > oldy) { 384 needed = sy - oldy; 385 386 /* 387 * Try to pull as much as possible out of scrolled history, if 388 * it is enabled. 389 */ 390 available = gd->hscrolled; 391 if (gd->flags & GRID_HISTORY && available > 0) { 392 if (available > needed) 393 available = needed; 394 gd->hscrolled -= available; 395 gd->hsize -= available; 396 } else 397 available = 0; 398 needed -= available; 399 400 /* Then fill the rest in with blanks. */ 401 for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++) 402 grid_empty_line(gd, i, 8); 403 } 404 405 /* Set the new size, and reset the scroll region. */ 406 gd->sy = sy; 407 s->rupper = 0; 408 s->rlower = screen_size_y(s) - 1; 409 } 410 411 /* Set selection. */ 412 void 413 screen_set_selection(struct screen *s, u_int sx, u_int sy, 414 u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc) 415 { 416 if (s->sel == NULL) 417 s->sel = xcalloc(1, sizeof *s->sel); 418 419 memcpy(&s->sel->cell, gc, sizeof s->sel->cell); 420 s->sel->hidden = 0; 421 s->sel->rectangle = rectangle; 422 s->sel->modekeys = modekeys; 423 424 s->sel->sx = sx; 425 s->sel->sy = sy; 426 s->sel->ex = ex; 427 s->sel->ey = ey; 428 } 429 430 /* Clear selection. */ 431 void 432 screen_clear_selection(struct screen *s) 433 { 434 free(s->sel); 435 s->sel = NULL; 436 } 437 438 /* Hide selection. */ 439 void 440 screen_hide_selection(struct screen *s) 441 { 442 if (s->sel != NULL) 443 s->sel->hidden = 1; 444 } 445 446 /* Check if cell in selection. */ 447 int 448 screen_check_selection(struct screen *s, u_int px, u_int py) 449 { 450 struct screen_sel *sel = s->sel; 451 u_int xx; 452 453 if (sel == NULL || sel->hidden) 454 return (0); 455 456 if (sel->rectangle) { 457 if (sel->sy < sel->ey) { 458 /* start line < end line -- downward selection. */ 459 if (py < sel->sy || py > sel->ey) 460 return (0); 461 } else if (sel->sy > sel->ey) { 462 /* start line > end line -- upward selection. */ 463 if (py > sel->sy || py < sel->ey) 464 return (0); 465 } else { 466 /* starting line == ending line. */ 467 if (py != sel->sy) 468 return (0); 469 } 470 471 /* 472 * Need to include the selection start row, but not the cursor 473 * row, which means the selection changes depending on which 474 * one is on the left. 475 */ 476 if (sel->ex < sel->sx) { 477 /* Cursor (ex) is on the left. */ 478 if (px < sel->ex) 479 return (0); 480 481 if (px > sel->sx) 482 return (0); 483 } else { 484 /* Selection start (sx) is on the left. */ 485 if (px < sel->sx) 486 return (0); 487 488 if (px > sel->ex) 489 return (0); 490 } 491 } else { 492 /* 493 * Like emacs, keep the top-left-most character, and drop the 494 * bottom-right-most, regardless of copy direction. 495 */ 496 if (sel->sy < sel->ey) { 497 /* starting line < ending line -- downward selection. */ 498 if (py < sel->sy || py > sel->ey) 499 return (0); 500 501 if (py == sel->sy && px < sel->sx) 502 return (0); 503 504 if (sel->modekeys == MODEKEY_EMACS) 505 xx = (sel->ex == 0 ? 0 : sel->ex - 1); 506 else 507 xx = sel->ex; 508 if (py == sel->ey && px > xx) 509 return (0); 510 } else if (sel->sy > sel->ey) { 511 /* starting line > ending line -- upward selection. */ 512 if (py > sel->sy || py < sel->ey) 513 return (0); 514 515 if (py == sel->ey && px < sel->ex) 516 return (0); 517 518 if (sel->modekeys == MODEKEY_EMACS) 519 xx = sel->sx - 1; 520 else 521 xx = sel->sx; 522 if (py == sel->sy && (sel->sx == 0 || px > xx)) 523 return (0); 524 } else { 525 /* starting line == ending line. */ 526 if (py != sel->sy) 527 return (0); 528 529 if (sel->ex < sel->sx) { 530 /* cursor (ex) is on the left */ 531 if (sel->modekeys == MODEKEY_EMACS) 532 xx = sel->sx - 1; 533 else 534 xx = sel->sx; 535 if (px > xx || px < sel->ex) 536 return (0); 537 } else { 538 /* selection start (sx) is on the left */ 539 if (sel->modekeys == MODEKEY_EMACS) 540 xx = (sel->ex == 0 ? 0 : sel->ex - 1); 541 else 542 xx = sel->ex; 543 if (px < sel->sx || px > xx) 544 return (0); 545 } 546 } 547 } 548 549 return (1); 550 } 551 552 /* Get selected grid cell. */ 553 void 554 screen_select_cell(struct screen *s, struct grid_cell *dst, 555 const struct grid_cell *src) 556 { 557 if (s->sel == NULL || s->sel->hidden) 558 return; 559 560 memcpy(dst, &s->sel->cell, sizeof *dst); 561 562 utf8_copy(&dst->data, &src->data); 563 dst->attr = dst->attr & ~GRID_ATTR_CHARSET; 564 dst->attr |= src->attr & GRID_ATTR_CHARSET; 565 dst->flags = src->flags; 566 } 567 568 /* Reflow wrapped lines. */ 569 static void 570 screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor) 571 { 572 u_int wx, wy; 573 574 if (cursor) { 575 grid_wrap_position(s->grid, *cx, *cy, &wx, &wy); 576 log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx, 577 wy); 578 } 579 580 grid_reflow(s->grid, new_x); 581 582 if (cursor) { 583 grid_unwrap_position(s->grid, cx, cy, wx, wy); 584 log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy); 585 } 586 else { 587 *cx = 0; 588 *cy = s->grid->hsize; 589 } 590 } 591 592 /* 593 * Enter alternative screen mode. A copy of the visible screen is saved and the 594 * history is not updated. 595 */ 596 void 597 screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor) 598 { 599 u_int sx, sy; 600 601 if (s->saved_grid != NULL) 602 return; 603 sx = screen_size_x(s); 604 sy = screen_size_y(s); 605 606 s->saved_grid = grid_create(sx, sy, 0); 607 grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy); 608 if (cursor) { 609 s->saved_cx = s->cx; 610 s->saved_cy = s->cy; 611 } 612 memcpy(&s->saved_cell, gc, sizeof s->saved_cell); 613 614 grid_view_clear(s->grid, 0, 0, sx, sy, 8); 615 616 s->saved_flags = s->grid->flags; 617 s->grid->flags &= ~GRID_HISTORY; 618 } 619 620 /* Exit alternate screen mode and restore the copied grid. */ 621 void 622 screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor) 623 { 624 u_int sx = screen_size_x(s), sy = screen_size_y(s); 625 626 /* 627 * If the current size is different, temporarily resize to the old size 628 * before copying back. 629 */ 630 if (s->saved_grid != NULL) 631 screen_resize(s, s->saved_grid->sx, s->saved_grid->sy, 0); 632 633 /* 634 * Restore the cursor position and cell. This happens even if not 635 * currently in the alternate screen. 636 */ 637 if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) { 638 s->cx = s->saved_cx; 639 s->cy = s->saved_cy; 640 if (gc != NULL) 641 memcpy(gc, &s->saved_cell, sizeof *gc); 642 } 643 644 /* If not in the alternate screen, do nothing more. */ 645 if (s->saved_grid == NULL) { 646 if (s->cx > screen_size_x(s) - 1) 647 s->cx = screen_size_x(s) - 1; 648 if (s->cy > screen_size_y(s) - 1) 649 s->cy = screen_size_y(s) - 1; 650 return; 651 } 652 653 /* Restore the saved grid. */ 654 grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0, 655 s->saved_grid->sy); 656 657 /* 658 * Turn history back on (so resize can use it) and then resize back to 659 * the current size. 660 */ 661 if (s->saved_flags & GRID_HISTORY) 662 s->grid->flags |= GRID_HISTORY; 663 screen_resize(s, sx, sy, 1); 664 665 grid_destroy(s->saved_grid); 666 s->saved_grid = NULL; 667 668 if (s->cx > screen_size_x(s) - 1) 669 s->cx = screen_size_x(s) - 1; 670 if (s->cy > screen_size_y(s) - 1) 671 s->cy = screen_size_y(s) - 1; 672 } 673 674 /* Get mode as a string. */ 675 const char * 676 screen_mode_to_string(int mode) 677 { 678 static char tmp[1024]; 679 680 if (mode == 0) 681 return ("NONE"); 682 if (mode == ALL_MODES) 683 return ("ALL"); 684 685 *tmp = '\0'; 686 if (mode & MODE_CURSOR) 687 strlcat(tmp, "CURSOR,", sizeof tmp); 688 if (mode & MODE_INSERT) 689 strlcat(tmp, "INSERT,", sizeof tmp); 690 if (mode & MODE_KCURSOR) 691 strlcat(tmp, "KCURSOR,", sizeof tmp); 692 if (mode & MODE_KKEYPAD) 693 strlcat(tmp, "KKEYPAD,", sizeof tmp); 694 if (mode & MODE_WRAP) 695 strlcat(tmp, "WRAP,", sizeof tmp); 696 if (mode & MODE_MOUSE_STANDARD) 697 strlcat(tmp, "MOUSE_STANDARD,", sizeof tmp); 698 if (mode & MODE_MOUSE_BUTTON) 699 strlcat(tmp, "MOUSE_BUTTON,", sizeof tmp); 700 if (mode & MODE_CURSOR_BLINKING) 701 strlcat(tmp, "CURSOR_BLINKING,", sizeof tmp); 702 if (mode & MODE_CURSOR_VERY_VISIBLE) 703 strlcat(tmp, "CURSOR_VERY_VISIBLE,", sizeof tmp); 704 if (mode & MODE_MOUSE_UTF8) 705 strlcat(tmp, "MOUSE_UTF8,", sizeof tmp); 706 if (mode & MODE_MOUSE_SGR) 707 strlcat(tmp, "MOUSE_SGR,", sizeof tmp); 708 if (mode & MODE_BRACKETPASTE) 709 strlcat(tmp, "BRACKETPASTE,", sizeof tmp); 710 if (mode & MODE_FOCUSON) 711 strlcat(tmp, "FOCUSON,", sizeof tmp); 712 if (mode & MODE_MOUSE_ALL) 713 strlcat(tmp, "MOUSE_ALL,", sizeof tmp); 714 if (mode & MODE_ORIGIN) 715 strlcat(tmp, "ORIGIN,", sizeof tmp); 716 if (mode & MODE_CRLF) 717 strlcat(tmp, "CRLF,", sizeof tmp); 718 if (mode & MODE_KEYS_EXTENDED) 719 strlcat(tmp, "KEYS_EXTENDED,", sizeof tmp); 720 if (mode & MODE_KEYS_EXTENDED_2) 721 strlcat(tmp, "KEYS_EXTENDED_2,", sizeof tmp); 722 tmp[strlen(tmp) - 1] = '\0'; 723 return (tmp); 724 } 725