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