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