Annotation of src/usr.bin/tmux/screen-write.c, Revision 1.29
1.29 ! nicm 1: /* $OpenBSD: screen-write.c,v 1.28 2009/10/12 16:33:39 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.17 nicm 25: void screen_write_initctx(struct screen_write_ctx *, struct tty_ctx *);
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.17 nicm 421: screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
1.1 nicm 422: {
423: struct screen *s = ctx->s;
424:
1.17 nicm 425: ttyctx->wp = ctx->wp;
1.1 nicm 426:
1.17 nicm 427: ttyctx->ocx = s->cx;
428: ttyctx->ocy = s->cy;
429:
430: ttyctx->orlower = s->rlower;
431: ttyctx->orupper = s->rupper;
1.1 nicm 432: }
433:
434: /* Cursor up by ny. */
435: void
436: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
437: {
438: struct screen *s = ctx->s;
439:
440: if (ny == 0)
441: ny = 1;
442:
1.12 nicm 443: if (s->cy < s->rupper) {
444: /* Above region. */
445: if (ny > s->cy)
446: ny = s->cy;
447: } else {
448: /* Below region. */
449: if (ny > s->cy - s->rupper)
450: ny = s->cy - s->rupper;
451: }
1.1 nicm 452: if (ny == 0)
453: return;
454:
455: s->cy -= ny;
456: }
457:
458: /* Cursor down by ny. */
459: void
460: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
461: {
462: struct screen *s = ctx->s;
463:
464: if (ny == 0)
465: ny = 1;
466:
1.12 nicm 467: if (s->cy > s->rlower) {
468: /* Below region. */
469: if (ny > screen_size_y(s) - 1 - s->cy)
470: ny = screen_size_y(s) - 1 - s->cy;
471: } else {
472: /* Above region. */
473: if (ny > s->rlower - s->cy)
474: ny = s->rlower - s->cy;
475: }
1.1 nicm 476: if (ny == 0)
477: return;
478:
479: s->cy += ny;
480: }
481:
482: /* Cursor right by nx. */
483: void
484: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
485: {
486: struct screen *s = ctx->s;
487:
488: if (nx == 0)
489: nx = 1;
490:
491: if (nx > screen_size_x(s) - 1 - s->cx)
492: nx = screen_size_x(s) - 1 - s->cx;
493: if (nx == 0)
494: return;
495:
496: s->cx += nx;
497: }
498:
499: /* Cursor left by nx. */
500: void
501: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
502: {
503: struct screen *s = ctx->s;
504:
505: if (nx == 0)
506: nx = 1;
507:
508: if (nx > s->cx)
509: nx = s->cx;
510: if (nx == 0)
511: return;
512:
513: s->cx -= nx;
1.5 nicm 514: }
515:
1.29 ! nicm 516: /* Backspace; cursor left unless at start of wrapped line when can move up. */
! 517: void
! 518: screen_write_backspace(struct screen_write_ctx *ctx)
! 519: {
! 520: struct screen *s = ctx->s;
! 521: struct grid_line *gl;
! 522:
! 523: if (s->cx == 0) {
! 524: if (s->cy == 0)
! 525: return;
! 526: gl = &s->grid->linedata[s->grid->hsize + s->cy - 1];
! 527: if (gl->flags & GRID_LINE_WRAPPED) {
! 528: s->cy--;
! 529: s->cx = screen_size_x(s) - 1;
! 530: }
! 531: } else
! 532: s->cx--;
! 533: }
! 534:
1.5 nicm 535: /* VT100 alignment test. */
536: void
537: screen_write_alignmenttest(struct screen_write_ctx *ctx)
538: {
539: struct screen *s = ctx->s;
1.17 nicm 540: struct tty_ctx ttyctx;
1.5 nicm 541: struct grid_cell gc;
542: u_int xx, yy;
543:
1.17 nicm 544: screen_write_initctx(ctx, &ttyctx);
545:
1.5 nicm 546: memcpy(&gc, &grid_default_cell, sizeof gc);
547: gc.data = 'E';
1.7 ray 548:
1.5 nicm 549: for (yy = 0; yy < screen_size_y(s); yy++) {
550: for (xx = 0; xx < screen_size_x(s); xx++)
551: grid_view_set_cell(s->grid, xx, yy, &gc);
552: }
1.7 ray 553:
1.5 nicm 554: s->cx = 0;
555: s->cy = 0;
556:
557: s->rupper = 0;
1.29 ! nicm 558:
1.5 nicm 559: s->rlower = screen_size_y(s) - 1;
560:
1.17 nicm 561: tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1 nicm 562: }
563:
564: /* Insert nx characters. */
565: void
566: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx)
567: {
568: struct screen *s = ctx->s;
1.17 nicm 569: struct tty_ctx ttyctx;
1.1 nicm 570:
571: if (nx == 0)
572: nx = 1;
573:
1.9 nicm 574: if (nx > screen_size_x(s) - s->cx)
575: nx = screen_size_x(s) - s->cx;
1.1 nicm 576: if (nx == 0)
577: return;
578:
1.17 nicm 579: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 580:
581: if (s->cx <= screen_size_x(s) - 1)
582: grid_view_insert_cells(s->grid, s->cx, s->cy, nx);
583:
1.17 nicm 584: ttyctx.num = nx;
585: tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1 nicm 586: }
587:
588: /* Delete nx characters. */
589: void
590: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx)
591: {
592: struct screen *s = ctx->s;
1.17 nicm 593: struct tty_ctx ttyctx;
1.1 nicm 594:
595: if (nx == 0)
596: nx = 1;
597:
1.9 nicm 598: if (nx > screen_size_x(s) - s->cx)
599: nx = screen_size_x(s) - s->cx;
1.1 nicm 600: if (nx == 0)
601: return;
602:
1.17 nicm 603: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 604:
605: if (s->cx <= screen_size_x(s) - 1)
606: grid_view_delete_cells(s->grid, s->cx, s->cy, nx);
607:
1.17 nicm 608: ttyctx.num = nx;
609: tty_write(tty_cmd_deletecharacter, &ttyctx);
1.1 nicm 610: }
611:
612: /* Insert ny lines. */
613: void
614: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny)
615: {
616: struct screen *s = ctx->s;
1.17 nicm 617: struct tty_ctx ttyctx;
1.1 nicm 618:
619: if (ny == 0)
620: ny = 1;
621:
1.11 nicm 622: if (s->cy < s->rupper || s->cy > s->rlower) {
623: if (ny > screen_size_y(s) - s->cy)
624: ny = screen_size_y(s) - s->cy;
625: if (ny == 0)
626: return;
627:
1.17 nicm 628: screen_write_initctx(ctx, &ttyctx);
1.11 nicm 629:
630: grid_view_insert_lines(s->grid, s->cy, ny);
631:
1.17 nicm 632: ttyctx.num = ny;
633: tty_write(tty_cmd_insertline, &ttyctx);
1.11 nicm 634: return;
635: }
636:
637: if (ny > s->rlower + 1 - s->cy)
638: ny = s->rlower + 1 - s->cy;
1.1 nicm 639: if (ny == 0)
640: return;
1.11 nicm 641:
1.17 nicm 642: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 643:
644: if (s->cy < s->rupper || s->cy > s->rlower)
645: grid_view_insert_lines(s->grid, s->cy, ny);
1.10 nicm 646: else
647: grid_view_insert_lines_region(s->grid, s->rlower, s->cy, ny);
1.1 nicm 648:
1.17 nicm 649: ttyctx.num = ny;
650: tty_write(tty_cmd_insertline, &ttyctx);
1.1 nicm 651: }
652:
653: /* Delete ny lines. */
654: void
655: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny)
656: {
657: struct screen *s = ctx->s;
1.17 nicm 658: struct tty_ctx ttyctx;
1.1 nicm 659:
660: if (ny == 0)
661: ny = 1;
662:
1.11 nicm 663: if (s->cy < s->rupper || s->cy > s->rlower) {
664: if (ny > screen_size_y(s) - s->cy)
665: ny = screen_size_y(s) - s->cy;
666: if (ny == 0)
667: return;
668:
1.17 nicm 669: screen_write_initctx(ctx, &ttyctx);
1.11 nicm 670:
671: grid_view_delete_lines(s->grid, s->cy, ny);
672:
1.17 nicm 673: ttyctx.num = ny;
674: tty_write(tty_cmd_deleteline, &ttyctx);
1.11 nicm 675: return;
676: }
677:
678: if (ny > s->rlower + 1 - s->cy)
679: ny = s->rlower + 1 - s->cy;
1.1 nicm 680: if (ny == 0)
681: return;
682:
1.17 nicm 683: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 684:
685: if (s->cy < s->rupper || s->cy > s->rlower)
686: grid_view_delete_lines(s->grid, s->cy, ny);
1.10 nicm 687: else
688: grid_view_delete_lines_region(s->grid, s->rlower, s->cy, ny);
1.1 nicm 689:
1.17 nicm 690: ttyctx.num = ny;
691: tty_write(tty_cmd_deleteline, &ttyctx);
1.1 nicm 692: }
693:
694: /* Clear line at cursor. */
695: void
696: screen_write_clearline(struct screen_write_ctx *ctx)
697: {
698: struct screen *s = ctx->s;
1.17 nicm 699: struct tty_ctx ttyctx;
1.1 nicm 700:
1.17 nicm 701: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 702:
703: grid_view_clear(s->grid, 0, s->cy, screen_size_x(s), 1);
704:
1.17 nicm 705: tty_write(tty_cmd_clearline, &ttyctx);
1.1 nicm 706: }
707:
708: /* Clear to end of line from cursor. */
709: void
710: screen_write_clearendofline(struct screen_write_ctx *ctx)
711: {
712: struct screen *s = ctx->s;
1.17 nicm 713: struct tty_ctx ttyctx;
1.1 nicm 714: u_int sx;
715:
1.17 nicm 716: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 717:
718: sx = screen_size_x(s);
719:
720: if (s->cx <= sx - 1)
721: grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
722:
1.17 nicm 723: tty_write(tty_cmd_clearendofline, &ttyctx);
1.1 nicm 724: }
725:
726: /* Clear to start of line from cursor. */
727: void
728: screen_write_clearstartofline(struct screen_write_ctx *ctx)
729: {
730: struct screen *s = ctx->s;
1.17 nicm 731: struct tty_ctx ttyctx;
1.1 nicm 732: u_int sx;
733:
1.17 nicm 734: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 735:
736: sx = screen_size_x(s);
737:
738: if (s->cx > sx - 1)
739: grid_view_clear(s->grid, 0, s->cy, sx, 1);
740: else
741: grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
742:
1.17 nicm 743: tty_write(tty_cmd_clearstartofline, &ttyctx);
1.1 nicm 744: }
745:
746: /* Move cursor to px,py. */
747: void
748: screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
749: {
750: struct screen *s = ctx->s;
751:
752: if (px > screen_size_x(s) - 1)
753: px = screen_size_x(s) - 1;
754: if (py > screen_size_y(s) - 1)
755: py = screen_size_y(s) - 1;
756:
757: s->cx = px;
758: s->cy = py;
759: }
760:
761: /* Set cursor mode. */
762: void
763: screen_write_cursormode(struct screen_write_ctx *ctx, int state)
764: {
765: struct screen *s = ctx->s;
766:
767: if (state)
768: s->mode |= MODE_CURSOR;
769: else
770: s->mode &= ~MODE_CURSOR;
771: }
772:
773: /* Reverse index (up with scroll). */
774: void
775: screen_write_reverseindex(struct screen_write_ctx *ctx)
776: {
777: struct screen *s = ctx->s;
1.17 nicm 778: struct tty_ctx ttyctx;
1.1 nicm 779:
1.17 nicm 780: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 781:
782: if (s->cy == s->rupper)
783: grid_view_scroll_region_down(s->grid, s->rupper, s->rlower);
784: else if (s->cy > 0)
785: s->cy--;
786:
1.17 nicm 787: tty_write(tty_cmd_reverseindex, &ttyctx);
1.1 nicm 788: }
789:
790: /* Set scroll region. */
791: void
792: screen_write_scrollregion(
793: struct screen_write_ctx *ctx, u_int rupper, u_int rlower)
794: {
795: struct screen *s = ctx->s;
796:
797: if (rupper > screen_size_y(s) - 1)
798: rupper = screen_size_y(s) - 1;
799: if (rlower > screen_size_y(s) - 1)
800: rlower = screen_size_y(s) - 1;
1.13 nicm 801: if (rupper >= rlower) /* cannot be one line */
1.1 nicm 802: return;
803:
804: /* Cursor moves to top-left. */
805: s->cx = 0;
806: s->cy = 0;
807:
808: s->rupper = rupper;
809: s->rlower = rlower;
810: }
811:
812: /* Set insert mode. */
813: void
814: screen_write_insertmode(struct screen_write_ctx *ctx, int state)
815: {
816: struct screen *s = ctx->s;
817:
818: if (state)
819: s->mode |= MODE_INSERT;
820: else
821: s->mode &= ~MODE_INSERT;
822: }
823:
824: /* Set mouse mode. */
825: void
826: screen_write_mousemode(struct screen_write_ctx *ctx, int state)
827: {
828: struct screen *s = ctx->s;
829:
830: if (state)
831: s->mode |= MODE_MOUSE;
832: else
833: s->mode &= ~MODE_MOUSE;
834: }
835:
836: /* Line feed (down with scroll). */
837: void
1.20 nicm 838: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped)
1.1 nicm 839: {
1.20 nicm 840: struct screen *s = ctx->s;
841: struct grid_line *gl;
842: struct tty_ctx ttyctx;
1.1 nicm 843:
1.17 nicm 844: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 845:
1.20 nicm 846: gl = &s->grid->linedata[s->grid->hsize + s->cy];
847: if (wrapped)
848: gl->flags |= GRID_LINE_WRAPPED;
849: else
850: gl->flags &= ~GRID_LINE_WRAPPED;
851:
1.1 nicm 852: if (s->cy == s->rlower)
853: grid_view_scroll_region_up(s->grid, s->rupper, s->rlower);
854: else if (s->cy < screen_size_y(s) - 1)
855: s->cy++;
856:
1.17 nicm 857: tty_write(tty_cmd_linefeed, &ttyctx);
1.1 nicm 858: }
859:
860: /* Carriage return (cursor to start of line). */
861: void
862: screen_write_carriagereturn(struct screen_write_ctx *ctx)
863: {
864: struct screen *s = ctx->s;
865:
866: s->cx = 0;
867: }
868:
869: /* Set keypad cursor keys mode. */
870: void
871: screen_write_kcursormode(struct screen_write_ctx *ctx, int state)
872: {
873: struct screen *s = ctx->s;
874:
875: if (state)
876: s->mode |= MODE_KCURSOR;
877: else
878: s->mode &= ~MODE_KCURSOR;
879: }
880:
881: /* Set keypad number keys mode. */
882: void
883: screen_write_kkeypadmode(struct screen_write_ctx *ctx, int state)
884: {
885: struct screen *s = ctx->s;
886:
887: if (state)
888: s->mode |= MODE_KKEYPAD;
889: else
890: s->mode &= ~MODE_KKEYPAD;
891: }
892:
893: /* Clear to end of screen from cursor. */
894: void
895: screen_write_clearendofscreen(struct screen_write_ctx *ctx)
896: {
897: struct screen *s = ctx->s;
1.17 nicm 898: struct tty_ctx ttyctx;
1.1 nicm 899: u_int sx, sy;
900:
1.17 nicm 901: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 902:
903: sx = screen_size_x(s);
904: sy = screen_size_y(s);
905:
906: if (s->cx <= sx - 1)
907: grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
908: grid_view_clear(s->grid, 0, s->cy + 1, sx, sy - (s->cy + 1));
909:
1.17 nicm 910: tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1 nicm 911: }
912:
913: /* Clear to start of screen. */
914: void
915: screen_write_clearstartofscreen(struct screen_write_ctx *ctx)
916: {
917: struct screen *s = ctx->s;
1.17 nicm 918: struct tty_ctx ttyctx;
1.1 nicm 919: u_int sx;
920:
1.17 nicm 921: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 922:
923: sx = screen_size_x(s);
924:
925: if (s->cy > 0)
1.4 nicm 926: grid_view_clear(s->grid, 0, 0, sx, s->cy);
1.1 nicm 927: if (s->cx > sx - 1)
928: grid_view_clear(s->grid, 0, s->cy, sx, 1);
929: else
1.4 nicm 930: grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
1.1 nicm 931:
1.17 nicm 932: tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1 nicm 933: }
934:
935: /* Clear entire screen. */
936: void
937: screen_write_clearscreen(struct screen_write_ctx *ctx)
938: {
939: struct screen *s = ctx->s;
1.17 nicm 940: struct tty_ctx ttyctx;
1.1 nicm 941:
1.17 nicm 942: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 943:
944: grid_view_clear(s->grid, 0, 0, screen_size_x(s), screen_size_y(s));
945:
1.17 nicm 946: tty_write(tty_cmd_clearscreen, &ttyctx);
1.1 nicm 947: }
948:
949: /* Write cell data. */
950: void
951: screen_write_cell(
952: struct screen_write_ctx *ctx, const struct grid_cell *gc, u_char *udata)
953: {
954: struct screen *s = ctx->s;
955: struct grid *gd = s->grid;
1.15 nicm 956: struct tty_ctx ttyctx;
1.1 nicm 957: struct grid_utf8 gu, *tmp_gu;
958: u_int width, xx, i;
1.18 nicm 959: struct grid_cell tmp_gc, tmp_gc2, *tmp_gcp;
1.6 nicm 960: int insert = 0;
1.1 nicm 961:
962: /* Ignore padding. */
963: if (gc->flags & GRID_FLAG_PADDING)
964: return;
965:
966: /* Find character width. */
967: if (gc->flags & GRID_FLAG_UTF8) {
968: width = utf8_width(udata);
969:
970: gu.width = width;
971: memcpy(&gu.data, udata, sizeof gu.data);
972: } else
973: width = 1;
974:
975: /* If the width is zero, combine onto the previous character. */
976: if (width == 0) {
977: if (s->cx == 0)
978: return;
1.18 nicm 979: tmp_gcp = grid_view_get_cell(gd, s->cx - 1, s->cy);
980: if (!(tmp_gcp->flags & GRID_FLAG_UTF8)) {
981: tmp_gcp->flags |= GRID_FLAG_UTF8;
1.1 nicm 982: memset(&gu.data, 0xff, sizeof gu.data);
1.18 nicm 983: *gu.data = tmp_gcp->data;
1.1 nicm 984: gu.width = 1;
985: grid_view_set_utf8(gd, s->cx - 1, s->cy, &gu);
986: }
987: tmp_gu = grid_view_get_utf8(gd, s->cx - 1, s->cy);
988:
989: for (i = 0; i < UTF8_SIZE; i++) {
990: if (tmp_gu->data[i] == 0xff)
991: break;
992: }
993: memcpy(tmp_gu->data + i, udata, UTF8_SIZE - i);
994:
995: /* Assume the previous character has just been input. */
1.17 nicm 996: screen_write_initctx(ctx, &ttyctx);
997: ttyctx.ptr = udata;
998: tty_write(tty_cmd_utf8character, &ttyctx);
1.1 nicm 999: return;
1000: }
1001:
1002: /* If the character is wider than the screen, don't print it. */
1003: if (width > screen_size_x(s)) {
1004: memcpy(&tmp_gc, gc, sizeof tmp_gc);
1005: tmp_gc.data = '_';
1006: width = 1;
1007: gc = &tmp_gc;
1008: }
1009:
1.6 nicm 1010: /* If in insert mode, make space for the cells. */
1011: if (s->mode & MODE_INSERT && s->cx <= screen_size_x(s) - width) {
1012: xx = screen_size_x(s) - s->cx - width;
1013: grid_move_cells(s->grid, s->cx + width, s->cx, s->cy, xx);
1014: insert = 1;
1015: }
1016:
1.20 nicm 1017: /* Check this will fit on the current line and wrap if not. */
1.1 nicm 1018: if (s->cx > screen_size_x(s) - width) {
1019: screen_write_carriagereturn(ctx);
1.20 nicm 1020: screen_write_linefeed(ctx, 1);
1.1 nicm 1021: }
1022:
1023: /* Sanity checks. */
1024: if (s->cx > screen_size_x(s) - 1 || s->cy > screen_size_y(s) - 1)
1025: return;
1026:
1027: /* Handle overwriting of UTF-8 characters. */
1028: screen_write_overwrite(ctx);
1029:
1030: /*
1031: * If the new character is UTF-8 wide, fill in padding cells. Have
1032: * already ensured there is enough room.
1033: */
1034: for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.18 nicm 1035: tmp_gcp = grid_view_get_cell(gd, xx, s->cy);
1036: if (tmp_gcp != NULL)
1037: tmp_gcp->flags |= GRID_FLAG_PADDING;
1.1 nicm 1038: }
1039:
1040: /* Set the cell. */
1041: grid_view_set_cell(gd, s->cx, s->cy, gc);
1042: if (gc->flags & GRID_FLAG_UTF8)
1043: grid_view_set_utf8(gd, s->cx, s->cy, &gu);
1044:
1045: /* Move the cursor. */
1.17 nicm 1046: screen_write_initctx(ctx, &ttyctx);
1.1 nicm 1047: s->cx += width;
1048:
1049: /* Draw to the screen if necessary. */
1.17 nicm 1050: if (insert) {
1051: ttyctx.num = width;
1052: tty_write(tty_cmd_insertcharacter, &ttyctx);
1053: }
1.15 nicm 1054: ttyctx.utf8 = &gu;
1.1 nicm 1055: if (screen_check_selection(s, s->cx - width, s->cy)) {
1.18 nicm 1056: memcpy(&tmp_gc2, &s->sel.cell, sizeof tmp_gc2);
1057: tmp_gc2.data = gc->data;
1.28 nicm 1058: tmp_gc2.flags = gc->flags &
1059: ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
1060: tmp_gc2.flags |= s->sel.cell.flags &
1061: (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.18 nicm 1062: ttyctx.cell = &tmp_gc2;
1.16 nicm 1063: tty_write(tty_cmd_cell, &ttyctx);
1.15 nicm 1064: } else {
1065: ttyctx.cell = gc;
1.16 nicm 1066: tty_write(tty_cmd_cell, &ttyctx);
1.15 nicm 1067: }
1.1 nicm 1068: }
1069:
1070: /*
1071: * UTF-8 wide characters are a bit of an annoyance. They take up more than one
1072: * cell on the screen, so following cells must not be drawn by marking them as
1073: * padding.
1074: *
1075: * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
1076: * character, it is necessary to also overwrite any other cells which covered
1077: * by the same character.
1078: */
1079: void
1080: screen_write_overwrite(struct screen_write_ctx *ctx)
1081: {
1082: struct screen *s = ctx->s;
1083: struct grid *gd = s->grid;
1084: const struct grid_cell *gc;
1085: const struct grid_utf8 *gu;
1086: u_int xx;
1087:
1088: gc = grid_view_peek_cell(gd, s->cx, s->cy);
1089: if (gc->flags & GRID_FLAG_PADDING) {
1090: /*
1091: * A padding cell, so clear any following and leading padding
1092: * cells back to the character. Don't overwrite the current
1093: * cell as that happens later anyway.
1094: */
1095: xx = s->cx + 1;
1096: while (--xx > 0) {
1097: gc = grid_view_peek_cell(gd, xx, s->cy);
1098: if (!(gc->flags & GRID_FLAG_PADDING))
1099: break;
1100: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1101: }
1102:
1103: /* Overwrite the character at the start of this padding. */
1104: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1105:
1106: /* Overwrite following padding cells. */
1107: xx = s->cx;
1108: while (++xx < screen_size_x(s)) {
1109: gc = grid_view_peek_cell(gd, xx, s->cy);
1110: if (!(gc->flags & GRID_FLAG_PADDING))
1111: break;
1112: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1113: }
1.22 nicm 1114: } else if (gc->flags & GRID_FLAG_UTF8) {
1115: gu = grid_view_peek_utf8(gd, s->cx, s->cy);
1116: if (gu->width > 1) {
1117: /*
1118: * An UTF-8 wide cell; overwrite following padding cells only.
1119: */
1120: xx = s->cx;
1121: while (++xx < screen_size_x(s)) {
1122: gc = grid_view_peek_cell(gd, xx, s->cy);
1123: if (!(gc->flags & GRID_FLAG_PADDING))
1124: break;
1125: grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1126: }
1.1 nicm 1127: }
1128: }
1129: }