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