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