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