Merge branch 'obsd-master'
[tmux.git] / screen-redraw.c
blob58383628bc9d2988330134490e8d3e384ebb137c
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
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 *);
29 static void screen_redraw_draw_pane(struct screen_redraw_ctx *,
30 struct window_pane *);
31 static void screen_redraw_set_context(struct client *,
32 struct screen_redraw_ctx *);
33 static void screen_redraw_draw_pane_scrollbars(struct screen_redraw_ctx *);
34 static void screen_redraw_draw_scrollbar(struct screen_redraw_ctx *,
35 struct window_pane *, int, int, int, u_int, u_int, u_int);
36 static void screen_redraw_draw_pane_scrollbar(struct screen_redraw_ctx *,
37 struct window_pane *);
39 #define START_ISOLATE "\342\201\246"
40 #define END_ISOLATE "\342\201\251"
42 /* Border in relation to a pane. */
43 enum screen_redraw_border_type {
44 SCREEN_REDRAW_OUTSIDE,
45 SCREEN_REDRAW_INSIDE,
46 SCREEN_REDRAW_BORDER_LEFT,
47 SCREEN_REDRAW_BORDER_RIGHT,
48 SCREEN_REDRAW_BORDER_TOP,
49 SCREEN_REDRAW_BORDER_BOTTOM
51 #define BORDER_MARKERS " +,.-"
53 /* Get cell border character. */
54 static void
55 screen_redraw_border_set(struct window *w, struct window_pane *wp,
56 enum pane_lines pane_lines, int cell_type, struct grid_cell *gc)
58 u_int idx;
60 if (cell_type == CELL_OUTSIDE && w->fill_character != NULL) {
61 utf8_copy(&gc->data, &w->fill_character[0]);
62 return;
65 switch (pane_lines) {
66 case PANE_LINES_NUMBER:
67 if (cell_type == CELL_OUTSIDE) {
68 gc->attr |= GRID_ATTR_CHARSET;
69 utf8_set(&gc->data, CELL_BORDERS[CELL_OUTSIDE]);
70 break;
72 gc->attr &= ~GRID_ATTR_CHARSET;
73 if (wp != NULL && window_pane_index(wp, &idx) == 0)
74 utf8_set(&gc->data, '0' + (idx % 10));
75 else
76 utf8_set(&gc->data, '*');
77 break;
78 case PANE_LINES_DOUBLE:
79 gc->attr &= ~GRID_ATTR_CHARSET;
80 utf8_copy(&gc->data, tty_acs_double_borders(cell_type));
81 break;
82 case PANE_LINES_HEAVY:
83 gc->attr &= ~GRID_ATTR_CHARSET;
84 utf8_copy(&gc->data, tty_acs_heavy_borders(cell_type));
85 break;
86 case PANE_LINES_SIMPLE:
87 gc->attr &= ~GRID_ATTR_CHARSET;
88 utf8_set(&gc->data, SIMPLE_BORDERS[cell_type]);
89 break;
90 default:
91 gc->attr |= GRID_ATTR_CHARSET;
92 utf8_set(&gc->data, CELL_BORDERS[cell_type]);
93 break;
97 /* Return if window has only two panes. */
98 static int
99 screen_redraw_two_panes(struct window *w, int direction)
101 struct window_pane *wp;
103 wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry);
104 if (wp == NULL)
105 return (0); /* one pane */
106 if (TAILQ_NEXT(wp, entry) != NULL)
107 return (0); /* more than two panes */
108 if (direction == 0 && wp->xoff == 0)
109 return (0);
110 if (direction == 1 && wp->yoff == 0)
111 return (0);
112 return (1);
115 /* Check if cell is on the border of a pane. */
116 static enum screen_redraw_border_type
117 screen_redraw_pane_border(struct screen_redraw_ctx *ctx, struct window_pane *wp,
118 u_int px, u_int py)
120 struct options *oo = wp->window->options;
121 u_int ex = wp->xoff + wp->sx, ey = wp->yoff + wp->sy;
122 int hsplit = 0, vsplit = 0, pane_status = ctx->pane_status;
123 int pane_scrollbars = ctx->pane_scrollbars, sb_w = 0;
124 int sb_pos = ctx->pane_scrollbars_pos;
126 /* Inside pane. */
127 if (px >= wp->xoff && px < ex && py >= wp->yoff && py < ey)
128 return (SCREEN_REDRAW_INSIDE);
130 /* Get pane indicator. */
131 switch (options_get_number(oo, "pane-border-indicators")) {
132 case PANE_BORDER_COLOUR:
133 case PANE_BORDER_BOTH:
134 hsplit = screen_redraw_two_panes(wp->window, 0);
135 vsplit = screen_redraw_two_panes(wp->window, 1);
136 break;
139 /* Are scrollbars enabled? */
140 if (window_pane_show_scrollbar(wp, pane_scrollbars))
141 sb_w = PANE_SCROLLBARS_WIDTH;
144 * Left/right borders. The wp->sy / 2 test is to colour only half the
145 * active window's border when there are two panes.
147 if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= ey) {
148 if (sb_pos == PANE_SCROLLBARS_LEFT) {
149 if (wp->xoff - sb_w == 0 && px == wp->sx + sb_w)
150 if (!hsplit || (hsplit && py <= wp->sy / 2))
151 return (SCREEN_REDRAW_BORDER_RIGHT);
152 if (wp->xoff - sb_w != 0 && px == wp->xoff - sb_w - 1)
153 if (!hsplit || (hsplit && py > wp->sy / 2))
154 return (SCREEN_REDRAW_BORDER_LEFT);
155 } else { /* sb_pos == PANE_SCROLLBARS_RIGHT */
156 if (wp->xoff == 0 && px == wp->sx + sb_w)
157 if (!hsplit || (hsplit && py <= wp->sy / 2))
158 return (SCREEN_REDRAW_BORDER_RIGHT);
159 if (wp->xoff != 0 && px == wp->xoff - 1)
160 if (!hsplit || (hsplit && py > wp->sy / 2))
161 return (SCREEN_REDRAW_BORDER_LEFT);
165 /* Top/bottom borders. */
166 if (vsplit && pane_status == PANE_STATUS_OFF && sb_w == 0) {
167 if (wp->yoff == 0 && py == wp->sy && px <= wp->sx / 2)
168 return (SCREEN_REDRAW_BORDER_BOTTOM);
169 if (wp->yoff != 0 && py == wp->yoff - 1 && px > wp->sx / 2)
170 return (SCREEN_REDRAW_BORDER_TOP);
171 } else {
172 if (sb_pos == PANE_SCROLLBARS_LEFT) {
173 if ((wp->xoff - sb_w == 0 || px >= wp->xoff - sb_w) &&
174 (px <= ex || (sb_w != 0 && px - 1 == ex))) {
175 if (wp->yoff != 0 && py == wp->yoff - 1)
176 return (SCREEN_REDRAW_BORDER_TOP);
177 if (py == ey)
178 return (SCREEN_REDRAW_BORDER_BOTTOM);
180 } else { /* sb_pos == PANE_SCROLLBARS_RIGHT */
181 if ((wp->xoff == 0 || px >= wp->xoff) &&
182 (px <= ex || (sb_w != 0 && px - 1 == ex))) {
183 if (wp->yoff != 0 && py == wp->yoff - 1)
184 return (SCREEN_REDRAW_BORDER_TOP);
185 if (py == ey)
186 return (SCREEN_REDRAW_BORDER_BOTTOM);
191 /* Outside pane. */
192 return (SCREEN_REDRAW_OUTSIDE);
195 /* Check if a cell is on a border. */
196 static int
197 screen_redraw_cell_border(struct screen_redraw_ctx *ctx, u_int px, u_int py)
199 struct client *c = ctx->c;
200 struct window *w = c->session->curw->window;
201 struct window_pane *wp;
202 u_int sy = w->sy;
204 if (ctx->pane_status == PANE_STATUS_BOTTOM)
205 sy--;
207 /* Outside the window? */
208 if (px > w->sx || py > sy)
209 return (0);
211 /* On the window border? */
212 if (px == w->sx || py == sy)
213 return (1);
215 /* Check all the panes. */
216 TAILQ_FOREACH(wp, &w->panes, entry) {
217 if (!window_pane_visible(wp))
218 continue;
219 switch (screen_redraw_pane_border(ctx, wp, px, py)) {
220 case SCREEN_REDRAW_INSIDE:
221 return (0);
222 case SCREEN_REDRAW_OUTSIDE:
223 break;
224 default:
225 return (1);
229 return (0);
232 /* Work out type of border cell from surrounding cells. */
233 static int
234 screen_redraw_type_of_cell(struct screen_redraw_ctx *ctx, u_int px, u_int py)
236 struct client *c = ctx->c;
237 int pane_status = ctx->pane_status;
238 struct window *w = c->session->curw->window;
239 u_int sx = w->sx, sy = w->sy;
240 int borders = 0;
242 if (pane_status == PANE_STATUS_BOTTOM)
243 sy--;
245 /* Is this outside the window? */
246 if (px > sx || py > sy)
247 return (CELL_OUTSIDE);
250 * Construct a bitmask of whether the cells to the left (bit 8), right,
251 * top, and bottom (bit 1) of this cell are borders.
253 * bits 8 4 2 1: 2
254 * 8 + 4
257 if (px == 0 || screen_redraw_cell_border(ctx, px - 1, py))
258 borders |= 8;
259 if (px <= sx && screen_redraw_cell_border(ctx, px + 1, py))
260 borders |= 4;
261 if (pane_status == PANE_STATUS_TOP) {
262 if (py != 0 &&
263 screen_redraw_cell_border(ctx, px, py - 1))
264 borders |= 2;
265 if (screen_redraw_cell_border(ctx, px, py + 1))
266 borders |= 1;
267 } else if (pane_status == PANE_STATUS_BOTTOM) {
268 if (py == 0 ||
269 screen_redraw_cell_border(ctx, px, py - 1))
270 borders |= 2;
271 if (py != sy &&
272 screen_redraw_cell_border(ctx, px, py + 1))
273 borders |= 1;
274 } else {
275 if (py == 0 ||
276 screen_redraw_cell_border(ctx, px, py - 1))
277 borders |= 2;
278 if (screen_redraw_cell_border(ctx, px, py + 1))
279 borders |= 1;
283 * Figure out what kind of border this cell is. Only one bit set
284 * doesn't make sense (can't have a border cell with no others
285 * connected).
287 switch (borders) {
288 case 15: /* 1111, left right top bottom */
289 return (CELL_JOIN);
290 case 14: /* 1110, left right top */
291 return (CELL_BOTTOMJOIN);
292 case 13: /* 1101, left right bottom */
293 return (CELL_TOPJOIN);
294 case 12: /* 1100, left right */
295 return (CELL_LEFTRIGHT);
296 case 11: /* 1011, left top bottom */
297 return (CELL_RIGHTJOIN);
298 case 10: /* 1010, left top */
299 return (CELL_BOTTOMRIGHT);
300 case 9: /* 1001, left bottom */
301 return (CELL_TOPRIGHT);
302 case 7: /* 0111, right top bottom */
303 return (CELL_LEFTJOIN);
304 case 6: /* 0110, right top */
305 return (CELL_BOTTOMLEFT);
306 case 5: /* 0101, right bottom */
307 return (CELL_TOPLEFT);
308 case 3: /* 0011, top bottom */
309 return (CELL_TOPBOTTOM);
311 return (CELL_OUTSIDE);
314 /* Check if cell inside a pane. */
315 static int
316 screen_redraw_check_cell(struct screen_redraw_ctx *ctx, u_int px, u_int py,
317 struct window_pane **wpp)
319 struct client *c = ctx->c;
320 struct window *w = c->session->curw->window;
321 struct window_pane *wp, *active;
322 int pane_status = ctx->pane_status;
323 u_int sx = w->sx, sy = w->sy;
324 int border, pane_scrollbars = ctx->pane_scrollbars;
325 u_int right, line;
326 int sb_pos = ctx->pane_scrollbars_pos;
327 int sb_w = PANE_SCROLLBARS_WIDTH;
329 *wpp = NULL;
331 if (px > sx || py > sy)
332 return (CELL_OUTSIDE);
333 if (px == sx || py == sy) /* window border */
334 return (screen_redraw_type_of_cell(ctx, px, py));
336 if (pane_status != PANE_STATUS_OFF) {
337 active = wp = server_client_get_pane(c);
338 do {
339 if (!window_pane_visible(wp))
340 goto next1;
342 if (pane_status == PANE_STATUS_TOP)
343 line = wp->yoff - 1;
344 else
345 line = wp->yoff + sy;
346 right = wp->xoff + 2 + wp->status_size - 1;
348 if (py == line && px >= wp->xoff + 2 && px <= right)
349 return (CELL_INSIDE);
351 next1:
352 wp = TAILQ_NEXT(wp, entry);
353 if (wp == NULL)
354 wp = TAILQ_FIRST(&w->panes);
355 } while (wp != active);
358 active = wp = server_client_get_pane(c);
359 do {
360 if (!window_pane_visible(wp))
361 goto next2;
362 *wpp = wp;
364 /* Check if CELL_SCROLLBAR */
365 if (window_pane_show_scrollbar(wp, pane_scrollbars)) {
367 if (pane_status == PANE_STATUS_TOP)
368 line = wp->yoff - 1;
369 else
370 line = wp->yoff + wp->sy;
373 * Check if py could lie within a scrollbar. If the
374 * pane is at the top then py == 0 to sy; if the pane
375 * is not at the top, then yoff to yoff + sy.
377 if ((pane_status && py != line) ||
378 (wp->yoff == 0 && py < wp->sy) ||
379 (py >= wp->yoff && py < wp->yoff + wp->sy)) {
380 /* Check if px lies within a scrollbar. */
381 if ((sb_pos == PANE_SCROLLBARS_RIGHT &&
382 (px >= wp->xoff + wp->sx &&
383 px < wp->xoff + wp->sx + sb_w)) ||
384 (sb_pos == PANE_SCROLLBARS_LEFT &&
385 (px >= wp->xoff - sb_w &&
386 px < wp->xoff)))
387 return (CELL_SCROLLBAR);
392 * If definitely inside, return. If not on border, skip.
393 * Otherwise work out the cell.
395 border = screen_redraw_pane_border(ctx, wp, px, py);
396 if (border == SCREEN_REDRAW_INSIDE)
397 return (CELL_INSIDE);
398 if (border == SCREEN_REDRAW_OUTSIDE)
399 goto next2;
400 return (screen_redraw_type_of_cell(ctx, px, py));
402 next2:
403 wp = TAILQ_NEXT(wp, entry);
404 if (wp == NULL)
405 wp = TAILQ_FIRST(&w->panes);
406 } while (wp != active);
408 return (CELL_OUTSIDE);
411 /* Check if the border of a particular pane. */
412 static int
413 screen_redraw_check_is(struct screen_redraw_ctx *ctx, u_int px, u_int py,
414 struct window_pane *wp)
416 enum screen_redraw_border_type border;
418 border = screen_redraw_pane_border(ctx, wp, px, py);
419 if (border != SCREEN_REDRAW_INSIDE && border != SCREEN_REDRAW_OUTSIDE)
420 return (1);
421 return (0);
424 /* Update pane status. */
425 static int
426 screen_redraw_make_pane_status(struct client *c, struct window_pane *wp,
427 struct screen_redraw_ctx *rctx, enum pane_lines pane_lines)
429 struct window *w = wp->window;
430 struct grid_cell gc;
431 const char *fmt;
432 struct format_tree *ft;
433 char *expanded;
434 int pane_status = rctx->pane_status;
435 u_int width, i, cell_type, px, py;
436 struct screen_write_ctx ctx;
437 struct screen old;
439 ft = format_create(c, NULL, FORMAT_PANE|wp->id, FORMAT_STATUS);
440 format_defaults(ft, c, c->session, c->session->curw, wp);
442 if (wp == server_client_get_pane(c))
443 style_apply(&gc, w->options, "pane-active-border-style", ft);
444 else
445 style_apply(&gc, w->options, "pane-border-style", ft);
446 fmt = options_get_string(wp->options, "pane-border-format");
448 expanded = format_expand_time(ft, fmt);
449 if (wp->sx < 4)
450 wp->status_size = width = 0;
451 else
452 wp->status_size = width = wp->sx - 4;
454 memcpy(&old, &wp->status_screen, sizeof old);
455 screen_init(&wp->status_screen, width, 1, 0);
456 wp->status_screen.mode = 0;
458 screen_write_start(&ctx, &wp->status_screen);
460 for (i = 0; i < width; i++) {
461 px = wp->xoff + 2 + i;
462 if (pane_status == PANE_STATUS_TOP)
463 py = wp->yoff - 1;
464 else
465 py = wp->yoff + wp->sy;
466 cell_type = screen_redraw_type_of_cell(rctx, px, py);
467 screen_redraw_border_set(w, wp, pane_lines, cell_type, &gc);
468 screen_write_cell(&ctx, &gc);
470 gc.attr &= ~GRID_ATTR_CHARSET;
472 screen_write_cursormove(&ctx, 0, 0, 0);
473 format_draw(&ctx, &gc, width, expanded, NULL, 0);
474 screen_write_stop(&ctx);
476 free(expanded);
477 format_free(ft);
479 if (grid_compare(wp->status_screen.grid, old.grid) == 0) {
480 screen_free(&old);
481 return (0);
483 screen_free(&old);
484 return (1);
487 /* Draw pane status. */
488 static void
489 screen_redraw_draw_pane_status(struct screen_redraw_ctx *ctx)
491 struct client *c = ctx->c;
492 struct window *w = c->session->curw->window;
493 struct tty *tty = &c->tty;
494 struct window_pane *wp;
495 struct screen *s;
496 u_int i, x, width, xoff, yoff, size;
498 log_debug("%s: %s @%u", __func__, c->name, w->id);
500 TAILQ_FOREACH(wp, &w->panes, entry) {
501 if (!window_pane_visible(wp))
502 continue;
503 s = &wp->status_screen;
505 size = wp->status_size;
506 if (ctx->pane_status == PANE_STATUS_TOP)
507 yoff = wp->yoff - 1;
508 else
509 yoff = wp->yoff + wp->sy;
510 xoff = wp->xoff + 2;
512 if (xoff + size <= ctx->ox ||
513 xoff >= ctx->ox + ctx->sx ||
514 yoff < ctx->oy ||
515 yoff >= ctx->oy + ctx->sy)
516 continue;
518 if (xoff >= ctx->ox && xoff + size <= ctx->ox + ctx->sx) {
519 /* All visible. */
520 i = 0;
521 x = xoff - ctx->ox;
522 width = size;
523 } else if (xoff < ctx->ox && xoff + size > ctx->ox + ctx->sx) {
524 /* Both left and right not visible. */
525 i = ctx->ox;
526 x = 0;
527 width = ctx->sx;
528 } else if (xoff < ctx->ox) {
529 /* Left not visible. */
530 i = ctx->ox - xoff;
531 x = 0;
532 width = size - i;
533 } else {
534 /* Right not visible. */
535 i = 0;
536 x = xoff - ctx->ox;
537 width = size - x;
540 if (ctx->statustop)
541 yoff += ctx->statuslines;
542 tty_draw_line(tty, s, i, 0, width, x, yoff - ctx->oy,
543 &grid_default_cell, NULL);
545 tty_cursor(tty, 0, 0);
548 /* Update status line and change flags if unchanged. */
549 static uint64_t
550 screen_redraw_update(struct screen_redraw_ctx *ctx, uint64_t flags)
552 struct client *c = ctx->c;
553 struct window *w = c->session->curw->window;
554 struct window_pane *wp;
555 int redraw;
556 enum pane_lines lines;
558 if (c->message_string != NULL)
559 redraw = status_message_redraw(c);
560 else if (c->prompt_string != NULL)
561 redraw = status_prompt_redraw(c);
562 else
563 redraw = status_redraw(c);
564 if (!redraw && (~flags & CLIENT_REDRAWSTATUSALWAYS))
565 flags &= ~CLIENT_REDRAWSTATUS;
567 if (c->overlay_draw != NULL)
568 flags |= CLIENT_REDRAWOVERLAY;
570 if (ctx->pane_status != PANE_STATUS_OFF) {
571 lines = ctx->pane_lines;
572 redraw = 0;
573 TAILQ_FOREACH(wp, &w->panes, entry) {
574 if (screen_redraw_make_pane_status(c, wp, ctx, lines))
575 redraw = 1;
577 if (redraw)
578 flags |= CLIENT_REDRAWBORDERS;
581 return (flags);
584 /* Set up redraw context. */
585 static void
586 screen_redraw_set_context(struct client *c, struct screen_redraw_ctx *ctx)
588 struct session *s = c->session;
589 struct options *oo = s->options;
590 struct window *w = s->curw->window;
591 struct options *wo = w->options;
592 u_int lines;
594 memset(ctx, 0, sizeof *ctx);
595 ctx->c = c;
597 lines = status_line_size(c);
598 if (c->message_string != NULL || c->prompt_string != NULL)
599 lines = (lines == 0) ? 1 : lines;
600 if (lines != 0 && options_get_number(oo, "status-position") == 0)
601 ctx->statustop = 1;
602 ctx->statuslines = lines;
604 ctx->pane_status = options_get_number(wo, "pane-border-status");
605 ctx->pane_lines = options_get_number(wo, "pane-border-lines");
607 ctx->pane_scrollbars = options_get_number(wo, "pane-scrollbars");
608 ctx->pane_scrollbars_pos = options_get_number(wo,
609 "pane-scrollbars-position");
611 tty_window_offset(&c->tty, &ctx->ox, &ctx->oy, &ctx->sx, &ctx->sy);
613 log_debug("%s: %s @%u ox=%u oy=%u sx=%u sy=%u %u/%d", __func__, c->name,
614 w->id, ctx->ox, ctx->oy, ctx->sx, ctx->sy, ctx->statuslines,
615 ctx->statustop);
618 /* Redraw entire screen. */
619 void
620 screen_redraw_screen(struct client *c)
622 struct screen_redraw_ctx ctx;
623 uint64_t flags;
625 if (c->flags & CLIENT_SUSPENDED)
626 return;
628 screen_redraw_set_context(c, &ctx);
630 flags = screen_redraw_update(&ctx, c->flags);
631 if ((flags & CLIENT_ALLREDRAWFLAGS) == 0)
632 return;
634 tty_sync_start(&c->tty);
635 tty_update_mode(&c->tty, c->tty.mode, NULL);
637 if (flags & (CLIENT_REDRAWWINDOW|CLIENT_REDRAWBORDERS)) {
638 log_debug("%s: redrawing borders", c->name);
639 screen_redraw_draw_borders(&ctx);
640 if (ctx.pane_status != PANE_STATUS_OFF)
641 screen_redraw_draw_pane_status(&ctx);
642 screen_redraw_draw_pane_scrollbars(&ctx);
644 if (flags & CLIENT_REDRAWWINDOW) {
645 log_debug("%s: redrawing panes", c->name);
646 screen_redraw_draw_panes(&ctx);
647 screen_redraw_draw_pane_scrollbars(&ctx);
649 if (ctx.statuslines != 0 &&
650 (flags & (CLIENT_REDRAWSTATUS|CLIENT_REDRAWSTATUSALWAYS))) {
651 log_debug("%s: redrawing status", c->name);
652 screen_redraw_draw_status(&ctx);
654 if (c->overlay_draw != NULL && (flags & CLIENT_REDRAWOVERLAY)) {
655 log_debug("%s: redrawing overlay", c->name);
656 c->overlay_draw(c, c->overlay_data, &ctx);
659 tty_reset(&c->tty);
662 /* Redraw a single pane and its scrollbar. */
663 void
664 screen_redraw_pane(struct client *c, struct window_pane *wp,
665 int redraw_scrollbar_only)
667 struct screen_redraw_ctx ctx;
669 if (!window_pane_visible(wp))
670 return;
672 screen_redraw_set_context(c, &ctx);
673 tty_sync_start(&c->tty);
674 tty_update_mode(&c->tty, c->tty.mode, NULL);
676 if (!redraw_scrollbar_only)
677 screen_redraw_draw_pane(&ctx, wp);
679 if (window_pane_show_scrollbar(wp, ctx.pane_scrollbars))
680 screen_redraw_draw_pane_scrollbar(&ctx, wp);
682 tty_reset(&c->tty);
685 /* Get border cell style. */
686 static const struct grid_cell *
687 screen_redraw_draw_borders_style(struct screen_redraw_ctx *ctx, u_int x,
688 u_int y, struct window_pane *wp)
690 struct client *c = ctx->c;
691 struct session *s = c->session;
692 struct window *w = s->curw->window;
693 struct window_pane *active = server_client_get_pane(c);
694 struct options *oo = w->options;
695 struct format_tree *ft;
697 if (wp->border_gc_set)
698 return (&wp->border_gc);
699 wp->border_gc_set = 1;
701 ft = format_create_defaults(NULL, c, s, s->curw, wp);
702 if (screen_redraw_check_is(ctx, x, y, active))
703 style_apply(&wp->border_gc, oo, "pane-active-border-style", ft);
704 else
705 style_apply(&wp->border_gc, oo, "pane-border-style", ft);
706 format_free(ft);
708 return (&wp->border_gc);
711 /* Draw a border cell. */
712 static void
713 screen_redraw_draw_borders_cell(struct screen_redraw_ctx *ctx, u_int i, u_int j)
715 struct client *c = ctx->c;
716 struct session *s = c->session;
717 struct window *w = s->curw->window;
718 struct options *oo = w->options;
719 struct tty *tty = &c->tty;
720 struct format_tree *ft;
721 struct window_pane *wp, *active = server_client_get_pane(c);
722 struct grid_cell gc;
723 const struct grid_cell *tmp;
724 struct overlay_ranges r;
725 u_int cell_type, x = ctx->ox + i, y = ctx->oy + j;
726 int arrows = 0, border, isolates;
728 if (c->overlay_check != NULL) {
729 c->overlay_check(c, c->overlay_data, x, y, 1, &r);
730 if (r.nx[0] + r.nx[1] == 0)
731 return;
734 cell_type = screen_redraw_check_cell(ctx, x, y, &wp);
735 if (cell_type == CELL_INSIDE || cell_type == CELL_SCROLLBAR)
736 return;
738 if (wp == NULL) {
739 if (!ctx->no_pane_gc_set) {
740 ft = format_create_defaults(NULL, c, s, s->curw, NULL);
741 memcpy(&ctx->no_pane_gc, &grid_default_cell, sizeof gc);
742 style_add(&ctx->no_pane_gc, oo, "pane-border-style",
743 ft);
744 format_free(ft);
745 ctx->no_pane_gc_set = 1;
747 memcpy(&gc, &ctx->no_pane_gc, sizeof gc);
748 } else {
749 tmp = screen_redraw_draw_borders_style(ctx, x, y, wp);
750 if (tmp == NULL)
751 return;
752 memcpy(&gc, tmp, sizeof gc);
754 if (server_is_marked(s, s->curw, marked_pane.wp) &&
755 screen_redraw_check_is(ctx, x, y, marked_pane.wp))
756 gc.attr ^= GRID_ATTR_REVERSE;
758 screen_redraw_border_set(w, wp, ctx->pane_lines, cell_type, &gc);
760 if (cell_type == CELL_TOPBOTTOM &&
761 (c->flags & CLIENT_UTF8) &&
762 tty_term_has(tty->term, TTYC_BIDI))
763 isolates = 1;
764 else
765 isolates = 0;
767 if (ctx->statustop)
768 tty_cursor(tty, i, ctx->statuslines + j);
769 else
770 tty_cursor(tty, i, j);
771 if (isolates)
772 tty_puts(tty, END_ISOLATE);
774 switch (options_get_number(oo, "pane-border-indicators")) {
775 case PANE_BORDER_ARROWS:
776 case PANE_BORDER_BOTH:
777 arrows = 1;
778 break;
781 if (wp != NULL && arrows) {
782 border = screen_redraw_pane_border(ctx, active, x, y);
783 if (((i == wp->xoff + 1 &&
784 (cell_type == CELL_LEFTRIGHT ||
785 (cell_type == CELL_TOPJOIN &&
786 border == SCREEN_REDRAW_BORDER_BOTTOM) ||
787 (cell_type == CELL_BOTTOMJOIN &&
788 border == SCREEN_REDRAW_BORDER_TOP))) ||
789 (j == wp->yoff + 1 &&
790 (cell_type == CELL_TOPBOTTOM ||
791 (cell_type == CELL_LEFTJOIN &&
792 border == SCREEN_REDRAW_BORDER_RIGHT) ||
793 (cell_type == CELL_RIGHTJOIN &&
794 border == SCREEN_REDRAW_BORDER_LEFT)))) &&
795 screen_redraw_check_is(ctx, x, y, active)) {
796 gc.attr |= GRID_ATTR_CHARSET;
797 utf8_set(&gc.data, BORDER_MARKERS[border]);
801 tty_cell(tty, &gc, &grid_default_cell, NULL, NULL);
802 if (isolates)
803 tty_puts(tty, START_ISOLATE);
806 /* Draw the borders. */
807 static void
808 screen_redraw_draw_borders(struct screen_redraw_ctx *ctx)
810 struct client *c = ctx->c;
811 struct session *s = c->session;
812 struct window *w = s->curw->window;
813 struct window_pane *wp;
814 u_int i, j;
816 log_debug("%s: %s @%u", __func__, c->name, w->id);
818 TAILQ_FOREACH(wp, &w->panes, entry)
819 wp->border_gc_set = 0;
821 for (j = 0; j < c->tty.sy - ctx->statuslines; j++) {
822 for (i = 0; i < c->tty.sx; i++)
823 screen_redraw_draw_borders_cell(ctx, i, j);
827 /* Draw the panes. */
828 static void
829 screen_redraw_draw_panes(struct screen_redraw_ctx *ctx)
831 struct client *c = ctx->c;
832 struct window *w = c->session->curw->window;
833 struct window_pane *wp;
835 log_debug("%s: %s @%u", __func__, c->name, w->id);
837 TAILQ_FOREACH(wp, &w->panes, entry) {
838 if (window_pane_visible(wp))
839 screen_redraw_draw_pane(ctx, wp);
843 /* Draw the status line. */
844 static void
845 screen_redraw_draw_status(struct screen_redraw_ctx *ctx)
847 struct client *c = ctx->c;
848 struct window *w = c->session->curw->window;
849 struct tty *tty = &c->tty;
850 struct screen *s = c->status.active;
851 u_int i, y;
853 log_debug("%s: %s @%u", __func__, c->name, w->id);
855 if (ctx->statustop)
856 y = 0;
857 else
858 y = c->tty.sy - ctx->statuslines;
859 for (i = 0; i < ctx->statuslines; i++) {
860 tty_draw_line(tty, s, 0, i, UINT_MAX, 0, y + i,
861 &grid_default_cell, NULL);
865 /* Draw one pane. */
866 static void
867 screen_redraw_draw_pane(struct screen_redraw_ctx *ctx, struct window_pane *wp)
869 struct client *c = ctx->c;
870 struct window *w = c->session->curw->window;
871 struct tty *tty = &c->tty;
872 struct screen *s = wp->screen;
873 struct colour_palette *palette = &wp->palette;
874 struct grid_cell defaults;
875 u_int i, j, top, x, y, width;
877 log_debug("%s: %s @%u %%%u", __func__, c->name, w->id, wp->id);
879 if (wp->xoff + wp->sx <= ctx->ox || wp->xoff >= ctx->ox + ctx->sx)
880 return;
881 if (ctx->statustop)
882 top = ctx->statuslines;
883 else
884 top = 0;
885 for (j = 0; j < wp->sy; j++) {
886 if (wp->yoff + j < ctx->oy || wp->yoff + j >= ctx->oy + ctx->sy)
887 continue;
888 y = top + wp->yoff + j - ctx->oy;
890 if (wp->xoff >= ctx->ox &&
891 wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
892 /* All visible. */
893 i = 0;
894 x = wp->xoff - ctx->ox;
895 width = wp->sx;
896 } else if (wp->xoff < ctx->ox &&
897 wp->xoff + wp->sx > ctx->ox + ctx->sx) {
898 /* Both left and right not visible. */
899 i = ctx->ox;
900 x = 0;
901 width = ctx->sx;
902 } else if (wp->xoff < ctx->ox) {
903 /* Left not visible. */
904 i = ctx->ox - wp->xoff;
905 x = 0;
906 width = wp->sx - i;
907 } else {
908 /* Right not visible. */
909 i = 0;
910 x = wp->xoff - ctx->ox;
911 width = ctx->sx - x;
913 log_debug("%s: %s %%%u line %u,%u at %u,%u, width %u",
914 __func__, c->name, wp->id, i, j, x, y, width);
916 tty_default_colours(&defaults, wp);
917 tty_draw_line(tty, s, i, j, width, x, y, &defaults, palette);
920 #ifdef ENABLE_SIXEL
921 tty_draw_images(c, wp, s);
922 #endif
925 /* Draw the panes scrollbars */
926 static void
927 screen_redraw_draw_pane_scrollbars(struct screen_redraw_ctx *ctx)
929 struct client *c = ctx->c;
930 struct window *w = c->session->curw->window;
931 struct window_pane *wp;
933 log_debug("%s: %s @%u", __func__, c->name, w->id);
935 TAILQ_FOREACH(wp, &w->panes, entry) {
936 if (window_pane_show_scrollbar(wp, ctx->pane_scrollbars) &&
937 window_pane_visible(wp))
938 screen_redraw_draw_pane_scrollbar(ctx, wp);
942 /* Draw pane scrollbar. */
943 void
944 screen_redraw_draw_pane_scrollbar(struct screen_redraw_ctx *ctx,
945 struct window_pane *wp)
947 struct screen *s = wp->screen;
948 double percent_view;
949 u_int sb = ctx->pane_scrollbars, total_height, sb_h = wp->sy;
950 u_int sb_pos = ctx->pane_scrollbars_pos, slider_h, slider_y;
951 u_int sb_w = PANE_SCROLLBARS_WIDTH, cm_y, cm_size;
952 int sb_x, sb_y = (int)(wp->yoff - ctx->oy); /* sb top */
954 if (window_pane_mode(wp) == WINDOW_PANE_NO_MODE) {
955 if (sb == PANE_SCROLLBARS_MODAL)
956 return;
957 /* Show slider at the bottom of the scrollbar. */
958 total_height = screen_size_y(s) + screen_hsize(s);
959 percent_view = (double)sb_h / total_height;
960 slider_h = (double)sb_h * percent_view;
961 slider_y = sb_h - slider_h;
962 } else {
963 if (TAILQ_FIRST(&wp->modes) == NULL)
964 return;
965 if (window_copy_get_current_offset(wp, &cm_y, &cm_size) == 0)
966 return;
967 total_height = cm_size + sb_h;
968 percent_view = (double)sb_h / (cm_size + sb_h);
969 slider_h = (double)sb_h * percent_view;
970 slider_y = (sb_h + 1) * ((double)cm_y / total_height);
973 if (sb_pos == PANE_SCROLLBARS_LEFT)
974 sb_x = (int)wp->xoff - sb_w - ctx->ox;
975 else
976 sb_x = (int)wp->xoff + wp->sx - ctx->ox;
978 if (slider_h < 1)
979 slider_h = 1;
980 if (slider_y >= sb_h)
981 slider_y = sb_h - 1;
983 screen_redraw_draw_scrollbar(ctx, wp, sb_pos, sb_x, sb_y, sb_h,
984 slider_h, slider_y);
986 /* Store current position and height of the slider */
987 wp->sb_slider_y = slider_y; /* top of slider y pos in scrollbar */
988 wp->sb_slider_h = slider_h; /* height of slider */
991 static void
992 screen_redraw_draw_scrollbar(struct screen_redraw_ctx *ctx,
993 struct window_pane *wp, int sb_pos, int sb_x, int sb_y, u_int sb_h,
994 u_int slider_h, u_int slider_y)
996 struct client *c = ctx->c;
997 struct window *w = wp->window;
998 struct tty *tty = &c->tty;
999 struct grid_cell gc, slgc, *gcp;
1000 u_int i, j, sb_w = PANE_SCROLLBARS_WIDTH;
1001 u_int pad_col = 0;
1002 int px, py, ox = ctx->ox, oy = ctx->oy;
1003 int sb_pad = PANE_SCROLLBARS_PADDING, sx = ctx->sx;
1004 int sy = ctx->sy, xoff = wp->xoff, yoff = wp->yoff;
1006 /* Set up default style. */
1007 style_apply(&gc, w->options, "pane-scrollbars-style", NULL);
1008 utf8_set(&gc.data, ' ');
1010 /* Set up style for slider. */
1011 memcpy(&slgc, &gc, sizeof slgc);
1012 slgc.bg = gc.fg;
1014 if (sb_pad != 0) {
1015 if (sb_pos == PANE_SCROLLBARS_RIGHT)
1016 pad_col = 0;
1017 else
1018 pad_col = sb_w - 1;
1021 for (i = 0; i < sb_w; i++) {
1022 for (j = 0; j < sb_h; j++) {
1023 px = sb_x + i;
1024 py = sb_y + j;
1025 if (px < xoff - ox - 1 || px >= sx || px < 0 ||
1026 py < yoff - oy - 1 || py >= sy || py < 0)
1027 continue;
1028 tty_cursor(tty, px, py);
1029 if (sb_pad && i == pad_col) {
1030 tty_cell(tty, &grid_default_cell,
1031 &grid_default_cell, NULL, NULL);
1032 } else {
1033 if (j >= slider_y && j < slider_y + slider_h)
1034 gcp = &slgc;
1035 else
1036 gcp = &gc;
1037 tty_cell(tty, gcp, &grid_default_cell, NULL,
1038 NULL);