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