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