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