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