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