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