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