Annotation of src/usr.bin/tmux/tty.c, Revision 1.269
1.269 ! nicm 1: /* $OpenBSD: tty.c,v 1.268 2017/04/25 18:30:29 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;
1.269 ! nicm 1120: u_int i, lines;
1.240 nicm 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:
1.269 ! nicm 1134: /*
! 1135: * Konsole has a bug where it will ignore SU if the parameter is more
! 1136: * than the height of the scroll region. Clamping the parameter doesn't
! 1137: * hurt in any case.
! 1138: */
! 1139: lines = tty->rlower - tty->rupper;
! 1140: if (lines > ctx->num)
! 1141: lines = ctx->num;
! 1142:
! 1143: if (lines == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1.241 nicm 1144: tty_cursor(tty, tty->rright, tty->rlower);
1.269 ! nicm 1145: for (i = 0; i < lines; i++)
1.240 nicm 1146: tty_putc(tty, '\n');
1147: } else
1.269 ! nicm 1148: tty_putcode1(tty, TTYC_INDN, lines);
1.1 nicm 1149: }
1150:
1151: void
1.24 nicm 1152: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1153: {
1.77 nicm 1154: struct window_pane *wp = ctx->wp;
1.12 nicm 1155: struct screen *s = wp->screen;
1.204 nicm 1156: u_int i, j;
1.229 nicm 1157: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 1158:
1.210 nicm 1159: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1160:
1.229 nicm 1161: tty_region_pane(tty, ctx, 0, sy - 1);
1.214 nicm 1162: tty_margin_off(tty);
1.39 nicm 1163:
1.176 nicm 1164: if (tty_pane_full_width(tty, ctx) &&
1.248 nicm 1165: ctx->yoff + wp->sy >= tty->sy - 1 &&
1.229 nicm 1166: status_at_line(tty->client) <= 0 &&
1167: tty_term_has(tty->term, TTYC_ED)) {
1168: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1169: tty_putcode(tty, TTYC_ED);
1170: } else if (tty_pane_full_width(tty, ctx) &&
1.210 nicm 1171: tty_term_has(tty->term, TTYC_EL) &&
1172: !tty_fake_bce(tty, wp, ctx->bg)) {
1.229 nicm 1173: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1174: tty_putcode(tty, TTYC_EL);
1.229 nicm 1175: if (ctx->ocy != sy - 1) {
1.41 nicm 1176: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.229 nicm 1177: for (i = ctx->ocy + 1; i < sy; i++) {
1.1 nicm 1178: tty_putcode(tty, TTYC_EL);
1.229 nicm 1179: if (i == sy - 1)
1.1 nicm 1180: continue;
1181: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
1182: tty->cy++;
1183: }
1184: }
1185: } else {
1.229 nicm 1186: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1187: tty_repeat_space(tty, sx - ctx->ocx);
1188: for (j = ctx->ocy + 1; j < sy; j++) {
1.41 nicm 1189: tty_cursor_pane(tty, ctx, 0, j);
1.229 nicm 1190: tty_repeat_space(tty, sx);
1.1 nicm 1191: }
1192: }
1193: }
1194:
1195: void
1.24 nicm 1196: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1197: {
1.77 nicm 1198: struct window_pane *wp = ctx->wp;
1.12 nicm 1199: struct screen *s = wp->screen;
1.204 nicm 1200: u_int i, j;
1.229 nicm 1201: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 1202:
1.227 nicm 1203: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1204:
1.229 nicm 1205: tty_region_pane(tty, ctx, 0, sy - 1);
1.214 nicm 1206: tty_margin_off(tty);
1.39 nicm 1207:
1.230 nicm 1208: if (tty_pane_full_width(tty, ctx) &&
1.210 nicm 1209: tty_term_has(tty->term, TTYC_EL) &&
1210: !tty_fake_bce(tty, wp, ctx->bg)) {
1.229 nicm 1211: tty_cursor_pane(tty, ctx, 0, 0);
1.14 nicm 1212: for (i = 0; i < ctx->ocy; i++) {
1.1 nicm 1213: tty_putcode(tty, TTYC_EL);
1214: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
1215: tty->cy++;
1216: }
1217: } else {
1.229 nicm 1218: tty_cursor_pane(tty, ctx, 0, 0);
1.14 nicm 1219: for (j = 0; j < ctx->ocy; j++) {
1.41 nicm 1220: tty_cursor_pane(tty, ctx, 0, j);
1.229 nicm 1221: tty_repeat_space(tty, sx);
1.1 nicm 1222: }
1223: }
1.229 nicm 1224: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133 nicm 1225: tty_repeat_space(tty, ctx->ocx + 1);
1.1 nicm 1226: }
1227:
1228: void
1.24 nicm 1229: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1230: {
1.77 nicm 1231: struct window_pane *wp = ctx->wp;
1.12 nicm 1232: struct screen *s = wp->screen;
1.204 nicm 1233: u_int i, j;
1.229 nicm 1234: u_int sx = screen_size_x(s), sy = screen_size_y(s);
1.1 nicm 1235:
1.210 nicm 1236: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1237:
1.229 nicm 1238: tty_region_pane(tty, ctx, 0, sy - 1);
1.214 nicm 1239: tty_margin_off(tty);
1.39 nicm 1240:
1.176 nicm 1241: if (tty_pane_full_width(tty, ctx) &&
1.267 nicm 1242: ctx->yoff + wp->sy >= tty->sy - 1 &&
1.229 nicm 1243: status_at_line(tty->client) <= 0 &&
1244: tty_term_has(tty->term, TTYC_ED)) {
1245: tty_cursor_pane(tty, ctx, 0, 0);
1246: tty_putcode(tty, TTYC_ED);
1.230 nicm 1247: } else if (tty_pane_full_width(tty, ctx) &&
1.210 nicm 1248: tty_term_has(tty->term, TTYC_EL) &&
1249: !tty_fake_bce(tty, wp, ctx->bg)) {
1.229 nicm 1250: tty_cursor_pane(tty, ctx, 0, 0);
1251: for (i = 0; i < sy; i++) {
1.1 nicm 1252: tty_putcode(tty, TTYC_EL);
1.229 nicm 1253: if (i != sy - 1) {
1.1 nicm 1254: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
1255: tty->cy++;
1256: }
1257: }
1258: } else {
1.229 nicm 1259: tty_cursor_pane(tty, ctx, 0, 0);
1260: for (j = 0; j < sy; j++) {
1.41 nicm 1261: tty_cursor_pane(tty, ctx, 0, j);
1.229 nicm 1262: tty_repeat_space(tty, sx);
1.1 nicm 1263: }
1.4 nicm 1264: }
1265: }
1266:
1267: void
1.24 nicm 1268: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 1269: {
1.12 nicm 1270: struct window_pane *wp = ctx->wp;
1271: struct screen *s = wp->screen;
1272: u_int i, j;
1.4 nicm 1273:
1.176 nicm 1274: tty_attributes(tty, &grid_default_cell, wp);
1.4 nicm 1275:
1.39 nicm 1276: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.214 nicm 1277: tty_margin_off(tty);
1.4 nicm 1278:
1279: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 1280: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 1281: for (i = 0; i < screen_size_x(s); i++)
1282: tty_putc(tty, 'E');
1.1 nicm 1283: }
1284: }
1285:
1286: void
1.24 nicm 1287: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1288: {
1.222 nicm 1289: if (ctx->xoff + ctx->ocx > tty->sx - 1 && ctx->ocy == ctx->orlower) {
1290: if (tty_pane_full_width(tty, ctx))
1291: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1292: else
1293: tty_margin_off(tty);
1294: }
1.46 nicm 1295:
1.253 nicm 1296: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1297:
1.237 nicm 1298: tty_cell(tty, ctx->cell, ctx->wp);
1.239 nicm 1299: }
1300:
1301: void
1302: tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
1303: {
1.253 nicm 1304: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.239 nicm 1305:
1306: tty_attributes(tty, ctx->cell, ctx->wp);
1307: tty_putn(tty, ctx->ptr, ctx->num, ctx->num);
1.106 nicm 1308: }
1309:
1310: void
1311: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1312: {
1313: char *buf;
1314: size_t off;
1315:
1316: if (!tty_term_has(tty->term, TTYC_MS))
1317: return;
1318:
1319: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1320: buf = xmalloc(off);
1321:
1322: b64_ntop(ctx->ptr, ctx->num, buf, off);
1323: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1324:
1.138 nicm 1325: free(buf);
1.100 nicm 1326: }
1327:
1328: void
1329: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1330: {
1.256 nicm 1331: tty_add(tty, ctx->ptr, ctx->num);
1.242 nicm 1332: tty_invalidate(tty);
1.1 nicm 1333: }
1334:
1.207 nicm 1335: static void
1.176 nicm 1336: tty_cell(struct tty *tty, const struct grid_cell *gc,
1337: const struct window_pane *wp)
1.1 nicm 1338: {
1.193 nicm 1339: u_int i;
1.1 nicm 1340:
1341: /* Skip last character if terminal is stupid. */
1.211 nicm 1342: if ((tty->term->flags & TERM_EARLYWRAP) &&
1343: tty->cy == tty->sy - 1 &&
1344: tty->cx == tty->sx - 1)
1.1 nicm 1345: return;
1346:
1347: /* If this is a padding character, do nothing. */
1348: if (gc->flags & GRID_FLAG_PADDING)
1349: return;
1350:
1351: /* Set the attributes. */
1.176 nicm 1352: tty_attributes(tty, gc, wp);
1.1 nicm 1353:
1.147 nicm 1354: /* Get the cell and if ASCII write with putc to do ACS translation. */
1.193 nicm 1355: if (gc->data.size == 1) {
1356: if (*gc->data.data < 0x20 || *gc->data.data == 0x7f)
1.1 nicm 1357: return;
1.193 nicm 1358: tty_putc(tty, *gc->data.data);
1.1 nicm 1359: return;
1360: }
1361:
1.147 nicm 1362: /* If not UTF-8, write _. */
1.1 nicm 1363: if (!(tty->flags & TTY_UTF8)) {
1.193 nicm 1364: for (i = 0; i < gc->data.width; i++)
1.1 nicm 1365: tty_putc(tty, '_');
1366: return;
1367: }
1368:
1.147 nicm 1369: /* Write the data. */
1.193 nicm 1370: tty_putn(tty, gc->data.data, gc->data.size, gc->data.width);
1.1 nicm 1371: }
1372:
1373: void
1374: tty_reset(struct tty *tty)
1375: {
1376: struct grid_cell *gc = &tty->cell;
1377:
1.228 nicm 1378: if (!grid_cells_equal(gc, &grid_default_cell)) {
1379: if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1380: tty_putcode(tty, TTYC_RMACS);
1381: tty_putcode(tty, TTYC_SGR0);
1382: memcpy(gc, &grid_default_cell, sizeof *gc);
1383: }
1.1 nicm 1384:
1.228 nicm 1385: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
1386: tty->last_wp = -1;
1.242 nicm 1387: }
1388:
1389: static void
1390: tty_invalidate(struct tty *tty)
1391: {
1392: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
1393:
1394: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
1395: tty->last_wp = -1;
1396:
1397: tty->cx = tty->cy = UINT_MAX;
1398:
1399: tty->rupper = tty->rleft = UINT_MAX;
1400: tty->rlower = tty->rright = UINT_MAX;
1401:
1402: if (tty->flags & TTY_STARTED) {
1403: tty_putcode(tty, TTYC_SGR0);
1404:
1405: tty->mode = ALL_MODES;
1406: tty_update_mode(tty, MODE_CURSOR, NULL);
1407:
1408: tty_cursor(tty, 0, 0);
1409: tty_region_off(tty);
1410: tty_margin_off(tty);
1411: } else
1412: tty->mode = MODE_CURSOR;
1.1 nicm 1413: }
1414:
1.214 nicm 1415: /* Turn off margin. */
1416: void
1417: tty_region_off(struct tty *tty)
1418: {
1419: tty_region(tty, 0, tty->sy - 1);
1420: }
1421:
1.40 nicm 1422: /* Set region inside pane. */
1.208 nicm 1423: static void
1.196 nicm 1424: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
1425: u_int rlower)
1.1 nicm 1426: {
1.113 nicm 1427: tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39 nicm 1428: }
1429:
1.40 nicm 1430: /* Set region at absolute position. */
1.214 nicm 1431: static void
1.40 nicm 1432: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 1433: {
1434: if (tty->rlower == rlower && tty->rupper == rupper)
1435: return;
1.1 nicm 1436: if (!tty_term_has(tty->term, TTYC_CSR))
1437: return;
1.39 nicm 1438:
1439: tty->rupper = rupper;
1440: tty->rlower = rlower;
1.55 nicm 1441:
1442: /*
1443: * Some terminals (such as PuTTY) do not correctly reset the cursor to
1444: * 0,0 if it is beyond the last column (they do not reset their wrap
1445: * flag so further output causes a line feed). As a workaround, do an
1446: * explicit move to 0 first.
1447: */
1448: if (tty->cx >= tty->sx)
1449: tty_cursor(tty, 0, tty->cy);
1.42 nicm 1450:
1.39 nicm 1451: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.249 nicm 1452: tty->cx = tty->cy = UINT_MAX;
1.212 nicm 1453: }
1454:
1.214 nicm 1455: /* Turn off margin. */
1456: void
1457: tty_margin_off(struct tty *tty)
1458: {
1459: tty_margin(tty, 0, tty->sx - 1);
1460: }
1461:
1.212 nicm 1462: /* Set margin inside pane. */
1463: static void
1464: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
1465: {
1466: tty_margin(tty, ctx->xoff, ctx->xoff + ctx->wp->sx - 1);
1467: }
1468:
1469: /* Set margin at absolute position. */
1.214 nicm 1470: static void
1.212 nicm 1471: tty_margin(struct tty *tty, u_int rleft, u_int rright)
1472: {
1473: char s[64];
1474:
1475: if (!tty_use_margin(tty))
1476: return;
1477: if (tty->rleft == rleft && tty->rright == rright)
1478: return;
1479:
1.232 nicm 1480: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.231 nicm 1481:
1.212 nicm 1482: tty->rleft = rleft;
1483: tty->rright = rright;
1484:
1.232 nicm 1485: if (rleft == 0 && rright == tty->sx - 1)
1486: snprintf(s, sizeof s, "\033[s");
1487: else
1488: snprintf(s, sizeof s, "\033[%u;%us", rleft + 1, rright + 1);
1.212 nicm 1489: tty_puts(tty, s);
1.249 nicm 1490: tty->cx = tty->cy = UINT_MAX;
1.253 nicm 1491: }
1492:
1493: /*
1494: * Move the cursor, unless it would wrap itself when the next character is
1495: * printed.
1496: */
1497: static void
1498: tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
1499: u_int cx, u_int cy)
1500: {
1.268 nicm 1501: if (!ctx->wrapped ||
1502: !tty_pane_full_width(tty, ctx) ||
1.253 nicm 1503: (tty->term->flags & TERM_EARLYWRAP) ||
1504: ctx->xoff + cx != 0 ||
1505: ctx->yoff + cy != tty->cy + 1 ||
1.254 nicm 1506: tty->cx < tty->sx ||
1507: tty->cy == tty->rlower)
1.253 nicm 1508: tty_cursor_pane(tty, ctx, cx, cy);
1509: else
1510: log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
1.1 nicm 1511: }
1512:
1.42 nicm 1513: /* Move cursor inside pane. */
1.208 nicm 1514: static void
1.41 nicm 1515: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1516: {
1.113 nicm 1517: tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41 nicm 1518: }
1519:
1.42 nicm 1520: /* Move cursor to absolute position. */
1.41 nicm 1521: void
1522: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 1523: {
1.42 nicm 1524: struct tty_term *term = tty->term;
1525: u_int thisx, thisy;
1526: int change;
1.77 nicm 1527:
1.42 nicm 1528: if (cx > tty->sx - 1)
1529: cx = tty->sx - 1;
1530:
1531: thisx = tty->cx;
1532: thisy = tty->cy;
1533:
1534: /* No change. */
1535: if (cx == thisx && cy == thisy)
1536: return;
1537:
1.43 nicm 1538: /* Very end of the line, just use absolute movement. */
1539: if (thisx > tty->sx - 1)
1540: goto absolute;
1541:
1.42 nicm 1542: /* Move to home position (0, 0). */
1543: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1544: tty_putcode(tty, TTYC_HOME);
1545: goto out;
1546: }
1547:
1548: /* Zero on the next line. */
1.48 nicm 1549: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1 nicm 1550: tty_putc(tty, '\r');
1.42 nicm 1551: tty_putc(tty, '\n');
1552: goto out;
1553: }
1554:
1.45 nicm 1555: /* Moving column or row. */
1.42 nicm 1556: if (cy == thisy) {
1.45 nicm 1557: /*
1558: * Moving column only, row staying the same.
1559: */
1560:
1.42 nicm 1561: /* To left edge. */
1.202 nicm 1562: if (cx == 0) {
1.42 nicm 1563: tty_putc(tty, '\r');
1564: goto out;
1565: }
1566:
1567: /* One to the left. */
1568: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1569: tty_putcode(tty, TTYC_CUB1);
1570: goto out;
1571: }
1572:
1573: /* One to the right. */
1574: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1575: tty_putcode(tty, TTYC_CUF1);
1576: goto out;
1577: }
1578:
1579: /* Calculate difference. */
1580: change = thisx - cx; /* +ve left, -ve right */
1581:
1582: /*
1583: * Use HPA if change is larger than absolute, otherwise move
1584: * the cursor with CUB/CUF.
1585: */
1.87 nicm 1586: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 1587: tty_putcode1(tty, TTYC_HPA, cx);
1588: goto out;
1589: } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1.202 nicm 1590: if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
1591: tty_putcode(tty, TTYC_CUB1);
1592: tty_putcode(tty, TTYC_CUB1);
1593: goto out;
1594: }
1.42 nicm 1595: tty_putcode1(tty, TTYC_CUB, change);
1596: goto out;
1597: } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1598: tty_putcode1(tty, TTYC_CUF, -change);
1599: goto out;
1600: }
1.45 nicm 1601: } else if (cx == thisx) {
1602: /*
1603: * Moving row only, column staying the same.
1604: */
1.42 nicm 1605:
1606: /* One above. */
1.77 nicm 1607: if (thisy != tty->rupper &&
1.42 nicm 1608: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1609: tty_putcode(tty, TTYC_CUU1);
1610: goto out;
1611: }
1612:
1613: /* One below. */
1.49 nicm 1614: if (thisy != tty->rlower &&
1.42 nicm 1615: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1616: tty_putcode(tty, TTYC_CUD1);
1617: goto out;
1618: }
1619:
1620: /* Calculate difference. */
1621: change = thisy - cy; /* +ve up, -ve down */
1622:
1623: /*
1.77 nicm 1624: * Try to use VPA if change is larger than absolute or if this
1625: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 1626: */
1.87 nicm 1627: if ((u_int) abs(change) > cy ||
1.42 nicm 1628: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 1629: (change > 0 && cy - change < tty->rupper)) {
1630: if (tty_term_has(term, TTYC_VPA)) {
1631: tty_putcode1(tty, TTYC_VPA, cy);
1632: goto out;
1633: }
1.42 nicm 1634: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1635: tty_putcode1(tty, TTYC_CUU, change);
1636: goto out;
1637: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1638: tty_putcode1(tty, TTYC_CUD, -change);
1639: goto out;
1640: }
1641: }
1642:
1.43 nicm 1643: absolute:
1.42 nicm 1644: /* Absolute movement. */
1645: tty_putcode2(tty, TTYC_CUP, cy, cx);
1646:
1647: out:
1648: tty->cx = cx;
1649: tty->cy = cy;
1.1 nicm 1650: }
1651:
1652: void
1.176 nicm 1653: tty_attributes(struct tty *tty, const struct grid_cell *gc,
1654: const struct window_pane *wp)
1.1 nicm 1655: {
1.63 nicm 1656: struct grid_cell *tc = &tty->cell, gc2;
1.255 nicm 1657: int changed;
1.61 nicm 1658:
1.228 nicm 1659: /* Ignore cell if it is the same as the last one. */
1660: if (wp != NULL &&
1661: (int)wp->id == tty->last_wp &&
1662: ~(wp->window->flags & WINDOW_STYLECHANGED) &&
1663: gc->attr == tty->last_cell.attr &&
1664: gc->fg == tty->last_cell.fg &&
1665: gc->bg == tty->last_cell.bg)
1666: return;
1667: tty->last_wp = (wp != NULL ? (int)wp->id : -1);
1668: memcpy(&tty->last_cell, gc, sizeof tty->last_cell);
1669:
1670: /* Copy cell and update default colours. */
1.85 nicm 1671: memcpy(&gc2, gc, sizeof gc2);
1.203 nicm 1672: if (wp != NULL)
1673: tty_default_colours(&gc2, wp);
1.63 nicm 1674:
1.18 nicm 1675: /*
1676: * If no setab, try to use the reverse attribute as a best-effort for a
1677: * non-default background. This is a bit of a hack but it doesn't do
1678: * any serious harm and makes a couple of applications happier.
1679: */
1680: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 1681: if (gc2.attr & GRID_ATTR_REVERSE) {
1682: if (gc2.fg != 7 && gc2.fg != 8)
1.64 nicm 1683: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 1684: } else {
1.85 nicm 1685: if (gc2.bg != 0 && gc2.bg != 8)
1.64 nicm 1686: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 1687: }
1688: }
1.1 nicm 1689:
1.85 nicm 1690: /* Fix up the colours if necessary. */
1.219 nicm 1691: tty_check_fg(tty, wp, &gc2);
1692: tty_check_bg(tty, wp, &gc2);
1.85 nicm 1693:
1.64 nicm 1694: /* If any bits are being cleared, reset everything. */
1.85 nicm 1695: if (tc->attr & ~gc2.attr)
1.64 nicm 1696: tty_reset(tty);
1697:
1698: /*
1699: * Set the colours. This may call tty_reset() (so it comes next) and
1700: * may add to (NOT remove) the desired attributes by changing new_attr.
1701: */
1.85 nicm 1702: tty_colours(tty, &gc2);
1.64 nicm 1703:
1.1 nicm 1704: /* Filter out attribute bits already set. */
1.85 nicm 1705: changed = gc2.attr & ~tc->attr;
1706: tc->attr = gc2.attr;
1.1 nicm 1707:
1708: /* Set the attributes. */
1709: if (changed & GRID_ATTR_BRIGHT)
1710: tty_putcode(tty, TTYC_BOLD);
1711: if (changed & GRID_ATTR_DIM)
1712: tty_putcode(tty, TTYC_DIM);
1.180 nicm 1713: if (changed & GRID_ATTR_ITALICS)
1714: tty_set_italics(tty);
1.1 nicm 1715: if (changed & GRID_ATTR_UNDERSCORE)
1716: tty_putcode(tty, TTYC_SMUL);
1717: if (changed & GRID_ATTR_BLINK)
1718: tty_putcode(tty, TTYC_BLINK);
1719: if (changed & GRID_ATTR_REVERSE) {
1720: if (tty_term_has(tty->term, TTYC_REV))
1721: tty_putcode(tty, TTYC_REV);
1722: else if (tty_term_has(tty->term, TTYC_SMSO))
1723: tty_putcode(tty, TTYC_SMSO);
1724: }
1725: if (changed & GRID_ATTR_HIDDEN)
1726: tty_putcode(tty, TTYC_INVIS);
1.255 nicm 1727: if (changed & GRID_ATTR_STRIKETHROUGH)
1728: tty_putcode(tty, TTYC_SMXX);
1.90 nicm 1729: if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1730: tty_putcode(tty, TTYC_SMACS);
1731: }
1732:
1.207 nicm 1733: static void
1.85 nicm 1734: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1735: {
1.61 nicm 1736: struct grid_cell *tc = &tty->cell;
1.204 nicm 1737: int have_ax;
1.61 nicm 1738:
1739: /* No changes? Nothing is necessary. */
1.204 nicm 1740: if (gc->fg == tc->fg && gc->bg == tc->bg)
1.63 nicm 1741: return;
1.1 nicm 1742:
1.61 nicm 1743: /*
1744: * Is either the default colour? This is handled specially because the
1745: * best solution might be to reset both colours to default, in which
1746: * case if only one is default need to fall onward to set the other
1747: * colour.
1748: */
1.204 nicm 1749: if (gc->fg == 8 || gc->bg == 8) {
1.61 nicm 1750: /*
1751: * If don't have AX but do have op, send sgr0 (op can't
1752: * actually be used because it is sometimes the same as sgr0
1753: * and sometimes isn't). This resets both colours to default.
1754: *
1755: * Otherwise, try to set the default colour only as needed.
1756: */
1.174 nicm 1757: have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61 nicm 1758: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1759: tty_reset(tty);
1760: else {
1.204 nicm 1761: if (gc->fg == 8 && tc->fg != 8) {
1.61 nicm 1762: if (have_ax)
1763: tty_puts(tty, "\033[39m");
1.204 nicm 1764: else if (tc->fg != 7)
1.61 nicm 1765: tty_putcode1(tty, TTYC_SETAF, 7);
1766: tc->fg = 8;
1767: }
1.204 nicm 1768: if (gc->bg == 8 && tc->bg != 8) {
1.77 nicm 1769: if (have_ax)
1.61 nicm 1770: tty_puts(tty, "\033[49m");
1.204 nicm 1771: else if (tc->bg != 0)
1.61 nicm 1772: tty_putcode1(tty, TTYC_SETAB, 0);
1773: tc->bg = 8;
1774: }
1775: }
1776: }
1.1 nicm 1777:
1.61 nicm 1778: /* Set the foreground colour. */
1.204 nicm 1779: if (gc->fg != 8 && gc->fg != tc->fg)
1.85 nicm 1780: tty_colours_fg(tty, gc);
1.1 nicm 1781:
1.61 nicm 1782: /*
1783: * Set the background colour. This must come after the foreground as
1784: * tty_colour_fg() can call tty_reset().
1785: */
1.204 nicm 1786: if (gc->bg != 8 && gc->bg != tc->bg)
1.73 nicm 1787: tty_colours_bg(tty, gc);
1.1 nicm 1788: }
1789:
1.219 nicm 1790: static void
1791: tty_check_fg(struct tty *tty, const struct window_pane *wp,
1792: struct grid_cell *gc)
1.85 nicm 1793: {
1.204 nicm 1794: u_char r, g, b;
1795: u_int colours;
1.223 nicm 1796: int c;
1.85 nicm 1797:
1.219 nicm 1798: /* Perform substitution if this pane has a palette */
1799: if ((~gc->flags & GRID_FLAG_NOPALETTE) &&
1.223 nicm 1800: (c = window_pane_get_palette(wp, gc->fg)) != -1)
1801: gc->fg = c;
1.219 nicm 1802:
1.199 nicm 1803: /* Is this a 24-bit colour? */
1.204 nicm 1804: if (gc->fg & COLOUR_FLAG_RGB) {
1.199 nicm 1805: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1806: if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204 nicm 1807: colour_split_rgb(gc->fg, &r, &g, &b);
1808: gc->fg = colour_find_rgb(r, g, b);
1809: } else
1.200 nicm 1810: return;
1.199 nicm 1811: }
1.224 nicm 1812:
1813: /* How many colours does this terminal have? */
1814: if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
1815: colours = 256;
1816: else
1817: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 1818:
1.85 nicm 1819: /* Is this a 256-colour colour? */
1.204 nicm 1820: if (gc->fg & COLOUR_FLAG_256) {
1.85 nicm 1821: /* And not a 256 colour mode? */
1.224 nicm 1822: if (colours != 256) {
1.85 nicm 1823: gc->fg = colour_256to16(gc->fg);
1824: if (gc->fg & 8) {
1825: gc->fg &= 7;
1.175 nicm 1826: if (colours >= 16)
1827: gc->fg += 90;
1828: else
1829: gc->attr |= GRID_ATTR_BRIGHT;
1.85 nicm 1830: } else
1831: gc->attr &= ~GRID_ATTR_BRIGHT;
1832: }
1833: return;
1834: }
1835:
1836: /* Is this an aixterm colour? */
1837: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1838: gc->fg -= 90;
1839: gc->attr |= GRID_ATTR_BRIGHT;
1840: }
1841: }
1842:
1.219 nicm 1843: static void
1844: tty_check_bg(struct tty *tty, const struct window_pane *wp,
1845: struct grid_cell *gc)
1.85 nicm 1846: {
1.204 nicm 1847: u_char r, g, b;
1848: u_int colours;
1.223 nicm 1849: int c;
1.85 nicm 1850:
1.219 nicm 1851: /* Perform substitution if this pane has a palette */
1852: if ((~gc->flags & GRID_FLAG_NOPALETTE) &&
1.223 nicm 1853: (c = window_pane_get_palette(wp, gc->bg)) != -1)
1854: gc->bg = c;
1.219 nicm 1855:
1.199 nicm 1856: /* Is this a 24-bit colour? */
1.204 nicm 1857: if (gc->bg & COLOUR_FLAG_RGB) {
1.199 nicm 1858: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1859: if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204 nicm 1860: colour_split_rgb(gc->bg, &r, &g, &b);
1861: gc->bg = colour_find_rgb(r, g, b);
1862: } else
1.200 nicm 1863: return;
1.199 nicm 1864: }
1.224 nicm 1865:
1866: /* How many colours does this terminal have? */
1867: if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
1868: colours = 256;
1869: else
1870: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 1871:
1.85 nicm 1872: /* Is this a 256-colour colour? */
1.204 nicm 1873: if (gc->bg & COLOUR_FLAG_256) {
1.85 nicm 1874: /*
1875: * And not a 256 colour mode? Translate to 16-colour
1876: * palette. Bold background doesn't exist portably, so just
1877: * discard the bold bit if set.
1878: */
1.224 nicm 1879: if (colours != 256) {
1.85 nicm 1880: gc->bg = colour_256to16(gc->bg);
1.175 nicm 1881: if (gc->bg & 8) {
1.85 nicm 1882: gc->bg &= 7;
1.175 nicm 1883: if (colours >= 16)
1884: gc->fg += 90;
1885: }
1.85 nicm 1886: }
1887: return;
1888: }
1889:
1890: /* Is this an aixterm colour? */
1.175 nicm 1891: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85 nicm 1892: gc->bg -= 90;
1893: }
1894:
1.207 nicm 1895: static void
1.85 nicm 1896: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1897: {
1.61 nicm 1898: struct grid_cell *tc = &tty->cell;
1.79 nicm 1899: char s[32];
1.1 nicm 1900:
1.204 nicm 1901: /* Is this a 24-bit or 256-colour colour? */
1902: if (gc->fg & COLOUR_FLAG_RGB ||
1903: gc->fg & COLOUR_FLAG_256) {
1904: if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61 nicm 1905: goto save_fg;
1.199 nicm 1906: /* Should not get here, already converted in tty_check_fg. */
1.85 nicm 1907: return;
1.1 nicm 1908: }
1909:
1.79 nicm 1910: /* Is this an aixterm bright colour? */
1.204 nicm 1911: if (gc->fg >= 90 && gc->fg <= 97) {
1912: xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
1.85 nicm 1913: tty_puts(tty, s);
1914: goto save_fg;
1.79 nicm 1915: }
1916:
1.61 nicm 1917: /* Otherwise set the foreground colour. */
1.204 nicm 1918: tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61 nicm 1919:
1.77 nicm 1920: save_fg:
1.61 nicm 1921: /* Save the new values in the terminal current cell. */
1.204 nicm 1922: tc->fg = gc->fg;
1.1 nicm 1923: }
1924:
1.207 nicm 1925: static void
1.73 nicm 1926: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1927: {
1.61 nicm 1928: struct grid_cell *tc = &tty->cell;
1.79 nicm 1929: char s[32];
1.1 nicm 1930:
1.204 nicm 1931: /* Is this a 24-bit or 256-colour colour? */
1932: if (gc->bg & COLOUR_FLAG_RGB ||
1933: gc->bg & COLOUR_FLAG_256) {
1934: if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61 nicm 1935: goto save_bg;
1.199 nicm 1936: /* Should not get here, already converted in tty_check_bg. */
1.85 nicm 1937: return;
1.79 nicm 1938: }
1939:
1940: /* Is this an aixterm bright colour? */
1.204 nicm 1941: if (gc->bg >= 90 && gc->bg <= 97) {
1942: xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
1.175 nicm 1943: tty_puts(tty, s);
1944: goto save_bg;
1.1 nicm 1945: }
1946:
1.61 nicm 1947: /* Otherwise set the background colour. */
1.204 nicm 1948: tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61 nicm 1949:
1950: save_bg:
1951: /* Save the new values in the terminal current cell. */
1.204 nicm 1952: tc->bg = gc->bg;
1.61 nicm 1953: }
1954:
1.207 nicm 1955: static int
1.204 nicm 1956: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61 nicm 1957: {
1.204 nicm 1958: u_char r, g, b;
1.61 nicm 1959: char s[32];
1960:
1.204 nicm 1961: if (colour & COLOUR_FLAG_256) {
1962: /*
1963: * If the user has specified -2 to the client, setaf and setab
1964: * may not work (or they may not want to use them), so send the
1965: * usual sequence.
1966: */
1967: if (tty->term_flags & TERM_256COLOURS)
1968: goto fallback_256;
1.189 nicm 1969:
1.204 nicm 1970: /*
1971: * If the terminfo entry has 256 colours and setaf and setab
1972: * exist, assume that they work correctly.
1973: */
1974: if (tty->term->flags & TERM_256COLOURS) {
1975: if (*type == '3') {
1976: if (!tty_term_has(tty->term, TTYC_SETAF))
1977: goto fallback_256;
1978: tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
1979: } else {
1980: if (!tty_term_has(tty->term, TTYC_SETAB))
1981: goto fallback_256;
1982: tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
1983: }
1984: return (0);
1.188 nicm 1985: }
1.204 nicm 1986: goto fallback_256;
1987: }
1988:
1989: if (colour & COLOUR_FLAG_RGB) {
1990: if (!tty_term_flag(tty->term, TTYC_TC))
1991: return (-1);
1992:
1993: colour_split_rgb(colour & 0xffffff, &r, &g, &b);
1994: xsnprintf(s, sizeof s, "\033[%s;2;%hhu;%hhu;%hhum", type,
1995: r, g, b);
1996: tty_puts(tty, s);
1.165 nicm 1997: return (0);
1998: }
1.61 nicm 1999:
1.165 nicm 2000: return (-1);
1.188 nicm 2001:
1.204 nicm 2002: fallback_256:
2003: xsnprintf(s, sizeof s, "\033[%s;5;%dm", type, colour & 0xff);
1.188 nicm 2004: tty_puts(tty, s);
2005: return (0);
1.176 nicm 2006: }
2007:
1.207 nicm 2008: static void
1.176 nicm 2009: tty_default_colours(struct grid_cell *gc, const struct window_pane *wp)
2010: {
1.203 nicm 2011: struct window *w = wp->window;
2012: struct options *oo = w->options;
2013: const struct grid_cell *agc, *pgc, *wgc;
1.223 nicm 2014: int c;
1.203 nicm 2015:
2016: if (w->flags & WINDOW_STYLECHANGED) {
2017: w->flags &= ~WINDOW_STYLECHANGED;
2018: agc = options_get_style(oo, "window-active-style");
2019: memcpy(&w->active_style, agc, sizeof w->active_style);
2020: wgc = options_get_style(oo, "window-style");
2021: memcpy(&w->style, wgc, sizeof w->style);
2022: } else {
2023: agc = &w->active_style;
2024: wgc = &w->style;
2025: }
1.176 nicm 2026: pgc = &wp->colgc;
2027:
1.204 nicm 2028: if (gc->fg == 8) {
2029: if (pgc->fg != 8)
1.176 nicm 2030: gc->fg = pgc->fg;
1.204 nicm 2031: else if (wp == w->active && agc->fg != 8)
1.176 nicm 2032: gc->fg = agc->fg;
1.204 nicm 2033: else
1.176 nicm 2034: gc->fg = wgc->fg;
1.219 nicm 2035:
1.223 nicm 2036: if (gc->fg != 8 &&
2037: (c = window_pane_get_palette(wp, gc->fg)) != -1)
2038: gc->fg = c;
1.176 nicm 2039: }
2040:
1.204 nicm 2041: if (gc->bg == 8) {
2042: if (pgc->bg != 8)
1.176 nicm 2043: gc->bg = pgc->bg;
1.204 nicm 2044: else if (wp == w->active && agc->bg != 8)
1.176 nicm 2045: gc->bg = agc->bg;
1.204 nicm 2046: else
1.176 nicm 2047: gc->bg = wgc->bg;
1.219 nicm 2048:
1.223 nicm 2049: if (gc->bg != 8 &&
2050: (c = window_pane_get_palette(wp, gc->bg)) != -1)
2051: gc->bg = c;
1.176 nicm 2052: }
1.210 nicm 2053: }
2054:
2055: static void
2056: tty_default_attributes(struct tty *tty, const struct window_pane *wp, u_int bg)
2057: {
2058: static struct grid_cell gc;
2059:
2060: memcpy(&gc, &grid_default_cell, sizeof gc);
2061: gc.bg = bg;
2062: tty_attributes(tty, &gc, wp);
1.1 nicm 2063: }