Annotation of src/usr.bin/tmux/screen-write.c, Revision 1.191
1.191 ! nicm 1: /* $OpenBSD: screen-write.c,v 1.190 2021/01/18 10:27:54 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: /* Initialize writing. */
263: void
264: screen_write_start(struct screen_write_ctx *ctx, struct screen *s)
265: {
266: screen_write_init(ctx, s);
1.122 nicm 267:
1.158 nicm 268: if (log_get_level() != 0) {
1.178 nicm 269: log_debug("%s: size %ux%u, no pane", __func__,
270: screen_size_x(ctx->s), screen_size_y(ctx->s));
1.139 nicm 271: }
1.1 nicm 272: }
273:
274: /* Finish writing. */
275: void
1.92 nicm 276: screen_write_stop(struct screen_write_ctx *ctx)
277: {
1.109 nicm 278: screen_write_collect_end(ctx);
1.164 nicm 279: screen_write_collect_flush(ctx, 0, __func__);
1.92 nicm 280:
1.109 nicm 281: log_debug("%s: %u cells (%u written, %u skipped)", __func__,
282: ctx->cells, ctx->written, ctx->skipped);
1.169 nicm 283: if (ctx->wp != NULL) {
284: ctx->wp->written += ctx->written;
285: ctx->wp->skipped += ctx->skipped;
1.163 nicm 286: }
1.92 nicm 287:
1.109 nicm 288: free(ctx->item);
1.52 nicm 289: }
290:
291: /* Reset screen state. */
292: void
293: screen_write_reset(struct screen_write_ctx *ctx)
294: {
1.61 nicm 295: struct screen *s = ctx->s;
1.52 nicm 296:
1.61 nicm 297: screen_reset_tabs(s);
298: screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
1.63 nicm 299:
1.141 nicm 300: s->mode = MODE_CURSOR | MODE_WRAP;
1.52 nicm 301:
1.99 nicm 302: screen_write_clearscreen(ctx, 8);
1.144 nicm 303: screen_write_set_cursor(ctx, 0, 0);
1.1 nicm 304: }
305:
306: /* Write character. */
307: void
1.86 nicm 308: screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.69 nicm 309: u_char ch)
1.1 nicm 310: {
1.86 nicm 311: struct grid_cell gc;
312:
313: memcpy(&gc, gcp, sizeof gc);
314:
315: utf8_set(&gc.data, ch);
316: screen_write_cell(ctx, &gc);
1.1 nicm 317: }
318:
1.2 nicm 319: /* Calculate string length. */
1.71 nicm 320: size_t
1.75 nicm 321: screen_write_strlen(const char *fmt, ...)
1.2 nicm 322: {
1.36 nicm 323: va_list ap;
324: char *msg;
1.76 nicm 325: struct utf8_data ud;
1.36 nicm 326: u_char *ptr;
327: size_t left, size = 0;
1.79 nicm 328: enum utf8_state more;
1.2 nicm 329:
330: va_start(ap, fmt);
331: xvasprintf(&msg, fmt, ap);
332: va_end(ap);
333:
334: ptr = msg;
335: while (*ptr != '\0') {
1.79 nicm 336: if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
1.36 nicm 337: ptr++;
1.2 nicm 338:
339: left = strlen(ptr);
1.77 nicm 340: if (left < (size_t)ud.size - 1)
1.36 nicm 341: break;
1.79 nicm 342: while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
1.2 nicm 343: ptr++;
1.36 nicm 344: ptr++;
345:
1.79 nicm 346: if (more == UTF8_DONE)
1.78 nicm 347: size += ud.width;
1.2 nicm 348: } else {
1.75 nicm 349: if (*ptr > 0x1f && *ptr < 0x7f)
350: size++;
1.2 nicm 351: ptr++;
352: }
1.7 ray 353: }
1.2 nicm 354:
1.56 nicm 355: free(msg);
1.2 nicm 356: return (size);
357: }
358:
1.179 nicm 359: /* Write string wrapped over lines. */
360: int
361: screen_write_text(struct screen_write_ctx *ctx, u_int cx, u_int width,
362: u_int lines, int more, const struct grid_cell *gcp, const char *fmt, ...)
363: {
364: struct screen *s = ctx->s;
365: va_list ap;
366: char *tmp;
367: u_int cy = s->cy, i, end, next, idx = 0, at, left;
368: struct utf8_data *text;
369: struct grid_cell gc;
370:
371: memcpy(&gc, gcp, sizeof gc);
372:
373: va_start(ap, fmt);
374: xvasprintf(&tmp, fmt, ap);
375: va_end(ap);
376:
377: text = utf8_fromcstr(tmp);
378: free(tmp);
379:
380: left = (cx + width) - s->cx;
381: for (;;) {
382: /* Find the end of what can fit on the line. */
383: at = 0;
384: for (end = idx; text[end].size != 0; end++) {
385: if (text[end].size == 1 && text[end].data[0] == '\n')
386: break;
387: if (at + text[end].width > left)
388: break;
389: at += text[end].width;
390: }
391:
392: /*
393: * If we're on a space, that's the end. If not, walk back to
394: * try and find one.
395: */
396: if (text[end].size == 0)
397: next = end;
398: else if (text[end].size == 1 && text[end].data[0] == '\n')
399: next = end + 1;
400: else if (text[end].size == 1 && text[end].data[0] == ' ')
401: next = end + 1;
402: else {
403: for (i = end; i > idx; i--) {
404: if (text[i].size == 1 && text[i].data[0] == ' ')
405: break;
406: }
407: if (i != idx) {
408: next = i + 1;
409: end = i;
410: } else
411: next = end;
412: }
413:
414: /* Print the line. */
415: for (i = idx; i < end; i++) {
416: utf8_copy(&gc.data, &text[i]);
417: screen_write_cell(ctx, &gc);
418: }
419:
420: /* If at the bottom, stop. */
421: idx = next;
422: if (s->cy == cy + lines - 1 || text[idx].size == 0)
423: break;
424:
425: screen_write_cursormove(ctx, cx, s->cy + 1, 0);
426: left = width;
427: }
428:
429: /*
430: * Fail if on the last line and there is more to come or at the end, or
431: * if the text was not entirely consumed.
432: */
433: if ((s->cy == cy + lines - 1 && (!more || s->cx == cx + width)) ||
434: text[idx].size != 0) {
435: free(text);
436: return (0);
437: }
438: free(text);
439:
440: /*
441: * If no more to come, move to the next line. Otherwise, leave on
442: * the same line (except if at the end).
443: */
444: if (!more || s->cx == cx + width)
445: screen_write_cursormove(ctx, cx, s->cy + 1, 0);
446: return (1);
447: }
448:
449: /* Write simple string (no maximum length). */
1.71 nicm 450: void
1.86 nicm 451: screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.71 nicm 452: const char *fmt, ...)
1.1 nicm 453: {
454: va_list ap;
455:
456: va_start(ap, fmt);
1.86 nicm 457: screen_write_vnputs(ctx, -1, gcp, fmt, ap);
1.2 nicm 458: va_end(ap);
459: }
460:
461: /* Write string with length limit (-1 for unlimited). */
1.71 nicm 462: void
463: screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86 nicm 464: const struct grid_cell *gcp, const char *fmt, ...)
1.2 nicm 465: {
466: va_list ap;
467:
468: va_start(ap, fmt);
1.86 nicm 469: screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
1.2 nicm 470: va_end(ap);
471: }
472:
473: void
1.3 nicm 474: screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86 nicm 475: const struct grid_cell *gcp, const char *fmt, va_list ap)
1.2 nicm 476: {
1.86 nicm 477: struct grid_cell gc;
478: struct utf8_data *ud = &gc.data;
1.36 nicm 479: char *msg;
480: u_char *ptr;
481: size_t left, size = 0;
1.79 nicm 482: enum utf8_state more;
1.2 nicm 483:
1.86 nicm 484: memcpy(&gc, gcp, sizeof gc);
1.1 nicm 485: xvasprintf(&msg, fmt, ap);
486:
1.2 nicm 487: ptr = msg;
488: while (*ptr != '\0') {
1.86 nicm 489: if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36 nicm 490: ptr++;
1.2 nicm 491:
492: left = strlen(ptr);
1.86 nicm 493: if (left < (size_t)ud->size - 1)
1.36 nicm 494: break;
1.86 nicm 495: while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.2 nicm 496: ptr++;
1.36 nicm 497: ptr++;
1.7 ray 498:
1.86 nicm 499: if (more != UTF8_DONE)
500: continue;
501: if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
502: while (size < (size_t)maxlen) {
503: screen_write_putc(ctx, &gc, ' ');
504: size++;
1.2 nicm 505: }
1.86 nicm 506: break;
1.2 nicm 507: }
1.86 nicm 508: size += ud->width;
509: screen_write_cell(ctx, &gc);
1.2 nicm 510: } else {
1.86 nicm 511: if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.2 nicm 512: break;
513:
1.57 nicm 514: if (*ptr == '\001')
1.86 nicm 515: gc.attr ^= GRID_ATTR_CHARSET;
1.187 nicm 516: else if (*ptr == '\n') {
517: screen_write_linefeed(ctx, 0, 8);
518: screen_write_carriagereturn(ctx);
519: } else if (*ptr > 0x1f && *ptr < 0x7f) {
1.57 nicm 520: size++;
1.86 nicm 521: screen_write_putc(ctx, &gc, *ptr);
1.75 nicm 522: }
1.24 nicm 523: ptr++;
524: }
525: }
526:
1.56 nicm 527: free(msg);
1.1 nicm 528: }
529:
1.132 nicm 530: /*
1.176 nicm 531: * Copy from another screen but without the selection stuff. Assumes the target
532: * region is already big enough.
1.132 nicm 533: */
534: void
535: screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src,
536: u_int px, u_int py, u_int nx, u_int ny)
537: {
538: struct screen *s = ctx->s;
539: struct grid *gd = src->grid;
540: struct grid_cell gc;
541: u_int xx, yy, cx, cy;
1.96 nicm 542:
1.132 nicm 543: if (nx == 0 || ny == 0)
544: return;
545:
546: cy = s->cy;
547: for (yy = py; yy < py + ny; yy++) {
1.134 nicm 548: if (yy >= gd->hsize + gd->sy)
549: break;
1.132 nicm 550: cx = s->cx;
551: for (xx = px; xx < px + nx; xx++) {
1.137 nicm 552: if (xx >= grid_get_line(gd, yy)->cellsize)
1.132 nicm 553: break;
554: grid_get_cell(gd, xx, yy, &gc);
1.135 nicm 555: if (xx + gc.data.width > px + nx)
556: break;
1.150 nicm 557: grid_view_set_cell(ctx->s->grid, cx, cy, &gc);
1.132 nicm 558: cx++;
559: }
1.1 nicm 560: cy++;
1.125 nicm 561: }
562: }
563:
1.129 nicm 564: /* Draw a horizontal line on screen. */
1.125 nicm 565: void
1.129 nicm 566: screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right)
1.125 nicm 567: {
568: struct screen *s = ctx->s;
569: struct grid_cell gc;
570: u_int cx, cy, i;
571:
572: cx = s->cx;
573: cy = s->cy;
574:
575: memcpy(&gc, &grid_default_cell, sizeof gc);
576: gc.attr |= GRID_ATTR_CHARSET;
577:
578: screen_write_putc(ctx, &gc, left ? 't' : 'q');
579: for (i = 1; i < nx - 1; i++)
580: screen_write_putc(ctx, &gc, 'q');
581: screen_write_putc(ctx, &gc, right ? 'u' : 'q');
1.129 nicm 582:
1.144 nicm 583: screen_write_set_cursor(ctx, cx, cy);
1.129 nicm 584: }
585:
586: /* Draw a horizontal line on screen. */
587: void
1.130 nicm 588: screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom)
1.129 nicm 589: {
590: struct screen *s = ctx->s;
591: struct grid_cell gc;
592: u_int cx, cy, i;
593:
594: cx = s->cx;
595: cy = s->cy;
596:
597: memcpy(&gc, &grid_default_cell, sizeof gc);
598: gc.attr |= GRID_ATTR_CHARSET;
599:
600: screen_write_putc(ctx, &gc, top ? 'w' : 'x');
601: for (i = 1; i < ny - 1; i++) {
1.144 nicm 602: screen_write_set_cursor(ctx, cx, cy + i);
1.129 nicm 603: screen_write_putc(ctx, &gc, 'x');
604: }
1.144 nicm 605: screen_write_set_cursor(ctx, cx, cy + ny - 1);
1.129 nicm 606: screen_write_putc(ctx, &gc, bottom ? 'v' : 'x');
1.152 nicm 607:
608: screen_write_set_cursor(ctx, cx, cy);
609: }
610:
611: /* Draw a menu on screen. */
612: void
1.161 nicm 613: screen_write_menu(struct screen_write_ctx *ctx, struct menu *menu,
614: int choice, const struct grid_cell *choice_gc)
1.152 nicm 615: {
616: struct screen *s = ctx->s;
1.161 nicm 617: struct grid_cell default_gc;
618: const struct grid_cell *gc = &default_gc;
1.152 nicm 619: u_int cx, cy, i, j;
1.153 nicm 620: const char *name;
1.152 nicm 621:
622: cx = s->cx;
623: cy = s->cy;
624:
1.161 nicm 625: memcpy(&default_gc, &grid_default_cell, sizeof default_gc);
1.152 nicm 626:
627: screen_write_box(ctx, menu->width + 4, menu->count + 2);
628: screen_write_cursormove(ctx, cx + 2, cy, 0);
1.161 nicm 629: format_draw(ctx, &default_gc, menu->width, menu->title, NULL);
1.152 nicm 630:
631: for (i = 0; i < menu->count; i++) {
1.153 nicm 632: name = menu->items[i].name;
633: if (name == NULL) {
1.152 nicm 634: screen_write_cursormove(ctx, cx, cy + 1 + i, 0);
635: screen_write_hline(ctx, menu->width + 4, 1, 1);
636: } else {
1.153 nicm 637: if (choice >= 0 && i == (u_int)choice && *name != '-')
1.161 nicm 638: gc = choice_gc;
1.152 nicm 639: screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
640: for (j = 0; j < menu->width; j++)
1.161 nicm 641: screen_write_putc(ctx, gc, ' ');
1.152 nicm 642: screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
1.153 nicm 643: if (*name == '-') {
644: name++;
1.161 nicm 645: default_gc.attr |= GRID_ATTR_DIM;
646: format_draw(ctx, gc, menu->width, name, NULL);
647: default_gc.attr &= ~GRID_ATTR_DIM;
1.153 nicm 648: } else
1.161 nicm 649: format_draw(ctx, gc, menu->width, name, NULL);
650: gc = &default_gc;
1.152 nicm 651: }
652: }
1.125 nicm 653:
1.144 nicm 654: screen_write_set_cursor(ctx, cx, cy);
1.125 nicm 655: }
656:
657: /* Draw a box on screen. */
658: void
659: screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny)
660: {
661: struct screen *s = ctx->s;
662: struct grid_cell gc;
663: u_int cx, cy, i;
664:
665: cx = s->cx;
666: cy = s->cy;
667:
668: memcpy(&gc, &grid_default_cell, sizeof gc);
669: gc.attr |= GRID_ATTR_CHARSET;
670:
671: screen_write_putc(ctx, &gc, 'l');
672: for (i = 1; i < nx - 1; i++)
673: screen_write_putc(ctx, &gc, 'q');
674: screen_write_putc(ctx, &gc, 'k');
675:
1.144 nicm 676: screen_write_set_cursor(ctx, cx, cy + ny - 1);
1.125 nicm 677: screen_write_putc(ctx, &gc, 'm');
678: for (i = 1; i < nx - 1; i++)
679: screen_write_putc(ctx, &gc, 'q');
680: screen_write_putc(ctx, &gc, 'j');
681:
682: for (i = 1; i < ny - 1; i++) {
1.144 nicm 683: screen_write_set_cursor(ctx, cx, cy + i);
1.125 nicm 684: screen_write_putc(ctx, &gc, 'x');
685: }
686: for (i = 1; i < ny - 1; i++) {
1.144 nicm 687: screen_write_set_cursor(ctx, cx + nx - 1, cy + i);
1.125 nicm 688: screen_write_putc(ctx, &gc, 'x');
689: }
690:
1.144 nicm 691: screen_write_set_cursor(ctx, cx, cy);
1.125 nicm 692: }
693:
1.132 nicm 694: /*
695: * Write a preview version of a window. Assumes target area is big enough and
696: * already cleared.
697: */
1.125 nicm 698: void
699: screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
700: u_int ny)
701: {
702: struct screen *s = ctx->s;
703: struct grid_cell gc;
704: u_int cx, cy, px, py;
705:
706: cx = s->cx;
707: cy = s->cy;
708:
709: /*
710: * If the cursor is on, pick the area around the cursor, otherwise use
711: * the top left.
712: */
713: if (src->mode & MODE_CURSOR) {
714: px = src->cx;
715: if (px < nx / 3)
716: px = 0;
717: else
718: px = px - nx / 3;
719: if (px + nx > screen_size_x(src)) {
720: if (nx > screen_size_x(src))
721: px = 0;
722: else
723: px = screen_size_x(src) - nx;
724: }
725: py = src->cy;
726: if (py < ny / 3)
727: py = 0;
728: else
729: py = py - ny / 3;
730: if (py + ny > screen_size_y(src)) {
731: if (ny > screen_size_y(src))
732: py = 0;
733: else
734: py = screen_size_y(src) - ny;
735: }
736: } else {
737: px = 0;
738: py = 0;
739: }
740:
1.132 nicm 741: screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
1.125 nicm 742:
743: if (src->mode & MODE_CURSOR) {
744: grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
745: gc.attr |= GRID_ATTR_REVERSE;
1.144 nicm 746: screen_write_set_cursor(ctx, cx + (src->cx - px),
1.125 nicm 747: cy + (src->cy - py));
748: screen_write_cell(ctx, &gc);
1.1 nicm 749: }
750: }
751:
1.61 nicm 752: /* Set a mode. */
753: void
754: screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
755: {
756: struct screen *s = ctx->s;
757:
758: s->mode |= mode;
759: }
760:
761: /* Clear a mode. */
762: void
763: screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
764: {
765: struct screen *s = ctx->s;
766:
767: s->mode &= ~mode;
768: }
769:
1.1 nicm 770: /* Cursor up by ny. */
771: void
772: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
773: {
774: struct screen *s = ctx->s;
1.139 nicm 775: u_int cx = s->cx, cy = s->cy;
1.1 nicm 776:
777: if (ny == 0)
778: ny = 1;
779:
1.139 nicm 780: if (cy < s->rupper) {
1.12 nicm 781: /* Above region. */
1.139 nicm 782: if (ny > cy)
783: ny = cy;
1.12 nicm 784: } else {
785: /* Below region. */
1.139 nicm 786: if (ny > cy - s->rupper)
787: ny = cy - s->rupper;
1.12 nicm 788: }
1.139 nicm 789: if (cx == screen_size_x(s))
790: cx--;
791:
792: cy -= ny;
1.1 nicm 793:
1.139 nicm 794: screen_write_set_cursor(ctx, cx, cy);
1.1 nicm 795: }
796:
797: /* Cursor down by ny. */
798: void
799: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
800: {
801: struct screen *s = ctx->s;
1.139 nicm 802: u_int cx = s->cx, cy = s->cy;
1.1 nicm 803:
804: if (ny == 0)
805: ny = 1;
806:
1.139 nicm 807: if (cy > s->rlower) {
1.12 nicm 808: /* Below region. */
1.139 nicm 809: if (ny > screen_size_y(s) - 1 - cy)
810: ny = screen_size_y(s) - 1 - cy;
1.12 nicm 811: } else {
812: /* Above region. */
1.139 nicm 813: if (ny > s->rlower - cy)
814: ny = s->rlower - cy;
1.12 nicm 815: }
1.139 nicm 816: if (cx == screen_size_x(s))
817: cx--;
818: else if (ny == 0)
1.1 nicm 819: return;
820:
1.139 nicm 821: cy += ny;
822:
823: screen_write_set_cursor(ctx, cx, cy);
1.1 nicm 824: }
825:
1.101 nicm 826: /* Cursor right by nx. */
1.1 nicm 827: void
828: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
829: {
830: struct screen *s = ctx->s;
1.139 nicm 831: u_int cx = s->cx, cy = s->cy;
1.1 nicm 832:
833: if (nx == 0)
834: nx = 1;
835:
1.139 nicm 836: if (nx > screen_size_x(s) - 1 - cx)
837: nx = screen_size_x(s) - 1 - cx;
1.1 nicm 838: if (nx == 0)
839: return;
840:
1.139 nicm 841: cx += nx;
842:
843: screen_write_set_cursor(ctx, cx, cy);
1.1 nicm 844: }
845:
846: /* Cursor left by nx. */
847: void
848: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
849: {
850: struct screen *s = ctx->s;
1.139 nicm 851: u_int cx = s->cx, cy = s->cy;
1.1 nicm 852:
853: if (nx == 0)
854: nx = 1;
855:
1.139 nicm 856: if (nx > cx)
857: nx = cx;
1.1 nicm 858: if (nx == 0)
859: return;
860:
1.139 nicm 861: cx -= nx;
862:
863: screen_write_set_cursor(ctx, cx, cy);
1.5 nicm 864: }
865:
1.29 nicm 866: /* Backspace; cursor left unless at start of wrapped line when can move up. */
867: void
868: screen_write_backspace(struct screen_write_ctx *ctx)
869: {
870: struct screen *s = ctx->s;
871: struct grid_line *gl;
1.139 nicm 872: u_int cx = s->cx, cy = s->cy;
1.29 nicm 873:
1.139 nicm 874: if (cx == 0) {
875: if (cy == 0)
1.29 nicm 876: return;
1.139 nicm 877: gl = grid_get_line(s->grid, s->grid->hsize + cy - 1);
1.29 nicm 878: if (gl->flags & GRID_LINE_WRAPPED) {
1.139 nicm 879: cy--;
880: cx = screen_size_x(s) - 1;
1.29 nicm 881: }
882: } else
1.139 nicm 883: cx--;
884:
885: screen_write_set_cursor(ctx, cx, cy);
1.29 nicm 886: }
887:
1.5 nicm 888: /* VT100 alignment test. */
889: void
890: screen_write_alignmenttest(struct screen_write_ctx *ctx)
891: {
892: struct screen *s = ctx->s;
1.17 nicm 893: struct tty_ctx ttyctx;
1.5 nicm 894: struct grid_cell gc;
895: u_int xx, yy;
896:
897: memcpy(&gc, &grid_default_cell, sizeof gc);
1.77 nicm 898: utf8_set(&gc.data, 'E');
1.7 ray 899:
1.5 nicm 900: for (yy = 0; yy < screen_size_y(s); yy++) {
901: for (xx = 0; xx < screen_size_x(s); xx++)
902: grid_view_set_cell(s->grid, xx, yy, &gc);
903: }
1.7 ray 904:
1.139 nicm 905: screen_write_set_cursor(ctx, 0, 0);
1.5 nicm 906:
907: s->rupper = 0;
908: s->rlower = screen_size_y(s) - 1;
1.143 nicm 909:
1.163 nicm 910: screen_write_initctx(ctx, &ttyctx, 1);
1.5 nicm 911:
1.109 nicm 912: screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1.17 nicm 913: tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1 nicm 914: }
915:
916: /* Insert nx characters. */
917: void
1.99 nicm 918: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1 nicm 919: {
920: struct screen *s = ctx->s;
1.17 nicm 921: struct tty_ctx ttyctx;
1.1 nicm 922:
923: if (nx == 0)
924: nx = 1;
925:
1.9 nicm 926: if (nx > screen_size_x(s) - s->cx)
927: nx = screen_size_x(s) - s->cx;
1.1 nicm 928: if (nx == 0)
929: return;
930:
1.107 nicm 931: if (s->cx > screen_size_x(s) - 1)
932: return;
933:
1.163 nicm 934: screen_write_initctx(ctx, &ttyctx, 0);
1.107 nicm 935: ttyctx.bg = bg;
1.1 nicm 936:
1.107 nicm 937: grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1.1 nicm 938:
1.164 nicm 939: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 940: ttyctx.num = nx;
941: tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1 nicm 942: }
943:
944: /* Delete nx characters. */
945: void
1.99 nicm 946: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1 nicm 947: {
948: struct screen *s = ctx->s;
1.17 nicm 949: struct tty_ctx ttyctx;
1.1 nicm 950:
951: if (nx == 0)
952: nx = 1;
953:
1.9 nicm 954: if (nx > screen_size_x(s) - s->cx)
955: nx = screen_size_x(s) - s->cx;
1.1 nicm 956: if (nx == 0)
957: return;
958:
1.107 nicm 959: if (s->cx > screen_size_x(s) - 1)
960: return;
961:
1.163 nicm 962: screen_write_initctx(ctx, &ttyctx, 0);
1.107 nicm 963: ttyctx.bg = bg;
1.1 nicm 964:
1.107 nicm 965: grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1.1 nicm 966:
1.164 nicm 967: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 968: ttyctx.num = nx;
969: tty_write(tty_cmd_deletecharacter, &ttyctx);
1.59 nicm 970: }
971:
972: /* Clear nx characters. */
973: void
1.121 nicm 974: screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.59 nicm 975: {
976: struct screen *s = ctx->s;
977: struct tty_ctx ttyctx;
978:
979: if (nx == 0)
980: nx = 1;
981:
982: if (nx > screen_size_x(s) - s->cx)
983: nx = screen_size_x(s) - s->cx;
984: if (nx == 0)
985: return;
986:
1.107 nicm 987: if (s->cx > screen_size_x(s) - 1)
988: return;
989:
1.163 nicm 990: screen_write_initctx(ctx, &ttyctx, 0);
1.121 nicm 991: ttyctx.bg = bg;
1.59 nicm 992:
1.124 nicm 993: grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
1.59 nicm 994:
1.164 nicm 995: screen_write_collect_flush(ctx, 0, __func__);
1.59 nicm 996: ttyctx.num = nx;
997: tty_write(tty_cmd_clearcharacter, &ttyctx);
1.1 nicm 998: }
999:
1000: /* Insert ny lines. */
1001: void
1.99 nicm 1002: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1 nicm 1003: {
1004: struct screen *s = ctx->s;
1.107 nicm 1005: struct grid *gd = s->grid;
1.17 nicm 1006: struct tty_ctx ttyctx;
1.1 nicm 1007:
1008: if (ny == 0)
1009: ny = 1;
1010:
1.11 nicm 1011: if (s->cy < s->rupper || s->cy > s->rlower) {
1012: if (ny > screen_size_y(s) - s->cy)
1013: ny = screen_size_y(s) - s->cy;
1014: if (ny == 0)
1015: return;
1016:
1.163 nicm 1017: screen_write_initctx(ctx, &ttyctx, 1);
1.107 nicm 1018: ttyctx.bg = bg;
1.11 nicm 1019:
1.107 nicm 1020: grid_view_insert_lines(gd, s->cy, ny, bg);
1.11 nicm 1021:
1.164 nicm 1022: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 1023: ttyctx.num = ny;
1024: tty_write(tty_cmd_insertline, &ttyctx);
1.11 nicm 1025: return;
1026: }
1027:
1028: if (ny > s->rlower + 1 - s->cy)
1029: ny = s->rlower + 1 - s->cy;
1.1 nicm 1030: if (ny == 0)
1031: return;
1.41 nicm 1032:
1.163 nicm 1033: screen_write_initctx(ctx, &ttyctx, 1);
1.107 nicm 1034: ttyctx.bg = bg;
1.1 nicm 1035:
1036: if (s->cy < s->rupper || s->cy > s->rlower)
1.107 nicm 1037: grid_view_insert_lines(gd, s->cy, ny, bg);
1038: else
1039: grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1 nicm 1040:
1.164 nicm 1041: screen_write_collect_flush(ctx, 0, __func__);
1042:
1.17 nicm 1043: ttyctx.num = ny;
1044: tty_write(tty_cmd_insertline, &ttyctx);
1.1 nicm 1045: }
1046:
1047: /* Delete ny lines. */
1048: void
1.99 nicm 1049: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1 nicm 1050: {
1051: struct screen *s = ctx->s;
1.107 nicm 1052: struct grid *gd = s->grid;
1.17 nicm 1053: struct tty_ctx ttyctx;
1.1 nicm 1054:
1055: if (ny == 0)
1056: ny = 1;
1057:
1.11 nicm 1058: if (s->cy < s->rupper || s->cy > s->rlower) {
1059: if (ny > screen_size_y(s) - s->cy)
1060: ny = screen_size_y(s) - s->cy;
1061: if (ny == 0)
1062: return;
1063:
1.163 nicm 1064: screen_write_initctx(ctx, &ttyctx, 1);
1.107 nicm 1065: ttyctx.bg = bg;
1.11 nicm 1066:
1.107 nicm 1067: grid_view_delete_lines(gd, s->cy, ny, bg);
1.11 nicm 1068:
1.164 nicm 1069: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 1070: ttyctx.num = ny;
1071: tty_write(tty_cmd_deleteline, &ttyctx);
1.11 nicm 1072: return;
1073: }
1.41 nicm 1074:
1.11 nicm 1075: if (ny > s->rlower + 1 - s->cy)
1076: ny = s->rlower + 1 - s->cy;
1.1 nicm 1077: if (ny == 0)
1078: return;
1079:
1.163 nicm 1080: screen_write_initctx(ctx, &ttyctx, 1);
1.107 nicm 1081: ttyctx.bg = bg;
1.1 nicm 1082:
1083: if (s->cy < s->rupper || s->cy > s->rlower)
1.107 nicm 1084: grid_view_delete_lines(gd, s->cy, ny, bg);
1085: else
1086: grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1 nicm 1087:
1.164 nicm 1088: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 1089: ttyctx.num = ny;
1090: tty_write(tty_cmd_deleteline, &ttyctx);
1.1 nicm 1091: }
1092:
1093: /* Clear line at cursor. */
1094: void
1.99 nicm 1095: screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1096: {
1.168 nicm 1097: struct screen *s = ctx->s;
1098: struct grid_line *gl;
1099: u_int sx = screen_size_x(s);
1.1 nicm 1100:
1.137 nicm 1101: gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.140 nicm 1102: if (gl->cellsize == 0 && COLOUR_DEFAULT(bg))
1.92 nicm 1103: return;
1.1 nicm 1104:
1.99 nicm 1105: grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1106:
1.109 nicm 1107: screen_write_collect_clear(ctx, s->cy, 1);
1.172 nicm 1108: ctx->s->write_list[s->cy].bg = 1 + bg;
1.168 nicm 1109: ctx->item->used = 0;
1.1 nicm 1110: }
1111:
1112: /* Clear to end of line from cursor. */
1113: void
1.99 nicm 1114: screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1115: {
1.165 nicm 1116: struct screen *s = ctx->s;
1117: struct grid_line *gl;
1118: u_int sx = screen_size_x(s);
1119: struct screen_write_collect_item *ci = ctx->item;
1120:
1121: if (s->cx == 0) {
1122: screen_write_clearline(ctx, bg);
1123: return;
1124: }
1.1 nicm 1125:
1.137 nicm 1126: gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.140 nicm 1127: if (s->cx > sx - 1 || (s->cx >= gl->cellsize && COLOUR_DEFAULT(bg)))
1.92 nicm 1128: return;
1.108 nicm 1129:
1.99 nicm 1130: grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
1131:
1.186 nicm 1132: screen_write_collect_clear_end(ctx, s->cy, s->cx);
1133: ci->x = s->cx;
1134: ci->type = CLEAR_END;
1135: ci->bg = bg;
1136: TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1137: ctx->item = xcalloc(1, sizeof *ctx->item);
1.1 nicm 1138: }
1139:
1140: /* Clear to start of line from cursor. */
1141: void
1.99 nicm 1142: screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1143: {
1.165 nicm 1144: struct screen *s = ctx->s;
1145: u_int sx = screen_size_x(s);
1146: struct screen_write_collect_item *ci = ctx->item;
1147:
1148: if (s->cx >= sx - 1) {
1149: screen_write_clearline(ctx, bg);
1150: return;
1151: }
1.1 nicm 1152:
1.109 nicm 1153: if (s->cx > sx - 1)
1.99 nicm 1154: grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109 nicm 1155: else
1.99 nicm 1156: grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1 nicm 1157:
1.186 nicm 1158: screen_write_collect_clear_start(ctx, s->cy, s->cx);
1159: ci->x = s->cx;
1160: ci->type = CLEAR_START;
1161: ci->bg = bg;
1162: TAILQ_INSERT_TAIL(&ctx->s->write_list[s->cy].items, ci, entry);
1163: ctx->item = xcalloc(1, sizeof *ctx->item);
1.1 nicm 1164: }
1165:
1.101 nicm 1166: /* Move cursor to px,py. */
1.1 nicm 1167: void
1.147 nicm 1168: screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py,
1169: int origin)
1.1 nicm 1170: {
1171: struct screen *s = ctx->s;
1172:
1.147 nicm 1173: if (origin && py != -1 && (s->mode & MODE_ORIGIN)) {
1.146 nicm 1174: if ((u_int)py > s->rlower - s->rupper)
1.144 nicm 1175: py = s->rlower;
1176: else
1177: py += s->rupper;
1178: }
1.145 nicm 1179:
1.146 nicm 1180: if (px != -1 && (u_int)px > screen_size_x(s) - 1)
1.145 nicm 1181: px = screen_size_x(s) - 1;
1.146 nicm 1182: if (py != -1 && (u_int)py > screen_size_y(s) - 1)
1.145 nicm 1183: py = screen_size_y(s) - 1;
1.1 nicm 1184:
1.139 nicm 1185: screen_write_set_cursor(ctx, px, py);
1.1 nicm 1186: }
1187:
1.101 nicm 1188: /* Reverse index (up with scroll). */
1.1 nicm 1189: void
1.122 nicm 1190: screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
1.1 nicm 1191: {
1192: struct screen *s = ctx->s;
1.17 nicm 1193: struct tty_ctx ttyctx;
1.1 nicm 1194:
1.165 nicm 1195: if (s->cy == s->rupper) {
1196: grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1197: screen_write_collect_flush(ctx, 0, __func__);
1198:
1199: screen_write_initctx(ctx, &ttyctx, 1);
1200: ttyctx.bg = bg;
1.1 nicm 1201:
1.165 nicm 1202: tty_write(tty_cmd_reverseindex, &ttyctx);
1203: } else if (s->cy > 0)
1.139 nicm 1204: screen_write_set_cursor(ctx, -1, s->cy - 1);
1.1 nicm 1205:
1206: }
1207:
1208: /* Set scroll region. */
1209: void
1.83 nicm 1210: screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
1211: u_int rlower)
1.1 nicm 1212: {
1213: struct screen *s = ctx->s;
1214:
1215: if (rupper > screen_size_y(s) - 1)
1216: rupper = screen_size_y(s) - 1;
1217: if (rlower > screen_size_y(s) - 1)
1218: rlower = screen_size_y(s) - 1;
1.13 nicm 1219: if (rupper >= rlower) /* cannot be one line */
1.1 nicm 1220: return;
1221:
1.164 nicm 1222: screen_write_collect_flush(ctx, 0, __func__);
1.110 nicm 1223:
1.1 nicm 1224: /* Cursor moves to top-left. */
1.139 nicm 1225: screen_write_set_cursor(ctx, 0, 0);
1.1 nicm 1226:
1227: s->rupper = rupper;
1228: s->rlower = rlower;
1229: }
1230:
1.34 nicm 1231: /* Line feed. */
1.1 nicm 1232: void
1.122 nicm 1233: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1.1 nicm 1234: {
1.20 nicm 1235: struct screen *s = ctx->s;
1.109 nicm 1236: struct grid *gd = s->grid;
1.20 nicm 1237: struct grid_line *gl;
1.1 nicm 1238:
1.137 nicm 1239: gl = grid_get_line(gd, gd->hsize + s->cy);
1.20 nicm 1240: if (wrapped)
1241: gl->flags |= GRID_LINE_WRAPPED;
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];
1.191 ! nicm 1542: if (cl->bg != 0) {
! 1543: screen_write_set_cursor(ctx, 0, y);
! 1544: screen_write_initctx(ctx, &ttyctx, 1);
! 1545: ttyctx.bg = cl->bg - 1;
! 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.167 nicm 1551: screen_write_initctx(ctx, &ttyctx, 1);
1.165 nicm 1552: ttyctx.bg = ci->bg;
1553: tty_write(tty_cmd_clearendofline, &ttyctx);
1554: } else if (ci->type == CLEAR_START) {
1.167 nicm 1555: screen_write_initctx(ctx, &ttyctx, 1);
1.165 nicm 1556: ttyctx.bg = ci->bg;
1557: tty_write(tty_cmd_clearstartofline, &ttyctx);
1558: } else {
1.167 nicm 1559: screen_write_initctx(ctx, &ttyctx, 0);
1.165 nicm 1560: ttyctx.cell = &ci->gc;
1561: ttyctx.wrapped = ci->wrapped;
1.172 nicm 1562: ttyctx.ptr = cl->data + ci->x;
1.165 nicm 1563: ttyctx.num = ci->used;
1564: tty_write(tty_cmd_cells, &ttyctx);
1565: }
1.116 nicm 1566:
1567: items++;
1568: written += ci->used;
1.109 nicm 1569:
1.172 nicm 1570: TAILQ_REMOVE(&cl->items, ci, entry);
1.109 nicm 1571: free(ci);
1572: }
1.172 nicm 1573: cl->bg = 0;
1.109 nicm 1574: }
1575: s->cx = cx; s->cy = cy;
1.116 nicm 1576:
1.164 nicm 1577: log_debug("%s: flushed %u items (%zu bytes) (%s)", __func__, items,
1578: written, from);
1.116 nicm 1579: ctx->written += written;
1.109 nicm 1580: }
1581:
1582: /* Finish and store collected cells. */
1583: void
1584: screen_write_collect_end(struct screen_write_ctx *ctx)
1585: {
1586: struct screen *s = ctx->s;
1587: struct screen_write_collect_item *ci = ctx->item;
1.172 nicm 1588: struct screen_write_collect_line *cl = &s->write_list[s->cy];
1.109 nicm 1589: struct grid_cell gc;
1.131 nicm 1590: u_int xx;
1.109 nicm 1591:
1592: if (ci->used == 0)
1593: return;
1594:
1595: ci->x = s->cx;
1.172 nicm 1596: TAILQ_INSERT_TAIL(&cl->items, ci, entry);
1.109 nicm 1597: ctx->item = xcalloc(1, sizeof *ctx->item);
1598:
1.170 nicm 1599: log_debug("%s: %u %.*s (at %u,%u)", __func__, ci->used,
1.172 nicm 1600: (int)ci->used, cl->data + ci->x, s->cx, s->cy);
1.109 nicm 1601:
1.131 nicm 1602: if (s->cx != 0) {
1603: for (xx = s->cx; xx > 0; xx--) {
1604: grid_view_get_cell(s->grid, xx, s->cy, &gc);
1605: if (~gc.flags & GRID_FLAG_PADDING)
1606: break;
1.136 nicm 1607: grid_view_set_cell(s->grid, xx, s->cy,
1608: &grid_default_cell);
1.131 nicm 1609: }
1.139 nicm 1610: if (gc.data.width > 1) {
1.136 nicm 1611: grid_view_set_cell(s->grid, xx, s->cy,
1612: &grid_default_cell);
1.139 nicm 1613: }
1.131 nicm 1614: }
1615:
1.172 nicm 1616: grid_view_set_cells(s->grid, s->cx, s->cy, &ci->gc, cl->data + ci->x,
1617: ci->used);
1.139 nicm 1618: screen_write_set_cursor(ctx, s->cx + ci->used, -1);
1.131 nicm 1619:
1620: for (xx = s->cx; xx < screen_size_x(s); xx++) {
1621: grid_view_get_cell(s->grid, xx, s->cy, &gc);
1622: if (~gc.flags & GRID_FLAG_PADDING)
1623: break;
1624: grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
1625: }
1.109 nicm 1626: }
1627:
1628: /* Write cell data, collecting if necessary. */
1629: void
1630: screen_write_collect_add(struct screen_write_ctx *ctx,
1631: const struct grid_cell *gc)
1632: {
1633: struct screen *s = ctx->s;
1634: struct screen_write_collect_item *ci;
1635: u_int sx = screen_size_x(s);
1636: int collect;
1637:
1638: /*
1639: * Don't need to check that the attributes and whatnot are still the
1.116 nicm 1640: * same - input_parse will end the collection when anything that isn't
1.159 nicm 1641: * a plain character is encountered.
1.109 nicm 1642: */
1643:
1644: collect = 1;
1.136 nicm 1645: if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f)
1.109 nicm 1646: collect = 0;
1647: else if (gc->attr & GRID_ATTR_CHARSET)
1648: collect = 0;
1649: else if (~s->mode & MODE_WRAP)
1650: collect = 0;
1651: else if (s->mode & MODE_INSERT)
1652: collect = 0;
1.138 nicm 1653: else if (s->sel != NULL)
1.109 nicm 1654: collect = 0;
1655: if (!collect) {
1656: screen_write_collect_end(ctx);
1.164 nicm 1657: screen_write_collect_flush(ctx, 0, __func__);
1.109 nicm 1658: screen_write_cell(ctx, gc);
1659: return;
1660: }
1661: ctx->cells++;
1662:
1663: if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
1664: screen_write_collect_end(ctx);
1.118 nicm 1665: ci = ctx->item; /* may have changed */
1666:
1.109 nicm 1667: if (s->cx > sx - 1) {
1.114 nicm 1668: log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.118 nicm 1669: ci->wrapped = 1;
1.122 nicm 1670: screen_write_linefeed(ctx, 1, 8);
1.139 nicm 1671: screen_write_set_cursor(ctx, 0, -1);
1.109 nicm 1672: }
1673:
1674: if (ci->used == 0)
1675: memcpy(&ci->gc, gc, sizeof ci->gc);
1.172 nicm 1676: if (ctx->s->write_list[s->cy].data == NULL)
1677: ctx->s->write_list[s->cy].data = xmalloc(screen_size_x(ctx->s));
1678: ctx->s->write_list[s->cy].data[s->cx + ci->used++] = gc->data.data[0];
1.109 nicm 1679: }
1680:
1.1 nicm 1681: /* Write cell data. */
1682: void
1.60 nicm 1683: screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1.1 nicm 1684: {
1685: struct screen *s = ctx->s;
1686: struct grid *gd = s->grid;
1.109 nicm 1687: struct grid_line *gl;
1688: struct grid_cell_entry *gce;
1689: struct grid_cell tmp_gc, now_gc;
1.15 nicm 1690: struct tty_ctx ttyctx;
1.92 nicm 1691: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.115 nicm 1692: u_int width = gc->data.width, xx, last, cx, cy;
1.109 nicm 1693: int selected, skip = 1;
1.1 nicm 1694:
1.109 nicm 1695: /* Ignore padding cells. */
1.1 nicm 1696: if (gc->flags & GRID_FLAG_PADDING)
1697: return;
1.109 nicm 1698: ctx->cells++;
1.32 nicm 1699:
1.109 nicm 1700: /* If the width is zero, combine onto the previous character. */
1.1 nicm 1701: if (width == 0) {
1.164 nicm 1702: screen_write_collect_flush(ctx, 0, __func__);
1.104 nicm 1703: if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) {
1.115 nicm 1704: cx = s->cx; cy = s->cy;
1.144 nicm 1705: screen_write_set_cursor(ctx, xx, s->cy);
1.163 nicm 1706: screen_write_initctx(ctx, &ttyctx, 0);
1.104 nicm 1707: ttyctx.cell = gc;
1708: tty_write(tty_cmd_cell, &ttyctx);
1.115 nicm 1709: s->cx = cx; s->cy = cy;
1.1 nicm 1710: }
1711: return;
1712: }
1.112 nicm 1713:
1714: /* Flush any existing scrolling. */
1.164 nicm 1715: screen_write_collect_flush(ctx, 1, __func__);
1.1 nicm 1716:
1.109 nicm 1717: /* If this character doesn't fit, ignore it. */
1718: if ((~s->mode & MODE_WRAP) &&
1719: width > 1 &&
1720: (width > sx || (s->cx != sx && s->cx > sx - width)))
1721: return;
1722:
1.6 nicm 1723: /* If in insert mode, make space for the cells. */
1.98 nicm 1724: if (s->mode & MODE_INSERT) {
1.109 nicm 1725: grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
1726: skip = 0;
1727: }
1.6 nicm 1728:
1.20 nicm 1729: /* Check this will fit on the current line and wrap if not. */
1.92 nicm 1730: if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1.128 nicm 1731: log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.122 nicm 1732: screen_write_linefeed(ctx, 1, 8);
1.139 nicm 1733: screen_write_set_cursor(ctx, 0, -1);
1.164 nicm 1734: screen_write_collect_flush(ctx, 1, __func__);
1.1 nicm 1735: }
1736:
1.64 nicm 1737: /* Sanity check cursor position. */
1.92 nicm 1738: if (s->cx > sx - width || s->cy > sy - 1)
1.1 nicm 1739: return;
1.163 nicm 1740: screen_write_initctx(ctx, &ttyctx, 0);
1.106 nicm 1741:
1.1 nicm 1742: /* Handle overwriting of UTF-8 characters. */
1.137 nicm 1743: gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.92 nicm 1744: if (gl->flags & GRID_LINE_EXTENDED) {
1745: grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
1746: if (screen_write_overwrite(ctx, &now_gc, width))
1747: skip = 0;
1748: }
1.1 nicm 1749:
1750: /*
1751: * If the new character is UTF-8 wide, fill in padding cells. Have
1752: * already ensured there is enough room.
1753: */
1.89 nicm 1754: for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.131 nicm 1755: log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
1.184 nicm 1756: grid_view_set_padding(gd, xx, s->cy);
1.89 nicm 1757: skip = 0;
1758: }
1759:
1760: /* If no change, do not draw. */
1.92 nicm 1761: if (skip) {
1762: if (s->cx >= gl->cellsize)
1763: skip = grid_cells_equal(gc, &grid_default_cell);
1764: else {
1765: gce = &gl->celldata[s->cx];
1766: if (gce->flags & GRID_FLAG_EXTENDED)
1767: skip = 0;
1.95 nicm 1768: else if (gc->flags != gce->flags)
1.92 nicm 1769: skip = 0;
1770: else if (gc->attr != gce->data.attr)
1771: skip = 0;
1772: else if (gc->fg != gce->data.fg)
1773: skip = 0;
1774: else if (gc->bg != gce->data.bg)
1775: skip = 0;
1.95 nicm 1776: else if (gc->data.width != 1)
1777: skip = 0;
1.109 nicm 1778: else if (gc->data.size != 1)
1779: skip = 0;
1.95 nicm 1780: else if (gce->data.data != gc->data.data[0])
1.92 nicm 1781: skip = 0;
1782: }
1783: }
1.1 nicm 1784:
1.127 nicm 1785: /* Update the selected flag and set the cell. */
1.90 nicm 1786: selected = screen_check_selection(s, s->cx, s->cy);
1.109 nicm 1787: if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1.90 nicm 1788: memcpy(&tmp_gc, gc, sizeof tmp_gc);
1789: tmp_gc.flags |= GRID_FLAG_SELECTED;
1790: grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1.109 nicm 1791: } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1.90 nicm 1792: memcpy(&tmp_gc, gc, sizeof tmp_gc);
1793: tmp_gc.flags &= ~GRID_FLAG_SELECTED;
1794: grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1795: } else if (!skip)
1.89 nicm 1796: grid_view_set_cell(gd, s->cx, s->cy, gc);
1.109 nicm 1797: if (selected)
1798: skip = 0;
1.1 nicm 1799:
1.64 nicm 1800: /*
1801: * Move the cursor. If not wrapping, stick at the last character and
1802: * replace it.
1803: */
1.65 nicm 1804: last = !(s->mode & MODE_WRAP);
1.92 nicm 1805: if (s->cx <= sx - last - width)
1.139 nicm 1806: screen_write_set_cursor(ctx, s->cx + width, -1);
1.64 nicm 1807: else
1.139 nicm 1808: screen_write_set_cursor(ctx, sx - last, -1);
1.1 nicm 1809:
1.89 nicm 1810: /* Create space for character in insert mode. */
1.109 nicm 1811: if (s->mode & MODE_INSERT) {
1.164 nicm 1812: screen_write_collect_flush(ctx, 0, __func__);
1.17 nicm 1813: ttyctx.num = width;
1814: tty_write(tty_cmd_insertcharacter, &ttyctx);
1815: }
1.89 nicm 1816:
1817: /* Write to the screen. */
1.109 nicm 1818: if (!skip) {
1819: if (selected) {
1820: screen_select_cell(s, &tmp_gc, gc);
1821: ttyctx.cell = &tmp_gc;
1822: } else
1823: ttyctx.cell = gc;
1.16 nicm 1824: tty_write(tty_cmd_cell, &ttyctx);
1.92 nicm 1825: ctx->written++;
1826: } else
1827: ctx->skipped++;
1.35 nicm 1828: }
1829:
1830: /* Combine a UTF-8 zero-width character onto the previous. */
1.104 nicm 1831: static const struct grid_cell *
1832: screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
1833: u_int *xx)
1.35 nicm 1834: {
1835: struct screen *s = ctx->s;
1836: struct grid *gd = s->grid;
1.104 nicm 1837: static struct grid_cell gc;
1838: u_int n;
1.35 nicm 1839:
1840: /* Can't combine if at 0. */
1841: if (s->cx == 0)
1.104 nicm 1842: return (NULL);
1.35 nicm 1843:
1.60 nicm 1844: /* Empty data is out. */
1845: if (ud->size == 0)
1.37 nicm 1846: fatalx("UTF-8 data empty");
1847:
1.60 nicm 1848: /* Retrieve the previous cell. */
1.119 nicm 1849: for (n = 1; n <= s->cx; n++) {
1.104 nicm 1850: grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
1851: if (~gc.flags & GRID_FLAG_PADDING)
1852: break;
1853: }
1.119 nicm 1854: if (n > s->cx)
1.104 nicm 1855: return (NULL);
1856: *xx = s->cx - n;
1.35 nicm 1857:
1.60 nicm 1858: /* Check there is enough space. */
1.77 nicm 1859: if (gc.data.size + ud->size > sizeof gc.data.data)
1.104 nicm 1860: return (NULL);
1861:
1862: log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size,
1863: ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy);
1.35 nicm 1864:
1.77 nicm 1865: /* Append the data. */
1866: memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
1867: gc.data.size += ud->size;
1868:
1869: /* Set the new cell. */
1.104 nicm 1870: grid_view_set_cell(gd, *xx, s->cy, &gc);
1.35 nicm 1871:
1.104 nicm 1872: return (&gc);
1.1 nicm 1873: }
1874:
1875: /*
1876: * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1877: * cell on the screen, so following cells must not be drawn by marking them as
1878: * padding.
1879: *
1880: * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1881: * character, it is necessary to also overwrite any other cells which covered
1882: * by the same character.
1883: */
1.89 nicm 1884: static int
1885: screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
1886: u_int width)
1.1 nicm 1887: {
1888: struct screen *s = ctx->s;
1889: struct grid *gd = s->grid;
1.89 nicm 1890: struct grid_cell tmp_gc;
1.1 nicm 1891: u_int xx;
1.89 nicm 1892: int done = 0;
1.1 nicm 1893:
1.89 nicm 1894: if (gc->flags & GRID_FLAG_PADDING) {
1.1 nicm 1895: /*
1896: * A padding cell, so clear any following and leading padding
1897: * cells back to the character. Don't overwrite the current
1898: * cell as that happens later anyway.
1899: */
1900: xx = s->cx + 1;
1901: while (--xx > 0) {
1.89 nicm 1902: grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1903: if (~tmp_gc.flags & GRID_FLAG_PADDING)
1.1 nicm 1904: break;
1.131 nicm 1905: log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
1.1 nicm 1906: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1907: }
1908:
1909: /* Overwrite the character at the start of this padding. */
1.131 nicm 1910: log_debug("%s: character at %u,%u", __func__, xx, s->cy);
1.1 nicm 1911: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1.89 nicm 1912: done = 1;
1.43 nicm 1913: }
1.1 nicm 1914:
1.43 nicm 1915: /*
1.95 nicm 1916: * Overwrite any padding cells that belong to any UTF-8 characters
1917: * we'll be overwriting with the current character.
1.43 nicm 1918: */
1.95 nicm 1919: if (width != 1 ||
1920: gc->data.width != 1 ||
1921: gc->flags & GRID_FLAG_PADDING) {
1.89 nicm 1922: xx = s->cx + width - 1;
1923: while (++xx < screen_size_x(s)) {
1924: grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
1925: if (~tmp_gc.flags & GRID_FLAG_PADDING)
1926: break;
1.160 nicm 1927: log_debug("%s: overwrite at %u,%u", __func__, xx,
1928: s->cy);
1.89 nicm 1929: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1930: done = 1;
1931: }
1.1 nicm 1932: }
1.89 nicm 1933:
1934: return (done);
1.50 nicm 1935: }
1936:
1.107 nicm 1937: /* Set external clipboard. */
1.50 nicm 1938: void
1939: screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
1940: {
1941: struct tty_ctx ttyctx;
1942:
1.163 nicm 1943: screen_write_initctx(ctx, &ttyctx, 0);
1.50 nicm 1944: ttyctx.ptr = str;
1945: ttyctx.num = len;
1946:
1947: tty_write(tty_cmd_setselection, &ttyctx);
1.47 nicm 1948: }
1949:
1.107 nicm 1950: /* Write unmodified string. */
1.47 nicm 1951: void
1952: screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
1953: {
1.87 nicm 1954: struct tty_ctx ttyctx;
1.47 nicm 1955:
1.163 nicm 1956: screen_write_initctx(ctx, &ttyctx, 0);
1.47 nicm 1957: ttyctx.ptr = str;
1958: ttyctx.num = len;
1959:
1960: tty_write(tty_cmd_rawstring, &ttyctx);
1.178 nicm 1961: }
1962:
1963: /* Turn alternate screen on. */
1964: void
1965: screen_write_alternateon(struct screen_write_ctx *ctx, struct grid_cell *gc,
1966: int cursor)
1967: {
1968: struct tty_ctx ttyctx;
1969: struct window_pane *wp = ctx->wp;
1970:
1971: if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
1972: return;
1973: screen_alternate_on(ctx->s, gc, cursor);
1974:
1975: screen_write_initctx(ctx, &ttyctx, 1);
1976: ttyctx.redraw_cb(&ttyctx);
1977: }
1978:
1979: /* Turn alternate screen off. */
1980: void
1981: screen_write_alternateoff(struct screen_write_ctx *ctx, struct grid_cell *gc,
1982: int cursor)
1983: {
1984: struct tty_ctx ttyctx;
1985: struct window_pane *wp = ctx->wp;
1986:
1987: if (wp != NULL && !options_get_number(wp->options, "alternate-screen"))
1988: return;
1989: screen_alternate_off(ctx->s, gc, cursor);
1990:
1991: screen_write_initctx(ctx, &ttyctx, 1);
1992: ttyctx.redraw_cb(&ttyctx);
1.1 nicm 1993: }