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