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