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