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