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