Annotation of src/usr.bin/tmux/tty.c, Revision 1.286
1.286 ! nicm 1: /* $OpenBSD: tty.c,v 1.285 2017/05/15 16:44:04 nicm Exp $ */
1.1 nicm 2:
3: /*
1.198 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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.212 nicm 24: #include <curses.h>
1.1 nicm 25: #include <errno.h>
26: #include <fcntl.h>
1.106 nicm 27: #include <resolv.h>
1.42 nicm 28: #include <stdlib.h>
1.1 nicm 29: #include <string.h>
30: #include <termios.h>
31: #include <unistd.h>
32:
33: #include "tmux.h"
34:
1.207 nicm 35: static int tty_log_fd = -1;
1.192 nicm 36:
1.207 nicm 37: static int tty_client_ready(struct client *, struct window_pane *);
1.201 nicm 38:
1.207 nicm 39: static void tty_set_italics(struct tty *);
40: static int tty_try_colour(struct tty *, int, const char *);
1.208 nicm 41: static void tty_force_cursor_colour(struct tty *, const char *);
42: static void tty_cursor_pane(struct tty *, const struct tty_ctx *, u_int,
43: u_int);
1.253 nicm 44: static void tty_cursor_pane_unless_wrap(struct tty *,
45: const struct tty_ctx *, u_int, u_int);
1.242 nicm 46: static void tty_invalidate(struct tty *);
1.207 nicm 47: static void tty_colours(struct tty *, const struct grid_cell *);
1.219 nicm 48: static void tty_check_fg(struct tty *, const struct window_pane *,
49: struct grid_cell *);
50: static void tty_check_bg(struct tty *, const struct window_pane *,
51: struct grid_cell *);
1.207 nicm 52: static void tty_colours_fg(struct tty *, const struct grid_cell *);
53: static void tty_colours_bg(struct tty *, const struct grid_cell *);
54:
1.208 nicm 55: static void tty_region_pane(struct tty *, const struct tty_ctx *, u_int,
56: u_int);
1.214 nicm 57: static void tty_region(struct tty *, u_int, u_int);
1.212 nicm 58: static void tty_margin_pane(struct tty *, const struct tty_ctx *);
1.214 nicm 59: static void tty_margin(struct tty *, u_int, u_int);
1.207 nicm 60: static int tty_large_region(struct tty *, const struct tty_ctx *);
1.210 nicm 61: static int tty_fake_bce(const struct tty *, const struct window_pane *,
62: u_int);
1.207 nicm 63: static void tty_redraw_region(struct tty *, const struct tty_ctx *);
64: static void tty_emulate_repeat(struct tty *, enum tty_code_code,
65: enum tty_code_code, u_int);
66: static void tty_repeat_space(struct tty *, u_int);
67: static void tty_cell(struct tty *, const struct grid_cell *,
68: const struct window_pane *);
69: static void tty_default_colours(struct grid_cell *,
70: const struct window_pane *);
1.210 nicm 71: static void tty_default_attributes(struct tty *, const struct window_pane *,
72: u_int);
1.1 nicm 73:
1.212 nicm 74: #define tty_use_margin(tty) \
1.265 nicm 75: ((tty)->term_type == TTY_VT420)
1.90 nicm 76:
1.264 nicm 77: #define tty_pane_full_width(tty, ctx) \
1.132 nicm 78: ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
79:
1.266 nicm 80: #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
81: #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
82: #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
83:
1.192 nicm 84: void
85: tty_create_log(void)
86: {
87: char name[64];
88:
89: xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
90:
91: tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
92: if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
93: fatal("fcntl failed");
94: }
95:
1.185 nicm 96: int
1.136 nicm 97: tty_init(struct tty *tty, struct client *c, int fd, char *term)
1.1 nicm 98: {
1.185 nicm 99: if (!isatty(fd))
100: return (-1);
101:
1.30 nicm 102: memset(tty, 0, sizeof *tty);
1.22 nicm 103:
1.9 nicm 104: if (term == NULL || *term == '\0')
1.220 nicm 105: tty->term_name = xstrdup("unknown");
1.1 nicm 106: else
1.220 nicm 107: tty->term_name = xstrdup(term);
1.258 nicm 108:
1.30 nicm 109: tty->fd = fd;
1.136 nicm 110: tty->client = c;
1.30 nicm 111:
1.108 nicm 112: tty->cstyle = 0;
1.107 nicm 113: tty->ccolour = xstrdup("");
1.30 nicm 114:
1.1 nicm 115: tty->flags = 0;
1.212 nicm 116:
1.1 nicm 117: tty->term_flags = 0;
1.212 nicm 118: tty->term_type = TTY_UNKNOWN;
1.185 nicm 119:
120: return (0);
1.31 nicm 121: }
122:
1.88 nicm 123: int
1.31 nicm 124: tty_resize(struct tty *tty)
125: {
1.258 nicm 126: struct client *c = tty->client;
127: struct winsize ws;
128: u_int sx, sy;
1.31 nicm 129:
130: if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88 nicm 131: sx = ws.ws_col;
132: if (sx == 0)
133: sx = 80;
134: sy = ws.ws_row;
135: if (sy == 0)
136: sy = 24;
137: } else {
138: sx = 80;
139: sy = 24;
1.31 nicm 140: }
1.258 nicm 141: log_debug("%s: %s now %ux%u", __func__, c->name, sx, sy);
142:
1.114 nicm 143: if (!tty_set_size(tty, sx, sy))
1.88 nicm 144: return (0);
1.242 nicm 145: tty_invalidate(tty);
1.114 nicm 146: return (1);
147: }
148:
149: int
1.212 nicm 150: tty_set_size(struct tty *tty, u_int sx, u_int sy)
151: {
1.114 nicm 152: if (sx == tty->sx && sy == tty->sy)
153: return (0);
154: tty->sx = sx;
155: tty->sy = sy;
1.88 nicm 156: return (1);
1.1 nicm 157: }
158:
1.244 nicm 159: static void
160: tty_read_callback(__unused int fd, __unused short events, void *data)
161: {
162: struct tty *tty = data;
1.258 nicm 163: struct client *c = tty->client;
1.244 nicm 164: size_t size = EVBUFFER_LENGTH(tty->in);
165: int nread;
166:
167: nread = evbuffer_read(tty->in, tty->fd, -1);
168: if (nread == -1)
169: return;
1.258 nicm 170: log_debug("%s: read %d bytes (already %zu)", c->name, nread, size);
1.244 nicm 171:
172: while (tty_keys_next(tty))
173: ;
174: }
175:
176: static void
1.266 nicm 177: tty_timer_callback(__unused int fd, __unused short events, void *data)
178: {
179: struct tty *tty = data;
180: struct client *c = tty->client;
181: struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
182:
183: log_debug("%s: %zu discarded", c->name, tty->discarded);
184:
185: c->flags |= CLIENT_REDRAW;
186: c->discarded += tty->discarded;
187:
188: if (tty->discarded < TTY_BLOCK_STOP(tty)) {
189: tty->flags &= ~TTY_BLOCK;
190: tty_invalidate(tty);
191: return;
192: }
193: tty->discarded = 0;
194: evtimer_add(&tty->timer, &tv);
195: }
196:
197: static int
198: tty_block_maybe(struct tty *tty)
199: {
200: struct client *c = tty->client;
201: size_t size = EVBUFFER_LENGTH(tty->out);
202: struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
203:
204: if (size < TTY_BLOCK_START(tty))
205: return (0);
206:
207: if (tty->flags & TTY_BLOCK)
208: return (1);
209: tty->flags |= TTY_BLOCK;
210:
211: log_debug("%s: can't keep up, %zu discarded", c->name, size);
212:
213: evbuffer_drain(tty->out, size);
214: c->discarded += size;
215:
216: tty->discarded = 0;
217: evtimer_add(&tty->timer, &tv);
218: return (1);
219: }
220:
221: static void
1.244 nicm 222: tty_write_callback(__unused int fd, __unused short events, void *data)
223: {
1.246 nicm 224: struct tty *tty = data;
1.258 nicm 225: struct client *c = tty->client;
1.246 nicm 226: size_t size = EVBUFFER_LENGTH(tty->out);
227: int nwrite;
1.244 nicm 228:
229: nwrite = evbuffer_write(tty->out, tty->fd);
230: if (nwrite == -1)
231: return;
1.258 nicm 232: log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size);
1.244 nicm 233:
1.270 nicm 234: if (c->redraw > 0) {
235: if ((size_t)nwrite >= c->redraw)
236: c->redraw = 0;
237: else
238: c->redraw -= nwrite;
239: log_debug("%s: waiting for redraw, %zu bytes left", c->name,
240: c->redraw);
241: } else if (tty_block_maybe(tty))
1.266 nicm 242: return;
243:
1.247 nicm 244: if (EVBUFFER_LENGTH(tty->out) != 0)
245: event_add(&tty->event_out, NULL);
1.244 nicm 246: }
247:
1.1 nicm 248: int
1.166 nicm 249: tty_open(struct tty *tty, char **cause)
1.1 nicm 250: {
1.220 nicm 251: tty->term = tty_term_find(tty->term_name, tty->fd, cause);
1.21 nicm 252: if (tty->term == NULL) {
253: tty_close(tty);
254: return (-1);
255: }
256: tty->flags |= TTY_OPENED;
1.1 nicm 257:
1.266 nicm 258: tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
1.244 nicm 259:
260: event_set(&tty->event_in, tty->fd, EV_PERSIST|EV_READ,
261: tty_read_callback, tty);
262: tty->in = evbuffer_new();
1.1 nicm 263:
1.244 nicm 264: event_set(&tty->event_out, tty->fd, EV_WRITE, tty_write_callback, tty);
265: tty->out = evbuffer_new();
1.1 nicm 266:
1.266 nicm 267: evtimer_set(&tty->timer, tty_timer_callback, tty);
268:
1.1 nicm 269: tty_start_tty(tty);
270:
1.148 nicm 271: tty_keys_build(tty);
1.1 nicm 272:
273: return (0);
274: }
275:
1.244 nicm 276: void
277: tty_start_tty(struct tty *tty)
1.1 nicm 278: {
1.285 nicm 279: struct client *c = tty->client;
280: struct termios tio;
1.33 nicm 281:
1.244 nicm 282: if (tty->fd != -1 && tcgetattr(tty->fd, &tty->tio) == 0) {
283: setblocking(tty->fd, 0);
284: event_add(&tty->event_in, NULL);
285:
286: memcpy(&tio, &tty->tio, sizeof tio);
287: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
288: tio.c_iflag |= IGNBRK;
289: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
290: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
291: ECHOPRT|ECHOKE|ISIG);
292: tio.c_cc[VMIN] = 1;
293: tio.c_cc[VTIME] = 0;
294: if (tcsetattr(tty->fd, TCSANOW, &tio) == 0)
295: tcflush(tty->fd, TCIOFLUSH);
296: }
1.1 nicm 297:
1.10 nicm 298: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 299:
1.252 nicm 300: tty_putcode(tty, TTYC_SMKX);
1.285 nicm 301: tty_putcode(tty, TTYC_CLEAR);
302:
303: if (tty_acs_needed(tty)) {
304: log_debug("%s: using capabilities for ACS", c->name);
1.90 nicm 305: tty_putcode(tty, TTYC_ENACS);
1.285 nicm 306: } else
307: log_debug("%s: using UTF-8 for ACS", c->name);
1.1 nicm 308:
309: tty_putcode(tty, TTYC_CNORM);
310: if (tty_term_has(tty->term, TTYC_KMOUS))
1.179 nicm 311: tty_puts(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l");
1.121 nicm 312:
1.189 nicm 313: if (tty_term_flag(tty->term, TTYC_XT)) {
1.191 nicm 314: if (options_get_number(global_options, "focus-events")) {
1.162 nicm 315: tty->flags |= TTY_FOCUS;
316: tty_puts(tty, "\033[?1004h");
317: }
1.265 nicm 318: tty_puts(tty, "\033[c");
1.162 nicm 319: }
1.1 nicm 320:
1.20 nicm 321: tty->flags |= TTY_STARTED;
1.242 nicm 322: tty_invalidate(tty);
1.107 nicm 323:
324: tty_force_cursor_colour(tty, "");
1.177 nicm 325:
326: tty->mouse_drag_flag = 0;
327: tty->mouse_drag_update = NULL;
328: tty->mouse_drag_release = NULL;
1.128 nicm 329: }
330:
331: void
1.1 nicm 332: tty_stop_tty(struct tty *tty)
333: {
334: struct winsize ws;
335:
1.20 nicm 336: if (!(tty->flags & TTY_STARTED))
337: return;
338: tty->flags &= ~TTY_STARTED;
339:
1.266 nicm 340: event_del(&tty->timer);
341: tty->flags &= ~TTY_BLOCK;
342:
1.244 nicm 343: event_del(&tty->event_in);
344: event_del(&tty->event_out);
1.66 nicm 345:
1.1 nicm 346: /*
347: * Be flexible about error handling and try not kill the server just
348: * because the fd is invalid. Things like ssh -t can easily leave us
349: * with a dead tty.
350: */
351: if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
352: return;
353: if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
354: return;
355:
356: tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.285 nicm 357: if (tty_acs_needed(tty))
1.90 nicm 358: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1 nicm 359: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
360: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
361: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.160 nicm 362: if (tty_term_has(tty->term, TTYC_SS) && tty->cstyle != 0) {
363: if (tty_term_has(tty->term, TTYC_SE))
364: tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
1.109 nicm 365: else
1.160 nicm 366: tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
1.108 nicm 367: }
1.173 nicm 368: if (tty->mode & MODE_BRACKETPASTE)
369: tty_raw(tty, "\033[?2004l");
1.107 nicm 370: tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1 nicm 371:
372: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
373: if (tty_term_has(tty->term, TTYC_KMOUS))
1.179 nicm 374: tty_raw(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l");
1.151 nicm 375:
1.189 nicm 376: if (tty_term_flag(tty->term, TTYC_XT)) {
1.162 nicm 377: if (tty->flags & TTY_FOCUS) {
378: tty->flags &= ~TTY_FOCUS;
1.172 nicm 379: tty_raw(tty, "\033[?1004l");
1.162 nicm 380: }
381: }
1.10 nicm 382:
1.212 nicm 383: if (tty_use_margin(tty))
384: tty_raw(tty, "\033[?69l"); /* DECLRMM */
1.10 nicm 385: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150 nicm 386:
387: setblocking(tty->fd, 1);
1.1 nicm 388: }
389:
390: void
1.20 nicm 391: tty_close(struct tty *tty)
1.1 nicm 392: {
1.123 nicm 393: if (event_initialized(&tty->key_timer))
394: evtimer_del(&tty->key_timer);
1.20 nicm 395: tty_stop_tty(tty);
1.1 nicm 396:
1.21 nicm 397: if (tty->flags & TTY_OPENED) {
1.244 nicm 398: evbuffer_free(tty->in);
399: event_del(&tty->event_in);
400: evbuffer_free(tty->out);
401: event_del(&tty->event_out);
1.66 nicm 402:
1.21 nicm 403: tty_term_free(tty->term);
404: tty_keys_free(tty);
405:
406: tty->flags &= ~TTY_OPENED;
407: }
1.1 nicm 408:
1.21 nicm 409: if (tty->fd != -1) {
410: close(tty->fd);
411: tty->fd = -1;
412: }
1.1 nicm 413: }
414:
415: void
1.20 nicm 416: tty_free(struct tty *tty)
1.1 nicm 417: {
1.20 nicm 418: tty_close(tty);
1.1 nicm 419:
1.138 nicm 420: free(tty->ccolour);
1.220 nicm 421: free(tty->term_name);
1.1 nicm 422: }
423:
424: void
1.212 nicm 425: tty_set_type(struct tty *tty, int type)
426: {
427: tty->term_type = type;
428:
429: if (tty_use_margin(tty))
430: tty_puts(tty, "\033[?69h"); /* DECLRMM */
431: }
432:
433: void
1.1 nicm 434: tty_raw(struct tty *tty, const char *s)
435: {
1.150 nicm 436: ssize_t n, slen;
437: u_int i;
438:
439: slen = strlen(s);
440: for (i = 0; i < 5; i++) {
441: n = write(tty->fd, s, slen);
442: if (n >= 0) {
443: s += n;
444: slen -= n;
445: if (slen == 0)
446: break;
447: } else if (n == -1 && errno != EAGAIN)
448: break;
449: usleep(100);
450: }
1.1 nicm 451: }
452:
453: void
454: tty_putcode(struct tty *tty, enum tty_code_code code)
455: {
456: tty_puts(tty, tty_term_string(tty->term, code));
457: }
458:
459: void
460: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
461: {
462: if (a < 0)
463: return;
464: tty_puts(tty, tty_term_string1(tty->term, code, a));
465: }
466:
467: void
468: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
469: {
470: if (a < 0 || b < 0)
471: return;
472: tty_puts(tty, tty_term_string2(tty->term, code, a, b));
473: }
474:
475: void
1.286 ! nicm 476: tty_putcode3(struct tty *tty, enum tty_code_code code, int a, int b, int c)
! 477: {
! 478: if (a < 0 || b < 0 || c < 0)
! 479: return;
! 480: tty_puts(tty, tty_term_string3(tty->term, code, a, b, c));
! 481: }
! 482:
! 483: void
1.107 nicm 484: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
485: {
486: if (a != NULL)
487: tty_puts(tty, tty_term_ptr1(tty->term, code, a));
488: }
489:
490: void
1.167 nicm 491: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
492: const void *b)
1.106 nicm 493: {
494: if (a != NULL && b != NULL)
495: tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
496: }
497:
1.243 nicm 498: static void
499: tty_add(struct tty *tty, const char *buf, size_t len)
500: {
1.258 nicm 501: struct client *c = tty->client;
502:
1.266 nicm 503: if (tty->flags & TTY_BLOCK) {
504: tty->discarded += len;
505: return;
506: }
507:
1.244 nicm 508: evbuffer_add(tty->out, buf, len);
1.270 nicm 509: log_debug("%s: %.*s", c->name, (int)len, buf);
1.266 nicm 510: c->written += len;
1.243 nicm 511:
512: if (tty_log_fd != -1)
513: write(tty_log_fd, buf, len);
1.244 nicm 514: if (tty->flags & TTY_STARTED)
515: event_add(&tty->event_out, NULL);
1.243 nicm 516: }
517:
1.106 nicm 518: void
1.1 nicm 519: tty_puts(struct tty *tty, const char *s)
520: {
1.243 nicm 521: if (*s != '\0')
522: tty_add(tty, s, strlen(s));
1.1 nicm 523: }
524:
525: void
526: tty_putc(struct tty *tty, u_char ch)
527: {
1.90 nicm 528: const char *acs;
1.1 nicm 529:
1.90 nicm 530: if (tty->cell.attr & GRID_ATTR_CHARSET) {
531: acs = tty_acs_get(tty, ch);
1.243 nicm 532: if (acs != NULL)
533: tty_add(tty, acs, strlen(acs));
534: else
535: tty_add(tty, &ch, 1);
536: } else
537: tty_add(tty, &ch, 1);
1.1 nicm 538:
539: if (ch >= 0x20 && ch != 0x7f) {
1.211 nicm 540: if (tty->cx >= tty->sx) {
1.46 nicm 541: tty->cx = 1;
542: if (tty->cy != tty->rlower)
543: tty->cy++;
1.211 nicm 544:
545: /*
546: * On !xenl terminals, force the cursor position to
547: * where we think it should be after a line wrap - this
548: * means it works on sensible terminals as well.
549: */
550: if (tty->term->flags & TERM_EARLYWRAP)
551: tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
1.1 nicm 552: } else
553: tty->cx++;
554: }
555: }
556:
557: void
1.147 nicm 558: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5 nicm 559: {
1.243 nicm 560: tty_add(tty, buf, len);
1.268 nicm 561: if (tty->cx + width > tty->sx) {
562: tty->cx = (tty->cx + width) - tty->sx;
563: if (tty->cx <= tty->sx)
564: tty->cy++;
565: else
566: tty->cx = tty->cy = UINT_MAX;
567: } else
1.254 nicm 568: tty->cx += width;
1.5 nicm 569: }
570:
1.207 nicm 571: static void
1.180 nicm 572: tty_set_italics(struct tty *tty)
573: {
574: const char *s;
575:
576: if (tty_term_has(tty->term, TTYC_SITM)) {
1.191 nicm 577: s = options_get_string(global_options, "default-terminal");
1.180 nicm 578: if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
579: tty_putcode(tty, TTYC_SITM);
580: return;
581: }
582: }
583: tty_putcode(tty, TTYC_SMSO);
584: }
585:
586: void
1.1 nicm 587: tty_set_title(struct tty *tty, const char *title)
588: {
1.105 nicm 589: if (!tty_term_has(tty->term, TTYC_TSL) ||
590: !tty_term_has(tty->term, TTYC_FSL))
1.1 nicm 591: return;
592:
1.105 nicm 593: tty_putcode(tty, TTYC_TSL);
1.1 nicm 594: tty_puts(tty, title);
1.105 nicm 595: tty_putcode(tty, TTYC_FSL);
1.1 nicm 596: }
597:
1.208 nicm 598: static void
1.107 nicm 599: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
600: {
601: if (*ccolour == '\0')
602: tty_putcode(tty, TTYC_CR);
603: else
1.160 nicm 604: tty_putcode_ptr1(tty, TTYC_CS, ccolour);
1.138 nicm 605: free(tty->ccolour);
1.107 nicm 606: tty->ccolour = xstrdup(ccolour);
607: }
608:
609: void
610: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1 nicm 611: {
612: int changed;
613:
1.197 nicm 614: if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0)
1.107 nicm 615: tty_force_cursor_colour(tty, s->ccolour);
616:
1.1 nicm 617: if (tty->flags & TTY_NOCURSOR)
618: mode &= ~MODE_CURSOR;
619:
620: changed = mode ^ tty->mode;
1.183 nicm 621: if (changed & MODE_BLINKING) {
622: if (tty_term_has(tty->term, TTYC_CVVIS))
623: tty_putcode(tty, TTYC_CVVIS);
624: else
625: tty_putcode(tty, TTYC_CNORM);
626: changed |= MODE_CURSOR;
627: }
628: if (changed & MODE_CURSOR) {
629: if (mode & MODE_CURSOR)
630: tty_putcode(tty, TTYC_CNORM);
631: else
1.1 nicm 632: tty_putcode(tty, TTYC_CIVIS);
1.108 nicm 633: }
1.181 nicm 634: if (s != NULL && tty->cstyle != s->cstyle) {
1.160 nicm 635: if (tty_term_has(tty->term, TTYC_SS)) {
1.108 nicm 636: if (s->cstyle == 0 &&
1.160 nicm 637: tty_term_has(tty->term, TTYC_SE))
638: tty_putcode(tty, TTYC_SE);
1.108 nicm 639: else
1.160 nicm 640: tty_putcode1(tty, TTYC_SS, s->cstyle);
1.108 nicm 641: }
642: tty->cstyle = s->cstyle;
1.1 nicm 643: }
1.195 nicm 644: if (changed & ALL_MOUSE_MODES) {
1.94 nicm 645: if (mode & ALL_MOUSE_MODES) {
1.153 nicm 646: /*
647: * Enable the SGR (1006) extension unconditionally, as
1.221 nicm 648: * it is safe from misinterpretation.
1.153 nicm 649: */
650: tty_puts(tty, "\033[?1006h");
1.225 nicm 651: if (mode & MODE_MOUSE_ALL)
652: tty_puts(tty, "\033[?1003h");
653: else if (mode & MODE_MOUSE_BUTTON)
1.94 nicm 654: tty_puts(tty, "\033[?1002h");
1.98 nicm 655: else if (mode & MODE_MOUSE_STANDARD)
656: tty_puts(tty, "\033[?1000h");
1.86 nicm 657: } else {
1.225 nicm 658: if (tty->mode & MODE_MOUSE_ALL)
659: tty_puts(tty, "\033[?1003l");
660: else if (tty->mode & MODE_MOUSE_BUTTON)
1.94 nicm 661: tty_puts(tty, "\033[?1002l");
1.98 nicm 662: else if (tty->mode & MODE_MOUSE_STANDARD)
663: tty_puts(tty, "\033[?1000l");
1.153 nicm 664: tty_puts(tty, "\033[?1006l");
1.86 nicm 665: }
1.115 nicm 666: }
667: if (changed & MODE_BRACKETPASTE) {
668: if (mode & MODE_BRACKETPASTE)
669: tty_puts(tty, "\033[?2004h");
670: else
671: tty_puts(tty, "\033[?2004l");
1.1 nicm 672: }
673: tty->mode = mode;
674: }
675:
1.207 nicm 676: static void
1.168 nicm 677: tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
678: enum tty_code_code code1, u_int n)
1.1 nicm 679: {
680: if (tty_term_has(tty->term, code))
681: tty_putcode1(tty, code, n);
682: else {
683: while (n-- > 0)
684: tty_putcode(tty, code1);
685: }
686: }
687:
1.207 nicm 688: static void
1.133 nicm 689: tty_repeat_space(struct tty *tty, u_int n)
690: {
1.257 nicm 691: static char s[500];
692:
693: if (*s != ' ')
694: memset(s, ' ', sizeof s);
695:
696: while (n > sizeof s) {
697: tty_putn(tty, s, sizeof s, sizeof s);
698: n -= sizeof s;
699: }
700: if (n != 0)
701: tty_putn(tty, s, n, n);
1.133 nicm 702: }
703:
1.1 nicm 704: /*
1.119 nicm 705: * Is the region large enough to be worth redrawing once later rather than
706: * probably several times now? Currently yes if it is more than 50% of the
707: * pane.
708: */
1.207 nicm 709: static int
1.194 nicm 710: tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
1.119 nicm 711: {
712: struct window_pane *wp = ctx->wp;
713:
714: return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
715: }
716:
717: /*
1.176 nicm 718: * Return if BCE is needed but the terminal doesn't have it - it'll need to be
719: * emulated.
720: */
1.207 nicm 721: static int
1.210 nicm 722: tty_fake_bce(const struct tty *tty, const struct window_pane *wp, u_int bg)
1.176 nicm 723: {
724: struct grid_cell gc;
725:
1.210 nicm 726: if (tty_term_flag(tty->term, TTYC_BCE))
727: return (0);
728:
1.176 nicm 729: memcpy(&gc, &grid_default_cell, sizeof gc);
1.203 nicm 730: if (wp != NULL)
731: tty_default_colours(&gc, wp);
1.176 nicm 732:
1.210 nicm 733: if (bg != 8 || gc.bg != 8)
734: return (1);
735: return (0);
1.176 nicm 736: }
737:
738: /*
1.1 nicm 739: * Redraw scroll region using data from screen (already updated). Used when
740: * CSR not supported, or window is a pane that doesn't take up the full
741: * width of the terminal.
742: */
1.207 nicm 743: static void
1.24 nicm 744: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 745: {
1.14 nicm 746: struct window_pane *wp = ctx->wp;
747: struct screen *s = wp->screen;
1.204 nicm 748: u_int i;
1.1 nicm 749:
750: /*
1.119 nicm 751: * If region is large, schedule a window redraw. In most cases this is
752: * likely to be followed by some more scrolling.
1.1 nicm 753: */
1.119 nicm 754: if (tty_large_region(tty, ctx)) {
1.1 nicm 755: wp->flags |= PANE_REDRAW;
756: return;
757: }
758:
1.14 nicm 759: if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
760: for (i = ctx->ocy; i < screen_size_y(s); i++)
1.176 nicm 761: tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff);
1.1 nicm 762: } else {
1.14 nicm 763: for (i = ctx->orupper; i <= ctx->orlower; i++)
1.176 nicm 764: tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff);
1.1 nicm 765: }
766: }
767:
1.271 nicm 768: static void
769: tty_clear_line(struct tty *tty, const struct window_pane *wp, u_int py,
770: u_int px, u_int nx, u_int bg)
771: {
772: log_debug("%s: %u at %u,%u", __func__, nx, px, py);
773:
774: /* Nothing to clear. */
775: if (nx == 0)
776: return;
777:
778: /* If genuine BCE is available, can try escape sequences. */
779: if (!tty_fake_bce(tty, wp, bg)) {
780: /* Off the end of the line, use EL if available. */
781: if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) {
782: tty_cursor(tty, px, py);
783: tty_putcode(tty, TTYC_EL);
784: return;
785: }
786:
787: /* At the start of the line. Use EL1. */
788: if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) {
789: tty_cursor(tty, px + nx - 1, py);
790: tty_putcode(tty, TTYC_EL1);
791: return;
792: }
793:
794: /* Section of line. Use ECH if possible. */
795: if (tty_term_has(tty->term, TTYC_ECH)) {
796: tty_cursor(tty, px, py);
797: tty_putcode1(tty, TTYC_ECH, nx);
798: return;
799: }
800: }
801:
802: /* Couldn't use an escape sequence, use spaces. */
1.272 nicm 803: tty_cursor(tty, px, py);
1.271 nicm 804: tty_repeat_space(tty, nx);
805: }
806:
807: static void
808: tty_clear_area(struct tty *tty, const struct window_pane *wp, u_int py,
809: u_int ny, u_int px, u_int nx, u_int bg)
810: {
811: u_int yy;
1.274 nicm 812: char tmp[64];
1.271 nicm 813:
814: log_debug("%s: %u,%u at %u,%u", __func__, nx, ny, px, py);
815:
816: /* Nothing to clear. */
817: if (nx == 0 || ny == 0)
818: return;
819:
820: /* If genuine BCE is available, can try escape sequences. */
821: if (!tty_fake_bce(tty, wp, bg)) {
1.276 nicm 822: /* Use ED if clearing off the bottom of the terminal. */
1.271 nicm 823: if (px == 0 &&
824: px + nx >= tty->sx &&
825: py + ny >= tty->sy &&
826: tty_term_has(tty->term, TTYC_ED)) {
827: tty_cursor(tty, 0, py);
828: tty_putcode(tty, TTYC_ED);
1.274 nicm 829: return;
830: }
831:
832: /*
1.276 nicm 833: * On VT420 compatible terminals we can use DECFRA if the
834: * background colour isn't default (because it doesn't work
835: * after SGR 0).
1.274 nicm 836: */
837: if (tty->term_type == TTY_VT420 && bg != 8) {
838: xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x",
839: py + 1, px + 1, py + ny, px + nx);
840: tty_puts(tty, tmp);
1.276 nicm 841: return;
842: }
843:
1.280 nicm 844: /* Full lines can be scrolled away to clear them. */
845: if (px == 0 &&
1.281 nicm 846: px + nx >= tty->sx &&
1.280 nicm 847: ny > 2 &&
848: tty_term_has(tty->term, TTYC_CSR) &&
849: tty_term_has(tty->term, TTYC_INDN)) {
850: tty_region(tty, py, py + ny - 1);
851: tty_margin_off(tty);
1.284 nicm 852: tty_putcode1(tty, TTYC_INDN, ny);
1.280 nicm 853: return;
854: }
855:
1.276 nicm 856: /*
857: * If margins are supported, can just scroll the area off to
858: * clear it.
859: */
1.277 nicm 860: if (nx > 2 &&
861: ny > 2 &&
1.280 nicm 862: tty_term_has(tty->term, TTYC_CSR) &&
1.277 nicm 863: tty_use_margin(tty) &&
864: tty_term_has(tty->term, TTYC_INDN)) {
1.276 nicm 865: tty_region(tty, py, py + ny - 1);
866: tty_margin(tty, px, px + nx - 1);
1.284 nicm 867: tty_putcode1(tty, TTYC_INDN, ny);
1.271 nicm 868: return;
869: }
870: }
871:
872: /* Couldn't use an escape sequence, loop over the lines. */
873: for (yy = py; yy < py + ny; yy++)
874: tty_clear_line(tty, wp, yy, px, nx, bg);
875: }
876:
1.1 nicm 877: void
1.176 nicm 878: tty_draw_pane(struct tty *tty, const struct window_pane *wp, u_int py, u_int ox,
879: u_int oy)
880: {
881: tty_draw_line(tty, wp, wp->screen, py, ox, oy);
882: }
883:
884: void
885: tty_draw_line(struct tty *tty, const struct window_pane *wp,
886: struct screen *s, u_int py, u_int ox, u_int oy)
1.1 nicm 887: {
1.250 nicm 888: struct grid_cell gc, last;
1.271 nicm 889: u_int i, j, sx, nx, width;
1.259 nicm 890: int flags, cleared = 0;
1.250 nicm 891: char buf[512];
892: size_t len;
1.1 nicm 893:
1.259 nicm 894: flags = (tty->flags & TTY_NOCURSOR);
1.181 nicm 895: tty->flags |= TTY_NOCURSOR;
896: tty_update_mode(tty, tty->mode, s);
1.35 nicm 897:
1.214 nicm 898: tty_region_off(tty);
899: tty_margin_off(tty);
900:
1.273 nicm 901: /*
902: * Clamp the width to cellsize - note this is not cellused, because
903: * there may be empty background cells after it (from BCE).
904: */
1.1 nicm 905: sx = screen_size_x(s);
1.273 nicm 906: if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
907: sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1 nicm 908: if (sx > tty->sx)
909: sx = tty->sx;
910:
1.268 nicm 911: if (wp == NULL ||
912: py == 0 ||
913: (~s->grid->linedata[s->grid->hsize + py - 1].flags & GRID_LINE_WRAPPED) ||
914: ox != 0 ||
915: tty->cx < tty->sx ||
916: screen_size_x(s) < tty->sx) {
917: if (screen_size_x(s) < tty->sx &&
918: ox == 0 &&
919: sx != screen_size_x(s) &&
920: tty_term_has(tty->term, TTYC_EL1) &&
921: !tty_fake_bce(tty, wp, 8)) {
922: tty_default_attributes(tty, wp, 8);
923: tty_cursor(tty, screen_size_x(s) - 1, oy + py);
924: tty_putcode(tty, TTYC_EL1);
925: cleared = 1;
926: }
927: if (sx != 0)
928: tty_cursor(tty, ox, oy + py);
929: } else
930: log_debug("%s: wrapped line %u", __func__, oy + py);
1.250 nicm 931:
932: memcpy(&last, &grid_default_cell, sizeof last);
933: len = 0;
934: width = 0;
1.46 nicm 935:
1.1 nicm 936: for (i = 0; i < sx; i++) {
1.193 nicm 937: grid_view_get_cell(s->grid, i, py, &gc);
1.250 nicm 938: if (len != 0 &&
1.251 nicm 939: (((~tty->flags & TTY_UTF8) &&
940: (gc.data.size != 1 ||
941: *gc.data.data >= 0x7f ||
942: gc.data.width != 1)) ||
943: (gc.attr & GRID_ATTR_CHARSET) ||
1.250 nicm 944: gc.flags != last.flags ||
945: gc.attr != last.attr ||
946: gc.fg != last.fg ||
947: gc.bg != last.bg ||
948: (sizeof buf) - len < gc.data.size)) {
949: tty_attributes(tty, &last, wp);
950: tty_putn(tty, buf, len, width);
951:
952: len = 0;
953: width = 0;
954: }
955:
956: if (gc.flags & GRID_FLAG_SELECTED)
957: screen_select_cell(s, &last, &gc);
958: else
959: memcpy(&last, &gc, sizeof last);
1.251 nicm 960: if (((~tty->flags & TTY_UTF8) &&
961: (gc.data.size != 1 ||
962: *gc.data.data >= 0x7f ||
963: gc.data.width != 1)) ||
964: (gc.attr & GRID_ATTR_CHARSET)) {
1.250 nicm 965: tty_attributes(tty, &last, wp);
1.251 nicm 966: if (~tty->flags & TTY_UTF8) {
967: for (j = 0; j < gc.data.width; j++)
968: tty_putc(tty, '_');
969: } else {
970: for (j = 0; j < gc.data.size; j++)
971: tty_putc(tty, gc.data.data[j]);
972: }
1.250 nicm 973: } else {
974: memcpy(buf + len, gc.data.data, gc.data.size);
975: len += gc.data.size;
976: width += gc.data.width;
977: }
978: }
979: if (len != 0) {
980: tty_attributes(tty, &last, wp);
981: tty_putn(tty, buf, len, width);
1.1 nicm 982: }
983:
1.271 nicm 984: nx = screen_size_x(s) - sx;
985: if (!cleared && sx < tty->sx && nx != 0) {
1.210 nicm 986: tty_default_attributes(tty, wp, 8);
1.271 nicm 987: tty_clear_line(tty, wp, oy + py, ox + sx, nx, 8);
1.35 nicm 988: }
1.1 nicm 989:
1.181 nicm 990: tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107 nicm 991: tty_update_mode(tty, tty->mode, s);
1.15 nicm 992: }
993:
1.201 nicm 994: static int
1.182 nicm 995: tty_client_ready(struct client *c, struct window_pane *wp)
996: {
997: if (c->session == NULL || c->tty.term == NULL)
998: return (0);
1.244 nicm 999: if (c->flags & (CLIENT_REDRAW|CLIENT_SUSPENDED))
1.182 nicm 1000: return (0);
1001: if (c->tty.flags & TTY_FREEZE)
1002: return (0);
1003: if (c->session->curw->window != wp->window)
1004: return (0);
1005: return (1);
1006: }
1007:
1.15 nicm 1008: void
1.182 nicm 1009: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1010: struct tty_ctx *ctx)
1.15 nicm 1011: {
1012: struct window_pane *wp = ctx->wp;
1013: struct client *c;
1014:
1.75 nicm 1015: /* wp can be NULL if updating the screen but not the terminal. */
1.15 nicm 1016: if (wp == NULL)
1017: return;
1018:
1.260 nicm 1019: if ((wp->flags & (PANE_REDRAW|PANE_DROP)) || !window_pane_visible(wp))
1.15 nicm 1020: return;
1021:
1.178 nicm 1022: TAILQ_FOREACH(c, &clients, entry) {
1.182 nicm 1023: if (!tty_client_ready(c, wp))
1.15 nicm 1024: continue;
1025:
1.139 nicm 1026: ctx->xoff = wp->xoff;
1027: ctx->yoff = wp->yoff;
1028: if (status_at_line(c) == 0)
1029: ctx->yoff++;
1.113 nicm 1030:
1.139 nicm 1031: cmdfn(&c->tty, ctx);
1.1 nicm 1032: }
1033: }
1034:
1035: void
1.24 nicm 1036: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1037: {
1.77 nicm 1038: struct window_pane *wp = ctx->wp;
1.1 nicm 1039:
1.210 nicm 1040: if (!tty_pane_full_width(tty, ctx) ||
1041: tty_fake_bce(tty, wp, ctx->bg) ||
1042: (!tty_term_has(tty->term, TTYC_ICH) &&
1043: !tty_term_has(tty->term, TTYC_ICH1))) {
1.176 nicm 1044: tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 1045: return;
1046: }
1047:
1.210 nicm 1048: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1049:
1.77 nicm 1050: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 1051:
1.206 nicm 1052: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1 nicm 1053: }
1054:
1055: void
1.24 nicm 1056: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1057: {
1.77 nicm 1058: struct window_pane *wp = ctx->wp;
1.1 nicm 1059:
1.210 nicm 1060: if (!tty_pane_full_width(tty, ctx) ||
1061: tty_fake_bce(tty, wp, ctx->bg) ||
1.26 nicm 1062: (!tty_term_has(tty->term, TTYC_DCH) &&
1063: !tty_term_has(tty->term, TTYC_DCH1))) {
1.176 nicm 1064: tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 1065: return;
1066: }
1067:
1.210 nicm 1068: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1069:
1.77 nicm 1070: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 1071:
1.206 nicm 1072: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146 nicm 1073: }
1074:
1075: void
1076: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1077: {
1.275 nicm 1078: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.146 nicm 1079:
1080: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1081:
1.210 nicm 1082: if (tty_term_has(tty->term, TTYC_ECH) &&
1.262 nicm 1083: !tty_fake_bce(tty, ctx->wp, 8))
1.146 nicm 1084: tty_putcode1(tty, TTYC_ECH, ctx->num);
1.257 nicm 1085: else
1086: tty_repeat_space(tty, ctx->num);
1.1 nicm 1087: }
1088:
1089: void
1.24 nicm 1090: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1091: {
1.210 nicm 1092: if (!tty_pane_full_width(tty, ctx) ||
1093: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.77 nicm 1094: !tty_term_has(tty->term, TTYC_CSR) ||
1.72 nicm 1095: !tty_term_has(tty->term, TTYC_IL1)) {
1.14 nicm 1096: tty_redraw_region(tty, ctx);
1.1 nicm 1097: return;
1098: }
1099:
1.210 nicm 1100: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1 nicm 1101:
1.77 nicm 1102: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1103: tty_margin_off(tty);
1.77 nicm 1104: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1105:
1.12 nicm 1106: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.279 nicm 1107: tty->cx = tty->cy = UINT_MAX;
1.1 nicm 1108: }
1109:
1110: void
1.24 nicm 1111: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1112: {
1.210 nicm 1113: if (!tty_pane_full_width(tty, ctx) ||
1114: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.72 nicm 1115: !tty_term_has(tty->term, TTYC_CSR) ||
1116: !tty_term_has(tty->term, TTYC_DL1)) {
1.14 nicm 1117: tty_redraw_region(tty, ctx);
1.1 nicm 1118: return;
1119: }
1120:
1.210 nicm 1121: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1 nicm 1122:
1.77 nicm 1123: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1124: tty_margin_off(tty);
1.77 nicm 1125: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1126:
1.12 nicm 1127: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.279 nicm 1128: tty->cx = tty->cy = UINT_MAX;
1.1 nicm 1129: }
1130:
1131: void
1.24 nicm 1132: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1133: {
1.77 nicm 1134: struct window_pane *wp = ctx->wp;
1.271 nicm 1135: u_int nx, py = ctx->yoff + ctx->ocy;
1.1 nicm 1136:
1.210 nicm 1137: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1138:
1.271 nicm 1139: nx = screen_size_x(wp->screen);
1140: tty_clear_line(tty, wp, py, ctx->xoff, nx, ctx->bg);
1.1 nicm 1141: }
1142:
1143: void
1.24 nicm 1144: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1145: {
1.77 nicm 1146: struct window_pane *wp = ctx->wp;
1.271 nicm 1147: u_int nx, py = ctx->yoff + ctx->ocy;
1.1 nicm 1148:
1.210 nicm 1149: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1150:
1.271 nicm 1151: nx = screen_size_x(wp->screen) - ctx->ocx;
1152: tty_clear_line(tty, wp, py, ctx->xoff + ctx->ocx, nx, ctx->bg);
1.1 nicm 1153: }
1154:
1155: void
1.24 nicm 1156: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1157: {
1.210 nicm 1158: struct window_pane *wp = ctx->wp;
1.282 nicm 1159: u_int py = ctx->yoff + ctx->ocy;
1.210 nicm 1160:
1161: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1162:
1.271 nicm 1163: tty_clear_line(tty, wp, py, ctx->xoff, ctx->ocx + 1, ctx->bg);
1.1 nicm 1164: }
1165:
1166: void
1.24 nicm 1167: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1168: {
1.278 nicm 1169: struct window_pane *wp = ctx->wp;
1170:
1.56 nicm 1171: if (ctx->ocy != ctx->orupper)
1172: return;
1173:
1.210 nicm 1174: if (!tty_pane_full_width(tty, ctx) ||
1.278 nicm 1175: tty_fake_bce(tty, wp, 8) ||
1.70 nicm 1176: !tty_term_has(tty->term, TTYC_CSR) ||
1177: !tty_term_has(tty->term, TTYC_RI)) {
1.14 nicm 1178: tty_redraw_region(tty, ctx);
1.1 nicm 1179: return;
1180: }
1181:
1.278 nicm 1182: tty_default_attributes(tty, wp, ctx->bg);
1.77 nicm 1183:
1.56 nicm 1184: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1185: tty_margin_off(tty);
1.56 nicm 1186: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 1187:
1.56 nicm 1188: tty_putcode(tty, TTYC_RI);
1.1 nicm 1189: }
1190:
1191: void
1.24 nicm 1192: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1193: {
1.77 nicm 1194: struct window_pane *wp = ctx->wp;
1.1 nicm 1195:
1.56 nicm 1196: if (ctx->ocy != ctx->orlower)
1197: return;
1198:
1.212 nicm 1199: if ((!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.262 nicm 1200: tty_fake_bce(tty, wp, 8) ||
1.1 nicm 1201: !tty_term_has(tty->term, TTYC_CSR)) {
1.236 nicm 1202: tty_redraw_region(tty, ctx);
1.1 nicm 1203: return;
1204: }
1.52 nicm 1205:
1.278 nicm 1206: tty_default_attributes(tty, wp, ctx->bg);
1.77 nicm 1207:
1.56 nicm 1208: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.212 nicm 1209: tty_margin_pane(tty, ctx);
1210:
1211: /*
1212: * If we want to wrap a pane, the cursor needs to be exactly on the
1213: * right of the region. But if the pane isn't on the right, it may be
1214: * off the edge - if so, move the cursor back to the right.
1215: */
1216: if (ctx->xoff + ctx->ocx > tty->rright)
1.216 nicm 1217: tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1.212 nicm 1218: else
1219: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 1220:
1.56 nicm 1221: tty_putc(tty, '\n');
1.240 nicm 1222: }
1223:
1224: void
1225: tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1226: {
1227: struct window_pane *wp = ctx->wp;
1.284 nicm 1228: u_int i;
1.240 nicm 1229:
1230: if ((!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.262 nicm 1231: tty_fake_bce(tty, wp, 8) ||
1.240 nicm 1232: !tty_term_has(tty->term, TTYC_CSR)) {
1233: tty_redraw_region(tty, ctx);
1234: return;
1235: }
1236:
1.278 nicm 1237: tty_default_attributes(tty, wp, ctx->bg);
1.240 nicm 1238:
1239: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1240: tty_margin_pane(tty, ctx);
1241:
1.284 nicm 1242: if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1.241 nicm 1243: tty_cursor(tty, tty->rright, tty->rlower);
1.284 nicm 1244: for (i = 0; i < ctx->num; i++)
1.240 nicm 1245: tty_putc(tty, '\n');
1246: } else
1.284 nicm 1247: tty_putcode1(tty, TTYC_INDN, ctx->num);
1.1 nicm 1248: }
1249:
1250: void
1.24 nicm 1251: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1252: {
1.77 nicm 1253: struct window_pane *wp = ctx->wp;
1.271 nicm 1254: u_int px, py, nx, ny;
1.1 nicm 1255:
1.210 nicm 1256: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1257:
1.271 nicm 1258: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1259: tty_margin_off(tty);
1.39 nicm 1260:
1.271 nicm 1261: px = ctx->xoff;
1262: nx = screen_size_x(wp->screen);
1263: py = ctx->yoff + ctx->ocy + 1;
1264: ny = screen_size_y(wp->screen) - ctx->ocy - 1;
1265:
1266: tty_clear_area(tty, wp, py, ny, px, nx, ctx->bg);
1267:
1268: px = ctx->xoff + ctx->ocx;
1269: nx = screen_size_x(wp->screen) - ctx->ocx;
1270: py = ctx->yoff + ctx->ocy;
1271:
1272: tty_clear_line(tty, wp, py, px, nx, ctx->bg);
1.1 nicm 1273: }
1274:
1275: void
1.24 nicm 1276: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1277: {
1.77 nicm 1278: struct window_pane *wp = ctx->wp;
1.271 nicm 1279: u_int px, py, nx, ny;
1.1 nicm 1280:
1.227 nicm 1281: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1282:
1.271 nicm 1283: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1284: tty_margin_off(tty);
1.39 nicm 1285:
1.271 nicm 1286: px = ctx->xoff;
1287: nx = screen_size_x(wp->screen);
1288: py = ctx->yoff;
1289: ny = ctx->ocy - 1;
1290:
1291: tty_clear_area(tty, wp, py, ny, px, nx, ctx->bg);
1292:
1293: px = ctx->xoff;
1294: nx = ctx->ocx + 1;
1295: py = ctx->yoff + ctx->ocy;
1296:
1297: tty_clear_line(tty, wp, py, px, nx, ctx->bg);
1.1 nicm 1298: }
1299:
1300: void
1.24 nicm 1301: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1302: {
1.77 nicm 1303: struct window_pane *wp = ctx->wp;
1.271 nicm 1304: u_int px, py, nx, ny;
1.1 nicm 1305:
1.210 nicm 1306: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1307:
1.271 nicm 1308: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1309: tty_margin_off(tty);
1.39 nicm 1310:
1.271 nicm 1311: px = ctx->xoff;
1312: nx = screen_size_x(wp->screen);
1313: py = ctx->yoff;
1314: ny = screen_size_y(wp->screen);
1315:
1316: tty_clear_area(tty, wp, py, ny, px, nx, ctx->bg);
1.4 nicm 1317: }
1318:
1319: void
1.24 nicm 1320: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 1321: {
1.12 nicm 1322: struct window_pane *wp = ctx->wp;
1323: struct screen *s = wp->screen;
1324: u_int i, j;
1.4 nicm 1325:
1.176 nicm 1326: tty_attributes(tty, &grid_default_cell, wp);
1.4 nicm 1327:
1.39 nicm 1328: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.214 nicm 1329: tty_margin_off(tty);
1.4 nicm 1330:
1331: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 1332: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 1333: for (i = 0; i < screen_size_x(s); i++)
1334: tty_putc(tty, 'E');
1.1 nicm 1335: }
1336: }
1337:
1338: void
1.24 nicm 1339: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1340: {
1.222 nicm 1341: if (ctx->xoff + ctx->ocx > tty->sx - 1 && ctx->ocy == ctx->orlower) {
1342: if (tty_pane_full_width(tty, ctx))
1343: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1344: else
1345: tty_margin_off(tty);
1346: }
1.46 nicm 1347:
1.253 nicm 1348: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1349:
1.237 nicm 1350: tty_cell(tty, ctx->cell, ctx->wp);
1.239 nicm 1351: }
1352:
1353: void
1354: tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
1355: {
1.253 nicm 1356: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.239 nicm 1357:
1358: tty_attributes(tty, ctx->cell, ctx->wp);
1359: tty_putn(tty, ctx->ptr, ctx->num, ctx->num);
1.106 nicm 1360: }
1361:
1362: void
1363: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1364: {
1365: char *buf;
1366: size_t off;
1367:
1368: if (!tty_term_has(tty->term, TTYC_MS))
1369: return;
1370:
1371: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1372: buf = xmalloc(off);
1373:
1374: b64_ntop(ctx->ptr, ctx->num, buf, off);
1375: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1376:
1.138 nicm 1377: free(buf);
1.100 nicm 1378: }
1379:
1380: void
1381: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1382: {
1.256 nicm 1383: tty_add(tty, ctx->ptr, ctx->num);
1.242 nicm 1384: tty_invalidate(tty);
1.1 nicm 1385: }
1386:
1.207 nicm 1387: static void
1.176 nicm 1388: tty_cell(struct tty *tty, const struct grid_cell *gc,
1389: const struct window_pane *wp)
1.1 nicm 1390: {
1.193 nicm 1391: u_int i;
1.1 nicm 1392:
1393: /* Skip last character if terminal is stupid. */
1.211 nicm 1394: if ((tty->term->flags & TERM_EARLYWRAP) &&
1395: tty->cy == tty->sy - 1 &&
1396: tty->cx == tty->sx - 1)
1.1 nicm 1397: return;
1398:
1399: /* If this is a padding character, do nothing. */
1400: if (gc->flags & GRID_FLAG_PADDING)
1401: return;
1402:
1403: /* Set the attributes. */
1.176 nicm 1404: tty_attributes(tty, gc, wp);
1.1 nicm 1405:
1.147 nicm 1406: /* Get the cell and if ASCII write with putc to do ACS translation. */
1.193 nicm 1407: if (gc->data.size == 1) {
1408: if (*gc->data.data < 0x20 || *gc->data.data == 0x7f)
1.1 nicm 1409: return;
1.193 nicm 1410: tty_putc(tty, *gc->data.data);
1.1 nicm 1411: return;
1412: }
1413:
1.147 nicm 1414: /* If not UTF-8, write _. */
1.1 nicm 1415: if (!(tty->flags & TTY_UTF8)) {
1.193 nicm 1416: for (i = 0; i < gc->data.width; i++)
1.1 nicm 1417: tty_putc(tty, '_');
1418: return;
1419: }
1420:
1.147 nicm 1421: /* Write the data. */
1.193 nicm 1422: tty_putn(tty, gc->data.data, gc->data.size, gc->data.width);
1.1 nicm 1423: }
1424:
1425: void
1426: tty_reset(struct tty *tty)
1427: {
1428: struct grid_cell *gc = &tty->cell;
1429:
1.228 nicm 1430: if (!grid_cells_equal(gc, &grid_default_cell)) {
1.285 nicm 1431: if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.228 nicm 1432: tty_putcode(tty, TTYC_RMACS);
1433: tty_putcode(tty, TTYC_SGR0);
1434: memcpy(gc, &grid_default_cell, sizeof *gc);
1435: }
1.1 nicm 1436:
1.228 nicm 1437: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
1438: tty->last_wp = -1;
1.242 nicm 1439: }
1440:
1441: static void
1442: tty_invalidate(struct tty *tty)
1443: {
1444: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
1445:
1446: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
1447: tty->last_wp = -1;
1448:
1449: tty->cx = tty->cy = UINT_MAX;
1450:
1451: tty->rupper = tty->rleft = UINT_MAX;
1452: tty->rlower = tty->rright = UINT_MAX;
1453:
1454: if (tty->flags & TTY_STARTED) {
1455: tty_putcode(tty, TTYC_SGR0);
1456:
1457: tty->mode = ALL_MODES;
1458: tty_update_mode(tty, MODE_CURSOR, NULL);
1459:
1460: tty_cursor(tty, 0, 0);
1461: tty_region_off(tty);
1462: tty_margin_off(tty);
1463: } else
1464: tty->mode = MODE_CURSOR;
1.1 nicm 1465: }
1466:
1.214 nicm 1467: /* Turn off margin. */
1468: void
1469: tty_region_off(struct tty *tty)
1470: {
1471: tty_region(tty, 0, tty->sy - 1);
1472: }
1473:
1.40 nicm 1474: /* Set region inside pane. */
1.208 nicm 1475: static void
1.196 nicm 1476: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
1477: u_int rlower)
1.1 nicm 1478: {
1.113 nicm 1479: tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39 nicm 1480: }
1481:
1.40 nicm 1482: /* Set region at absolute position. */
1.214 nicm 1483: static void
1.40 nicm 1484: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 1485: {
1486: if (tty->rlower == rlower && tty->rupper == rupper)
1487: return;
1.1 nicm 1488: if (!tty_term_has(tty->term, TTYC_CSR))
1489: return;
1.39 nicm 1490:
1491: tty->rupper = rupper;
1492: tty->rlower = rlower;
1.55 nicm 1493:
1494: /*
1495: * Some terminals (such as PuTTY) do not correctly reset the cursor to
1496: * 0,0 if it is beyond the last column (they do not reset their wrap
1497: * flag so further output causes a line feed). As a workaround, do an
1498: * explicit move to 0 first.
1499: */
1500: if (tty->cx >= tty->sx)
1501: tty_cursor(tty, 0, tty->cy);
1.42 nicm 1502:
1.39 nicm 1503: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.249 nicm 1504: tty->cx = tty->cy = UINT_MAX;
1.212 nicm 1505: }
1506:
1.214 nicm 1507: /* Turn off margin. */
1508: void
1509: tty_margin_off(struct tty *tty)
1510: {
1511: tty_margin(tty, 0, tty->sx - 1);
1512: }
1513:
1.212 nicm 1514: /* Set margin inside pane. */
1515: static void
1516: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
1517: {
1518: tty_margin(tty, ctx->xoff, ctx->xoff + ctx->wp->sx - 1);
1519: }
1520:
1521: /* Set margin at absolute position. */
1.214 nicm 1522: static void
1.212 nicm 1523: tty_margin(struct tty *tty, u_int rleft, u_int rright)
1524: {
1525: char s[64];
1526:
1527: if (!tty_use_margin(tty))
1528: return;
1529: if (tty->rleft == rleft && tty->rright == rright)
1530: return;
1531:
1.232 nicm 1532: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.231 nicm 1533:
1.212 nicm 1534: tty->rleft = rleft;
1535: tty->rright = rright;
1536:
1.232 nicm 1537: if (rleft == 0 && rright == tty->sx - 1)
1538: snprintf(s, sizeof s, "\033[s");
1539: else
1540: snprintf(s, sizeof s, "\033[%u;%us", rleft + 1, rright + 1);
1.212 nicm 1541: tty_puts(tty, s);
1.249 nicm 1542: tty->cx = tty->cy = UINT_MAX;
1.253 nicm 1543: }
1544:
1545: /*
1546: * Move the cursor, unless it would wrap itself when the next character is
1547: * printed.
1548: */
1549: static void
1550: tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
1551: u_int cx, u_int cy)
1552: {
1.268 nicm 1553: if (!ctx->wrapped ||
1554: !tty_pane_full_width(tty, ctx) ||
1.253 nicm 1555: (tty->term->flags & TERM_EARLYWRAP) ||
1556: ctx->xoff + cx != 0 ||
1557: ctx->yoff + cy != tty->cy + 1 ||
1.254 nicm 1558: tty->cx < tty->sx ||
1559: tty->cy == tty->rlower)
1.253 nicm 1560: tty_cursor_pane(tty, ctx, cx, cy);
1561: else
1562: log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
1.1 nicm 1563: }
1564:
1.42 nicm 1565: /* Move cursor inside pane. */
1.208 nicm 1566: static void
1.41 nicm 1567: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1568: {
1.113 nicm 1569: tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41 nicm 1570: }
1571:
1.42 nicm 1572: /* Move cursor to absolute position. */
1.41 nicm 1573: void
1574: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 1575: {
1.42 nicm 1576: struct tty_term *term = tty->term;
1577: u_int thisx, thisy;
1578: int change;
1.77 nicm 1579:
1.42 nicm 1580: if (cx > tty->sx - 1)
1581: cx = tty->sx - 1;
1582:
1583: thisx = tty->cx;
1584: thisy = tty->cy;
1585:
1586: /* No change. */
1587: if (cx == thisx && cy == thisy)
1588: return;
1589:
1.43 nicm 1590: /* Very end of the line, just use absolute movement. */
1591: if (thisx > tty->sx - 1)
1592: goto absolute;
1593:
1.42 nicm 1594: /* Move to home position (0, 0). */
1595: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1596: tty_putcode(tty, TTYC_HOME);
1597: goto out;
1598: }
1599:
1600: /* Zero on the next line. */
1.48 nicm 1601: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1 nicm 1602: tty_putc(tty, '\r');
1.42 nicm 1603: tty_putc(tty, '\n');
1604: goto out;
1605: }
1606:
1.45 nicm 1607: /* Moving column or row. */
1.42 nicm 1608: if (cy == thisy) {
1.45 nicm 1609: /*
1610: * Moving column only, row staying the same.
1611: */
1612:
1.42 nicm 1613: /* To left edge. */
1.202 nicm 1614: if (cx == 0) {
1.42 nicm 1615: tty_putc(tty, '\r');
1616: goto out;
1617: }
1618:
1619: /* One to the left. */
1620: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1621: tty_putcode(tty, TTYC_CUB1);
1622: goto out;
1623: }
1624:
1625: /* One to the right. */
1626: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1627: tty_putcode(tty, TTYC_CUF1);
1628: goto out;
1629: }
1630:
1631: /* Calculate difference. */
1632: change = thisx - cx; /* +ve left, -ve right */
1633:
1634: /*
1635: * Use HPA if change is larger than absolute, otherwise move
1636: * the cursor with CUB/CUF.
1637: */
1.87 nicm 1638: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 1639: tty_putcode1(tty, TTYC_HPA, cx);
1640: goto out;
1641: } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1.202 nicm 1642: if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
1643: tty_putcode(tty, TTYC_CUB1);
1644: tty_putcode(tty, TTYC_CUB1);
1645: goto out;
1646: }
1.42 nicm 1647: tty_putcode1(tty, TTYC_CUB, change);
1648: goto out;
1649: } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1650: tty_putcode1(tty, TTYC_CUF, -change);
1651: goto out;
1652: }
1.45 nicm 1653: } else if (cx == thisx) {
1654: /*
1655: * Moving row only, column staying the same.
1656: */
1.42 nicm 1657:
1658: /* One above. */
1.77 nicm 1659: if (thisy != tty->rupper &&
1.42 nicm 1660: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1661: tty_putcode(tty, TTYC_CUU1);
1662: goto out;
1663: }
1664:
1665: /* One below. */
1.49 nicm 1666: if (thisy != tty->rlower &&
1.42 nicm 1667: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1668: tty_putcode(tty, TTYC_CUD1);
1669: goto out;
1670: }
1671:
1672: /* Calculate difference. */
1673: change = thisy - cy; /* +ve up, -ve down */
1674:
1675: /*
1.77 nicm 1676: * Try to use VPA if change is larger than absolute or if this
1677: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 1678: */
1.87 nicm 1679: if ((u_int) abs(change) > cy ||
1.42 nicm 1680: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 1681: (change > 0 && cy - change < tty->rupper)) {
1682: if (tty_term_has(term, TTYC_VPA)) {
1683: tty_putcode1(tty, TTYC_VPA, cy);
1684: goto out;
1685: }
1.42 nicm 1686: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1687: tty_putcode1(tty, TTYC_CUU, change);
1688: goto out;
1689: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1690: tty_putcode1(tty, TTYC_CUD, -change);
1691: goto out;
1692: }
1693: }
1694:
1.43 nicm 1695: absolute:
1.42 nicm 1696: /* Absolute movement. */
1697: tty_putcode2(tty, TTYC_CUP, cy, cx);
1698:
1699: out:
1700: tty->cx = cx;
1701: tty->cy = cy;
1.1 nicm 1702: }
1703:
1704: void
1.176 nicm 1705: tty_attributes(struct tty *tty, const struct grid_cell *gc,
1706: const struct window_pane *wp)
1.1 nicm 1707: {
1.63 nicm 1708: struct grid_cell *tc = &tty->cell, gc2;
1.255 nicm 1709: int changed;
1.61 nicm 1710:
1.228 nicm 1711: /* Ignore cell if it is the same as the last one. */
1712: if (wp != NULL &&
1713: (int)wp->id == tty->last_wp &&
1714: ~(wp->window->flags & WINDOW_STYLECHANGED) &&
1715: gc->attr == tty->last_cell.attr &&
1716: gc->fg == tty->last_cell.fg &&
1717: gc->bg == tty->last_cell.bg)
1718: return;
1719: tty->last_wp = (wp != NULL ? (int)wp->id : -1);
1720: memcpy(&tty->last_cell, gc, sizeof tty->last_cell);
1721:
1722: /* Copy cell and update default colours. */
1.85 nicm 1723: memcpy(&gc2, gc, sizeof gc2);
1.203 nicm 1724: if (wp != NULL)
1725: tty_default_colours(&gc2, wp);
1.63 nicm 1726:
1.18 nicm 1727: /*
1728: * If no setab, try to use the reverse attribute as a best-effort for a
1729: * non-default background. This is a bit of a hack but it doesn't do
1730: * any serious harm and makes a couple of applications happier.
1731: */
1732: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 1733: if (gc2.attr & GRID_ATTR_REVERSE) {
1734: if (gc2.fg != 7 && gc2.fg != 8)
1.64 nicm 1735: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 1736: } else {
1.85 nicm 1737: if (gc2.bg != 0 && gc2.bg != 8)
1.64 nicm 1738: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 1739: }
1740: }
1.1 nicm 1741:
1.85 nicm 1742: /* Fix up the colours if necessary. */
1.219 nicm 1743: tty_check_fg(tty, wp, &gc2);
1744: tty_check_bg(tty, wp, &gc2);
1.85 nicm 1745:
1.64 nicm 1746: /* If any bits are being cleared, reset everything. */
1.85 nicm 1747: if (tc->attr & ~gc2.attr)
1.64 nicm 1748: tty_reset(tty);
1749:
1750: /*
1751: * Set the colours. This may call tty_reset() (so it comes next) and
1752: * may add to (NOT remove) the desired attributes by changing new_attr.
1753: */
1.85 nicm 1754: tty_colours(tty, &gc2);
1.64 nicm 1755:
1.1 nicm 1756: /* Filter out attribute bits already set. */
1.85 nicm 1757: changed = gc2.attr & ~tc->attr;
1758: tc->attr = gc2.attr;
1.1 nicm 1759:
1760: /* Set the attributes. */
1761: if (changed & GRID_ATTR_BRIGHT)
1762: tty_putcode(tty, TTYC_BOLD);
1763: if (changed & GRID_ATTR_DIM)
1764: tty_putcode(tty, TTYC_DIM);
1.180 nicm 1765: if (changed & GRID_ATTR_ITALICS)
1766: tty_set_italics(tty);
1.1 nicm 1767: if (changed & GRID_ATTR_UNDERSCORE)
1768: tty_putcode(tty, TTYC_SMUL);
1769: if (changed & GRID_ATTR_BLINK)
1770: tty_putcode(tty, TTYC_BLINK);
1771: if (changed & GRID_ATTR_REVERSE) {
1772: if (tty_term_has(tty->term, TTYC_REV))
1773: tty_putcode(tty, TTYC_REV);
1774: else if (tty_term_has(tty->term, TTYC_SMSO))
1775: tty_putcode(tty, TTYC_SMSO);
1776: }
1777: if (changed & GRID_ATTR_HIDDEN)
1778: tty_putcode(tty, TTYC_INVIS);
1.255 nicm 1779: if (changed & GRID_ATTR_STRIKETHROUGH)
1780: tty_putcode(tty, TTYC_SMXX);
1.285 nicm 1781: if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.1 nicm 1782: tty_putcode(tty, TTYC_SMACS);
1783: }
1784:
1.207 nicm 1785: static void
1.85 nicm 1786: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1787: {
1.61 nicm 1788: struct grid_cell *tc = &tty->cell;
1.204 nicm 1789: int have_ax;
1.61 nicm 1790:
1791: /* No changes? Nothing is necessary. */
1.204 nicm 1792: if (gc->fg == tc->fg && gc->bg == tc->bg)
1.63 nicm 1793: return;
1.1 nicm 1794:
1.61 nicm 1795: /*
1796: * Is either the default colour? This is handled specially because the
1797: * best solution might be to reset both colours to default, in which
1798: * case if only one is default need to fall onward to set the other
1799: * colour.
1800: */
1.204 nicm 1801: if (gc->fg == 8 || gc->bg == 8) {
1.61 nicm 1802: /*
1803: * If don't have AX but do have op, send sgr0 (op can't
1804: * actually be used because it is sometimes the same as sgr0
1805: * and sometimes isn't). This resets both colours to default.
1806: *
1807: * Otherwise, try to set the default colour only as needed.
1808: */
1.174 nicm 1809: have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61 nicm 1810: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1811: tty_reset(tty);
1812: else {
1.204 nicm 1813: if (gc->fg == 8 && tc->fg != 8) {
1.61 nicm 1814: if (have_ax)
1815: tty_puts(tty, "\033[39m");
1.204 nicm 1816: else if (tc->fg != 7)
1.61 nicm 1817: tty_putcode1(tty, TTYC_SETAF, 7);
1818: tc->fg = 8;
1819: }
1.204 nicm 1820: if (gc->bg == 8 && tc->bg != 8) {
1.77 nicm 1821: if (have_ax)
1.61 nicm 1822: tty_puts(tty, "\033[49m");
1.204 nicm 1823: else if (tc->bg != 0)
1.61 nicm 1824: tty_putcode1(tty, TTYC_SETAB, 0);
1825: tc->bg = 8;
1826: }
1827: }
1828: }
1.1 nicm 1829:
1.61 nicm 1830: /* Set the foreground colour. */
1.204 nicm 1831: if (gc->fg != 8 && gc->fg != tc->fg)
1.85 nicm 1832: tty_colours_fg(tty, gc);
1.1 nicm 1833:
1.61 nicm 1834: /*
1835: * Set the background colour. This must come after the foreground as
1836: * tty_colour_fg() can call tty_reset().
1837: */
1.204 nicm 1838: if (gc->bg != 8 && gc->bg != tc->bg)
1.73 nicm 1839: tty_colours_bg(tty, gc);
1.1 nicm 1840: }
1841:
1.219 nicm 1842: static void
1843: tty_check_fg(struct tty *tty, const struct window_pane *wp,
1844: struct grid_cell *gc)
1.85 nicm 1845: {
1.204 nicm 1846: u_char r, g, b;
1847: u_int colours;
1.223 nicm 1848: int c;
1.85 nicm 1849:
1.219 nicm 1850: /* Perform substitution if this pane has a palette */
1851: if ((~gc->flags & GRID_FLAG_NOPALETTE) &&
1.223 nicm 1852: (c = window_pane_get_palette(wp, gc->fg)) != -1)
1853: gc->fg = c;
1.219 nicm 1854:
1.199 nicm 1855: /* Is this a 24-bit colour? */
1.204 nicm 1856: if (gc->fg & COLOUR_FLAG_RGB) {
1.199 nicm 1857: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.286 ! nicm 1858: if (!tty_term_has(tty->term, TTYC_SETRGBF)) {
1.204 nicm 1859: colour_split_rgb(gc->fg, &r, &g, &b);
1860: gc->fg = colour_find_rgb(r, g, b);
1861: } else
1.200 nicm 1862: return;
1.199 nicm 1863: }
1.224 nicm 1864:
1865: /* How many colours does this terminal have? */
1866: if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
1867: colours = 256;
1868: else
1869: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 1870:
1.85 nicm 1871: /* Is this a 256-colour colour? */
1.204 nicm 1872: if (gc->fg & COLOUR_FLAG_256) {
1.85 nicm 1873: /* And not a 256 colour mode? */
1.224 nicm 1874: if (colours != 256) {
1.85 nicm 1875: gc->fg = colour_256to16(gc->fg);
1876: if (gc->fg & 8) {
1877: gc->fg &= 7;
1.175 nicm 1878: if (colours >= 16)
1879: gc->fg += 90;
1880: else
1881: gc->attr |= GRID_ATTR_BRIGHT;
1.85 nicm 1882: } else
1883: gc->attr &= ~GRID_ATTR_BRIGHT;
1884: }
1885: return;
1886: }
1887:
1888: /* Is this an aixterm colour? */
1889: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1890: gc->fg -= 90;
1891: gc->attr |= GRID_ATTR_BRIGHT;
1892: }
1893: }
1894:
1.219 nicm 1895: static void
1896: tty_check_bg(struct tty *tty, const struct window_pane *wp,
1897: struct grid_cell *gc)
1.85 nicm 1898: {
1.204 nicm 1899: u_char r, g, b;
1900: u_int colours;
1.223 nicm 1901: int c;
1.85 nicm 1902:
1.219 nicm 1903: /* Perform substitution if this pane has a palette */
1904: if ((~gc->flags & GRID_FLAG_NOPALETTE) &&
1.223 nicm 1905: (c = window_pane_get_palette(wp, gc->bg)) != -1)
1906: gc->bg = c;
1.219 nicm 1907:
1.199 nicm 1908: /* Is this a 24-bit colour? */
1.204 nicm 1909: if (gc->bg & COLOUR_FLAG_RGB) {
1.199 nicm 1910: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.286 ! nicm 1911: if (!tty_term_has(tty->term, TTYC_SETRGBB)) {
1.204 nicm 1912: colour_split_rgb(gc->bg, &r, &g, &b);
1913: gc->bg = colour_find_rgb(r, g, b);
1914: } else
1.200 nicm 1915: return;
1.199 nicm 1916: }
1.224 nicm 1917:
1918: /* How many colours does this terminal have? */
1919: if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
1920: colours = 256;
1921: else
1922: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 1923:
1.85 nicm 1924: /* Is this a 256-colour colour? */
1.204 nicm 1925: if (gc->bg & COLOUR_FLAG_256) {
1.85 nicm 1926: /*
1927: * And not a 256 colour mode? Translate to 16-colour
1928: * palette. Bold background doesn't exist portably, so just
1929: * discard the bold bit if set.
1930: */
1.224 nicm 1931: if (colours != 256) {
1.85 nicm 1932: gc->bg = colour_256to16(gc->bg);
1.175 nicm 1933: if (gc->bg & 8) {
1.85 nicm 1934: gc->bg &= 7;
1.175 nicm 1935: if (colours >= 16)
1936: gc->fg += 90;
1937: }
1.85 nicm 1938: }
1939: return;
1940: }
1941:
1942: /* Is this an aixterm colour? */
1.175 nicm 1943: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85 nicm 1944: gc->bg -= 90;
1945: }
1946:
1.207 nicm 1947: static void
1.85 nicm 1948: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1949: {
1.61 nicm 1950: struct grid_cell *tc = &tty->cell;
1.79 nicm 1951: char s[32];
1.1 nicm 1952:
1.204 nicm 1953: /* Is this a 24-bit or 256-colour colour? */
1954: if (gc->fg & COLOUR_FLAG_RGB ||
1955: gc->fg & COLOUR_FLAG_256) {
1956: if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61 nicm 1957: goto save_fg;
1.199 nicm 1958: /* Should not get here, already converted in tty_check_fg. */
1.85 nicm 1959: return;
1.1 nicm 1960: }
1961:
1.79 nicm 1962: /* Is this an aixterm bright colour? */
1.204 nicm 1963: if (gc->fg >= 90 && gc->fg <= 97) {
1964: xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
1.85 nicm 1965: tty_puts(tty, s);
1966: goto save_fg;
1.79 nicm 1967: }
1968:
1.61 nicm 1969: /* Otherwise set the foreground colour. */
1.204 nicm 1970: tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61 nicm 1971:
1.77 nicm 1972: save_fg:
1.61 nicm 1973: /* Save the new values in the terminal current cell. */
1.204 nicm 1974: tc->fg = gc->fg;
1.1 nicm 1975: }
1976:
1.207 nicm 1977: static void
1.73 nicm 1978: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1979: {
1.61 nicm 1980: struct grid_cell *tc = &tty->cell;
1.79 nicm 1981: char s[32];
1.1 nicm 1982:
1.204 nicm 1983: /* Is this a 24-bit or 256-colour colour? */
1984: if (gc->bg & COLOUR_FLAG_RGB ||
1985: gc->bg & COLOUR_FLAG_256) {
1986: if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61 nicm 1987: goto save_bg;
1.199 nicm 1988: /* Should not get here, already converted in tty_check_bg. */
1.85 nicm 1989: return;
1.79 nicm 1990: }
1991:
1992: /* Is this an aixterm bright colour? */
1.204 nicm 1993: if (gc->bg >= 90 && gc->bg <= 97) {
1994: xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
1.175 nicm 1995: tty_puts(tty, s);
1996: goto save_bg;
1.1 nicm 1997: }
1998:
1.61 nicm 1999: /* Otherwise set the background colour. */
1.204 nicm 2000: tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61 nicm 2001:
2002: save_bg:
2003: /* Save the new values in the terminal current cell. */
1.204 nicm 2004: tc->bg = gc->bg;
1.61 nicm 2005: }
2006:
1.207 nicm 2007: static int
1.204 nicm 2008: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61 nicm 2009: {
1.204 nicm 2010: u_char r, g, b;
1.61 nicm 2011: char s[32];
2012:
1.204 nicm 2013: if (colour & COLOUR_FLAG_256) {
2014: /*
2015: * If the user has specified -2 to the client, setaf and setab
2016: * may not work (or they may not want to use them), so send the
2017: * usual sequence.
2018: */
2019: if (tty->term_flags & TERM_256COLOURS)
2020: goto fallback_256;
1.189 nicm 2021:
1.204 nicm 2022: /*
2023: * If the terminfo entry has 256 colours and setaf and setab
2024: * exist, assume that they work correctly.
2025: */
2026: if (tty->term->flags & TERM_256COLOURS) {
2027: if (*type == '3') {
2028: if (!tty_term_has(tty->term, TTYC_SETAF))
2029: goto fallback_256;
2030: tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
2031: } else {
2032: if (!tty_term_has(tty->term, TTYC_SETAB))
2033: goto fallback_256;
2034: tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
2035: }
2036: return (0);
1.188 nicm 2037: }
1.204 nicm 2038: goto fallback_256;
2039: }
2040:
2041: if (colour & COLOUR_FLAG_RGB) {
1.286 ! nicm 2042: if (*type == '3') {
! 2043: if (!tty_term_has(tty->term, TTYC_SETRGBF))
! 2044: return (-1);
! 2045: colour_split_rgb(colour & 0xffffff, &r, &g, &b);
! 2046: tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
! 2047: } else {
! 2048: if (!tty_term_has(tty->term, TTYC_SETRGBB))
! 2049: return (-1);
! 2050: colour_split_rgb(colour & 0xffffff, &r, &g, &b);
! 2051: tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
! 2052: }
1.165 nicm 2053: return (0);
2054: }
1.61 nicm 2055:
1.165 nicm 2056: return (-1);
1.188 nicm 2057:
1.204 nicm 2058: fallback_256:
2059: xsnprintf(s, sizeof s, "\033[%s;5;%dm", type, colour & 0xff);
1.188 nicm 2060: tty_puts(tty, s);
2061: return (0);
1.176 nicm 2062: }
2063:
1.207 nicm 2064: static void
1.176 nicm 2065: tty_default_colours(struct grid_cell *gc, const struct window_pane *wp)
2066: {
1.203 nicm 2067: struct window *w = wp->window;
2068: struct options *oo = w->options;
2069: const struct grid_cell *agc, *pgc, *wgc;
1.223 nicm 2070: int c;
1.203 nicm 2071:
2072: if (w->flags & WINDOW_STYLECHANGED) {
2073: w->flags &= ~WINDOW_STYLECHANGED;
2074: agc = options_get_style(oo, "window-active-style");
2075: memcpy(&w->active_style, agc, sizeof w->active_style);
2076: wgc = options_get_style(oo, "window-style");
2077: memcpy(&w->style, wgc, sizeof w->style);
2078: } else {
2079: agc = &w->active_style;
2080: wgc = &w->style;
2081: }
1.176 nicm 2082: pgc = &wp->colgc;
2083:
1.204 nicm 2084: if (gc->fg == 8) {
2085: if (pgc->fg != 8)
1.176 nicm 2086: gc->fg = pgc->fg;
1.204 nicm 2087: else if (wp == w->active && agc->fg != 8)
1.176 nicm 2088: gc->fg = agc->fg;
1.204 nicm 2089: else
1.176 nicm 2090: gc->fg = wgc->fg;
1.219 nicm 2091:
1.223 nicm 2092: if (gc->fg != 8 &&
2093: (c = window_pane_get_palette(wp, gc->fg)) != -1)
2094: gc->fg = c;
1.176 nicm 2095: }
2096:
1.204 nicm 2097: if (gc->bg == 8) {
2098: if (pgc->bg != 8)
1.176 nicm 2099: gc->bg = pgc->bg;
1.204 nicm 2100: else if (wp == w->active && agc->bg != 8)
1.176 nicm 2101: gc->bg = agc->bg;
1.204 nicm 2102: else
1.176 nicm 2103: gc->bg = wgc->bg;
1.219 nicm 2104:
1.223 nicm 2105: if (gc->bg != 8 &&
2106: (c = window_pane_get_palette(wp, gc->bg)) != -1)
2107: gc->bg = c;
1.176 nicm 2108: }
1.210 nicm 2109: }
2110:
2111: static void
2112: tty_default_attributes(struct tty *tty, const struct window_pane *wp, u_int bg)
2113: {
2114: static struct grid_cell gc;
2115:
2116: memcpy(&gc, &grid_default_cell, sizeof gc);
2117: gc.bg = bg;
2118: tty_attributes(tty, &gc, wp);
1.1 nicm 2119: }