Annotation of src/usr.bin/tmux/screen-redraw.c, Revision 1.79
1.79 ! nicm 1: /* $OpenBSD: screen-redraw.c,v 1.78 2020/05/16 16:26:34 nicm Exp $ */
1.1 nicm 2:
3: /*
1.35 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.48 nicm 21: #include <stdlib.h>
1.1 nicm 22: #include <string.h>
23:
24: #include "tmux.h"
25:
1.50 nicm 26: static void screen_redraw_draw_borders(struct screen_redraw_ctx *);
27: static void screen_redraw_draw_panes(struct screen_redraw_ctx *);
28: static void screen_redraw_draw_status(struct screen_redraw_ctx *);
1.55 nicm 29: static void screen_redraw_draw_pane(struct screen_redraw_ctx *,
30: struct window_pane *);
1.79 ! nicm 31: static void screen_redraw_set_context(struct client *,
! 32: struct screen_redraw_ctx *);
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.78 nicm 50: static const struct utf8_data screen_redraw_double_borders[] = {
51: { "", 0, 0, 0 },
52: { "\342\225\221", 0, 3, 1 }, /* U+2551 */
53: { "\342\225\220", 0, 3, 1 }, /* U+2550 */
54: { "\342\225\224", 0, 3, 1 }, /* U+2554 */
55: { "\342\225\227", 0, 3, 1 }, /* U+2557 */
56: { "\342\225\232", 0, 3, 1 }, /* U+255A */
57: { "\342\225\235", 0, 3, 1 }, /* U+255D */
58: { "\342\225\246", 0, 3, 1 }, /* U+2566 */
59: { "\342\225\251", 0, 3, 1 }, /* U+2569 */
60: { "\342\225\240", 0, 3, 1 }, /* U+2560 */
61: { "\342\225\243", 0, 3, 1 }, /* U+2563 */
62: { "\342\225\254", 0, 3, 1 }, /* U+256C */
63: { "\302\267", 0, 2, 1 } /* U+00B7 */
64: };
65:
66: static const struct utf8_data screen_redraw_heavy_borders[] = {
67: { "", 0, 0, 0 },
68: { "\342\224\203", 0, 3, 1 }, /* U+2503 */
69: { "\342\224\201", 0, 3, 1 }, /* U+2501 */
70: { "\342\224\223", 0, 3, 1 }, /* U+2513 */
71: { "\342\224\217", 0, 3, 1 }, /* U+250F */
72: { "\342\224\227", 0, 3, 1 }, /* U+2517 */
73: { "\342\224\233", 0, 3, 1 }, /* U+251B */
74: { "\342\224\263", 0, 3, 1 }, /* U+2533 */
75: { "\342\224\273", 0, 3, 1 }, /* U+253B */
76: { "\342\224\243", 0, 3, 1 }, /* U+2523 */
77: { "\342\224\253", 0, 3, 1 }, /* U+252B */
78: { "\342\225\213", 0, 3, 1 }, /* U+254B */
79: { "\302\267", 0, 2, 1 } /* U+00B7 */
1.75 nicm 80: };
81:
82: enum screen_redraw_border_type {
83: SCREEN_REDRAW_OUTSIDE,
84: SCREEN_REDRAW_INSIDE,
85: SCREEN_REDRAW_BORDER
86: };
87:
1.78 nicm 88: /* Get cell border character. */
89: static void
90: screen_redraw_border_set(struct window_pane *wp, int pane_lines, int cell_type,
91: struct grid_cell *gc)
92: {
93: u_int idx;
94:
95: switch (pane_lines) {
96: case PANE_LINES_NUMBER:
97: if (cell_type == CELL_OUTSIDE) {
98: gc->attr |= GRID_ATTR_CHARSET;
99: utf8_set(&gc->data, CELL_BORDERS[CELL_OUTSIDE]);
100: break;
101: }
102: gc->attr &= ~GRID_ATTR_CHARSET;
103: if (wp != NULL && window_pane_index(wp, &idx) == 0)
104: utf8_set(&gc->data, '0' + (idx % 10));
105: else
106: utf8_set(&gc->data, '*');
107: break;
108: case PANE_LINES_DOUBLE:
109: gc->attr &= ~GRID_ATTR_CHARSET;
110: utf8_copy(&gc->data, &screen_redraw_double_borders[cell_type]);
111: break;
112: case PANE_LINES_HEAVY:
113: gc->attr &= ~GRID_ATTR_CHARSET;
114: utf8_copy(&gc->data, &screen_redraw_heavy_borders[cell_type]);
115: break;
116: case PANE_LINES_SIMPLE:
117: gc->attr &= ~GRID_ATTR_CHARSET;
118: utf8_set(&gc->data, " |-+++++++++."[cell_type]);
119: break;
120: default:
121: gc->attr |= GRID_ATTR_CHARSET;
122: utf8_set(&gc->data, CELL_BORDERS[cell_type]);
123: break;
124: }
125: }
126:
1.75 nicm 127: /* Return if window has only two panes. */
1.39 nicm 128: static int
1.75 nicm 129: screen_redraw_two_panes(struct window *w, int direction)
1.15 nicm 130: {
1.75 nicm 131: struct window_pane *wp;
132:
133: wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry);
134: if (wp == NULL)
135: return (0); /* one pane */
136: if (TAILQ_NEXT(wp, entry) != NULL)
137: return (0); /* more than two panes */
138: if (direction == 0 && wp->xoff == 0)
139: return (0);
140: if (direction == 1 && wp->yoff == 0)
141: return (0);
142: return (1);
143: }
144:
145: /* Check if cell is on the border of a pane. */
146: static enum screen_redraw_border_type
147: screen_redraw_pane_border(struct window_pane *wp, u_int px, u_int py,
148: int pane_status)
149: {
150: u_int ex = wp->xoff + wp->sx, ey = wp->yoff + wp->sy;
151:
1.15 nicm 152: /* Inside pane. */
1.75 nicm 153: if (px >= wp->xoff && px < ex && py >= wp->yoff && py < ey)
154: return (SCREEN_REDRAW_INSIDE);
1.15 nicm 155:
156: /* Left/right borders. */
1.75 nicm 157: if (pane_status == PANE_STATUS_OFF) {
158: if (screen_redraw_two_panes(wp->window, 0)) {
159: if (wp->xoff == 0 && px == wp->sx && py <= wp->sy / 2)
160: return (SCREEN_REDRAW_BORDER);
161: if (wp->xoff != 0 &&
162: px == wp->xoff - 1 &&
163: py > wp->sy / 2)
164: return (SCREEN_REDRAW_BORDER);
165: } else {
166: if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= ey) {
167: if (wp->xoff != 0 && px == wp->xoff - 1)
168: return (SCREEN_REDRAW_BORDER);
169: if (px == ex)
170: return (SCREEN_REDRAW_BORDER);
171: }
172: }
173: } else {
174: if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= ey) {
175: if (wp->xoff != 0 && px == wp->xoff - 1)
176: return (SCREEN_REDRAW_BORDER);
177: if (px == ex)
178: return (SCREEN_REDRAW_BORDER);
179: }
1.15 nicm 180: }
181:
182: /* Top/bottom borders. */
1.75 nicm 183: if (pane_status == PANE_STATUS_OFF) {
184: if (screen_redraw_two_panes(wp->window, 1)) {
185: if (wp->yoff == 0 && py == wp->sy && px <= wp->sx / 2)
186: return (SCREEN_REDRAW_BORDER);
187: if (wp->yoff != 0 &&
188: py == wp->yoff - 1 &&
189: px > wp->sx / 2)
190: return (SCREEN_REDRAW_BORDER);
191: } else {
192: if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= ex) {
193: if (wp->yoff != 0 && py == wp->yoff - 1)
194: return (SCREEN_REDRAW_BORDER);
195: if (py == ey)
196: return (SCREEN_REDRAW_BORDER);
197: }
198: }
199: } else if (pane_status == PANE_STATUS_TOP) {
200: if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= ex) {
201: if (wp->yoff != 0 && py == wp->yoff - 1)
202: return (SCREEN_REDRAW_BORDER);
203: }
204: } else {
205: if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= ex) {
206: if (py == ey)
207: return (SCREEN_REDRAW_BORDER);
208: }
1.15 nicm 209: }
210:
211: /* Outside pane. */
1.75 nicm 212: return (SCREEN_REDRAW_OUTSIDE);
1.15 nicm 213: }
214:
1.75 nicm 215: /* Check if a cell is on a border. */
1.39 nicm 216: static int
1.75 nicm 217: screen_redraw_cell_border(struct client *c, u_int px, u_int py, int pane_status)
1.1 nicm 218: {
219: struct window *w = c->session->curw->window;
220: struct window_pane *wp;
1.75 nicm 221:
222: /* Outside the window? */
223: if (px > w->sx || py > w->sy)
224: return (0);
225:
226: /* On the window border? */
227: if (px == w->sx || py == w->sy)
228: return (1);
1.1 nicm 229:
1.7 nicm 230: /* Check all the panes. */
1.1 nicm 231: TAILQ_FOREACH(wp, &w->panes, entry) {
1.3 nicm 232: if (!window_pane_visible(wp))
233: continue;
1.75 nicm 234: switch (screen_redraw_pane_border(wp, px, py, pane_status)) {
235: case SCREEN_REDRAW_INSIDE:
236: return (0);
237: case SCREEN_REDRAW_BORDER:
238: return (1);
239: case SCREEN_REDRAW_OUTSIDE:
240: break;
241: }
1.7 nicm 242: }
243:
244: return (0);
245: }
246:
1.75 nicm 247: /* Work out type of border cell from surrounding cells. */
248: static int
249: screen_redraw_type_of_cell(struct client *c, u_int px, u_int py,
250: int pane_status)
251: {
252: struct window *w = c->session->curw->window;
253: u_int sx = w->sx, sy = w->sy;
254: int borders = 0;
255:
1.79 ! nicm 256: /* Is this outside the window? */
! 257: if (px >= sx || py >= sy)
! 258: return (CELL_OUTSIDE);
! 259:
1.75 nicm 260: /*
261: * Construct a bitmask of whether the cells to the left (bit 4), right,
262: * top, and bottom (bit 1) of this cell are borders.
263: */
264: if (px == 0 || screen_redraw_cell_border(c, px - 1, py, pane_status))
265: borders |= 8;
266: if (px <= sx && screen_redraw_cell_border(c, px + 1, py, pane_status))
267: borders |= 4;
268: if (pane_status == PANE_STATUS_TOP) {
269: if (py != 0 &&
270: screen_redraw_cell_border(c, px, py - 1, pane_status))
271: borders |= 2;
1.79 ! nicm 272: if (screen_redraw_cell_border(c, px, py + 1, pane_status))
! 273: borders |= 1;
1.75 nicm 274: } else {
275: if (py == 0 ||
276: screen_redraw_cell_border(c, px, py - 1, pane_status))
1.79 ! nicm 277: borders |= 2;
! 278: if (py != sy - 1 &&
! 279: screen_redraw_cell_border(c, px, py + 1, pane_status))
! 280: borders |= 1;
1.75 nicm 281: }
282:
283: /*
284: * Figure out what kind of border this cell is. Only one bit set
285: * doesn't make sense (can't have a border cell with no others
286: * connected).
287: */
288: switch (borders) {
289: case 15: /* 1111, left right top bottom */
290: return (CELL_JOIN);
291: case 14: /* 1110, left right top */
292: return (CELL_BOTTOMJOIN);
293: case 13: /* 1101, left right bottom */
294: return (CELL_TOPJOIN);
295: case 12: /* 1100, left right */
296: return (CELL_TOPBOTTOM);
297: case 11: /* 1011, left top bottom */
298: return (CELL_RIGHTJOIN);
299: case 10: /* 1010, left top */
300: return (CELL_BOTTOMRIGHT);
301: case 9: /* 1001, left bottom */
302: return (CELL_TOPRIGHT);
303: case 7: /* 0111, right top bottom */
304: return (CELL_LEFTJOIN);
305: case 6: /* 0110, right top */
306: return (CELL_BOTTOMLEFT);
307: case 5: /* 0101, right bottom */
308: return (CELL_TOPLEFT);
309: case 3: /* 0011, top bottom */
310: return (CELL_LEFTRIGHT);
311: }
312: return (CELL_OUTSIDE);
313: }
314:
1.7 nicm 315: /* Check if cell inside a pane. */
1.39 nicm 316: static int
1.36 nicm 317: screen_redraw_check_cell(struct client *c, u_int px, u_int py, int pane_status,
1.24 nicm 318: struct window_pane **wpp)
1.7 nicm 319: {
320: struct window *w = c->session->curw->window;
1.77 nicm 321: struct window_pane *wp, *active;
1.75 nicm 322: int border;
1.36 nicm 323: u_int right, line;
1.7 nicm 324:
1.39 nicm 325: *wpp = NULL;
326:
1.79 ! nicm 327: if (px >= w->sx || py >= w->sy)
1.7 nicm 328: return (CELL_OUTSIDE);
1.75 nicm 329: if (px == w->sx || py == w->sy) /* window border */
330: return (screen_redraw_type_of_cell(c, px, py, pane_status));
1.7 nicm 331:
1.64 nicm 332: if (pane_status != PANE_STATUS_OFF) {
1.77 nicm 333: active = wp = server_client_get_pane(c);
1.75 nicm 334: do {
1.36 nicm 335: if (!window_pane_visible(wp))
1.75 nicm 336: goto next1;
1.36 nicm 337:
1.64 nicm 338: if (pane_status == PANE_STATUS_TOP)
1.36 nicm 339: line = wp->yoff - 1;
340: else
341: line = wp->yoff + wp->sy;
342: right = wp->xoff + 2 + wp->status_size - 1;
343:
344: if (py == line && px >= wp->xoff + 2 && px <= right)
345: return (CELL_INSIDE);
1.75 nicm 346:
347: next1:
348: wp = TAILQ_NEXT(wp, entry);
349: if (wp == NULL)
350: wp = TAILQ_FIRST(&w->panes);
1.77 nicm 351: } while (wp != active);
1.36 nicm 352: }
353:
1.77 nicm 354: active = wp = server_client_get_pane(c);
1.75 nicm 355: do {
1.7 nicm 356: if (!window_pane_visible(wp))
1.75 nicm 357: goto next2;
1.24 nicm 358: *wpp = wp;
1.7 nicm 359:
1.14 nicm 360: /*
1.75 nicm 361: * If definitely inside, return. If not on border, skip.
362: * Otherwise work out the cell.
1.7 nicm 363: */
1.75 nicm 364: border = screen_redraw_pane_border(wp, px, py, pane_status);
365: if (border == SCREEN_REDRAW_INSIDE)
366: return (CELL_INSIDE);
367: if (border == SCREEN_REDRAW_OUTSIDE)
368: goto next2;
369: return (screen_redraw_type_of_cell(c, px, py, pane_status));
370:
371: next2:
372: wp = TAILQ_NEXT(wp, entry);
373: if (wp == NULL)
374: wp = TAILQ_FIRST(&w->panes);
1.77 nicm 375: } while (wp != active);
1.1 nicm 376:
1.6 nicm 377: return (CELL_OUTSIDE);
1.1 nicm 378: }
379:
1.32 nicm 380: /* Check if the border of a particular pane. */
1.39 nicm 381: static int
1.75 nicm 382: screen_redraw_check_is(u_int px, u_int py, int pane_status,
383: struct window_pane *wp)
1.24 nicm 384: {
1.75 nicm 385: enum screen_redraw_border_type border;
1.36 nicm 386:
1.75 nicm 387: border = screen_redraw_pane_border(wp, px, py, pane_status);
388: if (border == SCREEN_REDRAW_BORDER)
1.24 nicm 389: return (1);
1.75 nicm 390: return (0);
1.36 nicm 391: }
392:
393: /* Update pane status. */
1.39 nicm 394: static int
1.79 ! nicm 395: screen_redraw_make_pane_status(struct client *c, struct window_pane *wp,
! 396: struct screen_redraw_ctx *rctx, int pane_lines)
1.36 nicm 397: {
1.79 ! nicm 398: struct window *w = wp->window;
1.36 nicm 399: struct grid_cell gc;
400: const char *fmt;
401: struct format_tree *ft;
1.59 nicm 402: char *expanded;
1.79 ! nicm 403: int pane_status = rctx->pane_status;
! 404: u_int width, i, cell_type, top, px, py;
1.36 nicm 405: struct screen_write_ctx ctx;
1.40 nicm 406: struct screen old;
1.36 nicm 407:
1.75 nicm 408: ft = format_create(c, NULL, FORMAT_PANE|wp->id, FORMAT_STATUS);
409: format_defaults(ft, c, c->session, c->session->curw, wp);
410:
1.77 nicm 411: if (wp == server_client_get_pane(c))
1.75 nicm 412: style_apply(&gc, w->options, "pane-active-border-style", ft);
1.36 nicm 413: else
1.75 nicm 414: style_apply(&gc, w->options, "pane-border-style", ft);
1.36 nicm 415: fmt = options_get_string(w->options, "pane-border-format");
416:
1.59 nicm 417: expanded = format_expand_time(ft, fmt);
1.60 nicm 418: if (wp->sx < 4)
419: wp->status_size = width = 0;
420: else
421: wp->status_size = width = wp->sx - 4;
1.59 nicm 422:
1.40 nicm 423: memcpy(&old, &wp->status_screen, sizeof old);
1.59 nicm 424: screen_init(&wp->status_screen, width, 1, 0);
1.36 nicm 425: wp->status_screen.mode = 0;
426:
1.76 nicm 427: screen_write_start(&ctx, &wp->status_screen);
1.59 nicm 428:
1.79 ! nicm 429: if (rctx->statustop)
! 430: top = rctx->statuslines;
! 431: else
! 432: top = 0;
! 433: for (i = 0; i < width; i++) {
! 434: px = wp->xoff + 2 + i;
! 435: if (rctx->pane_status == PANE_STATUS_TOP)
! 436: py = top + wp->yoff - 1;
! 437: else
! 438: py = top + wp->yoff + wp->sy;
! 439: cell_type = screen_redraw_type_of_cell(c, px, py, pane_status);
! 440: screen_redraw_border_set(wp, pane_lines, cell_type, &gc);
1.78 nicm 441: screen_write_cell(&ctx, &gc);
1.79 ! nicm 442: }
1.59 nicm 443: gc.attr &= ~GRID_ATTR_CHARSET;
1.36 nicm 444:
1.56 nicm 445: screen_write_cursormove(&ctx, 0, 0, 0);
1.59 nicm 446: format_draw(&ctx, &gc, width, expanded, NULL);
1.36 nicm 447: screen_write_stop(&ctx);
448:
1.59 nicm 449: free(expanded);
1.36 nicm 450: format_free(ft);
1.40 nicm 451:
452: if (grid_compare(wp->status_screen.grid, old.grid) == 0) {
453: screen_free(&old);
454: return (0);
455: }
456: screen_free(&old);
457: return (1);
1.36 nicm 458: }
459:
460: /* Draw pane status. */
1.39 nicm 461: static void
1.52 nicm 462: screen_redraw_draw_pane_status(struct screen_redraw_ctx *ctx)
1.36 nicm 463: {
1.53 nicm 464: struct client *c = ctx->c;
1.36 nicm 465: struct window *w = c->session->curw->window;
466: struct tty *tty = &c->tty;
467: struct window_pane *wp;
1.55 nicm 468: struct screen *s;
469: u_int i, x, width, xoff, yoff, size;
470:
471: log_debug("%s: %s @%u", __func__, c->name, w->id);
1.36 nicm 472:
473: TAILQ_FOREACH(wp, &w->panes, entry) {
1.40 nicm 474: if (!window_pane_visible(wp))
475: continue;
1.55 nicm 476: s = &wp->status_screen;
477:
478: size = wp->status_size;
1.64 nicm 479: if (ctx->pane_status == PANE_STATUS_TOP)
1.36 nicm 480: yoff = wp->yoff - 1;
481: else
482: yoff = wp->yoff + wp->sy;
1.55 nicm 483: xoff = wp->xoff + 2;
484:
485: if (xoff + size <= ctx->ox ||
486: xoff >= ctx->ox + ctx->sx ||
487: yoff < ctx->oy ||
488: yoff >= ctx->oy + ctx->sy)
489: continue;
490:
491: if (xoff >= ctx->ox && xoff + size <= ctx->ox + ctx->sx) {
492: /* All visible. */
493: i = 0;
494: x = xoff - ctx->ox;
495: width = size;
496: } else if (xoff < ctx->ox && xoff + size > ctx->ox + ctx->sx) {
497: /* Both left and right not visible. */
498: i = ctx->ox;
499: x = 0;
500: width = ctx->sx;
501: } else if (xoff < ctx->ox) {
502: /* Left not visible. */
503: i = ctx->ox - xoff;
504: x = 0;
505: width = size - i;
506: } else {
507: /* Right not visible. */
508: i = 0;
509: x = xoff - ctx->ox;
510: width = size - x;
511: }
1.36 nicm 512:
1.61 nicm 513: if (ctx->statustop)
514: yoff += ctx->statuslines;
1.76 nicm 515: tty_draw_line(tty, s, i, 0, width, x, yoff - ctx->oy,
516: &grid_default_cell, NULL);
1.36 nicm 517: }
518: tty_cursor(tty, 0, 0);
1.24 nicm 519: }
520:
1.38 nicm 521: /* Update status line and change flags if unchanged. */
1.54 nicm 522: static int
523: screen_redraw_update(struct client *c, int flags)
1.38 nicm 524: {
525: struct window *w = c->session->curw->window;
526: struct window_pane *wp;
527: struct options *wo = w->options;
1.78 nicm 528: int redraw, lines;
1.79 ! nicm 529: struct screen_redraw_ctx ctx;
1.38 nicm 530:
531: if (c->message_string != NULL)
532: redraw = status_message_redraw(c);
533: else if (c->prompt_string != NULL)
534: redraw = status_prompt_redraw(c);
535: else
536: redraw = status_redraw(c);
1.54 nicm 537: if (!redraw && (~flags & CLIENT_REDRAWSTATUSALWAYS))
538: flags &= ~CLIENT_REDRAWSTATUS;
1.38 nicm 539:
1.63 nicm 540: if (c->overlay_draw != NULL)
541: flags |= CLIENT_REDRAWOVERLAY;
542:
1.64 nicm 543: if (options_get_number(wo, "pane-border-status") != PANE_STATUS_OFF) {
1.79 ! nicm 544: screen_redraw_set_context(c, &ctx);
1.78 nicm 545: lines = options_get_number(wo, "pane-border-lines");
1.38 nicm 546: redraw = 0;
547: TAILQ_FOREACH(wp, &w->panes, entry) {
1.79 ! nicm 548: if (screen_redraw_make_pane_status(c, wp, &ctx, lines))
1.38 nicm 549: redraw = 1;
550: }
551: if (redraw)
1.54 nicm 552: flags |= CLIENT_REDRAWBORDERS;
1.38 nicm 553: }
1.54 nicm 554: return (flags);
1.38 nicm 555: }
556:
1.52 nicm 557: /* Set up redraw context. */
558: static void
559: screen_redraw_set_context(struct client *c, struct screen_redraw_ctx *ctx)
560: {
1.53 nicm 561: struct session *s = c->session;
562: struct options *oo = s->options;
563: struct window *w = s->curw->window;
564: struct options *wo = w->options;
1.61 nicm 565: u_int lines;
1.52 nicm 566:
567: memset(ctx, 0, sizeof *ctx);
568: ctx->c = c;
569:
1.61 nicm 570: lines = status_line_size(c);
1.55 nicm 571: if (c->message_string != NULL || c->prompt_string != NULL)
1.61 nicm 572: lines = (lines == 0) ? 1 : lines;
573: if (lines != 0 && options_get_number(oo, "status-position") == 0)
574: ctx->statustop = 1;
575: ctx->statuslines = lines;
576:
1.52 nicm 577: ctx->pane_status = options_get_number(wo, "pane-border-status");
1.78 nicm 578: ctx->pane_lines = options_get_number(wo, "pane-border-lines");
1.52 nicm 579:
1.55 nicm 580: tty_window_offset(&c->tty, &ctx->ox, &ctx->oy, &ctx->sx, &ctx->sy);
581:
582: log_debug("%s: %s @%u ox=%u oy=%u sx=%u sy=%u %u/%d", __func__, c->name,
1.61 nicm 583: w->id, ctx->ox, ctx->oy, ctx->sx, ctx->sy, ctx->statuslines,
584: ctx->statustop);
1.52 nicm 585: }
586:
1.4 nicm 587: /* Redraw entire screen. */
1.1 nicm 588: void
1.53 nicm 589: screen_redraw_screen(struct client *c)
1.1 nicm 590: {
1.53 nicm 591: struct screen_redraw_ctx ctx;
1.54 nicm 592: int flags;
1.18 nicm 593:
594: if (c->flags & CLIENT_SUSPENDED)
595: return;
1.1 nicm 596:
1.54 nicm 597: flags = screen_redraw_update(c, c->flags);
1.74 nicm 598: if ((flags & CLIENT_ALLREDRAWFLAGS) == 0)
599: return;
600:
1.52 nicm 601: screen_redraw_set_context(c, &ctx);
1.74 nicm 602: tty_update_mode(&c->tty, c->tty.mode, NULL);
1.67 nicm 603: tty_sync_start(&c->tty);
1.51 nicm 604:
1.54 nicm 605: if (flags & (CLIENT_REDRAWWINDOW|CLIENT_REDRAWBORDERS)) {
1.70 nicm 606: log_debug("%s: redrawing borders", c->name);
1.64 nicm 607: if (ctx.pane_status != PANE_STATUS_OFF)
1.53 nicm 608: screen_redraw_draw_pane_status(&ctx);
1.50 nicm 609: screen_redraw_draw_borders(&ctx);
1.53 nicm 610: }
1.70 nicm 611: if (flags & CLIENT_REDRAWWINDOW) {
612: log_debug("%s: redrawing panes", c->name);
1.50 nicm 613: screen_redraw_draw_panes(&ctx);
1.70 nicm 614: }
1.61 nicm 615: if (ctx.statuslines != 0 &&
1.70 nicm 616: (flags & (CLIENT_REDRAWSTATUS|CLIENT_REDRAWSTATUSALWAYS))) {
617: log_debug("%s: redrawing status", c->name);
1.50 nicm 618: screen_redraw_draw_status(&ctx);
1.70 nicm 619: }
620: if (c->overlay_draw != NULL && (flags & CLIENT_REDRAWOVERLAY)) {
621: log_debug("%s: redrawing overlay", c->name);
1.61 nicm 622: c->overlay_draw(c, &ctx);
1.70 nicm 623: }
1.67 nicm 624:
1.52 nicm 625: tty_reset(&c->tty);
1.26 nicm 626: }
1.4 nicm 627:
1.55 nicm 628: /* Redraw a single pane. */
1.26 nicm 629: void
630: screen_redraw_pane(struct client *c, struct window_pane *wp)
631: {
1.55 nicm 632: struct screen_redraw_ctx ctx;
1.26 nicm 633:
1.63 nicm 634: if (c->overlay_draw != NULL || !window_pane_visible(wp))
1.4 nicm 635: return;
1.1 nicm 636:
1.55 nicm 637: screen_redraw_set_context(c, &ctx);
1.74 nicm 638: tty_update_mode(&c->tty, c->tty.mode, NULL);
1.67 nicm 639: tty_sync_start(&c->tty);
1.26 nicm 640:
1.55 nicm 641: screen_redraw_draw_pane(&ctx, wp);
1.67 nicm 642:
1.26 nicm 643: tty_reset(&c->tty);
644: }
645:
1.75 nicm 646: /* Get border cell style. */
647: static const struct grid_cell *
648: screen_redraw_draw_borders_style(struct screen_redraw_ctx *ctx, u_int x,
649: u_int y, struct window_pane *wp)
650: {
651: struct client *c = ctx->c;
652: struct session *s = c->session;
653: struct window *w = s->curw->window;
1.77 nicm 654: struct window_pane *active = server_client_get_pane(c);
1.75 nicm 655: struct options *oo = w->options;
656: struct format_tree *ft;
657:
658: if (wp->border_gc_set)
659: return (&wp->border_gc);
660: wp->border_gc_set = 1;
661:
662: ft = format_create_defaults(NULL, c, s, s->curw, wp);
1.78 nicm 663: if (screen_redraw_check_is(x, y, ctx->pane_status, active))
664: style_apply(&wp->border_gc, oo, "pane-active-border-style", ft);
665: else
666: style_apply(&wp->border_gc, oo, "pane-border-style", ft);
667: format_free(ft);
1.75 nicm 668:
1.78 nicm 669: return (&wp->border_gc);
1.75 nicm 670: }
671:
1.50 nicm 672: /* Draw a border cell. */
673: static void
1.75 nicm 674: screen_redraw_draw_borders_cell(struct screen_redraw_ctx *ctx, u_int i, u_int j)
1.50 nicm 675: {
1.53 nicm 676: struct client *c = ctx->c;
677: struct session *s = c->session;
678: struct tty *tty = &c->tty;
679: struct window_pane *wp;
1.78 nicm 680: u_int cell_type, x = ctx->ox + i, y = ctx->oy + j;
1.75 nicm 681: int pane_status = ctx->pane_status;
1.78 nicm 682: struct grid_cell gc;
683: const struct grid_cell *tmp;
1.50 nicm 684:
1.66 nicm 685: if (c->overlay_check != NULL && !c->overlay_check(c, x, y))
686: return;
1.75 nicm 687:
1.78 nicm 688: cell_type = screen_redraw_check_cell(c, x, y, pane_status, &wp);
689: if (cell_type == CELL_INSIDE)
1.50 nicm 690: return;
691:
1.75 nicm 692: if (wp == NULL)
1.78 nicm 693: memcpy(&gc, &grid_default_cell, sizeof gc);
1.75 nicm 694: else {
1.78 nicm 695: tmp = screen_redraw_draw_borders_style(ctx, x, y, wp);
696: if (tmp == NULL)
1.75 nicm 697: return;
1.78 nicm 698: memcpy(&gc, tmp, sizeof gc);
1.75 nicm 699:
700: if (server_is_marked(s, s->curw, marked_pane.wp) &&
1.78 nicm 701: screen_redraw_check_is(x, y, pane_status, marked_pane.wp))
702: gc.attr ^= GRID_ATTR_REVERSE;
1.75 nicm 703: }
1.78 nicm 704: screen_redraw_border_set(wp, ctx->pane_lines, cell_type, &gc);
1.75 nicm 705:
1.61 nicm 706: if (ctx->statustop)
707: tty_cursor(tty, i, ctx->statuslines + j);
1.50 nicm 708: else
1.55 nicm 709: tty_cursor(tty, i, j);
1.78 nicm 710: tty_cell(tty, &gc, &grid_default_cell, NULL);
1.50 nicm 711: }
712:
1.26 nicm 713: /* Draw the borders. */
1.39 nicm 714: static void
1.50 nicm 715: screen_redraw_draw_borders(struct screen_redraw_ctx *ctx)
1.26 nicm 716: {
1.50 nicm 717: struct client *c = ctx->c;
1.32 nicm 718: struct session *s = c->session;
719: struct window *w = s->curw->window;
1.75 nicm 720: struct window_pane *wp;
1.55 nicm 721: u_int i, j;
722:
723: log_debug("%s: %s @%u", __func__, c->name, w->id);
1.26 nicm 724:
1.75 nicm 725: TAILQ_FOREACH(wp, &w->panes, entry)
726: wp->border_gc_set = 0;
727:
728: for (j = 0; j < c->tty.sy - ctx->statuslines; j++) {
729: for (i = 0; i < c->tty.sx; i++)
730: screen_redraw_draw_borders_cell(ctx, i, j);
1.28 nicm 731: }
1.26 nicm 732: }
1.1 nicm 733:
1.26 nicm 734: /* Draw the panes. */
1.39 nicm 735: static void
1.50 nicm 736: screen_redraw_draw_panes(struct screen_redraw_ctx *ctx)
1.26 nicm 737: {
1.50 nicm 738: struct client *c = ctx->c;
1.26 nicm 739: struct window *w = c->session->curw->window;
740: struct window_pane *wp;
1.47 nicm 741:
1.55 nicm 742: log_debug("%s: %s @%u", __func__, c->name, w->id);
743:
1.1 nicm 744: TAILQ_FOREACH(wp, &w->panes, entry) {
1.61 nicm 745: if (window_pane_visible(wp))
746: screen_redraw_draw_pane(ctx, wp);
1.1 nicm 747: }
748: }
749:
1.26 nicm 750: /* Draw the status line. */
1.39 nicm 751: static void
1.50 nicm 752: screen_redraw_draw_status(struct screen_redraw_ctx *ctx)
1.1 nicm 753: {
1.50 nicm 754: struct client *c = ctx->c;
1.55 nicm 755: struct window *w = c->session->curw->window;
1.26 nicm 756: struct tty *tty = &c->tty;
1.58 nicm 757: struct screen *s = c->status.active;
1.47 nicm 758: u_int i, y;
1.23 nicm 759:
1.55 nicm 760: log_debug("%s: %s @%u", __func__, c->name, w->id);
761:
1.61 nicm 762: if (ctx->statustop)
1.47 nicm 763: y = 0;
1.26 nicm 764: else
1.61 nicm 765: y = c->tty.sy - ctx->statuslines;
1.76 nicm 766: for (i = 0; i < ctx->statuslines; i++) {
767: tty_draw_line(tty, s, 0, i, UINT_MAX, 0, y + i,
768: &grid_default_cell, NULL);
769: }
1.55 nicm 770: }
771:
772: /* Draw one pane. */
773: static void
774: screen_redraw_draw_pane(struct screen_redraw_ctx *ctx, struct window_pane *wp)
775: {
776: struct client *c = ctx->c;
777: struct window *w = c->session->curw->window;
778: struct tty *tty = &c->tty;
779: struct screen *s;
1.76 nicm 780: struct grid_cell defaults;
1.55 nicm 781: u_int i, j, top, x, y, width;
782:
783: log_debug("%s: %s @%u %%%u", __func__, c->name, w->id, wp->id);
784:
785: if (wp->xoff + wp->sx <= ctx->ox || wp->xoff >= ctx->ox + ctx->sx)
786: return;
1.61 nicm 787: if (ctx->statustop)
788: top = ctx->statuslines;
1.55 nicm 789: else
790: top = 0;
791:
792: s = wp->screen;
793: for (j = 0; j < wp->sy; j++) {
794: if (wp->yoff + j < ctx->oy || wp->yoff + j >= ctx->oy + ctx->sy)
795: continue;
796: y = top + wp->yoff + j - ctx->oy;
797:
798: if (wp->xoff >= ctx->ox &&
799: wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
800: /* All visible. */
801: i = 0;
802: x = wp->xoff - ctx->ox;
803: width = wp->sx;
804: } else if (wp->xoff < ctx->ox &&
805: wp->xoff + wp->sx > ctx->ox + ctx->sx) {
806: /* Both left and right not visible. */
807: i = ctx->ox;
808: x = 0;
809: width = ctx->sx;
810: } else if (wp->xoff < ctx->ox) {
811: /* Left not visible. */
812: i = ctx->ox - wp->xoff;
813: x = 0;
814: width = wp->sx - i;
815: } else {
816: /* Right not visible. */
817: i = 0;
818: x = wp->xoff - ctx->ox;
819: width = ctx->sx - x;
820: }
821: log_debug("%s: %s %%%u line %u,%u at %u,%u, width %u",
822: __func__, c->name, wp->id, i, j, x, y, width);
823:
1.76 nicm 824: tty_default_colours(&defaults, wp);
825: tty_draw_line(tty, s, i, j, width, x, y, &defaults,
826: wp->palette);
1.55 nicm 827: }
1.1 nicm 828: }