Annotation of src/usr.bin/tmux/tty.c, Revision 1.328
1.328 ! nicm 1: /* $OpenBSD: tty.c,v 1.327 2019/06/26 13:05:06 nicm Exp $ */
1.1 nicm 2:
3: /*
1.198 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20: #include <sys/ioctl.h>
21:
1.106 nicm 22: #include <netinet/in.h>
23:
1.212 nicm 24: #include <curses.h>
1.1 nicm 25: #include <errno.h>
26: #include <fcntl.h>
1.106 nicm 27: #include <resolv.h>
1.42 nicm 28: #include <stdlib.h>
1.1 nicm 29: #include <string.h>
30: #include <termios.h>
31: #include <unistd.h>
32:
33: #include "tmux.h"
34:
1.207 nicm 35: static int tty_log_fd = -1;
1.192 nicm 36:
1.207 nicm 37: static int tty_client_ready(struct client *, struct window_pane *);
1.201 nicm 38:
1.207 nicm 39: static void tty_set_italics(struct tty *);
40: static int tty_try_colour(struct tty *, int, const char *);
1.208 nicm 41: static void tty_force_cursor_colour(struct tty *, const char *);
42: static void tty_cursor_pane(struct tty *, const struct tty_ctx *, u_int,
43: u_int);
1.253 nicm 44: static void tty_cursor_pane_unless_wrap(struct tty *,
45: const struct tty_ctx *, u_int, u_int);
1.242 nicm 46: static void tty_invalidate(struct tty *);
1.207 nicm 47: static void tty_colours(struct tty *, const struct grid_cell *);
1.317 nicm 48: static void tty_check_fg(struct tty *, struct window_pane *,
1.219 nicm 49: struct grid_cell *);
1.317 nicm 50: static void tty_check_bg(struct tty *, struct window_pane *,
1.219 nicm 51: struct grid_cell *);
1.328 ! nicm 52: static void tty_check_us(struct tty *, struct window_pane *,
! 53: struct grid_cell *);
1.207 nicm 54: static void tty_colours_fg(struct tty *, const struct grid_cell *);
55: static void tty_colours_bg(struct tty *, const struct grid_cell *);
1.328 ! nicm 56: static void tty_colours_us(struct tty *, const struct grid_cell *);
1.207 nicm 57:
1.208 nicm 58: static void tty_region_pane(struct tty *, const struct tty_ctx *, u_int,
59: u_int);
1.214 nicm 60: static void tty_region(struct tty *, u_int, u_int);
1.212 nicm 61: static void tty_margin_pane(struct tty *, const struct tty_ctx *);
1.214 nicm 62: static void tty_margin(struct tty *, u_int, u_int);
1.207 nicm 63: static int tty_large_region(struct tty *, const struct tty_ctx *);
1.317 nicm 64: static int tty_fake_bce(const struct tty *, struct window_pane *, u_int);
1.207 nicm 65: static void tty_redraw_region(struct tty *, const struct tty_ctx *);
66: static void tty_emulate_repeat(struct tty *, enum tty_code_code,
67: enum tty_code_code, u_int);
68: static void tty_repeat_space(struct tty *, u_int);
1.309 nicm 69: static void tty_draw_pane(struct tty *, const struct tty_ctx *, u_int);
1.207 nicm 70: static void tty_cell(struct tty *, const struct grid_cell *,
1.317 nicm 71: struct window_pane *);
72: static void tty_default_colours(struct grid_cell *, struct window_pane *);
73: static void tty_default_attributes(struct tty *, struct window_pane *,
1.210 nicm 74: u_int);
1.1 nicm 75:
1.212 nicm 76: #define tty_use_margin(tty) \
1.265 nicm 77: ((tty)->term_type == TTY_VT420)
1.90 nicm 78:
1.264 nicm 79: #define tty_pane_full_width(tty, ctx) \
1.132 nicm 80: ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
81:
1.266 nicm 82: #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
83: #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
84: #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
85:
1.192 nicm 86: void
87: tty_create_log(void)
88: {
89: char name[64];
90:
91: xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
92:
93: tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
94: if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
95: fatal("fcntl failed");
96: }
97:
1.185 nicm 98: int
1.136 nicm 99: tty_init(struct tty *tty, struct client *c, int fd, char *term)
1.1 nicm 100: {
1.185 nicm 101: if (!isatty(fd))
102: return (-1);
103:
1.30 nicm 104: memset(tty, 0, sizeof *tty);
1.22 nicm 105:
1.9 nicm 106: if (term == NULL || *term == '\0')
1.220 nicm 107: tty->term_name = xstrdup("unknown");
1.1 nicm 108: else
1.220 nicm 109: tty->term_name = xstrdup(term);
1.258 nicm 110:
1.30 nicm 111: tty->fd = fd;
1.136 nicm 112: tty->client = c;
1.30 nicm 113:
1.108 nicm 114: tty->cstyle = 0;
1.107 nicm 115: tty->ccolour = xstrdup("");
1.30 nicm 116:
1.1 nicm 117: tty->flags = 0;
1.212 nicm 118:
1.1 nicm 119: tty->term_flags = 0;
1.212 nicm 120: tty->term_type = TTY_UNKNOWN;
1.185 nicm 121:
122: return (0);
1.31 nicm 123: }
124:
1.287 nicm 125: void
1.31 nicm 126: tty_resize(struct tty *tty)
127: {
1.258 nicm 128: struct client *c = tty->client;
129: struct winsize ws;
130: u_int sx, sy;
1.31 nicm 131:
132: if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88 nicm 133: sx = ws.ws_col;
134: if (sx == 0)
135: sx = 80;
136: sy = ws.ws_row;
137: if (sy == 0)
138: sy = 24;
139: } else {
140: sx = 80;
141: sy = 24;
1.31 nicm 142: }
1.258 nicm 143: log_debug("%s: %s now %ux%u", __func__, c->name, sx, sy);
1.287 nicm 144: tty_set_size(tty, sx, sy);
1.242 nicm 145: tty_invalidate(tty);
1.114 nicm 146: }
147:
1.287 nicm 148: void
1.212 nicm 149: tty_set_size(struct tty *tty, u_int sx, u_int sy)
150: {
1.114 nicm 151: tty->sx = sx;
152: tty->sy = sy;
1.1 nicm 153: }
154:
1.244 nicm 155: static void
156: tty_read_callback(__unused int fd, __unused short events, void *data)
157: {
158: struct tty *tty = data;
1.258 nicm 159: struct client *c = tty->client;
1.244 nicm 160: size_t size = EVBUFFER_LENGTH(tty->in);
161: int nread;
162:
163: nread = evbuffer_read(tty->in, tty->fd, -1);
1.295 nicm 164: if (nread == 0 || nread == -1) {
1.290 nicm 165: event_del(&tty->event_in);
1.295 nicm 166: server_client_lost(tty->client);
1.244 nicm 167: return;
1.290 nicm 168: }
1.258 nicm 169: log_debug("%s: read %d bytes (already %zu)", c->name, nread, size);
1.244 nicm 170:
171: while (tty_keys_next(tty))
172: ;
173: }
174:
175: static void
1.266 nicm 176: tty_timer_callback(__unused int fd, __unused short events, void *data)
177: {
178: struct tty *tty = data;
179: struct client *c = tty->client;
180: struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
181:
182: log_debug("%s: %zu discarded", c->name, tty->discarded);
183:
1.305 nicm 184: c->flags |= CLIENT_ALLREDRAWFLAGS;
1.266 nicm 185: c->discarded += tty->discarded;
186:
187: if (tty->discarded < TTY_BLOCK_STOP(tty)) {
188: tty->flags &= ~TTY_BLOCK;
189: tty_invalidate(tty);
190: return;
191: }
192: tty->discarded = 0;
193: evtimer_add(&tty->timer, &tv);
194: }
195:
196: static int
197: tty_block_maybe(struct tty *tty)
198: {
199: struct client *c = tty->client;
200: size_t size = EVBUFFER_LENGTH(tty->out);
201: struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
202:
203: if (size < TTY_BLOCK_START(tty))
204: return (0);
205:
206: if (tty->flags & TTY_BLOCK)
207: return (1);
208: tty->flags |= TTY_BLOCK;
209:
210: log_debug("%s: can't keep up, %zu discarded", c->name, size);
211:
212: evbuffer_drain(tty->out, size);
213: c->discarded += size;
214:
215: tty->discarded = 0;
216: evtimer_add(&tty->timer, &tv);
217: return (1);
218: }
219:
220: static void
1.244 nicm 221: tty_write_callback(__unused int fd, __unused short events, void *data)
222: {
1.246 nicm 223: struct tty *tty = data;
1.258 nicm 224: struct client *c = tty->client;
1.246 nicm 225: size_t size = EVBUFFER_LENGTH(tty->out);
226: int nwrite;
1.244 nicm 227:
228: nwrite = evbuffer_write(tty->out, tty->fd);
229: if (nwrite == -1)
230: return;
1.258 nicm 231: log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size);
1.244 nicm 232:
1.270 nicm 233: if (c->redraw > 0) {
234: if ((size_t)nwrite >= c->redraw)
235: c->redraw = 0;
236: else
237: c->redraw -= nwrite;
238: log_debug("%s: waiting for redraw, %zu bytes left", c->name,
239: c->redraw);
240: } else if (tty_block_maybe(tty))
1.266 nicm 241: return;
242:
1.247 nicm 243: if (EVBUFFER_LENGTH(tty->out) != 0)
244: event_add(&tty->event_out, NULL);
1.244 nicm 245: }
246:
1.1 nicm 247: int
1.166 nicm 248: tty_open(struct tty *tty, char **cause)
1.1 nicm 249: {
1.220 nicm 250: tty->term = tty_term_find(tty->term_name, tty->fd, cause);
1.21 nicm 251: if (tty->term == NULL) {
252: tty_close(tty);
253: return (-1);
254: }
255: tty->flags |= TTY_OPENED;
1.1 nicm 256:
1.266 nicm 257: tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
1.244 nicm 258:
259: event_set(&tty->event_in, tty->fd, EV_PERSIST|EV_READ,
260: tty_read_callback, tty);
261: tty->in = evbuffer_new();
1.311 nicm 262: if (tty->in == NULL)
263: fatal("out of memory");
1.1 nicm 264:
1.244 nicm 265: event_set(&tty->event_out, tty->fd, EV_WRITE, tty_write_callback, tty);
266: tty->out = evbuffer_new();
1.311 nicm 267: if (tty->out == NULL)
268: fatal("out of memory");
1.1 nicm 269:
1.266 nicm 270: evtimer_set(&tty->timer, tty_timer_callback, tty);
271:
1.1 nicm 272: tty_start_tty(tty);
273:
1.148 nicm 274: tty_keys_build(tty);
1.1 nicm 275:
276: return (0);
277: }
278:
1.244 nicm 279: void
280: tty_start_tty(struct tty *tty)
1.1 nicm 281: {
1.285 nicm 282: struct client *c = tty->client;
283: struct termios tio;
1.33 nicm 284:
1.244 nicm 285: if (tty->fd != -1 && tcgetattr(tty->fd, &tty->tio) == 0) {
286: setblocking(tty->fd, 0);
287: event_add(&tty->event_in, NULL);
288:
289: memcpy(&tio, &tty->tio, sizeof tio);
290: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
291: tio.c_iflag |= IGNBRK;
292: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
293: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
294: ECHOPRT|ECHOKE|ISIG);
295: tio.c_cc[VMIN] = 1;
296: tio.c_cc[VTIME] = 0;
297: if (tcsetattr(tty->fd, TCSANOW, &tio) == 0)
298: tcflush(tty->fd, TCIOFLUSH);
299: }
1.1 nicm 300:
1.10 nicm 301: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 302:
1.252 nicm 303: tty_putcode(tty, TTYC_SMKX);
1.285 nicm 304: tty_putcode(tty, TTYC_CLEAR);
305:
306: if (tty_acs_needed(tty)) {
307: log_debug("%s: using capabilities for ACS", c->name);
1.90 nicm 308: tty_putcode(tty, TTYC_ENACS);
1.285 nicm 309: } else
310: log_debug("%s: using UTF-8 for ACS", c->name);
1.1 nicm 311:
312: tty_putcode(tty, TTYC_CNORM);
313: if (tty_term_has(tty->term, TTYC_KMOUS))
1.179 nicm 314: tty_puts(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l");
1.121 nicm 315:
1.189 nicm 316: if (tty_term_flag(tty->term, TTYC_XT)) {
1.191 nicm 317: if (options_get_number(global_options, "focus-events")) {
1.162 nicm 318: tty->flags |= TTY_FOCUS;
319: tty_puts(tty, "\033[?1004h");
320: }
1.265 nicm 321: tty_puts(tty, "\033[c");
1.162 nicm 322: }
1.1 nicm 323:
1.20 nicm 324: tty->flags |= TTY_STARTED;
1.242 nicm 325: tty_invalidate(tty);
1.107 nicm 326:
327: tty_force_cursor_colour(tty, "");
1.177 nicm 328:
329: tty->mouse_drag_flag = 0;
330: tty->mouse_drag_update = NULL;
331: tty->mouse_drag_release = NULL;
1.128 nicm 332: }
333:
334: void
1.1 nicm 335: tty_stop_tty(struct tty *tty)
336: {
337: struct winsize ws;
338:
1.20 nicm 339: if (!(tty->flags & TTY_STARTED))
340: return;
341: tty->flags &= ~TTY_STARTED;
342:
1.266 nicm 343: event_del(&tty->timer);
344: tty->flags &= ~TTY_BLOCK;
345:
1.244 nicm 346: event_del(&tty->event_in);
347: event_del(&tty->event_out);
1.66 nicm 348:
1.1 nicm 349: /*
350: * Be flexible about error handling and try not kill the server just
351: * because the fd is invalid. Things like ssh -t can easily leave us
352: * with a dead tty.
353: */
354: if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
355: return;
356: if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
357: return;
358:
359: tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.285 nicm 360: if (tty_acs_needed(tty))
1.90 nicm 361: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1 nicm 362: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
363: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
364: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.160 nicm 365: if (tty_term_has(tty->term, TTYC_SS) && tty->cstyle != 0) {
366: if (tty_term_has(tty->term, TTYC_SE))
367: tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
1.109 nicm 368: else
1.160 nicm 369: tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
1.108 nicm 370: }
1.173 nicm 371: if (tty->mode & MODE_BRACKETPASTE)
372: tty_raw(tty, "\033[?2004l");
1.107 nicm 373: tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1 nicm 374:
375: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
376: if (tty_term_has(tty->term, TTYC_KMOUS))
1.179 nicm 377: tty_raw(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l");
1.151 nicm 378:
1.189 nicm 379: if (tty_term_flag(tty->term, TTYC_XT)) {
1.162 nicm 380: if (tty->flags & TTY_FOCUS) {
381: tty->flags &= ~TTY_FOCUS;
1.172 nicm 382: tty_raw(tty, "\033[?1004l");
1.162 nicm 383: }
384: }
1.10 nicm 385:
1.212 nicm 386: if (tty_use_margin(tty))
387: tty_raw(tty, "\033[?69l"); /* DECLRMM */
1.10 nicm 388: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150 nicm 389:
390: setblocking(tty->fd, 1);
1.1 nicm 391: }
392:
393: void
1.20 nicm 394: tty_close(struct tty *tty)
1.1 nicm 395: {
1.123 nicm 396: if (event_initialized(&tty->key_timer))
397: evtimer_del(&tty->key_timer);
1.20 nicm 398: tty_stop_tty(tty);
1.1 nicm 399:
1.21 nicm 400: if (tty->flags & TTY_OPENED) {
1.244 nicm 401: evbuffer_free(tty->in);
402: event_del(&tty->event_in);
403: evbuffer_free(tty->out);
404: event_del(&tty->event_out);
1.66 nicm 405:
1.21 nicm 406: tty_term_free(tty->term);
407: tty_keys_free(tty);
408:
409: tty->flags &= ~TTY_OPENED;
410: }
1.1 nicm 411:
1.21 nicm 412: if (tty->fd != -1) {
413: close(tty->fd);
414: tty->fd = -1;
415: }
1.1 nicm 416: }
417:
418: void
1.20 nicm 419: tty_free(struct tty *tty)
1.1 nicm 420: {
1.20 nicm 421: tty_close(tty);
1.1 nicm 422:
1.138 nicm 423: free(tty->ccolour);
1.220 nicm 424: free(tty->term_name);
1.1 nicm 425: }
426:
427: void
1.212 nicm 428: tty_set_type(struct tty *tty, int type)
429: {
430: tty->term_type = type;
431:
432: if (tty_use_margin(tty))
433: tty_puts(tty, "\033[?69h"); /* DECLRMM */
434: }
435:
436: void
1.1 nicm 437: tty_raw(struct tty *tty, const char *s)
438: {
1.150 nicm 439: ssize_t n, slen;
440: u_int i;
441:
442: slen = strlen(s);
443: for (i = 0; i < 5; i++) {
444: n = write(tty->fd, s, slen);
445: if (n >= 0) {
446: s += n;
447: slen -= n;
448: if (slen == 0)
449: break;
450: } else if (n == -1 && errno != EAGAIN)
451: break;
452: usleep(100);
453: }
1.1 nicm 454: }
455:
456: void
457: tty_putcode(struct tty *tty, enum tty_code_code code)
458: {
459: tty_puts(tty, tty_term_string(tty->term, code));
460: }
461:
462: void
463: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
464: {
465: if (a < 0)
466: return;
467: tty_puts(tty, tty_term_string1(tty->term, code, a));
468: }
469:
470: void
471: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
472: {
473: if (a < 0 || b < 0)
474: return;
475: tty_puts(tty, tty_term_string2(tty->term, code, a, b));
476: }
477:
478: void
1.286 nicm 479: tty_putcode3(struct tty *tty, enum tty_code_code code, int a, int b, int c)
480: {
481: if (a < 0 || b < 0 || c < 0)
482: return;
483: tty_puts(tty, tty_term_string3(tty->term, code, a, b, c));
484: }
485:
486: void
1.107 nicm 487: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
488: {
489: if (a != NULL)
490: tty_puts(tty, tty_term_ptr1(tty->term, code, a));
491: }
492:
493: void
1.167 nicm 494: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
495: const void *b)
1.106 nicm 496: {
497: if (a != NULL && b != NULL)
498: tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
499: }
500:
1.243 nicm 501: static void
502: tty_add(struct tty *tty, const char *buf, size_t len)
503: {
1.258 nicm 504: struct client *c = tty->client;
505:
1.266 nicm 506: if (tty->flags & TTY_BLOCK) {
507: tty->discarded += len;
508: return;
509: }
510:
1.244 nicm 511: evbuffer_add(tty->out, buf, len);
1.270 nicm 512: log_debug("%s: %.*s", c->name, (int)len, buf);
1.266 nicm 513: c->written += len;
1.243 nicm 514:
515: if (tty_log_fd != -1)
516: write(tty_log_fd, buf, len);
1.244 nicm 517: if (tty->flags & TTY_STARTED)
518: event_add(&tty->event_out, NULL);
1.243 nicm 519: }
520:
1.106 nicm 521: void
1.1 nicm 522: tty_puts(struct tty *tty, const char *s)
523: {
1.243 nicm 524: if (*s != '\0')
525: tty_add(tty, s, strlen(s));
1.1 nicm 526: }
527:
528: void
529: tty_putc(struct tty *tty, u_char ch)
530: {
1.90 nicm 531: const char *acs;
1.1 nicm 532:
1.325 nicm 533: if ((tty->term->flags & TERM_EARLYWRAP) &&
534: ch >= 0x20 && ch != 0x7f &&
535: tty->cy == tty->sy - 1 &&
536: tty->cx + 1 >= tty->sx)
537: return;
538:
1.90 nicm 539: if (tty->cell.attr & GRID_ATTR_CHARSET) {
540: acs = tty_acs_get(tty, ch);
1.243 nicm 541: if (acs != NULL)
542: tty_add(tty, acs, strlen(acs));
543: else
544: tty_add(tty, &ch, 1);
545: } else
546: tty_add(tty, &ch, 1);
1.1 nicm 547:
548: if (ch >= 0x20 && ch != 0x7f) {
1.211 nicm 549: if (tty->cx >= tty->sx) {
1.46 nicm 550: tty->cx = 1;
551: if (tty->cy != tty->rlower)
552: tty->cy++;
1.211 nicm 553:
554: /*
555: * On !xenl terminals, force the cursor position to
556: * where we think it should be after a line wrap - this
557: * means it works on sensible terminals as well.
558: */
559: if (tty->term->flags & TERM_EARLYWRAP)
560: tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
1.1 nicm 561: } else
562: tty->cx++;
563: }
564: }
565:
566: void
1.147 nicm 567: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5 nicm 568: {
1.325 nicm 569: if ((tty->term->flags & TERM_EARLYWRAP) &&
570: tty->cy == tty->sy - 1 &&
571: tty->cx + len >= tty->sx)
572: len = tty->sx - tty->cx - 1;
573:
1.243 nicm 574: tty_add(tty, buf, len);
1.268 nicm 575: if (tty->cx + width > tty->sx) {
576: tty->cx = (tty->cx + width) - tty->sx;
577: if (tty->cx <= tty->sx)
578: tty->cy++;
579: else
580: tty->cx = tty->cy = UINT_MAX;
581: } else
1.254 nicm 582: tty->cx += width;
1.5 nicm 583: }
584:
1.207 nicm 585: static void
1.180 nicm 586: tty_set_italics(struct tty *tty)
587: {
588: const char *s;
589:
590: if (tty_term_has(tty->term, TTYC_SITM)) {
1.191 nicm 591: s = options_get_string(global_options, "default-terminal");
1.180 nicm 592: if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
593: tty_putcode(tty, TTYC_SITM);
594: return;
595: }
596: }
597: tty_putcode(tty, TTYC_SMSO);
598: }
599:
600: void
1.1 nicm 601: tty_set_title(struct tty *tty, const char *title)
602: {
1.105 nicm 603: if (!tty_term_has(tty->term, TTYC_TSL) ||
604: !tty_term_has(tty->term, TTYC_FSL))
1.1 nicm 605: return;
606:
1.105 nicm 607: tty_putcode(tty, TTYC_TSL);
1.1 nicm 608: tty_puts(tty, title);
1.105 nicm 609: tty_putcode(tty, TTYC_FSL);
1.1 nicm 610: }
611:
1.208 nicm 612: static void
1.107 nicm 613: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
614: {
615: if (*ccolour == '\0')
616: tty_putcode(tty, TTYC_CR);
617: else
1.160 nicm 618: tty_putcode_ptr1(tty, TTYC_CS, ccolour);
1.138 nicm 619: free(tty->ccolour);
1.107 nicm 620: tty->ccolour = xstrdup(ccolour);
621: }
622:
623: void
624: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1 nicm 625: {
626: int changed;
627:
1.197 nicm 628: if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0)
1.107 nicm 629: tty_force_cursor_colour(tty, s->ccolour);
630:
1.1 nicm 631: if (tty->flags & TTY_NOCURSOR)
632: mode &= ~MODE_CURSOR;
633:
634: changed = mode ^ tty->mode;
1.183 nicm 635: if (changed & MODE_BLINKING) {
636: if (tty_term_has(tty->term, TTYC_CVVIS))
637: tty_putcode(tty, TTYC_CVVIS);
638: else
639: tty_putcode(tty, TTYC_CNORM);
640: changed |= MODE_CURSOR;
641: }
642: if (changed & MODE_CURSOR) {
643: if (mode & MODE_CURSOR)
644: tty_putcode(tty, TTYC_CNORM);
645: else
1.1 nicm 646: tty_putcode(tty, TTYC_CIVIS);
1.108 nicm 647: }
1.181 nicm 648: if (s != NULL && tty->cstyle != s->cstyle) {
1.160 nicm 649: if (tty_term_has(tty->term, TTYC_SS)) {
1.108 nicm 650: if (s->cstyle == 0 &&
1.160 nicm 651: tty_term_has(tty->term, TTYC_SE))
652: tty_putcode(tty, TTYC_SE);
1.108 nicm 653: else
1.160 nicm 654: tty_putcode1(tty, TTYC_SS, s->cstyle);
1.108 nicm 655: }
656: tty->cstyle = s->cstyle;
1.1 nicm 657: }
1.195 nicm 658: if (changed & ALL_MOUSE_MODES) {
1.94 nicm 659: if (mode & ALL_MOUSE_MODES) {
1.153 nicm 660: /*
661: * Enable the SGR (1006) extension unconditionally, as
1.221 nicm 662: * it is safe from misinterpretation.
1.153 nicm 663: */
664: tty_puts(tty, "\033[?1006h");
1.225 nicm 665: if (mode & MODE_MOUSE_ALL)
666: tty_puts(tty, "\033[?1003h");
667: else if (mode & MODE_MOUSE_BUTTON)
1.94 nicm 668: tty_puts(tty, "\033[?1002h");
1.98 nicm 669: else if (mode & MODE_MOUSE_STANDARD)
670: tty_puts(tty, "\033[?1000h");
1.86 nicm 671: } else {
1.225 nicm 672: if (tty->mode & MODE_MOUSE_ALL)
673: tty_puts(tty, "\033[?1003l");
674: else if (tty->mode & MODE_MOUSE_BUTTON)
1.94 nicm 675: tty_puts(tty, "\033[?1002l");
1.98 nicm 676: else if (tty->mode & MODE_MOUSE_STANDARD)
677: tty_puts(tty, "\033[?1000l");
1.153 nicm 678: tty_puts(tty, "\033[?1006l");
1.86 nicm 679: }
1.115 nicm 680: }
681: if (changed & MODE_BRACKETPASTE) {
682: if (mode & MODE_BRACKETPASTE)
683: tty_puts(tty, "\033[?2004h");
684: else
685: tty_puts(tty, "\033[?2004l");
1.1 nicm 686: }
687: tty->mode = mode;
688: }
689:
1.207 nicm 690: static void
1.168 nicm 691: tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
692: enum tty_code_code code1, u_int n)
1.1 nicm 693: {
694: if (tty_term_has(tty->term, code))
695: tty_putcode1(tty, code, n);
696: else {
697: while (n-- > 0)
698: tty_putcode(tty, code1);
699: }
700: }
701:
1.207 nicm 702: static void
1.133 nicm 703: tty_repeat_space(struct tty *tty, u_int n)
704: {
1.257 nicm 705: static char s[500];
706:
707: if (*s != ' ')
708: memset(s, ' ', sizeof s);
709:
710: while (n > sizeof s) {
711: tty_putn(tty, s, sizeof s, sizeof s);
712: n -= sizeof s;
713: }
714: if (n != 0)
715: tty_putn(tty, s, n, n);
1.304 nicm 716: }
717:
1.309 nicm 718: /* Is this window bigger than the terminal? */
719: int
720: tty_window_bigger(struct tty *tty)
721: {
722: struct client *c = tty->client;
723: struct window *w = c->session->curw->window;
724:
725: return (tty->sx < w->sx || tty->sy - status_line_size(c) < w->sy);
726: }
727:
728: /* What offset should this window be drawn at? */
729: int
730: tty_window_offset(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
731: {
732: *ox = tty->oox;
733: *oy = tty->ooy;
734: *sx = tty->osx;
735: *sy = tty->osy;
736:
737: return (tty->oflag);
738: }
739:
740: /* What offset should this window be drawn at? */
741: static int
742: tty_window_offset1(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
743: {
744: struct client *c = tty->client;
745: struct window *w = c->session->curw->window;
746: struct window_pane *wp = w->active;
747: u_int cx, cy, lines;
748:
749: lines = status_line_size(c);
750:
751: if (tty->sx >= w->sx && tty->sy - lines >= w->sy) {
752: *ox = 0;
753: *oy = 0;
754: *sx = w->sx;
755: *sy = w->sy;
756:
757: c->pan_window = NULL;
758: return (0);
759: }
760:
761: *sx = tty->sx;
762: *sy = tty->sy - lines;
763:
764: if (c->pan_window == w) {
765: if (*sx >= w->sx)
766: c->pan_ox = 0;
767: else if (c->pan_ox + *sx > w->sx)
768: c->pan_ox = w->sx - *sx;
769: *ox = c->pan_ox;
770: if (*sy >= w->sy)
771: c->pan_oy = 0;
772: else if (c->pan_oy + *sy > w->sy)
773: c->pan_oy = w->sy - *sy;
774: *oy = c->pan_oy;
775: return (1);
776: }
777:
778: if (~wp->screen->mode & MODE_CURSOR) {
779: *ox = 0;
780: *oy = 0;
781: } else {
782: cx = wp->xoff + wp->screen->cx;
783: cy = wp->yoff + wp->screen->cy;
784:
785: if (cx < *sx)
786: *ox = 0;
787: else if (cx > w->sx - *sx)
788: *ox = w->sx - *sx;
789: else
790: *ox = cx - *sx / 2;
791:
792: if (cy < *sy)
793: *oy = 0;
794: else if (cy > w->sy - *sy)
795: *oy = w->sy - *sy;
796: else
797: *oy = cy - *sy / 2;
798: }
799:
800: c->pan_window = NULL;
801: return (1);
802: }
803:
804: /* Update stored offsets for a window and redraw if necessary. */
805: void
806: tty_update_window_offset(struct window *w)
807: {
808: struct client *c;
809:
810: TAILQ_FOREACH(c, &clients, entry) {
811: if (c->session != NULL && c->session->curw->window == w)
812: tty_update_client_offset(c);
813: }
814: }
815:
816: /* Update stored offsets for a client and redraw if necessary. */
817: void
818: tty_update_client_offset(struct client *c)
1.304 nicm 819: {
1.309 nicm 820: u_int ox, oy, sx, sy;
1.314 nicm 821:
822: if (~c->flags & CLIENT_TERMINAL)
823: return;
1.304 nicm 824:
1.309 nicm 825: c->tty.oflag = tty_window_offset1(&c->tty, &ox, &oy, &sx, &sy);
826: if (ox == c->tty.oox &&
827: oy == c->tty.ooy &&
828: sx == c->tty.osx &&
829: sy == c->tty.osy)
830: return;
831:
832: log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
833: __func__, c->name, c->tty.oox, c->tty.ooy, c->tty.osx, c->tty.osy,
834: ox, oy, sx, sy);
835:
836: c->tty.oox = ox;
837: c->tty.ooy = oy;
838: c->tty.osx = sx;
839: c->tty.osy = sy;
840:
841: c->flags |= (CLIENT_REDRAWWINDOW|CLIENT_REDRAWSTATUS);
1.133 nicm 842: }
843:
1.1 nicm 844: /*
1.119 nicm 845: * Is the region large enough to be worth redrawing once later rather than
846: * probably several times now? Currently yes if it is more than 50% of the
847: * pane.
848: */
1.207 nicm 849: static int
1.194 nicm 850: tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
1.119 nicm 851: {
852: struct window_pane *wp = ctx->wp;
853:
854: return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
855: }
856:
857: /*
1.176 nicm 858: * Return if BCE is needed but the terminal doesn't have it - it'll need to be
859: * emulated.
860: */
1.207 nicm 861: static int
1.317 nicm 862: tty_fake_bce(const struct tty *tty, struct window_pane *wp, u_int bg)
1.176 nicm 863: {
864: struct grid_cell gc;
865:
1.210 nicm 866: if (tty_term_flag(tty->term, TTYC_BCE))
867: return (0);
868:
1.176 nicm 869: memcpy(&gc, &grid_default_cell, sizeof gc);
1.203 nicm 870: if (wp != NULL)
871: tty_default_colours(&gc, wp);
1.176 nicm 872:
1.310 nicm 873: if (!COLOUR_DEFAULT(bg) || !COLOUR_DEFAULT(gc.bg))
1.210 nicm 874: return (1);
875: return (0);
1.176 nicm 876: }
877:
878: /*
1.1 nicm 879: * Redraw scroll region using data from screen (already updated). Used when
880: * CSR not supported, or window is a pane that doesn't take up the full
881: * width of the terminal.
882: */
1.207 nicm 883: static void
1.24 nicm 884: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 885: {
1.14 nicm 886: struct window_pane *wp = ctx->wp;
887: struct screen *s = wp->screen;
1.204 nicm 888: u_int i;
1.1 nicm 889:
890: /*
1.119 nicm 891: * If region is large, schedule a window redraw. In most cases this is
892: * likely to be followed by some more scrolling.
1.1 nicm 893: */
1.119 nicm 894: if (tty_large_region(tty, ctx)) {
1.1 nicm 895: wp->flags |= PANE_REDRAW;
896: return;
897: }
898:
1.14 nicm 899: if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
900: for (i = ctx->ocy; i < screen_size_y(s); i++)
1.309 nicm 901: tty_draw_pane(tty, ctx, i);
1.1 nicm 902: } else {
1.14 nicm 903: for (i = ctx->orupper; i <= ctx->orlower; i++)
1.309 nicm 904: tty_draw_pane(tty, ctx, i);
1.1 nicm 905: }
906: }
907:
1.309 nicm 908: /* Is this position visible in the pane? */
909: static int
910: tty_is_visible(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
911: u_int nx, u_int ny)
912: {
913: u_int xoff = ctx->xoff + px, yoff = ctx->yoff + py, lines;
914:
915: if (!ctx->bigger)
916: return (1);
917:
918: if (status_at_line(tty->client) == 0)
919: lines = status_line_size(tty->client);
920: else
921: lines = 0;
922:
923: if (xoff + nx <= ctx->ox || xoff >= ctx->ox + ctx->sx ||
1.320 nicm 924: yoff + ny <= ctx->oy || yoff >= lines + ctx->oy + ctx->sy)
1.309 nicm 925: return (0);
926: return (1);
927: }
928:
929: /* Clamp line position to visible part of pane. */
930: static int
931: tty_clamp_line(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
932: u_int nx, u_int *i, u_int *x, u_int *rx, u_int *ry)
933: {
934: struct window_pane *wp = ctx->wp;
935: u_int xoff = wp->xoff + px;
936:
937: if (!tty_is_visible(tty, ctx, px, py, nx, 1))
938: return (0);
939: *ry = ctx->yoff + py - ctx->oy;
940:
941: if (xoff >= ctx->ox && xoff + nx <= ctx->ox + ctx->sx) {
942: /* All visible. */
943: *i = 0;
944: *x = ctx->xoff + px - ctx->ox;
945: *rx = nx;
946: } else if (xoff < ctx->ox && xoff + nx > ctx->ox + ctx->sx) {
947: /* Both left and right not visible. */
948: *i = ctx->ox;
949: *x = 0;
950: *rx = ctx->sx;
951: } else if (xoff < ctx->ox) {
952: /* Left not visible. */
953: *i = ctx->ox - (ctx->xoff + px);
954: *x = 0;
955: *rx = nx - *i;
956: } else {
957: /* Right not visible. */
958: *i = 0;
959: *x = (ctx->xoff + px) - ctx->ox;
960: *rx = ctx->sx - *x;
961: }
962: if (*rx > nx)
963: fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
964:
965: return (1);
966: }
967:
968: /* Clear a line. */
1.271 nicm 969: static void
1.317 nicm 970: tty_clear_line(struct tty *tty, struct window_pane *wp, u_int py, u_int px,
971: u_int nx, u_int bg)
1.271 nicm 972: {
1.309 nicm 973: struct client *c = tty->client;
974:
975: log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1.271 nicm 976:
977: /* Nothing to clear. */
978: if (nx == 0)
979: return;
980:
981: /* If genuine BCE is available, can try escape sequences. */
982: if (!tty_fake_bce(tty, wp, bg)) {
983: /* Off the end of the line, use EL if available. */
984: if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) {
985: tty_cursor(tty, px, py);
986: tty_putcode(tty, TTYC_EL);
987: return;
988: }
989:
990: /* At the start of the line. Use EL1. */
991: if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) {
992: tty_cursor(tty, px + nx - 1, py);
993: tty_putcode(tty, TTYC_EL1);
994: return;
995: }
996:
997: /* Section of line. Use ECH if possible. */
998: if (tty_term_has(tty->term, TTYC_ECH)) {
999: tty_cursor(tty, px, py);
1000: tty_putcode1(tty, TTYC_ECH, nx);
1001: return;
1002: }
1003: }
1004:
1005: /* Couldn't use an escape sequence, use spaces. */
1.272 nicm 1006: tty_cursor(tty, px, py);
1.271 nicm 1007: tty_repeat_space(tty, nx);
1008: }
1009:
1.309 nicm 1010: /* Clear a line, adjusting to visible part of pane. */
1011: static void
1012: tty_clear_pane_line(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1013: u_int px, u_int nx, u_int bg)
1014: {
1015: struct client *c = tty->client;
1016: u_int i, x, rx, ry;
1017:
1018: log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1019:
1020: if (tty_clamp_line(tty, ctx, px, py, nx, &i, &x, &rx, &ry))
1021: tty_clear_line(tty, ctx->wp, ry, x, rx, bg);
1022: }
1023:
1024: /* Clamp area position to visible part of pane. */
1025: static int
1026: tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1027: u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
1028: u_int *ry)
1029: {
1030: struct window_pane *wp = ctx->wp;
1031: u_int xoff = wp->xoff + px, yoff = wp->yoff + py;
1032:
1033: if (!tty_is_visible(tty, ctx, px, py, nx, ny))
1034: return (0);
1035:
1036: if (xoff >= ctx->ox && xoff + nx <= ctx->ox + ctx->sx) {
1037: /* All visible. */
1038: *i = 0;
1039: *x = ctx->xoff + px - ctx->ox;
1040: *rx = nx;
1041: } else if (xoff < ctx->ox && xoff + nx > ctx->ox + ctx->sx) {
1042: /* Both left and right not visible. */
1043: *i = ctx->ox;
1044: *x = 0;
1045: *rx = ctx->sx;
1046: } else if (xoff < ctx->ox) {
1047: /* Left not visible. */
1048: *i = ctx->ox - (ctx->xoff + px);
1049: *x = 0;
1050: *rx = nx - *i;
1051: } else {
1052: /* Right not visible. */
1053: *i = 0;
1054: *x = (ctx->xoff + px) - ctx->ox;
1055: *rx = ctx->sx - *x;
1056: }
1057: if (*rx > nx)
1058: fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1059:
1060: if (yoff >= ctx->oy && yoff + ny <= ctx->oy + ctx->sy) {
1061: /* All visible. */
1062: *j = 0;
1063: *y = ctx->yoff + py - ctx->oy;
1064: *ry = ny;
1065: } else if (yoff < ctx->oy && yoff + ny > ctx->oy + ctx->sy) {
1.327 nicm 1066: /* Both top and bottom not visible. */
1.309 nicm 1067: *j = ctx->oy;
1068: *y = 0;
1069: *ry = ctx->sy;
1070: } else if (yoff < ctx->oy) {
1.327 nicm 1071: /* Top not visible. */
1.309 nicm 1072: *j = ctx->oy - (ctx->yoff + py);
1073: *y = 0;
1074: *ry = ny - *j;
1075: } else {
1.327 nicm 1076: /* Bottom not visible. */
1.309 nicm 1077: *j = 0;
1078: *y = (ctx->yoff + py) - ctx->oy;
1079: *ry = ctx->sy - *y;
1080: }
1081: if (*ry > ny)
1082: fatalx("%s: y too big, %u > %u", __func__, *ry, ny);
1083:
1084: return (1);
1085: }
1086:
1087: /* Clear an area, adjusting to visible part of pane. */
1.271 nicm 1088: static void
1.317 nicm 1089: tty_clear_area(struct tty *tty, struct window_pane *wp, u_int py, u_int ny,
1090: u_int px, u_int nx, u_int bg)
1.271 nicm 1091: {
1.309 nicm 1092: struct client *c = tty->client;
1093: u_int yy;
1094: char tmp[64];
1.271 nicm 1095:
1.309 nicm 1096: log_debug("%s: %s, %u,%u at %u,%u", __func__, c->name, nx, ny, px, py);
1.271 nicm 1097:
1098: /* Nothing to clear. */
1099: if (nx == 0 || ny == 0)
1100: return;
1101:
1102: /* If genuine BCE is available, can try escape sequences. */
1103: if (!tty_fake_bce(tty, wp, bg)) {
1.276 nicm 1104: /* Use ED if clearing off the bottom of the terminal. */
1.271 nicm 1105: if (px == 0 &&
1106: px + nx >= tty->sx &&
1107: py + ny >= tty->sy &&
1108: tty_term_has(tty->term, TTYC_ED)) {
1109: tty_cursor(tty, 0, py);
1110: tty_putcode(tty, TTYC_ED);
1.274 nicm 1111: return;
1112: }
1113:
1114: /*
1.276 nicm 1115: * On VT420 compatible terminals we can use DECFRA if the
1116: * background colour isn't default (because it doesn't work
1117: * after SGR 0).
1.274 nicm 1118: */
1.313 nicm 1119: if (tty->term_type == TTY_VT420 && !COLOUR_DEFAULT(bg)) {
1.274 nicm 1120: xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x",
1121: py + 1, px + 1, py + ny, px + nx);
1122: tty_puts(tty, tmp);
1.276 nicm 1123: return;
1124: }
1125:
1.280 nicm 1126: /* Full lines can be scrolled away to clear them. */
1127: if (px == 0 &&
1.281 nicm 1128: px + nx >= tty->sx &&
1.280 nicm 1129: ny > 2 &&
1130: tty_term_has(tty->term, TTYC_CSR) &&
1131: tty_term_has(tty->term, TTYC_INDN)) {
1132: tty_region(tty, py, py + ny - 1);
1133: tty_margin_off(tty);
1.284 nicm 1134: tty_putcode1(tty, TTYC_INDN, ny);
1.280 nicm 1135: return;
1136: }
1137:
1.276 nicm 1138: /*
1139: * If margins are supported, can just scroll the area off to
1140: * clear it.
1141: */
1.277 nicm 1142: if (nx > 2 &&
1143: ny > 2 &&
1.280 nicm 1144: tty_term_has(tty->term, TTYC_CSR) &&
1.277 nicm 1145: tty_use_margin(tty) &&
1146: tty_term_has(tty->term, TTYC_INDN)) {
1.276 nicm 1147: tty_region(tty, py, py + ny - 1);
1148: tty_margin(tty, px, px + nx - 1);
1.284 nicm 1149: tty_putcode1(tty, TTYC_INDN, ny);
1.271 nicm 1150: return;
1151: }
1152: }
1153:
1154: /* Couldn't use an escape sequence, loop over the lines. */
1155: for (yy = py; yy < py + ny; yy++)
1156: tty_clear_line(tty, wp, yy, px, nx, bg);
1157: }
1158:
1.309 nicm 1159: /* Clear an area in a pane. */
1160: static void
1161: tty_clear_pane_area(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1162: u_int ny, u_int px, u_int nx, u_int bg)
1163: {
1164: u_int i, j, x, y, rx, ry;
1165:
1166: if (tty_clamp_area(tty, ctx, px, py, nx, ny, &i, &j, &x, &y, &rx, &ry))
1167: tty_clear_area(tty, ctx->wp, y, ry, x, rx, bg);
1168: }
1169:
1170: static void
1171: tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
1.176 nicm 1172: {
1.309 nicm 1173: struct window_pane *wp = ctx->wp;
1174: struct screen *s = wp->screen;
1175: u_int nx = screen_size_x(s), i, x, rx, ry;
1176:
1177: log_debug("%s: %s %u %d", __func__, tty->client->name, py, ctx->bigger);
1178:
1179: if (!ctx->bigger) {
1180: tty_draw_line(tty, wp, s, 0, py, nx, ctx->xoff, ctx->yoff + py);
1181: return;
1182: }
1183: if (tty_clamp_line(tty, ctx, 0, py, nx, &i, &x, &rx, &ry))
1184: tty_draw_line(tty, wp, s, i, py, rx, x, ry);
1.176 nicm 1185: }
1186:
1.298 nicm 1187: static const struct grid_cell *
1188: tty_check_codeset(struct tty *tty, const struct grid_cell *gc)
1189: {
1190: static struct grid_cell new;
1191: u_int n;
1192:
1193: /* Characters less than 0x7f are always fine, no matter what. */
1194: if (gc->data.size == 1 && *gc->data.data < 0x7f)
1195: return (gc);
1196:
1197: /* UTF-8 terminal and a UTF-8 character - fine. */
1198: if (tty->flags & TTY_UTF8)
1199: return (gc);
1200:
1201: /* Replace by the right number of underscores. */
1202: n = gc->data.width;
1203: if (n > UTF8_SIZE)
1204: n = UTF8_SIZE;
1205: memcpy(&new, gc, sizeof new);
1206: new.data.size = n;
1207: memset(new.data.data, '_', n);
1208: return (&new);
1209: }
1210:
1.176 nicm 1211: void
1.317 nicm 1212: tty_draw_line(struct tty *tty, struct window_pane *wp, struct screen *s,
1213: u_int px, u_int py, u_int nx, u_int atx, u_int aty)
1.1 nicm 1214: {
1.297 nicm 1215: struct grid *gd = s->grid;
1.250 nicm 1216: struct grid_cell gc, last;
1.298 nicm 1217: const struct grid_cell *gcp;
1.309 nicm 1218: struct grid_line *gl;
1219: u_int i, j, ux, sx, width;
1.259 nicm 1220: int flags, cleared = 0;
1.250 nicm 1221: char buf[512];
1.315 nicm 1222: size_t len;
1.303 nicm 1223: u_int cellsize;
1.1 nicm 1224:
1.309 nicm 1225: log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__,
1226: px, py, nx, atx, aty);
1227:
1228: /*
1229: * py is the line in the screen to draw.
1230: * px is the start x and nx is the width to draw.
1231: * atx,aty is the line on the terminal to draw it.
1232: */
1233:
1.259 nicm 1234: flags = (tty->flags & TTY_NOCURSOR);
1.181 nicm 1235: tty->flags |= TTY_NOCURSOR;
1236: tty_update_mode(tty, tty->mode, s);
1.35 nicm 1237:
1.214 nicm 1238: tty_region_off(tty);
1239: tty_margin_off(tty);
1240:
1.273 nicm 1241: /*
1242: * Clamp the width to cellsize - note this is not cellused, because
1243: * there may be empty background cells after it (from BCE).
1244: */
1.1 nicm 1245: sx = screen_size_x(s);
1.309 nicm 1246: if (nx > sx)
1247: nx = sx;
1.303 nicm 1248: cellsize = grid_get_line(gd, gd->hsize + py)->cellsize;
1249: if (sx > cellsize)
1250: sx = cellsize;
1.1 nicm 1251: if (sx > tty->sx)
1252: sx = tty->sx;
1.309 nicm 1253: if (sx > nx)
1254: sx = nx;
1.292 nicm 1255: ux = 0;
1.1 nicm 1256:
1.309 nicm 1257: if (py == 0)
1258: gl = NULL;
1259: else
1260: gl = grid_get_line(gd, gd->hsize + py - 1);
1.268 nicm 1261: if (wp == NULL ||
1.309 nicm 1262: gl == NULL ||
1263: (~gl->flags & GRID_LINE_WRAPPED) ||
1264: atx != 0 ||
1.268 nicm 1265: tty->cx < tty->sx ||
1.309 nicm 1266: nx < tty->sx) {
1267: if (nx < tty->sx &&
1268: atx == 0 &&
1269: px + sx != nx &&
1.268 nicm 1270: tty_term_has(tty->term, TTYC_EL1) &&
1271: !tty_fake_bce(tty, wp, 8)) {
1272: tty_default_attributes(tty, wp, 8);
1.309 nicm 1273: tty_cursor(tty, nx - 1, aty);
1.268 nicm 1274: tty_putcode(tty, TTYC_EL1);
1275: cleared = 1;
1276: }
1277: } else
1.309 nicm 1278: log_debug("%s: wrapped line %u", __func__, aty);
1.250 nicm 1279:
1280: memcpy(&last, &grid_default_cell, sizeof last);
1281: len = 0;
1282: width = 0;
1.46 nicm 1283:
1.1 nicm 1284: for (i = 0; i < sx; i++) {
1.309 nicm 1285: grid_view_get_cell(gd, px + i, py, &gc);
1.298 nicm 1286: gcp = tty_check_codeset(tty, &gc);
1.250 nicm 1287: if (len != 0 &&
1.298 nicm 1288: ((gcp->attr & GRID_ATTR_CHARSET) ||
1289: gcp->flags != last.flags ||
1290: gcp->attr != last.attr ||
1291: gcp->fg != last.fg ||
1292: gcp->bg != last.bg ||
1.328 ! nicm 1293: gcp->us != last.us ||
1.315 nicm 1294: ux + width + gcp->data.width > nx ||
1.298 nicm 1295: (sizeof buf) - len < gcp->data.size)) {
1.316 nicm 1296: tty_attributes(tty, &last, wp);
1.315 nicm 1297: if (last.flags & GRID_FLAG_CLEARED) {
1298: log_debug("%s: %zu cleared", __func__, len);
1299: tty_clear_line(tty, wp, aty, atx + ux, width,
1300: last.bg);
1301: } else {
1302: tty_cursor(tty, atx + ux, aty);
1303: tty_putn(tty, buf, len, width);
1304: }
1.292 nicm 1305: ux += width;
1.250 nicm 1306:
1307: len = 0;
1308: width = 0;
1309: }
1310:
1.298 nicm 1311: if (gcp->flags & GRID_FLAG_SELECTED)
1.299 nicm 1312: screen_select_cell(s, &last, gcp);
1.250 nicm 1313: else
1.299 nicm 1314: memcpy(&last, gcp, sizeof last);
1.309 nicm 1315: if (ux + gcp->data.width > nx) {
1.299 nicm 1316: tty_attributes(tty, &last, wp);
1.315 nicm 1317: tty_cursor(tty, atx + ux, aty);
1.298 nicm 1318: for (j = 0; j < gcp->data.width; j++) {
1.309 nicm 1319: if (ux + j > nx)
1.297 nicm 1320: break;
1321: tty_putc(tty, ' ');
1322: ux++;
1323: }
1.299 nicm 1324: } else if (gcp->attr & GRID_ATTR_CHARSET) {
1325: tty_attributes(tty, &last, wp);
1.315 nicm 1326: tty_cursor(tty, atx + ux, aty);
1.299 nicm 1327: for (j = 0; j < gcp->data.size; j++)
1328: tty_putc(tty, gcp->data.data[j]);
1329: ux += gc.data.width;
1330: } else {
1.298 nicm 1331: memcpy(buf + len, gcp->data.data, gcp->data.size);
1332: len += gcp->data.size;
1333: width += gcp->data.width;
1.250 nicm 1334: }
1335: }
1.315 nicm 1336: if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1.316 nicm 1337: tty_attributes(tty, &last, wp);
1.315 nicm 1338: if (last.flags & GRID_FLAG_CLEARED) {
1339: log_debug("%s: %zu cleared (end)", __func__, len);
1340: tty_clear_line(tty, wp, aty, atx + ux, width, last.bg);
1341: } else {
1342: tty_cursor(tty, atx + ux, aty);
1.291 nicm 1343: tty_putn(tty, buf, len, width);
1344: }
1.315 nicm 1345: ux += width;
1.1 nicm 1346: }
1347:
1.309 nicm 1348: if (!cleared && ux < nx) {
1.315 nicm 1349: log_debug("%s: %u to end of line (%zu cleared)", __func__,
1350: nx - ux, len);
1.210 nicm 1351: tty_default_attributes(tty, wp, 8);
1.309 nicm 1352: tty_clear_line(tty, wp, aty, atx + ux, nx - ux, 8);
1.35 nicm 1353: }
1.1 nicm 1354:
1.181 nicm 1355: tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107 nicm 1356: tty_update_mode(tty, tty->mode, s);
1.15 nicm 1357: }
1358:
1.201 nicm 1359: static int
1.182 nicm 1360: tty_client_ready(struct client *c, struct window_pane *wp)
1361: {
1362: if (c->session == NULL || c->tty.term == NULL)
1363: return (0);
1.305 nicm 1364: if (c->flags & (CLIENT_REDRAWWINDOW|CLIENT_SUSPENDED))
1.182 nicm 1365: return (0);
1366: if (c->tty.flags & TTY_FREEZE)
1367: return (0);
1368: if (c->session->curw->window != wp->window)
1369: return (0);
1.309 nicm 1370: if (wp->layout_cell == NULL)
1371: return (0);
1.182 nicm 1372: return (1);
1373: }
1374:
1.15 nicm 1375: void
1.182 nicm 1376: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1377: struct tty_ctx *ctx)
1.15 nicm 1378: {
1379: struct window_pane *wp = ctx->wp;
1380: struct client *c;
1381:
1382: if (wp == NULL)
1383: return;
1.309 nicm 1384: if (wp->flags & (PANE_REDRAW|PANE_DROP))
1.15 nicm 1385: return;
1386:
1.178 nicm 1387: TAILQ_FOREACH(c, &clients, entry) {
1.182 nicm 1388: if (!tty_client_ready(c, wp))
1.15 nicm 1389: continue;
1390:
1.309 nicm 1391: ctx->bigger = tty_window_offset(&c->tty, &ctx->ox, &ctx->oy,
1392: &ctx->sx, &ctx->sy);
1393:
1.139 nicm 1394: ctx->xoff = wp->xoff;
1395: ctx->yoff = wp->yoff;
1.309 nicm 1396:
1.139 nicm 1397: if (status_at_line(c) == 0)
1.309 nicm 1398: ctx->yoff += status_line_size(c);
1.113 nicm 1399:
1.139 nicm 1400: cmdfn(&c->tty, ctx);
1.1 nicm 1401: }
1402: }
1403:
1404: void
1.24 nicm 1405: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1406: {
1.77 nicm 1407: struct window_pane *wp = ctx->wp;
1.1 nicm 1408:
1.309 nicm 1409: if (ctx->bigger ||
1410: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1411: tty_fake_bce(tty, wp, ctx->bg) ||
1412: (!tty_term_has(tty->term, TTYC_ICH) &&
1413: !tty_term_has(tty->term, TTYC_ICH1))) {
1.309 nicm 1414: tty_draw_pane(tty, ctx, ctx->ocy);
1.1 nicm 1415: return;
1416: }
1417:
1.210 nicm 1418: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1419:
1.77 nicm 1420: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 1421:
1.206 nicm 1422: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1 nicm 1423: }
1424:
1425: void
1.24 nicm 1426: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1427: {
1.77 nicm 1428: struct window_pane *wp = ctx->wp;
1.1 nicm 1429:
1.309 nicm 1430: if (ctx->bigger ||
1431: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1432: tty_fake_bce(tty, wp, ctx->bg) ||
1.26 nicm 1433: (!tty_term_has(tty->term, TTYC_DCH) &&
1434: !tty_term_has(tty->term, TTYC_DCH1))) {
1.309 nicm 1435: tty_draw_pane(tty, ctx, ctx->ocy);
1.1 nicm 1436: return;
1437: }
1438:
1.210 nicm 1439: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1440:
1.77 nicm 1441: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 1442:
1.206 nicm 1443: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146 nicm 1444: }
1445:
1446: void
1447: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1448: {
1.309 nicm 1449: if (ctx->bigger) {
1450: tty_draw_pane(tty, ctx, ctx->ocy);
1451: return;
1452: }
1453:
1.275 nicm 1454: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.146 nicm 1455:
1456: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1457:
1.210 nicm 1458: if (tty_term_has(tty->term, TTYC_ECH) &&
1.262 nicm 1459: !tty_fake_bce(tty, ctx->wp, 8))
1.146 nicm 1460: tty_putcode1(tty, TTYC_ECH, ctx->num);
1.257 nicm 1461: else
1462: tty_repeat_space(tty, ctx->num);
1.1 nicm 1463: }
1464:
1465: void
1.24 nicm 1466: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1467: {
1.309 nicm 1468: if (ctx->bigger ||
1469: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1470: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.77 nicm 1471: !tty_term_has(tty->term, TTYC_CSR) ||
1.307 nicm 1472: !tty_term_has(tty->term, TTYC_IL1) ||
1473: ctx->wp->sx == 1 ||
1474: ctx->wp->sy == 1) {
1.14 nicm 1475: tty_redraw_region(tty, ctx);
1.1 nicm 1476: return;
1477: }
1478:
1.210 nicm 1479: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1 nicm 1480:
1.77 nicm 1481: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1482: tty_margin_off(tty);
1.77 nicm 1483: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1484:
1.12 nicm 1485: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.279 nicm 1486: tty->cx = tty->cy = UINT_MAX;
1.1 nicm 1487: }
1488:
1489: void
1.24 nicm 1490: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1491: {
1.309 nicm 1492: if (ctx->bigger ||
1493: !tty_pane_full_width(tty, ctx) ||
1.210 nicm 1494: tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.72 nicm 1495: !tty_term_has(tty->term, TTYC_CSR) ||
1.307 nicm 1496: !tty_term_has(tty->term, TTYC_DL1) ||
1497: ctx->wp->sx == 1 ||
1498: ctx->wp->sy == 1) {
1.14 nicm 1499: tty_redraw_region(tty, ctx);
1.1 nicm 1500: return;
1501: }
1502:
1.210 nicm 1503: tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1 nicm 1504:
1.77 nicm 1505: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1506: tty_margin_off(tty);
1.77 nicm 1507: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1508:
1.12 nicm 1509: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.279 nicm 1510: tty->cx = tty->cy = UINT_MAX;
1.1 nicm 1511: }
1512:
1513: void
1.24 nicm 1514: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1515: {
1.77 nicm 1516: struct window_pane *wp = ctx->wp;
1.309 nicm 1517: u_int nx;
1.1 nicm 1518:
1.210 nicm 1519: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1520:
1.271 nicm 1521: nx = screen_size_x(wp->screen);
1.309 nicm 1522: tty_clear_pane_line(tty, ctx, ctx->ocy, 0, nx, ctx->bg);
1.1 nicm 1523: }
1524:
1525: void
1.24 nicm 1526: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1527: {
1.77 nicm 1528: struct window_pane *wp = ctx->wp;
1.309 nicm 1529: u_int nx;
1.1 nicm 1530:
1.210 nicm 1531: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1532:
1.271 nicm 1533: nx = screen_size_x(wp->screen) - ctx->ocx;
1.309 nicm 1534: tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1.1 nicm 1535: }
1536:
1537: void
1.24 nicm 1538: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1539: {
1.210 nicm 1540: struct window_pane *wp = ctx->wp;
1541:
1542: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1543:
1.309 nicm 1544: tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1.1 nicm 1545: }
1546:
1547: void
1.24 nicm 1548: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1549: {
1.278 nicm 1550: struct window_pane *wp = ctx->wp;
1551:
1.56 nicm 1552: if (ctx->ocy != ctx->orupper)
1553: return;
1554:
1.309 nicm 1555: if (ctx->bigger ||
1556: !tty_pane_full_width(tty, ctx) ||
1.278 nicm 1557: tty_fake_bce(tty, wp, 8) ||
1.70 nicm 1558: !tty_term_has(tty->term, TTYC_CSR) ||
1.307 nicm 1559: !tty_term_has(tty->term, TTYC_RI) ||
1560: ctx->wp->sx == 1 ||
1561: ctx->wp->sy == 1) {
1.14 nicm 1562: tty_redraw_region(tty, ctx);
1.1 nicm 1563: return;
1564: }
1565:
1.278 nicm 1566: tty_default_attributes(tty, wp, ctx->bg);
1.77 nicm 1567:
1.56 nicm 1568: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1569: tty_margin_off(tty);
1.56 nicm 1570: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 1571:
1.56 nicm 1572: tty_putcode(tty, TTYC_RI);
1.1 nicm 1573: }
1574:
1575: void
1.24 nicm 1576: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1577: {
1.77 nicm 1578: struct window_pane *wp = ctx->wp;
1.1 nicm 1579:
1.56 nicm 1580: if (ctx->ocy != ctx->orlower)
1581: return;
1582:
1.309 nicm 1583: if (ctx->bigger ||
1584: (!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.262 nicm 1585: tty_fake_bce(tty, wp, 8) ||
1.307 nicm 1586: !tty_term_has(tty->term, TTYC_CSR) ||
1587: wp->sx == 1 ||
1588: wp->sy == 1) {
1.236 nicm 1589: tty_redraw_region(tty, ctx);
1.1 nicm 1590: return;
1591: }
1.52 nicm 1592:
1.278 nicm 1593: tty_default_attributes(tty, wp, ctx->bg);
1.77 nicm 1594:
1.56 nicm 1595: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.212 nicm 1596: tty_margin_pane(tty, ctx);
1597:
1598: /*
1.301 nicm 1599: * If we want to wrap a pane while using margins, the cursor needs to
1600: * be exactly on the right of the region. If the cursor is entirely off
1601: * the edge - move it back to the right. Some terminals are funny about
1602: * this and insert extra spaces, so only use the right if margins are
1603: * enabled.
1.212 nicm 1604: */
1.301 nicm 1605: if (ctx->xoff + ctx->ocx > tty->rright) {
1606: if (!tty_use_margin(tty))
1607: tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
1608: else
1609: tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1610: } else
1.212 nicm 1611: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 1612:
1.56 nicm 1613: tty_putc(tty, '\n');
1.240 nicm 1614: }
1615:
1616: void
1617: tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1618: {
1619: struct window_pane *wp = ctx->wp;
1.284 nicm 1620: u_int i;
1.240 nicm 1621:
1.309 nicm 1622: if (ctx->bigger ||
1623: (!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.262 nicm 1624: tty_fake_bce(tty, wp, 8) ||
1.307 nicm 1625: !tty_term_has(tty->term, TTYC_CSR) ||
1626: wp->sx == 1 ||
1627: wp->sy == 1) {
1.240 nicm 1628: tty_redraw_region(tty, ctx);
1629: return;
1630: }
1631:
1.278 nicm 1632: tty_default_attributes(tty, wp, ctx->bg);
1.240 nicm 1633:
1634: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1635: tty_margin_pane(tty, ctx);
1636:
1.284 nicm 1637: if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1.301 nicm 1638: if (!tty_use_margin(tty))
1639: tty_cursor(tty, 0, tty->rlower);
1640: else
1641: tty_cursor(tty, tty->rright, tty->rlower);
1.284 nicm 1642: for (i = 0; i < ctx->num; i++)
1.240 nicm 1643: tty_putc(tty, '\n');
1.301 nicm 1644: } else {
1645: tty_cursor(tty, 0, tty->cy);
1.284 nicm 1646: tty_putcode1(tty, TTYC_INDN, ctx->num);
1.301 nicm 1647: }
1.1 nicm 1648: }
1649:
1650: void
1.24 nicm 1651: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1652: {
1.77 nicm 1653: struct window_pane *wp = ctx->wp;
1.271 nicm 1654: u_int px, py, nx, ny;
1.1 nicm 1655:
1.210 nicm 1656: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1657:
1.271 nicm 1658: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1659: tty_margin_off(tty);
1.39 nicm 1660:
1.309 nicm 1661: px = 0;
1.271 nicm 1662: nx = screen_size_x(wp->screen);
1.309 nicm 1663: py = ctx->ocy + 1;
1.271 nicm 1664: ny = screen_size_y(wp->screen) - ctx->ocy - 1;
1665:
1.309 nicm 1666: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271 nicm 1667:
1.309 nicm 1668: px = ctx->ocx;
1.271 nicm 1669: nx = screen_size_x(wp->screen) - ctx->ocx;
1.309 nicm 1670: py = ctx->ocy;
1.271 nicm 1671:
1.309 nicm 1672: tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1 nicm 1673: }
1674:
1675: void
1.24 nicm 1676: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1677: {
1.77 nicm 1678: struct window_pane *wp = ctx->wp;
1.271 nicm 1679: u_int px, py, nx, ny;
1.1 nicm 1680:
1.227 nicm 1681: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1682:
1.271 nicm 1683: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1684: tty_margin_off(tty);
1.39 nicm 1685:
1.309 nicm 1686: px = 0;
1.271 nicm 1687: nx = screen_size_x(wp->screen);
1.309 nicm 1688: py = 0;
1.319 nicm 1689: ny = ctx->ocy;
1.271 nicm 1690:
1.309 nicm 1691: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271 nicm 1692:
1.309 nicm 1693: px = 0;
1.271 nicm 1694: nx = ctx->ocx + 1;
1.309 nicm 1695: py = ctx->ocy;
1.271 nicm 1696:
1.309 nicm 1697: tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1 nicm 1698: }
1699:
1700: void
1.24 nicm 1701: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1702: {
1.77 nicm 1703: struct window_pane *wp = ctx->wp;
1.271 nicm 1704: u_int px, py, nx, ny;
1.1 nicm 1705:
1.210 nicm 1706: tty_default_attributes(tty, wp, ctx->bg);
1.1 nicm 1707:
1.271 nicm 1708: tty_region_pane(tty, ctx, 0, screen_size_y(wp->screen) - 1);
1.214 nicm 1709: tty_margin_off(tty);
1.39 nicm 1710:
1.309 nicm 1711: px = 0;
1.271 nicm 1712: nx = screen_size_x(wp->screen);
1.309 nicm 1713: py = 0;
1.271 nicm 1714: ny = screen_size_y(wp->screen);
1715:
1.309 nicm 1716: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.4 nicm 1717: }
1718:
1719: void
1.24 nicm 1720: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 1721: {
1.12 nicm 1722: struct window_pane *wp = ctx->wp;
1723: struct screen *s = wp->screen;
1724: u_int i, j;
1.4 nicm 1725:
1.309 nicm 1726: if (ctx->bigger) {
1727: wp->flags |= PANE_REDRAW;
1728: return;
1729: }
1730:
1.176 nicm 1731: tty_attributes(tty, &grid_default_cell, wp);
1.4 nicm 1732:
1.39 nicm 1733: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.214 nicm 1734: tty_margin_off(tty);
1.4 nicm 1735:
1736: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 1737: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 1738: for (i = 0; i < screen_size_x(s); i++)
1739: tty_putc(tty, 'E');
1.1 nicm 1740: }
1741: }
1742:
1743: void
1.24 nicm 1744: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1745: {
1.309 nicm 1746: if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1))
1747: return;
1748:
1749: if (ctx->xoff + ctx->ocx - ctx->ox > tty->sx - 1 &&
1.306 nicm 1750: ctx->ocy == ctx->orlower &&
1751: tty_pane_full_width(tty, ctx))
1752: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.46 nicm 1753:
1.306 nicm 1754: tty_margin_off(tty);
1.253 nicm 1755: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1756:
1.237 nicm 1757: tty_cell(tty, ctx->cell, ctx->wp);
1.239 nicm 1758: }
1759:
1760: void
1761: tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
1762: {
1.309 nicm 1763: struct window_pane *wp = ctx->wp;
1764:
1765: if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
1766: return;
1767:
1768: if (ctx->bigger &&
1769: (ctx->xoff + ctx->ocx < ctx->ox ||
1770: ctx->xoff + ctx->ocx + ctx->num > ctx->ox + ctx->sx)) {
1771: if (!ctx->wrapped ||
1772: !tty_pane_full_width(tty, ctx) ||
1773: (tty->term->flags & TERM_EARLYWRAP) ||
1774: ctx->xoff + ctx->ocx != 0 ||
1775: ctx->yoff + ctx->ocy != tty->cy + 1 ||
1776: tty->cx < tty->sx ||
1777: tty->cy == tty->rlower)
1778: tty_draw_pane(tty, ctx, ctx->ocy);
1779: else
1780: wp->flags |= PANE_REDRAW;
1781: return;
1782: }
1783:
1.306 nicm 1784: tty_margin_off(tty);
1.253 nicm 1785: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.239 nicm 1786:
1787: tty_attributes(tty, ctx->cell, ctx->wp);
1788: tty_putn(tty, ctx->ptr, ctx->num, ctx->num);
1.106 nicm 1789: }
1790:
1791: void
1792: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1793: {
1794: char *buf;
1795: size_t off;
1796:
1797: if (!tty_term_has(tty->term, TTYC_MS))
1798: return;
1799:
1800: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1801: buf = xmalloc(off);
1802:
1803: b64_ntop(ctx->ptr, ctx->num, buf, off);
1804: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1805:
1.138 nicm 1806: free(buf);
1.100 nicm 1807: }
1808:
1809: void
1810: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1811: {
1.256 nicm 1812: tty_add(tty, ctx->ptr, ctx->num);
1.242 nicm 1813: tty_invalidate(tty);
1.1 nicm 1814: }
1815:
1.207 nicm 1816: static void
1.317 nicm 1817: tty_cell(struct tty *tty, const struct grid_cell *gc, struct window_pane *wp)
1.1 nicm 1818: {
1.298 nicm 1819: const struct grid_cell *gcp;
1.1 nicm 1820:
1821: /* Skip last character if terminal is stupid. */
1.211 nicm 1822: if ((tty->term->flags & TERM_EARLYWRAP) &&
1823: tty->cy == tty->sy - 1 &&
1824: tty->cx == tty->sx - 1)
1.1 nicm 1825: return;
1826:
1827: /* If this is a padding character, do nothing. */
1828: if (gc->flags & GRID_FLAG_PADDING)
1829: return;
1830:
1831: /* Set the attributes. */
1.176 nicm 1832: tty_attributes(tty, gc, wp);
1.1 nicm 1833:
1.147 nicm 1834: /* Get the cell and if ASCII write with putc to do ACS translation. */
1.298 nicm 1835: gcp = tty_check_codeset(tty, gc);
1836: if (gcp->data.size == 1) {
1837: if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
1.1 nicm 1838: return;
1.298 nicm 1839: tty_putc(tty, *gcp->data.data);
1.1 nicm 1840: return;
1841: }
1842:
1.147 nicm 1843: /* Write the data. */
1.298 nicm 1844: tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
1.1 nicm 1845: }
1846:
1847: void
1848: tty_reset(struct tty *tty)
1849: {
1850: struct grid_cell *gc = &tty->cell;
1851:
1.228 nicm 1852: if (!grid_cells_equal(gc, &grid_default_cell)) {
1.285 nicm 1853: if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.228 nicm 1854: tty_putcode(tty, TTYC_RMACS);
1855: tty_putcode(tty, TTYC_SGR0);
1856: memcpy(gc, &grid_default_cell, sizeof *gc);
1857: }
1.1 nicm 1858:
1.228 nicm 1859: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
1860: tty->last_wp = -1;
1.242 nicm 1861: }
1862:
1863: static void
1864: tty_invalidate(struct tty *tty)
1865: {
1866: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
1867:
1868: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
1869: tty->last_wp = -1;
1870:
1871: tty->cx = tty->cy = UINT_MAX;
1872:
1873: tty->rupper = tty->rleft = UINT_MAX;
1874: tty->rlower = tty->rright = UINT_MAX;
1875:
1876: if (tty->flags & TTY_STARTED) {
1877: tty_putcode(tty, TTYC_SGR0);
1878:
1879: tty->mode = ALL_MODES;
1880: tty_update_mode(tty, MODE_CURSOR, NULL);
1881:
1882: tty_cursor(tty, 0, 0);
1883: tty_region_off(tty);
1884: tty_margin_off(tty);
1885: } else
1886: tty->mode = MODE_CURSOR;
1.1 nicm 1887: }
1888:
1.214 nicm 1889: /* Turn off margin. */
1890: void
1891: tty_region_off(struct tty *tty)
1892: {
1893: tty_region(tty, 0, tty->sy - 1);
1894: }
1895:
1.40 nicm 1896: /* Set region inside pane. */
1.208 nicm 1897: static void
1.196 nicm 1898: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
1899: u_int rlower)
1.1 nicm 1900: {
1.309 nicm 1901: tty_region(tty, ctx->yoff + rupper - ctx->oy,
1902: ctx->yoff + rlower - ctx->oy);
1.39 nicm 1903: }
1904:
1.40 nicm 1905: /* Set region at absolute position. */
1.214 nicm 1906: static void
1.40 nicm 1907: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 1908: {
1909: if (tty->rlower == rlower && tty->rupper == rupper)
1910: return;
1.1 nicm 1911: if (!tty_term_has(tty->term, TTYC_CSR))
1912: return;
1.39 nicm 1913:
1914: tty->rupper = rupper;
1915: tty->rlower = rlower;
1.55 nicm 1916:
1917: /*
1918: * Some terminals (such as PuTTY) do not correctly reset the cursor to
1919: * 0,0 if it is beyond the last column (they do not reset their wrap
1920: * flag so further output causes a line feed). As a workaround, do an
1921: * explicit move to 0 first.
1922: */
1923: if (tty->cx >= tty->sx)
1924: tty_cursor(tty, 0, tty->cy);
1.42 nicm 1925:
1.39 nicm 1926: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.249 nicm 1927: tty->cx = tty->cy = UINT_MAX;
1.212 nicm 1928: }
1929:
1.214 nicm 1930: /* Turn off margin. */
1931: void
1932: tty_margin_off(struct tty *tty)
1933: {
1934: tty_margin(tty, 0, tty->sx - 1);
1935: }
1936:
1.212 nicm 1937: /* Set margin inside pane. */
1938: static void
1939: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
1940: {
1.309 nicm 1941: tty_margin(tty, ctx->xoff - ctx->ox,
1942: ctx->xoff + ctx->wp->sx - 1 - ctx->ox);
1.212 nicm 1943: }
1944:
1945: /* Set margin at absolute position. */
1.214 nicm 1946: static void
1.212 nicm 1947: tty_margin(struct tty *tty, u_int rleft, u_int rright)
1948: {
1949: char s[64];
1950:
1951: if (!tty_use_margin(tty))
1952: return;
1953: if (tty->rleft == rleft && tty->rright == rright)
1954: return;
1955:
1.232 nicm 1956: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.231 nicm 1957:
1.212 nicm 1958: tty->rleft = rleft;
1959: tty->rright = rright;
1960:
1.232 nicm 1961: if (rleft == 0 && rright == tty->sx - 1)
1962: snprintf(s, sizeof s, "\033[s");
1963: else
1964: snprintf(s, sizeof s, "\033[%u;%us", rleft + 1, rright + 1);
1.212 nicm 1965: tty_puts(tty, s);
1.249 nicm 1966: tty->cx = tty->cy = UINT_MAX;
1.253 nicm 1967: }
1968:
1969: /*
1970: * Move the cursor, unless it would wrap itself when the next character is
1971: * printed.
1972: */
1973: static void
1974: tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
1975: u_int cx, u_int cy)
1976: {
1.268 nicm 1977: if (!ctx->wrapped ||
1978: !tty_pane_full_width(tty, ctx) ||
1.253 nicm 1979: (tty->term->flags & TERM_EARLYWRAP) ||
1980: ctx->xoff + cx != 0 ||
1981: ctx->yoff + cy != tty->cy + 1 ||
1.254 nicm 1982: tty->cx < tty->sx ||
1983: tty->cy == tty->rlower)
1.253 nicm 1984: tty_cursor_pane(tty, ctx, cx, cy);
1985: else
1986: log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
1.1 nicm 1987: }
1988:
1.42 nicm 1989: /* Move cursor inside pane. */
1.208 nicm 1990: static void
1.41 nicm 1991: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1992: {
1.309 nicm 1993: tty_cursor(tty, ctx->xoff + cx - ctx->ox, ctx->yoff + cy - ctx->oy);
1.41 nicm 1994: }
1995:
1.42 nicm 1996: /* Move cursor to absolute position. */
1.41 nicm 1997: void
1998: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 1999: {
1.42 nicm 2000: struct tty_term *term = tty->term;
2001: u_int thisx, thisy;
2002: int change;
1.77 nicm 2003:
1.42 nicm 2004: if (cx > tty->sx - 1)
2005: cx = tty->sx - 1;
2006:
2007: thisx = tty->cx;
2008: thisy = tty->cy;
2009:
2010: /* No change. */
2011: if (cx == thisx && cy == thisy)
2012: return;
2013:
1.43 nicm 2014: /* Very end of the line, just use absolute movement. */
2015: if (thisx > tty->sx - 1)
2016: goto absolute;
2017:
1.42 nicm 2018: /* Move to home position (0, 0). */
2019: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
2020: tty_putcode(tty, TTYC_HOME);
2021: goto out;
2022: }
2023:
2024: /* Zero on the next line. */
1.293 nicm 2025: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
2026: (!tty_use_margin(tty) || tty->rleft == 0)) {
1.1 nicm 2027: tty_putc(tty, '\r');
1.42 nicm 2028: tty_putc(tty, '\n');
2029: goto out;
2030: }
2031:
1.45 nicm 2032: /* Moving column or row. */
1.42 nicm 2033: if (cy == thisy) {
1.45 nicm 2034: /*
2035: * Moving column only, row staying the same.
2036: */
2037:
1.42 nicm 2038: /* To left edge. */
1.294 nicm 2039: if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
1.42 nicm 2040: tty_putc(tty, '\r');
2041: goto out;
2042: }
2043:
2044: /* One to the left. */
2045: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
2046: tty_putcode(tty, TTYC_CUB1);
2047: goto out;
2048: }
2049:
2050: /* One to the right. */
2051: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
2052: tty_putcode(tty, TTYC_CUF1);
2053: goto out;
2054: }
2055:
2056: /* Calculate difference. */
2057: change = thisx - cx; /* +ve left, -ve right */
2058:
2059: /*
2060: * Use HPA if change is larger than absolute, otherwise move
2061: * the cursor with CUB/CUF.
2062: */
1.87 nicm 2063: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 2064: tty_putcode1(tty, TTYC_HPA, cx);
2065: goto out;
2066: } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1.202 nicm 2067: if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
2068: tty_putcode(tty, TTYC_CUB1);
2069: tty_putcode(tty, TTYC_CUB1);
2070: goto out;
2071: }
1.42 nicm 2072: tty_putcode1(tty, TTYC_CUB, change);
2073: goto out;
2074: } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
2075: tty_putcode1(tty, TTYC_CUF, -change);
2076: goto out;
2077: }
1.45 nicm 2078: } else if (cx == thisx) {
2079: /*
2080: * Moving row only, column staying the same.
2081: */
1.42 nicm 2082:
2083: /* One above. */
1.77 nicm 2084: if (thisy != tty->rupper &&
1.42 nicm 2085: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
2086: tty_putcode(tty, TTYC_CUU1);
2087: goto out;
2088: }
2089:
2090: /* One below. */
1.49 nicm 2091: if (thisy != tty->rlower &&
1.42 nicm 2092: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
2093: tty_putcode(tty, TTYC_CUD1);
2094: goto out;
2095: }
2096:
2097: /* Calculate difference. */
2098: change = thisy - cy; /* +ve up, -ve down */
2099:
2100: /*
1.77 nicm 2101: * Try to use VPA if change is larger than absolute or if this
2102: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 2103: */
1.87 nicm 2104: if ((u_int) abs(change) > cy ||
1.42 nicm 2105: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 2106: (change > 0 && cy - change < tty->rupper)) {
2107: if (tty_term_has(term, TTYC_VPA)) {
2108: tty_putcode1(tty, TTYC_VPA, cy);
2109: goto out;
2110: }
1.42 nicm 2111: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
2112: tty_putcode1(tty, TTYC_CUU, change);
2113: goto out;
2114: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
2115: tty_putcode1(tty, TTYC_CUD, -change);
2116: goto out;
2117: }
2118: }
2119:
1.43 nicm 2120: absolute:
1.42 nicm 2121: /* Absolute movement. */
2122: tty_putcode2(tty, TTYC_CUP, cy, cx);
2123:
2124: out:
2125: tty->cx = cx;
2126: tty->cy = cy;
1.1 nicm 2127: }
2128:
2129: void
1.176 nicm 2130: tty_attributes(struct tty *tty, const struct grid_cell *gc,
1.317 nicm 2131: struct window_pane *wp)
1.1 nicm 2132: {
1.63 nicm 2133: struct grid_cell *tc = &tty->cell, gc2;
1.255 nicm 2134: int changed;
1.61 nicm 2135:
1.228 nicm 2136: /* Ignore cell if it is the same as the last one. */
2137: if (wp != NULL &&
2138: (int)wp->id == tty->last_wp &&
1.326 nicm 2139: ~(wp->flags & PANE_STYLECHANGED) &&
1.228 nicm 2140: gc->attr == tty->last_cell.attr &&
2141: gc->fg == tty->last_cell.fg &&
1.328 ! nicm 2142: gc->bg == tty->last_cell.bg &&
! 2143: gc->us == tty->last_cell.us)
1.228 nicm 2144: return;
2145: tty->last_wp = (wp != NULL ? (int)wp->id : -1);
2146: memcpy(&tty->last_cell, gc, sizeof tty->last_cell);
2147:
2148: /* Copy cell and update default colours. */
1.85 nicm 2149: memcpy(&gc2, gc, sizeof gc2);
1.203 nicm 2150: if (wp != NULL)
2151: tty_default_colours(&gc2, wp);
1.63 nicm 2152:
1.18 nicm 2153: /*
2154: * If no setab, try to use the reverse attribute as a best-effort for a
2155: * non-default background. This is a bit of a hack but it doesn't do
2156: * any serious harm and makes a couple of applications happier.
2157: */
2158: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 2159: if (gc2.attr & GRID_ATTR_REVERSE) {
1.310 nicm 2160: if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
1.64 nicm 2161: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 2162: } else {
1.310 nicm 2163: if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
1.64 nicm 2164: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 2165: }
2166: }
1.1 nicm 2167:
1.85 nicm 2168: /* Fix up the colours if necessary. */
1.219 nicm 2169: tty_check_fg(tty, wp, &gc2);
2170: tty_check_bg(tty, wp, &gc2);
1.328 ! nicm 2171: tty_check_us(tty, wp, &gc2);
1.85 nicm 2172:
1.328 ! nicm 2173: /*
! 2174: * If any bits are being cleared or the underline colour is now default,
! 2175: * reset everything.
! 2176: */
! 2177: if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
1.64 nicm 2178: tty_reset(tty);
2179:
2180: /*
2181: * Set the colours. This may call tty_reset() (so it comes next) and
1.328 ! nicm 2182: * may add to (NOT remove) the desired attributes.
1.64 nicm 2183: */
1.85 nicm 2184: tty_colours(tty, &gc2);
1.64 nicm 2185:
1.1 nicm 2186: /* Filter out attribute bits already set. */
1.85 nicm 2187: changed = gc2.attr & ~tc->attr;
2188: tc->attr = gc2.attr;
1.1 nicm 2189:
2190: /* Set the attributes. */
2191: if (changed & GRID_ATTR_BRIGHT)
2192: tty_putcode(tty, TTYC_BOLD);
2193: if (changed & GRID_ATTR_DIM)
2194: tty_putcode(tty, TTYC_DIM);
1.180 nicm 2195: if (changed & GRID_ATTR_ITALICS)
2196: tty_set_italics(tty);
1.308 nicm 2197: if (changed & GRID_ATTR_ALL_UNDERSCORE) {
2198: if ((changed & GRID_ATTR_UNDERSCORE) ||
2199: !tty_term_has(tty->term, TTYC_SMULX))
2200: tty_putcode(tty, TTYC_SMUL);
2201: else if (changed & GRID_ATTR_UNDERSCORE_2)
2202: tty_putcode1(tty, TTYC_SMULX, 2);
2203: else if (changed & GRID_ATTR_UNDERSCORE_3)
2204: tty_putcode1(tty, TTYC_SMULX, 3);
2205: else if (changed & GRID_ATTR_UNDERSCORE_4)
2206: tty_putcode1(tty, TTYC_SMULX, 4);
2207: else if (changed & GRID_ATTR_UNDERSCORE_5)
2208: tty_putcode1(tty, TTYC_SMULX, 5);
2209: }
1.1 nicm 2210: if (changed & GRID_ATTR_BLINK)
2211: tty_putcode(tty, TTYC_BLINK);
2212: if (changed & GRID_ATTR_REVERSE) {
2213: if (tty_term_has(tty->term, TTYC_REV))
2214: tty_putcode(tty, TTYC_REV);
2215: else if (tty_term_has(tty->term, TTYC_SMSO))
2216: tty_putcode(tty, TTYC_SMSO);
2217: }
2218: if (changed & GRID_ATTR_HIDDEN)
2219: tty_putcode(tty, TTYC_INVIS);
1.255 nicm 2220: if (changed & GRID_ATTR_STRIKETHROUGH)
2221: tty_putcode(tty, TTYC_SMXX);
1.324 nicm 2222: if (changed & GRID_ATTR_OVERLINE)
2223: tty_putcode(tty, TTYC_SMOL);
1.285 nicm 2224: if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.1 nicm 2225: tty_putcode(tty, TTYC_SMACS);
2226: }
2227:
1.207 nicm 2228: static void
1.85 nicm 2229: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2230: {
1.61 nicm 2231: struct grid_cell *tc = &tty->cell;
1.204 nicm 2232: int have_ax;
1.61 nicm 2233:
2234: /* No changes? Nothing is necessary. */
1.328 ! nicm 2235: if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
1.63 nicm 2236: return;
1.1 nicm 2237:
1.61 nicm 2238: /*
2239: * Is either the default colour? This is handled specially because the
2240: * best solution might be to reset both colours to default, in which
2241: * case if only one is default need to fall onward to set the other
2242: * colour.
2243: */
1.310 nicm 2244: if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
1.61 nicm 2245: /*
2246: * If don't have AX but do have op, send sgr0 (op can't
2247: * actually be used because it is sometimes the same as sgr0
2248: * and sometimes isn't). This resets both colours to default.
2249: *
2250: * Otherwise, try to set the default colour only as needed.
2251: */
1.174 nicm 2252: have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61 nicm 2253: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
2254: tty_reset(tty);
2255: else {
1.310 nicm 2256: if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
1.61 nicm 2257: if (have_ax)
2258: tty_puts(tty, "\033[39m");
1.204 nicm 2259: else if (tc->fg != 7)
1.61 nicm 2260: tty_putcode1(tty, TTYC_SETAF, 7);
1.310 nicm 2261: tc->fg = gc->fg;
1.61 nicm 2262: }
1.310 nicm 2263: if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
1.77 nicm 2264: if (have_ax)
1.61 nicm 2265: tty_puts(tty, "\033[49m");
1.204 nicm 2266: else if (tc->bg != 0)
1.61 nicm 2267: tty_putcode1(tty, TTYC_SETAB, 0);
1.312 nicm 2268: tc->bg = gc->bg;
1.61 nicm 2269: }
2270: }
2271: }
1.1 nicm 2272:
1.61 nicm 2273: /* Set the foreground colour. */
1.310 nicm 2274: if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
1.85 nicm 2275: tty_colours_fg(tty, gc);
1.1 nicm 2276:
1.61 nicm 2277: /*
2278: * Set the background colour. This must come after the foreground as
2279: * tty_colour_fg() can call tty_reset().
2280: */
1.310 nicm 2281: if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
1.73 nicm 2282: tty_colours_bg(tty, gc);
1.328 ! nicm 2283:
! 2284: /* Set the underscore color. */
! 2285: if (gc->us != tc->us)
! 2286: tty_colours_us(tty, gc);
1.1 nicm 2287: }
2288:
1.219 nicm 2289: static void
1.317 nicm 2290: tty_check_fg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
1.85 nicm 2291: {
1.204 nicm 2292: u_char r, g, b;
2293: u_int colours;
1.223 nicm 2294: int c;
1.85 nicm 2295:
1.288 nicm 2296: /*
2297: * Perform substitution if this pane has a palette. If the bright
2298: * attribute is set, use the bright entry in the palette by changing to
2299: * the aixterm colour.
2300: */
2301: if (~gc->flags & GRID_FLAG_NOPALETTE) {
2302: c = gc->fg;
1.289 nicm 2303: if (c < 8 && gc->attr & GRID_ATTR_BRIGHT)
1.288 nicm 2304: c += 90;
2305: if ((c = window_pane_get_palette(wp, c)) != -1)
2306: gc->fg = c;
2307: }
1.219 nicm 2308:
1.199 nicm 2309: /* Is this a 24-bit colour? */
1.204 nicm 2310: if (gc->fg & COLOUR_FLAG_RGB) {
1.199 nicm 2311: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.286 nicm 2312: if (!tty_term_has(tty->term, TTYC_SETRGBF)) {
1.204 nicm 2313: colour_split_rgb(gc->fg, &r, &g, &b);
2314: gc->fg = colour_find_rgb(r, g, b);
2315: } else
1.200 nicm 2316: return;
1.199 nicm 2317: }
1.224 nicm 2318:
2319: /* How many colours does this terminal have? */
2320: if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
2321: colours = 256;
2322: else
2323: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 2324:
1.85 nicm 2325: /* Is this a 256-colour colour? */
1.204 nicm 2326: if (gc->fg & COLOUR_FLAG_256) {
1.85 nicm 2327: /* And not a 256 colour mode? */
1.224 nicm 2328: if (colours != 256) {
1.85 nicm 2329: gc->fg = colour_256to16(gc->fg);
2330: if (gc->fg & 8) {
2331: gc->fg &= 7;
1.175 nicm 2332: if (colours >= 16)
2333: gc->fg += 90;
2334: else
2335: gc->attr |= GRID_ATTR_BRIGHT;
1.85 nicm 2336: } else
2337: gc->attr &= ~GRID_ATTR_BRIGHT;
2338: }
2339: return;
2340: }
2341:
2342: /* Is this an aixterm colour? */
2343: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
2344: gc->fg -= 90;
2345: gc->attr |= GRID_ATTR_BRIGHT;
2346: }
2347: }
2348:
1.219 nicm 2349: static void
1.317 nicm 2350: tty_check_bg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
1.85 nicm 2351: {
1.204 nicm 2352: u_char r, g, b;
2353: u_int colours;
1.223 nicm 2354: int c;
1.85 nicm 2355:
1.288 nicm 2356: /* Perform substitution if this pane has a palette. */
2357: if (~gc->flags & GRID_FLAG_NOPALETTE) {
2358: if ((c = window_pane_get_palette(wp, gc->bg)) != -1)
2359: gc->bg = c;
2360: }
1.219 nicm 2361:
1.199 nicm 2362: /* Is this a 24-bit colour? */
1.204 nicm 2363: if (gc->bg & COLOUR_FLAG_RGB) {
1.199 nicm 2364: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.286 nicm 2365: if (!tty_term_has(tty->term, TTYC_SETRGBB)) {
1.204 nicm 2366: colour_split_rgb(gc->bg, &r, &g, &b);
2367: gc->bg = colour_find_rgb(r, g, b);
2368: } else
1.200 nicm 2369: return;
1.199 nicm 2370: }
1.224 nicm 2371:
2372: /* How many colours does this terminal have? */
2373: if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
2374: colours = 256;
2375: else
2376: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 2377:
1.85 nicm 2378: /* Is this a 256-colour colour? */
1.204 nicm 2379: if (gc->bg & COLOUR_FLAG_256) {
1.85 nicm 2380: /*
2381: * And not a 256 colour mode? Translate to 16-colour
2382: * palette. Bold background doesn't exist portably, so just
2383: * discard the bold bit if set.
2384: */
1.224 nicm 2385: if (colours != 256) {
1.85 nicm 2386: gc->bg = colour_256to16(gc->bg);
1.175 nicm 2387: if (gc->bg & 8) {
1.85 nicm 2388: gc->bg &= 7;
1.175 nicm 2389: if (colours >= 16)
1.322 nicm 2390: gc->bg += 90;
1.175 nicm 2391: }
1.85 nicm 2392: }
2393: return;
2394: }
2395:
2396: /* Is this an aixterm colour? */
1.175 nicm 2397: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85 nicm 2398: gc->bg -= 90;
2399: }
2400:
1.207 nicm 2401: static void
1.328 ! nicm 2402: tty_check_us(__unused struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
! 2403: {
! 2404: int c;
! 2405:
! 2406: /* Perform substitution if this pane has a palette. */
! 2407: if (~gc->flags & GRID_FLAG_NOPALETTE) {
! 2408: if ((c = window_pane_get_palette(wp, gc->us)) != -1)
! 2409: gc->us = c;
! 2410: }
! 2411:
! 2412: /* Underscore colour is set as RGB so convert a 256 colour to RGB. */
! 2413: if (gc->us & COLOUR_FLAG_256)
! 2414: gc->us = colour_256toRGB (gc->us);
! 2415: }
! 2416:
! 2417: static void
1.85 nicm 2418: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2419: {
1.61 nicm 2420: struct grid_cell *tc = &tty->cell;
1.79 nicm 2421: char s[32];
1.1 nicm 2422:
1.204 nicm 2423: /* Is this a 24-bit or 256-colour colour? */
1.302 nicm 2424: if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
1.204 nicm 2425: if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61 nicm 2426: goto save_fg;
1.199 nicm 2427: /* Should not get here, already converted in tty_check_fg. */
1.85 nicm 2428: return;
1.1 nicm 2429: }
2430:
1.79 nicm 2431: /* Is this an aixterm bright colour? */
1.204 nicm 2432: if (gc->fg >= 90 && gc->fg <= 97) {
1.323 nicm 2433: if (tty->term_flags & TERM_256COLOURS) {
2434: xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
2435: tty_puts(tty, s);
2436: } else
2437: tty_putcode1(tty, TTYC_SETAF, gc->fg - 90 + 8);
1.85 nicm 2438: goto save_fg;
1.79 nicm 2439: }
2440:
1.61 nicm 2441: /* Otherwise set the foreground colour. */
1.204 nicm 2442: tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61 nicm 2443:
1.77 nicm 2444: save_fg:
1.61 nicm 2445: /* Save the new values in the terminal current cell. */
1.204 nicm 2446: tc->fg = gc->fg;
1.1 nicm 2447: }
2448:
1.207 nicm 2449: static void
1.73 nicm 2450: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2451: {
1.61 nicm 2452: struct grid_cell *tc = &tty->cell;
1.79 nicm 2453: char s[32];
1.1 nicm 2454:
1.204 nicm 2455: /* Is this a 24-bit or 256-colour colour? */
1.302 nicm 2456: if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
1.204 nicm 2457: if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61 nicm 2458: goto save_bg;
1.199 nicm 2459: /* Should not get here, already converted in tty_check_bg. */
1.85 nicm 2460: return;
1.79 nicm 2461: }
2462:
2463: /* Is this an aixterm bright colour? */
1.204 nicm 2464: if (gc->bg >= 90 && gc->bg <= 97) {
1.323 nicm 2465: if (tty->term_flags & TERM_256COLOURS) {
2466: xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
2467: tty_puts(tty, s);
2468: } else
2469: tty_putcode1(tty, TTYC_SETAB, gc->bg - 90 + 8);
1.175 nicm 2470: goto save_bg;
1.1 nicm 2471: }
2472:
1.61 nicm 2473: /* Otherwise set the background colour. */
1.204 nicm 2474: tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61 nicm 2475:
2476: save_bg:
2477: /* Save the new values in the terminal current cell. */
1.204 nicm 2478: tc->bg = gc->bg;
1.328 ! nicm 2479: }
! 2480:
! 2481: static void
! 2482: tty_colours_us(struct tty *tty, const struct grid_cell *gc)
! 2483: {
! 2484: struct grid_cell *tc = &tty->cell;
! 2485: u_int c;
! 2486: u_char r, g, b;
! 2487:
! 2488: /* Must be an RGB colour - this should never happen. */
! 2489: if (~gc->us & COLOUR_FLAG_RGB)
! 2490: return;
! 2491:
! 2492: /*
! 2493: * Setulc follows the ncurses(3) one argument "direct colour"
! 2494: * capability format. Calculate the colour value.
! 2495: */
! 2496: colour_split_rgb(gc->us, &r, &g, &b);
! 2497: c = (65536 * r) + (256 * g) + b;
! 2498:
! 2499: /* Write the colour. */
! 2500: tty_putcode1(tty, TTYC_SETULC, c);
! 2501:
! 2502: /* Save the new values in the terminal current cell. */
! 2503: tc->us = gc->us;
1.61 nicm 2504: }
2505:
1.207 nicm 2506: static int
1.204 nicm 2507: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61 nicm 2508: {
1.204 nicm 2509: u_char r, g, b;
1.61 nicm 2510: char s[32];
2511:
1.204 nicm 2512: if (colour & COLOUR_FLAG_256) {
2513: /*
1.300 nicm 2514: * If the user has specified -2 to the client (meaning
2515: * TERM_256COLOURS is set), setaf and setab may not work (or
2516: * they may not want to use them), so send the usual sequence.
2517: *
2518: * Also if RGB is set, setaf and setab do not support the 256
2519: * colour palette so use the sequences directly there too.
1.204 nicm 2520: */
1.300 nicm 2521: if ((tty->term_flags & TERM_256COLOURS) ||
2522: tty_term_has(tty->term, TTYC_RGB))
1.204 nicm 2523: goto fallback_256;
1.189 nicm 2524:
1.204 nicm 2525: /*
2526: * If the terminfo entry has 256 colours and setaf and setab
2527: * exist, assume that they work correctly.
2528: */
2529: if (tty->term->flags & TERM_256COLOURS) {
2530: if (*type == '3') {
2531: if (!tty_term_has(tty->term, TTYC_SETAF))
2532: goto fallback_256;
2533: tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
2534: } else {
2535: if (!tty_term_has(tty->term, TTYC_SETAB))
2536: goto fallback_256;
2537: tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
2538: }
2539: return (0);
1.188 nicm 2540: }
1.204 nicm 2541: goto fallback_256;
2542: }
2543:
2544: if (colour & COLOUR_FLAG_RGB) {
1.286 nicm 2545: if (*type == '3') {
2546: if (!tty_term_has(tty->term, TTYC_SETRGBF))
2547: return (-1);
2548: colour_split_rgb(colour & 0xffffff, &r, &g, &b);
2549: tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
2550: } else {
2551: if (!tty_term_has(tty->term, TTYC_SETRGBB))
2552: return (-1);
2553: colour_split_rgb(colour & 0xffffff, &r, &g, &b);
2554: tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
2555: }
1.165 nicm 2556: return (0);
2557: }
1.61 nicm 2558:
1.165 nicm 2559: return (-1);
1.188 nicm 2560:
1.204 nicm 2561: fallback_256:
2562: xsnprintf(s, sizeof s, "\033[%s;5;%dm", type, colour & 0xff);
1.300 nicm 2563: log_debug("%s: 256 colour fallback: %s", tty->client->name, s);
1.188 nicm 2564: tty_puts(tty, s);
2565: return (0);
1.176 nicm 2566: }
2567:
1.207 nicm 2568: static void
1.317 nicm 2569: tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
1.176 nicm 2570: {
1.326 nicm 2571: struct options *oo = wp->options;
2572: struct style *style, *active_style;
2573: int c;
2574:
2575: if (wp->flags & PANE_STYLECHANGED) {
2576: wp->flags &= ~PANE_STYLECHANGED;
2577:
2578: active_style = options_get_style(oo, "window-active-style");
2579: style = options_get_style(oo, "window-style");
2580:
2581: style_copy(&wp->cached_active_style, active_style);
2582: style_copy(&wp->cached_style, style);
1.203 nicm 2583: } else {
1.326 nicm 2584: active_style = &wp->cached_active_style;
2585: style = &wp->cached_style;
1.203 nicm 2586: }
1.176 nicm 2587:
1.204 nicm 2588: if (gc->fg == 8) {
1.326 nicm 2589: if (wp == wp->window->active && active_style->gc.fg != 8)
2590: gc->fg = active_style->gc.fg;
1.204 nicm 2591: else
1.326 nicm 2592: gc->fg = style->gc.fg;
1.219 nicm 2593:
1.310 nicm 2594: if (gc->fg != 8) {
2595: c = window_pane_get_palette(wp, gc->fg);
2596: if (c != -1)
2597: gc->fg = c;
2598: }
1.176 nicm 2599: }
2600:
1.204 nicm 2601: if (gc->bg == 8) {
1.326 nicm 2602: if (wp == wp->window->active && active_style->gc.bg != 8)
2603: gc->bg = active_style->gc.bg;
1.204 nicm 2604: else
1.326 nicm 2605: gc->bg = style->gc.bg;
1.219 nicm 2606:
1.310 nicm 2607: if (gc->bg != 8) {
2608: c = window_pane_get_palette(wp, gc->bg);
2609: if (c != -1)
2610: gc->bg = c;
2611: }
1.176 nicm 2612: }
1.210 nicm 2613: }
2614:
2615: static void
1.317 nicm 2616: tty_default_attributes(struct tty *tty, struct window_pane *wp, u_int bg)
1.210 nicm 2617: {
2618: static struct grid_cell gc;
2619:
2620: memcpy(&gc, &grid_default_cell, sizeof gc);
2621: gc.bg = bg;
2622: tty_attributes(tty, &gc, wp);
1.1 nicm 2623: }