Annotation of src/usr.bin/tmux/screen-redraw.c, Revision 1.24
1.24 ! nicm 1: /* $OpenBSD: screen-redraw.c,v 1.23 2013/03/25 10:12:01 nicm Exp $ */
1.1 nicm 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:
1.15 nicm 25: int screen_redraw_cell_border1(struct window_pane *, u_int, u_int);
1.7 nicm 26: int screen_redraw_cell_border(struct client *, u_int, u_int);
1.24 ! nicm 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:
1.10 nicm 32: void screen_redraw_draw_number(struct client *, struct window_pane *);
1.1 nicm 33:
1.6 nicm 34: #define CELL_INSIDE 0
1.7 nicm 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
1.6 nicm 47:
1.17 nicm 48: #define CELL_BORDERS " xqlkmjwvtun~"
49:
1.15 nicm 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:
1.7 nicm 79: /* Check if a cell is on the pane border. */
1.1 nicm 80: int
1.7 nicm 81: screen_redraw_cell_border(struct client *c, u_int px, u_int py)
1.1 nicm 82: {
83: struct window *w = c->session->curw->window;
84: struct window_pane *wp;
1.15 nicm 85: int retval;
1.1 nicm 86:
1.7 nicm 87: /* Check all the panes. */
1.1 nicm 88: TAILQ_FOREACH(wp, &w->panes, entry) {
1.3 nicm 89: if (!window_pane_visible(wp))
90: continue;
1.15 nicm 91: if ((retval = screen_redraw_cell_border1(wp, px, py)) != -1)
92: return (retval);
1.7 nicm 93: }
94:
95: return (0);
96: }
97:
98: /* Check if cell inside a pane. */
99: int
1.24 ! nicm 100: screen_redraw_check_cell(struct client *c, u_int px, u_int py,
! 101: struct window_pane **wpp)
1.7 nicm 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;
1.24 ! nicm 113: *wpp = wp;
1.7 nicm 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:
1.14 nicm 126: /*
1.7 nicm 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:
1.14 nicm 140: /*
1.7 nicm 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);
1.1 nicm 168: }
169: }
170:
1.24 ! nicm 171: *wpp = NULL;
1.6 nicm 172: return (CELL_OUTSIDE);
1.1 nicm 173: }
174:
1.24 ! nicm 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:
1.4 nicm 217: /* Redraw entire screen. */
1.1 nicm 218: void
1.15 nicm 219: screen_redraw_screen(struct client *c, int status_only, int borders_only)
1.1 nicm 220: {
221: struct window *w = c->session->curw->window;
1.21 nicm 222: struct options *oo = &c->session->options;
1.1 nicm 223: struct tty *tty = &c->tty;
224: struct window_pane *wp;
1.15 nicm 225: struct grid_cell active_gc, other_gc;
1.21 nicm 226: u_int i, j, type, top;
227: int status, spos, fg, bg;
1.18 nicm 228:
229: /* Suspended clients should not be updated. */
230: if (c->flags & CLIENT_SUSPENDED)
231: return;
1.1 nicm 232:
233: /* Get status line, er, status. */
1.21 nicm 234: spos = options_get_number(oo, "status-position");
1.4 nicm 235: if (c->message_string != NULL || c->prompt_string != NULL)
236: status = 1;
237: else
1.21 nicm 238: status = options_get_number(oo, "status");
239: top = 0;
240: if (status && spos == 0)
241: top = 1;
1.4 nicm 242:
243: /* If only drawing status and it is present, don't need the rest. */
244: if (status_only && status) {
1.21 nicm 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);
1.9 nicm 249: tty_reset(tty);
1.4 nicm 250: return;
251: }
1.1 nicm 252:
1.15 nicm 253: /* Set up pane border attributes. */
1.22 nicm 254: memcpy(&other_gc, &grid_marker_cell, sizeof other_gc);
255: memcpy(&active_gc, &grid_marker_cell, sizeof active_gc);
1.17 nicm 256: active_gc.attr = other_gc.attr = GRID_ATTR_CHARSET;
1.21 nicm 257: fg = options_get_number(oo, "pane-border-fg");
1.15 nicm 258: colour_set_fg(&other_gc, fg);
1.21 nicm 259: bg = options_get_number(oo, "pane-border-bg");
1.15 nicm 260: colour_set_bg(&other_gc, bg);
1.21 nicm 261: fg = options_get_number(oo, "pane-active-border-fg");
1.15 nicm 262: colour_set_fg(&active_gc, fg);
1.21 nicm 263: bg = options_get_number(oo, "pane-active-border-bg");
1.15 nicm 264: colour_set_bg(&active_gc, bg);
265:
1.6 nicm 266: /* Draw background and borders. */
1.1 nicm 267: for (j = 0; j < tty->sy - status; j++) {
1.21 nicm 268: if (status_only) {
269: if (spos == 1 && j != tty->sy - 1)
270: continue;
271: else if (spos == 0 && j != 0)
272: break;
273: }
1.1 nicm 274: for (i = 0; i < tty->sx; i++) {
1.24 ! nicm 275: type = screen_redraw_check_cell(c, i, j, &wp);
1.15 nicm 276: if (type == CELL_INSIDE)
277: continue;
1.24 ! nicm 278: if (screen_redraw_check_active(i, j, type, w, wp))
1.15 nicm 279: tty_attributes(tty, &active_gc);
280: else
281: tty_attributes(tty, &other_gc);
1.21 nicm 282: tty_cursor(tty, i, top + j);
1.17 nicm 283: tty_putc(tty, CELL_BORDERS[type]);
1.1 nicm 284: }
285: }
286:
1.15 nicm 287: /* If only drawing borders, that's it. */
288: if (borders_only)
289: return;
290:
291: /* Draw the panes, if necessary. */
1.1 nicm 292: TAILQ_FOREACH(wp, &w->panes, entry) {
1.3 nicm 293: if (!window_pane_visible(wp))
1.1 nicm 294: continue;
1.4 nicm 295: for (i = 0; i < wp->sy; i++) {
1.21 nicm 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);
1.4 nicm 304: }
1.10 nicm 305: if (c->flags & CLIENT_IDENTIFY)
306: screen_redraw_draw_number(c, wp);
1.1 nicm 307: }
308:
309: /* Draw the status line. */
1.21 nicm 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: }
1.9 nicm 316: tty_reset(tty);
1.1 nicm 317: }
318:
319: /* Draw a single pane. */
320: void
321: screen_redraw_pane(struct client *c, struct window_pane *wp)
322: {
1.21 nicm 323: u_int i, yoff;
1.23 nicm 324:
325: if (!window_pane_visible(wp))
326: return;
1.21 nicm 327:
328: yoff = wp->yoff;
329: if (status_at_line(c) == 0)
330: yoff++;
1.1 nicm 331:
332: for (i = 0; i < wp->sy; i++)
1.21 nicm 333: tty_draw_line(&c->tty, wp->screen, i, wp->xoff, yoff);
1.9 nicm 334: tty_reset(&c->tty);
1.10 nicm 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;
1.19 nicm 343: struct options *oo = &s->options;
1.16 nicm 344: struct window *w = wp->window;
1.10 nicm 345: struct grid_cell gc;
1.12 nicm 346: u_int idx, px, py, i, j, xoff, yoff;
1.16 nicm 347: int colour, active_colour;
1.10 nicm 348: char buf[16], *ptr;
349: size_t len;
350:
1.19 nicm 351: if (window_pane_index(wp, &idx) != 0)
352: fatalx("index not found");
1.10 nicm 353: len = xsnprintf(buf, sizeof buf, "%u", idx);
354:
355: if (wp->sx < len)
356: return;
1.16 nicm 357: colour = options_get_number(oo, "display-panes-colour");
358: active_colour = options_get_number(oo, "display-panes-active-colour");
1.10 nicm 359:
1.12 nicm 360: px = wp->sx / 2; py = wp->sy / 2;
361: xoff = wp->xoff; yoff = wp->yoff;
362:
1.10 nicm 363: if (wp->sx < len * 6 || wp->sy < 5) {
1.12 nicm 364: tty_cursor(tty, xoff + px - len / 2, yoff + py);
1.20 nicm 365: goto draw_text;
1.10 nicm 366: }
1.14 nicm 367:
1.10 nicm 368: px -= len * 3;
369: py -= 2;
370:
1.22 nicm 371: memcpy(&gc, &grid_marker_cell, sizeof gc);
1.16 nicm 372: if (w->active == wp)
373: colour_set_bg(&gc, active_colour);
374: else
375: colour_set_bg(&gc, colour);
1.10 nicm 376: tty_attributes(tty, &gc);
377: for (ptr = buf; *ptr != '\0'; ptr++) {
378: if (*ptr < '0' || *ptr > '9')
379: continue;
380: idx = *ptr - '0';
1.14 nicm 381:
1.10 nicm 382: for (j = 0; j < 5; j++) {
383: for (i = px; i < px + 5; i++) {
1.12 nicm 384: tty_cursor(tty, xoff + i, yoff + py + j);
1.11 nicm 385: if (clock_table[idx][j][i - px])
386: tty_putc(tty, ' ');
1.10 nicm 387: }
388: }
389: px += 6;
390: }
1.20 nicm 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:
1.22 nicm 398: memcpy(&gc, &grid_marker_cell, sizeof gc);
1.20 nicm 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);
1.1 nicm 407: }