xref: /openbsd/usr.bin/tmux/screen.c (revision a6445c1d)
1 /* $OpenBSD: screen.c,v 1.32 2014/11/06 09:17:25 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 	char host[HOST_NAME_MAX];
35 
36 	s->grid = grid_create(sx, sy, hlimit);
37 
38 	if (gethostname(host, HOST_NAME_MAX) == 0)
39 		s->title = xstrdup(host);
40 	else
41 		s->title = xstrdup("");
42 
43 	s->cstyle = 0;
44 	s->ccolour = xstrdup("");
45 	s->tabs = NULL;
46 
47 	screen_reinit(s);
48 }
49 
50 /* Reinitialise screen. */
51 void
52 screen_reinit(struct screen *s)
53 {
54 	s->cx = 0;
55 	s->cy = 0;
56 
57 	s->rupper = 0;
58 	s->rlower = screen_size_y(s) - 1;
59 
60 	s->mode = MODE_CURSOR | MODE_WRAP;
61 
62 	screen_reset_tabs(s);
63 
64 	grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
65 
66 	screen_clear_selection(s);
67 }
68 
69 /* Destroy a screen. */
70 void
71 screen_free(struct screen *s)
72 {
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_string)
104 {
105 	free(s->ccolour);
106 	s->ccolour = xstrdup(colour_string);
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 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 incrased.
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 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 the history to
183 	 * the top, then fill the remaining with blanks at the bottom.
184 	 */
185 
186 	/* Size decreasing. */
187 	if (sy < oldy) {
188 		needed = oldy - sy;
189 
190 		/* Delete as many lines as possible from the bottom. */
191 		available = oldy - 1 - s->cy;
192 		if (available > 0) {
193 			if (available > needed)
194 				available = needed;
195 			grid_view_delete_lines(gd, oldy - available, available);
196 		}
197 		needed -= available;
198 
199 		/*
200 		 * Now just increase the history size, if possible, to take
201 		 * over the lines which are left. If history is off, delete
202 		 * lines from the top.
203 		 *
204 		 * XXX Should apply history limit?
205 		 */
206 		available = s->cy;
207 		if (gd->flags & GRID_HISTORY)
208 			gd->hsize += needed;
209 		else if (needed > 0 && available > 0) {
210 			if (available > needed)
211 				available = needed;
212 			grid_view_delete_lines(gd, 0, available);
213 		}
214 		s->cy -= needed;
215 	}
216 
217 	/* Resize line arrays. */
218 	gd->linedata = xreallocarray(gd->linedata, gd->hsize + sy,
219 	    sizeof *gd->linedata);
220 
221 	/* Size increasing. */
222 	if (sy > oldy) {
223 		needed = sy - oldy;
224 
225 		/*
226 		 * Try to pull as much as possible out of the history, if is
227 		 * is enabled.
228 		 */
229 		available = gd->hsize;
230 		if (gd->flags & GRID_HISTORY && available > 0) {
231 			if (available > needed)
232 				available = needed;
233 			gd->hsize -= available;
234 			s->cy += available;
235 		} else
236 			available = 0;
237 		needed -= available;
238 
239 		/* Then fill the rest in with blanks. */
240 		for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
241 			memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
242 	}
243 
244 	/* Set the new size, and reset the scroll region. */
245 	gd->sy = sy;
246 	s->rupper = 0;
247 	s->rlower = screen_size_y(s) - 1;
248 }
249 
250 /* Set selection. */
251 void
252 screen_set_selection(struct screen *s, u_int sx, u_int sy,
253     u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
254 {
255 	struct screen_sel	*sel = &s->sel;
256 
257 	memcpy(&sel->cell, gc, sizeof sel->cell);
258 	sel->flag = 1;
259 	sel->rectflag = rectflag;
260 
261 	sel->sx = sx; sel->sy = sy;
262 	sel->ex = ex; sel->ey = ey;
263 }
264 
265 /* Clear selection. */
266 void
267 screen_clear_selection(struct screen *s)
268 {
269 	struct screen_sel	*sel = &s->sel;
270 
271 	sel->flag = 0;
272 	sel->lineflag = LINE_SEL_NONE;
273 }
274 
275 /* Check if cell in selection. */
276 int
277 screen_check_selection(struct screen *s, u_int px, u_int py)
278 {
279 	struct screen_sel	*sel = &s->sel;
280 	u_int			 xx;
281 
282 	if (!sel->flag)
283 		return (0);
284 
285 	if (sel->rectflag) {
286 		if (sel->sy < sel->ey) {
287 			/* start line < end line -- downward selection. */
288 			if (py < sel->sy || py > sel->ey)
289 				return (0);
290 		} else if (sel->sy > sel->ey) {
291 			/* start line > end line -- upward selection. */
292 			if (py > sel->sy || py < sel->ey)
293 				return (0);
294 		} else {
295 			/* starting line == ending line. */
296 			if (py != sel->sy)
297 				return (0);
298 		}
299 
300 		/*
301 		 * Need to include the selection start row, but not the cursor
302 		 * row, which means the selection changes depending on which
303 		 * one is on the left.
304 		 */
305 		if (sel->ex < sel->sx) {
306 			/* Cursor (ex) is on the left. */
307 			if (px < sel->ex)
308 				return (0);
309 
310 			if (px > sel->sx)
311 				return (0);
312 		} else {
313 			/* Selection start (sx) is on the left. */
314 			if (px < sel->sx)
315 				return (0);
316 
317 			if (px > sel->ex)
318 				return (0);
319 		}
320 	} else {
321 		/*
322 		 * Like emacs, keep the top-left-most character, and drop the
323 		 * bottom-right-most, regardless of copy direction.
324 		 */
325 		if (sel->sy < sel->ey) {
326 			/* starting line < ending line -- downward selection. */
327 			if (py < sel->sy || py > sel->ey)
328 				return (0);
329 
330 			if (py == sel->sy && px < sel->sx)
331 				return (0);
332 
333 			if (py == sel->ey && px > sel->ex)
334 				return (0);
335 		} else if (sel->sy > sel->ey) {
336 			/* starting line > ending line -- upward selection. */
337 			if (py > sel->sy || py < sel->ey)
338 				return (0);
339 
340 			if (py == sel->ey && px < sel->ex)
341 				return (0);
342 
343 			if (sel->modekeys == MODEKEY_EMACS)
344 				xx = sel->sx - 1;
345 			else
346 				xx = sel->sx;
347 			if (py == sel->sy && px > xx)
348 				return (0);
349 		} else {
350 			/* starting line == ending line. */
351 			if (py != sel->sy)
352 				return (0);
353 
354 			if (sel->ex < sel->sx) {
355 				/* cursor (ex) is on the left */
356 				if (sel->modekeys == MODEKEY_EMACS)
357 					xx = sel->sx - 1;
358 				else
359 					xx = sel->sx;
360 				if (px > xx || px < sel->ex)
361 					return (0);
362 			} else {
363 				/* selection start (sx) is on the left */
364 				if (px < sel->sx || px > sel->ex)
365 					return (0);
366 			}
367 		}
368 	}
369 
370 	return (1);
371 }
372 
373 /* Reflow wrapped lines. */
374 void
375 screen_reflow(struct screen *s, u_int new_x)
376 {
377 	struct grid	*old = s->grid;
378 	u_int		 change;
379 
380 	s->grid = grid_create(old->sx, old->sy, old->hlimit);
381 
382 	change = grid_reflow(s->grid, old, new_x);
383 	if (change < s->cy)
384 		s->cy -= change;
385 	else
386 		s->cy = 0;
387 }
388