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