1 /* $OpenBSD: screen-redraw.c,v 1.103 2024/11/15 13:12:20 nicm Exp $ */
2
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 <stdlib.h>
22 #include <string.h>
23
24 #include "tmux.h"
25
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 *);
38
39 #define START_ISOLATE "\342\201\246"
40 #define END_ISOLATE "\342\201\251"
41
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
50 };
51 #define BORDER_MARKERS " +,.-"
52
53 /* Get cell border character. */
54 static void
screen_redraw_border_set(struct window * w,struct window_pane * wp,enum pane_lines pane_lines,int cell_type,struct grid_cell * gc)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)
57 {
58 u_int idx;
59
60 if (cell_type == CELL_OUTSIDE && w->fill_character != NULL) {
61 utf8_copy(&gc->data, &w->fill_character[0]);
62 return;
63 }
64
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;
71 }
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;
94 }
95 }
96
97 /* Return if window has only two panes. */
98 static int
screen_redraw_two_panes(struct window * w,int direction)99 screen_redraw_two_panes(struct window *w, int direction)
100 {
101 struct window_pane *wp;
102
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);
113 }
114
115 /* Check if cell is on the border of a pane. */
116 static enum screen_redraw_border_type
screen_redraw_pane_border(struct screen_redraw_ctx * ctx,struct window_pane * wp,u_int px,u_int py)117 screen_redraw_pane_border(struct screen_redraw_ctx *ctx, struct window_pane *wp,
118 u_int px, u_int py)
119 {
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;
125
126 /* Inside pane. */
127 if (px >= wp->xoff && px < ex && py >= wp->yoff && py < ey)
128 return (SCREEN_REDRAW_INSIDE);
129
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;
137 }
138
139 /* Are scrollbars enabled? */
140 if (window_pane_show_scrollbar(wp, pane_scrollbars))
141 sb_w = wp->scrollbar_style.width + wp->scrollbar_style.pad;
142
143 /*
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.
146 */
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);
162 }
163 }
164
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 < ex + sb_w))) {
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);
179 }
180 } else { /* sb_pos == PANE_SCROLLBARS_RIGHT */
181 if ((wp->xoff == 0 || px >= wp->xoff) &&
182 (px <= ex || (sb_w != 0 && px < ex + sb_w))) {
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);
187 }
188 }
189 }
190
191 /* Outside pane. */
192 return (SCREEN_REDRAW_OUTSIDE);
193 }
194
195 /* Check if a cell is on a border. */
196 static int
screen_redraw_cell_border(struct screen_redraw_ctx * ctx,u_int px,u_int py)197 screen_redraw_cell_border(struct screen_redraw_ctx *ctx, u_int px, u_int py)
198 {
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;
203
204 if (ctx->pane_status == PANE_STATUS_BOTTOM)
205 sy--;
206
207 /* Outside the window? */
208 if (px > w->sx || py > sy)
209 return (0);
210
211 /* On the window border? */
212 if (px == w->sx || py == sy)
213 return (1);
214
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);
226 }
227 }
228
229 return (0);
230 }
231
232 /* Work out type of border cell from surrounding cells. */
233 static int
screen_redraw_type_of_cell(struct screen_redraw_ctx * ctx,u_int px,u_int py)234 screen_redraw_type_of_cell(struct screen_redraw_ctx *ctx, u_int px, u_int py)
235 {
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;
241
242 if (pane_status == PANE_STATUS_BOTTOM)
243 sy--;
244
245 /* Is this outside the window? */
246 if (px > sx || py > sy)
247 return (CELL_OUTSIDE);
248
249 /*
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.
252 *
253 * bits 8 4 2 1: 2
254 * 8 + 4
255 * 1
256 */
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;
280 }
281
282 /*
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).
286 */
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);
310 }
311 return (CELL_OUTSIDE);
312 }
313
314 /* Check if cell inside a pane. */
315 static int
screen_redraw_check_cell(struct screen_redraw_ctx * ctx,u_int px,u_int py,struct window_pane ** wpp)316 screen_redraw_check_cell(struct screen_redraw_ctx *ctx, u_int px, u_int py,
317 struct window_pane **wpp)
318 {
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;
328
329 *wpp = NULL;
330
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));
335
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;
341
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;
347
348 if (py == line && px >= wp->xoff + 2 && px <= right)
349 return (CELL_INSIDE);
350
351 next1:
352 wp = TAILQ_NEXT(wp, entry);
353 if (wp == NULL)
354 wp = TAILQ_FIRST(&w->panes);
355 } while (wp != active);
356 }
357
358 active = wp = server_client_get_pane(c);
359 do {
360 if (!window_pane_visible(wp))
361 goto next2;
362 *wpp = wp;
363
364 /* Check if CELL_SCROLLBAR */
365 if (window_pane_show_scrollbar(wp, pane_scrollbars)) {
366
367 if (pane_status == PANE_STATUS_TOP)
368 line = wp->yoff - 1;
369 else
370 line = wp->yoff + wp->sy;
371
372 /*
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.
376 */
377 sb_w = wp->scrollbar_style.width +
378 wp->scrollbar_style.pad;
379 if ((pane_status && py != line) ||
380 (wp->yoff == 0 && py < wp->sy) ||
381 (py >= wp->yoff && py < wp->yoff + wp->sy)) {
382 /* Check if px lies within a scrollbar. */
383 if ((sb_pos == PANE_SCROLLBARS_RIGHT &&
384 (px >= wp->xoff + wp->sx &&
385 px < wp->xoff + wp->sx + sb_w)) ||
386 (sb_pos == PANE_SCROLLBARS_LEFT &&
387 (px >= wp->xoff - sb_w &&
388 px < wp->xoff)))
389 return (CELL_SCROLLBAR);
390 }
391 }
392
393 /*
394 * If definitely inside, return. If not on border, skip.
395 * Otherwise work out the cell.
396 */
397 border = screen_redraw_pane_border(ctx, wp, px, py);
398 if (border == SCREEN_REDRAW_INSIDE)
399 return (CELL_INSIDE);
400 if (border == SCREEN_REDRAW_OUTSIDE)
401 goto next2;
402 return (screen_redraw_type_of_cell(ctx, px, py));
403
404 next2:
405 wp = TAILQ_NEXT(wp, entry);
406 if (wp == NULL)
407 wp = TAILQ_FIRST(&w->panes);
408 } while (wp != active);
409
410 return (CELL_OUTSIDE);
411 }
412
413 /* Check if the border of a particular pane. */
414 static int
screen_redraw_check_is(struct screen_redraw_ctx * ctx,u_int px,u_int py,struct window_pane * wp)415 screen_redraw_check_is(struct screen_redraw_ctx *ctx, u_int px, u_int py,
416 struct window_pane *wp)
417 {
418 enum screen_redraw_border_type border;
419
420 border = screen_redraw_pane_border(ctx, wp, px, py);
421 if (border != SCREEN_REDRAW_INSIDE && border != SCREEN_REDRAW_OUTSIDE)
422 return (1);
423 return (0);
424 }
425
426 /* Update pane status. */
427 static int
screen_redraw_make_pane_status(struct client * c,struct window_pane * wp,struct screen_redraw_ctx * rctx,enum pane_lines pane_lines)428 screen_redraw_make_pane_status(struct client *c, struct window_pane *wp,
429 struct screen_redraw_ctx *rctx, enum pane_lines pane_lines)
430 {
431 struct window *w = wp->window;
432 struct grid_cell gc;
433 const char *fmt;
434 struct format_tree *ft;
435 char *expanded;
436 int pane_status = rctx->pane_status;
437 u_int width, i, cell_type, px, py;
438 struct screen_write_ctx ctx;
439 struct screen old;
440
441 ft = format_create(c, NULL, FORMAT_PANE|wp->id, FORMAT_STATUS);
442 format_defaults(ft, c, c->session, c->session->curw, wp);
443
444 if (wp == server_client_get_pane(c))
445 style_apply(&gc, w->options, "pane-active-border-style", ft);
446 else
447 style_apply(&gc, w->options, "pane-border-style", ft);
448 fmt = options_get_string(wp->options, "pane-border-format");
449
450 expanded = format_expand_time(ft, fmt);
451 if (wp->sx < 4)
452 wp->status_size = width = 0;
453 else
454 wp->status_size = width = wp->sx - 4;
455
456 memcpy(&old, &wp->status_screen, sizeof old);
457 screen_init(&wp->status_screen, width, 1, 0);
458 wp->status_screen.mode = 0;
459
460 screen_write_start(&ctx, &wp->status_screen);
461
462 for (i = 0; i < width; i++) {
463 px = wp->xoff + 2 + i;
464 if (pane_status == PANE_STATUS_TOP)
465 py = wp->yoff - 1;
466 else
467 py = wp->yoff + wp->sy;
468 cell_type = screen_redraw_type_of_cell(rctx, px, py);
469 screen_redraw_border_set(w, wp, pane_lines, cell_type, &gc);
470 screen_write_cell(&ctx, &gc);
471 }
472 gc.attr &= ~GRID_ATTR_CHARSET;
473
474 screen_write_cursormove(&ctx, 0, 0, 0);
475 format_draw(&ctx, &gc, width, expanded, NULL, 0);
476 screen_write_stop(&ctx);
477
478 free(expanded);
479 format_free(ft);
480
481 if (grid_compare(wp->status_screen.grid, old.grid) == 0) {
482 screen_free(&old);
483 return (0);
484 }
485 screen_free(&old);
486 return (1);
487 }
488
489 /* Draw pane status. */
490 static void
screen_redraw_draw_pane_status(struct screen_redraw_ctx * ctx)491 screen_redraw_draw_pane_status(struct screen_redraw_ctx *ctx)
492 {
493 struct client *c = ctx->c;
494 struct window *w = c->session->curw->window;
495 struct tty *tty = &c->tty;
496 struct window_pane *wp;
497 struct screen *s;
498 u_int i, x, width, xoff, yoff, size;
499
500 log_debug("%s: %s @%u", __func__, c->name, w->id);
501
502 TAILQ_FOREACH(wp, &w->panes, entry) {
503 if (!window_pane_visible(wp))
504 continue;
505 s = &wp->status_screen;
506
507 size = wp->status_size;
508 if (ctx->pane_status == PANE_STATUS_TOP)
509 yoff = wp->yoff - 1;
510 else
511 yoff = wp->yoff + wp->sy;
512 xoff = wp->xoff + 2;
513
514 if (xoff + size <= ctx->ox ||
515 xoff >= ctx->ox + ctx->sx ||
516 yoff < ctx->oy ||
517 yoff >= ctx->oy + ctx->sy)
518 continue;
519
520 if (xoff >= ctx->ox && xoff + size <= ctx->ox + ctx->sx) {
521 /* All visible. */
522 i = 0;
523 x = xoff - ctx->ox;
524 width = size;
525 } else if (xoff < ctx->ox && xoff + size > ctx->ox + ctx->sx) {
526 /* Both left and right not visible. */
527 i = ctx->ox;
528 x = 0;
529 width = ctx->sx;
530 } else if (xoff < ctx->ox) {
531 /* Left not visible. */
532 i = ctx->ox - xoff;
533 x = 0;
534 width = size - i;
535 } else {
536 /* Right not visible. */
537 i = 0;
538 x = xoff - ctx->ox;
539 width = size - x;
540 }
541
542 if (ctx->statustop)
543 yoff += ctx->statuslines;
544 tty_draw_line(tty, s, i, 0, width, x, yoff - ctx->oy,
545 &grid_default_cell, NULL);
546 }
547 tty_cursor(tty, 0, 0);
548 }
549
550 /* Update status line and change flags if unchanged. */
551 static uint64_t
screen_redraw_update(struct screen_redraw_ctx * ctx,uint64_t flags)552 screen_redraw_update(struct screen_redraw_ctx *ctx, uint64_t flags)
553 {
554 struct client *c = ctx->c;
555 struct window *w = c->session->curw->window;
556 struct window_pane *wp;
557 int redraw;
558 enum pane_lines lines;
559
560 if (c->message_string != NULL)
561 redraw = status_message_redraw(c);
562 else if (c->prompt_string != NULL)
563 redraw = status_prompt_redraw(c);
564 else
565 redraw = status_redraw(c);
566 if (!redraw && (~flags & CLIENT_REDRAWSTATUSALWAYS))
567 flags &= ~CLIENT_REDRAWSTATUS;
568
569 if (c->overlay_draw != NULL)
570 flags |= CLIENT_REDRAWOVERLAY;
571
572 if (ctx->pane_status != PANE_STATUS_OFF) {
573 lines = ctx->pane_lines;
574 redraw = 0;
575 TAILQ_FOREACH(wp, &w->panes, entry) {
576 if (screen_redraw_make_pane_status(c, wp, ctx, lines))
577 redraw = 1;
578 }
579 if (redraw)
580 flags |= CLIENT_REDRAWBORDERS;
581 }
582
583 return (flags);
584 }
585
586 /* Set up redraw context. */
587 static void
screen_redraw_set_context(struct client * c,struct screen_redraw_ctx * ctx)588 screen_redraw_set_context(struct client *c, struct screen_redraw_ctx *ctx)
589 {
590 struct session *s = c->session;
591 struct options *oo = s->options;
592 struct window *w = s->curw->window;
593 struct options *wo = w->options;
594 u_int lines;
595
596 memset(ctx, 0, sizeof *ctx);
597 ctx->c = c;
598
599 lines = status_line_size(c);
600 if (c->message_string != NULL || c->prompt_string != NULL)
601 lines = (lines == 0) ? 1 : lines;
602 if (lines != 0 && options_get_number(oo, "status-position") == 0)
603 ctx->statustop = 1;
604 ctx->statuslines = lines;
605
606 ctx->pane_status = options_get_number(wo, "pane-border-status");
607 ctx->pane_lines = options_get_number(wo, "pane-border-lines");
608
609 ctx->pane_scrollbars = options_get_number(wo, "pane-scrollbars");
610 ctx->pane_scrollbars_pos = options_get_number(wo,
611 "pane-scrollbars-position");
612
613 tty_window_offset(&c->tty, &ctx->ox, &ctx->oy, &ctx->sx, &ctx->sy);
614
615 log_debug("%s: %s @%u ox=%u oy=%u sx=%u sy=%u %u/%d", __func__, c->name,
616 w->id, ctx->ox, ctx->oy, ctx->sx, ctx->sy, ctx->statuslines,
617 ctx->statustop);
618 }
619
620 /* Redraw entire screen. */
621 void
screen_redraw_screen(struct client * c)622 screen_redraw_screen(struct client *c)
623 {
624 struct screen_redraw_ctx ctx;
625 uint64_t flags;
626
627 if (c->flags & CLIENT_SUSPENDED)
628 return;
629
630 screen_redraw_set_context(c, &ctx);
631
632 flags = screen_redraw_update(&ctx, c->flags);
633 if ((flags & CLIENT_ALLREDRAWFLAGS) == 0)
634 return;
635
636 tty_sync_start(&c->tty);
637 tty_update_mode(&c->tty, c->tty.mode, NULL);
638
639 if (flags & (CLIENT_REDRAWWINDOW|CLIENT_REDRAWBORDERS)) {
640 log_debug("%s: redrawing borders", c->name);
641 screen_redraw_draw_borders(&ctx);
642 if (ctx.pane_status != PANE_STATUS_OFF)
643 screen_redraw_draw_pane_status(&ctx);
644 screen_redraw_draw_pane_scrollbars(&ctx);
645 }
646 if (flags & CLIENT_REDRAWWINDOW) {
647 log_debug("%s: redrawing panes", c->name);
648 screen_redraw_draw_panes(&ctx);
649 screen_redraw_draw_pane_scrollbars(&ctx);
650 }
651 if (ctx.statuslines != 0 &&
652 (flags & (CLIENT_REDRAWSTATUS|CLIENT_REDRAWSTATUSALWAYS))) {
653 log_debug("%s: redrawing status", c->name);
654 screen_redraw_draw_status(&ctx);
655 }
656 if (c->overlay_draw != NULL && (flags & CLIENT_REDRAWOVERLAY)) {
657 log_debug("%s: redrawing overlay", c->name);
658 c->overlay_draw(c, c->overlay_data, &ctx);
659 }
660
661 tty_reset(&c->tty);
662 }
663
664 /* Redraw a single pane and its scrollbar. */
665 void
screen_redraw_pane(struct client * c,struct window_pane * wp,int redraw_scrollbar_only)666 screen_redraw_pane(struct client *c, struct window_pane *wp,
667 int redraw_scrollbar_only)
668 {
669 struct screen_redraw_ctx ctx;
670
671 if (!window_pane_visible(wp))
672 return;
673
674 screen_redraw_set_context(c, &ctx);
675 tty_sync_start(&c->tty);
676 tty_update_mode(&c->tty, c->tty.mode, NULL);
677
678 if (!redraw_scrollbar_only)
679 screen_redraw_draw_pane(&ctx, wp);
680
681 if (window_pane_show_scrollbar(wp, ctx.pane_scrollbars))
682 screen_redraw_draw_pane_scrollbar(&ctx, wp);
683
684 tty_reset(&c->tty);
685 }
686
687 /* Get border cell style. */
688 static const struct grid_cell *
screen_redraw_draw_borders_style(struct screen_redraw_ctx * ctx,u_int x,u_int y,struct window_pane * wp)689 screen_redraw_draw_borders_style(struct screen_redraw_ctx *ctx, u_int x,
690 u_int y, struct window_pane *wp)
691 {
692 struct client *c = ctx->c;
693 struct session *s = c->session;
694 struct window *w = s->curw->window;
695 struct window_pane *active = server_client_get_pane(c);
696 struct options *oo = w->options;
697 struct format_tree *ft;
698
699 if (wp->border_gc_set)
700 return (&wp->border_gc);
701 wp->border_gc_set = 1;
702
703 ft = format_create_defaults(NULL, c, s, s->curw, wp);
704 if (screen_redraw_check_is(ctx, x, y, active))
705 style_apply(&wp->border_gc, oo, "pane-active-border-style", ft);
706 else
707 style_apply(&wp->border_gc, oo, "pane-border-style", ft);
708 format_free(ft);
709
710 return (&wp->border_gc);
711 }
712
713 /* Draw a border cell. */
714 static void
screen_redraw_draw_borders_cell(struct screen_redraw_ctx * ctx,u_int i,u_int j)715 screen_redraw_draw_borders_cell(struct screen_redraw_ctx *ctx, u_int i, u_int j)
716 {
717 struct client *c = ctx->c;
718 struct session *s = c->session;
719 struct window *w = s->curw->window;
720 struct options *oo = w->options;
721 struct tty *tty = &c->tty;
722 struct format_tree *ft;
723 struct window_pane *wp, *active = server_client_get_pane(c);
724 struct grid_cell gc;
725 const struct grid_cell *tmp;
726 struct overlay_ranges r;
727 u_int cell_type, x = ctx->ox + i, y = ctx->oy + j;
728 int arrows = 0, border, isolates;
729
730 if (c->overlay_check != NULL) {
731 c->overlay_check(c, c->overlay_data, x, y, 1, &r);
732 if (r.nx[0] + r.nx[1] == 0)
733 return;
734 }
735
736 cell_type = screen_redraw_check_cell(ctx, x, y, &wp);
737 if (cell_type == CELL_INSIDE || cell_type == CELL_SCROLLBAR)
738 return;
739
740 if (wp == NULL) {
741 if (!ctx->no_pane_gc_set) {
742 ft = format_create_defaults(NULL, c, s, s->curw, NULL);
743 memcpy(&ctx->no_pane_gc, &grid_default_cell, sizeof gc);
744 style_add(&ctx->no_pane_gc, oo, "pane-border-style",
745 ft);
746 format_free(ft);
747 ctx->no_pane_gc_set = 1;
748 }
749 memcpy(&gc, &ctx->no_pane_gc, sizeof gc);
750 } else {
751 tmp = screen_redraw_draw_borders_style(ctx, x, y, wp);
752 if (tmp == NULL)
753 return;
754 memcpy(&gc, tmp, sizeof gc);
755
756 if (server_is_marked(s, s->curw, marked_pane.wp) &&
757 screen_redraw_check_is(ctx, x, y, marked_pane.wp))
758 gc.attr ^= GRID_ATTR_REVERSE;
759 }
760 screen_redraw_border_set(w, wp, ctx->pane_lines, cell_type, &gc);
761
762 if (cell_type == CELL_TOPBOTTOM &&
763 (c->flags & CLIENT_UTF8) &&
764 tty_term_has(tty->term, TTYC_BIDI))
765 isolates = 1;
766 else
767 isolates = 0;
768
769 if (ctx->statustop)
770 tty_cursor(tty, i, ctx->statuslines + j);
771 else
772 tty_cursor(tty, i, j);
773 if (isolates)
774 tty_puts(tty, END_ISOLATE);
775
776 switch (options_get_number(oo, "pane-border-indicators")) {
777 case PANE_BORDER_ARROWS:
778 case PANE_BORDER_BOTH:
779 arrows = 1;
780 break;
781 }
782
783 if (wp != NULL && arrows) {
784 border = screen_redraw_pane_border(ctx, active, x, y);
785 if (((i == wp->xoff + 1 &&
786 (cell_type == CELL_LEFTRIGHT ||
787 (cell_type == CELL_TOPJOIN &&
788 border == SCREEN_REDRAW_BORDER_BOTTOM) ||
789 (cell_type == CELL_BOTTOMJOIN &&
790 border == SCREEN_REDRAW_BORDER_TOP))) ||
791 (j == wp->yoff + 1 &&
792 (cell_type == CELL_TOPBOTTOM ||
793 (cell_type == CELL_LEFTJOIN &&
794 border == SCREEN_REDRAW_BORDER_RIGHT) ||
795 (cell_type == CELL_RIGHTJOIN &&
796 border == SCREEN_REDRAW_BORDER_LEFT)))) &&
797 screen_redraw_check_is(ctx, x, y, active)) {
798 gc.attr |= GRID_ATTR_CHARSET;
799 utf8_set(&gc.data, BORDER_MARKERS[border]);
800 }
801 }
802
803 tty_cell(tty, &gc, &grid_default_cell, NULL, NULL);
804 if (isolates)
805 tty_puts(tty, START_ISOLATE);
806 }
807
808 /* Draw the borders. */
809 static void
screen_redraw_draw_borders(struct screen_redraw_ctx * ctx)810 screen_redraw_draw_borders(struct screen_redraw_ctx *ctx)
811 {
812 struct client *c = ctx->c;
813 struct session *s = c->session;
814 struct window *w = s->curw->window;
815 struct window_pane *wp;
816 u_int i, j;
817
818 log_debug("%s: %s @%u", __func__, c->name, w->id);
819
820 TAILQ_FOREACH(wp, &w->panes, entry)
821 wp->border_gc_set = 0;
822
823 for (j = 0; j < c->tty.sy - ctx->statuslines; j++) {
824 for (i = 0; i < c->tty.sx; i++)
825 screen_redraw_draw_borders_cell(ctx, i, j);
826 }
827 }
828
829 /* Draw the panes. */
830 static void
screen_redraw_draw_panes(struct screen_redraw_ctx * ctx)831 screen_redraw_draw_panes(struct screen_redraw_ctx *ctx)
832 {
833 struct client *c = ctx->c;
834 struct window *w = c->session->curw->window;
835 struct window_pane *wp;
836
837 log_debug("%s: %s @%u", __func__, c->name, w->id);
838
839 TAILQ_FOREACH(wp, &w->panes, entry) {
840 if (window_pane_visible(wp))
841 screen_redraw_draw_pane(ctx, wp);
842 }
843 }
844
845 /* Draw the status line. */
846 static void
screen_redraw_draw_status(struct screen_redraw_ctx * ctx)847 screen_redraw_draw_status(struct screen_redraw_ctx *ctx)
848 {
849 struct client *c = ctx->c;
850 struct window *w = c->session->curw->window;
851 struct tty *tty = &c->tty;
852 struct screen *s = c->status.active;
853 u_int i, y;
854
855 log_debug("%s: %s @%u", __func__, c->name, w->id);
856
857 if (ctx->statustop)
858 y = 0;
859 else
860 y = c->tty.sy - ctx->statuslines;
861 for (i = 0; i < ctx->statuslines; i++) {
862 tty_draw_line(tty, s, 0, i, UINT_MAX, 0, y + i,
863 &grid_default_cell, NULL);
864 }
865 }
866
867 /* Draw one pane. */
868 static void
screen_redraw_draw_pane(struct screen_redraw_ctx * ctx,struct window_pane * wp)869 screen_redraw_draw_pane(struct screen_redraw_ctx *ctx, struct window_pane *wp)
870 {
871 struct client *c = ctx->c;
872 struct window *w = c->session->curw->window;
873 struct tty *tty = &c->tty;
874 struct screen *s = wp->screen;
875 struct colour_palette *palette = &wp->palette;
876 struct grid_cell defaults;
877 u_int i, j, top, x, y, width;
878
879 log_debug("%s: %s @%u %%%u", __func__, c->name, w->id, wp->id);
880
881 if (wp->xoff + wp->sx <= ctx->ox || wp->xoff >= ctx->ox + ctx->sx)
882 return;
883 if (ctx->statustop)
884 top = ctx->statuslines;
885 else
886 top = 0;
887 for (j = 0; j < wp->sy; j++) {
888 if (wp->yoff + j < ctx->oy || wp->yoff + j >= ctx->oy + ctx->sy)
889 continue;
890 y = top + wp->yoff + j - ctx->oy;
891
892 if (wp->xoff >= ctx->ox &&
893 wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
894 /* All visible. */
895 i = 0;
896 x = wp->xoff - ctx->ox;
897 width = wp->sx;
898 } else if (wp->xoff < ctx->ox &&
899 wp->xoff + wp->sx > ctx->ox + ctx->sx) {
900 /* Both left and right not visible. */
901 i = ctx->ox;
902 x = 0;
903 width = ctx->sx;
904 } else if (wp->xoff < ctx->ox) {
905 /* Left not visible. */
906 i = ctx->ox - wp->xoff;
907 x = 0;
908 width = wp->sx - i;
909 } else {
910 /* Right not visible. */
911 i = 0;
912 x = wp->xoff - ctx->ox;
913 width = ctx->sx - x;
914 }
915 log_debug("%s: %s %%%u line %u,%u at %u,%u, width %u",
916 __func__, c->name, wp->id, i, j, x, y, width);
917
918 tty_default_colours(&defaults, wp);
919 tty_draw_line(tty, s, i, j, width, x, y, &defaults, palette);
920 }
921 }
922
923 /* Draw the panes scrollbars */
924 static void
screen_redraw_draw_pane_scrollbars(struct screen_redraw_ctx * ctx)925 screen_redraw_draw_pane_scrollbars(struct screen_redraw_ctx *ctx)
926 {
927 struct client *c = ctx->c;
928 struct window *w = c->session->curw->window;
929 struct window_pane *wp;
930
931 log_debug("%s: %s @%u", __func__, c->name, w->id);
932
933 TAILQ_FOREACH(wp, &w->panes, entry) {
934 if (window_pane_show_scrollbar(wp, ctx->pane_scrollbars) &&
935 window_pane_visible(wp))
936 screen_redraw_draw_pane_scrollbar(ctx, wp);
937 }
938 }
939
940 /* Draw pane scrollbar. */
941 void
screen_redraw_draw_pane_scrollbar(struct screen_redraw_ctx * ctx,struct window_pane * wp)942 screen_redraw_draw_pane_scrollbar(struct screen_redraw_ctx *ctx,
943 struct window_pane *wp)
944 {
945 struct screen *s = wp->screen;
946 double percent_view;
947 u_int sb = ctx->pane_scrollbars, total_height, sb_h = wp->sy;
948 u_int sb_pos = ctx->pane_scrollbars_pos, slider_h, slider_y;
949 int sb_w = wp->scrollbar_style.width;
950 int sb_pad = wp->scrollbar_style.pad;
951 int cm_y, cm_size, xoff = wp->xoff, ox = ctx->ox;
952 int sb_x, sb_y = (int)(wp->yoff - ctx->oy); /* sb top */
953
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);
971 }
972
973 if (sb_pos == PANE_SCROLLBARS_LEFT)
974 sb_x = xoff - sb_w - sb_pad - ox;
975 else
976 sb_x = xoff + wp->sx - ox;
977
978 if (slider_h < 1)
979 slider_h = 1;
980 if (slider_y >= sb_h)
981 slider_y = sb_h - 1;
982
983 screen_redraw_draw_scrollbar(ctx, wp, sb_pos, sb_x, sb_y, sb_h,
984 slider_h, slider_y);
985
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 */
989 }
990
991 static void
screen_redraw_draw_scrollbar(struct screen_redraw_ctx * ctx,struct window_pane * wp,int sb_pos,int sb_x,int sb_y,u_int sb_h,u_int slider_h,u_int slider_y)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)
995 {
996 struct client *c = ctx->c;
997 struct tty *tty = &c->tty;
998 struct grid_cell gc, slgc, *gcp;
999 struct style *sb_style = &wp->scrollbar_style;
1000 u_int i, j, imax, jmax;
1001 u_int sb_w = sb_style->width, sb_pad = sb_style->pad;
1002 int px, py, ox = ctx->ox, oy = ctx->oy;
1003 int sx = ctx->sx, sy = ctx->sy, xoff = wp->xoff;
1004 int yoff = wp->yoff;
1005
1006 /* Set up style for slider. */
1007 gc = sb_style->gc;
1008 memcpy(&slgc, &gc, sizeof slgc);
1009 slgc.fg = gc.bg;
1010 slgc.bg = gc.fg;
1011
1012 imax = sb_w + sb_pad;
1013 if ((int)imax + sb_x > sx)
1014 imax = sx - sb_x;
1015 jmax = sb_h;
1016 if ((int)jmax + sb_y > sy)
1017 jmax = sy - sb_y;
1018
1019 for (j = 0; j < jmax; j++) {
1020 py = sb_y + j;
1021 for (i = 0; i < imax; i++) {
1022 px = sb_x + i;
1023 if (px < xoff - ox - (int)sb_w - (int)sb_pad ||
1024 px >= sx || px < 0 ||
1025 py < yoff - oy - 1 ||
1026 py >= sy || py < 0)
1027 continue;
1028 tty_cursor(tty, px, py);
1029 if ((sb_pos == PANE_SCROLLBARS_LEFT &&
1030 i >= sb_w && i < sb_w + sb_pad) ||
1031 (sb_pos == PANE_SCROLLBARS_RIGHT &&
1032 i < sb_pad)) {
1033 tty_cell(tty, &grid_default_cell,
1034 &grid_default_cell, NULL, NULL);
1035 } else {
1036 if (j >= slider_y && j < slider_y + slider_h)
1037 gcp = &slgc;
1038 else
1039 gcp = &gc;
1040 tty_cell(tty, gcp, &grid_default_cell, NULL,
1041 NULL);
1042 }
1043 }
1044 }
1045 }
1046