Annotation of src/usr.bin/tmux/screen-write.c, Revision 1.110
1.110 ! nicm 1: /* $OpenBSD: screen-write.c,v 1.109 2017/02/08 16:45:18 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 *);
31: static void screen_write_collect_flush(struct screen_write_ctx *);
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);
88: screen_write_collect_flush(ctx);
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.109 nicm 573: screen_write_collect_flush(ctx);
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.109 nicm 601: screen_write_collect_flush(ctx);
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.109 nicm 628: screen_write_collect_flush(ctx);
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.109 nicm 655: screen_write_collect_flush(ctx);
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.109 nicm 674: screen_write_collect_flush(ctx);
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.109 nicm 701: screen_write_collect_flush(ctx);
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.109 nicm 720: screen_write_collect_flush(ctx);
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.17 nicm 744: tty_write(tty_cmd_clearline, &ttyctx);
1.1 nicm 745: }
746:
747: /* Clear to end of line from cursor. */
748: void
1.99 nicm 749: screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 750: {
1.92 nicm 751: struct screen *s = ctx->s;
752: struct grid_line *gl;
753: struct tty_ctx ttyctx;
754: u_int sx = screen_size_x(s);
1.1 nicm 755:
1.92 nicm 756: gl = &s->grid->linedata[s->grid->hsize + s->cy];
1.99 nicm 757: if (s->cx > sx - 1 || (s->cx >= gl->cellsize && bg == 8))
1.92 nicm 758: return;
1.108 nicm 759:
760: screen_write_initctx(ctx, &ttyctx);
761: ttyctx.bg = bg;
1.1 nicm 762:
1.99 nicm 763: grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
764:
1.109 nicm 765: if (s->cx == 0)
766: screen_write_collect_clear(ctx, s->cy, 1);
767: else
768: screen_write_collect_flush(ctx);
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);
790: else
791: screen_write_collect_flush(ctx);
1.17 nicm 792: tty_write(tty_cmd_clearstartofline, &ttyctx);
1.1 nicm 793: }
794:
1.101 nicm 795: /* Move cursor to px,py. */
1.1 nicm 796: void
797: screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
798: {
799: struct screen *s = ctx->s;
800:
801: if (px > screen_size_x(s) - 1)
802: px = screen_size_x(s) - 1;
803: if (py > screen_size_y(s) - 1)
804: py = screen_size_y(s) - 1;
805:
806: s->cx = px;
807: s->cy = py;
808: }
809:
1.101 nicm 810: /* Reverse index (up with scroll). */
1.1 nicm 811: void
812: screen_write_reverseindex(struct screen_write_ctx *ctx)
813: {
814: struct screen *s = ctx->s;
1.17 nicm 815: struct tty_ctx ttyctx;
1.1 nicm 816:
1.87 nicm 817: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 818:
1.109 nicm 819: if (s->cy == s->rupper)
1.1 nicm 820: grid_view_scroll_region_down(s->grid, s->rupper, s->rlower);
1.109 nicm 821: else if (s->cy > 0)
1.1 nicm 822: s->cy--;
823:
1.109 nicm 824: screen_write_collect_flush(ctx);
1.17 nicm 825: tty_write(tty_cmd_reverseindex, &ttyctx);
1.1 nicm 826: }
827:
828: /* Set scroll region. */
829: void
1.83 nicm 830: screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
831: u_int rlower)
1.1 nicm 832: {
833: struct screen *s = ctx->s;
834:
835: if (rupper > screen_size_y(s) - 1)
836: rupper = screen_size_y(s) - 1;
837: if (rlower > screen_size_y(s) - 1)
838: rlower = screen_size_y(s) - 1;
1.13 nicm 839: if (rupper >= rlower) /* cannot be one line */
1.1 nicm 840: return;
841:
1.110 ! nicm 842: screen_write_collect_flush(ctx);
! 843:
1.1 nicm 844: /* Cursor moves to top-left. */
845: s->cx = 0;
846: s->cy = 0;
847:
848: s->rupper = rupper;
849: s->rlower = rlower;
850: }
851:
1.34 nicm 852: /* Line feed. */
1.1 nicm 853: void
1.34 nicm 854: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped)
1.1 nicm 855: {
1.20 nicm 856: struct screen *s = ctx->s;
1.109 nicm 857: struct grid *gd = s->grid;
1.20 nicm 858: struct grid_line *gl;
1.1 nicm 859:
1.109 nicm 860: gl = &gd->linedata[gd->hsize + s->cy];
1.20 nicm 861: if (wrapped)
862: gl->flags |= GRID_LINE_WRAPPED;
1.73 nicm 863: else
864: gl->flags &= ~GRID_LINE_WRAPPED;
1.20 nicm 865:
1.92 nicm 866: if (s->cy == s->rlower) {
1.109 nicm 867: grid_view_scroll_region_up(gd, s->rupper, s->rlower);
868: screen_write_collect_scroll(ctx);
1.110 ! nicm 869: ctx->scrolled++;
1.109 nicm 870: } else if (s->cy < screen_size_y(s) - 1)
1.1 nicm 871: s->cy++;
872: }
873:
1.110 ! nicm 874: /* Scroll up. */
! 875: void
! 876: screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines)
! 877: {
! 878: struct screen *s = ctx->s;
! 879: struct grid *gd = s->grid;
! 880: u_int i;
! 881:
! 882: if (lines == 0)
! 883: lines = 1;
! 884: else if (lines > s->rlower - s->rupper + 1)
! 885: lines = s->rlower - s->rupper + 1;
! 886:
! 887: for (i = 0; i < lines; i++) {
! 888: grid_view_scroll_region_up(gd, s->rupper, s->rlower);
! 889: screen_write_collect_scroll(ctx);
! 890: }
! 891: ctx->scrolled += lines;
! 892: }
! 893:
1.1 nicm 894: /* Carriage return (cursor to start of line). */
895: void
896: screen_write_carriagereturn(struct screen_write_ctx *ctx)
897: {
898: struct screen *s = ctx->s;
899:
900: s->cx = 0;
901: }
902:
903: /* Clear to end of screen from cursor. */
904: void
1.99 nicm 905: screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 906: {
907: struct screen *s = ctx->s;
1.107 nicm 908: struct grid *gd = s->grid;
1.17 nicm 909: struct tty_ctx ttyctx;
1.92 nicm 910: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 911:
1.87 nicm 912: screen_write_initctx(ctx, &ttyctx);
1.99 nicm 913: ttyctx.bg = bg;
1.1 nicm 914:
1.46 nicm 915: /* Scroll into history if it is enabled and clearing entire screen. */
1.109 nicm 916: if (s->cx == 0 && s->cy == 0 && (gd->flags & GRID_HISTORY))
1.107 nicm 917: grid_view_clear_history(gd, bg);
1.109 nicm 918: else {
919: if (s->cx <= sx - 1)
1.107 nicm 920: grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
921: grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1.46 nicm 922: }
1.1 nicm 923:
1.109 nicm 924: screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
925: screen_write_collect_flush(ctx);
1.17 nicm 926: tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1 nicm 927: }
928:
929: /* Clear to start of screen. */
930: void
1.105 nicm 931: screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 932: {
933: struct screen *s = ctx->s;
1.17 nicm 934: struct tty_ctx ttyctx;
1.92 nicm 935: u_int sx = screen_size_x(s);
1.1 nicm 936:
1.87 nicm 937: screen_write_initctx(ctx, &ttyctx);
1.105 nicm 938: ttyctx.bg = bg;
1.1 nicm 939:
1.109 nicm 940: if (s->cy > 0)
1.105 nicm 941: grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1.109 nicm 942: if (s->cx > sx - 1)
943: grid_view_clear(s->grid, 0, s->cy, sx, 1, 8);
944: else
945: grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, 8);
1.1 nicm 946:
1.109 nicm 947: screen_write_collect_clear(ctx, 0, s->cy);
948: screen_write_collect_flush(ctx);
1.17 nicm 949: tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1 nicm 950: }
951:
952: /* Clear entire screen. */
953: void
1.99 nicm 954: screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 955: {
956: struct screen *s = ctx->s;
1.17 nicm 957: struct tty_ctx ttyctx;
1.92 nicm 958: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 959:
1.87 nicm 960: screen_write_initctx(ctx, &ttyctx);
1.99 nicm 961: ttyctx.bg = bg;
1.1 nicm 962:
1.46 nicm 963: /* Scroll into history if it is enabled. */
964: if (s->grid->flags & GRID_HISTORY)
1.99 nicm 965: grid_view_clear_history(s->grid, bg);
1.83 nicm 966: else
1.99 nicm 967: grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1.1 nicm 968:
1.109 nicm 969: screen_write_collect_clear(ctx, 0, sy);
1.17 nicm 970: tty_write(tty_cmd_clearscreen, &ttyctx);
1.51 nicm 971: }
972:
973: /* Clear entire history. */
974: void
975: screen_write_clearhistory(struct screen_write_ctx *ctx)
976: {
977: struct screen *s = ctx->s;
978: struct grid *gd = s->grid;
979:
1.99 nicm 980: grid_move_lines(gd, 0, gd->hsize, gd->sy, 8);
1.93 nicm 981: gd->hscrolled = gd->hsize = 0;
1.1 nicm 982: }
983:
1.109 nicm 984: /* Clear a collected line. */
985: static void
986: screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
987: {
988: struct screen_write_collect_item *ci, *tmp;
989: u_int i;
990: size_t size;
991:
992: for (i = y ; i < y + n; i++) {
993: if (TAILQ_EMPTY(&ctx->list[i].items))
994: continue;
995: size = 0;
996: TAILQ_FOREACH_SAFE(ci, &ctx->list[i].items, entry, tmp) {
997: size += ci->used;
998: TAILQ_REMOVE(&ctx->list[i].items, ci, entry);
999: free(ci);
1000: }
1001: ctx->skipped += size;
1002: log_debug("discarding %zu bytes on line %u", size, i);
1003: }
1004: }
1005:
1006: /* Scroll collected lines up. */
1007: static void
1008: screen_write_collect_scroll(struct screen_write_ctx *ctx)
1009: {
1010: struct screen *s = ctx->s;
1011: struct screen_write_collect_line *cl;
1012: u_int y;
1013:
1014: screen_write_collect_clear(ctx, s->rupper, 1);
1015: for (y = s->rupper; y < s->rlower; y++) {
1016: cl = &ctx->list[y + 1];
1017: TAILQ_CONCAT(&ctx->list[y].items, &cl->items, entry);
1018: TAILQ_INIT(&cl->items);
1019: }
1020: }
1021:
1022: /* Flush collected lines. */
1023: static void
1024: screen_write_collect_flush(struct screen_write_ctx *ctx)
1025: {
1026: struct screen *s = ctx->s;
1027: struct screen_write_collect_item *ci, *tmp;
1028: u_int y, cx, cy;
1029: struct tty_ctx ttyctx;
1.110 ! nicm 1030:
! 1031: if (ctx->scrolled != 0) {
! 1032: log_debug("%s: scrolled %u (region %u-%u)", __func__,
! 1033: ctx->scrolled, s->rupper, s->rlower);
! 1034: if (ctx->scrolled > s->rlower - s->rupper + 1)
! 1035: ctx->scrolled = s->rlower - s->rupper + 1;
! 1036:
! 1037: screen_write_initctx(ctx, &ttyctx);
! 1038: ttyctx.num = ctx->scrolled;
! 1039: tty_write(tty_cmd_scrollup, &ttyctx);
! 1040: }
! 1041: ctx->scrolled = 0;
1.109 nicm 1042:
1043: cx = s->cx; cy = s->cy;
1044: for (y = 0; y < screen_size_y(s); y++) {
1045: TAILQ_FOREACH_SAFE(ci, &ctx->list[y].items, entry, tmp) {
1046: screen_write_cursormove(ctx, ci->x, y);
1047: screen_write_initctx(ctx, &ttyctx);
1048: ttyctx.cell = &ci->gc;
1049: ttyctx.ptr = ci->data;
1050: ttyctx.num = ci->used;
1051: tty_write(tty_cmd_cells, &ttyctx);
1052: ctx->written += ci->used;
1053:
1054: TAILQ_REMOVE(&ctx->list[y].items, ci, entry);
1055: free(ci);
1056: }
1057: }
1058: s->cx = cx; s->cy = cy;
1059: }
1060:
1061: /* Finish and store collected cells. */
1062: void
1063: screen_write_collect_end(struct screen_write_ctx *ctx)
1064: {
1065: struct screen *s = ctx->s;
1066: struct screen_write_collect_item *ci = ctx->item;
1067: struct grid_cell gc;
1068:
1069: if (ci->used == 0)
1070: return;
1071: ci->data[ci->used] = '\0';
1072:
1073: ci->x = s->cx;
1074: TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
1075: ctx->item = xcalloc(1, sizeof *ctx->item);
1076:
1077: log_debug("%s: %u %s (at %u,%u)", __func__, ci->used, ci->data, s->cx,
1078: s->cy);
1079:
1080: memcpy(&gc, &ci->gc, sizeof gc);
1081: grid_view_set_cells(s->grid, s->cx, s->cy, &gc, ci->data, ci->used);
1082: s->cx += ci->used;
1083: }
1084:
1085: /* Write cell data, collecting if necessary. */
1086: void
1087: screen_write_collect_add(struct screen_write_ctx *ctx,
1088: const struct grid_cell *gc)
1089: {
1090: struct screen *s = ctx->s;
1091: struct screen_write_collect_item *ci;
1092: u_int sx = screen_size_x(s);
1093: int collect;
1094:
1095: /*
1096: * Don't need to check that the attributes and whatnot are still the
1097: * same - input_parse will do a flush when anything that isn't a plain
1098: * character is encountered. Also nothing should make it here that
1099: * isn't a single ASCII character.
1100: */
1101:
1102: collect = 1;
1103: if (gc->data.width != 1)
1104: collect = 0;
1105: else if (gc->attr & GRID_ATTR_CHARSET)
1106: collect = 0;
1107: else if (~s->mode & MODE_WRAP)
1108: collect = 0;
1109: else if (s->mode & MODE_INSERT)
1110: collect = 0;
1111: else if (s->sel.flag)
1112: collect = 0;
1113: if (!collect) {
1114: screen_write_collect_end(ctx);
1115: screen_write_cell(ctx, gc);
1116: return;
1117: }
1118: ctx->cells++;
1119:
1120: if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
1121: screen_write_collect_end(ctx);
1122: if (s->cx > sx - 1) {
1123: screen_write_linefeed(ctx, 1);
1124: s->cx = 0;
1125: }
1126:
1127: ci = ctx->item; /* may have changed */
1128: if (ci->used == 0)
1129: memcpy(&ci->gc, gc, sizeof ci->gc);
1130: ci->data[ci->used++] = gc->data.data[0];
1131: if (ci->used == (sizeof ci->data) - 1)
1132: screen_write_collect_end(ctx);
1133: }
1134:
1.1 nicm 1135: /* Write cell data. */
1136: void
1.60 nicm 1137: screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1.1 nicm 1138: {
1139: struct screen *s = ctx->s;
1140: struct grid *gd = s->grid;
1.109 nicm 1141: struct grid_line *gl;
1142: struct grid_cell_entry *gce;
1143: struct grid_cell tmp_gc, now_gc;
1.15 nicm 1144: struct tty_ctx ttyctx;
1.92 nicm 1145: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.109 nicm 1146: u_int width = gc->data.width, xx, last;
1147: int selected, skip = 1;
1.1 nicm 1148:
1.109 nicm 1149: /* Ignore padding cells. */
1.1 nicm 1150: if (gc->flags & GRID_FLAG_PADDING)
1151: return;
1.109 nicm 1152: ctx->cells++;
1.32 nicm 1153:
1.109 nicm 1154: /* If the width is zero, combine onto the previous character. */
1.1 nicm 1155: if (width == 0) {
1.104 nicm 1156: if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) {
1157: screen_write_cursormove(ctx, xx, s->cy);
1.87 nicm 1158: screen_write_initctx(ctx, &ttyctx);
1.104 nicm 1159: ttyctx.cell = gc;
1160: tty_write(tty_cmd_cell, &ttyctx);
1.1 nicm 1161: }
1162: return;
1163: }
1164:
1.109 nicm 1165: /* If this character doesn't fit, ignore it. */
1166: if ((~s->mode & MODE_WRAP) &&
1167: width > 1 &&
1168: (width > sx || (s->cx != sx && s->cx > sx - width)))
1169: return;
1170:
1.6 nicm 1171: /* If in insert mode, make space for the cells. */
1.98 nicm 1172: if (s->mode & MODE_INSERT) {
1.109 nicm 1173: grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
1174: skip = 0;
1175: }
1.6 nicm 1176:
1.20 nicm 1177: /* Check this will fit on the current line and wrap if not. */
1.92 nicm 1178: if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1.34 nicm 1179: screen_write_linefeed(ctx, 1);
1.109 nicm 1180: s->cx = 0;
1.1 nicm 1181: }
1182:
1.64 nicm 1183: /* Sanity check cursor position. */
1.92 nicm 1184: if (s->cx > sx - width || s->cy > sy - 1)
1.1 nicm 1185: return;
1.106 nicm 1186: screen_write_initctx(ctx, &ttyctx);
1187:
1.1 nicm 1188: /* Handle overwriting of UTF-8 characters. */
1.92 nicm 1189: gl = &s->grid->linedata[s->grid->hsize + s->cy];
1190: if (gl->flags & GRID_LINE_EXTENDED) {
1191: grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
1192: if (screen_write_overwrite(ctx, &now_gc, width))
1193: skip = 0;
1194: }
1.1 nicm 1195:
1196: /*
1197: * If the new character is UTF-8 wide, fill in padding cells. Have
1198: * already ensured there is enough room.
1199: */
1.89 nicm 1200: for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.88 nicm 1201: grid_view_set_cell(gd, xx, s->cy, &screen_write_pad_cell);
1.89 nicm 1202: skip = 0;
1203: }
1204:
1205: /* If no change, do not draw. */
1.92 nicm 1206: if (skip) {
1207: if (s->cx >= gl->cellsize)
1208: skip = grid_cells_equal(gc, &grid_default_cell);
1209: else {
1210: gce = &gl->celldata[s->cx];
1211: if (gce->flags & GRID_FLAG_EXTENDED)
1212: skip = 0;
1.95 nicm 1213: else if (gc->flags != gce->flags)
1.92 nicm 1214: skip = 0;
1215: else if (gc->attr != gce->data.attr)
1216: skip = 0;
1217: else if (gc->fg != gce->data.fg)
1218: skip = 0;
1219: else if (gc->bg != gce->data.bg)
1220: skip = 0;
1.95 nicm 1221: else if (gc->data.width != 1)
1222: skip = 0;
1.109 nicm 1223: else if (gc->data.size != 1)
1224: skip = 0;
1.95 nicm 1225: else if (gce->data.data != gc->data.data[0])
1.92 nicm 1226: skip = 0;
1227: }
1228: }
1.1 nicm 1229:
1.90 nicm 1230: /* Update the selection the flag and set the cell. */
1231: selected = screen_check_selection(s, s->cx, s->cy);
1.109 nicm 1232: if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1.90 nicm 1233: memcpy(&tmp_gc, gc, sizeof tmp_gc);
1234: tmp_gc.flags |= GRID_FLAG_SELECTED;
1235: grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1.109 nicm 1236: } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1.90 nicm 1237: memcpy(&tmp_gc, gc, sizeof tmp_gc);
1238: tmp_gc.flags &= ~GRID_FLAG_SELECTED;
1239: grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1240: } else if (!skip)
1.89 nicm 1241: grid_view_set_cell(gd, s->cx, s->cy, gc);
1.109 nicm 1242: if (selected)
1243: skip = 0;
1.1 nicm 1244:
1.64 nicm 1245: /*
1246: * Move the cursor. If not wrapping, stick at the last character and
1247: * replace it.
1248: */
1.65 nicm 1249: last = !(s->mode & MODE_WRAP);
1.92 nicm 1250: if (s->cx <= sx - last - width)
1.64 nicm 1251: s->cx += width;
1252: else
1.92 nicm 1253: s->cx = sx - last;
1.1 nicm 1254:
1.89 nicm 1255: /* Create space for character in insert mode. */
1.109 nicm 1256: if (s->mode & MODE_INSERT) {
1257: screen_write_collect_flush(ctx);
1.17 nicm 1258: ttyctx.num = width;
1259: tty_write(tty_cmd_insertcharacter, &ttyctx);
1260: }
1.89 nicm 1261:
1262: /* Write to the screen. */
1.109 nicm 1263: if (!skip) {
1264: if (selected) {
1265: screen_select_cell(s, &tmp_gc, gc);
1266: ttyctx.cell = &tmp_gc;
1267: } else
1268: ttyctx.cell = gc;
1.16 nicm 1269: tty_write(tty_cmd_cell, &ttyctx);
1.92 nicm 1270: ctx->written++;
1271: } else
1272: ctx->skipped++;
1.35 nicm 1273: }
1274:
1275: /* Combine a UTF-8 zero-width character onto the previous. */
1.104 nicm 1276: static const struct grid_cell *
1277: screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
1278: u_int *xx)
1.35 nicm 1279: {
1280: struct screen *s = ctx->s;
1281: struct grid *gd = s->grid;
1.104 nicm 1282: static struct grid_cell gc;
1283: u_int n;
1.35 nicm 1284:
1285: /* Can't combine if at 0. */
1286: if (s->cx == 0)
1.104 nicm 1287: return (NULL);
1.35 nicm 1288:
1.60 nicm 1289: /* Empty data is out. */
1290: if (ud->size == 0)
1.37 nicm 1291: fatalx("UTF-8 data empty");
1292:
1.60 nicm 1293: /* Retrieve the previous cell. */
1.104 nicm 1294: for (n = 1; n < s->cx; n++) {
1295: grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
1296: if (~gc.flags & GRID_FLAG_PADDING)
1297: break;
1298: }
1299: if (n == s->cx)
1300: return (NULL);
1301: *xx = s->cx - n;
1.35 nicm 1302:
1.60 nicm 1303: /* Check there is enough space. */
1.77 nicm 1304: if (gc.data.size + ud->size > sizeof gc.data.data)
1.104 nicm 1305: return (NULL);
1306:
1307: log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size,
1308: ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy);
1.35 nicm 1309:
1.77 nicm 1310: /* Append the data. */
1311: memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
1312: gc.data.size += ud->size;
1313:
1314: /* Set the new cell. */
1.104 nicm 1315: grid_view_set_cell(gd, *xx, s->cy, &gc);
1.35 nicm 1316:
1.104 nicm 1317: return (&gc);
1.1 nicm 1318: }
1319:
1320: /*
1321: * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1322: * cell on the screen, so following cells must not be drawn by marking them as
1323: * padding.
1324: *
1325: * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1326: * character, it is necessary to also overwrite any other cells which covered
1327: * by the same character.
1328: */
1.89 nicm 1329: static int
1330: screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
1331: u_int width)
1.1 nicm 1332: {
1333: struct screen *s = ctx->s;
1334: struct grid *gd = s->grid;
1.89 nicm 1335: struct grid_cell tmp_gc;
1.1 nicm 1336: u_int xx;
1.89 nicm 1337: int done = 0;
1.1 nicm 1338:
1.89 nicm 1339: if (gc->flags & GRID_FLAG_PADDING) {
1.1 nicm 1340: /*
1341: * A padding cell, so clear any following and leading padding
1342: * cells back to the character. Don't overwrite the current
1343: * cell as that happens later anyway.
1344: */
1345: xx = s->cx + 1;
1346: while (--xx > 0) {
1.89 nicm 1347: grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1348: if (~tmp_gc.flags & GRID_FLAG_PADDING)
1.1 nicm 1349: break;
1350: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1351: }
1352:
1353: /* Overwrite the character at the start of this padding. */
1354: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1.89 nicm 1355: done = 1;
1.43 nicm 1356: }
1.1 nicm 1357:
1.43 nicm 1358: /*
1.95 nicm 1359: * Overwrite any padding cells that belong to any UTF-8 characters
1360: * we'll be overwriting with the current character.
1.43 nicm 1361: */
1.95 nicm 1362: if (width != 1 ||
1363: gc->data.width != 1 ||
1364: gc->flags & GRID_FLAG_PADDING) {
1.89 nicm 1365: xx = s->cx + width - 1;
1366: while (++xx < screen_size_x(s)) {
1367: grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1368: if (~tmp_gc.flags & GRID_FLAG_PADDING)
1369: break;
1370: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1371: done = 1;
1372: }
1.1 nicm 1373: }
1.89 nicm 1374:
1375: return (done);
1.50 nicm 1376: }
1377:
1.107 nicm 1378: /* Set external clipboard. */
1.50 nicm 1379: void
1380: screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
1381: {
1382: struct tty_ctx ttyctx;
1383:
1.87 nicm 1384: screen_write_initctx(ctx, &ttyctx);
1.50 nicm 1385: ttyctx.ptr = str;
1386: ttyctx.num = len;
1387:
1388: tty_write(tty_cmd_setselection, &ttyctx);
1.47 nicm 1389: }
1390:
1.107 nicm 1391: /* Write unmodified string. */
1.47 nicm 1392: void
1393: screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
1394: {
1.87 nicm 1395: struct tty_ctx ttyctx;
1.47 nicm 1396:
1.87 nicm 1397: screen_write_initctx(ctx, &ttyctx);
1.47 nicm 1398: ttyctx.ptr = str;
1399: ttyctx.num = len;
1400:
1401: tty_write(tty_cmd_rawstring, &ttyctx);
1.1 nicm 1402: }