Missing Pp, from Tiago Cunha.
[tmux-openbsd.git] / screen-redraw.c
blob83eafa623a3f88de5ad0a96b6cca1283a3ea2a2a
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <string.h>
23 #include "tmux.h"
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 void screen_redraw_draw_number(struct client *, struct window_pane *);
30 #define CELL_INSIDE 0
31 #define CELL_LEFTRIGHT 1
32 #define CELL_TOPBOTTOM 2
33 #define CELL_TOPLEFT 3
34 #define CELL_TOPRIGHT 4
35 #define CELL_BOTTOMLEFT 5
36 #define CELL_BOTTOMRIGHT 6
37 #define CELL_TOPJOIN 7
38 #define CELL_BOTTOMJOIN 8
39 #define CELL_LEFTJOIN 9
40 #define CELL_RIGHTJOIN 10
41 #define CELL_JOIN 11
42 #define CELL_OUTSIDE 12
44 /* Check if cell is on the border of a particular pane. */
45 int
46 screen_redraw_cell_border1(struct window_pane *wp, u_int px, u_int py)
48 /* Inside pane. */
49 if (px >= wp->xoff && px < wp->xoff + wp->sx &&
50 py >= wp->yoff && py < wp->yoff + wp->sy)
51 return (0);
53 /* Left/right borders. */
54 if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= wp->yoff + wp->sy) {
55 if (wp->xoff != 0 && px == wp->xoff - 1)
56 return (1);
57 if (px == wp->xoff + wp->sx)
58 return (1);
61 /* Top/bottom borders. */
62 if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= wp->xoff + wp->sx) {
63 if (wp->yoff != 0 && py == wp->yoff - 1)
64 return (1);
65 if (py == wp->yoff + wp->sy)
66 return (1);
69 /* Outside pane. */
70 return (-1);
73 /* Check if a cell is on the pane border. */
74 int
75 screen_redraw_cell_border(struct client *c, u_int px, u_int py)
77 struct window *w = c->session->curw->window;
78 struct window_pane *wp;
79 int retval;
81 /* Check all the panes. */
82 TAILQ_FOREACH(wp, &w->panes, entry) {
83 if (!window_pane_visible(wp))
84 continue;
85 if ((retval = screen_redraw_cell_border1(wp, px, py)) != -1)
86 return (retval);
89 return (0);
92 /* Check if cell inside a pane. */
93 int
94 screen_redraw_check_cell(struct client *c, u_int px, u_int py)
96 struct window *w = c->session->curw->window;
97 struct window_pane *wp;
98 int borders;
100 if (px > w->sx || py > w->sy)
101 return (CELL_OUTSIDE);
103 TAILQ_FOREACH(wp, &w->panes, entry) {
104 if (!window_pane_visible(wp))
105 continue;
107 /* If outside the pane and its border, skip it. */
108 if ((wp->xoff != 0 && px < wp->xoff - 1) ||
109 px > wp->xoff + wp->sx ||
110 (wp->yoff != 0 && py < wp->yoff - 1) ||
111 py > wp->yoff + wp->sy)
112 continue;
114 /* If definitely inside, return so. */
115 if (!screen_redraw_cell_border(c, px, py))
116 return (CELL_INSIDE);
119 * Construct a bitmask of whether the cells to the left (bit
120 * 4), right, top, and bottom (bit 1) of this cell are borders.
122 borders = 0;
123 if (px == 0 || screen_redraw_cell_border(c, px - 1, py))
124 borders |= 8;
125 if (px <= w->sx && screen_redraw_cell_border(c, px + 1, py))
126 borders |= 4;
127 if (py == 0 || screen_redraw_cell_border(c, px, py - 1))
128 borders |= 2;
129 if (py <= w->sy && screen_redraw_cell_border(c, px, py + 1))
130 borders |= 1;
133 * Figure out what kind of border this cell is. Only one bit
134 * set doesn't make sense (can't have a border cell with no
135 * others connected).
137 switch (borders) {
138 case 15: /* 1111, left right top bottom */
139 return (CELL_JOIN);
140 case 14: /* 1110, left right top */
141 return (CELL_BOTTOMJOIN);
142 case 13: /* 1101, left right bottom */
143 return (CELL_TOPJOIN);
144 case 12: /* 1100, left right */
145 return (CELL_TOPBOTTOM);
146 case 11: /* 1011, left top bottom */
147 return (CELL_RIGHTJOIN);
148 case 10: /* 1010, left top */
149 return (CELL_BOTTOMRIGHT);
150 case 9: /* 1001, left bottom */
151 return (CELL_TOPRIGHT);
152 case 7: /* 0111, right top bottom */
153 return (CELL_LEFTJOIN);
154 case 6: /* 0110, right top */
155 return (CELL_BOTTOMLEFT);
156 case 5: /* 0101, right bottom */
157 return (CELL_TOPLEFT);
158 case 3: /* 0011, top bottom */
159 return (CELL_LEFTRIGHT);
163 return (CELL_OUTSIDE);
166 /* Redraw entire screen. */
167 void
168 screen_redraw_screen(struct client *c, int status_only, int borders_only)
170 struct window *w = c->session->curw->window;
171 struct tty *tty = &c->tty;
172 struct window_pane *wp;
173 struct grid_cell active_gc, other_gc;
174 u_int i, j, type;
175 int status, fg, bg;
176 const u_char *base, *ptr;
177 u_char ch, border[20];
179 /* Get status line, er, status. */
180 if (c->message_string != NULL || c->prompt_string != NULL)
181 status = 1;
182 else
183 status = options_get_number(&c->session->options, "status");
185 /* If only drawing status and it is present, don't need the rest. */
186 if (status_only && status) {
187 tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1);
188 tty_reset(tty);
189 return;
192 /* Set up pane border attributes. */
193 memcpy(&other_gc, &grid_default_cell, sizeof other_gc);
194 memcpy(&active_gc, &grid_default_cell, sizeof active_gc);
195 active_gc.data = other_gc.data = 'x'; /* not space */
196 fg = options_get_number(&c->session->options, "pane-border-fg");
197 colour_set_fg(&other_gc, fg);
198 bg = options_get_number(&c->session->options, "pane-border-bg");
199 colour_set_bg(&other_gc, bg);
200 fg = options_get_number(&c->session->options, "pane-active-border-fg");
201 colour_set_fg(&active_gc, fg);
202 bg = options_get_number(&c->session->options, "pane-active-border-bg");
203 colour_set_bg(&active_gc, bg);
205 /* Draw background and borders. */
206 strlcpy(border, " |-....--||+.", sizeof border);
207 if (tty_term_has(tty->term, TTYC_ACSC)) {
208 base = " xqlkmjwvtun~";
209 for (ptr = base; *ptr != '\0'; ptr++) {
210 if ((ch = tty_get_acs(tty, *ptr)) != '\0')
211 border[ptr - base] = ch;
213 other_gc.attr |= GRID_ATTR_CHARSET;
214 active_gc.attr |= GRID_ATTR_CHARSET;
216 for (j = 0; j < tty->sy - status; j++) {
217 if (status_only && j != tty->sy - 1)
218 continue;
219 for (i = 0; i < tty->sx; i++) {
220 type = screen_redraw_check_cell(c, i, j);
221 if (type == CELL_INSIDE)
222 continue;
223 if (screen_redraw_cell_border1(w->active, i, j) == 1)
224 tty_attributes(tty, &active_gc);
225 else
226 tty_attributes(tty, &other_gc);
227 tty_cursor(tty, i, j);
228 tty_putc(tty, border[type]);
232 /* If only drawing borders, that's it. */
233 if (borders_only)
234 return;
236 /* Draw the panes, if necessary. */
237 TAILQ_FOREACH(wp, &w->panes, entry) {
238 if (!window_pane_visible(wp))
239 continue;
240 for (i = 0; i < wp->sy; i++) {
241 if (status_only && wp->yoff + i != tty->sy - 1)
242 continue;
243 tty_draw_line(tty, wp->screen, i, wp->xoff, wp->yoff);
245 if (c->flags & CLIENT_IDENTIFY)
246 screen_redraw_draw_number(c, wp);
249 /* Draw the status line. */
250 if (status)
251 tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1);
252 tty_reset(tty);
255 /* Draw a single pane. */
256 void
257 screen_redraw_pane(struct client *c, struct window_pane *wp)
259 u_int i;
261 for (i = 0; i < wp->sy; i++)
262 tty_draw_line(&c->tty, wp->screen, i, wp->xoff, wp->yoff);
263 tty_reset(&c->tty);
266 /* Draw number on a pane. */
267 void
268 screen_redraw_draw_number(struct client *c, struct window_pane *wp)
270 struct tty *tty = &c->tty;
271 struct session *s = c->session;
272 struct grid_cell gc;
273 u_int idx, px, py, i, j, xoff, yoff;
274 int colour;
275 char buf[16], *ptr;
276 size_t len;
278 idx = window_pane_index(wp->window, wp);
279 len = xsnprintf(buf, sizeof buf, "%u", idx);
281 if (wp->sx < len)
282 return;
283 colour = options_get_number(&s->options, "display-panes-colour");
285 px = wp->sx / 2; py = wp->sy / 2;
286 xoff = wp->xoff; yoff = wp->yoff;
288 if (wp->sx < len * 6 || wp->sy < 5) {
289 tty_cursor(tty, xoff + px - len / 2, yoff + py);
290 memcpy(&gc, &grid_default_cell, sizeof gc);
291 gc.data = '_'; /* not space */
292 colour_set_fg(&gc, colour);
293 tty_attributes(tty, &gc);
294 tty_puts(tty, buf);
295 return;
298 px -= len * 3;
299 py -= 2;
301 memcpy(&gc, &grid_default_cell, sizeof gc);
302 gc.data = '_'; /* not space */
303 colour_set_bg(&gc, colour);
304 tty_attributes(tty, &gc);
305 for (ptr = buf; *ptr != '\0'; ptr++) {
306 if (*ptr < '0' || *ptr > '9')
307 continue;
308 idx = *ptr - '0';
310 for (j = 0; j < 5; j++) {
311 for (i = px; i < px + 5; i++) {
312 tty_cursor(tty, xoff + i, yoff + py + j);
313 if (clock_table[idx][j][i - px])
314 tty_putc(tty, ' ');
317 px += 6;