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