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