Annotation of src/usr.bin/tmux/tty.c, Revision 1.24
1.24 ! nicm 1: /* $OpenBSD: tty.c,v 1.23 2009/08/11 22:34:17 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.24 ! nicm 41: void tty_redraw_region(struct tty *, const 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
1.22 nicm 48: tty_init(struct tty *tty, int fd, char *path, char *term)
1.1 nicm 49: {
50: tty->path = xstrdup(path);
1.22 nicm 51: tty->fd = fd;
1.23 nicm 52: tty->log_fd = -1;
1.22 nicm 53:
1.9 nicm 54: if (term == NULL || *term == '\0')
1.1 nicm 55: tty->termname = xstrdup("unknown");
56: else
57: tty->termname = xstrdup(term);
58: tty->flags = 0;
59: tty->term_flags = 0;
60: }
61:
62: int
1.17 nicm 63: tty_open(struct tty *tty, const char *overrides, char **cause)
1.1 nicm 64: {
1.22 nicm 65: int mode;
1.1 nicm 66:
67: if (tty->fd == -1) {
1.22 nicm 68: tty->fd = open(tty->path, O_RDWR|O_NONBLOCK);
69: if (tty->fd == -1) {
70: xasprintf(cause, "%s: %s", tty->path, strerror(errno));
71: return (-1);
72: }
1.1 nicm 73: }
74:
75: if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
76: fatal("fcntl failed");
77: if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
1.9 nicm 78: fatal("fcntl failed");
1.1 nicm 79: if (fcntl(tty->fd, F_SETFD, FD_CLOEXEC) == -1)
80: fatal("fcntl failed");
81:
82: if (debug_level > 3)
83: tty->log_fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
84: else
85: tty->log_fd = -1;
86:
1.17 nicm 87: tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
1.21 nicm 88: if (tty->term == NULL) {
89: tty_close(tty);
90: return (-1);
91: }
92: tty->flags |= TTY_OPENED;
1.1 nicm 93:
94: tty->in = buffer_create(BUFSIZ);
95: tty->out = buffer_create(BUFSIZ);
96:
97: tty->flags &= TTY_UTF8;
98:
99: tty_start_tty(tty);
100:
101: tty_keys_init(tty);
102:
103: tty_fill_acs(tty);
104:
105: return (0);
106: }
107:
108: void
109: tty_start_tty(struct tty *tty)
110: {
111: struct termios tio;
112: int what;
113:
1.2 nicm 114: #if 0
1.1 nicm 115: tty_detect_utf8(tty);
1.2 nicm 116: #endif
1.1 nicm 117:
118: if (tcgetattr(tty->fd, &tty->tio) != 0)
119: fatal("tcgetattr failed");
120: memcpy(&tio, &tty->tio, sizeof tio);
121: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
122: tio.c_iflag |= IGNBRK;
123: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
124: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
125: ECHOPRT|ECHOKE|ECHOCTL|ISIG);
126: tio.c_cc[VMIN] = 1;
127: tio.c_cc[VTIME] = 0;
128: if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
129: fatal("tcsetattr failed");
130:
131: what = 0;
132: if (ioctl(tty->fd, TIOCFLUSH, &what) != 0)
133: fatal("ioctl(TIOCFLUSH)");
134:
1.10 nicm 135: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 136:
137: tty_putcode(tty, TTYC_SMKX);
138: tty_putcode(tty, TTYC_ENACS);
139: tty_putcode(tty, TTYC_CLEAR);
140:
141: tty_putcode(tty, TTYC_CNORM);
142: if (tty_term_has(tty->term, TTYC_KMOUS))
143: tty_puts(tty, "\033[?1000l");
144:
145: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
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:
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.12 nicm 606: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.1 nicm 607: }
608:
609: void
1.24 ! nicm 610: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 611: {
1.12 nicm 612: struct window_pane *wp = ctx->wp;
613: struct screen *s = wp->screen;
1.1 nicm 614:
615: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
616: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 617: tty_redraw_region(tty, ctx);
1.1 nicm 618: return;
619: }
620:
621: tty_reset(tty);
622:
1.14 nicm 623: tty_region(tty, ctx->orupper, ctx->orlower, wp->yoff);
1.1 nicm 624:
1.14 nicm 625: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.12 nicm 626: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1 nicm 627: }
628:
629: void
1.24 ! nicm 630: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 631: {
1.12 nicm 632: struct window_pane *wp = ctx->wp;
633: struct screen *s = wp->screen;
1.1 nicm 634:
635: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
636: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 637: tty_redraw_region(tty, ctx);
1.1 nicm 638: return;
639: }
640:
641: tty_reset(tty);
642:
1.14 nicm 643: tty_region(tty, ctx->orupper, ctx->orlower, wp->yoff);
1.1 nicm 644:
1.14 nicm 645: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.12 nicm 646: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1 nicm 647: }
648:
649: void
1.24 ! nicm 650: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 651: {
1.12 nicm 652: struct window_pane *wp = ctx->wp;
653: struct screen *s = wp->screen;
654: u_int i;
1.1 nicm 655:
656: tty_reset(tty);
657:
1.14 nicm 658: tty_cursor(tty, 0, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 659: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
660: tty_term_has(tty->term, TTYC_EL)) {
661: tty_putcode(tty, TTYC_EL);
662: } else {
663: for (i = 0; i < screen_size_x(s); i++)
664: tty_putc(tty, ' ');
665: }
666: }
667:
668: void
1.24 ! nicm 669: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 670: {
1.12 nicm 671: struct window_pane *wp = ctx->wp;
672: struct screen *s = wp->screen;
673: u_int i;
1.1 nicm 674:
675: tty_reset(tty);
676:
1.14 nicm 677: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 678: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
679: tty_term_has(tty->term, TTYC_EL))
680: tty_putcode(tty, TTYC_EL);
681: else {
1.14 nicm 682: for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1 nicm 683: tty_putc(tty, ' ');
684: }
685: }
686:
687: void
1.24 ! nicm 688: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 689: {
1.12 nicm 690: struct window_pane *wp = ctx->wp;
691: u_int i;
1.1 nicm 692:
693: tty_reset(tty);
694:
695: if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.14 nicm 696: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 697: tty_putcode(tty, TTYC_EL1);
698: } else {
1.14 nicm 699: tty_cursor(tty, 0, ctx->ocy, wp->xoff, wp->yoff);
700: for (i = 0; i < ctx->ocx + 1; i++)
1.1 nicm 701: tty_putc(tty, ' ');
702: }
703: }
704:
705: void
1.24 ! nicm 706: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 707: {
1.12 nicm 708: struct window_pane *wp = ctx->wp;
709: struct screen *s = wp->screen;
1.1 nicm 710:
711: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
712: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 713: tty_redraw_region(tty, ctx);
1.1 nicm 714: return;
715: }
716:
717: tty_reset(tty);
718:
1.14 nicm 719: tty_region(tty, ctx->orupper, ctx->orlower, wp->yoff);
1.1 nicm 720:
1.14 nicm 721: if (ctx->ocy == ctx->orupper) {
722: tty_cursor(tty, ctx->ocx, ctx->orupper, wp->xoff, wp->yoff);
1.1 nicm 723: tty_putcode(tty, TTYC_RI);
724: }
725: }
726:
727: void
1.24 ! nicm 728: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 729: {
1.12 nicm 730: struct window_pane *wp = ctx->wp;
731: struct screen *s = wp->screen;
1.1 nicm 732:
733: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
734: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 735: tty_redraw_region(tty, ctx);
1.1 nicm 736: return;
737: }
738:
739: tty_reset(tty);
740:
1.14 nicm 741: tty_region(tty, ctx->orupper, ctx->orlower, wp->yoff);
1.1 nicm 742:
1.14 nicm 743: if (ctx->ocy == ctx->orlower) {
744: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 745: tty_putc(tty, '\n');
746: }
747: }
748:
749: void
1.24 ! nicm 750: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 751: {
1.12 nicm 752: struct window_pane *wp = ctx->wp;
753: struct screen *s = wp->screen;
754: u_int i, j;
1.1 nicm 755:
756: tty_reset(tty);
757:
758: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
1.14 nicm 759: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 760: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
761: tty_term_has(tty->term, TTYC_EL)) {
762: tty_putcode(tty, TTYC_EL);
1.14 nicm 763: if (ctx->ocy != screen_size_y(s) - 1) {
764: tty_cursor(tty, 0, ctx->ocy + 1, wp->xoff, wp->yoff);
765: for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1 nicm 766: tty_putcode(tty, TTYC_EL);
767: if (i == screen_size_y(s) - 1)
768: continue;
769: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
770: tty->cy++;
771: }
772: }
773: } else {
1.14 nicm 774: for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1 nicm 775: tty_putc(tty, ' ');
1.14 nicm 776: for (j = ctx->ocy; j < screen_size_y(s); j++) {
1.1 nicm 777: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
778: for (i = 0; i < screen_size_x(s); i++)
779: tty_putc(tty, ' ');
780: }
781: }
782: }
783:
784: void
1.24 ! nicm 785: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 786: {
1.12 nicm 787: struct window_pane *wp = ctx->wp;
788: struct screen *s = wp->screen;
789: u_int i, j;
1.1 nicm 790:
791: tty_reset(tty);
792:
793: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
794: tty_cursor(tty, 0, 0, wp->xoff, wp->yoff);
795: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
796: tty_term_has(tty->term, TTYC_EL)) {
1.14 nicm 797: for (i = 0; i < ctx->ocy; i++) {
1.1 nicm 798: tty_putcode(tty, TTYC_EL);
799: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
800: tty->cy++;
801: }
802: } else {
1.14 nicm 803: for (j = 0; j < ctx->ocy; j++) {
1.1 nicm 804: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
805: for (i = 0; i < screen_size_x(s); i++)
806: tty_putc(tty, ' ');
807: }
808: }
1.14 nicm 809: for (i = 0; i <= ctx->ocx; i++)
1.1 nicm 810: tty_putc(tty, ' ');
811: }
812:
813: void
1.24 ! nicm 814: tty_cmd_clearscreen(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)) {
826: for (i = 0; i < screen_size_y(s); i++) {
827: tty_putcode(tty, TTYC_EL);
828: if (i != screen_size_y(s) - 1) {
829: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
830: tty->cy++;
831: }
832: }
833: } else {
834: for (j = 0; j < screen_size_y(s); j++) {
835: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
836: for (i = 0; i < screen_size_x(s); i++)
837: tty_putc(tty, ' ');
838: }
1.4 nicm 839: }
840: }
841:
842: void
1.24 ! nicm 843: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 844: {
1.12 nicm 845: struct window_pane *wp = ctx->wp;
846: struct screen *s = wp->screen;
847: u_int i, j;
1.4 nicm 848:
849: tty_reset(tty);
850:
851: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
852:
853: for (j = 0; j < screen_size_y(s); j++) {
854: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
855: for (i = 0; i < screen_size_x(s); i++)
856: tty_putc(tty, 'E');
1.1 nicm 857: }
858: }
859:
860: void
1.24 ! nicm 861: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 862: {
1.12 nicm 863: struct window_pane *wp = ctx->wp;
1.1 nicm 864:
1.14 nicm 865: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 866:
1.12 nicm 867: tty_cell(tty, ctx->cell, ctx->utf8);
1.1 nicm 868: }
869:
870: void
1.24 ! nicm 871: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 872: {
1.12 nicm 873: u_char *ptr = ctx->ptr;
1.11 nicm 874: size_t i;
1.1 nicm 875:
1.11 nicm 876: for (i = 0; i < UTF8_SIZE; i++) {
1.12 nicm 877: if (ptr[i] == 0xff)
1.11 nicm 878: break;
1.12 nicm 879: tty_putc(tty, ptr[i]);
1.11 nicm 880: }
1.1 nicm 881: }
882:
883: void
884: tty_cell(
885: struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
886: {
887: u_int i;
888:
889: /* Skip last character if terminal is stupid. */
890: if (tty->term->flags & TERM_EARLYWRAP &&
891: tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
892: return;
893:
894: /* If this is a padding character, do nothing. */
895: if (gc->flags & GRID_FLAG_PADDING)
896: return;
897:
898: /* Set the attributes. */
899: tty_attributes(tty, gc);
900:
901: /* If not UTF-8, write directly. */
902: if (!(gc->flags & GRID_FLAG_UTF8)) {
903: if (gc->data < 0x20 || gc->data == 0x7f)
904: return;
905: tty_putc(tty, gc->data);
906: return;
907: }
908:
909: /* If the terminal doesn't support UTF-8, write underscores. */
910: if (!(tty->flags & TTY_UTF8)) {
911: for (i = 0; i < gu->width; i++)
912: tty_putc(tty, '_');
913: return;
914: }
915:
916: /* Otherwise, write UTF-8. */
1.5 nicm 917: tty_pututf8(tty, gu);
1.1 nicm 918: }
919:
920: void
921: tty_reset(struct tty *tty)
922: {
923: struct grid_cell *gc = &tty->cell;
924:
925: if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
926: return;
927:
928: if (tty_term_has(tty->term, TTYC_RMACS) && gc->attr & GRID_ATTR_CHARSET)
929: tty_putcode(tty, TTYC_RMACS);
930: tty_putcode(tty, TTYC_SGR0);
931: memcpy(gc, &grid_default_cell, sizeof *gc);
932: }
933:
934: void
935: tty_region(struct tty *tty, u_int rupper, u_int rlower, u_int oy)
936: {
937: if (!tty_term_has(tty->term, TTYC_CSR))
938: return;
939: if (tty->rlower != oy + rlower || tty->rupper != oy + rupper) {
940: tty->rlower = oy + rlower;
941: tty->rupper = oy + rupper;
942: tty->cx = 0;
943: tty->cy = 0;
944: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
945: }
946: }
947:
948: void
949: tty_cursor(struct tty *tty, u_int cx, u_int cy, u_int ox, u_int oy)
950: {
951: if (ox + cx == 0 && tty->cx != 0 && tty->cy == oy + cy) {
952: tty->cx = 0;
953: tty_putc(tty, '\r');
954: } else if (tty->cx != ox + cx || tty->cy != oy + cy) {
955: tty->cx = ox + cx;
956: tty->cy = oy + cy;
957: tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
958: }
959: }
960:
961: void
962: tty_attributes(struct tty *tty, const struct grid_cell *gc)
963: {
964: struct grid_cell *tc = &tty->cell;
965: u_char changed;
1.18 nicm 966: u_int fg, bg, attr;
967:
968: /*
969: * If no setab, try to use the reverse attribute as a best-effort for a
970: * non-default background. This is a bit of a hack but it doesn't do
971: * any serious harm and makes a couple of applications happier.
972: */
973: fg = gc->fg; bg = gc->bg; attr = gc->attr;
974: if (!tty_term_has(tty->term, TTYC_SETAB)) {
975: if (attr & GRID_ATTR_REVERSE) {
976: if (fg != 7 && fg != 8)
977: attr &= ~GRID_ATTR_REVERSE;
978: } else {
979: if (bg != 0 && bg != 8)
980: attr |= GRID_ATTR_REVERSE;
981: }
982: }
1.1 nicm 983:
984: /* If any bits are being cleared, reset everything. */
1.18 nicm 985: if (tc->attr & ~attr)
1.1 nicm 986: tty_reset(tty);
987:
988: /* Filter out attribute bits already set. */
1.18 nicm 989: changed = attr & ~tc->attr;
990: tc->attr = attr;
1.1 nicm 991:
992: /* Set the attributes. */
993: if (changed & GRID_ATTR_BRIGHT)
994: tty_putcode(tty, TTYC_BOLD);
995: if (changed & GRID_ATTR_DIM)
996: tty_putcode(tty, TTYC_DIM);
997: if (changed & GRID_ATTR_ITALICS)
998: tty_putcode(tty, TTYC_SMSO);
999: if (changed & GRID_ATTR_UNDERSCORE)
1000: tty_putcode(tty, TTYC_SMUL);
1001: if (changed & GRID_ATTR_BLINK)
1002: tty_putcode(tty, TTYC_BLINK);
1003: if (changed & GRID_ATTR_REVERSE) {
1004: if (tty_term_has(tty->term, TTYC_REV))
1005: tty_putcode(tty, TTYC_REV);
1006: else if (tty_term_has(tty->term, TTYC_SMSO))
1007: tty_putcode(tty, TTYC_SMSO);
1008: }
1009: if (changed & GRID_ATTR_HIDDEN)
1010: tty_putcode(tty, TTYC_INVIS);
1011: if (changed & GRID_ATTR_CHARSET)
1012: tty_putcode(tty, TTYC_SMACS);
1013:
1014: /* Set foreground colour. */
1015: if (fg != tc->fg ||
1016: (gc->flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)) {
1017: tty_attributes_fg(tty, gc);
1018: tc->fg = fg;
1.8 nicm 1019: tc->flags &= ~GRID_FLAG_FG256;
1020: tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1 nicm 1021: }
1022:
1023: /* Set background colour. */
1024: if (bg != tc->bg ||
1025: (gc->flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)) {
1026: tty_attributes_bg(tty, gc);
1027: tc->bg = bg;
1.8 nicm 1028: tc->flags &= ~GRID_FLAG_BG256;
1029: tc->flags |= gc->flags & GRID_FLAG_BG256;
1.1 nicm 1030: }
1031: }
1032:
1033: int
1034: tty_try_256(struct tty *tty, u_char colour, const char *type)
1035: {
1036: char s[32];
1037:
1038: if (!(tty->term->flags & TERM_256COLOURS) &&
1039: !(tty->term_flags & TERM_256COLOURS))
1040: return (-1);
1041:
1042: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1043: tty_puts(tty, s);
1044: return (0);
1045: }
1046:
1047: int
1048: tty_try_88(struct tty *tty, u_char colour, const char *type)
1049: {
1050: char s[32];
1051:
1052: if (!(tty->term->flags & TERM_88COLOURS) &&
1053: !(tty->term_flags & TERM_88COLOURS))
1054: return (-1);
1055: colour = colour_256to88(colour);
1056:
1057: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1058: tty_puts(tty, s);
1059: return (0);
1060: }
1061:
1062: void
1063: tty_attributes_fg(struct tty *tty, const struct grid_cell *gc)
1064: {
1065: u_char fg;
1066:
1067: fg = gc->fg;
1068: if (gc->flags & GRID_FLAG_FG256) {
1069: if (tty_try_256(tty, fg, "38") == 0)
1070: return;
1071: if (tty_try_88(tty, fg, "38") == 0)
1072: return;
1073: fg = colour_256to16(fg);
1074: if (fg & 8) {
1075: fg &= 7;
1076: tty_putcode(tty, TTYC_BOLD);
1077: tty->cell.attr |= GRID_ATTR_BRIGHT;
1078: } else if (tty->cell.attr & GRID_ATTR_BRIGHT)
1079: tty_reset(tty);
1080: }
1081:
1082: if (fg == 8 &&
1083: !(tty->term->flags & TERM_HASDEFAULTS) &&
1084: !(tty->term_flags & TERM_HASDEFAULTS))
1085: fg = 7;
1086: if (fg == 8)
1087: tty_puts(tty, "\033[39m");
1088: else
1089: tty_putcode1(tty, TTYC_SETAF, fg);
1090: }
1091:
1092: void
1093: tty_attributes_bg(struct tty *tty, const struct grid_cell *gc)
1094: {
1095: u_char bg;
1096:
1097: bg = gc->bg;
1098: if (gc->flags & GRID_FLAG_BG256) {
1099: if (tty_try_256(tty, bg, "48") == 0)
1100: return;
1101: if (tty_try_88(tty, bg, "48") == 0)
1102: return;
1103: bg = colour_256to16(bg);
1104: if (bg & 8)
1105: bg &= 7;
1106: }
1107:
1108: if (bg == 8 &&
1109: !(tty->term->flags & TERM_HASDEFAULTS) &&
1110: !(tty->term_flags & TERM_HASDEFAULTS))
1111: bg = 0;
1112: if (bg == 8)
1113: tty_puts(tty, "\033[49m");
1114: else
1115: tty_putcode1(tty, TTYC_SETAB, bg);
1116: }