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