Annotation of src/usr.bin/tmux/screen-write.c, Revision 1.128
1.128 ! nicm 1: /* $OpenBSD: screen-write.c,v 1.127 2017/06/04 08:02:20 nicm Exp $ */
1.1 nicm 2:
3: /*
1.84 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20:
1.56 nicm 21: #include <stdlib.h>
1.1 nicm 22: #include <string.h>
23:
24: #include "tmux.h"
25:
1.87 nicm 26: static void screen_write_initctx(struct screen_write_ctx *,
27: struct tty_ctx *);
1.109 nicm 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 *);
1.111 nicm 31: static void screen_write_collect_flush(struct screen_write_ctx *, int);
1.87 nicm 32:
1.89 nicm 33: static int screen_write_overwrite(struct screen_write_ctx *,
34: struct grid_cell *, u_int);
1.104 nicm 35: static const struct grid_cell *screen_write_combine(struct screen_write_ctx *,
36: const struct utf8_data *, u_int *);
1.1 nicm 37:
1.88 nicm 38: static const struct grid_cell screen_write_pad_cell = {
1.91 nicm 39: GRID_FLAG_PADDING, 0, 8, 8, { { 0 }, 0, 0, 0 }
1.88 nicm 40: };
41:
1.109 nicm 42: struct screen_write_collect_item {
43: u_int x;
1.118 nicm 44: int wrapped;
1.109 nicm 45:
46: u_int used;
47: char data[256];
48:
49: struct grid_cell gc;
50:
1.126 nicm 51: TAILQ_ENTRY(screen_write_collect_item) entry;
1.109 nicm 52: };
53: struct screen_write_collect_line {
54: TAILQ_HEAD(, screen_write_collect_item) items;
55: };
1.92 nicm 56:
57: /* Initialize writing with a window. */
1.1 nicm 58: void
1.71 nicm 59: screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp,
60: struct screen *s)
1.1 nicm 61: {
1.108 nicm 62: char tmp[16];
1.109 nicm 63: u_int y;
64:
65: memset(ctx, 0, sizeof *ctx);
1.92 nicm 66:
1.1 nicm 67: ctx->wp = wp;
68: if (wp != NULL && s == NULL)
69: ctx->s = wp->screen;
70: else
71: ctx->s = s;
1.92 nicm 72:
1.109 nicm 73: ctx->list = xcalloc(screen_size_y(ctx->s), sizeof *ctx->list);
74: for (y = 0; y < screen_size_y(ctx->s); y++)
75: TAILQ_INIT(&ctx->list[y].items);
76: ctx->item = xcalloc(1, sizeof *ctx->item);
1.92 nicm 77:
1.122 nicm 78: ctx->scrolled = 0;
79: ctx->bg = 8;
80:
1.108 nicm 81: if (wp != NULL)
1.92 nicm 82: snprintf(tmp, sizeof tmp, "pane %%%u", wp->id);
83: log_debug("%s: size %ux%u, %s", __func__, screen_size_x(ctx->s),
1.108 nicm 84: screen_size_y(ctx->s), wp == NULL ? "no pane" : tmp);
1.1 nicm 85: }
86:
87: /* Finish writing. */
88: void
1.92 nicm 89: screen_write_stop(struct screen_write_ctx *ctx)
90: {
1.109 nicm 91: screen_write_collect_end(ctx);
1.111 nicm 92: screen_write_collect_flush(ctx, 0);
1.92 nicm 93:
1.109 nicm 94: log_debug("%s: %u cells (%u written, %u skipped)", __func__,
95: ctx->cells, ctx->written, ctx->skipped);
1.92 nicm 96:
1.109 nicm 97: free(ctx->item);
98: free(ctx->list); /* flush will have emptied */
1.52 nicm 99: }
100:
101: /* Reset screen state. */
102: void
103: screen_write_reset(struct screen_write_ctx *ctx)
104: {
1.61 nicm 105: struct screen *s = ctx->s;
1.52 nicm 106:
1.61 nicm 107: screen_reset_tabs(s);
108: screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
1.63 nicm 109:
1.67 nicm 110: s->mode &= ~(MODE_INSERT|MODE_KCURSOR|MODE_KKEYPAD|MODE_FOCUSON);
1.82 nicm 111: s->mode &= ~(ALL_MOUSE_MODES|MODE_MOUSE_UTF8|MODE_MOUSE_SGR);
1.52 nicm 112:
1.99 nicm 113: screen_write_clearscreen(ctx, 8);
1.52 nicm 114: screen_write_cursormove(ctx, 0, 0);
1.1 nicm 115: }
116:
117: /* Write character. */
118: void
1.86 nicm 119: screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.69 nicm 120: u_char ch)
1.1 nicm 121: {
1.86 nicm 122: struct grid_cell gc;
123:
124: memcpy(&gc, gcp, sizeof gc);
125:
126: utf8_set(&gc.data, ch);
127: screen_write_cell(ctx, &gc);
1.1 nicm 128: }
129:
1.24 nicm 130: /* Calculate string length, with embedded formatting. */
1.71 nicm 131: size_t
1.75 nicm 132: screen_write_cstrlen(const char *fmt, ...)
1.24 nicm 133: {
134: va_list ap;
135: char *msg, *msg2, *ptr, *ptr2;
136: size_t size;
137:
138: va_start(ap, fmt);
139: xvasprintf(&msg, fmt, ap);
140: va_end(ap);
141: msg2 = xmalloc(strlen(msg) + 1);
142:
143: ptr = msg;
144: ptr2 = msg2;
145: while (*ptr != '\0') {
146: if (ptr[0] == '#' && ptr[1] == '[') {
147: while (*ptr != ']' && *ptr != '\0')
148: ptr++;
149: if (*ptr == ']')
150: ptr++;
151: continue;
152: }
153: *ptr2++ = *ptr++;
154: }
155: *ptr2 = '\0';
156:
1.75 nicm 157: size = screen_write_strlen("%s", msg2);
1.24 nicm 158:
1.56 nicm 159: free(msg);
160: free(msg2);
1.24 nicm 161:
162: return (size);
163: }
164:
1.2 nicm 165: /* Calculate string length. */
1.71 nicm 166: size_t
1.75 nicm 167: screen_write_strlen(const char *fmt, ...)
1.2 nicm 168: {
1.36 nicm 169: va_list ap;
170: char *msg;
1.76 nicm 171: struct utf8_data ud;
1.36 nicm 172: u_char *ptr;
173: size_t left, size = 0;
1.79 nicm 174: enum utf8_state more;
1.2 nicm 175:
176: va_start(ap, fmt);
177: xvasprintf(&msg, fmt, ap);
178: va_end(ap);
179:
180: ptr = msg;
181: while (*ptr != '\0') {
1.79 nicm 182: if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
1.36 nicm 183: ptr++;
1.2 nicm 184:
185: left = strlen(ptr);
1.77 nicm 186: if (left < (size_t)ud.size - 1)
1.36 nicm 187: break;
1.79 nicm 188: while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
1.2 nicm 189: ptr++;
1.36 nicm 190: ptr++;
191:
1.79 nicm 192: if (more == UTF8_DONE)
1.78 nicm 193: size += ud.width;
1.2 nicm 194: } else {
1.75 nicm 195: if (*ptr > 0x1f && *ptr < 0x7f)
196: size++;
1.2 nicm 197: ptr++;
198: }
1.7 ray 199: }
1.2 nicm 200:
1.56 nicm 201: free(msg);
1.2 nicm 202: return (size);
203: }
204:
1.3 nicm 205: /* Write simple string (no UTF-8 or maximum length). */
1.71 nicm 206: void
1.86 nicm 207: screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.71 nicm 208: const char *fmt, ...)
1.1 nicm 209: {
210: va_list ap;
211:
212: va_start(ap, fmt);
1.86 nicm 213: screen_write_vnputs(ctx, -1, gcp, fmt, ap);
1.2 nicm 214: va_end(ap);
215: }
216:
217: /* Write string with length limit (-1 for unlimited). */
1.71 nicm 218: void
219: screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86 nicm 220: const struct grid_cell *gcp, const char *fmt, ...)
1.2 nicm 221: {
222: va_list ap;
223:
224: va_start(ap, fmt);
1.86 nicm 225: screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
1.2 nicm 226: va_end(ap);
227: }
228:
229: void
1.3 nicm 230: screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86 nicm 231: const struct grid_cell *gcp, const char *fmt, va_list ap)
1.2 nicm 232: {
1.86 nicm 233: struct grid_cell gc;
234: struct utf8_data *ud = &gc.data;
1.36 nicm 235: char *msg;
236: u_char *ptr;
237: size_t left, size = 0;
1.79 nicm 238: enum utf8_state more;
1.2 nicm 239:
1.86 nicm 240: memcpy(&gc, gcp, sizeof gc);
1.1 nicm 241: xvasprintf(&msg, fmt, ap);
242:
1.2 nicm 243: ptr = msg;
244: while (*ptr != '\0') {
1.86 nicm 245: if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36 nicm 246: ptr++;
1.2 nicm 247:
248: left = strlen(ptr);
1.86 nicm 249: if (left < (size_t)ud->size - 1)
1.36 nicm 250: break;
1.86 nicm 251: while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.2 nicm 252: ptr++;
1.36 nicm 253: ptr++;
1.7 ray 254:
1.86 nicm 255: if (more != UTF8_DONE)
256: continue;
257: if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
258: while (size < (size_t)maxlen) {
259: screen_write_putc(ctx, &gc, ' ');
260: size++;
1.2 nicm 261: }
1.86 nicm 262: break;
1.2 nicm 263: }
1.86 nicm 264: size += ud->width;
265: screen_write_cell(ctx, &gc);
1.2 nicm 266: } else {
1.86 nicm 267: if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.2 nicm 268: break;
269:
1.57 nicm 270: if (*ptr == '\001')
1.86 nicm 271: gc.attr ^= GRID_ATTR_CHARSET;
1.75 nicm 272: else if (*ptr > 0x1f && *ptr < 0x7f) {
1.57 nicm 273: size++;
1.86 nicm 274: screen_write_putc(ctx, &gc, *ptr);
1.57 nicm 275: }
1.2 nicm 276: ptr++;
277: }
278: }
1.1 nicm 279:
1.56 nicm 280: free(msg);
1.24 nicm 281: }
282:
283: /* Write string, similar to nputs, but with embedded formatting (#[]). */
1.71 nicm 284: void
1.75 nicm 285: screen_write_cnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86 nicm 286: const struct grid_cell *gcp, const char *fmt, ...)
1.24 nicm 287: {
1.86 nicm 288: struct grid_cell gc;
289: struct utf8_data *ud = &gc.data;
1.24 nicm 290: va_list ap;
291: char *msg;
1.36 nicm 292: u_char *ptr, *last;
1.24 nicm 293: size_t left, size = 0;
1.79 nicm 294: enum utf8_state more;
1.24 nicm 295:
1.86 nicm 296: memcpy(&gc, gcp, sizeof gc);
297:
1.24 nicm 298: va_start(ap, fmt);
299: xvasprintf(&msg, fmt, ap);
300: va_end(ap);
301:
302: ptr = msg;
303: while (*ptr != '\0') {
304: if (ptr[0] == '#' && ptr[1] == '[') {
305: ptr += 2;
306: last = ptr + strcspn(ptr, "]");
307: if (*last == '\0') {
308: /* No ]. Not much point in doing anything. */
309: break;
310: }
311: *last = '\0';
312:
1.86 nicm 313: style_parse(gcp, &gc, ptr);
1.24 nicm 314: ptr = last + 1;
315: continue;
316: }
317:
1.86 nicm 318: if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36 nicm 319: ptr++;
1.24 nicm 320:
321: left = strlen(ptr);
1.86 nicm 322: if (left < (size_t)ud->size - 1)
1.36 nicm 323: break;
1.86 nicm 324: while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.24 nicm 325: ptr++;
1.36 nicm 326: ptr++;
1.24 nicm 327:
1.86 nicm 328: if (more != UTF8_DONE)
329: continue;
330: if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
331: while (size < (size_t)maxlen) {
332: screen_write_putc(ctx, &gc, ' ');
333: size++;
1.24 nicm 334: }
1.86 nicm 335: break;
1.24 nicm 336: }
1.86 nicm 337: size += ud->width;
338: screen_write_cell(ctx, &gc);
1.24 nicm 339: } else {
1.86 nicm 340: if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.24 nicm 341: break;
342:
1.75 nicm 343: if (*ptr > 0x1f && *ptr < 0x7f) {
344: size++;
1.86 nicm 345: screen_write_putc(ctx, &gc, *ptr);
1.75 nicm 346: }
1.24 nicm 347: ptr++;
348: }
349: }
350:
1.56 nicm 351: free(msg);
1.1 nicm 352: }
353:
354: /* Copy from another screen. */
355: void
1.95 nicm 356: screen_write_copy(struct screen_write_ctx *ctx, struct screen *src, u_int px,
1.102 nicm 357: u_int py, u_int nx, u_int ny, bitstr_t *markbs,
358: const struct grid_cell *markgc)
1.1 nicm 359: {
360: struct screen *s = ctx->s;
361: struct grid *gd = src->grid;
1.77 nicm 362: struct grid_cell gc;
1.102 nicm 363: u_int xx, yy, cx, cy, b;
1.1 nicm 364:
1.125 nicm 365: if (nx == 0 || ny == 0)
366: return;
367:
1.1 nicm 368: cx = s->cx;
369: cy = s->cy;
1.96 nicm 370:
1.1 nicm 371: for (yy = py; yy < py + ny; yy++) {
1.96 nicm 372: for (xx = px; xx < px + nx; xx++) {
373: grid_get_cell(gd, xx, yy, &gc);
1.102 nicm 374: if (markbs != NULL) {
375: b = (yy * screen_size_x(src)) + xx;
376: if (bit_test(markbs, b)) {
377: gc.attr = markgc->attr;
378: gc.fg = markgc->fg;
379: gc.bg = markgc->bg;
380: }
381: }
1.96 nicm 382: screen_write_cell(ctx, &gc);
383: }
384:
1.1 nicm 385: cy++;
386: screen_write_cursormove(ctx, cx, cy);
1.125 nicm 387: }
388: }
389:
390: /* Draw a line on screen. */
391: void
392: screen_write_line(struct screen_write_ctx *ctx, u_int nx, int left, int right)
393: {
394: struct screen *s = ctx->s;
395: struct grid_cell gc;
396: u_int cx, cy, i;
397:
398: cx = s->cx;
399: cy = s->cy;
400:
401: memcpy(&gc, &grid_default_cell, sizeof gc);
402: gc.attr |= GRID_ATTR_CHARSET;
403:
404: screen_write_putc(ctx, &gc, left ? 't' : 'q');
405: for (i = 1; i < nx - 1; i++)
406: screen_write_putc(ctx, &gc, 'q');
407: screen_write_putc(ctx, &gc, right ? 'u' : 'q');
408:
409: screen_write_cursormove(ctx, cx, cy);
410: }
411:
412: /* Draw a box on screen. */
413: void
414: screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny)
415: {
416: struct screen *s = ctx->s;
417: struct grid_cell gc;
418: u_int cx, cy, i;
419:
420: cx = s->cx;
421: cy = s->cy;
422:
423: memcpy(&gc, &grid_default_cell, sizeof gc);
424: gc.attr |= GRID_ATTR_CHARSET;
425:
426: screen_write_putc(ctx, &gc, 'l');
427: for (i = 1; i < nx - 1; i++)
428: screen_write_putc(ctx, &gc, 'q');
429: screen_write_putc(ctx, &gc, 'k');
430:
431: screen_write_cursormove(ctx, cx, cy + ny - 1);
432: screen_write_putc(ctx, &gc, 'm');
433: for (i = 1; i < nx - 1; i++)
434: screen_write_putc(ctx, &gc, 'q');
435: screen_write_putc(ctx, &gc, 'j');
436:
437: for (i = 1; i < ny - 1; i++) {
438: screen_write_cursormove(ctx, cx, cy + i);
439: screen_write_putc(ctx, &gc, 'x');
440: }
441: for (i = 1; i < ny - 1; i++) {
442: screen_write_cursormove(ctx, cx + nx - 1, cy + i);
443: screen_write_putc(ctx, &gc, 'x');
444: }
445:
446: screen_write_cursormove(ctx, cx, cy);
447: }
448:
449: /* Write a preview version of a window. */
450: void
451: screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
452: u_int ny)
453: {
454: struct screen *s = ctx->s;
455: struct grid_cell gc;
456: u_int cx, cy, px, py;
457:
458: cx = s->cx;
459: cy = s->cy;
460:
461: /*
462: * If the cursor is on, pick the area around the cursor, otherwise use
463: * the top left.
464: */
465: if (src->mode & MODE_CURSOR) {
466: px = src->cx;
467: if (px < nx / 3)
468: px = 0;
469: else
470: px = px - nx / 3;
471: if (px + nx > screen_size_x(src)) {
472: if (nx > screen_size_x(src))
473: px = 0;
474: else
475: px = screen_size_x(src) - nx;
476: }
477: py = src->cy;
478: if (py < ny / 3)
479: py = 0;
480: else
481: py = py - ny / 3;
482: if (py + ny > screen_size_y(src)) {
483: if (ny > screen_size_y(src))
484: py = 0;
485: else
486: py = screen_size_y(src) - ny;
487: }
488: } else {
489: px = 0;
490: py = 0;
491: }
492:
493: screen_write_copy(ctx, src, px, src->grid->hsize + py, nx, ny, NULL,
494: NULL);
495:
496: if (src->mode & MODE_CURSOR) {
497: grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
498: gc.attr |= GRID_ATTR_REVERSE;
499: screen_write_cursormove(ctx, cx + (src->cx - px),
500: cy + (src->cy - py));
501: screen_write_cell(ctx, &gc);
1.1 nicm 502: }
503: }
504:
1.17 nicm 505: /* Set up context for TTY command. */
1.87 nicm 506: static void
507: screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
1.1 nicm 508: {
1.87 nicm 509: struct screen *s = ctx->s;
1.123 nicm 510:
511: memset(ttyctx, 0, sizeof *ttyctx);
1.1 nicm 512:
1.17 nicm 513: ttyctx->wp = ctx->wp;
1.1 nicm 514:
1.17 nicm 515: ttyctx->ocx = s->cx;
516: ttyctx->ocy = s->cy;
517:
518: ttyctx->orlower = s->rlower;
519: ttyctx->orupper = s->rupper;
1.87 nicm 520: }
1.31 nicm 521:
1.61 nicm 522: /* Set a mode. */
523: void
524: screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
525: {
526: struct screen *s = ctx->s;
527:
528: s->mode |= mode;
529: }
530:
531: /* Clear a mode. */
532: void
533: screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
534: {
535: struct screen *s = ctx->s;
536:
537: s->mode &= ~mode;
538: }
539:
1.1 nicm 540: /* Cursor up by ny. */
541: void
542: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
543: {
544: struct screen *s = ctx->s;
545:
546: if (ny == 0)
547: ny = 1;
548:
1.12 nicm 549: if (s->cy < s->rupper) {
550: /* Above region. */
551: if (ny > s->cy)
552: ny = s->cy;
553: } else {
554: /* Below region. */
555: if (ny > s->cy - s->rupper)
556: ny = s->cy - s->rupper;
557: }
1.66 nicm 558: if (s->cx == screen_size_x(s))
559: s->cx--;
1.1 nicm 560: if (ny == 0)
561: return;
562:
563: s->cy -= ny;
564: }
565:
566: /* Cursor down by ny. */
567: void
568: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
569: {
570: struct screen *s = ctx->s;
571:
572: if (ny == 0)
573: ny = 1;
574:
1.12 nicm 575: if (s->cy > s->rlower) {
576: /* Below region. */
577: if (ny > screen_size_y(s) - 1 - s->cy)
578: ny = screen_size_y(s) - 1 - s->cy;
579: } else {
580: /* Above region. */
581: if (ny > s->rlower - s->cy)
582: ny = s->rlower - s->cy;
583: }
1.66 nicm 584: if (s->cx == screen_size_x(s))
585: s->cx--;
1.1 nicm 586: if (ny == 0)
587: return;
588:
589: s->cy += ny;
590: }
591:
1.101 nicm 592: /* Cursor right by nx. */
1.1 nicm 593: void
594: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
595: {
596: struct screen *s = ctx->s;
597:
598: if (nx == 0)
599: nx = 1;
600:
601: if (nx > screen_size_x(s) - 1 - s->cx)
602: nx = screen_size_x(s) - 1 - s->cx;
603: if (nx == 0)
604: return;
605:
606: s->cx += nx;
607: }
608:
609: /* Cursor left by nx. */
610: void
611: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
612: {
613: struct screen *s = ctx->s;
614:
615: if (nx == 0)
616: nx = 1;
617:
618: if (nx > s->cx)
619: nx = s->cx;
620: if (nx == 0)
621: return;
622:
623: s->cx -= nx;
1.5 nicm 624: }
625:
1.29 nicm 626: /* Backspace; cursor left unless at start of wrapped line when can move up. */
627: void
628: screen_write_backspace(struct screen_write_ctx *ctx)
629: {
630: struct screen *s = ctx->s;
631: struct grid_line *gl;
632:
633: if (s->cx == 0) {
634: if (s->cy == 0)
635: return;
636: gl = &s->grid->linedata[s->grid->hsize + s->cy - 1];
637: if (gl->flags & GRID_LINE_WRAPPED) {
638: s->cy--;
639: s->cx = screen_size_x(s) - 1;
640: }
641: } else
642: s->cx--;
643: }
644:
1.5 nicm 645: /* VT100 alignment test. */
646: void
647: screen_write_alignmenttest(struct screen_write_ctx *ctx)
648: {
649: struct screen *s = ctx->s;
1.17 nicm 650: struct tty_ctx ttyctx;
1.5 nicm 651: struct grid_cell gc;
652: u_int xx, yy;
653:
1.87 nicm 654: screen_write_initctx(ctx, &ttyctx);
1.17 nicm 655:
1.5 nicm 656: memcpy(&gc, &grid_default_cell, sizeof gc);
1.77 nicm 657: utf8_set(&gc.data, 'E');
1.7 ray 658:
1.5 nicm 659: for (yy = 0; yy < screen_size_y(s); yy++) {
660: for (xx = 0; xx < screen_size_x(s); xx++)
661: grid_view_set_cell(s->grid, xx, yy, &gc);
662: }
1.7 ray 663:
1.5 nicm 664: s->cx = 0;
665: s->cy = 0;
666:
667: s->rupper = 0;
668: s->rlower = screen_size_y(s) - 1;
669:
1.109 nicm 670: screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1.17 nicm 671: tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1 nicm 672: }
673:
674: /* Insert nx characters. */
675: void
1.99 nicm 676: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1 nicm 677: {
678: struct screen *s = ctx->s;
1.17 nicm 679: struct tty_ctx ttyctx;
1.1 nicm 680:
681: if (nx == 0)
682: nx = 1;
683:
1.9 nicm 684: if (nx > screen_size_x(s) - s->cx)
685: nx = screen_size_x(s) - s->cx;
1.1 nicm 686: if (nx == 0)
687: return;
688:
1.107 nicm 689: if (s->cx > screen_size_x(s) - 1)
690: return;
691:
1.87 nicm 692: screen_write_initctx(ctx, &ttyctx);
1.107 nicm 693: ttyctx.bg = bg;
1.1 nicm 694:
1.107 nicm 695: grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1.1 nicm 696:
1.111 nicm 697: screen_write_collect_flush(ctx, 0);
1.17 nicm 698: ttyctx.num = nx;
699: tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1 nicm 700: }
701:
702: /* Delete nx characters. */
703: void
1.99 nicm 704: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1 nicm 705: {
706: struct screen *s = ctx->s;
1.17 nicm 707: struct tty_ctx ttyctx;
1.1 nicm 708:
709: if (nx == 0)
710: nx = 1;
711:
1.9 nicm 712: if (nx > screen_size_x(s) - s->cx)
713: nx = screen_size_x(s) - s->cx;
1.1 nicm 714: if (nx == 0)
715: return;
716:
1.107 nicm 717: if (s->cx > screen_size_x(s) - 1)
718: return;
719:
1.87 nicm 720: screen_write_initctx(ctx, &ttyctx);
1.107 nicm 721: ttyctx.bg = bg;
1.1 nicm 722:
1.107 nicm 723: grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1.1 nicm 724:
1.111 nicm 725: screen_write_collect_flush(ctx, 0);
1.17 nicm 726: ttyctx.num = nx;
727: tty_write(tty_cmd_deletecharacter, &ttyctx);
1.59 nicm 728: }
729:
730: /* Clear nx characters. */
731: void
1.121 nicm 732: screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.59 nicm 733: {
734: struct screen *s = ctx->s;
735: struct tty_ctx ttyctx;
736:
737: if (nx == 0)
738: nx = 1;
739:
740: if (nx > screen_size_x(s) - s->cx)
741: nx = screen_size_x(s) - s->cx;
742: if (nx == 0)
743: return;
744:
1.107 nicm 745: if (s->cx > screen_size_x(s) - 1)
746: return;
747:
1.87 nicm 748: screen_write_initctx(ctx, &ttyctx);
1.121 nicm 749: ttyctx.bg = bg;
1.59 nicm 750:
1.124 nicm 751: grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
1.59 nicm 752:
1.111 nicm 753: screen_write_collect_flush(ctx, 0);
1.59 nicm 754: ttyctx.num = nx;
755: tty_write(tty_cmd_clearcharacter, &ttyctx);
1.1 nicm 756: }
757:
758: /* Insert ny lines. */
759: void
1.99 nicm 760: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1 nicm 761: {
762: struct screen *s = ctx->s;
1.107 nicm 763: struct grid *gd = s->grid;
1.17 nicm 764: struct tty_ctx ttyctx;
1.1 nicm 765:
766: if (ny == 0)
767: ny = 1;
768:
1.11 nicm 769: if (s->cy < s->rupper || s->cy > s->rlower) {
770: if (ny > screen_size_y(s) - s->cy)
771: ny = screen_size_y(s) - s->cy;
772: if (ny == 0)
773: return;
774:
1.87 nicm 775: screen_write_initctx(ctx, &ttyctx);
1.107 nicm 776: ttyctx.bg = bg;
1.11 nicm 777:
1.107 nicm 778: grid_view_insert_lines(gd, s->cy, ny, bg);
1.11 nicm 779:
1.111 nicm 780: screen_write_collect_flush(ctx, 0);
1.17 nicm 781: ttyctx.num = ny;
782: tty_write(tty_cmd_insertline, &ttyctx);
1.11 nicm 783: return;
784: }
785:
786: if (ny > s->rlower + 1 - s->cy)
787: ny = s->rlower + 1 - s->cy;
1.1 nicm 788: if (ny == 0)
789: return;
1.41 nicm 790:
1.87 nicm 791: screen_write_initctx(ctx, &ttyctx);
1.107 nicm 792: ttyctx.bg = bg;
1.1 nicm 793:
794: if (s->cy < s->rupper || s->cy > s->rlower)
1.107 nicm 795: grid_view_insert_lines(gd, s->cy, ny, bg);
796: else
797: grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1 nicm 798:
1.111 nicm 799: screen_write_collect_flush(ctx, 0);
1.17 nicm 800: ttyctx.num = ny;
801: tty_write(tty_cmd_insertline, &ttyctx);
1.1 nicm 802: }
803:
804: /* Delete ny lines. */
805: void
1.99 nicm 806: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1 nicm 807: {
808: struct screen *s = ctx->s;
1.107 nicm 809: struct grid *gd = s->grid;
1.17 nicm 810: struct tty_ctx ttyctx;
1.1 nicm 811:
812: if (ny == 0)
813: ny = 1;
814:
1.11 nicm 815: if (s->cy < s->rupper || s->cy > s->rlower) {
816: if (ny > screen_size_y(s) - s->cy)
817: ny = screen_size_y(s) - s->cy;
818: if (ny == 0)
819: return;
820:
1.87 nicm 821: screen_write_initctx(ctx, &ttyctx);
1.107 nicm 822: ttyctx.bg = bg;
1.11 nicm 823:
1.107 nicm 824: grid_view_delete_lines(gd, s->cy, ny, bg);
1.11 nicm 825:
1.111 nicm 826: screen_write_collect_flush(ctx, 0);
1.17 nicm 827: ttyctx.num = ny;
828: tty_write(tty_cmd_deleteline, &ttyctx);
1.11 nicm 829: return;
830: }
1.41 nicm 831:
1.11 nicm 832: if (ny > s->rlower + 1 - s->cy)
833: ny = s->rlower + 1 - s->cy;
1.1 nicm 834: if (ny == 0)
835: return;
836:
1.87 nicm 837: screen_write_initctx(ctx, &ttyctx);
1.107 nicm 838: ttyctx.bg = bg;
1.1 nicm 839:
840: if (s->cy < s->rupper || s->cy > s->rlower)
1.107 nicm 841: grid_view_delete_lines(gd, s->cy, ny, bg);
842: else
843: grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1 nicm 844:
1.111 nicm 845: screen_write_collect_flush(ctx, 0);
1.17 nicm 846: ttyctx.num = ny;
847: tty_write(tty_cmd_deleteline, &ttyctx);
1.1 nicm 848: }
849:
850: /* Clear line at cursor. */
851: void
1.99 nicm 852: screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 853: {
1.92 nicm 854: struct screen *s = ctx->s;
855: struct grid_line *gl;
856: struct tty_ctx ttyctx;
857: u_int sx = screen_size_x(s);
1.1 nicm 858:
1.92 nicm 859: gl = &s->grid->linedata[s->grid->hsize + s->cy];
1.99 nicm 860: if (gl->cellsize == 0 && bg == 8)
1.92 nicm 861: return;
1.1 nicm 862:
1.108 nicm 863: screen_write_initctx(ctx, &ttyctx);
864: ttyctx.bg = bg;
865:
1.99 nicm 866: grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
867:
1.109 nicm 868: screen_write_collect_clear(ctx, s->cy, 1);
1.114 nicm 869: screen_write_collect_flush(ctx, 0);
1.17 nicm 870: tty_write(tty_cmd_clearline, &ttyctx);
1.1 nicm 871: }
872:
873: /* Clear to end of line from cursor. */
874: void
1.99 nicm 875: screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 876: {
1.92 nicm 877: struct screen *s = ctx->s;
878: struct grid_line *gl;
879: struct tty_ctx ttyctx;
880: u_int sx = screen_size_x(s);
1.1 nicm 881:
1.92 nicm 882: gl = &s->grid->linedata[s->grid->hsize + s->cy];
1.99 nicm 883: if (s->cx > sx - 1 || (s->cx >= gl->cellsize && bg == 8))
1.92 nicm 884: return;
1.108 nicm 885:
886: screen_write_initctx(ctx, &ttyctx);
887: ttyctx.bg = bg;
1.1 nicm 888:
1.99 nicm 889: grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
890:
1.109 nicm 891: if (s->cx == 0)
892: screen_write_collect_clear(ctx, s->cy, 1);
1.114 nicm 893: screen_write_collect_flush(ctx, 0);
1.41 nicm 894: tty_write(tty_cmd_clearendofline, &ttyctx);
1.1 nicm 895: }
896:
897: /* Clear to start of line from cursor. */
898: void
1.99 nicm 899: screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 900: {
901: struct screen *s = ctx->s;
1.17 nicm 902: struct tty_ctx ttyctx;
1.92 nicm 903: u_int sx = screen_size_x(s);
1.1 nicm 904:
1.87 nicm 905: screen_write_initctx(ctx, &ttyctx);
1.99 nicm 906: ttyctx.bg = bg;
1.1 nicm 907:
1.109 nicm 908: if (s->cx > sx - 1)
1.99 nicm 909: grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109 nicm 910: else
1.99 nicm 911: grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1 nicm 912:
1.109 nicm 913: if (s->cx > sx - 1)
914: screen_write_collect_clear(ctx, s->cy, 1);
1.114 nicm 915: screen_write_collect_flush(ctx, 0);
1.17 nicm 916: tty_write(tty_cmd_clearstartofline, &ttyctx);
1.1 nicm 917: }
918:
1.101 nicm 919: /* Move cursor to px,py. */
1.1 nicm 920: void
921: screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
922: {
923: struct screen *s = ctx->s;
924:
925: if (px > screen_size_x(s) - 1)
926: px = screen_size_x(s) - 1;
927: if (py > screen_size_y(s) - 1)
928: py = screen_size_y(s) - 1;
929:
930: s->cx = px;
931: s->cy = py;
932: }
933:
1.101 nicm 934: /* Reverse index (up with scroll). */
1.1 nicm 935: void
1.122 nicm 936: screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 937: {
938: struct screen *s = ctx->s;
1.17 nicm 939: struct tty_ctx ttyctx;
1.1 nicm 940:
1.87 nicm 941: screen_write_initctx(ctx, &ttyctx);
1.122 nicm 942: ttyctx.bg = bg;
1.1 nicm 943:
1.109 nicm 944: if (s->cy == s->rupper)
1.122 nicm 945: grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1.109 nicm 946: else if (s->cy > 0)
1.1 nicm 947: s->cy--;
948:
1.111 nicm 949: screen_write_collect_flush(ctx, 0);
1.17 nicm 950: tty_write(tty_cmd_reverseindex, &ttyctx);
1.1 nicm 951: }
952:
953: /* Set scroll region. */
954: void
1.83 nicm 955: screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
956: u_int rlower)
1.1 nicm 957: {
958: struct screen *s = ctx->s;
959:
960: if (rupper > screen_size_y(s) - 1)
961: rupper = screen_size_y(s) - 1;
962: if (rlower > screen_size_y(s) - 1)
963: rlower = screen_size_y(s) - 1;
1.13 nicm 964: if (rupper >= rlower) /* cannot be one line */
1.1 nicm 965: return;
966:
1.111 nicm 967: screen_write_collect_flush(ctx, 0);
1.110 nicm 968:
1.1 nicm 969: /* Cursor moves to top-left. */
970: s->cx = 0;
971: s->cy = 0;
972:
973: s->rupper = rupper;
974: s->rlower = rlower;
975: }
976:
1.34 nicm 977: /* Line feed. */
1.1 nicm 978: void
1.122 nicm 979: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1.1 nicm 980: {
1.20 nicm 981: struct screen *s = ctx->s;
1.109 nicm 982: struct grid *gd = s->grid;
1.20 nicm 983: struct grid_line *gl;
1.1 nicm 984:
1.109 nicm 985: gl = &gd->linedata[gd->hsize + s->cy];
1.20 nicm 986: if (wrapped)
987: gl->flags |= GRID_LINE_WRAPPED;
1.73 nicm 988: else
989: gl->flags &= ~GRID_LINE_WRAPPED;
1.20 nicm 990:
1.114 nicm 991: log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
992: s->rupper, s->rlower);
993:
1.122 nicm 994: if (bg != ctx->bg) {
995: screen_write_collect_flush(ctx, 1);
996: ctx->bg = bg;
997: }
998:
1.92 nicm 999: if (s->cy == s->rlower) {
1.122 nicm 1000: grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1.109 nicm 1001: screen_write_collect_scroll(ctx);
1.110 nicm 1002: ctx->scrolled++;
1.109 nicm 1003: } else if (s->cy < screen_size_y(s) - 1)
1.1 nicm 1004: s->cy++;
1005: }
1006:
1.110 nicm 1007: /* Scroll up. */
1008: void
1.122 nicm 1009: screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1.110 nicm 1010: {
1011: struct screen *s = ctx->s;
1012: struct grid *gd = s->grid;
1013: u_int i;
1014:
1015: if (lines == 0)
1016: lines = 1;
1017: else if (lines > s->rlower - s->rupper + 1)
1018: lines = s->rlower - s->rupper + 1;
1019:
1.122 nicm 1020: if (bg != ctx->bg) {
1021: screen_write_collect_flush(ctx, 1);
1022: ctx->bg = bg;
1023: }
1024:
1.110 nicm 1025: for (i = 0; i < lines; i++) {
1.122 nicm 1026: grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1.110 nicm 1027: screen_write_collect_scroll(ctx);
1028: }
1029: ctx->scrolled += lines;
1030: }
1031:
1.1 nicm 1032: /* Carriage return (cursor to start of line). */
1033: void
1034: screen_write_carriagereturn(struct screen_write_ctx *ctx)
1035: {
1036: struct screen *s = ctx->s;
1037:
1038: s->cx = 0;
1039: }
1040:
1041: /* Clear to end of screen from cursor. */
1042: void
1.99 nicm 1043: screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1044: {
1045: struct screen *s = ctx->s;
1.107 nicm 1046: struct grid *gd = s->grid;
1.17 nicm 1047: struct tty_ctx ttyctx;
1.92 nicm 1048: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 1049:
1.87 nicm 1050: screen_write_initctx(ctx, &ttyctx);
1.99 nicm 1051: ttyctx.bg = bg;
1.1 nicm 1052:
1.46 nicm 1053: /* Scroll into history if it is enabled and clearing entire screen. */
1.109 nicm 1054: if (s->cx == 0 && s->cy == 0 && (gd->flags & GRID_HISTORY))
1.107 nicm 1055: grid_view_clear_history(gd, bg);
1.109 nicm 1056: else {
1057: if (s->cx <= sx - 1)
1.107 nicm 1058: grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
1059: grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1.46 nicm 1060: }
1.1 nicm 1061:
1.109 nicm 1062: screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1.111 nicm 1063: screen_write_collect_flush(ctx, 0);
1.17 nicm 1064: tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1 nicm 1065: }
1066:
1067: /* Clear to start of screen. */
1068: void
1.105 nicm 1069: screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1070: {
1071: struct screen *s = ctx->s;
1.17 nicm 1072: struct tty_ctx ttyctx;
1.92 nicm 1073: u_int sx = screen_size_x(s);
1.1 nicm 1074:
1.87 nicm 1075: screen_write_initctx(ctx, &ttyctx);
1.105 nicm 1076: ttyctx.bg = bg;
1.1 nicm 1077:
1.109 nicm 1078: if (s->cy > 0)
1.105 nicm 1079: grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1.109 nicm 1080: if (s->cx > sx - 1)
1.120 nicm 1081: grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109 nicm 1082: else
1.120 nicm 1083: grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1 nicm 1084:
1.109 nicm 1085: screen_write_collect_clear(ctx, 0, s->cy);
1.111 nicm 1086: screen_write_collect_flush(ctx, 0);
1.17 nicm 1087: tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1 nicm 1088: }
1089:
1090: /* Clear entire screen. */
1091: void
1.99 nicm 1092: screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1093: {
1094: struct screen *s = ctx->s;
1.17 nicm 1095: struct tty_ctx ttyctx;
1.92 nicm 1096: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 1097:
1.87 nicm 1098: screen_write_initctx(ctx, &ttyctx);
1.99 nicm 1099: ttyctx.bg = bg;
1.1 nicm 1100:
1.46 nicm 1101: /* Scroll into history if it is enabled. */
1102: if (s->grid->flags & GRID_HISTORY)
1.99 nicm 1103: grid_view_clear_history(s->grid, bg);
1.83 nicm 1104: else
1.99 nicm 1105: grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1.1 nicm 1106:
1.109 nicm 1107: screen_write_collect_clear(ctx, 0, sy);
1.17 nicm 1108: tty_write(tty_cmd_clearscreen, &ttyctx);
1.51 nicm 1109: }
1110:
1111: /* Clear entire history. */
1112: void
1113: screen_write_clearhistory(struct screen_write_ctx *ctx)
1114: {
1115: struct screen *s = ctx->s;
1116: struct grid *gd = s->grid;
1117:
1.99 nicm 1118: grid_move_lines(gd, 0, gd->hsize, gd->sy, 8);
1.93 nicm 1119: gd->hscrolled = gd->hsize = 0;
1.1 nicm 1120: }
1121:
1.109 nicm 1122: /* Clear a collected line. */
1123: static void
1124: screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
1125: {
1126: struct screen_write_collect_item *ci, *tmp;
1127: u_int i;
1128: size_t size;
1129:
1130: for (i = y ; i < y + n; i++) {
1131: if (TAILQ_EMPTY(&ctx->list[i].items))
1132: continue;
1133: size = 0;
1134: TAILQ_FOREACH_SAFE(ci, &ctx->list[i].items, entry, tmp) {
1135: size += ci->used;
1136: TAILQ_REMOVE(&ctx->list[i].items, ci, entry);
1137: free(ci);
1138: }
1139: ctx->skipped += size;
1.114 nicm 1140: log_debug("%s: dropped %zu bytes (line %u)", __func__, size, i);
1.109 nicm 1141: }
1142: }
1143:
1144: /* Scroll collected lines up. */
1145: static void
1146: screen_write_collect_scroll(struct screen_write_ctx *ctx)
1147: {
1148: struct screen *s = ctx->s;
1149: struct screen_write_collect_line *cl;
1150: u_int y;
1151:
1.114 nicm 1152: log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1153: s->rupper, s->rlower);
1154:
1.109 nicm 1155: screen_write_collect_clear(ctx, s->rupper, 1);
1156: for (y = s->rupper; y < s->rlower; y++) {
1157: cl = &ctx->list[y + 1];
1158: TAILQ_CONCAT(&ctx->list[y].items, &cl->items, entry);
1159: TAILQ_INIT(&cl->items);
1160: }
1161: }
1162:
1163: /* Flush collected lines. */
1164: static void
1.111 nicm 1165: screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only)
1.109 nicm 1166: {
1167: struct screen *s = ctx->s;
1168: struct screen_write_collect_item *ci, *tmp;
1.116 nicm 1169: u_int y, cx, cy, items = 0;
1.109 nicm 1170: struct tty_ctx ttyctx;
1.116 nicm 1171: size_t written = 0;
1.110 nicm 1172:
1173: if (ctx->scrolled != 0) {
1174: log_debug("%s: scrolled %u (region %u-%u)", __func__,
1175: ctx->scrolled, s->rupper, s->rlower);
1176: if (ctx->scrolled > s->rlower - s->rupper + 1)
1177: ctx->scrolled = s->rlower - s->rupper + 1;
1178:
1179: screen_write_initctx(ctx, &ttyctx);
1180: ttyctx.num = ctx->scrolled;
1.122 nicm 1181: ttyctx.bg = ctx->bg;
1.110 nicm 1182: tty_write(tty_cmd_scrollup, &ttyctx);
1183: }
1184: ctx->scrolled = 0;
1.122 nicm 1185: ctx->bg = 8;
1186:
1.111 nicm 1187: if (scroll_only)
1188: return;
1.109 nicm 1189:
1190: cx = s->cx; cy = s->cy;
1191: for (y = 0; y < screen_size_y(s); y++) {
1192: TAILQ_FOREACH_SAFE(ci, &ctx->list[y].items, entry, tmp) {
1193: screen_write_cursormove(ctx, ci->x, y);
1194: screen_write_initctx(ctx, &ttyctx);
1195: ttyctx.cell = &ci->gc;
1.118 nicm 1196: ttyctx.wrapped = ci->wrapped;
1.109 nicm 1197: ttyctx.ptr = ci->data;
1198: ttyctx.num = ci->used;
1199: tty_write(tty_cmd_cells, &ttyctx);
1.116 nicm 1200:
1201: items++;
1202: written += ci->used;
1.109 nicm 1203:
1204: TAILQ_REMOVE(&ctx->list[y].items, ci, entry);
1205: free(ci);
1206: }
1207: }
1208: s->cx = cx; s->cy = cy;
1.116 nicm 1209:
1210: log_debug("%s: flushed %u items (%zu bytes)", __func__, items, written);
1211: ctx->written += written;
1.109 nicm 1212: }
1213:
1214: /* Finish and store collected cells. */
1215: void
1216: screen_write_collect_end(struct screen_write_ctx *ctx)
1217: {
1218: struct screen *s = ctx->s;
1219: struct screen_write_collect_item *ci = ctx->item;
1220: struct grid_cell gc;
1221:
1222: if (ci->used == 0)
1223: return;
1224: ci->data[ci->used] = '\0';
1225:
1226: ci->x = s->cx;
1227: TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
1228: ctx->item = xcalloc(1, sizeof *ctx->item);
1229:
1230: log_debug("%s: %u %s (at %u,%u)", __func__, ci->used, ci->data, s->cx,
1231: s->cy);
1232:
1233: memcpy(&gc, &ci->gc, sizeof gc);
1234: grid_view_set_cells(s->grid, s->cx, s->cy, &gc, ci->data, ci->used);
1235: s->cx += ci->used;
1236: }
1237:
1238: /* Write cell data, collecting if necessary. */
1239: void
1240: screen_write_collect_add(struct screen_write_ctx *ctx,
1241: const struct grid_cell *gc)
1242: {
1243: struct screen *s = ctx->s;
1244: struct screen_write_collect_item *ci;
1245: u_int sx = screen_size_x(s);
1246: int collect;
1247:
1248: /*
1249: * Don't need to check that the attributes and whatnot are still the
1.116 nicm 1250: * same - input_parse will end the collection when anything that isn't
1251: * a plain character is encountered. Also nothing should make it here
1252: * that isn't a single ASCII character.
1.109 nicm 1253: */
1254:
1255: collect = 1;
1.117 nicm 1256: if (gc->data.width != 1 || gc->data.size != 1)
1.109 nicm 1257: collect = 0;
1258: else if (gc->attr & GRID_ATTR_CHARSET)
1259: collect = 0;
1260: else if (~s->mode & MODE_WRAP)
1261: collect = 0;
1262: else if (s->mode & MODE_INSERT)
1263: collect = 0;
1264: else if (s->sel.flag)
1265: collect = 0;
1266: if (!collect) {
1267: screen_write_collect_end(ctx);
1.116 nicm 1268: screen_write_collect_flush(ctx, 0);
1.109 nicm 1269: screen_write_cell(ctx, gc);
1270: return;
1271: }
1272: ctx->cells++;
1273:
1274: if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
1275: screen_write_collect_end(ctx);
1.118 nicm 1276: ci = ctx->item; /* may have changed */
1277:
1.109 nicm 1278: if (s->cx > sx - 1) {
1.114 nicm 1279: log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.118 nicm 1280: ci->wrapped = 1;
1.122 nicm 1281: screen_write_linefeed(ctx, 1, 8);
1.109 nicm 1282: s->cx = 0;
1283: }
1284:
1285: if (ci->used == 0)
1286: memcpy(&ci->gc, gc, sizeof ci->gc);
1287: ci->data[ci->used++] = gc->data.data[0];
1288: if (ci->used == (sizeof ci->data) - 1)
1289: screen_write_collect_end(ctx);
1290: }
1291:
1.1 nicm 1292: /* Write cell data. */
1293: void
1.60 nicm 1294: screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1.1 nicm 1295: {
1296: struct screen *s = ctx->s;
1297: struct grid *gd = s->grid;
1.109 nicm 1298: struct grid_line *gl;
1299: struct grid_cell_entry *gce;
1300: struct grid_cell tmp_gc, now_gc;
1.15 nicm 1301: struct tty_ctx ttyctx;
1.92 nicm 1302: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.115 nicm 1303: u_int width = gc->data.width, xx, last, cx, cy;
1.109 nicm 1304: int selected, skip = 1;
1.1 nicm 1305:
1.109 nicm 1306: /* Ignore padding cells. */
1.1 nicm 1307: if (gc->flags & GRID_FLAG_PADDING)
1308: return;
1.109 nicm 1309: ctx->cells++;
1.32 nicm 1310:
1.109 nicm 1311: /* If the width is zero, combine onto the previous character. */
1.1 nicm 1312: if (width == 0) {
1.112 nicm 1313: screen_write_collect_flush(ctx, 0);
1.104 nicm 1314: if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) {
1.115 nicm 1315: cx = s->cx; cy = s->cy;
1.104 nicm 1316: screen_write_cursormove(ctx, xx, s->cy);
1.87 nicm 1317: screen_write_initctx(ctx, &ttyctx);
1.104 nicm 1318: ttyctx.cell = gc;
1319: tty_write(tty_cmd_cell, &ttyctx);
1.115 nicm 1320: s->cx = cx; s->cy = cy;
1.1 nicm 1321: }
1322: return;
1323: }
1.112 nicm 1324:
1325: /* Flush any existing scrolling. */
1326: screen_write_collect_flush(ctx, 1);
1.1 nicm 1327:
1.109 nicm 1328: /* If this character doesn't fit, ignore it. */
1329: if ((~s->mode & MODE_WRAP) &&
1330: width > 1 &&
1331: (width > sx || (s->cx != sx && s->cx > sx - width)))
1332: return;
1333:
1.6 nicm 1334: /* If in insert mode, make space for the cells. */
1.98 nicm 1335: if (s->mode & MODE_INSERT) {
1.109 nicm 1336: grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
1337: skip = 0;
1338: }
1.6 nicm 1339:
1.20 nicm 1340: /* Check this will fit on the current line and wrap if not. */
1.92 nicm 1341: if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1.128 ! nicm 1342: log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.122 nicm 1343: screen_write_linefeed(ctx, 1, 8);
1.109 nicm 1344: s->cx = 0;
1.128 ! nicm 1345: screen_write_collect_flush(ctx, 1);
1.1 nicm 1346: }
1347:
1.64 nicm 1348: /* Sanity check cursor position. */
1.92 nicm 1349: if (s->cx > sx - width || s->cy > sy - 1)
1.1 nicm 1350: return;
1.106 nicm 1351: screen_write_initctx(ctx, &ttyctx);
1352:
1.1 nicm 1353: /* Handle overwriting of UTF-8 characters. */
1.92 nicm 1354: gl = &s->grid->linedata[s->grid->hsize + s->cy];
1355: if (gl->flags & GRID_LINE_EXTENDED) {
1356: grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
1357: if (screen_write_overwrite(ctx, &now_gc, width))
1358: skip = 0;
1359: }
1.1 nicm 1360:
1361: /*
1362: * If the new character is UTF-8 wide, fill in padding cells. Have
1363: * already ensured there is enough room.
1364: */
1.89 nicm 1365: for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.88 nicm 1366: grid_view_set_cell(gd, xx, s->cy, &screen_write_pad_cell);
1.89 nicm 1367: skip = 0;
1368: }
1369:
1370: /* If no change, do not draw. */
1.92 nicm 1371: if (skip) {
1372: if (s->cx >= gl->cellsize)
1373: skip = grid_cells_equal(gc, &grid_default_cell);
1374: else {
1375: gce = &gl->celldata[s->cx];
1376: if (gce->flags & GRID_FLAG_EXTENDED)
1377: skip = 0;
1.95 nicm 1378: else if (gc->flags != gce->flags)
1.92 nicm 1379: skip = 0;
1380: else if (gc->attr != gce->data.attr)
1381: skip = 0;
1382: else if (gc->fg != gce->data.fg)
1383: skip = 0;
1384: else if (gc->bg != gce->data.bg)
1385: skip = 0;
1.95 nicm 1386: else if (gc->data.width != 1)
1387: skip = 0;
1.109 nicm 1388: else if (gc->data.size != 1)
1389: skip = 0;
1.95 nicm 1390: else if (gce->data.data != gc->data.data[0])
1.92 nicm 1391: skip = 0;
1392: }
1393: }
1.1 nicm 1394:
1.127 nicm 1395: /* Update the selected flag and set the cell. */
1.90 nicm 1396: selected = screen_check_selection(s, s->cx, s->cy);
1.109 nicm 1397: if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1.90 nicm 1398: memcpy(&tmp_gc, gc, sizeof tmp_gc);
1399: tmp_gc.flags |= GRID_FLAG_SELECTED;
1400: grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1.109 nicm 1401: } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1.90 nicm 1402: memcpy(&tmp_gc, gc, sizeof tmp_gc);
1403: tmp_gc.flags &= ~GRID_FLAG_SELECTED;
1404: grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1405: } else if (!skip)
1.89 nicm 1406: grid_view_set_cell(gd, s->cx, s->cy, gc);
1.109 nicm 1407: if (selected)
1408: skip = 0;
1.1 nicm 1409:
1.64 nicm 1410: /*
1411: * Move the cursor. If not wrapping, stick at the last character and
1412: * replace it.
1413: */
1.65 nicm 1414: last = !(s->mode & MODE_WRAP);
1.92 nicm 1415: if (s->cx <= sx - last - width)
1.64 nicm 1416: s->cx += width;
1417: else
1.92 nicm 1418: s->cx = sx - last;
1.1 nicm 1419:
1.89 nicm 1420: /* Create space for character in insert mode. */
1.109 nicm 1421: if (s->mode & MODE_INSERT) {
1.113 nicm 1422: screen_write_collect_flush(ctx, 0);
1.17 nicm 1423: ttyctx.num = width;
1424: tty_write(tty_cmd_insertcharacter, &ttyctx);
1425: }
1.89 nicm 1426:
1427: /* Write to the screen. */
1.109 nicm 1428: if (!skip) {
1429: if (selected) {
1430: screen_select_cell(s, &tmp_gc, gc);
1431: ttyctx.cell = &tmp_gc;
1432: } else
1433: ttyctx.cell = gc;
1.16 nicm 1434: tty_write(tty_cmd_cell, &ttyctx);
1.92 nicm 1435: ctx->written++;
1436: } else
1437: ctx->skipped++;
1.35 nicm 1438: }
1439:
1440: /* Combine a UTF-8 zero-width character onto the previous. */
1.104 nicm 1441: static const struct grid_cell *
1442: screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
1443: u_int *xx)
1.35 nicm 1444: {
1445: struct screen *s = ctx->s;
1446: struct grid *gd = s->grid;
1.104 nicm 1447: static struct grid_cell gc;
1448: u_int n;
1.35 nicm 1449:
1450: /* Can't combine if at 0. */
1451: if (s->cx == 0)
1.104 nicm 1452: return (NULL);
1.35 nicm 1453:
1.60 nicm 1454: /* Empty data is out. */
1455: if (ud->size == 0)
1.37 nicm 1456: fatalx("UTF-8 data empty");
1457:
1.60 nicm 1458: /* Retrieve the previous cell. */
1.119 nicm 1459: for (n = 1; n <= s->cx; n++) {
1.104 nicm 1460: grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
1461: if (~gc.flags & GRID_FLAG_PADDING)
1462: break;
1463: }
1.119 nicm 1464: if (n > s->cx)
1.104 nicm 1465: return (NULL);
1466: *xx = s->cx - n;
1.35 nicm 1467:
1.60 nicm 1468: /* Check there is enough space. */
1.77 nicm 1469: if (gc.data.size + ud->size > sizeof gc.data.data)
1.104 nicm 1470: return (NULL);
1471:
1472: log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size,
1473: ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy);
1.35 nicm 1474:
1.77 nicm 1475: /* Append the data. */
1476: memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
1477: gc.data.size += ud->size;
1478:
1479: /* Set the new cell. */
1.104 nicm 1480: grid_view_set_cell(gd, *xx, s->cy, &gc);
1.35 nicm 1481:
1.104 nicm 1482: return (&gc);
1.1 nicm 1483: }
1484:
1485: /*
1486: * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1487: * cell on the screen, so following cells must not be drawn by marking them as
1488: * padding.
1489: *
1490: * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1491: * character, it is necessary to also overwrite any other cells which covered
1492: * by the same character.
1493: */
1.89 nicm 1494: static int
1495: screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
1496: u_int width)
1.1 nicm 1497: {
1498: struct screen *s = ctx->s;
1499: struct grid *gd = s->grid;
1.89 nicm 1500: struct grid_cell tmp_gc;
1.1 nicm 1501: u_int xx;
1.89 nicm 1502: int done = 0;
1.1 nicm 1503:
1.89 nicm 1504: if (gc->flags & GRID_FLAG_PADDING) {
1.1 nicm 1505: /*
1506: * A padding cell, so clear any following and leading padding
1507: * cells back to the character. Don't overwrite the current
1508: * cell as that happens later anyway.
1509: */
1510: xx = s->cx + 1;
1511: while (--xx > 0) {
1.89 nicm 1512: grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1513: if (~tmp_gc.flags & GRID_FLAG_PADDING)
1.1 nicm 1514: break;
1515: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1516: }
1517:
1518: /* Overwrite the character at the start of this padding. */
1519: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1.89 nicm 1520: done = 1;
1.43 nicm 1521: }
1.1 nicm 1522:
1.43 nicm 1523: /*
1.95 nicm 1524: * Overwrite any padding cells that belong to any UTF-8 characters
1525: * we'll be overwriting with the current character.
1.43 nicm 1526: */
1.95 nicm 1527: if (width != 1 ||
1528: gc->data.width != 1 ||
1529: gc->flags & GRID_FLAG_PADDING) {
1.89 nicm 1530: xx = s->cx + width - 1;
1531: while (++xx < screen_size_x(s)) {
1532: grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1533: if (~tmp_gc.flags & GRID_FLAG_PADDING)
1534: break;
1535: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1536: done = 1;
1537: }
1.1 nicm 1538: }
1.89 nicm 1539:
1540: return (done);
1.50 nicm 1541: }
1542:
1.107 nicm 1543: /* Set external clipboard. */
1.50 nicm 1544: void
1545: screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
1546: {
1547: struct tty_ctx ttyctx;
1548:
1.87 nicm 1549: screen_write_initctx(ctx, &ttyctx);
1.50 nicm 1550: ttyctx.ptr = str;
1551: ttyctx.num = len;
1552:
1553: tty_write(tty_cmd_setselection, &ttyctx);
1.47 nicm 1554: }
1555:
1.107 nicm 1556: /* Write unmodified string. */
1.47 nicm 1557: void
1558: screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
1559: {
1.87 nicm 1560: struct tty_ctx ttyctx;
1.47 nicm 1561:
1.87 nicm 1562: screen_write_initctx(ctx, &ttyctx);
1.47 nicm 1563: ttyctx.ptr = str;
1564: ttyctx.num = len;
1565:
1566: tty_write(tty_cmd_rawstring, &ttyctx);
1.1 nicm 1567: }