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