Merge branch 'obsd-master'
[tmux.git] / screen-write.c
blob2128eda94db6743a9ab216afe40f033aeed75363
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 struct screen_write_citem *screen_write_collect_trim(
27 struct screen_write_ctx *, u_int, u_int, u_int, int *);
28 static void screen_write_collect_clear(struct screen_write_ctx *, u_int,
29 u_int);
30 static void screen_write_collect_scroll(struct screen_write_ctx *, u_int);
31 static void screen_write_collect_flush(struct screen_write_ctx *, int,
32 const char *);
33 static int screen_write_overwrite(struct screen_write_ctx *,
34 struct grid_cell *, u_int);
35 static int screen_write_combine(struct screen_write_ctx *,
36 const struct grid_cell *);
38 struct screen_write_citem {
39 u_int x;
40 int wrapped;
42 enum { TEXT, CLEAR } type;
43 u_int used;
44 u_int bg;
46 struct grid_cell gc;
48 TAILQ_ENTRY(screen_write_citem) entry;
50 struct screen_write_cline {
51 char *data;
52 TAILQ_HEAD(, screen_write_citem) items;
54 TAILQ_HEAD(, screen_write_citem) screen_write_citem_freelist =
55 TAILQ_HEAD_INITIALIZER(screen_write_citem_freelist);
57 static struct screen_write_citem *
58 screen_write_get_citem(void)
60 struct screen_write_citem *ci;
62 ci = TAILQ_FIRST(&screen_write_citem_freelist);
63 if (ci != NULL) {
64 TAILQ_REMOVE(&screen_write_citem_freelist, ci, entry);
65 memset(ci, 0, sizeof *ci);
66 return (ci);
68 return (xcalloc(1, sizeof *ci));
71 static void
72 screen_write_free_citem(struct screen_write_citem *ci)
74 TAILQ_INSERT_TAIL(&screen_write_citem_freelist, ci, entry);
77 static void
78 screen_write_offset_timer(__unused int fd, __unused short events, void *data)
80 struct window *w = data;
82 tty_update_window_offset(w);
85 /* Set cursor position. */
86 static void
87 screen_write_set_cursor(struct screen_write_ctx *ctx, int cx, int cy)
89 struct window_pane *wp = ctx->wp;
90 struct window *w;
91 struct screen *s = ctx->s;
92 struct timeval tv = { .tv_usec = 10000 };
94 if (cx != -1 && (u_int)cx == s->cx && cy != -1 && (u_int)cy == s->cy)
95 return;
97 if (cx != -1) {
98 if ((u_int)cx > screen_size_x(s)) /* allow last column */
99 cx = screen_size_x(s) - 1;
100 s->cx = cx;
102 if (cy != -1) {
103 if ((u_int)cy > screen_size_y(s) - 1)
104 cy = screen_size_y(s) - 1;
105 s->cy = cy;
108 if (wp == NULL)
109 return;
110 w = wp->window;
112 if (!event_initialized(&w->offset_timer))
113 evtimer_set(&w->offset_timer, screen_write_offset_timer, w);
114 if (!evtimer_pending(&w->offset_timer, NULL))
115 evtimer_add(&w->offset_timer, &tv);
118 /* Do a full redraw. */
119 static void
120 screen_write_redraw_cb(const struct tty_ctx *ttyctx)
122 struct window_pane *wp = ttyctx->arg;
124 if (wp != NULL)
125 wp->flags |= PANE_REDRAW;
128 /* Update context for client. */
129 static int
130 screen_write_set_client_cb(struct tty_ctx *ttyctx, struct client *c)
132 struct window_pane *wp = ttyctx->arg;
134 if (ttyctx->allow_invisible_panes) {
135 if (session_has(c->session, wp->window))
136 return (1);
137 return (0);
140 if (c->session->curw->window != wp->window)
141 return (0);
142 if (wp->layout_cell == NULL)
143 return (0);
145 if (wp->flags & (PANE_REDRAW|PANE_DROP))
146 return (-1);
147 if (c->flags & CLIENT_REDRAWPANES) {
149 * Redraw is already deferred to redraw another pane - redraw
150 * this one also when that happens.
152 log_debug("%s: adding %%%u to deferred redraw", __func__,
153 wp->id);
154 wp->flags |= PANE_REDRAW;
155 return (-1);
158 ttyctx->bigger = tty_window_offset(&c->tty, &ttyctx->wox, &ttyctx->woy,
159 &ttyctx->wsx, &ttyctx->wsy);
161 ttyctx->xoff = ttyctx->rxoff = wp->xoff;
162 ttyctx->yoff = ttyctx->ryoff = wp->yoff;
164 if (status_at_line(c) == 0)
165 ttyctx->yoff += status_line_size(c);
167 return (1);
170 /* Set up context for TTY command. */
171 static void
172 screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
173 int sync)
175 struct screen *s = ctx->s;
177 memset(ttyctx, 0, sizeof *ttyctx);
179 ttyctx->s = s;
180 ttyctx->sx = screen_size_x(s);
181 ttyctx->sy = screen_size_y(s);
183 ttyctx->ocx = s->cx;
184 ttyctx->ocy = s->cy;
185 ttyctx->orlower = s->rlower;
186 ttyctx->orupper = s->rupper;
188 memcpy(&ttyctx->defaults, &grid_default_cell, sizeof ttyctx->defaults);
189 if (ctx->init_ctx_cb != NULL) {
190 ctx->init_ctx_cb(ctx, ttyctx);
191 if (ttyctx->palette != NULL) {
192 if (ttyctx->defaults.fg == 8)
193 ttyctx->defaults.fg = ttyctx->palette->fg;
194 if (ttyctx->defaults.bg == 8)
195 ttyctx->defaults.bg = ttyctx->palette->bg;
197 } else {
198 ttyctx->redraw_cb = screen_write_redraw_cb;
199 if (ctx->wp != NULL) {
200 tty_default_colours(&ttyctx->defaults, ctx->wp);
201 ttyctx->palette = &ctx->wp->palette;
202 ttyctx->set_client_cb = screen_write_set_client_cb;
203 ttyctx->arg = ctx->wp;
207 if (~ctx->flags & SCREEN_WRITE_SYNC) {
209 * For the active pane or for an overlay (no pane), we want to
210 * only use synchronized updates if requested (commands that
211 * move the cursor); for other panes, always use it, since the
212 * cursor will have to move.
214 if (ctx->wp != NULL) {
215 if (ctx->wp != ctx->wp->window->active)
216 ttyctx->num = 1;
217 else
218 ttyctx->num = sync;
219 } else
220 ttyctx->num = 0x10|sync;
221 tty_write(tty_cmd_syncstart, ttyctx);
222 ctx->flags |= SCREEN_WRITE_SYNC;
226 /* Make write list. */
227 void
228 screen_write_make_list(struct screen *s)
230 u_int y;
232 s->write_list = xcalloc(screen_size_y(s), sizeof *s->write_list);
233 for (y = 0; y < screen_size_y(s); y++)
234 TAILQ_INIT(&s->write_list[y].items);
237 /* Free write list. */
238 void
239 screen_write_free_list(struct screen *s)
241 u_int y;
243 for (y = 0; y < screen_size_y(s); y++)
244 free(s->write_list[y].data);
245 free(s->write_list);
248 /* Set up for writing. */
249 static void
250 screen_write_init(struct screen_write_ctx *ctx, struct screen *s)
252 memset(ctx, 0, sizeof *ctx);
254 ctx->s = s;
256 if (ctx->s->write_list == NULL)
257 screen_write_make_list(ctx->s);
258 ctx->item = screen_write_get_citem();
260 ctx->scrolled = 0;
261 ctx->bg = 8;
264 /* Initialize writing with a pane. */
265 void
266 screen_write_start_pane(struct screen_write_ctx *ctx, struct window_pane *wp,
267 struct screen *s)
269 if (s == NULL)
270 s = wp->screen;
271 screen_write_init(ctx, s);
272 ctx->wp = wp;
274 if (log_get_level() != 0) {
275 log_debug("%s: size %ux%u, pane %%%u (at %u,%u)",
276 __func__, screen_size_x(ctx->s), screen_size_y(ctx->s),
277 wp->id, wp->xoff, wp->yoff);
281 /* Initialize writing with a callback. */
282 void
283 screen_write_start_callback(struct screen_write_ctx *ctx, struct screen *s,
284 screen_write_init_ctx_cb cb, void *arg)
286 screen_write_init(ctx, s);
288 ctx->init_ctx_cb = cb;
289 ctx->arg = arg;
291 if (log_get_level() != 0) {
292 log_debug("%s: size %ux%u, with callback", __func__,
293 screen_size_x(ctx->s), screen_size_y(ctx->s));
297 /* Initialize writing. */
298 void
299 screen_write_start(struct screen_write_ctx *ctx, struct screen *s)
301 screen_write_init(ctx, s);
303 if (log_get_level() != 0) {
304 log_debug("%s: size %ux%u, no pane", __func__,
305 screen_size_x(ctx->s), screen_size_y(ctx->s));
309 /* Finish writing. */
310 void
311 screen_write_stop(struct screen_write_ctx *ctx)
313 screen_write_collect_end(ctx);
314 screen_write_collect_flush(ctx, 0, __func__);
316 screen_write_free_citem(ctx->item);
319 /* Reset screen state. */
320 void
321 screen_write_reset(struct screen_write_ctx *ctx)
323 struct screen *s = ctx->s;
325 screen_reset_tabs(s);
326 screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
328 s->mode = MODE_CURSOR|MODE_WRAP;
330 if (options_get_number(global_options, "extended-keys") == 2)
331 s->mode = (s->mode & ~EXTENDED_KEY_MODES)|MODE_KEYS_EXTENDED;
333 screen_write_clearscreen(ctx, 8);
334 screen_write_set_cursor(ctx, 0, 0);
337 /* Write character. */
338 void
339 screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
340 u_char ch)
342 struct grid_cell gc;
344 memcpy(&gc, gcp, sizeof gc);
346 utf8_set(&gc.data, ch);
347 screen_write_cell(ctx, &gc);
350 /* Calculate string length. */
351 size_t
352 screen_write_strlen(const char *fmt, ...)
354 va_list ap;
355 char *msg;
356 struct utf8_data ud;
357 u_char *ptr;
358 size_t left, size = 0;
359 enum utf8_state more;
361 va_start(ap, fmt);
362 xvasprintf(&msg, fmt, ap);
363 va_end(ap);
365 ptr = msg;
366 while (*ptr != '\0') {
367 if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
368 ptr++;
370 left = strlen(ptr);
371 if (left < (size_t)ud.size - 1)
372 break;
373 while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
374 ptr++;
375 ptr++;
377 if (more == UTF8_DONE)
378 size += ud.width;
379 } else {
380 if (*ptr > 0x1f && *ptr < 0x7f)
381 size++;
382 ptr++;
386 free(msg);
387 return (size);
390 /* Write string wrapped over lines. */
392 screen_write_text(struct screen_write_ctx *ctx, u_int cx, u_int width,
393 u_int lines, int more, const struct grid_cell *gcp, const char *fmt, ...)
395 struct screen *s = ctx->s;
396 va_list ap;
397 char *tmp;
398 u_int cy = s->cy, i, end, next, idx = 0, at, left;
399 struct utf8_data *text;
400 struct grid_cell gc;
402 memcpy(&gc, gcp, sizeof gc);
404 va_start(ap, fmt);
405 xvasprintf(&tmp, fmt, ap);
406 va_end(ap);
408 text = utf8_fromcstr(tmp);
409 free(tmp);
411 left = (cx + width) - s->cx;
412 for (;;) {
413 /* Find the end of what can fit on the line. */
414 at = 0;
415 for (end = idx; text[end].size != 0; end++) {
416 if (text[end].size == 1 && text[end].data[0] == '\n')
417 break;
418 if (at + text[end].width > left)
419 break;
420 at += text[end].width;
424 * If we're on a space, that's the end. If not, walk back to
425 * try and find one.
427 if (text[end].size == 0)
428 next = end;
429 else if (text[end].size == 1 && text[end].data[0] == '\n')
430 next = end + 1;
431 else if (text[end].size == 1 && text[end].data[0] == ' ')
432 next = end + 1;
433 else {
434 for (i = end; i > idx; i--) {
435 if (text[i].size == 1 && text[i].data[0] == ' ')
436 break;
438 if (i != idx) {
439 next = i + 1;
440 end = i;
441 } else
442 next = end;
445 /* Print the line. */
446 for (i = idx; i < end; i++) {
447 utf8_copy(&gc.data, &text[i]);
448 screen_write_cell(ctx, &gc);
451 /* If at the bottom, stop. */
452 idx = next;
453 if (s->cy == cy + lines - 1 || text[idx].size == 0)
454 break;
456 screen_write_cursormove(ctx, cx, s->cy + 1, 0);
457 left = width;
461 * Fail if on the last line and there is more to come or at the end, or
462 * if the text was not entirely consumed.
464 if ((s->cy == cy + lines - 1 && (!more || s->cx == cx + width)) ||
465 text[idx].size != 0) {
466 free(text);
467 return (0);
469 free(text);
472 * If no more to come, move to the next line. Otherwise, leave on
473 * the same line (except if at the end).
475 if (!more || s->cx == cx + width)
476 screen_write_cursormove(ctx, cx, s->cy + 1, 0);
477 return (1);
480 /* Write simple string (no maximum length). */
481 void
482 screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
483 const char *fmt, ...)
485 va_list ap;
487 va_start(ap, fmt);
488 screen_write_vnputs(ctx, -1, gcp, fmt, ap);
489 va_end(ap);
492 /* Write string with length limit (-1 for unlimited). */
493 void
494 screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
495 const struct grid_cell *gcp, const char *fmt, ...)
497 va_list ap;
499 va_start(ap, fmt);
500 screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
501 va_end(ap);
504 void
505 screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
506 const struct grid_cell *gcp, const char *fmt, va_list ap)
508 struct grid_cell gc;
509 struct utf8_data *ud = &gc.data;
510 char *msg;
511 u_char *ptr;
512 size_t left, size = 0;
513 enum utf8_state more;
515 memcpy(&gc, gcp, sizeof gc);
516 xvasprintf(&msg, fmt, ap);
518 ptr = msg;
519 while (*ptr != '\0') {
520 if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
521 ptr++;
523 left = strlen(ptr);
524 if (left < (size_t)ud->size - 1)
525 break;
526 while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
527 ptr++;
528 ptr++;
530 if (more != UTF8_DONE)
531 continue;
532 if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
533 while (size < (size_t)maxlen) {
534 screen_write_putc(ctx, &gc, ' ');
535 size++;
537 break;
539 size += ud->width;
540 screen_write_cell(ctx, &gc);
541 } else {
542 if (maxlen > 0 && size + 1 > (size_t)maxlen)
543 break;
545 if (*ptr == '\001')
546 gc.attr ^= GRID_ATTR_CHARSET;
547 else if (*ptr == '\n') {
548 screen_write_linefeed(ctx, 0, 8);
549 screen_write_carriagereturn(ctx);
550 } else if (*ptr > 0x1f && *ptr < 0x7f) {
551 size++;
552 screen_write_putc(ctx, &gc, *ptr);
554 ptr++;
558 free(msg);
562 * Copy from another screen but without the selection stuff. Assumes the target
563 * region is already big enough.
565 void
566 screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src,
567 u_int px, u_int py, u_int nx, u_int ny)
569 struct screen *s = ctx->s;
570 struct window_pane *wp = ctx->wp;
571 struct tty_ctx ttyctx;
572 struct grid *gd = src->grid;
573 struct grid_cell gc;
574 u_int xx, yy, cx = s->cx, cy = s->cy;
576 if (nx == 0 || ny == 0)
577 return;
579 cy = s->cy;
580 for (yy = py; yy < py + ny; yy++) {
581 if (yy >= gd->hsize + gd->sy)
582 break;
583 s->cx = cx;
584 if (wp != NULL)
585 screen_write_initctx(ctx, &ttyctx, 0);
586 for (xx = px; xx < px + nx; xx++) {
587 if (xx >= grid_get_line(gd, yy)->cellsize)
588 break;
589 grid_get_cell(gd, xx, yy, &gc);
590 if (xx + gc.data.width > px + nx)
591 break;
592 grid_view_set_cell(ctx->s->grid, s->cx, s->cy, &gc);
593 if (wp != NULL) {
594 ttyctx.cell = &gc;
595 tty_write(tty_cmd_cell, &ttyctx);
596 ttyctx.ocx++;
598 s->cx++;
600 s->cy++;
603 s->cx = cx;
604 s->cy = cy;
607 /* Select character set for drawing border lines. */
608 static void
609 screen_write_box_border_set(enum box_lines lines, int cell_type,
610 struct grid_cell *gc)
612 switch (lines) {
613 case BOX_LINES_NONE:
614 break;
615 case BOX_LINES_DOUBLE:
616 gc->attr &= ~GRID_ATTR_CHARSET;
617 utf8_copy(&gc->data, tty_acs_double_borders(cell_type));
618 break;
619 case BOX_LINES_HEAVY:
620 gc->attr &= ~GRID_ATTR_CHARSET;
621 utf8_copy(&gc->data, tty_acs_heavy_borders(cell_type));
622 break;
623 case BOX_LINES_ROUNDED:
624 gc->attr &= ~GRID_ATTR_CHARSET;
625 utf8_copy(&gc->data, tty_acs_rounded_borders(cell_type));
626 break;
627 case BOX_LINES_SIMPLE:
628 gc->attr &= ~GRID_ATTR_CHARSET;
629 utf8_set(&gc->data, SIMPLE_BORDERS[cell_type]);
630 break;
631 case BOX_LINES_PADDED:
632 gc->attr &= ~GRID_ATTR_CHARSET;
633 utf8_set(&gc->data, PADDED_BORDERS[cell_type]);
634 break;
635 case BOX_LINES_SINGLE:
636 case BOX_LINES_DEFAULT:
637 gc->attr |= GRID_ATTR_CHARSET;
638 utf8_set(&gc->data, CELL_BORDERS[cell_type]);
639 break;
643 /* Draw a horizontal line on screen. */
644 void
645 screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right,
646 enum box_lines lines, const struct grid_cell *border_gc)
648 struct screen *s = ctx->s;
649 struct grid_cell gc;
650 u_int cx, cy, i;
652 cx = s->cx;
653 cy = s->cy;
655 if (border_gc != NULL)
656 memcpy(&gc, border_gc, sizeof gc);
657 else
658 memcpy(&gc, &grid_default_cell, sizeof gc);
659 gc.attr |= GRID_ATTR_CHARSET;
661 if (left)
662 screen_write_box_border_set(lines, CELL_LEFTJOIN, &gc);
663 else
664 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
665 screen_write_cell(ctx, &gc);
667 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
668 for (i = 1; i < nx - 1; i++)
669 screen_write_cell(ctx, &gc);
671 if (right)
672 screen_write_box_border_set(lines, CELL_RIGHTJOIN, &gc);
673 else
674 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
675 screen_write_cell(ctx, &gc);
677 screen_write_set_cursor(ctx, cx, cy);
680 /* Draw a vertical line on screen. */
681 void
682 screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom)
684 struct screen *s = ctx->s;
685 struct grid_cell gc;
686 u_int cx, cy, i;
688 cx = s->cx;
689 cy = s->cy;
691 memcpy(&gc, &grid_default_cell, sizeof gc);
692 gc.attr |= GRID_ATTR_CHARSET;
694 screen_write_putc(ctx, &gc, top ? 'w' : 'x');
695 for (i = 1; i < ny - 1; i++) {
696 screen_write_set_cursor(ctx, cx, cy + i);
697 screen_write_putc(ctx, &gc, 'x');
699 screen_write_set_cursor(ctx, cx, cy + ny - 1);
700 screen_write_putc(ctx, &gc, bottom ? 'v' : 'x');
702 screen_write_set_cursor(ctx, cx, cy);
705 /* Draw a menu on screen. */
706 void
707 screen_write_menu(struct screen_write_ctx *ctx, struct menu *menu, int choice,
708 enum box_lines lines, const struct grid_cell *menu_gc,
709 const struct grid_cell *border_gc, const struct grid_cell *choice_gc)
711 struct screen *s = ctx->s;
712 struct grid_cell default_gc;
713 const struct grid_cell *gc = &default_gc;
714 u_int cx, cy, i, j, width = menu->width;
715 const char *name;
717 cx = s->cx;
718 cy = s->cy;
720 memcpy(&default_gc, menu_gc, sizeof default_gc);
722 screen_write_box(ctx, menu->width + 4, menu->count + 2, lines,
723 border_gc, menu->title);
725 for (i = 0; i < menu->count; i++) {
726 name = menu->items[i].name;
727 if (name == NULL) {
728 screen_write_cursormove(ctx, cx, cy + 1 + i, 0);
729 screen_write_hline(ctx, width + 4, 1, 1, lines,
730 border_gc);
731 continue;
734 if (choice >= 0 && i == (u_int)choice && *name != '-')
735 gc = choice_gc;
737 screen_write_cursormove(ctx, cx + 1, cy + 1 + i, 0);
738 for (j = 0; j < width + 2; j++)
739 screen_write_putc(ctx, gc, ' ');
741 screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
742 if (*name == '-') {
743 default_gc.attr |= GRID_ATTR_DIM;
744 format_draw(ctx, gc, width, name + 1, NULL, 0);
745 default_gc.attr &= ~GRID_ATTR_DIM;
746 continue;
749 format_draw(ctx, gc, width, name, NULL, 0);
750 gc = &default_gc;
753 screen_write_set_cursor(ctx, cx, cy);
756 /* Draw a box on screen. */
757 void
758 screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny,
759 enum box_lines lines, const struct grid_cell *gcp, const char *title)
761 struct screen *s = ctx->s;
762 struct grid_cell gc;
763 u_int cx, cy, i;
765 cx = s->cx;
766 cy = s->cy;
768 if (gcp != NULL)
769 memcpy(&gc, gcp, sizeof gc);
770 else
771 memcpy(&gc, &grid_default_cell, sizeof gc);
773 gc.attr |= GRID_ATTR_CHARSET;
774 gc.flags |= GRID_FLAG_NOPALETTE;
776 /* Draw top border */
777 screen_write_box_border_set(lines, CELL_TOPLEFT, &gc);
778 screen_write_cell(ctx, &gc);
779 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
780 for (i = 1; i < nx - 1; i++)
781 screen_write_cell(ctx, &gc);
782 screen_write_box_border_set(lines, CELL_TOPRIGHT, &gc);
783 screen_write_cell(ctx, &gc);
785 /* Draw bottom border */
786 screen_write_set_cursor(ctx, cx, cy + ny - 1);
787 screen_write_box_border_set(lines, CELL_BOTTOMLEFT, &gc);
788 screen_write_cell(ctx, &gc);
789 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
790 for (i = 1; i < nx - 1; i++)
791 screen_write_cell(ctx, &gc);
792 screen_write_box_border_set(lines, CELL_BOTTOMRIGHT, &gc);
793 screen_write_cell(ctx, &gc);
795 /* Draw sides */
796 screen_write_box_border_set(lines, CELL_TOPBOTTOM, &gc);
797 for (i = 1; i < ny - 1; i++) {
798 /* left side */
799 screen_write_set_cursor(ctx, cx, cy + i);
800 screen_write_cell(ctx, &gc);
801 /* right side */
802 screen_write_set_cursor(ctx, cx + nx - 1, cy + i);
803 screen_write_cell(ctx, &gc);
806 if (title != NULL) {
807 gc.attr &= ~GRID_ATTR_CHARSET;
808 screen_write_cursormove(ctx, cx + 2, cy, 0);
809 format_draw(ctx, &gc, nx - 4, title, NULL, 0);
812 screen_write_set_cursor(ctx, cx, cy);
816 * Write a preview version of a window. Assumes target area is big enough and
817 * already cleared.
819 void
820 screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
821 u_int ny)
823 struct screen *s = ctx->s;
824 struct grid_cell gc;
825 u_int cx, cy, px, py;
827 cx = s->cx;
828 cy = s->cy;
831 * If the cursor is on, pick the area around the cursor, otherwise use
832 * the top left.
834 if (src->mode & MODE_CURSOR) {
835 px = src->cx;
836 if (px < nx / 3)
837 px = 0;
838 else
839 px = px - nx / 3;
840 if (px + nx > screen_size_x(src)) {
841 if (nx > screen_size_x(src))
842 px = 0;
843 else
844 px = screen_size_x(src) - nx;
846 py = src->cy;
847 if (py < ny / 3)
848 py = 0;
849 else
850 py = py - ny / 3;
851 if (py + ny > screen_size_y(src)) {
852 if (ny > screen_size_y(src))
853 py = 0;
854 else
855 py = screen_size_y(src) - ny;
857 } else {
858 px = 0;
859 py = 0;
862 screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
864 if (src->mode & MODE_CURSOR) {
865 grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
866 gc.attr |= GRID_ATTR_REVERSE;
867 screen_write_set_cursor(ctx, cx + (src->cx - px),
868 cy + (src->cy - py));
869 screen_write_cell(ctx, &gc);
873 /* Set a mode. */
874 void
875 screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
877 struct screen *s = ctx->s;
879 s->mode |= mode;
881 if (log_get_level() != 0)
882 log_debug("%s: %s", __func__, screen_mode_to_string(mode));
885 /* Clear a mode. */
886 void
887 screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
889 struct screen *s = ctx->s;
891 s->mode &= ~mode;
893 if (log_get_level() != 0)
894 log_debug("%s: %s", __func__, screen_mode_to_string(mode));
897 /* Cursor up by ny. */
898 void
899 screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
901 struct screen *s = ctx->s;
902 u_int cx = s->cx, cy = s->cy;
904 if (ny == 0)
905 ny = 1;
907 if (cy < s->rupper) {
908 /* Above region. */
909 if (ny > cy)
910 ny = cy;
911 } else {
912 /* Below region. */
913 if (ny > cy - s->rupper)
914 ny = cy - s->rupper;
916 if (cx == screen_size_x(s))
917 cx--;
919 cy -= ny;
921 screen_write_set_cursor(ctx, cx, cy);
924 /* Cursor down by ny. */
925 void
926 screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
928 struct screen *s = ctx->s;
929 u_int cx = s->cx, cy = s->cy;
931 if (ny == 0)
932 ny = 1;
934 if (cy > s->rlower) {
935 /* Below region. */
936 if (ny > screen_size_y(s) - 1 - cy)
937 ny = screen_size_y(s) - 1 - cy;
938 } else {
939 /* Above region. */
940 if (ny > s->rlower - cy)
941 ny = s->rlower - cy;
943 if (cx == screen_size_x(s))
944 cx--;
945 else if (ny == 0)
946 return;
948 cy += ny;
950 screen_write_set_cursor(ctx, cx, cy);
953 /* Cursor right by nx. */
954 void
955 screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
957 struct screen *s = ctx->s;
958 u_int cx = s->cx, cy = s->cy;
960 if (nx == 0)
961 nx = 1;
963 if (nx > screen_size_x(s) - 1 - cx)
964 nx = screen_size_x(s) - 1 - cx;
965 if (nx == 0)
966 return;
968 cx += nx;
970 screen_write_set_cursor(ctx, cx, cy);
973 /* Cursor left by nx. */
974 void
975 screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
977 struct screen *s = ctx->s;
978 u_int cx = s->cx, cy = s->cy;
980 if (nx == 0)
981 nx = 1;
983 if (nx > cx)
984 nx = cx;
985 if (nx == 0)
986 return;
988 cx -= nx;
990 screen_write_set_cursor(ctx, cx, cy);
993 /* Backspace; cursor left unless at start of wrapped line when can move up. */
994 void
995 screen_write_backspace(struct screen_write_ctx *ctx)
997 struct screen *s = ctx->s;
998 struct grid_line *gl;
999 u_int cx = s->cx, cy = s->cy;
1001 if (cx == 0) {
1002 if (cy == 0)
1003 return;
1004 gl = grid_get_line(s->grid, s->grid->hsize + cy - 1);
1005 if (gl->flags & GRID_LINE_WRAPPED) {
1006 cy--;
1007 cx = screen_size_x(s) - 1;
1009 } else
1010 cx--;
1012 screen_write_set_cursor(ctx, cx, cy);
1015 /* VT100 alignment test. */
1016 void
1017 screen_write_alignmenttest(struct screen_write_ctx *ctx)
1019 struct screen *s = ctx->s;
1020 struct tty_ctx ttyctx;
1021 struct grid_cell gc;
1022 u_int xx, yy;
1024 memcpy(&gc, &grid_default_cell, sizeof gc);
1025 utf8_set(&gc.data, 'E');
1027 #ifdef ENABLE_SIXEL
1028 if (image_free_all(s) && ctx->wp != NULL)
1029 ctx->wp->flags |= PANE_REDRAW;
1030 #endif
1032 for (yy = 0; yy < screen_size_y(s); yy++) {
1033 for (xx = 0; xx < screen_size_x(s); xx++)
1034 grid_view_set_cell(s->grid, xx, yy, &gc);
1037 screen_write_set_cursor(ctx, 0, 0);
1039 s->rupper = 0;
1040 s->rlower = screen_size_y(s) - 1;
1042 screen_write_initctx(ctx, &ttyctx, 1);
1044 screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1045 tty_write(tty_cmd_alignmenttest, &ttyctx);
1048 /* Insert nx characters. */
1049 void
1050 screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1052 struct screen *s = ctx->s;
1053 struct tty_ctx ttyctx;
1055 if (nx == 0)
1056 nx = 1;
1058 if (nx > screen_size_x(s) - s->cx)
1059 nx = screen_size_x(s) - s->cx;
1060 if (nx == 0)
1061 return;
1063 if (s->cx > screen_size_x(s) - 1)
1064 return;
1066 #ifdef ENABLE_SIXEL
1067 if (image_check_line(s, s->cy, 1) && ctx->wp != NULL)
1068 ctx->wp->flags |= PANE_REDRAW;
1069 #endif
1071 screen_write_initctx(ctx, &ttyctx, 0);
1072 ttyctx.bg = bg;
1074 grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1076 screen_write_collect_flush(ctx, 0, __func__);
1077 ttyctx.num = nx;
1078 tty_write(tty_cmd_insertcharacter, &ttyctx);
1081 /* Delete nx characters. */
1082 void
1083 screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1085 struct screen *s = ctx->s;
1086 struct tty_ctx ttyctx;
1088 if (nx == 0)
1089 nx = 1;
1091 if (nx > screen_size_x(s) - s->cx)
1092 nx = screen_size_x(s) - s->cx;
1093 if (nx == 0)
1094 return;
1096 if (s->cx > screen_size_x(s) - 1)
1097 return;
1099 #ifdef ENABLE_SIXEL
1100 if (image_check_line(s, s->cy, 1) && ctx->wp != NULL)
1101 ctx->wp->flags |= PANE_REDRAW;
1102 #endif
1104 screen_write_initctx(ctx, &ttyctx, 0);
1105 ttyctx.bg = bg;
1107 grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1109 screen_write_collect_flush(ctx, 0, __func__);
1110 ttyctx.num = nx;
1111 tty_write(tty_cmd_deletecharacter, &ttyctx);
1114 /* Clear nx characters. */
1115 void
1116 screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1118 struct screen *s = ctx->s;
1119 struct tty_ctx ttyctx;
1121 if (nx == 0)
1122 nx = 1;
1124 if (nx > screen_size_x(s) - s->cx)
1125 nx = screen_size_x(s) - s->cx;
1126 if (nx == 0)
1127 return;
1129 if (s->cx > screen_size_x(s) - 1)
1130 return;
1132 #ifdef ENABLE_SIXEL
1133 if (image_check_line(s, s->cy, 1) && ctx->wp != NULL)
1134 ctx->wp->flags |= PANE_REDRAW;
1135 #endif
1137 screen_write_initctx(ctx, &ttyctx, 0);
1138 ttyctx.bg = bg;
1140 grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
1142 screen_write_collect_flush(ctx, 0, __func__);
1143 ttyctx.num = nx;
1144 tty_write(tty_cmd_clearcharacter, &ttyctx);
1147 /* Insert ny lines. */
1148 void
1149 screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1151 struct screen *s = ctx->s;
1152 struct grid *gd = s->grid;
1153 struct tty_ctx ttyctx;
1155 #ifdef ENABLE_SIXEL
1156 u_int sy = screen_size_y(s);
1157 #endif
1159 if (ny == 0)
1160 ny = 1;
1162 #ifdef ENABLE_SIXEL
1163 if (image_check_line(s, s->cy, sy - s->cy) && ctx->wp != NULL)
1164 ctx->wp->flags |= PANE_REDRAW;
1165 #endif
1167 if (s->cy < s->rupper || s->cy > s->rlower) {
1168 if (ny > screen_size_y(s) - s->cy)
1169 ny = screen_size_y(s) - s->cy;
1170 if (ny == 0)
1171 return;
1173 screen_write_initctx(ctx, &ttyctx, 1);
1174 ttyctx.bg = bg;
1176 grid_view_insert_lines(gd, s->cy, ny, bg);
1178 screen_write_collect_flush(ctx, 0, __func__);
1179 ttyctx.num = ny;
1180 tty_write(tty_cmd_insertline, &ttyctx);
1181 return;
1184 if (ny > s->rlower + 1 - s->cy)
1185 ny = s->rlower + 1 - s->cy;
1186 if (ny == 0)
1187 return;
1189 screen_write_initctx(ctx, &ttyctx, 1);
1190 ttyctx.bg = bg;
1192 if (s->cy < s->rupper || s->cy > s->rlower)
1193 grid_view_insert_lines(gd, s->cy, ny, bg);
1194 else
1195 grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1197 screen_write_collect_flush(ctx, 0, __func__);
1199 ttyctx.num = ny;
1200 tty_write(tty_cmd_insertline, &ttyctx);
1203 /* Delete ny lines. */
1204 void
1205 screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1207 struct screen *s = ctx->s;
1208 struct grid *gd = s->grid;
1209 struct tty_ctx ttyctx;
1210 u_int sy = screen_size_y(s);
1212 if (ny == 0)
1213 ny = 1;
1215 #ifdef ENABLE_SIXEL
1216 if (image_check_line(s, s->cy, sy - s->cy) && ctx->wp != NULL)
1217 ctx->wp->flags |= PANE_REDRAW;
1218 #endif
1220 if (s->cy < s->rupper || s->cy > s->rlower) {
1221 if (ny > sy - s->cy)
1222 ny = sy - s->cy;
1223 if (ny == 0)
1224 return;
1226 screen_write_initctx(ctx, &ttyctx, 1);
1227 ttyctx.bg = bg;
1229 grid_view_delete_lines(gd, s->cy, ny, bg);
1231 screen_write_collect_flush(ctx, 0, __func__);
1232 ttyctx.num = ny;
1233 tty_write(tty_cmd_deleteline, &ttyctx);
1234 return;
1237 if (ny > s->rlower + 1 - s->cy)
1238 ny = s->rlower + 1 - s->cy;
1239 if (ny == 0)
1240 return;
1242 screen_write_initctx(ctx, &ttyctx, 1);
1243 ttyctx.bg = bg;
1245 if (s->cy < s->rupper || s->cy > s->rlower)
1246 grid_view_delete_lines(gd, s->cy, ny, bg);
1247 else
1248 grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1250 screen_write_collect_flush(ctx, 0, __func__);
1251 ttyctx.num = ny;
1252 tty_write(tty_cmd_deleteline, &ttyctx);
1255 /* Clear line at cursor. */
1256 void
1257 screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1259 struct screen *s = ctx->s;
1260 struct grid_line *gl;
1261 u_int sx = screen_size_x(s);
1262 struct screen_write_citem *ci = ctx->item;
1264 gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1265 if (gl->cellsize == 0 && COLOUR_DEFAULT(bg))
1266 return;
1268 #ifdef ENABLE_SIXEL
1269 if (image_check_line(s, s->cy, 1) && ctx->wp != NULL)
1270 ctx->wp->flags |= PANE_REDRAW;
1271 #endif
1273 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1275 screen_write_collect_clear(ctx, s->cy, 1);
1276 ci->x = 0;
1277 ci->used = sx;
1278 ci->type = CLEAR;
1279 ci->bg = bg;
1280 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1281 ctx->item = screen_write_get_citem();
1284 /* Clear to end of line from cursor. */
1285 void
1286 screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1288 struct screen *s = ctx->s;
1289 struct grid_line *gl;
1290 u_int sx = screen_size_x(s);
1291 struct screen_write_citem *ci = ctx->item, *before;
1293 if (s->cx == 0) {
1294 screen_write_clearline(ctx, bg);
1295 return;
1298 gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1299 if (s->cx > sx - 1 || (s->cx >= gl->cellsize && COLOUR_DEFAULT(bg)))
1300 return;
1302 #ifdef ENABLE_SIXEL
1303 if (image_check_line(s, s->cy, 1) && ctx->wp != NULL)
1304 ctx->wp->flags |= PANE_REDRAW;
1305 #endif
1307 grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
1309 before = screen_write_collect_trim(ctx, s->cy, s->cx, sx - s->cx, NULL);
1310 ci->x = s->cx;
1311 ci->used = sx - s->cx;
1312 ci->type = CLEAR;
1313 ci->bg = bg;
1314 if (before == NULL)
1315 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1316 else
1317 TAILQ_INSERT_BEFORE(before, ci, entry);
1318 ctx->item = screen_write_get_citem();
1321 /* Clear to start of line from cursor. */
1322 void
1323 screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1325 struct screen *s = ctx->s;
1326 u_int sx = screen_size_x(s);
1327 struct screen_write_citem *ci = ctx->item, *before;
1329 if (s->cx >= sx - 1) {
1330 screen_write_clearline(ctx, bg);
1331 return;
1334 #ifdef ENABLE_SIXEL
1335 if (image_check_line(s, s->cy, 1) && ctx->wp != NULL)
1336 ctx->wp->flags |= PANE_REDRAW;
1337 #endif
1339 if (s->cx > sx - 1)
1340 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1341 else
1342 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1344 before = screen_write_collect_trim(ctx, s->cy, 0, s->cx + 1, NULL);
1345 ci->x = 0;
1346 ci->used = s->cx + 1;
1347 ci->type = CLEAR;
1348 ci->bg = bg;
1349 if (before == NULL)
1350 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1351 else
1352 TAILQ_INSERT_BEFORE(before, ci, entry);
1353 ctx->item = screen_write_get_citem();
1356 /* Move cursor to px,py. */
1357 void
1358 screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py,
1359 int origin)
1361 struct screen *s = ctx->s;
1363 if (origin && py != -1 && (s->mode & MODE_ORIGIN)) {
1364 if ((u_int)py > s->rlower - s->rupper)
1365 py = s->rlower;
1366 else
1367 py += s->rupper;
1370 if (px != -1 && (u_int)px > screen_size_x(s) - 1)
1371 px = screen_size_x(s) - 1;
1372 if (py != -1 && (u_int)py > screen_size_y(s) - 1)
1373 py = screen_size_y(s) - 1;
1375 log_debug("%s: from %u,%u to %u,%u", __func__, s->cx, s->cy, px, py);
1376 screen_write_set_cursor(ctx, px, py);
1379 /* Reverse index (up with scroll). */
1380 void
1381 screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
1383 struct screen *s = ctx->s;
1384 struct tty_ctx ttyctx;
1386 if (s->cy == s->rupper) {
1387 #ifdef ENABLE_SIXEL
1388 if (image_free_all(s) && ctx->wp != NULL)
1389 ctx->wp->flags |= PANE_REDRAW;
1390 #endif
1392 grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1393 screen_write_collect_flush(ctx, 0, __func__);
1395 screen_write_initctx(ctx, &ttyctx, 1);
1396 ttyctx.bg = bg;
1398 tty_write(tty_cmd_reverseindex, &ttyctx);
1399 } else if (s->cy > 0)
1400 screen_write_set_cursor(ctx, -1, s->cy - 1);
1404 /* Set scroll region. */
1405 void
1406 screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
1407 u_int rlower)
1409 struct screen *s = ctx->s;
1411 if (rupper > screen_size_y(s) - 1)
1412 rupper = screen_size_y(s) - 1;
1413 if (rlower > screen_size_y(s) - 1)
1414 rlower = screen_size_y(s) - 1;
1415 if (rupper >= rlower) /* cannot be one line */
1416 return;
1418 screen_write_collect_flush(ctx, 0, __func__);
1420 /* Cursor moves to top-left. */
1421 screen_write_set_cursor(ctx, 0, 0);
1423 s->rupper = rupper;
1424 s->rlower = rlower;
1427 /* Line feed. */
1428 void
1429 screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1431 struct screen *s = ctx->s;
1432 struct grid *gd = s->grid;
1433 struct grid_line *gl;
1434 #ifdef ENABLE_SIXEL
1435 int redraw = 0;
1436 #endif
1437 u_int rupper = s->rupper, rlower = s->rlower;
1439 gl = grid_get_line(gd, gd->hsize + s->cy);
1440 if (wrapped)
1441 gl->flags |= GRID_LINE_WRAPPED;
1443 log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1444 rupper, rlower);
1446 if (bg != ctx->bg) {
1447 screen_write_collect_flush(ctx, 1, __func__);
1448 ctx->bg = bg;
1451 if (s->cy == s->rlower) {
1452 #ifdef ENABLE_SIXEL
1453 if (rlower == screen_size_y(s) - 1)
1454 redraw = image_scroll_up(s, 1);
1455 else
1456 redraw = image_check_line(s, rupper, rlower - rupper);
1457 if (redraw && ctx->wp != NULL)
1458 ctx->wp->flags |= PANE_REDRAW;
1459 #endif
1460 grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1461 screen_write_collect_scroll(ctx, bg);
1462 ctx->scrolled++;
1463 } else if (s->cy < screen_size_y(s) - 1)
1464 screen_write_set_cursor(ctx, -1, s->cy + 1);
1467 /* Scroll up. */
1468 void
1469 screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1471 struct screen *s = ctx->s;
1472 struct grid *gd = s->grid;
1473 u_int i;
1475 if (lines == 0)
1476 lines = 1;
1477 else if (lines > s->rlower - s->rupper + 1)
1478 lines = s->rlower - s->rupper + 1;
1480 if (bg != ctx->bg) {
1481 screen_write_collect_flush(ctx, 1, __func__);
1482 ctx->bg = bg;
1485 #ifdef ENABLE_SIXEL
1486 if (image_scroll_up(s, lines) && ctx->wp != NULL)
1487 ctx->wp->flags |= PANE_REDRAW;
1488 #endif
1490 for (i = 0; i < lines; i++) {
1491 grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1492 screen_write_collect_scroll(ctx, bg);
1494 ctx->scrolled += lines;
1497 /* Scroll down. */
1498 void
1499 screen_write_scrolldown(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1501 struct screen *s = ctx->s;
1502 struct grid *gd = s->grid;
1503 struct tty_ctx ttyctx;
1504 u_int i;
1506 screen_write_initctx(ctx, &ttyctx, 1);
1507 ttyctx.bg = bg;
1509 if (lines == 0)
1510 lines = 1;
1511 else if (lines > s->rlower - s->rupper + 1)
1512 lines = s->rlower - s->rupper + 1;
1514 #ifdef ENABLE_SIXEL
1515 if (image_free_all(s) && ctx->wp != NULL)
1516 ctx->wp->flags |= PANE_REDRAW;
1517 #endif
1519 for (i = 0; i < lines; i++)
1520 grid_view_scroll_region_down(gd, s->rupper, s->rlower, bg);
1522 screen_write_collect_flush(ctx, 0, __func__);
1523 ttyctx.num = lines;
1524 tty_write(tty_cmd_scrolldown, &ttyctx);
1527 /* Carriage return (cursor to start of line). */
1528 void
1529 screen_write_carriagereturn(struct screen_write_ctx *ctx)
1531 screen_write_set_cursor(ctx, 0, -1);
1534 /* Clear to end of screen from cursor. */
1535 void
1536 screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1538 struct screen *s = ctx->s;
1539 struct grid *gd = s->grid;
1540 struct tty_ctx ttyctx;
1541 u_int sx = screen_size_x(s), sy = screen_size_y(s);
1543 #ifdef ENABLE_SIXEL
1544 if (image_check_line(s, s->cy, sy - s->cy) && ctx->wp != NULL)
1545 ctx->wp->flags |= PANE_REDRAW;
1546 #endif
1548 screen_write_initctx(ctx, &ttyctx, 1);
1549 ttyctx.bg = bg;
1551 /* Scroll into history if it is enabled and clearing entire screen. */
1552 if (s->cx == 0 &&
1553 s->cy == 0 &&
1554 (gd->flags & GRID_HISTORY) &&
1555 ctx->wp != NULL &&
1556 options_get_number(ctx->wp->options, "scroll-on-clear"))
1557 grid_view_clear_history(gd, bg);
1558 else {
1559 if (s->cx <= sx - 1)
1560 grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
1561 grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1564 screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1565 screen_write_collect_flush(ctx, 0, __func__);
1566 tty_write(tty_cmd_clearendofscreen, &ttyctx);
1569 /* Clear to start of screen. */
1570 void
1571 screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1573 struct screen *s = ctx->s;
1574 struct tty_ctx ttyctx;
1575 u_int sx = screen_size_x(s);
1577 #ifdef ENABLE_SIXEL
1578 if (image_check_line(s, 0, s->cy - 1) && ctx->wp != NULL)
1579 ctx->wp->flags |= PANE_REDRAW;
1580 #endif
1582 screen_write_initctx(ctx, &ttyctx, 1);
1583 ttyctx.bg = bg;
1585 if (s->cy > 0)
1586 grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1587 if (s->cx > sx - 1)
1588 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1589 else
1590 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1592 screen_write_collect_clear(ctx, 0, s->cy);
1593 screen_write_collect_flush(ctx, 0, __func__);
1594 tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1597 /* Clear entire screen. */
1598 void
1599 screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1601 struct screen *s = ctx->s;
1602 struct tty_ctx ttyctx;
1603 u_int sx = screen_size_x(s), sy = screen_size_y(s);
1605 #ifdef ENABLE_SIXEL
1606 if (image_free_all(s) && ctx->wp != NULL)
1607 ctx->wp->flags |= PANE_REDRAW;
1608 #endif
1610 screen_write_initctx(ctx, &ttyctx, 1);
1611 ttyctx.bg = bg;
1613 /* Scroll into history if it is enabled. */
1614 if ((s->grid->flags & GRID_HISTORY) &&
1615 ctx->wp != NULL &&
1616 options_get_number(ctx->wp->options, "scroll-on-clear"))
1617 grid_view_clear_history(s->grid, bg);
1618 else
1619 grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1621 screen_write_collect_clear(ctx, 0, sy);
1622 tty_write(tty_cmd_clearscreen, &ttyctx);
1625 /* Clear entire history. */
1626 void
1627 screen_write_clearhistory(struct screen_write_ctx *ctx)
1629 grid_clear_history(ctx->s->grid);
1632 /* Force a full redraw. */
1633 void
1634 screen_write_fullredraw(struct screen_write_ctx *ctx)
1636 struct tty_ctx ttyctx;
1638 screen_write_collect_flush(ctx, 0, __func__);
1640 screen_write_initctx(ctx, &ttyctx, 1);
1641 if (ttyctx.redraw_cb != NULL)
1642 ttyctx.redraw_cb(&ttyctx);
1645 /* Trim collected items. */
1646 static struct screen_write_citem *
1647 screen_write_collect_trim(struct screen_write_ctx *ctx, u_int y, u_int x,
1648 u_int used, int *wrapped)
1650 struct screen_write_cline *cl = &ctx->s->write_list[y];
1651 struct screen_write_citem *ci, *ci2, *tmp, *before = NULL;
1652 u_int sx = x, ex = x + used - 1;
1653 u_int csx, cex;
1655 if (TAILQ_EMPTY(&cl->items))
1656 return (NULL);
1657 TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) {
1658 csx = ci->x;
1659 cex = ci->x + ci->used - 1;
1661 /* Item is entirely before. */
1662 if (cex < sx) {
1663 log_debug("%s: %p %u-%u before %u-%u", __func__, ci,
1664 csx, cex, sx, ex);
1665 continue;
1668 /* Item is entirely after. */
1669 if (csx > ex) {
1670 log_debug("%s: %p %u-%u after %u-%u", __func__, ci,
1671 csx, cex, sx, ex);
1672 before = ci;
1673 break;
1676 /* Item is entirely inside. */
1677 if (csx >= sx && cex <= ex) {
1678 log_debug("%s: %p %u-%u inside %u-%u", __func__, ci,
1679 csx, cex, sx, ex);
1680 TAILQ_REMOVE(&cl->items, ci, entry);
1681 screen_write_free_citem(ci);
1682 if (csx == 0 && ci->wrapped && wrapped != NULL)
1683 *wrapped = 1;
1684 continue;
1687 /* Item under the start. */
1688 if (csx < sx && cex >= sx && cex <= ex) {
1689 log_debug("%s: %p %u-%u start %u-%u", __func__, ci,
1690 csx, cex, sx, ex);
1691 ci->used = sx - csx;
1692 log_debug("%s: %p now %u-%u", __func__, ci, ci->x,
1693 ci->x + ci->used + 1);
1694 continue;
1697 /* Item covers the end. */
1698 if (cex > ex && csx >= sx && csx <= ex) {
1699 log_debug("%s: %p %u-%u end %u-%u", __func__, ci,
1700 csx, cex, sx, ex);
1701 ci->x = ex + 1;
1702 ci->used = cex - ex;
1703 log_debug("%s: %p now %u-%u", __func__, ci, ci->x,
1704 ci->x + ci->used + 1);
1705 before = ci;
1706 break;
1709 /* Item must cover both sides. */
1710 log_debug("%s: %p %u-%u under %u-%u", __func__, ci,
1711 csx, cex, sx, ex);
1712 ci2 = screen_write_get_citem();
1713 ci2->type = ci->type;
1714 ci2->bg = ci->bg;
1715 memcpy(&ci2->gc, &ci->gc, sizeof ci2->gc);
1716 TAILQ_INSERT_AFTER(&cl->items, ci, ci2, entry);
1718 ci->used = sx - csx;
1719 ci2->x = ex + 1;
1720 ci2->used = cex - ex;
1722 log_debug("%s: %p now %u-%u (%p) and %u-%u (%p)", __func__, ci,
1723 ci->x, ci->x + ci->used - 1, ci, ci2->x,
1724 ci2->x + ci2->used - 1, ci2);
1725 before = ci2;
1726 break;
1728 return (before);
1731 /* Clear collected lines. */
1732 static void
1733 screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
1735 struct screen_write_cline *cl;
1736 u_int i;
1738 for (i = y; i < y + n; i++) {
1739 cl = &ctx->s->write_list[i];
1740 TAILQ_CONCAT(&screen_write_citem_freelist, &cl->items, entry);
1744 /* Scroll collected lines up. */
1745 static void
1746 screen_write_collect_scroll(struct screen_write_ctx *ctx, u_int bg)
1748 struct screen *s = ctx->s;
1749 struct screen_write_cline *cl;
1750 u_int y;
1751 char *saved;
1752 struct screen_write_citem *ci;
1754 log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1755 s->rupper, s->rlower);
1757 screen_write_collect_clear(ctx, s->rupper, 1);
1758 saved = ctx->s->write_list[s->rupper].data;
1759 for (y = s->rupper; y < s->rlower; y++) {
1760 cl = &ctx->s->write_list[y + 1];
1761 TAILQ_CONCAT(&ctx->s->write_list[y].items, &cl->items, entry);
1762 ctx->s->write_list[y].data = cl->data;
1764 ctx->s->write_list[s->rlower].data = saved;
1766 ci = screen_write_get_citem();
1767 ci->x = 0;
1768 ci->used = screen_size_x(s);
1769 ci->type = CLEAR;
1770 ci->bg = bg;
1771 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->rlower].items, ci, entry);
1774 /* Flush collected lines. */
1775 static void
1776 screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only,
1777 const char *from)
1779 struct screen *s = ctx->s;
1780 struct screen_write_citem *ci, *tmp;
1781 struct screen_write_cline *cl;
1782 u_int y, cx, cy, last, items = 0;
1783 struct tty_ctx ttyctx;
1785 if (ctx->scrolled != 0) {
1786 log_debug("%s: scrolled %u (region %u-%u)", __func__,
1787 ctx->scrolled, s->rupper, s->rlower);
1788 if (ctx->scrolled > s->rlower - s->rupper + 1)
1789 ctx->scrolled = s->rlower - s->rupper + 1;
1791 screen_write_initctx(ctx, &ttyctx, 1);
1792 ttyctx.num = ctx->scrolled;
1793 ttyctx.bg = ctx->bg;
1794 tty_write(tty_cmd_scrollup, &ttyctx);
1796 ctx->scrolled = 0;
1797 ctx->bg = 8;
1799 if (scroll_only)
1800 return;
1802 cx = s->cx; cy = s->cy;
1803 for (y = 0; y < screen_size_y(s); y++) {
1804 cl = &ctx->s->write_list[y];
1805 last = UINT_MAX;
1806 TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) {
1807 if (last != UINT_MAX && ci->x <= last) {
1808 fatalx("collect list not in order: %u <= %u",
1809 ci->x, last);
1811 screen_write_set_cursor(ctx, ci->x, y);
1812 if (ci->type == CLEAR) {
1813 screen_write_initctx(ctx, &ttyctx, 1);
1814 ttyctx.bg = ci->bg;
1815 ttyctx.num = ci->used;
1816 tty_write(tty_cmd_clearcharacter, &ttyctx);
1817 } else {
1818 screen_write_initctx(ctx, &ttyctx, 0);
1819 ttyctx.cell = &ci->gc;
1820 ttyctx.wrapped = ci->wrapped;
1821 ttyctx.ptr = cl->data + ci->x;
1822 ttyctx.num = ci->used;
1823 tty_write(tty_cmd_cells, &ttyctx);
1825 items++;
1827 TAILQ_REMOVE(&cl->items, ci, entry);
1828 screen_write_free_citem(ci);
1829 last = ci->x;
1832 s->cx = cx; s->cy = cy;
1834 log_debug("%s: flushed %u items (%s)", __func__, items, from);
1837 /* Finish and store collected cells. */
1838 void
1839 screen_write_collect_end(struct screen_write_ctx *ctx)
1841 struct screen *s = ctx->s;
1842 struct screen_write_citem *ci = ctx->item, *before;
1843 struct screen_write_cline *cl = &s->write_list[s->cy];
1844 struct grid_cell gc;
1845 u_int xx;
1846 int wrapped = ci->wrapped;
1848 if (ci->used == 0)
1849 return;
1851 before = screen_write_collect_trim(ctx, s->cy, s->cx, ci->used,
1852 &wrapped);
1853 ci->x = s->cx;
1854 ci->wrapped = wrapped;
1855 if (before == NULL)
1856 TAILQ_INSERT_TAIL(&cl->items, ci, entry);
1857 else
1858 TAILQ_INSERT_BEFORE(before, ci, entry);
1859 ctx->item = screen_write_get_citem();
1861 log_debug("%s: %u %.*s (at %u,%u)", __func__, ci->used,
1862 (int)ci->used, cl->data + ci->x, s->cx, s->cy);
1864 if (s->cx != 0) {
1865 for (xx = s->cx; xx > 0; xx--) {
1866 grid_view_get_cell(s->grid, xx, s->cy, &gc);
1867 if (~gc.flags & GRID_FLAG_PADDING)
1868 break;
1869 grid_view_set_cell(s->grid, xx, s->cy,
1870 &grid_default_cell);
1872 if (gc.data.width > 1) {
1873 grid_view_set_cell(s->grid, xx, s->cy,
1874 &grid_default_cell);
1878 #ifdef ENABLE_SIXEL
1879 if (image_check_area(s, s->cx, s->cy, ci->used, 1) && ctx->wp != NULL)
1880 ctx->wp->flags |= PANE_REDRAW;
1881 #endif
1883 grid_view_set_cells(s->grid, s->cx, s->cy, &ci->gc, cl->data + ci->x,
1884 ci->used);
1885 screen_write_set_cursor(ctx, s->cx + ci->used, -1);
1887 for (xx = s->cx; xx < screen_size_x(s); xx++) {
1888 grid_view_get_cell(s->grid, xx, s->cy, &gc);
1889 if (~gc.flags & GRID_FLAG_PADDING)
1890 break;
1891 grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
1895 /* Write cell data, collecting if necessary. */
1896 void
1897 screen_write_collect_add(struct screen_write_ctx *ctx,
1898 const struct grid_cell *gc)
1900 struct screen *s = ctx->s;
1901 struct screen_write_citem *ci;
1902 u_int sx = screen_size_x(s);
1903 int collect;
1906 * Don't need to check that the attributes and whatnot are still the
1907 * same - input_parse will end the collection when anything that isn't
1908 * a plain character is encountered.
1911 collect = 1;
1912 if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f)
1913 collect = 0;
1914 else if (gc->attr & GRID_ATTR_CHARSET)
1915 collect = 0;
1916 else if (~s->mode & MODE_WRAP)
1917 collect = 0;
1918 else if (s->mode & MODE_INSERT)
1919 collect = 0;
1920 else if (s->sel != NULL)
1921 collect = 0;
1922 if (!collect) {
1923 screen_write_collect_end(ctx);
1924 screen_write_collect_flush(ctx, 0, __func__);
1925 screen_write_cell(ctx, gc);
1926 return;
1929 if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
1930 screen_write_collect_end(ctx);
1931 ci = ctx->item; /* may have changed */
1933 if (s->cx > sx - 1) {
1934 log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1935 ci->wrapped = 1;
1936 screen_write_linefeed(ctx, 1, 8);
1937 screen_write_set_cursor(ctx, 0, -1);
1940 if (ci->used == 0)
1941 memcpy(&ci->gc, gc, sizeof ci->gc);
1942 if (ctx->s->write_list[s->cy].data == NULL)
1943 ctx->s->write_list[s->cy].data = xmalloc(screen_size_x(ctx->s));
1944 ctx->s->write_list[s->cy].data[s->cx + ci->used++] = gc->data.data[0];
1947 /* Write cell data. */
1948 void
1949 screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1951 struct screen *s = ctx->s;
1952 struct grid *gd = s->grid;
1953 const struct utf8_data *ud = &gc->data;
1954 struct grid_line *gl;
1955 struct grid_cell_entry *gce;
1956 struct grid_cell tmp_gc, now_gc;
1957 struct tty_ctx ttyctx;
1958 u_int sx = screen_size_x(s), sy = screen_size_y(s);
1959 u_int width = ud->width, xx, not_wrap;
1960 int selected, skip = 1;
1962 /* Ignore padding cells. */
1963 if (gc->flags & GRID_FLAG_PADDING)
1964 return;
1966 /* Get the previous cell to check for combining. */
1967 if (screen_write_combine(ctx, gc) != 0)
1968 return;
1970 /* Flush any existing scrolling. */
1971 screen_write_collect_flush(ctx, 1, __func__);
1973 /* If this character doesn't fit, ignore it. */
1974 if ((~s->mode & MODE_WRAP) &&
1975 width > 1 &&
1976 (width > sx || (s->cx != sx && s->cx > sx - width)))
1977 return;
1979 /* If in insert mode, make space for the cells. */
1980 if (s->mode & MODE_INSERT) {
1981 grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
1982 skip = 0;
1985 /* Check this will fit on the current line and wrap if not. */
1986 if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1987 log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1988 screen_write_linefeed(ctx, 1, 8);
1989 screen_write_set_cursor(ctx, 0, -1);
1990 screen_write_collect_flush(ctx, 1, __func__);
1993 /* Sanity check cursor position. */
1994 if (s->cx > sx - width || s->cy > sy - 1)
1995 return;
1996 screen_write_initctx(ctx, &ttyctx, 0);
1998 /* Handle overwriting of UTF-8 characters. */
1999 gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
2000 if (gl->flags & GRID_LINE_EXTENDED) {
2001 grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
2002 if (screen_write_overwrite(ctx, &now_gc, width))
2003 skip = 0;
2007 * If the new character is UTF-8 wide, fill in padding cells. Have
2008 * already ensured there is enough room.
2010 for (xx = s->cx + 1; xx < s->cx + width; xx++) {
2011 log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
2012 grid_view_set_padding(gd, xx, s->cy);
2013 skip = 0;
2016 /* If no change, do not draw. */
2017 if (skip) {
2018 if (s->cx >= gl->cellsize)
2019 skip = grid_cells_equal(gc, &grid_default_cell);
2020 else {
2021 gce = &gl->celldata[s->cx];
2022 if (gce->flags & GRID_FLAG_EXTENDED)
2023 skip = 0;
2024 else if (gc->flags != gce->flags)
2025 skip = 0;
2026 else if (gc->attr != gce->data.attr)
2027 skip = 0;
2028 else if (gc->fg != gce->data.fg)
2029 skip = 0;
2030 else if (gc->bg != gce->data.bg)
2031 skip = 0;
2032 else if (gc->data.width != 1)
2033 skip = 0;
2034 else if (gc->data.size != 1)
2035 skip = 0;
2036 else if (gce->data.data != gc->data.data[0])
2037 skip = 0;
2041 /* Update the selected flag and set the cell. */
2042 selected = screen_check_selection(s, s->cx, s->cy);
2043 if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
2044 memcpy(&tmp_gc, gc, sizeof tmp_gc);
2045 tmp_gc.flags |= GRID_FLAG_SELECTED;
2046 grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
2047 } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
2048 memcpy(&tmp_gc, gc, sizeof tmp_gc);
2049 tmp_gc.flags &= ~GRID_FLAG_SELECTED;
2050 grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
2051 } else if (!skip)
2052 grid_view_set_cell(gd, s->cx, s->cy, gc);
2053 if (selected)
2054 skip = 0;
2057 * Move the cursor. If not wrapping, stick at the last character and
2058 * replace it.
2060 not_wrap = !(s->mode & MODE_WRAP);
2061 if (s->cx <= sx - not_wrap - width)
2062 screen_write_set_cursor(ctx, s->cx + width, -1);
2063 else
2064 screen_write_set_cursor(ctx, sx - not_wrap, -1);
2066 /* Create space for character in insert mode. */
2067 if (s->mode & MODE_INSERT) {
2068 screen_write_collect_flush(ctx, 0, __func__);
2069 ttyctx.num = width;
2070 tty_write(tty_cmd_insertcharacter, &ttyctx);
2073 /* Write to the screen. */
2074 if (!skip) {
2075 if (selected) {
2076 screen_select_cell(s, &tmp_gc, gc);
2077 ttyctx.cell = &tmp_gc;
2078 } else
2079 ttyctx.cell = gc;
2080 tty_write(tty_cmd_cell, &ttyctx);
2084 /* Combine a UTF-8 zero-width character onto the previous if necessary. */
2085 static int
2086 screen_write_combine(struct screen_write_ctx *ctx, const struct grid_cell *gc)
2088 struct screen *s = ctx->s;
2089 struct grid *gd = s->grid;
2090 const struct utf8_data *ud = &gc->data;
2091 u_int n, cx = s->cx, cy = s->cy;
2092 struct grid_cell last;
2093 struct tty_ctx ttyctx;
2094 int force_wide = 0, zero_width = 0;
2097 * Is this character which makes no sense without being combined? If
2098 * this is true then flag it here and discard the character (return 1)
2099 * if we cannot combine it.
2101 if (utf8_is_zwj(ud))
2102 zero_width = 1;
2103 else if (utf8_is_vs(ud))
2104 zero_width = force_wide = 1;
2105 else if (ud->width == 0)
2106 zero_width = 1;
2108 /* Cannot combine empty character or at left. */
2109 if (ud->size < 2 || cx == 0)
2110 return (zero_width);
2111 log_debug("%s: character %.*s at %u,%u (width %u)", __func__,
2112 (int)ud->size, ud->data, cx, cy, ud->width);
2114 /* Find the cell to combine with. */
2115 n = 1;
2116 grid_view_get_cell(gd, cx - n, cy, &last);
2117 if (cx != 1 && (last.flags & GRID_FLAG_PADDING)) {
2118 n = 2;
2119 grid_view_get_cell(gd, cx - n, cy, &last);
2121 if (n != last.data.width || (last.flags & GRID_FLAG_PADDING))
2122 return (zero_width);
2125 * Check if we need to combine characters. This could be zero width
2126 * (set above), a modifier character (with an existing Unicode
2127 * character) or a previous ZWJ.
2129 if (!zero_width) {
2130 if (utf8_is_modifier(ud)) {
2131 if (last.data.size < 2)
2132 return (0);
2133 force_wide = 1;
2134 } else if (!utf8_has_zwj(&last.data))
2135 return (0);
2138 /* Check if this combined character would be too long. */
2139 if (last.data.size + ud->size > sizeof last.data.data)
2140 return (0);
2142 /* Combining; flush any pending output. */
2143 screen_write_collect_flush(ctx, 0, __func__);
2145 log_debug("%s: %.*s -> %.*s at %u,%u (offset %u, width %u)", __func__,
2146 (int)ud->size, ud->data, (int)last.data.size, last.data.data,
2147 cx - n, cy, n, last.data.width);
2149 /* Append the data. */
2150 memcpy(last.data.data + last.data.size, ud->data, ud->size);
2151 last.data.size += ud->size;
2153 /* Force the width to 2 for modifiers and variation selector. */
2154 if (last.data.width == 1 && force_wide) {
2155 last.data.width = 2;
2156 n = 2;
2157 cx++;
2158 } else
2159 force_wide = 0;
2161 /* Set the new cell. */
2162 grid_view_set_cell(gd, cx - n, cy, &last);
2163 if (force_wide)
2164 grid_view_set_padding(gd, cx - 1, cy);
2167 * Redraw the combined cell. If forcing the cell to width 2, reset the
2168 * cached cursor position in the tty, since we don't really know
2169 * whether the terminal thought the character was width 1 or width 2
2170 * and what it is going to do now.
2172 screen_write_set_cursor(ctx, cx - n, cy);
2173 screen_write_initctx(ctx, &ttyctx, 0);
2174 ttyctx.cell = &last;
2175 ttyctx.num = force_wide; /* reset cached cursor position */
2176 tty_write(tty_cmd_cell, &ttyctx);
2177 screen_write_set_cursor(ctx, cx, cy);
2179 return (1);
2183 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
2184 * cell on the screen, so following cells must not be drawn by marking them as
2185 * padding.
2187 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
2188 * character, it is necessary to also overwrite any other cells which covered
2189 * by the same character.
2191 static int
2192 screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
2193 u_int width)
2195 struct screen *s = ctx->s;
2196 struct grid *gd = s->grid;
2197 struct grid_cell tmp_gc;
2198 u_int xx;
2199 int done = 0;
2201 if (gc->flags & GRID_FLAG_PADDING) {
2203 * A padding cell, so clear any following and leading padding
2204 * cells back to the character. Don't overwrite the current
2205 * cell as that happens later anyway.
2207 xx = s->cx + 1;
2208 while (--xx > 0) {
2209 grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
2210 if (~tmp_gc.flags & GRID_FLAG_PADDING)
2211 break;
2212 log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
2213 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2216 /* Overwrite the character at the start of this padding. */
2217 log_debug("%s: character at %u,%u", __func__, xx, s->cy);
2218 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2219 done = 1;
2223 * Overwrite any padding cells that belong to any UTF-8 characters
2224 * we'll be overwriting with the current character.
2226 if (width != 1 ||
2227 gc->data.width != 1 ||
2228 gc->flags & GRID_FLAG_PADDING) {
2229 xx = s->cx + width - 1;
2230 while (++xx < screen_size_x(s)) {
2231 grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
2232 if (~tmp_gc.flags & GRID_FLAG_PADDING)
2233 break;
2234 log_debug("%s: overwrite at %u,%u", __func__, xx,
2235 s->cy);
2236 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2237 done = 1;
2241 return (done);
2244 /* Set external clipboard. */
2245 void
2246 screen_write_setselection(struct screen_write_ctx *ctx, const char *flags,
2247 u_char *str, u_int len)
2249 struct tty_ctx ttyctx;
2251 screen_write_initctx(ctx, &ttyctx, 0);
2252 ttyctx.ptr = str;
2253 ttyctx.ptr2 = (void *)flags;
2254 ttyctx.num = len;
2256 tty_write(tty_cmd_setselection, &ttyctx);
2259 /* Write unmodified string. */
2260 void
2261 screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len,
2262 int allow_invisible_panes)
2264 struct tty_ctx ttyctx;
2266 screen_write_initctx(ctx, &ttyctx, 0);
2267 ttyctx.ptr = str;
2268 ttyctx.num = len;
2269 ttyctx.allow_invisible_panes = allow_invisible_panes;
2271 tty_write(tty_cmd_rawstring, &ttyctx);
2274 #ifdef ENABLE_SIXEL
2275 /* Write a SIXEL image. */
2276 void
2277 screen_write_sixelimage(struct screen_write_ctx *ctx, struct sixel_image *si,
2278 u_int bg)
2280 struct screen *s = ctx->s;
2281 struct grid *gd = s->grid;
2282 struct tty_ctx ttyctx;
2283 u_int x, y, sx, sy, cx = s->cx, cy = s->cy, i, lines;
2284 struct sixel_image *new;
2286 sixel_size_in_cells(si, &x, &y);
2287 if (x > screen_size_x(s) || y > screen_size_y(s)) {
2288 if (x > screen_size_x(s) - cx)
2289 sx = screen_size_x(s) - cx;
2290 else
2291 sx = x;
2292 if (y > screen_size_y(s) - 1)
2293 sy = screen_size_y(s) - 1;
2294 else
2295 sy = y;
2296 new = sixel_scale(si, 0, 0, 0, y - sy, sx, sy, 1);
2297 sixel_free(si);
2298 si = new;
2300 /* Bail out if the image cannot be scaled. */
2301 if (si == NULL)
2302 return;
2303 sixel_size_in_cells(si, &x, &y);
2306 sy = screen_size_y(s) - cy;
2307 if (sy < y) {
2308 lines = y - sy + 1;
2309 if (image_scroll_up(s, lines) && ctx->wp != NULL)
2310 ctx->wp->flags |= PANE_REDRAW;
2311 for (i = 0; i < lines; i++) {
2312 grid_view_scroll_region_up(gd, 0, screen_size_y(s) - 1,
2313 bg);
2314 screen_write_collect_scroll(ctx, bg);
2316 ctx->scrolled += lines;
2317 if (lines > cy)
2318 screen_write_cursormove(ctx, -1, 0, 0);
2319 else
2320 screen_write_cursormove(ctx, -1, cy - lines, 0);
2322 screen_write_collect_flush(ctx, 0, __func__);
2324 screen_write_initctx(ctx, &ttyctx, 0);
2325 ttyctx.ptr = image_store(s, si);
2327 tty_write(tty_cmd_sixelimage, &ttyctx);
2329 screen_write_cursormove(ctx, 0, cy + y, 0);
2331 #endif
2333 /* Turn alternate screen on. */
2334 void
2335 screen_write_alternateon(struct screen_write_ctx *ctx, struct grid_cell *gc,
2336 int cursor)
2338 struct tty_ctx ttyctx;
2339 struct window_pane *wp = ctx->wp;
2341 if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
2342 return;
2344 screen_write_collect_flush(ctx, 0, __func__);
2345 screen_alternate_on(ctx->s, gc, cursor);
2347 screen_write_initctx(ctx, &ttyctx, 1);
2348 if (ttyctx.redraw_cb != NULL)
2349 ttyctx.redraw_cb(&ttyctx);
2352 /* Turn alternate screen off. */
2353 void
2354 screen_write_alternateoff(struct screen_write_ctx *ctx, struct grid_cell *gc,
2355 int cursor)
2357 struct tty_ctx ttyctx;
2358 struct window_pane *wp = ctx->wp;
2360 if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
2361 return;
2363 screen_write_collect_flush(ctx, 0, __func__);
2364 screen_alternate_off(ctx->s, gc, cursor);
2366 screen_write_initctx(ctx, &ttyctx, 1);
2367 if (ttyctx.redraw_cb != NULL)
2368 ttyctx.redraw_cb(&ttyctx);