Annotation of src/usr.bin/tmux/tty.c, Revision 1.206
1.206 ! nicm 1: /* $OpenBSD: tty.c,v 1.205 2016/07/15 00:49:08 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.1 nicm 24: #include <errno.h>
25: #include <fcntl.h>
1.106 nicm 26: #include <resolv.h>
1.42 nicm 27: #include <stdlib.h>
1.1 nicm 28: #include <string.h>
29: #include <termios.h>
30: #include <unistd.h>
31:
32: #include "tmux.h"
33:
1.192 nicm 34: static int tty_log_fd = -1;
35:
1.67 nicm 36: void tty_read_callback(struct bufferevent *, void *);
1.66 nicm 37: void tty_error_callback(struct bufferevent *, short, void *);
38:
1.201 nicm 39: static int tty_client_ready(struct client *, struct window_pane *);
40:
1.180 nicm 41: void tty_set_italics(struct tty *);
1.204 nicm 42: int tty_try_colour(struct tty *, int, const char *);
1.1 nicm 43:
1.85 nicm 44: void tty_colours(struct tty *, const struct grid_cell *);
45: void tty_check_fg(struct tty *, struct grid_cell *);
46: void tty_check_bg(struct tty *, struct grid_cell *);
47: void tty_colours_fg(struct tty *, const struct grid_cell *);
1.73 nicm 48: void tty_colours_bg(struct tty *, const struct grid_cell *);
1.1 nicm 49:
1.120 nicm 50: int tty_large_region(struct tty *, const struct tty_ctx *);
1.176 nicm 51: int tty_fake_bce(const struct tty *, const struct window_pane *);
1.24 nicm 52: void tty_redraw_region(struct tty *, const struct tty_ctx *);
1.196 nicm 53: void tty_emulate_repeat(struct tty *, enum tty_code_code, enum tty_code_code,
54: u_int);
1.133 nicm 55: void tty_repeat_space(struct tty *, u_int);
1.176 nicm 56: void tty_cell(struct tty *, const struct grid_cell *,
57: const struct window_pane *);
58: void tty_default_colours(struct grid_cell *, const struct window_pane *);
1.1 nicm 59:
1.90 nicm 60: #define tty_use_acs(tty) \
1.131 nicm 61: (tty_term_has((tty)->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
1.90 nicm 62:
1.132 nicm 63: #define tty_pane_full_width(tty, ctx) \
64: ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
65:
1.192 nicm 66: void
67: tty_create_log(void)
68: {
69: char name[64];
70:
71: xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
72:
73: tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
74: if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
75: fatal("fcntl failed");
76: }
77:
1.185 nicm 78: int
1.136 nicm 79: tty_init(struct tty *tty, struct client *c, int fd, char *term)
1.1 nicm 80: {
1.30 nicm 81: char *path;
82:
1.185 nicm 83: if (!isatty(fd))
84: return (-1);
85:
1.30 nicm 86: memset(tty, 0, sizeof *tty);
1.22 nicm 87:
1.9 nicm 88: if (term == NULL || *term == '\0')
1.1 nicm 89: tty->termname = xstrdup("unknown");
90: else
91: tty->termname = xstrdup(term);
1.30 nicm 92: tty->fd = fd;
1.136 nicm 93: tty->client = c;
1.30 nicm 94:
95: if ((path = ttyname(fd)) == NULL)
1.185 nicm 96: return (-1);
1.30 nicm 97: tty->path = xstrdup(path);
1.108 nicm 98: tty->cstyle = 0;
1.107 nicm 99: tty->ccolour = xstrdup("");
1.30 nicm 100:
1.1 nicm 101: tty->flags = 0;
102: tty->term_flags = 0;
1.185 nicm 103:
104: return (0);
1.31 nicm 105: }
106:
1.88 nicm 107: int
1.31 nicm 108: tty_resize(struct tty *tty)
109: {
110: struct winsize ws;
1.88 nicm 111: u_int sx, sy;
1.31 nicm 112:
113: if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88 nicm 114: sx = ws.ws_col;
115: if (sx == 0)
116: sx = 80;
117: sy = ws.ws_row;
118: if (sy == 0)
119: sy = 24;
120: } else {
121: sx = 80;
122: sy = 24;
1.31 nicm 123: }
1.114 nicm 124: if (!tty_set_size(tty, sx, sy))
1.88 nicm 125: return (0);
1.31 nicm 126:
127: tty->cx = UINT_MAX;
128: tty->cy = UINT_MAX;
129:
130: tty->rupper = UINT_MAX;
131: tty->rlower = UINT_MAX;
1.88 nicm 132:
133: /*
134: * If the terminal has been started, reset the actual scroll region and
135: * cursor position, as this may not have happened.
136: */
137: if (tty->flags & TTY_STARTED) {
138: tty_cursor(tty, 0, 0);
139: tty_region(tty, 0, tty->sy - 1);
140: }
141:
1.114 nicm 142: return (1);
143: }
144:
145: int
146: tty_set_size(struct tty *tty, u_int sx, u_int sy) {
147: if (sx == tty->sx && sy == tty->sy)
148: return (0);
149: tty->sx = sx;
150: tty->sy = sy;
1.88 nicm 151: return (1);
1.1 nicm 152: }
153:
154: int
1.166 nicm 155: tty_open(struct tty *tty, char **cause)
1.1 nicm 156: {
1.166 nicm 157: tty->term = tty_term_find(tty->termname, tty->fd, cause);
1.21 nicm 158: if (tty->term == NULL) {
159: tty_close(tty);
160: return (-1);
161: }
162: tty->flags |= TTY_OPENED;
1.1 nicm 163:
1.149 nicm 164: tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_TIMER);
1.1 nicm 165:
1.196 nicm 166: tty->event = bufferevent_new(tty->fd, tty_read_callback, NULL,
167: tty_error_callback, tty);
1.1 nicm 168:
169: tty_start_tty(tty);
170:
1.148 nicm 171: tty_keys_build(tty);
1.1 nicm 172:
173: return (0);
174: }
175:
176: void
1.194 nicm 177: tty_read_callback(__unused struct bufferevent *bufev, void *data)
1.67 nicm 178: {
179: struct tty *tty = data;
180:
181: while (tty_keys_next(tty))
182: ;
183: }
184:
185: void
1.194 nicm 186: tty_error_callback(__unused struct bufferevent *bufev, __unused short what,
187: __unused void *data)
1.66 nicm 188: {
189: }
190:
191: void
1.127 nicm 192: tty_init_termios(int fd, struct termios *orig_tio, struct bufferevent *bufev)
1.1 nicm 193: {
1.128 nicm 194: struct termios tio;
1.33 nicm 195:
1.127 nicm 196: if (fd == -1 || tcgetattr(fd, orig_tio) != 0)
1.33 nicm 197: return;
1.1 nicm 198:
1.127 nicm 199: setblocking(fd, 0);
1.34 nicm 200:
1.127 nicm 201: if (bufev != NULL)
202: bufferevent_enable(bufev, EV_READ|EV_WRITE);
1.66 nicm 203:
1.127 nicm 204: memcpy(&tio, orig_tio, sizeof tio);
1.1 nicm 205: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
206: tio.c_iflag |= IGNBRK;
207: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
208: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
1.169 jsg 209: ECHOPRT|ECHOKE|ISIG);
1.1 nicm 210: tio.c_cc[VMIN] = 1;
1.60 deraadt 211: tio.c_cc[VTIME] = 0;
1.127 nicm 212: if (tcsetattr(fd, TCSANOW, &tio) == 0)
213: tcflush(fd, TCIOFLUSH);
214: }
215:
216: void
217: tty_start_tty(struct tty *tty)
218: {
219: tty_init_termios(tty->fd, &tty->tio, tty->event);
1.1 nicm 220:
1.10 nicm 221: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 222:
1.25 nicm 223: tty_putcode(tty, TTYC_SGR0);
224: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
225:
1.76 nicm 226: tty_putcode(tty, TTYC_RMKX);
1.90 nicm 227: if (tty_use_acs(tty))
228: tty_putcode(tty, TTYC_ENACS);
1.1 nicm 229: tty_putcode(tty, TTYC_CLEAR);
230:
231: tty_putcode(tty, TTYC_CNORM);
232: if (tty_term_has(tty->term, TTYC_KMOUS))
1.179 nicm 233: tty_puts(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l");
1.121 nicm 234:
1.189 nicm 235: if (tty_term_flag(tty->term, TTYC_XT)) {
1.191 nicm 236: if (options_get_number(global_options, "focus-events")) {
1.162 nicm 237: tty->flags |= TTY_FOCUS;
238: tty_puts(tty, "\033[?1004h");
239: }
240: }
1.1 nicm 241:
242: tty->cx = UINT_MAX;
243: tty->cy = UINT_MAX;
244:
245: tty->rlower = UINT_MAX;
246: tty->rupper = UINT_MAX;
247:
248: tty->mode = MODE_CURSOR;
1.20 nicm 249:
250: tty->flags |= TTY_STARTED;
1.107 nicm 251:
252: tty_force_cursor_colour(tty, "");
1.177 nicm 253:
254: tty->mouse_drag_flag = 0;
255: tty->mouse_drag_update = NULL;
256: tty->mouse_drag_release = NULL;
1.128 nicm 257: }
258:
259: void
1.1 nicm 260: tty_stop_tty(struct tty *tty)
261: {
262: struct winsize ws;
263:
1.20 nicm 264: if (!(tty->flags & TTY_STARTED))
265: return;
266: tty->flags &= ~TTY_STARTED;
267:
1.66 nicm 268: bufferevent_disable(tty->event, EV_READ|EV_WRITE);
269:
1.1 nicm 270: /*
271: * Be flexible about error handling and try not kill the server just
272: * because the fd is invalid. Things like ssh -t can easily leave us
273: * with a dead tty.
274: */
275: if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
276: return;
277: if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
278: return;
279:
280: tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.90 nicm 281: if (tty_use_acs(tty))
282: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1 nicm 283: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
284: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
285: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.160 nicm 286: if (tty_term_has(tty->term, TTYC_SS) && tty->cstyle != 0) {
287: if (tty_term_has(tty->term, TTYC_SE))
288: tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
1.109 nicm 289: else
1.160 nicm 290: tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
1.108 nicm 291: }
1.173 nicm 292: if (tty->mode & MODE_BRACKETPASTE)
293: tty_raw(tty, "\033[?2004l");
1.107 nicm 294: tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1 nicm 295:
296: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
297: if (tty_term_has(tty->term, TTYC_KMOUS))
1.179 nicm 298: tty_raw(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l");
1.151 nicm 299:
1.189 nicm 300: if (tty_term_flag(tty->term, TTYC_XT)) {
1.162 nicm 301: if (tty->flags & TTY_FOCUS) {
302: tty->flags &= ~TTY_FOCUS;
1.172 nicm 303: tty_raw(tty, "\033[?1004l");
1.162 nicm 304: }
305: }
1.10 nicm 306:
307: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150 nicm 308:
309: setblocking(tty->fd, 1);
1.1 nicm 310: }
311:
312: void
1.20 nicm 313: tty_close(struct tty *tty)
1.1 nicm 314: {
1.123 nicm 315: if (event_initialized(&tty->key_timer))
316: evtimer_del(&tty->key_timer);
1.20 nicm 317: tty_stop_tty(tty);
1.1 nicm 318:
1.21 nicm 319: if (tty->flags & TTY_OPENED) {
1.66 nicm 320: bufferevent_free(tty->event);
321:
1.21 nicm 322: tty_term_free(tty->term);
323: tty_keys_free(tty);
324:
325: tty->flags &= ~TTY_OPENED;
326: }
1.1 nicm 327:
1.21 nicm 328: if (tty->fd != -1) {
329: close(tty->fd);
330: tty->fd = -1;
331: }
1.1 nicm 332: }
333:
334: void
1.20 nicm 335: tty_free(struct tty *tty)
1.1 nicm 336: {
1.20 nicm 337: tty_close(tty);
1.1 nicm 338:
1.138 nicm 339: free(tty->ccolour);
1.190 nicm 340: free(tty->path);
341: free(tty->termname);
1.1 nicm 342: }
343:
344: void
345: tty_raw(struct tty *tty, const char *s)
346: {
1.150 nicm 347: ssize_t n, slen;
348: u_int i;
349:
350: slen = strlen(s);
351: for (i = 0; i < 5; i++) {
352: n = write(tty->fd, s, slen);
353: if (n >= 0) {
354: s += n;
355: slen -= n;
356: if (slen == 0)
357: break;
358: } else if (n == -1 && errno != EAGAIN)
359: break;
360: usleep(100);
361: }
1.1 nicm 362: }
363:
364: void
365: tty_putcode(struct tty *tty, enum tty_code_code code)
366: {
367: tty_puts(tty, tty_term_string(tty->term, code));
368: }
369:
370: void
371: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
372: {
373: if (a < 0)
374: return;
375: tty_puts(tty, tty_term_string1(tty->term, code, a));
376: }
377:
378: void
379: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
380: {
381: if (a < 0 || b < 0)
382: return;
383: tty_puts(tty, tty_term_string2(tty->term, code, a, b));
384: }
385:
386: void
1.107 nicm 387: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
388: {
389: if (a != NULL)
390: tty_puts(tty, tty_term_ptr1(tty->term, code, a));
391: }
392:
393: void
1.167 nicm 394: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
395: const void *b)
1.106 nicm 396: {
397: if (a != NULL && b != NULL)
398: tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
399: }
400:
401: void
1.1 nicm 402: tty_puts(struct tty *tty, const char *s)
403: {
404: if (*s == '\0')
405: return;
1.66 nicm 406: bufferevent_write(tty->event, s, strlen(s));
1.1 nicm 407:
1.192 nicm 408: if (tty_log_fd != -1)
409: write(tty_log_fd, s, strlen(s));
1.1 nicm 410: }
411:
412: void
413: tty_putc(struct tty *tty, u_char ch)
414: {
1.90 nicm 415: const char *acs;
416: u_int sx;
1.1 nicm 417:
1.90 nicm 418: if (tty->cell.attr & GRID_ATTR_CHARSET) {
419: acs = tty_acs_get(tty, ch);
420: if (acs != NULL)
421: bufferevent_write(tty->event, acs, strlen(acs));
422: else
423: bufferevent_write(tty->event, &ch, 1);
424: } else
425: bufferevent_write(tty->event, &ch, 1);
1.1 nicm 426:
427: if (ch >= 0x20 && ch != 0x7f) {
428: sx = tty->sx;
429: if (tty->term->flags & TERM_EARLYWRAP)
430: sx--;
431:
1.46 nicm 432: if (tty->cx >= sx) {
433: tty->cx = 1;
434: if (tty->cy != tty->rlower)
435: tty->cy++;
1.1 nicm 436: } else
437: tty->cx++;
438: }
439:
1.192 nicm 440: if (tty_log_fd != -1)
441: write(tty_log_fd, &ch, 1);
1.1 nicm 442: }
443:
444: void
1.147 nicm 445: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5 nicm 446: {
1.147 nicm 447: bufferevent_write(tty->event, buf, len);
1.192 nicm 448: if (tty_log_fd != -1)
449: write(tty_log_fd, buf, len);
1.147 nicm 450: tty->cx += width;
1.5 nicm 451: }
452:
453: void
1.180 nicm 454: tty_set_italics(struct tty *tty)
455: {
456: const char *s;
457:
458: if (tty_term_has(tty->term, TTYC_SITM)) {
1.191 nicm 459: s = options_get_string(global_options, "default-terminal");
1.180 nicm 460: if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
461: tty_putcode(tty, TTYC_SITM);
462: return;
463: }
464: }
465: tty_putcode(tty, TTYC_SMSO);
466: }
467:
468: void
1.1 nicm 469: tty_set_title(struct tty *tty, const char *title)
470: {
1.105 nicm 471: if (!tty_term_has(tty->term, TTYC_TSL) ||
472: !tty_term_has(tty->term, TTYC_FSL))
1.1 nicm 473: return;
474:
1.105 nicm 475: tty_putcode(tty, TTYC_TSL);
1.1 nicm 476: tty_puts(tty, title);
1.105 nicm 477: tty_putcode(tty, TTYC_FSL);
1.1 nicm 478: }
479:
480: void
1.107 nicm 481: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
482: {
483: if (*ccolour == '\0')
484: tty_putcode(tty, TTYC_CR);
485: else
1.160 nicm 486: tty_putcode_ptr1(tty, TTYC_CS, ccolour);
1.138 nicm 487: free(tty->ccolour);
1.107 nicm 488: tty->ccolour = xstrdup(ccolour);
489: }
490:
491: void
492: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1 nicm 493: {
494: int changed;
495:
1.197 nicm 496: if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0)
1.107 nicm 497: tty_force_cursor_colour(tty, s->ccolour);
498:
1.1 nicm 499: if (tty->flags & TTY_NOCURSOR)
500: mode &= ~MODE_CURSOR;
501:
502: changed = mode ^ tty->mode;
1.183 nicm 503: if (changed & MODE_BLINKING) {
504: if (tty_term_has(tty->term, TTYC_CVVIS))
505: tty_putcode(tty, TTYC_CVVIS);
506: else
507: tty_putcode(tty, TTYC_CNORM);
508: changed |= MODE_CURSOR;
509: }
510: if (changed & MODE_CURSOR) {
511: if (mode & MODE_CURSOR)
512: tty_putcode(tty, TTYC_CNORM);
513: else
1.1 nicm 514: tty_putcode(tty, TTYC_CIVIS);
1.108 nicm 515: }
1.181 nicm 516: if (s != NULL && tty->cstyle != s->cstyle) {
1.160 nicm 517: if (tty_term_has(tty->term, TTYC_SS)) {
1.108 nicm 518: if (s->cstyle == 0 &&
1.160 nicm 519: tty_term_has(tty->term, TTYC_SE))
520: tty_putcode(tty, TTYC_SE);
1.108 nicm 521: else
1.160 nicm 522: tty_putcode1(tty, TTYC_SS, s->cstyle);
1.108 nicm 523: }
524: tty->cstyle = s->cstyle;
1.1 nicm 525: }
1.195 nicm 526: if (changed & ALL_MOUSE_MODES) {
1.94 nicm 527: if (mode & ALL_MOUSE_MODES) {
1.153 nicm 528: /*
529: * Enable the SGR (1006) extension unconditionally, as
530: * this is safe from misinterpretation. Do it in this
531: * order, because in some terminals it's the last one
532: * that takes effect and SGR is the preferred one.
533: */
534: tty_puts(tty, "\033[?1006h");
1.170 nicm 535: if (mode & MODE_MOUSE_BUTTON)
1.94 nicm 536: tty_puts(tty, "\033[?1002h");
1.98 nicm 537: else if (mode & MODE_MOUSE_STANDARD)
538: tty_puts(tty, "\033[?1000h");
1.86 nicm 539: } else {
1.170 nicm 540: if (tty->mode & MODE_MOUSE_BUTTON)
1.94 nicm 541: tty_puts(tty, "\033[?1002l");
1.98 nicm 542: else if (tty->mode & MODE_MOUSE_STANDARD)
543: tty_puts(tty, "\033[?1000l");
1.153 nicm 544: tty_puts(tty, "\033[?1006l");
1.86 nicm 545: }
1.76 nicm 546: }
547: if (changed & MODE_KKEYPAD) {
548: if (mode & MODE_KKEYPAD)
549: tty_putcode(tty, TTYC_SMKX);
550: else
551: tty_putcode(tty, TTYC_RMKX);
1.115 nicm 552: }
553: if (changed & MODE_BRACKETPASTE) {
554: if (mode & MODE_BRACKETPASTE)
555: tty_puts(tty, "\033[?2004h");
556: else
557: tty_puts(tty, "\033[?2004l");
1.1 nicm 558: }
559: tty->mode = mode;
560: }
561:
562: void
1.168 nicm 563: tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
564: enum tty_code_code code1, u_int n)
1.1 nicm 565: {
566: if (tty_term_has(tty->term, code))
567: tty_putcode1(tty, code, n);
568: else {
569: while (n-- > 0)
570: tty_putcode(tty, code1);
571: }
572: }
573:
1.133 nicm 574: void
575: tty_repeat_space(struct tty *tty, u_int n)
576: {
577: while (n-- > 0)
578: tty_putc(tty, ' ');
579: }
580:
1.1 nicm 581: /*
1.119 nicm 582: * Is the region large enough to be worth redrawing once later rather than
583: * probably several times now? Currently yes if it is more than 50% of the
584: * pane.
585: */
586: int
1.194 nicm 587: tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
1.119 nicm 588: {
589: struct window_pane *wp = ctx->wp;
590:
591: return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
592: }
593:
594: /*
1.176 nicm 595: * Return if BCE is needed but the terminal doesn't have it - it'll need to be
596: * emulated.
597: */
598: int
599: tty_fake_bce(const struct tty *tty, const struct window_pane *wp)
600: {
601: struct grid_cell gc;
602:
603: memcpy(&gc, &grid_default_cell, sizeof gc);
1.203 nicm 604: if (wp != NULL)
605: tty_default_colours(&gc, wp);
1.176 nicm 606:
1.204 nicm 607: if (gc.bg == 8)
1.176 nicm 608: return (0);
609: return (!tty_term_flag(tty->term, TTYC_BCE));
610: }
611:
612: /*
1.1 nicm 613: * Redraw scroll region using data from screen (already updated). Used when
614: * CSR not supported, or window is a pane that doesn't take up the full
615: * width of the terminal.
616: */
617: void
1.24 nicm 618: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 619: {
1.14 nicm 620: struct window_pane *wp = ctx->wp;
621: struct screen *s = wp->screen;
1.204 nicm 622: u_int i;
1.1 nicm 623:
624: /*
1.119 nicm 625: * If region is large, schedule a window redraw. In most cases this is
626: * likely to be followed by some more scrolling.
1.1 nicm 627: */
1.119 nicm 628: if (tty_large_region(tty, ctx)) {
1.1 nicm 629: wp->flags |= PANE_REDRAW;
630: return;
631: }
632:
1.14 nicm 633: if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
634: for (i = ctx->ocy; i < screen_size_y(s); i++)
1.176 nicm 635: tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff);
1.1 nicm 636: } else {
1.14 nicm 637: for (i = ctx->orupper; i <= ctx->orlower; i++)
1.176 nicm 638: tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff);
1.1 nicm 639: }
640: }
641:
642: void
1.176 nicm 643: tty_draw_pane(struct tty *tty, const struct window_pane *wp, u_int py, u_int ox,
644: u_int oy)
645: {
646: tty_draw_line(tty, wp, wp->screen, py, ox, oy);
647: }
648:
649: void
650: tty_draw_line(struct tty *tty, const struct window_pane *wp,
651: struct screen *s, u_int py, u_int ox, u_int oy)
1.1 nicm 652: {
1.193 nicm 653: struct grid_cell gc;
1.46 nicm 654: struct grid_line *gl;
1.1 nicm 655: u_int i, sx;
1.181 nicm 656: int flags;
1.1 nicm 657:
1.181 nicm 658: flags = tty->flags & TTY_NOCURSOR;
659: tty->flags |= TTY_NOCURSOR;
660: tty_update_mode(tty, tty->mode, s);
1.35 nicm 661:
1.1 nicm 662: sx = screen_size_x(s);
1.19 nicm 663: if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
664: sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1 nicm 665: if (sx > tty->sx)
666: sx = tty->sx;
667:
1.46 nicm 668: /*
1.174 nicm 669: * Don't move the cursor to the start position if it will wrap there
1.50 nicm 670: * itself.
1.46 nicm 671: */
672: gl = NULL;
673: if (py != 0)
674: gl = &s->grid->linedata[s->grid->hsize + py - 1];
1.83 nicm 675: if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
1.47 nicm 676: tty->cx < tty->sx || ox != 0 ||
677: (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
1.46 nicm 678: tty_cursor(tty, ox, oy + py);
679:
1.1 nicm 680: for (i = 0; i < sx; i++) {
1.193 nicm 681: grid_view_get_cell(s->grid, i, py, &gc);
682: tty_cell(tty, &gc, wp);
1.1 nicm 683: }
684:
1.181 nicm 685: if (sx < tty->sx) {
686: tty_attributes(tty, &grid_default_cell, wp);
687:
688: tty_cursor(tty, ox + sx, oy + py);
689: if (sx != screen_size_x(s) &&
690: ox + screen_size_x(s) >= tty->sx &&
691: tty_term_has(tty->term, TTYC_EL) &&
692: !tty_fake_bce(tty, wp))
693: tty_putcode(tty, TTYC_EL);
694: else
695: tty_repeat_space(tty, screen_size_x(s) - sx);
1.35 nicm 696: }
1.1 nicm 697:
1.181 nicm 698: tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107 nicm 699: tty_update_mode(tty, tty->mode, s);
1.15 nicm 700: }
701:
1.201 nicm 702: static int
1.182 nicm 703: tty_client_ready(struct client *c, struct window_pane *wp)
704: {
705: if (c->session == NULL || c->tty.term == NULL)
706: return (0);
707: if (c->flags & CLIENT_SUSPENDED)
708: return (0);
709: if (c->tty.flags & TTY_FREEZE)
710: return (0);
711: if (c->session->curw->window != wp->window)
712: return (0);
713: return (1);
714: }
715:
1.15 nicm 716: void
1.182 nicm 717: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
718: struct tty_ctx *ctx)
1.15 nicm 719: {
720: struct window_pane *wp = ctx->wp;
721: struct client *c;
722:
1.75 nicm 723: /* wp can be NULL if updating the screen but not the terminal. */
1.15 nicm 724: if (wp == NULL)
725: return;
726:
727: if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
728: return;
1.129 nicm 729: if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
1.15 nicm 730: return;
731:
1.178 nicm 732: TAILQ_FOREACH(c, &clients, entry) {
1.182 nicm 733: if (!tty_client_ready(c, wp))
1.15 nicm 734: continue;
735:
1.139 nicm 736: ctx->xoff = wp->xoff;
737: ctx->yoff = wp->yoff;
738: if (status_at_line(c) == 0)
739: ctx->yoff++;
1.113 nicm 740:
1.139 nicm 741: cmdfn(&c->tty, ctx);
1.1 nicm 742: }
743: }
744:
745: void
1.24 nicm 746: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 747: {
1.77 nicm 748: struct window_pane *wp = ctx->wp;
1.1 nicm 749:
1.206 ! nicm 750: if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, wp) ||
! 751: (!tty_term_has(tty->term, TTYC_DCH) &&
! 752: !tty_term_has(tty->term, TTYC_DCH1))) {
1.176 nicm 753: tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 754: return;
755: }
756:
1.176 nicm 757: tty_attributes(tty, &grid_default_cell, wp);
1.1 nicm 758:
1.77 nicm 759: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 760:
1.206 ! nicm 761: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1 nicm 762: }
763:
764: void
1.24 nicm 765: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 766: {
1.77 nicm 767: struct window_pane *wp = ctx->wp;
1.1 nicm 768:
1.176 nicm 769: if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, wp) ||
1.26 nicm 770: (!tty_term_has(tty->term, TTYC_DCH) &&
771: !tty_term_has(tty->term, TTYC_DCH1))) {
1.176 nicm 772: tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 773: return;
774: }
775:
1.176 nicm 776: tty_attributes(tty, &grid_default_cell, wp);
1.1 nicm 777:
1.77 nicm 778: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 779:
1.206 ! nicm 780: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146 nicm 781: }
782:
783: void
784: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
785: {
786: u_int i;
787:
1.176 nicm 788: tty_attributes(tty, &grid_default_cell, ctx->wp);
1.146 nicm 789:
790: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
791:
1.176 nicm 792: if (tty_term_has(tty->term, TTYC_ECH) && !tty_fake_bce(tty, ctx->wp))
1.146 nicm 793: tty_putcode1(tty, TTYC_ECH, ctx->num);
794: else {
795: for (i = 0; i < ctx->num; i++)
796: tty_putc(tty, ' ');
797: }
1.1 nicm 798: }
799:
800: void
1.24 nicm 801: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 802: {
1.176 nicm 803: if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, ctx->wp) ||
1.77 nicm 804: !tty_term_has(tty->term, TTYC_CSR) ||
1.72 nicm 805: !tty_term_has(tty->term, TTYC_IL1)) {
1.14 nicm 806: tty_redraw_region(tty, ctx);
1.1 nicm 807: return;
808: }
809:
1.176 nicm 810: tty_attributes(tty, &grid_default_cell, ctx->wp);
1.1 nicm 811:
1.77 nicm 812: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
813: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 814:
1.12 nicm 815: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1 nicm 816: }
817:
818: void
1.24 nicm 819: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 820: {
1.176 nicm 821: if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, ctx->wp) ||
1.72 nicm 822: !tty_term_has(tty->term, TTYC_CSR) ||
823: !tty_term_has(tty->term, TTYC_DL1)) {
1.14 nicm 824: tty_redraw_region(tty, ctx);
1.1 nicm 825: return;
826: }
827:
1.176 nicm 828: tty_attributes(tty, &grid_default_cell, ctx->wp);
1.1 nicm 829:
1.77 nicm 830: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
831: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 832:
1.12 nicm 833: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1 nicm 834: }
835:
836: void
1.24 nicm 837: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 838: {
1.77 nicm 839: struct window_pane *wp = ctx->wp;
1.12 nicm 840: struct screen *s = wp->screen;
1.1 nicm 841:
1.176 nicm 842: tty_attributes(tty, &grid_default_cell, wp);
1.1 nicm 843:
1.77 nicm 844: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41 nicm 845:
1.176 nicm 846: if (tty_pane_full_width(tty, ctx) && !tty_fake_bce(tty, wp) &&
847: tty_term_has(tty->term, TTYC_EL))
1.1 nicm 848: tty_putcode(tty, TTYC_EL);
1.133 nicm 849: else
850: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 851: }
852:
853: void
1.24 nicm 854: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 855: {
1.77 nicm 856: struct window_pane *wp = ctx->wp;
1.12 nicm 857: struct screen *s = wp->screen;
1.1 nicm 858:
1.176 nicm 859: tty_attributes(tty, &grid_default_cell, wp);
1.1 nicm 860:
1.41 nicm 861: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
862:
1.176 nicm 863: if (tty_pane_full_width(tty, ctx) &&
864: tty_term_has(tty->term, TTYC_EL) && !tty_fake_bce(tty, wp))
1.1 nicm 865: tty_putcode(tty, TTYC_EL);
1.133 nicm 866: else
867: tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.1 nicm 868: }
869:
870: void
1.24 nicm 871: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 872: {
1.176 nicm 873: tty_attributes(tty, &grid_default_cell, ctx->wp);
1.1 nicm 874:
1.176 nicm 875: if (ctx->xoff == 0 && tty_term_has(tty->term, TTYC_EL1) &&
876: !tty_fake_bce(tty, ctx->wp)) {
1.41 nicm 877: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 878: tty_putcode(tty, TTYC_EL1);
879: } else {
1.41 nicm 880: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133 nicm 881: tty_repeat_space(tty, ctx->ocx + 1);
1.1 nicm 882: }
883: }
884:
885: void
1.24 nicm 886: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 887: {
1.56 nicm 888: if (ctx->ocy != ctx->orupper)
889: return;
890:
1.176 nicm 891: if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, ctx->wp) ||
1.70 nicm 892: !tty_term_has(tty->term, TTYC_CSR) ||
893: !tty_term_has(tty->term, TTYC_RI)) {
1.14 nicm 894: tty_redraw_region(tty, ctx);
1.1 nicm 895: return;
896: }
897:
1.176 nicm 898: tty_attributes(tty, &grid_default_cell, ctx->wp);
1.77 nicm 899:
1.56 nicm 900: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
901: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 902:
1.56 nicm 903: tty_putcode(tty, TTYC_RI);
1.1 nicm 904: }
905:
906: void
1.24 nicm 907: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 908: {
1.77 nicm 909: struct window_pane *wp = ctx->wp;
1.1 nicm 910:
1.56 nicm 911: if (ctx->ocy != ctx->orlower)
912: return;
913:
1.176 nicm 914: if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, wp) ||
1.1 nicm 915: !tty_term_has(tty->term, TTYC_CSR)) {
1.128 nicm 916: if (tty_large_region(tty, ctx))
917: wp->flags |= PANE_REDRAW;
1.140 nicm 918: else
1.128 nicm 919: tty_redraw_region(tty, ctx);
1.1 nicm 920: return;
921: }
1.52 nicm 922:
923: /*
924: * If this line wrapped naturally (ctx->num is nonzero), don't do
925: * anything - the cursor can just be moved to the last cell and wrap
926: * naturally.
927: */
928: if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
929: return;
1.1 nicm 930:
1.176 nicm 931: tty_attributes(tty, &grid_default_cell, wp);
1.77 nicm 932:
1.56 nicm 933: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
934: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 935:
1.56 nicm 936: tty_putc(tty, '\n');
1.1 nicm 937: }
938:
939: void
1.24 nicm 940: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 941: {
1.77 nicm 942: struct window_pane *wp = ctx->wp;
1.12 nicm 943: struct screen *s = wp->screen;
1.204 nicm 944: u_int i, j;
1.1 nicm 945:
1.176 nicm 946: tty_attributes(tty, &grid_default_cell, wp);
1.1 nicm 947:
1.39 nicm 948: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 949: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39 nicm 950:
1.176 nicm 951: if (tty_pane_full_width(tty, ctx) &&
952: tty_term_has(tty->term, TTYC_EL) && !tty_fake_bce(tty, wp)) {
1.1 nicm 953: tty_putcode(tty, TTYC_EL);
1.14 nicm 954: if (ctx->ocy != screen_size_y(s) - 1) {
1.41 nicm 955: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14 nicm 956: for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1 nicm 957: tty_putcode(tty, TTYC_EL);
958: if (i == screen_size_y(s) - 1)
959: continue;
960: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
961: tty->cy++;
962: }
963: }
964: } else {
1.133 nicm 965: tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.68 nicm 966: for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
1.41 nicm 967: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 968: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 969: }
970: }
971: }
972:
973: void
1.24 nicm 974: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 975: {
1.77 nicm 976: struct window_pane *wp = ctx->wp;
1.12 nicm 977: struct screen *s = wp->screen;
1.204 nicm 978: u_int i, j;
1.1 nicm 979:
1.176 nicm 980: tty_attributes(tty, &grid_default_cell, wp);
1.1 nicm 981:
1.39 nicm 982: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 983: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 984:
1.176 nicm 985: if (tty_pane_full_width(tty, ctx) &&
986: tty_term_has(tty->term, TTYC_EL) && !tty_fake_bce(tty, wp)) {
1.14 nicm 987: for (i = 0; i < ctx->ocy; i++) {
1.1 nicm 988: tty_putcode(tty, TTYC_EL);
989: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
990: tty->cy++;
991: }
992: } else {
1.14 nicm 993: for (j = 0; j < ctx->ocy; j++) {
1.41 nicm 994: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 995: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 996: }
997: }
1.133 nicm 998: tty_repeat_space(tty, ctx->ocx + 1);
1.1 nicm 999: }
1000:
1001: void
1.24 nicm 1002: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1003: {
1.77 nicm 1004: struct window_pane *wp = ctx->wp;
1.12 nicm 1005: struct screen *s = wp->screen;
1.204 nicm 1006: u_int i, j;
1.1 nicm 1007:
1.176 nicm 1008: tty_attributes(tty, &grid_default_cell, wp);
1.1 nicm 1009:
1.39 nicm 1010: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 1011: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 1012:
1.176 nicm 1013: if (tty_pane_full_width(tty, ctx) &&
1014: tty_term_has(tty->term, TTYC_EL) && !tty_fake_bce(tty, wp)) {
1.1 nicm 1015: for (i = 0; i < screen_size_y(s); i++) {
1016: tty_putcode(tty, TTYC_EL);
1017: if (i != screen_size_y(s) - 1) {
1018: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
1019: tty->cy++;
1020: }
1021: }
1022: } else {
1023: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 1024: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 1025: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 1026: }
1.4 nicm 1027: }
1028: }
1029:
1030: void
1.24 nicm 1031: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 1032: {
1.12 nicm 1033: struct window_pane *wp = ctx->wp;
1034: struct screen *s = wp->screen;
1035: u_int i, j;
1.4 nicm 1036:
1.176 nicm 1037: tty_attributes(tty, &grid_default_cell, wp);
1.4 nicm 1038:
1.39 nicm 1039: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4 nicm 1040:
1041: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 1042: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 1043: for (i = 0; i < screen_size_x(s); i++)
1044: tty_putc(tty, 'E');
1.1 nicm 1045: }
1046: }
1047:
1048: void
1.24 nicm 1049: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1050: {
1.46 nicm 1051: struct window_pane *wp = ctx->wp;
1052: struct screen *s = wp->screen;
1.203 nicm 1053: u_int cx, width;
1.46 nicm 1054:
1.203 nicm 1055: if (ctx->ocy == ctx->orlower)
1056: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.46 nicm 1057:
1.57 nicm 1058: /* Is the cursor in the very last position? */
1.193 nicm 1059: width = ctx->cell->data.width;
1.102 nicm 1060: if (ctx->ocx > wp->sx - width) {
1.113 nicm 1061: if (ctx->xoff != 0 || wp->sx != tty->sx) {
1.57 nicm 1062: /*
1063: * The pane doesn't fill the entire line, the linefeed
1064: * will already have happened, so just move the cursor.
1065: */
1.125 nicm 1066: if (ctx->ocy != wp->yoff + wp->screen->rlower)
1.122 nicm 1067: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1068: else
1069: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.57 nicm 1070: } else if (tty->cx < tty->sx) {
1071: /*
1072: * The cursor isn't in the last position already, so
1.102 nicm 1073: * move as far left as possible and redraw the last
1.57 nicm 1074: * cell to move into the last position.
1075: */
1.193 nicm 1076: cx = screen_size_x(s) - ctx->last_cell.data.width;
1.50 nicm 1077: tty_cursor_pane(tty, ctx, cx, ctx->ocy);
1.176 nicm 1078: tty_cell(tty, &ctx->last_cell, wp);
1.50 nicm 1079: }
1080: } else
1.46 nicm 1081: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1082:
1.176 nicm 1083: tty_cell(tty, ctx->cell, wp);
1.1 nicm 1084: }
1085:
1086: void
1.24 nicm 1087: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1088: {
1.53 nicm 1089: struct window_pane *wp = ctx->wp;
1.1 nicm 1090:
1.53 nicm 1091: /*
1092: * Cannot rely on not being a partial character, so just redraw the
1093: * whole line.
1094: */
1.176 nicm 1095: tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.106 nicm 1096: }
1097:
1098: void
1099: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1100: {
1101: char *buf;
1102: size_t off;
1103:
1104: if (!tty_term_has(tty->term, TTYC_MS))
1105: return;
1106:
1107: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1108: buf = xmalloc(off);
1109:
1110: b64_ntop(ctx->ptr, ctx->num, buf, off);
1111: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1112:
1.138 nicm 1113: free(buf);
1.100 nicm 1114: }
1115:
1116: void
1117: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1118: {
1119: u_int i;
1120: u_char *str = ctx->ptr;
1121:
1122: for (i = 0; i < ctx->num; i++)
1123: tty_putc(tty, str[i]);
1.141 nicm 1124:
1125: tty->cx = tty->cy = UINT_MAX;
1126: tty->rupper = tty->rlower = UINT_MAX;
1127:
1.176 nicm 1128: tty_attributes(tty, &grid_default_cell, ctx->wp);
1.141 nicm 1129: tty_cursor(tty, 0, 0);
1.1 nicm 1130: }
1131:
1132: void
1.176 nicm 1133: tty_cell(struct tty *tty, const struct grid_cell *gc,
1134: const struct window_pane *wp)
1.1 nicm 1135: {
1.193 nicm 1136: u_int i;
1.1 nicm 1137:
1138: /* Skip last character if terminal is stupid. */
1139: if (tty->term->flags & TERM_EARLYWRAP &&
1140: tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
1141: return;
1142:
1143: /* If this is a padding character, do nothing. */
1144: if (gc->flags & GRID_FLAG_PADDING)
1145: return;
1146:
1147: /* Set the attributes. */
1.176 nicm 1148: tty_attributes(tty, gc, wp);
1.1 nicm 1149:
1.147 nicm 1150: /* Get the cell and if ASCII write with putc to do ACS translation. */
1.193 nicm 1151: if (gc->data.size == 1) {
1152: if (*gc->data.data < 0x20 || *gc->data.data == 0x7f)
1.1 nicm 1153: return;
1.193 nicm 1154: tty_putc(tty, *gc->data.data);
1.1 nicm 1155: return;
1156: }
1157:
1.147 nicm 1158: /* If not UTF-8, write _. */
1.1 nicm 1159: if (!(tty->flags & TTY_UTF8)) {
1.193 nicm 1160: for (i = 0; i < gc->data.width; i++)
1.1 nicm 1161: tty_putc(tty, '_');
1162: return;
1163: }
1164:
1.147 nicm 1165: /* Write the data. */
1.193 nicm 1166: tty_putn(tty, gc->data.data, gc->data.size, gc->data.width);
1.1 nicm 1167: }
1168:
1169: void
1170: tty_reset(struct tty *tty)
1171: {
1172: struct grid_cell *gc = &tty->cell;
1173:
1.205 nicm 1174: if (grid_cells_equal(gc, &grid_default_cell))
1.1 nicm 1175: return;
1176:
1.90 nicm 1177: if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1178: tty_putcode(tty, TTYC_RMACS);
1179: tty_putcode(tty, TTYC_SGR0);
1180: memcpy(gc, &grid_default_cell, sizeof *gc);
1181: }
1182:
1.40 nicm 1183: /* Set region inside pane. */
1.1 nicm 1184: void
1.196 nicm 1185: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
1186: u_int rlower)
1.1 nicm 1187: {
1.113 nicm 1188: tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39 nicm 1189: }
1190:
1.40 nicm 1191: /* Set region at absolute position. */
1.39 nicm 1192: void
1.40 nicm 1193: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 1194: {
1195: if (tty->rlower == rlower && tty->rupper == rupper)
1196: return;
1.1 nicm 1197: if (!tty_term_has(tty->term, TTYC_CSR))
1198: return;
1.39 nicm 1199:
1200: tty->rupper = rupper;
1201: tty->rlower = rlower;
1.55 nicm 1202:
1203: /*
1204: * Some terminals (such as PuTTY) do not correctly reset the cursor to
1205: * 0,0 if it is beyond the last column (they do not reset their wrap
1206: * flag so further output causes a line feed). As a workaround, do an
1207: * explicit move to 0 first.
1208: */
1209: if (tty->cx >= tty->sx)
1210: tty_cursor(tty, 0, tty->cy);
1.42 nicm 1211:
1.39 nicm 1212: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.78 nicm 1213: tty_cursor(tty, 0, 0);
1.1 nicm 1214: }
1215:
1.42 nicm 1216: /* Move cursor inside pane. */
1.1 nicm 1217: void
1.41 nicm 1218: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1219: {
1.113 nicm 1220: tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41 nicm 1221: }
1222:
1.42 nicm 1223: /* Move cursor to absolute position. */
1.41 nicm 1224: void
1225: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 1226: {
1.42 nicm 1227: struct tty_term *term = tty->term;
1228: u_int thisx, thisy;
1229: int change;
1.77 nicm 1230:
1.42 nicm 1231: if (cx > tty->sx - 1)
1232: cx = tty->sx - 1;
1233:
1234: thisx = tty->cx;
1235: thisy = tty->cy;
1236:
1237: /* No change. */
1238: if (cx == thisx && cy == thisy)
1239: return;
1240:
1.43 nicm 1241: /* Very end of the line, just use absolute movement. */
1242: if (thisx > tty->sx - 1)
1243: goto absolute;
1244:
1.42 nicm 1245: /* Move to home position (0, 0). */
1246: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1247: tty_putcode(tty, TTYC_HOME);
1248: goto out;
1249: }
1250:
1251: /* Zero on the next line. */
1.48 nicm 1252: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1 nicm 1253: tty_putc(tty, '\r');
1.42 nicm 1254: tty_putc(tty, '\n');
1255: goto out;
1256: }
1257:
1.45 nicm 1258: /* Moving column or row. */
1.42 nicm 1259: if (cy == thisy) {
1.45 nicm 1260: /*
1261: * Moving column only, row staying the same.
1262: */
1263:
1.42 nicm 1264: /* To left edge. */
1.202 nicm 1265: if (cx == 0) {
1.42 nicm 1266: tty_putc(tty, '\r');
1267: goto out;
1268: }
1269:
1270: /* One to the left. */
1271: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1272: tty_putcode(tty, TTYC_CUB1);
1273: goto out;
1274: }
1275:
1276: /* One to the right. */
1277: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1278: tty_putcode(tty, TTYC_CUF1);
1279: goto out;
1280: }
1281:
1282: /* Calculate difference. */
1283: change = thisx - cx; /* +ve left, -ve right */
1284:
1285: /*
1286: * Use HPA if change is larger than absolute, otherwise move
1287: * the cursor with CUB/CUF.
1288: */
1.87 nicm 1289: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 1290: tty_putcode1(tty, TTYC_HPA, cx);
1291: goto out;
1292: } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1.202 nicm 1293: if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
1294: tty_putcode(tty, TTYC_CUB1);
1295: tty_putcode(tty, TTYC_CUB1);
1296: goto out;
1297: }
1.42 nicm 1298: tty_putcode1(tty, TTYC_CUB, change);
1299: goto out;
1300: } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1301: tty_putcode1(tty, TTYC_CUF, -change);
1302: goto out;
1303: }
1.45 nicm 1304: } else if (cx == thisx) {
1305: /*
1306: * Moving row only, column staying the same.
1307: */
1.42 nicm 1308:
1309: /* One above. */
1.77 nicm 1310: if (thisy != tty->rupper &&
1.42 nicm 1311: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1312: tty_putcode(tty, TTYC_CUU1);
1313: goto out;
1314: }
1315:
1316: /* One below. */
1.49 nicm 1317: if (thisy != tty->rlower &&
1.42 nicm 1318: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1319: tty_putcode(tty, TTYC_CUD1);
1320: goto out;
1321: }
1322:
1323: /* Calculate difference. */
1324: change = thisy - cy; /* +ve up, -ve down */
1325:
1326: /*
1.77 nicm 1327: * Try to use VPA if change is larger than absolute or if this
1328: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 1329: */
1.87 nicm 1330: if ((u_int) abs(change) > cy ||
1.42 nicm 1331: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 1332: (change > 0 && cy - change < tty->rupper)) {
1333: if (tty_term_has(term, TTYC_VPA)) {
1334: tty_putcode1(tty, TTYC_VPA, cy);
1335: goto out;
1336: }
1.42 nicm 1337: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1338: tty_putcode1(tty, TTYC_CUU, change);
1339: goto out;
1340: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1341: tty_putcode1(tty, TTYC_CUD, -change);
1342: goto out;
1343: }
1344: }
1345:
1.43 nicm 1346: absolute:
1.42 nicm 1347: /* Absolute movement. */
1348: tty_putcode2(tty, TTYC_CUP, cy, cx);
1349:
1350: out:
1351: tty->cx = cx;
1352: tty->cy = cy;
1.1 nicm 1353: }
1354:
1355: void
1.176 nicm 1356: tty_attributes(struct tty *tty, const struct grid_cell *gc,
1357: const struct window_pane *wp)
1.1 nicm 1358: {
1.63 nicm 1359: struct grid_cell *tc = &tty->cell, gc2;
1.85 nicm 1360: u_char changed;
1.61 nicm 1361:
1.85 nicm 1362: memcpy(&gc2, gc, sizeof gc2);
1.203 nicm 1363: if (wp != NULL)
1364: tty_default_colours(&gc2, wp);
1.63 nicm 1365:
1.18 nicm 1366: /*
1367: * If no setab, try to use the reverse attribute as a best-effort for a
1368: * non-default background. This is a bit of a hack but it doesn't do
1369: * any serious harm and makes a couple of applications happier.
1370: */
1371: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 1372: if (gc2.attr & GRID_ATTR_REVERSE) {
1373: if (gc2.fg != 7 && gc2.fg != 8)
1.64 nicm 1374: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 1375: } else {
1.85 nicm 1376: if (gc2.bg != 0 && gc2.bg != 8)
1.64 nicm 1377: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 1378: }
1379: }
1.1 nicm 1380:
1.85 nicm 1381: /* Fix up the colours if necessary. */
1382: tty_check_fg(tty, &gc2);
1383: tty_check_bg(tty, &gc2);
1384:
1.64 nicm 1385: /* If any bits are being cleared, reset everything. */
1.85 nicm 1386: if (tc->attr & ~gc2.attr)
1.64 nicm 1387: tty_reset(tty);
1388:
1389: /*
1390: * Set the colours. This may call tty_reset() (so it comes next) and
1391: * may add to (NOT remove) the desired attributes by changing new_attr.
1392: */
1.85 nicm 1393: tty_colours(tty, &gc2);
1.64 nicm 1394:
1.1 nicm 1395: /* Filter out attribute bits already set. */
1.85 nicm 1396: changed = gc2.attr & ~tc->attr;
1397: tc->attr = gc2.attr;
1.1 nicm 1398:
1399: /* Set the attributes. */
1400: if (changed & GRID_ATTR_BRIGHT)
1401: tty_putcode(tty, TTYC_BOLD);
1402: if (changed & GRID_ATTR_DIM)
1403: tty_putcode(tty, TTYC_DIM);
1.180 nicm 1404: if (changed & GRID_ATTR_ITALICS)
1405: tty_set_italics(tty);
1.1 nicm 1406: if (changed & GRID_ATTR_UNDERSCORE)
1407: tty_putcode(tty, TTYC_SMUL);
1408: if (changed & GRID_ATTR_BLINK)
1409: tty_putcode(tty, TTYC_BLINK);
1410: if (changed & GRID_ATTR_REVERSE) {
1411: if (tty_term_has(tty->term, TTYC_REV))
1412: tty_putcode(tty, TTYC_REV);
1413: else if (tty_term_has(tty->term, TTYC_SMSO))
1414: tty_putcode(tty, TTYC_SMSO);
1415: }
1416: if (changed & GRID_ATTR_HIDDEN)
1417: tty_putcode(tty, TTYC_INVIS);
1.90 nicm 1418: if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1419: tty_putcode(tty, TTYC_SMACS);
1420: }
1421:
1.61 nicm 1422: void
1.85 nicm 1423: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1424: {
1.61 nicm 1425: struct grid_cell *tc = &tty->cell;
1.204 nicm 1426: int have_ax;
1.61 nicm 1427:
1428: /* No changes? Nothing is necessary. */
1.204 nicm 1429: if (gc->fg == tc->fg && gc->bg == tc->bg)
1.63 nicm 1430: return;
1.1 nicm 1431:
1.61 nicm 1432: /*
1433: * Is either the default colour? This is handled specially because the
1434: * best solution might be to reset both colours to default, in which
1435: * case if only one is default need to fall onward to set the other
1436: * colour.
1437: */
1.204 nicm 1438: if (gc->fg == 8 || gc->bg == 8) {
1.61 nicm 1439: /*
1440: * If don't have AX but do have op, send sgr0 (op can't
1441: * actually be used because it is sometimes the same as sgr0
1442: * and sometimes isn't). This resets both colours to default.
1443: *
1444: * Otherwise, try to set the default colour only as needed.
1445: */
1.174 nicm 1446: have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61 nicm 1447: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1448: tty_reset(tty);
1449: else {
1.204 nicm 1450: if (gc->fg == 8 && tc->fg != 8) {
1.61 nicm 1451: if (have_ax)
1452: tty_puts(tty, "\033[39m");
1.204 nicm 1453: else if (tc->fg != 7)
1.61 nicm 1454: tty_putcode1(tty, TTYC_SETAF, 7);
1455: tc->fg = 8;
1456: }
1.204 nicm 1457: if (gc->bg == 8 && tc->bg != 8) {
1.77 nicm 1458: if (have_ax)
1.61 nicm 1459: tty_puts(tty, "\033[49m");
1.204 nicm 1460: else if (tc->bg != 0)
1.61 nicm 1461: tty_putcode1(tty, TTYC_SETAB, 0);
1462: tc->bg = 8;
1463: }
1464: }
1465: }
1.1 nicm 1466:
1.61 nicm 1467: /* Set the foreground colour. */
1.204 nicm 1468: if (gc->fg != 8 && gc->fg != tc->fg)
1.85 nicm 1469: tty_colours_fg(tty, gc);
1.1 nicm 1470:
1.61 nicm 1471: /*
1472: * Set the background colour. This must come after the foreground as
1473: * tty_colour_fg() can call tty_reset().
1474: */
1.204 nicm 1475: if (gc->bg != 8 && gc->bg != tc->bg)
1.73 nicm 1476: tty_colours_bg(tty, gc);
1.1 nicm 1477: }
1478:
1479: void
1.85 nicm 1480: tty_check_fg(struct tty *tty, struct grid_cell *gc)
1481: {
1.204 nicm 1482: u_char r, g, b;
1483: u_int colours;
1.85 nicm 1484:
1.199 nicm 1485: /* Is this a 24-bit colour? */
1.204 nicm 1486: if (gc->fg & COLOUR_FLAG_RGB) {
1.199 nicm 1487: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1488: if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204 nicm 1489: colour_split_rgb(gc->fg, &r, &g, &b);
1490: gc->fg = colour_find_rgb(r, g, b);
1491: } else
1.200 nicm 1492: return;
1.199 nicm 1493: }
1.175 nicm 1494: colours = tty_term_number(tty->term, TTYC_COLORS);
1495:
1.85 nicm 1496: /* Is this a 256-colour colour? */
1.204 nicm 1497: if (gc->fg & COLOUR_FLAG_256) {
1.85 nicm 1498: /* And not a 256 colour mode? */
1.158 nicm 1499: if (!(tty->term->flags & TERM_256COLOURS) &&
1.85 nicm 1500: !(tty->term_flags & TERM_256COLOURS)) {
1501: gc->fg = colour_256to16(gc->fg);
1502: if (gc->fg & 8) {
1503: gc->fg &= 7;
1.175 nicm 1504: if (colours >= 16)
1505: gc->fg += 90;
1506: else
1507: gc->attr |= GRID_ATTR_BRIGHT;
1.85 nicm 1508: } else
1509: gc->attr &= ~GRID_ATTR_BRIGHT;
1510: }
1511: return;
1512: }
1513:
1514: /* Is this an aixterm colour? */
1515: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1516: gc->fg -= 90;
1517: gc->attr |= GRID_ATTR_BRIGHT;
1518: }
1519: }
1520:
1521: void
1522: tty_check_bg(struct tty *tty, struct grid_cell *gc)
1523: {
1.204 nicm 1524: u_char r, g, b;
1525: u_int colours;
1.85 nicm 1526:
1.199 nicm 1527: /* Is this a 24-bit colour? */
1.204 nicm 1528: if (gc->bg & COLOUR_FLAG_RGB) {
1.199 nicm 1529: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1530: if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204 nicm 1531: colour_split_rgb(gc->bg, &r, &g, &b);
1532: gc->bg = colour_find_rgb(r, g, b);
1533: } else
1.200 nicm 1534: return;
1.199 nicm 1535: }
1.175 nicm 1536: colours = tty_term_number(tty->term, TTYC_COLORS);
1537:
1.85 nicm 1538: /* Is this a 256-colour colour? */
1.204 nicm 1539: if (gc->bg & COLOUR_FLAG_256) {
1.85 nicm 1540: /*
1541: * And not a 256 colour mode? Translate to 16-colour
1542: * palette. Bold background doesn't exist portably, so just
1543: * discard the bold bit if set.
1544: */
1.158 nicm 1545: if (!(tty->term->flags & TERM_256COLOURS) &&
1.85 nicm 1546: !(tty->term_flags & TERM_256COLOURS)) {
1547: gc->bg = colour_256to16(gc->bg);
1.175 nicm 1548: if (gc->bg & 8) {
1.85 nicm 1549: gc->bg &= 7;
1.175 nicm 1550: if (colours >= 16)
1551: gc->fg += 90;
1552: }
1.85 nicm 1553: }
1554: return;
1555: }
1556:
1557: /* Is this an aixterm colour? */
1.175 nicm 1558: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85 nicm 1559: gc->bg -= 90;
1560: }
1561:
1562: void
1563: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1564: {
1.61 nicm 1565: struct grid_cell *tc = &tty->cell;
1.79 nicm 1566: char s[32];
1.1 nicm 1567:
1.204 nicm 1568: /* Is this a 24-bit or 256-colour colour? */
1569: if (gc->fg & COLOUR_FLAG_RGB ||
1570: gc->fg & COLOUR_FLAG_256) {
1571: if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61 nicm 1572: goto save_fg;
1.199 nicm 1573: /* Should not get here, already converted in tty_check_fg. */
1.85 nicm 1574: return;
1.1 nicm 1575: }
1576:
1.79 nicm 1577: /* Is this an aixterm bright colour? */
1.204 nicm 1578: if (gc->fg >= 90 && gc->fg <= 97) {
1579: xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
1.85 nicm 1580: tty_puts(tty, s);
1581: goto save_fg;
1.79 nicm 1582: }
1583:
1.61 nicm 1584: /* Otherwise set the foreground colour. */
1.204 nicm 1585: tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61 nicm 1586:
1.77 nicm 1587: save_fg:
1.61 nicm 1588: /* Save the new values in the terminal current cell. */
1.204 nicm 1589: tc->fg = gc->fg;
1.1 nicm 1590: }
1591:
1592: void
1.73 nicm 1593: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1594: {
1.61 nicm 1595: struct grid_cell *tc = &tty->cell;
1.79 nicm 1596: char s[32];
1.1 nicm 1597:
1.204 nicm 1598: /* Is this a 24-bit or 256-colour colour? */
1599: if (gc->bg & COLOUR_FLAG_RGB ||
1600: gc->bg & COLOUR_FLAG_256) {
1601: if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61 nicm 1602: goto save_bg;
1.199 nicm 1603: /* Should not get here, already converted in tty_check_bg. */
1.85 nicm 1604: return;
1.79 nicm 1605: }
1606:
1607: /* Is this an aixterm bright colour? */
1.204 nicm 1608: if (gc->bg >= 90 && gc->bg <= 97) {
1609: xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
1.175 nicm 1610: tty_puts(tty, s);
1611: goto save_bg;
1.1 nicm 1612: }
1613:
1.61 nicm 1614: /* Otherwise set the background colour. */
1.204 nicm 1615: tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61 nicm 1616:
1617: save_bg:
1618: /* Save the new values in the terminal current cell. */
1.204 nicm 1619: tc->bg = gc->bg;
1.61 nicm 1620: }
1621:
1622: int
1.204 nicm 1623: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61 nicm 1624: {
1.204 nicm 1625: u_char r, g, b;
1.61 nicm 1626: char s[32];
1627:
1.204 nicm 1628: if (colour & COLOUR_FLAG_256) {
1629: /*
1630: * If the user has specified -2 to the client, setaf and setab
1631: * may not work (or they may not want to use them), so send the
1632: * usual sequence.
1633: */
1634: if (tty->term_flags & TERM_256COLOURS)
1635: goto fallback_256;
1.189 nicm 1636:
1.204 nicm 1637: /*
1638: * If the terminfo entry has 256 colours and setaf and setab
1639: * exist, assume that they work correctly.
1640: */
1641: if (tty->term->flags & TERM_256COLOURS) {
1642: if (*type == '3') {
1643: if (!tty_term_has(tty->term, TTYC_SETAF))
1644: goto fallback_256;
1645: tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
1646: } else {
1647: if (!tty_term_has(tty->term, TTYC_SETAB))
1648: goto fallback_256;
1649: tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
1650: }
1651: return (0);
1.188 nicm 1652: }
1.204 nicm 1653: goto fallback_256;
1654: }
1655:
1656: if (colour & COLOUR_FLAG_RGB) {
1657: if (!tty_term_flag(tty->term, TTYC_TC))
1658: return (-1);
1659:
1660: colour_split_rgb(colour & 0xffffff, &r, &g, &b);
1661: xsnprintf(s, sizeof s, "\033[%s;2;%hhu;%hhu;%hhum", type,
1662: r, g, b);
1663: tty_puts(tty, s);
1.165 nicm 1664: return (0);
1665: }
1.61 nicm 1666:
1.165 nicm 1667: return (-1);
1.188 nicm 1668:
1.204 nicm 1669: fallback_256:
1670: xsnprintf(s, sizeof s, "\033[%s;5;%dm", type, colour & 0xff);
1.188 nicm 1671: tty_puts(tty, s);
1672: return (0);
1.176 nicm 1673: }
1674:
1675: void
1676: tty_default_colours(struct grid_cell *gc, const struct window_pane *wp)
1677: {
1.203 nicm 1678: struct window *w = wp->window;
1679: struct options *oo = w->options;
1680: const struct grid_cell *agc, *pgc, *wgc;
1681:
1682: if (w->flags & WINDOW_STYLECHANGED) {
1683: w->flags &= ~WINDOW_STYLECHANGED;
1684: agc = options_get_style(oo, "window-active-style");
1685: memcpy(&w->active_style, agc, sizeof w->active_style);
1686: wgc = options_get_style(oo, "window-style");
1687: memcpy(&w->style, wgc, sizeof w->style);
1688: } else {
1689: agc = &w->active_style;
1690: wgc = &w->style;
1691: }
1.176 nicm 1692: pgc = &wp->colgc;
1693:
1.204 nicm 1694: if (gc->fg == 8) {
1695: if (pgc->fg != 8)
1.176 nicm 1696: gc->fg = pgc->fg;
1.204 nicm 1697: else if (wp == w->active && agc->fg != 8)
1.176 nicm 1698: gc->fg = agc->fg;
1.204 nicm 1699: else
1.176 nicm 1700: gc->fg = wgc->fg;
1701: }
1702:
1.204 nicm 1703: if (gc->bg == 8) {
1704: if (pgc->bg != 8)
1.176 nicm 1705: gc->bg = pgc->bg;
1.204 nicm 1706: else if (wp == w->active && agc->bg != 8)
1.176 nicm 1707: gc->bg = agc->bg;
1.204 nicm 1708: else
1.176 nicm 1709: gc->bg = wgc->bg;
1710: }
1.1 nicm 1711: }