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