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