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