Annotation of src/usr.bin/tmux/screen-write.c, Revision 1.165
1.164 nicm 1: /* $OpenBSD: screen-write.c,v 1.163 2020/04/16 16:13: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.109 nicm 26: static void screen_write_collect_clear(struct screen_write_ctx *, u_int,
27: u_int);
1.165 ! nicm 28: static int screen_write_collect_clear_end(struct screen_write_ctx *, u_int,
! 29: u_int, u_int);
! 30: static int screen_write_collect_clear_start(struct screen_write_ctx *, u_int,
! 31: u_int, u_int);
1.109 nicm 32: static void screen_write_collect_scroll(struct screen_write_ctx *);
1.164 nicm 33: static void screen_write_collect_flush(struct screen_write_ctx *, int,
34: const char *);
1.87 nicm 35:
1.89 nicm 36: static int screen_write_overwrite(struct screen_write_ctx *,
37: struct grid_cell *, u_int);
1.104 nicm 38: static const struct grid_cell *screen_write_combine(struct screen_write_ctx *,
39: const struct utf8_data *, u_int *);
1.1 nicm 40:
1.88 nicm 41: static const struct grid_cell screen_write_pad_cell = {
1.155 nicm 42: { { 0 }, 0, 0, 0 }, 0, GRID_FLAG_PADDING, 0, 8, 8
1.88 nicm 43: };
44:
1.109 nicm 45: struct screen_write_collect_item {
46: u_int x;
1.118 nicm 47: int wrapped;
1.109 nicm 48:
1.165 ! nicm 49: enum { TEXT, CLEAR, CLEAR_END, CLEAR_START } type;
1.109 nicm 50: u_int used;
1.165 ! nicm 51: union {
! 52: u_int bg;
! 53: char data[256];
! 54: };
1.109 nicm 55:
56: struct grid_cell gc;
57:
1.126 nicm 58: TAILQ_ENTRY(screen_write_collect_item) entry;
1.109 nicm 59: };
60: struct screen_write_collect_line {
61: TAILQ_HEAD(, screen_write_collect_item) items;
62: };
1.92 nicm 63:
1.139 nicm 64: static void
65: screen_write_offset_timer(__unused int fd, __unused short events, void *data)
66: {
67: struct window *w = data;
68:
69: tty_update_window_offset(w);
70: }
71:
72: /* Set cursor position. */
73: static void
74: screen_write_set_cursor(struct screen_write_ctx *ctx, int cx, int cy)
75: {
76: struct window_pane *wp = ctx->wp;
77: struct window *w;
78: struct screen *s = ctx->s;
79: struct timeval tv = { .tv_usec = 10000 };
80:
81: if (cx != -1 && (u_int)cx == s->cx && cy != -1 && (u_int)cy == s->cy)
82: return;
83:
1.144 nicm 84: if (cx != -1) {
1.145 nicm 85: if ((u_int)cx > screen_size_x(s)) /* allow last column */
1.144 nicm 86: cx = screen_size_x(s) - 1;
1.139 nicm 87: s->cx = cx;
1.144 nicm 88: }
89: if (cy != -1) {
90: if ((u_int)cy > screen_size_y(s) - 1)
91: cy = screen_size_y(s) - 1;
1.139 nicm 92: s->cy = cy;
1.144 nicm 93: }
1.139 nicm 94:
95: if (wp == NULL)
96: return;
97: w = wp->window;
98:
99: if (!event_initialized(&w->offset_timer))
100: evtimer_set(&w->offset_timer, screen_write_offset_timer, w);
101: if (!evtimer_pending(&w->offset_timer, NULL))
102: evtimer_add(&w->offset_timer, &tv);
103: }
104:
1.163 nicm 105: /* Set up context for TTY command. */
106: static void
107: screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx,
108: int sync)
109: {
110: struct screen *s = ctx->s;
111:
112: memset(ttyctx, 0, sizeof *ttyctx);
113:
114: ttyctx->wp = ctx->wp;
115:
116: ttyctx->ocx = s->cx;
117: ttyctx->ocy = s->cy;
118:
119: ttyctx->orlower = s->rlower;
120: ttyctx->orupper = s->rupper;
121:
122: if (sync && !ctx->sync && ttyctx->wp != NULL) {
123: log_debug("%s: starting sync", __func__);
124: tty_write(tty_cmd_syncstart, ttyctx);
125: ctx->sync = 1;
126: }
127: }
128:
1.92 nicm 129: /* Initialize writing with a window. */
1.1 nicm 130: void
1.71 nicm 131: screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp,
132: struct screen *s)
1.1 nicm 133: {
1.163 nicm 134: u_int y;
1.109 nicm 135:
136: memset(ctx, 0, sizeof *ctx);
1.92 nicm 137:
1.1 nicm 138: ctx->wp = wp;
139: if (wp != NULL && s == NULL)
140: ctx->s = wp->screen;
141: else
142: ctx->s = s;
1.92 nicm 143:
1.109 nicm 144: ctx->list = xcalloc(screen_size_y(ctx->s), sizeof *ctx->list);
145: for (y = 0; y < screen_size_y(ctx->s); y++)
146: TAILQ_INIT(&ctx->list[y].items);
147: ctx->item = xcalloc(1, sizeof *ctx->item);
1.92 nicm 148:
1.122 nicm 149: ctx->scrolled = 0;
150: ctx->bg = 8;
151:
1.158 nicm 152: if (log_get_level() != 0) {
153: if (wp != NULL) {
154: log_debug("%s: size %ux%u, pane %%%u (at %u,%u)",
155: __func__, screen_size_x(ctx->s),
156: screen_size_y(ctx->s), wp->id, wp->xoff, wp->yoff);
157: } else {
158: log_debug("%s: size %ux%u, no pane",
159: __func__, screen_size_x(ctx->s),
160: screen_size_y(ctx->s));
161: }
1.139 nicm 162: }
1.1 nicm 163: }
164:
165: /* Finish writing. */
166: void
1.92 nicm 167: screen_write_stop(struct screen_write_ctx *ctx)
168: {
1.162 nicm 169: struct tty_ctx ttyctx;
170:
1.109 nicm 171: screen_write_collect_end(ctx);
1.164 nicm 172: screen_write_collect_flush(ctx, 0, __func__);
1.92 nicm 173:
1.109 nicm 174: log_debug("%s: %u cells (%u written, %u skipped)", __func__,
175: ctx->cells, ctx->written, ctx->skipped);
1.162 nicm 176:
1.163 nicm 177: if (ctx->sync) {
178: screen_write_initctx(ctx, &ttyctx, 0);
179: tty_write(tty_cmd_syncend, &ttyctx);
180: log_debug("%s: ending sync", __func__);
181: }
1.92 nicm 182:
1.109 nicm 183: free(ctx->item);
184: free(ctx->list); /* flush will have emptied */
1.52 nicm 185: }
186:
187: /* Reset screen state. */
188: void
189: screen_write_reset(struct screen_write_ctx *ctx)
190: {
1.61 nicm 191: struct screen *s = ctx->s;
1.52 nicm 192:
1.61 nicm 193: screen_reset_tabs(s);
194: screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
1.63 nicm 195:
1.141 nicm 196: s->mode = MODE_CURSOR | MODE_WRAP;
1.52 nicm 197:
1.99 nicm 198: screen_write_clearscreen(ctx, 8);
1.144 nicm 199: screen_write_set_cursor(ctx, 0, 0);
1.1 nicm 200: }
201:
202: /* Write character. */
203: void
1.86 nicm 204: screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.69 nicm 205: u_char ch)
1.1 nicm 206: {
1.86 nicm 207: struct grid_cell gc;
208:
209: memcpy(&gc, gcp, sizeof gc);
210:
211: utf8_set(&gc.data, ch);
212: screen_write_cell(ctx, &gc);
1.1 nicm 213: }
214:
1.2 nicm 215: /* Calculate string length. */
1.71 nicm 216: size_t
1.75 nicm 217: screen_write_strlen(const char *fmt, ...)
1.2 nicm 218: {
1.36 nicm 219: va_list ap;
220: char *msg;
1.76 nicm 221: struct utf8_data ud;
1.36 nicm 222: u_char *ptr;
223: size_t left, size = 0;
1.79 nicm 224: enum utf8_state more;
1.2 nicm 225:
226: va_start(ap, fmt);
227: xvasprintf(&msg, fmt, ap);
228: va_end(ap);
229:
230: ptr = msg;
231: while (*ptr != '\0') {
1.79 nicm 232: if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
1.36 nicm 233: ptr++;
1.2 nicm 234:
235: left = strlen(ptr);
1.77 nicm 236: if (left < (size_t)ud.size - 1)
1.36 nicm 237: break;
1.79 nicm 238: while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
1.2 nicm 239: ptr++;
1.36 nicm 240: ptr++;
241:
1.79 nicm 242: if (more == UTF8_DONE)
1.78 nicm 243: size += ud.width;
1.2 nicm 244: } else {
1.75 nicm 245: if (*ptr > 0x1f && *ptr < 0x7f)
246: size++;
1.2 nicm 247: ptr++;
248: }
1.7 ray 249: }
1.2 nicm 250:
1.56 nicm 251: free(msg);
1.2 nicm 252: return (size);
253: }
254:
1.3 nicm 255: /* Write simple string (no UTF-8 or maximum length). */
1.71 nicm 256: void
1.86 nicm 257: screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.71 nicm 258: const char *fmt, ...)
1.1 nicm 259: {
260: va_list ap;
261:
262: va_start(ap, fmt);
1.86 nicm 263: screen_write_vnputs(ctx, -1, gcp, fmt, ap);
1.2 nicm 264: va_end(ap);
265: }
266:
267: /* Write string with length limit (-1 for unlimited). */
1.71 nicm 268: void
269: screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86 nicm 270: const struct grid_cell *gcp, const char *fmt, ...)
1.2 nicm 271: {
272: va_list ap;
273:
274: va_start(ap, fmt);
1.86 nicm 275: screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
1.2 nicm 276: va_end(ap);
277: }
278:
279: void
1.3 nicm 280: screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86 nicm 281: const struct grid_cell *gcp, const char *fmt, va_list ap)
1.2 nicm 282: {
1.86 nicm 283: struct grid_cell gc;
284: struct utf8_data *ud = &gc.data;
1.36 nicm 285: char *msg;
286: u_char *ptr;
287: size_t left, size = 0;
1.79 nicm 288: enum utf8_state more;
1.2 nicm 289:
1.86 nicm 290: memcpy(&gc, gcp, sizeof gc);
1.1 nicm 291: xvasprintf(&msg, fmt, ap);
292:
1.2 nicm 293: ptr = msg;
294: while (*ptr != '\0') {
1.86 nicm 295: if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36 nicm 296: ptr++;
1.2 nicm 297:
298: left = strlen(ptr);
1.86 nicm 299: if (left < (size_t)ud->size - 1)
1.36 nicm 300: break;
1.86 nicm 301: while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.2 nicm 302: ptr++;
1.36 nicm 303: ptr++;
1.7 ray 304:
1.86 nicm 305: if (more != UTF8_DONE)
306: continue;
307: if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
308: while (size < (size_t)maxlen) {
309: screen_write_putc(ctx, &gc, ' ');
310: size++;
1.2 nicm 311: }
1.86 nicm 312: break;
1.2 nicm 313: }
1.86 nicm 314: size += ud->width;
315: screen_write_cell(ctx, &gc);
1.2 nicm 316: } else {
1.86 nicm 317: if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.2 nicm 318: break;
319:
1.57 nicm 320: if (*ptr == '\001')
1.86 nicm 321: gc.attr ^= GRID_ATTR_CHARSET;
1.75 nicm 322: else if (*ptr > 0x1f && *ptr < 0x7f) {
1.57 nicm 323: size++;
1.86 nicm 324: screen_write_putc(ctx, &gc, *ptr);
1.75 nicm 325: }
1.24 nicm 326: ptr++;
327: }
328: }
329:
1.56 nicm 330: free(msg);
1.1 nicm 331: }
332:
1.132 nicm 333: /* Copy from another screen. Assumes target region is big enough. */
1.1 nicm 334: void
1.95 nicm 335: screen_write_copy(struct screen_write_ctx *ctx, struct screen *src, u_int px,
1.132 nicm 336: u_int py, u_int nx, u_int ny, bitstr_t *mbs, const struct grid_cell *mgc)
1.1 nicm 337: {
338: struct screen *s = ctx->s;
339: struct grid *gd = src->grid;
1.77 nicm 340: struct grid_cell gc;
1.102 nicm 341: u_int xx, yy, cx, cy, b;
1.1 nicm 342:
1.125 nicm 343: if (nx == 0 || ny == 0)
344: return;
345:
1.1 nicm 346: cx = s->cx;
347: cy = s->cy;
1.96 nicm 348:
1.1 nicm 349: for (yy = py; yy < py + ny; yy++) {
1.96 nicm 350: for (xx = px; xx < px + nx; xx++) {
351: grid_get_cell(gd, xx, yy, &gc);
1.132 nicm 352: if (mbs != NULL) {
1.102 nicm 353: b = (yy * screen_size_x(src)) + xx;
1.132 nicm 354: if (bit_test(mbs, b)) {
355: gc.attr = mgc->attr;
356: gc.fg = mgc->fg;
357: gc.bg = mgc->bg;
1.102 nicm 358: }
359: }
1.135 nicm 360: if (xx + gc.data.width <= px + nx)
361: screen_write_cell(ctx, &gc);
1.96 nicm 362: }
1.132 nicm 363: cy++;
1.144 nicm 364: screen_write_set_cursor(ctx, cx, cy);
1.132 nicm 365: }
366: }
367:
368: /*
369: * Copy from another screen but without the selection stuff. Also assumes the
1.150 nicm 370: * target region is already big enough.
1.132 nicm 371: */
372: void
373: screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src,
374: u_int px, u_int py, u_int nx, u_int ny)
375: {
376: struct screen *s = ctx->s;
377: struct grid *gd = src->grid;
378: struct grid_cell gc;
379: u_int xx, yy, cx, cy;
1.96 nicm 380:
1.132 nicm 381: if (nx == 0 || ny == 0)
382: return;
383:
384: cy = s->cy;
385: for (yy = py; yy < py + ny; yy++) {
1.134 nicm 386: if (yy >= gd->hsize + gd->sy)
387: break;
1.132 nicm 388: cx = s->cx;
389: for (xx = px; xx < px + nx; xx++) {
1.137 nicm 390: if (xx >= grid_get_line(gd, yy)->cellsize)
1.132 nicm 391: break;
392: grid_get_cell(gd, xx, yy, &gc);
1.135 nicm 393: if (xx + gc.data.width > px + nx)
394: break;
1.150 nicm 395: grid_view_set_cell(ctx->s->grid, cx, cy, &gc);
1.132 nicm 396: cx++;
397: }
1.1 nicm 398: cy++;
1.125 nicm 399: }
400: }
401:
1.129 nicm 402: /* Draw a horizontal line on screen. */
1.125 nicm 403: void
1.129 nicm 404: screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right)
1.125 nicm 405: {
406: struct screen *s = ctx->s;
407: struct grid_cell gc;
408: u_int cx, cy, i;
409:
410: cx = s->cx;
411: cy = s->cy;
412:
413: memcpy(&gc, &grid_default_cell, sizeof gc);
414: gc.attr |= GRID_ATTR_CHARSET;
415:
416: screen_write_putc(ctx, &gc, left ? 't' : 'q');
417: for (i = 1; i < nx - 1; i++)
418: screen_write_putc(ctx, &gc, 'q');
419: screen_write_putc(ctx, &gc, right ? 'u' : 'q');
1.129 nicm 420:
1.144 nicm 421: screen_write_set_cursor(ctx, cx, cy);
1.129 nicm 422: }
423:
424: /* Draw a horizontal line on screen. */
425: void
1.130 nicm 426: screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom)
1.129 nicm 427: {
428: struct screen *s = ctx->s;
429: struct grid_cell gc;
430: u_int cx, cy, i;
431:
432: cx = s->cx;
433: cy = s->cy;
434:
435: memcpy(&gc, &grid_default_cell, sizeof gc);
436: gc.attr |= GRID_ATTR_CHARSET;
437:
438: screen_write_putc(ctx, &gc, top ? 'w' : 'x');
439: for (i = 1; i < ny - 1; i++) {
1.144 nicm 440: screen_write_set_cursor(ctx, cx, cy + i);
1.129 nicm 441: screen_write_putc(ctx, &gc, 'x');
442: }
1.144 nicm 443: screen_write_set_cursor(ctx, cx, cy + ny - 1);
1.129 nicm 444: screen_write_putc(ctx, &gc, bottom ? 'v' : 'x');
1.152 nicm 445:
446: screen_write_set_cursor(ctx, cx, cy);
447: }
448:
449: /* Draw a menu on screen. */
450: void
1.161 nicm 451: screen_write_menu(struct screen_write_ctx *ctx, struct menu *menu,
452: int choice, const struct grid_cell *choice_gc)
1.152 nicm 453: {
454: struct screen *s = ctx->s;
1.161 nicm 455: struct grid_cell default_gc;
456: const struct grid_cell *gc = &default_gc;
1.152 nicm 457: u_int cx, cy, i, j;
1.153 nicm 458: const char *name;
1.152 nicm 459:
460: cx = s->cx;
461: cy = s->cy;
462:
1.161 nicm 463: memcpy(&default_gc, &grid_default_cell, sizeof default_gc);
1.152 nicm 464:
465: screen_write_box(ctx, menu->width + 4, menu->count + 2);
466: screen_write_cursormove(ctx, cx + 2, cy, 0);
1.161 nicm 467: format_draw(ctx, &default_gc, menu->width, menu->title, NULL);
1.152 nicm 468:
469: for (i = 0; i < menu->count; i++) {
1.153 nicm 470: name = menu->items[i].name;
471: if (name == NULL) {
1.152 nicm 472: screen_write_cursormove(ctx, cx, cy + 1 + i, 0);
473: screen_write_hline(ctx, menu->width + 4, 1, 1);
474: } else {
1.153 nicm 475: if (choice >= 0 && i == (u_int)choice && *name != '-')
1.161 nicm 476: gc = choice_gc;
1.152 nicm 477: screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
478: for (j = 0; j < menu->width; j++)
1.161 nicm 479: screen_write_putc(ctx, gc, ' ');
1.152 nicm 480: screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
1.153 nicm 481: if (*name == '-') {
482: name++;
1.161 nicm 483: default_gc.attr |= GRID_ATTR_DIM;
484: format_draw(ctx, gc, menu->width, name, NULL);
485: default_gc.attr &= ~GRID_ATTR_DIM;
1.153 nicm 486: } else
1.161 nicm 487: format_draw(ctx, gc, menu->width, name, NULL);
488: gc = &default_gc;
1.152 nicm 489: }
490: }
1.125 nicm 491:
1.144 nicm 492: screen_write_set_cursor(ctx, cx, cy);
1.125 nicm 493: }
494:
495: /* Draw a box on screen. */
496: void
497: screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny)
498: {
499: struct screen *s = ctx->s;
500: struct grid_cell gc;
501: u_int cx, cy, i;
502:
503: cx = s->cx;
504: cy = s->cy;
505:
506: memcpy(&gc, &grid_default_cell, sizeof gc);
507: gc.attr |= GRID_ATTR_CHARSET;
508:
509: screen_write_putc(ctx, &gc, 'l');
510: for (i = 1; i < nx - 1; i++)
511: screen_write_putc(ctx, &gc, 'q');
512: screen_write_putc(ctx, &gc, 'k');
513:
1.144 nicm 514: screen_write_set_cursor(ctx, cx, cy + ny - 1);
1.125 nicm 515: screen_write_putc(ctx, &gc, 'm');
516: for (i = 1; i < nx - 1; i++)
517: screen_write_putc(ctx, &gc, 'q');
518: screen_write_putc(ctx, &gc, 'j');
519:
520: for (i = 1; i < ny - 1; i++) {
1.144 nicm 521: screen_write_set_cursor(ctx, cx, cy + i);
1.125 nicm 522: screen_write_putc(ctx, &gc, 'x');
523: }
524: for (i = 1; i < ny - 1; i++) {
1.144 nicm 525: screen_write_set_cursor(ctx, cx + nx - 1, cy + i);
1.125 nicm 526: screen_write_putc(ctx, &gc, 'x');
527: }
528:
1.144 nicm 529: screen_write_set_cursor(ctx, cx, cy);
1.125 nicm 530: }
531:
1.132 nicm 532: /*
533: * Write a preview version of a window. Assumes target area is big enough and
534: * already cleared.
535: */
1.125 nicm 536: void
537: screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
538: u_int ny)
539: {
540: struct screen *s = ctx->s;
541: struct grid_cell gc;
542: u_int cx, cy, px, py;
543:
544: cx = s->cx;
545: cy = s->cy;
546:
547: /*
548: * If the cursor is on, pick the area around the cursor, otherwise use
549: * the top left.
550: */
551: if (src->mode & MODE_CURSOR) {
552: px = src->cx;
553: if (px < nx / 3)
554: px = 0;
555: else
556: px = px - nx / 3;
557: if (px + nx > screen_size_x(src)) {
558: if (nx > screen_size_x(src))
559: px = 0;
560: else
561: px = screen_size_x(src) - nx;
562: }
563: py = src->cy;
564: if (py < ny / 3)
565: py = 0;
566: else
567: py = py - ny / 3;
568: if (py + ny > screen_size_y(src)) {
569: if (ny > screen_size_y(src))
570: py = 0;
571: else
572: py = screen_size_y(src) - ny;
573: }
574: } else {
575: px = 0;
576: py = 0;
577: }
578:
1.132 nicm 579: screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
1.125 nicm 580:
581: if (src->mode & MODE_CURSOR) {
582: grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
583: gc.attr |= GRID_ATTR_REVERSE;
1.144 nicm 584: screen_write_set_cursor(ctx, cx + (src->cx - px),
1.125 nicm 585: cy + (src->cy - py));
586: screen_write_cell(ctx, &gc);
1.1 nicm 587: }
588: }
589:
1.61 nicm 590: /* Set a mode. */
591: void
592: screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
593: {
594: struct screen *s = ctx->s;
595:
596: s->mode |= mode;
597: }
598:
599: /* Clear a mode. */
600: void
601: screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
602: {
603: struct screen *s = ctx->s;
604:
605: s->mode &= ~mode;
606: }
607:
1.1 nicm 608: /* Cursor up by ny. */
609: void
610: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
611: {
612: struct screen *s = ctx->s;
1.139 nicm 613: u_int cx = s->cx, cy = s->cy;
1.1 nicm 614:
615: if (ny == 0)
616: ny = 1;
617:
1.139 nicm 618: if (cy < s->rupper) {
1.12 nicm 619: /* Above region. */
1.139 nicm 620: if (ny > cy)
621: ny = cy;
1.12 nicm 622: } else {
623: /* Below region. */
1.139 nicm 624: if (ny > cy - s->rupper)
625: ny = cy - s->rupper;
1.12 nicm 626: }
1.139 nicm 627: if (cx == screen_size_x(s))
628: cx--;
629:
630: cy -= ny;
1.1 nicm 631:
1.139 nicm 632: screen_write_set_cursor(ctx, cx, cy);
1.1 nicm 633: }
634:
635: /* Cursor down by ny. */
636: void
637: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
638: {
639: struct screen *s = ctx->s;
1.139 nicm 640: u_int cx = s->cx, cy = s->cy;
1.1 nicm 641:
642: if (ny == 0)
643: ny = 1;
644:
1.139 nicm 645: if (cy > s->rlower) {
1.12 nicm 646: /* Below region. */
1.139 nicm 647: if (ny > screen_size_y(s) - 1 - cy)
648: ny = screen_size_y(s) - 1 - cy;
1.12 nicm 649: } else {
650: /* Above region. */
1.139 nicm 651: if (ny > s->rlower - cy)
652: ny = s->rlower - cy;
1.12 nicm 653: }
1.139 nicm 654: if (cx == screen_size_x(s))
655: cx--;
656: else if (ny == 0)
1.1 nicm 657: return;
658:
1.139 nicm 659: cy += ny;
660:
661: screen_write_set_cursor(ctx, cx, cy);
1.1 nicm 662: }
663:
1.101 nicm 664: /* Cursor right by nx. */
1.1 nicm 665: void
666: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
667: {
668: struct screen *s = ctx->s;
1.139 nicm 669: u_int cx = s->cx, cy = s->cy;
1.1 nicm 670:
671: if (nx == 0)
672: nx = 1;
673:
1.139 nicm 674: if (nx > screen_size_x(s) - 1 - cx)
675: nx = screen_size_x(s) - 1 - cx;
1.1 nicm 676: if (nx == 0)
677: return;
678:
1.139 nicm 679: cx += nx;
680:
681: screen_write_set_cursor(ctx, cx, cy);
1.1 nicm 682: }
683:
684: /* Cursor left by nx. */
685: void
686: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
687: {
688: struct screen *s = ctx->s;
1.139 nicm 689: u_int cx = s->cx, cy = s->cy;
1.1 nicm 690:
691: if (nx == 0)
692: nx = 1;
693:
1.139 nicm 694: if (nx > cx)
695: nx = cx;
1.1 nicm 696: if (nx == 0)
697: return;
698:
1.139 nicm 699: cx -= nx;
700:
701: screen_write_set_cursor(ctx, cx, cy);
1.5 nicm 702: }
703:
1.29 nicm 704: /* Backspace; cursor left unless at start of wrapped line when can move up. */
705: void
706: screen_write_backspace(struct screen_write_ctx *ctx)
707: {
708: struct screen *s = ctx->s;
709: struct grid_line *gl;
1.139 nicm 710: u_int cx = s->cx, cy = s->cy;
1.29 nicm 711:
1.139 nicm 712: if (cx == 0) {
713: if (cy == 0)
1.29 nicm 714: return;
1.139 nicm 715: gl = grid_get_line(s->grid, s->grid->hsize + cy - 1);
1.29 nicm 716: if (gl->flags & GRID_LINE_WRAPPED) {
1.139 nicm 717: cy--;
718: cx = screen_size_x(s) - 1;
1.29 nicm 719: }
720: } else
1.139 nicm 721: cx--;
722:
723: screen_write_set_cursor(ctx, cx, cy);
1.29 nicm 724: }
725:
1.5 nicm 726: /* VT100 alignment test. */
727: void
728: screen_write_alignmenttest(struct screen_write_ctx *ctx)
729: {
730: struct screen *s = ctx->s;
1.17 nicm 731: struct tty_ctx ttyctx;
1.5 nicm 732: struct grid_cell gc;
733: u_int xx, yy;
734:
735: memcpy(&gc, &grid_default_cell, sizeof gc);
1.77 nicm 736: utf8_set(&gc.data, 'E');
1.7 ray 737:
1.5 nicm 738: for (yy = 0; yy < screen_size_y(s); yy++) {
739: for (xx = 0; xx < screen_size_x(s); xx++)
740: grid_view_set_cell(s->grid, xx, yy, &gc);
741: }
1.7 ray 742:
1.139 nicm 743: screen_write_set_cursor(ctx, 0, 0);
1.5 nicm 744:
745: s->rupper = 0;
746: s->rlower = screen_size_y(s) - 1;
1.143 nicm 747:
1.163 nicm 748: screen_write_initctx(ctx, &ttyctx, 1);
1.5 nicm 749:
1.109 nicm 750: screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1.17 nicm 751: tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1 nicm 752: }
753:
754: /* Insert nx characters. */
755: void
1.99 nicm 756: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1 nicm 757: {
758: struct screen *s = ctx->s;
1.17 nicm 759: struct tty_ctx ttyctx;
1.1 nicm 760:
761: if (nx == 0)
762: nx = 1;
763:
1.9 nicm 764: if (nx > screen_size_x(s) - s->cx)
765: nx = screen_size_x(s) - s->cx;
1.1 nicm 766: if (nx == 0)
767: return;
768:
1.107 nicm 769: if (s->cx > screen_size_x(s) - 1)
770: return;
771:
1.163 nicm 772: screen_write_initctx(ctx, &ttyctx, 0);
1.107 nicm 773: ttyctx.bg = bg;
1.1 nicm 774:
1.107 nicm 775: grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1.1 nicm 776:
1.164 nicm 777: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 778: ttyctx.num = nx;
779: tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1 nicm 780: }
781:
782: /* Delete nx characters. */
783: void
1.99 nicm 784: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1 nicm 785: {
786: struct screen *s = ctx->s;
1.17 nicm 787: struct tty_ctx ttyctx;
1.1 nicm 788:
789: if (nx == 0)
790: nx = 1;
791:
1.9 nicm 792: if (nx > screen_size_x(s) - s->cx)
793: nx = screen_size_x(s) - s->cx;
1.1 nicm 794: if (nx == 0)
795: return;
796:
1.107 nicm 797: if (s->cx > screen_size_x(s) - 1)
798: return;
799:
1.163 nicm 800: screen_write_initctx(ctx, &ttyctx, 0);
1.107 nicm 801: ttyctx.bg = bg;
1.1 nicm 802:
1.107 nicm 803: grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1.1 nicm 804:
1.164 nicm 805: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 806: ttyctx.num = nx;
807: tty_write(tty_cmd_deletecharacter, &ttyctx);
1.59 nicm 808: }
809:
810: /* Clear nx characters. */
811: void
1.121 nicm 812: screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.59 nicm 813: {
814: struct screen *s = ctx->s;
815: struct tty_ctx ttyctx;
816:
817: if (nx == 0)
818: nx = 1;
819:
820: if (nx > screen_size_x(s) - s->cx)
821: nx = screen_size_x(s) - s->cx;
822: if (nx == 0)
823: return;
824:
1.107 nicm 825: if (s->cx > screen_size_x(s) - 1)
826: return;
827:
1.163 nicm 828: screen_write_initctx(ctx, &ttyctx, 0);
1.121 nicm 829: ttyctx.bg = bg;
1.59 nicm 830:
1.124 nicm 831: grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
1.59 nicm 832:
1.164 nicm 833: screen_write_collect_flush(ctx, 0, __func__);
1.59 nicm 834: ttyctx.num = nx;
835: tty_write(tty_cmd_clearcharacter, &ttyctx);
1.1 nicm 836: }
837:
838: /* Insert ny lines. */
839: void
1.99 nicm 840: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1 nicm 841: {
842: struct screen *s = ctx->s;
1.107 nicm 843: struct grid *gd = s->grid;
1.17 nicm 844: struct tty_ctx ttyctx;
1.1 nicm 845:
846: if (ny == 0)
847: ny = 1;
848:
1.11 nicm 849: if (s->cy < s->rupper || s->cy > s->rlower) {
850: if (ny > screen_size_y(s) - s->cy)
851: ny = screen_size_y(s) - s->cy;
852: if (ny == 0)
853: return;
854:
1.163 nicm 855: screen_write_initctx(ctx, &ttyctx, 1);
1.107 nicm 856: ttyctx.bg = bg;
1.11 nicm 857:
1.107 nicm 858: grid_view_insert_lines(gd, s->cy, ny, bg);
1.11 nicm 859:
1.164 nicm 860: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 861: ttyctx.num = ny;
862: tty_write(tty_cmd_insertline, &ttyctx);
1.11 nicm 863: return;
864: }
865:
866: if (ny > s->rlower + 1 - s->cy)
867: ny = s->rlower + 1 - s->cy;
1.1 nicm 868: if (ny == 0)
869: return;
1.41 nicm 870:
1.163 nicm 871: screen_write_initctx(ctx, &ttyctx, 1);
1.107 nicm 872: ttyctx.bg = bg;
1.1 nicm 873:
874: if (s->cy < s->rupper || s->cy > s->rlower)
1.107 nicm 875: grid_view_insert_lines(gd, s->cy, ny, bg);
876: else
877: grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1 nicm 878:
1.164 nicm 879: screen_write_collect_flush(ctx, 0, __func__);
880:
1.17 nicm 881: ttyctx.num = ny;
882: tty_write(tty_cmd_insertline, &ttyctx);
1.1 nicm 883: }
884:
885: /* Delete ny lines. */
886: void
1.99 nicm 887: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1 nicm 888: {
889: struct screen *s = ctx->s;
1.107 nicm 890: struct grid *gd = s->grid;
1.17 nicm 891: struct tty_ctx ttyctx;
1.1 nicm 892:
893: if (ny == 0)
894: ny = 1;
895:
1.11 nicm 896: if (s->cy < s->rupper || s->cy > s->rlower) {
897: if (ny > screen_size_y(s) - s->cy)
898: ny = screen_size_y(s) - s->cy;
899: if (ny == 0)
900: return;
901:
1.163 nicm 902: screen_write_initctx(ctx, &ttyctx, 1);
1.107 nicm 903: ttyctx.bg = bg;
1.11 nicm 904:
1.107 nicm 905: grid_view_delete_lines(gd, s->cy, ny, bg);
1.11 nicm 906:
1.164 nicm 907: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 908: ttyctx.num = ny;
909: tty_write(tty_cmd_deleteline, &ttyctx);
1.11 nicm 910: return;
911: }
1.41 nicm 912:
1.11 nicm 913: if (ny > s->rlower + 1 - s->cy)
914: ny = s->rlower + 1 - s->cy;
1.1 nicm 915: if (ny == 0)
916: return;
917:
1.163 nicm 918: screen_write_initctx(ctx, &ttyctx, 1);
1.107 nicm 919: ttyctx.bg = bg;
1.1 nicm 920:
921: if (s->cy < s->rupper || s->cy > s->rlower)
1.107 nicm 922: grid_view_delete_lines(gd, s->cy, ny, bg);
923: else
924: grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1 nicm 925:
1.164 nicm 926: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 927: ttyctx.num = ny;
928: tty_write(tty_cmd_deleteline, &ttyctx);
1.1 nicm 929: }
930:
931: /* Clear line at cursor. */
932: void
1.99 nicm 933: screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 934: {
1.165 ! nicm 935: struct screen *s = ctx->s;
! 936: struct grid_line *gl;
! 937: u_int sx = screen_size_x(s);
! 938: struct screen_write_collect_item *ci = ctx->item;
1.1 nicm 939:
1.137 nicm 940: gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.140 nicm 941: if (gl->cellsize == 0 && COLOUR_DEFAULT(bg))
1.92 nicm 942: return;
1.1 nicm 943:
1.99 nicm 944: grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
945:
1.109 nicm 946: screen_write_collect_clear(ctx, s->cy, 1);
1.165 ! nicm 947: ci->x = 0;
! 948: ci->type = CLEAR;
! 949: ci->bg = bg;
! 950: TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
! 951: ctx->item = xcalloc(1, sizeof *ctx->item);
1.1 nicm 952: }
953:
954: /* Clear to end of line from cursor. */
955: void
1.99 nicm 956: screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 957: {
1.165 ! nicm 958: struct screen *s = ctx->s;
! 959: struct grid_line *gl;
! 960: struct tty_ctx ttyctx;
! 961: u_int sx = screen_size_x(s);
! 962: struct screen_write_collect_item *ci = ctx->item;
! 963:
! 964: if (s->cx == 0) {
! 965: screen_write_clearline(ctx, bg);
! 966: return;
! 967: }
1.1 nicm 968:
1.137 nicm 969: gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.140 nicm 970: if (s->cx > sx - 1 || (s->cx >= gl->cellsize && COLOUR_DEFAULT(bg)))
1.92 nicm 971: return;
1.108 nicm 972:
1.163 nicm 973: screen_write_initctx(ctx, &ttyctx, 1);
1.108 nicm 974: ttyctx.bg = bg;
1.1 nicm 975:
1.99 nicm 976: grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
977:
1.165 ! nicm 978: if (!screen_write_collect_clear_end(ctx, s->cy, s->cx, bg)) {
! 979: ci->x = s->cx;
! 980: ci->type = CLEAR_END;
! 981: ci->bg = bg;
! 982: TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
! 983: ctx->item = xcalloc(1, sizeof *ctx->item);
! 984: }
1.1 nicm 985: }
986:
987: /* Clear to start of line from cursor. */
988: void
1.99 nicm 989: screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 990: {
1.165 ! nicm 991: struct screen *s = ctx->s;
! 992: struct tty_ctx ttyctx;
! 993: u_int sx = screen_size_x(s);
! 994: struct screen_write_collect_item *ci = ctx->item;
! 995:
! 996: if (s->cx >= sx - 1) {
! 997: screen_write_clearline(ctx, bg);
! 998: return;
! 999: }
1.1 nicm 1000:
1.163 nicm 1001: screen_write_initctx(ctx, &ttyctx, 1);
1.99 nicm 1002: ttyctx.bg = bg;
1.1 nicm 1003:
1.109 nicm 1004: if (s->cx > sx - 1)
1.99 nicm 1005: grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109 nicm 1006: else
1.99 nicm 1007: grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1 nicm 1008:
1.165 ! nicm 1009: if (!screen_write_collect_clear_start(ctx, s->cy, s->cx, bg)) {
! 1010: ci->x = s->cx;
! 1011: ci->type = CLEAR_START;
! 1012: ci->bg = bg;
! 1013: TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
! 1014: ctx->item = xcalloc(1, sizeof *ctx->item);
! 1015: }
1.1 nicm 1016: }
1017:
1.101 nicm 1018: /* Move cursor to px,py. */
1.1 nicm 1019: void
1.147 nicm 1020: screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py,
1021: int origin)
1.1 nicm 1022: {
1023: struct screen *s = ctx->s;
1024:
1.147 nicm 1025: if (origin && py != -1 && (s->mode & MODE_ORIGIN)) {
1.146 nicm 1026: if ((u_int)py > s->rlower - s->rupper)
1.144 nicm 1027: py = s->rlower;
1028: else
1029: py += s->rupper;
1030: }
1.145 nicm 1031:
1.146 nicm 1032: if (px != -1 && (u_int)px > screen_size_x(s) - 1)
1.145 nicm 1033: px = screen_size_x(s) - 1;
1.146 nicm 1034: if (py != -1 && (u_int)py > screen_size_y(s) - 1)
1.145 nicm 1035: py = screen_size_y(s) - 1;
1.1 nicm 1036:
1.139 nicm 1037: screen_write_set_cursor(ctx, px, py);
1.1 nicm 1038: }
1039:
1.101 nicm 1040: /* Reverse index (up with scroll). */
1.1 nicm 1041: void
1.122 nicm 1042: screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1043: {
1044: struct screen *s = ctx->s;
1.17 nicm 1045: struct tty_ctx ttyctx;
1.1 nicm 1046:
1.165 ! nicm 1047: if (s->cy == s->rupper) {
! 1048: grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
! 1049: screen_write_collect_flush(ctx, 0, __func__);
! 1050:
! 1051: screen_write_initctx(ctx, &ttyctx, 1);
! 1052: ttyctx.bg = bg;
1.1 nicm 1053:
1.165 ! nicm 1054: tty_write(tty_cmd_reverseindex, &ttyctx);
! 1055: } else if (s->cy > 0)
1.139 nicm 1056: screen_write_set_cursor(ctx, -1, s->cy - 1);
1.1 nicm 1057:
1058: }
1059:
1060: /* Set scroll region. */
1061: void
1.83 nicm 1062: screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
1063: u_int rlower)
1.1 nicm 1064: {
1065: struct screen *s = ctx->s;
1066:
1067: if (rupper > screen_size_y(s) - 1)
1068: rupper = screen_size_y(s) - 1;
1069: if (rlower > screen_size_y(s) - 1)
1070: rlower = screen_size_y(s) - 1;
1.13 nicm 1071: if (rupper >= rlower) /* cannot be one line */
1.1 nicm 1072: return;
1073:
1.164 nicm 1074: screen_write_collect_flush(ctx, 0, __func__);
1.110 nicm 1075:
1.1 nicm 1076: /* Cursor moves to top-left. */
1.139 nicm 1077: screen_write_set_cursor(ctx, 0, 0);
1.1 nicm 1078:
1079: s->rupper = rupper;
1080: s->rlower = rlower;
1081: }
1082:
1.34 nicm 1083: /* Line feed. */
1.1 nicm 1084: void
1.122 nicm 1085: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1.1 nicm 1086: {
1.20 nicm 1087: struct screen *s = ctx->s;
1.109 nicm 1088: struct grid *gd = s->grid;
1.20 nicm 1089: struct grid_line *gl;
1.1 nicm 1090:
1.137 nicm 1091: gl = grid_get_line(gd, gd->hsize + s->cy);
1.20 nicm 1092: if (wrapped)
1093: gl->flags |= GRID_LINE_WRAPPED;
1.73 nicm 1094: else
1095: gl->flags &= ~GRID_LINE_WRAPPED;
1.20 nicm 1096:
1.114 nicm 1097: log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1098: s->rupper, s->rlower);
1099:
1.122 nicm 1100: if (bg != ctx->bg) {
1.164 nicm 1101: screen_write_collect_flush(ctx, 1, __func__);
1.122 nicm 1102: ctx->bg = bg;
1103: }
1104:
1.92 nicm 1105: if (s->cy == s->rlower) {
1.122 nicm 1106: grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1.109 nicm 1107: screen_write_collect_scroll(ctx);
1.110 nicm 1108: ctx->scrolled++;
1.109 nicm 1109: } else if (s->cy < screen_size_y(s) - 1)
1.139 nicm 1110: screen_write_set_cursor(ctx, -1, s->cy + 1);
1.1 nicm 1111: }
1112:
1.110 nicm 1113: /* Scroll up. */
1114: void
1.122 nicm 1115: screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1.110 nicm 1116: {
1117: struct screen *s = ctx->s;
1118: struct grid *gd = s->grid;
1119: u_int i;
1120:
1121: if (lines == 0)
1122: lines = 1;
1123: else if (lines > s->rlower - s->rupper + 1)
1124: lines = s->rlower - s->rupper + 1;
1125:
1.122 nicm 1126: if (bg != ctx->bg) {
1.164 nicm 1127: screen_write_collect_flush(ctx, 1, __func__);
1.122 nicm 1128: ctx->bg = bg;
1129: }
1130:
1.110 nicm 1131: for (i = 0; i < lines; i++) {
1.122 nicm 1132: grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1.110 nicm 1133: screen_write_collect_scroll(ctx);
1134: }
1135: ctx->scrolled += lines;
1.157 nicm 1136: }
1137:
1138: /* Scroll down. */
1139: void
1140: screen_write_scrolldown(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1141: {
1142: struct screen *s = ctx->s;
1143: struct grid *gd = s->grid;
1144: struct tty_ctx ttyctx;
1145: u_int i;
1146:
1.163 nicm 1147: screen_write_initctx(ctx, &ttyctx, 1);
1.157 nicm 1148: ttyctx.bg = bg;
1149:
1150: if (lines == 0)
1151: lines = 1;
1152: else if (lines > s->rlower - s->rupper + 1)
1153: lines = s->rlower - s->rupper + 1;
1154:
1155: for (i = 0; i < lines; i++)
1156: grid_view_scroll_region_down(gd, s->rupper, s->rlower, bg);
1157:
1.164 nicm 1158: screen_write_collect_flush(ctx, 0, __func__);
1.157 nicm 1159: ttyctx.num = lines;
1160: tty_write(tty_cmd_scrolldown, &ttyctx);
1.110 nicm 1161: }
1162:
1.1 nicm 1163: /* Carriage return (cursor to start of line). */
1164: void
1165: screen_write_carriagereturn(struct screen_write_ctx *ctx)
1166: {
1.139 nicm 1167: screen_write_set_cursor(ctx, 0, -1);
1.1 nicm 1168: }
1169:
1170: /* Clear to end of screen from cursor. */
1171: void
1.99 nicm 1172: screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1173: {
1174: struct screen *s = ctx->s;
1.107 nicm 1175: struct grid *gd = s->grid;
1.17 nicm 1176: struct tty_ctx ttyctx;
1.92 nicm 1177: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 1178:
1.163 nicm 1179: screen_write_initctx(ctx, &ttyctx, 1);
1.99 nicm 1180: ttyctx.bg = bg;
1.1 nicm 1181:
1.46 nicm 1182: /* Scroll into history if it is enabled and clearing entire screen. */
1.109 nicm 1183: if (s->cx == 0 && s->cy == 0 && (gd->flags & GRID_HISTORY))
1.107 nicm 1184: grid_view_clear_history(gd, bg);
1.109 nicm 1185: else {
1186: if (s->cx <= sx - 1)
1.107 nicm 1187: grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
1188: grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1.46 nicm 1189: }
1.1 nicm 1190:
1.109 nicm 1191: screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1.164 nicm 1192: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 1193: tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1 nicm 1194: }
1195:
1196: /* Clear to start of screen. */
1197: void
1.105 nicm 1198: screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1199: {
1200: struct screen *s = ctx->s;
1.17 nicm 1201: struct tty_ctx ttyctx;
1.92 nicm 1202: u_int sx = screen_size_x(s);
1.1 nicm 1203:
1.163 nicm 1204: screen_write_initctx(ctx, &ttyctx, 1);
1.105 nicm 1205: ttyctx.bg = bg;
1.1 nicm 1206:
1.109 nicm 1207: if (s->cy > 0)
1.105 nicm 1208: grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1.109 nicm 1209: if (s->cx > sx - 1)
1.120 nicm 1210: grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109 nicm 1211: else
1.120 nicm 1212: grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1 nicm 1213:
1.109 nicm 1214: screen_write_collect_clear(ctx, 0, s->cy);
1.164 nicm 1215: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 1216: tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1 nicm 1217: }
1218:
1219: /* Clear entire screen. */
1220: void
1.99 nicm 1221: screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1222: {
1223: struct screen *s = ctx->s;
1.17 nicm 1224: struct tty_ctx ttyctx;
1.92 nicm 1225: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 1226:
1.163 nicm 1227: screen_write_initctx(ctx, &ttyctx, 1);
1.99 nicm 1228: ttyctx.bg = bg;
1.1 nicm 1229:
1.46 nicm 1230: /* Scroll into history if it is enabled. */
1231: if (s->grid->flags & GRID_HISTORY)
1.99 nicm 1232: grid_view_clear_history(s->grid, bg);
1.83 nicm 1233: else
1.99 nicm 1234: grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1.1 nicm 1235:
1.109 nicm 1236: screen_write_collect_clear(ctx, 0, sy);
1.17 nicm 1237: tty_write(tty_cmd_clearscreen, &ttyctx);
1.51 nicm 1238: }
1239:
1240: /* Clear entire history. */
1241: void
1242: screen_write_clearhistory(struct screen_write_ctx *ctx)
1243: {
1.156 nicm 1244: grid_clear_history(ctx->s->grid);
1.1 nicm 1245: }
1246:
1.165 ! nicm 1247: /* Clear to start of a collected line. */
! 1248: static int
! 1249: screen_write_collect_clear_start(struct screen_write_ctx *ctx, u_int y, u_int x,
! 1250: u_int bg)
! 1251: {
! 1252: struct screen_write_collect_item *ci, *tmp;
! 1253: size_t size = 0;
! 1254: u_int items = 0;
! 1255: int redundant = 0;
! 1256:
! 1257: if (TAILQ_EMPTY(&ctx->list[y].items))
! 1258: return (0);
! 1259: TAILQ_FOREACH_SAFE(ci, &ctx->list[y].items, entry, tmp) {
! 1260: switch (ci->type) {
! 1261: case CLEAR:
! 1262: continue;
! 1263: case CLEAR_START:
! 1264: if (ci->x >= x) {
! 1265: if (ci->bg == bg)
! 1266: redundant = 1;
! 1267: continue;
! 1268: }
! 1269: break;
! 1270: case CLEAR_END:
! 1271: if (ci->x <= x)
! 1272: ci->x = x;
! 1273: continue;
! 1274: case TEXT:
! 1275: if (ci->x > x)
! 1276: continue;
! 1277: break;
! 1278: }
! 1279: items++;
! 1280: size += ci->used;
! 1281: TAILQ_REMOVE(&ctx->list[y].items, ci, entry);
! 1282: free(ci);
! 1283: }
! 1284: ctx->skipped += size;
! 1285: log_debug("%s: dropped %u items (%zu bytes) (line %u)", __func__, items,
! 1286: size, y);
! 1287: return (redundant);
! 1288: }
! 1289:
! 1290: /* Clear to end of a collected line. */
! 1291: static int
! 1292: screen_write_collect_clear_end(struct screen_write_ctx *ctx, u_int y, u_int x,
! 1293: u_int bg)
! 1294: {
! 1295: struct screen_write_collect_item *ci, *tmp;
! 1296: size_t size = 0;
! 1297: int redundant = 0;
! 1298: u_int items = 0;
! 1299:
! 1300: if (TAILQ_EMPTY(&ctx->list[y].items))
! 1301: return (0);
! 1302: TAILQ_FOREACH_SAFE(ci, &ctx->list[y].items, entry, tmp) {
! 1303: switch (ci->type) {
! 1304: case CLEAR:
! 1305: continue;
! 1306: case CLEAR_START:
! 1307: if (ci->x >= x)
! 1308: ci->x = x;
! 1309: continue;
! 1310: case CLEAR_END:
! 1311: if (ci->x <= x) {
! 1312: if (ci->bg == bg)
! 1313: redundant = 1;
! 1314: continue;
! 1315: }
! 1316: break;
! 1317: case TEXT:
! 1318: if (ci->x < x)
! 1319: continue;
! 1320: break;
! 1321: }
! 1322: items++;
! 1323: size += ci->used;
! 1324: TAILQ_REMOVE(&ctx->list[y].items, ci, entry);
! 1325: free(ci);
! 1326: }
! 1327: ctx->skipped += size;
! 1328: log_debug("%s: dropped %u items (%zu bytes) (line %u)", __func__, items,
! 1329: size, y);
! 1330: return (redundant);
! 1331: }
! 1332:
! 1333: /* Clear part of a collected line. */
1.109 nicm 1334: static void
1335: screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
1336: {
1337: struct screen_write_collect_item *ci, *tmp;
1.165 ! nicm 1338: u_int i, items;
1.109 nicm 1339: size_t size;
1340:
1.151 nicm 1341: for (i = y; i < y + n; i++) {
1.109 nicm 1342: if (TAILQ_EMPTY(&ctx->list[i].items))
1343: continue;
1.165 ! nicm 1344: items = 0;
1.109 nicm 1345: size = 0;
1346: TAILQ_FOREACH_SAFE(ci, &ctx->list[i].items, entry, tmp) {
1.165 ! nicm 1347: items++;
1.109 nicm 1348: size += ci->used;
1349: TAILQ_REMOVE(&ctx->list[i].items, ci, entry);
1350: free(ci);
1351: }
1352: ctx->skipped += size;
1.165 ! nicm 1353: log_debug("%s: dropped %u items (%zu bytes) (line %u)",
! 1354: __func__, items, size, y);
1.109 nicm 1355: }
1356: }
1357:
1358: /* Scroll collected lines up. */
1359: static void
1360: screen_write_collect_scroll(struct screen_write_ctx *ctx)
1361: {
1362: struct screen *s = ctx->s;
1363: struct screen_write_collect_line *cl;
1364: u_int y;
1365:
1.114 nicm 1366: log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
1367: s->rupper, s->rlower);
1368:
1.109 nicm 1369: screen_write_collect_clear(ctx, s->rupper, 1);
1370: for (y = s->rupper; y < s->rlower; y++) {
1371: cl = &ctx->list[y + 1];
1372: TAILQ_CONCAT(&ctx->list[y].items, &cl->items, entry);
1373: }
1374: }
1375:
1376: /* Flush collected lines. */
1377: static void
1.164 nicm 1378: screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only,
1379: const char *from)
1.109 nicm 1380: {
1381: struct screen *s = ctx->s;
1382: struct screen_write_collect_item *ci, *tmp;
1.116 nicm 1383: u_int y, cx, cy, items = 0;
1.109 nicm 1384: struct tty_ctx ttyctx;
1.116 nicm 1385: size_t written = 0;
1.110 nicm 1386:
1387: if (ctx->scrolled != 0) {
1388: log_debug("%s: scrolled %u (region %u-%u)", __func__,
1389: ctx->scrolled, s->rupper, s->rlower);
1390: if (ctx->scrolled > s->rlower - s->rupper + 1)
1391: ctx->scrolled = s->rlower - s->rupper + 1;
1392:
1.163 nicm 1393: screen_write_initctx(ctx, &ttyctx, 1);
1.110 nicm 1394: ttyctx.num = ctx->scrolled;
1.122 nicm 1395: ttyctx.bg = ctx->bg;
1.110 nicm 1396: tty_write(tty_cmd_scrollup, &ttyctx);
1397: }
1398: ctx->scrolled = 0;
1.122 nicm 1399: ctx->bg = 8;
1400:
1.111 nicm 1401: if (scroll_only)
1402: return;
1.109 nicm 1403:
1404: cx = s->cx; cy = s->cy;
1405: for (y = 0; y < screen_size_y(s); y++) {
1406: TAILQ_FOREACH_SAFE(ci, &ctx->list[y].items, entry, tmp) {
1.144 nicm 1407: screen_write_set_cursor(ctx, ci->x, y);
1.163 nicm 1408: screen_write_initctx(ctx, &ttyctx, 0);
1.165 ! nicm 1409: if (ci->type == CLEAR) {
! 1410: ttyctx.bg = ci->bg;
! 1411: tty_write(tty_cmd_clearline, &ttyctx);
! 1412: } else if (ci->type == CLEAR_END) {
! 1413: ttyctx.bg = ci->bg;
! 1414: tty_write(tty_cmd_clearendofline, &ttyctx);
! 1415: } else if (ci->type == CLEAR_START) {
! 1416: ttyctx.bg = ci->bg;
! 1417: tty_write(tty_cmd_clearstartofline, &ttyctx);
! 1418: } else {
! 1419: ttyctx.cell = &ci->gc;
! 1420: ttyctx.wrapped = ci->wrapped;
! 1421: ttyctx.ptr = ci->data;
! 1422: ttyctx.num = ci->used;
! 1423: tty_write(tty_cmd_cells, &ttyctx);
! 1424: }
1.116 nicm 1425:
1426: items++;
1427: written += ci->used;
1.109 nicm 1428:
1429: TAILQ_REMOVE(&ctx->list[y].items, ci, entry);
1430: free(ci);
1431: }
1432: }
1433: s->cx = cx; s->cy = cy;
1.116 nicm 1434:
1.164 nicm 1435: log_debug("%s: flushed %u items (%zu bytes) (%s)", __func__, items,
1436: written, from);
1.116 nicm 1437: ctx->written += written;
1.109 nicm 1438: }
1439:
1440: /* Finish and store collected cells. */
1441: void
1442: screen_write_collect_end(struct screen_write_ctx *ctx)
1443: {
1444: struct screen *s = ctx->s;
1445: struct screen_write_collect_item *ci = ctx->item;
1446: struct grid_cell gc;
1.131 nicm 1447: u_int xx;
1.109 nicm 1448:
1449: if (ci->used == 0)
1450: return;
1451: ci->data[ci->used] = '\0';
1452:
1453: ci->x = s->cx;
1454: TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
1455: ctx->item = xcalloc(1, sizeof *ctx->item);
1456:
1457: log_debug("%s: %u %s (at %u,%u)", __func__, ci->used, ci->data, s->cx,
1458: s->cy);
1459:
1.131 nicm 1460: if (s->cx != 0) {
1461: for (xx = s->cx; xx > 0; xx--) {
1462: grid_view_get_cell(s->grid, xx, s->cy, &gc);
1463: if (~gc.flags & GRID_FLAG_PADDING)
1464: break;
1.136 nicm 1465: grid_view_set_cell(s->grid, xx, s->cy,
1466: &grid_default_cell);
1.131 nicm 1467: }
1.139 nicm 1468: if (gc.data.width > 1) {
1.136 nicm 1469: grid_view_set_cell(s->grid, xx, s->cy,
1470: &grid_default_cell);
1.139 nicm 1471: }
1.131 nicm 1472: }
1473:
1.159 nicm 1474: grid_view_set_cells(s->grid, s->cx, s->cy, &ci->gc, ci->data, ci->used);
1.139 nicm 1475: screen_write_set_cursor(ctx, s->cx + ci->used, -1);
1.131 nicm 1476:
1477: for (xx = s->cx; xx < screen_size_x(s); xx++) {
1478: grid_view_get_cell(s->grid, xx, s->cy, &gc);
1479: if (~gc.flags & GRID_FLAG_PADDING)
1480: break;
1481: grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
1482: }
1.109 nicm 1483: }
1484:
1485: /* Write cell data, collecting if necessary. */
1486: void
1487: screen_write_collect_add(struct screen_write_ctx *ctx,
1488: const struct grid_cell *gc)
1489: {
1490: struct screen *s = ctx->s;
1491: struct screen_write_collect_item *ci;
1492: u_int sx = screen_size_x(s);
1493: int collect;
1494:
1495: /*
1496: * Don't need to check that the attributes and whatnot are still the
1.116 nicm 1497: * same - input_parse will end the collection when anything that isn't
1.159 nicm 1498: * a plain character is encountered.
1.109 nicm 1499: */
1500:
1501: collect = 1;
1.136 nicm 1502: if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f)
1.109 nicm 1503: collect = 0;
1504: else if (gc->attr & GRID_ATTR_CHARSET)
1505: collect = 0;
1506: else if (~s->mode & MODE_WRAP)
1507: collect = 0;
1508: else if (s->mode & MODE_INSERT)
1509: collect = 0;
1.138 nicm 1510: else if (s->sel != NULL)
1.109 nicm 1511: collect = 0;
1512: if (!collect) {
1513: screen_write_collect_end(ctx);
1.164 nicm 1514: screen_write_collect_flush(ctx, 0, __func__);
1.109 nicm 1515: screen_write_cell(ctx, gc);
1516: return;
1517: }
1518: ctx->cells++;
1519:
1520: if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
1521: screen_write_collect_end(ctx);
1.118 nicm 1522: ci = ctx->item; /* may have changed */
1523:
1.109 nicm 1524: if (s->cx > sx - 1) {
1.114 nicm 1525: log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.118 nicm 1526: ci->wrapped = 1;
1.122 nicm 1527: screen_write_linefeed(ctx, 1, 8);
1.139 nicm 1528: screen_write_set_cursor(ctx, 0, -1);
1.109 nicm 1529: }
1530:
1531: if (ci->used == 0)
1532: memcpy(&ci->gc, gc, sizeof ci->gc);
1533: ci->data[ci->used++] = gc->data.data[0];
1534: if (ci->used == (sizeof ci->data) - 1)
1535: screen_write_collect_end(ctx);
1536: }
1537:
1.1 nicm 1538: /* Write cell data. */
1539: void
1.60 nicm 1540: screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1.1 nicm 1541: {
1542: struct screen *s = ctx->s;
1543: struct grid *gd = s->grid;
1.109 nicm 1544: struct grid_line *gl;
1545: struct grid_cell_entry *gce;
1546: struct grid_cell tmp_gc, now_gc;
1.15 nicm 1547: struct tty_ctx ttyctx;
1.92 nicm 1548: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.115 nicm 1549: u_int width = gc->data.width, xx, last, cx, cy;
1.109 nicm 1550: int selected, skip = 1;
1.1 nicm 1551:
1.109 nicm 1552: /* Ignore padding cells. */
1.1 nicm 1553: if (gc->flags & GRID_FLAG_PADDING)
1554: return;
1.109 nicm 1555: ctx->cells++;
1.32 nicm 1556:
1.109 nicm 1557: /* If the width is zero, combine onto the previous character. */
1.1 nicm 1558: if (width == 0) {
1.164 nicm 1559: screen_write_collect_flush(ctx, 0, __func__);
1.104 nicm 1560: if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) {
1.115 nicm 1561: cx = s->cx; cy = s->cy;
1.144 nicm 1562: screen_write_set_cursor(ctx, xx, s->cy);
1.163 nicm 1563: screen_write_initctx(ctx, &ttyctx, 0);
1.104 nicm 1564: ttyctx.cell = gc;
1565: tty_write(tty_cmd_cell, &ttyctx);
1.115 nicm 1566: s->cx = cx; s->cy = cy;
1.1 nicm 1567: }
1568: return;
1569: }
1.112 nicm 1570:
1571: /* Flush any existing scrolling. */
1.164 nicm 1572: screen_write_collect_flush(ctx, 1, __func__);
1.1 nicm 1573:
1.109 nicm 1574: /* If this character doesn't fit, ignore it. */
1575: if ((~s->mode & MODE_WRAP) &&
1576: width > 1 &&
1577: (width > sx || (s->cx != sx && s->cx > sx - width)))
1578: return;
1579:
1.6 nicm 1580: /* If in insert mode, make space for the cells. */
1.98 nicm 1581: if (s->mode & MODE_INSERT) {
1.109 nicm 1582: grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
1583: skip = 0;
1584: }
1.6 nicm 1585:
1.20 nicm 1586: /* Check this will fit on the current line and wrap if not. */
1.92 nicm 1587: if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1.128 nicm 1588: log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.122 nicm 1589: screen_write_linefeed(ctx, 1, 8);
1.139 nicm 1590: screen_write_set_cursor(ctx, 0, -1);
1.164 nicm 1591: screen_write_collect_flush(ctx, 1, __func__);
1.1 nicm 1592: }
1593:
1.64 nicm 1594: /* Sanity check cursor position. */
1.92 nicm 1595: if (s->cx > sx - width || s->cy > sy - 1)
1.1 nicm 1596: return;
1.163 nicm 1597: screen_write_initctx(ctx, &ttyctx, 0);
1.106 nicm 1598:
1.1 nicm 1599: /* Handle overwriting of UTF-8 characters. */
1.137 nicm 1600: gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.92 nicm 1601: if (gl->flags & GRID_LINE_EXTENDED) {
1602: grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
1603: if (screen_write_overwrite(ctx, &now_gc, width))
1604: skip = 0;
1605: }
1.1 nicm 1606:
1607: /*
1608: * If the new character is UTF-8 wide, fill in padding cells. Have
1609: * already ensured there is enough room.
1610: */
1.89 nicm 1611: for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.131 nicm 1612: log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
1.88 nicm 1613: grid_view_set_cell(gd, xx, s->cy, &screen_write_pad_cell);
1.89 nicm 1614: skip = 0;
1615: }
1616:
1617: /* If no change, do not draw. */
1.92 nicm 1618: if (skip) {
1619: if (s->cx >= gl->cellsize)
1620: skip = grid_cells_equal(gc, &grid_default_cell);
1621: else {
1622: gce = &gl->celldata[s->cx];
1623: if (gce->flags & GRID_FLAG_EXTENDED)
1624: skip = 0;
1.95 nicm 1625: else if (gc->flags != gce->flags)
1.92 nicm 1626: skip = 0;
1627: else if (gc->attr != gce->data.attr)
1628: skip = 0;
1629: else if (gc->fg != gce->data.fg)
1630: skip = 0;
1631: else if (gc->bg != gce->data.bg)
1632: skip = 0;
1.95 nicm 1633: else if (gc->data.width != 1)
1634: skip = 0;
1.109 nicm 1635: else if (gc->data.size != 1)
1636: skip = 0;
1.95 nicm 1637: else if (gce->data.data != gc->data.data[0])
1.92 nicm 1638: skip = 0;
1639: }
1640: }
1.1 nicm 1641:
1.127 nicm 1642: /* Update the selected flag and set the cell. */
1.90 nicm 1643: selected = screen_check_selection(s, s->cx, s->cy);
1.109 nicm 1644: if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1.90 nicm 1645: memcpy(&tmp_gc, gc, sizeof tmp_gc);
1646: tmp_gc.flags |= GRID_FLAG_SELECTED;
1647: grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1.109 nicm 1648: } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1.90 nicm 1649: memcpy(&tmp_gc, gc, sizeof tmp_gc);
1650: tmp_gc.flags &= ~GRID_FLAG_SELECTED;
1651: grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1652: } else if (!skip)
1.89 nicm 1653: grid_view_set_cell(gd, s->cx, s->cy, gc);
1.109 nicm 1654: if (selected)
1655: skip = 0;
1.1 nicm 1656:
1.64 nicm 1657: /*
1658: * Move the cursor. If not wrapping, stick at the last character and
1659: * replace it.
1660: */
1.65 nicm 1661: last = !(s->mode & MODE_WRAP);
1.92 nicm 1662: if (s->cx <= sx - last - width)
1.139 nicm 1663: screen_write_set_cursor(ctx, s->cx + width, -1);
1.64 nicm 1664: else
1.139 nicm 1665: screen_write_set_cursor(ctx, sx - last, -1);
1.1 nicm 1666:
1.89 nicm 1667: /* Create space for character in insert mode. */
1.109 nicm 1668: if (s->mode & MODE_INSERT) {
1.164 nicm 1669: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 1670: ttyctx.num = width;
1671: tty_write(tty_cmd_insertcharacter, &ttyctx);
1672: }
1.89 nicm 1673:
1674: /* Write to the screen. */
1.109 nicm 1675: if (!skip) {
1676: if (selected) {
1677: screen_select_cell(s, &tmp_gc, gc);
1678: ttyctx.cell = &tmp_gc;
1679: } else
1680: ttyctx.cell = gc;
1.16 nicm 1681: tty_write(tty_cmd_cell, &ttyctx);
1.92 nicm 1682: ctx->written++;
1683: } else
1684: ctx->skipped++;
1.35 nicm 1685: }
1686:
1687: /* Combine a UTF-8 zero-width character onto the previous. */
1.104 nicm 1688: static const struct grid_cell *
1689: screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
1690: u_int *xx)
1.35 nicm 1691: {
1692: struct screen *s = ctx->s;
1693: struct grid *gd = s->grid;
1.104 nicm 1694: static struct grid_cell gc;
1695: u_int n;
1.35 nicm 1696:
1697: /* Can't combine if at 0. */
1698: if (s->cx == 0)
1.104 nicm 1699: return (NULL);
1.35 nicm 1700:
1.60 nicm 1701: /* Empty data is out. */
1702: if (ud->size == 0)
1.37 nicm 1703: fatalx("UTF-8 data empty");
1704:
1.60 nicm 1705: /* Retrieve the previous cell. */
1.119 nicm 1706: for (n = 1; n <= s->cx; n++) {
1.104 nicm 1707: grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
1708: if (~gc.flags & GRID_FLAG_PADDING)
1709: break;
1710: }
1.119 nicm 1711: if (n > s->cx)
1.104 nicm 1712: return (NULL);
1713: *xx = s->cx - n;
1.35 nicm 1714:
1.60 nicm 1715: /* Check there is enough space. */
1.77 nicm 1716: if (gc.data.size + ud->size > sizeof gc.data.data)
1.104 nicm 1717: return (NULL);
1718:
1719: log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size,
1720: ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy);
1.35 nicm 1721:
1.77 nicm 1722: /* Append the data. */
1723: memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
1724: gc.data.size += ud->size;
1725:
1726: /* Set the new cell. */
1.104 nicm 1727: grid_view_set_cell(gd, *xx, s->cy, &gc);
1.35 nicm 1728:
1.104 nicm 1729: return (&gc);
1.1 nicm 1730: }
1731:
1732: /*
1733: * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1734: * cell on the screen, so following cells must not be drawn by marking them as
1735: * padding.
1736: *
1737: * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1738: * character, it is necessary to also overwrite any other cells which covered
1739: * by the same character.
1740: */
1.89 nicm 1741: static int
1742: screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
1743: u_int width)
1.1 nicm 1744: {
1745: struct screen *s = ctx->s;
1746: struct grid *gd = s->grid;
1.89 nicm 1747: struct grid_cell tmp_gc;
1.1 nicm 1748: u_int xx;
1.89 nicm 1749: int done = 0;
1.1 nicm 1750:
1.89 nicm 1751: if (gc->flags & GRID_FLAG_PADDING) {
1.1 nicm 1752: /*
1753: * A padding cell, so clear any following and leading padding
1754: * cells back to the character. Don't overwrite the current
1755: * cell as that happens later anyway.
1756: */
1757: xx = s->cx + 1;
1758: while (--xx > 0) {
1.89 nicm 1759: grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1760: if (~tmp_gc.flags & GRID_FLAG_PADDING)
1.1 nicm 1761: break;
1.131 nicm 1762: log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
1.1 nicm 1763: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1764: }
1765:
1766: /* Overwrite the character at the start of this padding. */
1.131 nicm 1767: log_debug("%s: character at %u,%u", __func__, xx, s->cy);
1.1 nicm 1768: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1.89 nicm 1769: done = 1;
1.43 nicm 1770: }
1.1 nicm 1771:
1.43 nicm 1772: /*
1.95 nicm 1773: * Overwrite any padding cells that belong to any UTF-8 characters
1774: * we'll be overwriting with the current character.
1.43 nicm 1775: */
1.95 nicm 1776: if (width != 1 ||
1777: gc->data.width != 1 ||
1778: gc->flags & GRID_FLAG_PADDING) {
1.89 nicm 1779: xx = s->cx + width - 1;
1780: while (++xx < screen_size_x(s)) {
1781: grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1782: if (~tmp_gc.flags & GRID_FLAG_PADDING)
1783: break;
1.160 nicm 1784: log_debug("%s: overwrite at %u,%u", __func__, xx,
1785: s->cy);
1.89 nicm 1786: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1787: done = 1;
1788: }
1.1 nicm 1789: }
1.89 nicm 1790:
1791: return (done);
1.50 nicm 1792: }
1793:
1.107 nicm 1794: /* Set external clipboard. */
1.50 nicm 1795: void
1796: screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
1797: {
1798: struct tty_ctx ttyctx;
1799:
1.163 nicm 1800: screen_write_initctx(ctx, &ttyctx, 0);
1.50 nicm 1801: ttyctx.ptr = str;
1802: ttyctx.num = len;
1803:
1804: tty_write(tty_cmd_setselection, &ttyctx);
1.47 nicm 1805: }
1806:
1.107 nicm 1807: /* Write unmodified string. */
1.47 nicm 1808: void
1809: screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
1810: {
1.87 nicm 1811: struct tty_ctx ttyctx;
1.47 nicm 1812:
1.163 nicm 1813: screen_write_initctx(ctx, &ttyctx, 0);
1.47 nicm 1814: ttyctx.ptr = str;
1815: ttyctx.num = len;
1816:
1817: tty_write(tty_cmd_rawstring, &ttyctx);
1.1 nicm 1818: }