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