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