Annotation of src/usr.bin/tmux/screen.c, Revision 1.49
1.49 ! nicm 1: /* $OpenBSD: screen.c,v 1.48 2017/10/05 13:29:18 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.48 nicm 28: struct screen_title_entry {
29: char *text;
30:
31: TAILQ_ENTRY(screen_title_entry) entry;
32: };
33: TAILQ_HEAD(screen_titles, screen_title_entry);
34:
1.41 nicm 35: static void screen_resize_x(struct screen *, u_int);
36: static void screen_resize_y(struct screen *, u_int);
1.1 nicm 37:
1.42 nicm 38: static void screen_reflow(struct screen *, u_int);
39:
1.48 nicm 40: /* Free titles stack. */
41: static void
42: screen_free_titles(struct screen *s)
43: {
44: struct screen_title_entry *title_entry;
45:
46: if (s->titles == NULL)
47: return;
48:
49: while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) {
50: TAILQ_REMOVE(s->titles, title_entry, entry);
51: free(title_entry->text);
52: free(title_entry);
53: }
54:
55: free(s->titles);
56: s->titles = NULL;
57: }
58:
1.1 nicm 59: /* Create a new screen. */
60: void
61: screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
62: {
63: s->grid = grid_create(sx, sy, hlimit);
1.34 nicm 64: s->title = xstrdup("");
1.48 nicm 65: s->titles = NULL;
1.1 nicm 66:
1.21 nicm 67: s->cstyle = 0;
1.20 nicm 68: s->ccolour = xstrdup("");
1.3 nicm 69: s->tabs = NULL;
70:
1.1 nicm 71: screen_reinit(s);
72: }
73:
74: /* Reinitialise screen. */
75: void
76: screen_reinit(struct screen *s)
77: {
78: s->cx = 0;
79: s->cy = 0;
80:
81: s->rupper = 0;
82: s->rlower = screen_size_y(s) - 1;
83:
1.16 nicm 84: s->mode = MODE_CURSOR | MODE_WRAP;
1.12 nicm 85:
1.3 nicm 86: screen_reset_tabs(s);
1.1 nicm 87:
1.44 nicm 88: grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
1.1 nicm 89:
90: screen_clear_selection(s);
1.48 nicm 91: screen_free_titles(s);
1.1 nicm 92: }
93:
94: /* Destroy a screen. */
95: void
96: screen_free(struct screen *s)
97: {
1.23 nicm 98: free(s->tabs);
99: free(s->title);
100: free(s->ccolour);
1.48 nicm 101:
1.1 nicm 102: grid_destroy(s->grid);
1.48 nicm 103:
104: screen_free_titles(s);
1.1 nicm 105: }
106:
1.3 nicm 107: /* Reset tabs to default, eight spaces apart. */
108: void
109: screen_reset_tabs(struct screen *s)
110: {
111: u_int i;
112:
1.23 nicm 113: free(s->tabs);
1.3 nicm 114:
115: if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
116: fatal("bit_alloc failed");
117: for (i = 8; i < screen_size_x(s); i += 8)
118: bit_set(s->tabs, i);
1.21 nicm 119: }
120:
121: /* Set screen cursor style. */
122: void
123: screen_set_cursor_style(struct screen *s, u_int style)
124: {
1.22 nicm 125: if (style <= 6)
1.21 nicm 126: s->cstyle = style;
1.20 nicm 127: }
128:
129: /* Set screen cursor colour. */
130: void
1.36 nicm 131: screen_set_cursor_colour(struct screen *s, const char *colour)
1.20 nicm 132: {
1.23 nicm 133: free(s->ccolour);
1.36 nicm 134: s->ccolour = xstrdup(colour);
1.3 nicm 135: }
136:
1.1 nicm 137: /* Set screen title. */
138: void
139: screen_set_title(struct screen *s, const char *title)
140: {
1.23 nicm 141: free(s->title);
1.47 nicm 142: utf8_stravis(&s->title, title, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.48 nicm 143: }
144:
145: /* Push the current title onto the stack. */
146: void
147: screen_push_title(struct screen *s)
148: {
149: struct screen_title_entry *title_entry;
150:
151: if (s->titles == NULL) {
152: s->titles = xmalloc(sizeof *s->titles);
153: TAILQ_INIT(s->titles);
154: }
155: title_entry = xmalloc(sizeof *title_entry);
156: title_entry->text = xstrdup(s->title);
157: TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
158: }
159:
160: /*
161: * Pop a title from the stack and set it as the screen title. If the stack is
162: * empty, do nothing.
163: */
164: void
165: screen_pop_title(struct screen *s)
166: {
167: struct screen_title_entry *title_entry;
168:
169: if (s->titles == NULL)
170: return;
171:
172: title_entry = TAILQ_FIRST(s->titles);
173: if (title_entry != NULL) {
174: screen_set_title(s, title_entry->text);
175:
176: TAILQ_REMOVE(s->titles, title_entry, entry);
177: free(title_entry->text);
178: free(title_entry);
179: }
1.1 nicm 180: }
181:
182: /* Resize screen. */
183: void
1.24 nicm 184: screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
1.1 nicm 185: {
186: if (sx < 1)
187: sx = 1;
188: if (sy < 1)
189: sy = 1;
190:
1.3 nicm 191: if (sx != screen_size_x(s)) {
1.1 nicm 192: screen_resize_x(s, sx);
1.3 nicm 193:
194: /*
195: * It is unclear what should happen to tabs on resize. xterm
196: * seems to try and maintain them, rxvt resets them. Resetting
197: * is simpler and more reliable so let's do that.
198: */
199: screen_reset_tabs(s);
1.49 ! nicm 200: } else
! 201: reflow = 0;
1.3 nicm 202:
1.1 nicm 203: if (sy != screen_size_y(s))
204: screen_resize_y(s, sy);
1.24 nicm 205:
206: if (reflow)
207: screen_reflow(s, sx);
1.1 nicm 208: }
209:
1.41 nicm 210: static void
1.1 nicm 211: screen_resize_x(struct screen *s, u_int sx)
212: {
213: struct grid *gd = s->grid;
214:
215: if (sx == 0)
216: fatalx("zero size");
217:
1.7 nicm 218: /*
219: * Treat resizing horizontally simply: just ensure the cursor is
220: * on-screen and change the size. Don't bother to truncate any lines -
1.38 nicm 221: * then the data should be accessible if the size is then increased.
1.7 nicm 222: *
223: * The only potential wrinkle is if UTF-8 double-width characters are
224: * left in the last column, but UTF-8 terminals should deal with this
225: * sanely.
226: */
1.1 nicm 227: if (s->cx >= sx)
228: s->cx = sx - 1;
229: gd->sx = sx;
230: }
231:
1.41 nicm 232: static void
1.1 nicm 233: screen_resize_y(struct screen *s, u_int sy)
234: {
235: struct grid *gd = s->grid;
1.4 nicm 236: u_int needed, available, oldy, i;
1.1 nicm 237:
238: if (sy == 0)
239: fatalx("zero size");
1.4 nicm 240: oldy = screen_size_y(s);
241:
1.12 nicm 242: /*
1.4 nicm 243: * When resizing:
244: *
245: * If the height is decreasing, delete lines from the bottom until
246: * hitting the cursor, then push lines from the top into the history.
1.12 nicm 247: *
1.40 nicm 248: * When increasing, pull as many lines as possible from scrolled
249: * history (not explicitly cleared from view) to the top, then fill the
250: * remaining with blanks at the bottom.
1.4 nicm 251: */
1.1 nicm 252:
253: /* Size decreasing. */
1.4 nicm 254: if (sy < oldy) {
255: needed = oldy - sy;
1.1 nicm 256:
1.4 nicm 257: /* Delete as many lines as possible from the bottom. */
258: available = oldy - 1 - s->cy;
259: if (available > 0) {
260: if (available > needed)
261: available = needed;
1.44 nicm 262: grid_view_delete_lines(gd, oldy - available, available,
263: 8);
1.1 nicm 264: }
1.4 nicm 265: needed -= available;
1.1 nicm 266:
1.4 nicm 267: /*
1.8 nicm 268: * Now just increase the history size, if possible, to take
269: * over the lines which are left. If history is off, delete
270: * lines from the top.
1.4 nicm 271: */
1.8 nicm 272: available = s->cy;
1.40 nicm 273: if (gd->flags & GRID_HISTORY) {
274: gd->hscrolled += needed;
1.8 nicm 275: gd->hsize += needed;
1.40 nicm 276: } else if (needed > 0 && available > 0) {
1.8 nicm 277: if (available > needed)
278: available = needed;
1.44 nicm 279: grid_view_delete_lines(gd, 0, available, 8);
1.8 nicm 280: }
1.4 nicm 281: s->cy -= needed;
1.12 nicm 282: }
1.1 nicm 283:
284: /* Resize line arrays. */
1.30 nicm 285: gd->linedata = xreallocarray(gd->linedata, gd->hsize + sy,
286: sizeof *gd->linedata);
1.1 nicm 287:
288: /* Size increasing. */
1.4 nicm 289: if (sy > oldy) {
290: needed = sy - oldy;
291:
1.8 nicm 292: /*
1.41 nicm 293: * Try to pull as much as possible out of scrolled history, if
294: * is is enabled.
1.8 nicm 295: */
1.40 nicm 296: available = gd->hscrolled;
1.8 nicm 297: if (gd->flags & GRID_HISTORY && available > 0) {
1.4 nicm 298: if (available > needed)
299: available = needed;
1.40 nicm 300: gd->hscrolled -= available;
1.4 nicm 301: gd->hsize -= available;
302: s->cy += available;
1.8 nicm 303: } else
304: available = 0;
1.4 nicm 305: needed -= available;
306:
307: /* Then fill the rest in with blanks. */
1.11 nicm 308: for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
309: memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
1.1 nicm 310: }
311:
1.4 nicm 312: /* Set the new size, and reset the scroll region. */
1.1 nicm 313: gd->sy = sy;
314: s->rupper = 0;
315: s->rlower = screen_size_y(s) - 1;
316: }
317:
318: /* Set selection. */
319: void
1.14 nicm 320: screen_set_selection(struct screen *s, u_int sx, u_int sy,
321: u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
1.1 nicm 322: {
323: struct screen_sel *sel = &s->sel;
324:
325: memcpy(&sel->cell, gc, sizeof sel->cell);
1.13 nicm 326: sel->flag = 1;
1.45 nicm 327: sel->hidden = 0;
328:
1.14 nicm 329: sel->rectflag = rectflag;
1.13 nicm 330:
1.14 nicm 331: sel->sx = sx; sel->sy = sy;
332: sel->ex = ex; sel->ey = ey;
1.1 nicm 333: }
334:
335: /* Clear selection. */
336: void
337: screen_clear_selection(struct screen *s)
338: {
339: struct screen_sel *sel = &s->sel;
340:
341: sel->flag = 0;
1.45 nicm 342: sel->hidden = 0;
1.32 nicm 343: sel->lineflag = LINE_SEL_NONE;
1.1 nicm 344: }
345:
1.45 nicm 346: /* Hide selection. */
347: void
348: screen_hide_selection(struct screen *s)
349: {
350: struct screen_sel *sel = &s->sel;
351:
352: sel->hidden = 1;
353: }
354:
1.1 nicm 355: /* Check if cell in selection. */
356: int
357: screen_check_selection(struct screen *s, u_int px, u_int py)
358: {
359: struct screen_sel *sel = &s->sel;
1.28 nicm 360: u_int xx;
1.1 nicm 361:
1.45 nicm 362: if (!sel->flag || sel->hidden)
1.1 nicm 363: return (0);
364:
1.14 nicm 365: if (sel->rectflag) {
366: if (sel->sy < sel->ey) {
367: /* start line < end line -- downward selection. */
368: if (py < sel->sy || py > sel->ey)
369: return (0);
370: } else if (sel->sy > sel->ey) {
371: /* start line > end line -- upward selection. */
372: if (py > sel->sy || py < sel->ey)
373: return (0);
374: } else {
375: /* starting line == ending line. */
376: if (py != sel->sy)
377: return (0);
378: }
379:
380: /*
381: * Need to include the selection start row, but not the cursor
382: * row, which means the selection changes depending on which
383: * one is on the left.
384: */
385: if (sel->ex < sel->sx) {
386: /* Cursor (ex) is on the left. */
1.17 nicm 387: if (px < sel->ex)
1.14 nicm 388: return (0);
389:
390: if (px > sel->sx)
391: return (0);
392: } else {
393: /* Selection start (sx) is on the left. */
394: if (px < sel->sx)
395: return (0);
396:
1.17 nicm 397: if (px > sel->ex)
1.14 nicm 398: return (0);
399: }
400: } else {
401: /*
402: * Like emacs, keep the top-left-most character, and drop the
403: * bottom-right-most, regardless of copy direction.
404: */
405: if (sel->sy < sel->ey) {
406: /* starting line < ending line -- downward selection. */
407: if (py < sel->sy || py > sel->ey)
408: return (0);
409:
1.28 nicm 410: if (py == sel->sy && px < sel->sx)
1.29 nicm 411: return (0);
1.28 nicm 412:
413: if (py == sel->ey && px > sel->ex)
1.14 nicm 414: return (0);
415: } else if (sel->sy > sel->ey) {
416: /* starting line > ending line -- upward selection. */
417: if (py > sel->sy || py < sel->ey)
418: return (0);
419:
1.28 nicm 420: if (py == sel->ey && px < sel->ex)
421: return (0);
422:
423: if (sel->modekeys == MODEKEY_EMACS)
424: xx = sel->sx - 1;
425: else
426: xx = sel->sx;
1.46 nicm 427: if (py == sel->sy && (sel->sx == 0 || px > xx))
1.14 nicm 428: return (0);
429: } else {
430: /* starting line == ending line. */
431: if (py != sel->sy)
432: return (0);
433:
434: if (sel->ex < sel->sx) {
435: /* cursor (ex) is on the left */
1.28 nicm 436: if (sel->modekeys == MODEKEY_EMACS)
437: xx = sel->sx - 1;
438: else
439: xx = sel->sx;
440: if (px > xx || px < sel->ex)
1.14 nicm 441: return (0);
442: } else {
443: /* selection start (sx) is on the left */
444: if (px < sel->sx || px > sel->ex)
445: return (0);
446: }
447: }
1.1 nicm 448: }
449:
450: return (1);
1.43 nicm 451: }
452:
453: /* Get selected grid cell. */
454: void
455: screen_select_cell(struct screen *s, struct grid_cell *dst,
456: const struct grid_cell *src)
457: {
1.45 nicm 458: if (!s->sel.flag || s->sel.hidden)
1.43 nicm 459: return;
460:
461: memcpy(dst, &s->sel.cell, sizeof *dst);
462:
463: utf8_copy(&dst->data, &src->data);
464: dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
465: dst->attr |= src->attr & GRID_ATTR_CHARSET;
466: dst->flags = src->flags;
1.24 nicm 467: }
468:
469: /* Reflow wrapped lines. */
1.42 nicm 470: static void
1.25 nicm 471: screen_reflow(struct screen *s, u_int new_x)
1.24 nicm 472: {
1.25 nicm 473: struct grid *old = s->grid;
1.26 nicm 474: u_int change;
1.24 nicm 475:
1.25 nicm 476: s->grid = grid_create(old->sx, old->sy, old->hlimit);
1.26 nicm 477:
478: change = grid_reflow(s->grid, old, new_x);
479: if (change < s->cy)
480: s->cy -= change;
481: else
482: s->cy = 0;
1.1 nicm 483: }