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