Annotation of src/usr.bin/tmux/tty.c, Revision 1.139
1.139 ! nicm 1: /* $OpenBSD: tty.c,v 1.138 2012/07/10 11:53:01 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.138 nicm 348: free(tty->ccolour);
1.1 nicm 349: if (tty->path != NULL)
1.138 nicm 350: free(tty->path);
1.1 nicm 351: if (tty->termname != NULL)
1.138 nicm 352: free(tty->termname);
1.1 nicm 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);
1.138 nicm 471: free(tty->ccolour);
1.107 nicm 472: tty->ccolour = xstrdup(ccolour);
473: }
474:
475: void
476: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1 nicm 477: {
478: int changed;
479:
1.107 nicm 480: if (strcmp(s->ccolour, tty->ccolour))
481: tty_force_cursor_colour(tty, s->ccolour);
482:
1.1 nicm 483: if (tty->flags & TTY_NOCURSOR)
484: mode &= ~MODE_CURSOR;
485:
486: changed = mode ^ tty->mode;
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;
670: u_int i;
671:
1.75 nicm 672: /* wp can be NULL if updating the screen but not the terminal. */
1.15 nicm 673: if (wp == NULL)
674: return;
675:
676: if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
677: return;
1.129 nicm 678: if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
1.15 nicm 679: return;
680:
681: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
682: c = ARRAY_ITEM(&clients, i);
1.139 ! nicm 683: if (c == NULL || c->session == NULL || c->tty.term == NULL)
1.15 nicm 684: continue;
1.139 ! nicm 685: if (c->flags & (CLIENT_SUSPENDED|TTY_FREEZE))
! 686: continue;
! 687: if (c->session->curw->window != wp->window)
1.15 nicm 688: continue;
689:
1.139 ! nicm 690: ctx->xoff = wp->xoff;
! 691: ctx->yoff = wp->yoff;
! 692: if (status_at_line(c) == 0)
! 693: ctx->yoff++;
1.113 nicm 694:
1.139 ! nicm 695: cmdfn(&c->tty, ctx);
1.1 nicm 696: }
697: }
698:
699: void
1.130 nicm 700: tty_cra_pane(struct tty *tty, const struct tty_ctx *ctx,
701: u_int t, u_int l, u_int b, u_int r, u_int tt, u_int tl)
702: {
703: char tmp[64];
704:
705: snprintf(tmp, sizeof tmp,
706: "\033[%u;%u;%u;%u;1;%u;%u;1$v",
707: ctx->yoff + t + 1,
708: ctx->xoff + l + 1,
709: ctx->yoff + b + 1,
710: ctx->xoff + r + 1,
711: ctx->yoff + tt + 1,
712: ctx->xoff + tl + 1);
713: tty_puts(tty, tmp);
714: }
715:
716: void
717: tty_era_pane(struct tty *tty, const struct tty_ctx *ctx,
718: u_int t, u_int l, u_int b, u_int r)
719: {
720: char tmp[64];
721:
722: snprintf(tmp, sizeof tmp,
723: "\033[%u;%u;%u;%u$z",
724: ctx->yoff + t + 1,
725: ctx->xoff + l + 1,
726: ctx->yoff + b + 1,
727: ctx->xoff + r + 1);
728: tty_puts(tty, tmp);
729: }
730:
731: void
1.24 nicm 732: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 733: {
1.77 nicm 734: struct window_pane *wp = ctx->wp;
1.1 nicm 735:
1.135 nicm 736: if (!tty_pane_full_width(tty, ctx)) {
1.113 nicm 737: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 738: return;
739: }
740:
741: tty_reset(tty);
742:
1.77 nicm 743: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 744:
1.1 nicm 745: if (tty_term_has(tty->term, TTYC_ICH) ||
746: tty_term_has(tty->term, TTYC_ICH1))
1.12 nicm 747: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.134 nicm 748: else
1.113 nicm 749: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 750: }
751:
752: void
1.24 nicm 753: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 754: {
1.77 nicm 755: struct window_pane *wp = ctx->wp;
1.1 nicm 756:
1.135 nicm 757: if (!tty_pane_full_width(tty, ctx) ||
1.26 nicm 758: (!tty_term_has(tty->term, TTYC_DCH) &&
759: !tty_term_has(tty->term, TTYC_DCH1))) {
1.113 nicm 760: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 761: return;
762: }
763:
764: tty_reset(tty);
765:
1.77 nicm 766: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 767:
1.26 nicm 768: if (tty_term_has(tty->term, TTYC_DCH) ||
769: tty_term_has(tty->term, TTYC_DCH1))
770: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.1 nicm 771: }
772:
773: void
1.24 nicm 774: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 775: {
1.132 nicm 776: if (!tty_pane_full_width(tty, ctx) ||
1.77 nicm 777: !tty_term_has(tty->term, TTYC_CSR) ||
1.72 nicm 778: !tty_term_has(tty->term, TTYC_IL1)) {
1.14 nicm 779: tty_redraw_region(tty, ctx);
1.1 nicm 780: return;
781: }
782:
783: tty_reset(tty);
784:
1.77 nicm 785: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
786: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 787:
1.12 nicm 788: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1 nicm 789: }
790:
791: void
1.24 nicm 792: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 793: {
1.132 nicm 794: if (!tty_pane_full_width(tty, ctx) ||
1.72 nicm 795: !tty_term_has(tty->term, TTYC_CSR) ||
796: !tty_term_has(tty->term, TTYC_DL1)) {
1.14 nicm 797: tty_redraw_region(tty, ctx);
1.1 nicm 798: return;
799: }
800:
801: tty_reset(tty);
802:
1.77 nicm 803: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
804: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 805:
1.12 nicm 806: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1 nicm 807: }
808:
809: void
1.24 nicm 810: tty_cmd_clearline(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.77 nicm 817: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41 nicm 818:
1.133 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));
1.1 nicm 823: }
824:
825: void
1.24 nicm 826: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 827: {
1.77 nicm 828: struct window_pane *wp = ctx->wp;
1.12 nicm 829: struct screen *s = wp->screen;
1.1 nicm 830:
831: tty_reset(tty);
832:
1.41 nicm 833: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
834:
1.132 nicm 835: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
1.1 nicm 836: tty_putcode(tty, TTYC_EL);
1.133 nicm 837: else
838: tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.1 nicm 839: }
840:
841: void
1.24 nicm 842: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 843: {
844: tty_reset(tty);
845:
1.113 nicm 846: if (ctx->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.41 nicm 847: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 848: tty_putcode(tty, TTYC_EL1);
849: } else {
1.41 nicm 850: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133 nicm 851: tty_repeat_space(tty, ctx->ocx + 1);
1.1 nicm 852: }
853: }
854:
855: void
1.24 nicm 856: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 857: {
1.56 nicm 858: if (ctx->ocy != ctx->orupper)
859: return;
860:
1.132 nicm 861: if (!tty_pane_full_width(tty, ctx) ||
1.70 nicm 862: !tty_term_has(tty->term, TTYC_CSR) ||
863: !tty_term_has(tty->term, TTYC_RI)) {
1.14 nicm 864: tty_redraw_region(tty, ctx);
1.1 nicm 865: return;
866: }
867:
1.56 nicm 868: tty_reset(tty);
1.77 nicm 869:
1.56 nicm 870: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
871: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 872:
1.56 nicm 873: tty_putcode(tty, TTYC_RI);
1.1 nicm 874: }
875:
876: void
1.24 nicm 877: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 878: {
1.77 nicm 879: struct window_pane *wp = ctx->wp;
1.12 nicm 880: struct screen *s = wp->screen;
1.1 nicm 881:
1.56 nicm 882: if (ctx->ocy != ctx->orlower)
883: return;
884:
1.132 nicm 885: if (!tty_pane_full_width(tty, ctx) ||
1.1 nicm 886: !tty_term_has(tty->term, TTYC_CSR)) {
1.128 nicm 887: if (tty_large_region(tty, ctx))
888: wp->flags |= PANE_REDRAW;
1.130 nicm 889: else if (tty_use_rect(tty)) {
890: tty_cra_pane (tty, ctx, ctx->orupper + 1, 0,
891: ctx->orlower, screen_size_x(s) - 1,
892: ctx->orupper, 0);
1.128 nicm 893: tty_cmd_clearline(tty, ctx);
894: } else
895: tty_redraw_region(tty, ctx);
1.1 nicm 896: return;
897: }
1.52 nicm 898:
899: /*
900: * If this line wrapped naturally (ctx->num is nonzero), don't do
901: * anything - the cursor can just be moved to the last cell and wrap
902: * naturally.
903: */
904: if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
905: return;
1.1 nicm 906:
1.56 nicm 907: tty_reset(tty);
1.77 nicm 908:
1.56 nicm 909: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
910: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 911:
1.56 nicm 912: tty_putc(tty, '\n');
1.1 nicm 913: }
914:
915: void
1.24 nicm 916: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 917: {
1.77 nicm 918: struct window_pane *wp = ctx->wp;
1.12 nicm 919: struct screen *s = wp->screen;
920: u_int i, j;
1.1 nicm 921:
922: tty_reset(tty);
923:
1.39 nicm 924: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 925: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39 nicm 926:
1.132 nicm 927: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.1 nicm 928: tty_putcode(tty, TTYC_EL);
1.14 nicm 929: if (ctx->ocy != screen_size_y(s) - 1) {
1.41 nicm 930: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14 nicm 931: for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1 nicm 932: tty_putcode(tty, TTYC_EL);
933: if (i == screen_size_y(s) - 1)
934: continue;
935: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
936: tty->cy++;
937: }
938: }
939: } else {
1.133 nicm 940: tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.68 nicm 941: for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
1.41 nicm 942: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 943: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 944: }
945: }
946: }
947:
948: void
1.24 nicm 949: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 950: {
1.77 nicm 951: struct window_pane *wp = ctx->wp;
1.12 nicm 952: struct screen *s = wp->screen;
953: u_int i, j;
1.1 nicm 954:
955: tty_reset(tty);
956:
1.39 nicm 957: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 958: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 959:
1.132 nicm 960: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.14 nicm 961: for (i = 0; i < ctx->ocy; i++) {
1.1 nicm 962: tty_putcode(tty, TTYC_EL);
963: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
964: tty->cy++;
965: }
966: } else {
1.14 nicm 967: for (j = 0; j < ctx->ocy; j++) {
1.41 nicm 968: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 969: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 970: }
971: }
1.133 nicm 972: tty_repeat_space(tty, ctx->ocx + 1);
1.1 nicm 973: }
974:
975: void
1.24 nicm 976: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 977: {
1.77 nicm 978: struct window_pane *wp = ctx->wp;
1.12 nicm 979: struct screen *s = wp->screen;
980: u_int i, j;
1.1 nicm 981:
982: tty_reset(tty);
983:
1.39 nicm 984: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 985: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 986:
1.132 nicm 987: if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.1 nicm 988: for (i = 0; i < screen_size_y(s); i++) {
989: tty_putcode(tty, TTYC_EL);
990: if (i != screen_size_y(s) - 1) {
991: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
992: tty->cy++;
993: }
994: }
995: } else {
996: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 997: tty_cursor_pane(tty, ctx, 0, j);
1.133 nicm 998: tty_repeat_space(tty, screen_size_x(s));
1.1 nicm 999: }
1.4 nicm 1000: }
1001: }
1002:
1003: void
1.24 nicm 1004: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 1005: {
1.12 nicm 1006: struct window_pane *wp = ctx->wp;
1007: struct screen *s = wp->screen;
1008: u_int i, j;
1.4 nicm 1009:
1010: tty_reset(tty);
1011:
1.39 nicm 1012: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4 nicm 1013:
1014: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 1015: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 1016: for (i = 0; i < screen_size_x(s); i++)
1017: tty_putc(tty, 'E');
1.1 nicm 1018: }
1019: }
1020:
1021: void
1.24 nicm 1022: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1023: {
1.46 nicm 1024: struct window_pane *wp = ctx->wp;
1025: struct screen *s = wp->screen;
1.58 nicm 1026: u_int cx;
1.102 nicm 1027: u_int width;
1028: const struct grid_cell *gc = ctx->cell;
1029: const struct grid_utf8 *gu = ctx->utf8;
1030:
1031: if (gc->flags & GRID_FLAG_UTF8)
1032: width = gu->width;
1033: else
1034: width = 1;
1.46 nicm 1035:
1036: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1037:
1.57 nicm 1038: /* Is the cursor in the very last position? */
1.102 nicm 1039: if (ctx->ocx > wp->sx - width) {
1.113 nicm 1040: if (ctx->xoff != 0 || wp->sx != tty->sx) {
1.57 nicm 1041: /*
1042: * The pane doesn't fill the entire line, the linefeed
1043: * will already have happened, so just move the cursor.
1044: */
1.125 nicm 1045: if (ctx->ocy != wp->yoff + wp->screen->rlower)
1.122 nicm 1046: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1047: else
1048: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.57 nicm 1049: } else if (tty->cx < tty->sx) {
1050: /*
1051: * The cursor isn't in the last position already, so
1.102 nicm 1052: * move as far left as possible and redraw the last
1.57 nicm 1053: * cell to move into the last position.
1054: */
1.111 nicm 1055: if (ctx->last_cell.flags & GRID_FLAG_UTF8)
1056: cx = screen_size_x(s) - ctx->last_utf8.width;
1057: else
1058: cx = screen_size_x(s) - 1;
1.50 nicm 1059: tty_cursor_pane(tty, ctx, cx, ctx->ocy);
1060: tty_cell(tty, &ctx->last_cell, &ctx->last_utf8);
1061: }
1062: } else
1.46 nicm 1063: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1064:
1.12 nicm 1065: tty_cell(tty, ctx->cell, ctx->utf8);
1.1 nicm 1066: }
1067:
1068: void
1.24 nicm 1069: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1070: {
1.53 nicm 1071: struct window_pane *wp = ctx->wp;
1.1 nicm 1072:
1.53 nicm 1073: /*
1074: * Cannot rely on not being a partial character, so just redraw the
1075: * whole line.
1076: */
1.113 nicm 1077: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.106 nicm 1078: }
1079:
1080: void
1081: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1082: {
1083: char *buf;
1084: size_t off;
1085:
1086: if (!tty_term_has(tty->term, TTYC_MS))
1087: return;
1088:
1089: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1090: buf = xmalloc(off);
1091:
1092: b64_ntop(ctx->ptr, ctx->num, buf, off);
1093: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1094:
1.138 nicm 1095: free(buf);
1.100 nicm 1096: }
1097:
1098: void
1099: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1100: {
1101: u_int i;
1102: u_char *str = ctx->ptr;
1103:
1104: for (i = 0; i < ctx->num; i++)
1105: tty_putc(tty, str[i]);
1.1 nicm 1106: }
1107:
1108: void
1109: tty_cell(
1110: struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
1111: {
1112: u_int i;
1113:
1114: /* Skip last character if terminal is stupid. */
1115: if (tty->term->flags & TERM_EARLYWRAP &&
1116: tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
1117: return;
1118:
1119: /* If this is a padding character, do nothing. */
1120: if (gc->flags & GRID_FLAG_PADDING)
1121: return;
1122:
1123: /* Set the attributes. */
1124: tty_attributes(tty, gc);
1125:
1126: /* If not UTF-8, write directly. */
1127: if (!(gc->flags & GRID_FLAG_UTF8)) {
1128: if (gc->data < 0x20 || gc->data == 0x7f)
1129: return;
1130: tty_putc(tty, gc->data);
1131: return;
1132: }
1133:
1134: /* If the terminal doesn't support UTF-8, write underscores. */
1135: if (!(tty->flags & TTY_UTF8)) {
1136: for (i = 0; i < gu->width; i++)
1137: tty_putc(tty, '_');
1138: return;
1139: }
1140:
1141: /* Otherwise, write UTF-8. */
1.5 nicm 1142: tty_pututf8(tty, gu);
1.1 nicm 1143: }
1144:
1145: void
1146: tty_reset(struct tty *tty)
1147: {
1148: struct grid_cell *gc = &tty->cell;
1149:
1150: if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
1151: return;
1152:
1.90 nicm 1153: if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1154: tty_putcode(tty, TTYC_RMACS);
1155: tty_putcode(tty, TTYC_SGR0);
1156: memcpy(gc, &grid_default_cell, sizeof *gc);
1157: }
1158:
1.40 nicm 1159: /* Set region inside pane. */
1.1 nicm 1160: void
1.39 nicm 1161: tty_region_pane(
1162: struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1.1 nicm 1163: {
1.113 nicm 1164: tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39 nicm 1165: }
1166:
1.40 nicm 1167: /* Set region at absolute position. */
1.39 nicm 1168: void
1.40 nicm 1169: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 1170: {
1171: if (tty->rlower == rlower && tty->rupper == rupper)
1172: return;
1.1 nicm 1173: if (!tty_term_has(tty->term, TTYC_CSR))
1174: return;
1.39 nicm 1175:
1176: tty->rupper = rupper;
1177: tty->rlower = rlower;
1.55 nicm 1178:
1179: /*
1180: * Some terminals (such as PuTTY) do not correctly reset the cursor to
1181: * 0,0 if it is beyond the last column (they do not reset their wrap
1182: * flag so further output causes a line feed). As a workaround, do an
1183: * explicit move to 0 first.
1184: */
1185: if (tty->cx >= tty->sx)
1186: tty_cursor(tty, 0, tty->cy);
1.42 nicm 1187:
1.39 nicm 1188: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.78 nicm 1189: tty_cursor(tty, 0, 0);
1.1 nicm 1190: }
1191:
1.42 nicm 1192: /* Move cursor inside pane. */
1.1 nicm 1193: void
1.41 nicm 1194: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1195: {
1.113 nicm 1196: tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41 nicm 1197: }
1198:
1.42 nicm 1199: /* Move cursor to absolute position. */
1.41 nicm 1200: void
1201: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 1202: {
1.42 nicm 1203: struct tty_term *term = tty->term;
1204: u_int thisx, thisy;
1205: int change;
1.77 nicm 1206:
1.42 nicm 1207: if (cx > tty->sx - 1)
1208: cx = tty->sx - 1;
1209:
1210: thisx = tty->cx;
1211: thisy = tty->cy;
1212:
1213: /* No change. */
1214: if (cx == thisx && cy == thisy)
1215: return;
1216:
1.43 nicm 1217: /* Very end of the line, just use absolute movement. */
1218: if (thisx > tty->sx - 1)
1219: goto absolute;
1220:
1.42 nicm 1221: /* Move to home position (0, 0). */
1222: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1223: tty_putcode(tty, TTYC_HOME);
1224: goto out;
1225: }
1226:
1227: /* Zero on the next line. */
1.48 nicm 1228: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1 nicm 1229: tty_putc(tty, '\r');
1.42 nicm 1230: tty_putc(tty, '\n');
1231: goto out;
1232: }
1233:
1.45 nicm 1234: /* Moving column or row. */
1.42 nicm 1235: if (cy == thisy) {
1.45 nicm 1236: /*
1237: * Moving column only, row staying the same.
1238: */
1239:
1.42 nicm 1240: /* To left edge. */
1241: if (cx == 0) {
1242: tty_putc(tty, '\r');
1243: goto out;
1244: }
1245:
1246: /* One to the left. */
1247: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1248: tty_putcode(tty, TTYC_CUB1);
1249: goto out;
1250: }
1251:
1252: /* One to the right. */
1253: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1254: tty_putcode(tty, TTYC_CUF1);
1255: goto out;
1256: }
1257:
1258: /* Calculate difference. */
1259: change = thisx - cx; /* +ve left, -ve right */
1260:
1261: /*
1262: * Use HPA if change is larger than absolute, otherwise move
1263: * the cursor with CUB/CUF.
1264: */
1.87 nicm 1265: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 1266: tty_putcode1(tty, TTYC_HPA, cx);
1267: goto out;
1268: } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1269: tty_putcode1(tty, TTYC_CUB, change);
1270: goto out;
1271: } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1272: tty_putcode1(tty, TTYC_CUF, -change);
1273: goto out;
1274: }
1.45 nicm 1275: } else if (cx == thisx) {
1276: /*
1277: * Moving row only, column staying the same.
1278: */
1.42 nicm 1279:
1280: /* One above. */
1.77 nicm 1281: if (thisy != tty->rupper &&
1.42 nicm 1282: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1283: tty_putcode(tty, TTYC_CUU1);
1284: goto out;
1285: }
1286:
1287: /* One below. */
1.49 nicm 1288: if (thisy != tty->rlower &&
1.42 nicm 1289: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1290: tty_putcode(tty, TTYC_CUD1);
1291: goto out;
1292: }
1293:
1294: /* Calculate difference. */
1295: change = thisy - cy; /* +ve up, -ve down */
1296:
1297: /*
1.77 nicm 1298: * Try to use VPA if change is larger than absolute or if this
1299: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 1300: */
1.87 nicm 1301: if ((u_int) abs(change) > cy ||
1.42 nicm 1302: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 1303: (change > 0 && cy - change < tty->rupper)) {
1304: if (tty_term_has(term, TTYC_VPA)) {
1305: tty_putcode1(tty, TTYC_VPA, cy);
1306: goto out;
1307: }
1.42 nicm 1308: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1309: tty_putcode1(tty, TTYC_CUU, change);
1310: goto out;
1311: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1312: tty_putcode1(tty, TTYC_CUD, -change);
1313: goto out;
1314: }
1315: }
1316:
1.43 nicm 1317: absolute:
1.42 nicm 1318: /* Absolute movement. */
1319: tty_putcode2(tty, TTYC_CUP, cy, cx);
1320:
1321: out:
1322: tty->cx = cx;
1323: tty->cy = cy;
1.1 nicm 1324: }
1325:
1326: void
1327: tty_attributes(struct tty *tty, const struct grid_cell *gc)
1328: {
1.63 nicm 1329: struct grid_cell *tc = &tty->cell, gc2;
1.85 nicm 1330: u_char changed;
1.61 nicm 1331:
1.85 nicm 1332: memcpy(&gc2, gc, sizeof gc2);
1.63 nicm 1333:
1.18 nicm 1334: /*
1335: * If no setab, try to use the reverse attribute as a best-effort for a
1336: * non-default background. This is a bit of a hack but it doesn't do
1337: * any serious harm and makes a couple of applications happier.
1338: */
1339: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 1340: if (gc2.attr & GRID_ATTR_REVERSE) {
1341: if (gc2.fg != 7 && gc2.fg != 8)
1.64 nicm 1342: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 1343: } else {
1.85 nicm 1344: if (gc2.bg != 0 && gc2.bg != 8)
1.64 nicm 1345: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 1346: }
1347: }
1.1 nicm 1348:
1.85 nicm 1349: /* Fix up the colours if necessary. */
1350: tty_check_fg(tty, &gc2);
1351: tty_check_bg(tty, &gc2);
1352:
1.64 nicm 1353: /* If any bits are being cleared, reset everything. */
1.85 nicm 1354: if (tc->attr & ~gc2.attr)
1.64 nicm 1355: tty_reset(tty);
1356:
1357: /*
1358: * Set the colours. This may call tty_reset() (so it comes next) and
1359: * may add to (NOT remove) the desired attributes by changing new_attr.
1360: */
1.85 nicm 1361: tty_colours(tty, &gc2);
1.64 nicm 1362:
1.1 nicm 1363: /* Filter out attribute bits already set. */
1.85 nicm 1364: changed = gc2.attr & ~tc->attr;
1365: tc->attr = gc2.attr;
1.1 nicm 1366:
1367: /* Set the attributes. */
1368: if (changed & GRID_ATTR_BRIGHT)
1369: tty_putcode(tty, TTYC_BOLD);
1370: if (changed & GRID_ATTR_DIM)
1371: tty_putcode(tty, TTYC_DIM);
1372: if (changed & GRID_ATTR_ITALICS)
1.104 nicm 1373: {
1374: if (tty_term_has(tty->term, TTYC_SITM))
1375: tty_putcode(tty, TTYC_SITM);
1376: else
1377: tty_putcode(tty, TTYC_SMSO);
1378: }
1.1 nicm 1379: if (changed & GRID_ATTR_UNDERSCORE)
1380: tty_putcode(tty, TTYC_SMUL);
1381: if (changed & GRID_ATTR_BLINK)
1382: tty_putcode(tty, TTYC_BLINK);
1383: if (changed & GRID_ATTR_REVERSE) {
1384: if (tty_term_has(tty->term, TTYC_REV))
1385: tty_putcode(tty, TTYC_REV);
1386: else if (tty_term_has(tty->term, TTYC_SMSO))
1387: tty_putcode(tty, TTYC_SMSO);
1388: }
1389: if (changed & GRID_ATTR_HIDDEN)
1390: tty_putcode(tty, TTYC_INVIS);
1.90 nicm 1391: if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1392: tty_putcode(tty, TTYC_SMACS);
1393: }
1394:
1.61 nicm 1395: void
1.85 nicm 1396: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1397: {
1.61 nicm 1398: struct grid_cell *tc = &tty->cell;
1.62 nicm 1399: u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
1400: int have_ax, fg_default, bg_default;
1.61 nicm 1401:
1402: /* No changes? Nothing is necessary. */
1.62 nicm 1403: if (fg == tc->fg && bg == tc->bg &&
1404: ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1.63 nicm 1405: return;
1.1 nicm 1406:
1.61 nicm 1407: /*
1408: * Is either the default colour? This is handled specially because the
1409: * best solution might be to reset both colours to default, in which
1410: * case if only one is default need to fall onward to set the other
1411: * colour.
1412: */
1.62 nicm 1413: fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
1414: bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1.61 nicm 1415: if (fg_default || bg_default) {
1416: /*
1417: * If don't have AX but do have op, send sgr0 (op can't
1418: * actually be used because it is sometimes the same as sgr0
1419: * and sometimes isn't). This resets both colours to default.
1420: *
1421: * Otherwise, try to set the default colour only as needed.
1422: */
1423: have_ax = tty_term_has(tty->term, TTYC_AX);
1424: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1425: tty_reset(tty);
1426: else {
1427: if (fg_default &&
1.81 nicm 1428: (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1.61 nicm 1429: if (have_ax)
1430: tty_puts(tty, "\033[39m");
1.81 nicm 1431: else if (tc->fg != 7 ||
1432: tc->flags & GRID_FLAG_FG256)
1.61 nicm 1433: tty_putcode1(tty, TTYC_SETAF, 7);
1434: tc->fg = 8;
1435: tc->flags &= ~GRID_FLAG_FG256;
1436: }
1437: if (bg_default &&
1.81 nicm 1438: (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1.77 nicm 1439: if (have_ax)
1.61 nicm 1440: tty_puts(tty, "\033[49m");
1.81 nicm 1441: else if (tc->bg != 0 ||
1442: tc->flags & GRID_FLAG_BG256)
1.61 nicm 1443: tty_putcode1(tty, TTYC_SETAB, 0);
1444: tc->bg = 8;
1445: tc->flags &= ~GRID_FLAG_BG256;
1446: }
1447: }
1448: }
1.1 nicm 1449:
1.61 nicm 1450: /* Set the foreground colour. */
1451: if (!fg_default && (fg != tc->fg ||
1.62 nicm 1452: ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1.85 nicm 1453: tty_colours_fg(tty, gc);
1.1 nicm 1454:
1.61 nicm 1455: /*
1456: * Set the background colour. This must come after the foreground as
1457: * tty_colour_fg() can call tty_reset().
1458: */
1459: if (!bg_default && (bg != tc->bg ||
1.62 nicm 1460: ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1.73 nicm 1461: tty_colours_bg(tty, gc);
1.1 nicm 1462: }
1463:
1464: void
1.85 nicm 1465: tty_check_fg(struct tty *tty, struct grid_cell *gc)
1466: {
1467: u_int colours;
1468:
1469: /* Is this a 256-colour colour? */
1470: if (gc->flags & GRID_FLAG_FG256) {
1471: /* And not a 256 colour mode? */
1472: if (!(tty->term->flags & TERM_88COLOURS) &&
1473: !(tty->term_flags & TERM_88COLOURS) &&
1474: !(tty->term->flags & TERM_256COLOURS) &&
1475: !(tty->term_flags & TERM_256COLOURS)) {
1476: gc->fg = colour_256to16(gc->fg);
1477: if (gc->fg & 8) {
1478: gc->fg &= 7;
1479: gc->attr |= GRID_ATTR_BRIGHT;
1480: } else
1481: gc->attr &= ~GRID_ATTR_BRIGHT;
1482: gc->flags &= ~GRID_FLAG_FG256;
1483: }
1484: return;
1485: }
1486:
1487: /* Is this an aixterm colour? */
1488: colours = tty_term_number(tty->term, TTYC_COLORS);
1489: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1490: gc->fg -= 90;
1491: gc->attr |= GRID_ATTR_BRIGHT;
1492: }
1493: }
1494:
1495: void
1496: tty_check_bg(struct tty *tty, struct grid_cell *gc)
1497: {
1498: u_int colours;
1499:
1500: /* Is this a 256-colour colour? */
1501: if (gc->flags & GRID_FLAG_BG256) {
1502: /*
1503: * And not a 256 colour mode? Translate to 16-colour
1504: * palette. Bold background doesn't exist portably, so just
1505: * discard the bold bit if set.
1506: */
1507: if (!(tty->term->flags & TERM_88COLOURS) &&
1508: !(tty->term_flags & TERM_88COLOURS) &&
1509: !(tty->term->flags & TERM_256COLOURS) &&
1510: !(tty->term_flags & TERM_256COLOURS)) {
1511: gc->bg = colour_256to16(gc->bg);
1512: if (gc->bg & 8)
1513: gc->bg &= 7;
1514: gc->attr &= ~GRID_ATTR_BRIGHT;
1515: gc->flags &= ~GRID_FLAG_BG256;
1516: }
1517: return;
1518: }
1519:
1520: /* Is this an aixterm colour? */
1521: colours = tty_term_number(tty->term, TTYC_COLORS);
1.112 nicm 1522: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16) {
1.85 nicm 1523: gc->bg -= 90;
1524: gc->attr |= GRID_ATTR_BRIGHT;
1525: }
1526: }
1527:
1528: void
1529: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1530: {
1.61 nicm 1531: struct grid_cell *tc = &tty->cell;
1532: u_char fg = gc->fg;
1.79 nicm 1533: char s[32];
1.1 nicm 1534:
1.61 nicm 1535: /* Is this a 256-colour colour? */
1.1 nicm 1536: if (gc->flags & GRID_FLAG_FG256) {
1.61 nicm 1537: /* Try as 256 colours or translating to 88. */
1.1 nicm 1538: if (tty_try_256(tty, fg, "38") == 0)
1.61 nicm 1539: goto save_fg;
1.1 nicm 1540: if (tty_try_88(tty, fg, "38") == 0)
1.61 nicm 1541: goto save_fg;
1.85 nicm 1542: /* Else already handled by tty_check_fg. */
1543: return;
1.1 nicm 1544: }
1545:
1.79 nicm 1546: /* Is this an aixterm bright colour? */
1547: if (fg >= 90 && fg <= 97) {
1.85 nicm 1548: xsnprintf(s, sizeof s, "\033[%dm", fg);
1549: tty_puts(tty, s);
1550: goto save_fg;
1.79 nicm 1551: }
1552:
1.61 nicm 1553: /* Otherwise set the foreground colour. */
1554: tty_putcode1(tty, TTYC_SETAF, fg);
1555:
1.77 nicm 1556: save_fg:
1.61 nicm 1557: /* Save the new values in the terminal current cell. */
1558: tc->fg = fg;
1559: tc->flags &= ~GRID_FLAG_FG256;
1560: tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1 nicm 1561: }
1562:
1563: void
1.73 nicm 1564: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1565: {
1.61 nicm 1566: struct grid_cell *tc = &tty->cell;
1567: u_char bg = gc->bg;
1.79 nicm 1568: char s[32];
1.1 nicm 1569:
1.61 nicm 1570: /* Is this a 256-colour colour? */
1.1 nicm 1571: if (gc->flags & GRID_FLAG_BG256) {
1.61 nicm 1572: /* Try as 256 colours or translating to 88. */
1.1 nicm 1573: if (tty_try_256(tty, bg, "48") == 0)
1.61 nicm 1574: goto save_bg;
1.1 nicm 1575: if (tty_try_88(tty, bg, "48") == 0)
1.61 nicm 1576: goto save_bg;
1.85 nicm 1577: /* Else already handled by tty_check_bg. */
1578: return;
1.79 nicm 1579: }
1580:
1581: /* Is this an aixterm bright colour? */
1.112 nicm 1582: if (bg >= 90 && bg <= 97) {
1.79 nicm 1583: /* 16 colour terminals or above only. */
1584: if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
1.112 nicm 1585: xsnprintf(s, sizeof s, "\033[%dm", bg + 10);
1.79 nicm 1586: tty_puts(tty, s);
1587: goto save_bg;
1588: }
1.112 nicm 1589: bg -= 90;
1.79 nicm 1590: /* no such thing as a bold background */
1.1 nicm 1591: }
1592:
1.61 nicm 1593: /* Otherwise set the background colour. */
1594: tty_putcode1(tty, TTYC_SETAB, bg);
1595:
1596: save_bg:
1597: /* Save the new values in the terminal current cell. */
1598: tc->bg = bg;
1599: tc->flags &= ~GRID_FLAG_BG256;
1600: tc->flags |= gc->flags & GRID_FLAG_BG256;
1601: }
1602:
1603: int
1604: tty_try_256(struct tty *tty, u_char colour, const char *type)
1605: {
1606: char s[32];
1607:
1608: if (!(tty->term->flags & TERM_256COLOURS) &&
1609: !(tty->term_flags & TERM_256COLOURS))
1610: return (-1);
1611:
1612: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1613: tty_puts(tty, s);
1614: return (0);
1615: }
1616:
1617: int
1618: tty_try_88(struct tty *tty, u_char colour, const char *type)
1619: {
1620: char s[32];
1621:
1622: if (!(tty->term->flags & TERM_88COLOURS) &&
1623: !(tty->term_flags & TERM_88COLOURS))
1624: return (-1);
1625: colour = colour_256to88(colour);
1626:
1627: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1628: tty_puts(tty, s);
1629: return (0);
1.110 nicm 1630: }
1631:
1632: void
1633: tty_bell(struct tty *tty)
1634: {
1635: tty_putcode(tty, TTYC_BEL);
1.1 nicm 1636: }