xref: /openbsd-src/usr.bin/tmux/screen.c (revision 9b9d2a55a62c8e82206c25f94fcc7f4e2765250e)
1 /* $OpenBSD: screen.c,v 1.34 2015/08/28 17:11:12 nicm Exp $ */
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 
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
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);
35 	s->title = xstrdup("");
36 
37 	s->cstyle = 0;
38 	s->ccolour = xstrdup("");
39 	s->tabs = NULL;
40 
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 
54 	s->mode = MODE_CURSOR | MODE_WRAP;
55 
56 	screen_reset_tabs(s);
57 
58 	grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
59 
60 	screen_clear_selection(s);
61 }
62 
63 /* Destroy a screen. */
64 void
65 screen_free(struct screen *s)
66 {
67 	free(s->tabs);
68 	free(s->title);
69 	free(s->ccolour);
70 	grid_destroy(s->grid);
71 }
72 
73 /* Reset tabs to default, eight spaces apart. */
74 void
75 screen_reset_tabs(struct screen *s)
76 {
77 	u_int	i;
78 
79 	free(s->tabs);
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);
85 }
86 
87 /* Set screen cursor style. */
88 void
89 screen_set_cursor_style(struct screen *s, u_int style)
90 {
91 	if (style <= 6)
92 		s->cstyle = style;
93 }
94 
95 /* Set screen cursor colour. */
96 void
97 screen_set_cursor_colour(struct screen *s, const char *colour_string)
98 {
99 	free(s->ccolour);
100 	s->ccolour = xstrdup(colour_string);
101 }
102 
103 /* Set screen title. */
104 void
105 screen_set_title(struct screen *s, const char *title)
106 {
107 	free(s->title);
108 	s->title = xstrdup(title);
109 }
110 
111 /* Resize screen. */
112 void
113 screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
114 {
115 	if (sx < 1)
116 		sx = 1;
117 	if (sy < 1)
118 		sy = 1;
119 
120 	if (sx != screen_size_x(s)) {
121 		screen_resize_x(s, sx);
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 
131 	if (sy != screen_size_y(s))
132 		screen_resize_y(s, sy);
133 
134 	if (reflow)
135 		screen_reflow(s, sx);
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 
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 	 */
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;
164 	u_int		 needed, available, oldy, i;
165 
166 	if (sy == 0)
167 		fatalx("zero size");
168 	oldy = screen_size_y(s);
169 
170 	/*
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.
175 	 *
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 	 */
179 
180 	/* Size decreasing. */
181 	if (sy < oldy) {
182 		needed = oldy - sy;
183 
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);
190 		}
191 		needed -= available;
192 
193 		/*
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.
197 		 *
198 		 * XXX Should apply history limit?
199 		 */
200 		available = s->cy;
201 		if (gd->flags & GRID_HISTORY)
202 			gd->hsize += needed;
203 		else if (needed > 0 && available > 0) {
204 			if (available > needed)
205 				available = needed;
206 			grid_view_delete_lines(gd, 0, available);
207 		}
208 		s->cy -= needed;
209 	}
210 
211 	/* Resize line arrays. */
212 	gd->linedata = xreallocarray(gd->linedata, gd->hsize + sy,
213 	    sizeof *gd->linedata);
214 
215 	/* Size increasing. */
216 	if (sy > oldy) {
217 		needed = sy - oldy;
218 
219 		/*
220 		 * Try to pull as much as possible out of the history, if is
221 		 * is enabled.
222 		 */
223 		available = gd->hsize;
224 		if (gd->flags & GRID_HISTORY && available > 0) {
225 			if (available > needed)
226 				available = needed;
227 			gd->hsize -= available;
228 			s->cy += available;
229 		} else
230 			available = 0;
231 		needed -= available;
232 
233 		/* Then fill the rest in with blanks. */
234 		for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
235 			memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
236 	}
237 
238 	/* Set the new size, and reset the scroll region. */
239 	gd->sy = sy;
240 	s->rupper = 0;
241 	s->rlower = screen_size_y(s) - 1;
242 }
243 
244 /* Set selection. */
245 void
246 screen_set_selection(struct screen *s, u_int sx, u_int sy,
247     u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
248 {
249 	struct screen_sel	*sel = &s->sel;
250 
251 	memcpy(&sel->cell, gc, sizeof sel->cell);
252 	sel->flag = 1;
253 	sel->rectflag = rectflag;
254 
255 	sel->sx = sx; sel->sy = sy;
256 	sel->ex = ex; sel->ey = ey;
257 }
258 
259 /* Clear selection. */
260 void
261 screen_clear_selection(struct screen *s)
262 {
263 	struct screen_sel	*sel = &s->sel;
264 
265 	sel->flag = 0;
266 	sel->lineflag = LINE_SEL_NONE;
267 }
268 
269 /* Check if cell in selection. */
270 int
271 screen_check_selection(struct screen *s, u_int px, u_int py)
272 {
273 	struct screen_sel	*sel = &s->sel;
274 	u_int			 xx;
275 
276 	if (!sel->flag)
277 		return (0);
278 
279 	if (sel->rectflag) {
280 		if (sel->sy < sel->ey) {
281 			/* start line < end line -- downward selection. */
282 			if (py < sel->sy || py > sel->ey)
283 				return (0);
284 		} else if (sel->sy > sel->ey) {
285 			/* start line > end line -- upward selection. */
286 			if (py > sel->sy || py < sel->ey)
287 				return (0);
288 		} else {
289 			/* starting line == ending line. */
290 			if (py != sel->sy)
291 				return (0);
292 		}
293 
294 		/*
295 		 * Need to include the selection start row, but not the cursor
296 		 * row, which means the selection changes depending on which
297 		 * one is on the left.
298 		 */
299 		if (sel->ex < sel->sx) {
300 			/* Cursor (ex) is on the left. */
301 			if (px < sel->ex)
302 				return (0);
303 
304 			if (px > sel->sx)
305 				return (0);
306 		} else {
307 			/* Selection start (sx) is on the left. */
308 			if (px < sel->sx)
309 				return (0);
310 
311 			if (px > sel->ex)
312 				return (0);
313 		}
314 	} else {
315 		/*
316 		 * Like emacs, keep the top-left-most character, and drop the
317 		 * bottom-right-most, regardless of copy direction.
318 		 */
319 		if (sel->sy < sel->ey) {
320 			/* starting line < ending line -- downward selection. */
321 			if (py < sel->sy || py > sel->ey)
322 				return (0);
323 
324 			if (py == sel->sy && px < sel->sx)
325 				return (0);
326 
327 			if (py == sel->ey && px > sel->ex)
328 				return (0);
329 		} else if (sel->sy > sel->ey) {
330 			/* starting line > ending line -- upward selection. */
331 			if (py > sel->sy || py < sel->ey)
332 				return (0);
333 
334 			if (py == sel->ey && px < sel->ex)
335 				return (0);
336 
337 			if (sel->modekeys == MODEKEY_EMACS)
338 				xx = sel->sx - 1;
339 			else
340 				xx = sel->sx;
341 			if (py == sel->sy && px > xx)
342 				return (0);
343 		} else {
344 			/* starting line == ending line. */
345 			if (py != sel->sy)
346 				return (0);
347 
348 			if (sel->ex < sel->sx) {
349 				/* cursor (ex) is on the left */
350 				if (sel->modekeys == MODEKEY_EMACS)
351 					xx = sel->sx - 1;
352 				else
353 					xx = sel->sx;
354 				if (px > xx || px < sel->ex)
355 					return (0);
356 			} else {
357 				/* selection start (sx) is on the left */
358 				if (px < sel->sx || px > sel->ex)
359 					return (0);
360 			}
361 		}
362 	}
363 
364 	return (1);
365 }
366 
367 /* Reflow wrapped lines. */
368 void
369 screen_reflow(struct screen *s, u_int new_x)
370 {
371 	struct grid	*old = s->grid;
372 	u_int		 change;
373 
374 	s->grid = grid_create(old->sx, old->sy, old->hlimit);
375 
376 	change = grid_reflow(s->grid, old, new_x);
377 	if (change < s->cy)
378 		s->cy -= change;
379 	else
380 		s->cy = 0;
381 }
382