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