xref: /openbsd-src/usr.bin/tmux/screen-redraw.c (revision 48950c12d106c85f315112191a0228d7b83b9510)
1 /* $OpenBSD: screen-redraw.c,v 1.24 2013/03/25 11:41:49 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 <string.h>
22 
23 #include "tmux.h"
24 
25 int	screen_redraw_cell_border1(struct window_pane *, u_int, u_int);
26 int	screen_redraw_cell_border(struct client *, u_int, u_int);
27 int	screen_redraw_check_cell(struct client *, u_int, u_int,
28 	    struct window_pane **);
29 int	screen_redraw_check_active(u_int, u_int, int, struct window *,
30 	    struct window_pane *);
31 
32 void	screen_redraw_draw_number(struct client *, struct window_pane *);
33 
34 #define CELL_INSIDE 0
35 #define CELL_LEFTRIGHT 1
36 #define CELL_TOPBOTTOM 2
37 #define CELL_TOPLEFT 3
38 #define CELL_TOPRIGHT 4
39 #define CELL_BOTTOMLEFT 5
40 #define CELL_BOTTOMRIGHT 6
41 #define CELL_TOPJOIN 7
42 #define CELL_BOTTOMJOIN 8
43 #define CELL_LEFTJOIN 9
44 #define CELL_RIGHTJOIN 10
45 #define CELL_JOIN 11
46 #define CELL_OUTSIDE 12
47 
48 #define CELL_BORDERS " xqlkmjwvtun~"
49 
50 /* Check if cell is on the border of a particular pane. */
51 int
52 screen_redraw_cell_border1(struct window_pane *wp, u_int px, u_int py)
53 {
54 	/* Inside pane. */
55 	if (px >= wp->xoff && px < wp->xoff + wp->sx &&
56 	    py >= wp->yoff && py < wp->yoff + wp->sy)
57 		return (0);
58 
59 	/* Left/right borders. */
60 	if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= wp->yoff + wp->sy) {
61 		if (wp->xoff != 0 && px == wp->xoff - 1)
62 			return (1);
63 		if (px == wp->xoff + wp->sx)
64 			return (1);
65 	}
66 
67 	/* Top/bottom borders. */
68 	if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= wp->xoff + wp->sx) {
69 		if (wp->yoff != 0 && py == wp->yoff - 1)
70 			return (1);
71 		if (py == wp->yoff + wp->sy)
72 			return (1);
73 	}
74 
75 	/* Outside pane. */
76 	return (-1);
77 }
78 
79 /* Check if a cell is on the pane border. */
80 int
81 screen_redraw_cell_border(struct client *c, u_int px, u_int py)
82 {
83 	struct window		*w = c->session->curw->window;
84 	struct window_pane	*wp;
85 	int			 retval;
86 
87 	/* Check all the panes. */
88 	TAILQ_FOREACH(wp, &w->panes, entry) {
89 		if (!window_pane_visible(wp))
90 			continue;
91 		if ((retval = screen_redraw_cell_border1(wp, px, py)) != -1)
92 			return (retval);
93 	}
94 
95 	return (0);
96 }
97 
98 /* Check if cell inside a pane. */
99 int
100 screen_redraw_check_cell(struct client *c, u_int px, u_int py,
101     struct window_pane **wpp)
102 {
103 	struct window		*w = c->session->curw->window;
104 	struct window_pane	*wp;
105 	int			 borders;
106 
107 	if (px > w->sx || py > w->sy)
108 		return (CELL_OUTSIDE);
109 
110 	TAILQ_FOREACH(wp, &w->panes, entry) {
111 		if (!window_pane_visible(wp))
112 			continue;
113 		*wpp = wp;
114 
115 		/* If outside the pane and its border, skip it. */
116 		if ((wp->xoff != 0 && px < wp->xoff - 1) ||
117 		    px > wp->xoff + wp->sx ||
118 		    (wp->yoff != 0 && py < wp->yoff - 1) ||
119 		    py > wp->yoff + wp->sy)
120 			continue;
121 
122 		/* If definitely inside, return so. */
123 		if (!screen_redraw_cell_border(c, px, py))
124 			return (CELL_INSIDE);
125 
126 		/*
127 		 * Construct a bitmask of whether the cells to the left (bit
128 		 * 4), right, top, and bottom (bit 1) of this cell are borders.
129 		 */
130 		borders = 0;
131 		if (px == 0 || screen_redraw_cell_border(c, px - 1, py))
132 			borders |= 8;
133 		if (px <= w->sx && screen_redraw_cell_border(c, px + 1, py))
134 			borders |= 4;
135 		if (py == 0 || screen_redraw_cell_border(c, px, py - 1))
136 			borders |= 2;
137 		if (py <= w->sy && screen_redraw_cell_border(c, px, py + 1))
138 			borders |= 1;
139 
140 		/*
141 		 * Figure out what kind of border this cell is. Only one bit
142 		 * set doesn't make sense (can't have a border cell with no
143 		 * others connected).
144 		 */
145 		switch (borders) {
146 		case 15:	/* 1111, left right top bottom */
147 			return (CELL_JOIN);
148 		case 14:	/* 1110, left right top */
149 			return (CELL_BOTTOMJOIN);
150 		case 13:	/* 1101, left right bottom */
151 			return (CELL_TOPJOIN);
152 		case 12:	/* 1100, left right */
153 			return (CELL_TOPBOTTOM);
154 		case 11:	/* 1011, left top bottom */
155 			return (CELL_RIGHTJOIN);
156 		case 10:	/* 1010, left top */
157 			return (CELL_BOTTOMRIGHT);
158 		case 9:		/* 1001, left bottom */
159 			return (CELL_TOPRIGHT);
160 		case 7:		/* 0111, right top bottom */
161 			return (CELL_LEFTJOIN);
162 		case 6:		/* 0110, right top */
163 			return (CELL_BOTTOMLEFT);
164 		case 5:		/* 0101, right bottom */
165 			return (CELL_TOPLEFT);
166 		case 3:		/* 0011, top bottom */
167 			return (CELL_LEFTRIGHT);
168 		}
169 	}
170 
171 	*wpp = NULL;
172 	return (CELL_OUTSIDE);
173 }
174 
175 /* Check active pane indicator. */
176 int
177 screen_redraw_check_active(u_int px, u_int py, int type, struct window *w,
178     struct window_pane *wp)
179 {
180 	/* Is this off the active pane border? */
181 	if (screen_redraw_cell_border1(w->active, px, py) != 1)
182 		return (0);
183 
184 	/* If there are more than two panes, that's enough. */
185 	if (window_count_panes(w) != 2)
186 		return (1);
187 
188 	/* Else if the cell is not a border cell, forget it. */
189 	if (wp == NULL || (type == CELL_OUTSIDE || type == CELL_INSIDE))
190 		return (1);
191 
192 	/* Check if the pane covers the whole width. */
193 	if (wp->xoff == 0 && wp->sx == w->sx) {
194 		/* This can either be the top pane or the bottom pane. */
195 		if (wp->yoff == 0) { /* top pane */
196 			if (wp == w->active)
197 				return (px <= wp->sx / 2);
198 			return (px > wp->sx / 2);
199 		}
200 		return (0);
201 	}
202 
203 	/* Check if the pane covers the whole height. */
204 	if (wp->yoff == 0 && wp->sy == w->sy) {
205 		/* This can either be the left pane or the right pane. */
206 		if (wp->xoff == 0) { /* left pane */
207 			if (wp == w->active)
208 				return (py <= wp->sy / 2);
209 			return (py > wp->sy / 2);
210 		}
211 		return (0);
212 	}
213 
214 	return (type);
215 }
216 
217 /* Redraw entire screen. */
218 void
219 screen_redraw_screen(struct client *c, int status_only, int borders_only)
220 {
221 	struct window		*w = c->session->curw->window;
222 	struct options		*oo = &c->session->options;
223 	struct tty		*tty = &c->tty;
224 	struct window_pane	*wp;
225 	struct grid_cell	 active_gc, other_gc;
226 	u_int		 	 i, j, type, top;
227 	int		 	 status, spos, fg, bg;
228 
229 	/* Suspended clients should not be updated. */
230 	if (c->flags & CLIENT_SUSPENDED)
231 		return;
232 
233 	/* Get status line, er, status. */
234 	spos = options_get_number(oo, "status-position");
235 	if (c->message_string != NULL || c->prompt_string != NULL)
236 		status = 1;
237 	else
238 		status = options_get_number(oo, "status");
239 	top = 0;
240 	if (status && spos == 0)
241 		top = 1;
242 
243 	/* If only drawing status and it is present, don't need the rest. */
244 	if (status_only && status) {
245 		if (top)
246 			tty_draw_line(tty, &c->status, 0, 0, 0);
247 		else
248 			tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1);
249 		tty_reset(tty);
250 		return;
251 	}
252 
253 	/* Set up pane border attributes. */
254 	memcpy(&other_gc, &grid_marker_cell, sizeof other_gc);
255 	memcpy(&active_gc, &grid_marker_cell, sizeof active_gc);
256 	active_gc.attr = other_gc.attr = GRID_ATTR_CHARSET;
257 	fg = options_get_number(oo, "pane-border-fg");
258 	colour_set_fg(&other_gc, fg);
259 	bg = options_get_number(oo, "pane-border-bg");
260 	colour_set_bg(&other_gc, bg);
261 	fg = options_get_number(oo, "pane-active-border-fg");
262 	colour_set_fg(&active_gc, fg);
263 	bg = options_get_number(oo, "pane-active-border-bg");
264 	colour_set_bg(&active_gc, bg);
265 
266 	/* Draw background and borders. */
267 	for (j = 0; j < tty->sy - status; j++) {
268 		if (status_only) {
269 			if (spos == 1 && j != tty->sy - 1)
270 				continue;
271 			else if (spos == 0 && j != 0)
272 				break;
273 		}
274 		for (i = 0; i < tty->sx; i++) {
275 			type = screen_redraw_check_cell(c, i, j, &wp);
276 			if (type == CELL_INSIDE)
277 				continue;
278 			if (screen_redraw_check_active(i, j, type, w, wp))
279 				tty_attributes(tty, &active_gc);
280 			else
281 				tty_attributes(tty, &other_gc);
282 			tty_cursor(tty, i, top + j);
283 			tty_putc(tty, CELL_BORDERS[type]);
284 		}
285 	}
286 
287 	/* If only drawing borders, that's it. */
288 	if (borders_only)
289 		return;
290 
291 	/* Draw the panes, if necessary. */
292 	TAILQ_FOREACH(wp, &w->panes, entry) {
293 		if (!window_pane_visible(wp))
294 			continue;
295 		for (i = 0; i < wp->sy; i++) {
296 			if (status_only) {
297 				if (spos == 1 && wp->yoff + i != tty->sy - 1)
298 					continue;
299 				else if (spos == 0 && wp->yoff + i != 0)
300 					break;
301 			}
302 			tty_draw_line(
303 			    tty, wp->screen, i, wp->xoff, top + wp->yoff);
304 		}
305 		if (c->flags & CLIENT_IDENTIFY)
306 			screen_redraw_draw_number(c, wp);
307 	}
308 
309 	/* Draw the status line. */
310 	if (status) {
311 		if (top)
312 			tty_draw_line(tty, &c->status, 0, 0, 0);
313 		else
314 			tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1);
315 	}
316 	tty_reset(tty);
317 }
318 
319 /* Draw a single pane. */
320 void
321 screen_redraw_pane(struct client *c, struct window_pane *wp)
322 {
323 	u_int	i, yoff;
324 
325 	if (!window_pane_visible(wp))
326 		return;
327 
328 	yoff = wp->yoff;
329 	if (status_at_line(c) == 0)
330 		yoff++;
331 
332 	for (i = 0; i < wp->sy; i++)
333 		tty_draw_line(&c->tty, wp->screen, i, wp->xoff, yoff);
334 	tty_reset(&c->tty);
335 }
336 
337 /* Draw number on a pane. */
338 void
339 screen_redraw_draw_number(struct client *c, struct window_pane *wp)
340 {
341 	struct tty		*tty = &c->tty;
342 	struct session		*s = c->session;
343 	struct options		*oo = &s->options;
344 	struct window		*w = wp->window;
345 	struct grid_cell	 gc;
346 	u_int			 idx, px, py, i, j, xoff, yoff;
347 	int			 colour, active_colour;
348 	char			 buf[16], *ptr;
349 	size_t			 len;
350 
351 	if (window_pane_index(wp, &idx) != 0)
352 		fatalx("index not found");
353 	len = xsnprintf(buf, sizeof buf, "%u", idx);
354 
355 	if (wp->sx < len)
356 		return;
357 	colour = options_get_number(oo, "display-panes-colour");
358 	active_colour = options_get_number(oo, "display-panes-active-colour");
359 
360 	px = wp->sx / 2; py = wp->sy / 2;
361 	xoff = wp->xoff; yoff = wp->yoff;
362 
363 	if (wp->sx < len * 6 || wp->sy < 5) {
364 		tty_cursor(tty, xoff + px - len / 2, yoff + py);
365 		goto draw_text;
366 	}
367 
368 	px -= len * 3;
369 	py -= 2;
370 
371 	memcpy(&gc, &grid_marker_cell, sizeof gc);
372 	if (w->active == wp)
373 		colour_set_bg(&gc, active_colour);
374 	else
375 		colour_set_bg(&gc, colour);
376 	tty_attributes(tty, &gc);
377 	for (ptr = buf; *ptr != '\0'; ptr++) {
378 		if (*ptr < '0' || *ptr > '9')
379 			continue;
380 		idx = *ptr - '0';
381 
382 		for (j = 0; j < 5; j++) {
383 			for (i = px; i < px + 5; i++) {
384 				tty_cursor(tty, xoff + i, yoff + py + j);
385 				if (clock_table[idx][j][i - px])
386 					tty_putc(tty, ' ');
387 			}
388 		}
389 		px += 6;
390 	}
391 
392 	len = xsnprintf(buf, sizeof buf, "%ux%u", wp->sx, wp->sy);
393 	if (wp->sx < len || wp->sy < 6)
394 		return;
395 	tty_cursor(tty, xoff + wp->sx - len, yoff);
396 
397 draw_text:
398 	memcpy(&gc, &grid_marker_cell, sizeof gc);
399 	if (w->active == wp)
400 		colour_set_fg(&gc, active_colour);
401 	else
402 		colour_set_fg(&gc, colour);
403 	tty_attributes(tty, &gc);
404 	tty_puts(tty, buf);
405 
406 	tty_cursor(tty, 0, 0);
407 }
408