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