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