Don't die if fail to get root directory, from Ben Boeckel.
[tmux-openbsd.git] / screen-write.c
blob784f02e2abbc58a97f411623057f7eafcb7f002d
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <string.h>
23 #include "tmux.h"
25 void screen_write_initctx(struct screen_write_ctx *, struct tty_ctx *, int);
26 void screen_write_overwrite(struct screen_write_ctx *, u_int);
27 int screen_write_combine(
28 struct screen_write_ctx *, const struct utf8_data *);
30 /* Initialise writing with a window. */
31 void
32 screen_write_start(
33 struct screen_write_ctx *ctx, struct window_pane *wp, struct screen *s)
35 ctx->wp = wp;
36 if (wp != NULL && s == NULL)
37 ctx->s = wp->screen;
38 else
39 ctx->s = s;
42 /* Finish writing. */
43 /* ARGSUSED */
44 void
45 screen_write_stop(unused struct screen_write_ctx *ctx)
50 /* Reset screen state. */
51 void
52 screen_write_reset(struct screen_write_ctx *ctx)
54 screen_reset_tabs(ctx->s);
56 screen_write_scrollregion(ctx, 0, screen_size_y(ctx->s) - 1);
58 screen_write_insertmode(ctx, 0);
59 screen_write_kcursormode(ctx, 0);
60 screen_write_kkeypadmode(ctx, 0);
61 screen_write_mousemode_off(ctx);
63 screen_write_clearscreen(ctx);
64 screen_write_cursormove(ctx, 0, 0);
67 /* Write character. */
68 void
69 screen_write_putc(
70 struct screen_write_ctx *ctx, struct grid_cell *gc, u_char ch)
72 gc->data = ch;
73 screen_write_cell(ctx, gc, NULL);
76 /* Calculate string length, with embedded formatting. */
77 size_t printflike2
78 screen_write_cstrlen(int utf8flag, const char *fmt, ...)
80 va_list ap;
81 char *msg, *msg2, *ptr, *ptr2;
82 size_t size;
84 va_start(ap, fmt);
85 xvasprintf(&msg, fmt, ap);
86 va_end(ap);
87 msg2 = xmalloc(strlen(msg) + 1);
89 ptr = msg;
90 ptr2 = msg2;
91 while (*ptr != '\0') {
92 if (ptr[0] == '#' && ptr[1] == '[') {
93 while (*ptr != ']' && *ptr != '\0')
94 ptr++;
95 if (*ptr == ']')
96 ptr++;
97 continue;
99 *ptr2++ = *ptr++;
101 *ptr2 = '\0';
103 size = screen_write_strlen(utf8flag, "%s", msg2);
105 xfree(msg);
106 xfree(msg2);
108 return (size);
111 /* Calculate string length. */
112 size_t printflike2
113 screen_write_strlen(int utf8flag, const char *fmt, ...)
115 va_list ap;
116 char *msg;
117 struct utf8_data utf8data;
118 u_char *ptr;
119 size_t left, size = 0;
121 va_start(ap, fmt);
122 xvasprintf(&msg, fmt, ap);
123 va_end(ap);
125 ptr = msg;
126 while (*ptr != '\0') {
127 if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) {
128 ptr++;
130 left = strlen(ptr);
131 if (left < utf8data.size - 1)
132 break;
133 while (utf8_append(&utf8data, *ptr))
134 ptr++;
135 ptr++;
137 size += utf8data.width;
138 } else {
139 size++;
140 ptr++;
144 xfree(msg);
145 return (size);
148 /* Write simple string (no UTF-8 or maximum length). */
149 void printflike3
150 screen_write_puts(
151 struct screen_write_ctx *ctx, struct grid_cell *gc, const char *fmt, ...)
153 va_list ap;
155 va_start(ap, fmt);
156 screen_write_vnputs(ctx, -1, gc, 0, fmt, ap);
157 va_end(ap);
160 /* Write string with length limit (-1 for unlimited). */
161 void printflike5
162 screen_write_nputs(struct screen_write_ctx *ctx,
163 ssize_t maxlen, struct grid_cell *gc, int utf8flag, const char *fmt, ...)
165 va_list ap;
167 va_start(ap, fmt);
168 screen_write_vnputs(ctx, maxlen, gc, utf8flag, fmt, ap);
169 va_end(ap);
172 void
173 screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
174 struct grid_cell *gc, int utf8flag, const char *fmt, va_list ap)
176 char *msg;
177 struct utf8_data utf8data;
178 u_char *ptr;
179 size_t left, size = 0;
181 xvasprintf(&msg, fmt, ap);
183 ptr = msg;
184 while (*ptr != '\0') {
185 if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) {
186 ptr++;
188 left = strlen(ptr);
189 if (left < utf8data.size - 1)
190 break;
191 while (utf8_append(&utf8data, *ptr))
192 ptr++;
193 ptr++;
195 if (maxlen > 0 &&
196 size + utf8data.width > (size_t) maxlen) {
197 while (size < (size_t) maxlen) {
198 screen_write_putc(ctx, gc, ' ');
199 size++;
201 break;
203 size += utf8data.width;
205 gc->flags |= GRID_FLAG_UTF8;
206 screen_write_cell(ctx, gc, &utf8data);
207 gc->flags &= ~GRID_FLAG_UTF8;
208 } else {
209 if (maxlen > 0 && size + 1 > (size_t) maxlen)
210 break;
212 size++;
213 screen_write_putc(ctx, gc, *ptr);
214 ptr++;
218 xfree(msg);
221 /* Write string, similar to nputs, but with embedded formatting (#[]). */
222 void printflike5
223 screen_write_cnputs(struct screen_write_ctx *ctx,
224 ssize_t maxlen, struct grid_cell *gc, int utf8flag, const char *fmt, ...)
226 struct grid_cell lgc;
227 struct utf8_data utf8data;
228 va_list ap;
229 char *msg;
230 u_char *ptr, *last;
231 size_t left, size = 0;
233 va_start(ap, fmt);
234 xvasprintf(&msg, fmt, ap);
235 va_end(ap);
237 memcpy(&lgc, gc, sizeof lgc);
239 ptr = msg;
240 while (*ptr != '\0') {
241 if (ptr[0] == '#' && ptr[1] == '[') {
242 ptr += 2;
243 last = ptr + strcspn(ptr, "]");
244 if (*last == '\0') {
245 /* No ]. Not much point in doing anything. */
246 break;
248 *last = '\0';
250 screen_write_parsestyle(gc, &lgc, ptr);
251 ptr = last + 1;
252 continue;
255 if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) {
256 ptr++;
258 left = strlen(ptr);
259 if (left < utf8data.size - 1)
260 break;
261 while (utf8_append(&utf8data, *ptr))
262 ptr++;
263 ptr++;
265 if (maxlen > 0 &&
266 size + utf8data.width > (size_t) maxlen) {
267 while (size < (size_t) maxlen) {
268 screen_write_putc(ctx, gc, ' ');
269 size++;
271 break;
273 size += utf8data.width;
275 lgc.flags |= GRID_FLAG_UTF8;
276 screen_write_cell(ctx, &lgc, &utf8data);
277 lgc.flags &= ~GRID_FLAG_UTF8;
278 } else {
279 if (maxlen > 0 && size + 1 > (size_t) maxlen)
280 break;
282 size++;
283 screen_write_putc(ctx, &lgc, *ptr);
284 ptr++;
288 xfree(msg);
291 /* Parse an embedded style of the form "fg=colour,bg=colour,bright,...". */
292 void
293 screen_write_parsestyle(
294 struct grid_cell *defgc, struct grid_cell *gc, const char *in)
296 const char delimiters[] = " ,";
297 char tmp[32];
298 int val;
299 size_t end;
300 u_char fg, bg, attr, flags;
302 if (*in == '\0')
303 return;
304 if (strchr(delimiters, in[strlen(in) - 1]) != NULL)
305 return;
307 fg = gc->fg;
308 bg = gc->bg;
309 attr = gc->attr;
310 flags = gc->flags;
311 do {
312 end = strcspn(in, delimiters);
313 if (end > (sizeof tmp) - 1)
314 return;
315 memcpy(tmp, in, end);
316 tmp[end] = '\0';
318 if (strcasecmp(tmp, "default") == 0) {
319 fg = defgc->fg;
320 bg = defgc->bg;
321 attr = defgc->attr;
322 } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
323 if ((val = colour_fromstring(tmp + 3)) == -1)
324 return;
325 if (*in == 'f' || *in == 'F') {
326 if (val != 8) {
327 if (val & 0x100) {
328 flags |= GRID_FLAG_FG256;
329 val &= ~0x100;
330 } else
331 flags &= ~GRID_FLAG_FG256;
332 fg = val;
333 } else
334 fg = defgc->fg;
335 } else if (*in == 'b' || *in == 'B') {
336 if (val != 8) {
337 if (val & 0x100) {
338 flags |= GRID_FLAG_BG256;
339 val &= ~0x100;
340 } else
341 flags &= ~GRID_FLAG_BG256;
342 bg = val;
343 } else
344 bg = defgc->bg;
345 } else
346 return;
347 } else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
348 if ((val = attributes_fromstring(tmp + 2)) == -1)
349 return;
350 attr &= ~val;
351 } else {
352 if ((val = attributes_fromstring(tmp)) == -1)
353 return;
354 attr |= val;
357 in += end + strspn(in + end, delimiters);
358 } while (*in != '\0');
359 gc->fg = fg;
360 gc->bg = bg;
361 gc->attr = attr;
362 gc->flags = flags;
365 /* Copy from another screen. */
366 void
367 screen_write_copy(struct screen_write_ctx *ctx,
368 struct screen *src, u_int px, u_int py, u_int nx, u_int ny)
370 struct screen *s = ctx->s;
371 struct grid *gd = src->grid;
372 struct grid_line *gl;
373 const struct grid_cell *gc;
374 const struct grid_utf8 *gu;
375 struct utf8_data utf8data;
376 u_int xx, yy, cx, cy, ax, bx;
378 cx = s->cx;
379 cy = s->cy;
380 for (yy = py; yy < py + ny; yy++) {
381 gl = &gd->linedata[yy];
382 if (yy < gd->hsize + gd->sy) {
384 * Find start and end position and copy between
385 * them. Limit to the real end of the line then use a
386 * clear EOL only if copying to the end, otherwise
387 * could overwrite whatever is there already.
389 if (px > gl->cellsize)
390 ax = gl->cellsize;
391 else
392 ax = px;
393 if (px + nx == gd->sx && px + nx > gl->cellsize)
394 bx = gl->cellsize;
395 else
396 bx = px + nx;
398 for (xx = ax; xx < bx; xx++) {
399 if (xx >= gl->cellsize)
400 gc = &grid_default_cell;
401 else
402 gc = &gl->celldata[xx];
403 if (!(gc->flags & GRID_FLAG_UTF8)) {
404 screen_write_cell(ctx, gc, NULL);
405 continue;
407 /* Reinject the UTF-8 sequence. */
408 gu = &gl->utf8data[xx];
409 utf8data.size = grid_utf8_copy(
410 gu, utf8data.data, sizeof utf8data.data);
411 utf8data.width = gu->width;
412 screen_write_cell(ctx, gc, &utf8data);
414 if (px + nx == gd->sx && px + nx > gl->cellsize)
415 screen_write_clearendofline(ctx);
416 } else
417 screen_write_clearline(ctx);
418 cy++;
419 screen_write_cursormove(ctx, cx, cy);
423 /* Set up context for TTY command. */
424 void
425 screen_write_initctx(
426 struct screen_write_ctx *ctx, struct tty_ctx *ttyctx, int save_last)
428 struct screen *s = ctx->s;
429 struct grid *gd = s->grid;
430 const struct grid_cell *gc;
431 const struct grid_utf8 *gu;
432 u_int xx;
434 ttyctx->wp = ctx->wp;
436 ttyctx->ocx = s->cx;
437 ttyctx->ocy = s->cy;
439 ttyctx->orlower = s->rlower;
440 ttyctx->orupper = s->rupper;
442 if (!save_last)
443 return;
445 /* Save the last cell on the screen. */
446 gc = &grid_default_cell;
447 for (xx = 1; xx <= screen_size_x(s); xx++) {
448 gc = grid_view_peek_cell(gd, screen_size_x(s) - xx, s->cy);
449 if (!(gc->flags & GRID_FLAG_PADDING))
450 break;
452 ttyctx->last_width = xx;
453 memcpy(&ttyctx->last_cell, gc, sizeof ttyctx->last_cell);
454 if (gc->flags & GRID_FLAG_UTF8) {
455 gu = grid_view_peek_utf8(gd, screen_size_x(s) - xx, s->cy);
456 memcpy(&ttyctx->last_utf8, gu, sizeof ttyctx->last_utf8);
460 /* Cursor up by ny. */
461 void
462 screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
464 struct screen *s = ctx->s;
466 if (ny == 0)
467 ny = 1;
469 if (s->cy < s->rupper) {
470 /* Above region. */
471 if (ny > s->cy)
472 ny = s->cy;
473 } else {
474 /* Below region. */
475 if (ny > s->cy - s->rupper)
476 ny = s->cy - s->rupper;
478 if (ny == 0)
479 return;
481 s->cy -= ny;
484 /* Cursor down by ny. */
485 void
486 screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
488 struct screen *s = ctx->s;
490 if (ny == 0)
491 ny = 1;
493 if (s->cy > s->rlower) {
494 /* Below region. */
495 if (ny > screen_size_y(s) - 1 - s->cy)
496 ny = screen_size_y(s) - 1 - s->cy;
497 } else {
498 /* Above region. */
499 if (ny > s->rlower - s->cy)
500 ny = s->rlower - s->cy;
502 if (ny == 0)
503 return;
505 s->cy += ny;
508 /* Cursor right by nx. */
509 void
510 screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
512 struct screen *s = ctx->s;
514 if (nx == 0)
515 nx = 1;
517 if (nx > screen_size_x(s) - 1 - s->cx)
518 nx = screen_size_x(s) - 1 - s->cx;
519 if (nx == 0)
520 return;
522 s->cx += nx;
525 /* Cursor left by nx. */
526 void
527 screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
529 struct screen *s = ctx->s;
531 if (nx == 0)
532 nx = 1;
534 if (nx > s->cx)
535 nx = s->cx;
536 if (nx == 0)
537 return;
539 s->cx -= nx;
542 /* Backspace; cursor left unless at start of wrapped line when can move up. */
543 void
544 screen_write_backspace(struct screen_write_ctx *ctx)
546 struct screen *s = ctx->s;
547 struct grid_line *gl;
549 if (s->cx == 0) {
550 if (s->cy == 0)
551 return;
552 gl = &s->grid->linedata[s->grid->hsize + s->cy - 1];
553 if (gl->flags & GRID_LINE_WRAPPED) {
554 s->cy--;
555 s->cx = screen_size_x(s) - 1;
557 } else
558 s->cx--;
561 /* VT100 alignment test. */
562 void
563 screen_write_alignmenttest(struct screen_write_ctx *ctx)
565 struct screen *s = ctx->s;
566 struct tty_ctx ttyctx;
567 struct grid_cell gc;
568 u_int xx, yy;
570 screen_write_initctx(ctx, &ttyctx, 0);
572 memcpy(&gc, &grid_default_cell, sizeof gc);
573 gc.data = 'E';
575 for (yy = 0; yy < screen_size_y(s); yy++) {
576 for (xx = 0; xx < screen_size_x(s); xx++)
577 grid_view_set_cell(s->grid, xx, yy, &gc);
580 s->cx = 0;
581 s->cy = 0;
583 s->rupper = 0;
585 s->rlower = screen_size_y(s) - 1;
587 tty_write(tty_cmd_alignmenttest, &ttyctx);
590 /* Insert nx characters. */
591 void
592 screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx)
594 struct screen *s = ctx->s;
595 struct tty_ctx ttyctx;
597 if (nx == 0)
598 nx = 1;
600 if (nx > screen_size_x(s) - s->cx)
601 nx = screen_size_x(s) - s->cx;
602 if (nx == 0)
603 return;
605 screen_write_initctx(ctx, &ttyctx, 0);
607 if (s->cx <= screen_size_x(s) - 1)
608 grid_view_insert_cells(s->grid, s->cx, s->cy, nx);
610 ttyctx.num = nx;
611 tty_write(tty_cmd_insertcharacter, &ttyctx);
614 /* Delete nx characters. */
615 void
616 screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx)
618 struct screen *s = ctx->s;
619 struct tty_ctx ttyctx;
621 if (nx == 0)
622 nx = 1;
624 if (nx > screen_size_x(s) - s->cx)
625 nx = screen_size_x(s) - s->cx;
626 if (nx == 0)
627 return;
629 screen_write_initctx(ctx, &ttyctx, 0);
631 if (s->cx <= screen_size_x(s) - 1)
632 grid_view_delete_cells(s->grid, s->cx, s->cy, nx);
634 ttyctx.num = nx;
635 tty_write(tty_cmd_deletecharacter, &ttyctx);
638 /* Insert ny lines. */
639 void
640 screen_write_insertline(struct screen_write_ctx *ctx, u_int ny)
642 struct screen *s = ctx->s;
643 struct tty_ctx ttyctx;
645 if (ny == 0)
646 ny = 1;
648 if (s->cy < s->rupper || s->cy > s->rlower) {
649 if (ny > screen_size_y(s) - s->cy)
650 ny = screen_size_y(s) - s->cy;
651 if (ny == 0)
652 return;
654 screen_write_initctx(ctx, &ttyctx, 0);
656 grid_view_insert_lines(s->grid, s->cy, ny);
658 ttyctx.num = ny;
659 tty_write(tty_cmd_insertline, &ttyctx);
660 return;
663 if (ny > s->rlower + 1 - s->cy)
664 ny = s->rlower + 1 - s->cy;
665 if (ny == 0)
666 return;
668 screen_write_initctx(ctx, &ttyctx, 0);
670 if (s->cy < s->rupper || s->cy > s->rlower)
671 grid_view_insert_lines(s->grid, s->cy, ny);
672 else
673 grid_view_insert_lines_region(s->grid, s->rlower, s->cy, ny);
675 ttyctx.num = ny;
676 tty_write(tty_cmd_insertline, &ttyctx);
679 /* Delete ny lines. */
680 void
681 screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny)
683 struct screen *s = ctx->s;
684 struct tty_ctx ttyctx;
686 if (ny == 0)
687 ny = 1;
689 if (s->cy < s->rupper || s->cy > s->rlower) {
690 if (ny > screen_size_y(s) - s->cy)
691 ny = screen_size_y(s) - s->cy;
692 if (ny == 0)
693 return;
695 screen_write_initctx(ctx, &ttyctx, 0);
697 grid_view_delete_lines(s->grid, s->cy, ny);
699 ttyctx.num = ny;
700 tty_write(tty_cmd_deleteline, &ttyctx);
701 return;
704 if (ny > s->rlower + 1 - s->cy)
705 ny = s->rlower + 1 - s->cy;
706 if (ny == 0)
707 return;
709 screen_write_initctx(ctx, &ttyctx, 0);
711 if (s->cy < s->rupper || s->cy > s->rlower)
712 grid_view_delete_lines(s->grid, s->cy, ny);
713 else
714 grid_view_delete_lines_region(s->grid, s->rlower, s->cy, ny);
716 ttyctx.num = ny;
717 tty_write(tty_cmd_deleteline, &ttyctx);
720 /* Clear line at cursor. */
721 void
722 screen_write_clearline(struct screen_write_ctx *ctx)
724 struct screen *s = ctx->s;
725 struct tty_ctx ttyctx;
727 screen_write_initctx(ctx, &ttyctx, 0);
729 grid_view_clear(s->grid, 0, s->cy, screen_size_x(s), 1);
731 tty_write(tty_cmd_clearline, &ttyctx);
734 /* Clear to end of line from cursor. */
735 void
736 screen_write_clearendofline(struct screen_write_ctx *ctx)
738 struct screen *s = ctx->s;
739 struct tty_ctx ttyctx;
740 u_int sx;
742 screen_write_initctx(ctx, &ttyctx, 0);
744 sx = screen_size_x(s);
746 if (s->cx <= sx - 1)
747 grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
749 tty_write(tty_cmd_clearendofline, &ttyctx);
752 /* Clear to start of line from cursor. */
753 void
754 screen_write_clearstartofline(struct screen_write_ctx *ctx)
756 struct screen *s = ctx->s;
757 struct tty_ctx ttyctx;
758 u_int sx;
760 screen_write_initctx(ctx, &ttyctx, 0);
762 sx = screen_size_x(s);
764 if (s->cx > sx - 1)
765 grid_view_clear(s->grid, 0, s->cy, sx, 1);
766 else
767 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
769 tty_write(tty_cmd_clearstartofline, &ttyctx);
772 /* Move cursor to px,py. */
773 void
774 screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
776 struct screen *s = ctx->s;
778 if (px > screen_size_x(s) - 1)
779 px = screen_size_x(s) - 1;
780 if (py > screen_size_y(s) - 1)
781 py = screen_size_y(s) - 1;
783 s->cx = px;
784 s->cy = py;
787 /* Set cursor mode. */
788 void
789 screen_write_cursormode(struct screen_write_ctx *ctx, int state)
791 struct screen *s = ctx->s;
793 if (state)
794 s->mode |= MODE_CURSOR;
795 else
796 s->mode &= ~MODE_CURSOR;
799 /* Reverse index (up with scroll). */
800 void
801 screen_write_reverseindex(struct screen_write_ctx *ctx)
803 struct screen *s = ctx->s;
804 struct tty_ctx ttyctx;
806 screen_write_initctx(ctx, &ttyctx, 0);
808 if (s->cy == s->rupper)
809 grid_view_scroll_region_down(s->grid, s->rupper, s->rlower);
810 else if (s->cy > 0)
811 s->cy--;
813 tty_write(tty_cmd_reverseindex, &ttyctx);
816 /* Set scroll region. */
817 void
818 screen_write_scrollregion(
819 struct screen_write_ctx *ctx, u_int rupper, u_int rlower)
821 struct screen *s = ctx->s;
823 if (rupper > screen_size_y(s) - 1)
824 rupper = screen_size_y(s) - 1;
825 if (rlower > screen_size_y(s) - 1)
826 rlower = screen_size_y(s) - 1;
827 if (rupper >= rlower) /* cannot be one line */
828 return;
830 /* Cursor moves to top-left. */
831 s->cx = 0;
832 s->cy = 0;
834 s->rupper = rupper;
835 s->rlower = rlower;
838 /* Set insert mode. */
839 void
840 screen_write_insertmode(struct screen_write_ctx *ctx, int state)
842 struct screen *s = ctx->s;
844 if (state)
845 s->mode |= MODE_INSERT;
846 else
847 s->mode &= ~MODE_INSERT;
850 /* Set UTF-8 mouse mode. */
851 void
852 screen_write_utf8mousemode(struct screen_write_ctx *ctx, int state)
854 struct screen *s = ctx->s;
856 if (state)
857 s->mode |= MODE_MOUSE_UTF8;
858 else
859 s->mode &= ~MODE_MOUSE_UTF8;
862 /* Set mouse mode off. */
863 void
864 screen_write_mousemode_off(struct screen_write_ctx *ctx)
866 struct screen *s = ctx->s;
868 s->mode &= ~ALL_MOUSE_MODES;
871 /* Set mouse mode on. */
872 void
873 screen_write_mousemode_on(struct screen_write_ctx *ctx, int mode)
875 struct screen *s = ctx->s;
877 s->mode &= ~ALL_MOUSE_MODES;
878 s->mode |= mode;
881 /* Line feed. */
882 void
883 screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped)
885 struct screen *s = ctx->s;
886 struct grid_line *gl;
887 struct tty_ctx ttyctx;
889 screen_write_initctx(ctx, &ttyctx, 0);
891 gl = &s->grid->linedata[s->grid->hsize + s->cy];
892 if (wrapped)
893 gl->flags |= GRID_LINE_WRAPPED;
894 else
895 gl->flags &= ~GRID_LINE_WRAPPED;
897 if (s->cy == s->rlower)
898 grid_view_scroll_region_up(s->grid, s->rupper, s->rlower);
899 else if (s->cy < screen_size_y(s) - 1)
900 s->cy++;
902 ttyctx.num = wrapped;
903 tty_write(tty_cmd_linefeed, &ttyctx);
906 /* Carriage return (cursor to start of line). */
907 void
908 screen_write_carriagereturn(struct screen_write_ctx *ctx)
910 struct screen *s = ctx->s;
912 s->cx = 0;
915 /* Set keypad cursor keys mode. */
916 void
917 screen_write_kcursormode(struct screen_write_ctx *ctx, int state)
919 struct screen *s = ctx->s;
921 if (state)
922 s->mode |= MODE_KCURSOR;
923 else
924 s->mode &= ~MODE_KCURSOR;
927 /* Set keypad number keys mode. */
928 void
929 screen_write_kkeypadmode(struct screen_write_ctx *ctx, int state)
931 struct screen *s = ctx->s;
933 if (state)
934 s->mode |= MODE_KKEYPAD;
935 else
936 s->mode &= ~MODE_KKEYPAD;
939 /* Clear to end of screen from cursor. */
940 void
941 screen_write_clearendofscreen(struct screen_write_ctx *ctx)
943 struct screen *s = ctx->s;
944 struct tty_ctx ttyctx;
945 u_int sx, sy;
947 screen_write_initctx(ctx, &ttyctx, 0);
949 sx = screen_size_x(s);
950 sy = screen_size_y(s);
952 /* Scroll into history if it is enabled and clearing entire screen. */
953 if (s->cy == 0 && s->grid->flags & GRID_HISTORY)
954 grid_view_clear_history(s->grid);
955 else {
956 if (s->cx <= sx - 1)
957 grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
958 grid_view_clear(s->grid, 0, s->cy + 1, sx, sy - (s->cy + 1));
961 tty_write(tty_cmd_clearendofscreen, &ttyctx);
964 /* Clear to start of screen. */
965 void
966 screen_write_clearstartofscreen(struct screen_write_ctx *ctx)
968 struct screen *s = ctx->s;
969 struct tty_ctx ttyctx;
970 u_int sx;
972 screen_write_initctx(ctx, &ttyctx, 0);
974 sx = screen_size_x(s);
976 if (s->cy > 0)
977 grid_view_clear(s->grid, 0, 0, sx, s->cy);
978 if (s->cx > sx - 1)
979 grid_view_clear(s->grid, 0, s->cy, sx, 1);
980 else
981 grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
983 tty_write(tty_cmd_clearstartofscreen, &ttyctx);
986 /* Clear entire screen. */
987 void
988 screen_write_clearscreen(struct screen_write_ctx *ctx)
990 struct screen *s = ctx->s;
991 struct tty_ctx ttyctx;
993 screen_write_initctx(ctx, &ttyctx, 0);
995 /* Scroll into history if it is enabled. */
996 if (s->grid->flags & GRID_HISTORY)
997 grid_view_clear_history(s->grid);
998 else {
999 grid_view_clear(
1000 s->grid, 0, 0, screen_size_x(s), screen_size_y(s));
1003 tty_write(tty_cmd_clearscreen, &ttyctx);
1006 /* Clear entire history. */
1007 void
1008 screen_write_clearhistory(struct screen_write_ctx *ctx)
1010 struct screen *s = ctx->s;
1011 struct grid *gd = s->grid;
1013 grid_move_lines(gd, 0, gd->hsize, gd->sy);
1014 gd->hsize = 0;
1017 /* Write cell data. */
1018 void
1019 screen_write_cell(struct screen_write_ctx *ctx,
1020 const struct grid_cell *gc, const struct utf8_data *utf8data)
1022 struct screen *s = ctx->s;
1023 struct grid *gd = s->grid;
1024 struct tty_ctx ttyctx;
1025 struct grid_utf8 gu;
1026 u_int width, xx;
1027 struct grid_cell tmp_gc, *tmp_gcp;
1028 int insert = 0;
1030 /* Ignore padding. */
1031 if (gc->flags & GRID_FLAG_PADDING)
1032 return;
1034 /* Find character width. */
1035 if (gc->flags & GRID_FLAG_UTF8)
1036 width = utf8data->width;
1037 else
1038 width = 1;
1041 * If this is a wide character and there is no room on the screen, for
1042 * the entire character, don't print it.
1044 if (!(s->mode & MODE_WRAP)
1045 && (width > 1 && (width > screen_size_x(s) ||
1046 (s->cx != screen_size_x(s)
1047 && s->cx > screen_size_x(s) - width))))
1048 return;
1051 * If the width is zero, combine onto the previous character, if
1052 * there is space.
1054 if (width == 0) {
1055 if (screen_write_combine(ctx, utf8data) == 0) {
1056 screen_write_initctx(ctx, &ttyctx, 0);
1057 tty_write(tty_cmd_utf8character, &ttyctx);
1059 return;
1062 /* Initialise the redraw context, saving the last cell. */
1063 screen_write_initctx(ctx, &ttyctx, 1);
1065 /* If in insert mode, make space for the cells. */
1066 if (s->mode & MODE_INSERT && s->cx <= screen_size_x(s) - width) {
1067 xx = screen_size_x(s) - s->cx - width;
1068 grid_move_cells(s->grid, s->cx + width, s->cx, s->cy, xx);
1069 insert = 1;
1072 /* Check this will fit on the current line and wrap if not. */
1073 if ((s->mode & MODE_WRAP) && s->cx > screen_size_x(s) - width) {
1074 screen_write_linefeed(ctx, 1);
1075 s->cx = 0; /* carriage return */
1078 /* Sanity checks. */
1079 if (((s->mode & MODE_WRAP) && s->cx > screen_size_x(s) - width)
1080 || s->cy > screen_size_y(s) - 1)
1081 return;
1083 /* Handle overwriting of UTF-8 characters. */
1084 screen_write_overwrite(ctx, width);
1087 * If the new character is UTF-8 wide, fill in padding cells. Have
1088 * already ensured there is enough room.
1090 for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1091 tmp_gcp = grid_view_get_cell(gd, xx, s->cy);
1092 if (tmp_gcp != NULL)
1093 tmp_gcp->flags |= GRID_FLAG_PADDING;
1096 /* Set the cell. */
1097 grid_view_set_cell(gd, s->cx, s->cy, gc);
1098 if (gc->flags & GRID_FLAG_UTF8) {
1099 /* Construct UTF-8 and write it. */
1100 grid_utf8_set(&gu, utf8data);
1101 grid_view_set_utf8(gd, s->cx, s->cy, &gu);
1104 /* Move the cursor. */
1105 s->cx += width;
1107 /* Draw to the screen if necessary. */
1108 if (insert) {
1109 ttyctx.num = width;
1110 tty_write(tty_cmd_insertcharacter, &ttyctx);
1112 ttyctx.utf8 = &gu;
1113 if (screen_check_selection(s, s->cx - width, s->cy)) {
1114 memcpy(&tmp_gc, &s->sel.cell, sizeof tmp_gc);
1115 tmp_gc.data = gc->data;
1116 tmp_gc.flags = gc->flags &
1117 ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
1118 tmp_gc.flags |= s->sel.cell.flags &
1119 (GRID_FLAG_FG256|GRID_FLAG_BG256);
1120 ttyctx.cell = &tmp_gc;
1121 tty_write(tty_cmd_cell, &ttyctx);
1122 } else {
1123 ttyctx.cell = gc;
1124 tty_write(tty_cmd_cell, &ttyctx);
1128 /* Combine a UTF-8 zero-width character onto the previous. */
1130 screen_write_combine(
1131 struct screen_write_ctx *ctx, const struct utf8_data *utf8data)
1133 struct screen *s = ctx->s;
1134 struct grid *gd = s->grid;
1135 struct grid_cell *gc;
1136 struct grid_utf8 *gu, tmp_gu;
1137 u_int i;
1139 /* Can't combine if at 0. */
1140 if (s->cx == 0)
1141 return (-1);
1143 /* Empty utf8data is out. */
1144 if (utf8data->size == 0)
1145 fatalx("UTF-8 data empty");
1147 /* Retrieve the previous cell and convert to UTF-8 if not already. */
1148 gc = grid_view_get_cell(gd, s->cx - 1, s->cy);
1149 if (!(gc->flags & GRID_FLAG_UTF8)) {
1150 tmp_gu.data[0] = gc->data;
1151 tmp_gu.data[1] = 0xff;
1152 tmp_gu.width = 1;
1154 grid_view_set_utf8(gd, s->cx - 1, s->cy, &tmp_gu);
1155 gc->flags |= GRID_FLAG_UTF8;
1158 /* Append the current cell. */
1159 gu = grid_view_get_utf8(gd, s->cx - 1, s->cy);
1160 if (grid_utf8_append(gu, utf8data) != 0) {
1161 /* Failed: scrap this character and replace with underscores. */
1162 if (gu->width == 1) {
1163 gc->data = '_';
1164 gc->flags &= ~GRID_FLAG_UTF8;
1165 } else {
1166 for (i = 0; i < gu->width && i != sizeof gu->data; i++)
1167 gu->data[i] = '_';
1168 if (i != sizeof gu->data)
1169 gu->data[i] = 0xff;
1170 gu->width = i;
1174 return (0);
1178 * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1179 * cell on the screen, so following cells must not be drawn by marking them as
1180 * padding.
1182 * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1183 * character, it is necessary to also overwrite any other cells which covered
1184 * by the same character.
1186 void
1187 screen_write_overwrite(struct screen_write_ctx *ctx, u_int width)
1189 struct screen *s = ctx->s;
1190 struct grid *gd = s->grid;
1191 const struct grid_cell *gc;
1192 u_int xx;
1194 gc = grid_view_peek_cell(gd, s->cx, s->cy);
1195 if (gc->flags & GRID_FLAG_PADDING) {
1197 * A padding cell, so clear any following and leading padding
1198 * cells back to the character. Don't overwrite the current
1199 * cell as that happens later anyway.
1201 xx = s->cx + 1;
1202 while (--xx > 0) {
1203 gc = grid_view_peek_cell(gd, xx, s->cy);
1204 if (!(gc->flags & GRID_FLAG_PADDING))
1205 break;
1206 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1209 /* Overwrite the character at the start of this padding. */
1210 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1214 * Overwrite any padding cells that belong to a UTF-8 character
1215 * we'll be overwriting with the current character.
1217 xx = s->cx + width - 1;
1218 while (++xx < screen_size_x(s)) {
1219 gc = grid_view_peek_cell(gd, xx, s->cy);
1220 if (!(gc->flags & GRID_FLAG_PADDING))
1221 break;
1222 grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1226 void
1227 screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
1229 struct tty_ctx ttyctx;
1231 screen_write_initctx(ctx, &ttyctx, 0);
1232 ttyctx.ptr = str;
1233 ttyctx.num = len;
1235 tty_write(tty_cmd_setselection, &ttyctx);
1238 void
1239 screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
1241 struct tty_ctx ttyctx;
1243 screen_write_initctx(ctx, &ttyctx, 0);
1244 ttyctx.ptr = str;
1245 ttyctx.num = len;
1247 tty_write(tty_cmd_rawstring, &ttyctx);