Annotation of src/usr.bin/tmux/tty.c, Revision 1.365
1.365 ! nicm 1: /* $OpenBSD: tty.c,v 1.364 2020/05/16 14:16:25 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.317 nicm 48: static void tty_check_fg(struct tty *, struct window_pane *,
1.219 nicm 49: struct grid_cell *);
1.317 nicm 50: static void tty_check_bg(struct tty *, struct window_pane *,
1.219 nicm 51: struct grid_cell *);
1.328 nicm 52: static void tty_check_us(struct tty *, struct window_pane *,
53: struct grid_cell *);
1.207 nicm 54: static void tty_colours_fg(struct tty *, const struct grid_cell *);
55: static void tty_colours_bg(struct tty *, const struct grid_cell *);
1.328 nicm 56: static void tty_colours_us(struct tty *, const struct grid_cell *);
1.207 nicm 57:
1.208 nicm 58: static void tty_region_pane(struct tty *, const struct tty_ctx *, u_int,
59: u_int);
1.214 nicm 60: static void tty_region(struct tty *, u_int, u_int);
1.212 nicm 61: static void tty_margin_pane(struct tty *, const struct tty_ctx *);
1.214 nicm 62: static void tty_margin(struct tty *, u_int, u_int);
1.207 nicm 63: static int tty_large_region(struct tty *, const struct tty_ctx *);
1.317 nicm 64: static int tty_fake_bce(const struct tty *, struct window_pane *, u_int);
1.207 nicm 65: static void tty_redraw_region(struct tty *, const struct tty_ctx *);
66: static void tty_emulate_repeat(struct tty *, enum tty_code_code,
67: enum tty_code_code, u_int);
68: static void tty_repeat_space(struct tty *, u_int);
1.309 nicm 69: static void tty_draw_pane(struct tty *, const struct tty_ctx *, u_int);
1.207 nicm 70: static void tty_cell(struct tty *, const struct grid_cell *,
1.317 nicm 71: struct window_pane *);
72: static void tty_default_colours(struct grid_cell *, struct window_pane *);
73: static void tty_default_attributes(struct tty *, struct window_pane *,
1.210 nicm 74: u_int);
1.1 nicm 75:
1.212 nicm 76: #define tty_use_margin(tty) \
1.359 nicm 77: (tty->term->flags & TERM_DECSLRM)
1.90 nicm 78:
1.264 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.266 nicm 82: #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
83: #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
84: #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
85:
1.192 nicm 86: void
87: tty_create_log(void)
88: {
89: char name[64];
90:
91: xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
92:
93: tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
94: if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
95: fatal("fcntl failed");
96: }
97:
1.185 nicm 98: int
1.359 nicm 99: tty_init(struct tty *tty, struct client *c, int fd)
1.1 nicm 100: {
1.185 nicm 101: if (!isatty(fd))
102: return (-1);
103:
1.30 nicm 104: memset(tty, 0, sizeof *tty);
1.22 nicm 105:
1.30 nicm 106: tty->fd = fd;
1.136 nicm 107: tty->client = c;
1.30 nicm 108:
1.108 nicm 109: tty->cstyle = 0;
1.107 nicm 110: tty->ccolour = xstrdup("");
1.30 nicm 111:
1.185 nicm 112: return (0);
1.31 nicm 113: }
114:
1.287 nicm 115: void
1.31 nicm 116: tty_resize(struct tty *tty)
117: {
1.258 nicm 118: struct client *c = tty->client;
119: struct winsize ws;
1.334 nicm 120: u_int sx, sy, xpixel, ypixel;
1.31 nicm 121:
122: if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88 nicm 123: sx = ws.ws_col;
1.334 nicm 124: if (sx == 0) {
1.88 nicm 125: sx = 80;
1.334 nicm 126: xpixel = 0;
127: } else
128: xpixel = ws.ws_xpixel / sx;
1.88 nicm 129: sy = ws.ws_row;
1.334 nicm 130: if (sy == 0) {
1.88 nicm 131: sy = 24;
1.334 nicm 132: ypixel = 0;
133: } else
134: ypixel = ws.ws_ypixel / sy;
1.88 nicm 135: } else {
136: sx = 80;
137: sy = 24;
1.334 nicm 138: xpixel = 0;
139: ypixel = 0;
1.31 nicm 140: }
1.334 nicm 141: log_debug("%s: %s now %ux%u (%ux%u)", __func__, c->name, sx, sy,
142: xpixel, ypixel);
143: tty_set_size(tty, sx, sy, xpixel, ypixel);
1.242 nicm 144: tty_invalidate(tty);
1.114 nicm 145: }
146:
1.287 nicm 147: void
1.334 nicm 148: tty_set_size(struct tty *tty, u_int sx, u_int sy, u_int xpixel, u_int ypixel)
1.212 nicm 149: {
1.114 nicm 150: tty->sx = sx;
151: tty->sy = sy;
1.334 nicm 152: tty->xpixel = xpixel;
153: tty->ypixel = ypixel;
1.1 nicm 154: }
155:
1.244 nicm 156: static void
157: tty_read_callback(__unused int fd, __unused short events, void *data)
158: {
159: struct tty *tty = data;
1.258 nicm 160: struct client *c = tty->client;
1.244 nicm 161: size_t size = EVBUFFER_LENGTH(tty->in);
162: int nread;
163:
164: nread = evbuffer_read(tty->in, tty->fd, -1);
1.295 nicm 165: if (nread == 0 || nread == -1) {
1.290 nicm 166: event_del(&tty->event_in);
1.295 nicm 167: server_client_lost(tty->client);
1.244 nicm 168: return;
1.290 nicm 169: }
1.258 nicm 170: log_debug("%s: read %d bytes (already %zu)", c->name, nread, size);
1.244 nicm 171:
172: while (tty_keys_next(tty))
173: ;
174: }
175:
176: static void
1.266 nicm 177: tty_timer_callback(__unused int fd, __unused short events, void *data)
178: {
179: struct tty *tty = data;
180: struct client *c = tty->client;
181: struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
182:
183: log_debug("%s: %zu discarded", c->name, tty->discarded);
184:
1.305 nicm 185: c->flags |= CLIENT_ALLREDRAWFLAGS;
1.266 nicm 186: c->discarded += tty->discarded;
187:
188: if (tty->discarded < TTY_BLOCK_STOP(tty)) {
189: tty->flags &= ~TTY_BLOCK;
190: tty_invalidate(tty);
191: return;
192: }
193: tty->discarded = 0;
194: evtimer_add(&tty->timer, &tv);
195: }
196:
197: static int
198: tty_block_maybe(struct tty *tty)
199: {
200: struct client *c = tty->client;
201: size_t size = EVBUFFER_LENGTH(tty->out);
202: struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
203:
204: if (size < TTY_BLOCK_START(tty))
205: return (0);
206:
207: if (tty->flags & TTY_BLOCK)
208: return (1);
209: tty->flags |= TTY_BLOCK;
210:
211: log_debug("%s: can't keep up, %zu discarded", c->name, size);
212:
213: evbuffer_drain(tty->out, size);
214: c->discarded += size;
215:
216: tty->discarded = 0;
217: evtimer_add(&tty->timer, &tv);
218: return (1);
219: }
220:
221: static void
1.244 nicm 222: tty_write_callback(__unused int fd, __unused short events, void *data)
223: {
1.246 nicm 224: struct tty *tty = data;
1.258 nicm 225: struct client *c = tty->client;
1.246 nicm 226: size_t size = EVBUFFER_LENGTH(tty->out);
227: int nwrite;
1.244 nicm 228:
229: nwrite = evbuffer_write(tty->out, tty->fd);
230: if (nwrite == -1)
231: return;
1.258 nicm 232: log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size);
1.244 nicm 233:
1.270 nicm 234: if (c->redraw > 0) {
235: if ((size_t)nwrite >= c->redraw)
236: c->redraw = 0;
237: else
238: c->redraw -= nwrite;
239: log_debug("%s: waiting for redraw, %zu bytes left", c->name,
240: c->redraw);
241: } else if (tty_block_maybe(tty))
1.266 nicm 242: return;
243:
1.247 nicm 244: if (EVBUFFER_LENGTH(tty->out) != 0)
245: event_add(&tty->event_out, NULL);
1.244 nicm 246: }
247:
1.1 nicm 248: int
1.166 nicm 249: tty_open(struct tty *tty, char **cause)
1.1 nicm 250: {
1.359 nicm 251: struct client *c = tty->client;
252:
253: tty->term = tty_term_create(tty, c->term_name, &c->term_features,
254: tty->fd, cause);
1.21 nicm 255: if (tty->term == NULL) {
256: tty_close(tty);
257: return (-1);
258: }
259: tty->flags |= TTY_OPENED;
1.1 nicm 260:
1.266 nicm 261: tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
1.244 nicm 262:
263: event_set(&tty->event_in, tty->fd, EV_PERSIST|EV_READ,
264: tty_read_callback, tty);
265: tty->in = evbuffer_new();
1.311 nicm 266: if (tty->in == NULL)
267: fatal("out of memory");
1.1 nicm 268:
1.244 nicm 269: event_set(&tty->event_out, tty->fd, EV_WRITE, tty_write_callback, tty);
270: tty->out = evbuffer_new();
1.311 nicm 271: if (tty->out == NULL)
272: fatal("out of memory");
1.1 nicm 273:
1.266 nicm 274: evtimer_set(&tty->timer, tty_timer_callback, tty);
275:
1.1 nicm 276: tty_start_tty(tty);
277:
1.148 nicm 278: tty_keys_build(tty);
1.1 nicm 279:
280: return (0);
281: }
282:
1.340 nicm 283: static void
284: tty_start_timer_callback(__unused int fd, __unused short events, void *data)
285: {
286: struct tty *tty = data;
287: struct client *c = tty->client;
288:
289: log_debug("%s: start timer fired", c->name);
1.364 nicm 290: tty->flags |= (TTY_HAVEDA|TTY_HAVEXDA);
1.340 nicm 291: }
292:
1.244 nicm 293: void
294: tty_start_tty(struct tty *tty)
1.1 nicm 295: {
1.285 nicm 296: struct client *c = tty->client;
297: struct termios tio;
1.340 nicm 298: struct timeval tv = { .tv_sec = 1 };
1.33 nicm 299:
1.244 nicm 300: if (tty->fd != -1 && tcgetattr(tty->fd, &tty->tio) == 0) {
301: setblocking(tty->fd, 0);
302: event_add(&tty->event_in, NULL);
303:
304: memcpy(&tio, &tty->tio, sizeof tio);
305: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
306: tio.c_iflag |= IGNBRK;
307: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
308: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
309: ECHOPRT|ECHOKE|ISIG);
310: tio.c_cc[VMIN] = 1;
311: tio.c_cc[VTIME] = 0;
312: if (tcsetattr(tty->fd, TCSANOW, &tio) == 0)
313: tcflush(tty->fd, TCIOFLUSH);
314: }
1.1 nicm 315:
1.10 nicm 316: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 317:
1.252 nicm 318: tty_putcode(tty, TTYC_SMKX);
1.285 nicm 319: tty_putcode(tty, TTYC_CLEAR);
320:
321: if (tty_acs_needed(tty)) {
322: log_debug("%s: using capabilities for ACS", c->name);
1.90 nicm 323: tty_putcode(tty, TTYC_ENACS);
1.285 nicm 324: } else
325: log_debug("%s: using UTF-8 for ACS", c->name);
1.1 nicm 326:
327: tty_putcode(tty, TTYC_CNORM);
1.343 nicm 328: if (tty_term_has(tty->term, TTYC_KMOUS)) {
329: tty_puts(tty, "\033[?1000l\033[?1002l\033[?1003l");
330: tty_puts(tty, "\033[?1006l\033[?1005l");
331: }
1.121 nicm 332:
1.189 nicm 333: if (tty_term_flag(tty->term, TTYC_XT)) {
1.191 nicm 334: if (options_get_number(global_options, "focus-events")) {
1.162 nicm 335: tty->flags |= TTY_FOCUS;
336: tty_puts(tty, "\033[?1004h");
337: }
1.352 nicm 338: tty_puts(tty, "\033[?7727h");
1.347 nicm 339: }
1.1 nicm 340:
1.340 nicm 341: evtimer_set(&tty->start_timer, tty_start_timer_callback, tty);
342: evtimer_add(&tty->start_timer, &tv);
343:
1.20 nicm 344: tty->flags |= TTY_STARTED;
1.242 nicm 345: tty_invalidate(tty);
1.107 nicm 346:
1.336 nicm 347: if (*tty->ccolour != '\0')
348: tty_force_cursor_colour(tty, "");
1.177 nicm 349:
350: tty->mouse_drag_flag = 0;
351: tty->mouse_drag_update = NULL;
352: tty->mouse_drag_release = NULL;
1.347 nicm 353: }
354:
355: void
356: tty_send_requests(struct tty *tty)
357: {
358: if (~tty->flags & TTY_STARTED)
359: return;
360:
361: if (tty_term_flag(tty->term, TTYC_XT)) {
362: if (~tty->flags & TTY_HAVEDA)
1.351 nicm 363: tty_puts(tty, "\033[>c");
1.364 nicm 364: if (~tty->flags & TTY_HAVEXDA)
365: tty_puts(tty, "\033[>q");
1.347 nicm 366: } else
1.364 nicm 367: tty->flags |= (TTY_HAVEDA|TTY_HAVEXDA);
1.128 nicm 368: }
369:
370: void
1.1 nicm 371: tty_stop_tty(struct tty *tty)
372: {
373: struct winsize ws;
374:
1.20 nicm 375: if (!(tty->flags & TTY_STARTED))
376: return;
377: tty->flags &= ~TTY_STARTED;
1.340 nicm 378:
379: evtimer_del(&tty->start_timer);
1.20 nicm 380:
1.266 nicm 381: event_del(&tty->timer);
382: tty->flags &= ~TTY_BLOCK;
383:
1.244 nicm 384: event_del(&tty->event_in);
385: event_del(&tty->event_out);
1.66 nicm 386:
1.1 nicm 387: /*
388: * Be flexible about error handling and try not kill the server just
389: * because the fd is invalid. Things like ssh -t can easily leave us
390: * with a dead tty.
391: */
392: if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
393: return;
394: if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
395: return;
396:
397: tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.285 nicm 398: if (tty_acs_needed(tty))
1.90 nicm 399: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1 nicm 400: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
401: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
402: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.160 nicm 403: if (tty_term_has(tty->term, TTYC_SS) && tty->cstyle != 0) {
404: if (tty_term_has(tty->term, TTYC_SE))
405: tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
1.109 nicm 406: else
1.160 nicm 407: tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
1.108 nicm 408: }
1.173 nicm 409: if (tty->mode & MODE_BRACKETPASTE)
410: tty_raw(tty, "\033[?2004l");
1.336 nicm 411: if (*tty->ccolour != '\0')
412: tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1 nicm 413:
414: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
1.343 nicm 415: if (tty_term_has(tty->term, TTYC_KMOUS)) {
416: tty_raw(tty, "\033[?1000l\033[?1002l\033[?1003l");
417: tty_raw(tty, "\033[?1006l\033[?1005l");
418: }
1.151 nicm 419:
1.189 nicm 420: if (tty_term_flag(tty->term, TTYC_XT)) {
1.162 nicm 421: if (tty->flags & TTY_FOCUS) {
422: tty->flags &= ~TTY_FOCUS;
1.172 nicm 423: tty_raw(tty, "\033[?1004l");
1.162 nicm 424: }
1.352 nicm 425: tty_raw(tty, "\033[?7727l");
1.162 nicm 426: }
1.10 nicm 427:
1.212 nicm 428: if (tty_use_margin(tty))
1.365 ! nicm 429: tty_raw(tty, tty_term_string(tty->term, TTYC_DSMG));
1.10 nicm 430: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150 nicm 431:
432: setblocking(tty->fd, 1);
1.1 nicm 433: }
434:
435: void
1.20 nicm 436: tty_close(struct tty *tty)
1.1 nicm 437: {
1.123 nicm 438: if (event_initialized(&tty->key_timer))
439: evtimer_del(&tty->key_timer);
1.20 nicm 440: tty_stop_tty(tty);
1.1 nicm 441:
1.21 nicm 442: if (tty->flags & TTY_OPENED) {
1.244 nicm 443: evbuffer_free(tty->in);
444: event_del(&tty->event_in);
445: evbuffer_free(tty->out);
446: event_del(&tty->event_out);
1.66 nicm 447:
1.21 nicm 448: tty_term_free(tty->term);
449: tty_keys_free(tty);
450:
451: tty->flags &= ~TTY_OPENED;
452: }
1.1 nicm 453:
1.21 nicm 454: if (tty->fd != -1) {
455: close(tty->fd);
456: tty->fd = -1;
457: }
1.1 nicm 458: }
459:
460: void
1.20 nicm 461: tty_free(struct tty *tty)
1.1 nicm 462: {
1.20 nicm 463: tty_close(tty);
1.138 nicm 464: free(tty->ccolour);
1.1 nicm 465: }
466:
467: void
1.359 nicm 468: tty_update_features(struct tty *tty)
1.212 nicm 469: {
1.359 nicm 470: struct client *c = tty->client;
471:
1.362 nicm 472: if (tty_apply_features(tty->term, c->term_features))
473: tty_term_apply_overrides(tty->term);
1.212 nicm 474:
475: if (tty_use_margin(tty))
1.365 ! nicm 476: tty_putcode(tty, TTYC_ENMG);
1.212 nicm 477: }
478:
479: void
1.1 nicm 480: tty_raw(struct tty *tty, const char *s)
481: {
1.150 nicm 482: ssize_t n, slen;
483: u_int i;
484:
485: slen = strlen(s);
486: for (i = 0; i < 5; i++) {
487: n = write(tty->fd, s, slen);
488: if (n >= 0) {
489: s += n;
490: slen -= n;
491: if (slen == 0)
492: break;
493: } else if (n == -1 && errno != EAGAIN)
494: break;
495: usleep(100);
496: }
1.1 nicm 497: }
498:
499: void
500: tty_putcode(struct tty *tty, enum tty_code_code code)
501: {
502: tty_puts(tty, tty_term_string(tty->term, code));
503: }
504:
505: void
506: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
507: {
508: if (a < 0)
509: return;
510: tty_puts(tty, tty_term_string1(tty->term, code, a));
511: }
512:
513: void
514: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
515: {
516: if (a < 0 || b < 0)
517: return;
518: tty_puts(tty, tty_term_string2(tty->term, code, a, b));
519: }
520:
521: void
1.286 nicm 522: tty_putcode3(struct tty *tty, enum tty_code_code code, int a, int b, int c)
523: {
524: if (a < 0 || b < 0 || c < 0)
525: return;
526: tty_puts(tty, tty_term_string3(tty->term, code, a, b, c));
527: }
528:
529: void
1.107 nicm 530: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
531: {
532: if (a != NULL)
533: tty_puts(tty, tty_term_ptr1(tty->term, code, a));
534: }
535:
536: void
1.167 nicm 537: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
538: const void *b)
1.106 nicm 539: {
540: if (a != NULL && b != NULL)
541: tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
542: }
543:
1.243 nicm 544: static void
545: tty_add(struct tty *tty, const char *buf, size_t len)
546: {
1.258 nicm 547: struct client *c = tty->client;
548:
1.266 nicm 549: if (tty->flags & TTY_BLOCK) {
550: tty->discarded += len;
551: return;
552: }
553:
1.244 nicm 554: evbuffer_add(tty->out, buf, len);
1.270 nicm 555: log_debug("%s: %.*s", c->name, (int)len, buf);
1.266 nicm 556: c->written += len;
1.243 nicm 557:
558: if (tty_log_fd != -1)
559: write(tty_log_fd, buf, len);
1.244 nicm 560: if (tty->flags & TTY_STARTED)
561: event_add(&tty->event_out, NULL);
1.243 nicm 562: }
563:
1.106 nicm 564: void
1.1 nicm 565: tty_puts(struct tty *tty, const char *s)
566: {
1.243 nicm 567: if (*s != '\0')
568: tty_add(tty, s, strlen(s));
1.1 nicm 569: }
570:
571: void
572: tty_putc(struct tty *tty, u_char ch)
573: {
1.90 nicm 574: const char *acs;
1.1 nicm 575:
1.359 nicm 576: if ((tty->term->flags & TERM_NOXENL) &&
1.325 nicm 577: ch >= 0x20 && ch != 0x7f &&
578: tty->cy == tty->sy - 1 &&
579: tty->cx + 1 >= tty->sx)
580: return;
581:
1.90 nicm 582: if (tty->cell.attr & GRID_ATTR_CHARSET) {
583: acs = tty_acs_get(tty, ch);
1.243 nicm 584: if (acs != NULL)
585: tty_add(tty, acs, strlen(acs));
586: else
587: tty_add(tty, &ch, 1);
588: } else
589: tty_add(tty, &ch, 1);
1.1 nicm 590:
591: if (ch >= 0x20 && ch != 0x7f) {
1.211 nicm 592: if (tty->cx >= tty->sx) {
1.46 nicm 593: tty->cx = 1;
594: if (tty->cy != tty->rlower)
595: tty->cy++;
1.211 nicm 596:
597: /*
598: * On !xenl terminals, force the cursor position to
599: * where we think it should be after a line wrap - this
600: * means it works on sensible terminals as well.
601: */
1.359 nicm 602: if (tty->term->flags & TERM_NOXENL)
1.211 nicm 603: tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
1.1 nicm 604: } else
605: tty->cx++;
606: }
607: }
608:
609: void
1.147 nicm 610: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5 nicm 611: {
1.359 nicm 612: if ((tty->term->flags & TERM_NOXENL) &&
1.325 nicm 613: tty->cy == tty->sy - 1 &&
614: tty->cx + len >= tty->sx)
615: len = tty->sx - tty->cx - 1;
616:
1.243 nicm 617: tty_add(tty, buf, len);
1.268 nicm 618: if (tty->cx + width > tty->sx) {
619: tty->cx = (tty->cx + width) - tty->sx;
620: if (tty->cx <= tty->sx)
621: tty->cy++;
622: else
623: tty->cx = tty->cy = UINT_MAX;
624: } else
1.254 nicm 625: tty->cx += width;
1.5 nicm 626: }
627:
1.207 nicm 628: static void
1.180 nicm 629: tty_set_italics(struct tty *tty)
630: {
631: const char *s;
632:
633: if (tty_term_has(tty->term, TTYC_SITM)) {
1.191 nicm 634: s = options_get_string(global_options, "default-terminal");
1.180 nicm 635: if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
636: tty_putcode(tty, TTYC_SITM);
637: return;
638: }
639: }
640: tty_putcode(tty, TTYC_SMSO);
641: }
642:
643: void
1.1 nicm 644: tty_set_title(struct tty *tty, const char *title)
645: {
1.105 nicm 646: if (!tty_term_has(tty->term, TTYC_TSL) ||
647: !tty_term_has(tty->term, TTYC_FSL))
1.1 nicm 648: return;
649:
1.105 nicm 650: tty_putcode(tty, TTYC_TSL);
1.1 nicm 651: tty_puts(tty, title);
1.105 nicm 652: tty_putcode(tty, TTYC_FSL);
1.1 nicm 653: }
654:
1.208 nicm 655: static void
1.107 nicm 656: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
657: {
658: if (*ccolour == '\0')
659: tty_putcode(tty, TTYC_CR);
660: else
1.160 nicm 661: tty_putcode_ptr1(tty, TTYC_CS, ccolour);
1.138 nicm 662: free(tty->ccolour);
1.107 nicm 663: tty->ccolour = xstrdup(ccolour);
664: }
665:
666: void
667: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1 nicm 668: {
1.344 nicm 669: struct client *c = tty->client;
670: int changed;
1.1 nicm 671:
1.197 nicm 672: if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0)
1.107 nicm 673: tty_force_cursor_colour(tty, s->ccolour);
674:
1.1 nicm 675: if (tty->flags & TTY_NOCURSOR)
676: mode &= ~MODE_CURSOR;
677:
678: changed = mode ^ tty->mode;
1.344 nicm 679: log_debug("%s: update mode %x to %x", c->name, tty->mode, mode);
680:
1.183 nicm 681: if (changed & MODE_BLINKING) {
682: if (tty_term_has(tty->term, TTYC_CVVIS))
683: tty_putcode(tty, TTYC_CVVIS);
684: else
685: tty_putcode(tty, TTYC_CNORM);
686: changed |= MODE_CURSOR;
687: }
688: if (changed & MODE_CURSOR) {
689: if (mode & MODE_CURSOR)
690: tty_putcode(tty, TTYC_CNORM);
691: else
1.1 nicm 692: tty_putcode(tty, TTYC_CIVIS);
1.108 nicm 693: }
1.181 nicm 694: if (s != NULL && tty->cstyle != s->cstyle) {
1.160 nicm 695: if (tty_term_has(tty->term, TTYC_SS)) {
1.108 nicm 696: if (s->cstyle == 0 &&
1.160 nicm 697: tty_term_has(tty->term, TTYC_SE))
698: tty_putcode(tty, TTYC_SE);
1.108 nicm 699: else
1.160 nicm 700: tty_putcode1(tty, TTYC_SS, s->cstyle);
1.108 nicm 701: }
702: tty->cstyle = s->cstyle;
1.1 nicm 703: }
1.344 nicm 704: if ((changed & ALL_MOUSE_MODES) &&
705: tty_term_has(tty->term, TTYC_KMOUS)) {
706: if ((mode & ALL_MOUSE_MODES) == 0)
707: tty_puts(tty, "\033[?1006l");
708: if ((changed & MODE_MOUSE_STANDARD) &&
709: (~mode & MODE_MOUSE_STANDARD))
710: tty_puts(tty, "\033[?1000l");
711: if ((changed & MODE_MOUSE_BUTTON) &&
712: (~mode & MODE_MOUSE_BUTTON))
713: tty_puts(tty, "\033[?1002l");
714: if ((changed & MODE_MOUSE_ALL) &&
715: (~mode & MODE_MOUSE_ALL))
716: tty_puts(tty, "\033[?1003l");
717:
718: if (mode & ALL_MOUSE_MODES)
1.153 nicm 719: tty_puts(tty, "\033[?1006h");
1.344 nicm 720: if ((changed & MODE_MOUSE_STANDARD) &&
721: (mode & MODE_MOUSE_STANDARD))
722: tty_puts(tty, "\033[?1000h");
723: if ((changed & MODE_MOUSE_BUTTON) &&
724: (mode & MODE_MOUSE_BUTTON))
725: tty_puts(tty, "\033[?1002h");
726: if ((changed & MODE_MOUSE_ALL) &&
727: (mode & MODE_MOUSE_ALL))
728: tty_puts(tty, "\033[?1003h");
1.115 nicm 729: }
730: if (changed & MODE_BRACKETPASTE) {
731: if (mode & MODE_BRACKETPASTE)
732: tty_puts(tty, "\033[?2004h");
733: else
734: tty_puts(tty, "\033[?2004l");
1.1 nicm 735: }
736: tty->mode = mode;
737: }
738:
1.207 nicm 739: static void
1.168 nicm 740: tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
741: enum tty_code_code code1, u_int n)
1.1 nicm 742: {
743: if (tty_term_has(tty->term, code))
744: tty_putcode1(tty, code, n);
745: else {
746: while (n-- > 0)
747: tty_putcode(tty, code1);
748: }
749: }
750:
1.207 nicm 751: static void
1.133 nicm 752: tty_repeat_space(struct tty *tty, u_int n)
753: {
1.257 nicm 754: static char s[500];
755:
756: if (*s != ' ')
757: memset(s, ' ', sizeof s);
758:
759: while (n > sizeof s) {
760: tty_putn(tty, s, sizeof s, sizeof s);
761: n -= sizeof s;
762: }
763: if (n != 0)
764: tty_putn(tty, s, n, n);
1.304 nicm 765: }
766:
1.309 nicm 767: /* Is this window bigger than the terminal? */
768: int
769: tty_window_bigger(struct tty *tty)
770: {
771: struct client *c = tty->client;
772: struct window *w = c->session->curw->window;
773:
774: return (tty->sx < w->sx || tty->sy - status_line_size(c) < w->sy);
775: }
776:
777: /* What offset should this window be drawn at? */
778: int
779: tty_window_offset(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
780: {
781: *ox = tty->oox;
782: *oy = tty->ooy;
783: *sx = tty->osx;
784: *sy = tty->osy;
785:
786: return (tty->oflag);
787: }
788:
789: /* What offset should this window be drawn at? */
790: static int
791: tty_window_offset1(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
792: {
793: struct client *c = tty->client;
794: struct window *w = c->session->curw->window;
795: struct window_pane *wp = w->active;
796: u_int cx, cy, lines;
797:
798: lines = status_line_size(c);
799:
800: if (tty->sx >= w->sx && tty->sy - lines >= w->sy) {
801: *ox = 0;
802: *oy = 0;
803: *sx = w->sx;
804: *sy = w->sy;
805:
806: c->pan_window = NULL;
807: return (0);
808: }
809:
810: *sx = tty->sx;
811: *sy = tty->sy - lines;
812:
813: if (c->pan_window == w) {
814: if (*sx >= w->sx)
815: c->pan_ox = 0;
816: else if (c->pan_ox + *sx > w->sx)
817: c->pan_ox = w->sx - *sx;
818: *ox = c->pan_ox;
819: if (*sy >= w->sy)
820: c->pan_oy = 0;
821: else if (c->pan_oy + *sy > w->sy)
822: c->pan_oy = w->sy - *sy;
823: *oy = c->pan_oy;
824: return (1);
825: }
826:
827: if (~wp->screen->mode & MODE_CURSOR) {
828: *ox = 0;
829: *oy = 0;
830: } else {
831: cx = wp->xoff + wp->screen->cx;
832: cy = wp->yoff + wp->screen->cy;
833:
834: if (cx < *sx)
835: *ox = 0;
836: else if (cx > w->sx - *sx)
837: *ox = w->sx - *sx;
838: else
839: *ox = cx - *sx / 2;
840:
841: if (cy < *sy)
842: *oy = 0;
843: else if (cy > w->sy - *sy)
844: *oy = w->sy - *sy;
845: else
846: *oy = cy - *sy / 2;
847: }
848:
849: c->pan_window = NULL;
850: return (1);
851: }
852:
853: /* Update stored offsets for a window and redraw if necessary. */
854: void
855: tty_update_window_offset(struct window *w)
856: {
857: struct client *c;
858:
859: TAILQ_FOREACH(c, &clients, entry) {
860: if (c->session != NULL && c->session->curw->window == w)
861: tty_update_client_offset(c);
862: }
863: }
864:
865: /* Update stored offsets for a client and redraw if necessary. */
866: void
867: tty_update_client_offset(struct client *c)
1.304 nicm 868: {
1.309 nicm 869: u_int ox, oy, sx, sy;
1.314 nicm 870:
871: if (~c->flags & CLIENT_TERMINAL)
872: return;
1.304 nicm 873:
1.309 nicm 874: c->tty.oflag = tty_window_offset1(&c->tty, &ox, &oy, &sx, &sy);
875: if (ox == c->tty.oox &&
876: oy == c->tty.ooy &&
877: sx == c->tty.osx &&
878: sy == c->tty.osy)
879: return;
880:
881: log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
882: __func__, c->name, c->tty.oox, c->tty.ooy, c->tty.osx, c->tty.osy,
883: ox, oy, sx, sy);
884:
885: c->tty.oox = ox;
886: c->tty.ooy = oy;
887: c->tty.osx = sx;
888: c->tty.osy = sy;
889:
890: c->flags |= (CLIENT_REDRAWWINDOW|CLIENT_REDRAWSTATUS);
1.133 nicm 891: }
892:
1.1 nicm 893: /*
1.119 nicm 894: * Is the region large enough to be worth redrawing once later rather than
895: * probably several times now? Currently yes if it is more than 50% of the
896: * pane.
897: */
1.207 nicm 898: static int
1.194 nicm 899: tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
1.119 nicm 900: {
901: struct window_pane *wp = ctx->wp;
902:
903: return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
904: }
905:
906: /*
1.176 nicm 907: * Return if BCE is needed but the terminal doesn't have it - it'll need to be
908: * emulated.
909: */
1.207 nicm 910: static int
1.317 nicm 911: tty_fake_bce(const struct tty *tty, struct window_pane *wp, u_int bg)
1.176 nicm 912: {
913: struct grid_cell gc;
914:
1.210 nicm 915: if (tty_term_flag(tty->term, TTYC_BCE))
916: return (0);
917:
1.176 nicm 918: memcpy(&gc, &grid_default_cell, sizeof gc);
1.203 nicm 919: if (wp != NULL)
920: tty_default_colours(&gc, wp);
1.176 nicm 921:
1.310 nicm 922: if (!COLOUR_DEFAULT(bg) || !COLOUR_DEFAULT(gc.bg))
1.210 nicm 923: return (1);
924: return (0);
1.176 nicm 925: }
926:
927: /*
1.1 nicm 928: * Redraw scroll region using data from screen (already updated). Used when
929: * CSR not supported, or window is a pane that doesn't take up the full
930: * width of the terminal.
931: */
1.207 nicm 932: static void
1.24 nicm 933: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 934: {
1.358 nicm 935: struct client *c = tty->client;
1.14 nicm 936: struct window_pane *wp = ctx->wp;
937: struct screen *s = wp->screen;
1.204 nicm 938: u_int i;
1.1 nicm 939:
940: /*
1.119 nicm 941: * If region is large, schedule a window redraw. In most cases this is
942: * likely to be followed by some more scrolling.
1.1 nicm 943: */
1.119 nicm 944: if (tty_large_region(tty, ctx)) {
1.358 nicm 945: log_debug("%s: %s, large redraw of %%%u", __func__, c->name, wp->id);
1.1 nicm 946: wp->flags |= PANE_REDRAW;
947: return;
948: }
949:
1.14 nicm 950: if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
951: for (i = ctx->ocy; i < screen_size_y(s); i++)
1.309 nicm 952: tty_draw_pane(tty, ctx, i);
1.1 nicm 953: } else {
1.14 nicm 954: for (i = ctx->orupper; i <= ctx->orlower; i++)
1.309 nicm 955: tty_draw_pane(tty, ctx, i);
1.1 nicm 956: }
957: }
958:
1.309 nicm 959: /* Is this position visible in the pane? */
960: static int
961: tty_is_visible(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
962: u_int nx, u_int ny)
963: {
964: u_int xoff = ctx->xoff + px, yoff = ctx->yoff + py, lines;
965:
966: if (!ctx->bigger)
967: return (1);
968:
969: if (status_at_line(tty->client) == 0)
970: lines = status_line_size(tty->client);
971: else
972: lines = 0;
973:
974: if (xoff + nx <= ctx->ox || xoff >= ctx->ox + ctx->sx ||
1.320 nicm 975: yoff + ny <= ctx->oy || yoff >= lines + ctx->oy + ctx->sy)
1.309 nicm 976: return (0);
977: return (1);
978: }
979:
980: /* Clamp line position to visible part of pane. */
981: static int
982: tty_clamp_line(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
983: u_int nx, u_int *i, u_int *x, u_int *rx, u_int *ry)
984: {
985: struct window_pane *wp = ctx->wp;
986: u_int xoff = wp->xoff + px;
987:
988: if (!tty_is_visible(tty, ctx, px, py, nx, 1))
989: return (0);
990: *ry = ctx->yoff + py - ctx->oy;
991:
992: if (xoff >= ctx->ox && xoff + nx <= ctx->ox + ctx->sx) {
993: /* All visible. */
994: *i = 0;
995: *x = ctx->xoff + px - ctx->ox;
996: *rx = nx;
997: } else if (xoff < ctx->ox && xoff + nx > ctx->ox + ctx->sx) {
998: /* Both left and right not visible. */
999: *i = ctx->ox;
1000: *x = 0;
1001: *rx = ctx->sx;
1002: } else if (xoff < ctx->ox) {
1003: /* Left not visible. */
1004: *i = ctx->ox - (ctx->xoff + px);
1005: *x = 0;
1006: *rx = nx - *i;
1007: } else {
1008: /* Right not visible. */
1009: *i = 0;
1010: *x = (ctx->xoff + px) - ctx->ox;
1011: *rx = ctx->sx - *x;
1012: }
1013: if (*rx > nx)
1014: fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1015:
1016: return (1);
1017: }
1018:
1019: /* Clear a line. */
1.271 nicm 1020: static void
1.317 nicm 1021: tty_clear_line(struct tty *tty, struct window_pane *wp, u_int py, u_int px,
1022: u_int nx, u_int bg)
1.271 nicm 1023: {
1.309 nicm 1024: struct client *c = tty->client;
1025:
1026: log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1.271 nicm 1027:
1028: /* Nothing to clear. */
1029: if (nx == 0)
1030: return;
1031:
1032: /* If genuine BCE is available, can try escape sequences. */
1033: if (!tty_fake_bce(tty, wp, bg)) {
1034: /* Off the end of the line, use EL if available. */
1035: if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) {
1036: tty_cursor(tty, px, py);
1037: tty_putcode(tty, TTYC_EL);
1038: return;
1039: }
1040:
1041: /* At the start of the line. Use EL1. */
1042: if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1043: tty_cursor(tty, px + nx - 1, py);
1044: tty_putcode(tty, TTYC_EL1);
1045: return;
1046: }
1047:
1048: /* Section of line. Use ECH if possible. */
1049: if (tty_term_has(tty->term, TTYC_ECH)) {
1050: tty_cursor(tty, px, py);
1051: tty_putcode1(tty, TTYC_ECH, nx);
1052: return;
1053: }
1054: }
1055:
1056: /* Couldn't use an escape sequence, use spaces. */
1.272 nicm 1057: tty_cursor(tty, px, py);
1.271 nicm 1058: tty_repeat_space(tty, nx);
1059: }
1060:
1.309 nicm 1061: /* Clear a line, adjusting to visible part of pane. */
1062: static void
1063: tty_clear_pane_line(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1064: u_int px, u_int nx, u_int bg)
1065: {
1066: struct client *c = tty->client;
1067: u_int i, x, rx, ry;
1068:
1069: log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1070:
1071: if (tty_clamp_line(tty, ctx, px, py, nx, &i, &x, &rx, &ry))
1072: tty_clear_line(tty, ctx->wp, ry, x, rx, bg);
1073: }
1074:
1075: /* Clamp area position to visible part of pane. */
1076: static int
1077: tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1078: u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
1079: u_int *ry)
1080: {
1081: struct window_pane *wp = ctx->wp;
1082: u_int xoff = wp->xoff + px, yoff = wp->yoff + py;
1083:
1084: if (!tty_is_visible(tty, ctx, px, py, nx, ny))
1085: return (0);
1086:
1087: if (xoff >= ctx->ox && xoff + nx <= ctx->ox + ctx->sx) {
1088: /* All visible. */
1089: *i = 0;
1090: *x = ctx->xoff + px - ctx->ox;
1091: *rx = nx;
1092: } else if (xoff < ctx->ox && xoff + nx > ctx->ox + ctx->sx) {
1093: /* Both left and right not visible. */
1094: *i = ctx->ox;
1095: *x = 0;
1096: *rx = ctx->sx;
1097: } else if (xoff < ctx->ox) {
1098: /* Left not visible. */
1099: *i = ctx->ox - (ctx->xoff + px);
1100: *x = 0;
1101: *rx = nx - *i;
1102: } else {
1103: /* Right not visible. */
1104: *i = 0;
1105: *x = (ctx->xoff + px) - ctx->ox;
1106: *rx = ctx->sx - *x;
1107: }
1108: if (*rx > nx)
1109: fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1110:
1111: if (yoff >= ctx->oy && yoff + ny <= ctx->oy + ctx->sy) {
1112: /* All visible. */
1113: *j = 0;
1114: *y = ctx->yoff + py - ctx->oy;
1115: *ry = ny;
1116: } else if (yoff < ctx->oy && yoff + ny > ctx->oy + ctx->sy) {
1.327 nicm 1117: /* Both top and bottom not visible. */
1.309 nicm 1118: *j = ctx->oy;
1119: *y = 0;
1120: *ry = ctx->sy;
1121: } else if (yoff < ctx->oy) {
1.327 nicm 1122: /* Top not visible. */
1.309 nicm 1123: *j = ctx->oy - (ctx->yoff + py);
1124: *y = 0;
1125: *ry = ny - *j;
1126: } else {
1.327 nicm 1127: /* Bottom not visible. */
1.309 nicm 1128: *j = 0;
1129: *y = (ctx->yoff + py) - ctx->oy;
1130: *ry = ctx->sy - *y;
1131: }
1132: if (*ry > ny)
1133: fatalx("%s: y too big, %u > %u", __func__, *ry, ny);
1134:
1135: return (1);
1136: }
1137:
1138: /* Clear an area, adjusting to visible part of pane. */
1.271 nicm 1139: static void
1.317 nicm 1140: tty_clear_area(struct tty *tty, struct window_pane *wp, u_int py, u_int ny,
1141: u_int px, u_int nx, u_int bg)
1.271 nicm 1142: {
1.309 nicm 1143: struct client *c = tty->client;
1144: u_int yy;
1145: char tmp[64];
1.271 nicm 1146:
1.309 nicm 1147: log_debug("%s: %s, %u,%u at %u,%u", __func__, c->name, nx, ny, px, py);
1.271 nicm 1148:
1149: /* Nothing to clear. */
1150: if (nx == 0 || ny == 0)
1151: return;
1152:
1153: /* If genuine BCE is available, can try escape sequences. */
1154: if (!tty_fake_bce(tty, wp, bg)) {
1.276 nicm 1155: /* Use ED if clearing off the bottom of the terminal. */
1.271 nicm 1156: if (px == 0 &&
1157: px + nx >= tty->sx &&
1158: py + ny >= tty->sy &&
1159: tty_term_has(tty->term, TTYC_ED)) {
1160: tty_cursor(tty, 0, py);
1161: tty_putcode(tty, TTYC_ED);
1.274 nicm 1162: return;
1163: }
1164:
1165: /*
1.276 nicm 1166: * On VT420 compatible terminals we can use DECFRA if the
1167: * background colour isn't default (because it doesn't work
1168: * after SGR 0).
1.274 nicm 1169: */
1.359 nicm 1170: if ((tty->term->flags & TERM_DECFRA) && !COLOUR_DEFAULT(bg)) {
1.274 nicm 1171: xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x",
1172: py + 1, px + 1, py + ny, px + nx);
1173: tty_puts(tty, tmp);
1.276 nicm 1174: return;
1175: }
1176:
1.280 nicm 1177: /* Full lines can be scrolled away to clear them. */
1178: if (px == 0 &&
1.281 nicm 1179: px + nx >= tty->sx &&
1.280 nicm 1180: ny > 2 &&
1181: tty_term_has(tty->term, TTYC_CSR) &&
1182: tty_term_has(tty->term, TTYC_INDN)) {
1183: tty_region(tty, py, py + ny - 1);
1184: tty_margin_off(tty);
1.284 nicm 1185: tty_putcode1(tty, TTYC_INDN, ny);
1.280 nicm 1186: return;
1187: }
1188:
1.276 nicm 1189: /*
1190: * If margins are supported, can just scroll the area off to
1191: * clear it.
1192: */
1.277 nicm 1193: if (nx > 2 &&
1194: ny > 2 &&
1.280 nicm 1195: tty_term_has(tty->term, TTYC_CSR) &&
1.277 nicm 1196: tty_use_margin(tty) &&
1197: tty_term_has(tty->term, TTYC_INDN)) {
1.276 nicm 1198: tty_region(tty, py, py + ny - 1);
1199: tty_margin(tty, px, px + nx - 1);
1.284 nicm 1200: tty_putcode1(tty, TTYC_INDN, ny);
1.271 nicm 1201: return;
1202: }
1203: }
1204:
1205: /* Couldn't use an escape sequence, loop over the lines. */
1206: for (yy = py; yy < py + ny; yy++)
1207: tty_clear_line(tty, wp, yy, px, nx, bg);
1208: }
1209:
1.309 nicm 1210: /* Clear an area in a pane. */
1211: static void
1212: tty_clear_pane_area(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1213: u_int ny, u_int px, u_int nx, u_int bg)
1214: {
1215: u_int i, j, x, y, rx, ry;
1216:
1217: if (tty_clamp_area(tty, ctx, px, py, nx, ny, &i, &j, &x, &y, &rx, &ry))
1218: tty_clear_area(tty, ctx->wp, y, ry, x, rx, bg);
1219: }
1220:
1221: static void
1222: tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
1.176 nicm 1223: {
1.309 nicm 1224: struct window_pane *wp = ctx->wp;
1225: struct screen *s = wp->screen;
1226: u_int nx = screen_size_x(s), i, x, rx, ry;
1227:
1228: log_debug("%s: %s %u %d", __func__, tty->client->name, py, ctx->bigger);
1229:
1230: if (!ctx->bigger) {
1231: tty_draw_line(tty, wp, s, 0, py, nx, ctx->xoff, ctx->yoff + py);
1232: return;
1233: }
1234: if (tty_clamp_line(tty, ctx, 0, py, nx, &i, &x, &rx, &ry))
1235: tty_draw_line(tty, wp, s, i, py, rx, x, ry);
1.176 nicm 1236: }
1237:
1.298 nicm 1238: static const struct grid_cell *
1239: tty_check_codeset(struct tty *tty, const struct grid_cell *gc)
1240: {
1241: static struct grid_cell new;
1242: u_int n;
1243:
1244: /* Characters less than 0x7f are always fine, no matter what. */
1245: if (gc->data.size == 1 && *gc->data.data < 0x7f)
1246: return (gc);
1247:
1248: /* UTF-8 terminal and a UTF-8 character - fine. */
1.359 nicm 1249: if (tty->client->flags & CLIENT_UTF8)
1.298 nicm 1250: return (gc);
1251:
1252: /* Replace by the right number of underscores. */
1253: n = gc->data.width;
1254: if (n > UTF8_SIZE)
1255: n = UTF8_SIZE;
1256: memcpy(&new, gc, sizeof new);
1257: new.data.size = n;
1258: memset(new.data.data, '_', n);
1259: return (&new);
1260: }
1261:
1.346 nicm 1262: static int
1263: tty_check_overlay(struct tty *tty, u_int px, u_int py)
1264: {
1265: struct client *c = tty->client;
1266:
1267: if (c->overlay_check == NULL)
1268: return (1);
1269: return (c->overlay_check(c, px, py));
1270: }
1271:
1.176 nicm 1272: void
1.317 nicm 1273: tty_draw_line(struct tty *tty, struct window_pane *wp, struct screen *s,
1274: u_int px, u_int py, u_int nx, u_int atx, u_int aty)
1.1 nicm 1275: {
1.297 nicm 1276: struct grid *gd = s->grid;
1.250 nicm 1277: struct grid_cell gc, last;
1.298 nicm 1278: const struct grid_cell *gcp;
1.309 nicm 1279: struct grid_line *gl;
1280: u_int i, j, ux, sx, width;
1.329 nicm 1281: int flags, cleared = 0, wrapped = 0;
1.250 nicm 1282: char buf[512];
1.315 nicm 1283: size_t len;
1.303 nicm 1284: u_int cellsize;
1.1 nicm 1285:
1.309 nicm 1286: log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__,
1287: px, py, nx, atx, aty);
1288:
1289: /*
1290: * py is the line in the screen to draw.
1291: * px is the start x and nx is the width to draw.
1292: * atx,aty is the line on the terminal to draw it.
1293: */
1294:
1.259 nicm 1295: flags = (tty->flags & TTY_NOCURSOR);
1.181 nicm 1296: tty->flags |= TTY_NOCURSOR;
1297: tty_update_mode(tty, tty->mode, s);
1.35 nicm 1298:
1.214 nicm 1299: tty_region_off(tty);
1300: tty_margin_off(tty);
1301:
1.273 nicm 1302: /*
1303: * Clamp the width to cellsize - note this is not cellused, because
1304: * there may be empty background cells after it (from BCE).
1305: */
1.1 nicm 1306: sx = screen_size_x(s);
1.309 nicm 1307: if (nx > sx)
1308: nx = sx;
1.303 nicm 1309: cellsize = grid_get_line(gd, gd->hsize + py)->cellsize;
1310: if (sx > cellsize)
1311: sx = cellsize;
1.1 nicm 1312: if (sx > tty->sx)
1313: sx = tty->sx;
1.309 nicm 1314: if (sx > nx)
1315: sx = nx;
1.292 nicm 1316: ux = 0;
1.1 nicm 1317:
1.309 nicm 1318: if (py == 0)
1319: gl = NULL;
1320: else
1321: gl = grid_get_line(gd, gd->hsize + py - 1);
1.268 nicm 1322: if (wp == NULL ||
1.309 nicm 1323: gl == NULL ||
1324: (~gl->flags & GRID_LINE_WRAPPED) ||
1325: atx != 0 ||
1.268 nicm 1326: tty->cx < tty->sx ||
1.309 nicm 1327: nx < tty->sx) {
1328: if (nx < tty->sx &&
1329: atx == 0 &&
1330: px + sx != nx &&
1.268 nicm 1331: tty_term_has(tty->term, TTYC_EL1) &&
1332: !tty_fake_bce(tty, wp, 8)) {
1333: tty_default_attributes(tty, wp, 8);
1.309 nicm 1334: tty_cursor(tty, nx - 1, aty);
1.268 nicm 1335: tty_putcode(tty, TTYC_EL1);
1336: cleared = 1;
1337: }
1.329 nicm 1338: } else {
1.309 nicm 1339: log_debug("%s: wrapped line %u", __func__, aty);
1.329 nicm 1340: wrapped = 1;
1341: }
1.250 nicm 1342:
1343: memcpy(&last, &grid_default_cell, sizeof last);
1344: len = 0;
1345: width = 0;
1.46 nicm 1346:
1.1 nicm 1347: for (i = 0; i < sx; i++) {
1.309 nicm 1348: grid_view_get_cell(gd, px + i, py, &gc);
1.298 nicm 1349: gcp = tty_check_codeset(tty, &gc);
1.250 nicm 1350: if (len != 0 &&
1.346 nicm 1351: (!tty_check_overlay(tty, atx + ux + width, aty) ||
1352: (gcp->attr & GRID_ATTR_CHARSET) ||
1.298 nicm 1353: gcp->flags != last.flags ||
1354: gcp->attr != last.attr ||
1355: gcp->fg != last.fg ||
1356: gcp->bg != last.bg ||
1.328 nicm 1357: gcp->us != last.us ||
1.315 nicm 1358: ux + width + gcp->data.width > nx ||
1.298 nicm 1359: (sizeof buf) - len < gcp->data.size)) {
1.316 nicm 1360: tty_attributes(tty, &last, wp);
1.315 nicm 1361: if (last.flags & GRID_FLAG_CLEARED) {
1362: log_debug("%s: %zu cleared", __func__, len);
1363: tty_clear_line(tty, wp, aty, atx + ux, width,
1364: last.bg);
1365: } else {
1.329 nicm 1366: if (!wrapped || atx != 0 || ux != 0)
1367: tty_cursor(tty, atx + ux, aty);
1.315 nicm 1368: tty_putn(tty, buf, len, width);
1369: }
1.292 nicm 1370: ux += width;
1.250 nicm 1371:
1372: len = 0;
1373: width = 0;
1.329 nicm 1374: wrapped = 0;
1.250 nicm 1375: }
1376:
1.298 nicm 1377: if (gcp->flags & GRID_FLAG_SELECTED)
1.299 nicm 1378: screen_select_cell(s, &last, gcp);
1.250 nicm 1379: else
1.299 nicm 1380: memcpy(&last, gcp, sizeof last);
1.346 nicm 1381: if (!tty_check_overlay(tty, atx + ux, aty))
1382: ux += gcp->data.width;
1383: else if (ux + gcp->data.width > nx) {
1.299 nicm 1384: tty_attributes(tty, &last, wp);
1.315 nicm 1385: tty_cursor(tty, atx + ux, aty);
1.298 nicm 1386: for (j = 0; j < gcp->data.width; j++) {
1.309 nicm 1387: if (ux + j > nx)
1.297 nicm 1388: break;
1389: tty_putc(tty, ' ');
1390: ux++;
1391: }
1.299 nicm 1392: } else if (gcp->attr & GRID_ATTR_CHARSET) {
1393: tty_attributes(tty, &last, wp);
1.315 nicm 1394: tty_cursor(tty, atx + ux, aty);
1.299 nicm 1395: for (j = 0; j < gcp->data.size; j++)
1396: tty_putc(tty, gcp->data.data[j]);
1.346 nicm 1397: ux += gcp->data.width;
1.299 nicm 1398: } else {
1.298 nicm 1399: memcpy(buf + len, gcp->data.data, gcp->data.size);
1400: len += gcp->data.size;
1401: width += gcp->data.width;
1.250 nicm 1402: }
1403: }
1.315 nicm 1404: if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1.316 nicm 1405: tty_attributes(tty, &last, wp);
1.315 nicm 1406: if (last.flags & GRID_FLAG_CLEARED) {
1407: log_debug("%s: %zu cleared (end)", __func__, len);
1408: tty_clear_line(tty, wp, aty, atx + ux, width, last.bg);
1409: } else {
1.329 nicm 1410: if (!wrapped || atx != 0 || ux != 0)
1411: tty_cursor(tty, atx + ux, aty);
1.291 nicm 1412: tty_putn(tty, buf, len, width);
1413: }
1.315 nicm 1414: ux += width;
1.1 nicm 1415: }
1416:
1.309 nicm 1417: if (!cleared && ux < nx) {
1.315 nicm 1418: log_debug("%s: %u to end of line (%zu cleared)", __func__,
1419: nx - ux, len);
1.210 nicm 1420: tty_default_attributes(tty, wp, 8);
1.309 nicm 1421: tty_clear_line(tty, wp, aty, atx + ux, nx - ux, 8);
1.35 nicm 1422: }
1.1 nicm 1423:
1.181 nicm 1424: tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107 nicm 1425: tty_update_mode(tty, tty->mode, s);
1.15 nicm 1426: }
1427:
1.348 nicm 1428: void
1429: tty_sync_start(struct tty *tty)
1430: {
1.361 nicm 1431: if (tty->flags & TTY_BLOCK)
1432: return;
1433: if (tty->flags & TTY_SYNCING)
1434: return;
1435: tty->flags |= TTY_SYNCING;
1436:
1437: if (tty_term_has(tty->term, TTYC_SYNC)) {
1438: log_debug("%s sync start", tty->client->name);
1.360 nicm 1439: tty_putcode1(tty, TTYC_SYNC, 1);
1.357 nicm 1440: }
1.348 nicm 1441: }
1442:
1443: void
1444: tty_sync_end(struct tty *tty)
1445: {
1.361 nicm 1446: if (tty->flags & TTY_BLOCK)
1447: return;
1448: if (~tty->flags & TTY_SYNCING)
1449: return;
1450: tty->flags &= ~TTY_SYNCING;
1451:
1452: if (tty_term_has(tty->term, TTYC_SYNC)) {
1453: log_debug("%s sync end", tty->client->name);
1.360 nicm 1454: tty_putcode1(tty, TTYC_SYNC, 2);
1.357 nicm 1455: }
1.348 nicm 1456: }
1457:
1.201 nicm 1458: static int
1.182 nicm 1459: tty_client_ready(struct client *c, struct window_pane *wp)
1460: {
1461: if (c->session == NULL || c->tty.term == NULL)
1462: return (0);
1.305 nicm 1463: if (c->flags & (CLIENT_REDRAWWINDOW|CLIENT_SUSPENDED))
1.182 nicm 1464: return (0);
1465: if (c->tty.flags & TTY_FREEZE)
1466: return (0);
1467: if (c->session->curw->window != wp->window)
1468: return (0);
1.309 nicm 1469: if (wp->layout_cell == NULL)
1470: return (0);
1.182 nicm 1471: return (1);
1472: }
1473:
1.15 nicm 1474: void
1.182 nicm 1475: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1476: struct tty_ctx *ctx)
1.15 nicm 1477: {
1478: struct window_pane *wp = ctx->wp;
1479: struct client *c;
1480:
1481: if (wp == NULL)
1482: return;
1.309 nicm 1483: if (wp->flags & (PANE_REDRAW|PANE_DROP))
1.15 nicm 1484: return;
1485:
1.178 nicm 1486: TAILQ_FOREACH(c, &clients, entry) {
1.182 nicm 1487: if (!tty_client_ready(c, wp))
1.15 nicm 1488: continue;
1.358 nicm 1489: if (c->flags & CLIENT_REDRAWPANES) {
1490: /*
1491: * Redraw is already deferred to redraw another pane -
1492: * redraw this one also when that happens.
1493: */
1.363 nicm 1494: log_debug("adding %%%u to deferred redraw", wp->id);
1.358 nicm 1495: wp->flags |= PANE_REDRAW;
1496: break;
1497: }
1.15 nicm 1498:
1.309 nicm 1499: ctx->bigger = tty_window_offset(&c->tty, &ctx->ox, &ctx->oy,
1500: &ctx->sx, &ctx->sy);
1501:
1.139 nicm 1502: ctx->xoff = wp->xoff;
1503: ctx->yoff = wp->yoff;
1.309 nicm 1504:
1.139 nicm 1505: if (status_at_line(c) == 0)
1.309 nicm 1506: ctx->yoff += status_line_size(c);
1.113 nicm 1507:
1.139 nicm 1508: cmdfn(&c->tty, ctx);
1.1 nicm 1509: }
1510: }
1511:
1512: void
1.24 nicm 1513: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1514: {
1.77 nicm 1515: struct window_pane *wp = ctx->wp;
1.1 nicm 1516:
1.309 nicm 1517: if (ctx->bigger ||
1518: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1519: tty_fake_bce(tty, wp, ctx->bg) ||
1520: (!tty_term_has(tty->term, TTYC_ICH) &&
1521: !tty_term_has(tty->term, TTYC_ICH1))) {
1.309 nicm 1522: tty_draw_pane(tty, ctx, ctx->ocy);
1.1 nicm 1523: return;
1524: }
1525:
1.210 nicm 1526: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1527:
1.77 nicm 1528: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 1529:
1.206 nicm 1530: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1 nicm 1531: }
1532:
1533: void
1.24 nicm 1534: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1535: {
1.77 nicm 1536: struct window_pane *wp = ctx->wp;
1.1 nicm 1537:
1.309 nicm 1538: if (ctx->bigger ||
1539: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1540: tty_fake_bce(tty, wp, ctx->bg) ||
1.26 nicm 1541: (!tty_term_has(tty->term, TTYC_DCH) &&
1542: !tty_term_has(tty->term, TTYC_DCH1))) {
1.309 nicm 1543: tty_draw_pane(tty, ctx, ctx->ocy);
1.1 nicm 1544: return;
1545: }
1546:
1.210 nicm 1547: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1548:
1.77 nicm 1549: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 1550:
1.206 nicm 1551: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146 nicm 1552: }
1553:
1554: void
1555: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1556: {
1.309 nicm 1557: if (ctx->bigger) {
1558: tty_draw_pane(tty, ctx, ctx->ocy);
1559: return;
1560: }
1561:
1.275 nicm 1562: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.146 nicm 1563:
1564: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1565:
1.210 nicm 1566: if (tty_term_has(tty->term, TTYC_ECH) &&
1.262 nicm 1567: !tty_fake_bce(tty, ctx->wp, 8))
1.146 nicm 1568: tty_putcode1(tty, TTYC_ECH, ctx->num);
1.257 nicm 1569: else
1570: tty_repeat_space(tty, ctx->num);
1.1 nicm 1571: }
1572:
1573: void
1.24 nicm 1574: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1575: {
1.309 nicm 1576: if (ctx->bigger ||
1577: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1578: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.77 nicm 1579: !tty_term_has(tty->term, TTYC_CSR) ||
1.307 nicm 1580: !tty_term_has(tty->term, TTYC_IL1) ||
1581: ctx->wp->sx == 1 ||
1582: ctx->wp->sy == 1) {
1.14 nicm 1583: tty_redraw_region(tty, ctx);
1.1 nicm 1584: return;
1585: }
1586:
1.210 nicm 1587: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1 nicm 1588:
1.77 nicm 1589: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1590: tty_margin_off(tty);
1.77 nicm 1591: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1592:
1.12 nicm 1593: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.279 nicm 1594: tty->cx = tty->cy = UINT_MAX;
1.1 nicm 1595: }
1596:
1597: void
1.24 nicm 1598: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1599: {
1.309 nicm 1600: if (ctx->bigger ||
1601: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1602: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.72 nicm 1603: !tty_term_has(tty->term, TTYC_CSR) ||
1.307 nicm 1604: !tty_term_has(tty->term, TTYC_DL1) ||
1605: ctx->wp->sx == 1 ||
1606: ctx->wp->sy == 1) {
1.14 nicm 1607: tty_redraw_region(tty, ctx);
1.1 nicm 1608: return;
1609: }
1610:
1.210 nicm 1611: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1 nicm 1612:
1.77 nicm 1613: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1614: tty_margin_off(tty);
1.77 nicm 1615: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1616:
1.12 nicm 1617: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.279 nicm 1618: tty->cx = tty->cy = UINT_MAX;
1.1 nicm 1619: }
1620:
1621: void
1.24 nicm 1622: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1623: {
1.77 nicm 1624: struct window_pane *wp = ctx->wp;
1.309 nicm 1625: u_int nx;
1.1 nicm 1626:
1.210 nicm 1627: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1628:
1.271 nicm 1629: nx = screen_size_x(wp->screen);
1.309 nicm 1630: tty_clear_pane_line(tty, ctx, ctx->ocy, 0, nx, ctx->bg);
1.1 nicm 1631: }
1632:
1633: void
1.24 nicm 1634: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1635: {
1.77 nicm 1636: struct window_pane *wp = ctx->wp;
1.309 nicm 1637: u_int nx;
1.1 nicm 1638:
1.210 nicm 1639: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1640:
1.271 nicm 1641: nx = screen_size_x(wp->screen) - ctx->ocx;
1.309 nicm 1642: tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1.1 nicm 1643: }
1644:
1645: void
1.24 nicm 1646: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1647: {
1.210 nicm 1648: struct window_pane *wp = ctx->wp;
1649:
1650: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1651:
1.309 nicm 1652: tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1.1 nicm 1653: }
1654:
1655: void
1.24 nicm 1656: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1657: {
1.278 nicm 1658: struct window_pane *wp = ctx->wp;
1659:
1.56 nicm 1660: if (ctx->ocy != ctx->orupper)
1661: return;
1662:
1.309 nicm 1663: if (ctx->bigger ||
1.331 nicm 1664: (!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.278 nicm 1665: tty_fake_bce(tty, wp, 8) ||
1.70 nicm 1666: !tty_term_has(tty->term, TTYC_CSR) ||
1.331 nicm 1667: (!tty_term_has(tty->term, TTYC_RI) &&
1668: !tty_term_has(tty->term, TTYC_RIN)) ||
1.307 nicm 1669: ctx->wp->sx == 1 ||
1670: ctx->wp->sy == 1) {
1.14 nicm 1671: tty_redraw_region(tty, ctx);
1.1 nicm 1672: return;
1673: }
1674:
1.278 nicm 1675: tty_default_attributes(tty, wp, ctx->bg);
1.77 nicm 1676:
1.56 nicm 1677: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.331 nicm 1678: tty_margin_pane(tty, ctx);
1.56 nicm 1679: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 1680:
1.331 nicm 1681: if (tty_term_has(tty->term, TTYC_RI))
1682: tty_putcode(tty, TTYC_RI);
1683: else
1684: tty_putcode1(tty, TTYC_RIN, 1);
1.1 nicm 1685: }
1686:
1687: void
1.24 nicm 1688: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1689: {
1.77 nicm 1690: struct window_pane *wp = ctx->wp;
1.1 nicm 1691:
1.56 nicm 1692: if (ctx->ocy != ctx->orlower)
1693: return;
1694:
1.309 nicm 1695: if (ctx->bigger ||
1696: (!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.262 nicm 1697: tty_fake_bce(tty, wp, 8) ||
1.307 nicm 1698: !tty_term_has(tty->term, TTYC_CSR) ||
1699: wp->sx == 1 ||
1700: wp->sy == 1) {
1.236 nicm 1701: tty_redraw_region(tty, ctx);
1.1 nicm 1702: return;
1703: }
1.52 nicm 1704:
1.278 nicm 1705: tty_default_attributes(tty, wp, ctx->bg);
1.77 nicm 1706:
1.56 nicm 1707: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.212 nicm 1708: tty_margin_pane(tty, ctx);
1709:
1710: /*
1.301 nicm 1711: * If we want to wrap a pane while using margins, the cursor needs to
1712: * be exactly on the right of the region. If the cursor is entirely off
1713: * the edge - move it back to the right. Some terminals are funny about
1714: * this and insert extra spaces, so only use the right if margins are
1715: * enabled.
1.212 nicm 1716: */
1.301 nicm 1717: if (ctx->xoff + ctx->ocx > tty->rright) {
1718: if (!tty_use_margin(tty))
1719: tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
1720: else
1721: tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1722: } else
1.212 nicm 1723: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 1724:
1.56 nicm 1725: tty_putc(tty, '\n');
1.240 nicm 1726: }
1727:
1728: void
1729: tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1730: {
1731: struct window_pane *wp = ctx->wp;
1.284 nicm 1732: u_int i;
1.240 nicm 1733:
1.309 nicm 1734: if (ctx->bigger ||
1735: (!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.262 nicm 1736: tty_fake_bce(tty, wp, 8) ||
1.307 nicm 1737: !tty_term_has(tty->term, TTYC_CSR) ||
1738: wp->sx == 1 ||
1739: wp->sy == 1) {
1.240 nicm 1740: tty_redraw_region(tty, ctx);
1741: return;
1742: }
1743:
1.278 nicm 1744: tty_default_attributes(tty, wp, ctx->bg);
1.240 nicm 1745:
1746: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1747: tty_margin_pane(tty, ctx);
1748:
1.284 nicm 1749: if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1.301 nicm 1750: if (!tty_use_margin(tty))
1751: tty_cursor(tty, 0, tty->rlower);
1752: else
1753: tty_cursor(tty, tty->rright, tty->rlower);
1.284 nicm 1754: for (i = 0; i < ctx->num; i++)
1.240 nicm 1755: tty_putc(tty, '\n');
1.301 nicm 1756: } else {
1.353 nicm 1757: if (tty->cy == UINT_MAX)
1758: tty_cursor(tty, 0, 0);
1759: else
1760: tty_cursor(tty, 0, tty->cy);
1.284 nicm 1761: tty_putcode1(tty, TTYC_INDN, ctx->num);
1.331 nicm 1762: }
1763: }
1764:
1765: void
1766: tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
1767: {
1768: struct window_pane *wp = ctx->wp;
1769: u_int i;
1770:
1771: if (ctx->bigger ||
1772: (!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1773: tty_fake_bce(tty, wp, 8) ||
1774: !tty_term_has(tty->term, TTYC_CSR) ||
1775: (!tty_term_has(tty->term, TTYC_RI) &&
1776: !tty_term_has(tty->term, TTYC_RIN)) ||
1777: wp->sx == 1 ||
1778: wp->sy == 1) {
1779: tty_redraw_region(tty, ctx);
1780: return;
1781: }
1782:
1783: tty_default_attributes(tty, wp, ctx->bg);
1784:
1785: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1786: tty_margin_pane(tty, ctx);
1787: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1788:
1789: if (tty_term_has(tty->term, TTYC_RIN))
1790: tty_putcode1(tty, TTYC_RIN, ctx->num);
1791: else {
1792: for (i = 0; i < ctx->num; i++)
1793: tty_putcode(tty, TTYC_RI);
1.301 nicm 1794: }
1.1 nicm 1795: }
1796:
1797: void
1.24 nicm 1798: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1799: {
1.77 nicm 1800: struct window_pane *wp = ctx->wp;
1.271 nicm 1801: u_int px, py, nx, ny;
1.1 nicm 1802:
1.210 nicm 1803: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1804:
1.271 nicm 1805: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1806: tty_margin_off(tty);
1.39 nicm 1807:
1.309 nicm 1808: px = 0;
1.271 nicm 1809: nx = screen_size_x(wp->screen);
1.309 nicm 1810: py = ctx->ocy + 1;
1.271 nicm 1811: ny = screen_size_y(wp->screen) - ctx->ocy - 1;
1812:
1.309 nicm 1813: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271 nicm 1814:
1.309 nicm 1815: px = ctx->ocx;
1.271 nicm 1816: nx = screen_size_x(wp->screen) - ctx->ocx;
1.309 nicm 1817: py = ctx->ocy;
1.271 nicm 1818:
1.309 nicm 1819: tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1 nicm 1820: }
1821:
1822: void
1.24 nicm 1823: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1824: {
1.77 nicm 1825: struct window_pane *wp = ctx->wp;
1.271 nicm 1826: u_int px, py, nx, ny;
1.1 nicm 1827:
1.227 nicm 1828: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1829:
1.271 nicm 1830: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1831: tty_margin_off(tty);
1.39 nicm 1832:
1.309 nicm 1833: px = 0;
1.271 nicm 1834: nx = screen_size_x(wp->screen);
1.309 nicm 1835: py = 0;
1.319 nicm 1836: ny = ctx->ocy;
1.271 nicm 1837:
1.309 nicm 1838: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271 nicm 1839:
1.309 nicm 1840: px = 0;
1.271 nicm 1841: nx = ctx->ocx + 1;
1.309 nicm 1842: py = ctx->ocy;
1.271 nicm 1843:
1.309 nicm 1844: tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1 nicm 1845: }
1846:
1847: void
1.24 nicm 1848: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1849: {
1.77 nicm 1850: struct window_pane *wp = ctx->wp;
1.271 nicm 1851: u_int px, py, nx, ny;
1.1 nicm 1852:
1.210 nicm 1853: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1854:
1.271 nicm 1855: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1856: tty_margin_off(tty);
1.39 nicm 1857:
1.309 nicm 1858: px = 0;
1.271 nicm 1859: nx = screen_size_x(wp->screen);
1.309 nicm 1860: py = 0;
1.271 nicm 1861: ny = screen_size_y(wp->screen);
1862:
1.309 nicm 1863: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.4 nicm 1864: }
1865:
1866: void
1.24 nicm 1867: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 1868: {
1.12 nicm 1869: struct window_pane *wp = ctx->wp;
1870: struct screen *s = wp->screen;
1871: u_int i, j;
1.4 nicm 1872:
1.309 nicm 1873: if (ctx->bigger) {
1874: wp->flags |= PANE_REDRAW;
1875: return;
1876: }
1877:
1.176 nicm 1878: tty_attributes(tty, &grid_default_cell, wp);
1.4 nicm 1879:
1.39 nicm 1880: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.214 nicm 1881: tty_margin_off(tty);
1.4 nicm 1882:
1883: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 1884: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 1885: for (i = 0; i < screen_size_x(s); i++)
1886: tty_putc(tty, 'E');
1.1 nicm 1887: }
1888: }
1889:
1890: void
1.24 nicm 1891: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1892: {
1.309 nicm 1893: if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1))
1894: return;
1895:
1896: if (ctx->xoff + ctx->ocx - ctx->ox > tty->sx - 1 &&
1.306 nicm 1897: ctx->ocy == ctx->orlower &&
1898: tty_pane_full_width(tty, ctx))
1899: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.46 nicm 1900:
1.306 nicm 1901: tty_margin_off(tty);
1.253 nicm 1902: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1903:
1.237 nicm 1904: tty_cell(tty, ctx->cell, ctx->wp);
1.239 nicm 1905: }
1906:
1907: void
1908: tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
1909: {
1.309 nicm 1910: struct window_pane *wp = ctx->wp;
1911:
1912: if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
1913: return;
1914:
1915: if (ctx->bigger &&
1916: (ctx->xoff + ctx->ocx < ctx->ox ||
1917: ctx->xoff + ctx->ocx + ctx->num > ctx->ox + ctx->sx)) {
1918: if (!ctx->wrapped ||
1919: !tty_pane_full_width(tty, ctx) ||
1.359 nicm 1920: (tty->term->flags & TERM_NOXENL) ||
1.309 nicm 1921: ctx->xoff + ctx->ocx != 0 ||
1922: ctx->yoff + ctx->ocy != tty->cy + 1 ||
1923: tty->cx < tty->sx ||
1924: tty->cy == tty->rlower)
1925: tty_draw_pane(tty, ctx, ctx->ocy);
1926: else
1927: wp->flags |= PANE_REDRAW;
1928: return;
1929: }
1930:
1.306 nicm 1931: tty_margin_off(tty);
1.253 nicm 1932: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.239 nicm 1933:
1934: tty_attributes(tty, ctx->cell, ctx->wp);
1935: tty_putn(tty, ctx->ptr, ctx->num, ctx->num);
1.106 nicm 1936: }
1937:
1938: void
1939: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1940: {
1941: char *buf;
1942: size_t off;
1943:
1944: if (!tty_term_has(tty->term, TTYC_MS))
1945: return;
1946:
1947: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1948: buf = xmalloc(off);
1949:
1950: b64_ntop(ctx->ptr, ctx->num, buf, off);
1951: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1952:
1.138 nicm 1953: free(buf);
1.100 nicm 1954: }
1955:
1956: void
1957: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1958: {
1.256 nicm 1959: tty_add(tty, ctx->ptr, ctx->num);
1.242 nicm 1960: tty_invalidate(tty);
1.348 nicm 1961: }
1962:
1963: void
1964: tty_cmd_syncstart(struct tty *tty, __unused const struct tty_ctx *ctx)
1965: {
1966: tty_sync_start(tty);
1.1 nicm 1967: }
1968:
1.207 nicm 1969: static void
1.317 nicm 1970: tty_cell(struct tty *tty, const struct grid_cell *gc, struct window_pane *wp)
1.1 nicm 1971: {
1.298 nicm 1972: const struct grid_cell *gcp;
1.1 nicm 1973:
1974: /* Skip last character if terminal is stupid. */
1.359 nicm 1975: if ((tty->term->flags & TERM_NOXENL) &&
1.211 nicm 1976: tty->cy == tty->sy - 1 &&
1977: tty->cx == tty->sx - 1)
1.1 nicm 1978: return;
1979:
1980: /* If this is a padding character, do nothing. */
1981: if (gc->flags & GRID_FLAG_PADDING)
1982: return;
1983:
1984: /* Set the attributes. */
1.176 nicm 1985: tty_attributes(tty, gc, wp);
1.1 nicm 1986:
1.147 nicm 1987: /* Get the cell and if ASCII write with putc to do ACS translation. */
1.298 nicm 1988: gcp = tty_check_codeset(tty, gc);
1989: if (gcp->data.size == 1) {
1990: if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
1.1 nicm 1991: return;
1.298 nicm 1992: tty_putc(tty, *gcp->data.data);
1.1 nicm 1993: return;
1994: }
1995:
1.147 nicm 1996: /* Write the data. */
1.298 nicm 1997: tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
1.1 nicm 1998: }
1999:
2000: void
2001: tty_reset(struct tty *tty)
2002: {
2003: struct grid_cell *gc = &tty->cell;
2004:
1.228 nicm 2005: if (!grid_cells_equal(gc, &grid_default_cell)) {
1.285 nicm 2006: if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.228 nicm 2007: tty_putcode(tty, TTYC_RMACS);
2008: tty_putcode(tty, TTYC_SGR0);
2009: memcpy(gc, &grid_default_cell, sizeof *gc);
2010: }
1.1 nicm 2011:
1.228 nicm 2012: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2013: tty->last_wp = -1;
1.242 nicm 2014: }
2015:
2016: static void
2017: tty_invalidate(struct tty *tty)
2018: {
2019: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
2020:
2021: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2022: tty->last_wp = -1;
2023:
2024: tty->cx = tty->cy = UINT_MAX;
2025:
2026: tty->rupper = tty->rleft = UINT_MAX;
2027: tty->rlower = tty->rright = UINT_MAX;
2028:
2029: if (tty->flags & TTY_STARTED) {
1.330 nicm 2030: if (tty_use_margin(tty))
1.365 ! nicm 2031: tty_putcode(tty, TTYC_ENMG);
1.242 nicm 2032: tty_putcode(tty, TTYC_SGR0);
2033:
2034: tty->mode = ALL_MODES;
2035: tty_update_mode(tty, MODE_CURSOR, NULL);
2036:
2037: tty_cursor(tty, 0, 0);
2038: tty_region_off(tty);
2039: tty_margin_off(tty);
2040: } else
2041: tty->mode = MODE_CURSOR;
1.1 nicm 2042: }
2043:
1.214 nicm 2044: /* Turn off margin. */
2045: void
2046: tty_region_off(struct tty *tty)
2047: {
2048: tty_region(tty, 0, tty->sy - 1);
2049: }
2050:
1.40 nicm 2051: /* Set region inside pane. */
1.208 nicm 2052: static void
1.196 nicm 2053: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
2054: u_int rlower)
1.1 nicm 2055: {
1.309 nicm 2056: tty_region(tty, ctx->yoff + rupper - ctx->oy,
2057: ctx->yoff + rlower - ctx->oy);
1.39 nicm 2058: }
2059:
1.40 nicm 2060: /* Set region at absolute position. */
1.214 nicm 2061: static void
1.40 nicm 2062: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 2063: {
2064: if (tty->rlower == rlower && tty->rupper == rupper)
2065: return;
1.1 nicm 2066: if (!tty_term_has(tty->term, TTYC_CSR))
2067: return;
1.39 nicm 2068:
2069: tty->rupper = rupper;
2070: tty->rlower = rlower;
1.55 nicm 2071:
2072: /*
2073: * Some terminals (such as PuTTY) do not correctly reset the cursor to
2074: * 0,0 if it is beyond the last column (they do not reset their wrap
2075: * flag so further output causes a line feed). As a workaround, do an
2076: * explicit move to 0 first.
2077: */
1.353 nicm 2078: if (tty->cx >= tty->sx) {
2079: if (tty->cy == UINT_MAX)
2080: tty_cursor(tty, 0, 0);
2081: else
2082: tty_cursor(tty, 0, tty->cy);
2083: }
1.42 nicm 2084:
1.39 nicm 2085: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.249 nicm 2086: tty->cx = tty->cy = UINT_MAX;
1.212 nicm 2087: }
2088:
1.214 nicm 2089: /* Turn off margin. */
2090: void
2091: tty_margin_off(struct tty *tty)
2092: {
2093: tty_margin(tty, 0, tty->sx - 1);
2094: }
2095:
1.212 nicm 2096: /* Set margin inside pane. */
2097: static void
2098: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
2099: {
1.309 nicm 2100: tty_margin(tty, ctx->xoff - ctx->ox,
2101: ctx->xoff + ctx->wp->sx - 1 - ctx->ox);
1.212 nicm 2102: }
2103:
2104: /* Set margin at absolute position. */
1.214 nicm 2105: static void
1.212 nicm 2106: tty_margin(struct tty *tty, u_int rleft, u_int rright)
2107: {
2108: if (!tty_use_margin(tty))
2109: return;
2110: if (tty->rleft == rleft && tty->rright == rright)
2111: return;
2112:
1.232 nicm 2113: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.231 nicm 2114:
1.212 nicm 2115: tty->rleft = rleft;
2116: tty->rright = rright;
2117:
1.232 nicm 2118: if (rleft == 0 && rright == tty->sx - 1)
1.365 ! nicm 2119: tty_putcode(tty, TTYC_CLMG);
1.232 nicm 2120: else
1.365 ! nicm 2121: tty_putcode2(tty, TTYC_CMG, rleft, rright);
1.249 nicm 2122: tty->cx = tty->cy = UINT_MAX;
1.253 nicm 2123: }
2124:
2125: /*
2126: * Move the cursor, unless it would wrap itself when the next character is
2127: * printed.
2128: */
2129: static void
2130: tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
2131: u_int cx, u_int cy)
2132: {
1.268 nicm 2133: if (!ctx->wrapped ||
2134: !tty_pane_full_width(tty, ctx) ||
1.359 nicm 2135: (tty->term->flags & TERM_NOXENL) ||
1.253 nicm 2136: ctx->xoff + cx != 0 ||
2137: ctx->yoff + cy != tty->cy + 1 ||
1.254 nicm 2138: tty->cx < tty->sx ||
2139: tty->cy == tty->rlower)
1.253 nicm 2140: tty_cursor_pane(tty, ctx, cx, cy);
2141: else
2142: log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
1.1 nicm 2143: }
2144:
1.42 nicm 2145: /* Move cursor inside pane. */
1.208 nicm 2146: static void
1.41 nicm 2147: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
2148: {
1.309 nicm 2149: tty_cursor(tty, ctx->xoff + cx - ctx->ox, ctx->yoff + cy - ctx->oy);
1.41 nicm 2150: }
2151:
1.42 nicm 2152: /* Move cursor to absolute position. */
1.41 nicm 2153: void
2154: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 2155: {
1.42 nicm 2156: struct tty_term *term = tty->term;
2157: u_int thisx, thisy;
2158: int change;
1.354 nicm 2159:
2160: if (tty->flags & TTY_BLOCK)
2161: return;
1.77 nicm 2162:
1.42 nicm 2163: if (cx > tty->sx - 1)
2164: cx = tty->sx - 1;
2165:
2166: thisx = tty->cx;
2167: thisy = tty->cy;
2168:
2169: /* No change. */
2170: if (cx == thisx && cy == thisy)
2171: return;
2172:
1.43 nicm 2173: /* Very end of the line, just use absolute movement. */
2174: if (thisx > tty->sx - 1)
2175: goto absolute;
2176:
1.42 nicm 2177: /* Move to home position (0, 0). */
2178: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
2179: tty_putcode(tty, TTYC_HOME);
2180: goto out;
2181: }
2182:
2183: /* Zero on the next line. */
1.293 nicm 2184: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
2185: (!tty_use_margin(tty) || tty->rleft == 0)) {
1.1 nicm 2186: tty_putc(tty, '\r');
1.42 nicm 2187: tty_putc(tty, '\n');
2188: goto out;
2189: }
2190:
1.45 nicm 2191: /* Moving column or row. */
1.42 nicm 2192: if (cy == thisy) {
1.45 nicm 2193: /*
2194: * Moving column only, row staying the same.
2195: */
2196:
1.42 nicm 2197: /* To left edge. */
1.294 nicm 2198: if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
1.42 nicm 2199: tty_putc(tty, '\r');
2200: goto out;
2201: }
2202:
2203: /* One to the left. */
2204: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
2205: tty_putcode(tty, TTYC_CUB1);
2206: goto out;
2207: }
2208:
2209: /* One to the right. */
2210: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
2211: tty_putcode(tty, TTYC_CUF1);
2212: goto out;
2213: }
2214:
2215: /* Calculate difference. */
2216: change = thisx - cx; /* +ve left, -ve right */
2217:
2218: /*
2219: * Use HPA if change is larger than absolute, otherwise move
2220: * the cursor with CUB/CUF.
2221: */
1.87 nicm 2222: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 2223: tty_putcode1(tty, TTYC_HPA, cx);
2224: goto out;
1.333 nicm 2225: } else if (change > 0 &&
2226: tty_term_has(term, TTYC_CUB) &&
2227: !tty_use_margin(tty)) {
1.202 nicm 2228: if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
2229: tty_putcode(tty, TTYC_CUB1);
2230: tty_putcode(tty, TTYC_CUB1);
2231: goto out;
2232: }
1.42 nicm 2233: tty_putcode1(tty, TTYC_CUB, change);
2234: goto out;
1.333 nicm 2235: } else if (change < 0 &&
2236: tty_term_has(term, TTYC_CUF) &&
2237: !tty_use_margin(tty)) {
1.42 nicm 2238: tty_putcode1(tty, TTYC_CUF, -change);
2239: goto out;
2240: }
1.45 nicm 2241: } else if (cx == thisx) {
2242: /*
2243: * Moving row only, column staying the same.
2244: */
1.42 nicm 2245:
2246: /* One above. */
1.77 nicm 2247: if (thisy != tty->rupper &&
1.42 nicm 2248: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
2249: tty_putcode(tty, TTYC_CUU1);
2250: goto out;
2251: }
2252:
2253: /* One below. */
1.49 nicm 2254: if (thisy != tty->rlower &&
1.42 nicm 2255: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
2256: tty_putcode(tty, TTYC_CUD1);
2257: goto out;
2258: }
2259:
2260: /* Calculate difference. */
2261: change = thisy - cy; /* +ve up, -ve down */
2262:
2263: /*
1.77 nicm 2264: * Try to use VPA if change is larger than absolute or if this
2265: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 2266: */
1.87 nicm 2267: if ((u_int) abs(change) > cy ||
1.42 nicm 2268: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 2269: (change > 0 && cy - change < tty->rupper)) {
2270: if (tty_term_has(term, TTYC_VPA)) {
2271: tty_putcode1(tty, TTYC_VPA, cy);
2272: goto out;
2273: }
1.42 nicm 2274: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
2275: tty_putcode1(tty, TTYC_CUU, change);
2276: goto out;
2277: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
2278: tty_putcode1(tty, TTYC_CUD, -change);
2279: goto out;
2280: }
2281: }
2282:
1.43 nicm 2283: absolute:
1.42 nicm 2284: /* Absolute movement. */
2285: tty_putcode2(tty, TTYC_CUP, cy, cx);
2286:
2287: out:
2288: tty->cx = cx;
2289: tty->cy = cy;
1.1 nicm 2290: }
2291:
2292: void
1.176 nicm 2293: tty_attributes(struct tty *tty, const struct grid_cell *gc,
1.317 nicm 2294: struct window_pane *wp)
1.1 nicm 2295: {
1.63 nicm 2296: struct grid_cell *tc = &tty->cell, gc2;
1.255 nicm 2297: int changed;
1.61 nicm 2298:
1.228 nicm 2299: /* Ignore cell if it is the same as the last one. */
2300: if (wp != NULL &&
2301: (int)wp->id == tty->last_wp &&
1.326 nicm 2302: ~(wp->flags & PANE_STYLECHANGED) &&
1.228 nicm 2303: gc->attr == tty->last_cell.attr &&
2304: gc->fg == tty->last_cell.fg &&
1.328 nicm 2305: gc->bg == tty->last_cell.bg &&
2306: gc->us == tty->last_cell.us)
1.228 nicm 2307: return;
2308: tty->last_wp = (wp != NULL ? (int)wp->id : -1);
2309: memcpy(&tty->last_cell, gc, sizeof tty->last_cell);
2310:
2311: /* Copy cell and update default colours. */
1.85 nicm 2312: memcpy(&gc2, gc, sizeof gc2);
1.203 nicm 2313: if (wp != NULL)
2314: tty_default_colours(&gc2, wp);
1.63 nicm 2315:
1.18 nicm 2316: /*
2317: * If no setab, try to use the reverse attribute as a best-effort for a
2318: * non-default background. This is a bit of a hack but it doesn't do
2319: * any serious harm and makes a couple of applications happier.
2320: */
2321: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 2322: if (gc2.attr & GRID_ATTR_REVERSE) {
1.310 nicm 2323: if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
1.64 nicm 2324: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 2325: } else {
1.310 nicm 2326: if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
1.64 nicm 2327: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 2328: }
2329: }
1.1 nicm 2330:
1.85 nicm 2331: /* Fix up the colours if necessary. */
1.219 nicm 2332: tty_check_fg(tty, wp, &gc2);
2333: tty_check_bg(tty, wp, &gc2);
1.328 nicm 2334: tty_check_us(tty, wp, &gc2);
1.85 nicm 2335:
1.328 nicm 2336: /*
2337: * If any bits are being cleared or the underline colour is now default,
2338: * reset everything.
2339: */
2340: if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
1.64 nicm 2341: tty_reset(tty);
2342:
2343: /*
2344: * Set the colours. This may call tty_reset() (so it comes next) and
1.328 nicm 2345: * may add to (NOT remove) the desired attributes.
1.64 nicm 2346: */
1.85 nicm 2347: tty_colours(tty, &gc2);
1.64 nicm 2348:
1.1 nicm 2349: /* Filter out attribute bits already set. */
1.85 nicm 2350: changed = gc2.attr & ~tc->attr;
2351: tc->attr = gc2.attr;
1.1 nicm 2352:
2353: /* Set the attributes. */
2354: if (changed & GRID_ATTR_BRIGHT)
2355: tty_putcode(tty, TTYC_BOLD);
2356: if (changed & GRID_ATTR_DIM)
2357: tty_putcode(tty, TTYC_DIM);
1.180 nicm 2358: if (changed & GRID_ATTR_ITALICS)
2359: tty_set_italics(tty);
1.308 nicm 2360: if (changed & GRID_ATTR_ALL_UNDERSCORE) {
2361: if ((changed & GRID_ATTR_UNDERSCORE) ||
2362: !tty_term_has(tty->term, TTYC_SMULX))
2363: tty_putcode(tty, TTYC_SMUL);
2364: else if (changed & GRID_ATTR_UNDERSCORE_2)
2365: tty_putcode1(tty, TTYC_SMULX, 2);
2366: else if (changed & GRID_ATTR_UNDERSCORE_3)
2367: tty_putcode1(tty, TTYC_SMULX, 3);
2368: else if (changed & GRID_ATTR_UNDERSCORE_4)
2369: tty_putcode1(tty, TTYC_SMULX, 4);
2370: else if (changed & GRID_ATTR_UNDERSCORE_5)
2371: tty_putcode1(tty, TTYC_SMULX, 5);
2372: }
1.1 nicm 2373: if (changed & GRID_ATTR_BLINK)
2374: tty_putcode(tty, TTYC_BLINK);
2375: if (changed & GRID_ATTR_REVERSE) {
2376: if (tty_term_has(tty->term, TTYC_REV))
2377: tty_putcode(tty, TTYC_REV);
2378: else if (tty_term_has(tty->term, TTYC_SMSO))
2379: tty_putcode(tty, TTYC_SMSO);
2380: }
2381: if (changed & GRID_ATTR_HIDDEN)
2382: tty_putcode(tty, TTYC_INVIS);
1.255 nicm 2383: if (changed & GRID_ATTR_STRIKETHROUGH)
2384: tty_putcode(tty, TTYC_SMXX);
1.324 nicm 2385: if (changed & GRID_ATTR_OVERLINE)
2386: tty_putcode(tty, TTYC_SMOL);
1.285 nicm 2387: if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.1 nicm 2388: tty_putcode(tty, TTYC_SMACS);
2389: }
2390:
1.207 nicm 2391: static void
1.85 nicm 2392: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2393: {
1.61 nicm 2394: struct grid_cell *tc = &tty->cell;
1.204 nicm 2395: int have_ax;
1.61 nicm 2396:
2397: /* No changes? Nothing is necessary. */
1.328 nicm 2398: if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
1.63 nicm 2399: return;
1.1 nicm 2400:
1.61 nicm 2401: /*
2402: * Is either the default colour? This is handled specially because the
2403: * best solution might be to reset both colours to default, in which
2404: * case if only one is default need to fall onward to set the other
2405: * colour.
2406: */
1.310 nicm 2407: if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
1.61 nicm 2408: /*
2409: * If don't have AX but do have op, send sgr0 (op can't
2410: * actually be used because it is sometimes the same as sgr0
2411: * and sometimes isn't). This resets both colours to default.
2412: *
2413: * Otherwise, try to set the default colour only as needed.
2414: */
1.174 nicm 2415: have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61 nicm 2416: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
2417: tty_reset(tty);
2418: else {
1.310 nicm 2419: if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
1.61 nicm 2420: if (have_ax)
2421: tty_puts(tty, "\033[39m");
1.204 nicm 2422: else if (tc->fg != 7)
1.61 nicm 2423: tty_putcode1(tty, TTYC_SETAF, 7);
1.310 nicm 2424: tc->fg = gc->fg;
1.61 nicm 2425: }
1.310 nicm 2426: if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
1.77 nicm 2427: if (have_ax)
1.61 nicm 2428: tty_puts(tty, "\033[49m");
1.204 nicm 2429: else if (tc->bg != 0)
1.61 nicm 2430: tty_putcode1(tty, TTYC_SETAB, 0);
1.312 nicm 2431: tc->bg = gc->bg;
1.61 nicm 2432: }
2433: }
2434: }
1.1 nicm 2435:
1.61 nicm 2436: /* Set the foreground colour. */
1.310 nicm 2437: if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
1.85 nicm 2438: tty_colours_fg(tty, gc);
1.1 nicm 2439:
1.61 nicm 2440: /*
2441: * Set the background colour. This must come after the foreground as
2442: * tty_colour_fg() can call tty_reset().
2443: */
1.310 nicm 2444: if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
1.73 nicm 2445: tty_colours_bg(tty, gc);
1.328 nicm 2446:
2447: /* Set the underscore color. */
2448: if (gc->us != tc->us)
2449: tty_colours_us(tty, gc);
1.1 nicm 2450: }
2451:
1.219 nicm 2452: static void
1.317 nicm 2453: tty_check_fg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
1.85 nicm 2454: {
1.204 nicm 2455: u_char r, g, b;
2456: u_int colours;
1.223 nicm 2457: int c;
1.85 nicm 2458:
1.288 nicm 2459: /*
2460: * Perform substitution if this pane has a palette. If the bright
2461: * attribute is set, use the bright entry in the palette by changing to
2462: * the aixterm colour.
2463: */
2464: if (~gc->flags & GRID_FLAG_NOPALETTE) {
2465: c = gc->fg;
1.289 nicm 2466: if (c < 8 && gc->attr & GRID_ATTR_BRIGHT)
1.288 nicm 2467: c += 90;
2468: if ((c = window_pane_get_palette(wp, c)) != -1)
2469: gc->fg = c;
2470: }
1.219 nicm 2471:
1.199 nicm 2472: /* Is this a 24-bit colour? */
1.204 nicm 2473: if (gc->fg & COLOUR_FLAG_RGB) {
1.199 nicm 2474: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.359 nicm 2475: if (tty->term->flags & TERM_RGBCOLOURS)
1.200 nicm 2476: return;
1.341 nicm 2477: colour_split_rgb(gc->fg, &r, &g, &b);
2478: gc->fg = colour_find_rgb(r, g, b);
1.199 nicm 2479: }
1.224 nicm 2480:
2481: /* How many colours does this terminal have? */
1.359 nicm 2482: if (tty->term->flags & TERM_256COLOURS)
1.224 nicm 2483: colours = 256;
2484: else
2485: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 2486:
1.85 nicm 2487: /* Is this a 256-colour colour? */
1.204 nicm 2488: if (gc->fg & COLOUR_FLAG_256) {
1.85 nicm 2489: /* And not a 256 colour mode? */
1.224 nicm 2490: if (colours != 256) {
1.85 nicm 2491: gc->fg = colour_256to16(gc->fg);
2492: if (gc->fg & 8) {
2493: gc->fg &= 7;
1.175 nicm 2494: if (colours >= 16)
2495: gc->fg += 90;
1.332 nicm 2496: }
1.85 nicm 2497: }
2498: return;
2499: }
2500:
2501: /* Is this an aixterm colour? */
2502: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
2503: gc->fg -= 90;
2504: gc->attr |= GRID_ATTR_BRIGHT;
2505: }
2506: }
2507:
1.219 nicm 2508: static void
1.317 nicm 2509: tty_check_bg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
1.85 nicm 2510: {
1.204 nicm 2511: u_char r, g, b;
2512: u_int colours;
1.223 nicm 2513: int c;
1.85 nicm 2514:
1.288 nicm 2515: /* Perform substitution if this pane has a palette. */
2516: if (~gc->flags & GRID_FLAG_NOPALETTE) {
2517: if ((c = window_pane_get_palette(wp, gc->bg)) != -1)
2518: gc->bg = c;
2519: }
1.219 nicm 2520:
1.199 nicm 2521: /* Is this a 24-bit colour? */
1.204 nicm 2522: if (gc->bg & COLOUR_FLAG_RGB) {
1.199 nicm 2523: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.359 nicm 2524: if (tty->term->flags & TERM_RGBCOLOURS)
1.200 nicm 2525: return;
1.341 nicm 2526: colour_split_rgb(gc->bg, &r, &g, &b);
2527: gc->bg = colour_find_rgb(r, g, b);
1.199 nicm 2528: }
1.224 nicm 2529:
2530: /* How many colours does this terminal have? */
1.359 nicm 2531: if (tty->term->flags & TERM_256COLOURS)
1.224 nicm 2532: colours = 256;
2533: else
2534: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 2535:
1.85 nicm 2536: /* Is this a 256-colour colour? */
1.204 nicm 2537: if (gc->bg & COLOUR_FLAG_256) {
1.85 nicm 2538: /*
2539: * And not a 256 colour mode? Translate to 16-colour
2540: * palette. Bold background doesn't exist portably, so just
2541: * discard the bold bit if set.
2542: */
1.224 nicm 2543: if (colours != 256) {
1.85 nicm 2544: gc->bg = colour_256to16(gc->bg);
1.175 nicm 2545: if (gc->bg & 8) {
1.85 nicm 2546: gc->bg &= 7;
1.175 nicm 2547: if (colours >= 16)
1.322 nicm 2548: gc->bg += 90;
1.175 nicm 2549: }
1.85 nicm 2550: }
2551: return;
2552: }
2553:
2554: /* Is this an aixterm colour? */
1.175 nicm 2555: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85 nicm 2556: gc->bg -= 90;
2557: }
2558:
1.207 nicm 2559: static void
1.335 nicm 2560: tty_check_us(__unused struct tty *tty, struct window_pane *wp,
2561: struct grid_cell *gc)
1.328 nicm 2562: {
2563: int c;
2564:
2565: /* Perform substitution if this pane has a palette. */
2566: if (~gc->flags & GRID_FLAG_NOPALETTE) {
2567: if ((c = window_pane_get_palette(wp, gc->us)) != -1)
2568: gc->us = c;
2569: }
2570:
2571: /* Underscore colour is set as RGB so convert a 256 colour to RGB. */
2572: if (gc->us & COLOUR_FLAG_256)
2573: gc->us = colour_256toRGB (gc->us);
2574: }
2575:
2576: static void
1.85 nicm 2577: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2578: {
1.61 nicm 2579: struct grid_cell *tc = &tty->cell;
1.79 nicm 2580: char s[32];
1.1 nicm 2581:
1.204 nicm 2582: /* Is this a 24-bit or 256-colour colour? */
1.302 nicm 2583: if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
1.204 nicm 2584: if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61 nicm 2585: goto save_fg;
1.199 nicm 2586: /* Should not get here, already converted in tty_check_fg. */
1.85 nicm 2587: return;
1.1 nicm 2588: }
2589:
1.79 nicm 2590: /* Is this an aixterm bright colour? */
1.204 nicm 2591: if (gc->fg >= 90 && gc->fg <= 97) {
1.359 nicm 2592: if (tty->term->flags & TERM_256COLOURS) {
1.323 nicm 2593: xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
2594: tty_puts(tty, s);
2595: } else
2596: tty_putcode1(tty, TTYC_SETAF, gc->fg - 90 + 8);
1.85 nicm 2597: goto save_fg;
1.79 nicm 2598: }
2599:
1.61 nicm 2600: /* Otherwise set the foreground colour. */
1.204 nicm 2601: tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61 nicm 2602:
1.77 nicm 2603: save_fg:
1.61 nicm 2604: /* Save the new values in the terminal current cell. */
1.204 nicm 2605: tc->fg = gc->fg;
1.1 nicm 2606: }
2607:
1.207 nicm 2608: static void
1.73 nicm 2609: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2610: {
1.61 nicm 2611: struct grid_cell *tc = &tty->cell;
1.79 nicm 2612: char s[32];
1.1 nicm 2613:
1.204 nicm 2614: /* Is this a 24-bit or 256-colour colour? */
1.302 nicm 2615: if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
1.204 nicm 2616: if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61 nicm 2617: goto save_bg;
1.199 nicm 2618: /* Should not get here, already converted in tty_check_bg. */
1.85 nicm 2619: return;
1.79 nicm 2620: }
2621:
2622: /* Is this an aixterm bright colour? */
1.204 nicm 2623: if (gc->bg >= 90 && gc->bg <= 97) {
1.359 nicm 2624: if (tty->term->flags & TERM_256COLOURS) {
1.323 nicm 2625: xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
2626: tty_puts(tty, s);
2627: } else
2628: tty_putcode1(tty, TTYC_SETAB, gc->bg - 90 + 8);
1.175 nicm 2629: goto save_bg;
1.1 nicm 2630: }
2631:
1.61 nicm 2632: /* Otherwise set the background colour. */
1.204 nicm 2633: tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61 nicm 2634:
2635: save_bg:
2636: /* Save the new values in the terminal current cell. */
1.204 nicm 2637: tc->bg = gc->bg;
1.328 nicm 2638: }
2639:
2640: static void
2641: tty_colours_us(struct tty *tty, const struct grid_cell *gc)
2642: {
2643: struct grid_cell *tc = &tty->cell;
2644: u_int c;
2645: u_char r, g, b;
2646:
2647: /* Must be an RGB colour - this should never happen. */
2648: if (~gc->us & COLOUR_FLAG_RGB)
2649: return;
2650:
2651: /*
2652: * Setulc follows the ncurses(3) one argument "direct colour"
2653: * capability format. Calculate the colour value.
2654: */
2655: colour_split_rgb(gc->us, &r, &g, &b);
2656: c = (65536 * r) + (256 * g) + b;
2657:
2658: /* Write the colour. */
2659: tty_putcode1(tty, TTYC_SETULC, c);
2660:
2661: /* Save the new values in the terminal current cell. */
2662: tc->us = gc->us;
1.61 nicm 2663: }
2664:
1.207 nicm 2665: static int
1.204 nicm 2666: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61 nicm 2667: {
1.204 nicm 2668: u_char r, g, b;
1.61 nicm 2669:
1.204 nicm 2670: if (colour & COLOUR_FLAG_256) {
1.359 nicm 2671: if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
2672: tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
2673: else if (tty_term_has(tty->term, TTYC_SETAB))
2674: tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
2675: return (0);
1.204 nicm 2676: }
2677:
2678: if (colour & COLOUR_FLAG_RGB) {
1.341 nicm 2679: colour_split_rgb(colour & 0xffffff, &r, &g, &b);
1.359 nicm 2680: if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
1.286 nicm 2681: tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
1.359 nicm 2682: else if (tty_term_has(tty->term, TTYC_SETRGBB))
1.286 nicm 2683: tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
1.165 nicm 2684: return (0);
2685: }
1.61 nicm 2686:
1.165 nicm 2687: return (-1);
1.176 nicm 2688: }
2689:
1.207 nicm 2690: static void
1.317 nicm 2691: tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
1.176 nicm 2692: {
1.326 nicm 2693: struct options *oo = wp->options;
2694: struct style *style, *active_style;
2695: int c;
2696:
2697: if (wp->flags & PANE_STYLECHANGED) {
2698: wp->flags &= ~PANE_STYLECHANGED;
2699:
2700: active_style = options_get_style(oo, "window-active-style");
2701: style = options_get_style(oo, "window-style");
2702:
2703: style_copy(&wp->cached_active_style, active_style);
2704: style_copy(&wp->cached_style, style);
1.203 nicm 2705: } else {
1.326 nicm 2706: active_style = &wp->cached_active_style;
2707: style = &wp->cached_style;
1.203 nicm 2708: }
1.176 nicm 2709:
1.204 nicm 2710: if (gc->fg == 8) {
1.326 nicm 2711: if (wp == wp->window->active && active_style->gc.fg != 8)
2712: gc->fg = active_style->gc.fg;
1.204 nicm 2713: else
1.326 nicm 2714: gc->fg = style->gc.fg;
1.219 nicm 2715:
1.310 nicm 2716: if (gc->fg != 8) {
2717: c = window_pane_get_palette(wp, gc->fg);
2718: if (c != -1)
2719: gc->fg = c;
2720: }
1.176 nicm 2721: }
2722:
1.204 nicm 2723: if (gc->bg == 8) {
1.326 nicm 2724: if (wp == wp->window->active && active_style->gc.bg != 8)
2725: gc->bg = active_style->gc.bg;
1.204 nicm 2726: else
1.326 nicm 2727: gc->bg = style->gc.bg;
1.219 nicm 2728:
1.310 nicm 2729: if (gc->bg != 8) {
2730: c = window_pane_get_palette(wp, gc->bg);
2731: if (c != -1)
2732: gc->bg = c;
2733: }
1.176 nicm 2734: }
1.210 nicm 2735: }
2736:
2737: static void
1.317 nicm 2738: tty_default_attributes(struct tty *tty, struct window_pane *wp, u_int bg)
1.210 nicm 2739: {
2740: static struct grid_cell gc;
2741:
2742: memcpy(&gc, &grid_default_cell, sizeof gc);
2743: gc.bg = bg;
2744: tty_attributes(tty, &gc, wp);
1.1 nicm 2745: }