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