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