xref: /openbsd-src/usr.bin/tmux/screen.c (revision 4b70baf6e17fc8b27fc1f7fa7929335753fa94c3)
1 /* $OpenBSD: screen.c,v 1.55 2019/04/02 08:45:32 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 #include <vis.h>
25 
26 #include "tmux.h"
27 
28 /* Selected area in screen. */
29 struct screen_sel {
30 	int		 hidden;
31 	int		 rectangle;
32 	int		 modekeys;
33 
34 	u_int		 sx;
35 	u_int		 sy;
36 
37 	u_int		 ex;
38 	u_int		 ey;
39 
40 	struct grid_cell cell;
41 };
42 
43 /* Entry on title stack. */
44 struct screen_title_entry {
45 	char				*text;
46 
47 	TAILQ_ENTRY(screen_title_entry)	 entry;
48 };
49 TAILQ_HEAD(screen_titles, screen_title_entry);
50 
51 static void	screen_resize_y(struct screen *, u_int);
52 
53 static void	screen_reflow(struct screen *, u_int);
54 
55 /* Free titles stack. */
56 static void
57 screen_free_titles(struct screen *s)
58 {
59 	struct screen_title_entry	*title_entry;
60 
61 	if (s->titles == NULL)
62 		return;
63 
64 	while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) {
65 		TAILQ_REMOVE(s->titles, title_entry, entry);
66 		free(title_entry->text);
67 		free(title_entry);
68 	}
69 
70 	free(s->titles);
71 	s->titles = NULL;
72 }
73 
74 /* Create a new screen. */
75 void
76 screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
77 {
78 	s->grid = grid_create(sx, sy, hlimit);
79 	s->title = xstrdup("");
80 	s->titles = NULL;
81 
82 	s->cstyle = 0;
83 	s->ccolour = xstrdup("");
84 	s->tabs = NULL;
85 	s->sel = NULL;
86 
87 	screen_reinit(s);
88 }
89 
90 /* Reinitialise screen. */
91 void
92 screen_reinit(struct screen *s)
93 {
94 	s->cx = 0;
95 	s->cy = 0;
96 
97 	s->rupper = 0;
98 	s->rlower = screen_size_y(s) - 1;
99 
100 	s->mode = MODE_CURSOR | MODE_WRAP;
101 
102 	screen_reset_tabs(s);
103 
104 	grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
105 
106 	screen_clear_selection(s);
107 	screen_free_titles(s);
108 }
109 
110 /* Destroy a screen. */
111 void
112 screen_free(struct screen *s)
113 {
114 	free(s->sel);
115 	free(s->tabs);
116 	free(s->title);
117 	free(s->ccolour);
118 
119 	grid_destroy(s->grid);
120 
121 	screen_free_titles(s);
122 }
123 
124 /* Reset tabs to default, eight spaces apart. */
125 void
126 screen_reset_tabs(struct screen *s)
127 {
128 	u_int	i;
129 
130 	free(s->tabs);
131 
132 	if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
133 		fatal("bit_alloc failed");
134 	for (i = 8; i < screen_size_x(s); i += 8)
135 		bit_set(s->tabs, i);
136 }
137 
138 /* Set screen cursor style. */
139 void
140 screen_set_cursor_style(struct screen *s, u_int style)
141 {
142 	if (style <= 6)
143 		s->cstyle = style;
144 }
145 
146 /* Set screen cursor colour. */
147 void
148 screen_set_cursor_colour(struct screen *s, const char *colour)
149 {
150 	free(s->ccolour);
151 	s->ccolour = xstrdup(colour);
152 }
153 
154 /* Set screen title. */
155 void
156 screen_set_title(struct screen *s, const char *title)
157 {
158 	free(s->title);
159 	utf8_stravis(&s->title, title, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
160 }
161 
162 /* Push the current title onto the stack. */
163 void
164 screen_push_title(struct screen *s)
165 {
166 	struct screen_title_entry *title_entry;
167 
168 	if (s->titles == NULL) {
169 		s->titles = xmalloc(sizeof *s->titles);
170 		TAILQ_INIT(s->titles);
171 	}
172 	title_entry = xmalloc(sizeof *title_entry);
173 	title_entry->text = xstrdup(s->title);
174 	TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
175 }
176 
177 /*
178  * Pop a title from the stack and set it as the screen title. If the stack is
179  * empty, do nothing.
180  */
181 void
182 screen_pop_title(struct screen *s)
183 {
184 	struct screen_title_entry *title_entry;
185 
186 	if (s->titles == NULL)
187 		return;
188 
189 	title_entry = TAILQ_FIRST(s->titles);
190 	if (title_entry != NULL) {
191 		screen_set_title(s, title_entry->text);
192 
193 		TAILQ_REMOVE(s->titles, title_entry, entry);
194 		free(title_entry->text);
195 		free(title_entry);
196 	}
197 }
198 
199 /* Resize screen. */
200 void
201 screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
202 {
203 	if (sx < 1)
204 		sx = 1;
205 	if (sy < 1)
206 		sy = 1;
207 
208 	if (sx != screen_size_x(s)) {
209 		s->grid->sx = sx;
210 		screen_reset_tabs(s);
211 	} else
212 		reflow = 0;
213 
214 	if (sy != screen_size_y(s))
215 		screen_resize_y(s, sy);
216 
217 	if (reflow)
218 		screen_reflow(s, sx);
219 }
220 
221 static void
222 screen_resize_y(struct screen *s, u_int sy)
223 {
224 	struct grid	*gd = s->grid;
225 	u_int		 needed, available, oldy, i;
226 
227 	if (sy == 0)
228 		fatalx("zero size");
229 	oldy = screen_size_y(s);
230 
231 	/*
232 	 * When resizing:
233 	 *
234 	 * If the height is decreasing, delete lines from the bottom until
235 	 * hitting the cursor, then push lines from the top into the history.
236 	 *
237 	 * When increasing, pull as many lines as possible from scrolled
238 	 * history (not explicitly cleared from view) to the top, then fill the
239 	 * remaining with blanks at the bottom.
240 	 */
241 
242 	/* Size decreasing. */
243 	if (sy < oldy) {
244 		needed = oldy - sy;
245 
246 		/* Delete as many lines as possible from the bottom. */
247 		available = oldy - 1 - s->cy;
248 		if (available > 0) {
249 			if (available > needed)
250 				available = needed;
251 			grid_view_delete_lines(gd, oldy - available, available,
252 			    8);
253 		}
254 		needed -= available;
255 
256 		/*
257 		 * Now just increase the history size, if possible, to take
258 		 * over the lines which are left. If history is off, delete
259 		 * lines from the top.
260 		 */
261 		available = s->cy;
262 		if (gd->flags & GRID_HISTORY) {
263 			gd->hscrolled += needed;
264 			gd->hsize += needed;
265 		} else if (needed > 0 && available > 0) {
266 			if (available > needed)
267 				available = needed;
268 			grid_view_delete_lines(gd, 0, available, 8);
269 		}
270 		s->cy -= needed;
271 	}
272 
273 	/* Resize line array. */
274 	grid_adjust_lines(gd, gd->hsize + sy);
275 
276 	/* Size increasing. */
277 	if (sy > oldy) {
278 		needed = sy - oldy;
279 
280 		/*
281 		 * Try to pull as much as possible out of scrolled history, if
282 		 * is is enabled.
283 		 */
284 		available = gd->hscrolled;
285 		if (gd->flags & GRID_HISTORY && available > 0) {
286 			if (available > needed)
287 				available = needed;
288 			gd->hscrolled -= available;
289 			gd->hsize -= available;
290 			s->cy += available;
291 		} else
292 			available = 0;
293 		needed -= available;
294 
295 		/* Then fill the rest in with blanks. */
296 		for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
297 			memset(grid_get_line(gd, i), 0, sizeof(struct grid_line));
298 	}
299 
300 	/* Set the new size, and reset the scroll region. */
301 	gd->sy = sy;
302 	s->rupper = 0;
303 	s->rlower = screen_size_y(s) - 1;
304 }
305 
306 /* Set selection. */
307 void
308 screen_set_selection(struct screen *s, u_int sx, u_int sy,
309     u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
310 {
311 	if (s->sel == NULL)
312 		s->sel = xcalloc(1, sizeof *s->sel);
313 
314 	memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
315 	s->sel->hidden = 0;
316 	s->sel->rectangle = rectangle;
317 	s->sel->modekeys = modekeys;
318 
319 	s->sel->sx = sx;
320 	s->sel->sy = sy;
321 	s->sel->ex = ex;
322 	s->sel->ey = ey;
323 }
324 
325 /* Clear selection. */
326 void
327 screen_clear_selection(struct screen *s)
328 {
329 	free(s->sel);
330 	s->sel = NULL;
331 }
332 
333 /* Hide selection. */
334 void
335 screen_hide_selection(struct screen *s)
336 {
337 	if (s->sel != NULL)
338 		s->sel->hidden = 1;
339 }
340 
341 /* Check if cell in selection. */
342 int
343 screen_check_selection(struct screen *s, u_int px, u_int py)
344 {
345 	struct screen_sel	*sel = s->sel;
346 	u_int			 xx;
347 
348 	if (sel == NULL || sel->hidden)
349 		return (0);
350 
351 	if (sel->rectangle) {
352 		if (sel->sy < sel->ey) {
353 			/* start line < end line -- downward selection. */
354 			if (py < sel->sy || py > sel->ey)
355 				return (0);
356 		} else if (sel->sy > sel->ey) {
357 			/* start line > end line -- upward selection. */
358 			if (py > sel->sy || py < sel->ey)
359 				return (0);
360 		} else {
361 			/* starting line == ending line. */
362 			if (py != sel->sy)
363 				return (0);
364 		}
365 
366 		/*
367 		 * Need to include the selection start row, but not the cursor
368 		 * row, which means the selection changes depending on which
369 		 * one is on the left.
370 		 */
371 		if (sel->ex < sel->sx) {
372 			/* Cursor (ex) is on the left. */
373 			if (px < sel->ex)
374 				return (0);
375 
376 			if (px > sel->sx)
377 				return (0);
378 		} else {
379 			/* Selection start (sx) is on the left. */
380 			if (px < sel->sx)
381 				return (0);
382 
383 			if (px > sel->ex)
384 				return (0);
385 		}
386 	} else {
387 		/*
388 		 * Like emacs, keep the top-left-most character, and drop the
389 		 * bottom-right-most, regardless of copy direction.
390 		 */
391 		if (sel->sy < sel->ey) {
392 			/* starting line < ending line -- downward selection. */
393 			if (py < sel->sy || py > sel->ey)
394 				return (0);
395 
396 			if (py == sel->sy && px < sel->sx)
397 				return (0);
398 
399 			if (sel->modekeys == MODEKEY_EMACS)
400 				xx = (sel->ex == 0 ? 0 : sel->ex - 1);
401 			else
402 				xx = sel->ex;
403 			if (py == sel->ey && px > xx)
404 				return (0);
405 		} else if (sel->sy > sel->ey) {
406 			/* starting line > ending line -- upward selection. */
407 			if (py > sel->sy || py < sel->ey)
408 				return (0);
409 
410 			if (py == sel->ey && px < sel->ex)
411 				return (0);
412 
413 			if (sel->modekeys == MODEKEY_EMACS)
414 				xx = sel->sx - 1;
415 			else
416 				xx = sel->sx;
417 			if (py == sel->sy && (sel->sx == 0 || px > xx))
418 				return (0);
419 		} else {
420 			/* starting line == ending line. */
421 			if (py != sel->sy)
422 				return (0);
423 
424 			if (sel->ex < sel->sx) {
425 				/* cursor (ex) is on the left */
426 				if (sel->modekeys == MODEKEY_EMACS)
427 					xx = sel->sx - 1;
428 				else
429 					xx = sel->sx;
430 				if (px > xx || px < sel->ex)
431 					return (0);
432 			} else {
433 				/* selection start (sx) is on the left */
434 				if (sel->modekeys == MODEKEY_EMACS)
435 					xx = (sel->ex == 0 ? 0 : sel->ex - 1);
436 				else
437 					xx = sel->ex;
438 				if (px < sel->sx || px > xx)
439 					return (0);
440 			}
441 		}
442 	}
443 
444 	return (1);
445 }
446 
447 /* Get selected grid cell. */
448 void
449 screen_select_cell(struct screen *s, struct grid_cell *dst,
450     const struct grid_cell *src)
451 {
452 	if (s->sel == NULL || s->sel->hidden)
453 		return;
454 
455 	memcpy(dst, &s->sel->cell, sizeof *dst);
456 
457 	utf8_copy(&dst->data, &src->data);
458 	dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
459 	dst->attr |= src->attr & GRID_ATTR_CHARSET;
460 	dst->flags = src->flags;
461 }
462 
463 /* Reflow wrapped lines. */
464 static void
465 screen_reflow(struct screen *s, u_int new_x)
466 {
467 	u_int		cx = s->cx, cy = s->grid->hsize + s->cy, wx, wy;
468 	struct timeval	start, tv;
469 
470 	gettimeofday(&start, NULL);
471 
472 	grid_wrap_position(s->grid, cx, cy, &wx, &wy);
473 	log_debug("%s: cursor %u,%u is %u,%u", __func__, cx, cy, wx, wy);
474 
475 	grid_reflow(s->grid, new_x);
476 
477 	grid_unwrap_position(s->grid, &cx, &cy, wx, wy);
478 	log_debug("%s: new cursor is %u,%u", __func__, cx, cy);
479 
480 	if (cy >= s->grid->hsize) {
481 		s->cx = cx;
482 		s->cy = cy - s->grid->hsize;
483 	} else {
484 		s->cx = 0;
485 		s->cy = 0;
486 	}
487 
488 	gettimeofday(&tv, NULL);
489 	timersub(&tv, &start, &tv);
490 
491 	log_debug("%s: reflow took %llu.%06u seconds", __func__,
492 	    (unsigned long long)tv.tv_sec, (u_int)tv.tv_usec);
493 }
494