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