Free title earlier, from Alexis Hildebrandt.
[tmux-openbsd.git] / screen-write.c
blobd7c196e183d76601113741cad838a69498847a55
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 *);
34 static int screen_write_overwrite(struct screen_write_ctx *,
35 struct grid_cell *, u_int);
36 static const struct grid_cell *screen_write_combine(struct screen_write_ctx *,
37 const struct utf8_data *, u_int *, u_int *);
39 struct screen_write_citem {
40 u_int x;
41 int wrapped;
43 enum { TEXT, CLEAR } type;
44 u_int used;
45 u_int bg;
47 struct grid_cell gc;
49 TAILQ_ENTRY(screen_write_citem) entry;
51 struct screen_write_cline {
52 char *data;
53 TAILQ_HEAD(, screen_write_citem) items;
55 TAILQ_HEAD(, screen_write_citem) screen_write_citem_freelist =
56 TAILQ_HEAD_INITIALIZER(screen_write_citem_freelist);
58 static struct screen_write_citem *
59 screen_write_get_citem(void)
61 struct screen_write_citem *ci;
63 ci = TAILQ_FIRST(&screen_write_citem_freelist);
64 if (ci != NULL) {
65 TAILQ_REMOVE(&screen_write_citem_freelist, ci, entry);
66 memset(ci, 0, sizeof *ci);
67 return (ci);
69 return (xcalloc(1, sizeof *ci));
72 static void
73 screen_write_free_citem(struct screen_write_citem *ci)
75 TAILQ_INSERT_TAIL(&screen_write_citem_freelist, ci, entry);
78 static void
79 screen_write_offset_timer(__unused int fd, __unused short events, void *data)
81 struct window *w = data;
83 tty_update_window_offset(w);
86 /* Set cursor position. */
87 static void
88 screen_write_set_cursor(struct screen_write_ctx *ctx, int cx, int cy)
90 struct window_pane *wp = ctx->wp;
91 struct window *w;
92 struct screen *s = ctx->s;
93 struct timeval tv = { .tv_usec = 10000 };
95 if (cx != -1 && (u_int)cx == s->cx && cy != -1 && (u_int)cy == s->cy)
96 return;
98 if (cx != -1) {
99 if ((u_int)cx > screen_size_x(s)) /* allow last column */
100 cx = screen_size_x(s) - 1;
101 s->cx = cx;
103 if (cy != -1) {
104 if ((u_int)cy > screen_size_y(s) - 1)
105 cy = screen_size_y(s) - 1;
106 s->cy = cy;
109 if (wp == NULL)
110 return;
111 w = wp->window;
113 if (!event_initialized(&w->offset_timer))
114 evtimer_set(&w->offset_timer, screen_write_offset_timer, w);
115 if (!evtimer_pending(&w->offset_timer, NULL))
116 evtimer_add(&w->offset_timer, &tv);
119 /* Do a full redraw. */
120 static void
121 screen_write_redraw_cb(const struct tty_ctx *ttyctx)
123 struct window_pane *wp = ttyctx->arg;
125 if (wp != NULL)
126 wp->flags |= PANE_REDRAW;
129 /* Update context for client. */
130 static int
131 screen_write_set_client_cb(struct tty_ctx *ttyctx, struct client *c)
133 struct window_pane *wp = ttyctx->arg;
135 if (ttyctx->allow_invisible_panes) {
136 if (session_has(c->session, wp->window))
137 return (1);
138 return (0);
141 if (c->session->curw->window != wp->window)
142 return (0);
143 if (wp->layout_cell == NULL)
144 return (0);
146 if (wp->flags & (PANE_REDRAW|PANE_DROP))
147 return (-1);
148 if (c->flags & CLIENT_REDRAWPANES) {
150 * Redraw is already deferred to redraw another pane - redraw
151 * this one also when that happens.
153 log_debug("%s: adding %%%u to deferred redraw", __func__,
154 wp->id);
155 wp->flags |= PANE_REDRAW;
156 return (-1);
159 ttyctx->bigger = tty_window_offset(&c->tty, &ttyctx->wox, &ttyctx->woy,
160 &ttyctx->wsx, &ttyctx->wsy);
162 ttyctx->xoff = ttyctx->rxoff = wp->xoff;
163 ttyctx->yoff = ttyctx->ryoff = wp->yoff;
165 if (status_at_line(c) == 0)
166 ttyctx->yoff += status_line_size(c);
168 return (1);
171 /* Set up context for TTY command. */
172 static void
173 screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
174 int sync)
176 struct screen *s = ctx->s;
178 memset(ttyctx, 0, sizeof *ttyctx);
180 ttyctx->s = s;
181 ttyctx->sx = screen_size_x(s);
182 ttyctx->sy = screen_size_y(s);
184 ttyctx->ocx = s->cx;
185 ttyctx->ocy = s->cy;
186 ttyctx->orlower = s->rlower;
187 ttyctx->orupper = s->rupper;
189 memcpy(&ttyctx->defaults, &grid_default_cell, sizeof ttyctx->defaults);
190 if (ctx->init_ctx_cb != NULL) {
191 ctx->init_ctx_cb(ctx, ttyctx);
192 if (ttyctx->palette != NULL) {
193 if (ttyctx->defaults.fg == 8)
194 ttyctx->defaults.fg = ttyctx->palette->fg;
195 if (ttyctx->defaults.bg == 8)
196 ttyctx->defaults.bg = ttyctx->palette->bg;
198 } else {
199 ttyctx->redraw_cb = screen_write_redraw_cb;
200 if (ctx->wp != NULL) {
201 tty_default_colours(&ttyctx->defaults, ctx->wp);
202 ttyctx->palette = &ctx->wp->palette;
203 ttyctx->set_client_cb = screen_write_set_client_cb;
204 ttyctx->arg = ctx->wp;
208 if (~ctx->flags & SCREEN_WRITE_SYNC) {
210 * For the active pane or for an overlay (no pane), we want to
211 * only use synchronized updates if requested (commands that
212 * move the cursor); for other panes, always use it, since the
213 * cursor will have to move.
215 if (ctx->wp != NULL) {
216 if (ctx->wp != ctx->wp->window->active)
217 ttyctx->num = 1;
218 else
219 ttyctx->num = sync;
220 } else
221 ttyctx->num = 0x10|sync;
222 tty_write(tty_cmd_syncstart, ttyctx);
223 ctx->flags |= SCREEN_WRITE_SYNC;
227 /* Make write list. */
228 void
229 screen_write_make_list(struct screen *s)
231 u_int y;
233 s->write_list = xcalloc(screen_size_y(s), sizeof *s->write_list);
234 for (y = 0; y < screen_size_y(s); y++)
235 TAILQ_INIT(&s->write_list[y].items);
238 /* Free write list. */
239 void
240 screen_write_free_list(struct screen *s)
242 u_int y;
244 for (y = 0; y < screen_size_y(s); y++)
245 free(s->write_list[y].data);
246 free(s->write_list);
249 /* Set up for writing. */
250 static void
251 screen_write_init(struct screen_write_ctx *ctx, struct screen *s)
253 memset(ctx, 0, sizeof *ctx);
255 ctx->s = s;
257 if (ctx->s->write_list == NULL)
258 screen_write_make_list(ctx->s);
259 ctx->item = screen_write_get_citem();
261 ctx->scrolled = 0;
262 ctx->bg = 8;
265 /* Initialize writing with a pane. */
266 void
267 screen_write_start_pane(struct screen_write_ctx *ctx, struct window_pane *wp,
268 struct screen *s)
270 if (s == NULL)
271 s = wp->screen;
272 screen_write_init(ctx, s);
273 ctx->wp = wp;
275 if (log_get_level() != 0) {
276 log_debug("%s: size %ux%u, pane %%%u (at %u,%u)",
277 __func__, screen_size_x(ctx->s), screen_size_y(ctx->s),
278 wp->id, wp->xoff, wp->yoff);
282 /* Initialize writing with a callback. */
283 void
284 screen_write_start_callback(struct screen_write_ctx *ctx, struct screen *s,
285 screen_write_init_ctx_cb cb, void *arg)
287 screen_write_init(ctx, s);
289 ctx->init_ctx_cb = cb;
290 ctx->arg = arg;
292 if (log_get_level() != 0) {
293 log_debug("%s: size %ux%u, with callback", __func__,
294 screen_size_x(ctx->s), screen_size_y(ctx->s));
298 /* Initialize writing. */
299 void
300 screen_write_start(struct screen_write_ctx *ctx, struct screen *s)
302 screen_write_init(ctx, s);
304 if (log_get_level() != 0) {
305 log_debug("%s: size %ux%u, no pane", __func__,
306 screen_size_x(ctx->s), screen_size_y(ctx->s));
310 /* Finish writing. */
311 void
312 screen_write_stop(struct screen_write_ctx *ctx)
314 screen_write_collect_end(ctx);
315 screen_write_collect_flush(ctx, 0, __func__);
317 screen_write_free_citem(ctx->item);
320 /* Reset screen state. */
321 void
322 screen_write_reset(struct screen_write_ctx *ctx)
324 struct screen *s = ctx->s;
326 screen_reset_tabs(s);
327 screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
329 s->mode = MODE_CURSOR|MODE_WRAP;
330 if (options_get_number(global_options, "extended-keys") == 2)
331 s->mode |= MODE_KEXTENDED;
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 grid *gd = src->grid;
571 struct grid_cell gc;
572 u_int xx, yy, cx, cy;
574 if (nx == 0 || ny == 0)
575 return;
577 cy = s->cy;
578 for (yy = py; yy < py + ny; yy++) {
579 if (yy >= gd->hsize + gd->sy)
580 break;
581 cx = s->cx;
582 for (xx = px; xx < px + nx; xx++) {
583 if (xx >= grid_get_line(gd, yy)->cellsize)
584 break;
585 grid_get_cell(gd, xx, yy, &gc);
586 if (xx + gc.data.width > px + nx)
587 break;
588 grid_view_set_cell(ctx->s->grid, cx, cy, &gc);
589 cx++;
591 cy++;
595 /* Draw a horizontal line on screen. */
596 void
597 screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right)
599 struct screen *s = ctx->s;
600 struct grid_cell gc;
601 u_int cx, cy, i;
603 cx = s->cx;
604 cy = s->cy;
606 memcpy(&gc, &grid_default_cell, sizeof gc);
607 gc.attr |= GRID_ATTR_CHARSET;
609 screen_write_putc(ctx, &gc, left ? 't' : 'q');
610 for (i = 1; i < nx - 1; i++)
611 screen_write_putc(ctx, &gc, 'q');
612 screen_write_putc(ctx, &gc, right ? 'u' : 'q');
614 screen_write_set_cursor(ctx, cx, cy);
617 /* Draw a vertical line on screen. */
618 void
619 screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom)
621 struct screen *s = ctx->s;
622 struct grid_cell gc;
623 u_int cx, cy, i;
625 cx = s->cx;
626 cy = s->cy;
628 memcpy(&gc, &grid_default_cell, sizeof gc);
629 gc.attr |= GRID_ATTR_CHARSET;
631 screen_write_putc(ctx, &gc, top ? 'w' : 'x');
632 for (i = 1; i < ny - 1; i++) {
633 screen_write_set_cursor(ctx, cx, cy + i);
634 screen_write_putc(ctx, &gc, 'x');
636 screen_write_set_cursor(ctx, cx, cy + ny - 1);
637 screen_write_putc(ctx, &gc, bottom ? 'v' : 'x');
639 screen_write_set_cursor(ctx, cx, cy);
642 /* Draw a menu on screen. */
643 void
644 screen_write_menu(struct screen_write_ctx *ctx, struct menu *menu,
645 int choice, const struct grid_cell *choice_gc)
647 struct screen *s = ctx->s;
648 struct grid_cell default_gc;
649 const struct grid_cell *gc = &default_gc;
650 u_int cx, cy, i, j;
651 const char *name;
653 cx = s->cx;
654 cy = s->cy;
656 memcpy(&default_gc, &grid_default_cell, sizeof default_gc);
658 screen_write_box(ctx, menu->width + 4, menu->count + 2,
659 BOX_LINES_DEFAULT, &default_gc, menu->title);
661 for (i = 0; i < menu->count; i++) {
662 name = menu->items[i].name;
663 if (name == NULL) {
664 screen_write_cursormove(ctx, cx, cy + 1 + i, 0);
665 screen_write_hline(ctx, menu->width + 4, 1, 1);
666 } else {
667 if (choice >= 0 && i == (u_int)choice && *name != '-')
668 gc = choice_gc;
669 screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
670 for (j = 0; j < menu->width; j++)
671 screen_write_putc(ctx, gc, ' ');
672 screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
673 if (*name == '-') {
674 name++;
675 default_gc.attr |= GRID_ATTR_DIM;
676 format_draw(ctx, gc, menu->width, name, NULL,
678 default_gc.attr &= ~GRID_ATTR_DIM;
679 } else
680 format_draw(ctx, gc, menu->width, name, NULL,
681 gc == choice_gc);
682 gc = &default_gc;
686 screen_write_set_cursor(ctx, cx, cy);
689 static void
690 screen_write_box_border_set(enum box_lines box_lines, int cell_type,
691 struct grid_cell *gc)
693 switch (box_lines) {
694 case BOX_LINES_NONE:
695 break;
696 case BOX_LINES_DOUBLE:
697 gc->attr &= ~GRID_ATTR_CHARSET;
698 utf8_copy(&gc->data, tty_acs_double_borders(cell_type));
699 break;
700 case BOX_LINES_HEAVY:
701 gc->attr &= ~GRID_ATTR_CHARSET;
702 utf8_copy(&gc->data, tty_acs_heavy_borders(cell_type));
703 break;
704 case BOX_LINES_ROUNDED:
705 gc->attr &= ~GRID_ATTR_CHARSET;
706 utf8_copy(&gc->data, tty_acs_rounded_borders(cell_type));
707 break;
708 case BOX_LINES_SIMPLE:
709 gc->attr &= ~GRID_ATTR_CHARSET;
710 utf8_set(&gc->data, SIMPLE_BORDERS[cell_type]);
711 break;
712 case BOX_LINES_PADDED:
713 gc->attr &= ~GRID_ATTR_CHARSET;
714 utf8_set(&gc->data, PADDED_BORDERS[cell_type]);
715 break;
716 case BOX_LINES_SINGLE:
717 case BOX_LINES_DEFAULT:
718 gc->attr |= GRID_ATTR_CHARSET;
719 utf8_set(&gc->data, CELL_BORDERS[cell_type]);
720 break;
724 /* Draw a box on screen. */
725 void
726 screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny,
727 enum box_lines lines, const struct grid_cell *gcp, const char *title)
729 struct screen *s = ctx->s;
730 struct grid_cell gc;
731 u_int cx, cy, i;
733 cx = s->cx;
734 cy = s->cy;
736 if (gcp != NULL)
737 memcpy(&gc, gcp, sizeof gc);
738 else
739 memcpy(&gc, &grid_default_cell, sizeof gc);
741 gc.attr |= GRID_ATTR_CHARSET;
742 gc.flags |= GRID_FLAG_NOPALETTE;
744 /* Draw top border */
745 screen_write_box_border_set(lines, CELL_TOPLEFT, &gc);
746 screen_write_cell(ctx, &gc);
747 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
748 for (i = 1; i < nx - 1; i++)
749 screen_write_cell(ctx, &gc);
750 screen_write_box_border_set(lines, CELL_TOPRIGHT, &gc);
751 screen_write_cell(ctx, &gc);
753 /* Draw bottom border */
754 screen_write_set_cursor(ctx, cx, cy + ny - 1);
755 screen_write_box_border_set(lines, CELL_BOTTOMLEFT, &gc);
756 screen_write_cell(ctx, &gc);
757 screen_write_box_border_set(lines, CELL_LEFTRIGHT, &gc);
758 for (i = 1; i < nx - 1; i++)
759 screen_write_cell(ctx, &gc);
760 screen_write_box_border_set(lines, CELL_BOTTOMRIGHT, &gc);
761 screen_write_cell(ctx, &gc);
763 /* Draw sides */
764 screen_write_box_border_set(lines, CELL_TOPBOTTOM, &gc);
765 for (i = 1; i < ny - 1; i++) {
766 /* left side */
767 screen_write_set_cursor(ctx, cx, cy + i);
768 screen_write_cell(ctx, &gc);
769 /* right side */
770 screen_write_set_cursor(ctx, cx + nx - 1, cy + i);
771 screen_write_cell(ctx, &gc);
774 if (title != NULL) {
775 gc.attr &= ~GRID_ATTR_CHARSET;
776 screen_write_cursormove(ctx, cx + 2, cy, 0);
777 format_draw(ctx, &gc, nx - 4, title, NULL, 0);
780 screen_write_set_cursor(ctx, cx, cy);
784 * Write a preview version of a window. Assumes target area is big enough and
785 * already cleared.
787 void
788 screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
789 u_int ny)
791 struct screen *s = ctx->s;
792 struct grid_cell gc;
793 u_int cx, cy, px, py;
795 cx = s->cx;
796 cy = s->cy;
799 * If the cursor is on, pick the area around the cursor, otherwise use
800 * the top left.
802 if (src->mode & MODE_CURSOR) {
803 px = src->cx;
804 if (px < nx / 3)
805 px = 0;
806 else
807 px = px - nx / 3;
808 if (px + nx > screen_size_x(src)) {
809 if (nx > screen_size_x(src))
810 px = 0;
811 else
812 px = screen_size_x(src) - nx;
814 py = src->cy;
815 if (py < ny / 3)
816 py = 0;
817 else
818 py = py - ny / 3;
819 if (py + ny > screen_size_y(src)) {
820 if (ny > screen_size_y(src))
821 py = 0;
822 else
823 py = screen_size_y(src) - ny;
825 } else {
826 px = 0;
827 py = 0;
830 screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
832 if (src->mode & MODE_CURSOR) {
833 grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
834 gc.attr |= GRID_ATTR_REVERSE;
835 screen_write_set_cursor(ctx, cx + (src->cx - px),
836 cy + (src->cy - py));
837 screen_write_cell(ctx, &gc);
841 /* Set a mode. */
842 void
843 screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
845 struct screen *s = ctx->s;
847 s->mode |= mode;
849 if (log_get_level() != 0)
850 log_debug("%s: %s", __func__, screen_mode_to_string(mode));
853 /* Clear a mode. */
854 void
855 screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
857 struct screen *s = ctx->s;
859 s->mode &= ~mode;
861 if (log_get_level() != 0)
862 log_debug("%s: %s", __func__, screen_mode_to_string(mode));
865 /* Cursor up by ny. */
866 void
867 screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
869 struct screen *s = ctx->s;
870 u_int cx = s->cx, cy = s->cy;
872 if (ny == 0)
873 ny = 1;
875 if (cy < s->rupper) {
876 /* Above region. */
877 if (ny > cy)
878 ny = cy;
879 } else {
880 /* Below region. */
881 if (ny > cy - s->rupper)
882 ny = cy - s->rupper;
884 if (cx == screen_size_x(s))
885 cx--;
887 cy -= ny;
889 screen_write_set_cursor(ctx, cx, cy);
892 /* Cursor down by ny. */
893 void
894 screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
896 struct screen *s = ctx->s;
897 u_int cx = s->cx, cy = s->cy;
899 if (ny == 0)
900 ny = 1;
902 if (cy > s->rlower) {
903 /* Below region. */
904 if (ny > screen_size_y(s) - 1 - cy)
905 ny = screen_size_y(s) - 1 - cy;
906 } else {
907 /* Above region. */
908 if (ny > s->rlower - cy)
909 ny = s->rlower - cy;
911 if (cx == screen_size_x(s))
912 cx--;
913 else if (ny == 0)
914 return;
916 cy += ny;
918 screen_write_set_cursor(ctx, cx, cy);
921 /* Cursor right by nx. */
922 void
923 screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
925 struct screen *s = ctx->s;
926 u_int cx = s->cx, cy = s->cy;
928 if (nx == 0)
929 nx = 1;
931 if (nx > screen_size_x(s) - 1 - cx)
932 nx = screen_size_x(s) - 1 - cx;
933 if (nx == 0)
934 return;
936 cx += nx;
938 screen_write_set_cursor(ctx, cx, cy);
941 /* Cursor left by nx. */
942 void
943 screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
945 struct screen *s = ctx->s;
946 u_int cx = s->cx, cy = s->cy;
948 if (nx == 0)
949 nx = 1;
951 if (nx > cx)
952 nx = cx;
953 if (nx == 0)
954 return;
956 cx -= nx;
958 screen_write_set_cursor(ctx, cx, cy);
961 /* Backspace; cursor left unless at start of wrapped line when can move up. */
962 void
963 screen_write_backspace(struct screen_write_ctx *ctx)
965 struct screen *s = ctx->s;
966 struct grid_line *gl;
967 u_int cx = s->cx, cy = s->cy;
969 if (cx == 0) {
970 if (cy == 0)
971 return;
972 gl = grid_get_line(s->grid, s->grid->hsize + cy - 1);
973 if (gl->flags & GRID_LINE_WRAPPED) {
974 cy--;
975 cx = screen_size_x(s) - 1;
977 } else
978 cx--;
980 screen_write_set_cursor(ctx, cx, cy);
983 /* VT100 alignment test. */
984 void
985 screen_write_alignmenttest(struct screen_write_ctx *ctx)
987 struct screen *s = ctx->s;
988 struct tty_ctx ttyctx;
989 struct grid_cell gc;
990 u_int xx, yy;
992 memcpy(&gc, &grid_default_cell, sizeof gc);
993 utf8_set(&gc.data, 'E');
995 for (yy = 0; yy < screen_size_y(s); yy++) {
996 for (xx = 0; xx < screen_size_x(s); xx++)
997 grid_view_set_cell(s->grid, xx, yy, &gc);
1000 screen_write_set_cursor(ctx, 0, 0);
1002 s->rupper = 0;
1003 s->rlower = screen_size_y(s) - 1;
1005 screen_write_initctx(ctx, &ttyctx, 1);
1007 screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1008 tty_write(tty_cmd_alignmenttest, &ttyctx);
1011 /* Insert nx characters. */
1012 void
1013 screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1015 struct screen *s = ctx->s;
1016 struct tty_ctx ttyctx;
1018 if (nx == 0)
1019 nx = 1;
1021 if (nx > screen_size_x(s) - s->cx)
1022 nx = screen_size_x(s) - s->cx;
1023 if (nx == 0)
1024 return;
1026 if (s->cx > screen_size_x(s) - 1)
1027 return;
1029 screen_write_initctx(ctx, &ttyctx, 0);
1030 ttyctx.bg = bg;
1032 grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1034 screen_write_collect_flush(ctx, 0, __func__);
1035 ttyctx.num = nx;
1036 tty_write(tty_cmd_insertcharacter, &ttyctx);
1039 /* Delete nx characters. */
1040 void
1041 screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1043 struct screen *s = ctx->s;
1044 struct tty_ctx ttyctx;
1046 if (nx == 0)
1047 nx = 1;
1049 if (nx > screen_size_x(s) - s->cx)
1050 nx = screen_size_x(s) - s->cx;
1051 if (nx == 0)
1052 return;
1054 if (s->cx > screen_size_x(s) - 1)
1055 return;
1057 screen_write_initctx(ctx, &ttyctx, 0);
1058 ttyctx.bg = bg;
1060 grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1062 screen_write_collect_flush(ctx, 0, __func__);
1063 ttyctx.num = nx;
1064 tty_write(tty_cmd_deletecharacter, &ttyctx);
1067 /* Clear nx characters. */
1068 void
1069 screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1071 struct screen *s = ctx->s;
1072 struct tty_ctx ttyctx;
1074 if (nx == 0)
1075 nx = 1;
1077 if (nx > screen_size_x(s) - s->cx)
1078 nx = screen_size_x(s) - s->cx;
1079 if (nx == 0)
1080 return;
1082 if (s->cx > screen_size_x(s) - 1)
1083 return;
1085 screen_write_initctx(ctx, &ttyctx, 0);
1086 ttyctx.bg = bg;
1088 grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
1090 screen_write_collect_flush(ctx, 0, __func__);
1091 ttyctx.num = nx;
1092 tty_write(tty_cmd_clearcharacter, &ttyctx);
1095 /* Insert ny lines. */
1096 void
1097 screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1099 struct screen *s = ctx->s;
1100 struct grid *gd = s->grid;
1101 struct tty_ctx ttyctx;
1103 if (ny == 0)
1104 ny = 1;
1106 if (s->cy < s->rupper || s->cy > s->rlower) {
1107 if (ny > screen_size_y(s) - s->cy)
1108 ny = screen_size_y(s) - s->cy;
1109 if (ny == 0)
1110 return;
1112 screen_write_initctx(ctx, &ttyctx, 1);
1113 ttyctx.bg = bg;
1115 grid_view_insert_lines(gd, s->cy, ny, bg);
1117 screen_write_collect_flush(ctx, 0, __func__);
1118 ttyctx.num = ny;
1119 tty_write(tty_cmd_insertline, &ttyctx);
1120 return;
1123 if (ny > s->rlower + 1 - s->cy)
1124 ny = s->rlower + 1 - s->cy;
1125 if (ny == 0)
1126 return;
1128 screen_write_initctx(ctx, &ttyctx, 1);
1129 ttyctx.bg = bg;
1131 if (s->cy < s->rupper || s->cy > s->rlower)
1132 grid_view_insert_lines(gd, s->cy, ny, bg);
1133 else
1134 grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1136 screen_write_collect_flush(ctx, 0, __func__);
1138 ttyctx.num = ny;
1139 tty_write(tty_cmd_insertline, &ttyctx);
1142 /* Delete ny lines. */
1143 void
1144 screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1146 struct screen *s = ctx->s;
1147 struct grid *gd = s->grid;
1148 struct tty_ctx ttyctx;
1150 if (ny == 0)
1151 ny = 1;
1153 if (s->cy < s->rupper || s->cy > s->rlower) {
1154 if (ny > screen_size_y(s) - s->cy)
1155 ny = screen_size_y(s) - s->cy;
1156 if (ny == 0)
1157 return;
1159 screen_write_initctx(ctx, &ttyctx, 1);
1160 ttyctx.bg = bg;
1162 grid_view_delete_lines(gd, s->cy, ny, bg);
1164 screen_write_collect_flush(ctx, 0, __func__);
1165 ttyctx.num = ny;
1166 tty_write(tty_cmd_deleteline, &ttyctx);
1167 return;
1170 if (ny > s->rlower + 1 - s->cy)
1171 ny = s->rlower + 1 - s->cy;
1172 if (ny == 0)
1173 return;
1175 screen_write_initctx(ctx, &ttyctx, 1);
1176 ttyctx.bg = bg;
1178 if (s->cy < s->rupper || s->cy > s->rlower)
1179 grid_view_delete_lines(gd, s->cy, ny, bg);
1180 else
1181 grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1183 screen_write_collect_flush(ctx, 0, __func__);
1184 ttyctx.num = ny;
1185 tty_write(tty_cmd_deleteline, &ttyctx);
1188 /* Clear line at cursor. */
1189 void
1190 screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1192 struct screen *s = ctx->s;
1193 struct grid_line *gl;
1194 u_int sx = screen_size_x(s);
1195 struct screen_write_citem *ci = ctx->item;
1197 gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1198 if (gl->cellsize == 0 && COLOUR_DEFAULT(bg))
1199 return;
1201 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1203 screen_write_collect_clear(ctx, s->cy, 1);
1204 ci->x = 0;
1205 ci->used = sx;
1206 ci->type = CLEAR;
1207 ci->bg = bg;
1208 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1209 ctx->item = screen_write_get_citem();
1212 /* Clear to end of line from cursor. */
1213 void
1214 screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1216 struct screen *s = ctx->s;
1217 struct grid_line *gl;
1218 u_int sx = screen_size_x(s);
1219 struct screen_write_citem *ci = ctx->item, *before;
1221 if (s->cx == 0) {
1222 screen_write_clearline(ctx, bg);
1223 return;
1226 gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1227 if (s->cx > sx - 1 || (s->cx >= gl->cellsize && COLOUR_DEFAULT(bg)))
1228 return;
1230 grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
1232 before = screen_write_collect_trim(ctx, s->cy, s->cx, sx - s->cx, NULL);
1233 ci->x = s->cx;
1234 ci->used = sx - s->cx;
1235 ci->type = CLEAR;
1236 ci->bg = bg;
1237 if (before == NULL)
1238 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1239 else
1240 TAILQ_INSERT_BEFORE(before, ci, entry);
1241 ctx->item = screen_write_get_citem();
1244 /* Clear to start of line from cursor. */
1245 void
1246 screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1248 struct screen *s = ctx->s;
1249 u_int sx = screen_size_x(s);
1250 struct screen_write_citem *ci = ctx->item, *before;
1252 if (s->cx >= sx - 1) {
1253 screen_write_clearline(ctx, bg);
1254 return;
1257 if (s->cx > sx - 1)
1258 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1259 else
1260 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1262 before = screen_write_collect_trim(ctx, s->cy, 0, s->cx + 1, NULL);
1263 ci->x = 0;
1264 ci->used = s->cx + 1;
1265 ci->type = CLEAR;
1266 ci->bg = bg;
1267 if (before == NULL)
1268 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1269 else
1270 TAILQ_INSERT_BEFORE(before, ci, entry);
1271 ctx->item = screen_write_get_citem();
1274 /* Move cursor to px,py. */
1275 void
1276 screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py,
1277 int origin)
1279 struct screen *s = ctx->s;
1281 if (origin && py != -1 && (s->mode & MODE_ORIGIN)) {
1282 if ((u_int)py > s->rlower - s->rupper)
1283 py = s->rlower;
1284 else
1285 py += s->rupper;
1288 if (px != -1 && (u_int)px > screen_size_x(s) - 1)
1289 px = screen_size_x(s) - 1;
1290 if (py != -1 && (u_int)py > screen_size_y(s) - 1)
1291 py = screen_size_y(s) - 1;
1293 log_debug("%s: from %u,%u to %u,%u", __func__, s->cx, s->cy, px, py);
1294 screen_write_set_cursor(ctx, px, py);
1297 /* Reverse index (up with scroll). */
1298 void
1299 screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
1301 struct screen *s = ctx->s;
1302 struct tty_ctx ttyctx;
1304 if (s->cy == s->rupper) {
1305 grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1306 screen_write_collect_flush(ctx, 0, __func__);
1308 screen_write_initctx(ctx, &ttyctx, 1);
1309 ttyctx.bg = bg;
1311 tty_write(tty_cmd_reverseindex, &ttyctx);
1312 } else if (s->cy > 0)
1313 screen_write_set_cursor(ctx, -1, s->cy - 1);
1317 /* Set scroll region. */
1318 void
1319 screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
1320 u_int rlower)
1322 struct screen *s = ctx->s;
1324 if (rupper > screen_size_y(s) - 1)
1325 rupper = screen_size_y(s) - 1;
1326 if (rlower > screen_size_y(s) - 1)
1327 rlower = screen_size_y(s) - 1;
1328 if (rupper >= rlower) /* cannot be one line */
1329 return;
1331 screen_write_collect_flush(ctx, 0, __func__);
1333 /* Cursor moves to top-left. */
1334 screen_write_set_cursor(ctx, 0, 0);
1336 s->rupper = rupper;
1337 s->rlower = rlower;
1340 /* Line feed. */
1341 void
1342 screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1344 struct screen *s = ctx->s;
1345 struct grid *gd = s->grid;
1346 struct grid_line *gl;
1348 gl = grid_get_line(gd, gd->hsize + s->cy);
1349 if (wrapped)
1350 gl->flags |= GRID_LINE_WRAPPED;
1352 log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1353 s->rupper, s->rlower);
1355 if (bg != ctx->bg) {
1356 screen_write_collect_flush(ctx, 1, __func__);
1357 ctx->bg = bg;
1360 if (s->cy == s->rlower) {
1361 grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1362 screen_write_collect_scroll(ctx, bg);
1363 ctx->scrolled++;
1364 } else if (s->cy < screen_size_y(s) - 1)
1365 screen_write_set_cursor(ctx, -1, s->cy + 1);
1368 /* Scroll up. */
1369 void
1370 screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1372 struct screen *s = ctx->s;
1373 struct grid *gd = s->grid;
1374 u_int i;
1376 if (lines == 0)
1377 lines = 1;
1378 else if (lines > s->rlower - s->rupper + 1)
1379 lines = s->rlower - s->rupper + 1;
1381 if (bg != ctx->bg) {
1382 screen_write_collect_flush(ctx, 1, __func__);
1383 ctx->bg = bg;
1386 for (i = 0; i < lines; i++) {
1387 grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1388 screen_write_collect_scroll(ctx, bg);
1390 ctx->scrolled += lines;
1393 /* Scroll down. */
1394 void
1395 screen_write_scrolldown(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1397 struct screen *s = ctx->s;
1398 struct grid *gd = s->grid;
1399 struct tty_ctx ttyctx;
1400 u_int i;
1402 screen_write_initctx(ctx, &ttyctx, 1);
1403 ttyctx.bg = bg;
1405 if (lines == 0)
1406 lines = 1;
1407 else if (lines > s->rlower - s->rupper + 1)
1408 lines = s->rlower - s->rupper + 1;
1410 for (i = 0; i < lines; i++)
1411 grid_view_scroll_region_down(gd, s->rupper, s->rlower, bg);
1413 screen_write_collect_flush(ctx, 0, __func__);
1414 ttyctx.num = lines;
1415 tty_write(tty_cmd_scrolldown, &ttyctx);
1418 /* Carriage return (cursor to start of line). */
1419 void
1420 screen_write_carriagereturn(struct screen_write_ctx *ctx)
1422 screen_write_set_cursor(ctx, 0, -1);
1425 /* Clear to end of screen from cursor. */
1426 void
1427 screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1429 struct screen *s = ctx->s;
1430 struct grid *gd = s->grid;
1431 struct tty_ctx ttyctx;
1432 u_int sx = screen_size_x(s), sy = screen_size_y(s);
1434 screen_write_initctx(ctx, &ttyctx, 1);
1435 ttyctx.bg = bg;
1437 /* Scroll into history if it is enabled and clearing entire screen. */
1438 if (s->cx == 0 &&
1439 s->cy == 0 &&
1440 (gd->flags & GRID_HISTORY) &&
1441 ctx->wp != NULL &&
1442 options_get_number(ctx->wp->options, "scroll-on-clear"))
1443 grid_view_clear_history(gd, bg);
1444 else {
1445 if (s->cx <= sx - 1)
1446 grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
1447 grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1450 screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1451 screen_write_collect_flush(ctx, 0, __func__);
1452 tty_write(tty_cmd_clearendofscreen, &ttyctx);
1455 /* Clear to start of screen. */
1456 void
1457 screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1459 struct screen *s = ctx->s;
1460 struct tty_ctx ttyctx;
1461 u_int sx = screen_size_x(s);
1463 screen_write_initctx(ctx, &ttyctx, 1);
1464 ttyctx.bg = bg;
1466 if (s->cy > 0)
1467 grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1468 if (s->cx > sx - 1)
1469 grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1470 else
1471 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1473 screen_write_collect_clear(ctx, 0, s->cy);
1474 screen_write_collect_flush(ctx, 0, __func__);
1475 tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1478 /* Clear entire screen. */
1479 void
1480 screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1482 struct screen *s = ctx->s;
1483 struct tty_ctx ttyctx;
1484 u_int sx = screen_size_x(s), sy = screen_size_y(s);
1486 screen_write_initctx(ctx, &ttyctx, 1);
1487 ttyctx.bg = bg;
1489 /* Scroll into history if it is enabled. */
1490 if ((s->grid->flags & GRID_HISTORY) &&
1491 ctx->wp != NULL &&
1492 options_get_number(ctx->wp->options, "scroll-on-clear"))
1493 grid_view_clear_history(s->grid, bg);
1494 else
1495 grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1497 screen_write_collect_clear(ctx, 0, sy);
1498 tty_write(tty_cmd_clearscreen, &ttyctx);
1501 /* Clear entire history. */
1502 void
1503 screen_write_clearhistory(struct screen_write_ctx *ctx)
1505 grid_clear_history(ctx->s->grid);
1508 /* Force a full redraw. */
1509 void
1510 screen_write_fullredraw(struct screen_write_ctx *ctx)
1512 struct tty_ctx ttyctx;
1514 screen_write_collect_flush(ctx, 0, __func__);
1516 screen_write_initctx(ctx, &ttyctx, 1);
1517 if (ttyctx.redraw_cb != NULL)
1518 ttyctx.redraw_cb(&ttyctx);
1521 /* Trim collected items. */
1522 static struct screen_write_citem *
1523 screen_write_collect_trim(struct screen_write_ctx *ctx, u_int y, u_int x,
1524 u_int used, int *wrapped)
1526 struct screen_write_cline *cl = &ctx->s->write_list[y];
1527 struct screen_write_citem *ci, *ci2, *tmp, *before = NULL;
1528 u_int sx = x, ex = x + used - 1;
1529 u_int csx, cex;
1531 if (TAILQ_EMPTY(&cl->items))
1532 return (NULL);
1533 TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) {
1534 csx = ci->x;
1535 cex = ci->x + ci->used - 1;
1537 /* Item is entirely before. */
1538 if (cex < sx) {
1539 log_debug("%s: %p %u-%u before %u-%u", __func__, ci,
1540 csx, cex, sx, ex);
1541 continue;
1544 /* Item is entirely after. */
1545 if (csx > ex) {
1546 log_debug("%s: %p %u-%u after %u-%u", __func__, ci,
1547 csx, cex, sx, ex);
1548 before = ci;
1549 break;
1552 /* Item is entirely inside. */
1553 if (csx >= sx && cex <= ex) {
1554 log_debug("%s: %p %u-%u inside %u-%u", __func__, ci,
1555 csx, cex, sx, ex);
1556 TAILQ_REMOVE(&cl->items, ci, entry);
1557 screen_write_free_citem(ci);
1558 if (csx == 0 && ci->wrapped && wrapped != NULL)
1559 *wrapped = 1;
1560 continue;
1563 /* Item under the start. */
1564 if (csx < sx && cex >= sx && cex <= ex) {
1565 log_debug("%s: %p %u-%u start %u-%u", __func__, ci,
1566 csx, cex, sx, ex);
1567 ci->used = sx - csx;
1568 log_debug("%s: %p now %u-%u", __func__, ci, ci->x,
1569 ci->x + ci->used + 1);
1570 continue;
1573 /* Item covers the end. */
1574 if (cex > ex && csx >= sx && csx <= ex) {
1575 log_debug("%s: %p %u-%u end %u-%u", __func__, ci,
1576 csx, cex, sx, ex);
1577 ci->x = ex + 1;
1578 ci->used = cex - ex;
1579 log_debug("%s: %p now %u-%u", __func__, ci, ci->x,
1580 ci->x + ci->used + 1);
1581 before = ci;
1582 break;
1585 /* Item must cover both sides. */
1586 log_debug("%s: %p %u-%u under %u-%u", __func__, ci,
1587 csx, cex, sx, ex);
1588 ci2 = screen_write_get_citem();
1589 ci2->type = ci->type;
1590 ci2->bg = ci->bg;
1591 memcpy(&ci2->gc, &ci->gc, sizeof ci2->gc);
1592 TAILQ_INSERT_AFTER(&cl->items, ci, ci2, entry);
1594 ci->used = sx - csx;
1595 ci2->x = ex + 1;
1596 ci2->used = cex - ex;
1598 log_debug("%s: %p now %u-%u (%p) and %u-%u (%p)", __func__, ci,
1599 ci->x, ci->x + ci->used - 1, ci, ci2->x,
1600 ci2->x + ci2->used - 1, ci2);
1601 before = ci2;
1602 break;
1604 return (before);
1607 /* Clear collected lines. */
1608 static void
1609 screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
1611 struct screen_write_cline *cl;
1612 u_int i;
1614 for (i = y; i < y + n; i++) {
1615 cl = &ctx->s->write_list[i];
1616 TAILQ_CONCAT(&screen_write_citem_freelist, &cl->items, entry);
1620 /* Scroll collected lines up. */
1621 static void
1622 screen_write_collect_scroll(struct screen_write_ctx *ctx, u_int bg)
1624 struct screen *s = ctx->s;
1625 struct screen_write_cline *cl;
1626 u_int y;
1627 char *saved;
1628 struct screen_write_citem *ci;
1630 log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1631 s->rupper, s->rlower);
1633 screen_write_collect_clear(ctx, s->rupper, 1);
1634 saved = ctx->s->write_list[s->rupper].data;
1635 for (y = s->rupper; y < s->rlower; y++) {
1636 cl = &ctx->s->write_list[y + 1];
1637 TAILQ_CONCAT(&ctx->s->write_list[y].items, &cl->items, entry);
1638 ctx->s->write_list[y].data = cl->data;
1640 ctx->s->write_list[s->rlower].data = saved;
1642 ci = screen_write_get_citem();
1643 ci->x = 0;
1644 ci->used = screen_size_x(s);
1645 ci->type = CLEAR;
1646 ci->bg = bg;
1647 TAILQ_INSERT_TAIL(&ctx->s->write_list[s->rlower].items, ci, entry);
1650 /* Flush collected lines. */
1651 static void
1652 screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only,
1653 const char *from)
1655 struct screen *s = ctx->s;
1656 struct screen_write_citem *ci, *tmp;
1657 struct screen_write_cline *cl;
1658 u_int y, cx, cy, last, items = 0;
1659 struct tty_ctx ttyctx;
1661 if (ctx->scrolled != 0) {
1662 log_debug("%s: scrolled %u (region %u-%u)", __func__,
1663 ctx->scrolled, s->rupper, s->rlower);
1664 if (ctx->scrolled > s->rlower - s->rupper + 1)
1665 ctx->scrolled = s->rlower - s->rupper + 1;
1667 screen_write_initctx(ctx, &ttyctx, 1);
1668 ttyctx.num = ctx->scrolled;
1669 ttyctx.bg = ctx->bg;
1670 tty_write(tty_cmd_scrollup, &ttyctx);
1672 ctx->scrolled = 0;
1673 ctx->bg = 8;
1675 if (scroll_only)
1676 return;
1678 cx = s->cx; cy = s->cy;
1679 for (y = 0; y < screen_size_y(s); y++) {
1680 cl = &ctx->s->write_list[y];
1681 last = UINT_MAX;
1682 TAILQ_FOREACH_SAFE(ci, &cl->items, entry, tmp) {
1683 if (last != UINT_MAX && ci->x <= last) {
1684 fatalx("collect list not in order: %u <= %u",
1685 ci->x, last);
1687 screen_write_set_cursor(ctx, ci->x, y);
1688 if (ci->type == CLEAR) {
1689 screen_write_initctx(ctx, &ttyctx, 1);
1690 ttyctx.bg = ci->bg;
1691 ttyctx.num = ci->used;
1692 tty_write(tty_cmd_clearcharacter, &ttyctx);
1693 } else {
1694 screen_write_initctx(ctx, &ttyctx, 0);
1695 ttyctx.cell = &ci->gc;
1696 ttyctx.wrapped = ci->wrapped;
1697 ttyctx.ptr = cl->data + ci->x;
1698 ttyctx.num = ci->used;
1699 tty_write(tty_cmd_cells, &ttyctx);
1701 items++;
1703 TAILQ_REMOVE(&cl->items, ci, entry);
1704 screen_write_free_citem(ci);
1705 last = ci->x;
1708 s->cx = cx; s->cy = cy;
1710 log_debug("%s: flushed %u items (%s)", __func__, items, from);
1713 /* Finish and store collected cells. */
1714 void
1715 screen_write_collect_end(struct screen_write_ctx *ctx)
1717 struct screen *s = ctx->s;
1718 struct screen_write_citem *ci = ctx->item, *before;
1719 struct screen_write_cline *cl = &s->write_list[s->cy];
1720 struct grid_cell gc;
1721 u_int xx;
1722 int wrapped = ci->wrapped;
1724 if (ci->used == 0)
1725 return;
1727 before = screen_write_collect_trim(ctx, s->cy, s->cx, ci->used,
1728 &wrapped);
1729 ci->x = s->cx;
1730 ci->wrapped = wrapped;
1731 if (before == NULL)
1732 TAILQ_INSERT_TAIL(&cl->items, ci, entry);
1733 else
1734 TAILQ_INSERT_BEFORE(before, ci, entry);
1735 ctx->item = screen_write_get_citem();
1737 log_debug("%s: %u %.*s (at %u,%u)", __func__, ci->used,
1738 (int)ci->used, cl->data + ci->x, s->cx, s->cy);
1740 if (s->cx != 0) {
1741 for (xx = s->cx; xx > 0; xx--) {
1742 grid_view_get_cell(s->grid, xx, s->cy, &gc);
1743 if (~gc.flags & GRID_FLAG_PADDING)
1744 break;
1745 grid_view_set_cell(s->grid, xx, s->cy,
1746 &grid_default_cell);
1748 if (gc.data.width > 1) {
1749 grid_view_set_cell(s->grid, xx, s->cy,
1750 &grid_default_cell);
1754 grid_view_set_cells(s->grid, s->cx, s->cy, &ci->gc, cl->data + ci->x,
1755 ci->used);
1756 screen_write_set_cursor(ctx, s->cx + ci->used, -1);
1758 for (xx = s->cx; xx < screen_size_x(s); xx++) {
1759 grid_view_get_cell(s->grid, xx, s->cy, &gc);
1760 if (~gc.flags & GRID_FLAG_PADDING)
1761 break;
1762 grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
1766 /* Write cell data, collecting if necessary. */
1767 void
1768 screen_write_collect_add(struct screen_write_ctx *ctx,
1769 const struct grid_cell *gc)
1771 struct screen *s = ctx->s;
1772 struct screen_write_citem *ci;
1773 u_int sx = screen_size_x(s);
1774 int collect;
1777 * Don't need to check that the attributes and whatnot are still the
1778 * same - input_parse will end the collection when anything that isn't
1779 * a plain character is encountered.
1782 collect = 1;
1783 if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f)
1784 collect = 0;
1785 else if (gc->attr & GRID_ATTR_CHARSET)
1786 collect = 0;
1787 else if (~s->mode & MODE_WRAP)
1788 collect = 0;
1789 else if (s->mode & MODE_INSERT)
1790 collect = 0;
1791 else if (s->sel != NULL)
1792 collect = 0;
1793 if (!collect) {
1794 screen_write_collect_end(ctx);
1795 screen_write_collect_flush(ctx, 0, __func__);
1796 screen_write_cell(ctx, gc);
1797 return;
1800 if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
1801 screen_write_collect_end(ctx);
1802 ci = ctx->item; /* may have changed */
1804 if (s->cx > sx - 1) {
1805 log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1806 ci->wrapped = 1;
1807 screen_write_linefeed(ctx, 1, 8);
1808 screen_write_set_cursor(ctx, 0, -1);
1811 if (ci->used == 0)
1812 memcpy(&ci->gc, gc, sizeof ci->gc);
1813 if (ctx->s->write_list[s->cy].data == NULL)
1814 ctx->s->write_list[s->cy].data = xmalloc(screen_size_x(ctx->s));
1815 ctx->s->write_list[s->cy].data[s->cx + ci->used++] = gc->data.data[0];
1818 /* Write cell data. */
1819 void
1820 screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1822 struct screen *s = ctx->s;
1823 struct grid *gd = s->grid;
1824 const struct utf8_data *ud = &gc->data;
1825 const struct utf8_data zwj = { "\342\200\215", 0, 3, 0 };
1826 struct grid_line *gl;
1827 struct grid_cell_entry *gce;
1828 struct grid_cell tmp_gc, now_gc;
1829 struct tty_ctx ttyctx;
1830 u_int sx = screen_size_x(s), sy = screen_size_y(s);
1831 u_int width = gc->data.width, xx, last, cx, cy;
1832 int selected, skip = 1;
1834 /* Ignore padding cells. */
1835 if (gc->flags & GRID_FLAG_PADDING)
1836 return;
1839 * If this is a zero width joiner, set the flag so the next character
1840 * will be treated as zero width and appended. Note that we assume a
1841 * ZWJ will not change the width - the width of the first character is
1842 * used.
1844 if (ud->size == 3 && memcmp(ud->data, "\342\200\215", 3) == 0) {
1845 log_debug("zero width joiner at %u,%u", s->cx, s->cy);
1846 ctx->flags |= SCREEN_WRITE_ZWJ;
1847 return;
1851 * If the width is zero, combine onto the previous character. We always
1852 * combine with the cell to the left of the cursor position. In theory,
1853 * the application could have moved the cursor somewhere else, but if
1854 * they are silly enough to do that, who cares?
1856 if (ctx->flags & SCREEN_WRITE_ZWJ) {
1857 screen_write_collect_flush(ctx, 0, __func__);
1858 screen_write_combine(ctx, &zwj, &xx, &cx);
1860 if (width == 0 || (ctx->flags & SCREEN_WRITE_ZWJ)) {
1861 ctx->flags &= ~SCREEN_WRITE_ZWJ;
1862 screen_write_collect_flush(ctx, 0, __func__);
1863 if ((gc = screen_write_combine(ctx, ud, &xx, &cx)) != NULL) {
1864 cy = s->cy;
1865 screen_write_set_cursor(ctx, xx, s->cy);
1866 screen_write_initctx(ctx, &ttyctx, 0);
1867 ttyctx.cell = gc;
1868 tty_write(tty_cmd_cell, &ttyctx);
1869 s->cx = cx; s->cy = cy;
1871 return;
1874 /* Flush any existing scrolling. */
1875 screen_write_collect_flush(ctx, 1, __func__);
1877 /* If this character doesn't fit, ignore it. */
1878 if ((~s->mode & MODE_WRAP) &&
1879 width > 1 &&
1880 (width > sx || (s->cx != sx && s->cx > sx - width)))
1881 return;
1883 /* If in insert mode, make space for the cells. */
1884 if (s->mode & MODE_INSERT) {
1885 grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
1886 skip = 0;
1889 /* Check this will fit on the current line and wrap if not. */
1890 if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1891 log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1892 screen_write_linefeed(ctx, 1, 8);
1893 screen_write_set_cursor(ctx, 0, -1);
1894 screen_write_collect_flush(ctx, 1, __func__);
1897 /* Sanity check cursor position. */
1898 if (s->cx > sx - width || s->cy > sy - 1)
1899 return;
1900 screen_write_initctx(ctx, &ttyctx, 0);
1902 /* Handle overwriting of UTF-8 characters. */
1903 gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1904 if (gl->flags & GRID_LINE_EXTENDED) {
1905 grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
1906 if (screen_write_overwrite(ctx, &now_gc, width))
1907 skip = 0;
1911 * If the new character is UTF-8 wide, fill in padding cells. Have
1912 * already ensured there is enough room.
1914 for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1915 log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
1916 grid_view_set_padding(gd, xx, s->cy);
1917 skip = 0;
1920 /* If no change, do not draw. */
1921 if (skip) {
1922 if (s->cx >= gl->cellsize)
1923 skip = grid_cells_equal(gc, &grid_default_cell);
1924 else {
1925 gce = &gl->celldata[s->cx];
1926 if (gce->flags & GRID_FLAG_EXTENDED)
1927 skip = 0;
1928 else if (gc->flags != gce->flags)
1929 skip = 0;
1930 else if (gc->attr != gce->data.attr)
1931 skip = 0;
1932 else if (gc->fg != gce->data.fg)
1933 skip = 0;
1934 else if (gc->bg != gce->data.bg)
1935 skip = 0;
1936 else if (gc->data.width != 1)
1937 skip = 0;
1938 else if (gc->data.size != 1)
1939 skip = 0;
1940 else if (gce->data.data != gc->data.data[0])
1941 skip = 0;
1945 /* Update the selected flag and set the cell. */
1946 selected = screen_check_selection(s, s->cx, s->cy);
1947 if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1948 memcpy(&tmp_gc, gc, sizeof tmp_gc);
1949 tmp_gc.flags |= GRID_FLAG_SELECTED;
1950 grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1951 } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1952 memcpy(&tmp_gc, gc, sizeof tmp_gc);
1953 tmp_gc.flags &= ~GRID_FLAG_SELECTED;
1954 grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1955 } else if (!skip)
1956 grid_view_set_cell(gd, s->cx, s->cy, gc);
1957 if (selected)
1958 skip = 0;
1961 * Move the cursor. If not wrapping, stick at the last character and
1962 * replace it.
1964 last = !(s->mode & MODE_WRAP);
1965 if (s->cx <= sx - last - width)
1966 screen_write_set_cursor(ctx, s->cx + width, -1);
1967 else
1968 screen_write_set_cursor(ctx, sx - last, -1);
1970 /* Create space for character in insert mode. */
1971 if (s->mode & MODE_INSERT) {
1972 screen_write_collect_flush(ctx, 0, __func__);
1973 ttyctx.num = width;
1974 tty_write(tty_cmd_insertcharacter, &ttyctx);
1977 /* Write to the screen. */
1978 if (!skip) {
1979 if (selected) {
1980 screen_select_cell(s, &tmp_gc, gc);
1981 ttyctx.cell = &tmp_gc;
1982 } else
1983 ttyctx.cell = gc;
1984 tty_write(tty_cmd_cell, &ttyctx);
1988 /* Combine a UTF-8 zero-width character onto the previous. */
1989 static const struct grid_cell *
1990 screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
1991 u_int *xx, u_int *cx)
1993 struct screen *s = ctx->s;
1994 struct grid *gd = s->grid;
1995 static struct grid_cell gc;
1996 u_int n, width;
1998 /* Can't combine if at 0. */
1999 if (s->cx == 0) {
2000 *xx = 0;
2001 return (NULL);
2003 *xx = s->cx;
2005 /* Empty data is out. */
2006 if (ud->size == 0)
2007 fatalx("UTF-8 data empty");
2009 /* Retrieve the previous cell. */
2010 for (n = 1; n <= s->cx; n++) {
2011 grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
2012 if (~gc.flags & GRID_FLAG_PADDING)
2013 break;
2015 if (n > s->cx)
2016 return (NULL);
2018 /* Check there is enough space. */
2019 if (gc.data.size + ud->size > sizeof gc.data.data)
2020 return (NULL);
2021 (*xx) -= n;
2023 log_debug("%s: %.*s onto %.*s at %u,%u (width %u)", __func__,
2024 (int)ud->size, ud->data, (int)gc.data.size, gc.data.data, *xx,
2025 s->cy, gc.data.width);
2027 /* Append the data. */
2028 memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
2029 gc.data.size += ud->size;
2030 width = gc.data.width;
2032 /* If this is U+FE0F VARIATION SELECTOR-16, force the width to 2. */
2033 if (gc.data.width == 1 &&
2034 ud->size == 3 &&
2035 memcmp(ud->data, "\357\270\217", 3) == 0) {
2036 grid_view_set_padding(gd, (*xx) + 1, s->cy);
2037 gc.data.width = 2;
2038 width += 2;
2041 /* Set the new cell. */
2042 grid_view_set_cell(gd, *xx, s->cy, &gc);
2044 *cx = (*xx) + width;
2045 log_debug("%s: character at %u; cursor at %u", __func__, *xx, *cx);
2046 return (&gc);
2050 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
2051 * cell on the screen, so following cells must not be drawn by marking them as
2052 * padding.
2054 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
2055 * character, it is necessary to also overwrite any other cells which covered
2056 * by the same character.
2058 static int
2059 screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
2060 u_int width)
2062 struct screen *s = ctx->s;
2063 struct grid *gd = s->grid;
2064 struct grid_cell tmp_gc;
2065 u_int xx;
2066 int done = 0;
2068 if (gc->flags & GRID_FLAG_PADDING) {
2070 * A padding cell, so clear any following and leading padding
2071 * cells back to the character. Don't overwrite the current
2072 * cell as that happens later anyway.
2074 xx = s->cx + 1;
2075 while (--xx > 0) {
2076 grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
2077 if (~tmp_gc.flags & GRID_FLAG_PADDING)
2078 break;
2079 log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
2080 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2083 /* Overwrite the character at the start of this padding. */
2084 log_debug("%s: character at %u,%u", __func__, xx, s->cy);
2085 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2086 done = 1;
2090 * Overwrite any padding cells that belong to any UTF-8 characters
2091 * we'll be overwriting with the current character.
2093 if (width != 1 ||
2094 gc->data.width != 1 ||
2095 gc->flags & GRID_FLAG_PADDING) {
2096 xx = s->cx + width - 1;
2097 while (++xx < screen_size_x(s)) {
2098 grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
2099 if (~tmp_gc.flags & GRID_FLAG_PADDING)
2100 break;
2101 log_debug("%s: overwrite at %u,%u", __func__, xx,
2102 s->cy);
2103 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
2104 done = 1;
2108 return (done);
2111 /* Set external clipboard. */
2112 void
2113 screen_write_setselection(struct screen_write_ctx *ctx, const char *flags,
2114 u_char *str, u_int len)
2116 struct tty_ctx ttyctx;
2118 screen_write_initctx(ctx, &ttyctx, 0);
2119 ttyctx.ptr = str;
2120 ttyctx.ptr2 = (void *)flags;
2121 ttyctx.num = len;
2123 tty_write(tty_cmd_setselection, &ttyctx);
2126 /* Write unmodified string. */
2127 void
2128 screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len,
2129 int allow_invisible_panes)
2131 struct tty_ctx ttyctx;
2133 screen_write_initctx(ctx, &ttyctx, 0);
2134 ttyctx.ptr = str;
2135 ttyctx.num = len;
2136 ttyctx.allow_invisible_panes = allow_invisible_panes;
2138 tty_write(tty_cmd_rawstring, &ttyctx);
2141 /* Turn alternate screen on. */
2142 void
2143 screen_write_alternateon(struct screen_write_ctx *ctx, struct grid_cell *gc,
2144 int cursor)
2146 struct tty_ctx ttyctx;
2147 struct window_pane *wp = ctx->wp;
2149 if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
2150 return;
2152 screen_write_collect_flush(ctx, 0, __func__);
2153 screen_alternate_on(ctx->s, gc, cursor);
2155 screen_write_initctx(ctx, &ttyctx, 1);
2156 if (ttyctx.redraw_cb != NULL)
2157 ttyctx.redraw_cb(&ttyctx);
2160 /* Turn alternate screen off. */
2161 void
2162 screen_write_alternateoff(struct screen_write_ctx *ctx, struct grid_cell *gc,
2163 int cursor)
2165 struct tty_ctx ttyctx;
2166 struct window_pane *wp = ctx->wp;
2168 if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
2169 return;
2171 screen_write_collect_flush(ctx, 0, __func__);
2172 screen_alternate_off(ctx->s, gc, cursor);
2174 screen_write_initctx(ctx, &ttyctx, 1);
2175 if (ttyctx.redraw_cb != NULL)
2176 ttyctx.redraw_cb(&ttyctx);