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