Annotation of src/usr.bin/tmux/tty.c, Revision 1.28
1.28 ! nicm 1: /* $OpenBSD: tty.c,v 1.27 2009/08/31 20:46:19 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;
1.28 ! nicm 515: tmpgc.flags = gc->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
! 516: tmpgc.flags |= s->sel.cell.flags & (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.16 nicm 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:
1.26 nicm 599: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
600: (!tty_term_has(tty->term, TTYC_DCH) &&
601: !tty_term_has(tty->term, TTYC_DCH1))) {
1.14 nicm 602: tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 603: return;
604: }
605:
606: tty_reset(tty);
607:
1.14 nicm 608: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.26 nicm 609: if (tty_term_has(tty->term, TTYC_DCH) ||
610: tty_term_has(tty->term, TTYC_DCH1))
611: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.1 nicm 612: }
613:
614: void
1.24 nicm 615: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 616: {
1.12 nicm 617: struct window_pane *wp = ctx->wp;
618: struct screen *s = wp->screen;
1.1 nicm 619:
620: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
621: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 622: tty_redraw_region(tty, ctx);
1.1 nicm 623: return;
624: }
625:
626: tty_reset(tty);
627:
1.14 nicm 628: tty_region(tty, ctx->orupper, ctx->orlower, wp->yoff);
1.1 nicm 629:
1.14 nicm 630: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.12 nicm 631: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1 nicm 632: }
633:
634: void
1.24 nicm 635: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 636: {
1.12 nicm 637: struct window_pane *wp = ctx->wp;
638: struct screen *s = wp->screen;
1.1 nicm 639:
640: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
641: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 642: tty_redraw_region(tty, ctx);
1.1 nicm 643: return;
644: }
645:
646: tty_reset(tty);
647:
1.14 nicm 648: tty_region(tty, ctx->orupper, ctx->orlower, wp->yoff);
1.1 nicm 649:
1.14 nicm 650: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.12 nicm 651: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1 nicm 652: }
653:
654: void
1.24 nicm 655: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 656: {
1.12 nicm 657: struct window_pane *wp = ctx->wp;
658: struct screen *s = wp->screen;
659: u_int i;
1.1 nicm 660:
661: tty_reset(tty);
662:
1.14 nicm 663: tty_cursor(tty, 0, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 664: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
665: tty_term_has(tty->term, TTYC_EL)) {
666: tty_putcode(tty, TTYC_EL);
667: } else {
668: for (i = 0; i < screen_size_x(s); i++)
669: tty_putc(tty, ' ');
670: }
671: }
672:
673: void
1.24 nicm 674: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 675: {
1.12 nicm 676: struct window_pane *wp = ctx->wp;
677: struct screen *s = wp->screen;
678: u_int i;
1.1 nicm 679:
680: tty_reset(tty);
681:
1.14 nicm 682: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 683: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
684: tty_term_has(tty->term, TTYC_EL))
685: tty_putcode(tty, TTYC_EL);
686: else {
1.14 nicm 687: for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1 nicm 688: tty_putc(tty, ' ');
689: }
690: }
691:
692: void
1.24 nicm 693: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 694: {
1.12 nicm 695: struct window_pane *wp = ctx->wp;
696: u_int i;
1.1 nicm 697:
698: tty_reset(tty);
699:
700: if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.14 nicm 701: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 702: tty_putcode(tty, TTYC_EL1);
703: } else {
1.14 nicm 704: tty_cursor(tty, 0, ctx->ocy, wp->xoff, wp->yoff);
705: for (i = 0; i < ctx->ocx + 1; i++)
1.1 nicm 706: tty_putc(tty, ' ');
707: }
708: }
709:
710: void
1.24 nicm 711: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 712: {
1.12 nicm 713: struct window_pane *wp = ctx->wp;
714: struct screen *s = wp->screen;
1.1 nicm 715:
716: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
717: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 718: tty_redraw_region(tty, ctx);
1.1 nicm 719: return;
720: }
721:
722: tty_reset(tty);
723:
1.14 nicm 724: tty_region(tty, ctx->orupper, ctx->orlower, wp->yoff);
1.1 nicm 725:
1.14 nicm 726: if (ctx->ocy == ctx->orupper) {
727: tty_cursor(tty, ctx->ocx, ctx->orupper, wp->xoff, wp->yoff);
1.1 nicm 728: tty_putcode(tty, TTYC_RI);
729: }
730: }
731:
732: void
1.24 nicm 733: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 734: {
1.12 nicm 735: struct window_pane *wp = ctx->wp;
736: struct screen *s = wp->screen;
1.1 nicm 737:
738: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
739: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 740: tty_redraw_region(tty, ctx);
1.1 nicm 741: return;
742: }
743:
744: tty_reset(tty);
745:
1.14 nicm 746: tty_region(tty, ctx->orupper, ctx->orlower, wp->yoff);
1.1 nicm 747:
1.14 nicm 748: if (ctx->ocy == ctx->orlower) {
749: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 750: tty_putc(tty, '\n');
751: }
752: }
753:
754: void
1.24 nicm 755: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 756: {
1.12 nicm 757: struct window_pane *wp = ctx->wp;
758: struct screen *s = wp->screen;
759: u_int i, j;
1.1 nicm 760:
761: tty_reset(tty);
762:
763: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
1.14 nicm 764: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 765: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
766: tty_term_has(tty->term, TTYC_EL)) {
767: tty_putcode(tty, TTYC_EL);
1.14 nicm 768: if (ctx->ocy != screen_size_y(s) - 1) {
769: tty_cursor(tty, 0, ctx->ocy + 1, wp->xoff, wp->yoff);
770: for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1 nicm 771: tty_putcode(tty, TTYC_EL);
772: if (i == screen_size_y(s) - 1)
773: continue;
774: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
775: tty->cy++;
776: }
777: }
778: } else {
1.14 nicm 779: for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1 nicm 780: tty_putc(tty, ' ');
1.14 nicm 781: for (j = ctx->ocy; j < screen_size_y(s); j++) {
1.1 nicm 782: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
783: for (i = 0; i < screen_size_x(s); i++)
784: tty_putc(tty, ' ');
785: }
786: }
787: }
788:
789: void
1.24 nicm 790: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 791: {
1.12 nicm 792: struct window_pane *wp = ctx->wp;
793: struct screen *s = wp->screen;
794: u_int i, j;
1.1 nicm 795:
796: tty_reset(tty);
797:
798: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
799: tty_cursor(tty, 0, 0, wp->xoff, wp->yoff);
800: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
801: tty_term_has(tty->term, TTYC_EL)) {
1.14 nicm 802: for (i = 0; i < ctx->ocy; i++) {
1.1 nicm 803: tty_putcode(tty, TTYC_EL);
804: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
805: tty->cy++;
806: }
807: } else {
1.14 nicm 808: for (j = 0; j < ctx->ocy; j++) {
1.1 nicm 809: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
810: for (i = 0; i < screen_size_x(s); i++)
811: tty_putc(tty, ' ');
812: }
813: }
1.14 nicm 814: for (i = 0; i <= ctx->ocx; i++)
1.1 nicm 815: tty_putc(tty, ' ');
816: }
817:
818: void
1.24 nicm 819: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 820: {
1.12 nicm 821: struct window_pane *wp = ctx->wp;
822: struct screen *s = wp->screen;
823: u_int i, j;
1.1 nicm 824:
825: tty_reset(tty);
826:
827: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
828: tty_cursor(tty, 0, 0, wp->xoff, wp->yoff);
829: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
830: tty_term_has(tty->term, TTYC_EL)) {
831: for (i = 0; i < screen_size_y(s); i++) {
832: tty_putcode(tty, TTYC_EL);
833: if (i != screen_size_y(s) - 1) {
834: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
835: tty->cy++;
836: }
837: }
838: } else {
839: for (j = 0; j < screen_size_y(s); j++) {
840: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
841: for (i = 0; i < screen_size_x(s); i++)
842: tty_putc(tty, ' ');
843: }
1.4 nicm 844: }
845: }
846:
847: void
1.24 nicm 848: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 849: {
1.12 nicm 850: struct window_pane *wp = ctx->wp;
851: struct screen *s = wp->screen;
852: u_int i, j;
1.4 nicm 853:
854: tty_reset(tty);
855:
856: tty_region(tty, 0, screen_size_y(s) - 1, wp->yoff);
857:
858: for (j = 0; j < screen_size_y(s); j++) {
859: tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
860: for (i = 0; i < screen_size_x(s); i++)
861: tty_putc(tty, 'E');
1.1 nicm 862: }
863: }
864:
865: void
1.24 nicm 866: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 867: {
1.12 nicm 868: struct window_pane *wp = ctx->wp;
1.1 nicm 869:
1.14 nicm 870: tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 871:
1.12 nicm 872: tty_cell(tty, ctx->cell, ctx->utf8);
1.1 nicm 873: }
874:
875: void
1.24 nicm 876: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 877: {
1.12 nicm 878: u_char *ptr = ctx->ptr;
1.11 nicm 879: size_t i;
1.1 nicm 880:
1.11 nicm 881: for (i = 0; i < UTF8_SIZE; i++) {
1.12 nicm 882: if (ptr[i] == 0xff)
1.11 nicm 883: break;
1.12 nicm 884: tty_putc(tty, ptr[i]);
1.11 nicm 885: }
1.1 nicm 886: }
887:
888: void
889: tty_cell(
890: struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
891: {
892: u_int i;
893:
894: /* Skip last character if terminal is stupid. */
895: if (tty->term->flags & TERM_EARLYWRAP &&
896: tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
897: return;
898:
899: /* If this is a padding character, do nothing. */
900: if (gc->flags & GRID_FLAG_PADDING)
901: return;
902:
903: /* Set the attributes. */
904: tty_attributes(tty, gc);
905:
906: /* If not UTF-8, write directly. */
907: if (!(gc->flags & GRID_FLAG_UTF8)) {
908: if (gc->data < 0x20 || gc->data == 0x7f)
909: return;
910: tty_putc(tty, gc->data);
911: return;
912: }
913:
914: /* If the terminal doesn't support UTF-8, write underscores. */
915: if (!(tty->flags & TTY_UTF8)) {
916: for (i = 0; i < gu->width; i++)
917: tty_putc(tty, '_');
918: return;
919: }
920:
921: /* Otherwise, write UTF-8. */
1.5 nicm 922: tty_pututf8(tty, gu);
1.1 nicm 923: }
924:
925: void
926: tty_reset(struct tty *tty)
927: {
928: struct grid_cell *gc = &tty->cell;
929:
930: if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
931: return;
932:
933: if (tty_term_has(tty->term, TTYC_RMACS) && gc->attr & GRID_ATTR_CHARSET)
934: tty_putcode(tty, TTYC_RMACS);
935: tty_putcode(tty, TTYC_SGR0);
936: memcpy(gc, &grid_default_cell, sizeof *gc);
937: }
938:
939: void
940: tty_region(struct tty *tty, u_int rupper, u_int rlower, u_int oy)
941: {
942: if (!tty_term_has(tty->term, TTYC_CSR))
943: return;
944: if (tty->rlower != oy + rlower || tty->rupper != oy + rupper) {
945: tty->rlower = oy + rlower;
946: tty->rupper = oy + rupper;
947: tty->cx = 0;
948: tty->cy = 0;
949: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
950: }
951: }
952:
953: void
954: tty_cursor(struct tty *tty, u_int cx, u_int cy, u_int ox, u_int oy)
955: {
956: if (ox + cx == 0 && tty->cx != 0 && tty->cy == oy + cy) {
957: tty->cx = 0;
958: tty_putc(tty, '\r');
959: } else if (tty->cx != ox + cx || tty->cy != oy + cy) {
960: tty->cx = ox + cx;
961: tty->cy = oy + cy;
962: tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
963: }
964: }
965:
966: void
967: tty_attributes(struct tty *tty, const struct grid_cell *gc)
968: {
969: struct grid_cell *tc = &tty->cell;
970: u_char changed;
1.18 nicm 971: u_int fg, bg, attr;
972:
973: /*
974: * If no setab, try to use the reverse attribute as a best-effort for a
975: * non-default background. This is a bit of a hack but it doesn't do
976: * any serious harm and makes a couple of applications happier.
977: */
978: fg = gc->fg; bg = gc->bg; attr = gc->attr;
979: if (!tty_term_has(tty->term, TTYC_SETAB)) {
980: if (attr & GRID_ATTR_REVERSE) {
981: if (fg != 7 && fg != 8)
982: attr &= ~GRID_ATTR_REVERSE;
983: } else {
984: if (bg != 0 && bg != 8)
985: attr |= GRID_ATTR_REVERSE;
986: }
987: }
1.1 nicm 988:
989: /* If any bits are being cleared, reset everything. */
1.18 nicm 990: if (tc->attr & ~attr)
1.1 nicm 991: tty_reset(tty);
992:
993: /* Filter out attribute bits already set. */
1.18 nicm 994: changed = attr & ~tc->attr;
995: tc->attr = attr;
1.1 nicm 996:
997: /* Set the attributes. */
998: if (changed & GRID_ATTR_BRIGHT)
999: tty_putcode(tty, TTYC_BOLD);
1000: if (changed & GRID_ATTR_DIM)
1001: tty_putcode(tty, TTYC_DIM);
1002: if (changed & GRID_ATTR_ITALICS)
1003: tty_putcode(tty, TTYC_SMSO);
1004: if (changed & GRID_ATTR_UNDERSCORE)
1005: tty_putcode(tty, TTYC_SMUL);
1006: if (changed & GRID_ATTR_BLINK)
1007: tty_putcode(tty, TTYC_BLINK);
1008: if (changed & GRID_ATTR_REVERSE) {
1009: if (tty_term_has(tty->term, TTYC_REV))
1010: tty_putcode(tty, TTYC_REV);
1011: else if (tty_term_has(tty->term, TTYC_SMSO))
1012: tty_putcode(tty, TTYC_SMSO);
1013: }
1014: if (changed & GRID_ATTR_HIDDEN)
1015: tty_putcode(tty, TTYC_INVIS);
1016: if (changed & GRID_ATTR_CHARSET)
1017: tty_putcode(tty, TTYC_SMACS);
1018:
1019: /* Set foreground colour. */
1020: if (fg != tc->fg ||
1021: (gc->flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)) {
1022: tty_attributes_fg(tty, gc);
1023: tc->fg = fg;
1.8 nicm 1024: tc->flags &= ~GRID_FLAG_FG256;
1025: tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1 nicm 1026: }
1027:
1028: /* Set background colour. */
1029: if (bg != tc->bg ||
1030: (gc->flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)) {
1031: tty_attributes_bg(tty, gc);
1032: tc->bg = bg;
1.8 nicm 1033: tc->flags &= ~GRID_FLAG_BG256;
1034: tc->flags |= gc->flags & GRID_FLAG_BG256;
1.1 nicm 1035: }
1036: }
1037:
1038: int
1039: tty_try_256(struct tty *tty, u_char colour, const char *type)
1040: {
1041: char s[32];
1042:
1043: if (!(tty->term->flags & TERM_256COLOURS) &&
1044: !(tty->term_flags & TERM_256COLOURS))
1045: return (-1);
1046:
1047: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1048: tty_puts(tty, s);
1049: return (0);
1050: }
1051:
1052: int
1053: tty_try_88(struct tty *tty, u_char colour, const char *type)
1054: {
1055: char s[32];
1056:
1057: if (!(tty->term->flags & TERM_88COLOURS) &&
1058: !(tty->term_flags & TERM_88COLOURS))
1059: return (-1);
1060: colour = colour_256to88(colour);
1061:
1062: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1063: tty_puts(tty, s);
1064: return (0);
1065: }
1066:
1067: void
1068: tty_attributes_fg(struct tty *tty, const struct grid_cell *gc)
1069: {
1070: u_char fg;
1071:
1072: fg = gc->fg;
1073: if (gc->flags & GRID_FLAG_FG256) {
1074: if (tty_try_256(tty, fg, "38") == 0)
1075: return;
1076: if (tty_try_88(tty, fg, "38") == 0)
1077: return;
1078: fg = colour_256to16(fg);
1079: if (fg & 8) {
1080: fg &= 7;
1081: tty_putcode(tty, TTYC_BOLD);
1082: tty->cell.attr |= GRID_ATTR_BRIGHT;
1083: } else if (tty->cell.attr & GRID_ATTR_BRIGHT)
1084: tty_reset(tty);
1085: }
1086:
1087: if (fg == 8 &&
1088: !(tty->term->flags & TERM_HASDEFAULTS) &&
1089: !(tty->term_flags & TERM_HASDEFAULTS))
1090: fg = 7;
1091: if (fg == 8)
1092: tty_puts(tty, "\033[39m");
1093: else
1094: tty_putcode1(tty, TTYC_SETAF, fg);
1095: }
1096:
1097: void
1098: tty_attributes_bg(struct tty *tty, const struct grid_cell *gc)
1099: {
1100: u_char bg;
1101:
1102: bg = gc->bg;
1103: if (gc->flags & GRID_FLAG_BG256) {
1104: if (tty_try_256(tty, bg, "48") == 0)
1105: return;
1106: if (tty_try_88(tty, bg, "48") == 0)
1107: return;
1108: bg = colour_256to16(bg);
1109: if (bg & 8)
1110: bg &= 7;
1111: }
1112:
1113: if (bg == 8 &&
1114: !(tty->term->flags & TERM_HASDEFAULTS) &&
1115: !(tty->term_flags & TERM_HASDEFAULTS))
1116: bg = 0;
1117: if (bg == 8)
1118: tty_puts(tty, "\033[49m");
1119: else
1120: tty_putcode1(tty, TTYC_SETAB, bg);
1121: }