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