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