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