Annotation of src/usr.bin/tmux/screen.c, Revision 1.67
1.67 ! nicm 1: /* $OpenBSD: screen.c,v 1.66 2020/04/22 08:48:44 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.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.67 ! nicm 223: /* Resize screen with options. */
1.1 nicm 224: void
1.62 nicm 225: screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow,
1.67 ! nicm 226: int eat_empty, int cursor)
1.1 nicm 227: {
1.67 ! nicm 228: u_int cx = s->cx, cy = s->grid->hsize + s->cy;
1.64 nicm 229:
1.65 nicm 230: if (s->write_list != NULL)
1.64 nicm 231: screen_write_free_list(s);
1.62 nicm 232:
1.63 nicm 233: log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
234: __func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy,
1.67 ! nicm 235: cx, cy);
1.62 nicm 236:
1.1 nicm 237: if (sx < 1)
238: sx = 1;
239: if (sy < 1)
240: sy = 1;
241:
1.3 nicm 242: if (sx != screen_size_x(s)) {
1.54 nicm 243: s->grid->sx = sx;
1.3 nicm 244: screen_reset_tabs(s);
1.49 nicm 245: } else
246: reflow = 0;
1.3 nicm 247:
1.1 nicm 248: if (sy != screen_size_y(s))
1.67 ! nicm 249: screen_resize_y(s, sy, eat_empty, &cy);
1.24 nicm 250:
251: if (reflow)
1.67 ! nicm 252: screen_reflow(s, sx, &cx, &cy, cursor);
1.62 nicm 253:
1.67 ! nicm 254: if (cy >= s->grid->hsize) {
! 255: s->cx = cx;
! 256: s->cy = cy - s->grid->hsize;
1.62 nicm 257: } else {
258: s->cx = 0;
259: s->cy = 0;
260: }
1.67 ! nicm 261:
1.63 nicm 262: log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx,
1.67 ! nicm 263: s->cy, cx, cy);
1.65 nicm 264:
265: if (s->write_list != NULL)
266: screen_write_make_list(s);
1.62 nicm 267: }
268:
269: /* Resize screen. */
270: void
271: screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
272: {
1.67 ! nicm 273: screen_resize_cursor(s, sx, sy, reflow, 1, 1);
1.1 nicm 274: }
275:
1.41 nicm 276: static void
1.66 nicm 277: screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy)
1.1 nicm 278: {
279: struct grid *gd = s->grid;
1.4 nicm 280: u_int needed, available, oldy, i;
1.1 nicm 281:
282: if (sy == 0)
283: fatalx("zero size");
1.4 nicm 284: oldy = screen_size_y(s);
285:
1.12 nicm 286: /*
1.4 nicm 287: * When resizing:
288: *
289: * If the height is decreasing, delete lines from the bottom until
290: * hitting the cursor, then push lines from the top into the history.
1.12 nicm 291: *
1.40 nicm 292: * When increasing, pull as many lines as possible from scrolled
293: * history (not explicitly cleared from view) to the top, then fill the
294: * remaining with blanks at the bottom.
1.4 nicm 295: */
1.1 nicm 296:
297: /* Size decreasing. */
1.4 nicm 298: if (sy < oldy) {
299: needed = oldy - sy;
1.1 nicm 300:
1.4 nicm 301: /* Delete as many lines as possible from the bottom. */
1.62 nicm 302: if (eat_empty) {
303: available = oldy - 1 - s->cy;
304: if (available > 0) {
305: if (available > needed)
306: available = needed;
307: grid_view_delete_lines(gd, oldy - available,
308: available, 8);
309: }
310: needed -= available;
1.1 nicm 311: }
312:
1.4 nicm 313: /*
1.8 nicm 314: * Now just increase the history size, if possible, to take
315: * over the lines which are left. If history is off, delete
316: * lines from the top.
1.4 nicm 317: */
1.8 nicm 318: available = s->cy;
1.40 nicm 319: if (gd->flags & GRID_HISTORY) {
320: gd->hscrolled += needed;
1.8 nicm 321: gd->hsize += needed;
1.40 nicm 322: } else if (needed > 0 && available > 0) {
1.8 nicm 323: if (available > needed)
324: available = needed;
1.44 nicm 325: grid_view_delete_lines(gd, 0, available, 8);
1.66 nicm 326: (*cy) -= available;
1.8 nicm 327: }
1.12 nicm 328: }
1.1 nicm 329:
1.51 nicm 330: /* Resize line array. */
331: grid_adjust_lines(gd, gd->hsize + sy);
1.1 nicm 332:
333: /* Size increasing. */
1.4 nicm 334: if (sy > oldy) {
335: needed = sy - oldy;
336:
1.8 nicm 337: /*
1.41 nicm 338: * Try to pull as much as possible out of scrolled history, if
339: * is is enabled.
1.8 nicm 340: */
1.40 nicm 341: available = gd->hscrolled;
1.8 nicm 342: if (gd->flags & GRID_HISTORY && available > 0) {
1.4 nicm 343: if (available > needed)
344: available = needed;
1.40 nicm 345: gd->hscrolled -= available;
1.4 nicm 346: gd->hsize -= available;
1.8 nicm 347: } else
348: available = 0;
1.4 nicm 349: needed -= available;
350:
351: /* Then fill the rest in with blanks. */
1.11 nicm 352: for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
1.61 nicm 353: grid_empty_line(gd, i, 8);
1.1 nicm 354: }
355:
1.4 nicm 356: /* Set the new size, and reset the scroll region. */
1.1 nicm 357: gd->sy = sy;
358: s->rupper = 0;
359: s->rlower = screen_size_y(s) - 1;
360: }
361:
362: /* Set selection. */
363: void
1.14 nicm 364: screen_set_selection(struct screen *s, u_int sx, u_int sy,
1.52 nicm 365: u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
1.1 nicm 366: {
1.52 nicm 367: if (s->sel == NULL)
368: s->sel = xcalloc(1, sizeof *s->sel);
1.1 nicm 369:
1.52 nicm 370: memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
371: s->sel->hidden = 0;
372: s->sel->rectangle = rectangle;
373: s->sel->modekeys = modekeys;
374:
375: s->sel->sx = sx;
376: s->sel->sy = sy;
377: s->sel->ex = ex;
378: s->sel->ey = ey;
1.1 nicm 379: }
380:
381: /* Clear selection. */
382: void
383: screen_clear_selection(struct screen *s)
384: {
1.52 nicm 385: free(s->sel);
386: s->sel = NULL;
1.1 nicm 387: }
388:
1.45 nicm 389: /* Hide selection. */
390: void
391: screen_hide_selection(struct screen *s)
392: {
1.52 nicm 393: if (s->sel != NULL)
394: s->sel->hidden = 1;
1.45 nicm 395: }
396:
1.1 nicm 397: /* Check if cell in selection. */
398: int
399: screen_check_selection(struct screen *s, u_int px, u_int py)
400: {
1.52 nicm 401: struct screen_sel *sel = s->sel;
1.28 nicm 402: u_int xx;
1.1 nicm 403:
1.52 nicm 404: if (sel == NULL || sel->hidden)
1.1 nicm 405: return (0);
406:
1.52 nicm 407: if (sel->rectangle) {
1.14 nicm 408: if (sel->sy < sel->ey) {
409: /* start line < end line -- downward selection. */
410: if (py < sel->sy || py > sel->ey)
411: return (0);
412: } else if (sel->sy > sel->ey) {
413: /* start line > end line -- upward selection. */
414: if (py > sel->sy || py < sel->ey)
415: return (0);
416: } else {
417: /* starting line == ending line. */
418: if (py != sel->sy)
419: return (0);
420: }
421:
422: /*
423: * Need to include the selection start row, but not the cursor
424: * row, which means the selection changes depending on which
425: * one is on the left.
426: */
427: if (sel->ex < sel->sx) {
428: /* Cursor (ex) is on the left. */
1.17 nicm 429: if (px < sel->ex)
1.14 nicm 430: return (0);
431:
432: if (px > sel->sx)
433: return (0);
434: } else {
435: /* Selection start (sx) is on the left. */
436: if (px < sel->sx)
437: return (0);
438:
1.17 nicm 439: if (px > sel->ex)
1.14 nicm 440: return (0);
441: }
442: } else {
443: /*
444: * Like emacs, keep the top-left-most character, and drop the
445: * bottom-right-most, regardless of copy direction.
446: */
447: if (sel->sy < sel->ey) {
448: /* starting line < ending line -- downward selection. */
449: if (py < sel->sy || py > sel->ey)
450: return (0);
451:
1.28 nicm 452: if (py == sel->sy && px < sel->sx)
1.29 nicm 453: return (0);
1.28 nicm 454:
1.53 nicm 455: if (sel->modekeys == MODEKEY_EMACS)
456: xx = (sel->ex == 0 ? 0 : sel->ex - 1);
457: else
458: xx = sel->ex;
459: if (py == sel->ey && px > xx)
1.14 nicm 460: return (0);
461: } else if (sel->sy > sel->ey) {
462: /* starting line > ending line -- upward selection. */
463: if (py > sel->sy || py < sel->ey)
464: return (0);
465:
1.28 nicm 466: if (py == sel->ey && px < sel->ex)
467: return (0);
468:
469: if (sel->modekeys == MODEKEY_EMACS)
470: xx = sel->sx - 1;
471: else
472: xx = sel->sx;
1.46 nicm 473: if (py == sel->sy && (sel->sx == 0 || px > xx))
1.14 nicm 474: return (0);
475: } else {
476: /* starting line == ending line. */
477: if (py != sel->sy)
478: return (0);
479:
480: if (sel->ex < sel->sx) {
481: /* cursor (ex) is on the left */
1.28 nicm 482: if (sel->modekeys == MODEKEY_EMACS)
483: xx = sel->sx - 1;
484: else
485: xx = sel->sx;
486: if (px > xx || px < sel->ex)
1.14 nicm 487: return (0);
488: } else {
489: /* selection start (sx) is on the left */
1.53 nicm 490: if (sel->modekeys == MODEKEY_EMACS)
491: xx = (sel->ex == 0 ? 0 : sel->ex - 1);
492: else
493: xx = sel->ex;
494: if (px < sel->sx || px > xx)
1.14 nicm 495: return (0);
496: }
497: }
1.1 nicm 498: }
499:
500: return (1);
1.43 nicm 501: }
502:
503: /* Get selected grid cell. */
504: void
505: screen_select_cell(struct screen *s, struct grid_cell *dst,
506: const struct grid_cell *src)
507: {
1.52 nicm 508: if (s->sel == NULL || s->sel->hidden)
1.43 nicm 509: return;
510:
1.52 nicm 511: memcpy(dst, &s->sel->cell, sizeof *dst);
1.43 nicm 512:
513: utf8_copy(&dst->data, &src->data);
514: dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
515: dst->attr |= src->attr & GRID_ATTR_CHARSET;
516: dst->flags = src->flags;
1.24 nicm 517: }
518:
519: /* Reflow wrapped lines. */
1.42 nicm 520: static void
1.67 ! nicm 521: screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor)
1.24 nicm 522: {
1.62 nicm 523: u_int wx, wy;
1.54 nicm 524:
1.67 ! nicm 525: if (cursor) {
! 526: grid_wrap_position(s->grid, *cx, *cy, &wx, &wy);
! 527: log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx,
! 528: wy);
! 529: }
1.54 nicm 530:
531: grid_reflow(s->grid, new_x);
532:
1.67 ! nicm 533: if (cursor) {
! 534: grid_unwrap_position(s->grid, cx, cy, wx, wy);
! 535: log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy);
! 536: }
! 537: else {
! 538: *cx = 0;
! 539: *cy = s->grid->hsize;
! 540: }
1.59 nicm 541: }
542:
543: /*
544: * Enter alternative screen mode. A copy of the visible screen is saved and the
545: * history is not updated.
546: */
547: void
548: screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor)
549: {
550: u_int sx, sy;
551:
552: if (s->saved_grid != NULL)
553: return;
554: sx = screen_size_x(s);
555: sy = screen_size_y(s);
556:
557: s->saved_grid = grid_create(sx, sy, 0);
558: grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy);
559: if (cursor) {
560: s->saved_cx = s->cx;
561: s->saved_cy = s->cy;
562: }
563: memcpy(&s->saved_cell, gc, sizeof s->saved_cell);
564:
565: grid_view_clear(s->grid, 0, 0, sx, sy, 8);
566:
1.60 nicm 567: s->saved_flags = s->grid->flags;
1.59 nicm 568: s->grid->flags &= ~GRID_HISTORY;
569: }
570:
571: /* Exit alternate screen mode and restore the copied grid. */
572: void
573: screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor)
574: {
575: u_int sx, sy;
576:
577: /*
578: * Restore the cursor position and cell. This happens even if not
579: * currently in the alternate screen.
580: */
581: if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) {
582: s->cx = s->saved_cx;
583: if (s->cx > screen_size_x(s) - 1)
584: s->cx = screen_size_x(s) - 1;
585: s->cy = s->saved_cy;
586: if (s->cy > screen_size_y(s) - 1)
587: s->cy = screen_size_y(s) - 1;
588: if (gc != NULL)
589: memcpy(gc, &s->saved_cell, sizeof *gc);
590: }
591:
592: if (s->saved_grid == NULL)
593: return;
594: sx = screen_size_x(s);
595: sy = screen_size_y(s);
596:
597: /*
598: * If the current size is bigger, temporarily resize to the old size
599: * before copying back.
600: */
601: if (sy > s->saved_grid->sy)
602: screen_resize(s, sx, s->saved_grid->sy, 1);
603:
604: /* Restore the saved grid. */
605: grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0, sy);
606:
607: /*
608: * Turn history back on (so resize can use it) and then resize back to
609: * the current size.
610: */
1.60 nicm 611: if (s->saved_flags & GRID_HISTORY)
612: s->grid->flags |= GRID_HISTORY;
1.59 nicm 613: if (sy > s->saved_grid->sy || sx != s->saved_grid->sx)
614: screen_resize(s, sx, sy, 1);
615:
616: grid_destroy(s->saved_grid);
617: s->saved_grid = NULL;
1.1 nicm 618: }