Annotation of src/usr.bin/tmux/screen.c, Revision 1.39
1.39 ! nicm 1: /* $OpenBSD: screen.c,v 1.38 2016/06/10 11:46:15 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.1 nicm 24:
25: #include "tmux.h"
26:
27: void screen_resize_x(struct screen *, u_int);
28: void screen_resize_y(struct screen *, u_int);
29:
30: /* Create a new screen. */
31: void
32: screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
33: {
34: s->grid = grid_create(sx, sy, hlimit);
1.34 nicm 35: s->title = xstrdup("");
1.1 nicm 36:
1.21 nicm 37: s->cstyle = 0;
1.20 nicm 38: s->ccolour = xstrdup("");
1.3 nicm 39: s->tabs = NULL;
40:
1.39 ! nicm 41: s->dirty = NULL;
! 42: s->dirtysize = 0;
! 43:
1.1 nicm 44: screen_reinit(s);
45: }
46:
47: /* Reinitialise screen. */
48: void
49: screen_reinit(struct screen *s)
50: {
51: s->cx = 0;
52: s->cy = 0;
53:
54: s->rupper = 0;
55: s->rlower = screen_size_y(s) - 1;
56:
1.16 nicm 57: s->mode = MODE_CURSOR | MODE_WRAP;
1.12 nicm 58:
1.3 nicm 59: screen_reset_tabs(s);
1.1 nicm 60:
1.6 nicm 61: grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
1.1 nicm 62:
63: screen_clear_selection(s);
64: }
65:
66: /* Destroy a screen. */
67: void
68: screen_free(struct screen *s)
69: {
1.39 ! nicm 70: free(s->dirty);
1.23 nicm 71: free(s->tabs);
72: free(s->title);
73: free(s->ccolour);
1.1 nicm 74: grid_destroy(s->grid);
75: }
76:
1.3 nicm 77: /* Reset tabs to default, eight spaces apart. */
78: void
79: screen_reset_tabs(struct screen *s)
80: {
81: u_int i;
82:
1.23 nicm 83: free(s->tabs);
1.3 nicm 84:
85: if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
86: fatal("bit_alloc failed");
87: for (i = 8; i < screen_size_x(s); i += 8)
88: bit_set(s->tabs, i);
1.21 nicm 89: }
90:
91: /* Set screen cursor style. */
92: void
93: screen_set_cursor_style(struct screen *s, u_int style)
94: {
1.22 nicm 95: if (style <= 6)
1.21 nicm 96: s->cstyle = style;
1.20 nicm 97: }
98:
99: /* Set screen cursor colour. */
100: void
1.36 nicm 101: screen_set_cursor_colour(struct screen *s, const char *colour)
1.20 nicm 102: {
1.23 nicm 103: free(s->ccolour);
1.36 nicm 104: s->ccolour = xstrdup(colour);
1.3 nicm 105: }
106:
1.1 nicm 107: /* Set screen title. */
108: void
109: screen_set_title(struct screen *s, const char *title)
110: {
1.23 nicm 111: free(s->title);
1.27 nicm 112: s->title = xstrdup(title);
1.1 nicm 113: }
114:
115: /* Resize screen. */
116: void
1.24 nicm 117: screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
1.1 nicm 118: {
119: if (sx < 1)
120: sx = 1;
121: if (sy < 1)
122: sy = 1;
123:
1.3 nicm 124: if (sx != screen_size_x(s)) {
1.1 nicm 125: screen_resize_x(s, sx);
1.3 nicm 126:
127: /*
128: * It is unclear what should happen to tabs on resize. xterm
129: * seems to try and maintain them, rxvt resets them. Resetting
130: * is simpler and more reliable so let's do that.
131: */
132: screen_reset_tabs(s);
133: }
134:
1.1 nicm 135: if (sy != screen_size_y(s))
136: screen_resize_y(s, sy);
1.24 nicm 137:
138: if (reflow)
139: screen_reflow(s, sx);
1.1 nicm 140: }
141:
142: void
143: screen_resize_x(struct screen *s, u_int sx)
144: {
145: struct grid *gd = s->grid;
146:
147: if (sx == 0)
148: fatalx("zero size");
149:
1.7 nicm 150: /*
151: * Treat resizing horizontally simply: just ensure the cursor is
152: * on-screen and change the size. Don't bother to truncate any lines -
1.38 nicm 153: * then the data should be accessible if the size is then increased.
1.7 nicm 154: *
155: * The only potential wrinkle is if UTF-8 double-width characters are
156: * left in the last column, but UTF-8 terminals should deal with this
157: * sanely.
158: */
1.1 nicm 159: if (s->cx >= sx)
160: s->cx = sx - 1;
161: gd->sx = sx;
162: }
163:
164: void
165: screen_resize_y(struct screen *s, u_int sy)
166: {
167: struct grid *gd = s->grid;
1.4 nicm 168: u_int needed, available, oldy, i;
1.1 nicm 169:
170: if (sy == 0)
171: fatalx("zero size");
1.4 nicm 172: oldy = screen_size_y(s);
173:
1.12 nicm 174: /*
1.4 nicm 175: * When resizing:
176: *
177: * If the height is decreasing, delete lines from the bottom until
178: * hitting the cursor, then push lines from the top into the history.
1.12 nicm 179: *
1.4 nicm 180: * When increasing, pull as many lines as possible from the history to
181: * the top, then fill the remaining with blanks at the bottom.
182: */
1.1 nicm 183:
184: /* Size decreasing. */
1.4 nicm 185: if (sy < oldy) {
186: needed = oldy - sy;
1.1 nicm 187:
1.4 nicm 188: /* Delete as many lines as possible from the bottom. */
189: available = oldy - 1 - s->cy;
190: if (available > 0) {
191: if (available > needed)
192: available = needed;
193: grid_view_delete_lines(gd, oldy - available, available);
1.1 nicm 194: }
1.4 nicm 195: needed -= available;
1.1 nicm 196:
1.4 nicm 197: /*
1.8 nicm 198: * Now just increase the history size, if possible, to take
199: * over the lines which are left. If history is off, delete
200: * lines from the top.
1.4 nicm 201: */
1.8 nicm 202: available = s->cy;
203: if (gd->flags & GRID_HISTORY)
204: gd->hsize += needed;
1.9 nicm 205: else if (needed > 0 && available > 0) {
1.8 nicm 206: if (available > needed)
207: available = needed;
208: grid_view_delete_lines(gd, 0, available);
209: }
1.4 nicm 210: s->cy -= needed;
1.12 nicm 211: }
1.1 nicm 212:
213: /* Resize line arrays. */
1.30 nicm 214: gd->linedata = xreallocarray(gd->linedata, gd->hsize + sy,
215: sizeof *gd->linedata);
1.1 nicm 216:
217: /* Size increasing. */
1.4 nicm 218: if (sy > oldy) {
219: needed = sy - oldy;
220:
1.8 nicm 221: /*
222: * Try to pull as much as possible out of the history, if is
223: * is enabled.
224: */
1.4 nicm 225: available = gd->hsize;
1.8 nicm 226: if (gd->flags & GRID_HISTORY && available > 0) {
1.4 nicm 227: if (available > needed)
228: available = needed;
229: gd->hsize -= available;
230: s->cy += available;
1.8 nicm 231: } else
232: available = 0;
1.4 nicm 233: needed -= available;
234:
235: /* Then fill the rest in with blanks. */
1.11 nicm 236: for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
237: memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
1.1 nicm 238: }
239:
1.4 nicm 240: /* Set the new size, and reset the scroll region. */
1.1 nicm 241: gd->sy = sy;
242: s->rupper = 0;
243: s->rlower = screen_size_y(s) - 1;
244: }
245:
246: /* Set selection. */
247: void
1.14 nicm 248: screen_set_selection(struct screen *s, u_int sx, u_int sy,
249: u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
1.1 nicm 250: {
251: struct screen_sel *sel = &s->sel;
252:
253: memcpy(&sel->cell, gc, sizeof sel->cell);
1.13 nicm 254: sel->flag = 1;
1.14 nicm 255: sel->rectflag = rectflag;
1.13 nicm 256:
1.14 nicm 257: sel->sx = sx; sel->sy = sy;
258: sel->ex = ex; sel->ey = ey;
1.1 nicm 259: }
260:
261: /* Clear selection. */
262: void
263: screen_clear_selection(struct screen *s)
264: {
265: struct screen_sel *sel = &s->sel;
266:
267: sel->flag = 0;
1.32 nicm 268: sel->lineflag = LINE_SEL_NONE;
1.1 nicm 269: }
270:
271: /* Check if cell in selection. */
272: int
273: screen_check_selection(struct screen *s, u_int px, u_int py)
274: {
275: struct screen_sel *sel = &s->sel;
1.28 nicm 276: u_int xx;
1.1 nicm 277:
1.14 nicm 278: if (!sel->flag)
1.1 nicm 279: return (0);
280:
1.14 nicm 281: if (sel->rectflag) {
282: if (sel->sy < sel->ey) {
283: /* start line < end line -- downward selection. */
284: if (py < sel->sy || py > sel->ey)
285: return (0);
286: } else if (sel->sy > sel->ey) {
287: /* start line > end line -- upward selection. */
288: if (py > sel->sy || py < sel->ey)
289: return (0);
290: } else {
291: /* starting line == ending line. */
292: if (py != sel->sy)
293: return (0);
294: }
295:
296: /*
297: * Need to include the selection start row, but not the cursor
298: * row, which means the selection changes depending on which
299: * one is on the left.
300: */
301: if (sel->ex < sel->sx) {
302: /* Cursor (ex) is on the left. */
1.17 nicm 303: if (px < sel->ex)
1.14 nicm 304: return (0);
305:
306: if (px > sel->sx)
307: return (0);
308: } else {
309: /* Selection start (sx) is on the left. */
310: if (px < sel->sx)
311: return (0);
312:
1.17 nicm 313: if (px > sel->ex)
1.14 nicm 314: return (0);
315: }
316: } else {
317: /*
318: * Like emacs, keep the top-left-most character, and drop the
319: * bottom-right-most, regardless of copy direction.
320: */
321: if (sel->sy < sel->ey) {
322: /* starting line < ending line -- downward selection. */
323: if (py < sel->sy || py > sel->ey)
324: return (0);
325:
1.28 nicm 326: if (py == sel->sy && px < sel->sx)
1.29 nicm 327: return (0);
1.28 nicm 328:
329: if (py == sel->ey && px > sel->ex)
1.14 nicm 330: return (0);
331: } else if (sel->sy > sel->ey) {
332: /* starting line > ending line -- upward selection. */
333: if (py > sel->sy || py < sel->ey)
334: return (0);
335:
1.28 nicm 336: if (py == sel->ey && px < sel->ex)
337: return (0);
338:
339: if (sel->modekeys == MODEKEY_EMACS)
340: xx = sel->sx - 1;
341: else
342: xx = sel->sx;
343: if (py == sel->sy && px > xx)
1.14 nicm 344: return (0);
345: } else {
346: /* starting line == ending line. */
347: if (py != sel->sy)
348: return (0);
349:
350: if (sel->ex < sel->sx) {
351: /* cursor (ex) is on the left */
1.28 nicm 352: if (sel->modekeys == MODEKEY_EMACS)
353: xx = sel->sx - 1;
354: else
355: xx = sel->sx;
356: if (px > xx || px < sel->ex)
1.14 nicm 357: return (0);
358: } else {
359: /* selection start (sx) is on the left */
360: if (px < sel->sx || px > sel->ex)
361: return (0);
362: }
363: }
1.1 nicm 364: }
365:
366: return (1);
1.24 nicm 367: }
368:
369: /* Reflow wrapped lines. */
370: void
1.25 nicm 371: screen_reflow(struct screen *s, u_int new_x)
1.24 nicm 372: {
1.25 nicm 373: struct grid *old = s->grid;
1.26 nicm 374: u_int change;
1.24 nicm 375:
1.25 nicm 376: s->grid = grid_create(old->sx, old->sy, old->hlimit);
1.26 nicm 377:
378: change = grid_reflow(s->grid, old, new_x);
379: if (change < s->cy)
380: s->cy -= change;
381: else
382: s->cy = 0;
1.1 nicm 383: }