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