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