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