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