Annotation of src/usr.bin/tmux/tty.c, Revision 1.9
1.9 ! nicm 1: /* $OpenBSD: tty.c,v 1.8 2009/06/27 12:57:14 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: #include <sys/ioctl.h>
21:
22: #include <errno.h>
23: #include <fcntl.h>
24: #include <string.h>
25: #include <termios.h>
26: #include <unistd.h>
27:
28: #include "tmux.h"
29:
30: void tty_fill_acs(struct tty *);
31:
32: void tty_raw(struct tty *, const char *);
33:
34: int tty_try_256(struct tty *, u_char, const char *);
35: int tty_try_88(struct tty *, u_char, const char *);
36:
37: void tty_attributes(struct tty *, const struct grid_cell *);
38: void tty_attributes_fg(struct tty *, const struct grid_cell *);
39: void tty_attributes_bg(struct tty *, const struct grid_cell *);
40:
1.4 nicm 41: void tty_cmd_alignmenttest(struct tty *, struct window_pane *, va_list);
1.1 nicm 42: void tty_cmd_cell(struct tty *, struct window_pane *, va_list);
43: void tty_cmd_clearendofline(struct tty *, struct window_pane *, va_list);
44: void tty_cmd_clearendofscreen(struct tty *, struct window_pane *, va_list);
45: void tty_cmd_clearline(struct tty *, struct window_pane *, va_list);
46: void tty_cmd_clearscreen(struct tty *, struct window_pane *, va_list);
47: void tty_cmd_clearstartofline(struct tty *, struct window_pane *, va_list);
48: void tty_cmd_clearstartofscreen(struct tty *, struct window_pane *, va_list);
49: void tty_cmd_deletecharacter(struct tty *, struct window_pane *, va_list);
50: void tty_cmd_deleteline(struct tty *, struct window_pane *, va_list);
51: void tty_cmd_insertcharacter(struct tty *, struct window_pane *, va_list);
52: void tty_cmd_insertline(struct tty *, struct window_pane *, va_list);
53: void tty_cmd_linefeed(struct tty *, struct window_pane *, va_list);
54: void tty_cmd_raw(struct tty *, struct window_pane *, va_list);
55: void tty_cmd_reverseindex(struct tty *, struct window_pane *, va_list);
56:
57: void (*tty_cmds[])(struct tty *, struct window_pane *, va_list) = {
1.4 nicm 58: tty_cmd_alignmenttest,
1.1 nicm 59: tty_cmd_cell,
60: tty_cmd_clearendofline,
61: tty_cmd_clearendofscreen,
62: tty_cmd_clearline,
63: tty_cmd_clearscreen,
64: tty_cmd_clearstartofline,
65: tty_cmd_clearstartofscreen,
66: tty_cmd_deletecharacter,
67: tty_cmd_deleteline,
68: tty_cmd_insertcharacter,
69: tty_cmd_insertline,
70: tty_cmd_linefeed,
71: tty_cmd_raw,
72: tty_cmd_reverseindex,
73: };
74:
75: void
76: tty_init(struct tty *tty, char *path, char *term)
77: {
78: tty->path = xstrdup(path);
1.9 ! nicm 79: if (term == NULL || *term == '\0')
1.1 nicm 80: tty->termname = xstrdup("unknown");
81: else
82: tty->termname = xstrdup(term);
83: tty->flags = 0;
84: tty->term_flags = 0;
85: }
86:
87: int
88: tty_open(struct tty *tty, char **cause)
89: {
90: int mode;
91:
92: tty->fd = open(tty->path, O_RDWR|O_NONBLOCK);
93: if (tty->fd == -1) {
94: xasprintf(cause, "%s: %s", tty->path, strerror(errno));
95: return (-1);
96: }
97:
98: if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
99: fatal("fcntl failed");
100: if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
1.9 ! nicm 101: fatal("fcntl failed");
1.1 nicm 102: if (fcntl(tty->fd, F_SETFD, FD_CLOEXEC) == -1)
103: fatal("fcntl failed");
104:
105: if (debug_level > 3)
106: tty->log_fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
107: else
108: tty->log_fd = -1;
109:
110: if ((tty->term = tty_term_find(tty->termname, tty->fd, cause)) == NULL)
111: goto error;
112:
113: tty->in = buffer_create(BUFSIZ);
114: tty->out = buffer_create(BUFSIZ);
115:
116: tty->flags &= TTY_UTF8;
117:
118: tty_start_tty(tty);
119:
120: tty_keys_init(tty);
121:
122: tty_fill_acs(tty);
123:
124: return (0);
125:
126: error:
127: close(tty->fd);
128: tty->fd = -1;
129:
130: return (-1);
131: }
132:
133: void
134: tty_start_tty(struct tty *tty)
135: {
136: struct termios tio;
137: int what;
138:
1.2 nicm 139: #if 0
1.1 nicm 140: tty_detect_utf8(tty);
1.2 nicm 141: #endif
1.1 nicm 142:
143: if (tcgetattr(tty->fd, &tty->tio) != 0)
144: fatal("tcgetattr failed");
145: memcpy(&tio, &tty->tio, sizeof tio);
146: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
147: tio.c_iflag |= IGNBRK;
148: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
149: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
150: ECHOPRT|ECHOKE|ECHOCTL|ISIG);
151: tio.c_cc[VMIN] = 1;
152: tio.c_cc[VTIME] = 0;
153: if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
154: fatal("tcsetattr failed");
155:
156: what = 0;
157: if (ioctl(tty->fd, TIOCFLUSH, &what) != 0)
158: fatal("ioctl(TIOCFLUSH)");
159:
160: tty_putcode(tty, TTYC_IS1);
161: tty_putcode(tty, TTYC_IS2);
162: tty_putcode(tty, TTYC_IS3);
163:
164: tty_putcode(tty, TTYC_SMCUP);
165: tty_putcode(tty, TTYC_SMKX);
166: tty_putcode(tty, TTYC_ENACS);
167: tty_putcode(tty, TTYC_CLEAR);
168:
169: tty_putcode(tty, TTYC_CNORM);
170: if (tty_term_has(tty->term, TTYC_KMOUS))
171: tty_puts(tty, "\033[?1000l");
172:
173: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
174:
175: tty->cx = UINT_MAX;
176: tty->cy = UINT_MAX;
177:
178: tty->rlower = UINT_MAX;
179: tty->rupper = UINT_MAX;
180:
181: tty->mode = MODE_CURSOR;
182: }
183:
184: void
185: tty_stop_tty(struct tty *tty)
186: {
187: struct winsize ws;
188:
189: /*
190: * Be flexible about error handling and try not kill the server just
191: * because the fd is invalid. Things like ssh -t can easily leave us
192: * with a dead tty.
193: */
194: if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
195: return;
196: if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
197: return;
198:
199: tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
200: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
201: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
202: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
203: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
204: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
205:
206: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
207: if (tty_term_has(tty->term, TTYC_KMOUS))
208: tty_raw(tty, "\033[?1000l");
209: }
210:
1.2 nicm 211: #if 0
1.1 nicm 212: void
213: tty_detect_utf8(struct tty *tty)
214: {
215: struct pollfd pfd;
216: char buf[7];
217: size_t len;
218: ssize_t n;
219: int nfds;
220: struct termios tio, old_tio;
221: int what;
222:
223: if (tty->flags & TTY_UTF8)
224: return;
225:
226: /*
227: * If the terminal looks reasonably likely to support this, try to
228: * write a three-byte UTF-8 wide character to the terminal, then read
229: * the cursor position.
230: *
231: * XXX This entire function is a hack.
232: */
233:
234: /* Check if the terminal looks sort of vt100. */
235: if (strstr(tty_term_string(tty->term, TTYC_CLEAR), "[2J") == NULL ||
236: strstr(tty_term_string(tty->term, TTYC_CUP), "H") == NULL)
237: return;
238:
239: if (tcgetattr(tty->fd, &old_tio) != 0)
240: fatal("tcgetattr failed");
241: cfmakeraw(&tio);
242: if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
243: fatal("tcsetattr failed");
244:
245: what = 0;
246: if (ioctl(tty->fd, TIOCFLUSH, &what) != 0)
247: fatal("ioctl(TIOCFLUSH)");
248:
249: #define UTF8_TEST_DATA "\033[H\357\277\246\033[6n"
250: if (write(tty->fd, UTF8_TEST_DATA, (sizeof UTF8_TEST_DATA) - 1) == -1)
251: fatal("write failed");
252: #undef UTF8_TEST_DATA
253:
254: len = 0;
255: for (;;) {
256: pfd.fd = tty->fd;
257: pfd.events = POLLIN;
258:
259: nfds = poll(&pfd, 1, 500);
260: if (nfds == -1) {
261: if (errno == EAGAIN || errno == EINTR)
262: continue;
263: fatal("poll failed");
264: }
265: if (nfds == 0)
266: break;
267: if (pfd.revents & (POLLERR|POLLNVAL|POLLHUP))
268: break;
269: if (!(pfd.revents & POLLIN))
270: continue;
271:
272: if ((n = read(tty->fd, buf + len, 1)) != 1)
273: break;
274: buf[++len] = '\0';
275:
276: if (len == (sizeof buf) - 1) {
277: if (strcmp(buf, "\033[1;3R") == 0)
278: tty->flags |= TTY_UTF8;
279: break;
280: }
281: }
282:
283: if (tcsetattr(tty->fd, TCSANOW, &old_tio) != 0)
284: fatal("tcsetattr failed");
285: }
1.2 nicm 286: #endif
1.1 nicm 287:
288: void
289: tty_fill_acs(struct tty *tty)
290: {
291: const char *ptr;
292:
293: memset(tty->acs, 0, sizeof tty->acs);
294: if (!tty_term_has(tty->term, TTYC_ACSC))
295: return;
296:
297: ptr = tty_term_string(tty->term, TTYC_ACSC);
298: if (strlen(ptr) % 2 != 0)
299: return;
300: for (; *ptr != '\0'; ptr += 2)
301: tty->acs[(u_char) ptr[0]] = ptr[1];
302: }
303:
304: u_char
305: tty_get_acs(struct tty *tty, u_char ch)
306: {
307: if (tty->acs[ch] != '\0')
308: return (tty->acs[ch]);
309: return (ch);
310: }
311:
312: void
313: tty_close(struct tty *tty, int no_stop)
314: {
315: if (tty->fd == -1)
316: return;
317:
318: if (tty->log_fd != -1) {
319: close(tty->log_fd);
320: tty->log_fd = -1;
321: }
322:
323: if (!no_stop)
324: tty_stop_tty(tty);
325:
326: tty_term_free(tty->term);
327: tty_keys_free(tty);
328:
329: close(tty->fd);
330: tty->fd = -1;
331:
332: buffer_destroy(tty->in);
333: buffer_destroy(tty->out);
334: }
335:
336: void
337: tty_free(struct tty *tty, int no_stop)
338: {
339: tty_close(tty, no_stop);
340:
341: if (tty->path != NULL)
342: xfree(tty->path);
343: if (tty->termname != NULL)
344: xfree(tty->termname);
345: }
346:
347: void
348: tty_raw(struct tty *tty, const char *s)
349: {
350: write(tty->fd, s, strlen(s));
351: }
352:
353: void
354: tty_putcode(struct tty *tty, enum tty_code_code code)
355: {
356: tty_puts(tty, tty_term_string(tty->term, code));
357: }
358:
359: void
360: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
361: {
362: if (a < 0)
363: return;
364: tty_puts(tty, tty_term_string1(tty->term, code, a));
365: }
366:
367: void
368: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
369: {
370: if (a < 0 || b < 0)
371: return;
372: tty_puts(tty, tty_term_string2(tty->term, code, a, b));
373: }
374:
375: void
376: tty_puts(struct tty *tty, const char *s)
377: {
378: if (*s == '\0')
379: return;
380: buffer_write(tty->out, s, strlen(s));
381:
382: if (tty->log_fd != -1)
383: write(tty->log_fd, s, strlen(s));
384: }
385:
386: void
387: tty_putc(struct tty *tty, u_char ch)
388: {
389: u_int sx;
390:
391: if (tty->cell.attr & GRID_ATTR_CHARSET)
392: ch = tty_get_acs(tty, ch);
393: buffer_write8(tty->out, ch);
394:
395: if (ch >= 0x20 && ch != 0x7f) {
396: sx = tty->sx;
397: if (tty->term->flags & TERM_EARLYWRAP)
398: sx--;
399:
400: if (tty->cx == sx) {
401: tty->cx = 0;
402: tty->cy++;
403: } else
404: tty->cx++;
405: }
406:
407: if (tty->log_fd != -1)
408: write(tty->log_fd, &ch, 1);
409: }
410:
411: void
1.5 nicm 412: tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
413: {
414: u_int i, width;
415:
416: for (i = 0; i < UTF8_SIZE; i++) {
417: if (gu->data[i] == 0xff)
418: break;
419: buffer_write8(tty->out, gu->data[i]);
420: if (tty->log_fd != -1)
421: write(tty->log_fd, &gu->data[i], 1);
422: }
423:
424: width = utf8_width(gu->data);
425: tty->cx += width;
426: }
427:
428: void
1.1 nicm 429: tty_set_title(struct tty *tty, const char *title)
430: {
431: if (strstr(tty->termname, "xterm") == NULL &&
432: strstr(tty->termname, "rxvt") == NULL &&
433: strcmp(tty->termname, "screen") != 0)
434: return;
435:
436: tty_puts(tty, "\033]0;");
437: tty_puts(tty, title);
438: tty_putc(tty, '\007');
439: }
440:
441: void
442: tty_update_mode(struct tty *tty, int mode)
443: {
444: int changed;
445:
446: if (tty->flags & TTY_NOCURSOR)
447: mode &= ~MODE_CURSOR;
448:
449: changed = mode ^ tty->mode;
450: if (changed & MODE_CURSOR) {
451: if (mode & MODE_CURSOR)
452: tty_putcode(tty, TTYC_CNORM);
453: else
454: tty_putcode(tty, TTYC_CIVIS);
455: }
456: if (changed & MODE_MOUSE) {
457: if (mode & MODE_MOUSE)
458: tty_puts(tty, "\033[?1000h");
459: else
460: tty_puts(tty, "\033[?1000l");
461: }
462: tty->mode = mode;
463: }
464:
465: void
466: tty_emulate_repeat(
467: struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
468: {
469: if (tty_term_has(tty->term, code))
470: tty_putcode1(tty, code, n);
471: else {
472: while (n-- > 0)
473: tty_putcode(tty, code1);
474: }
475: }
476:
477: /*
478: * Redraw scroll region using data from screen (already updated). Used when
479: * CSR not supported, or window is a pane that doesn't take up the full
480: * width of the terminal.
481: */
482: void
483: tty_redraw_region(struct tty *tty, struct window_pane *wp)
484: {
485: struct screen *s = wp->screen;
486: u_int i;
487:
488: /*
489: * If region is >= 50% of the screen, just schedule a window redraw. In
490: * most cases, this is likely to be followed by some more scrolling -
491: * without this, the entire pane ends up being redrawn many times which
492: * can be much more data.
493: */
494: if (s->old_rupper - s->old_rlower >= screen_size_y(s) / 2) {
495: wp->flags |= PANE_REDRAW;
496: return;
497: }
498:
499: if (s->old_cy < s->old_rupper || s->old_cy > s->old_rlower) {
500: for (i = s->old_cy; i < screen_size_y(s); i++)
501: tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
502: } else {
503: for (i = s->old_rupper; i <= s->old_rlower; i++)
504: tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
505: }
506: }
507:
508: void
509: tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
510: {
511: const struct grid_cell *gc;
512: const struct grid_utf8 *gu;
513: u_int i, sx;
514:
515: sx = screen_size_x(s);
516: if (sx > s->grid->size[s->grid->hsize + py])
517: sx = s->grid->size[s->grid->hsize + py];
518: if (sx > tty->sx)
519: sx = tty->sx;
520:
521: tty_cursor(tty, 0, py, ox, oy);
522: for (i = 0; i < sx; i++) {
523: gc = grid_view_peek_cell(s->grid, i, py);
524:
525: gu = NULL;
526: if (gc->flags & GRID_FLAG_UTF8)
527: gu = grid_view_peek_utf8(s->grid, i, py);
528:
529: if (screen_check_selection(s, i, py)) {
530: s->sel.cell.data = gc->data;
531: tty_cell(tty, &s->sel.cell, gu);
532: } else
533: tty_cell(tty, gc, gu);
534: }
535:
536: if (sx >= tty->sx)
537: return;
538: tty_reset(tty);
539:
540: tty_cursor(tty, sx, py, ox, oy);
541: if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
542: tty_putcode(tty, TTYC_EL);
543: else {
544: for (i = sx; i < screen_size_x(s); i++)
545: tty_putc(tty, ' ');
546: }
547: }
548:
549: void
550: tty_vwrite(
551: struct tty *tty, struct window_pane *wp, enum tty_cmd cmd, va_list ap)
552: {
553: if (tty->flags & TTY_FREEZE || tty->term == NULL)
554: return;
555: if (tty_cmds[cmd] != NULL)
556: tty_cmds[cmd](tty, wp, ap);
557: }
558:
559: void
560: tty_cmd_insertcharacter(struct tty *tty, struct window_pane *wp, va_list ap)
561: {
562: struct screen *s = wp->screen;
563: u_int ua;
564:
565: if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
566: tty_draw_line(tty, wp->screen, s->old_cy, wp->xoff, wp->yoff);
567: return;
568: }
569:
570: ua = va_arg(ap, u_int);
571:
572: tty_reset(tty);
573:
574: tty_cursor(tty, s->old_cx, s->old_cy, wp->xoff, wp->yoff);
575: if (tty_term_has(tty->term, TTYC_ICH) ||
576: tty_term_has(tty->term, TTYC_ICH1))
577: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ua);
578: else {
579: tty_putcode(tty, TTYC_SMIR);
580: while (ua-- > 0)
581: tty_putc(tty, ' ');
582: tty_putcode(tty, TTYC_RMIR);
583: }
584: }
585:
586: void
587: tty_cmd_deletecharacter(struct tty *tty, struct window_pane *wp, va_list ap)
588: {
589: struct screen *s = wp->screen;
590: u_int ua;
591:
592: if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
593: tty_draw_line(tty, wp->screen, s->old_cy, wp->xoff, wp->yoff);
594: return;
595: }
596:
597: ua = va_arg(ap, u_int);
598:
599: tty_reset(tty);
600:
601: tty_cursor(tty, s->old_cx, s->old_cy, wp->xoff, wp->yoff);
602: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ua);
603: }
604:
605: void
606: tty_cmd_insertline(struct tty *tty, struct window_pane *wp, va_list ap)
607: {
608: struct screen *s = wp->screen;
609: u_int ua;
610:
611: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
612: !tty_term_has(tty->term, TTYC_CSR)) {
613: tty_redraw_region(tty, wp);
614: return;
615: }
616:
617: ua = va_arg(ap, u_int);
618:
619: tty_reset(tty);
620:
621: tty_region(tty, s->old_rupper, s->old_rlower, wp->yoff);
622:
623: tty_cursor(tty, s->old_cx, s->old_cy, wp->xoff, wp->yoff);
624: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ua);
625: }
626:
627: void
628: tty_cmd_deleteline(struct tty *tty, struct window_pane *wp, va_list ap)
629: {
630: struct screen *s = wp->screen;
631: u_int ua;
632:
633: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
634: !tty_term_has(tty->term, TTYC_CSR)) {
635: tty_redraw_region(tty, wp);
636: return;
637: }
638:
639: ua = va_arg(ap, u_int);
640:
641: tty_reset(tty);
642:
643: tty_region(tty, s->old_rupper, s->old_rlower, wp->yoff);
644:
645: tty_cursor(tty, s->old_cx, s->old_cy, wp->xoff, wp->yoff);
646: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ua);
647: }
648:
649: void
650: tty_cmd_clearline(struct tty *tty, struct window_pane *wp, unused va_list ap)
651: {
652: struct screen *s = wp->screen;
653: u_int i;
654:
655: tty_reset(tty);
656:
657: tty_cursor(tty, 0, s->old_cy, wp->xoff, wp->yoff);
658: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
659: tty_term_has(tty->term, TTYC_EL)) {
660: tty_putcode(tty, TTYC_EL);
661: } else {
662: for (i = 0; i < screen_size_x(s); i++)
663: tty_putc(tty, ' ');
664: }
665: }
666:
667: void
668: tty_cmd_clearendofline(
669: struct tty *tty, struct window_pane *wp, unused va_list ap)
670: {
671: struct screen *s = wp->screen;
672: u_int i;
673:
674: tty_reset(tty);
675:
676: tty_cursor(tty, s->old_cx, s->old_cy, wp->xoff, wp->yoff);
677: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
678: tty_term_has(tty->term, TTYC_EL))
679: tty_putcode(tty, TTYC_EL);
680: else {
681: for (i = s->old_cx; i < screen_size_x(s); i++)
682: tty_putc(tty, ' ');
683: }
684: }
685:
686: void
687: tty_cmd_clearstartofline(
688: struct tty *tty, struct window_pane *wp, unused va_list ap)
689: {
690: struct screen *s = wp->screen;
691: u_int i;
692:
693: tty_reset(tty);
694:
695: if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
696: tty_cursor(tty, s->old_cx, s->old_cy, wp->xoff, wp->yoff);
697: tty_putcode(tty, TTYC_EL1);
698: } else {
699: tty_cursor(tty, 0, s->old_cy, wp->xoff, wp->yoff);
700: for (i = 0; i < s->old_cx + 1; i++)
701: tty_putc(tty, ' ');
702: }
703: }
704:
705: void
706: tty_cmd_reverseindex(struct tty *tty, struct window_pane *wp, unused va_list ap)
707: {
708: struct screen *s = wp->screen;
709:
710: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
711: !tty_term_has(tty->term, TTYC_CSR)) {
712: tty_redraw_region(tty, wp);
713: return;
714: }
715:
716: tty_reset(tty);
717:
718: tty_region(tty, s->old_rupper, s->old_rlower, wp->yoff);
719:
720: if (s->old_cy == s->old_rupper) {
721: tty_cursor(tty, s->old_cx, s->old_rupper, wp->xoff, wp->yoff);
722: tty_putcode(tty, TTYC_RI);
723: }
724: }
725:
726: void
727: tty_cmd_linefeed(struct tty *tty, struct window_pane *wp, unused va_list ap)
728: {
729: struct screen *s = wp->screen;
730:
731: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
732: !tty_term_has(tty->term, TTYC_CSR)) {
733: tty_redraw_region(tty, wp);
734: return;
735: }
736:
737: tty_reset(tty);
738:
739: tty_region(tty, s->old_rupper, s->old_rlower, wp->yoff);
740:
741: if (s->old_cy == s->old_rlower) {
742: tty_cursor(tty, s->old_cx, s->old_cy, wp->xoff, wp->yoff);
743: tty_putc(tty, '\n');
744: }
745: }
746:
747: void
748: tty_cmd_clearendofscreen(
749: struct tty *tty, struct window_pane *wp, unused va_list ap)
750: {
751: struct screen *s = wp->screen;
1.6 nicm 752: u_int i, j;
1.1 nicm 753:
754: tty_reset(tty);
755:
756: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
757: tty_cursor(tty, s->old_cx, s->old_cy, wp->xoff, wp->yoff);
758: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
759: tty_term_has(tty->term, TTYC_EL)) {
760: tty_putcode(tty, TTYC_EL);
761: if (s->old_cy != screen_size_y(s) - 1) {
762: tty_cursor(tty, 0, s->old_cy + 1, wp->xoff, wp->yoff);
763: for (i = s->old_cy + 1; i < screen_size_y(s); i++) {
764: tty_putcode(tty, TTYC_EL);
765: if (i == screen_size_y(s) - 1)
766: continue;
767: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
768: tty->cy++;
769: }
770: }
771: } else {
772: for (i = s->old_cx; i < screen_size_x(s); i++)
773: tty_putc(tty, ' ');
774: for (j = s->old_cy; j < screen_size_y(s); j++) {
775: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
776: for (i = 0; i < screen_size_x(s); i++)
777: tty_putc(tty, ' ');
778: }
779: }
780: }
781:
782: void
783: tty_cmd_clearstartofscreen(
784: struct tty *tty, struct window_pane *wp, unused va_list ap)
785: {
786: struct screen *s = wp->screen;
787: u_int i, j;
788:
789: tty_reset(tty);
790:
791: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
792: tty_cursor(tty, 0, 0, wp->xoff, wp->yoff);
793: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
794: tty_term_has(tty->term, TTYC_EL)) {
795: for (i = 0; i < s->old_cy; i++) {
796: tty_putcode(tty, TTYC_EL);
797: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
798: tty->cy++;
799: }
800: } else {
801: for (j = 0; j < s->old_cy; j++) {
802: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
803: for (i = 0; i < screen_size_x(s); i++)
804: tty_putc(tty, ' ');
805: }
806: }
1.3 nicm 807: for (i = 0; i <= s->old_cx; i++)
1.1 nicm 808: tty_putc(tty, ' ');
809: }
810:
811: void
812: tty_cmd_clearscreen(
813: struct tty *tty, struct window_pane *wp, unused va_list ap)
814: {
815: struct screen *s = wp->screen;
816: u_int i, j;
817:
818: tty_reset(tty);
819:
820: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
821: tty_cursor(tty, 0, 0, wp->xoff, wp->yoff);
822: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
823: tty_term_has(tty->term, TTYC_EL)) {
824: for (i = 0; i < screen_size_y(s); i++) {
825: tty_putcode(tty, TTYC_EL);
826: if (i != screen_size_y(s) - 1) {
827: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
828: tty->cy++;
829: }
830: }
831: } else {
832: for (j = 0; j < screen_size_y(s); j++) {
833: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
834: for (i = 0; i < screen_size_x(s); i++)
835: tty_putc(tty, ' ');
836: }
1.4 nicm 837: }
838: }
839:
840: void
841: tty_cmd_alignmenttest(
842: struct tty *tty, struct window_pane *wp, unused va_list ap)
843: {
844: struct screen *s = wp->screen;
845: u_int i, j;
846:
847: tty_reset(tty);
848:
849: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
850:
851: for (j = 0; j < screen_size_y(s); j++) {
852: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
853: for (i = 0; i < screen_size_x(s); i++)
854: tty_putc(tty, 'E');
1.1 nicm 855: }
856: }
857:
858: void
859: tty_cmd_cell(struct tty *tty, struct window_pane *wp, va_list ap)
860: {
861: struct screen *s = wp->screen;
862: struct grid_cell *gc;
863: struct grid_utf8 *gu;
864:
865: gc = va_arg(ap, struct grid_cell *);
866: gu = va_arg(ap, struct grid_utf8 *);
867:
868: tty_cursor(tty, s->old_cx, s->old_cy, wp->xoff, wp->yoff);
869:
870: tty_cell(tty, gc, gu);
871: }
872:
873: void
874: tty_cmd_raw(struct tty *tty, unused struct window_pane *wp, va_list ap)
875: {
876: u_char *buf;
877: size_t i, len;
878:
879: buf = va_arg(ap, u_char *);
880: len = va_arg(ap, size_t);
881:
882: for (i = 0; i < len; i++)
883: tty_putc(tty, buf[i]);
884: }
885:
886: void
887: tty_cell(
888: struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
889: {
890: u_int i;
891:
892: /* Skip last character if terminal is stupid. */
893: if (tty->term->flags & TERM_EARLYWRAP &&
894: tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
895: return;
896:
897: /* If this is a padding character, do nothing. */
898: if (gc->flags & GRID_FLAG_PADDING)
899: return;
900:
901: /* Set the attributes. */
902: tty_attributes(tty, gc);
903:
904: /* If not UTF-8, write directly. */
905: if (!(gc->flags & GRID_FLAG_UTF8)) {
906: if (gc->data < 0x20 || gc->data == 0x7f)
907: return;
908: tty_putc(tty, gc->data);
909: return;
910: }
911:
912: /* If the terminal doesn't support UTF-8, write underscores. */
913: if (!(tty->flags & TTY_UTF8)) {
914: for (i = 0; i < gu->width; i++)
915: tty_putc(tty, '_');
916: return;
917: }
918:
919: /* Otherwise, write UTF-8. */
1.5 nicm 920: tty_pututf8(tty, gu);
1.1 nicm 921: }
922:
923: void
924: tty_reset(struct tty *tty)
925: {
926: struct grid_cell *gc = &tty->cell;
927:
928: if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
929: return;
930:
931: if (tty_term_has(tty->term, TTYC_RMACS) && gc->attr & GRID_ATTR_CHARSET)
932: tty_putcode(tty, TTYC_RMACS);
933: tty_putcode(tty, TTYC_SGR0);
934: memcpy(gc, &grid_default_cell, sizeof *gc);
935: }
936:
937: void
938: tty_region(struct tty *tty, u_int rupper, u_int rlower, u_int oy)
939: {
940: if (!tty_term_has(tty->term, TTYC_CSR))
941: return;
942: if (tty->rlower != oy + rlower || tty->rupper != oy + rupper) {
943: tty->rlower = oy + rlower;
944: tty->rupper = oy + rupper;
945: tty->cx = 0;
946: tty->cy = 0;
947: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
948: }
949: }
950:
951: void
952: tty_cursor(struct tty *tty, u_int cx, u_int cy, u_int ox, u_int oy)
953: {
954: if (ox + cx == 0 && tty->cx != 0 && tty->cy == oy + cy) {
955: tty->cx = 0;
956: tty_putc(tty, '\r');
957: } else if (tty->cx != ox + cx || tty->cy != oy + cy) {
958: tty->cx = ox + cx;
959: tty->cy = oy + cy;
960: tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
961: }
962: }
963:
964: void
965: tty_attributes(struct tty *tty, const struct grid_cell *gc)
966: {
967: struct grid_cell *tc = &tty->cell;
968: u_char changed;
969: u_int fg, bg;
970:
971: /* If any bits are being cleared, reset everything. */
972: if (tc->attr & ~gc->attr)
973: tty_reset(tty);
974:
975: /* Filter out attribute bits already set. */
976: changed = gc->attr & ~tc->attr;
977: tc->attr = gc->attr;
978:
979: /* Set the attributes. */
980: fg = gc->fg;
981: bg = gc->bg;
982: if (changed & GRID_ATTR_BRIGHT)
983: tty_putcode(tty, TTYC_BOLD);
984: if (changed & GRID_ATTR_DIM)
985: tty_putcode(tty, TTYC_DIM);
986: if (changed & GRID_ATTR_ITALICS)
987: tty_putcode(tty, TTYC_SMSO);
988: if (changed & GRID_ATTR_UNDERSCORE)
989: tty_putcode(tty, TTYC_SMUL);
990: if (changed & GRID_ATTR_BLINK)
991: tty_putcode(tty, TTYC_BLINK);
992: if (changed & GRID_ATTR_REVERSE) {
993: if (tty_term_has(tty->term, TTYC_REV))
994: tty_putcode(tty, TTYC_REV);
995: else if (tty_term_has(tty->term, TTYC_SMSO))
996: tty_putcode(tty, TTYC_SMSO);
997: }
998: if (changed & GRID_ATTR_HIDDEN)
999: tty_putcode(tty, TTYC_INVIS);
1000: if (changed & GRID_ATTR_CHARSET)
1001: tty_putcode(tty, TTYC_SMACS);
1002:
1003: /* Set foreground colour. */
1004: if (fg != tc->fg ||
1005: (gc->flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)) {
1006: tty_attributes_fg(tty, gc);
1007: tc->fg = fg;
1.8 nicm 1008: tc->flags &= ~GRID_FLAG_FG256;
1009: tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1 nicm 1010: }
1011:
1012: /* Set background colour. */
1013: if (bg != tc->bg ||
1014: (gc->flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)) {
1015: tty_attributes_bg(tty, gc);
1016: tc->bg = bg;
1.8 nicm 1017: tc->flags &= ~GRID_FLAG_BG256;
1018: tc->flags |= gc->flags & GRID_FLAG_BG256;
1.1 nicm 1019: }
1020: }
1021:
1022: int
1023: tty_try_256(struct tty *tty, u_char colour, const char *type)
1024: {
1025: char s[32];
1026:
1027: if (!(tty->term->flags & TERM_256COLOURS) &&
1028: !(tty->term_flags & TERM_256COLOURS))
1029: return (-1);
1030:
1031: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1032: tty_puts(tty, s);
1033: return (0);
1034: }
1035:
1036: int
1037: tty_try_88(struct tty *tty, u_char colour, const char *type)
1038: {
1039: char s[32];
1040:
1041: if (!(tty->term->flags & TERM_88COLOURS) &&
1042: !(tty->term_flags & TERM_88COLOURS))
1043: return (-1);
1044: colour = colour_256to88(colour);
1045:
1046: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1047: tty_puts(tty, s);
1048: return (0);
1049: }
1050:
1051: void
1052: tty_attributes_fg(struct tty *tty, const struct grid_cell *gc)
1053: {
1054: u_char fg;
1055:
1056: fg = gc->fg;
1057: if (gc->flags & GRID_FLAG_FG256) {
1058: if (tty_try_256(tty, fg, "38") == 0)
1059: return;
1060: if (tty_try_88(tty, fg, "38") == 0)
1061: return;
1062: fg = colour_256to16(fg);
1063: if (fg & 8) {
1064: fg &= 7;
1065: tty_putcode(tty, TTYC_BOLD);
1066: tty->cell.attr |= GRID_ATTR_BRIGHT;
1067: } else if (tty->cell.attr & GRID_ATTR_BRIGHT)
1068: tty_reset(tty);
1069: }
1070:
1071: if (fg == 8 &&
1072: !(tty->term->flags & TERM_HASDEFAULTS) &&
1073: !(tty->term_flags & TERM_HASDEFAULTS))
1074: fg = 7;
1075: if (fg == 8)
1076: tty_puts(tty, "\033[39m");
1077: else
1078: tty_putcode1(tty, TTYC_SETAF, fg);
1079: }
1080:
1081: void
1082: tty_attributes_bg(struct tty *tty, const struct grid_cell *gc)
1083: {
1084: u_char bg;
1085:
1086: bg = gc->bg;
1087: if (gc->flags & GRID_FLAG_BG256) {
1088: if (tty_try_256(tty, bg, "48") == 0)
1089: return;
1090: if (tty_try_88(tty, bg, "48") == 0)
1091: return;
1092: bg = colour_256to16(bg);
1093: if (bg & 8)
1094: bg &= 7;
1095: }
1096:
1097: if (bg == 8 &&
1098: !(tty->term->flags & TERM_HASDEFAULTS) &&
1099: !(tty->term_flags & TERM_HASDEFAULTS))
1100: bg = 0;
1101: if (bg == 8)
1102: tty_puts(tty, "\033[49m");
1103: else
1104: tty_putcode1(tty, TTYC_SETAB, bg);
1105: }