Annotation of src/usr.bin/tmux/tty.c, Revision 1.436
1.436 ! nicm 1: /* $OpenBSD: tty.c,v 1.435 2023/09/15 15:49:05 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>
1.436 ! nicm 31: #include <time.h>
1.1 nicm 32: #include <unistd.h>
33:
34: #include "tmux.h"
35:
1.207 nicm 36: static int tty_log_fd = -1;
1.192 nicm 37:
1.207 nicm 38: static void tty_set_italics(struct tty *);
39: static int tty_try_colour(struct tty *, int, const char *);
1.410 nicm 40: static void tty_force_cursor_colour(struct tty *, int);
1.208 nicm 41: static void tty_cursor_pane(struct tty *, const struct tty_ctx *, u_int,
42: u_int);
1.253 nicm 43: static void tty_cursor_pane_unless_wrap(struct tty *,
44: const struct tty_ctx *, u_int, u_int);
1.242 nicm 45: static void tty_invalidate(struct tty *);
1.207 nicm 46: static void tty_colours(struct tty *, const struct grid_cell *);
1.399 nicm 47: static void tty_check_fg(struct tty *, struct colour_palette *,
48: struct grid_cell *);
49: static void tty_check_bg(struct tty *, struct colour_palette *,
50: struct grid_cell *);
51: static void tty_check_us(struct tty *, struct colour_palette *,
52: struct grid_cell *);
1.207 nicm 53: static void tty_colours_fg(struct tty *, const struct grid_cell *);
54: static void tty_colours_bg(struct tty *, const struct grid_cell *);
1.328 nicm 55: static void tty_colours_us(struct tty *, const struct grid_cell *);
1.207 nicm 56:
1.208 nicm 57: static void tty_region_pane(struct tty *, const struct tty_ctx *, u_int,
58: u_int);
1.214 nicm 59: static void tty_region(struct tty *, u_int, u_int);
1.212 nicm 60: static void tty_margin_pane(struct tty *, const struct tty_ctx *);
1.214 nicm 61: static void tty_margin(struct tty *, u_int, u_int);
1.207 nicm 62: static int tty_large_region(struct tty *, const struct tty_ctx *);
1.373 nicm 63: static int tty_fake_bce(const struct tty *, const struct grid_cell *,
64: 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.373 nicm 70: static void tty_default_attributes(struct tty *, const struct grid_cell *,
1.421 nicm 71: struct colour_palette *, u_int, struct hyperlinks *);
1.394 nicm 72: static int tty_check_overlay(struct tty *, u_int, u_int);
1.406 nicm 73: static void tty_check_overlay_range(struct tty *, u_int, u_int, u_int,
74: struct overlay_ranges *);
1.1 nicm 75:
1.212 nicm 76: #define tty_use_margin(tty) \
1.359 nicm 77: (tty->term->flags & TERM_DECSLRM)
1.372 nicm 78: #define tty_full_width(tty, ctx) \
79: ((ctx)->xoff == 0 && (ctx)->sx >= (tty)->sx)
1.132 nicm 80:
1.266 nicm 81: #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
82: #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
83: #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
84:
1.416 nicm 85: #define TTY_QUERY_TIMEOUT 5
1.434 nicm 86: #define TTY_REQUEST_LIMIT 30
1.416 nicm 87:
1.192 nicm 88: void
89: tty_create_log(void)
90: {
91: char name[64];
92:
93: xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
94:
95: tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
96: if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
97: fatal("fcntl failed");
98: }
99:
1.185 nicm 100: int
1.380 nicm 101: tty_init(struct tty *tty, struct client *c)
1.1 nicm 102: {
1.380 nicm 103: if (!isatty(c->fd))
1.185 nicm 104: return (-1);
105:
1.30 nicm 106: memset(tty, 0, sizeof *tty);
1.136 nicm 107: tty->client = c;
1.30 nicm 108:
1.391 nicm 109: tty->cstyle = SCREEN_CURSOR_DEFAULT;
1.410 nicm 110: tty->ccolour = -1;
1.426 nicm 111: tty->fg = tty->bg = -1;
1.30 nicm 112:
1.380 nicm 113: if (tcgetattr(c->fd, &tty->tio) != 0)
114: return (-1);
1.185 nicm 115: return (0);
1.31 nicm 116: }
117:
1.287 nicm 118: void
1.31 nicm 119: tty_resize(struct tty *tty)
120: {
1.258 nicm 121: struct client *c = tty->client;
122: struct winsize ws;
1.334 nicm 123: u_int sx, sy, xpixel, ypixel;
1.31 nicm 124:
1.380 nicm 125: if (ioctl(c->fd, TIOCGWINSZ, &ws) != -1) {
1.88 nicm 126: sx = ws.ws_col;
1.334 nicm 127: if (sx == 0) {
1.88 nicm 128: sx = 80;
1.334 nicm 129: xpixel = 0;
130: } else
131: xpixel = ws.ws_xpixel / sx;
1.88 nicm 132: sy = ws.ws_row;
1.334 nicm 133: if (sy == 0) {
1.88 nicm 134: sy = 24;
1.334 nicm 135: ypixel = 0;
136: } else
137: ypixel = ws.ws_ypixel / sy;
1.88 nicm 138: } else {
139: sx = 80;
140: sy = 24;
1.334 nicm 141: xpixel = 0;
142: ypixel = 0;
1.31 nicm 143: }
1.334 nicm 144: log_debug("%s: %s now %ux%u (%ux%u)", __func__, c->name, sx, sy,
145: xpixel, ypixel);
146: tty_set_size(tty, sx, sy, xpixel, ypixel);
1.242 nicm 147: tty_invalidate(tty);
1.114 nicm 148: }
149:
1.287 nicm 150: void
1.334 nicm 151: tty_set_size(struct tty *tty, u_int sx, u_int sy, u_int xpixel, u_int ypixel)
1.212 nicm 152: {
1.114 nicm 153: tty->sx = sx;
154: tty->sy = sy;
1.334 nicm 155: tty->xpixel = xpixel;
156: tty->ypixel = ypixel;
1.1 nicm 157: }
158:
1.244 nicm 159: static void
160: tty_read_callback(__unused int fd, __unused short events, void *data)
161: {
162: struct tty *tty = data;
1.258 nicm 163: struct client *c = tty->client;
1.375 nicm 164: const char *name = c->name;
1.244 nicm 165: size_t size = EVBUFFER_LENGTH(tty->in);
166: int nread;
167:
1.380 nicm 168: nread = evbuffer_read(tty->in, c->fd, -1);
1.295 nicm 169: if (nread == 0 || nread == -1) {
1.375 nicm 170: if (nread == 0)
171: log_debug("%s: read closed", name);
172: else
173: log_debug("%s: read error: %s", name, strerror(errno));
1.290 nicm 174: event_del(&tty->event_in);
1.295 nicm 175: server_client_lost(tty->client);
1.244 nicm 176: return;
1.290 nicm 177: }
1.375 nicm 178: log_debug("%s: read %d bytes (already %zu)", name, nread, size);
1.244 nicm 179:
180: while (tty_keys_next(tty))
181: ;
182: }
183:
184: static void
1.266 nicm 185: tty_timer_callback(__unused int fd, __unused short events, void *data)
186: {
187: struct tty *tty = data;
188: struct client *c = tty->client;
189: struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
190:
191: log_debug("%s: %zu discarded", c->name, tty->discarded);
192:
1.305 nicm 193: c->flags |= CLIENT_ALLREDRAWFLAGS;
1.266 nicm 194: c->discarded += tty->discarded;
195:
196: if (tty->discarded < TTY_BLOCK_STOP(tty)) {
197: tty->flags &= ~TTY_BLOCK;
198: tty_invalidate(tty);
199: return;
200: }
201: tty->discarded = 0;
202: evtimer_add(&tty->timer, &tv);
203: }
204:
205: static int
206: tty_block_maybe(struct tty *tty)
207: {
208: struct client *c = tty->client;
209: size_t size = EVBUFFER_LENGTH(tty->out);
210: struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
211:
1.414 nicm 212: if (size == 0)
213: tty->flags &= ~TTY_NOBLOCK;
214: else if (tty->flags & TTY_NOBLOCK)
215: return (0);
216:
1.266 nicm 217: if (size < TTY_BLOCK_START(tty))
218: return (0);
219:
220: if (tty->flags & TTY_BLOCK)
221: return (1);
222: tty->flags |= TTY_BLOCK;
223:
224: log_debug("%s: can't keep up, %zu discarded", c->name, size);
225:
226: evbuffer_drain(tty->out, size);
227: c->discarded += size;
228:
229: tty->discarded = 0;
230: evtimer_add(&tty->timer, &tv);
231: return (1);
232: }
233:
234: static void
1.244 nicm 235: tty_write_callback(__unused int fd, __unused short events, void *data)
236: {
1.246 nicm 237: struct tty *tty = data;
1.258 nicm 238: struct client *c = tty->client;
1.246 nicm 239: size_t size = EVBUFFER_LENGTH(tty->out);
240: int nwrite;
1.244 nicm 241:
1.380 nicm 242: nwrite = evbuffer_write(tty->out, c->fd);
1.244 nicm 243: if (nwrite == -1)
244: return;
1.258 nicm 245: log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size);
1.244 nicm 246:
1.270 nicm 247: if (c->redraw > 0) {
248: if ((size_t)nwrite >= c->redraw)
249: c->redraw = 0;
250: else
251: c->redraw -= nwrite;
252: log_debug("%s: waiting for redraw, %zu bytes left", c->name,
253: c->redraw);
254: } else if (tty_block_maybe(tty))
1.266 nicm 255: return;
256:
1.247 nicm 257: if (EVBUFFER_LENGTH(tty->out) != 0)
258: event_add(&tty->event_out, NULL);
1.244 nicm 259: }
260:
1.1 nicm 261: int
1.166 nicm 262: tty_open(struct tty *tty, char **cause)
1.1 nicm 263: {
1.359 nicm 264: struct client *c = tty->client;
265:
1.389 nicm 266: tty->term = tty_term_create(tty, c->term_name, c->term_caps,
267: c->term_ncaps, &c->term_features, cause);
1.21 nicm 268: if (tty->term == NULL) {
269: tty_close(tty);
270: return (-1);
271: }
272: tty->flags |= TTY_OPENED;
1.1 nicm 273:
1.266 nicm 274: tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
1.244 nicm 275:
1.380 nicm 276: event_set(&tty->event_in, c->fd, EV_PERSIST|EV_READ,
1.244 nicm 277: tty_read_callback, tty);
278: tty->in = evbuffer_new();
1.311 nicm 279: if (tty->in == NULL)
280: fatal("out of memory");
1.1 nicm 281:
1.380 nicm 282: event_set(&tty->event_out, c->fd, EV_WRITE, tty_write_callback, tty);
1.244 nicm 283: tty->out = evbuffer_new();
1.311 nicm 284: if (tty->out == NULL)
285: fatal("out of memory");
1.1 nicm 286:
1.266 nicm 287: evtimer_set(&tty->timer, tty_timer_callback, tty);
288:
1.1 nicm 289: tty_start_tty(tty);
1.148 nicm 290: tty_keys_build(tty);
1.1 nicm 291:
292: return (0);
293: }
294:
1.340 nicm 295: static void
296: tty_start_timer_callback(__unused int fd, __unused short events, void *data)
297: {
298: struct tty *tty = data;
299: struct client *c = tty->client;
300:
301: log_debug("%s: start timer fired", c->name);
1.425 nicm 302: if ((tty->flags & (TTY_HAVEDA|TTY_HAVEDA2|TTY_HAVEXDA)) == 0)
1.378 nicm 303: tty_update_features(tty);
1.426 nicm 304: tty->flags |= TTY_ALL_REQUEST_FLAGS;
1.340 nicm 305: }
306:
1.244 nicm 307: void
308: tty_start_tty(struct tty *tty)
1.1 nicm 309: {
1.285 nicm 310: struct client *c = tty->client;
311: struct termios tio;
1.416 nicm 312: struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT };
1.33 nicm 313:
1.380 nicm 314: setblocking(c->fd, 0);
315: event_add(&tty->event_in, NULL);
316:
317: memcpy(&tio, &tty->tio, sizeof tio);
318: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
319: tio.c_iflag |= IGNBRK;
320: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
321: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|ECHOPRT|
322: ECHOKE|ISIG);
323: tio.c_cc[VMIN] = 1;
324: tio.c_cc[VTIME] = 0;
325: if (tcsetattr(c->fd, TCSANOW, &tio) == 0)
1.404 nicm 326: tcflush(c->fd, TCOFLUSH);
1.1 nicm 327:
1.10 nicm 328: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 329:
1.252 nicm 330: tty_putcode(tty, TTYC_SMKX);
1.285 nicm 331: tty_putcode(tty, TTYC_CLEAR);
332:
333: if (tty_acs_needed(tty)) {
334: log_debug("%s: using capabilities for ACS", c->name);
1.90 nicm 335: tty_putcode(tty, TTYC_ENACS);
1.285 nicm 336: } else
337: log_debug("%s: using UTF-8 for ACS", c->name);
1.1 nicm 338:
339: tty_putcode(tty, TTYC_CNORM);
1.343 nicm 340: if (tty_term_has(tty->term, TTYC_KMOUS)) {
341: tty_puts(tty, "\033[?1000l\033[?1002l\033[?1003l");
342: tty_puts(tty, "\033[?1006l\033[?1005l");
343: }
1.427 nicm 344: if (tty_term_has(tty->term, TTYC_ENBP))
345: tty_putcode(tty, TTYC_ENBP);
1.121 nicm 346:
1.340 nicm 347: evtimer_set(&tty->start_timer, tty_start_timer_callback, tty);
348: evtimer_add(&tty->start_timer, &tv);
349:
1.20 nicm 350: tty->flags |= TTY_STARTED;
1.242 nicm 351: tty_invalidate(tty);
1.107 nicm 352:
1.410 nicm 353: if (tty->ccolour != -1)
354: tty_force_cursor_colour(tty, -1);
1.177 nicm 355:
356: tty->mouse_drag_flag = 0;
357: tty->mouse_drag_update = NULL;
358: tty->mouse_drag_release = NULL;
1.347 nicm 359: }
360:
361: void
362: tty_send_requests(struct tty *tty)
363: {
364: if (~tty->flags & TTY_STARTED)
365: return;
366:
1.369 nicm 367: if (tty->term->flags & TERM_VT100LIKE) {
1.425 nicm 368: if (~tty->term->flags & TTY_HAVEDA)
369: tty_puts(tty, "\033[c");
370: if (~tty->flags & TTY_HAVEDA2)
1.351 nicm 371: tty_puts(tty, "\033[>c");
1.364 nicm 372: if (~tty->flags & TTY_HAVEXDA)
373: tty_puts(tty, "\033[>q");
1.434 nicm 374: tty_puts(tty, "\033]10;?\033\\");
375: tty_puts(tty, "\033]11;?\033\\");
1.347 nicm 376: } else
1.426 nicm 377: tty->flags |= TTY_ALL_REQUEST_FLAGS;
1.436 ! nicm 378: tty->last_requests = time(NULL);
1.434 nicm 379: }
380:
381: void
382: tty_repeat_requests(struct tty *tty)
383: {
1.436 ! nicm 384: time_t t = time(NULL);
1.434 nicm 385:
386: if (~tty->flags & TTY_STARTED)
387: return;
388:
389: if (t - tty->last_requests <= TTY_REQUEST_LIMIT)
390: return;
391: tty->last_requests = t;
392:
393: if (tty->term->flags & TERM_VT100LIKE) {
394: tty_puts(tty, "\033]10;?\033\\");
395: tty_puts(tty, "\033]11;?\033\\");
396: }
1.128 nicm 397: }
398:
399: void
1.1 nicm 400: tty_stop_tty(struct tty *tty)
401: {
1.380 nicm 402: struct client *c = tty->client;
403: struct winsize ws;
1.1 nicm 404:
1.20 nicm 405: if (!(tty->flags & TTY_STARTED))
406: return;
407: tty->flags &= ~TTY_STARTED;
1.340 nicm 408:
409: evtimer_del(&tty->start_timer);
1.20 nicm 410:
1.266 nicm 411: event_del(&tty->timer);
412: tty->flags &= ~TTY_BLOCK;
413:
1.244 nicm 414: event_del(&tty->event_in);
415: event_del(&tty->event_out);
1.66 nicm 416:
1.1 nicm 417: /*
418: * Be flexible about error handling and try not kill the server just
419: * because the fd is invalid. Things like ssh -t can easily leave us
420: * with a dead tty.
421: */
1.380 nicm 422: if (ioctl(c->fd, TIOCGWINSZ, &ws) == -1)
1.1 nicm 423: return;
1.380 nicm 424: if (tcsetattr(c->fd, TCSANOW, &tty->tio) == -1)
1.1 nicm 425: return;
426:
1.430 nicm 427: tty_raw(tty, tty_term_string_ii(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.285 nicm 428: if (tty_acs_needed(tty))
1.90 nicm 429: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1 nicm 430: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
431: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
432: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.391 nicm 433: if (tty->cstyle != SCREEN_CURSOR_DEFAULT) {
1.160 nicm 434: if (tty_term_has(tty->term, TTYC_SE))
435: tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
1.391 nicm 436: else if (tty_term_has(tty->term, TTYC_SS))
1.430 nicm 437: tty_raw(tty, tty_term_string_i(tty->term, TTYC_SS, 0));
1.108 nicm 438: }
1.410 nicm 439: if (tty->ccolour != -1)
1.336 nicm 440: tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1 nicm 441:
442: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
1.343 nicm 443: if (tty_term_has(tty->term, TTYC_KMOUS)) {
444: tty_raw(tty, "\033[?1000l\033[?1002l\033[?1003l");
445: tty_raw(tty, "\033[?1006l\033[?1005l");
446: }
1.427 nicm 447: if (tty_term_has(tty->term, TTYC_DSBP))
448: tty_raw(tty, tty_term_string(tty->term, TTYC_DSBP));
1.151 nicm 449:
1.369 nicm 450: if (tty->term->flags & TERM_VT100LIKE)
1.352 nicm 451: tty_raw(tty, "\033[?7727l");
1.378 nicm 452: tty_raw(tty, tty_term_string(tty->term, TTYC_DSFCS));
453: tty_raw(tty, tty_term_string(tty->term, TTYC_DSEKS));
1.10 nicm 454:
1.212 nicm 455: if (tty_use_margin(tty))
1.365 nicm 456: tty_raw(tty, tty_term_string(tty->term, TTYC_DSMG));
1.10 nicm 457: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150 nicm 458:
1.380 nicm 459: setblocking(c->fd, 1);
1.1 nicm 460: }
461:
462: void
1.20 nicm 463: tty_close(struct tty *tty)
1.1 nicm 464: {
1.123 nicm 465: if (event_initialized(&tty->key_timer))
466: evtimer_del(&tty->key_timer);
1.20 nicm 467: tty_stop_tty(tty);
1.1 nicm 468:
1.21 nicm 469: if (tty->flags & TTY_OPENED) {
1.244 nicm 470: evbuffer_free(tty->in);
471: event_del(&tty->event_in);
472: evbuffer_free(tty->out);
473: event_del(&tty->event_out);
1.66 nicm 474:
1.21 nicm 475: tty_term_free(tty->term);
476: tty_keys_free(tty);
477:
478: tty->flags &= ~TTY_OPENED;
479: }
1.1 nicm 480: }
481:
482: void
1.20 nicm 483: tty_free(struct tty *tty)
1.1 nicm 484: {
1.20 nicm 485: tty_close(tty);
1.1 nicm 486: }
487:
488: void
1.359 nicm 489: tty_update_features(struct tty *tty)
1.212 nicm 490: {
1.359 nicm 491: struct client *c = tty->client;
492:
1.362 nicm 493: if (tty_apply_features(tty->term, c->term_features))
494: tty_term_apply_overrides(tty->term);
1.212 nicm 495:
496: if (tty_use_margin(tty))
1.365 nicm 497: tty_putcode(tty, TTYC_ENMG);
1.378 nicm 498: if (options_get_number(global_options, "extended-keys"))
499: tty_puts(tty, tty_term_string(tty->term, TTYC_ENEKS));
500: if (options_get_number(global_options, "focus-events"))
1.379 nicm 501: tty_puts(tty, tty_term_string(tty->term, TTYC_ENFCS));
1.378 nicm 502: if (tty->term->flags & TERM_VT100LIKE)
503: tty_puts(tty, "\033[?7727h");
1.429 nicm 504:
1.431 nicm 505: /*
506: * Features might have changed since the first draw during attach. For
507: * example, this happens when DA responses are received.
508: */
509: server_redraw_client(c);
510:
1.429 nicm 511: tty_invalidate(tty);
1.212 nicm 512: }
513:
514: void
1.1 nicm 515: tty_raw(struct tty *tty, const char *s)
516: {
1.380 nicm 517: struct client *c = tty->client;
518: ssize_t n, slen;
519: u_int i;
1.150 nicm 520:
521: slen = strlen(s);
522: for (i = 0; i < 5; i++) {
1.380 nicm 523: n = write(c->fd, s, slen);
1.150 nicm 524: if (n >= 0) {
525: s += n;
526: slen -= n;
527: if (slen == 0)
528: break;
529: } else if (n == -1 && errno != EAGAIN)
530: break;
531: usleep(100);
532: }
1.1 nicm 533: }
534:
535: void
536: tty_putcode(struct tty *tty, enum tty_code_code code)
537: {
538: tty_puts(tty, tty_term_string(tty->term, code));
539: }
540:
541: void
1.430 nicm 542: tty_putcode_i(struct tty *tty, enum tty_code_code code, int a)
1.1 nicm 543: {
544: if (a < 0)
545: return;
1.430 nicm 546: tty_puts(tty, tty_term_string_i(tty->term, code, a));
1.1 nicm 547: }
548:
549: void
1.430 nicm 550: tty_putcode_ii(struct tty *tty, enum tty_code_code code, int a, int b)
1.1 nicm 551: {
552: if (a < 0 || b < 0)
553: return;
1.430 nicm 554: tty_puts(tty, tty_term_string_ii(tty->term, code, a, b));
1.1 nicm 555: }
556:
557: void
1.430 nicm 558: tty_putcode_iii(struct tty *tty, enum tty_code_code code, int a, int b, int c)
1.286 nicm 559: {
560: if (a < 0 || b < 0 || c < 0)
561: return;
1.430 nicm 562: tty_puts(tty, tty_term_string_iii(tty->term, code, a, b, c));
1.286 nicm 563: }
564:
565: void
1.430 nicm 566: tty_putcode_s(struct tty *tty, enum tty_code_code code, const char *a)
1.107 nicm 567: {
568: if (a != NULL)
1.430 nicm 569: tty_puts(tty, tty_term_string_s(tty->term, code, a));
1.107 nicm 570: }
571:
572: void
1.430 nicm 573: tty_putcode_ss(struct tty *tty, enum tty_code_code code, const char *a,
574: const char *b)
1.106 nicm 575: {
576: if (a != NULL && b != NULL)
1.430 nicm 577: tty_puts(tty, tty_term_string_ss(tty->term, code, a, b));
1.106 nicm 578: }
579:
1.243 nicm 580: static void
581: tty_add(struct tty *tty, const char *buf, size_t len)
582: {
1.258 nicm 583: struct client *c = tty->client;
584:
1.266 nicm 585: if (tty->flags & TTY_BLOCK) {
586: tty->discarded += len;
587: return;
588: }
589:
1.244 nicm 590: evbuffer_add(tty->out, buf, len);
1.270 nicm 591: log_debug("%s: %.*s", c->name, (int)len, buf);
1.266 nicm 592: c->written += len;
1.243 nicm 593:
594: if (tty_log_fd != -1)
595: write(tty_log_fd, buf, len);
1.244 nicm 596: if (tty->flags & TTY_STARTED)
597: event_add(&tty->event_out, NULL);
1.243 nicm 598: }
599:
1.106 nicm 600: void
1.1 nicm 601: tty_puts(struct tty *tty, const char *s)
602: {
1.243 nicm 603: if (*s != '\0')
604: tty_add(tty, s, strlen(s));
1.1 nicm 605: }
606:
607: void
608: tty_putc(struct tty *tty, u_char ch)
609: {
1.90 nicm 610: const char *acs;
1.1 nicm 611:
1.382 nicm 612: if ((tty->term->flags & TERM_NOAM) &&
1.325 nicm 613: ch >= 0x20 && ch != 0x7f &&
614: tty->cy == tty->sy - 1 &&
615: tty->cx + 1 >= tty->sx)
616: return;
617:
1.90 nicm 618: if (tty->cell.attr & GRID_ATTR_CHARSET) {
619: acs = tty_acs_get(tty, ch);
1.243 nicm 620: if (acs != NULL)
621: tty_add(tty, acs, strlen(acs));
622: else
623: tty_add(tty, &ch, 1);
624: } else
625: tty_add(tty, &ch, 1);
1.1 nicm 626:
627: if (ch >= 0x20 && ch != 0x7f) {
1.211 nicm 628: if (tty->cx >= tty->sx) {
1.46 nicm 629: tty->cx = 1;
630: if (tty->cy != tty->rlower)
631: tty->cy++;
1.211 nicm 632:
633: /*
1.382 nicm 634: * On !am terminals, force the cursor position to where
635: * we think it should be after a line wrap - this means
636: * it works on sensible terminals as well.
1.211 nicm 637: */
1.382 nicm 638: if (tty->term->flags & TERM_NOAM)
1.430 nicm 639: tty_putcode_ii(tty, TTYC_CUP, tty->cy, tty->cx);
1.1 nicm 640: } else
641: tty->cx++;
642: }
643: }
644:
645: void
1.147 nicm 646: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5 nicm 647: {
1.382 nicm 648: if ((tty->term->flags & TERM_NOAM) &&
1.325 nicm 649: tty->cy == tty->sy - 1 &&
650: tty->cx + len >= tty->sx)
651: len = tty->sx - tty->cx - 1;
652:
1.243 nicm 653: tty_add(tty, buf, len);
1.268 nicm 654: if (tty->cx + width > tty->sx) {
655: tty->cx = (tty->cx + width) - tty->sx;
656: if (tty->cx <= tty->sx)
657: tty->cy++;
658: else
659: tty->cx = tty->cy = UINT_MAX;
660: } else
1.254 nicm 661: tty->cx += width;
1.5 nicm 662: }
663:
1.207 nicm 664: static void
1.180 nicm 665: tty_set_italics(struct tty *tty)
666: {
667: const char *s;
668:
669: if (tty_term_has(tty->term, TTYC_SITM)) {
1.191 nicm 670: s = options_get_string(global_options, "default-terminal");
1.180 nicm 671: if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
672: tty_putcode(tty, TTYC_SITM);
673: return;
674: }
675: }
676: tty_putcode(tty, TTYC_SMSO);
677: }
678:
679: void
1.1 nicm 680: tty_set_title(struct tty *tty, const char *title)
681: {
1.105 nicm 682: if (!tty_term_has(tty->term, TTYC_TSL) ||
683: !tty_term_has(tty->term, TTYC_FSL))
1.1 nicm 684: return;
685:
1.105 nicm 686: tty_putcode(tty, TTYC_TSL);
1.418 nicm 687: tty_puts(tty, title);
688: tty_putcode(tty, TTYC_FSL);
689: }
690:
691: void
692: tty_set_path(struct tty *tty, const char *title)
693: {
694: if (!tty_term_has(tty->term, TTYC_SWD) ||
695: !tty_term_has(tty->term, TTYC_FSL))
696: return;
697:
698: tty_putcode(tty, TTYC_SWD);
1.1 nicm 699: tty_puts(tty, title);
1.105 nicm 700: tty_putcode(tty, TTYC_FSL);
1.1 nicm 701: }
702:
1.208 nicm 703: static void
1.410 nicm 704: tty_force_cursor_colour(struct tty *tty, int c)
1.107 nicm 705: {
1.410 nicm 706: u_char r, g, b;
1.420 nicm 707: char s[13];
1.410 nicm 708:
709: if (c != -1)
710: c = colour_force_rgb(c);
711: if (c == tty->ccolour)
712: return;
713: if (c == -1)
1.107 nicm 714: tty_putcode(tty, TTYC_CR);
1.410 nicm 715: else {
716: colour_split_rgb(c, &r, &g, &b);
717: xsnprintf(s, sizeof s, "rgb:%02hhx/%02hhx/%02hhx", r, g, b);
1.430 nicm 718: tty_putcode_s(tty, TTYC_CS, s);
1.410 nicm 719: }
720: tty->ccolour = c;
1.107 nicm 721: }
722:
1.411 nicm 723: static int
724: tty_update_cursor(struct tty *tty, int mode, struct screen *s)
1.404 nicm 725: {
1.410 nicm 726: enum screen_cursor_style cstyle;
1.411 nicm 727: int ccolour, changed, cmode = mode;
1.404 nicm 728:
729: /* Set cursor colour if changed. */
1.410 nicm 730: if (s != NULL) {
731: ccolour = s->ccolour;
732: if (s->ccolour == -1)
733: ccolour = s->default_ccolour;
734: tty_force_cursor_colour(tty, ccolour);
735: }
1.404 nicm 736:
737: /* If cursor is off, set as invisible. */
1.411 nicm 738: if (~cmode & MODE_CURSOR) {
739: if (tty->mode & MODE_CURSOR)
1.404 nicm 740: tty_putcode(tty, TTYC_CIVIS);
1.411 nicm 741: return (cmode);
1.404 nicm 742: }
743:
744: /* Check if blinking or very visible flag changed or style changed. */
745: if (s == NULL)
746: cstyle = tty->cstyle;
1.411 nicm 747: else {
1.404 nicm 748: cstyle = s->cstyle;
1.411 nicm 749: if (cstyle == SCREEN_CURSOR_DEFAULT) {
750: if (~cmode & MODE_CURSOR_BLINKING_SET) {
751: if (s->default_mode & MODE_CURSOR_BLINKING)
752: cmode |= MODE_CURSOR_BLINKING;
753: else
754: cmode &= ~MODE_CURSOR_BLINKING;
755: }
756: cstyle = s->default_cstyle;
757: }
758: }
759:
760: /* If nothing changed, do nothing. */
761: changed = cmode ^ tty->mode;
1.404 nicm 762: if ((changed & CURSOR_MODES) == 0 && cstyle == tty->cstyle)
1.411 nicm 763: return (cmode);
1.404 nicm 764:
765: /*
766: * Set cursor style. If an explicit style has been set with DECSCUSR,
767: * set it if supported, otherwise send cvvis for blinking styles.
768: *
769: * If no style, has been set (SCREEN_CURSOR_DEFAULT), then send cvvis
770: * if either the blinking or very visible flags are set.
771: */
772: tty_putcode(tty, TTYC_CNORM);
773: switch (cstyle) {
774: case SCREEN_CURSOR_DEFAULT:
1.405 nicm 775: if (tty->cstyle != SCREEN_CURSOR_DEFAULT) {
776: if (tty_term_has(tty->term, TTYC_SE))
777: tty_putcode(tty, TTYC_SE);
778: else
1.430 nicm 779: tty_putcode_i(tty, TTYC_SS, 0);
1.405 nicm 780: }
1.411 nicm 781: if (cmode & (MODE_CURSOR_BLINKING|MODE_CURSOR_VERY_VISIBLE))
1.404 nicm 782: tty_putcode(tty, TTYC_CVVIS);
783: break;
784: case SCREEN_CURSOR_BLOCK:
785: if (tty_term_has(tty->term, TTYC_SS)) {
1.411 nicm 786: if (cmode & MODE_CURSOR_BLINKING)
1.430 nicm 787: tty_putcode_i(tty, TTYC_SS, 1);
1.404 nicm 788: else
1.430 nicm 789: tty_putcode_i(tty, TTYC_SS, 2);
1.411 nicm 790: } else if (cmode & MODE_CURSOR_BLINKING)
1.404 nicm 791: tty_putcode(tty, TTYC_CVVIS);
792: break;
793: case SCREEN_CURSOR_UNDERLINE:
794: if (tty_term_has(tty->term, TTYC_SS)) {
1.411 nicm 795: if (cmode & MODE_CURSOR_BLINKING)
1.430 nicm 796: tty_putcode_i(tty, TTYC_SS, 3);
1.404 nicm 797: else
1.430 nicm 798: tty_putcode_i(tty, TTYC_SS, 4);
1.411 nicm 799: } else if (cmode & MODE_CURSOR_BLINKING)
1.404 nicm 800: tty_putcode(tty, TTYC_CVVIS);
801: break;
802: case SCREEN_CURSOR_BAR:
803: if (tty_term_has(tty->term, TTYC_SS)) {
1.411 nicm 804: if (cmode & MODE_CURSOR_BLINKING)
1.430 nicm 805: tty_putcode_i(tty, TTYC_SS, 5);
1.404 nicm 806: else
1.430 nicm 807: tty_putcode_i(tty, TTYC_SS, 6);
1.411 nicm 808: } else if (cmode & MODE_CURSOR_BLINKING)
1.404 nicm 809: tty_putcode(tty, TTYC_CVVIS);
810: break;
811: }
812: tty->cstyle = cstyle;
1.411 nicm 813: return (cmode);
1.404 nicm 814: }
815:
1.107 nicm 816: void
817: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1 nicm 818: {
1.411 nicm 819: struct tty_term *term = tty->term;
1.404 nicm 820: struct client *c = tty->client;
821: int changed;
1.107 nicm 822:
1.1 nicm 823: if (tty->flags & TTY_NOCURSOR)
824: mode &= ~MODE_CURSOR;
825:
1.411 nicm 826: if (tty_update_cursor(tty, mode, s) & MODE_CURSOR_BLINKING)
827: mode |= MODE_CURSOR_BLINKING;
828: else
829: mode &= ~MODE_CURSOR_BLINKING;
830:
1.1 nicm 831: changed = mode ^ tty->mode;
1.392 nicm 832: if (log_get_level() != 0 && changed != 0) {
833: log_debug("%s: current mode %s", c->name,
834: screen_mode_to_string(tty->mode));
835: log_debug("%s: setting mode %s", c->name,
836: screen_mode_to_string(mode));
837: }
1.344 nicm 838:
1.411 nicm 839: if ((changed & ALL_MOUSE_MODES) && tty_term_has(term, TTYC_KMOUS)) {
1.386 nicm 840: /*
1.415 nicm 841: * If the mouse modes have changed, clear then all and apply
842: * again. There are differences in how terminals track the
843: * various bits.
1.386 nicm 844: */
1.415 nicm 845: tty_puts(tty, "\033[?1006l\033[?1000l\033[?1002l\033[?1003l");
1.344 nicm 846: if (mode & ALL_MOUSE_MODES)
1.153 nicm 847: tty_puts(tty, "\033[?1006h");
1.415 nicm 848: if (mode & MODE_MOUSE_ALL)
849: tty_puts(tty, "\033[?1000h\033[?1002h\033[?1003h");
1.419 nicm 850: else if (mode & MODE_MOUSE_BUTTON)
1.415 nicm 851: tty_puts(tty, "\033[?1000h\033[?1002h");
852: else if (mode & MODE_MOUSE_STANDARD)
1.344 nicm 853: tty_puts(tty, "\033[?1000h");
1.1 nicm 854: }
855: tty->mode = mode;
856: }
857:
1.207 nicm 858: static void
1.168 nicm 859: tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
860: enum tty_code_code code1, u_int n)
1.1 nicm 861: {
862: if (tty_term_has(tty->term, code))
1.430 nicm 863: tty_putcode_i(tty, code, n);
1.1 nicm 864: else {
865: while (n-- > 0)
866: tty_putcode(tty, code1);
867: }
868: }
869:
1.207 nicm 870: static void
1.133 nicm 871: tty_repeat_space(struct tty *tty, u_int n)
872: {
1.257 nicm 873: static char s[500];
874:
875: if (*s != ' ')
876: memset(s, ' ', sizeof s);
877:
878: while (n > sizeof s) {
879: tty_putn(tty, s, sizeof s, sizeof s);
880: n -= sizeof s;
881: }
882: if (n != 0)
883: tty_putn(tty, s, n, n);
1.304 nicm 884: }
885:
1.309 nicm 886: /* Is this window bigger than the terminal? */
887: int
888: tty_window_bigger(struct tty *tty)
889: {
890: struct client *c = tty->client;
891: struct window *w = c->session->curw->window;
892:
893: return (tty->sx < w->sx || tty->sy - status_line_size(c) < w->sy);
894: }
895:
896: /* What offset should this window be drawn at? */
897: int
898: tty_window_offset(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
899: {
900: *ox = tty->oox;
901: *oy = tty->ooy;
902: *sx = tty->osx;
903: *sy = tty->osy;
904:
905: return (tty->oflag);
906: }
907:
908: /* What offset should this window be drawn at? */
909: static int
910: tty_window_offset1(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
911: {
912: struct client *c = tty->client;
913: struct window *w = c->session->curw->window;
1.376 nicm 914: struct window_pane *wp = server_client_get_pane(c);
1.309 nicm 915: u_int cx, cy, lines;
916:
917: lines = status_line_size(c);
918:
919: if (tty->sx >= w->sx && tty->sy - lines >= w->sy) {
920: *ox = 0;
921: *oy = 0;
922: *sx = w->sx;
923: *sy = w->sy;
924:
925: c->pan_window = NULL;
926: return (0);
927: }
928:
929: *sx = tty->sx;
930: *sy = tty->sy - lines;
931:
932: if (c->pan_window == w) {
933: if (*sx >= w->sx)
934: c->pan_ox = 0;
935: else if (c->pan_ox + *sx > w->sx)
936: c->pan_ox = w->sx - *sx;
937: *ox = c->pan_ox;
938: if (*sy >= w->sy)
939: c->pan_oy = 0;
940: else if (c->pan_oy + *sy > w->sy)
941: c->pan_oy = w->sy - *sy;
942: *oy = c->pan_oy;
943: return (1);
944: }
945:
946: if (~wp->screen->mode & MODE_CURSOR) {
947: *ox = 0;
948: *oy = 0;
949: } else {
950: cx = wp->xoff + wp->screen->cx;
951: cy = wp->yoff + wp->screen->cy;
952:
953: if (cx < *sx)
954: *ox = 0;
955: else if (cx > w->sx - *sx)
956: *ox = w->sx - *sx;
957: else
958: *ox = cx - *sx / 2;
959:
960: if (cy < *sy)
961: *oy = 0;
962: else if (cy > w->sy - *sy)
963: *oy = w->sy - *sy;
964: else
965: *oy = cy - *sy / 2;
966: }
967:
968: c->pan_window = NULL;
969: return (1);
970: }
971:
972: /* Update stored offsets for a window and redraw if necessary. */
973: void
974: tty_update_window_offset(struct window *w)
975: {
976: struct client *c;
977:
978: TAILQ_FOREACH(c, &clients, entry) {
1.413 nicm 979: if (c->session != NULL &&
980: c->session->curw != NULL &&
981: c->session->curw->window == w)
1.309 nicm 982: tty_update_client_offset(c);
983: }
984: }
985:
986: /* Update stored offsets for a client and redraw if necessary. */
987: void
988: tty_update_client_offset(struct client *c)
1.304 nicm 989: {
1.309 nicm 990: u_int ox, oy, sx, sy;
1.314 nicm 991:
992: if (~c->flags & CLIENT_TERMINAL)
993: return;
1.304 nicm 994:
1.309 nicm 995: c->tty.oflag = tty_window_offset1(&c->tty, &ox, &oy, &sx, &sy);
996: if (ox == c->tty.oox &&
997: oy == c->tty.ooy &&
998: sx == c->tty.osx &&
999: sy == c->tty.osy)
1000: return;
1001:
1002: log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
1003: __func__, c->name, c->tty.oox, c->tty.ooy, c->tty.osx, c->tty.osy,
1004: ox, oy, sx, sy);
1005:
1006: c->tty.oox = ox;
1007: c->tty.ooy = oy;
1008: c->tty.osx = sx;
1009: c->tty.osy = sy;
1010:
1011: c->flags |= (CLIENT_REDRAWWINDOW|CLIENT_REDRAWSTATUS);
1.133 nicm 1012: }
1013:
1.1 nicm 1014: /*
1.119 nicm 1015: * Is the region large enough to be worth redrawing once later rather than
1016: * probably several times now? Currently yes if it is more than 50% of the
1017: * pane.
1018: */
1.207 nicm 1019: static int
1.194 nicm 1020: tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
1.119 nicm 1021: {
1.372 nicm 1022: return (ctx->orlower - ctx->orupper >= ctx->sy / 2);
1.119 nicm 1023: }
1024:
1025: /*
1.176 nicm 1026: * Return if BCE is needed but the terminal doesn't have it - it'll need to be
1027: * emulated.
1028: */
1.207 nicm 1029: static int
1.373 nicm 1030: tty_fake_bce(const struct tty *tty, const struct grid_cell *gc, u_int bg)
1.176 nicm 1031: {
1.210 nicm 1032: if (tty_term_flag(tty->term, TTYC_BCE))
1033: return (0);
1.373 nicm 1034: if (!COLOUR_DEFAULT(bg) || !COLOUR_DEFAULT(gc->bg))
1.210 nicm 1035: return (1);
1036: return (0);
1.176 nicm 1037: }
1038:
1039: /*
1.1 nicm 1040: * Redraw scroll region using data from screen (already updated). Used when
1041: * CSR not supported, or window is a pane that doesn't take up the full
1042: * width of the terminal.
1043: */
1.207 nicm 1044: static void
1.24 nicm 1045: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1046: {
1.358 nicm 1047: struct client *c = tty->client;
1.204 nicm 1048: u_int i;
1.1 nicm 1049:
1050: /*
1.373 nicm 1051: * If region is large, schedule a redraw. In most cases this is likely
1052: * to be followed by some more scrolling.
1.1 nicm 1053: */
1.119 nicm 1054: if (tty_large_region(tty, ctx)) {
1.373 nicm 1055: log_debug("%s: %s large redraw", __func__, c->name);
1056: ctx->redraw_cb(ctx);
1.1 nicm 1057: return;
1058: }
1059:
1.393 nicm 1060: for (i = ctx->orupper; i <= ctx->orlower; i++)
1061: tty_draw_pane(tty, ctx, i);
1.1 nicm 1062: }
1063:
1.309 nicm 1064: /* Is this position visible in the pane? */
1065: static int
1.374 nicm 1066: tty_is_visible(__unused struct tty *tty, const struct tty_ctx *ctx, u_int px,
1067: u_int py, u_int nx, u_int ny)
1.309 nicm 1068: {
1.374 nicm 1069: u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1.309 nicm 1070:
1071: if (!ctx->bigger)
1072: return (1);
1073:
1.372 nicm 1074: if (xoff + nx <= ctx->wox || xoff >= ctx->wox + ctx->wsx ||
1.374 nicm 1075: yoff + ny <= ctx->woy || yoff >= ctx->woy + ctx->wsy)
1.309 nicm 1076: return (0);
1077: return (1);
1078: }
1079:
1080: /* Clamp line position to visible part of pane. */
1081: static int
1082: tty_clamp_line(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1083: u_int nx, u_int *i, u_int *x, u_int *rx, u_int *ry)
1084: {
1.373 nicm 1085: u_int xoff = ctx->rxoff + px;
1.309 nicm 1086:
1087: if (!tty_is_visible(tty, ctx, px, py, nx, 1))
1088: return (0);
1.372 nicm 1089: *ry = ctx->yoff + py - ctx->woy;
1.309 nicm 1090:
1.372 nicm 1091: if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1.309 nicm 1092: /* All visible. */
1093: *i = 0;
1.372 nicm 1094: *x = ctx->xoff + px - ctx->wox;
1.309 nicm 1095: *rx = nx;
1.372 nicm 1096: } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1.309 nicm 1097: /* Both left and right not visible. */
1.372 nicm 1098: *i = ctx->wox;
1.309 nicm 1099: *x = 0;
1.372 nicm 1100: *rx = ctx->wsx;
1101: } else if (xoff < ctx->wox) {
1.309 nicm 1102: /* Left not visible. */
1.372 nicm 1103: *i = ctx->wox - (ctx->xoff + px);
1.309 nicm 1104: *x = 0;
1105: *rx = nx - *i;
1106: } else {
1107: /* Right not visible. */
1108: *i = 0;
1.372 nicm 1109: *x = (ctx->xoff + px) - ctx->wox;
1110: *rx = ctx->wsx - *x;
1.309 nicm 1111: }
1112: if (*rx > nx)
1113: fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1114:
1115: return (1);
1116: }
1117:
1118: /* Clear a line. */
1.271 nicm 1119: static void
1.373 nicm 1120: tty_clear_line(struct tty *tty, const struct grid_cell *defaults, u_int py,
1121: u_int px, u_int nx, u_int bg)
1.271 nicm 1122: {
1.406 nicm 1123: struct client *c = tty->client;
1124: struct overlay_ranges r;
1125: u_int i;
1.309 nicm 1126:
1127: log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1.271 nicm 1128:
1129: /* Nothing to clear. */
1130: if (nx == 0)
1131: return;
1132:
1133: /* If genuine BCE is available, can try escape sequences. */
1.394 nicm 1134: if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1.271 nicm 1135: /* Off the end of the line, use EL if available. */
1136: if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) {
1137: tty_cursor(tty, px, py);
1138: tty_putcode(tty, TTYC_EL);
1139: return;
1140: }
1141:
1142: /* At the start of the line. Use EL1. */
1143: if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1144: tty_cursor(tty, px + nx - 1, py);
1145: tty_putcode(tty, TTYC_EL1);
1146: return;
1147: }
1148:
1149: /* Section of line. Use ECH if possible. */
1150: if (tty_term_has(tty->term, TTYC_ECH)) {
1151: tty_cursor(tty, px, py);
1.430 nicm 1152: tty_putcode_i(tty, TTYC_ECH, nx);
1.271 nicm 1153: return;
1154: }
1155: }
1156:
1.394 nicm 1157: /*
1158: * Couldn't use an escape sequence, use spaces. Clear only the visible
1159: * bit if there is an overlay.
1160: */
1.406 nicm 1161: tty_check_overlay_range(tty, px, py, nx, &r);
1162: for (i = 0; i < OVERLAY_MAX_RANGES; i++) {
1163: if (r.nx[i] == 0)
1164: continue;
1165: tty_cursor(tty, r.px[i], py);
1166: tty_repeat_space(tty, r.nx[i]);
1.394 nicm 1167: }
1.271 nicm 1168: }
1169:
1.309 nicm 1170: /* Clear a line, adjusting to visible part of pane. */
1171: static void
1172: tty_clear_pane_line(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1173: u_int px, u_int nx, u_int bg)
1174: {
1175: struct client *c = tty->client;
1176: u_int i, x, rx, ry;
1177:
1178: log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1179:
1180: if (tty_clamp_line(tty, ctx, px, py, nx, &i, &x, &rx, &ry))
1.373 nicm 1181: tty_clear_line(tty, &ctx->defaults, ry, x, rx, bg);
1.309 nicm 1182: }
1183:
1184: /* Clamp area position to visible part of pane. */
1185: static int
1186: tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1187: u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
1188: u_int *ry)
1189: {
1.373 nicm 1190: u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1.309 nicm 1191:
1192: if (!tty_is_visible(tty, ctx, px, py, nx, ny))
1193: return (0);
1194:
1.372 nicm 1195: if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1.309 nicm 1196: /* All visible. */
1197: *i = 0;
1.372 nicm 1198: *x = ctx->xoff + px - ctx->wox;
1.309 nicm 1199: *rx = nx;
1.372 nicm 1200: } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1.309 nicm 1201: /* Both left and right not visible. */
1.372 nicm 1202: *i = ctx->wox;
1.309 nicm 1203: *x = 0;
1.372 nicm 1204: *rx = ctx->wsx;
1205: } else if (xoff < ctx->wox) {
1.309 nicm 1206: /* Left not visible. */
1.372 nicm 1207: *i = ctx->wox - (ctx->xoff + px);
1.309 nicm 1208: *x = 0;
1209: *rx = nx - *i;
1210: } else {
1211: /* Right not visible. */
1212: *i = 0;
1.372 nicm 1213: *x = (ctx->xoff + px) - ctx->wox;
1214: *rx = ctx->wsx - *x;
1.309 nicm 1215: }
1216: if (*rx > nx)
1217: fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1218:
1.372 nicm 1219: if (yoff >= ctx->woy && yoff + ny <= ctx->woy + ctx->wsy) {
1.309 nicm 1220: /* All visible. */
1221: *j = 0;
1.372 nicm 1222: *y = ctx->yoff + py - ctx->woy;
1.309 nicm 1223: *ry = ny;
1.372 nicm 1224: } else if (yoff < ctx->woy && yoff + ny > ctx->woy + ctx->wsy) {
1.327 nicm 1225: /* Both top and bottom not visible. */
1.372 nicm 1226: *j = ctx->woy;
1.309 nicm 1227: *y = 0;
1.372 nicm 1228: *ry = ctx->wsy;
1229: } else if (yoff < ctx->woy) {
1.327 nicm 1230: /* Top not visible. */
1.372 nicm 1231: *j = ctx->woy - (ctx->yoff + py);
1.309 nicm 1232: *y = 0;
1233: *ry = ny - *j;
1234: } else {
1.327 nicm 1235: /* Bottom not visible. */
1.309 nicm 1236: *j = 0;
1.372 nicm 1237: *y = (ctx->yoff + py) - ctx->woy;
1238: *ry = ctx->wsy - *y;
1.309 nicm 1239: }
1240: if (*ry > ny)
1241: fatalx("%s: y too big, %u > %u", __func__, *ry, ny);
1242:
1243: return (1);
1244: }
1245:
1246: /* Clear an area, adjusting to visible part of pane. */
1.271 nicm 1247: static void
1.373 nicm 1248: tty_clear_area(struct tty *tty, const struct grid_cell *defaults, u_int py,
1249: u_int ny, u_int px, u_int nx, u_int bg)
1.271 nicm 1250: {
1.309 nicm 1251: struct client *c = tty->client;
1252: u_int yy;
1253: char tmp[64];
1.271 nicm 1254:
1.309 nicm 1255: log_debug("%s: %s, %u,%u at %u,%u", __func__, c->name, nx, ny, px, py);
1.271 nicm 1256:
1257: /* Nothing to clear. */
1258: if (nx == 0 || ny == 0)
1259: return;
1260:
1261: /* If genuine BCE is available, can try escape sequences. */
1.394 nicm 1262: if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1.276 nicm 1263: /* Use ED if clearing off the bottom of the terminal. */
1.271 nicm 1264: if (px == 0 &&
1265: px + nx >= tty->sx &&
1266: py + ny >= tty->sy &&
1267: tty_term_has(tty->term, TTYC_ED)) {
1268: tty_cursor(tty, 0, py);
1269: tty_putcode(tty, TTYC_ED);
1.274 nicm 1270: return;
1271: }
1272:
1273: /*
1.276 nicm 1274: * On VT420 compatible terminals we can use DECFRA if the
1275: * background colour isn't default (because it doesn't work
1276: * after SGR 0).
1.274 nicm 1277: */
1.359 nicm 1278: if ((tty->term->flags & TERM_DECFRA) && !COLOUR_DEFAULT(bg)) {
1.274 nicm 1279: xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x",
1280: py + 1, px + 1, py + ny, px + nx);
1281: tty_puts(tty, tmp);
1.276 nicm 1282: return;
1283: }
1284:
1.280 nicm 1285: /* Full lines can be scrolled away to clear them. */
1286: if (px == 0 &&
1.281 nicm 1287: px + nx >= tty->sx &&
1.280 nicm 1288: ny > 2 &&
1289: tty_term_has(tty->term, TTYC_CSR) &&
1290: tty_term_has(tty->term, TTYC_INDN)) {
1291: tty_region(tty, py, py + ny - 1);
1292: tty_margin_off(tty);
1.430 nicm 1293: tty_putcode_i(tty, TTYC_INDN, ny);
1.280 nicm 1294: return;
1295: }
1296:
1.276 nicm 1297: /*
1298: * If margins are supported, can just scroll the area off to
1299: * clear it.
1300: */
1.277 nicm 1301: if (nx > 2 &&
1302: ny > 2 &&
1.280 nicm 1303: tty_term_has(tty->term, TTYC_CSR) &&
1.277 nicm 1304: tty_use_margin(tty) &&
1305: tty_term_has(tty->term, TTYC_INDN)) {
1.276 nicm 1306: tty_region(tty, py, py + ny - 1);
1307: tty_margin(tty, px, px + nx - 1);
1.430 nicm 1308: tty_putcode_i(tty, TTYC_INDN, ny);
1.271 nicm 1309: return;
1310: }
1311: }
1312:
1313: /* Couldn't use an escape sequence, loop over the lines. */
1314: for (yy = py; yy < py + ny; yy++)
1.373 nicm 1315: tty_clear_line(tty, defaults, yy, px, nx, bg);
1.271 nicm 1316: }
1317:
1.309 nicm 1318: /* Clear an area in a pane. */
1319: static void
1320: tty_clear_pane_area(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1321: u_int ny, u_int px, u_int nx, u_int bg)
1322: {
1323: u_int i, j, x, y, rx, ry;
1324:
1325: if (tty_clamp_area(tty, ctx, px, py, nx, ny, &i, &j, &x, &y, &rx, &ry))
1.373 nicm 1326: tty_clear_area(tty, &ctx->defaults, y, ry, x, rx, bg);
1.309 nicm 1327: }
1328:
1329: static void
1330: tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
1.176 nicm 1331: {
1.373 nicm 1332: struct screen *s = ctx->s;
1333: u_int nx = ctx->sx, i, x, rx, ry;
1.309 nicm 1334:
1335: log_debug("%s: %s %u %d", __func__, tty->client->name, py, ctx->bigger);
1336:
1337: if (!ctx->bigger) {
1.373 nicm 1338: tty_draw_line(tty, s, 0, py, nx, ctx->xoff, ctx->yoff + py,
1339: &ctx->defaults, ctx->palette);
1.309 nicm 1340: return;
1341: }
1.373 nicm 1342: if (tty_clamp_line(tty, ctx, 0, py, nx, &i, &x, &rx, &ry)) {
1343: tty_draw_line(tty, s, i, py, rx, x, ry, &ctx->defaults,
1344: ctx->palette);
1345: }
1.176 nicm 1346: }
1347:
1.298 nicm 1348: static const struct grid_cell *
1349: tty_check_codeset(struct tty *tty, const struct grid_cell *gc)
1350: {
1351: static struct grid_cell new;
1.377 nicm 1352: int c;
1.298 nicm 1353:
1354: /* Characters less than 0x7f are always fine, no matter what. */
1355: if (gc->data.size == 1 && *gc->data.data < 0x7f)
1356: return (gc);
1357:
1358: /* UTF-8 terminal and a UTF-8 character - fine. */
1.359 nicm 1359: if (tty->client->flags & CLIENT_UTF8)
1.298 nicm 1360: return (gc);
1.377 nicm 1361: memcpy(&new, gc, sizeof new);
1362:
1363: /* See if this can be mapped to an ACS character. */
1364: c = tty_acs_reverse_get(tty, gc->data.data, gc->data.size);
1365: if (c != -1) {
1366: utf8_set(&new.data, c);
1367: new.attr |= GRID_ATTR_CHARSET;
1368: return (&new);
1369: }
1.298 nicm 1370:
1371: /* Replace by the right number of underscores. */
1.377 nicm 1372: new.data.size = gc->data.width;
1373: if (new.data.size > UTF8_SIZE)
1374: new.data.size = UTF8_SIZE;
1375: memset(new.data.data, '_', new.data.size);
1.298 nicm 1376: return (&new);
1377: }
1378:
1.406 nicm 1379: /*
1380: * Check if a single character is obstructed by the overlay and return a
1381: * boolean.
1382: */
1.346 nicm 1383: static int
1384: tty_check_overlay(struct tty *tty, u_int px, u_int py)
1385: {
1.406 nicm 1386: struct overlay_ranges r;
1387:
1388: /*
1389: * A unit width range will always return nx[2] == 0 from a check, even
1390: * with multiple overlays, so it's sufficient to check just the first
1391: * two entries.
1392: */
1393: tty_check_overlay_range(tty, px, py, 1, &r);
1394: if (r.nx[0] + r.nx[1] == 0)
1395: return (0);
1396: return (1);
1397: }
1398:
1399: /* Return parts of the input range which are visible. */
1400: static void
1401: tty_check_overlay_range(struct tty *tty, u_int px, u_int py, u_int nx,
1402: struct overlay_ranges *r)
1403: {
1.346 nicm 1404: struct client *c = tty->client;
1405:
1.406 nicm 1406: if (c->overlay_check == NULL) {
1407: r->px[0] = px;
1408: r->nx[0] = nx;
1409: r->px[1] = 0;
1410: r->nx[1] = 0;
1411: r->px[2] = 0;
1412: r->nx[2] = 0;
1413: return;
1414: }
1415:
1416: c->overlay_check(c, c->overlay_data, px, py, nx, r);
1.346 nicm 1417: }
1418:
1.176 nicm 1419: void
1.373 nicm 1420: tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx,
1.399 nicm 1421: u_int atx, u_int aty, const struct grid_cell *defaults,
1422: struct colour_palette *palette)
1.1 nicm 1423: {
1.297 nicm 1424: struct grid *gd = s->grid;
1.250 nicm 1425: struct grid_cell gc, last;
1.298 nicm 1426: const struct grid_cell *gcp;
1.309 nicm 1427: struct grid_line *gl;
1.394 nicm 1428: struct client *c = tty->client;
1.406 nicm 1429: struct overlay_ranges r;
1430: u_int i, j, ux, sx, width, hidden, eux, nxx;
1431: u_int cellsize;
1.396 nicm 1432: int flags, cleared = 0, wrapped = 0;
1.250 nicm 1433: char buf[512];
1.315 nicm 1434: size_t len;
1.1 nicm 1435:
1.309 nicm 1436: log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__,
1437: px, py, nx, atx, aty);
1.399 nicm 1438: log_debug("%s: defaults: fg=%d, bg=%d", __func__, defaults->fg,
1439: defaults->bg);
1.309 nicm 1440:
1441: /*
1442: * py is the line in the screen to draw.
1443: * px is the start x and nx is the width to draw.
1444: * atx,aty is the line on the terminal to draw it.
1445: */
1446:
1.259 nicm 1447: flags = (tty->flags & TTY_NOCURSOR);
1.181 nicm 1448: tty->flags |= TTY_NOCURSOR;
1449: tty_update_mode(tty, tty->mode, s);
1.35 nicm 1450:
1.214 nicm 1451: tty_region_off(tty);
1452: tty_margin_off(tty);
1453:
1.273 nicm 1454: /*
1455: * Clamp the width to cellsize - note this is not cellused, because
1456: * there may be empty background cells after it (from BCE).
1457: */
1.1 nicm 1458: sx = screen_size_x(s);
1.309 nicm 1459: if (nx > sx)
1460: nx = sx;
1.303 nicm 1461: cellsize = grid_get_line(gd, gd->hsize + py)->cellsize;
1462: if (sx > cellsize)
1463: sx = cellsize;
1.1 nicm 1464: if (sx > tty->sx)
1465: sx = tty->sx;
1.309 nicm 1466: if (sx > nx)
1467: sx = nx;
1.292 nicm 1468: ux = 0;
1.1 nicm 1469:
1.309 nicm 1470: if (py == 0)
1471: gl = NULL;
1472: else
1473: gl = grid_get_line(gd, gd->hsize + py - 1);
1.373 nicm 1474: if (gl == NULL ||
1.309 nicm 1475: (~gl->flags & GRID_LINE_WRAPPED) ||
1476: atx != 0 ||
1.268 nicm 1477: tty->cx < tty->sx ||
1.309 nicm 1478: nx < tty->sx) {
1479: if (nx < tty->sx &&
1480: atx == 0 &&
1481: px + sx != nx &&
1.268 nicm 1482: tty_term_has(tty->term, TTYC_EL1) &&
1.394 nicm 1483: !tty_fake_bce(tty, defaults, 8) &&
1484: c->overlay_check == NULL) {
1.421 nicm 1485: tty_default_attributes(tty, defaults, palette, 8,
1486: s->hyperlinks);
1.309 nicm 1487: tty_cursor(tty, nx - 1, aty);
1.268 nicm 1488: tty_putcode(tty, TTYC_EL1);
1489: cleared = 1;
1490: }
1.329 nicm 1491: } else {
1.309 nicm 1492: log_debug("%s: wrapped line %u", __func__, aty);
1.329 nicm 1493: wrapped = 1;
1494: }
1.250 nicm 1495:
1496: memcpy(&last, &grid_default_cell, sizeof last);
1497: len = 0;
1498: width = 0;
1.46 nicm 1499:
1.1 nicm 1500: for (i = 0; i < sx; i++) {
1.309 nicm 1501: grid_view_get_cell(gd, px + i, py, &gc);
1.298 nicm 1502: gcp = tty_check_codeset(tty, &gc);
1.250 nicm 1503: if (len != 0 &&
1.346 nicm 1504: (!tty_check_overlay(tty, atx + ux + width, aty) ||
1505: (gcp->attr & GRID_ATTR_CHARSET) ||
1.298 nicm 1506: gcp->flags != last.flags ||
1507: gcp->attr != last.attr ||
1508: gcp->fg != last.fg ||
1509: gcp->bg != last.bg ||
1.328 nicm 1510: gcp->us != last.us ||
1.421 nicm 1511: gcp->link != last.link ||
1.315 nicm 1512: ux + width + gcp->data.width > nx ||
1.298 nicm 1513: (sizeof buf) - len < gcp->data.size)) {
1.421 nicm 1514: tty_attributes(tty, &last, defaults, palette,
1515: s->hyperlinks);
1.315 nicm 1516: if (last.flags & GRID_FLAG_CLEARED) {
1517: log_debug("%s: %zu cleared", __func__, len);
1.373 nicm 1518: tty_clear_line(tty, defaults, aty, atx + ux,
1519: width, last.bg);
1.315 nicm 1520: } else {
1.329 nicm 1521: if (!wrapped || atx != 0 || ux != 0)
1522: tty_cursor(tty, atx + ux, aty);
1.315 nicm 1523: tty_putn(tty, buf, len, width);
1524: }
1.292 nicm 1525: ux += width;
1.250 nicm 1526:
1527: len = 0;
1528: width = 0;
1.329 nicm 1529: wrapped = 0;
1.250 nicm 1530: }
1531:
1.298 nicm 1532: if (gcp->flags & GRID_FLAG_SELECTED)
1.299 nicm 1533: screen_select_cell(s, &last, gcp);
1.250 nicm 1534: else
1.299 nicm 1535: memcpy(&last, gcp, sizeof last);
1.395 nicm 1536:
1.406 nicm 1537: tty_check_overlay_range(tty, atx + ux, aty, gcp->data.width,
1538: &r);
1.395 nicm 1539: hidden = 0;
1.406 nicm 1540: for (j = 0; j < OVERLAY_MAX_RANGES; j++)
1541: hidden += r.nx[j];
1542: hidden = gcp->data.width - hidden;
1.396 nicm 1543: if (hidden != 0 && hidden == gcp->data.width) {
1544: if (~gcp->flags & GRID_FLAG_PADDING)
1545: ux += gcp->data.width;
1546: } else if (hidden != 0 || ux + gcp->data.width > nx) {
1.395 nicm 1547: if (~gcp->flags & GRID_FLAG_PADDING) {
1.421 nicm 1548: tty_attributes(tty, &last, defaults, palette,
1549: s->hyperlinks);
1.406 nicm 1550: for (j = 0; j < OVERLAY_MAX_RANGES; j++) {
1551: if (r.nx[j] == 0)
1552: continue;
1553: /* Effective width drawn so far. */
1554: eux = r.px[j] - atx;
1555: if (eux < nx) {
1556: tty_cursor(tty, r.px[j], aty);
1557: nxx = nx - eux;
1558: if (r.nx[j] > nxx)
1559: r.nx[j] = nxx;
1560: tty_repeat_space(tty, r.nx[j]);
1561: ux = eux + r.nx[j];
1.397 nicm 1562: }
1.395 nicm 1563: }
1.297 nicm 1564: }
1.299 nicm 1565: } else if (gcp->attr & GRID_ATTR_CHARSET) {
1.421 nicm 1566: tty_attributes(tty, &last, defaults, palette,
1567: s->hyperlinks);
1.315 nicm 1568: tty_cursor(tty, atx + ux, aty);
1.299 nicm 1569: for (j = 0; j < gcp->data.size; j++)
1570: tty_putc(tty, gcp->data.data[j]);
1.346 nicm 1571: ux += gcp->data.width;
1.381 nicm 1572: } else if (~gcp->flags & GRID_FLAG_PADDING) {
1.298 nicm 1573: memcpy(buf + len, gcp->data.data, gcp->data.size);
1574: len += gcp->data.size;
1575: width += gcp->data.width;
1.250 nicm 1576: }
1577: }
1.315 nicm 1578: if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1.421 nicm 1579: tty_attributes(tty, &last, defaults, palette, s->hyperlinks);
1.315 nicm 1580: if (last.flags & GRID_FLAG_CLEARED) {
1581: log_debug("%s: %zu cleared (end)", __func__, len);
1.373 nicm 1582: tty_clear_line(tty, defaults, aty, atx + ux, width,
1583: last.bg);
1.315 nicm 1584: } else {
1.329 nicm 1585: if (!wrapped || atx != 0 || ux != 0)
1586: tty_cursor(tty, atx + ux, aty);
1.291 nicm 1587: tty_putn(tty, buf, len, width);
1588: }
1.315 nicm 1589: ux += width;
1.1 nicm 1590: }
1591:
1.309 nicm 1592: if (!cleared && ux < nx) {
1.315 nicm 1593: log_debug("%s: %u to end of line (%zu cleared)", __func__,
1594: nx - ux, len);
1.421 nicm 1595: tty_default_attributes(tty, defaults, palette, 8,
1596: s->hyperlinks);
1.373 nicm 1597: tty_clear_line(tty, defaults, aty, atx + ux, nx - ux, 8);
1.35 nicm 1598: }
1.1 nicm 1599:
1.181 nicm 1600: tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107 nicm 1601: tty_update_mode(tty, tty->mode, s);
1.15 nicm 1602: }
1603:
1.348 nicm 1604: void
1605: tty_sync_start(struct tty *tty)
1606: {
1.361 nicm 1607: if (tty->flags & TTY_BLOCK)
1608: return;
1609: if (tty->flags & TTY_SYNCING)
1610: return;
1611: tty->flags |= TTY_SYNCING;
1612:
1613: if (tty_term_has(tty->term, TTYC_SYNC)) {
1614: log_debug("%s sync start", tty->client->name);
1.430 nicm 1615: tty_putcode_i(tty, TTYC_SYNC, 1);
1.357 nicm 1616: }
1.348 nicm 1617: }
1618:
1619: void
1620: tty_sync_end(struct tty *tty)
1621: {
1.361 nicm 1622: if (tty->flags & TTY_BLOCK)
1623: return;
1624: if (~tty->flags & TTY_SYNCING)
1625: return;
1626: tty->flags &= ~TTY_SYNCING;
1627:
1628: if (tty_term_has(tty->term, TTYC_SYNC)) {
1629: log_debug("%s sync end", tty->client->name);
1.430 nicm 1630: tty_putcode_i(tty, TTYC_SYNC, 2);
1.357 nicm 1631: }
1.348 nicm 1632: }
1633:
1.201 nicm 1634: static int
1.428 nicm 1635: tty_client_ready(const struct tty_ctx *ctx, struct client *c)
1.182 nicm 1636: {
1637: if (c->session == NULL || c->tty.term == NULL)
1638: return (0);
1.428 nicm 1639: if (c->flags & CLIENT_SUSPENDED)
1640: return (0);
1641:
1642: /*
1643: * If invisible panes are allowed (used for passthrough), don't care if
1644: * redrawing or frozen.
1645: */
1646: if (ctx->allow_invisible_panes)
1647: return (1);
1648:
1649: if (c->flags & CLIENT_REDRAWWINDOW)
1.182 nicm 1650: return (0);
1651: if (c->tty.flags & TTY_FREEZE)
1652: return (0);
1653: return (1);
1654: }
1655:
1.15 nicm 1656: void
1.182 nicm 1657: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1658: struct tty_ctx *ctx)
1.15 nicm 1659: {
1.373 nicm 1660: struct client *c;
1661: int state;
1.15 nicm 1662:
1.373 nicm 1663: if (ctx->set_client_cb == NULL)
1.15 nicm 1664: return;
1.178 nicm 1665: TAILQ_FOREACH(c, &clients, entry) {
1.428 nicm 1666: if (tty_client_ready(ctx, c)) {
1.423 nicm 1667: state = ctx->set_client_cb(ctx, c);
1668: if (state == -1)
1669: break;
1670: if (state == 0)
1671: continue;
1.428 nicm 1672: cmdfn(&c->tty, ctx);
1.423 nicm 1673: }
1.1 nicm 1674: }
1675: }
1676:
1677: void
1.24 nicm 1678: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1679: {
1.394 nicm 1680: struct client *c = tty->client;
1681:
1.309 nicm 1682: if (ctx->bigger ||
1.372 nicm 1683: !tty_full_width(tty, ctx) ||
1.373 nicm 1684: tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1.210 nicm 1685: (!tty_term_has(tty->term, TTYC_ICH) &&
1.394 nicm 1686: !tty_term_has(tty->term, TTYC_ICH1)) ||
1687: c->overlay_check != NULL) {
1.309 nicm 1688: tty_draw_pane(tty, ctx, ctx->ocy);
1.1 nicm 1689: return;
1690: }
1691:
1.421 nicm 1692: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1693: ctx->s->hyperlinks);
1.1 nicm 1694:
1.77 nicm 1695: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 1696:
1.206 nicm 1697: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1 nicm 1698: }
1699:
1700: void
1.24 nicm 1701: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1702: {
1.394 nicm 1703: struct client *c = tty->client;
1704:
1.309 nicm 1705: if (ctx->bigger ||
1.372 nicm 1706: !tty_full_width(tty, ctx) ||
1.373 nicm 1707: tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1.26 nicm 1708: (!tty_term_has(tty->term, TTYC_DCH) &&
1.394 nicm 1709: !tty_term_has(tty->term, TTYC_DCH1)) ||
1710: c->overlay_check != NULL) {
1.309 nicm 1711: tty_draw_pane(tty, ctx, ctx->ocy);
1.1 nicm 1712: return;
1713: }
1714:
1.421 nicm 1715: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1716: ctx->s->hyperlinks);
1.1 nicm 1717:
1.77 nicm 1718: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 1719:
1.206 nicm 1720: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146 nicm 1721: }
1722:
1723: void
1724: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1725: {
1.421 nicm 1726: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1727: ctx->s->hyperlinks);
1.146 nicm 1728:
1.388 nicm 1729: tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, ctx->num, ctx->bg);
1.1 nicm 1730: }
1731:
1732: void
1.24 nicm 1733: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1734: {
1.394 nicm 1735: struct client *c = tty->client;
1736:
1.309 nicm 1737: if (ctx->bigger ||
1.372 nicm 1738: !tty_full_width(tty, ctx) ||
1.373 nicm 1739: tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1.77 nicm 1740: !tty_term_has(tty->term, TTYC_CSR) ||
1.307 nicm 1741: !tty_term_has(tty->term, TTYC_IL1) ||
1.373 nicm 1742: ctx->sx == 1 ||
1.394 nicm 1743: ctx->sy == 1 ||
1744: c->overlay_check != NULL) {
1.14 nicm 1745: tty_redraw_region(tty, ctx);
1.1 nicm 1746: return;
1747: }
1748:
1.421 nicm 1749: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1750: ctx->s->hyperlinks);
1.1 nicm 1751:
1.77 nicm 1752: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1753: tty_margin_off(tty);
1.77 nicm 1754: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1755:
1.12 nicm 1756: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.279 nicm 1757: tty->cx = tty->cy = UINT_MAX;
1.1 nicm 1758: }
1759:
1760: void
1.24 nicm 1761: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1762: {
1.394 nicm 1763: struct client *c = tty->client;
1764:
1.309 nicm 1765: if (ctx->bigger ||
1.372 nicm 1766: !tty_full_width(tty, ctx) ||
1.373 nicm 1767: tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1.72 nicm 1768: !tty_term_has(tty->term, TTYC_CSR) ||
1.307 nicm 1769: !tty_term_has(tty->term, TTYC_DL1) ||
1.373 nicm 1770: ctx->sx == 1 ||
1.394 nicm 1771: ctx->sy == 1 ||
1772: c->overlay_check != NULL) {
1.14 nicm 1773: tty_redraw_region(tty, ctx);
1.1 nicm 1774: return;
1775: }
1776:
1.421 nicm 1777: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1778: ctx->s->hyperlinks);
1.1 nicm 1779:
1.77 nicm 1780: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214 nicm 1781: tty_margin_off(tty);
1.77 nicm 1782: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1783:
1.12 nicm 1784: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.279 nicm 1785: tty->cx = tty->cy = UINT_MAX;
1.1 nicm 1786: }
1787:
1788: void
1.24 nicm 1789: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1790: {
1.421 nicm 1791: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1792: ctx->s->hyperlinks);
1.1 nicm 1793:
1.372 nicm 1794: tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg);
1.1 nicm 1795: }
1796:
1797: void
1.24 nicm 1798: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1799: {
1.373 nicm 1800: u_int nx = ctx->sx - ctx->ocx;
1.1 nicm 1801:
1.421 nicm 1802: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1803: ctx->s->hyperlinks);
1.1 nicm 1804:
1.309 nicm 1805: tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1.1 nicm 1806: }
1807:
1808: void
1.24 nicm 1809: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1810: {
1.421 nicm 1811: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1812: ctx->s->hyperlinks);
1.1 nicm 1813:
1.309 nicm 1814: tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1.1 nicm 1815: }
1816:
1817: void
1.24 nicm 1818: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1819: {
1.394 nicm 1820: struct client *c = tty->client;
1821:
1.56 nicm 1822: if (ctx->ocy != ctx->orupper)
1823: return;
1824:
1.309 nicm 1825: if (ctx->bigger ||
1.372 nicm 1826: (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.373 nicm 1827: tty_fake_bce(tty, &ctx->defaults, 8) ||
1.70 nicm 1828: !tty_term_has(tty->term, TTYC_CSR) ||
1.331 nicm 1829: (!tty_term_has(tty->term, TTYC_RI) &&
1830: !tty_term_has(tty->term, TTYC_RIN)) ||
1.373 nicm 1831: ctx->sx == 1 ||
1.394 nicm 1832: ctx->sy == 1 ||
1833: c->overlay_check != NULL) {
1.14 nicm 1834: tty_redraw_region(tty, ctx);
1.1 nicm 1835: return;
1836: }
1837:
1.421 nicm 1838: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1839: ctx->s->hyperlinks);
1.77 nicm 1840:
1.56 nicm 1841: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.331 nicm 1842: tty_margin_pane(tty, ctx);
1.56 nicm 1843: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 1844:
1.331 nicm 1845: if (tty_term_has(tty->term, TTYC_RI))
1846: tty_putcode(tty, TTYC_RI);
1847: else
1.430 nicm 1848: tty_putcode_i(tty, TTYC_RIN, 1);
1.1 nicm 1849: }
1850:
1851: void
1.24 nicm 1852: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1853: {
1.394 nicm 1854: struct client *c = tty->client;
1855:
1.56 nicm 1856: if (ctx->ocy != ctx->orlower)
1857: return;
1858:
1.309 nicm 1859: if (ctx->bigger ||
1.372 nicm 1860: (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.373 nicm 1861: tty_fake_bce(tty, &ctx->defaults, 8) ||
1.307 nicm 1862: !tty_term_has(tty->term, TTYC_CSR) ||
1.373 nicm 1863: ctx->sx == 1 ||
1.394 nicm 1864: ctx->sy == 1 ||
1865: c->overlay_check != NULL) {
1.236 nicm 1866: tty_redraw_region(tty, ctx);
1.1 nicm 1867: return;
1868: }
1.52 nicm 1869:
1.421 nicm 1870: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1871: ctx->s->hyperlinks);
1.77 nicm 1872:
1.56 nicm 1873: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.212 nicm 1874: tty_margin_pane(tty, ctx);
1875:
1876: /*
1.301 nicm 1877: * If we want to wrap a pane while using margins, the cursor needs to
1878: * be exactly on the right of the region. If the cursor is entirely off
1879: * the edge - move it back to the right. Some terminals are funny about
1880: * this and insert extra spaces, so only use the right if margins are
1881: * enabled.
1.212 nicm 1882: */
1.301 nicm 1883: if (ctx->xoff + ctx->ocx > tty->rright) {
1884: if (!tty_use_margin(tty))
1885: tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
1886: else
1887: tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1888: } else
1.212 nicm 1889: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 1890:
1.56 nicm 1891: tty_putc(tty, '\n');
1.240 nicm 1892: }
1893:
1894: void
1895: tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1896: {
1.394 nicm 1897: struct client *c = tty->client;
1898: u_int i;
1.240 nicm 1899:
1.309 nicm 1900: if (ctx->bigger ||
1.372 nicm 1901: (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.373 nicm 1902: tty_fake_bce(tty, &ctx->defaults, 8) ||
1.307 nicm 1903: !tty_term_has(tty->term, TTYC_CSR) ||
1.373 nicm 1904: ctx->sx == 1 ||
1.394 nicm 1905: ctx->sy == 1 ||
1906: c->overlay_check != NULL) {
1.240 nicm 1907: tty_redraw_region(tty, ctx);
1908: return;
1909: }
1910:
1.421 nicm 1911: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1912: ctx->s->hyperlinks);
1.240 nicm 1913:
1914: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1915: tty_margin_pane(tty, ctx);
1916:
1.284 nicm 1917: if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1.301 nicm 1918: if (!tty_use_margin(tty))
1919: tty_cursor(tty, 0, tty->rlower);
1920: else
1921: tty_cursor(tty, tty->rright, tty->rlower);
1.284 nicm 1922: for (i = 0; i < ctx->num; i++)
1.240 nicm 1923: tty_putc(tty, '\n');
1.301 nicm 1924: } else {
1.353 nicm 1925: if (tty->cy == UINT_MAX)
1926: tty_cursor(tty, 0, 0);
1927: else
1928: tty_cursor(tty, 0, tty->cy);
1.430 nicm 1929: tty_putcode_i(tty, TTYC_INDN, ctx->num);
1.331 nicm 1930: }
1931: }
1932:
1933: void
1934: tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
1935: {
1.394 nicm 1936: u_int i;
1937: struct client *c = tty->client;
1.331 nicm 1938:
1939: if (ctx->bigger ||
1.372 nicm 1940: (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.373 nicm 1941: tty_fake_bce(tty, &ctx->defaults, 8) ||
1.331 nicm 1942: !tty_term_has(tty->term, TTYC_CSR) ||
1943: (!tty_term_has(tty->term, TTYC_RI) &&
1944: !tty_term_has(tty->term, TTYC_RIN)) ||
1.373 nicm 1945: ctx->sx == 1 ||
1.394 nicm 1946: ctx->sy == 1 ||
1947: c->overlay_check != NULL) {
1.331 nicm 1948: tty_redraw_region(tty, ctx);
1949: return;
1950: }
1951:
1.421 nicm 1952: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1953: ctx->s->hyperlinks);
1.331 nicm 1954:
1955: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1956: tty_margin_pane(tty, ctx);
1957: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1958:
1959: if (tty_term_has(tty->term, TTYC_RIN))
1.430 nicm 1960: tty_putcode_i(tty, TTYC_RIN, ctx->num);
1.331 nicm 1961: else {
1962: for (i = 0; i < ctx->num; i++)
1963: tty_putcode(tty, TTYC_RI);
1.301 nicm 1964: }
1.1 nicm 1965: }
1966:
1967: void
1.24 nicm 1968: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1969: {
1.373 nicm 1970: u_int px, py, nx, ny;
1.1 nicm 1971:
1.421 nicm 1972: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1973: ctx->s->hyperlinks);
1.1 nicm 1974:
1.372 nicm 1975: tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214 nicm 1976: tty_margin_off(tty);
1.39 nicm 1977:
1.309 nicm 1978: px = 0;
1.372 nicm 1979: nx = ctx->sx;
1.309 nicm 1980: py = ctx->ocy + 1;
1.372 nicm 1981: ny = ctx->sy - ctx->ocy - 1;
1.271 nicm 1982:
1.309 nicm 1983: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271 nicm 1984:
1.309 nicm 1985: px = ctx->ocx;
1.372 nicm 1986: nx = ctx->sx - ctx->ocx;
1.309 nicm 1987: py = ctx->ocy;
1.271 nicm 1988:
1.309 nicm 1989: tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1 nicm 1990: }
1991:
1992: void
1.24 nicm 1993: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1994: {
1.373 nicm 1995: u_int px, py, nx, ny;
1.1 nicm 1996:
1.421 nicm 1997: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1998: ctx->s->hyperlinks);
1.1 nicm 1999:
1.372 nicm 2000: tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214 nicm 2001: tty_margin_off(tty);
1.39 nicm 2002:
1.309 nicm 2003: px = 0;
1.372 nicm 2004: nx = ctx->sx;
1.309 nicm 2005: py = 0;
1.319 nicm 2006: ny = ctx->ocy;
1.271 nicm 2007:
1.309 nicm 2008: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271 nicm 2009:
1.309 nicm 2010: px = 0;
1.271 nicm 2011: nx = ctx->ocx + 1;
1.309 nicm 2012: py = ctx->ocy;
1.271 nicm 2013:
1.309 nicm 2014: tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1 nicm 2015: }
2016:
2017: void
1.24 nicm 2018: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 2019: {
1.373 nicm 2020: u_int px, py, nx, ny;
1.1 nicm 2021:
1.421 nicm 2022: tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
2023: ctx->s->hyperlinks);
1.1 nicm 2024:
1.372 nicm 2025: tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214 nicm 2026: tty_margin_off(tty);
1.39 nicm 2027:
1.309 nicm 2028: px = 0;
1.372 nicm 2029: nx = ctx->sx;
1.309 nicm 2030: py = 0;
1.372 nicm 2031: ny = ctx->sy;
1.271 nicm 2032:
1.309 nicm 2033: tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.4 nicm 2034: }
2035:
2036: void
1.24 nicm 2037: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 2038: {
1.373 nicm 2039: u_int i, j;
1.4 nicm 2040:
1.309 nicm 2041: if (ctx->bigger) {
1.373 nicm 2042: ctx->redraw_cb(ctx);
1.309 nicm 2043: return;
2044: }
2045:
1.421 nicm 2046: tty_attributes(tty, &grid_default_cell, &ctx->defaults, ctx->palette,
2047: ctx->s->hyperlinks);
1.4 nicm 2048:
1.372 nicm 2049: tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214 nicm 2050: tty_margin_off(tty);
1.4 nicm 2051:
1.372 nicm 2052: for (j = 0; j < ctx->sy; j++) {
1.41 nicm 2053: tty_cursor_pane(tty, ctx, 0, j);
1.372 nicm 2054: for (i = 0; i < ctx->sx; i++)
1.4 nicm 2055: tty_putc(tty, 'E');
1.1 nicm 2056: }
2057: }
2058:
2059: void
1.24 nicm 2060: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 2061: {
1.394 nicm 2062: const struct grid_cell *gcp = ctx->cell;
2063: struct screen *s = ctx->s;
1.406 nicm 2064: struct overlay_ranges r;
2065: u_int px, py, i, vis = 0;
1.394 nicm 2066:
2067: px = ctx->xoff + ctx->ocx - ctx->wox;
2068: py = ctx->yoff + ctx->ocy - ctx->woy;
2069: if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1) ||
2070: (gcp->data.width == 1 && !tty_check_overlay(tty, px, py)))
2071: return;
2072:
2073: /* Handle partially obstructed wide characters. */
2074: if (gcp->data.width > 1) {
1.406 nicm 2075: tty_check_overlay_range(tty, px, py, gcp->data.width, &r);
2076: for (i = 0; i < OVERLAY_MAX_RANGES; i++)
2077: vis += r.nx[i];
2078: if (vis < gcp->data.width) {
1.408 nicm 2079: tty_draw_line(tty, s, s->cx, s->cy, gcp->data.width,
2080: px, py, &ctx->defaults, ctx->palette);
2081: return;
1.394 nicm 2082: }
2083: }
1.309 nicm 2084:
1.372 nicm 2085: if (ctx->xoff + ctx->ocx - ctx->wox > tty->sx - 1 &&
1.306 nicm 2086: ctx->ocy == ctx->orlower &&
1.372 nicm 2087: tty_full_width(tty, ctx))
1.306 nicm 2088: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.46 nicm 2089:
1.306 nicm 2090: tty_margin_off(tty);
1.253 nicm 2091: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 2092:
1.421 nicm 2093: tty_cell(tty, ctx->cell, &ctx->defaults, ctx->palette,
2094: ctx->s->hyperlinks);
1.435 nicm 2095:
2096: if (ctx->num == 1)
2097: tty_invalidate(tty);
1.239 nicm 2098: }
2099:
2100: void
2101: tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
2102: {
1.407 nicm 2103: struct overlay_ranges r;
2104: u_int i, px, py, cx;
2105: char *cp = ctx->ptr;
1.394 nicm 2106:
1.309 nicm 2107: if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
2108: return;
2109:
2110: if (ctx->bigger &&
1.372 nicm 2111: (ctx->xoff + ctx->ocx < ctx->wox ||
2112: ctx->xoff + ctx->ocx + ctx->num > ctx->wox + ctx->wsx)) {
1.309 nicm 2113: if (!ctx->wrapped ||
1.372 nicm 2114: !tty_full_width(tty, ctx) ||
1.382 nicm 2115: (tty->term->flags & TERM_NOAM) ||
1.309 nicm 2116: ctx->xoff + ctx->ocx != 0 ||
2117: ctx->yoff + ctx->ocy != tty->cy + 1 ||
2118: tty->cx < tty->sx ||
2119: tty->cy == tty->rlower)
2120: tty_draw_pane(tty, ctx, ctx->ocy);
2121: else
1.373 nicm 2122: ctx->redraw_cb(ctx);
1.309 nicm 2123: return;
2124: }
2125:
1.306 nicm 2126: tty_margin_off(tty);
1.253 nicm 2127: tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.421 nicm 2128: tty_attributes(tty, ctx->cell, &ctx->defaults, ctx->palette, ctx->s->hyperlinks);
1.239 nicm 2129:
1.407 nicm 2130: /* Get tty position from pane position for overlay check. */
2131: px = ctx->xoff + ctx->ocx - ctx->wox;
2132: py = ctx->yoff + ctx->ocy - ctx->woy;
2133:
2134: tty_check_overlay_range(tty, px, py, ctx->num, &r);
1.406 nicm 2135: for (i = 0; i < OVERLAY_MAX_RANGES; i++) {
2136: if (r.nx[i] == 0)
2137: continue;
1.407 nicm 2138: /* Convert back to pane position for printing. */
2139: cx = r.px[i] - ctx->xoff + ctx->wox;
2140: tty_cursor_pane_unless_wrap(tty, ctx, cx, ctx->ocy);
2141: tty_putn(tty, cp + r.px[i] - px, r.nx[i], r.nx[i]);
1.394 nicm 2142: }
1.106 nicm 2143: }
2144:
2145: void
2146: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
2147: {
1.420 nicm 2148: tty_set_selection(tty, ctx->ptr2, ctx->ptr, ctx->num);
1.383 nicm 2149: }
2150:
2151: void
1.420 nicm 2152: tty_set_selection(struct tty *tty, const char *flags, const char *buf,
2153: size_t len)
1.383 nicm 2154: {
2155: char *encoded;
2156: size_t size;
1.106 nicm 2157:
1.384 nicm 2158: if (~tty->flags & TTY_STARTED)
2159: return;
1.106 nicm 2160: if (!tty_term_has(tty->term, TTYC_MS))
1.383 nicm 2161: return;
1.106 nicm 2162:
1.383 nicm 2163: size = 4 * ((len + 2) / 3) + 1; /* storage for base64 */
2164: encoded = xmalloc(size);
1.106 nicm 2165:
1.383 nicm 2166: b64_ntop(buf, len, encoded, size);
1.414 nicm 2167: tty->flags |= TTY_NOBLOCK;
1.430 nicm 2168: tty_putcode_ss(tty, TTYC_MS, flags, encoded);
1.106 nicm 2169:
1.383 nicm 2170: free(encoded);
1.100 nicm 2171: }
2172:
2173: void
2174: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
2175: {
1.414 nicm 2176: tty->flags |= TTY_NOBLOCK;
1.256 nicm 2177: tty_add(tty, ctx->ptr, ctx->num);
1.242 nicm 2178: tty_invalidate(tty);
1.348 nicm 2179: }
2180:
2181: void
1.402 nicm 2182: tty_cmd_syncstart(struct tty *tty, const struct tty_ctx *ctx)
1.348 nicm 2183: {
1.402 nicm 2184: if (ctx->num == 0x11) {
2185: /*
1.403 nicm 2186: * This is an overlay and a command that moves the cursor so
1.402 nicm 2187: * start synchronized updates.
2188: */
2189: tty_sync_start(tty);
2190: } else if (~ctx->num & 0x10) {
2191: /*
2192: * This is a pane. If there is an overlay, always start;
2193: * otherwise, only if requested.
2194: */
2195: if (ctx->num || tty->client->overlay_draw != NULL)
2196: tty_sync_start(tty);
2197: }
1.1 nicm 2198: }
2199:
1.377 nicm 2200: void
1.373 nicm 2201: tty_cell(struct tty *tty, const struct grid_cell *gc,
1.421 nicm 2202: const struct grid_cell *defaults, struct colour_palette *palette,
2203: struct hyperlinks *hl)
1.1 nicm 2204: {
1.298 nicm 2205: const struct grid_cell *gcp;
1.1 nicm 2206:
2207: /* Skip last character if terminal is stupid. */
1.382 nicm 2208: if ((tty->term->flags & TERM_NOAM) &&
1.211 nicm 2209: tty->cy == tty->sy - 1 &&
2210: tty->cx == tty->sx - 1)
1.1 nicm 2211: return;
2212:
2213: /* If this is a padding character, do nothing. */
2214: if (gc->flags & GRID_FLAG_PADDING)
2215: return;
2216:
1.377 nicm 2217: /* Check the output codeset and apply attributes. */
2218: gcp = tty_check_codeset(tty, gc);
1.421 nicm 2219: tty_attributes(tty, gcp, defaults, palette, hl);
1.1 nicm 2220:
1.377 nicm 2221: /* If it is a single character, write with putc to handle ACS. */
1.298 nicm 2222: if (gcp->data.size == 1) {
1.421 nicm 2223: tty_attributes(tty, gcp, defaults, palette, hl);
1.298 nicm 2224: if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
1.1 nicm 2225: return;
1.298 nicm 2226: tty_putc(tty, *gcp->data.data);
1.1 nicm 2227: return;
2228: }
2229:
1.147 nicm 2230: /* Write the data. */
1.298 nicm 2231: tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
1.1 nicm 2232: }
2233:
2234: void
2235: tty_reset(struct tty *tty)
2236: {
2237: struct grid_cell *gc = &tty->cell;
2238:
1.228 nicm 2239: if (!grid_cells_equal(gc, &grid_default_cell)) {
1.421 nicm 2240: if (gc->link != 0)
1.430 nicm 2241: tty_putcode_ss(tty, TTYC_HLS, "", "");
1.285 nicm 2242: if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.228 nicm 2243: tty_putcode(tty, TTYC_RMACS);
2244: tty_putcode(tty, TTYC_SGR0);
2245: memcpy(gc, &grid_default_cell, sizeof *gc);
2246: }
2247: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
1.242 nicm 2248: }
2249:
2250: static void
2251: tty_invalidate(struct tty *tty)
2252: {
2253: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
2254: memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2255:
2256: tty->cx = tty->cy = UINT_MAX;
2257: tty->rupper = tty->rleft = UINT_MAX;
2258: tty->rlower = tty->rright = UINT_MAX;
2259:
2260: if (tty->flags & TTY_STARTED) {
1.330 nicm 2261: if (tty_use_margin(tty))
1.365 nicm 2262: tty_putcode(tty, TTYC_ENMG);
1.242 nicm 2263: tty_putcode(tty, TTYC_SGR0);
2264:
2265: tty->mode = ALL_MODES;
2266: tty_update_mode(tty, MODE_CURSOR, NULL);
2267:
2268: tty_cursor(tty, 0, 0);
2269: tty_region_off(tty);
2270: tty_margin_off(tty);
2271: } else
2272: tty->mode = MODE_CURSOR;
1.1 nicm 2273: }
2274:
1.214 nicm 2275: /* Turn off margin. */
2276: void
2277: tty_region_off(struct tty *tty)
2278: {
2279: tty_region(tty, 0, tty->sy - 1);
2280: }
2281:
1.40 nicm 2282: /* Set region inside pane. */
1.208 nicm 2283: static void
1.196 nicm 2284: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
2285: u_int rlower)
1.1 nicm 2286: {
1.372 nicm 2287: tty_region(tty, ctx->yoff + rupper - ctx->woy,
2288: ctx->yoff + rlower - ctx->woy);
1.39 nicm 2289: }
2290:
1.40 nicm 2291: /* Set region at absolute position. */
1.214 nicm 2292: static void
1.40 nicm 2293: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 2294: {
2295: if (tty->rlower == rlower && tty->rupper == rupper)
2296: return;
1.1 nicm 2297: if (!tty_term_has(tty->term, TTYC_CSR))
2298: return;
1.39 nicm 2299:
2300: tty->rupper = rupper;
2301: tty->rlower = rlower;
1.55 nicm 2302:
2303: /*
2304: * Some terminals (such as PuTTY) do not correctly reset the cursor to
2305: * 0,0 if it is beyond the last column (they do not reset their wrap
2306: * flag so further output causes a line feed). As a workaround, do an
2307: * explicit move to 0 first.
2308: */
1.353 nicm 2309: if (tty->cx >= tty->sx) {
2310: if (tty->cy == UINT_MAX)
2311: tty_cursor(tty, 0, 0);
2312: else
2313: tty_cursor(tty, 0, tty->cy);
2314: }
1.42 nicm 2315:
1.430 nicm 2316: tty_putcode_ii(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.249 nicm 2317: tty->cx = tty->cy = UINT_MAX;
1.212 nicm 2318: }
2319:
1.214 nicm 2320: /* Turn off margin. */
2321: void
2322: tty_margin_off(struct tty *tty)
2323: {
2324: tty_margin(tty, 0, tty->sx - 1);
2325: }
2326:
1.212 nicm 2327: /* Set margin inside pane. */
2328: static void
2329: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
2330: {
1.372 nicm 2331: tty_margin(tty, ctx->xoff - ctx->wox,
1.373 nicm 2332: ctx->xoff + ctx->sx - 1 - ctx->wox);
1.212 nicm 2333: }
2334:
2335: /* Set margin at absolute position. */
1.214 nicm 2336: static void
1.212 nicm 2337: tty_margin(struct tty *tty, u_int rleft, u_int rright)
2338: {
2339: if (!tty_use_margin(tty))
2340: return;
2341: if (tty->rleft == rleft && tty->rright == rright)
2342: return;
2343:
1.430 nicm 2344: tty_putcode_ii(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.231 nicm 2345:
1.212 nicm 2346: tty->rleft = rleft;
2347: tty->rright = rright;
2348:
1.232 nicm 2349: if (rleft == 0 && rright == tty->sx - 1)
1.365 nicm 2350: tty_putcode(tty, TTYC_CLMG);
1.232 nicm 2351: else
1.430 nicm 2352: tty_putcode_ii(tty, TTYC_CMG, rleft, rright);
1.249 nicm 2353: tty->cx = tty->cy = UINT_MAX;
1.253 nicm 2354: }
2355:
2356: /*
2357: * Move the cursor, unless it would wrap itself when the next character is
2358: * printed.
2359: */
2360: static void
2361: tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
2362: u_int cx, u_int cy)
2363: {
1.268 nicm 2364: if (!ctx->wrapped ||
1.372 nicm 2365: !tty_full_width(tty, ctx) ||
1.382 nicm 2366: (tty->term->flags & TERM_NOAM) ||
1.253 nicm 2367: ctx->xoff + cx != 0 ||
2368: ctx->yoff + cy != tty->cy + 1 ||
1.254 nicm 2369: tty->cx < tty->sx ||
2370: tty->cy == tty->rlower)
1.253 nicm 2371: tty_cursor_pane(tty, ctx, cx, cy);
2372: else
2373: log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
1.1 nicm 2374: }
2375:
1.42 nicm 2376: /* Move cursor inside pane. */
1.208 nicm 2377: static void
1.41 nicm 2378: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
2379: {
1.372 nicm 2380: tty_cursor(tty, ctx->xoff + cx - ctx->wox, ctx->yoff + cy - ctx->woy);
1.41 nicm 2381: }
2382:
1.42 nicm 2383: /* Move cursor to absolute position. */
1.41 nicm 2384: void
2385: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 2386: {
1.42 nicm 2387: struct tty_term *term = tty->term;
2388: u_int thisx, thisy;
2389: int change;
1.354 nicm 2390:
2391: if (tty->flags & TTY_BLOCK)
2392: return;
1.77 nicm 2393:
1.42 nicm 2394: thisx = tty->cx;
2395: thisy = tty->cy;
2396:
1.409 nicm 2397: /*
2398: * If in the automargin space, and want to be there, do not move.
2399: * Otherwise, force the cursor to be in range (and complain).
2400: */
2401: if (cx == thisx && cy == thisy && cx == tty->sx)
2402: return;
2403: if (cx > tty->sx - 1) {
2404: log_debug("%s: x too big %u > %u", __func__, cx, tty->sx - 1);
2405: cx = tty->sx - 1;
2406: }
2407:
1.42 nicm 2408: /* No change. */
2409: if (cx == thisx && cy == thisy)
2410: return;
2411:
1.409 nicm 2412: /* Currently at the very end of the line - use absolute movement. */
1.43 nicm 2413: if (thisx > tty->sx - 1)
2414: goto absolute;
2415:
1.42 nicm 2416: /* Move to home position (0, 0). */
2417: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
2418: tty_putcode(tty, TTYC_HOME);
2419: goto out;
2420: }
2421:
2422: /* Zero on the next line. */
1.293 nicm 2423: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
2424: (!tty_use_margin(tty) || tty->rleft == 0)) {
1.1 nicm 2425: tty_putc(tty, '\r');
1.42 nicm 2426: tty_putc(tty, '\n');
2427: goto out;
2428: }
2429:
1.45 nicm 2430: /* Moving column or row. */
1.42 nicm 2431: if (cy == thisy) {
1.45 nicm 2432: /*
2433: * Moving column only, row staying the same.
2434: */
2435:
1.42 nicm 2436: /* To left edge. */
1.294 nicm 2437: if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
1.42 nicm 2438: tty_putc(tty, '\r');
2439: goto out;
2440: }
2441:
2442: /* One to the left. */
2443: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
2444: tty_putcode(tty, TTYC_CUB1);
2445: goto out;
2446: }
2447:
2448: /* One to the right. */
2449: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
2450: tty_putcode(tty, TTYC_CUF1);
2451: goto out;
2452: }
2453:
2454: /* Calculate difference. */
2455: change = thisx - cx; /* +ve left, -ve right */
2456:
2457: /*
2458: * Use HPA if change is larger than absolute, otherwise move
2459: * the cursor with CUB/CUF.
2460: */
1.87 nicm 2461: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.430 nicm 2462: tty_putcode_i(tty, TTYC_HPA, cx);
1.42 nicm 2463: goto out;
1.333 nicm 2464: } else if (change > 0 &&
2465: tty_term_has(term, TTYC_CUB) &&
2466: !tty_use_margin(tty)) {
1.202 nicm 2467: if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
2468: tty_putcode(tty, TTYC_CUB1);
2469: tty_putcode(tty, TTYC_CUB1);
2470: goto out;
2471: }
1.430 nicm 2472: tty_putcode_i(tty, TTYC_CUB, change);
1.42 nicm 2473: goto out;
1.333 nicm 2474: } else if (change < 0 &&
2475: tty_term_has(term, TTYC_CUF) &&
2476: !tty_use_margin(tty)) {
1.430 nicm 2477: tty_putcode_i(tty, TTYC_CUF, -change);
1.42 nicm 2478: goto out;
2479: }
1.45 nicm 2480: } else if (cx == thisx) {
2481: /*
2482: * Moving row only, column staying the same.
2483: */
1.42 nicm 2484:
2485: /* One above. */
1.77 nicm 2486: if (thisy != tty->rupper &&
1.42 nicm 2487: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
2488: tty_putcode(tty, TTYC_CUU1);
2489: goto out;
2490: }
2491:
2492: /* One below. */
1.49 nicm 2493: if (thisy != tty->rlower &&
1.42 nicm 2494: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
2495: tty_putcode(tty, TTYC_CUD1);
2496: goto out;
2497: }
2498:
2499: /* Calculate difference. */
2500: change = thisy - cy; /* +ve up, -ve down */
2501:
2502: /*
1.77 nicm 2503: * Try to use VPA if change is larger than absolute or if this
2504: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 2505: */
1.87 nicm 2506: if ((u_int) abs(change) > cy ||
1.42 nicm 2507: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 2508: (change > 0 && cy - change < tty->rupper)) {
2509: if (tty_term_has(term, TTYC_VPA)) {
1.430 nicm 2510: tty_putcode_i(tty, TTYC_VPA, cy);
1.44 nicm 2511: goto out;
2512: }
1.42 nicm 2513: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1.430 nicm 2514: tty_putcode_i(tty, TTYC_CUU, change);
1.42 nicm 2515: goto out;
2516: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1.430 nicm 2517: tty_putcode_i(tty, TTYC_CUD, -change);
1.42 nicm 2518: goto out;
2519: }
2520: }
2521:
1.43 nicm 2522: absolute:
1.42 nicm 2523: /* Absolute movement. */
1.430 nicm 2524: tty_putcode_ii(tty, TTYC_CUP, cy, cx);
1.42 nicm 2525:
2526: out:
2527: tty->cx = cx;
2528: tty->cy = cy;
1.1 nicm 2529: }
2530:
1.421 nicm 2531: static void
2532: tty_hyperlink(struct tty *tty, const struct grid_cell *gc,
2533: struct hyperlinks *hl)
2534: {
2535: const char *uri, *id;
2536:
2537: if (gc->link == tty->cell.link)
2538: return;
2539: tty->cell.link = gc->link;
2540:
2541: if (hl == NULL)
2542: return;
2543:
1.422 nicm 2544: if (gc->link == 0 || !hyperlinks_get(hl, gc->link, &uri, NULL, &id))
1.430 nicm 2545: tty_putcode_ss(tty, TTYC_HLS, "", "");
1.421 nicm 2546: else
1.430 nicm 2547: tty_putcode_ss(tty, TTYC_HLS, id, uri);
1.421 nicm 2548: }
2549:
1.1 nicm 2550: void
1.176 nicm 2551: tty_attributes(struct tty *tty, const struct grid_cell *gc,
1.421 nicm 2552: const struct grid_cell *defaults, struct colour_palette *palette,
2553: struct hyperlinks *hl)
1.1 nicm 2554: {
1.63 nicm 2555: struct grid_cell *tc = &tty->cell, gc2;
1.255 nicm 2556: int changed;
1.61 nicm 2557:
1.373 nicm 2558: /* Copy cell and update default colours. */
2559: memcpy(&gc2, gc, sizeof gc2);
1.399 nicm 2560: if (~gc->flags & GRID_FLAG_NOPALETTE) {
2561: if (gc2.fg == 8)
2562: gc2.fg = defaults->fg;
2563: if (gc2.bg == 8)
2564: gc2.bg = defaults->bg;
2565: }
1.373 nicm 2566:
1.228 nicm 2567: /* Ignore cell if it is the same as the last one. */
1.373 nicm 2568: if (gc2.attr == tty->last_cell.attr &&
2569: gc2.fg == tty->last_cell.fg &&
2570: gc2.bg == tty->last_cell.bg &&
1.421 nicm 2571: gc2.us == tty->last_cell.us &&
2572: gc2.link == tty->last_cell.link)
1.228 nicm 2573: return;
1.63 nicm 2574:
1.18 nicm 2575: /*
2576: * If no setab, try to use the reverse attribute as a best-effort for a
2577: * non-default background. This is a bit of a hack but it doesn't do
2578: * any serious harm and makes a couple of applications happier.
2579: */
2580: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 2581: if (gc2.attr & GRID_ATTR_REVERSE) {
1.310 nicm 2582: if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
1.64 nicm 2583: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 2584: } else {
1.310 nicm 2585: if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
1.64 nicm 2586: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 2587: }
2588: }
1.1 nicm 2589:
1.85 nicm 2590: /* Fix up the colours if necessary. */
1.373 nicm 2591: tty_check_fg(tty, palette, &gc2);
2592: tty_check_bg(tty, palette, &gc2);
2593: tty_check_us(tty, palette, &gc2);
1.85 nicm 2594:
1.328 nicm 2595: /*
2596: * If any bits are being cleared or the underline colour is now default,
2597: * reset everything.
2598: */
2599: if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
1.64 nicm 2600: tty_reset(tty);
2601:
2602: /*
2603: * Set the colours. This may call tty_reset() (so it comes next) and
1.328 nicm 2604: * may add to (NOT remove) the desired attributes.
1.64 nicm 2605: */
1.85 nicm 2606: tty_colours(tty, &gc2);
1.64 nicm 2607:
1.1 nicm 2608: /* Filter out attribute bits already set. */
1.85 nicm 2609: changed = gc2.attr & ~tc->attr;
2610: tc->attr = gc2.attr;
1.1 nicm 2611:
2612: /* Set the attributes. */
2613: if (changed & GRID_ATTR_BRIGHT)
2614: tty_putcode(tty, TTYC_BOLD);
2615: if (changed & GRID_ATTR_DIM)
2616: tty_putcode(tty, TTYC_DIM);
1.180 nicm 2617: if (changed & GRID_ATTR_ITALICS)
2618: tty_set_italics(tty);
1.308 nicm 2619: if (changed & GRID_ATTR_ALL_UNDERSCORE) {
2620: if ((changed & GRID_ATTR_UNDERSCORE) ||
2621: !tty_term_has(tty->term, TTYC_SMULX))
2622: tty_putcode(tty, TTYC_SMUL);
2623: else if (changed & GRID_ATTR_UNDERSCORE_2)
1.430 nicm 2624: tty_putcode_i(tty, TTYC_SMULX, 2);
1.308 nicm 2625: else if (changed & GRID_ATTR_UNDERSCORE_3)
1.430 nicm 2626: tty_putcode_i(tty, TTYC_SMULX, 3);
1.308 nicm 2627: else if (changed & GRID_ATTR_UNDERSCORE_4)
1.430 nicm 2628: tty_putcode_i(tty, TTYC_SMULX, 4);
1.308 nicm 2629: else if (changed & GRID_ATTR_UNDERSCORE_5)
1.430 nicm 2630: tty_putcode_i(tty, TTYC_SMULX, 5);
1.308 nicm 2631: }
1.1 nicm 2632: if (changed & GRID_ATTR_BLINK)
2633: tty_putcode(tty, TTYC_BLINK);
2634: if (changed & GRID_ATTR_REVERSE) {
2635: if (tty_term_has(tty->term, TTYC_REV))
2636: tty_putcode(tty, TTYC_REV);
2637: else if (tty_term_has(tty->term, TTYC_SMSO))
2638: tty_putcode(tty, TTYC_SMSO);
2639: }
2640: if (changed & GRID_ATTR_HIDDEN)
2641: tty_putcode(tty, TTYC_INVIS);
1.255 nicm 2642: if (changed & GRID_ATTR_STRIKETHROUGH)
2643: tty_putcode(tty, TTYC_SMXX);
1.324 nicm 2644: if (changed & GRID_ATTR_OVERLINE)
2645: tty_putcode(tty, TTYC_SMOL);
1.285 nicm 2646: if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.1 nicm 2647: tty_putcode(tty, TTYC_SMACS);
1.373 nicm 2648:
1.421 nicm 2649: /* Set hyperlink if any. */
2650: tty_hyperlink(tty, gc, hl);
2651:
1.373 nicm 2652: memcpy(&tty->last_cell, &gc2, sizeof tty->last_cell);
1.1 nicm 2653: }
2654:
1.207 nicm 2655: static void
1.85 nicm 2656: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2657: {
1.61 nicm 2658: struct grid_cell *tc = &tty->cell;
1.204 nicm 2659: int have_ax;
1.61 nicm 2660:
2661: /* No changes? Nothing is necessary. */
1.328 nicm 2662: if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
1.63 nicm 2663: return;
1.1 nicm 2664:
1.61 nicm 2665: /*
2666: * Is either the default colour? This is handled specially because the
2667: * best solution might be to reset both colours to default, in which
2668: * case if only one is default need to fall onward to set the other
2669: * colour.
2670: */
1.310 nicm 2671: if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
1.61 nicm 2672: /*
2673: * If don't have AX but do have op, send sgr0 (op can't
2674: * actually be used because it is sometimes the same as sgr0
2675: * and sometimes isn't). This resets both colours to default.
2676: *
2677: * Otherwise, try to set the default colour only as needed.
2678: */
1.174 nicm 2679: have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61 nicm 2680: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
2681: tty_reset(tty);
2682: else {
1.310 nicm 2683: if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
1.61 nicm 2684: if (have_ax)
2685: tty_puts(tty, "\033[39m");
1.204 nicm 2686: else if (tc->fg != 7)
1.430 nicm 2687: tty_putcode_i(tty, TTYC_SETAF, 7);
1.310 nicm 2688: tc->fg = gc->fg;
1.61 nicm 2689: }
1.310 nicm 2690: if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
1.77 nicm 2691: if (have_ax)
1.61 nicm 2692: tty_puts(tty, "\033[49m");
1.204 nicm 2693: else if (tc->bg != 0)
1.430 nicm 2694: tty_putcode_i(tty, TTYC_SETAB, 0);
1.312 nicm 2695: tc->bg = gc->bg;
1.61 nicm 2696: }
2697: }
2698: }
1.1 nicm 2699:
1.61 nicm 2700: /* Set the foreground colour. */
1.310 nicm 2701: if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
1.85 nicm 2702: tty_colours_fg(tty, gc);
1.1 nicm 2703:
1.61 nicm 2704: /*
2705: * Set the background colour. This must come after the foreground as
2706: * tty_colour_fg() can call tty_reset().
2707: */
1.310 nicm 2708: if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
1.73 nicm 2709: tty_colours_bg(tty, gc);
1.328 nicm 2710:
1.399 nicm 2711: /* Set the underscore colour. */
1.328 nicm 2712: if (gc->us != tc->us)
2713: tty_colours_us(tty, gc);
1.1 nicm 2714: }
2715:
1.219 nicm 2716: static void
1.399 nicm 2717: tty_check_fg(struct tty *tty, struct colour_palette *palette,
2718: struct grid_cell *gc)
1.85 nicm 2719: {
1.204 nicm 2720: u_char r, g, b;
2721: u_int colours;
1.223 nicm 2722: int c;
1.85 nicm 2723:
1.288 nicm 2724: /*
2725: * Perform substitution if this pane has a palette. If the bright
1.424 nicm 2726: * attribute is set and Nobr is not present, use the bright entry in
2727: * the palette by changing to the aixterm colour
1.288 nicm 2728: */
2729: if (~gc->flags & GRID_FLAG_NOPALETTE) {
2730: c = gc->fg;
1.424 nicm 2731: if (c < 8 &&
2732: gc->attr & GRID_ATTR_BRIGHT &&
2733: !tty_term_has(tty->term, TTYC_NOBR))
1.288 nicm 2734: c += 90;
1.399 nicm 2735: if ((c = colour_palette_get(palette, c)) != -1)
1.288 nicm 2736: gc->fg = c;
2737: }
1.219 nicm 2738:
1.199 nicm 2739: /* Is this a 24-bit colour? */
1.204 nicm 2740: if (gc->fg & COLOUR_FLAG_RGB) {
1.199 nicm 2741: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.359 nicm 2742: if (tty->term->flags & TERM_RGBCOLOURS)
1.200 nicm 2743: return;
1.341 nicm 2744: colour_split_rgb(gc->fg, &r, &g, &b);
2745: gc->fg = colour_find_rgb(r, g, b);
1.199 nicm 2746: }
1.224 nicm 2747:
2748: /* How many colours does this terminal have? */
1.359 nicm 2749: if (tty->term->flags & TERM_256COLOURS)
1.224 nicm 2750: colours = 256;
2751: else
2752: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 2753:
1.85 nicm 2754: /* Is this a 256-colour colour? */
1.204 nicm 2755: if (gc->fg & COLOUR_FLAG_256) {
1.85 nicm 2756: /* And not a 256 colour mode? */
1.387 nicm 2757: if (colours < 256) {
1.85 nicm 2758: gc->fg = colour_256to16(gc->fg);
2759: if (gc->fg & 8) {
2760: gc->fg &= 7;
1.175 nicm 2761: if (colours >= 16)
2762: gc->fg += 90;
1.332 nicm 2763: }
1.85 nicm 2764: }
2765: return;
2766: }
2767:
2768: /* Is this an aixterm colour? */
2769: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
2770: gc->fg -= 90;
2771: gc->attr |= GRID_ATTR_BRIGHT;
2772: }
2773: }
2774:
1.219 nicm 2775: static void
1.399 nicm 2776: tty_check_bg(struct tty *tty, struct colour_palette *palette,
2777: struct grid_cell *gc)
1.85 nicm 2778: {
1.204 nicm 2779: u_char r, g, b;
2780: u_int colours;
1.223 nicm 2781: int c;
1.85 nicm 2782:
1.288 nicm 2783: /* Perform substitution if this pane has a palette. */
2784: if (~gc->flags & GRID_FLAG_NOPALETTE) {
1.399 nicm 2785: if ((c = colour_palette_get(palette, gc->bg)) != -1)
1.288 nicm 2786: gc->bg = c;
2787: }
1.219 nicm 2788:
1.199 nicm 2789: /* Is this a 24-bit colour? */
1.204 nicm 2790: if (gc->bg & COLOUR_FLAG_RGB) {
1.199 nicm 2791: /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.359 nicm 2792: if (tty->term->flags & TERM_RGBCOLOURS)
1.200 nicm 2793: return;
1.341 nicm 2794: colour_split_rgb(gc->bg, &r, &g, &b);
2795: gc->bg = colour_find_rgb(r, g, b);
1.199 nicm 2796: }
1.224 nicm 2797:
2798: /* How many colours does this terminal have? */
1.359 nicm 2799: if (tty->term->flags & TERM_256COLOURS)
1.224 nicm 2800: colours = 256;
2801: else
2802: colours = tty_term_number(tty->term, TTYC_COLORS);
1.175 nicm 2803:
1.85 nicm 2804: /* Is this a 256-colour colour? */
1.204 nicm 2805: if (gc->bg & COLOUR_FLAG_256) {
1.85 nicm 2806: /*
2807: * And not a 256 colour mode? Translate to 16-colour
2808: * palette. Bold background doesn't exist portably, so just
2809: * discard the bold bit if set.
2810: */
1.387 nicm 2811: if (colours < 256) {
1.85 nicm 2812: gc->bg = colour_256to16(gc->bg);
1.175 nicm 2813: if (gc->bg & 8) {
1.85 nicm 2814: gc->bg &= 7;
1.175 nicm 2815: if (colours >= 16)
1.322 nicm 2816: gc->bg += 90;
1.175 nicm 2817: }
1.85 nicm 2818: }
2819: return;
2820: }
2821:
2822: /* Is this an aixterm colour? */
1.175 nicm 2823: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85 nicm 2824: gc->bg -= 90;
2825: }
2826:
1.207 nicm 2827: static void
1.399 nicm 2828: tty_check_us(__unused struct tty *tty, struct colour_palette *palette,
2829: struct grid_cell *gc)
1.328 nicm 2830: {
2831: int c;
2832:
2833: /* Perform substitution if this pane has a palette. */
2834: if (~gc->flags & GRID_FLAG_NOPALETTE) {
1.399 nicm 2835: if ((c = colour_palette_get(palette, gc->us)) != -1)
1.328 nicm 2836: gc->us = c;
2837: }
2838:
1.433 nicm 2839: /* Convert underscore colour if only RGB can be supported. */
2840: if (!tty_term_has(tty->term, TTYC_SETULC1)) {
2841: if ((c = colour_force_rgb (gc->us)) == -1)
2842: gc->us = 8;
2843: else
2844: gc->us = c;
2845: }
1.328 nicm 2846: }
2847:
2848: static void
1.85 nicm 2849: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2850: {
1.61 nicm 2851: struct grid_cell *tc = &tty->cell;
1.79 nicm 2852: char s[32];
1.1 nicm 2853:
1.204 nicm 2854: /* Is this a 24-bit or 256-colour colour? */
1.302 nicm 2855: if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
1.204 nicm 2856: if (tty_try_colour(tty, gc->fg, "38") == 0)
1.385 nicm 2857: goto save;
1.199 nicm 2858: /* Should not get here, already converted in tty_check_fg. */
1.85 nicm 2859: return;
1.1 nicm 2860: }
2861:
1.79 nicm 2862: /* Is this an aixterm bright colour? */
1.204 nicm 2863: if (gc->fg >= 90 && gc->fg <= 97) {
1.359 nicm 2864: if (tty->term->flags & TERM_256COLOURS) {
1.323 nicm 2865: xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
2866: tty_puts(tty, s);
2867: } else
1.430 nicm 2868: tty_putcode_i(tty, TTYC_SETAF, gc->fg - 90 + 8);
1.385 nicm 2869: goto save;
1.79 nicm 2870: }
2871:
1.61 nicm 2872: /* Otherwise set the foreground colour. */
1.430 nicm 2873: tty_putcode_i(tty, TTYC_SETAF, gc->fg);
1.61 nicm 2874:
1.385 nicm 2875: save:
1.61 nicm 2876: /* Save the new values in the terminal current cell. */
1.204 nicm 2877: tc->fg = gc->fg;
1.1 nicm 2878: }
2879:
1.207 nicm 2880: static void
1.73 nicm 2881: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 2882: {
1.61 nicm 2883: struct grid_cell *tc = &tty->cell;
1.79 nicm 2884: char s[32];
1.1 nicm 2885:
1.204 nicm 2886: /* Is this a 24-bit or 256-colour colour? */
1.302 nicm 2887: if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
1.204 nicm 2888: if (tty_try_colour(tty, gc->bg, "48") == 0)
1.385 nicm 2889: goto save;
1.199 nicm 2890: /* Should not get here, already converted in tty_check_bg. */
1.85 nicm 2891: return;
1.79 nicm 2892: }
2893:
2894: /* Is this an aixterm bright colour? */
1.204 nicm 2895: if (gc->bg >= 90 && gc->bg <= 97) {
1.359 nicm 2896: if (tty->term->flags & TERM_256COLOURS) {
1.323 nicm 2897: xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
2898: tty_puts(tty, s);
2899: } else
1.430 nicm 2900: tty_putcode_i(tty, TTYC_SETAB, gc->bg - 90 + 8);
1.385 nicm 2901: goto save;
1.1 nicm 2902: }
2903:
1.61 nicm 2904: /* Otherwise set the background colour. */
1.430 nicm 2905: tty_putcode_i(tty, TTYC_SETAB, gc->bg);
1.61 nicm 2906:
1.385 nicm 2907: save:
1.61 nicm 2908: /* Save the new values in the terminal current cell. */
1.204 nicm 2909: tc->bg = gc->bg;
1.328 nicm 2910: }
2911:
2912: static void
2913: tty_colours_us(struct tty *tty, const struct grid_cell *gc)
2914: {
2915: struct grid_cell *tc = &tty->cell;
2916: u_int c;
2917: u_char r, g, b;
2918:
1.385 nicm 2919: /* Clear underline colour. */
1.432 nicm 2920: if (COLOUR_DEFAULT(gc->us)) {
1.385 nicm 2921: tty_putcode(tty, TTYC_OL);
2922: goto save;
2923: }
2924:
1.433 nicm 2925: /*
2926: * If this is not an RGB colour, use Setulc1 if it exists, otherwise
2927: * convert.
2928: */
2929: if (~gc->us & COLOUR_FLAG_RGB) {
2930: c = gc->us;
2931: if ((~c & COLOUR_FLAG_256) && (c >= 90 && c <= 97))
2932: c -= 82;
2933: tty_putcode_i(tty, TTYC_SETULC1, c & ~COLOUR_FLAG_256);
1.328 nicm 2934: return;
1.433 nicm 2935: }
1.328 nicm 2936:
2937: /*
1.385 nicm 2938: * Setulc and setal follows the ncurses(3) one argument "direct colour"
1.328 nicm 2939: * capability format. Calculate the colour value.
2940: */
2941: colour_split_rgb(gc->us, &r, &g, &b);
2942: c = (65536 * r) + (256 * g) + b;
2943:
1.385 nicm 2944: /*
2945: * Write the colour. Only use setal if the RGB flag is set because the
2946: * non-RGB version may be wrong.
2947: */
2948: if (tty_term_has(tty->term, TTYC_SETULC))
1.430 nicm 2949: tty_putcode_i(tty, TTYC_SETULC, c);
1.385 nicm 2950: else if (tty_term_has(tty->term, TTYC_SETAL) &&
2951: tty_term_has(tty->term, TTYC_RGB))
1.430 nicm 2952: tty_putcode_i(tty, TTYC_SETAL, c);
1.328 nicm 2953:
1.385 nicm 2954: save:
1.328 nicm 2955: /* Save the new values in the terminal current cell. */
2956: tc->us = gc->us;
1.61 nicm 2957: }
2958:
1.207 nicm 2959: static int
1.204 nicm 2960: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61 nicm 2961: {
1.204 nicm 2962: u_char r, g, b;
1.61 nicm 2963:
1.204 nicm 2964: if (colour & COLOUR_FLAG_256) {
1.359 nicm 2965: if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
1.430 nicm 2966: tty_putcode_i(tty, TTYC_SETAF, colour & 0xff);
1.359 nicm 2967: else if (tty_term_has(tty->term, TTYC_SETAB))
1.430 nicm 2968: tty_putcode_i(tty, TTYC_SETAB, colour & 0xff);
1.359 nicm 2969: return (0);
1.204 nicm 2970: }
2971:
2972: if (colour & COLOUR_FLAG_RGB) {
1.341 nicm 2973: colour_split_rgb(colour & 0xffffff, &r, &g, &b);
1.359 nicm 2974: if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
1.430 nicm 2975: tty_putcode_iii(tty, TTYC_SETRGBF, r, g, b);
1.359 nicm 2976: else if (tty_term_has(tty->term, TTYC_SETRGBB))
1.430 nicm 2977: tty_putcode_iii(tty, TTYC_SETRGBB, r, g, b);
1.165 nicm 2978: return (0);
2979: }
1.61 nicm 2980:
1.165 nicm 2981: return (-1);
1.176 nicm 2982: }
2983:
1.207 nicm 2984: static void
1.371 nicm 2985: tty_window_default_style(struct grid_cell *gc, struct window_pane *wp)
2986: {
2987: memcpy(gc, &grid_default_cell, sizeof *gc);
1.399 nicm 2988: gc->fg = wp->palette.fg;
2989: gc->bg = wp->palette.bg;
1.371 nicm 2990: }
2991:
1.373 nicm 2992: void
1.317 nicm 2993: tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
1.176 nicm 2994: {
1.400 nicm 2995: struct options *oo = wp->options;
2996: struct format_tree *ft;
1.373 nicm 2997:
2998: memcpy(gc, &grid_default_cell, sizeof *gc);
1.326 nicm 2999:
3000: if (wp->flags & PANE_STYLECHANGED) {
1.400 nicm 3001: log_debug("%%%u: style changed", wp->id);
1.326 nicm 3002: wp->flags &= ~PANE_STYLECHANGED;
1.371 nicm 3003:
1.400 nicm 3004: ft = format_create(NULL, NULL, FORMAT_PANE|wp->id,
3005: FORMAT_NOJOBS);
3006: format_defaults(ft, NULL, NULL, NULL, wp);
1.371 nicm 3007: tty_window_default_style(&wp->cached_active_gc, wp);
1.400 nicm 3008: style_add(&wp->cached_active_gc, oo, "window-active-style", ft);
1.371 nicm 3009: tty_window_default_style(&wp->cached_gc, wp);
1.400 nicm 3010: style_add(&wp->cached_gc, oo, "window-style", ft);
3011: format_free(ft);
1.203 nicm 3012: }
1.176 nicm 3013:
1.204 nicm 3014: if (gc->fg == 8) {
1.370 nicm 3015: if (wp == wp->window->active && wp->cached_active_gc.fg != 8)
3016: gc->fg = wp->cached_active_gc.fg;
1.204 nicm 3017: else
1.370 nicm 3018: gc->fg = wp->cached_gc.fg;
1.176 nicm 3019: }
3020:
1.204 nicm 3021: if (gc->bg == 8) {
1.370 nicm 3022: if (wp == wp->window->active && wp->cached_active_gc.bg != 8)
3023: gc->bg = wp->cached_active_gc.bg;
1.204 nicm 3024: else
1.370 nicm 3025: gc->bg = wp->cached_gc.bg;
1.176 nicm 3026: }
1.210 nicm 3027: }
3028:
3029: static void
1.373 nicm 3030: tty_default_attributes(struct tty *tty, const struct grid_cell *defaults,
1.421 nicm 3031: struct colour_palette *palette, u_int bg, struct hyperlinks *hl)
1.210 nicm 3032: {
1.373 nicm 3033: struct grid_cell gc;
1.210 nicm 3034:
3035: memcpy(&gc, &grid_default_cell, sizeof gc);
3036: gc.bg = bg;
1.421 nicm 3037: tty_attributes(tty, &gc, defaults, palette, hl);
1.1 nicm 3038: }
1.416 nicm 3039:
3040: static void
1.417 nicm 3041: tty_clipboard_query_callback(__unused int fd, __unused short events, void *data)
1.416 nicm 3042: {
3043: struct tty *tty = data;
1.417 nicm 3044: struct client *c = tty->client;
3045:
3046: c->flags &= ~CLIENT_CLIPBOARDBUFFER;
3047: free(c->clipboard_panes);
3048: c->clipboard_panes = NULL;
3049: c->clipboard_npanes = 0;
1.416 nicm 3050:
3051: tty->flags &= ~TTY_OSC52QUERY;
3052: }
3053:
3054: void
1.417 nicm 3055: tty_clipboard_query(struct tty *tty)
1.416 nicm 3056: {
3057: struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT };
3058:
3059: if ((~tty->flags & TTY_STARTED) || (tty->flags & TTY_OSC52QUERY))
3060: return;
1.430 nicm 3061: tty_putcode_ss(tty, TTYC_MS, "", "?");
1.417 nicm 3062:
1.416 nicm 3063: tty->flags |= TTY_OSC52QUERY;
1.417 nicm 3064: evtimer_set(&tty->clipboard_timer, tty_clipboard_query_callback, tty);
3065: evtimer_add(&tty->clipboard_timer, &tv);
1.416 nicm 3066: }