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