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