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