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