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