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