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