screen-redraw.c revision 1.1 1 1.1 jmmv /* $Id: screen-redraw.c,v 1.1 2011/03/10 09:15:38 jmmv Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1 jmmv * Copyright (c) 2007 Nicholas Marriott <nicm (at) users.sourceforge.net>
5 1.1 jmmv *
6 1.1 jmmv * Permission to use, copy, modify, and distribute this software for any
7 1.1 jmmv * purpose with or without fee is hereby granted, provided that the above
8 1.1 jmmv * copyright notice and this permission notice appear in all copies.
9 1.1 jmmv *
10 1.1 jmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 jmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 jmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 jmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 jmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 1.1 jmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 1.1 jmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 jmmv */
18 1.1 jmmv
19 1.1 jmmv #include <sys/types.h>
20 1.1 jmmv
21 1.1 jmmv #include <string.h>
22 1.1 jmmv
23 1.1 jmmv #include "tmux.h"
24 1.1 jmmv
25 1.1 jmmv int screen_redraw_cell_border1(struct window_pane *, u_int, u_int);
26 1.1 jmmv int screen_redraw_cell_border(struct client *, u_int, u_int);
27 1.1 jmmv int screen_redraw_check_cell(struct client *, u_int, u_int);
28 1.1 jmmv void screen_redraw_draw_number(struct client *, struct window_pane *);
29 1.1 jmmv
30 1.1 jmmv #define CELL_INSIDE 0
31 1.1 jmmv #define CELL_LEFTRIGHT 1
32 1.1 jmmv #define CELL_TOPBOTTOM 2
33 1.1 jmmv #define CELL_TOPLEFT 3
34 1.1 jmmv #define CELL_TOPRIGHT 4
35 1.1 jmmv #define CELL_BOTTOMLEFT 5
36 1.1 jmmv #define CELL_BOTTOMRIGHT 6
37 1.1 jmmv #define CELL_TOPJOIN 7
38 1.1 jmmv #define CELL_BOTTOMJOIN 8
39 1.1 jmmv #define CELL_LEFTJOIN 9
40 1.1 jmmv #define CELL_RIGHTJOIN 10
41 1.1 jmmv #define CELL_JOIN 11
42 1.1 jmmv #define CELL_OUTSIDE 12
43 1.1 jmmv
44 1.1 jmmv #define CELL_BORDERS " xqlkmjwvtun~"
45 1.1 jmmv
46 1.1 jmmv /* Check if cell is on the border of a particular pane. */
47 1.1 jmmv int
48 1.1 jmmv screen_redraw_cell_border1(struct window_pane *wp, u_int px, u_int py)
49 1.1 jmmv {
50 1.1 jmmv /* Inside pane. */
51 1.1 jmmv if (px >= wp->xoff && px < wp->xoff + wp->sx &&
52 1.1 jmmv py >= wp->yoff && py < wp->yoff + wp->sy)
53 1.1 jmmv return (0);
54 1.1 jmmv
55 1.1 jmmv /* Left/right borders. */
56 1.1 jmmv if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= wp->yoff + wp->sy) {
57 1.1 jmmv if (wp->xoff != 0 && px == wp->xoff - 1)
58 1.1 jmmv return (1);
59 1.1 jmmv if (px == wp->xoff + wp->sx)
60 1.1 jmmv return (1);
61 1.1 jmmv }
62 1.1 jmmv
63 1.1 jmmv /* Top/bottom borders. */
64 1.1 jmmv if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= wp->xoff + wp->sx) {
65 1.1 jmmv if (wp->yoff != 0 && py == wp->yoff - 1)
66 1.1 jmmv return (1);
67 1.1 jmmv if (py == wp->yoff + wp->sy)
68 1.1 jmmv return (1);
69 1.1 jmmv }
70 1.1 jmmv
71 1.1 jmmv /* Outside pane. */
72 1.1 jmmv return (-1);
73 1.1 jmmv }
74 1.1 jmmv
75 1.1 jmmv /* Check if a cell is on the pane border. */
76 1.1 jmmv int
77 1.1 jmmv screen_redraw_cell_border(struct client *c, u_int px, u_int py)
78 1.1 jmmv {
79 1.1 jmmv struct window *w = c->session->curw->window;
80 1.1 jmmv struct window_pane *wp;
81 1.1 jmmv int retval;
82 1.1 jmmv
83 1.1 jmmv /* Check all the panes. */
84 1.1 jmmv TAILQ_FOREACH(wp, &w->panes, entry) {
85 1.1 jmmv if (!window_pane_visible(wp))
86 1.1 jmmv continue;
87 1.1 jmmv if ((retval = screen_redraw_cell_border1(wp, px, py)) != -1)
88 1.1 jmmv return (retval);
89 1.1 jmmv }
90 1.1 jmmv
91 1.1 jmmv return (0);
92 1.1 jmmv }
93 1.1 jmmv
94 1.1 jmmv /* Check if cell inside a pane. */
95 1.1 jmmv int
96 1.1 jmmv screen_redraw_check_cell(struct client *c, u_int px, u_int py)
97 1.1 jmmv {
98 1.1 jmmv struct window *w = c->session->curw->window;
99 1.1 jmmv struct window_pane *wp;
100 1.1 jmmv int borders;
101 1.1 jmmv
102 1.1 jmmv if (px > w->sx || py > w->sy)
103 1.1 jmmv return (CELL_OUTSIDE);
104 1.1 jmmv
105 1.1 jmmv TAILQ_FOREACH(wp, &w->panes, entry) {
106 1.1 jmmv if (!window_pane_visible(wp))
107 1.1 jmmv continue;
108 1.1 jmmv
109 1.1 jmmv /* If outside the pane and its border, skip it. */
110 1.1 jmmv if ((wp->xoff != 0 && px < wp->xoff - 1) ||
111 1.1 jmmv px > wp->xoff + wp->sx ||
112 1.1 jmmv (wp->yoff != 0 && py < wp->yoff - 1) ||
113 1.1 jmmv py > wp->yoff + wp->sy)
114 1.1 jmmv continue;
115 1.1 jmmv
116 1.1 jmmv /* If definitely inside, return so. */
117 1.1 jmmv if (!screen_redraw_cell_border(c, px, py))
118 1.1 jmmv return (CELL_INSIDE);
119 1.1 jmmv
120 1.1 jmmv /*
121 1.1 jmmv * Construct a bitmask of whether the cells to the left (bit
122 1.1 jmmv * 4), right, top, and bottom (bit 1) of this cell are borders.
123 1.1 jmmv */
124 1.1 jmmv borders = 0;
125 1.1 jmmv if (px == 0 || screen_redraw_cell_border(c, px - 1, py))
126 1.1 jmmv borders |= 8;
127 1.1 jmmv if (px <= w->sx && screen_redraw_cell_border(c, px + 1, py))
128 1.1 jmmv borders |= 4;
129 1.1 jmmv if (py == 0 || screen_redraw_cell_border(c, px, py - 1))
130 1.1 jmmv borders |= 2;
131 1.1 jmmv if (py <= w->sy && screen_redraw_cell_border(c, px, py + 1))
132 1.1 jmmv borders |= 1;
133 1.1 jmmv
134 1.1 jmmv /*
135 1.1 jmmv * Figure out what kind of border this cell is. Only one bit
136 1.1 jmmv * set doesn't make sense (can't have a border cell with no
137 1.1 jmmv * others connected).
138 1.1 jmmv */
139 1.1 jmmv switch (borders) {
140 1.1 jmmv case 15: /* 1111, left right top bottom */
141 1.1 jmmv return (CELL_JOIN);
142 1.1 jmmv case 14: /* 1110, left right top */
143 1.1 jmmv return (CELL_BOTTOMJOIN);
144 1.1 jmmv case 13: /* 1101, left right bottom */
145 1.1 jmmv return (CELL_TOPJOIN);
146 1.1 jmmv case 12: /* 1100, left right */
147 1.1 jmmv return (CELL_TOPBOTTOM);
148 1.1 jmmv case 11: /* 1011, left top bottom */
149 1.1 jmmv return (CELL_RIGHTJOIN);
150 1.1 jmmv case 10: /* 1010, left top */
151 1.1 jmmv return (CELL_BOTTOMRIGHT);
152 1.1 jmmv case 9: /* 1001, left bottom */
153 1.1 jmmv return (CELL_TOPRIGHT);
154 1.1 jmmv case 7: /* 0111, right top bottom */
155 1.1 jmmv return (CELL_LEFTJOIN);
156 1.1 jmmv case 6: /* 0110, right top */
157 1.1 jmmv return (CELL_BOTTOMLEFT);
158 1.1 jmmv case 5: /* 0101, right bottom */
159 1.1 jmmv return (CELL_TOPLEFT);
160 1.1 jmmv case 3: /* 0011, top bottom */
161 1.1 jmmv return (CELL_LEFTRIGHT);
162 1.1 jmmv }
163 1.1 jmmv }
164 1.1 jmmv
165 1.1 jmmv return (CELL_OUTSIDE);
166 1.1 jmmv }
167 1.1 jmmv
168 1.1 jmmv /* Redraw entire screen. */
169 1.1 jmmv void
170 1.1 jmmv screen_redraw_screen(struct client *c, int status_only, int borders_only)
171 1.1 jmmv {
172 1.1 jmmv struct window *w = c->session->curw->window;
173 1.1 jmmv struct tty *tty = &c->tty;
174 1.1 jmmv struct window_pane *wp;
175 1.1 jmmv struct grid_cell active_gc, other_gc;
176 1.1 jmmv u_int i, j, type;
177 1.1 jmmv int status, fg, bg;
178 1.1 jmmv
179 1.1 jmmv /* Get status line, er, status. */
180 1.1 jmmv if (c->message_string != NULL || c->prompt_string != NULL)
181 1.1 jmmv status = 1;
182 1.1 jmmv else
183 1.1 jmmv status = options_get_number(&c->session->options, "status");
184 1.1 jmmv
185 1.1 jmmv /* If only drawing status and it is present, don't need the rest. */
186 1.1 jmmv if (status_only && status) {
187 1.1 jmmv tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1);
188 1.1 jmmv tty_reset(tty);
189 1.1 jmmv return;
190 1.1 jmmv }
191 1.1 jmmv
192 1.1 jmmv /* Set up pane border attributes. */
193 1.1 jmmv memcpy(&other_gc, &grid_default_cell, sizeof other_gc);
194 1.1 jmmv memcpy(&active_gc, &grid_default_cell, sizeof active_gc);
195 1.1 jmmv active_gc.data = other_gc.data = 'x'; /* not space */
196 1.1 jmmv active_gc.attr = other_gc.attr = GRID_ATTR_CHARSET;
197 1.1 jmmv fg = options_get_number(&c->session->options, "pane-border-fg");
198 1.1 jmmv colour_set_fg(&other_gc, fg);
199 1.1 jmmv bg = options_get_number(&c->session->options, "pane-border-bg");
200 1.1 jmmv colour_set_bg(&other_gc, bg);
201 1.1 jmmv fg = options_get_number(&c->session->options, "pane-active-border-fg");
202 1.1 jmmv colour_set_fg(&active_gc, fg);
203 1.1 jmmv bg = options_get_number(&c->session->options, "pane-active-border-bg");
204 1.1 jmmv colour_set_bg(&active_gc, bg);
205 1.1 jmmv
206 1.1 jmmv /* Draw background and borders. */
207 1.1 jmmv for (j = 0; j < tty->sy - status; j++) {
208 1.1 jmmv if (status_only && j != tty->sy - 1)
209 1.1 jmmv continue;
210 1.1 jmmv for (i = 0; i < tty->sx; i++) {
211 1.1 jmmv type = screen_redraw_check_cell(c, i, j);
212 1.1 jmmv if (type == CELL_INSIDE)
213 1.1 jmmv continue;
214 1.1 jmmv if (screen_redraw_cell_border1(w->active, i, j) == 1)
215 1.1 jmmv tty_attributes(tty, &active_gc);
216 1.1 jmmv else
217 1.1 jmmv tty_attributes(tty, &other_gc);
218 1.1 jmmv tty_cursor(tty, i, j);
219 1.1 jmmv tty_putc(tty, CELL_BORDERS[type]);
220 1.1 jmmv }
221 1.1 jmmv }
222 1.1 jmmv
223 1.1 jmmv /* If only drawing borders, that's it. */
224 1.1 jmmv if (borders_only)
225 1.1 jmmv return;
226 1.1 jmmv
227 1.1 jmmv /* Draw the panes, if necessary. */
228 1.1 jmmv TAILQ_FOREACH(wp, &w->panes, entry) {
229 1.1 jmmv if (!window_pane_visible(wp))
230 1.1 jmmv continue;
231 1.1 jmmv for (i = 0; i < wp->sy; i++) {
232 1.1 jmmv if (status_only && wp->yoff + i != tty->sy - 1)
233 1.1 jmmv continue;
234 1.1 jmmv tty_draw_line(tty, wp->screen, i, wp->xoff, wp->yoff);
235 1.1 jmmv }
236 1.1 jmmv if (c->flags & CLIENT_IDENTIFY)
237 1.1 jmmv screen_redraw_draw_number(c, wp);
238 1.1 jmmv }
239 1.1 jmmv
240 1.1 jmmv /* Draw the status line. */
241 1.1 jmmv if (status)
242 1.1 jmmv tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1);
243 1.1 jmmv tty_reset(tty);
244 1.1 jmmv }
245 1.1 jmmv
246 1.1 jmmv /* Draw a single pane. */
247 1.1 jmmv void
248 1.1 jmmv screen_redraw_pane(struct client *c, struct window_pane *wp)
249 1.1 jmmv {
250 1.1 jmmv u_int i;
251 1.1 jmmv
252 1.1 jmmv for (i = 0; i < wp->sy; i++)
253 1.1 jmmv tty_draw_line(&c->tty, wp->screen, i, wp->xoff, wp->yoff);
254 1.1 jmmv tty_reset(&c->tty);
255 1.1 jmmv }
256 1.1 jmmv
257 1.1 jmmv /* Draw number on a pane. */
258 1.1 jmmv void
259 1.1 jmmv screen_redraw_draw_number(struct client *c, struct window_pane *wp)
260 1.1 jmmv {
261 1.1 jmmv struct tty *tty = &c->tty;
262 1.1 jmmv struct session *s = c->session;
263 1.1 jmmv struct options *oo = &s->options;
264 1.1 jmmv struct window *w = wp->window;
265 1.1 jmmv struct grid_cell gc;
266 1.1 jmmv u_int idx, px, py, i, j, xoff, yoff;
267 1.1 jmmv int colour, active_colour;
268 1.1 jmmv char buf[16], *ptr;
269 1.1 jmmv size_t len;
270 1.1 jmmv
271 1.1 jmmv idx = window_pane_index(w, wp);
272 1.1 jmmv len = xsnprintf(buf, sizeof buf, "%u", idx);
273 1.1 jmmv
274 1.1 jmmv if (wp->sx < len)
275 1.1 jmmv return;
276 1.1 jmmv colour = options_get_number(oo, "display-panes-colour");
277 1.1 jmmv active_colour = options_get_number(oo, "display-panes-active-colour");
278 1.1 jmmv
279 1.1 jmmv px = wp->sx / 2; py = wp->sy / 2;
280 1.1 jmmv xoff = wp->xoff; yoff = wp->yoff;
281 1.1 jmmv
282 1.1 jmmv if (wp->sx < len * 6 || wp->sy < 5) {
283 1.1 jmmv tty_cursor(tty, xoff + px - len / 2, yoff + py);
284 1.1 jmmv memcpy(&gc, &grid_default_cell, sizeof gc);
285 1.1 jmmv gc.data = '_'; /* not space */
286 1.1 jmmv if (w->active == wp)
287 1.1 jmmv colour_set_fg(&gc, active_colour);
288 1.1 jmmv else
289 1.1 jmmv colour_set_fg(&gc, colour);
290 1.1 jmmv tty_attributes(tty, &gc);
291 1.1 jmmv tty_puts(tty, buf);
292 1.1 jmmv return;
293 1.1 jmmv }
294 1.1 jmmv
295 1.1 jmmv px -= len * 3;
296 1.1 jmmv py -= 2;
297 1.1 jmmv
298 1.1 jmmv memcpy(&gc, &grid_default_cell, sizeof gc);
299 1.1 jmmv gc.data = '_'; /* not space */
300 1.1 jmmv if (w->active == wp)
301 1.1 jmmv colour_set_bg(&gc, active_colour);
302 1.1 jmmv else
303 1.1 jmmv colour_set_bg(&gc, colour);
304 1.1 jmmv tty_attributes(tty, &gc);
305 1.1 jmmv for (ptr = buf; *ptr != '\0'; ptr++) {
306 1.1 jmmv if (*ptr < '0' || *ptr > '9')
307 1.1 jmmv continue;
308 1.1 jmmv idx = *ptr - '0';
309 1.1 jmmv
310 1.1 jmmv for (j = 0; j < 5; j++) {
311 1.1 jmmv for (i = px; i < px + 5; i++) {
312 1.1 jmmv tty_cursor(tty, xoff + i, yoff + py + j);
313 1.1 jmmv if (clock_table[idx][j][i - px])
314 1.1 jmmv tty_putc(tty, ' ');
315 1.1 jmmv }
316 1.1 jmmv }
317 1.1 jmmv px += 6;
318 1.1 jmmv }
319 1.1 jmmv }
320