Annotation of src/usr.bin/tmux/tty.c, Revision 1.173
1.173 ! nicm 1: /* $OpenBSD: tty.c,v 1.172 2015/01/30 12:33:03 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.1 nicm 24: #include <errno.h>
25: #include <fcntl.h>
1.106 nicm 26: #include <resolv.h>
1.42 nicm 27: #include <stdlib.h>
1.1 nicm 28: #include <string.h>
29: #include <termios.h>
30: #include <unistd.h>
31:
32: #include "tmux.h"
33:
1.67 nicm 34: void tty_read_callback(struct bufferevent *, void *);
1.66 nicm 35: void tty_error_callback(struct bufferevent *, short, void *);
36:
1.1 nicm 37: int tty_try_256(struct tty *, u_char, const char *);
38:
1.85 nicm 39: void tty_colours(struct tty *, const struct grid_cell *);
40: void tty_check_fg(struct tty *, struct grid_cell *);
41: void tty_check_bg(struct tty *, struct grid_cell *);
42: void tty_colours_fg(struct tty *, const struct grid_cell *);
1.73 nicm 43: void tty_colours_bg(struct tty *, const struct grid_cell *);
1.1 nicm 44:
1.120 nicm 45: int tty_large_region(struct tty *, const struct tty_ctx *);
1.24 nicm 46: void tty_redraw_region(struct tty *, const struct tty_ctx *);
1.13 nicm 47: void tty_emulate_repeat(
48: struct tty *, enum tty_code_code, enum tty_code_code, u_int);
1.133 nicm 49: void tty_repeat_space(struct tty *, u_int);
1.147 nicm 50: void tty_cell(struct tty *, const struct grid_cell *);
1.1 nicm 51:
1.90 nicm 52: #define tty_use_acs(tty) \
1.131 nicm 53: (tty_term_has((tty)->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
1.90 nicm 54:
1.132 nicm 55: #define tty_pane_full_width(tty, ctx) \
56: ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
57:
1.1 nicm 58: void
1.136 nicm 59: tty_init(struct tty *tty, struct client *c, int fd, char *term)
1.1 nicm 60: {
1.30 nicm 61: char *path;
62:
63: memset(tty, 0, sizeof *tty);
1.23 nicm 64: tty->log_fd = -1;
1.22 nicm 65:
1.9 nicm 66: if (term == NULL || *term == '\0')
1.1 nicm 67: tty->termname = xstrdup("unknown");
68: else
69: tty->termname = xstrdup(term);
1.30 nicm 70: tty->fd = fd;
1.136 nicm 71: tty->client = c;
1.30 nicm 72:
73: if ((path = ttyname(fd)) == NULL)
74: fatalx("ttyname failed");
75: tty->path = xstrdup(path);
1.108 nicm 76: tty->cstyle = 0;
1.107 nicm 77: tty->ccolour = xstrdup("");
1.30 nicm 78:
1.1 nicm 79: tty->flags = 0;
80: tty->term_flags = 0;
1.31 nicm 81: }
82:
1.88 nicm 83: int
1.31 nicm 84: tty_resize(struct tty *tty)
85: {
86: struct winsize ws;
1.88 nicm 87: u_int sx, sy;
1.31 nicm 88:
89: if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88 nicm 90: sx = ws.ws_col;
91: if (sx == 0)
92: sx = 80;
93: sy = ws.ws_row;
94: if (sy == 0)
95: sy = 24;
96: } else {
97: sx = 80;
98: sy = 24;
1.31 nicm 99: }
1.114 nicm 100: if (!tty_set_size(tty, sx, sy))
1.88 nicm 101: return (0);
1.31 nicm 102:
103: tty->cx = UINT_MAX;
104: tty->cy = UINT_MAX;
105:
106: tty->rupper = UINT_MAX;
107: tty->rlower = UINT_MAX;
1.88 nicm 108:
109: /*
110: * If the terminal has been started, reset the actual scroll region and
111: * cursor position, as this may not have happened.
112: */
113: if (tty->flags & TTY_STARTED) {
114: tty_cursor(tty, 0, 0);
115: tty_region(tty, 0, tty->sy - 1);
116: }
117:
1.114 nicm 118: return (1);
119: }
120:
121: int
122: tty_set_size(struct tty *tty, u_int sx, u_int sy) {
123: if (sx == tty->sx && sy == tty->sy)
124: return (0);
125: tty->sx = sx;
126: tty->sy = sy;
1.88 nicm 127: return (1);
1.1 nicm 128: }
129:
130: int
1.166 nicm 131: tty_open(struct tty *tty, char **cause)
1.1 nicm 132: {
1.88 nicm 133: char out[64];
1.30 nicm 134: int fd;
1.1 nicm 135:
1.30 nicm 136: if (debug_level > 3) {
1.88 nicm 137: xsnprintf(out, sizeof out, "tmux-out-%ld.log", (long) getpid());
138: fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1.30 nicm 139: if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
140: fatal("fcntl failed");
141: tty->log_fd = fd;
1.1 nicm 142: }
143:
1.166 nicm 144: tty->term = tty_term_find(tty->termname, tty->fd, cause);
1.21 nicm 145: if (tty->term == NULL) {
146: tty_close(tty);
147: return (-1);
148: }
149: tty->flags |= TTY_OPENED;
1.1 nicm 150:
1.149 nicm 151: tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_TIMER);
1.1 nicm 152:
1.66 nicm 153: tty->event = bufferevent_new(
1.67 nicm 154: tty->fd, tty_read_callback, NULL, tty_error_callback, tty);
1.1 nicm 155:
156: tty_start_tty(tty);
157:
1.148 nicm 158: tty_keys_build(tty);
1.1 nicm 159:
160: return (0);
161: }
162:
163: void
1.67 nicm 164: tty_read_callback(unused struct bufferevent *bufev, void *data)
165: {
166: struct tty *tty = data;
167:
168: while (tty_keys_next(tty))
169: ;
170: }
171:
172: void
1.66 nicm 173: tty_error_callback(
174: unused struct bufferevent *bufev, unused short what, unused void *data)
175: {
176: }
177:
178: void
1.127 nicm 179: tty_init_termios(int fd, struct termios *orig_tio, struct bufferevent *bufev)
1.1 nicm 180: {
1.128 nicm 181: struct termios tio;
1.33 nicm 182:
1.127 nicm 183: if (fd == -1 || tcgetattr(fd, orig_tio) != 0)
1.33 nicm 184: return;
1.1 nicm 185:
1.127 nicm 186: setblocking(fd, 0);
1.34 nicm 187:
1.127 nicm 188: if (bufev != NULL)
189: bufferevent_enable(bufev, EV_READ|EV_WRITE);
1.66 nicm 190:
1.127 nicm 191: memcpy(&tio, orig_tio, sizeof tio);
1.1 nicm 192: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
193: tio.c_iflag |= IGNBRK;
194: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
195: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
1.169 jsg 196: ECHOPRT|ECHOKE|ISIG);
1.1 nicm 197: tio.c_cc[VMIN] = 1;
1.60 deraadt 198: tio.c_cc[VTIME] = 0;
1.127 nicm 199: if (tcsetattr(fd, TCSANOW, &tio) == 0)
200: tcflush(fd, TCIOFLUSH);
201: }
202:
203: void
204: tty_start_tty(struct tty *tty)
205: {
206: tty_init_termios(tty->fd, &tty->tio, tty->event);
1.1 nicm 207:
1.10 nicm 208: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 209:
1.25 nicm 210: tty_putcode(tty, TTYC_SGR0);
211: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
212:
1.76 nicm 213: tty_putcode(tty, TTYC_RMKX);
1.90 nicm 214: if (tty_use_acs(tty))
215: tty_putcode(tty, TTYC_ENACS);
1.1 nicm 216: tty_putcode(tty, TTYC_CLEAR);
217:
218: tty_putcode(tty, TTYC_CNORM);
219: if (tty_term_has(tty->term, TTYC_KMOUS))
1.153 nicm 220: tty_puts(tty, "\033[?1000l\033[?1006l\033[?1005l");
1.121 nicm 221:
1.162 nicm 222: if (tty_term_has(tty->term, TTYC_XT)) {
223: if (options_get_number(&global_options, "focus-events")) {
224: tty->flags |= TTY_FOCUS;
225: tty_puts(tty, "\033[?1004h");
226: }
1.163 nicm 227: tty_puts(tty, "\033[c");
1.162 nicm 228: }
1.1 nicm 229:
230: tty->cx = UINT_MAX;
231: tty->cy = UINT_MAX;
232:
233: tty->rlower = UINT_MAX;
234: tty->rupper = UINT_MAX;
235:
236: tty->mode = MODE_CURSOR;
1.20 nicm 237:
238: tty->flags |= TTY_STARTED;
1.107 nicm 239:
240: tty_force_cursor_colour(tty, "");
1.145 nicm 241: }
242:
243: void
244: tty_set_class(struct tty *tty, u_int class)
245: {
246: if (tty->class != 0)
247: return;
248: tty->class = class;
1.128 nicm 249: }
250:
251: void
1.1 nicm 252: tty_stop_tty(struct tty *tty)
253: {
254: struct winsize ws;
255:
1.20 nicm 256: if (!(tty->flags & TTY_STARTED))
257: return;
258: tty->flags &= ~TTY_STARTED;
259:
1.66 nicm 260: bufferevent_disable(tty->event, EV_READ|EV_WRITE);
261:
1.1 nicm 262: /*
263: * Be flexible about error handling and try not kill the server just
264: * because the fd is invalid. Things like ssh -t can easily leave us
265: * with a dead tty.
266: */
267: if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
268: return;
269: if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
270: return;
271:
272: tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.90 nicm 273: if (tty_use_acs(tty))
274: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1 nicm 275: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
276: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
277: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.160 nicm 278: if (tty_term_has(tty->term, TTYC_SS) && tty->cstyle != 0) {
279: if (tty_term_has(tty->term, TTYC_SE))
280: tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
1.109 nicm 281: else
1.160 nicm 282: tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
1.108 nicm 283: }
1.173 ! nicm 284: if (tty->mode & MODE_BRACKETPASTE)
! 285: tty_raw(tty, "\033[?2004l");
1.107 nicm 286: tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1 nicm 287:
288: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
289: if (tty_term_has(tty->term, TTYC_KMOUS))
1.153 nicm 290: tty_raw(tty, "\033[?1000l\033[?1006l\033[?1005l");
1.151 nicm 291:
1.162 nicm 292: if (tty_term_has(tty->term, TTYC_XT)) {
293: if (tty->flags & TTY_FOCUS) {
294: tty->flags &= ~TTY_FOCUS;
1.172 nicm 295: tty_raw(tty, "\033[?1004l");
1.162 nicm 296: }
297: }
1.10 nicm 298:
299: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150 nicm 300:
301: setblocking(tty->fd, 1);
1.1 nicm 302: }
303:
304: void
1.20 nicm 305: tty_close(struct tty *tty)
1.1 nicm 306: {
307: if (tty->log_fd != -1) {
308: close(tty->log_fd);
309: tty->log_fd = -1;
310: }
311:
1.123 nicm 312: if (event_initialized(&tty->key_timer))
313: evtimer_del(&tty->key_timer);
1.20 nicm 314: tty_stop_tty(tty);
1.1 nicm 315:
1.21 nicm 316: if (tty->flags & TTY_OPENED) {
1.66 nicm 317: bufferevent_free(tty->event);
318:
1.21 nicm 319: tty_term_free(tty->term);
320: tty_keys_free(tty);
321:
322: tty->flags &= ~TTY_OPENED;
323: }
1.1 nicm 324:
1.21 nicm 325: if (tty->fd != -1) {
326: close(tty->fd);
327: tty->fd = -1;
328: }
1.1 nicm 329: }
330:
331: void
1.20 nicm 332: tty_free(struct tty *tty)
1.1 nicm 333: {
1.20 nicm 334: tty_close(tty);
1.1 nicm 335:
1.138 nicm 336: free(tty->ccolour);
1.1 nicm 337: if (tty->path != NULL)
1.138 nicm 338: free(tty->path);
1.1 nicm 339: if (tty->termname != NULL)
1.138 nicm 340: free(tty->termname);
1.1 nicm 341: }
342:
343: void
344: tty_raw(struct tty *tty, const char *s)
345: {
1.150 nicm 346: ssize_t n, slen;
347: u_int i;
348:
349: slen = strlen(s);
350: for (i = 0; i < 5; i++) {
351: n = write(tty->fd, s, slen);
352: if (n >= 0) {
353: s += n;
354: slen -= n;
355: if (slen == 0)
356: break;
357: } else if (n == -1 && errno != EAGAIN)
358: break;
359: usleep(100);
360: }
1.1 nicm 361: }
362:
363: void
364: tty_putcode(struct tty *tty, enum tty_code_code code)
365: {
366: tty_puts(tty, tty_term_string(tty->term, code));
367: }
368:
369: void
370: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
371: {
372: if (a < 0)
373: return;
374: tty_puts(tty, tty_term_string1(tty->term, code, a));
375: }
376:
377: void
378: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
379: {
380: if (a < 0 || b < 0)
381: return;
382: tty_puts(tty, tty_term_string2(tty->term, code, a, b));
383: }
384:
385: void
1.107 nicm 386: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
387: {
388: if (a != NULL)
389: tty_puts(tty, tty_term_ptr1(tty->term, code, a));
390: }
391:
392: void
1.167 nicm 393: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
394: const void *b)
1.106 nicm 395: {
396: if (a != NULL && b != NULL)
397: tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
398: }
399:
400: void
1.1 nicm 401: tty_puts(struct tty *tty, const char *s)
402: {
403: if (*s == '\0')
404: return;
1.66 nicm 405: bufferevent_write(tty->event, s, strlen(s));
1.1 nicm 406:
407: if (tty->log_fd != -1)
408: write(tty->log_fd, s, strlen(s));
409: }
410:
411: void
412: tty_putc(struct tty *tty, u_char ch)
413: {
1.90 nicm 414: const char *acs;
415: u_int sx;
1.1 nicm 416:
1.90 nicm 417: if (tty->cell.attr & GRID_ATTR_CHARSET) {
418: acs = tty_acs_get(tty, ch);
419: if (acs != NULL)
420: bufferevent_write(tty->event, acs, strlen(acs));
421: else
422: bufferevent_write(tty->event, &ch, 1);
423: } else
424: bufferevent_write(tty->event, &ch, 1);
1.1 nicm 425:
426: if (ch >= 0x20 && ch != 0x7f) {
427: sx = tty->sx;
428: if (tty->term->flags & TERM_EARLYWRAP)
429: sx--;
430:
1.46 nicm 431: if (tty->cx >= sx) {
432: tty->cx = 1;
433: if (tty->cy != tty->rlower)
434: tty->cy++;
1.1 nicm 435: } else
436: tty->cx++;
437: }
438:
439: if (tty->log_fd != -1)
440: write(tty->log_fd, &ch, 1);
441: }
442:
443: void
1.147 nicm 444: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5 nicm 445: {
1.147 nicm 446: bufferevent_write(tty->event, buf, len);
1.71 nicm 447: if (tty->log_fd != -1)
1.147 nicm 448: write(tty->log_fd, buf, len);
449: tty->cx += width;
1.5 nicm 450: }
451:
452: void
1.1 nicm 453: tty_set_title(struct tty *tty, const char *title)
454: {
1.105 nicm 455: if (!tty_term_has(tty->term, TTYC_TSL) ||
456: !tty_term_has(tty->term, TTYC_FSL))
1.1 nicm 457: return;
458:
1.105 nicm 459: tty_putcode(tty, TTYC_TSL);
1.1 nicm 460: tty_puts(tty, title);
1.105 nicm 461: tty_putcode(tty, TTYC_FSL);
1.1 nicm 462: }
463:
464: void
1.107 nicm 465: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
466: {
467: if (*ccolour == '\0')
468: tty_putcode(tty, TTYC_CR);
469: else
1.160 nicm 470: tty_putcode_ptr1(tty, TTYC_CS, ccolour);
1.138 nicm 471: free(tty->ccolour);
1.107 nicm 472: tty->ccolour = xstrdup(ccolour);
473: }
474:
475: void
476: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1 nicm 477: {
478: int changed;
479:
1.107 nicm 480: if (strcmp(s->ccolour, tty->ccolour))
481: tty_force_cursor_colour(tty, s->ccolour);
482:
1.1 nicm 483: if (tty->flags & TTY_NOCURSOR)
484: mode &= ~MODE_CURSOR;
485:
486: changed = mode ^ tty->mode;
1.171 nicm 487: if (changed & (MODE_CURSOR|MODE_BLINKING)) {
488: if (mode & MODE_CURSOR) {
489: if (mode & MODE_BLINKING &&
490: tty_term_has(tty->term, TTYC_CVVIS))
491: tty_putcode(tty, TTYC_CVVIS);
492: else
493: tty_putcode(tty, TTYC_CNORM);
494: } else
1.1 nicm 495: tty_putcode(tty, TTYC_CIVIS);
1.108 nicm 496: }
497: if (tty->cstyle != s->cstyle) {
1.160 nicm 498: if (tty_term_has(tty->term, TTYC_SS)) {
1.108 nicm 499: if (s->cstyle == 0 &&
1.160 nicm 500: tty_term_has(tty->term, TTYC_SE))
501: tty_putcode(tty, TTYC_SE);
1.108 nicm 502: else
1.160 nicm 503: tty_putcode1(tty, TTYC_SS, s->cstyle);
1.108 nicm 504: }
505: tty->cstyle = s->cstyle;
1.1 nicm 506: }
1.154 nicm 507: if (changed & (ALL_MOUSE_MODES|MODE_MOUSE_UTF8)) {
1.94 nicm 508: if (mode & ALL_MOUSE_MODES) {
1.153 nicm 509: /*
510: * Enable the UTF-8 (1005) extension if configured to.
511: * Enable the SGR (1006) extension unconditionally, as
512: * this is safe from misinterpretation. Do it in this
513: * order, because in some terminals it's the last one
514: * that takes effect and SGR is the preferred one.
515: */
1.95 nicm 516: if (mode & MODE_MOUSE_UTF8)
517: tty_puts(tty, "\033[?1005h");
1.154 nicm 518: else
519: tty_puts(tty, "\033[?1005l");
1.153 nicm 520: tty_puts(tty, "\033[?1006h");
521:
1.170 nicm 522: if (mode & MODE_MOUSE_BUTTON)
1.94 nicm 523: tty_puts(tty, "\033[?1002h");
1.98 nicm 524: else if (mode & MODE_MOUSE_STANDARD)
525: tty_puts(tty, "\033[?1000h");
1.86 nicm 526: } else {
1.170 nicm 527: if (tty->mode & MODE_MOUSE_BUTTON)
1.94 nicm 528: tty_puts(tty, "\033[?1002l");
1.98 nicm 529: else if (tty->mode & MODE_MOUSE_STANDARD)
530: tty_puts(tty, "\033[?1000l");
1.153 nicm 531:
532: tty_puts(tty, "\033[?1006l");
1.95 nicm 533: if (tty->mode & MODE_MOUSE_UTF8)
534: tty_puts(tty, "\033[?1005l");
1.86 nicm 535: }
1.76 nicm 536: }
537: if (changed & MODE_KKEYPAD) {
538: if (mode & MODE_KKEYPAD)
539: tty_putcode(tty, TTYC_SMKX);
540: else
541: tty_putcode(tty, TTYC_RMKX);
1.115 nicm 542: }
543: if (changed & MODE_BRACKETPASTE) {
544: if (mode & MODE_BRACKETPASTE)
545: tty_puts(tty, "\033[?2004h");
546: else
547: tty_puts(tty, "\033[?2004l");
1.1 nicm 548: }
549: tty->mode = mode;
550: }
551:
552: void
1.168 nicm 553: tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
554: enum tty_code_code code1, u_int n)
1.1 nicm 555: {
556: if (tty_term_has(tty->term, code))
557: tty_putcode1(tty, code, n);
558: else {
559: while (n-- > 0)
560: tty_putcode(tty, code1);
561: }
562: }
563:
1.133 nicm 564: void
565: tty_repeat_space(struct tty *tty, u_int n)
566: {
567: while (n-- > 0)
568: tty_putc(tty, ' ');
569: }
570:
1.1 nicm 571: /*
1.119 nicm 572: * Is the region large enough to be worth redrawing once later rather than
573: * probably several times now? Currently yes if it is more than 50% of the
574: * pane.
575: */
576: int
577: tty_large_region(unused struct tty *tty, const struct tty_ctx *ctx)
578: {
579: struct window_pane *wp = ctx->wp;
580:
581: return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
582: }
583:
584: /*
1.1 nicm 585: * Redraw scroll region using data from screen (already updated). Used when
586: * CSR not supported, or window is a pane that doesn't take up the full
587: * width of the terminal.
588: */
589: void
1.24 nicm 590: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 591: {
1.14 nicm 592: struct window_pane *wp = ctx->wp;
593: struct screen *s = wp->screen;
594: u_int i;
1.1 nicm 595:
596: /*
1.119 nicm 597: * If region is large, schedule a window redraw. In most cases this is
598: * likely to be followed by some more scrolling.
1.1 nicm 599: */
1.119 nicm 600: if (tty_large_region(tty, ctx)) {
1.1 nicm 601: wp->flags |= PANE_REDRAW;
602: return;
603: }
604:
1.14 nicm 605: if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
606: for (i = ctx->ocy; i < screen_size_y(s); i++)
1.113 nicm 607: tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
1.1 nicm 608: } else {
1.14 nicm 609: for (i = ctx->orupper; i <= ctx->orlower; i++)
1.113 nicm 610: tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
1.1 nicm 611: }
612: }
613:
614: void
615: tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
616: {
617: const struct grid_cell *gc;
1.46 nicm 618: struct grid_line *gl;
1.16 nicm 619: struct grid_cell tmpgc;
1.147 nicm 620: struct utf8_data ud;
1.1 nicm 621: u_int i, sx;
622:
1.107 nicm 623: tty_update_mode(tty, tty->mode & ~MODE_CURSOR, s);
1.35 nicm 624:
1.1 nicm 625: sx = screen_size_x(s);
1.19 nicm 626: if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
627: sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1 nicm 628: if (sx > tty->sx)
629: sx = tty->sx;
630:
1.46 nicm 631: /*
632: * Don't move the cursor to the start permission if it will wrap there
1.50 nicm 633: * itself.
1.46 nicm 634: */
635: gl = NULL;
636: if (py != 0)
637: gl = &s->grid->linedata[s->grid->hsize + py - 1];
1.83 nicm 638: if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
1.47 nicm 639: tty->cx < tty->sx || ox != 0 ||
640: (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
1.46 nicm 641: tty_cursor(tty, ox, oy + py);
642:
1.1 nicm 643: for (i = 0; i < sx; i++) {
644: gc = grid_view_peek_cell(s->grid, i, py);
645: if (screen_check_selection(s, i, py)) {
1.16 nicm 646: memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
1.147 nicm 647: grid_cell_get(gc, &ud);
648: grid_cell_set(&tmpgc, &ud);
1.77 nicm 649: tmpgc.flags = gc->flags &
1.38 nicm 650: ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
651: tmpgc.flags |= s->sel.cell.flags &
652: (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.147 nicm 653: tty_cell(tty, &tmpgc);
1.1 nicm 654: } else
1.147 nicm 655: tty_cell(tty, gc);
1.1 nicm 656: }
657:
1.35 nicm 658: if (sx >= tty->sx) {
1.107 nicm 659: tty_update_mode(tty, tty->mode, s);
1.1 nicm 660: return;
1.35 nicm 661: }
1.1 nicm 662: tty_reset(tty);
663:
1.41 nicm 664: tty_cursor(tty, ox + sx, oy + py);
1.126 nicm 665: if (sx != screen_size_x(s) && ox + screen_size_x(s) >= tty->sx &&
1.117 nicm 666: tty_term_has(tty->term, TTYC_EL))
1.1 nicm 667: tty_putcode(tty, TTYC_EL);
1.133 nicm 668: else
669: tty_repeat_space(tty, screen_size_x(s) - sx);
1.107 nicm 670: tty_update_mode(tty, tty->mode, s);
1.15 nicm 671: }
672:
673: void
1.113 nicm 674: tty_write(
675: void (*cmdfn)(struct tty *, const struct tty_ctx *), struct tty_ctx *ctx)
1.15 nicm 676: {
677: struct window_pane *wp = ctx->wp;
678: struct client *c;
679: u_int i;
680:
1.75 nicm 681: /* wp can be NULL if updating the screen but not the terminal. */
1.15 nicm 682: if (wp == NULL)
683: return;
684:
685: if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
686: return;
1.129 nicm 687: if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
1.15 nicm 688: return;
689:
690: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
691: c = ARRAY_ITEM(&clients, i);
1.139 nicm 692: if (c == NULL || c->session == NULL || c->tty.term == NULL)
1.15 nicm 693: continue;
1.142 nicm 694: if (c->flags & CLIENT_SUSPENDED)
695: continue;
696: if (c->tty.flags & TTY_FREEZE)
1.139 nicm 697: continue;
698: if (c->session->curw->window != wp->window)
1.15 nicm 699: continue;
700:
1.139 nicm 701: ctx->xoff = wp->xoff;
702: ctx->yoff = wp->yoff;
703: if (status_at_line(c) == 0)
704: ctx->yoff++;
1.113 nicm 705:
1.139 nicm 706: cmdfn(&c->tty, ctx);
1.1 nicm 707: }
708: }
709:
710: void
1.24 nicm 711: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 712: {
1.77 nicm 713: struct window_pane *wp = ctx->wp;
1.1 nicm 714:
1.135 nicm 715: if (!tty_pane_full_width(tty, ctx)) {
1.113 nicm 716: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 717: return;
718: }
719:
720: tty_reset(tty);
721:
1.77 nicm 722: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 723:
1.1 nicm 724: if (tty_term_has(tty->term, TTYC_ICH) ||
725: tty_term_has(tty->term, TTYC_ICH1))
1.12 nicm 726: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.134 nicm 727: else
1.113 nicm 728: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 729: }
730:
731: void
1.24 nicm 732: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 733: {
1.77 nicm 734: struct window_pane *wp = ctx->wp;
1.1 nicm 735:
1.135 nicm 736: if (!tty_pane_full_width(tty, ctx) ||
1.26 nicm 737: (!tty_term_has(tty->term, TTYC_DCH) &&
738: !tty_term_has(tty->term, TTYC_DCH1))) {
1.113 nicm 739: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 740: return;
741: }
742:
743: tty_reset(tty);
744:
1.77 nicm 745: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 746:
1.26 nicm 747: if (tty_term_has(tty->term, TTYC_DCH) ||
748: tty_term_has(tty->term, TTYC_DCH1))
749: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146 nicm 750: }
751:
752: void
753: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
754: {
755: u_int i;
756:
757: tty_reset(tty);
758:
759: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
760:
761: if (tty_term_has(tty->term, TTYC_ECH))
762: tty_putcode1(tty, TTYC_ECH, ctx->num);
763: else {
764: for (i = 0; i < ctx->num; i++)
765: tty_putc(tty, ' ');
766: }
1.1 nicm 767: }
768:
769: void
1.24 nicm 770: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 771: {
1.132 nicm 772: if (!tty_pane_full_width(tty, ctx) ||
1.77 nicm 773: !tty_term_has(tty->term, TTYC_CSR) ||
1.72 nicm 774: !tty_term_has(tty->term, TTYC_IL1)) {
1.14 nicm 775: tty_redraw_region(tty, ctx);
1.1 nicm 776: return;
777: }
778:
779: tty_reset(tty);
780:
1.77 nicm 781: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
782: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 783:
1.12 nicm 784: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1 nicm 785: }
786:
787: void
1.24 nicm 788: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 789: {
1.132 nicm 790: if (!tty_pane_full_width(tty, ctx) ||
1.72 nicm 791: !tty_term_has(tty->term, TTYC_CSR) ||
792: !tty_term_has(tty->term, TTYC_DL1)) {
1.14 nicm 793: tty_redraw_region(tty, ctx);
1.1 nicm 794: return;
795: }
796:
797: tty_reset(tty);
798:
1.77 nicm 799: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
800: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 801:
1.12 nicm 802: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1 nicm 803: }
804:
805: void
1.24 nicm 806: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 807: {
1.77 nicm 808: struct window_pane *wp = ctx->wp;
1.12 nicm 809: struct screen *s = wp->screen;
1.1 nicm 810:
811: tty_reset(tty);
812:
1.77 nicm 813: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41 nicm 814:
1.133 nicm 815: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
1.1 nicm 816: tty_putcode(tty, TTYC_EL);
1.133 nicm 817: else
818: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 819: }
820:
821: void
1.24 nicm 822: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 823: {
1.77 nicm 824: struct window_pane *wp = ctx->wp;
1.12 nicm 825: struct screen *s = wp->screen;
1.1 nicm 826:
827: tty_reset(tty);
828:
1.41 nicm 829: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
830:
1.132 nicm 831: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
1.1 nicm 832: tty_putcode(tty, TTYC_EL);
1.133 nicm 833: else
834: tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.1 nicm 835: }
836:
837: void
1.24 nicm 838: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 839: {
840: tty_reset(tty);
841:
1.113 nicm 842: if (ctx->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.41 nicm 843: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 844: tty_putcode(tty, TTYC_EL1);
845: } else {
1.41 nicm 846: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133 nicm 847: tty_repeat_space(tty, ctx->ocx + 1);
1.1 nicm 848: }
849: }
850:
851: void
1.24 nicm 852: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 853: {
1.56 nicm 854: if (ctx->ocy != ctx->orupper)
855: return;
856:
1.132 nicm 857: if (!tty_pane_full_width(tty, ctx) ||
1.70 nicm 858: !tty_term_has(tty->term, TTYC_CSR) ||
859: !tty_term_has(tty->term, TTYC_RI)) {
1.14 nicm 860: tty_redraw_region(tty, ctx);
1.1 nicm 861: return;
862: }
863:
1.56 nicm 864: tty_reset(tty);
1.77 nicm 865:
1.56 nicm 866: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
867: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 868:
1.56 nicm 869: tty_putcode(tty, TTYC_RI);
1.1 nicm 870: }
871:
872: void
1.24 nicm 873: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 874: {
1.77 nicm 875: struct window_pane *wp = ctx->wp;
1.1 nicm 876:
1.56 nicm 877: if (ctx->ocy != ctx->orlower)
878: return;
879:
1.132 nicm 880: if (!tty_pane_full_width(tty, ctx) ||
1.1 nicm 881: !tty_term_has(tty->term, TTYC_CSR)) {
1.128 nicm 882: if (tty_large_region(tty, ctx))
883: wp->flags |= PANE_REDRAW;
1.140 nicm 884: else
1.128 nicm 885: tty_redraw_region(tty, ctx);
1.1 nicm 886: return;
887: }
1.52 nicm 888:
889: /*
890: * If this line wrapped naturally (ctx->num is nonzero), don't do
891: * anything - the cursor can just be moved to the last cell and wrap
892: * naturally.
893: */
894: if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
895: return;
1.1 nicm 896:
1.56 nicm 897: tty_reset(tty);
1.77 nicm 898:
1.56 nicm 899: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
900: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 901:
1.56 nicm 902: tty_putc(tty, '\n');
1.1 nicm 903: }
904:
905: void
1.24 nicm 906: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 907: {
1.77 nicm 908: struct window_pane *wp = ctx->wp;
1.12 nicm 909: struct screen *s = wp->screen;
910: u_int i, j;
1.1 nicm 911:
912: tty_reset(tty);
913:
1.39 nicm 914: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 915: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39 nicm 916:
1.132 nicm 917: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.1 nicm 918: tty_putcode(tty, TTYC_EL);
1.14 nicm 919: if (ctx->ocy != screen_size_y(s) - 1) {
1.41 nicm 920: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14 nicm 921: for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1 nicm 922: tty_putcode(tty, TTYC_EL);
923: if (i == screen_size_y(s) - 1)
924: continue;
925: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
926: tty->cy++;
927: }
928: }
929: } else {
1.133 nicm 930: tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.68 nicm 931: for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
1.41 nicm 932: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 933: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 934: }
935: }
936: }
937:
938: void
1.24 nicm 939: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 940: {
1.77 nicm 941: struct window_pane *wp = ctx->wp;
1.12 nicm 942: struct screen *s = wp->screen;
943: u_int i, j;
1.1 nicm 944:
945: tty_reset(tty);
946:
1.39 nicm 947: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 948: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 949:
1.132 nicm 950: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.14 nicm 951: for (i = 0; i < ctx->ocy; i++) {
1.1 nicm 952: tty_putcode(tty, TTYC_EL);
953: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
954: tty->cy++;
955: }
956: } else {
1.14 nicm 957: for (j = 0; j < ctx->ocy; j++) {
1.41 nicm 958: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 959: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 960: }
961: }
1.133 nicm 962: tty_repeat_space(tty, ctx->ocx + 1);
1.1 nicm 963: }
964:
965: void
1.24 nicm 966: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 967: {
1.77 nicm 968: struct window_pane *wp = ctx->wp;
1.12 nicm 969: struct screen *s = wp->screen;
970: u_int i, j;
1.1 nicm 971:
972: tty_reset(tty);
973:
1.39 nicm 974: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 975: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 976:
1.132 nicm 977: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.1 nicm 978: for (i = 0; i < screen_size_y(s); i++) {
979: tty_putcode(tty, TTYC_EL);
980: if (i != screen_size_y(s) - 1) {
981: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
982: tty->cy++;
983: }
984: }
985: } else {
986: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 987: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 988: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 989: }
1.4 nicm 990: }
991: }
992:
993: void
1.24 nicm 994: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 995: {
1.12 nicm 996: struct window_pane *wp = ctx->wp;
997: struct screen *s = wp->screen;
998: u_int i, j;
1.4 nicm 999:
1000: tty_reset(tty);
1001:
1.39 nicm 1002: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4 nicm 1003:
1004: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 1005: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 1006: for (i = 0; i < screen_size_x(s); i++)
1007: tty_putc(tty, 'E');
1.1 nicm 1008: }
1009: }
1010:
1011: void
1.24 nicm 1012: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1013: {
1.46 nicm 1014: struct window_pane *wp = ctx->wp;
1015: struct screen *s = wp->screen;
1.58 nicm 1016: u_int cx;
1.102 nicm 1017: u_int width;
1.46 nicm 1018:
1019: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1020:
1.57 nicm 1021: /* Is the cursor in the very last position? */
1.147 nicm 1022: width = grid_cell_width(ctx->cell);
1.102 nicm 1023: if (ctx->ocx > wp->sx - width) {
1.113 nicm 1024: if (ctx->xoff != 0 || wp->sx != tty->sx) {
1.57 nicm 1025: /*
1026: * The pane doesn't fill the entire line, the linefeed
1027: * will already have happened, so just move the cursor.
1028: */
1.125 nicm 1029: if (ctx->ocy != wp->yoff + wp->screen->rlower)
1.122 nicm 1030: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1031: else
1032: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.57 nicm 1033: } else if (tty->cx < tty->sx) {
1034: /*
1035: * The cursor isn't in the last position already, so
1.102 nicm 1036: * move as far left as possible and redraw the last
1.57 nicm 1037: * cell to move into the last position.
1038: */
1.147 nicm 1039: cx = screen_size_x(s) - grid_cell_width(&ctx->last_cell);
1.50 nicm 1040: tty_cursor_pane(tty, ctx, cx, ctx->ocy);
1.147 nicm 1041: tty_cell(tty, &ctx->last_cell);
1.50 nicm 1042: }
1043: } else
1.46 nicm 1044: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1045:
1.147 nicm 1046: tty_cell(tty, ctx->cell);
1.1 nicm 1047: }
1048:
1049: void
1.24 nicm 1050: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1051: {
1.53 nicm 1052: struct window_pane *wp = ctx->wp;
1.1 nicm 1053:
1.53 nicm 1054: /*
1055: * Cannot rely on not being a partial character, so just redraw the
1056: * whole line.
1057: */
1.113 nicm 1058: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.106 nicm 1059: }
1060:
1061: void
1062: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1063: {
1064: char *buf;
1065: size_t off;
1066:
1067: if (!tty_term_has(tty->term, TTYC_MS))
1068: return;
1069:
1070: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1071: buf = xmalloc(off);
1072:
1073: b64_ntop(ctx->ptr, ctx->num, buf, off);
1074: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1075:
1.138 nicm 1076: free(buf);
1.100 nicm 1077: }
1078:
1079: void
1080: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1081: {
1082: u_int i;
1083: u_char *str = ctx->ptr;
1084:
1085: for (i = 0; i < ctx->num; i++)
1086: tty_putc(tty, str[i]);
1.141 nicm 1087:
1088: tty->cx = tty->cy = UINT_MAX;
1089: tty->rupper = tty->rlower = UINT_MAX;
1090:
1091: tty_reset(tty);
1092: tty_cursor(tty, 0, 0);
1.1 nicm 1093: }
1094:
1095: void
1.147 nicm 1096: tty_cell(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1097: {
1.147 nicm 1098: struct utf8_data ud;
1099: u_int i;
1.1 nicm 1100:
1101: /* Skip last character if terminal is stupid. */
1102: if (tty->term->flags & TERM_EARLYWRAP &&
1103: tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
1104: return;
1105:
1106: /* If this is a padding character, do nothing. */
1107: if (gc->flags & GRID_FLAG_PADDING)
1108: return;
1109:
1110: /* Set the attributes. */
1111: tty_attributes(tty, gc);
1112:
1.147 nicm 1113: /* Get the cell and if ASCII write with putc to do ACS translation. */
1114: grid_cell_get(gc, &ud);
1115: if (ud.size == 1) {
1116: if (*ud.data < 0x20 || *ud.data == 0x7f)
1.1 nicm 1117: return;
1.147 nicm 1118: tty_putc(tty, *ud.data);
1.1 nicm 1119: return;
1120: }
1121:
1.147 nicm 1122: /* If not UTF-8, write _. */
1.1 nicm 1123: if (!(tty->flags & TTY_UTF8)) {
1.147 nicm 1124: for (i = 0; i < ud.width; i++)
1.1 nicm 1125: tty_putc(tty, '_');
1126: return;
1127: }
1128:
1.147 nicm 1129: /* Write the data. */
1130: tty_putn(tty, ud.data, ud.size, ud.width);
1.1 nicm 1131: }
1132:
1133: void
1134: tty_reset(struct tty *tty)
1135: {
1136: struct grid_cell *gc = &tty->cell;
1137:
1138: if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
1139: return;
1140:
1.90 nicm 1141: if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1142: tty_putcode(tty, TTYC_RMACS);
1143: tty_putcode(tty, TTYC_SGR0);
1144: memcpy(gc, &grid_default_cell, sizeof *gc);
1145: }
1146:
1.40 nicm 1147: /* Set region inside pane. */
1.1 nicm 1148: void
1.39 nicm 1149: tty_region_pane(
1150: struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1.1 nicm 1151: {
1.113 nicm 1152: tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39 nicm 1153: }
1154:
1.40 nicm 1155: /* Set region at absolute position. */
1.39 nicm 1156: void
1.40 nicm 1157: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 1158: {
1159: if (tty->rlower == rlower && tty->rupper == rupper)
1160: return;
1.1 nicm 1161: if (!tty_term_has(tty->term, TTYC_CSR))
1162: return;
1.39 nicm 1163:
1164: tty->rupper = rupper;
1165: tty->rlower = rlower;
1.55 nicm 1166:
1167: /*
1168: * Some terminals (such as PuTTY) do not correctly reset the cursor to
1169: * 0,0 if it is beyond the last column (they do not reset their wrap
1170: * flag so further output causes a line feed). As a workaround, do an
1171: * explicit move to 0 first.
1172: */
1173: if (tty->cx >= tty->sx)
1174: tty_cursor(tty, 0, tty->cy);
1.42 nicm 1175:
1.39 nicm 1176: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.78 nicm 1177: tty_cursor(tty, 0, 0);
1.1 nicm 1178: }
1179:
1.42 nicm 1180: /* Move cursor inside pane. */
1.1 nicm 1181: void
1.41 nicm 1182: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1183: {
1.113 nicm 1184: tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41 nicm 1185: }
1186:
1.42 nicm 1187: /* Move cursor to absolute position. */
1.41 nicm 1188: void
1189: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 1190: {
1.42 nicm 1191: struct tty_term *term = tty->term;
1192: u_int thisx, thisy;
1193: int change;
1.77 nicm 1194:
1.42 nicm 1195: if (cx > tty->sx - 1)
1196: cx = tty->sx - 1;
1197:
1198: thisx = tty->cx;
1199: thisy = tty->cy;
1200:
1201: /* No change. */
1202: if (cx == thisx && cy == thisy)
1203: return;
1204:
1.43 nicm 1205: /* Very end of the line, just use absolute movement. */
1206: if (thisx > tty->sx - 1)
1207: goto absolute;
1208:
1.42 nicm 1209: /* Move to home position (0, 0). */
1210: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1211: tty_putcode(tty, TTYC_HOME);
1212: goto out;
1213: }
1214:
1215: /* Zero on the next line. */
1.48 nicm 1216: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1 nicm 1217: tty_putc(tty, '\r');
1.42 nicm 1218: tty_putc(tty, '\n');
1219: goto out;
1220: }
1221:
1.45 nicm 1222: /* Moving column or row. */
1.42 nicm 1223: if (cy == thisy) {
1.45 nicm 1224: /*
1225: * Moving column only, row staying the same.
1226: */
1227:
1.42 nicm 1228: /* To left edge. */
1229: if (cx == 0) {
1230: tty_putc(tty, '\r');
1231: goto out;
1232: }
1233:
1234: /* One to the left. */
1235: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1236: tty_putcode(tty, TTYC_CUB1);
1237: goto out;
1238: }
1239:
1240: /* One to the right. */
1241: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1242: tty_putcode(tty, TTYC_CUF1);
1243: goto out;
1244: }
1245:
1246: /* Calculate difference. */
1247: change = thisx - cx; /* +ve left, -ve right */
1248:
1249: /*
1250: * Use HPA if change is larger than absolute, otherwise move
1251: * the cursor with CUB/CUF.
1252: */
1.87 nicm 1253: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 1254: tty_putcode1(tty, TTYC_HPA, cx);
1255: goto out;
1256: } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1257: tty_putcode1(tty, TTYC_CUB, change);
1258: goto out;
1259: } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1260: tty_putcode1(tty, TTYC_CUF, -change);
1261: goto out;
1262: }
1.45 nicm 1263: } else if (cx == thisx) {
1264: /*
1265: * Moving row only, column staying the same.
1266: */
1.42 nicm 1267:
1268: /* One above. */
1.77 nicm 1269: if (thisy != tty->rupper &&
1.42 nicm 1270: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1271: tty_putcode(tty, TTYC_CUU1);
1272: goto out;
1273: }
1274:
1275: /* One below. */
1.49 nicm 1276: if (thisy != tty->rlower &&
1.42 nicm 1277: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1278: tty_putcode(tty, TTYC_CUD1);
1279: goto out;
1280: }
1281:
1282: /* Calculate difference. */
1283: change = thisy - cy; /* +ve up, -ve down */
1284:
1285: /*
1.77 nicm 1286: * Try to use VPA if change is larger than absolute or if this
1287: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 1288: */
1.87 nicm 1289: if ((u_int) abs(change) > cy ||
1.42 nicm 1290: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 1291: (change > 0 && cy - change < tty->rupper)) {
1292: if (tty_term_has(term, TTYC_VPA)) {
1293: tty_putcode1(tty, TTYC_VPA, cy);
1294: goto out;
1295: }
1.42 nicm 1296: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1297: tty_putcode1(tty, TTYC_CUU, change);
1298: goto out;
1299: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1300: tty_putcode1(tty, TTYC_CUD, -change);
1301: goto out;
1302: }
1303: }
1304:
1.43 nicm 1305: absolute:
1.42 nicm 1306: /* Absolute movement. */
1307: tty_putcode2(tty, TTYC_CUP, cy, cx);
1308:
1309: out:
1310: tty->cx = cx;
1311: tty->cy = cy;
1.1 nicm 1312: }
1313:
1314: void
1315: tty_attributes(struct tty *tty, const struct grid_cell *gc)
1316: {
1.63 nicm 1317: struct grid_cell *tc = &tty->cell, gc2;
1.85 nicm 1318: u_char changed;
1.61 nicm 1319:
1.85 nicm 1320: memcpy(&gc2, gc, sizeof gc2);
1.63 nicm 1321:
1.18 nicm 1322: /*
1323: * If no setab, try to use the reverse attribute as a best-effort for a
1324: * non-default background. This is a bit of a hack but it doesn't do
1325: * any serious harm and makes a couple of applications happier.
1326: */
1327: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 1328: if (gc2.attr & GRID_ATTR_REVERSE) {
1329: if (gc2.fg != 7 && gc2.fg != 8)
1.64 nicm 1330: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 1331: } else {
1.85 nicm 1332: if (gc2.bg != 0 && gc2.bg != 8)
1.64 nicm 1333: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 1334: }
1335: }
1.1 nicm 1336:
1.85 nicm 1337: /* Fix up the colours if necessary. */
1338: tty_check_fg(tty, &gc2);
1339: tty_check_bg(tty, &gc2);
1340:
1.64 nicm 1341: /* If any bits are being cleared, reset everything. */
1.85 nicm 1342: if (tc->attr & ~gc2.attr)
1.64 nicm 1343: tty_reset(tty);
1344:
1345: /*
1346: * Set the colours. This may call tty_reset() (so it comes next) and
1347: * may add to (NOT remove) the desired attributes by changing new_attr.
1348: */
1.85 nicm 1349: tty_colours(tty, &gc2);
1.64 nicm 1350:
1.1 nicm 1351: /* Filter out attribute bits already set. */
1.85 nicm 1352: changed = gc2.attr & ~tc->attr;
1353: tc->attr = gc2.attr;
1.1 nicm 1354:
1355: /* Set the attributes. */
1356: if (changed & GRID_ATTR_BRIGHT)
1357: tty_putcode(tty, TTYC_BOLD);
1358: if (changed & GRID_ATTR_DIM)
1359: tty_putcode(tty, TTYC_DIM);
1.164 nicm 1360: if (changed & GRID_ATTR_ITALICS) {
1.104 nicm 1361: if (tty_term_has(tty->term, TTYC_SITM))
1362: tty_putcode(tty, TTYC_SITM);
1363: else
1364: tty_putcode(tty, TTYC_SMSO);
1365: }
1.1 nicm 1366: if (changed & GRID_ATTR_UNDERSCORE)
1367: tty_putcode(tty, TTYC_SMUL);
1368: if (changed & GRID_ATTR_BLINK)
1369: tty_putcode(tty, TTYC_BLINK);
1370: if (changed & GRID_ATTR_REVERSE) {
1371: if (tty_term_has(tty->term, TTYC_REV))
1372: tty_putcode(tty, TTYC_REV);
1373: else if (tty_term_has(tty->term, TTYC_SMSO))
1374: tty_putcode(tty, TTYC_SMSO);
1375: }
1376: if (changed & GRID_ATTR_HIDDEN)
1377: tty_putcode(tty, TTYC_INVIS);
1.90 nicm 1378: if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1379: tty_putcode(tty, TTYC_SMACS);
1380: }
1381:
1.61 nicm 1382: void
1.85 nicm 1383: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1384: {
1.61 nicm 1385: struct grid_cell *tc = &tty->cell;
1.62 nicm 1386: u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
1387: int have_ax, fg_default, bg_default;
1.61 nicm 1388:
1389: /* No changes? Nothing is necessary. */
1.62 nicm 1390: if (fg == tc->fg && bg == tc->bg &&
1391: ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1.63 nicm 1392: return;
1.1 nicm 1393:
1.61 nicm 1394: /*
1395: * Is either the default colour? This is handled specially because the
1396: * best solution might be to reset both colours to default, in which
1397: * case if only one is default need to fall onward to set the other
1398: * colour.
1399: */
1.62 nicm 1400: fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
1401: bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1.61 nicm 1402: if (fg_default || bg_default) {
1403: /*
1404: * If don't have AX but do have op, send sgr0 (op can't
1405: * actually be used because it is sometimes the same as sgr0
1406: * and sometimes isn't). This resets both colours to default.
1407: *
1408: * Otherwise, try to set the default colour only as needed.
1409: */
1410: have_ax = tty_term_has(tty->term, TTYC_AX);
1411: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1412: tty_reset(tty);
1413: else {
1414: if (fg_default &&
1.81 nicm 1415: (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1.61 nicm 1416: if (have_ax)
1417: tty_puts(tty, "\033[39m");
1.81 nicm 1418: else if (tc->fg != 7 ||
1419: tc->flags & GRID_FLAG_FG256)
1.61 nicm 1420: tty_putcode1(tty, TTYC_SETAF, 7);
1421: tc->fg = 8;
1422: tc->flags &= ~GRID_FLAG_FG256;
1423: }
1424: if (bg_default &&
1.81 nicm 1425: (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1.77 nicm 1426: if (have_ax)
1.61 nicm 1427: tty_puts(tty, "\033[49m");
1.81 nicm 1428: else if (tc->bg != 0 ||
1429: tc->flags & GRID_FLAG_BG256)
1.61 nicm 1430: tty_putcode1(tty, TTYC_SETAB, 0);
1431: tc->bg = 8;
1432: tc->flags &= ~GRID_FLAG_BG256;
1433: }
1434: }
1435: }
1.1 nicm 1436:
1.61 nicm 1437: /* Set the foreground colour. */
1438: if (!fg_default && (fg != tc->fg ||
1.62 nicm 1439: ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1.85 nicm 1440: tty_colours_fg(tty, gc);
1.1 nicm 1441:
1.61 nicm 1442: /*
1443: * Set the background colour. This must come after the foreground as
1444: * tty_colour_fg() can call tty_reset().
1445: */
1446: if (!bg_default && (bg != tc->bg ||
1.62 nicm 1447: ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1.73 nicm 1448: tty_colours_bg(tty, gc);
1.1 nicm 1449: }
1450:
1451: void
1.85 nicm 1452: tty_check_fg(struct tty *tty, struct grid_cell *gc)
1453: {
1454: u_int colours;
1455:
1456: /* Is this a 256-colour colour? */
1457: if (gc->flags & GRID_FLAG_FG256) {
1458: /* And not a 256 colour mode? */
1.158 nicm 1459: if (!(tty->term->flags & TERM_256COLOURS) &&
1.85 nicm 1460: !(tty->term_flags & TERM_256COLOURS)) {
1461: gc->fg = colour_256to16(gc->fg);
1462: if (gc->fg & 8) {
1463: gc->fg &= 7;
1464: gc->attr |= GRID_ATTR_BRIGHT;
1465: } else
1466: gc->attr &= ~GRID_ATTR_BRIGHT;
1467: gc->flags &= ~GRID_FLAG_FG256;
1468: }
1469: return;
1470: }
1471:
1472: /* Is this an aixterm colour? */
1473: colours = tty_term_number(tty->term, TTYC_COLORS);
1474: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1475: gc->fg -= 90;
1476: gc->attr |= GRID_ATTR_BRIGHT;
1477: }
1478: }
1479:
1480: void
1481: tty_check_bg(struct tty *tty, struct grid_cell *gc)
1482: {
1483: u_int colours;
1484:
1485: /* Is this a 256-colour colour? */
1486: if (gc->flags & GRID_FLAG_BG256) {
1487: /*
1488: * And not a 256 colour mode? Translate to 16-colour
1489: * palette. Bold background doesn't exist portably, so just
1490: * discard the bold bit if set.
1491: */
1.158 nicm 1492: if (!(tty->term->flags & TERM_256COLOURS) &&
1.85 nicm 1493: !(tty->term_flags & TERM_256COLOURS)) {
1494: gc->bg = colour_256to16(gc->bg);
1495: if (gc->bg & 8)
1496: gc->bg &= 7;
1497: gc->attr &= ~GRID_ATTR_BRIGHT;
1498: gc->flags &= ~GRID_FLAG_BG256;
1499: }
1500: return;
1501: }
1502:
1503: /* Is this an aixterm colour? */
1504: colours = tty_term_number(tty->term, TTYC_COLORS);
1.112 nicm 1505: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16) {
1.85 nicm 1506: gc->bg -= 90;
1507: gc->attr |= GRID_ATTR_BRIGHT;
1508: }
1509: }
1510:
1511: void
1512: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1513: {
1.61 nicm 1514: struct grid_cell *tc = &tty->cell;
1515: u_char fg = gc->fg;
1.79 nicm 1516: char s[32];
1.1 nicm 1517:
1.61 nicm 1518: /* Is this a 256-colour colour? */
1.1 nicm 1519: if (gc->flags & GRID_FLAG_FG256) {
1.158 nicm 1520: /* Try as 256 colours. */
1.1 nicm 1521: if (tty_try_256(tty, fg, "38") == 0)
1.61 nicm 1522: goto save_fg;
1.85 nicm 1523: /* Else already handled by tty_check_fg. */
1524: return;
1.1 nicm 1525: }
1526:
1.79 nicm 1527: /* Is this an aixterm bright colour? */
1528: if (fg >= 90 && fg <= 97) {
1.85 nicm 1529: xsnprintf(s, sizeof s, "\033[%dm", fg);
1530: tty_puts(tty, s);
1531: goto save_fg;
1.79 nicm 1532: }
1533:
1.61 nicm 1534: /* Otherwise set the foreground colour. */
1535: tty_putcode1(tty, TTYC_SETAF, fg);
1536:
1.77 nicm 1537: save_fg:
1.61 nicm 1538: /* Save the new values in the terminal current cell. */
1539: tc->fg = fg;
1540: tc->flags &= ~GRID_FLAG_FG256;
1541: tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1 nicm 1542: }
1543:
1544: void
1.73 nicm 1545: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1546: {
1.61 nicm 1547: struct grid_cell *tc = &tty->cell;
1548: u_char bg = gc->bg;
1.79 nicm 1549: char s[32];
1.1 nicm 1550:
1.61 nicm 1551: /* Is this a 256-colour colour? */
1.1 nicm 1552: if (gc->flags & GRID_FLAG_BG256) {
1.158 nicm 1553: /* Try as 256 colours. */
1.1 nicm 1554: if (tty_try_256(tty, bg, "48") == 0)
1.61 nicm 1555: goto save_bg;
1.85 nicm 1556: /* Else already handled by tty_check_bg. */
1557: return;
1.79 nicm 1558: }
1559:
1560: /* Is this an aixterm bright colour? */
1.112 nicm 1561: if (bg >= 90 && bg <= 97) {
1.79 nicm 1562: /* 16 colour terminals or above only. */
1563: if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
1.112 nicm 1564: xsnprintf(s, sizeof s, "\033[%dm", bg + 10);
1.79 nicm 1565: tty_puts(tty, s);
1566: goto save_bg;
1567: }
1.112 nicm 1568: bg -= 90;
1.79 nicm 1569: /* no such thing as a bold background */
1.1 nicm 1570: }
1571:
1.61 nicm 1572: /* Otherwise set the background colour. */
1573: tty_putcode1(tty, TTYC_SETAB, bg);
1574:
1575: save_bg:
1576: /* Save the new values in the terminal current cell. */
1577: tc->bg = bg;
1578: tc->flags &= ~GRID_FLAG_BG256;
1579: tc->flags |= gc->flags & GRID_FLAG_BG256;
1580: }
1581:
1582: int
1583: tty_try_256(struct tty *tty, u_char colour, const char *type)
1584: {
1585: char s[32];
1586:
1.165 nicm 1587: /*
1588: * If the terminfo entry has 256 colours, assume that setaf and setab
1589: * work correctly.
1590: */
1591: if (tty->term->flags & TERM_256COLOURS) {
1592: if (*type == '3')
1593: tty_putcode1(tty, TTYC_SETAF, colour);
1594: else
1595: tty_putcode1(tty, TTYC_SETAB, colour);
1596: return (0);
1597: }
1598:
1599: /*
1600: * If the user has specified -2 to the client, setaf and setab may not
1601: * work, so send the usual sequence.
1602: */
1603: if (tty->term_flags & TERM_256COLOURS) {
1604: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1605: tty_puts(tty, s);
1606: return (0);
1607: }
1.61 nicm 1608:
1.165 nicm 1609: return (-1);
1.110 nicm 1610: }
1611:
1612: void
1613: tty_bell(struct tty *tty)
1614: {
1615: tty_putcode(tty, TTYC_BEL);
1.1 nicm 1616: }