Annotation of src/usr.bin/tmux/tty.c, Revision 1.360
1.360 ! nicm 1: /* $OpenBSD: tty.c,v 1.359 2020/04/20 13:25:36 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);
290: tty->flags |= (TTY_HAVEDA|TTY_HAVEDSR);
291: }
292:
1.244 nicm 293: void
294: tty_start_tty(struct tty *tty)
1.1 nicm 295: {
1.285 nicm 296: struct client *c = tty->client;
297: struct termios tio;
1.340 nicm 298: struct timeval tv = { .tv_sec = 1 };
1.33 nicm 299:
1.244 nicm 300: if (tty->fd != -1 && tcgetattr(tty->fd, &tty->tio) == 0) {
301: setblocking(tty->fd, 0);
302: event_add(&tty->event_in, NULL);
303:
304: memcpy(&tio, &tty->tio, sizeof tio);
305: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
306: tio.c_iflag |= IGNBRK;
307: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
308: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
309: ECHOPRT|ECHOKE|ISIG);
310: tio.c_cc[VMIN] = 1;
311: tio.c_cc[VTIME] = 0;
312: if (tcsetattr(tty->fd, TCSANOW, &tio) == 0)
313: tcflush(tty->fd, TCIOFLUSH);
314: }
1.1 nicm 315:
1.10 nicm 316: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 317:
1.252 nicm 318: tty_putcode(tty, TTYC_SMKX);
1.285 nicm 319: tty_putcode(tty, TTYC_CLEAR);
320:
321: if (tty_acs_needed(tty)) {
322: log_debug("%s: using capabilities for ACS", c->name);
1.90 nicm 323: tty_putcode(tty, TTYC_ENACS);
1.285 nicm 324: } else
325: log_debug("%s: using UTF-8 for ACS", c->name);
1.1 nicm 326:
327: tty_putcode(tty, TTYC_CNORM);
1.343 nicm 328: if (tty_term_has(tty->term, TTYC_KMOUS)) {
329: tty_puts(tty, "\033[?1000l\033[?1002l\033[?1003l");
330: tty_puts(tty, "\033[?1006l\033[?1005l");
331: }
1.121 nicm 332:
1.189 nicm 333: if (tty_term_flag(tty->term, TTYC_XT)) {
1.191 nicm 334: if (options_get_number(global_options, "focus-events")) {
1.162 nicm 335: tty->flags |= TTY_FOCUS;
336: tty_puts(tty, "\033[?1004h");
337: }
1.352 nicm 338: tty_puts(tty, "\033[?7727h");
1.347 nicm 339: }
1.1 nicm 340:
1.340 nicm 341: evtimer_set(&tty->start_timer, tty_start_timer_callback, tty);
342: evtimer_add(&tty->start_timer, &tv);
343:
1.20 nicm 344: tty->flags |= TTY_STARTED;
1.242 nicm 345: tty_invalidate(tty);
1.107 nicm 346:
1.336 nicm 347: if (*tty->ccolour != '\0')
348: tty_force_cursor_colour(tty, "");
1.177 nicm 349:
350: tty->mouse_drag_flag = 0;
351: tty->mouse_drag_update = NULL;
352: tty->mouse_drag_release = NULL;
1.347 nicm 353: }
354:
355: void
356: tty_send_requests(struct tty *tty)
357: {
358: if (~tty->flags & TTY_STARTED)
359: return;
360:
361: if (tty_term_flag(tty->term, TTYC_XT)) {
362: if (~tty->flags & TTY_HAVEDA)
1.351 nicm 363: tty_puts(tty, "\033[>c");
1.347 nicm 364: if (~tty->flags & TTY_HAVEDSR)
365: tty_puts(tty, "\033[1337n");
366: } else
367: tty->flags |= (TTY_HAVEDA|TTY_HAVEDSR);
1.128 nicm 368: }
369:
370: void
1.1 nicm 371: tty_stop_tty(struct tty *tty)
372: {
373: struct winsize ws;
374:
1.20 nicm 375: if (!(tty->flags & TTY_STARTED))
376: return;
377: tty->flags &= ~TTY_STARTED;
1.340 nicm 378:
379: evtimer_del(&tty->start_timer);
1.20 nicm 380:
1.266 nicm 381: event_del(&tty->timer);
382: tty->flags &= ~TTY_BLOCK;
383:
1.244 nicm 384: event_del(&tty->event_in);
385: event_del(&tty->event_out);
1.66 nicm 386:
1.1 nicm 387: /*
388: * Be flexible about error handling and try not kill the server just
389: * because the fd is invalid. Things like ssh -t can easily leave us
390: * with a dead tty.
391: */
392: if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
393: return;
394: if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
395: return;
396:
397: tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.285 nicm 398: if (tty_acs_needed(tty))
1.90 nicm 399: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1 nicm 400: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
401: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
402: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.160 nicm 403: if (tty_term_has(tty->term, TTYC_SS) && tty->cstyle != 0) {
404: if (tty_term_has(tty->term, TTYC_SE))
405: tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
1.109 nicm 406: else
1.160 nicm 407: tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
1.108 nicm 408: }
1.173 nicm 409: if (tty->mode & MODE_BRACKETPASTE)
410: tty_raw(tty, "\033[?2004l");
1.336 nicm 411: if (*tty->ccolour != '\0')
412: tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1 nicm 413:
414: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
1.343 nicm 415: if (tty_term_has(tty->term, TTYC_KMOUS)) {
416: tty_raw(tty, "\033[?1000l\033[?1002l\033[?1003l");
417: tty_raw(tty, "\033[?1006l\033[?1005l");
418: }
1.151 nicm 419:
1.189 nicm 420: if (tty_term_flag(tty->term, TTYC_XT)) {
1.162 nicm 421: if (tty->flags & TTY_FOCUS) {
422: tty->flags &= ~TTY_FOCUS;
1.172 nicm 423: tty_raw(tty, "\033[?1004l");
1.162 nicm 424: }
1.352 nicm 425: tty_raw(tty, "\033[?7727l");
1.162 nicm 426: }
1.10 nicm 427:
1.212 nicm 428: if (tty_use_margin(tty))
429: tty_raw(tty, "\033[?69l"); /* DECLRMM */
1.10 nicm 430: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150 nicm 431:
432: setblocking(tty->fd, 1);
1.1 nicm 433: }
434:
435: void
1.20 nicm 436: tty_close(struct tty *tty)
1.1 nicm 437: {
1.123 nicm 438: if (event_initialized(&tty->key_timer))
439: evtimer_del(&tty->key_timer);
1.20 nicm 440: tty_stop_tty(tty);
1.1 nicm 441:
1.21 nicm 442: if (tty->flags & TTY_OPENED) {
1.244 nicm 443: evbuffer_free(tty->in);
444: event_del(&tty->event_in);
445: evbuffer_free(tty->out);
446: event_del(&tty->event_out);
1.66 nicm 447:
1.21 nicm 448: tty_term_free(tty->term);
449: tty_keys_free(tty);
450:
451: tty->flags &= ~TTY_OPENED;
452: }
1.1 nicm 453:
1.21 nicm 454: if (tty->fd != -1) {
455: close(tty->fd);
456: tty->fd = -1;
457: }
1.1 nicm 458: }
459:
460: void
1.20 nicm 461: tty_free(struct tty *tty)
1.1 nicm 462: {
1.20 nicm 463: tty_close(tty);
1.138 nicm 464: free(tty->ccolour);
1.1 nicm 465: }
466:
467: void
1.359 nicm 468: tty_update_features(struct tty *tty)
1.212 nicm 469: {
1.359 nicm 470: struct client *c = tty->client;
471:
472: tty_apply_features(tty->term, c->term_features);
1.212 nicm 473:
474: if (tty_use_margin(tty))
475: tty_puts(tty, "\033[?69h"); /* DECLRMM */
476: }
477:
478: void
1.1 nicm 479: tty_raw(struct tty *tty, const char *s)
480: {
1.150 nicm 481: ssize_t n, slen;
482: u_int i;
483:
484: slen = strlen(s);
485: for (i = 0; i < 5; i++) {
486: n = write(tty->fd, s, slen);
487: if (n >= 0) {
488: s += n;
489: slen -= n;
490: if (slen == 0)
491: break;
492: } else if (n == -1 && errno != EAGAIN)
493: break;
494: usleep(100);
495: }
1.1 nicm 496: }
497:
498: void
499: tty_putcode(struct tty *tty, enum tty_code_code code)
500: {
501: tty_puts(tty, tty_term_string(tty->term, code));
502: }
503:
504: void
505: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
506: {
507: if (a < 0)
508: return;
509: tty_puts(tty, tty_term_string1(tty->term, code, a));
510: }
511:
512: void
513: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
514: {
515: if (a < 0 || b < 0)
516: return;
517: tty_puts(tty, tty_term_string2(tty->term, code, a, b));
518: }
519:
520: void
1.286 nicm 521: tty_putcode3(struct tty *tty, enum tty_code_code code, int a, int b, int c)
522: {
523: if (a < 0 || b < 0 || c < 0)
524: return;
525: tty_puts(tty, tty_term_string3(tty->term, code, a, b, c));
526: }
527:
528: void
1.107 nicm 529: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
530: {
531: if (a != NULL)
532: tty_puts(tty, tty_term_ptr1(tty->term, code, a));
533: }
534:
535: void
1.167 nicm 536: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
537: const void *b)
1.106 nicm 538: {
539: if (a != NULL && b != NULL)
540: tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
541: }
542:
1.243 nicm 543: static void
544: tty_add(struct tty *tty, const char *buf, size_t len)
545: {
1.258 nicm 546: struct client *c = tty->client;
547:
1.266 nicm 548: if (tty->flags & TTY_BLOCK) {
549: tty->discarded += len;
550: return;
551: }
552:
1.244 nicm 553: evbuffer_add(tty->out, buf, len);
1.270 nicm 554: log_debug("%s: %.*s", c->name, (int)len, buf);
1.266 nicm 555: c->written += len;
1.243 nicm 556:
557: if (tty_log_fd != -1)
558: write(tty_log_fd, buf, len);
1.244 nicm 559: if (tty->flags & TTY_STARTED)
560: event_add(&tty->event_out, NULL);
1.243 nicm 561: }
562:
1.106 nicm 563: void
1.1 nicm 564: tty_puts(struct tty *tty, const char *s)
565: {
1.243 nicm 566: if (*s != '\0')
567: tty_add(tty, s, strlen(s));
1.1 nicm 568: }
569:
570: void
571: tty_putc(struct tty *tty, u_char ch)
572: {
1.90 nicm 573: const char *acs;
1.1 nicm 574:
1.359 nicm 575: if ((tty->term->flags & TERM_NOXENL) &&
1.325 nicm 576: ch >= 0x20 && ch != 0x7f &&
577: tty->cy == tty->sy - 1 &&
578: tty->cx + 1 >= tty->sx)
579: return;
580:
1.90 nicm 581: if (tty->cell.attr & GRID_ATTR_CHARSET) {
582: acs = tty_acs_get(tty, ch);
1.243 nicm 583: if (acs != NULL)
584: tty_add(tty, acs, strlen(acs));
585: else
586: tty_add(tty, &ch, 1);
587: } else
588: tty_add(tty, &ch, 1);
1.1 nicm 589:
590: if (ch >= 0x20 && ch != 0x7f) {
1.211 nicm 591: if (tty->cx >= tty->sx) {
1.46 nicm 592: tty->cx = 1;
593: if (tty->cy != tty->rlower)
594: tty->cy++;
1.211 nicm 595:
596: /*
597: * On !xenl terminals, force the cursor position to
598: * where we think it should be after a line wrap - this
599: * means it works on sensible terminals as well.
600: */
1.359 nicm 601: if (tty->term->flags & TERM_NOXENL)
1.211 nicm 602: tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
1.1 nicm 603: } else
604: tty->cx++;
605: }
606: }
607:
608: void
1.147 nicm 609: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5 nicm 610: {
1.359 nicm 611: if ((tty->term->flags & TERM_NOXENL) &&
1.325 nicm 612: tty->cy == tty->sy - 1 &&
613: tty->cx + len >= tty->sx)
614: len = tty->sx - tty->cx - 1;
615:
1.243 nicm 616: tty_add(tty, buf, len);
1.268 nicm 617: if (tty->cx + width > tty->sx) {
618: tty->cx = (tty->cx + width) - tty->sx;
619: if (tty->cx <= tty->sx)
620: tty->cy++;
621: else
622: tty->cx = tty->cy = UINT_MAX;
623: } else
1.254 nicm 624: tty->cx += width;
1.5 nicm 625: }
626:
1.207 nicm 627: static void
1.180 nicm 628: tty_set_italics(struct tty *tty)
629: {
630: const char *s;
631:
632: if (tty_term_has(tty->term, TTYC_SITM)) {
1.191 nicm 633: s = options_get_string(global_options, "default-terminal");
1.180 nicm 634: if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
635: tty_putcode(tty, TTYC_SITM);
636: return;
637: }
638: }
639: tty_putcode(tty, TTYC_SMSO);
640: }
641:
642: void
1.1 nicm 643: tty_set_title(struct tty *tty, const char *title)
644: {
1.105 nicm 645: if (!tty_term_has(tty->term, TTYC_TSL) ||
646: !tty_term_has(tty->term, TTYC_FSL))
1.1 nicm 647: return;
648:
1.105 nicm 649: tty_putcode(tty, TTYC_TSL);
1.1 nicm 650: tty_puts(tty, title);
1.105 nicm 651: tty_putcode(tty, TTYC_FSL);
1.1 nicm 652: }
653:
1.208 nicm 654: static void
1.107 nicm 655: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
656: {
657: if (*ccolour == '\0')
658: tty_putcode(tty, TTYC_CR);
659: else
1.160 nicm 660: tty_putcode_ptr1(tty, TTYC_CS, ccolour);
1.138 nicm 661: free(tty->ccolour);
1.107 nicm 662: tty->ccolour = xstrdup(ccolour);
663: }
664:
665: void
666: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1 nicm 667: {
1.344 nicm 668: struct client *c = tty->client;
669: int changed;
1.1 nicm 670:
1.197 nicm 671: if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0)
1.107 nicm 672: tty_force_cursor_colour(tty, s->ccolour);
673:
1.1 nicm 674: if (tty->flags & TTY_NOCURSOR)
675: mode &= ~MODE_CURSOR;
676:
677: changed = mode ^ tty->mode;
1.344 nicm 678: log_debug("%s: update mode %x to %x", c->name, tty->mode, mode);
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)
731: tty_puts(tty, "\033[?2004h");
732: else
733: tty_puts(tty, "\033[?2004l");
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.359 nicm 1430: if ((~tty->flags & TTY_SYNCING) && tty_term_has(tty->term, TTYC_SYNC)) {
1.360 ! nicm 1431: tty_putcode1(tty, TTYC_SYNC, 1);
1.357 nicm 1432: tty->flags |= TTY_SYNCING;
1433: }
1.348 nicm 1434: }
1435:
1436: void
1437: tty_sync_end(struct tty *tty)
1438: {
1.359 nicm 1439: if ((tty->flags & TTY_SYNCING) && tty_term_has(tty->term, TTYC_SYNC)) {
1.360 ! nicm 1440: tty_putcode1(tty, TTYC_SYNC, 2);
1.357 nicm 1441: tty->flags &= ~TTY_SYNCING;
1442: }
1.348 nicm 1443: }
1444:
1.201 nicm 1445: static int
1.182 nicm 1446: tty_client_ready(struct client *c, struct window_pane *wp)
1447: {
1448: if (c->session == NULL || c->tty.term == NULL)
1449: return (0);
1.305 nicm 1450: if (c->flags & (CLIENT_REDRAWWINDOW|CLIENT_SUSPENDED))
1.182 nicm 1451: return (0);
1452: if (c->tty.flags & TTY_FREEZE)
1453: return (0);
1454: if (c->session->curw->window != wp->window)
1455: return (0);
1.309 nicm 1456: if (wp->layout_cell == NULL)
1457: return (0);
1.182 nicm 1458: return (1);
1459: }
1460:
1.15 nicm 1461: void
1.182 nicm 1462: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1463: struct tty_ctx *ctx)
1.15 nicm 1464: {
1465: struct window_pane *wp = ctx->wp;
1466: struct client *c;
1467:
1468: if (wp == NULL)
1469: return;
1.309 nicm 1470: if (wp->flags & (PANE_REDRAW|PANE_DROP))
1.15 nicm 1471: return;
1472:
1.178 nicm 1473: TAILQ_FOREACH(c, &clients, entry) {
1.182 nicm 1474: if (!tty_client_ready(c, wp))
1.15 nicm 1475: continue;
1.358 nicm 1476: if (c->flags & CLIENT_REDRAWPANES) {
1477: /*
1478: * Redraw is already deferred to redraw another pane -
1479: * redraw this one also when that happens.
1480: */
1481: wp->flags |= PANE_REDRAW;
1482: break;
1483: }
1.15 nicm 1484:
1.309 nicm 1485: ctx->bigger = tty_window_offset(&c->tty, &ctx->ox, &ctx->oy,
1486: &ctx->sx, &ctx->sy);
1487:
1.139 nicm 1488: ctx->xoff = wp->xoff;
1489: ctx->yoff = wp->yoff;
1.309 nicm 1490:
1.139 nicm 1491: if (status_at_line(c) == 0)
1.309 nicm 1492: ctx->yoff += status_line_size(c);
1.113 nicm 1493:
1.139 nicm 1494: cmdfn(&c->tty, ctx);
1.1 nicm 1495: }
1496: }
1497:
1498: void
1.24 nicm 1499: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1500: {
1.77 nicm 1501: struct window_pane *wp = ctx->wp;
1.1 nicm 1502:
1.309 nicm 1503: if (ctx->bigger ||
1504: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1505: tty_fake_bce(tty, wp, ctx->bg) ||
1506: (!tty_term_has(tty->term, TTYC_ICH) &&
1507: !tty_term_has(tty->term, TTYC_ICH1))) {
1.309 nicm 1508: tty_draw_pane(tty, ctx, ctx->ocy);
1.1 nicm 1509: return;
1510: }
1511:
1.210 nicm 1512: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1513:
1.77 nicm 1514: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 1515:
1.206 nicm 1516: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1 nicm 1517: }
1518:
1519: void
1.24 nicm 1520: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1521: {
1.77 nicm 1522: struct window_pane *wp = ctx->wp;
1.1 nicm 1523:
1.309 nicm 1524: if (ctx->bigger ||
1525: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1526: tty_fake_bce(tty, wp, ctx->bg) ||
1.26 nicm 1527: (!tty_term_has(tty->term, TTYC_DCH) &&
1528: !tty_term_has(tty->term, TTYC_DCH1))) {
1.309 nicm 1529: tty_draw_pane(tty, ctx, ctx->ocy);
1.1 nicm 1530: return;
1531: }
1532:
1.210 nicm 1533: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1534:
1.77 nicm 1535: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 1536:
1.206 nicm 1537: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146 nicm 1538: }
1539:
1540: void
1541: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1542: {
1.309 nicm 1543: if (ctx->bigger) {
1544: tty_draw_pane(tty, ctx, ctx->ocy);
1545: return;
1546: }
1547:
1.275 nicm 1548: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.146 nicm 1549:
1550: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1551:
1.210 nicm 1552: if (tty_term_has(tty->term, TTYC_ECH) &&
1.262 nicm 1553: !tty_fake_bce(tty, ctx->wp, 8))
1.146 nicm 1554: tty_putcode1(tty, TTYC_ECH, ctx->num);
1.257 nicm 1555: else
1556: tty_repeat_space(tty, ctx->num);
1.1 nicm 1557: }
1558:
1559: void
1.24 nicm 1560: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1561: {
1.309 nicm 1562: if (ctx->bigger ||
1563: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1564: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.77 nicm 1565: !tty_term_has(tty->term, TTYC_CSR) ||
1.307 nicm 1566: !tty_term_has(tty->term, TTYC_IL1) ||
1567: ctx->wp->sx == 1 ||
1568: ctx->wp->sy == 1) {
1.14 nicm 1569: tty_redraw_region(tty, ctx);
1.1 nicm 1570: return;
1571: }
1572:
1.210 nicm 1573: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1 nicm 1574:
1.77 nicm 1575: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1576: tty_margin_off(tty);
1.77 nicm 1577: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1578:
1.12 nicm 1579: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.279 nicm 1580: tty->cx = tty->cy = UINT_MAX;
1.1 nicm 1581: }
1582:
1583: void
1.24 nicm 1584: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1585: {
1.309 nicm 1586: if (ctx->bigger ||
1587: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1588: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.72 nicm 1589: !tty_term_has(tty->term, TTYC_CSR) ||
1.307 nicm 1590: !tty_term_has(tty->term, TTYC_DL1) ||
1591: ctx->wp->sx == 1 ||
1592: ctx->wp->sy == 1) {
1.14 nicm 1593: tty_redraw_region(tty, ctx);
1.1 nicm 1594: return;
1595: }
1596:
1.210 nicm 1597: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1 nicm 1598:
1.77 nicm 1599: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1600: tty_margin_off(tty);
1.77 nicm 1601: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1602:
1.12 nicm 1603: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.279 nicm 1604: tty->cx = tty->cy = UINT_MAX;
1.1 nicm 1605: }
1606:
1607: void
1.24 nicm 1608: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1609: {
1.77 nicm 1610: struct window_pane *wp = ctx->wp;
1.309 nicm 1611: u_int nx;
1.1 nicm 1612:
1.210 nicm 1613: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1614:
1.271 nicm 1615: nx = screen_size_x(wp->screen);
1.309 nicm 1616: tty_clear_pane_line(tty, ctx, ctx->ocy, 0, nx, ctx->bg);
1.1 nicm 1617: }
1618:
1619: void
1.24 nicm 1620: tty_cmd_clearendofline(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) - ctx->ocx;
1.309 nicm 1628: tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1.1 nicm 1629: }
1630:
1631: void
1.24 nicm 1632: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1633: {
1.210 nicm 1634: struct window_pane *wp = ctx->wp;
1635:
1636: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1637:
1.309 nicm 1638: tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1.1 nicm 1639: }
1640:
1641: void
1.24 nicm 1642: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1643: {
1.278 nicm 1644: struct window_pane *wp = ctx->wp;
1645:
1.56 nicm 1646: if (ctx->ocy != ctx->orupper)
1647: return;
1648:
1.309 nicm 1649: if (ctx->bigger ||
1.331 nicm 1650: (!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.278 nicm 1651: tty_fake_bce(tty, wp, 8) ||
1.70 nicm 1652: !tty_term_has(tty->term, TTYC_CSR) ||
1.331 nicm 1653: (!tty_term_has(tty->term, TTYC_RI) &&
1654: !tty_term_has(tty->term, TTYC_RIN)) ||
1.307 nicm 1655: ctx->wp->sx == 1 ||
1656: ctx->wp->sy == 1) {
1.14 nicm 1657: tty_redraw_region(tty, ctx);
1.1 nicm 1658: return;
1659: }
1660:
1.278 nicm 1661: tty_default_attributes(tty, wp, ctx->bg);
1.77 nicm 1662:
1.56 nicm 1663: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.331 nicm 1664: tty_margin_pane(tty, ctx);
1.56 nicm 1665: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 1666:
1.331 nicm 1667: if (tty_term_has(tty->term, TTYC_RI))
1668: tty_putcode(tty, TTYC_RI);
1669: else
1670: tty_putcode1(tty, TTYC_RIN, 1);
1.1 nicm 1671: }
1672:
1673: void
1.24 nicm 1674: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1675: {
1.77 nicm 1676: struct window_pane *wp = ctx->wp;
1.1 nicm 1677:
1.56 nicm 1678: if (ctx->ocy != ctx->orlower)
1679: return;
1680:
1.309 nicm 1681: if (ctx->bigger ||
1682: (!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.262 nicm 1683: tty_fake_bce(tty, wp, 8) ||
1.307 nicm 1684: !tty_term_has(tty->term, TTYC_CSR) ||
1685: wp->sx == 1 ||
1686: wp->sy == 1) {
1.236 nicm 1687: tty_redraw_region(tty, ctx);
1.1 nicm 1688: return;
1689: }
1.52 nicm 1690:
1.278 nicm 1691: tty_default_attributes(tty, wp, ctx->bg);
1.77 nicm 1692:
1.56 nicm 1693: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.212 nicm 1694: tty_margin_pane(tty, ctx);
1695:
1696: /*
1.301 nicm 1697: * If we want to wrap a pane while using margins, the cursor needs to
1698: * be exactly on the right of the region. If the cursor is entirely off
1699: * the edge - move it back to the right. Some terminals are funny about
1700: * this and insert extra spaces, so only use the right if margins are
1701: * enabled.
1.212 nicm 1702: */
1.301 nicm 1703: if (ctx->xoff + ctx->ocx > tty->rright) {
1704: if (!tty_use_margin(tty))
1705: tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
1706: else
1707: tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1708: } else
1.212 nicm 1709: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 1710:
1.56 nicm 1711: tty_putc(tty, '\n');
1.240 nicm 1712: }
1713:
1714: void
1715: tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1716: {
1717: struct window_pane *wp = ctx->wp;
1.284 nicm 1718: u_int i;
1.240 nicm 1719:
1.309 nicm 1720: if (ctx->bigger ||
1721: (!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.262 nicm 1722: tty_fake_bce(tty, wp, 8) ||
1.307 nicm 1723: !tty_term_has(tty->term, TTYC_CSR) ||
1724: wp->sx == 1 ||
1725: wp->sy == 1) {
1.240 nicm 1726: tty_redraw_region(tty, ctx);
1727: return;
1728: }
1729:
1.278 nicm 1730: tty_default_attributes(tty, wp, ctx->bg);
1.240 nicm 1731:
1732: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1733: tty_margin_pane(tty, ctx);
1734:
1.284 nicm 1735: if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1.301 nicm 1736: if (!tty_use_margin(tty))
1737: tty_cursor(tty, 0, tty->rlower);
1738: else
1739: tty_cursor(tty, tty->rright, tty->rlower);
1.284 nicm 1740: for (i = 0; i < ctx->num; i++)
1.240 nicm 1741: tty_putc(tty, '\n');
1.301 nicm 1742: } else {
1.353 nicm 1743: if (tty->cy == UINT_MAX)
1744: tty_cursor(tty, 0, 0);
1745: else
1746: tty_cursor(tty, 0, tty->cy);
1.284 nicm 1747: tty_putcode1(tty, TTYC_INDN, ctx->num);
1.331 nicm 1748: }
1749: }
1750:
1751: void
1752: tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
1753: {
1754: struct window_pane *wp = ctx->wp;
1755: u_int i;
1756:
1757: if (ctx->bigger ||
1758: (!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1759: tty_fake_bce(tty, wp, 8) ||
1760: !tty_term_has(tty->term, TTYC_CSR) ||
1761: (!tty_term_has(tty->term, TTYC_RI) &&
1762: !tty_term_has(tty->term, TTYC_RIN)) ||
1763: wp->sx == 1 ||
1764: wp->sy == 1) {
1765: tty_redraw_region(tty, ctx);
1766: return;
1767: }
1768:
1769: tty_default_attributes(tty, wp, ctx->bg);
1770:
1771: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1772: tty_margin_pane(tty, ctx);
1773: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1774:
1775: if (tty_term_has(tty->term, TTYC_RIN))
1776: tty_putcode1(tty, TTYC_RIN, ctx->num);
1777: else {
1778: for (i = 0; i < ctx->num; i++)
1779: tty_putcode(tty, TTYC_RI);
1.301 nicm 1780: }
1.1 nicm 1781: }
1782:
1783: void
1.24 nicm 1784: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1785: {
1.77 nicm 1786: struct window_pane *wp = ctx->wp;
1.271 nicm 1787: u_int px, py, nx, ny;
1.1 nicm 1788:
1.210 nicm 1789: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1790:
1.271 nicm 1791: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1792: tty_margin_off(tty);
1.39 nicm 1793:
1.309 nicm 1794: px = 0;
1.271 nicm 1795: nx = screen_size_x(wp->screen);
1.309 nicm 1796: py = ctx->ocy + 1;
1.271 nicm 1797: ny = screen_size_y(wp->screen) - ctx->ocy - 1;
1798:
1.309 nicm 1799: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271 nicm 1800:
1.309 nicm 1801: px = ctx->ocx;
1.271 nicm 1802: nx = screen_size_x(wp->screen) - ctx->ocx;
1.309 nicm 1803: py = ctx->ocy;
1.271 nicm 1804:
1.309 nicm 1805: tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1 nicm 1806: }
1807:
1808: void
1.24 nicm 1809: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1810: {
1.77 nicm 1811: struct window_pane *wp = ctx->wp;
1.271 nicm 1812: u_int px, py, nx, ny;
1.1 nicm 1813:
1.227 nicm 1814: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1815:
1.271 nicm 1816: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1817: tty_margin_off(tty);
1.39 nicm 1818:
1.309 nicm 1819: px = 0;
1.271 nicm 1820: nx = screen_size_x(wp->screen);
1.309 nicm 1821: py = 0;
1.319 nicm 1822: ny = ctx->ocy;
1.271 nicm 1823:
1.309 nicm 1824: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271 nicm 1825:
1.309 nicm 1826: px = 0;
1.271 nicm 1827: nx = ctx->ocx + 1;
1.309 nicm 1828: py = ctx->ocy;
1.271 nicm 1829:
1.309 nicm 1830: tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1 nicm 1831: }
1832:
1833: void
1.24 nicm 1834: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1835: {
1.77 nicm 1836: struct window_pane *wp = ctx->wp;
1.271 nicm 1837: u_int px, py, nx, ny;
1.1 nicm 1838:
1.210 nicm 1839: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1840:
1.271 nicm 1841: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1842: tty_margin_off(tty);
1.39 nicm 1843:
1.309 nicm 1844: px = 0;
1.271 nicm 1845: nx = screen_size_x(wp->screen);
1.309 nicm 1846: py = 0;
1.271 nicm 1847: ny = screen_size_y(wp->screen);
1848:
1.309 nicm 1849: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.4 nicm 1850: }
1851:
1852: void
1.24 nicm 1853: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 1854: {
1.12 nicm 1855: struct window_pane *wp = ctx->wp;
1856: struct screen *s = wp->screen;
1857: u_int i, j;
1.4 nicm 1858:
1.309 nicm 1859: if (ctx->bigger) {
1860: wp->flags |= PANE_REDRAW;
1861: return;
1862: }
1863:
1.176 nicm 1864: tty_attributes(tty, &grid_default_cell, wp);
1.4 nicm 1865:
1.39 nicm 1866: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.214 nicm 1867: tty_margin_off(tty);
1.4 nicm 1868:
1869: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 1870: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 1871: for (i = 0; i < screen_size_x(s); i++)
1872: tty_putc(tty, 'E');
1.1 nicm 1873: }
1874: }
1875:
1876: void
1.24 nicm 1877: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1878: {
1.309 nicm 1879: if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1))
1880: return;
1881:
1882: if (ctx->xoff + ctx->ocx - ctx->ox > tty->sx - 1 &&
1.306 nicm 1883: ctx->ocy == ctx->orlower &&
1884: tty_pane_full_width(tty, ctx))
1885: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.46 nicm 1886:
1.306 nicm 1887: tty_margin_off(tty);
1.253 nicm 1888: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1889:
1.237 nicm 1890: tty_cell(tty, ctx->cell, ctx->wp);
1.239 nicm 1891: }
1892:
1893: void
1894: tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
1895: {
1.309 nicm 1896: struct window_pane *wp = ctx->wp;
1897:
1898: if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
1899: return;
1900:
1901: if (ctx->bigger &&
1902: (ctx->xoff + ctx->ocx < ctx->ox ||
1903: ctx->xoff + ctx->ocx + ctx->num > ctx->ox + ctx->sx)) {
1904: if (!ctx->wrapped ||
1905: !tty_pane_full_width(tty, ctx) ||
1.359 nicm 1906: (tty->term->flags & TERM_NOXENL) ||
1.309 nicm 1907: ctx->xoff + ctx->ocx != 0 ||
1908: ctx->yoff + ctx->ocy != tty->cy + 1 ||
1909: tty->cx < tty->sx ||
1910: tty->cy == tty->rlower)
1911: tty_draw_pane(tty, ctx, ctx->ocy);
1912: else
1913: wp->flags |= PANE_REDRAW;
1914: return;
1915: }
1916:
1.306 nicm 1917: tty_margin_off(tty);
1.253 nicm 1918: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.239 nicm 1919:
1920: tty_attributes(tty, ctx->cell, ctx->wp);
1921: tty_putn(tty, ctx->ptr, ctx->num, ctx->num);
1.106 nicm 1922: }
1923:
1924: void
1925: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1926: {
1927: char *buf;
1928: size_t off;
1929:
1930: if (!tty_term_has(tty->term, TTYC_MS))
1931: return;
1932:
1933: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1934: buf = xmalloc(off);
1935:
1936: b64_ntop(ctx->ptr, ctx->num, buf, off);
1937: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1938:
1.138 nicm 1939: free(buf);
1.100 nicm 1940: }
1941:
1942: void
1943: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1944: {
1.256 nicm 1945: tty_add(tty, ctx->ptr, ctx->num);
1.242 nicm 1946: tty_invalidate(tty);
1.348 nicm 1947: }
1948:
1949: void
1950: tty_cmd_syncstart(struct tty *tty, __unused const struct tty_ctx *ctx)
1951: {
1952: tty_sync_start(tty);
1953: }
1954:
1955: void
1956: tty_cmd_syncend(struct tty *tty, __unused const struct tty_ctx *ctx)
1957: {
1958: tty_sync_end(tty);
1.1 nicm 1959: }
1960:
1.207 nicm 1961: static void
1.317 nicm 1962: tty_cell(struct tty *tty, const struct grid_cell *gc, struct window_pane *wp)
1.1 nicm 1963: {
1.298 nicm 1964: const struct grid_cell *gcp;
1.1 nicm 1965:
1966: /* Skip last character if terminal is stupid. */
1.359 nicm 1967: if ((tty->term->flags & TERM_NOXENL) &&
1.211 nicm 1968: tty->cy == tty->sy - 1 &&
1969: tty->cx == tty->sx - 1)
1.1 nicm 1970: return;
1971:
1972: /* If this is a padding character, do nothing. */
1973: if (gc->flags & GRID_FLAG_PADDING)
1974: return;
1975:
1976: /* Set the attributes. */
1.176 nicm 1977: tty_attributes(tty, gc, wp);
1.1 nicm 1978:
1.147 nicm 1979: /* Get the cell and if ASCII write with putc to do ACS translation. */
1.298 nicm 1980: gcp = tty_check_codeset(tty, gc);
1981: if (gcp->data.size == 1) {
1982: if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
1.1 nicm 1983: return;
1.298 nicm 1984: tty_putc(tty, *gcp->data.data);
1.1 nicm 1985: return;
1986: }
1987:
1.147 nicm 1988: /* Write the data. */
1.298 nicm 1989: tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
1.1 nicm 1990: }
1991:
1992: void
1993: tty_reset(struct tty *tty)
1994: {
1995: struct grid_cell *gc = &tty->cell;
1996:
1.228 nicm 1997: if (!grid_cells_equal(gc, &grid_default_cell)) {
1.285 nicm 1998: if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.228 nicm 1999: tty_putcode(tty, TTYC_RMACS);
2000: tty_putcode(tty, TTYC_SGR0);
2001: memcpy(gc, &grid_default_cell, sizeof *gc);
2002: }
1.1 nicm 2003:
1.228 nicm 2004: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2005: tty->last_wp = -1;
1.242 nicm 2006: }
2007:
2008: static void
2009: tty_invalidate(struct tty *tty)
2010: {
2011: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
2012:
2013: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2014: tty->last_wp = -1;
2015:
2016: tty->cx = tty->cy = UINT_MAX;
2017:
2018: tty->rupper = tty->rleft = UINT_MAX;
2019: tty->rlower = tty->rright = UINT_MAX;
2020:
2021: if (tty->flags & TTY_STARTED) {
1.330 nicm 2022: if (tty_use_margin(tty))
2023: tty_puts(tty, "\033[?69h"); /* DECLRMM */
1.242 nicm 2024: tty_putcode(tty, TTYC_SGR0);
2025:
2026: tty->mode = ALL_MODES;
2027: tty_update_mode(tty, MODE_CURSOR, NULL);
2028:
2029: tty_cursor(tty, 0, 0);
2030: tty_region_off(tty);
2031: tty_margin_off(tty);
2032: } else
2033: tty->mode = MODE_CURSOR;
1.1 nicm 2034: }
2035:
1.214 nicm 2036: /* Turn off margin. */
2037: void
2038: tty_region_off(struct tty *tty)
2039: {
2040: tty_region(tty, 0, tty->sy - 1);
2041: }
2042:
1.40 nicm 2043: /* Set region inside pane. */
1.208 nicm 2044: static void
1.196 nicm 2045: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
2046: u_int rlower)
1.1 nicm 2047: {
1.309 nicm 2048: tty_region(tty, ctx->yoff + rupper - ctx->oy,
2049: ctx->yoff + rlower - ctx->oy);
1.39 nicm 2050: }
2051:
1.40 nicm 2052: /* Set region at absolute position. */
1.214 nicm 2053: static void
1.40 nicm 2054: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 2055: {
2056: if (tty->rlower == rlower && tty->rupper == rupper)
2057: return;
1.1 nicm 2058: if (!tty_term_has(tty->term, TTYC_CSR))
2059: return;
1.39 nicm 2060:
2061: tty->rupper = rupper;
2062: tty->rlower = rlower;
1.55 nicm 2063:
2064: /*
2065: * Some terminals (such as PuTTY) do not correctly reset the cursor to
2066: * 0,0 if it is beyond the last column (they do not reset their wrap
2067: * flag so further output causes a line feed). As a workaround, do an
2068: * explicit move to 0 first.
2069: */
1.353 nicm 2070: if (tty->cx >= tty->sx) {
2071: if (tty->cy == UINT_MAX)
2072: tty_cursor(tty, 0, 0);
2073: else
2074: tty_cursor(tty, 0, tty->cy);
2075: }
1.42 nicm 2076:
1.39 nicm 2077: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.249 nicm 2078: tty->cx = tty->cy = UINT_MAX;
1.212 nicm 2079: }
2080:
1.214 nicm 2081: /* Turn off margin. */
2082: void
2083: tty_margin_off(struct tty *tty)
2084: {
2085: tty_margin(tty, 0, tty->sx - 1);
2086: }
2087:
1.212 nicm 2088: /* Set margin inside pane. */
2089: static void
2090: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
2091: {
1.309 nicm 2092: tty_margin(tty, ctx->xoff - ctx->ox,
2093: ctx->xoff + ctx->wp->sx - 1 - ctx->ox);
1.212 nicm 2094: }
2095:
2096: /* Set margin at absolute position. */
1.214 nicm 2097: static void
1.212 nicm 2098: tty_margin(struct tty *tty, u_int rleft, u_int rright)
2099: {
2100: char s[64];
2101:
2102: if (!tty_use_margin(tty))
2103: return;
2104: if (tty->rleft == rleft && tty->rright == rright)
2105: return;
2106:
1.232 nicm 2107: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.231 nicm 2108:
1.212 nicm 2109: tty->rleft = rleft;
2110: tty->rright = rright;
2111:
1.232 nicm 2112: if (rleft == 0 && rright == tty->sx - 1)
2113: snprintf(s, sizeof s, "\033[s");
2114: else
2115: snprintf(s, sizeof s, "\033[%u;%us", rleft + 1, rright + 1);
1.212 nicm 2116: tty_puts(tty, s);
1.249 nicm 2117: tty->cx = tty->cy = UINT_MAX;
1.253 nicm 2118: }
2119:
2120: /*
2121: * Move the cursor, unless it would wrap itself when the next character is
2122: * printed.
2123: */
2124: static void
2125: tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
2126: u_int cx, u_int cy)
2127: {
1.268 nicm 2128: if (!ctx->wrapped ||
2129: !tty_pane_full_width(tty, ctx) ||
1.359 nicm 2130: (tty->term->flags & TERM_NOXENL) ||
1.253 nicm 2131: ctx->xoff + cx != 0 ||
2132: ctx->yoff + cy != tty->cy + 1 ||
1.254 nicm 2133: tty->cx < tty->sx ||
2134: tty->cy == tty->rlower)
1.253 nicm 2135: tty_cursor_pane(tty, ctx, cx, cy);
2136: else
2137: log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
1.1 nicm 2138: }
2139:
1.42 nicm 2140: /* Move cursor inside pane. */
1.208 nicm 2141: static void
1.41 nicm 2142: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
2143: {
1.309 nicm 2144: tty_cursor(tty, ctx->xoff + cx - ctx->ox, ctx->yoff + cy - ctx->oy);
1.41 nicm 2145: }
2146:
1.42 nicm 2147: /* Move cursor to absolute position. */
1.41 nicm 2148: void
2149: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 2150: {
1.42 nicm 2151: struct tty_term *term = tty->term;
2152: u_int thisx, thisy;
2153: int change;
1.354 nicm 2154:
2155: if (tty->flags & TTY_BLOCK)
2156: return;
1.77 nicm 2157:
1.42 nicm 2158: if (cx > tty->sx - 1)
2159: cx = tty->sx - 1;
2160:
2161: thisx = tty->cx;
2162: thisy = tty->cy;
2163:
2164: /* No change. */
2165: if (cx == thisx && cy == thisy)
2166: return;
2167:
1.43 nicm 2168: /* Very end of the line, just use absolute movement. */
2169: if (thisx > tty->sx - 1)
2170: goto absolute;
2171:
1.42 nicm 2172: /* Move to home position (0, 0). */
2173: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
2174: tty_putcode(tty, TTYC_HOME);
2175: goto out;
2176: }
2177:
2178: /* Zero on the next line. */
1.293 nicm 2179: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
2180: (!tty_use_margin(tty) || tty->rleft == 0)) {
1.1 nicm 2181: tty_putc(tty, '\r');
1.42 nicm 2182: tty_putc(tty, '\n');
2183: goto out;
2184: }
2185:
1.45 nicm 2186: /* Moving column or row. */
1.42 nicm 2187: if (cy == thisy) {
1.45 nicm 2188: /*
2189: * Moving column only, row staying the same.
2190: */
2191:
1.42 nicm 2192: /* To left edge. */
1.294 nicm 2193: if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
1.42 nicm 2194: tty_putc(tty, '\r');
2195: goto out;
2196: }
2197:
2198: /* One to the left. */
2199: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
2200: tty_putcode(tty, TTYC_CUB1);
2201: goto out;
2202: }
2203:
2204: /* One to the right. */
2205: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
2206: tty_putcode(tty, TTYC_CUF1);
2207: goto out;
2208: }
2209:
2210: /* Calculate difference. */
2211: change = thisx - cx; /* +ve left, -ve right */
2212:
2213: /*
2214: * Use HPA if change is larger than absolute, otherwise move
2215: * the cursor with CUB/CUF.
2216: */
1.87 nicm 2217: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 2218: tty_putcode1(tty, TTYC_HPA, cx);
2219: goto out;
1.333 nicm 2220: } else if (change > 0 &&
2221: tty_term_has(term, TTYC_CUB) &&
2222: !tty_use_margin(tty)) {
1.202 nicm 2223: if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
2224: tty_putcode(tty, TTYC_CUB1);
2225: tty_putcode(tty, TTYC_CUB1);
2226: goto out;
2227: }
1.42 nicm 2228: tty_putcode1(tty, TTYC_CUB, change);
2229: goto out;
1.333 nicm 2230: } else if (change < 0 &&
2231: tty_term_has(term, TTYC_CUF) &&
2232: !tty_use_margin(tty)) {
1.42 nicm 2233: tty_putcode1(tty, TTYC_CUF, -change);
2234: goto out;
2235: }
1.45 nicm 2236: } else if (cx == thisx) {
2237: /*
2238: * Moving row only, column staying the same.
2239: */
1.42 nicm 2240:
2241: /* One above. */
1.77 nicm 2242: if (thisy != tty->rupper &&
1.42 nicm 2243: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
2244: tty_putcode(tty, TTYC_CUU1);
2245: goto out;
2246: }
2247:
2248: /* One below. */
1.49 nicm 2249: if (thisy != tty->rlower &&
1.42 nicm 2250: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
2251: tty_putcode(tty, TTYC_CUD1);
2252: goto out;
2253: }
2254:
2255: /* Calculate difference. */
2256: change = thisy - cy; /* +ve up, -ve down */
2257:
2258: /*
1.77 nicm 2259: * Try to use VPA if change is larger than absolute or if this
2260: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 2261: */
1.87 nicm 2262: if ((u_int) abs(change) > cy ||
1.42 nicm 2263: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 2264: (change > 0 && cy - change < tty->rupper)) {
2265: if (tty_term_has(term, TTYC_VPA)) {
2266: tty_putcode1(tty, TTYC_VPA, cy);
2267: goto out;
2268: }
1.42 nicm 2269: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
2270: tty_putcode1(tty, TTYC_CUU, change);
2271: goto out;
2272: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
2273: tty_putcode1(tty, TTYC_CUD, -change);
2274: goto out;
2275: }
2276: }
2277:
1.43 nicm 2278: absolute:
1.42 nicm 2279: /* Absolute movement. */
2280: tty_putcode2(tty, TTYC_CUP, cy, cx);
2281:
2282: out:
2283: tty->cx = cx;
2284: tty->cy = cy;
1.1 nicm 2285: }
2286:
2287: void
1.176 nicm 2288: tty_attributes(struct tty *tty, const struct grid_cell *gc,
1.317 nicm 2289: struct window_pane *wp)
1.1 nicm 2290: {
1.63 nicm 2291: struct grid_cell *tc = &tty->cell, gc2;
1.255 nicm 2292: int changed;
1.61 nicm 2293:
1.228 nicm 2294: /* Ignore cell if it is the same as the last one. */
2295: if (wp != NULL &&
2296: (int)wp->id == tty->last_wp &&
1.326 nicm 2297: ~(wp->flags & PANE_STYLECHANGED) &&
1.228 nicm 2298: gc->attr == tty->last_cell.attr &&
2299: gc->fg == tty->last_cell.fg &&
1.328 nicm 2300: gc->bg == tty->last_cell.bg &&
2301: gc->us == tty->last_cell.us)
1.228 nicm 2302: return;
2303: tty->last_wp = (wp != NULL ? (int)wp->id : -1);
2304: memcpy(&tty->last_cell, gc, sizeof tty->last_cell);
2305:
2306: /* Copy cell and update default colours. */
1.85 nicm 2307: memcpy(&gc2, gc, sizeof gc2);
1.203 nicm 2308: if (wp != NULL)
2309: tty_default_colours(&gc2, wp);
1.63 nicm 2310:
1.18 nicm 2311: /*
2312: * If no setab, try to use the reverse attribute as a best-effort for a
2313: * non-default background. This is a bit of a hack but it doesn't do
2314: * any serious harm and makes a couple of applications happier.
2315: */
2316: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 2317: if (gc2.attr & GRID_ATTR_REVERSE) {
1.310 nicm 2318: if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
1.64 nicm 2319: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 2320: } else {
1.310 nicm 2321: if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
1.64 nicm 2322: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 2323: }
2324: }
1.1 nicm 2325:
1.85 nicm 2326: /* Fix up the colours if necessary. */
1.219 nicm 2327: tty_check_fg(tty, wp, &gc2);
2328: tty_check_bg(tty, wp, &gc2);
1.328 nicm 2329: tty_check_us(tty, wp, &gc2);
1.85 nicm 2330:
1.328 nicm 2331: /*
2332: * If any bits are being cleared or the underline colour is now default,
2333: * reset everything.
2334: */
2335: if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
1.64 nicm 2336: tty_reset(tty);
2337:
2338: /*
2339: * Set the colours. This may call tty_reset() (so it comes next) and
1.328 nicm 2340: * may add to (NOT remove) the desired attributes.
1.64 nicm 2341: */
1.85 nicm 2342: tty_colours(tty, &gc2);
1.64 nicm 2343:
1.1 nicm 2344: /* Filter out attribute bits already set. */
1.85 nicm 2345: changed = gc2.attr & ~tc->attr;
2346: tc->attr = gc2.attr;
1.1 nicm 2347:
2348: /* Set the attributes. */
2349: if (changed & GRID_ATTR_BRIGHT)
2350: tty_putcode(tty, TTYC_BOLD);
2351: if (changed & GRID_ATTR_DIM)
2352: tty_putcode(tty, TTYC_DIM);
1.180 nicm 2353: if (changed & GRID_ATTR_ITALICS)
2354: tty_set_italics(tty);
1.308 nicm 2355: if (changed & GRID_ATTR_ALL_UNDERSCORE) {
2356: if ((changed & GRID_ATTR_UNDERSCORE) ||
2357: !tty_term_has(tty->term, TTYC_SMULX))
2358: tty_putcode(tty, TTYC_SMUL);
2359: else if (changed & GRID_ATTR_UNDERSCORE_2)
2360: tty_putcode1(tty, TTYC_SMULX, 2);
2361: else if (changed & GRID_ATTR_UNDERSCORE_3)
2362: tty_putcode1(tty, TTYC_SMULX, 3);
2363: else if (changed & GRID_ATTR_UNDERSCORE_4)
2364: tty_putcode1(tty, TTYC_SMULX, 4);
2365: else if (changed & GRID_ATTR_UNDERSCORE_5)
2366: tty_putcode1(tty, TTYC_SMULX, 5);
2367: }
1.1 nicm 2368: if (changed & GRID_ATTR_BLINK)
2369: tty_putcode(tty, TTYC_BLINK);
2370: if (changed & GRID_ATTR_REVERSE) {
2371: if (tty_term_has(tty->term, TTYC_REV))
2372: tty_putcode(tty, TTYC_REV);
2373: else if (tty_term_has(tty->term, TTYC_SMSO))
2374: tty_putcode(tty, TTYC_SMSO);
2375: }
2376: if (changed & GRID_ATTR_HIDDEN)
2377: tty_putcode(tty, TTYC_INVIS);
1.255 nicm 2378: if (changed & GRID_ATTR_STRIKETHROUGH)
2379: tty_putcode(tty, TTYC_SMXX);
1.324 nicm 2380: if (changed & GRID_ATTR_OVERLINE)
2381: tty_putcode(tty, TTYC_SMOL);
1.285 nicm 2382: if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.1 nicm 2383: tty_putcode(tty, TTYC_SMACS);
2384: }
2385:
1.207 nicm 2386: static void
1.85 nicm 2387: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2388: {
1.61 nicm 2389: struct grid_cell *tc = &tty->cell;
1.204 nicm 2390: int have_ax;
1.61 nicm 2391:
2392: /* No changes? Nothing is necessary. */
1.328 nicm 2393: if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
1.63 nicm 2394: return;
1.1 nicm 2395:
1.61 nicm 2396: /*
2397: * Is either the default colour? This is handled specially because the
2398: * best solution might be to reset both colours to default, in which
2399: * case if only one is default need to fall onward to set the other
2400: * colour.
2401: */
1.310 nicm 2402: if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
1.61 nicm 2403: /*
2404: * If don't have AX but do have op, send sgr0 (op can't
2405: * actually be used because it is sometimes the same as sgr0
2406: * and sometimes isn't). This resets both colours to default.
2407: *
2408: * Otherwise, try to set the default colour only as needed.
2409: */
1.174 nicm 2410: have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61 nicm 2411: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
2412: tty_reset(tty);
2413: else {
1.310 nicm 2414: if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
1.61 nicm 2415: if (have_ax)
2416: tty_puts(tty, "\033[39m");
1.204 nicm 2417: else if (tc->fg != 7)
1.61 nicm 2418: tty_putcode1(tty, TTYC_SETAF, 7);
1.310 nicm 2419: tc->fg = gc->fg;
1.61 nicm 2420: }
1.310 nicm 2421: if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
1.77 nicm 2422: if (have_ax)
1.61 nicm 2423: tty_puts(tty, "\033[49m");
1.204 nicm 2424: else if (tc->bg != 0)
1.61 nicm 2425: tty_putcode1(tty, TTYC_SETAB, 0);
1.312 nicm 2426: tc->bg = gc->bg;
1.61 nicm 2427: }
2428: }
2429: }
1.1 nicm 2430:
1.61 nicm 2431: /* Set the foreground colour. */
1.310 nicm 2432: if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
1.85 nicm 2433: tty_colours_fg(tty, gc);
1.1 nicm 2434:
1.61 nicm 2435: /*
2436: * Set the background colour. This must come after the foreground as
2437: * tty_colour_fg() can call tty_reset().
2438: */
1.310 nicm 2439: if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
1.73 nicm 2440: tty_colours_bg(tty, gc);
1.328 nicm 2441:
2442: /* Set the underscore color. */
2443: if (gc->us != tc->us)
2444: tty_colours_us(tty, gc);
1.1 nicm 2445: }
2446:
1.219 nicm 2447: static void
1.317 nicm 2448: tty_check_fg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
1.85 nicm 2449: {
1.204 nicm 2450: u_char r, g, b;
2451: u_int colours;
1.223 nicm 2452: int c;
1.85 nicm 2453:
1.288 nicm 2454: /*
2455: * Perform substitution if this pane has a palette. If the bright
2456: * attribute is set, use the bright entry in the palette by changing to
2457: * the aixterm colour.
2458: */
2459: if (~gc->flags & GRID_FLAG_NOPALETTE) {
2460: c = gc->fg;
1.289 nicm 2461: if (c < 8 && gc->attr & GRID_ATTR_BRIGHT)
1.288 nicm 2462: c += 90;
2463: if ((c = window_pane_get_palette(wp, c)) != -1)
2464: gc->fg = c;
2465: }
1.219 nicm 2466:
1.199 nicm 2467: /* Is this a 24-bit colour? */
1.204 nicm 2468: if (gc->fg & COLOUR_FLAG_RGB) {
1.199 nicm 2469: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.359 nicm 2470: if (tty->term->flags & TERM_RGBCOLOURS)
1.200 nicm 2471: return;
1.341 nicm 2472: colour_split_rgb(gc->fg, &r, &g, &b);
2473: gc->fg = colour_find_rgb(r, g, b);
1.199 nicm 2474: }
1.224 nicm 2475:
2476: /* How many colours does this terminal have? */
1.359 nicm 2477: if (tty->term->flags & TERM_256COLOURS)
1.224 nicm 2478: colours = 256;
2479: else
2480: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 2481:
1.85 nicm 2482: /* Is this a 256-colour colour? */
1.204 nicm 2483: if (gc->fg & COLOUR_FLAG_256) {
1.85 nicm 2484: /* And not a 256 colour mode? */
1.224 nicm 2485: if (colours != 256) {
1.85 nicm 2486: gc->fg = colour_256to16(gc->fg);
2487: if (gc->fg & 8) {
2488: gc->fg &= 7;
1.175 nicm 2489: if (colours >= 16)
2490: gc->fg += 90;
1.332 nicm 2491: }
1.85 nicm 2492: }
2493: return;
2494: }
2495:
2496: /* Is this an aixterm colour? */
2497: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
2498: gc->fg -= 90;
2499: gc->attr |= GRID_ATTR_BRIGHT;
2500: }
2501: }
2502:
1.219 nicm 2503: static void
1.317 nicm 2504: tty_check_bg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
1.85 nicm 2505: {
1.204 nicm 2506: u_char r, g, b;
2507: u_int colours;
1.223 nicm 2508: int c;
1.85 nicm 2509:
1.288 nicm 2510: /* Perform substitution if this pane has a palette. */
2511: if (~gc->flags & GRID_FLAG_NOPALETTE) {
2512: if ((c = window_pane_get_palette(wp, gc->bg)) != -1)
2513: gc->bg = c;
2514: }
1.219 nicm 2515:
1.199 nicm 2516: /* Is this a 24-bit colour? */
1.204 nicm 2517: if (gc->bg & COLOUR_FLAG_RGB) {
1.199 nicm 2518: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.359 nicm 2519: if (tty->term->flags & TERM_RGBCOLOURS)
1.200 nicm 2520: return;
1.341 nicm 2521: colour_split_rgb(gc->bg, &r, &g, &b);
2522: gc->bg = colour_find_rgb(r, g, b);
1.199 nicm 2523: }
1.224 nicm 2524:
2525: /* How many colours does this terminal have? */
1.359 nicm 2526: if (tty->term->flags & TERM_256COLOURS)
1.224 nicm 2527: colours = 256;
2528: else
2529: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 2530:
1.85 nicm 2531: /* Is this a 256-colour colour? */
1.204 nicm 2532: if (gc->bg & COLOUR_FLAG_256) {
1.85 nicm 2533: /*
2534: * And not a 256 colour mode? Translate to 16-colour
2535: * palette. Bold background doesn't exist portably, so just
2536: * discard the bold bit if set.
2537: */
1.224 nicm 2538: if (colours != 256) {
1.85 nicm 2539: gc->bg = colour_256to16(gc->bg);
1.175 nicm 2540: if (gc->bg & 8) {
1.85 nicm 2541: gc->bg &= 7;
1.175 nicm 2542: if (colours >= 16)
1.322 nicm 2543: gc->bg += 90;
1.175 nicm 2544: }
1.85 nicm 2545: }
2546: return;
2547: }
2548:
2549: /* Is this an aixterm colour? */
1.175 nicm 2550: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85 nicm 2551: gc->bg -= 90;
2552: }
2553:
1.207 nicm 2554: static void
1.335 nicm 2555: tty_check_us(__unused struct tty *tty, struct window_pane *wp,
2556: struct grid_cell *gc)
1.328 nicm 2557: {
2558: int c;
2559:
2560: /* Perform substitution if this pane has a palette. */
2561: if (~gc->flags & GRID_FLAG_NOPALETTE) {
2562: if ((c = window_pane_get_palette(wp, gc->us)) != -1)
2563: gc->us = c;
2564: }
2565:
2566: /* Underscore colour is set as RGB so convert a 256 colour to RGB. */
2567: if (gc->us & COLOUR_FLAG_256)
2568: gc->us = colour_256toRGB (gc->us);
2569: }
2570:
2571: static void
1.85 nicm 2572: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2573: {
1.61 nicm 2574: struct grid_cell *tc = &tty->cell;
1.79 nicm 2575: char s[32];
1.1 nicm 2576:
1.204 nicm 2577: /* Is this a 24-bit or 256-colour colour? */
1.302 nicm 2578: if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
1.204 nicm 2579: if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61 nicm 2580: goto save_fg;
1.199 nicm 2581: /* Should not get here, already converted in tty_check_fg. */
1.85 nicm 2582: return;
1.1 nicm 2583: }
2584:
1.79 nicm 2585: /* Is this an aixterm bright colour? */
1.204 nicm 2586: if (gc->fg >= 90 && gc->fg <= 97) {
1.359 nicm 2587: if (tty->term->flags & TERM_256COLOURS) {
1.323 nicm 2588: xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
2589: tty_puts(tty, s);
2590: } else
2591: tty_putcode1(tty, TTYC_SETAF, gc->fg - 90 + 8);
1.85 nicm 2592: goto save_fg;
1.79 nicm 2593: }
2594:
1.61 nicm 2595: /* Otherwise set the foreground colour. */
1.204 nicm 2596: tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61 nicm 2597:
1.77 nicm 2598: save_fg:
1.61 nicm 2599: /* Save the new values in the terminal current cell. */
1.204 nicm 2600: tc->fg = gc->fg;
1.1 nicm 2601: }
2602:
1.207 nicm 2603: static void
1.73 nicm 2604: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2605: {
1.61 nicm 2606: struct grid_cell *tc = &tty->cell;
1.79 nicm 2607: char s[32];
1.1 nicm 2608:
1.204 nicm 2609: /* Is this a 24-bit or 256-colour colour? */
1.302 nicm 2610: if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
1.204 nicm 2611: if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61 nicm 2612: goto save_bg;
1.199 nicm 2613: /* Should not get here, already converted in tty_check_bg. */
1.85 nicm 2614: return;
1.79 nicm 2615: }
2616:
2617: /* Is this an aixterm bright colour? */
1.204 nicm 2618: if (gc->bg >= 90 && gc->bg <= 97) {
1.359 nicm 2619: if (tty->term->flags & TERM_256COLOURS) {
1.323 nicm 2620: xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
2621: tty_puts(tty, s);
2622: } else
2623: tty_putcode1(tty, TTYC_SETAB, gc->bg - 90 + 8);
1.175 nicm 2624: goto save_bg;
1.1 nicm 2625: }
2626:
1.61 nicm 2627: /* Otherwise set the background colour. */
1.204 nicm 2628: tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61 nicm 2629:
2630: save_bg:
2631: /* Save the new values in the terminal current cell. */
1.204 nicm 2632: tc->bg = gc->bg;
1.328 nicm 2633: }
2634:
2635: static void
2636: tty_colours_us(struct tty *tty, const struct grid_cell *gc)
2637: {
2638: struct grid_cell *tc = &tty->cell;
2639: u_int c;
2640: u_char r, g, b;
2641:
2642: /* Must be an RGB colour - this should never happen. */
2643: if (~gc->us & COLOUR_FLAG_RGB)
2644: return;
2645:
2646: /*
2647: * Setulc follows the ncurses(3) one argument "direct colour"
2648: * capability format. Calculate the colour value.
2649: */
2650: colour_split_rgb(gc->us, &r, &g, &b);
2651: c = (65536 * r) + (256 * g) + b;
2652:
2653: /* Write the colour. */
2654: tty_putcode1(tty, TTYC_SETULC, c);
2655:
2656: /* Save the new values in the terminal current cell. */
2657: tc->us = gc->us;
1.61 nicm 2658: }
2659:
1.207 nicm 2660: static int
1.204 nicm 2661: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61 nicm 2662: {
1.204 nicm 2663: u_char r, g, b;
1.61 nicm 2664:
1.204 nicm 2665: if (colour & COLOUR_FLAG_256) {
1.359 nicm 2666: if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
2667: tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
2668: else if (tty_term_has(tty->term, TTYC_SETAB))
2669: tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
2670: return (0);
1.204 nicm 2671: }
2672:
2673: if (colour & COLOUR_FLAG_RGB) {
1.341 nicm 2674: colour_split_rgb(colour & 0xffffff, &r, &g, &b);
1.359 nicm 2675: if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
1.286 nicm 2676: tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
1.359 nicm 2677: else if (tty_term_has(tty->term, TTYC_SETRGBB))
1.286 nicm 2678: tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
1.165 nicm 2679: return (0);
2680: }
1.61 nicm 2681:
1.165 nicm 2682: return (-1);
1.176 nicm 2683: }
2684:
1.207 nicm 2685: static void
1.317 nicm 2686: tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
1.176 nicm 2687: {
1.326 nicm 2688: struct options *oo = wp->options;
2689: struct style *style, *active_style;
2690: int c;
2691:
2692: if (wp->flags & PANE_STYLECHANGED) {
2693: wp->flags &= ~PANE_STYLECHANGED;
2694:
2695: active_style = options_get_style(oo, "window-active-style");
2696: style = options_get_style(oo, "window-style");
2697:
2698: style_copy(&wp->cached_active_style, active_style);
2699: style_copy(&wp->cached_style, style);
1.203 nicm 2700: } else {
1.326 nicm 2701: active_style = &wp->cached_active_style;
2702: style = &wp->cached_style;
1.203 nicm 2703: }
1.176 nicm 2704:
1.204 nicm 2705: if (gc->fg == 8) {
1.326 nicm 2706: if (wp == wp->window->active && active_style->gc.fg != 8)
2707: gc->fg = active_style->gc.fg;
1.204 nicm 2708: else
1.326 nicm 2709: gc->fg = style->gc.fg;
1.219 nicm 2710:
1.310 nicm 2711: if (gc->fg != 8) {
2712: c = window_pane_get_palette(wp, gc->fg);
2713: if (c != -1)
2714: gc->fg = c;
2715: }
1.176 nicm 2716: }
2717:
1.204 nicm 2718: if (gc->bg == 8) {
1.326 nicm 2719: if (wp == wp->window->active && active_style->gc.bg != 8)
2720: gc->bg = active_style->gc.bg;
1.204 nicm 2721: else
1.326 nicm 2722: gc->bg = style->gc.bg;
1.219 nicm 2723:
1.310 nicm 2724: if (gc->bg != 8) {
2725: c = window_pane_get_palette(wp, gc->bg);
2726: if (c != -1)
2727: gc->bg = c;
2728: }
1.176 nicm 2729: }
1.210 nicm 2730: }
2731:
2732: static void
1.317 nicm 2733: tty_default_attributes(struct tty *tty, struct window_pane *wp, u_int bg)
1.210 nicm 2734: {
2735: static struct grid_cell gc;
2736:
2737: memcpy(&gc, &grid_default_cell, sizeof gc);
2738: gc.bg = bg;
2739: tty_attributes(tty, &gc, wp);
1.1 nicm 2740: }