Annotation of src/usr.bin/tmux/tty.c, Revision 1.151
1.151 ! nicm 1: /* $OpenBSD: tty.c,v 1.150 2013/03/22 10:29:31 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:
1.106 nicm 22: #include <netinet/in.h>
23:
1.1 nicm 24: #include <errno.h>
25: #include <fcntl.h>
1.106 nicm 26: #include <resolv.h>
1.42 nicm 27: #include <stdlib.h>
1.1 nicm 28: #include <string.h>
29: #include <termios.h>
30: #include <unistd.h>
31:
32: #include "tmux.h"
33:
1.67 nicm 34: void tty_read_callback(struct bufferevent *, void *);
1.66 nicm 35: void tty_error_callback(struct bufferevent *, short, void *);
36:
1.1 nicm 37: int tty_try_256(struct tty *, u_char, const char *);
38: int tty_try_88(struct tty *, u_char, const char *);
39:
1.85 nicm 40: void tty_colours(struct tty *, const struct grid_cell *);
41: void tty_check_fg(struct tty *, struct grid_cell *);
42: void tty_check_bg(struct tty *, struct grid_cell *);
43: void tty_colours_fg(struct tty *, const struct grid_cell *);
1.73 nicm 44: void tty_colours_bg(struct tty *, const struct grid_cell *);
1.1 nicm 45:
1.120 nicm 46: int tty_large_region(struct tty *, const struct tty_ctx *);
1.24 nicm 47: void tty_redraw_region(struct tty *, const struct tty_ctx *);
1.13 nicm 48: void tty_emulate_repeat(
49: struct tty *, enum tty_code_code, enum tty_code_code, u_int);
1.133 nicm 50: void tty_repeat_space(struct tty *, u_int);
1.147 nicm 51: void tty_cell(struct tty *, const struct grid_cell *);
1.1 nicm 52:
1.90 nicm 53: #define tty_use_acs(tty) \
1.131 nicm 54: (tty_term_has((tty)->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
1.90 nicm 55:
1.132 nicm 56: #define tty_pane_full_width(tty, ctx) \
57: ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
58:
1.1 nicm 59: void
1.136 nicm 60: tty_init(struct tty *tty, struct client *c, int fd, char *term)
1.1 nicm 61: {
1.30 nicm 62: char *path;
63:
64: memset(tty, 0, sizeof *tty);
1.23 nicm 65: tty->log_fd = -1;
1.22 nicm 66:
1.9 nicm 67: if (term == NULL || *term == '\0')
1.1 nicm 68: tty->termname = xstrdup("unknown");
69: else
70: tty->termname = xstrdup(term);
1.30 nicm 71: tty->fd = fd;
1.136 nicm 72: tty->client = c;
1.30 nicm 73:
74: if ((path = ttyname(fd)) == NULL)
75: fatalx("ttyname failed");
76: tty->path = xstrdup(path);
1.108 nicm 77: tty->cstyle = 0;
1.107 nicm 78: tty->ccolour = xstrdup("");
1.30 nicm 79:
1.1 nicm 80: tty->flags = 0;
81: tty->term_flags = 0;
1.31 nicm 82: }
83:
1.88 nicm 84: int
1.31 nicm 85: tty_resize(struct tty *tty)
86: {
87: struct winsize ws;
1.88 nicm 88: u_int sx, sy;
1.31 nicm 89:
90: if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88 nicm 91: sx = ws.ws_col;
92: if (sx == 0)
93: sx = 80;
94: sy = ws.ws_row;
95: if (sy == 0)
96: sy = 24;
97: } else {
98: sx = 80;
99: sy = 24;
1.31 nicm 100: }
1.114 nicm 101: if (!tty_set_size(tty, sx, sy))
1.88 nicm 102: return (0);
1.31 nicm 103:
104: tty->cx = UINT_MAX;
105: tty->cy = UINT_MAX;
106:
107: tty->rupper = UINT_MAX;
108: tty->rlower = UINT_MAX;
1.88 nicm 109:
110: /*
111: * If the terminal has been started, reset the actual scroll region and
112: * cursor position, as this may not have happened.
113: */
114: if (tty->flags & TTY_STARTED) {
115: tty_cursor(tty, 0, 0);
116: tty_region(tty, 0, tty->sy - 1);
117: }
118:
1.114 nicm 119: return (1);
120: }
121:
122: int
123: tty_set_size(struct tty *tty, u_int sx, u_int sy) {
124: if (sx == tty->sx && sy == tty->sy)
125: return (0);
126: tty->sx = sx;
127: tty->sy = sy;
1.88 nicm 128: return (1);
1.1 nicm 129: }
130:
131: int
1.17 nicm 132: tty_open(struct tty *tty, const char *overrides, char **cause)
1.1 nicm 133: {
1.88 nicm 134: char out[64];
1.30 nicm 135: int fd;
1.1 nicm 136:
1.30 nicm 137: if (debug_level > 3) {
1.88 nicm 138: xsnprintf(out, sizeof out, "tmux-out-%ld.log", (long) getpid());
139: fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1.30 nicm 140: if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
141: fatal("fcntl failed");
142: tty->log_fd = fd;
1.1 nicm 143: }
144:
1.17 nicm 145: tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
1.21 nicm 146: if (tty->term == NULL) {
147: tty_close(tty);
148: return (-1);
149: }
150: tty->flags |= TTY_OPENED;
1.1 nicm 151:
1.149 nicm 152: tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_TIMER);
1.1 nicm 153:
1.66 nicm 154: tty->event = bufferevent_new(
1.67 nicm 155: tty->fd, tty_read_callback, NULL, tty_error_callback, tty);
1.1 nicm 156:
157: tty_start_tty(tty);
158:
1.148 nicm 159: tty_keys_build(tty);
1.1 nicm 160:
161: return (0);
162: }
163:
1.73 nicm 164: /* ARGSUSED */
1.1 nicm 165: void
1.67 nicm 166: tty_read_callback(unused struct bufferevent *bufev, void *data)
167: {
168: struct tty *tty = data;
169:
170: while (tty_keys_next(tty))
171: ;
172: }
173:
1.73 nicm 174: /* ARGSUSED */
1.67 nicm 175: void
1.66 nicm 176: tty_error_callback(
177: unused struct bufferevent *bufev, unused short what, unused void *data)
178: {
179: }
180:
181: void
1.127 nicm 182: tty_init_termios(int fd, struct termios *orig_tio, struct bufferevent *bufev)
1.1 nicm 183: {
1.128 nicm 184: struct termios tio;
1.33 nicm 185:
1.127 nicm 186: if (fd == -1 || tcgetattr(fd, orig_tio) != 0)
1.33 nicm 187: return;
1.1 nicm 188:
1.127 nicm 189: setblocking(fd, 0);
1.34 nicm 190:
1.127 nicm 191: if (bufev != NULL)
192: bufferevent_enable(bufev, EV_READ|EV_WRITE);
1.66 nicm 193:
1.127 nicm 194: memcpy(&tio, orig_tio, sizeof tio);
1.1 nicm 195: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
196: tio.c_iflag |= IGNBRK;
197: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
198: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
199: ECHOPRT|ECHOKE|ECHOCTL|ISIG);
200: tio.c_cc[VMIN] = 1;
1.60 deraadt 201: tio.c_cc[VTIME] = 0;
1.127 nicm 202: if (tcsetattr(fd, TCSANOW, &tio) == 0)
203: tcflush(fd, TCIOFLUSH);
204: }
205:
206: void
207: tty_start_tty(struct tty *tty)
208: {
209: tty_init_termios(tty->fd, &tty->tio, tty->event);
1.1 nicm 210:
1.10 nicm 211: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 212:
1.25 nicm 213: tty_putcode(tty, TTYC_SGR0);
214: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
215:
1.76 nicm 216: tty_putcode(tty, TTYC_RMKX);
1.90 nicm 217: if (tty_use_acs(tty))
218: tty_putcode(tty, TTYC_ENACS);
1.1 nicm 219: tty_putcode(tty, TTYC_CLEAR);
220:
221: tty_putcode(tty, TTYC_CNORM);
222: if (tty_term_has(tty->term, TTYC_KMOUS))
223: tty_puts(tty, "\033[?1000l");
1.121 nicm 224:
225: if (tty_term_has(tty->term, TTYC_XT))
1.151 ! nicm 226: tty_puts(tty, "\033[c\033[>4;1m");
1.1 nicm 227:
228: tty->cx = UINT_MAX;
229: tty->cy = UINT_MAX;
230:
231: tty->rlower = UINT_MAX;
232: tty->rupper = UINT_MAX;
233:
234: tty->mode = MODE_CURSOR;
1.20 nicm 235:
236: tty->flags |= TTY_STARTED;
1.107 nicm 237:
238: tty_force_cursor_colour(tty, "");
1.145 nicm 239: }
240:
241: void
242: tty_set_class(struct tty *tty, u_int class)
243: {
244: if (tty->class != 0)
245: return;
246: tty->class = class;
1.128 nicm 247: }
248:
249: void
1.1 nicm 250: tty_stop_tty(struct tty *tty)
251: {
252: struct winsize ws;
253:
1.20 nicm 254: if (!(tty->flags & TTY_STARTED))
255: return;
256: tty->flags &= ~TTY_STARTED;
257:
1.66 nicm 258: bufferevent_disable(tty->event, EV_READ|EV_WRITE);
259:
1.1 nicm 260: /*
261: * Be flexible about error handling and try not kill the server just
262: * because the fd is invalid. Things like ssh -t can easily leave us
263: * with a dead tty.
264: */
265: if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
266: return;
267: if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
268: return;
269:
270: tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.90 nicm 271: if (tty_use_acs(tty))
272: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1 nicm 273: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
274: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
275: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.108 nicm 276: if (tty_term_has(tty->term, TTYC_CS1) && tty->cstyle != 0) {
277: if (tty_term_has(tty->term, TTYC_CSR1))
278: tty_raw(tty, tty_term_string(tty->term, TTYC_CSR1));
1.109 nicm 279: else
1.108 nicm 280: tty_raw(tty, tty_term_string1(tty->term, TTYC_CS1, 0));
281: }
1.107 nicm 282: tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1 nicm 283:
284: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
285: if (tty_term_has(tty->term, TTYC_KMOUS))
286: tty_raw(tty, "\033[?1000l");
1.151 ! nicm 287:
! 288: if (tty_term_has(tty->term, TTYC_XT))
! 289: tty_puts(tty, "\033[>4m");
1.10 nicm 290:
291: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150 nicm 292:
293: setblocking(tty->fd, 1);
1.1 nicm 294: }
295:
296: void
1.20 nicm 297: tty_close(struct tty *tty)
1.1 nicm 298: {
299: if (tty->log_fd != -1) {
300: close(tty->log_fd);
301: tty->log_fd = -1;
302: }
303:
1.123 nicm 304: if (event_initialized(&tty->key_timer))
305: evtimer_del(&tty->key_timer);
1.20 nicm 306: tty_stop_tty(tty);
1.1 nicm 307:
1.21 nicm 308: if (tty->flags & TTY_OPENED) {
1.66 nicm 309: bufferevent_free(tty->event);
310:
1.21 nicm 311: tty_term_free(tty->term);
312: tty_keys_free(tty);
313:
314: tty->flags &= ~TTY_OPENED;
315: }
1.1 nicm 316:
1.21 nicm 317: if (tty->fd != -1) {
318: close(tty->fd);
319: tty->fd = -1;
320: }
1.1 nicm 321: }
322:
323: void
1.20 nicm 324: tty_free(struct tty *tty)
1.1 nicm 325: {
1.20 nicm 326: tty_close(tty);
1.1 nicm 327:
1.138 nicm 328: free(tty->ccolour);
1.1 nicm 329: if (tty->path != NULL)
1.138 nicm 330: free(tty->path);
1.1 nicm 331: if (tty->termname != NULL)
1.138 nicm 332: free(tty->termname);
1.1 nicm 333: }
334:
335: void
336: tty_raw(struct tty *tty, const char *s)
337: {
1.150 nicm 338: ssize_t n, slen;
339: u_int i;
340:
341: slen = strlen(s);
342: for (i = 0; i < 5; i++) {
343: n = write(tty->fd, s, slen);
344: if (n >= 0) {
345: s += n;
346: slen -= n;
347: if (slen == 0)
348: break;
349: } else if (n == -1 && errno != EAGAIN)
350: break;
351: usleep(100);
352: }
1.1 nicm 353: }
354:
355: void
356: tty_putcode(struct tty *tty, enum tty_code_code code)
357: {
358: tty_puts(tty, tty_term_string(tty->term, code));
359: }
360:
361: void
362: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
363: {
364: if (a < 0)
365: return;
366: tty_puts(tty, tty_term_string1(tty->term, code, a));
367: }
368:
369: void
370: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
371: {
372: if (a < 0 || b < 0)
373: return;
374: tty_puts(tty, tty_term_string2(tty->term, code, a, b));
375: }
376:
377: void
1.107 nicm 378: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
379: {
380: if (a != NULL)
381: tty_puts(tty, tty_term_ptr1(tty->term, code, a));
382: }
383:
384: void
1.106 nicm 385: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a, const void *b)
386: {
387: if (a != NULL && b != NULL)
388: tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
389: }
390:
391: void
1.1 nicm 392: tty_puts(struct tty *tty, const char *s)
393: {
394: if (*s == '\0')
395: return;
1.66 nicm 396: bufferevent_write(tty->event, s, strlen(s));
1.1 nicm 397:
398: if (tty->log_fd != -1)
399: write(tty->log_fd, s, strlen(s));
400: }
401:
402: void
403: tty_putc(struct tty *tty, u_char ch)
404: {
1.90 nicm 405: const char *acs;
406: u_int sx;
1.1 nicm 407:
1.90 nicm 408: if (tty->cell.attr & GRID_ATTR_CHARSET) {
409: acs = tty_acs_get(tty, ch);
410: if (acs != NULL)
411: bufferevent_write(tty->event, acs, strlen(acs));
412: else
413: bufferevent_write(tty->event, &ch, 1);
414: } else
415: bufferevent_write(tty->event, &ch, 1);
1.1 nicm 416:
417: if (ch >= 0x20 && ch != 0x7f) {
418: sx = tty->sx;
419: if (tty->term->flags & TERM_EARLYWRAP)
420: sx--;
421:
1.46 nicm 422: if (tty->cx >= sx) {
423: tty->cx = 1;
424: if (tty->cy != tty->rlower)
425: tty->cy++;
1.1 nicm 426: } else
427: tty->cx++;
428: }
429:
430: if (tty->log_fd != -1)
431: write(tty->log_fd, &ch, 1);
432: }
433:
434: void
1.147 nicm 435: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5 nicm 436: {
1.147 nicm 437: bufferevent_write(tty->event, buf, len);
1.71 nicm 438: if (tty->log_fd != -1)
1.147 nicm 439: write(tty->log_fd, buf, len);
440: tty->cx += width;
1.5 nicm 441: }
442:
443: void
1.1 nicm 444: tty_set_title(struct tty *tty, const char *title)
445: {
1.105 nicm 446: if (!tty_term_has(tty->term, TTYC_TSL) ||
447: !tty_term_has(tty->term, TTYC_FSL))
1.1 nicm 448: return;
449:
1.105 nicm 450: tty_putcode(tty, TTYC_TSL);
1.1 nicm 451: tty_puts(tty, title);
1.105 nicm 452: tty_putcode(tty, TTYC_FSL);
1.1 nicm 453: }
454:
455: void
1.107 nicm 456: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
457: {
458: if (*ccolour == '\0')
459: tty_putcode(tty, TTYC_CR);
460: else
461: tty_putcode_ptr1(tty, TTYC_CC, ccolour);
1.138 nicm 462: free(tty->ccolour);
1.107 nicm 463: tty->ccolour = xstrdup(ccolour);
464: }
465:
466: void
467: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1 nicm 468: {
469: int changed;
470:
1.107 nicm 471: if (strcmp(s->ccolour, tty->ccolour))
472: tty_force_cursor_colour(tty, s->ccolour);
473:
1.1 nicm 474: if (tty->flags & TTY_NOCURSOR)
475: mode &= ~MODE_CURSOR;
476:
477: changed = mode ^ tty->mode;
478: if (changed & MODE_CURSOR) {
479: if (mode & MODE_CURSOR)
480: tty_putcode(tty, TTYC_CNORM);
481: else
482: tty_putcode(tty, TTYC_CIVIS);
1.108 nicm 483: }
484: if (tty->cstyle != s->cstyle) {
485: if (tty_term_has(tty->term, TTYC_CS1)) {
486: if (s->cstyle == 0 &&
487: tty_term_has(tty->term, TTYC_CSR1))
488: tty_putcode(tty, TTYC_CSR1);
489: else
490: tty_putcode1(tty, TTYC_CS1, s->cstyle);
491: }
492: tty->cstyle = s->cstyle;
1.1 nicm 493: }
1.94 nicm 494: if (changed & ALL_MOUSE_MODES) {
495: if (mode & ALL_MOUSE_MODES) {
1.95 nicm 496: if (mode & MODE_MOUSE_UTF8)
497: tty_puts(tty, "\033[?1005h");
1.98 nicm 498: if (mode & MODE_MOUSE_ANY)
499: tty_puts(tty, "\033[?1003h");
1.94 nicm 500: else if (mode & MODE_MOUSE_BUTTON)
501: tty_puts(tty, "\033[?1002h");
1.98 nicm 502: else if (mode & MODE_MOUSE_STANDARD)
503: tty_puts(tty, "\033[?1000h");
1.86 nicm 504: } else {
1.98 nicm 505: if (tty->mode & MODE_MOUSE_ANY)
506: tty_puts(tty, "\033[?1003l");
1.94 nicm 507: else if (tty->mode & MODE_MOUSE_BUTTON)
508: tty_puts(tty, "\033[?1002l");
1.98 nicm 509: else if (tty->mode & MODE_MOUSE_STANDARD)
510: tty_puts(tty, "\033[?1000l");
1.95 nicm 511: if (tty->mode & MODE_MOUSE_UTF8)
512: tty_puts(tty, "\033[?1005l");
1.86 nicm 513: }
1.76 nicm 514: }
515: if (changed & MODE_KKEYPAD) {
516: if (mode & MODE_KKEYPAD)
517: tty_putcode(tty, TTYC_SMKX);
518: else
519: tty_putcode(tty, TTYC_RMKX);
1.115 nicm 520: }
521: if (changed & MODE_BRACKETPASTE) {
522: if (mode & MODE_BRACKETPASTE)
523: tty_puts(tty, "\033[?2004h");
524: else
525: tty_puts(tty, "\033[?2004l");
1.1 nicm 526: }
527: tty->mode = mode;
528: }
529:
530: void
531: tty_emulate_repeat(
532: struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
533: {
534: if (tty_term_has(tty->term, code))
535: tty_putcode1(tty, code, n);
536: else {
537: while (n-- > 0)
538: tty_putcode(tty, code1);
539: }
540: }
541:
1.133 nicm 542: void
543: tty_repeat_space(struct tty *tty, u_int n)
544: {
545: while (n-- > 0)
546: tty_putc(tty, ' ');
547: }
548:
1.1 nicm 549: /*
1.119 nicm 550: * Is the region large enough to be worth redrawing once later rather than
551: * probably several times now? Currently yes if it is more than 50% of the
552: * pane.
553: */
554: int
555: tty_large_region(unused struct tty *tty, const struct tty_ctx *ctx)
556: {
557: struct window_pane *wp = ctx->wp;
558:
559: return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
560: }
561:
562: /*
1.1 nicm 563: * Redraw scroll region using data from screen (already updated). Used when
564: * CSR not supported, or window is a pane that doesn't take up the full
565: * width of the terminal.
566: */
567: void
1.24 nicm 568: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 569: {
1.14 nicm 570: struct window_pane *wp = ctx->wp;
571: struct screen *s = wp->screen;
572: u_int i;
1.1 nicm 573:
574: /*
1.119 nicm 575: * If region is large, schedule a window redraw. In most cases this is
576: * likely to be followed by some more scrolling.
1.1 nicm 577: */
1.119 nicm 578: if (tty_large_region(tty, ctx)) {
1.1 nicm 579: wp->flags |= PANE_REDRAW;
580: return;
581: }
582:
1.14 nicm 583: if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
584: for (i = ctx->ocy; i < screen_size_y(s); i++)
1.113 nicm 585: tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
1.1 nicm 586: } else {
1.14 nicm 587: for (i = ctx->orupper; i <= ctx->orlower; i++)
1.113 nicm 588: tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
1.1 nicm 589: }
590: }
591:
592: void
593: tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
594: {
595: const struct grid_cell *gc;
1.46 nicm 596: struct grid_line *gl;
1.16 nicm 597: struct grid_cell tmpgc;
1.147 nicm 598: struct utf8_data ud;
1.1 nicm 599: u_int i, sx;
600:
1.107 nicm 601: tty_update_mode(tty, tty->mode & ~MODE_CURSOR, s);
1.35 nicm 602:
1.1 nicm 603: sx = screen_size_x(s);
1.19 nicm 604: if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
605: sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1 nicm 606: if (sx > tty->sx)
607: sx = tty->sx;
608:
1.46 nicm 609: /*
610: * Don't move the cursor to the start permission if it will wrap there
1.50 nicm 611: * itself.
1.46 nicm 612: */
613: gl = NULL;
614: if (py != 0)
615: gl = &s->grid->linedata[s->grid->hsize + py - 1];
1.83 nicm 616: if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
1.47 nicm 617: tty->cx < tty->sx || ox != 0 ||
618: (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
1.46 nicm 619: tty_cursor(tty, ox, oy + py);
620:
1.1 nicm 621: for (i = 0; i < sx; i++) {
622: gc = grid_view_peek_cell(s->grid, i, py);
623: if (screen_check_selection(s, i, py)) {
1.16 nicm 624: memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
1.147 nicm 625: grid_cell_get(gc, &ud);
626: grid_cell_set(&tmpgc, &ud);
1.77 nicm 627: tmpgc.flags = gc->flags &
1.38 nicm 628: ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
629: tmpgc.flags |= s->sel.cell.flags &
630: (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.147 nicm 631: tty_cell(tty, &tmpgc);
1.1 nicm 632: } else
1.147 nicm 633: tty_cell(tty, gc);
1.1 nicm 634: }
635:
1.35 nicm 636: if (sx >= tty->sx) {
1.107 nicm 637: tty_update_mode(tty, tty->mode, s);
1.1 nicm 638: return;
1.35 nicm 639: }
1.1 nicm 640: tty_reset(tty);
641:
1.41 nicm 642: tty_cursor(tty, ox + sx, oy + py);
1.126 nicm 643: if (sx != screen_size_x(s) && ox + screen_size_x(s) >= tty->sx &&
1.117 nicm 644: tty_term_has(tty->term, TTYC_EL))
1.1 nicm 645: tty_putcode(tty, TTYC_EL);
1.133 nicm 646: else
647: tty_repeat_space(tty, screen_size_x(s) - sx);
1.107 nicm 648: tty_update_mode(tty, tty->mode, s);
1.15 nicm 649: }
650:
651: void
1.113 nicm 652: tty_write(
653: void (*cmdfn)(struct tty *, const struct tty_ctx *), struct tty_ctx *ctx)
1.15 nicm 654: {
655: struct window_pane *wp = ctx->wp;
656: struct client *c;
657: u_int i;
658:
1.75 nicm 659: /* wp can be NULL if updating the screen but not the terminal. */
1.15 nicm 660: if (wp == NULL)
661: return;
662:
663: if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
664: return;
1.129 nicm 665: if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
1.15 nicm 666: return;
667:
668: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
669: c = ARRAY_ITEM(&clients, i);
1.139 nicm 670: if (c == NULL || c->session == NULL || c->tty.term == NULL)
1.15 nicm 671: continue;
1.142 nicm 672: if (c->flags & CLIENT_SUSPENDED)
673: continue;
674: if (c->tty.flags & TTY_FREEZE)
1.139 nicm 675: continue;
676: if (c->session->curw->window != wp->window)
1.15 nicm 677: continue;
678:
1.139 nicm 679: ctx->xoff = wp->xoff;
680: ctx->yoff = wp->yoff;
681: if (status_at_line(c) == 0)
682: ctx->yoff++;
1.113 nicm 683:
1.139 nicm 684: cmdfn(&c->tty, ctx);
1.1 nicm 685: }
686: }
687:
688: void
1.24 nicm 689: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 690: {
1.77 nicm 691: struct window_pane *wp = ctx->wp;
1.1 nicm 692:
1.135 nicm 693: if (!tty_pane_full_width(tty, ctx)) {
1.113 nicm 694: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 695: return;
696: }
697:
698: tty_reset(tty);
699:
1.77 nicm 700: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 701:
1.1 nicm 702: if (tty_term_has(tty->term, TTYC_ICH) ||
703: tty_term_has(tty->term, TTYC_ICH1))
1.12 nicm 704: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.134 nicm 705: else
1.113 nicm 706: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 707: }
708:
709: void
1.24 nicm 710: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 711: {
1.77 nicm 712: struct window_pane *wp = ctx->wp;
1.1 nicm 713:
1.135 nicm 714: if (!tty_pane_full_width(tty, ctx) ||
1.26 nicm 715: (!tty_term_has(tty->term, TTYC_DCH) &&
716: !tty_term_has(tty->term, TTYC_DCH1))) {
1.113 nicm 717: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 718: return;
719: }
720:
721: tty_reset(tty);
722:
1.77 nicm 723: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 724:
1.26 nicm 725: if (tty_term_has(tty->term, TTYC_DCH) ||
726: tty_term_has(tty->term, TTYC_DCH1))
727: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146 nicm 728: }
729:
730: void
731: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
732: {
733: u_int i;
734:
735: tty_reset(tty);
736:
737: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
738:
739: if (tty_term_has(tty->term, TTYC_ECH))
740: tty_putcode1(tty, TTYC_ECH, ctx->num);
741: else {
742: for (i = 0; i < ctx->num; i++)
743: tty_putc(tty, ' ');
744: }
1.1 nicm 745: }
746:
747: void
1.24 nicm 748: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 749: {
1.132 nicm 750: if (!tty_pane_full_width(tty, ctx) ||
1.77 nicm 751: !tty_term_has(tty->term, TTYC_CSR) ||
1.72 nicm 752: !tty_term_has(tty->term, TTYC_IL1)) {
1.14 nicm 753: tty_redraw_region(tty, ctx);
1.1 nicm 754: return;
755: }
756:
757: tty_reset(tty);
758:
1.77 nicm 759: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
760: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 761:
1.12 nicm 762: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1 nicm 763: }
764:
765: void
1.24 nicm 766: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 767: {
1.132 nicm 768: if (!tty_pane_full_width(tty, ctx) ||
1.72 nicm 769: !tty_term_has(tty->term, TTYC_CSR) ||
770: !tty_term_has(tty->term, TTYC_DL1)) {
1.14 nicm 771: tty_redraw_region(tty, ctx);
1.1 nicm 772: return;
773: }
774:
775: tty_reset(tty);
776:
1.77 nicm 777: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
778: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 779:
1.12 nicm 780: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1 nicm 781: }
782:
783: void
1.24 nicm 784: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 785: {
1.77 nicm 786: struct window_pane *wp = ctx->wp;
1.12 nicm 787: struct screen *s = wp->screen;
1.1 nicm 788:
789: tty_reset(tty);
790:
1.77 nicm 791: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41 nicm 792:
1.133 nicm 793: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
1.1 nicm 794: tty_putcode(tty, TTYC_EL);
1.133 nicm 795: else
796: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 797: }
798:
799: void
1.24 nicm 800: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 801: {
1.77 nicm 802: struct window_pane *wp = ctx->wp;
1.12 nicm 803: struct screen *s = wp->screen;
1.1 nicm 804:
805: tty_reset(tty);
806:
1.41 nicm 807: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
808:
1.132 nicm 809: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
1.1 nicm 810: tty_putcode(tty, TTYC_EL);
1.133 nicm 811: else
812: tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.1 nicm 813: }
814:
815: void
1.24 nicm 816: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 817: {
818: tty_reset(tty);
819:
1.113 nicm 820: if (ctx->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.41 nicm 821: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 822: tty_putcode(tty, TTYC_EL1);
823: } else {
1.41 nicm 824: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133 nicm 825: tty_repeat_space(tty, ctx->ocx + 1);
1.1 nicm 826: }
827: }
828:
829: void
1.24 nicm 830: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 831: {
1.56 nicm 832: if (ctx->ocy != ctx->orupper)
833: return;
834:
1.132 nicm 835: if (!tty_pane_full_width(tty, ctx) ||
1.70 nicm 836: !tty_term_has(tty->term, TTYC_CSR) ||
837: !tty_term_has(tty->term, TTYC_RI)) {
1.14 nicm 838: tty_redraw_region(tty, ctx);
1.1 nicm 839: return;
840: }
841:
1.56 nicm 842: tty_reset(tty);
1.77 nicm 843:
1.56 nicm 844: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
845: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 846:
1.56 nicm 847: tty_putcode(tty, TTYC_RI);
1.1 nicm 848: }
849:
850: void
1.24 nicm 851: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 852: {
1.77 nicm 853: struct window_pane *wp = ctx->wp;
1.1 nicm 854:
1.56 nicm 855: if (ctx->ocy != ctx->orlower)
856: return;
857:
1.132 nicm 858: if (!tty_pane_full_width(tty, ctx) ||
1.1 nicm 859: !tty_term_has(tty->term, TTYC_CSR)) {
1.128 nicm 860: if (tty_large_region(tty, ctx))
861: wp->flags |= PANE_REDRAW;
1.140 nicm 862: else
1.128 nicm 863: tty_redraw_region(tty, ctx);
1.1 nicm 864: return;
865: }
1.52 nicm 866:
867: /*
868: * If this line wrapped naturally (ctx->num is nonzero), don't do
869: * anything - the cursor can just be moved to the last cell and wrap
870: * naturally.
871: */
872: if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
873: return;
1.1 nicm 874:
1.56 nicm 875: tty_reset(tty);
1.77 nicm 876:
1.56 nicm 877: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
878: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 879:
1.56 nicm 880: tty_putc(tty, '\n');
1.1 nicm 881: }
882:
883: void
1.24 nicm 884: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 885: {
1.77 nicm 886: struct window_pane *wp = ctx->wp;
1.12 nicm 887: struct screen *s = wp->screen;
888: u_int i, j;
1.1 nicm 889:
890: tty_reset(tty);
891:
1.39 nicm 892: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 893: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39 nicm 894:
1.132 nicm 895: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.1 nicm 896: tty_putcode(tty, TTYC_EL);
1.14 nicm 897: if (ctx->ocy != screen_size_y(s) - 1) {
1.41 nicm 898: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14 nicm 899: for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1 nicm 900: tty_putcode(tty, TTYC_EL);
901: if (i == screen_size_y(s) - 1)
902: continue;
903: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
904: tty->cy++;
905: }
906: }
907: } else {
1.133 nicm 908: tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.68 nicm 909: for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
1.41 nicm 910: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 911: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 912: }
913: }
914: }
915:
916: void
1.24 nicm 917: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 918: {
1.77 nicm 919: struct window_pane *wp = ctx->wp;
1.12 nicm 920: struct screen *s = wp->screen;
921: u_int i, j;
1.1 nicm 922:
923: tty_reset(tty);
924:
1.39 nicm 925: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 926: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 927:
1.132 nicm 928: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.14 nicm 929: for (i = 0; i < ctx->ocy; i++) {
1.1 nicm 930: tty_putcode(tty, TTYC_EL);
931: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
932: tty->cy++;
933: }
934: } else {
1.14 nicm 935: for (j = 0; j < ctx->ocy; j++) {
1.41 nicm 936: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 937: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 938: }
939: }
1.133 nicm 940: tty_repeat_space(tty, ctx->ocx + 1);
1.1 nicm 941: }
942:
943: void
1.24 nicm 944: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 945: {
1.77 nicm 946: struct window_pane *wp = ctx->wp;
1.12 nicm 947: struct screen *s = wp->screen;
948: u_int i, j;
1.1 nicm 949:
950: tty_reset(tty);
951:
1.39 nicm 952: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 953: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 954:
1.132 nicm 955: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.1 nicm 956: for (i = 0; i < screen_size_y(s); i++) {
957: tty_putcode(tty, TTYC_EL);
958: if (i != screen_size_y(s) - 1) {
959: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
960: tty->cy++;
961: }
962: }
963: } else {
964: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 965: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 966: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 967: }
1.4 nicm 968: }
969: }
970:
971: void
1.24 nicm 972: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 973: {
1.12 nicm 974: struct window_pane *wp = ctx->wp;
975: struct screen *s = wp->screen;
976: u_int i, j;
1.4 nicm 977:
978: tty_reset(tty);
979:
1.39 nicm 980: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4 nicm 981:
982: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 983: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 984: for (i = 0; i < screen_size_x(s); i++)
985: tty_putc(tty, 'E');
1.1 nicm 986: }
987: }
988:
989: void
1.24 nicm 990: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 991: {
1.46 nicm 992: struct window_pane *wp = ctx->wp;
993: struct screen *s = wp->screen;
1.58 nicm 994: u_int cx;
1.102 nicm 995: u_int width;
1.46 nicm 996:
997: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
998:
1.57 nicm 999: /* Is the cursor in the very last position? */
1.147 nicm 1000: width = grid_cell_width(ctx->cell);
1.102 nicm 1001: if (ctx->ocx > wp->sx - width) {
1.113 nicm 1002: if (ctx->xoff != 0 || wp->sx != tty->sx) {
1.57 nicm 1003: /*
1004: * The pane doesn't fill the entire line, the linefeed
1005: * will already have happened, so just move the cursor.
1006: */
1.125 nicm 1007: if (ctx->ocy != wp->yoff + wp->screen->rlower)
1.122 nicm 1008: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1009: else
1010: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.57 nicm 1011: } else if (tty->cx < tty->sx) {
1012: /*
1013: * The cursor isn't in the last position already, so
1.102 nicm 1014: * move as far left as possible and redraw the last
1.57 nicm 1015: * cell to move into the last position.
1016: */
1.147 nicm 1017: cx = screen_size_x(s) - grid_cell_width(&ctx->last_cell);
1.50 nicm 1018: tty_cursor_pane(tty, ctx, cx, ctx->ocy);
1.147 nicm 1019: tty_cell(tty, &ctx->last_cell);
1.50 nicm 1020: }
1021: } else
1.46 nicm 1022: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1023:
1.147 nicm 1024: tty_cell(tty, ctx->cell);
1.1 nicm 1025: }
1026:
1027: void
1.24 nicm 1028: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1029: {
1.53 nicm 1030: struct window_pane *wp = ctx->wp;
1.1 nicm 1031:
1.53 nicm 1032: /*
1033: * Cannot rely on not being a partial character, so just redraw the
1034: * whole line.
1035: */
1.113 nicm 1036: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.106 nicm 1037: }
1038:
1039: void
1040: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1041: {
1042: char *buf;
1043: size_t off;
1044:
1045: if (!tty_term_has(tty->term, TTYC_MS))
1046: return;
1047:
1048: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1049: buf = xmalloc(off);
1050:
1051: b64_ntop(ctx->ptr, ctx->num, buf, off);
1052: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1053:
1.138 nicm 1054: free(buf);
1.100 nicm 1055: }
1056:
1057: void
1058: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1059: {
1060: u_int i;
1061: u_char *str = ctx->ptr;
1062:
1063: for (i = 0; i < ctx->num; i++)
1064: tty_putc(tty, str[i]);
1.141 nicm 1065:
1066: tty->cx = tty->cy = UINT_MAX;
1067: tty->rupper = tty->rlower = UINT_MAX;
1068:
1069: tty_reset(tty);
1070: tty_cursor(tty, 0, 0);
1.1 nicm 1071: }
1072:
1073: void
1.147 nicm 1074: tty_cell(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1075: {
1.147 nicm 1076: struct utf8_data ud;
1077: u_int i;
1.1 nicm 1078:
1079: /* Skip last character if terminal is stupid. */
1080: if (tty->term->flags & TERM_EARLYWRAP &&
1081: tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
1082: return;
1083:
1084: /* If this is a padding character, do nothing. */
1085: if (gc->flags & GRID_FLAG_PADDING)
1086: return;
1087:
1088: /* Set the attributes. */
1089: tty_attributes(tty, gc);
1090:
1.147 nicm 1091: /* Get the cell and if ASCII write with putc to do ACS translation. */
1092: grid_cell_get(gc, &ud);
1093: if (ud.size == 1) {
1094: if (*ud.data < 0x20 || *ud.data == 0x7f)
1.1 nicm 1095: return;
1.147 nicm 1096: tty_putc(tty, *ud.data);
1.1 nicm 1097: return;
1098: }
1099:
1.147 nicm 1100: /* If not UTF-8, write _. */
1.1 nicm 1101: if (!(tty->flags & TTY_UTF8)) {
1.147 nicm 1102: for (i = 0; i < ud.width; i++)
1.1 nicm 1103: tty_putc(tty, '_');
1104: return;
1105: }
1106:
1.147 nicm 1107: /* Write the data. */
1108: tty_putn(tty, ud.data, ud.size, ud.width);
1.1 nicm 1109: }
1110:
1111: void
1112: tty_reset(struct tty *tty)
1113: {
1114: struct grid_cell *gc = &tty->cell;
1115:
1116: if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
1117: return;
1118:
1.90 nicm 1119: if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1120: tty_putcode(tty, TTYC_RMACS);
1121: tty_putcode(tty, TTYC_SGR0);
1122: memcpy(gc, &grid_default_cell, sizeof *gc);
1123: }
1124:
1.40 nicm 1125: /* Set region inside pane. */
1.1 nicm 1126: void
1.39 nicm 1127: tty_region_pane(
1128: struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1.1 nicm 1129: {
1.113 nicm 1130: tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39 nicm 1131: }
1132:
1.40 nicm 1133: /* Set region at absolute position. */
1.39 nicm 1134: void
1.40 nicm 1135: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 1136: {
1137: if (tty->rlower == rlower && tty->rupper == rupper)
1138: return;
1.1 nicm 1139: if (!tty_term_has(tty->term, TTYC_CSR))
1140: return;
1.39 nicm 1141:
1142: tty->rupper = rupper;
1143: tty->rlower = rlower;
1.55 nicm 1144:
1145: /*
1146: * Some terminals (such as PuTTY) do not correctly reset the cursor to
1147: * 0,0 if it is beyond the last column (they do not reset their wrap
1148: * flag so further output causes a line feed). As a workaround, do an
1149: * explicit move to 0 first.
1150: */
1151: if (tty->cx >= tty->sx)
1152: tty_cursor(tty, 0, tty->cy);
1.42 nicm 1153:
1.39 nicm 1154: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.78 nicm 1155: tty_cursor(tty, 0, 0);
1.1 nicm 1156: }
1157:
1.42 nicm 1158: /* Move cursor inside pane. */
1.1 nicm 1159: void
1.41 nicm 1160: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1161: {
1.113 nicm 1162: tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41 nicm 1163: }
1164:
1.42 nicm 1165: /* Move cursor to absolute position. */
1.41 nicm 1166: void
1167: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 1168: {
1.42 nicm 1169: struct tty_term *term = tty->term;
1170: u_int thisx, thisy;
1171: int change;
1.77 nicm 1172:
1.42 nicm 1173: if (cx > tty->sx - 1)
1174: cx = tty->sx - 1;
1175:
1176: thisx = tty->cx;
1177: thisy = tty->cy;
1178:
1179: /* No change. */
1180: if (cx == thisx && cy == thisy)
1181: return;
1182:
1.43 nicm 1183: /* Very end of the line, just use absolute movement. */
1184: if (thisx > tty->sx - 1)
1185: goto absolute;
1186:
1.42 nicm 1187: /* Move to home position (0, 0). */
1188: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1189: tty_putcode(tty, TTYC_HOME);
1190: goto out;
1191: }
1192:
1193: /* Zero on the next line. */
1.48 nicm 1194: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1 nicm 1195: tty_putc(tty, '\r');
1.42 nicm 1196: tty_putc(tty, '\n');
1197: goto out;
1198: }
1199:
1.45 nicm 1200: /* Moving column or row. */
1.42 nicm 1201: if (cy == thisy) {
1.45 nicm 1202: /*
1203: * Moving column only, row staying the same.
1204: */
1205:
1.42 nicm 1206: /* To left edge. */
1207: if (cx == 0) {
1208: tty_putc(tty, '\r');
1209: goto out;
1210: }
1211:
1212: /* One to the left. */
1213: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1214: tty_putcode(tty, TTYC_CUB1);
1215: goto out;
1216: }
1217:
1218: /* One to the right. */
1219: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1220: tty_putcode(tty, TTYC_CUF1);
1221: goto out;
1222: }
1223:
1224: /* Calculate difference. */
1225: change = thisx - cx; /* +ve left, -ve right */
1226:
1227: /*
1228: * Use HPA if change is larger than absolute, otherwise move
1229: * the cursor with CUB/CUF.
1230: */
1.87 nicm 1231: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 1232: tty_putcode1(tty, TTYC_HPA, cx);
1233: goto out;
1234: } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1235: tty_putcode1(tty, TTYC_CUB, change);
1236: goto out;
1237: } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1238: tty_putcode1(tty, TTYC_CUF, -change);
1239: goto out;
1240: }
1.45 nicm 1241: } else if (cx == thisx) {
1242: /*
1243: * Moving row only, column staying the same.
1244: */
1.42 nicm 1245:
1246: /* One above. */
1.77 nicm 1247: if (thisy != tty->rupper &&
1.42 nicm 1248: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1249: tty_putcode(tty, TTYC_CUU1);
1250: goto out;
1251: }
1252:
1253: /* One below. */
1.49 nicm 1254: if (thisy != tty->rlower &&
1.42 nicm 1255: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1256: tty_putcode(tty, TTYC_CUD1);
1257: goto out;
1258: }
1259:
1260: /* Calculate difference. */
1261: change = thisy - cy; /* +ve up, -ve down */
1262:
1263: /*
1.77 nicm 1264: * Try to use VPA if change is larger than absolute or if this
1265: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 1266: */
1.87 nicm 1267: if ((u_int) abs(change) > cy ||
1.42 nicm 1268: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 1269: (change > 0 && cy - change < tty->rupper)) {
1270: if (tty_term_has(term, TTYC_VPA)) {
1271: tty_putcode1(tty, TTYC_VPA, cy);
1272: goto out;
1273: }
1.42 nicm 1274: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1275: tty_putcode1(tty, TTYC_CUU, change);
1276: goto out;
1277: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1278: tty_putcode1(tty, TTYC_CUD, -change);
1279: goto out;
1280: }
1281: }
1282:
1.43 nicm 1283: absolute:
1.42 nicm 1284: /* Absolute movement. */
1285: tty_putcode2(tty, TTYC_CUP, cy, cx);
1286:
1287: out:
1288: tty->cx = cx;
1289: tty->cy = cy;
1.1 nicm 1290: }
1291:
1292: void
1293: tty_attributes(struct tty *tty, const struct grid_cell *gc)
1294: {
1.63 nicm 1295: struct grid_cell *tc = &tty->cell, gc2;
1.85 nicm 1296: u_char changed;
1.61 nicm 1297:
1.85 nicm 1298: memcpy(&gc2, gc, sizeof gc2);
1.63 nicm 1299:
1.18 nicm 1300: /*
1301: * If no setab, try to use the reverse attribute as a best-effort for a
1302: * non-default background. This is a bit of a hack but it doesn't do
1303: * any serious harm and makes a couple of applications happier.
1304: */
1305: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 1306: if (gc2.attr & GRID_ATTR_REVERSE) {
1307: if (gc2.fg != 7 && gc2.fg != 8)
1.64 nicm 1308: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 1309: } else {
1.85 nicm 1310: if (gc2.bg != 0 && gc2.bg != 8)
1.64 nicm 1311: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 1312: }
1313: }
1.1 nicm 1314:
1.85 nicm 1315: /* Fix up the colours if necessary. */
1316: tty_check_fg(tty, &gc2);
1317: tty_check_bg(tty, &gc2);
1318:
1.64 nicm 1319: /* If any bits are being cleared, reset everything. */
1.85 nicm 1320: if (tc->attr & ~gc2.attr)
1.64 nicm 1321: tty_reset(tty);
1322:
1323: /*
1324: * Set the colours. This may call tty_reset() (so it comes next) and
1325: * may add to (NOT remove) the desired attributes by changing new_attr.
1326: */
1.85 nicm 1327: tty_colours(tty, &gc2);
1.64 nicm 1328:
1.1 nicm 1329: /* Filter out attribute bits already set. */
1.85 nicm 1330: changed = gc2.attr & ~tc->attr;
1331: tc->attr = gc2.attr;
1.1 nicm 1332:
1333: /* Set the attributes. */
1334: if (changed & GRID_ATTR_BRIGHT)
1335: tty_putcode(tty, TTYC_BOLD);
1336: if (changed & GRID_ATTR_DIM)
1337: tty_putcode(tty, TTYC_DIM);
1338: if (changed & GRID_ATTR_ITALICS)
1.104 nicm 1339: {
1340: if (tty_term_has(tty->term, TTYC_SITM))
1341: tty_putcode(tty, TTYC_SITM);
1342: else
1343: tty_putcode(tty, TTYC_SMSO);
1344: }
1.1 nicm 1345: if (changed & GRID_ATTR_UNDERSCORE)
1346: tty_putcode(tty, TTYC_SMUL);
1347: if (changed & GRID_ATTR_BLINK)
1348: tty_putcode(tty, TTYC_BLINK);
1349: if (changed & GRID_ATTR_REVERSE) {
1350: if (tty_term_has(tty->term, TTYC_REV))
1351: tty_putcode(tty, TTYC_REV);
1352: else if (tty_term_has(tty->term, TTYC_SMSO))
1353: tty_putcode(tty, TTYC_SMSO);
1354: }
1355: if (changed & GRID_ATTR_HIDDEN)
1356: tty_putcode(tty, TTYC_INVIS);
1.90 nicm 1357: if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1358: tty_putcode(tty, TTYC_SMACS);
1359: }
1360:
1.61 nicm 1361: void
1.85 nicm 1362: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1363: {
1.61 nicm 1364: struct grid_cell *tc = &tty->cell;
1.62 nicm 1365: u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
1366: int have_ax, fg_default, bg_default;
1.61 nicm 1367:
1368: /* No changes? Nothing is necessary. */
1.62 nicm 1369: if (fg == tc->fg && bg == tc->bg &&
1370: ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1.63 nicm 1371: return;
1.1 nicm 1372:
1.61 nicm 1373: /*
1374: * Is either the default colour? This is handled specially because the
1375: * best solution might be to reset both colours to default, in which
1376: * case if only one is default need to fall onward to set the other
1377: * colour.
1378: */
1.62 nicm 1379: fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
1380: bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1.61 nicm 1381: if (fg_default || bg_default) {
1382: /*
1383: * If don't have AX but do have op, send sgr0 (op can't
1384: * actually be used because it is sometimes the same as sgr0
1385: * and sometimes isn't). This resets both colours to default.
1386: *
1387: * Otherwise, try to set the default colour only as needed.
1388: */
1389: have_ax = tty_term_has(tty->term, TTYC_AX);
1390: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1391: tty_reset(tty);
1392: else {
1393: if (fg_default &&
1.81 nicm 1394: (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1.61 nicm 1395: if (have_ax)
1396: tty_puts(tty, "\033[39m");
1.81 nicm 1397: else if (tc->fg != 7 ||
1398: tc->flags & GRID_FLAG_FG256)
1.61 nicm 1399: tty_putcode1(tty, TTYC_SETAF, 7);
1400: tc->fg = 8;
1401: tc->flags &= ~GRID_FLAG_FG256;
1402: }
1403: if (bg_default &&
1.81 nicm 1404: (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1.77 nicm 1405: if (have_ax)
1.61 nicm 1406: tty_puts(tty, "\033[49m");
1.81 nicm 1407: else if (tc->bg != 0 ||
1408: tc->flags & GRID_FLAG_BG256)
1.61 nicm 1409: tty_putcode1(tty, TTYC_SETAB, 0);
1410: tc->bg = 8;
1411: tc->flags &= ~GRID_FLAG_BG256;
1412: }
1413: }
1414: }
1.1 nicm 1415:
1.61 nicm 1416: /* Set the foreground colour. */
1417: if (!fg_default && (fg != tc->fg ||
1.62 nicm 1418: ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1.85 nicm 1419: tty_colours_fg(tty, gc);
1.1 nicm 1420:
1.61 nicm 1421: /*
1422: * Set the background colour. This must come after the foreground as
1423: * tty_colour_fg() can call tty_reset().
1424: */
1425: if (!bg_default && (bg != tc->bg ||
1.62 nicm 1426: ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1.73 nicm 1427: tty_colours_bg(tty, gc);
1.1 nicm 1428: }
1429:
1430: void
1.85 nicm 1431: tty_check_fg(struct tty *tty, struct grid_cell *gc)
1432: {
1433: u_int colours;
1434:
1435: /* Is this a 256-colour colour? */
1436: if (gc->flags & GRID_FLAG_FG256) {
1437: /* And not a 256 colour mode? */
1438: if (!(tty->term->flags & TERM_88COLOURS) &&
1439: !(tty->term_flags & TERM_88COLOURS) &&
1440: !(tty->term->flags & TERM_256COLOURS) &&
1441: !(tty->term_flags & TERM_256COLOURS)) {
1442: gc->fg = colour_256to16(gc->fg);
1443: if (gc->fg & 8) {
1444: gc->fg &= 7;
1445: gc->attr |= GRID_ATTR_BRIGHT;
1446: } else
1447: gc->attr &= ~GRID_ATTR_BRIGHT;
1448: gc->flags &= ~GRID_FLAG_FG256;
1449: }
1450: return;
1451: }
1452:
1453: /* Is this an aixterm colour? */
1454: colours = tty_term_number(tty->term, TTYC_COLORS);
1455: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1456: gc->fg -= 90;
1457: gc->attr |= GRID_ATTR_BRIGHT;
1458: }
1459: }
1460:
1461: void
1462: tty_check_bg(struct tty *tty, struct grid_cell *gc)
1463: {
1464: u_int colours;
1465:
1466: /* Is this a 256-colour colour? */
1467: if (gc->flags & GRID_FLAG_BG256) {
1468: /*
1469: * And not a 256 colour mode? Translate to 16-colour
1470: * palette. Bold background doesn't exist portably, so just
1471: * discard the bold bit if set.
1472: */
1473: if (!(tty->term->flags & TERM_88COLOURS) &&
1474: !(tty->term_flags & TERM_88COLOURS) &&
1475: !(tty->term->flags & TERM_256COLOURS) &&
1476: !(tty->term_flags & TERM_256COLOURS)) {
1477: gc->bg = colour_256to16(gc->bg);
1478: if (gc->bg & 8)
1479: gc->bg &= 7;
1480: gc->attr &= ~GRID_ATTR_BRIGHT;
1481: gc->flags &= ~GRID_FLAG_BG256;
1482: }
1483: return;
1484: }
1485:
1486: /* Is this an aixterm colour? */
1487: colours = tty_term_number(tty->term, TTYC_COLORS);
1.112 nicm 1488: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16) {
1.85 nicm 1489: gc->bg -= 90;
1490: gc->attr |= GRID_ATTR_BRIGHT;
1491: }
1492: }
1493:
1494: void
1495: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1496: {
1.61 nicm 1497: struct grid_cell *tc = &tty->cell;
1498: u_char fg = gc->fg;
1.79 nicm 1499: char s[32];
1.1 nicm 1500:
1.61 nicm 1501: /* Is this a 256-colour colour? */
1.1 nicm 1502: if (gc->flags & GRID_FLAG_FG256) {
1.61 nicm 1503: /* Try as 256 colours or translating to 88. */
1.1 nicm 1504: if (tty_try_256(tty, fg, "38") == 0)
1.61 nicm 1505: goto save_fg;
1.1 nicm 1506: if (tty_try_88(tty, fg, "38") == 0)
1.61 nicm 1507: goto save_fg;
1.85 nicm 1508: /* Else already handled by tty_check_fg. */
1509: return;
1.1 nicm 1510: }
1511:
1.79 nicm 1512: /* Is this an aixterm bright colour? */
1513: if (fg >= 90 && fg <= 97) {
1.85 nicm 1514: xsnprintf(s, sizeof s, "\033[%dm", fg);
1515: tty_puts(tty, s);
1516: goto save_fg;
1.79 nicm 1517: }
1518:
1.61 nicm 1519: /* Otherwise set the foreground colour. */
1520: tty_putcode1(tty, TTYC_SETAF, fg);
1521:
1.77 nicm 1522: save_fg:
1.61 nicm 1523: /* Save the new values in the terminal current cell. */
1524: tc->fg = fg;
1525: tc->flags &= ~GRID_FLAG_FG256;
1526: tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1 nicm 1527: }
1528:
1529: void
1.73 nicm 1530: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1531: {
1.61 nicm 1532: struct grid_cell *tc = &tty->cell;
1533: u_char bg = gc->bg;
1.79 nicm 1534: char s[32];
1.1 nicm 1535:
1.61 nicm 1536: /* Is this a 256-colour colour? */
1.1 nicm 1537: if (gc->flags & GRID_FLAG_BG256) {
1.61 nicm 1538: /* Try as 256 colours or translating to 88. */
1.1 nicm 1539: if (tty_try_256(tty, bg, "48") == 0)
1.61 nicm 1540: goto save_bg;
1.1 nicm 1541: if (tty_try_88(tty, bg, "48") == 0)
1.61 nicm 1542: goto save_bg;
1.85 nicm 1543: /* Else already handled by tty_check_bg. */
1544: return;
1.79 nicm 1545: }
1546:
1547: /* Is this an aixterm bright colour? */
1.112 nicm 1548: if (bg >= 90 && bg <= 97) {
1.79 nicm 1549: /* 16 colour terminals or above only. */
1550: if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
1.112 nicm 1551: xsnprintf(s, sizeof s, "\033[%dm", bg + 10);
1.79 nicm 1552: tty_puts(tty, s);
1553: goto save_bg;
1554: }
1.112 nicm 1555: bg -= 90;
1.79 nicm 1556: /* no such thing as a bold background */
1.1 nicm 1557: }
1558:
1.61 nicm 1559: /* Otherwise set the background colour. */
1560: tty_putcode1(tty, TTYC_SETAB, bg);
1561:
1562: save_bg:
1563: /* Save the new values in the terminal current cell. */
1564: tc->bg = bg;
1565: tc->flags &= ~GRID_FLAG_BG256;
1566: tc->flags |= gc->flags & GRID_FLAG_BG256;
1567: }
1568:
1569: int
1570: tty_try_256(struct tty *tty, u_char colour, const char *type)
1571: {
1572: char s[32];
1573:
1574: if (!(tty->term->flags & TERM_256COLOURS) &&
1575: !(tty->term_flags & TERM_256COLOURS))
1576: return (-1);
1577:
1578: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1579: tty_puts(tty, s);
1580: return (0);
1581: }
1582:
1583: int
1584: tty_try_88(struct tty *tty, u_char colour, const char *type)
1585: {
1586: char s[32];
1587:
1588: if (!(tty->term->flags & TERM_88COLOURS) &&
1589: !(tty->term_flags & TERM_88COLOURS))
1590: return (-1);
1591: colour = colour_256to88(colour);
1592:
1593: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1594: tty_puts(tty, s);
1595: return (0);
1.110 nicm 1596: }
1597:
1598: void
1599: tty_bell(struct tty *tty)
1600: {
1601: tty_putcode(tty, TTYC_BEL);
1.1 nicm 1602: }