Annotation of src/usr.bin/tmux/screen-write.c, Revision 1.34
1.34 ! nicm 1: /* $OpenBSD: screen-write.c,v 1.33 2009/10/17 08:35:38 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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:
21: #include <string.h>
22:
23: #include "tmux.h"
24:
1.31 nicm 25: void screen_write_initctx(struct screen_write_ctx *, struct tty_ctx *, int);
1.1 nicm 26: void screen_write_overwrite(struct screen_write_ctx *);
27:
28: /* Initialise writing with a window. */
29: void
30: screen_write_start(
31: struct screen_write_ctx *ctx, struct window_pane *wp, struct screen *s)
32: {
33: ctx->wp = wp;
34: if (wp != NULL && s == NULL)
35: ctx->s = wp->screen;
36: else
37: ctx->s = s;
38: }
39:
40: /* Finish writing. */
41: void
42: screen_write_stop(unused struct screen_write_ctx *ctx)
43: {
44: }
45:
46: /* Write character. */
47: void
48: screen_write_putc(
49: struct screen_write_ctx *ctx, struct grid_cell *gc, u_char ch)
50: {
51: gc->data = ch;
52: screen_write_cell(ctx, gc, NULL);
53: }
54:
1.24 nicm 55: /* Calculate string length, with embedded formatting. */
56: size_t printflike2
57: screen_write_cstrlen(int utf8flag, const char *fmt, ...)
58: {
59: va_list ap;
60: char *msg, *msg2, *ptr, *ptr2;
61: size_t size;
62:
63: va_start(ap, fmt);
64: xvasprintf(&msg, fmt, ap);
65: va_end(ap);
66: msg2 = xmalloc(strlen(msg) + 1);
67:
68: ptr = msg;
69: ptr2 = msg2;
70: while (*ptr != '\0') {
71: if (ptr[0] == '#' && ptr[1] == '[') {
72: while (*ptr != ']' && *ptr != '\0')
73: ptr++;
74: if (*ptr == ']')
75: ptr++;
76: continue;
77: }
78: *ptr2++ = *ptr++;
79: }
80: *ptr2 = '\0';
81:
82: size = screen_write_strlen(utf8flag, "%s", msg2);
83:
84: xfree(msg);
85: xfree(msg2);
86:
87: return (size);
88: }
89:
1.2 nicm 90: /* Calculate string length. */
1.3 nicm 91: size_t printflike2
92: screen_write_strlen(int utf8flag, const char *fmt, ...)
1.2 nicm 93: {
94: va_list ap;
95: char *msg;
96: u_char *ptr, utf8buf[4];
97: size_t left, size = 0;
98:
99: va_start(ap, fmt);
100: xvasprintf(&msg, fmt, ap);
101: va_end(ap);
102:
103: ptr = msg;
104: while (*ptr != '\0') {
1.3 nicm 105: if (utf8flag && *ptr > 0x7f) {
1.2 nicm 106: memset(utf8buf, 0xff, sizeof utf8buf);
107:
108: left = strlen(ptr);
109: if (*ptr >= 0xc2 && *ptr <= 0xdf && left >= 2) {
110: memcpy(utf8buf, ptr, 2);
111: ptr += 2;
112: } else if (*ptr >= 0xe0 && *ptr <= 0xef && left >= 3) {
113: memcpy(utf8buf, ptr, 3);
114: ptr += 3;
115: } else if (*ptr >= 0xf0 && *ptr <= 0xf4 && left >= 4) {
116: memcpy(utf8buf, ptr, 4);
117: ptr += 4;
118: } else {
119: *utf8buf = *ptr;
120: ptr++;
121: }
122: size += utf8_width(utf8buf);
123: } else {
124: size++;
125: ptr++;
126: }
1.7 ray 127: }
1.2 nicm 128:
1.19 nicm 129: xfree(msg);
1.2 nicm 130: return (size);
131: }
132:
1.3 nicm 133: /* Write simple string (no UTF-8 or maximum length). */
1.1 nicm 134: void printflike3
135: screen_write_puts(
136: struct screen_write_ctx *ctx, struct grid_cell *gc, const char *fmt, ...)
137: {
138: va_list ap;
139:
140: va_start(ap, fmt);
1.3 nicm 141: screen_write_vnputs(ctx, -1, gc, 0, fmt, ap);
1.2 nicm 142: va_end(ap);
143: }
144:
145: /* Write string with length limit (-1 for unlimited). */
1.3 nicm 146: void printflike5
1.2 nicm 147: screen_write_nputs(struct screen_write_ctx *ctx,
1.3 nicm 148: ssize_t maxlen, struct grid_cell *gc, int utf8flag, const char *fmt, ...)
1.2 nicm 149: {
150: va_list ap;
151:
152: va_start(ap, fmt);
1.3 nicm 153: screen_write_vnputs(ctx, maxlen, gc, utf8flag, fmt, ap);
1.2 nicm 154: va_end(ap);
155: }
156:
157: void
1.3 nicm 158: screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
159: struct grid_cell *gc, int utf8flag, const char *fmt, va_list ap)
1.2 nicm 160: {
161: char *msg;
162: u_char *ptr, utf8buf[4];
163: size_t left, size = 0;
164: int width;
165:
1.1 nicm 166: xvasprintf(&msg, fmt, ap);
167:
1.2 nicm 168: ptr = msg;
169: while (*ptr != '\0') {
1.3 nicm 170: if (utf8flag && *ptr > 0x7f) {
1.2 nicm 171: memset(utf8buf, 0xff, sizeof utf8buf);
172:
173: left = strlen(ptr);
174: if (*ptr >= 0xc2 && *ptr <= 0xdf && left >= 2) {
175: memcpy(utf8buf, ptr, 2);
176: ptr += 2;
177: } else if (*ptr >= 0xe0 && *ptr <= 0xef && left >= 3) {
178: memcpy(utf8buf, ptr, 3);
179: ptr += 3;
180: } else if (*ptr >= 0xf0 && *ptr <= 0xf4 && left >= 4) {
181: memcpy(utf8buf, ptr, 4);
182: ptr += 4;
183: } else {
184: *utf8buf = *ptr;
185: ptr++;
186: }
1.7 ray 187:
1.2 nicm 188: width = utf8_width(utf8buf);
189: if (maxlen > 0 && size + width > (size_t) maxlen) {
190: while (size < (size_t) maxlen) {
191: screen_write_putc(ctx, gc, ' ');
192: size++;
193: }
194: break;
195: }
196: size += width;
197:
198: gc->flags |= GRID_FLAG_UTF8;
199: screen_write_cell(ctx, gc, utf8buf);
200: gc->flags &= ~GRID_FLAG_UTF8;
201:
202: } else {
1.8 nicm 203: if (maxlen > 0 && size + 1 > (size_t) maxlen)
1.2 nicm 204: break;
205:
206: size++;
207: screen_write_putc(ctx, gc, *ptr);
208: ptr++;
209: }
210: }
1.1 nicm 211:
212: xfree(msg);
1.24 nicm 213: }
214:
215: /* Write string, similar to nputs, but with embedded formatting (#[]). */
216: void printflike5
217: screen_write_cnputs(struct screen_write_ctx *ctx,
218: ssize_t maxlen, struct grid_cell *gc, int utf8flag, const char *fmt, ...)
219: {
220: struct grid_cell lgc;
221: va_list ap;
222: char *msg;
223: u_char *ptr, *last, utf8buf[4];
224: size_t left, size = 0;
225: int width;
226:
227: va_start(ap, fmt);
228: xvasprintf(&msg, fmt, ap);
229: va_end(ap);
230:
231: memcpy(&lgc, gc, sizeof lgc);
232:
233: ptr = msg;
234: while (*ptr != '\0') {
235: if (ptr[0] == '#' && ptr[1] == '[') {
236: ptr += 2;
237: last = ptr + strcspn(ptr, "]");
238: if (*last == '\0') {
239: /* No ]. Not much point in doing anything. */
240: break;
241: }
242: *last = '\0';
243:
244: screen_write_parsestyle(gc, &lgc, ptr);
245: ptr = last + 1;
246: continue;
247: }
248:
249: if (utf8flag && *ptr > 0x7f) {
250: memset(utf8buf, 0xff, sizeof utf8buf);
251:
252: left = strlen(ptr);
253: if (*ptr >= 0xc2 && *ptr <= 0xdf && left >= 2) {
254: memcpy(utf8buf, ptr, 2);
255: ptr += 2;
256: } else if (*ptr >= 0xe0 && *ptr <= 0xef && left >= 3) {
257: memcpy(utf8buf, ptr, 3);
258: ptr += 3;
259: } else if (*ptr >= 0xf0 && *ptr <= 0xf4 && left >= 4) {
260: memcpy(utf8buf, ptr, 4);
261: ptr += 4;
262: } else {
263: *utf8buf = *ptr;
264: ptr++;
265: }
266:
267: width = utf8_width(utf8buf);
268: if (maxlen > 0 && size + width > (size_t) maxlen) {
269: while (size < (size_t) maxlen) {
270: screen_write_putc(ctx, gc, ' ');
271: size++;
272: }
273: break;
274: }
275: size += width;
276:
277: lgc.flags |= GRID_FLAG_UTF8;
278: screen_write_cell(ctx, &lgc, utf8buf);
279: lgc.flags &= ~GRID_FLAG_UTF8;
280:
281: } else {
282: if (maxlen > 0 && size + 1 > (size_t) maxlen)
283: break;
284:
285: size++;
286: screen_write_putc(ctx, &lgc, *ptr);
287: ptr++;
288: }
289: }
290:
291: xfree(msg);
292: }
293:
294: /* Parse an embedded style of the form "fg=colour,bg=colour,bright,...". */
295: void
296: screen_write_parsestyle(
297: struct grid_cell *defgc, struct grid_cell *gc, const char *in)
298: {
299: const char delimiters[] = " ,";
300: char tmp[32];
301: int val;
302: size_t end;
1.25 nicm 303: u_char fg, bg, attr, flags;
1.24 nicm 304:
305: if (*in == '\0')
306: return;
307: if (strchr(delimiters, in[strlen(in) - 1]) != NULL)
308: return;
309:
310: fg = gc->fg;
311: bg = gc->bg;
1.25 nicm 312: attr = gc->attr;
313: flags = gc->flags;
1.24 nicm 314: do {
315: end = strcspn(in, delimiters);
316: if (end > (sizeof tmp) - 1)
317: return;
318: memcpy(tmp, in, end);
319: tmp[end] = '\0';
320:
321: if (strcasecmp(tmp, "default") == 0) {
322: fg = defgc->fg;
323: bg = defgc->bg;
324: attr = defgc->attr;
325: } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
326: if ((val = colour_fromstring(tmp + 3)) == -1)
327: return;
328: if (*in == 'f' || *in == 'F') {
1.25 nicm 329: if (val != 8) {
330: if (val & 0x100) {
331: flags |= GRID_FLAG_FG256;
332: val &= ~0x100;
333: } else
334: flags &= ~GRID_FLAG_FG256;
1.24 nicm 335: fg = val;
1.25 nicm 336: } else
1.24 nicm 337: fg = defgc->fg;
338: } else if (*in == 'b' || *in == 'B') {
1.25 nicm 339: if (val != 8) {
340: if (val & 0x100) {
341: flags |= GRID_FLAG_BG256;
342: val &= ~0x100;
343: } else
344: flags &= ~GRID_FLAG_BG256;
1.24 nicm 345: bg = val;
1.25 nicm 346: } else
1.24 nicm 347: bg = defgc->bg;
348: } else
349: return;
1.27 nicm 350: } else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
351: if ((val = attributes_fromstring(tmp + 2)) == -1)
352: return;
353: attr &= ~val;
1.24 nicm 354: } else {
355: if ((val = attributes_fromstring(tmp)) == -1)
356: return;
357: attr |= val;
358: }
359:
360: in += end + strspn(in + end, delimiters);
361: } while (*in != '\0');
362: gc->fg = fg;
363: gc->bg = bg;
364: gc->attr = attr;
1.25 nicm 365: gc->flags = flags;
1.1 nicm 366: }
367:
368: /* Copy from another screen. */
369: void
370: screen_write_copy(struct screen_write_ctx *ctx,
371: struct screen *src, u_int px, u_int py, u_int nx, u_int ny)
372: {
373: struct screen *s = ctx->s;
374: struct grid *gd = src->grid;
1.21 nicm 375: struct grid_line *gl;
1.1 nicm 376: const struct grid_cell *gc;
377: u_char *udata;
1.26 nicm 378: u_int xx, yy, cx, cy, ax, bx;
1.1 nicm 379:
380: cx = s->cx;
381: cy = s->cy;
382: for (yy = py; yy < py + ny; yy++) {
1.21 nicm 383: gl = &gd->linedata[yy];
1.26 nicm 384: if (yy < gd->hsize + gd->sy) {
385: /*
386: * Find start and end position and copy between
387: * them. Limit to the real end of the line then use a
388: * clear EOL only if copying to the end, otherwise
389: * could overwrite whatever is there already.
390: */
391: if (px > gl->cellsize)
392: ax = gl->cellsize;
393: else
394: ax = px;
395: if (px + nx == gd->sx && px + nx > gl->cellsize)
396: bx = gl->cellsize;
397: else
398: bx = px + nx;
399: for (xx = ax; xx < bx; xx++) {
400: udata = NULL;
401: if (xx >= gl->cellsize)
402: gc = &grid_default_cell;
403: else {
404: gc = &gl->celldata[xx];
405: if (gc->flags & GRID_FLAG_UTF8)
406: udata = gl->utf8data[xx].data;
407: }
408: screen_write_cell(ctx, gc, udata);
1.1 nicm 409: }
1.26 nicm 410: if (px + nx == gd->sx && px + nx > gl->cellsize)
411: screen_write_clearendofline(ctx);
412: } else
413: screen_write_clearline(ctx);
1.1 nicm 414: cy++;
415: screen_write_cursormove(ctx, cx, cy);
416: }
417: }
418:
1.17 nicm 419: /* Set up context for TTY command. */
1.1 nicm 420: void
1.31 nicm 421: screen_write_initctx(
422: struct screen_write_ctx *ctx, struct tty_ctx *ttyctx, int save_last)
1.1 nicm 423: {
1.31 nicm 424: struct screen *s = ctx->s;
425: struct grid *gd = s->grid;
426: const struct grid_cell *gc;
427: const struct grid_utf8 *gu;
428: u_int xx;
1.1 nicm 429:
1.17 nicm 430: ttyctx->wp = ctx->wp;
1.1 nicm 431:
1.17 nicm 432: ttyctx->ocx = s->cx;
433: ttyctx->ocy = s->cy;
434:
435: ttyctx->orlower = s->rlower;
436: ttyctx->orupper = s->rupper;
1.31 nicm 437:
438: if (!save_last)
439: return;
440:
441: /* Save the last cell on the screen. */
442: gc = NULL;
443: for (xx = 1; xx < screen_size_x(s); xx++) {
444: gc = grid_view_peek_cell(gd, screen_size_x(s) - xx, s->cy);
445: if (!(gc->flags & GRID_FLAG_PADDING))
446: break;
447: }
448: ttyctx->last_width = xx;
449: memcpy(&ttyctx->last_cell, gc, sizeof ttyctx->last_cell);
450: if (gc->flags & GRID_FLAG_UTF8) {
451: gu = grid_view_peek_utf8(gd, screen_size_x(s) - xx, s->cy);
452: memcpy(&ttyctx->last_utf8, gu, sizeof ttyctx->last_utf8);
453: }
1.1 nicm 454: }
455:
456: /* Cursor up by ny. */
457: void
458: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
459: {
460: struct screen *s = ctx->s;
461:
462: if (ny == 0)
463: ny = 1;
464:
1.12 nicm 465: if (s->cy < s->rupper) {
466: /* Above region. */
467: if (ny > s->cy)
468: ny = s->cy;
469: } else {
470: /* Below region. */
471: if (ny > s->cy - s->rupper)
472: ny = s->cy - s->rupper;
473: }
1.1 nicm 474: if (ny == 0)
475: return;
476:
477: s->cy -= ny;
478: }
479:
480: /* Cursor down by ny. */
481: void
482: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
483: {
484: struct screen *s = ctx->s;
485:
486: if (ny == 0)
487: ny = 1;
488:
1.12 nicm 489: if (s->cy > s->rlower) {
490: /* Below region. */
491: if (ny > screen_size_y(s) - 1 - s->cy)
492: ny = screen_size_y(s) - 1 - s->cy;
493: } else {
494: /* Above region. */
495: if (ny > s->rlower - s->cy)
496: ny = s->rlower - s->cy;
497: }
1.1 nicm 498: if (ny == 0)
499: return;
500:
501: s->cy += ny;
502: }
503:
504: /* Cursor right by nx. */
505: void
506: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
507: {
508: struct screen *s = ctx->s;
509:
510: if (nx == 0)
511: nx = 1;
512:
513: if (nx > screen_size_x(s) - 1 - s->cx)
514: nx = screen_size_x(s) - 1 - s->cx;
515: if (nx == 0)
516: return;
517:
518: s->cx += nx;
519: }
520:
521: /* Cursor left by nx. */
522: void
523: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
524: {
525: struct screen *s = ctx->s;
526:
527: if (nx == 0)
528: nx = 1;
529:
530: if (nx > s->cx)
531: nx = s->cx;
532: if (nx == 0)
533: return;
534:
535: s->cx -= nx;
1.5 nicm 536: }
537:
1.29 nicm 538: /* Backspace; cursor left unless at start of wrapped line when can move up. */
539: void
540: screen_write_backspace(struct screen_write_ctx *ctx)
541: {
542: struct screen *s = ctx->s;
543: struct grid_line *gl;
544:
545: if (s->cx == 0) {
546: if (s->cy == 0)
547: return;
548: gl = &s->grid->linedata[s->grid->hsize + s->cy - 1];
549: if (gl->flags & GRID_LINE_WRAPPED) {
550: s->cy--;
551: s->cx = screen_size_x(s) - 1;
552: }
553: } else
554: s->cx--;
555: }
556:
1.5 nicm 557: /* VT100 alignment test. */
558: void
559: screen_write_alignmenttest(struct screen_write_ctx *ctx)
560: {
561: struct screen *s = ctx->s;
1.17 nicm 562: struct tty_ctx ttyctx;
1.5 nicm 563: struct grid_cell gc;
564: u_int xx, yy;
565:
1.31 nicm 566: screen_write_initctx(ctx, &ttyctx, 0);
1.17 nicm 567:
1.5 nicm 568: memcpy(&gc, &grid_default_cell, sizeof gc);
569: gc.data = 'E';
1.7 ray 570:
1.5 nicm 571: for (yy = 0; yy < screen_size_y(s); yy++) {
572: for (xx = 0; xx < screen_size_x(s); xx++)
573: grid_view_set_cell(s->grid, xx, yy, &gc);
574: }
1.7 ray 575:
1.5 nicm 576: s->cx = 0;
577: s->cy = 0;
578:
579: s->rupper = 0;
1.29 nicm 580:
1.5 nicm 581: s->rlower = screen_size_y(s) - 1;
582:
1.17 nicm 583: tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1 nicm 584: }
585:
586: /* Insert nx characters. */
587: void
588: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx)
589: {
590: struct screen *s = ctx->s;
1.17 nicm 591: struct tty_ctx ttyctx;
1.1 nicm 592:
593: if (nx == 0)
594: nx = 1;
595:
1.9 nicm 596: if (nx > screen_size_x(s) - s->cx)
597: nx = screen_size_x(s) - s->cx;
1.1 nicm 598: if (nx == 0)
599: return;
600:
1.31 nicm 601: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 602:
603: if (s->cx <= screen_size_x(s) - 1)
604: grid_view_insert_cells(s->grid, s->cx, s->cy, nx);
605:
1.17 nicm 606: ttyctx.num = nx;
607: tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1 nicm 608: }
609:
610: /* Delete nx characters. */
611: void
612: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx)
613: {
614: struct screen *s = ctx->s;
1.17 nicm 615: struct tty_ctx ttyctx;
1.1 nicm 616:
617: if (nx == 0)
618: nx = 1;
619:
1.9 nicm 620: if (nx > screen_size_x(s) - s->cx)
621: nx = screen_size_x(s) - s->cx;
1.1 nicm 622: if (nx == 0)
623: return;
624:
1.31 nicm 625: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 626:
627: if (s->cx <= screen_size_x(s) - 1)
628: grid_view_delete_cells(s->grid, s->cx, s->cy, nx);
629:
1.17 nicm 630: ttyctx.num = nx;
631: tty_write(tty_cmd_deletecharacter, &ttyctx);
1.1 nicm 632: }
633:
634: /* Insert ny lines. */
635: void
636: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny)
637: {
638: struct screen *s = ctx->s;
1.17 nicm 639: struct tty_ctx ttyctx;
1.1 nicm 640:
641: if (ny == 0)
642: ny = 1;
643:
1.11 nicm 644: if (s->cy < s->rupper || s->cy > s->rlower) {
645: if (ny > screen_size_y(s) - s->cy)
646: ny = screen_size_y(s) - s->cy;
647: if (ny == 0)
648: return;
649:
1.31 nicm 650: screen_write_initctx(ctx, &ttyctx, 0);
1.11 nicm 651:
652: grid_view_insert_lines(s->grid, s->cy, ny);
653:
1.17 nicm 654: ttyctx.num = ny;
655: tty_write(tty_cmd_insertline, &ttyctx);
1.11 nicm 656: return;
657: }
658:
659: if (ny > s->rlower + 1 - s->cy)
660: ny = s->rlower + 1 - s->cy;
1.1 nicm 661: if (ny == 0)
662: return;
1.11 nicm 663:
1.31 nicm 664: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 665:
666: if (s->cy < s->rupper || s->cy > s->rlower)
667: grid_view_insert_lines(s->grid, s->cy, ny);
1.10 nicm 668: else
669: grid_view_insert_lines_region(s->grid, s->rlower, s->cy, ny);
1.1 nicm 670:
1.17 nicm 671: ttyctx.num = ny;
672: tty_write(tty_cmd_insertline, &ttyctx);
1.1 nicm 673: }
674:
675: /* Delete ny lines. */
676: void
677: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny)
678: {
679: struct screen *s = ctx->s;
1.17 nicm 680: struct tty_ctx ttyctx;
1.1 nicm 681:
682: if (ny == 0)
683: ny = 1;
684:
1.11 nicm 685: if (s->cy < s->rupper || s->cy > s->rlower) {
686: if (ny > screen_size_y(s) - s->cy)
687: ny = screen_size_y(s) - s->cy;
688: if (ny == 0)
689: return;
690:
1.31 nicm 691: screen_write_initctx(ctx, &ttyctx, 0);
1.11 nicm 692:
693: grid_view_delete_lines(s->grid, s->cy, ny);
694:
1.17 nicm 695: ttyctx.num = ny;
696: tty_write(tty_cmd_deleteline, &ttyctx);
1.11 nicm 697: return;
698: }
699:
700: if (ny > s->rlower + 1 - s->cy)
701: ny = s->rlower + 1 - s->cy;
1.1 nicm 702: if (ny == 0)
703: return;
704:
1.31 nicm 705: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 706:
707: if (s->cy < s->rupper || s->cy > s->rlower)
708: grid_view_delete_lines(s->grid, s->cy, ny);
1.10 nicm 709: else
710: grid_view_delete_lines_region(s->grid, s->rlower, s->cy, ny);
1.1 nicm 711:
1.17 nicm 712: ttyctx.num = ny;
713: tty_write(tty_cmd_deleteline, &ttyctx);
1.1 nicm 714: }
715:
716: /* Clear line at cursor. */
717: void
718: screen_write_clearline(struct screen_write_ctx *ctx)
719: {
720: struct screen *s = ctx->s;
1.17 nicm 721: struct tty_ctx ttyctx;
1.1 nicm 722:
1.31 nicm 723: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 724:
725: grid_view_clear(s->grid, 0, s->cy, screen_size_x(s), 1);
726:
1.17 nicm 727: tty_write(tty_cmd_clearline, &ttyctx);
1.1 nicm 728: }
729:
730: /* Clear to end of line from cursor. */
731: void
732: screen_write_clearendofline(struct screen_write_ctx *ctx)
733: {
734: struct screen *s = ctx->s;
1.17 nicm 735: struct tty_ctx ttyctx;
1.1 nicm 736: u_int sx;
737:
1.31 nicm 738: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 739:
740: sx = screen_size_x(s);
741:
742: if (s->cx <= sx - 1)
743: grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
744:
1.17 nicm 745: tty_write(tty_cmd_clearendofline, &ttyctx);
1.1 nicm 746: }
747:
748: /* Clear to start of line from cursor. */
749: void
750: screen_write_clearstartofline(struct screen_write_ctx *ctx)
751: {
752: struct screen *s = ctx->s;
1.17 nicm 753: struct tty_ctx ttyctx;
1.1 nicm 754: u_int sx;
755:
1.31 nicm 756: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 757:
758: sx = screen_size_x(s);
759:
760: if (s->cx > sx - 1)
761: grid_view_clear(s->grid, 0, s->cy, sx, 1);
762: else
763: grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
764:
1.17 nicm 765: tty_write(tty_cmd_clearstartofline, &ttyctx);
1.1 nicm 766: }
767:
768: /* Move cursor to px,py. */
769: void
770: screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
771: {
772: struct screen *s = ctx->s;
773:
774: if (px > screen_size_x(s) - 1)
775: px = screen_size_x(s) - 1;
776: if (py > screen_size_y(s) - 1)
777: py = screen_size_y(s) - 1;
778:
779: s->cx = px;
780: s->cy = py;
781: }
782:
783: /* Set cursor mode. */
784: void
785: screen_write_cursormode(struct screen_write_ctx *ctx, int state)
786: {
787: struct screen *s = ctx->s;
788:
789: if (state)
790: s->mode |= MODE_CURSOR;
791: else
792: s->mode &= ~MODE_CURSOR;
793: }
794:
795: /* Reverse index (up with scroll). */
796: void
797: screen_write_reverseindex(struct screen_write_ctx *ctx)
798: {
799: struct screen *s = ctx->s;
1.17 nicm 800: struct tty_ctx ttyctx;
1.1 nicm 801:
1.31 nicm 802: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 803:
804: if (s->cy == s->rupper)
805: grid_view_scroll_region_down(s->grid, s->rupper, s->rlower);
806: else if (s->cy > 0)
807: s->cy--;
808:
1.17 nicm 809: tty_write(tty_cmd_reverseindex, &ttyctx);
1.1 nicm 810: }
811:
812: /* Set scroll region. */
813: void
814: screen_write_scrollregion(
815: struct screen_write_ctx *ctx, u_int rupper, u_int rlower)
816: {
817: struct screen *s = ctx->s;
818:
819: if (rupper > screen_size_y(s) - 1)
820: rupper = screen_size_y(s) - 1;
821: if (rlower > screen_size_y(s) - 1)
822: rlower = screen_size_y(s) - 1;
1.13 nicm 823: if (rupper >= rlower) /* cannot be one line */
1.1 nicm 824: return;
825:
826: /* Cursor moves to top-left. */
827: s->cx = 0;
828: s->cy = 0;
829:
830: s->rupper = rupper;
831: s->rlower = rlower;
832: }
833:
834: /* Set insert mode. */
835: void
836: screen_write_insertmode(struct screen_write_ctx *ctx, int state)
837: {
838: struct screen *s = ctx->s;
839:
840: if (state)
841: s->mode |= MODE_INSERT;
842: else
843: s->mode &= ~MODE_INSERT;
844: }
845:
846: /* Set mouse mode. */
847: void
848: screen_write_mousemode(struct screen_write_ctx *ctx, int state)
849: {
850: struct screen *s = ctx->s;
851:
852: if (state)
853: s->mode |= MODE_MOUSE;
854: else
855: s->mode &= ~MODE_MOUSE;
856: }
857:
1.34 ! nicm 858: /* Line feed. */
1.1 nicm 859: void
1.34 ! nicm 860: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped)
1.1 nicm 861: {
1.20 nicm 862: struct screen *s = ctx->s;
863: struct grid_line *gl;
1.34 ! nicm 864: struct tty_ctx ttyctx;
! 865:
! 866: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 867:
1.20 nicm 868: gl = &s->grid->linedata[s->grid->hsize + s->cy];
869: if (wrapped)
870: gl->flags |= GRID_LINE_WRAPPED;
871: else
872: gl->flags &= ~GRID_LINE_WRAPPED;
873:
1.1 nicm 874: if (s->cy == s->rlower)
875: grid_view_scroll_region_up(s->grid, s->rupper, s->rlower);
876: else if (s->cy < screen_size_y(s) - 1)
877: s->cy++;
878:
1.34 ! nicm 879: ttyctx.num = wrapped;
1.17 nicm 880: tty_write(tty_cmd_linefeed, &ttyctx);
1.1 nicm 881: }
882:
883: /* Carriage return (cursor to start of line). */
884: void
885: screen_write_carriagereturn(struct screen_write_ctx *ctx)
886: {
887: struct screen *s = ctx->s;
888:
889: s->cx = 0;
890: }
891:
892: /* Set keypad cursor keys mode. */
893: void
894: screen_write_kcursormode(struct screen_write_ctx *ctx, int state)
895: {
896: struct screen *s = ctx->s;
897:
898: if (state)
899: s->mode |= MODE_KCURSOR;
900: else
901: s->mode &= ~MODE_KCURSOR;
902: }
903:
904: /* Set keypad number keys mode. */
905: void
906: screen_write_kkeypadmode(struct screen_write_ctx *ctx, int state)
907: {
908: struct screen *s = ctx->s;
909:
910: if (state)
911: s->mode |= MODE_KKEYPAD;
912: else
913: s->mode &= ~MODE_KKEYPAD;
914: }
915:
916: /* Clear to end of screen from cursor. */
917: void
918: screen_write_clearendofscreen(struct screen_write_ctx *ctx)
919: {
920: struct screen *s = ctx->s;
1.17 nicm 921: struct tty_ctx ttyctx;
1.1 nicm 922: u_int sx, sy;
923:
1.31 nicm 924: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 925:
926: sx = screen_size_x(s);
927: sy = screen_size_y(s);
928:
929: if (s->cx <= sx - 1)
930: grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
931: grid_view_clear(s->grid, 0, s->cy + 1, sx, sy - (s->cy + 1));
932:
1.17 nicm 933: tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1 nicm 934: }
935:
936: /* Clear to start of screen. */
937: void
938: screen_write_clearstartofscreen(struct screen_write_ctx *ctx)
939: {
940: struct screen *s = ctx->s;
1.17 nicm 941: struct tty_ctx ttyctx;
1.1 nicm 942: u_int sx;
943:
1.31 nicm 944: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 945:
946: sx = screen_size_x(s);
947:
948: if (s->cy > 0)
1.4 nicm 949: grid_view_clear(s->grid, 0, 0, sx, s->cy);
1.1 nicm 950: if (s->cx > sx - 1)
951: grid_view_clear(s->grid, 0, s->cy, sx, 1);
952: else
1.4 nicm 953: grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
1.1 nicm 954:
1.17 nicm 955: tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1 nicm 956: }
957:
958: /* Clear entire screen. */
959: void
960: screen_write_clearscreen(struct screen_write_ctx *ctx)
961: {
962: struct screen *s = ctx->s;
1.17 nicm 963: struct tty_ctx ttyctx;
1.1 nicm 964:
1.31 nicm 965: screen_write_initctx(ctx, &ttyctx, 0);
1.1 nicm 966:
967: grid_view_clear(s->grid, 0, 0, screen_size_x(s), screen_size_y(s));
968:
1.17 nicm 969: tty_write(tty_cmd_clearscreen, &ttyctx);
1.1 nicm 970: }
971:
972: /* Write cell data. */
973: void
974: screen_write_cell(
975: struct screen_write_ctx *ctx, const struct grid_cell *gc, u_char *udata)
976: {
977: struct screen *s = ctx->s;
978: struct grid *gd = s->grid;
1.15 nicm 979: struct tty_ctx ttyctx;
1.1 nicm 980: struct grid_utf8 gu, *tmp_gu;
981: u_int width, xx, i;
1.33 nicm 982: struct grid_cell tmp_gc, *tmp_gcp;
1.6 nicm 983: int insert = 0;
1.1 nicm 984:
985: /* Ignore padding. */
986: if (gc->flags & GRID_FLAG_PADDING)
987: return;
988:
989: /* Find character width. */
990: if (gc->flags & GRID_FLAG_UTF8) {
991: width = utf8_width(udata);
992:
993: gu.width = width;
994: memcpy(&gu.data, udata, sizeof gu.data);
995: } else
996: width = 1;
997:
1.32 nicm 998: /*
999: * If this is a wide character and there is no room on the screen, for
1000: * the entire character, don't print it.
1001: */
1002: if (width > 1 && (width > screen_size_x(s) ||
1003: (s->cx != screen_size_x(s) && s->cx > screen_size_x(s) - width)))
1004: return;
1005:
1.1 nicm 1006: /* If the width is zero, combine onto the previous character. */
1007: if (width == 0) {
1008: if (s->cx == 0)
1009: return;
1.18 nicm 1010: tmp_gcp = grid_view_get_cell(gd, s->cx - 1, s->cy);
1011: if (!(tmp_gcp->flags & GRID_FLAG_UTF8)) {
1012: tmp_gcp->flags |= GRID_FLAG_UTF8;
1.1 nicm 1013: memset(&gu.data, 0xff, sizeof gu.data);
1.18 nicm 1014: *gu.data = tmp_gcp->data;
1.1 nicm 1015: gu.width = 1;
1016: grid_view_set_utf8(gd, s->cx - 1, s->cy, &gu);
1017: }
1018: tmp_gu = grid_view_get_utf8(gd, s->cx - 1, s->cy);
1019:
1020: for (i = 0; i < UTF8_SIZE; i++) {
1021: if (tmp_gu->data[i] == 0xff)
1022: break;
1023: }
1024: memcpy(tmp_gu->data + i, udata, UTF8_SIZE - i);
1025:
1.31 nicm 1026: /*
1027: * Assume the previous character has just been input.
1028: * XXX There is no guarantee this is true, need to redraw
1029: * entire line.
1030: */
1031: screen_write_initctx(ctx, &ttyctx, 0);
1.17 nicm 1032: ttyctx.ptr = udata;
1033: tty_write(tty_cmd_utf8character, &ttyctx);
1.1 nicm 1034: return;
1035: }
1036:
1.31 nicm 1037: /* Initialise the redraw context, saving the last cell. */
1038: screen_write_initctx(ctx, &ttyctx, 1);
1039:
1.6 nicm 1040: /* If in insert mode, make space for the cells. */
1041: if (s->mode & MODE_INSERT && s->cx <= screen_size_x(s) - width) {
1042: xx = screen_size_x(s) - s->cx - width;
1043: grid_move_cells(s->grid, s->cx + width, s->cx, s->cy, xx);
1044: insert = 1;
1045: }
1046:
1.20 nicm 1047: /* Check this will fit on the current line and wrap if not. */
1.1 nicm 1048: if (s->cx > screen_size_x(s) - width) {
1.30 nicm 1049: /*
1050: * Don't update the terminal now, just update the screen and
1051: * leave the cursor to scroll naturally, unless this is only
1052: * part of the screen width.
1053: */
1.34 ! nicm 1054: screen_write_linefeed(ctx, 1);
1.30 nicm 1055: s->cx = 0; /* carriage return */
1.1 nicm 1056: }
1057:
1058: /* Sanity checks. */
1059: if (s->cx > screen_size_x(s) - 1 || s->cy > screen_size_y(s) - 1)
1060: return;
1061:
1062: /* Handle overwriting of UTF-8 characters. */
1063: screen_write_overwrite(ctx);
1064:
1065: /*
1066: * If the new character is UTF-8 wide, fill in padding cells. Have
1067: * already ensured there is enough room.
1068: */
1069: for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.18 nicm 1070: tmp_gcp = grid_view_get_cell(gd, xx, s->cy);
1071: if (tmp_gcp != NULL)
1072: tmp_gcp->flags |= GRID_FLAG_PADDING;
1.1 nicm 1073: }
1074:
1075: /* Set the cell. */
1076: grid_view_set_cell(gd, s->cx, s->cy, gc);
1077: if (gc->flags & GRID_FLAG_UTF8)
1078: grid_view_set_utf8(gd, s->cx, s->cy, &gu);
1079:
1080: /* Move the cursor. */
1081: s->cx += width;
1082:
1083: /* Draw to the screen if necessary. */
1.17 nicm 1084: if (insert) {
1085: ttyctx.num = width;
1086: tty_write(tty_cmd_insertcharacter, &ttyctx);
1087: }
1.15 nicm 1088: ttyctx.utf8 = &gu;
1.1 nicm 1089: if (screen_check_selection(s, s->cx - width, s->cy)) {
1.33 nicm 1090: memcpy(&tmp_gc, &s->sel.cell, sizeof tmp_gc);
1091: tmp_gc.data = gc->data;
1092: tmp_gc.flags = gc->flags &
1.28 nicm 1093: ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
1.33 nicm 1094: tmp_gc.flags |= s->sel.cell.flags &
1.28 nicm 1095: (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.33 nicm 1096: ttyctx.cell = &tmp_gc;
1.16 nicm 1097: tty_write(tty_cmd_cell, &ttyctx);
1.15 nicm 1098: } else {
1099: ttyctx.cell = gc;
1.16 nicm 1100: tty_write(tty_cmd_cell, &ttyctx);
1.15 nicm 1101: }
1.1 nicm 1102: }
1103:
1104: /*
1105: * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1106: * cell on the screen, so following cells must not be drawn by marking them as
1107: * padding.
1108: *
1109: * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1110: * character, it is necessary to also overwrite any other cells which covered
1111: * by the same character.
1112: */
1113: void
1114: screen_write_overwrite(struct screen_write_ctx *ctx)
1115: {
1116: struct screen *s = ctx->s;
1117: struct grid *gd = s->grid;
1118: const struct grid_cell *gc;
1119: const struct grid_utf8 *gu;
1120: u_int xx;
1121:
1122: gc = grid_view_peek_cell(gd, s->cx, s->cy);
1123: if (gc->flags & GRID_FLAG_PADDING) {
1124: /*
1125: * A padding cell, so clear any following and leading padding
1126: * cells back to the character. Don't overwrite the current
1127: * cell as that happens later anyway.
1128: */
1129: xx = s->cx + 1;
1130: while (--xx > 0) {
1131: gc = grid_view_peek_cell(gd, xx, s->cy);
1132: if (!(gc->flags & GRID_FLAG_PADDING))
1133: break;
1134: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1135: }
1136:
1137: /* Overwrite the character at the start of this padding. */
1138: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1139:
1140: /* Overwrite following padding cells. */
1141: xx = s->cx;
1142: while (++xx < screen_size_x(s)) {
1143: gc = grid_view_peek_cell(gd, xx, s->cy);
1144: if (!(gc->flags & GRID_FLAG_PADDING))
1145: break;
1146: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1147: }
1.22 nicm 1148: } else if (gc->flags & GRID_FLAG_UTF8) {
1149: gu = grid_view_peek_utf8(gd, s->cx, s->cy);
1150: if (gu->width > 1) {
1151: /*
1152: * An UTF-8 wide cell; overwrite following padding cells only.
1153: */
1154: xx = s->cx;
1155: while (++xx < screen_size_x(s)) {
1156: gc = grid_view_peek_cell(gd, xx, s->cy);
1157: if (!(gc->flags & GRID_FLAG_PADDING))
1158: break;
1159: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1160: }
1.1 nicm 1161: }
1162: }
1163: }