Annotation of src/usr.bin/tmux/tty.c, Revision 1.259
1.259 ! nicm 1: /* $OpenBSD: tty.c,v 1.258 2017/04/05 10:49:46 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;
1.259 ! nicm 696: int flags, cleared = 0;
1.250 nicm 697: char buf[512];
698: size_t len;
1.1 nicm 699:
1.259 ! nicm 700: flags = (tty->flags & TTY_NOCURSOR);
1.181 nicm 701: tty->flags |= TTY_NOCURSOR;
702: tty_update_mode(tty, tty->mode, s);
1.35 nicm 703:
1.214 nicm 704: tty_region_off(tty);
705: tty_margin_off(tty);
706:
1.1 nicm 707: sx = screen_size_x(s);
1.250 nicm 708: if (sx > s->grid->linedata[s->grid->hsize + py].cellused)
709: sx = s->grid->linedata[s->grid->hsize + py].cellused;
1.1 nicm 710: if (sx > tty->sx)
711: sx = tty->sx;
712:
1.259 ! nicm 713: if (screen_size_x(s) < tty->sx &&
! 714: ox == 0 &&
! 715: sx != screen_size_x(s) &&
! 716: tty_term_has(tty->term, TTYC_EL1) &&
! 717: !tty_fake_bce(tty, wp, 8)) {
! 718: tty_default_attributes(tty, wp, 8);
! 719: tty_cursor(tty, screen_size_x(s) - 1, oy + py);
! 720: tty_putcode(tty, TTYC_EL1);
! 721: cleared = 1;
! 722: }
1.250 nicm 723: tty_cursor(tty, ox, oy + py);
724:
725: memcpy(&last, &grid_default_cell, sizeof last);
726: len = 0;
727: width = 0;
1.46 nicm 728:
1.1 nicm 729: for (i = 0; i < sx; i++) {
1.193 nicm 730: grid_view_get_cell(s->grid, i, py, &gc);
1.250 nicm 731: if (len != 0 &&
1.251 nicm 732: (((~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: gc.flags != last.flags ||
738: gc.attr != last.attr ||
739: gc.fg != last.fg ||
740: gc.bg != last.bg ||
741: (sizeof buf) - len < gc.data.size)) {
742: tty_attributes(tty, &last, wp);
743: tty_putn(tty, buf, len, width);
744:
745: len = 0;
746: width = 0;
747: }
748:
749: if (gc.flags & GRID_FLAG_SELECTED)
750: screen_select_cell(s, &last, &gc);
751: else
752: memcpy(&last, &gc, sizeof last);
1.251 nicm 753: if (((~tty->flags & TTY_UTF8) &&
754: (gc.data.size != 1 ||
755: *gc.data.data >= 0x7f ||
756: gc.data.width != 1)) ||
757: (gc.attr & GRID_ATTR_CHARSET)) {
1.250 nicm 758: tty_attributes(tty, &last, wp);
1.251 nicm 759: if (~tty->flags & TTY_UTF8) {
760: for (j = 0; j < gc.data.width; j++)
761: tty_putc(tty, '_');
762: } else {
763: for (j = 0; j < gc.data.size; j++)
764: tty_putc(tty, gc.data.data[j]);
765: }
1.250 nicm 766: } else {
767: memcpy(buf + len, gc.data.data, gc.data.size);
768: len += gc.data.size;
769: width += gc.data.width;
770: }
771: }
772: if (len != 0) {
773: tty_attributes(tty, &last, wp);
774: tty_putn(tty, buf, len, width);
1.1 nicm 775: }
776:
1.259 ! nicm 777: if (!cleared && sx < tty->sx) {
1.210 nicm 778: tty_default_attributes(tty, wp, 8);
1.181 nicm 779:
780: tty_cursor(tty, ox + sx, oy + py);
781: if (sx != screen_size_x(s) &&
782: ox + screen_size_x(s) >= tty->sx &&
783: tty_term_has(tty->term, TTYC_EL) &&
1.210 nicm 784: !tty_fake_bce(tty, wp, 8))
1.181 nicm 785: tty_putcode(tty, TTYC_EL);
786: else
787: tty_repeat_space(tty, screen_size_x(s) - sx);
1.35 nicm 788: }
1.1 nicm 789:
1.181 nicm 790: tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107 nicm 791: tty_update_mode(tty, tty->mode, s);
1.15 nicm 792: }
793:
1.201 nicm 794: static int
1.182 nicm 795: tty_client_ready(struct client *c, struct window_pane *wp)
796: {
797: if (c->session == NULL || c->tty.term == NULL)
798: return (0);
1.244 nicm 799: if (c->flags & (CLIENT_REDRAW|CLIENT_SUSPENDED))
1.182 nicm 800: return (0);
801: if (c->tty.flags & TTY_FREEZE)
802: return (0);
803: if (c->session->curw->window != wp->window)
804: return (0);
805: return (1);
806: }
807:
1.15 nicm 808: void
1.182 nicm 809: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
810: struct tty_ctx *ctx)
1.15 nicm 811: {
812: struct window_pane *wp = ctx->wp;
813: struct client *c;
814:
1.75 nicm 815: /* wp can be NULL if updating the screen but not the terminal. */
1.15 nicm 816: if (wp == NULL)
817: return;
818:
819: if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
820: return;
1.129 nicm 821: if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
1.15 nicm 822: return;
823:
1.178 nicm 824: TAILQ_FOREACH(c, &clients, entry) {
1.182 nicm 825: if (!tty_client_ready(c, wp))
1.15 nicm 826: continue;
827:
1.139 nicm 828: ctx->xoff = wp->xoff;
829: ctx->yoff = wp->yoff;
830: if (status_at_line(c) == 0)
831: ctx->yoff++;
1.113 nicm 832:
1.139 nicm 833: cmdfn(&c->tty, ctx);
1.1 nicm 834: }
835: }
836:
837: void
1.24 nicm 838: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 839: {
1.77 nicm 840: struct window_pane *wp = ctx->wp;
1.1 nicm 841:
1.210 nicm 842: if (!tty_pane_full_width(tty, ctx) ||
843: tty_fake_bce(tty, wp, ctx->bg) ||
844: (!tty_term_has(tty->term, TTYC_ICH) &&
845: !tty_term_has(tty->term, TTYC_ICH1))) {
1.176 nicm 846: tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 847: return;
848: }
849:
1.210 nicm 850: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 851:
1.77 nicm 852: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 853:
1.206 nicm 854: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1 nicm 855: }
856:
857: void
1.24 nicm 858: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 859: {
1.77 nicm 860: struct window_pane *wp = ctx->wp;
1.1 nicm 861:
1.210 nicm 862: if (!tty_pane_full_width(tty, ctx) ||
863: tty_fake_bce(tty, wp, ctx->bg) ||
1.26 nicm 864: (!tty_term_has(tty->term, TTYC_DCH) &&
865: !tty_term_has(tty->term, TTYC_DCH1))) {
1.176 nicm 866: tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 867: return;
868: }
869:
1.210 nicm 870: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 871:
1.77 nicm 872: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 873:
1.206 nicm 874: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146 nicm 875: }
876:
877: void
878: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
879: {
1.176 nicm 880: tty_attributes(tty, &grid_default_cell, ctx->wp);
1.146 nicm 881:
882: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
883:
1.210 nicm 884: if (tty_term_has(tty->term, TTYC_ECH) &&
885: !tty_fake_bce(tty, ctx->wp, ctx->bg))
1.146 nicm 886: tty_putcode1(tty, TTYC_ECH, ctx->num);
1.257 nicm 887: else
888: tty_repeat_space(tty, ctx->num);
1.1 nicm 889: }
890:
891: void
1.24 nicm 892: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 893: {
1.210 nicm 894: if (!tty_pane_full_width(tty, ctx) ||
895: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.77 nicm 896: !tty_term_has(tty->term, TTYC_CSR) ||
1.72 nicm 897: !tty_term_has(tty->term, TTYC_IL1)) {
1.14 nicm 898: tty_redraw_region(tty, ctx);
1.1 nicm 899: return;
900: }
901:
1.210 nicm 902: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1 nicm 903:
1.77 nicm 904: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 905: tty_margin_off(tty);
1.77 nicm 906: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 907:
1.12 nicm 908: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1 nicm 909: }
910:
911: void
1.24 nicm 912: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 913: {
1.210 nicm 914: if (!tty_pane_full_width(tty, ctx) ||
915: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.72 nicm 916: !tty_term_has(tty->term, TTYC_CSR) ||
917: !tty_term_has(tty->term, TTYC_DL1)) {
1.14 nicm 918: tty_redraw_region(tty, ctx);
1.1 nicm 919: return;
920: }
921:
1.210 nicm 922: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1 nicm 923:
1.77 nicm 924: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 925: tty_margin_off(tty);
1.77 nicm 926: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 927:
1.12 nicm 928: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1 nicm 929: }
930:
931: void
1.24 nicm 932: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 933: {
1.77 nicm 934: struct window_pane *wp = ctx->wp;
1.12 nicm 935: struct screen *s = wp->screen;
1.229 nicm 936: u_int sx = screen_size_x(s);
1.1 nicm 937:
1.210 nicm 938: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 939:
1.77 nicm 940: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41 nicm 941:
1.210 nicm 942: if (tty_pane_full_width(tty, ctx) &&
943: !tty_fake_bce(tty, wp, ctx->bg) &&
1.176 nicm 944: tty_term_has(tty->term, TTYC_EL))
1.1 nicm 945: tty_putcode(tty, TTYC_EL);
1.133 nicm 946: else
1.229 nicm 947: tty_repeat_space(tty, sx);
1.1 nicm 948: }
949:
950: void
1.24 nicm 951: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 952: {
1.77 nicm 953: struct window_pane *wp = ctx->wp;
1.12 nicm 954: struct screen *s = wp->screen;
1.229 nicm 955: u_int sx = screen_size_x(s);
1.1 nicm 956:
1.210 nicm 957: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 958:
1.41 nicm 959: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
960:
1.176 nicm 961: if (tty_pane_full_width(tty, ctx) &&
1.210 nicm 962: tty_term_has(tty->term, TTYC_EL) &&
963: !tty_fake_bce(tty, wp, ctx->bg))
1.1 nicm 964: tty_putcode(tty, TTYC_EL);
1.133 nicm 965: else
1.229 nicm 966: tty_repeat_space(tty, sx - ctx->ocx);
1.1 nicm 967: }
968:
969: void
1.24 nicm 970: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 971: {
1.210 nicm 972: struct window_pane *wp = ctx->wp;
973:
974: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 975:
1.210 nicm 976: if (ctx->xoff == 0 &&
977: tty_term_has(tty->term, TTYC_EL1) &&
1.238 nicm 978: !tty_fake_bce(tty, ctx->wp, ctx->bg)) {
979: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 980: tty_putcode(tty, TTYC_EL1);
1.238 nicm 981: } else {
982: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133 nicm 983: tty_repeat_space(tty, ctx->ocx + 1);
1.238 nicm 984: }
1.1 nicm 985: }
986:
987: void
1.24 nicm 988: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 989: {
1.56 nicm 990: if (ctx->ocy != ctx->orupper)
991: return;
992:
1.210 nicm 993: if (!tty_pane_full_width(tty, ctx) ||
994: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.70 nicm 995: !tty_term_has(tty->term, TTYC_CSR) ||
996: !tty_term_has(tty->term, TTYC_RI)) {
1.14 nicm 997: tty_redraw_region(tty, ctx);
1.1 nicm 998: return;
999: }
1000:
1.176 nicm 1001: tty_attributes(tty, &grid_default_cell, ctx->wp);
1.77 nicm 1002:
1.56 nicm 1003: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1004: tty_margin_off(tty);
1.56 nicm 1005: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 1006:
1.56 nicm 1007: tty_putcode(tty, TTYC_RI);
1.1 nicm 1008: }
1009:
1010: void
1.24 nicm 1011: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1012: {
1.77 nicm 1013: struct window_pane *wp = ctx->wp;
1.1 nicm 1014:
1.56 nicm 1015: if (ctx->ocy != ctx->orlower)
1016: return;
1017:
1.212 nicm 1018: if ((!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.210 nicm 1019: tty_fake_bce(tty, wp, ctx->bg) ||
1.1 nicm 1020: !tty_term_has(tty->term, TTYC_CSR)) {
1.236 nicm 1021: tty_redraw_region(tty, ctx);
1.1 nicm 1022: return;
1023: }
1.52 nicm 1024:
1.176 nicm 1025: tty_attributes(tty, &grid_default_cell, wp);
1.77 nicm 1026:
1.56 nicm 1027: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.212 nicm 1028: tty_margin_pane(tty, ctx);
1029:
1030: /*
1031: * If we want to wrap a pane, the cursor needs to be exactly on the
1032: * right of the region. But if the pane isn't on the right, it may be
1033: * off the edge - if so, move the cursor back to the right.
1034: */
1035: if (ctx->xoff + ctx->ocx > tty->rright)
1.216 nicm 1036: tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1.212 nicm 1037: else
1038: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 1039:
1.56 nicm 1040: tty_putc(tty, '\n');
1.240 nicm 1041: }
1042:
1043: void
1044: tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1045: {
1046: struct window_pane *wp = ctx->wp;
1047: u_int i;
1048:
1049: if ((!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1050: tty_fake_bce(tty, wp, ctx->bg) ||
1051: !tty_term_has(tty->term, TTYC_CSR)) {
1052: tty_redraw_region(tty, ctx);
1053: return;
1054: }
1055:
1056: tty_attributes(tty, &grid_default_cell, wp);
1057:
1058: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1059: tty_margin_pane(tty, ctx);
1060:
1061: if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1.241 nicm 1062: tty_cursor(tty, tty->rright, tty->rlower);
1.240 nicm 1063: for (i = 0; i < ctx->num; i++)
1064: tty_putc(tty, '\n');
1065: } else
1066: tty_putcode1(tty, TTYC_INDN, ctx->num);
1.1 nicm 1067: }
1068:
1069: void
1.24 nicm 1070: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1071: {
1.77 nicm 1072: struct window_pane *wp = ctx->wp;
1.12 nicm 1073: struct screen *s = wp->screen;
1.204 nicm 1074: u_int i, j;
1.229 nicm 1075: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 1076:
1.210 nicm 1077: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1078:
1.229 nicm 1079: tty_region_pane(tty, ctx, 0, sy - 1);
1.214 nicm 1080: tty_margin_off(tty);
1.39 nicm 1081:
1.176 nicm 1082: if (tty_pane_full_width(tty, ctx) &&
1.248 nicm 1083: ctx->yoff + wp->sy >= tty->sy - 1 &&
1.229 nicm 1084: status_at_line(tty->client) <= 0 &&
1085: tty_term_has(tty->term, TTYC_ED)) {
1086: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1087: tty_putcode(tty, TTYC_ED);
1088: } else if (tty_pane_full_width(tty, ctx) &&
1.210 nicm 1089: tty_term_has(tty->term, TTYC_EL) &&
1090: !tty_fake_bce(tty, wp, ctx->bg)) {
1.229 nicm 1091: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1092: tty_putcode(tty, TTYC_EL);
1.229 nicm 1093: if (ctx->ocy != sy - 1) {
1.41 nicm 1094: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.229 nicm 1095: for (i = ctx->ocy + 1; i < sy; i++) {
1.1 nicm 1096: tty_putcode(tty, TTYC_EL);
1.229 nicm 1097: if (i == sy - 1)
1.1 nicm 1098: continue;
1099: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
1100: tty->cy++;
1101: }
1102: }
1103: } else {
1.229 nicm 1104: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1105: tty_repeat_space(tty, sx - ctx->ocx);
1106: for (j = ctx->ocy + 1; j < sy; j++) {
1.41 nicm 1107: tty_cursor_pane(tty, ctx, 0, j);
1.229 nicm 1108: tty_repeat_space(tty, sx);
1.1 nicm 1109: }
1110: }
1111: }
1112:
1113: void
1.24 nicm 1114: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1115: {
1.77 nicm 1116: struct window_pane *wp = ctx->wp;
1.12 nicm 1117: struct screen *s = wp->screen;
1.204 nicm 1118: u_int i, j;
1.229 nicm 1119: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 1120:
1.227 nicm 1121: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1122:
1.229 nicm 1123: tty_region_pane(tty, ctx, 0, sy - 1);
1.214 nicm 1124: tty_margin_off(tty);
1.39 nicm 1125:
1.230 nicm 1126: if (tty_pane_full_width(tty, ctx) &&
1.210 nicm 1127: tty_term_has(tty->term, TTYC_EL) &&
1128: !tty_fake_bce(tty, wp, ctx->bg)) {
1.229 nicm 1129: tty_cursor_pane(tty, ctx, 0, 0);
1.14 nicm 1130: for (i = 0; i < ctx->ocy; i++) {
1.1 nicm 1131: tty_putcode(tty, TTYC_EL);
1132: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
1133: tty->cy++;
1134: }
1135: } else {
1.229 nicm 1136: tty_cursor_pane(tty, ctx, 0, 0);
1.14 nicm 1137: for (j = 0; j < ctx->ocy; j++) {
1.41 nicm 1138: tty_cursor_pane(tty, ctx, 0, j);
1.229 nicm 1139: tty_repeat_space(tty, sx);
1.1 nicm 1140: }
1141: }
1.229 nicm 1142: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133 nicm 1143: tty_repeat_space(tty, ctx->ocx + 1);
1.1 nicm 1144: }
1145:
1146: void
1.24 nicm 1147: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1148: {
1.77 nicm 1149: struct window_pane *wp = ctx->wp;
1.12 nicm 1150: struct screen *s = wp->screen;
1.204 nicm 1151: u_int i, j;
1.229 nicm 1152: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 1153:
1.210 nicm 1154: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1155:
1.229 nicm 1156: tty_region_pane(tty, ctx, 0, sy - 1);
1.214 nicm 1157: tty_margin_off(tty);
1.39 nicm 1158:
1.176 nicm 1159: if (tty_pane_full_width(tty, ctx) &&
1.229 nicm 1160: status_at_line(tty->client) <= 0 &&
1161: tty_term_has(tty->term, TTYC_ED)) {
1162: tty_cursor_pane(tty, ctx, 0, 0);
1163: tty_putcode(tty, TTYC_ED);
1.230 nicm 1164: } else if (tty_pane_full_width(tty, ctx) &&
1.210 nicm 1165: tty_term_has(tty->term, TTYC_EL) &&
1166: !tty_fake_bce(tty, wp, ctx->bg)) {
1.229 nicm 1167: tty_cursor_pane(tty, ctx, 0, 0);
1168: for (i = 0; i < sy; i++) {
1.1 nicm 1169: tty_putcode(tty, TTYC_EL);
1.229 nicm 1170: if (i != sy - 1) {
1.1 nicm 1171: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
1172: tty->cy++;
1173: }
1174: }
1175: } else {
1.229 nicm 1176: tty_cursor_pane(tty, ctx, 0, 0);
1177: for (j = 0; j < sy; j++) {
1.41 nicm 1178: tty_cursor_pane(tty, ctx, 0, j);
1.229 nicm 1179: tty_repeat_space(tty, sx);
1.1 nicm 1180: }
1.4 nicm 1181: }
1182: }
1183:
1184: void
1.24 nicm 1185: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 1186: {
1.12 nicm 1187: struct window_pane *wp = ctx->wp;
1188: struct screen *s = wp->screen;
1189: u_int i, j;
1.4 nicm 1190:
1.176 nicm 1191: tty_attributes(tty, &grid_default_cell, wp);
1.4 nicm 1192:
1.39 nicm 1193: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.214 nicm 1194: tty_margin_off(tty);
1.4 nicm 1195:
1196: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 1197: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 1198: for (i = 0; i < screen_size_x(s); i++)
1199: tty_putc(tty, 'E');
1.1 nicm 1200: }
1201: }
1202:
1203: void
1.24 nicm 1204: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1205: {
1.222 nicm 1206: if (ctx->xoff + ctx->ocx > tty->sx - 1 && ctx->ocy == ctx->orlower) {
1207: if (tty_pane_full_width(tty, ctx))
1208: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1209: else
1210: tty_margin_off(tty);
1211: }
1.46 nicm 1212:
1.253 nicm 1213: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1214:
1.237 nicm 1215: tty_cell(tty, ctx->cell, ctx->wp);
1.239 nicm 1216: }
1217:
1218: void
1219: tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
1220: {
1.253 nicm 1221: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.239 nicm 1222:
1223: tty_attributes(tty, ctx->cell, ctx->wp);
1224: tty_putn(tty, ctx->ptr, ctx->num, ctx->num);
1.106 nicm 1225: }
1226:
1227: void
1228: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1229: {
1230: char *buf;
1231: size_t off;
1232:
1233: if (!tty_term_has(tty->term, TTYC_MS))
1234: return;
1235:
1236: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1237: buf = xmalloc(off);
1238:
1239: b64_ntop(ctx->ptr, ctx->num, buf, off);
1240: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1241:
1.138 nicm 1242: free(buf);
1.100 nicm 1243: }
1244:
1245: void
1246: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1247: {
1.256 nicm 1248: tty_add(tty, ctx->ptr, ctx->num);
1.242 nicm 1249: tty_invalidate(tty);
1.1 nicm 1250: }
1251:
1.207 nicm 1252: static void
1.176 nicm 1253: tty_cell(struct tty *tty, const struct grid_cell *gc,
1254: const struct window_pane *wp)
1.1 nicm 1255: {
1.193 nicm 1256: u_int i;
1.1 nicm 1257:
1258: /* Skip last character if terminal is stupid. */
1.211 nicm 1259: if ((tty->term->flags & TERM_EARLYWRAP) &&
1260: tty->cy == tty->sy - 1 &&
1261: tty->cx == tty->sx - 1)
1.1 nicm 1262: return;
1263:
1264: /* If this is a padding character, do nothing. */
1265: if (gc->flags & GRID_FLAG_PADDING)
1266: return;
1267:
1268: /* Set the attributes. */
1.176 nicm 1269: tty_attributes(tty, gc, wp);
1.1 nicm 1270:
1.147 nicm 1271: /* Get the cell and if ASCII write with putc to do ACS translation. */
1.193 nicm 1272: if (gc->data.size == 1) {
1273: if (*gc->data.data < 0x20 || *gc->data.data == 0x7f)
1.1 nicm 1274: return;
1.193 nicm 1275: tty_putc(tty, *gc->data.data);
1.1 nicm 1276: return;
1277: }
1278:
1.147 nicm 1279: /* If not UTF-8, write _. */
1.1 nicm 1280: if (!(tty->flags & TTY_UTF8)) {
1.193 nicm 1281: for (i = 0; i < gc->data.width; i++)
1.1 nicm 1282: tty_putc(tty, '_');
1283: return;
1284: }
1285:
1.147 nicm 1286: /* Write the data. */
1.193 nicm 1287: tty_putn(tty, gc->data.data, gc->data.size, gc->data.width);
1.1 nicm 1288: }
1289:
1290: void
1291: tty_reset(struct tty *tty)
1292: {
1293: struct grid_cell *gc = &tty->cell;
1294:
1.228 nicm 1295: if (!grid_cells_equal(gc, &grid_default_cell)) {
1296: if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1297: tty_putcode(tty, TTYC_RMACS);
1298: tty_putcode(tty, TTYC_SGR0);
1299: memcpy(gc, &grid_default_cell, sizeof *gc);
1300: }
1.1 nicm 1301:
1.228 nicm 1302: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
1303: tty->last_wp = -1;
1.242 nicm 1304: }
1305:
1306: static void
1307: tty_invalidate(struct tty *tty)
1308: {
1309: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
1310:
1311: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
1312: tty->last_wp = -1;
1313:
1314: tty->cx = tty->cy = UINT_MAX;
1315:
1316: tty->rupper = tty->rleft = UINT_MAX;
1317: tty->rlower = tty->rright = UINT_MAX;
1318:
1319: if (tty->flags & TTY_STARTED) {
1320: tty_putcode(tty, TTYC_SGR0);
1321:
1322: tty->mode = ALL_MODES;
1323: tty_update_mode(tty, MODE_CURSOR, NULL);
1324:
1325: tty_cursor(tty, 0, 0);
1326: tty_region_off(tty);
1327: tty_margin_off(tty);
1328: } else
1329: tty->mode = MODE_CURSOR;
1.1 nicm 1330: }
1331:
1.214 nicm 1332: /* Turn off margin. */
1333: void
1334: tty_region_off(struct tty *tty)
1335: {
1336: tty_region(tty, 0, tty->sy - 1);
1337: }
1338:
1.40 nicm 1339: /* Set region inside pane. */
1.208 nicm 1340: static void
1.196 nicm 1341: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
1342: u_int rlower)
1.1 nicm 1343: {
1.113 nicm 1344: tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39 nicm 1345: }
1346:
1.40 nicm 1347: /* Set region at absolute position. */
1.214 nicm 1348: static void
1.40 nicm 1349: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 1350: {
1351: if (tty->rlower == rlower && tty->rupper == rupper)
1352: return;
1.1 nicm 1353: if (!tty_term_has(tty->term, TTYC_CSR))
1354: return;
1.39 nicm 1355:
1356: tty->rupper = rupper;
1357: tty->rlower = rlower;
1.55 nicm 1358:
1359: /*
1360: * Some terminals (such as PuTTY) do not correctly reset the cursor to
1361: * 0,0 if it is beyond the last column (they do not reset their wrap
1362: * flag so further output causes a line feed). As a workaround, do an
1363: * explicit move to 0 first.
1364: */
1365: if (tty->cx >= tty->sx)
1366: tty_cursor(tty, 0, tty->cy);
1.42 nicm 1367:
1.39 nicm 1368: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.249 nicm 1369: tty->cx = tty->cy = UINT_MAX;
1.212 nicm 1370: }
1371:
1.214 nicm 1372: /* Turn off margin. */
1373: void
1374: tty_margin_off(struct tty *tty)
1375: {
1376: tty_margin(tty, 0, tty->sx - 1);
1377: }
1378:
1.212 nicm 1379: /* Set margin inside pane. */
1380: static void
1381: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
1382: {
1383: tty_margin(tty, ctx->xoff, ctx->xoff + ctx->wp->sx - 1);
1384: }
1385:
1386: /* Set margin at absolute position. */
1.214 nicm 1387: static void
1.212 nicm 1388: tty_margin(struct tty *tty, u_int rleft, u_int rright)
1389: {
1390: char s[64];
1391:
1392: if (!tty_use_margin(tty))
1393: return;
1394: if (tty->rleft == rleft && tty->rright == rright)
1395: return;
1396:
1.232 nicm 1397: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.231 nicm 1398:
1.212 nicm 1399: tty->rleft = rleft;
1400: tty->rright = rright;
1401:
1.232 nicm 1402: if (rleft == 0 && rright == tty->sx - 1)
1403: snprintf(s, sizeof s, "\033[s");
1404: else
1405: snprintf(s, sizeof s, "\033[%u;%us", rleft + 1, rright + 1);
1.212 nicm 1406: tty_puts(tty, s);
1.249 nicm 1407: tty->cx = tty->cy = UINT_MAX;
1.253 nicm 1408: }
1409:
1410: /*
1411: * Move the cursor, unless it would wrap itself when the next character is
1412: * printed.
1413: */
1414: static void
1415: tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
1416: u_int cx, u_int cy)
1417: {
1418: if (!tty_pane_full_width(tty, ctx) ||
1419: (tty->term->flags & TERM_EARLYWRAP) ||
1420: ctx->xoff + cx != 0 ||
1421: ctx->yoff + cy != tty->cy + 1 ||
1.254 nicm 1422: tty->cx < tty->sx ||
1423: tty->cy == tty->rlower)
1.253 nicm 1424: tty_cursor_pane(tty, ctx, cx, cy);
1425: else
1426: log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
1.1 nicm 1427: }
1428:
1.42 nicm 1429: /* Move cursor inside pane. */
1.208 nicm 1430: static void
1.41 nicm 1431: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1432: {
1.113 nicm 1433: tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41 nicm 1434: }
1435:
1.42 nicm 1436: /* Move cursor to absolute position. */
1.41 nicm 1437: void
1438: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 1439: {
1.42 nicm 1440: struct tty_term *term = tty->term;
1441: u_int thisx, thisy;
1442: int change;
1.77 nicm 1443:
1.42 nicm 1444: if (cx > tty->sx - 1)
1445: cx = tty->sx - 1;
1446:
1447: thisx = tty->cx;
1448: thisy = tty->cy;
1449:
1450: /* No change. */
1451: if (cx == thisx && cy == thisy)
1452: return;
1453:
1.43 nicm 1454: /* Very end of the line, just use absolute movement. */
1455: if (thisx > tty->sx - 1)
1456: goto absolute;
1457:
1.42 nicm 1458: /* Move to home position (0, 0). */
1459: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1460: tty_putcode(tty, TTYC_HOME);
1461: goto out;
1462: }
1463:
1464: /* Zero on the next line. */
1.48 nicm 1465: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1 nicm 1466: tty_putc(tty, '\r');
1.42 nicm 1467: tty_putc(tty, '\n');
1468: goto out;
1469: }
1470:
1.45 nicm 1471: /* Moving column or row. */
1.42 nicm 1472: if (cy == thisy) {
1.45 nicm 1473: /*
1474: * Moving column only, row staying the same.
1475: */
1476:
1.42 nicm 1477: /* To left edge. */
1.202 nicm 1478: if (cx == 0) {
1.42 nicm 1479: tty_putc(tty, '\r');
1480: goto out;
1481: }
1482:
1483: /* One to the left. */
1484: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1485: tty_putcode(tty, TTYC_CUB1);
1486: goto out;
1487: }
1488:
1489: /* One to the right. */
1490: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1491: tty_putcode(tty, TTYC_CUF1);
1492: goto out;
1493: }
1494:
1495: /* Calculate difference. */
1496: change = thisx - cx; /* +ve left, -ve right */
1497:
1498: /*
1499: * Use HPA if change is larger than absolute, otherwise move
1500: * the cursor with CUB/CUF.
1501: */
1.87 nicm 1502: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 1503: tty_putcode1(tty, TTYC_HPA, cx);
1504: goto out;
1505: } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1.202 nicm 1506: if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
1507: tty_putcode(tty, TTYC_CUB1);
1508: tty_putcode(tty, TTYC_CUB1);
1509: goto out;
1510: }
1.42 nicm 1511: tty_putcode1(tty, TTYC_CUB, change);
1512: goto out;
1513: } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1514: tty_putcode1(tty, TTYC_CUF, -change);
1515: goto out;
1516: }
1.45 nicm 1517: } else if (cx == thisx) {
1518: /*
1519: * Moving row only, column staying the same.
1520: */
1.42 nicm 1521:
1522: /* One above. */
1.77 nicm 1523: if (thisy != tty->rupper &&
1.42 nicm 1524: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1525: tty_putcode(tty, TTYC_CUU1);
1526: goto out;
1527: }
1528:
1529: /* One below. */
1.49 nicm 1530: if (thisy != tty->rlower &&
1.42 nicm 1531: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1532: tty_putcode(tty, TTYC_CUD1);
1533: goto out;
1534: }
1535:
1536: /* Calculate difference. */
1537: change = thisy - cy; /* +ve up, -ve down */
1538:
1539: /*
1.77 nicm 1540: * Try to use VPA if change is larger than absolute or if this
1541: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 1542: */
1.87 nicm 1543: if ((u_int) abs(change) > cy ||
1.42 nicm 1544: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 1545: (change > 0 && cy - change < tty->rupper)) {
1546: if (tty_term_has(term, TTYC_VPA)) {
1547: tty_putcode1(tty, TTYC_VPA, cy);
1548: goto out;
1549: }
1.42 nicm 1550: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1551: tty_putcode1(tty, TTYC_CUU, change);
1552: goto out;
1553: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1554: tty_putcode1(tty, TTYC_CUD, -change);
1555: goto out;
1556: }
1557: }
1558:
1.43 nicm 1559: absolute:
1.42 nicm 1560: /* Absolute movement. */
1561: tty_putcode2(tty, TTYC_CUP, cy, cx);
1562:
1563: out:
1564: tty->cx = cx;
1565: tty->cy = cy;
1.1 nicm 1566: }
1567:
1568: void
1.176 nicm 1569: tty_attributes(struct tty *tty, const struct grid_cell *gc,
1570: const struct window_pane *wp)
1.1 nicm 1571: {
1.63 nicm 1572: struct grid_cell *tc = &tty->cell, gc2;
1.255 nicm 1573: int changed;
1.61 nicm 1574:
1.228 nicm 1575: /* Ignore cell if it is the same as the last one. */
1576: if (wp != NULL &&
1577: (int)wp->id == tty->last_wp &&
1578: ~(wp->window->flags & WINDOW_STYLECHANGED) &&
1579: gc->attr == tty->last_cell.attr &&
1580: gc->fg == tty->last_cell.fg &&
1581: gc->bg == tty->last_cell.bg)
1582: return;
1583: tty->last_wp = (wp != NULL ? (int)wp->id : -1);
1584: memcpy(&tty->last_cell, gc, sizeof tty->last_cell);
1585:
1586: /* Copy cell and update default colours. */
1.85 nicm 1587: memcpy(&gc2, gc, sizeof gc2);
1.203 nicm 1588: if (wp != NULL)
1589: tty_default_colours(&gc2, wp);
1.63 nicm 1590:
1.18 nicm 1591: /*
1592: * If no setab, try to use the reverse attribute as a best-effort for a
1593: * non-default background. This is a bit of a hack but it doesn't do
1594: * any serious harm and makes a couple of applications happier.
1595: */
1596: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 1597: if (gc2.attr & GRID_ATTR_REVERSE) {
1598: if (gc2.fg != 7 && gc2.fg != 8)
1.64 nicm 1599: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 1600: } else {
1.85 nicm 1601: if (gc2.bg != 0 && gc2.bg != 8)
1.64 nicm 1602: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 1603: }
1604: }
1.1 nicm 1605:
1.85 nicm 1606: /* Fix up the colours if necessary. */
1.219 nicm 1607: tty_check_fg(tty, wp, &gc2);
1608: tty_check_bg(tty, wp, &gc2);
1.85 nicm 1609:
1.64 nicm 1610: /* If any bits are being cleared, reset everything. */
1.85 nicm 1611: if (tc->attr & ~gc2.attr)
1.64 nicm 1612: tty_reset(tty);
1613:
1614: /*
1615: * Set the colours. This may call tty_reset() (so it comes next) and
1616: * may add to (NOT remove) the desired attributes by changing new_attr.
1617: */
1.85 nicm 1618: tty_colours(tty, &gc2);
1.64 nicm 1619:
1.1 nicm 1620: /* Filter out attribute bits already set. */
1.85 nicm 1621: changed = gc2.attr & ~tc->attr;
1622: tc->attr = gc2.attr;
1.1 nicm 1623:
1624: /* Set the attributes. */
1625: if (changed & GRID_ATTR_BRIGHT)
1626: tty_putcode(tty, TTYC_BOLD);
1627: if (changed & GRID_ATTR_DIM)
1628: tty_putcode(tty, TTYC_DIM);
1.180 nicm 1629: if (changed & GRID_ATTR_ITALICS)
1630: tty_set_italics(tty);
1.1 nicm 1631: if (changed & GRID_ATTR_UNDERSCORE)
1632: tty_putcode(tty, TTYC_SMUL);
1633: if (changed & GRID_ATTR_BLINK)
1634: tty_putcode(tty, TTYC_BLINK);
1635: if (changed & GRID_ATTR_REVERSE) {
1636: if (tty_term_has(tty->term, TTYC_REV))
1637: tty_putcode(tty, TTYC_REV);
1638: else if (tty_term_has(tty->term, TTYC_SMSO))
1639: tty_putcode(tty, TTYC_SMSO);
1640: }
1641: if (changed & GRID_ATTR_HIDDEN)
1642: tty_putcode(tty, TTYC_INVIS);
1.255 nicm 1643: if (changed & GRID_ATTR_STRIKETHROUGH)
1644: tty_putcode(tty, TTYC_SMXX);
1.90 nicm 1645: if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1646: tty_putcode(tty, TTYC_SMACS);
1647: }
1648:
1.207 nicm 1649: static void
1.85 nicm 1650: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1651: {
1.61 nicm 1652: struct grid_cell *tc = &tty->cell;
1.204 nicm 1653: int have_ax;
1.61 nicm 1654:
1655: /* No changes? Nothing is necessary. */
1.204 nicm 1656: if (gc->fg == tc->fg && gc->bg == tc->bg)
1.63 nicm 1657: return;
1.1 nicm 1658:
1.61 nicm 1659: /*
1660: * Is either the default colour? This is handled specially because the
1661: * best solution might be to reset both colours to default, in which
1662: * case if only one is default need to fall onward to set the other
1663: * colour.
1664: */
1.204 nicm 1665: if (gc->fg == 8 || gc->bg == 8) {
1.61 nicm 1666: /*
1667: * If don't have AX but do have op, send sgr0 (op can't
1668: * actually be used because it is sometimes the same as sgr0
1669: * and sometimes isn't). This resets both colours to default.
1670: *
1671: * Otherwise, try to set the default colour only as needed.
1672: */
1.174 nicm 1673: have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61 nicm 1674: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1675: tty_reset(tty);
1676: else {
1.204 nicm 1677: if (gc->fg == 8 && tc->fg != 8) {
1.61 nicm 1678: if (have_ax)
1679: tty_puts(tty, "\033[39m");
1.204 nicm 1680: else if (tc->fg != 7)
1.61 nicm 1681: tty_putcode1(tty, TTYC_SETAF, 7);
1682: tc->fg = 8;
1683: }
1.204 nicm 1684: if (gc->bg == 8 && tc->bg != 8) {
1.77 nicm 1685: if (have_ax)
1.61 nicm 1686: tty_puts(tty, "\033[49m");
1.204 nicm 1687: else if (tc->bg != 0)
1.61 nicm 1688: tty_putcode1(tty, TTYC_SETAB, 0);
1689: tc->bg = 8;
1690: }
1691: }
1692: }
1.1 nicm 1693:
1.61 nicm 1694: /* Set the foreground colour. */
1.204 nicm 1695: if (gc->fg != 8 && gc->fg != tc->fg)
1.85 nicm 1696: tty_colours_fg(tty, gc);
1.1 nicm 1697:
1.61 nicm 1698: /*
1699: * Set the background colour. This must come after the foreground as
1700: * tty_colour_fg() can call tty_reset().
1701: */
1.204 nicm 1702: if (gc->bg != 8 && gc->bg != tc->bg)
1.73 nicm 1703: tty_colours_bg(tty, gc);
1.1 nicm 1704: }
1705:
1.219 nicm 1706: static void
1707: tty_check_fg(struct tty *tty, const struct window_pane *wp,
1708: struct grid_cell *gc)
1.85 nicm 1709: {
1.204 nicm 1710: u_char r, g, b;
1711: u_int colours;
1.223 nicm 1712: int c;
1.85 nicm 1713:
1.219 nicm 1714: /* Perform substitution if this pane has a palette */
1715: if ((~gc->flags & GRID_FLAG_NOPALETTE) &&
1.223 nicm 1716: (c = window_pane_get_palette(wp, gc->fg)) != -1)
1717: gc->fg = c;
1.219 nicm 1718:
1.199 nicm 1719: /* Is this a 24-bit colour? */
1.204 nicm 1720: if (gc->fg & COLOUR_FLAG_RGB) {
1.199 nicm 1721: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1722: if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204 nicm 1723: colour_split_rgb(gc->fg, &r, &g, &b);
1724: gc->fg = colour_find_rgb(r, g, b);
1725: } else
1.200 nicm 1726: return;
1.199 nicm 1727: }
1.224 nicm 1728:
1729: /* How many colours does this terminal have? */
1730: if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
1731: colours = 256;
1732: else
1733: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 1734:
1.85 nicm 1735: /* Is this a 256-colour colour? */
1.204 nicm 1736: if (gc->fg & COLOUR_FLAG_256) {
1.85 nicm 1737: /* And not a 256 colour mode? */
1.224 nicm 1738: if (colours != 256) {
1.85 nicm 1739: gc->fg = colour_256to16(gc->fg);
1740: if (gc->fg & 8) {
1741: gc->fg &= 7;
1.175 nicm 1742: if (colours >= 16)
1743: gc->fg += 90;
1744: else
1745: gc->attr |= GRID_ATTR_BRIGHT;
1.85 nicm 1746: } else
1747: gc->attr &= ~GRID_ATTR_BRIGHT;
1748: }
1749: return;
1750: }
1751:
1752: /* Is this an aixterm colour? */
1753: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1754: gc->fg -= 90;
1755: gc->attr |= GRID_ATTR_BRIGHT;
1756: }
1757: }
1758:
1.219 nicm 1759: static void
1760: tty_check_bg(struct tty *tty, const struct window_pane *wp,
1761: struct grid_cell *gc)
1.85 nicm 1762: {
1.204 nicm 1763: u_char r, g, b;
1764: u_int colours;
1.223 nicm 1765: int c;
1.85 nicm 1766:
1.219 nicm 1767: /* Perform substitution if this pane has a palette */
1768: if ((~gc->flags & GRID_FLAG_NOPALETTE) &&
1.223 nicm 1769: (c = window_pane_get_palette(wp, gc->bg)) != -1)
1770: gc->bg = c;
1.219 nicm 1771:
1.199 nicm 1772: /* Is this a 24-bit colour? */
1.204 nicm 1773: if (gc->bg & COLOUR_FLAG_RGB) {
1.199 nicm 1774: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1775: if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204 nicm 1776: colour_split_rgb(gc->bg, &r, &g, &b);
1777: gc->bg = colour_find_rgb(r, g, b);
1778: } else
1.200 nicm 1779: return;
1.199 nicm 1780: }
1.224 nicm 1781:
1782: /* How many colours does this terminal have? */
1783: if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
1784: colours = 256;
1785: else
1786: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 1787:
1.85 nicm 1788: /* Is this a 256-colour colour? */
1.204 nicm 1789: if (gc->bg & COLOUR_FLAG_256) {
1.85 nicm 1790: /*
1791: * And not a 256 colour mode? Translate to 16-colour
1792: * palette. Bold background doesn't exist portably, so just
1793: * discard the bold bit if set.
1794: */
1.224 nicm 1795: if (colours != 256) {
1.85 nicm 1796: gc->bg = colour_256to16(gc->bg);
1.175 nicm 1797: if (gc->bg & 8) {
1.85 nicm 1798: gc->bg &= 7;
1.175 nicm 1799: if (colours >= 16)
1800: gc->fg += 90;
1801: }
1.85 nicm 1802: }
1803: return;
1804: }
1805:
1806: /* Is this an aixterm colour? */
1.175 nicm 1807: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85 nicm 1808: gc->bg -= 90;
1809: }
1810:
1.207 nicm 1811: static void
1.85 nicm 1812: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1813: {
1.61 nicm 1814: struct grid_cell *tc = &tty->cell;
1.79 nicm 1815: char s[32];
1.1 nicm 1816:
1.204 nicm 1817: /* Is this a 24-bit or 256-colour colour? */
1818: if (gc->fg & COLOUR_FLAG_RGB ||
1819: gc->fg & COLOUR_FLAG_256) {
1820: if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61 nicm 1821: goto save_fg;
1.199 nicm 1822: /* Should not get here, already converted in tty_check_fg. */
1.85 nicm 1823: return;
1.1 nicm 1824: }
1825:
1.79 nicm 1826: /* Is this an aixterm bright colour? */
1.204 nicm 1827: if (gc->fg >= 90 && gc->fg <= 97) {
1828: xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
1.85 nicm 1829: tty_puts(tty, s);
1830: goto save_fg;
1.79 nicm 1831: }
1832:
1.61 nicm 1833: /* Otherwise set the foreground colour. */
1.204 nicm 1834: tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61 nicm 1835:
1.77 nicm 1836: save_fg:
1.61 nicm 1837: /* Save the new values in the terminal current cell. */
1.204 nicm 1838: tc->fg = gc->fg;
1.1 nicm 1839: }
1840:
1.207 nicm 1841: static void
1.73 nicm 1842: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1843: {
1.61 nicm 1844: struct grid_cell *tc = &tty->cell;
1.79 nicm 1845: char s[32];
1.1 nicm 1846:
1.204 nicm 1847: /* Is this a 24-bit or 256-colour colour? */
1848: if (gc->bg & COLOUR_FLAG_RGB ||
1849: gc->bg & COLOUR_FLAG_256) {
1850: if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61 nicm 1851: goto save_bg;
1.199 nicm 1852: /* Should not get here, already converted in tty_check_bg. */
1.85 nicm 1853: return;
1.79 nicm 1854: }
1855:
1856: /* Is this an aixterm bright colour? */
1.204 nicm 1857: if (gc->bg >= 90 && gc->bg <= 97) {
1858: xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
1.175 nicm 1859: tty_puts(tty, s);
1860: goto save_bg;
1.1 nicm 1861: }
1862:
1.61 nicm 1863: /* Otherwise set the background colour. */
1.204 nicm 1864: tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61 nicm 1865:
1866: save_bg:
1867: /* Save the new values in the terminal current cell. */
1.204 nicm 1868: tc->bg = gc->bg;
1.61 nicm 1869: }
1870:
1.207 nicm 1871: static int
1.204 nicm 1872: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61 nicm 1873: {
1.204 nicm 1874: u_char r, g, b;
1.61 nicm 1875: char s[32];
1876:
1.204 nicm 1877: if (colour & COLOUR_FLAG_256) {
1878: /*
1879: * If the user has specified -2 to the client, setaf and setab
1880: * may not work (or they may not want to use them), so send the
1881: * usual sequence.
1882: */
1883: if (tty->term_flags & TERM_256COLOURS)
1884: goto fallback_256;
1.189 nicm 1885:
1.204 nicm 1886: /*
1887: * If the terminfo entry has 256 colours and setaf and setab
1888: * exist, assume that they work correctly.
1889: */
1890: if (tty->term->flags & TERM_256COLOURS) {
1891: if (*type == '3') {
1892: if (!tty_term_has(tty->term, TTYC_SETAF))
1893: goto fallback_256;
1894: tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
1895: } else {
1896: if (!tty_term_has(tty->term, TTYC_SETAB))
1897: goto fallback_256;
1898: tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
1899: }
1900: return (0);
1.188 nicm 1901: }
1.204 nicm 1902: goto fallback_256;
1903: }
1904:
1905: if (colour & COLOUR_FLAG_RGB) {
1906: if (!tty_term_flag(tty->term, TTYC_TC))
1907: return (-1);
1908:
1909: colour_split_rgb(colour & 0xffffff, &r, &g, &b);
1910: xsnprintf(s, sizeof s, "\033[%s;2;%hhu;%hhu;%hhum", type,
1911: r, g, b);
1912: tty_puts(tty, s);
1.165 nicm 1913: return (0);
1914: }
1.61 nicm 1915:
1.165 nicm 1916: return (-1);
1.188 nicm 1917:
1.204 nicm 1918: fallback_256:
1919: xsnprintf(s, sizeof s, "\033[%s;5;%dm", type, colour & 0xff);
1.188 nicm 1920: tty_puts(tty, s);
1921: return (0);
1.176 nicm 1922: }
1923:
1.207 nicm 1924: static void
1.176 nicm 1925: tty_default_colours(struct grid_cell *gc, const struct window_pane *wp)
1926: {
1.203 nicm 1927: struct window *w = wp->window;
1928: struct options *oo = w->options;
1929: const struct grid_cell *agc, *pgc, *wgc;
1.223 nicm 1930: int c;
1.203 nicm 1931:
1932: if (w->flags & WINDOW_STYLECHANGED) {
1933: w->flags &= ~WINDOW_STYLECHANGED;
1934: agc = options_get_style(oo, "window-active-style");
1935: memcpy(&w->active_style, agc, sizeof w->active_style);
1936: wgc = options_get_style(oo, "window-style");
1937: memcpy(&w->style, wgc, sizeof w->style);
1938: } else {
1939: agc = &w->active_style;
1940: wgc = &w->style;
1941: }
1.176 nicm 1942: pgc = &wp->colgc;
1943:
1.204 nicm 1944: if (gc->fg == 8) {
1945: if (pgc->fg != 8)
1.176 nicm 1946: gc->fg = pgc->fg;
1.204 nicm 1947: else if (wp == w->active && agc->fg != 8)
1.176 nicm 1948: gc->fg = agc->fg;
1.204 nicm 1949: else
1.176 nicm 1950: gc->fg = wgc->fg;
1.219 nicm 1951:
1.223 nicm 1952: if (gc->fg != 8 &&
1953: (c = window_pane_get_palette(wp, gc->fg)) != -1)
1954: gc->fg = c;
1.176 nicm 1955: }
1956:
1.204 nicm 1957: if (gc->bg == 8) {
1958: if (pgc->bg != 8)
1.176 nicm 1959: gc->bg = pgc->bg;
1.204 nicm 1960: else if (wp == w->active && agc->bg != 8)
1.176 nicm 1961: gc->bg = agc->bg;
1.204 nicm 1962: else
1.176 nicm 1963: gc->bg = wgc->bg;
1.219 nicm 1964:
1.223 nicm 1965: if (gc->bg != 8 &&
1966: (c = window_pane_get_palette(wp, gc->bg)) != -1)
1967: gc->bg = c;
1.176 nicm 1968: }
1.210 nicm 1969: }
1970:
1971: static void
1972: tty_default_attributes(struct tty *tty, const struct window_pane *wp, u_int bg)
1973: {
1974: static struct grid_cell gc;
1975:
1976: memcpy(&gc, &grid_default_cell, sizeof gc);
1977: gc.bg = bg;
1978: tty_attributes(tty, &gc, wp);
1.1 nicm 1979: }