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