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