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