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