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