Annotation of src/usr.bin/tmux/screen.c, Revision 1.71
1.71 ! nicm 1: /* $OpenBSD: screen.c,v 1.70 2021/03/12 08:39:17 nicm Exp $ */
1.1 nicm 2:
3: /*
1.37 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.3 nicm 21: #include <stdlib.h>
1.1 nicm 22: #include <string.h>
1.16 nicm 23: #include <unistd.h>
1.47 nicm 24: #include <vis.h>
1.1 nicm 25:
26: #include "tmux.h"
27:
1.52 nicm 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. */
1.48 nicm 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:
1.66 nicm 51: static void screen_resize_y(struct screen *, u_int, int, u_int *);
1.67 nicm 52: static void screen_reflow(struct screen *, u_int, u_int *, u_int *, int);
1.42 nicm 53:
1.48 nicm 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:
1.1 nicm 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);
1.59 nicm 78: s->saved_grid = NULL;
79:
1.34 nicm 80: s->title = xstrdup("");
1.48 nicm 81: s->titles = NULL;
1.68 nicm 82: s->path = NULL;
1.1 nicm 83:
1.21 nicm 84: s->cstyle = 0;
1.20 nicm 85: s->ccolour = xstrdup("");
1.3 nicm 86: s->tabs = NULL;
1.52 nicm 87: s->sel = NULL;
1.3 nicm 88:
1.64 nicm 89: s->write_list = NULL;
90:
1.1 nicm 91: screen_reinit(s);
92: }
93:
94: /* Reinitialise screen. */
95: void
96: screen_reinit(struct screen *s)
97: {
98: s->cx = 0;
99: s->cy = 0;
100:
101: s->rupper = 0;
102: s->rlower = screen_size_y(s) - 1;
103:
1.71 ! nicm 104: s->mode = MODE_CURSOR|MODE_WRAP;
! 105: if (options_get_number(global_options, "extended-keys") == 2)
! 106: s->mode |= MODE_KEXTENDED;
1.12 nicm 107:
1.59 nicm 108: if (s->saved_grid != NULL)
109: screen_alternate_off(s, NULL, 0);
110: s->saved_cx = UINT_MAX;
111: s->saved_cy = UINT_MAX;
112:
1.3 nicm 113: screen_reset_tabs(s);
1.1 nicm 114:
1.44 nicm 115: grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
1.1 nicm 116:
117: screen_clear_selection(s);
1.48 nicm 118: screen_free_titles(s);
1.1 nicm 119: }
120:
121: /* Destroy a screen. */
122: void
123: screen_free(struct screen *s)
124: {
1.52 nicm 125: free(s->sel);
1.23 nicm 126: free(s->tabs);
1.68 nicm 127: free(s->path);
1.23 nicm 128: free(s->title);
129: free(s->ccolour);
1.48 nicm 130:
1.64 nicm 131: if (s->write_list != NULL)
132: screen_write_free_list(s);
133:
1.59 nicm 134: if (s->saved_grid != NULL)
135: grid_destroy(s->saved_grid);
1.1 nicm 136: grid_destroy(s->grid);
1.48 nicm 137:
138: screen_free_titles(s);
1.1 nicm 139: }
140:
1.3 nicm 141: /* Reset tabs to default, eight spaces apart. */
142: void
143: screen_reset_tabs(struct screen *s)
144: {
145: u_int i;
146:
1.23 nicm 147: free(s->tabs);
1.3 nicm 148:
149: if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
150: fatal("bit_alloc failed");
151: for (i = 8; i < screen_size_x(s); i += 8)
152: bit_set(s->tabs, i);
1.21 nicm 153: }
154:
155: /* Set screen cursor style. */
156: void
157: screen_set_cursor_style(struct screen *s, u_int style)
158: {
1.70 nicm 159: if (style <= 6) {
1.21 nicm 160: s->cstyle = style;
1.70 nicm 161: s->mode &= ~MODE_BLINKING;
162: }
1.20 nicm 163: }
164:
165: /* Set screen cursor colour. */
166: void
1.36 nicm 167: screen_set_cursor_colour(struct screen *s, const char *colour)
1.20 nicm 168: {
1.23 nicm 169: free(s->ccolour);
1.36 nicm 170: s->ccolour = xstrdup(colour);
1.3 nicm 171: }
172:
1.1 nicm 173: /* Set screen title. */
1.57 nicm 174: int
1.1 nicm 175: screen_set_title(struct screen *s, const char *title)
176: {
1.57 nicm 177: if (!utf8_isvalid(title))
178: return (0);
1.23 nicm 179: free(s->title);
1.57 nicm 180: s->title = xstrdup(title);
181: return (1);
1.56 nicm 182: }
183:
184: /* Set screen path. */
185: void
186: screen_set_path(struct screen *s, const char *path)
187: {
188: free(s->path);
189: utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.48 nicm 190: }
191:
192: /* Push the current title onto the stack. */
193: void
194: screen_push_title(struct screen *s)
195: {
196: struct screen_title_entry *title_entry;
197:
198: if (s->titles == NULL) {
199: s->titles = xmalloc(sizeof *s->titles);
200: TAILQ_INIT(s->titles);
201: }
202: title_entry = xmalloc(sizeof *title_entry);
203: title_entry->text = xstrdup(s->title);
204: TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
205: }
206:
207: /*
208: * Pop a title from the stack and set it as the screen title. If the stack is
209: * empty, do nothing.
210: */
211: void
212: screen_pop_title(struct screen *s)
213: {
214: struct screen_title_entry *title_entry;
215:
216: if (s->titles == NULL)
217: return;
218:
219: title_entry = TAILQ_FIRST(s->titles);
220: if (title_entry != NULL) {
221: screen_set_title(s, title_entry->text);
222:
223: TAILQ_REMOVE(s->titles, title_entry, entry);
224: free(title_entry->text);
225: free(title_entry);
226: }
1.1 nicm 227: }
228:
1.67 nicm 229: /* Resize screen with options. */
1.1 nicm 230: void
1.62 nicm 231: screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow,
1.67 nicm 232: int eat_empty, int cursor)
1.1 nicm 233: {
1.67 nicm 234: u_int cx = s->cx, cy = s->grid->hsize + s->cy;
1.64 nicm 235:
1.65 nicm 236: if (s->write_list != NULL)
1.64 nicm 237: screen_write_free_list(s);
1.62 nicm 238:
1.63 nicm 239: log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
240: __func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy,
1.67 nicm 241: cx, cy);
1.62 nicm 242:
1.1 nicm 243: if (sx < 1)
244: sx = 1;
245: if (sy < 1)
246: sy = 1;
247:
1.3 nicm 248: if (sx != screen_size_x(s)) {
1.54 nicm 249: s->grid->sx = sx;
1.3 nicm 250: screen_reset_tabs(s);
1.49 nicm 251: } else
252: reflow = 0;
1.3 nicm 253:
1.1 nicm 254: if (sy != screen_size_y(s))
1.67 nicm 255: screen_resize_y(s, sy, eat_empty, &cy);
1.24 nicm 256:
257: if (reflow)
1.67 nicm 258: screen_reflow(s, sx, &cx, &cy, cursor);
1.62 nicm 259:
1.67 nicm 260: if (cy >= s->grid->hsize) {
261: s->cx = cx;
262: s->cy = cy - s->grid->hsize;
1.62 nicm 263: } else {
264: s->cx = 0;
265: s->cy = 0;
266: }
1.67 nicm 267:
1.63 nicm 268: log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx,
1.67 nicm 269: s->cy, cx, cy);
1.65 nicm 270:
271: if (s->write_list != NULL)
272: screen_write_make_list(s);
1.62 nicm 273: }
274:
275: /* Resize screen. */
276: void
277: screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
278: {
1.67 nicm 279: screen_resize_cursor(s, sx, sy, reflow, 1, 1);
1.1 nicm 280: }
281:
1.41 nicm 282: static void
1.66 nicm 283: screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy)
1.1 nicm 284: {
285: struct grid *gd = s->grid;
1.4 nicm 286: u_int needed, available, oldy, i;
1.1 nicm 287:
288: if (sy == 0)
289: fatalx("zero size");
1.4 nicm 290: oldy = screen_size_y(s);
291:
1.12 nicm 292: /*
1.4 nicm 293: * When resizing:
294: *
295: * If the height is decreasing, delete lines from the bottom until
296: * hitting the cursor, then push lines from the top into the history.
1.12 nicm 297: *
1.40 nicm 298: * When increasing, pull as many lines as possible from scrolled
299: * history (not explicitly cleared from view) to the top, then fill the
300: * remaining with blanks at the bottom.
1.4 nicm 301: */
1.1 nicm 302:
303: /* Size decreasing. */
1.4 nicm 304: if (sy < oldy) {
305: needed = oldy - sy;
1.1 nicm 306:
1.4 nicm 307: /* Delete as many lines as possible from the bottom. */
1.62 nicm 308: if (eat_empty) {
309: available = oldy - 1 - s->cy;
310: if (available > 0) {
311: if (available > needed)
312: available = needed;
313: grid_view_delete_lines(gd, oldy - available,
314: available, 8);
315: }
316: needed -= available;
1.1 nicm 317: }
318:
1.4 nicm 319: /*
1.8 nicm 320: * Now just increase the history size, if possible, to take
321: * over the lines which are left. If history is off, delete
322: * lines from the top.
1.4 nicm 323: */
1.8 nicm 324: available = s->cy;
1.40 nicm 325: if (gd->flags & GRID_HISTORY) {
326: gd->hscrolled += needed;
1.8 nicm 327: gd->hsize += needed;
1.40 nicm 328: } else if (needed > 0 && available > 0) {
1.8 nicm 329: if (available > needed)
330: available = needed;
1.44 nicm 331: grid_view_delete_lines(gd, 0, available, 8);
1.66 nicm 332: (*cy) -= available;
1.8 nicm 333: }
1.12 nicm 334: }
1.1 nicm 335:
1.51 nicm 336: /* Resize line array. */
337: grid_adjust_lines(gd, gd->hsize + sy);
1.1 nicm 338:
339: /* Size increasing. */
1.4 nicm 340: if (sy > oldy) {
341: needed = sy - oldy;
342:
1.8 nicm 343: /*
1.41 nicm 344: * Try to pull as much as possible out of scrolled history, if
345: * is is enabled.
1.8 nicm 346: */
1.40 nicm 347: available = gd->hscrolled;
1.8 nicm 348: if (gd->flags & GRID_HISTORY && available > 0) {
1.4 nicm 349: if (available > needed)
350: available = needed;
1.40 nicm 351: gd->hscrolled -= available;
1.4 nicm 352: gd->hsize -= available;
1.8 nicm 353: } else
354: available = 0;
1.4 nicm 355: needed -= available;
356:
357: /* Then fill the rest in with blanks. */
1.11 nicm 358: for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
1.61 nicm 359: grid_empty_line(gd, i, 8);
1.1 nicm 360: }
361:
1.4 nicm 362: /* Set the new size, and reset the scroll region. */
1.1 nicm 363: gd->sy = sy;
364: s->rupper = 0;
365: s->rlower = screen_size_y(s) - 1;
366: }
367:
368: /* Set selection. */
369: void
1.14 nicm 370: screen_set_selection(struct screen *s, u_int sx, u_int sy,
1.52 nicm 371: u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
1.1 nicm 372: {
1.52 nicm 373: if (s->sel == NULL)
374: s->sel = xcalloc(1, sizeof *s->sel);
1.1 nicm 375:
1.52 nicm 376: memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
377: s->sel->hidden = 0;
378: s->sel->rectangle = rectangle;
379: s->sel->modekeys = modekeys;
380:
381: s->sel->sx = sx;
382: s->sel->sy = sy;
383: s->sel->ex = ex;
384: s->sel->ey = ey;
1.1 nicm 385: }
386:
387: /* Clear selection. */
388: void
389: screen_clear_selection(struct screen *s)
390: {
1.52 nicm 391: free(s->sel);
392: s->sel = NULL;
1.1 nicm 393: }
394:
1.45 nicm 395: /* Hide selection. */
396: void
397: screen_hide_selection(struct screen *s)
398: {
1.52 nicm 399: if (s->sel != NULL)
400: s->sel->hidden = 1;
1.45 nicm 401: }
402:
1.1 nicm 403: /* Check if cell in selection. */
404: int
405: screen_check_selection(struct screen *s, u_int px, u_int py)
406: {
1.52 nicm 407: struct screen_sel *sel = s->sel;
1.28 nicm 408: u_int xx;
1.1 nicm 409:
1.52 nicm 410: if (sel == NULL || sel->hidden)
1.1 nicm 411: return (0);
412:
1.52 nicm 413: if (sel->rectangle) {
1.14 nicm 414: if (sel->sy < sel->ey) {
415: /* start line < end line -- downward selection. */
416: if (py < sel->sy || py > sel->ey)
417: return (0);
418: } else if (sel->sy > sel->ey) {
419: /* start line > end line -- upward selection. */
420: if (py > sel->sy || py < sel->ey)
421: return (0);
422: } else {
423: /* starting line == ending line. */
424: if (py != sel->sy)
425: return (0);
426: }
427:
428: /*
429: * Need to include the selection start row, but not the cursor
430: * row, which means the selection changes depending on which
431: * one is on the left.
432: */
433: if (sel->ex < sel->sx) {
434: /* Cursor (ex) is on the left. */
1.17 nicm 435: if (px < sel->ex)
1.14 nicm 436: return (0);
437:
438: if (px > sel->sx)
439: return (0);
440: } else {
441: /* Selection start (sx) is on the left. */
442: if (px < sel->sx)
443: return (0);
444:
1.17 nicm 445: if (px > sel->ex)
1.14 nicm 446: return (0);
447: }
448: } else {
449: /*
450: * Like emacs, keep the top-left-most character, and drop the
451: * bottom-right-most, regardless of copy direction.
452: */
453: if (sel->sy < sel->ey) {
454: /* starting line < ending line -- downward selection. */
455: if (py < sel->sy || py > sel->ey)
456: return (0);
457:
1.28 nicm 458: if (py == sel->sy && px < sel->sx)
1.29 nicm 459: return (0);
1.28 nicm 460:
1.53 nicm 461: if (sel->modekeys == MODEKEY_EMACS)
462: xx = (sel->ex == 0 ? 0 : sel->ex - 1);
463: else
464: xx = sel->ex;
465: if (py == sel->ey && px > xx)
1.14 nicm 466: return (0);
467: } else if (sel->sy > sel->ey) {
468: /* starting line > ending line -- upward selection. */
469: if (py > sel->sy || py < sel->ey)
470: return (0);
471:
1.28 nicm 472: if (py == sel->ey && px < sel->ex)
473: return (0);
474:
475: if (sel->modekeys == MODEKEY_EMACS)
476: xx = sel->sx - 1;
477: else
478: xx = sel->sx;
1.46 nicm 479: if (py == sel->sy && (sel->sx == 0 || px > xx))
1.14 nicm 480: return (0);
481: } else {
482: /* starting line == ending line. */
483: if (py != sel->sy)
484: return (0);
485:
486: if (sel->ex < sel->sx) {
487: /* cursor (ex) is on the left */
1.28 nicm 488: if (sel->modekeys == MODEKEY_EMACS)
489: xx = sel->sx - 1;
490: else
491: xx = sel->sx;
492: if (px > xx || px < sel->ex)
1.14 nicm 493: return (0);
494: } else {
495: /* selection start (sx) is on the left */
1.53 nicm 496: if (sel->modekeys == MODEKEY_EMACS)
497: xx = (sel->ex == 0 ? 0 : sel->ex - 1);
498: else
499: xx = sel->ex;
500: if (px < sel->sx || px > xx)
1.14 nicm 501: return (0);
502: }
503: }
1.1 nicm 504: }
505:
506: return (1);
1.43 nicm 507: }
508:
509: /* Get selected grid cell. */
510: void
511: screen_select_cell(struct screen *s, struct grid_cell *dst,
512: const struct grid_cell *src)
513: {
1.52 nicm 514: if (s->sel == NULL || s->sel->hidden)
1.43 nicm 515: return;
516:
1.52 nicm 517: memcpy(dst, &s->sel->cell, sizeof *dst);
1.43 nicm 518:
519: utf8_copy(&dst->data, &src->data);
520: dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
521: dst->attr |= src->attr & GRID_ATTR_CHARSET;
522: dst->flags = src->flags;
1.24 nicm 523: }
524:
525: /* Reflow wrapped lines. */
1.42 nicm 526: static void
1.67 nicm 527: screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor)
1.24 nicm 528: {
1.62 nicm 529: u_int wx, wy;
1.54 nicm 530:
1.67 nicm 531: if (cursor) {
532: grid_wrap_position(s->grid, *cx, *cy, &wx, &wy);
533: log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx,
534: wy);
535: }
1.54 nicm 536:
537: grid_reflow(s->grid, new_x);
538:
1.67 nicm 539: if (cursor) {
540: grid_unwrap_position(s->grid, cx, cy, wx, wy);
541: log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy);
542: }
543: else {
544: *cx = 0;
545: *cy = s->grid->hsize;
546: }
1.59 nicm 547: }
548:
549: /*
550: * Enter alternative screen mode. A copy of the visible screen is saved and the
551: * history is not updated.
552: */
553: void
554: screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor)
555: {
556: u_int sx, sy;
557:
558: if (s->saved_grid != NULL)
559: return;
560: sx = screen_size_x(s);
561: sy = screen_size_y(s);
562:
563: s->saved_grid = grid_create(sx, sy, 0);
564: grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy);
565: if (cursor) {
566: s->saved_cx = s->cx;
567: s->saved_cy = s->cy;
568: }
569: memcpy(&s->saved_cell, gc, sizeof s->saved_cell);
570:
571: grid_view_clear(s->grid, 0, 0, sx, sy, 8);
572:
1.60 nicm 573: s->saved_flags = s->grid->flags;
1.59 nicm 574: s->grid->flags &= ~GRID_HISTORY;
575: }
576:
577: /* Exit alternate screen mode and restore the copied grid. */
578: void
579: screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor)
580: {
1.69 nicm 581: u_int sx = screen_size_x(s), sy = screen_size_y(s);
582:
583: /*
584: * If the current size is different, temporarily resize to the old size
585: * before copying back.
586: */
587: if (s->saved_grid != NULL)
588: screen_resize(s, s->saved_grid->sx, s->saved_grid->sy, 1);
1.59 nicm 589:
590: /*
591: * Restore the cursor position and cell. This happens even if not
592: * currently in the alternate screen.
593: */
594: if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) {
595: s->cx = s->saved_cx;
596: s->cy = s->saved_cy;
597: if (gc != NULL)
598: memcpy(gc, &s->saved_cell, sizeof *gc);
599: }
600:
1.69 nicm 601: /* If not in the alternate screen, do nothing more. */
602: if (s->saved_grid == NULL) {
603: if (s->cx > screen_size_x(s) - 1)
604: s->cx = screen_size_x(s) - 1;
605: if (s->cy > screen_size_y(s) - 1)
606: s->cy = screen_size_y(s) - 1;
1.59 nicm 607: return;
1.69 nicm 608: }
1.59 nicm 609:
610: /* Restore the saved grid. */
1.69 nicm 611: grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0,
612: s->saved_grid->sy);
1.59 nicm 613:
614: /*
615: * Turn history back on (so resize can use it) and then resize back to
616: * the current size.
617: */
1.60 nicm 618: if (s->saved_flags & GRID_HISTORY)
619: s->grid->flags |= GRID_HISTORY;
1.69 nicm 620: screen_resize(s, sx, sy, 1);
1.59 nicm 621:
622: grid_destroy(s->saved_grid);
623: s->saved_grid = NULL;
1.69 nicm 624:
625: if (s->cx > screen_size_x(s) - 1)
626: s->cx = screen_size_x(s) - 1;
627: if (s->cy > screen_size_y(s) - 1)
628: s->cy = screen_size_y(s) - 1;
1.1 nicm 629: }