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