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