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