If redrawing line 0 of the screen onto the tty, there can't be a wrap flag on
[tmux-openbsd.git] / tty.c
blob02ceb54edbae7f39e89f3bc6443fb646c6603239
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <termios.h>
27 #include <unistd.h>
29 #include "tmux.h"
31 void tty_read_callback(struct bufferevent *, void *);
32 void tty_error_callback(struct bufferevent *, short, void *);
34 void tty_fill_acs(struct tty *);
36 int tty_try_256(struct tty *, u_char, const char *);
37 int tty_try_88(struct tty *, u_char, const char *);
39 void tty_colours(struct tty *, const struct grid_cell *, u_char *);
40 void tty_colours_fg(struct tty *, const struct grid_cell *, u_char *);
41 void tty_colours_bg(struct tty *, const struct grid_cell *);
43 void tty_redraw_region(struct tty *, const struct tty_ctx *);
44 void tty_emulate_repeat(
45 struct tty *, enum tty_code_code, enum tty_code_code, u_int);
46 void tty_cell(struct tty *,
47 const struct grid_cell *, const struct grid_utf8 *);
49 void
50 tty_init(struct tty *tty, int fd, char *term)
52 char *path;
54 memset(tty, 0, sizeof *tty);
55 tty->log_fd = -1;
57 if (term == NULL || *term == '\0')
58 tty->termname = xstrdup("unknown");
59 else
60 tty->termname = xstrdup(term);
62 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
63 fatal("fcntl failed");
64 tty->fd = fd;
66 if ((path = ttyname(fd)) == NULL)
67 fatalx("ttyname failed");
68 tty->path = xstrdup(path);
70 tty->flags = 0;
71 tty->term_flags = 0;
74 void
75 tty_resize(struct tty *tty)
77 struct winsize ws;
79 if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
80 tty->sx = ws.ws_col;
81 tty->sy = ws.ws_row;
83 if (tty->sx == 0)
84 tty->sx = 80;
85 if (tty->sy == 0)
86 tty->sy = 24;
88 tty->cx = UINT_MAX;
89 tty->cy = UINT_MAX;
91 tty->rupper = UINT_MAX;
92 tty->rlower = UINT_MAX;
95 int
96 tty_open(struct tty *tty, const char *overrides, char **cause)
98 int fd;
100 if (debug_level > 3) {
101 fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
102 if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
103 fatal("fcntl failed");
104 tty->log_fd = fd;
107 tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
108 if (tty->term == NULL) {
109 tty_close(tty);
110 return (-1);
112 tty->flags |= TTY_OPENED;
114 tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_ESCAPE);
116 tty->event = bufferevent_new(
117 tty->fd, tty_read_callback, NULL, tty_error_callback, tty);
119 tty_start_tty(tty);
121 tty_keys_init(tty);
123 tty_fill_acs(tty);
125 return (0);
128 /* ARGSUSED */
129 void
130 tty_read_callback(unused struct bufferevent *bufev, void *data)
132 struct tty *tty = data;
134 while (tty_keys_next(tty))
138 /* ARGSUSED */
139 void
140 tty_error_callback(
141 unused struct bufferevent *bufev, unused short what, unused void *data)
145 void
146 tty_start_tty(struct tty *tty)
148 struct termios tio;
149 int mode;
151 if (tty->fd == -1)
152 return;
154 if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
155 fatal("fcntl failed");
156 if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
157 fatal("fcntl failed");
159 bufferevent_enable(tty->event, EV_READ|EV_WRITE);
161 if (tcgetattr(tty->fd, &tty->tio) != 0)
162 fatal("tcgetattr failed");
163 memcpy(&tio, &tty->tio, sizeof tio);
164 tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
165 tio.c_iflag |= IGNBRK;
166 tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
167 tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
168 ECHOPRT|ECHOKE|ECHOCTL|ISIG);
169 tio.c_cc[VMIN] = 1;
170 tio.c_cc[VTIME] = 0;
171 if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
172 fatal("tcsetattr failed");
173 tcflush(tty->fd, TCIOFLUSH);
175 tty_putcode(tty, TTYC_SMCUP);
177 tty_putcode(tty, TTYC_SGR0);
178 memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
180 tty_putcode(tty, TTYC_RMKX);
181 tty_putcode(tty, TTYC_ENACS);
182 tty_putcode(tty, TTYC_CLEAR);
184 tty_putcode(tty, TTYC_CNORM);
185 if (tty_term_has(tty->term, TTYC_KMOUS))
186 tty_puts(tty, "\033[?1000l");
188 tty->cx = UINT_MAX;
189 tty->cy = UINT_MAX;
191 tty->rlower = UINT_MAX;
192 tty->rupper = UINT_MAX;
194 tty->mode = MODE_CURSOR;
196 tty->flags |= TTY_STARTED;
199 void
200 tty_stop_tty(struct tty *tty)
202 struct winsize ws;
203 int mode;
205 if (!(tty->flags & TTY_STARTED))
206 return;
207 tty->flags &= ~TTY_STARTED;
209 bufferevent_disable(tty->event, EV_READ|EV_WRITE);
212 * Be flexible about error handling and try not kill the server just
213 * because the fd is invalid. Things like ssh -t can easily leave us
214 * with a dead tty.
216 if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
217 return;
218 if (fcntl(tty->fd, F_SETFL, mode & ~O_NONBLOCK) == -1)
219 return;
220 if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
221 return;
222 if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
223 return;
225 tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
226 tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
227 tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
228 tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
229 tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
231 tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
232 if (tty_term_has(tty->term, TTYC_KMOUS))
233 tty_raw(tty, "\033[?1000l");
235 tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
238 void
239 tty_fill_acs(struct tty *tty)
241 const char *ptr;
243 memset(tty->acs, 0, sizeof tty->acs);
244 if (!tty_term_has(tty->term, TTYC_ACSC))
245 return;
247 ptr = tty_term_string(tty->term, TTYC_ACSC);
248 if (strlen(ptr) % 2 != 0)
249 return;
250 for (; *ptr != '\0'; ptr += 2)
251 tty->acs[(u_char) ptr[0]] = ptr[1];
254 u_char
255 tty_get_acs(struct tty *tty, u_char ch)
257 if (tty->acs[ch] != '\0')
258 return (tty->acs[ch]);
259 return (ch);
262 void
263 tty_close(struct tty *tty)
265 if (tty->log_fd != -1) {
266 close(tty->log_fd);
267 tty->log_fd = -1;
270 evtimer_del(&tty->key_timer);
271 tty_stop_tty(tty);
273 if (tty->flags & TTY_OPENED) {
274 bufferevent_free(tty->event);
276 tty_term_free(tty->term);
277 tty_keys_free(tty);
279 tty->flags &= ~TTY_OPENED;
282 if (tty->fd != -1) {
283 close(tty->fd);
284 tty->fd = -1;
288 void
289 tty_free(struct tty *tty)
291 tty_close(tty);
293 if (tty->path != NULL)
294 xfree(tty->path);
295 if (tty->termname != NULL)
296 xfree(tty->termname);
299 void
300 tty_raw(struct tty *tty, const char *s)
302 write(tty->fd, s, strlen(s));
305 void
306 tty_putcode(struct tty *tty, enum tty_code_code code)
308 tty_puts(tty, tty_term_string(tty->term, code));
311 void
312 tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
314 if (a < 0)
315 return;
316 tty_puts(tty, tty_term_string1(tty->term, code, a));
319 void
320 tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
322 if (a < 0 || b < 0)
323 return;
324 tty_puts(tty, tty_term_string2(tty->term, code, a, b));
327 void
328 tty_puts(struct tty *tty, const char *s)
330 if (*s == '\0')
331 return;
332 bufferevent_write(tty->event, s, strlen(s));
334 if (tty->log_fd != -1)
335 write(tty->log_fd, s, strlen(s));
338 void
339 tty_putc(struct tty *tty, u_char ch)
341 u_int sx;
343 if (tty->cell.attr & GRID_ATTR_CHARSET)
344 ch = tty_get_acs(tty, ch);
345 bufferevent_write(tty->event, &ch, 1);
347 if (ch >= 0x20 && ch != 0x7f) {
348 sx = tty->sx;
349 if (tty->term->flags & TERM_EARLYWRAP)
350 sx--;
352 if (tty->cx >= sx) {
353 tty->cx = 1;
354 if (tty->cy != tty->rlower)
355 tty->cy++;
356 } else
357 tty->cx++;
360 if (tty->log_fd != -1)
361 write(tty->log_fd, &ch, 1);
364 void
365 tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
367 size_t size;
369 size = grid_utf8_size(gu);
370 bufferevent_write(tty->event, gu->data, size);
371 if (tty->log_fd != -1)
372 write(tty->log_fd, gu->data, size);
373 tty->cx += gu->width;
376 void
377 tty_set_title(struct tty *tty, const char *title)
379 if (strstr(tty->termname, "xterm") == NULL &&
380 strstr(tty->termname, "rxvt") == NULL &&
381 strcmp(tty->termname, "screen") != 0)
382 return;
384 tty_puts(tty, "\033]0;");
385 tty_puts(tty, title);
386 tty_putc(tty, '\007');
389 void
390 tty_update_mode(struct tty *tty, int mode)
392 int changed;
394 if (tty->flags & TTY_NOCURSOR)
395 mode &= ~MODE_CURSOR;
397 changed = mode ^ tty->mode;
398 if (changed & MODE_CURSOR) {
399 if (mode & MODE_CURSOR)
400 tty_putcode(tty, TTYC_CNORM);
401 else
402 tty_putcode(tty, TTYC_CIVIS);
404 if (changed & MODE_MOUSE) {
405 if (mode & MODE_MOUSE)
406 tty_puts(tty, "\033[?1000h");
407 else
408 tty_puts(tty, "\033[?1000l");
410 if (changed & MODE_KKEYPAD) {
411 if (mode & MODE_KKEYPAD)
412 tty_putcode(tty, TTYC_SMKX);
413 else
414 tty_putcode(tty, TTYC_RMKX);
416 tty->mode = mode;
419 void
420 tty_emulate_repeat(
421 struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
423 if (tty_term_has(tty->term, code))
424 tty_putcode1(tty, code, n);
425 else {
426 while (n-- > 0)
427 tty_putcode(tty, code1);
432 * Redraw scroll region using data from screen (already updated). Used when
433 * CSR not supported, or window is a pane that doesn't take up the full
434 * width of the terminal.
436 void
437 tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
439 struct window_pane *wp = ctx->wp;
440 struct screen *s = wp->screen;
441 u_int i;
444 * If region is >= 50% of the screen, just schedule a window redraw. In
445 * most cases, this is likely to be followed by some more scrolling -
446 * without this, the entire pane ends up being redrawn many times which
447 * can be much more data.
449 if (ctx->orupper - ctx->orlower >= screen_size_y(s) / 2) {
450 wp->flags |= PANE_REDRAW;
451 return;
454 if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
455 for (i = ctx->ocy; i < screen_size_y(s); i++)
456 tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
457 } else {
458 for (i = ctx->orupper; i <= ctx->orlower; i++)
459 tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
463 void
464 tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
466 const struct grid_cell *gc;
467 struct grid_line *gl;
468 struct grid_cell tmpgc;
469 const struct grid_utf8 *gu;
470 u_int i, sx;
472 tty_update_mode(tty, tty->mode & ~MODE_CURSOR);
474 sx = screen_size_x(s);
475 if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
476 sx = s->grid->linedata[s->grid->hsize + py].cellsize;
477 if (sx > tty->sx)
478 sx = tty->sx;
481 * Don't move the cursor to the start permission if it will wrap there
482 * itself.
484 gl = NULL;
485 if (py != 0)
486 gl = &s->grid->linedata[s->grid->hsize + py - 1];
487 if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
488 tty->cx < tty->sx || ox != 0 ||
489 (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
490 tty_cursor(tty, ox, oy + py);
492 for (i = 0; i < sx; i++) {
493 gc = grid_view_peek_cell(s->grid, i, py);
495 gu = NULL;
496 if (gc->flags & GRID_FLAG_UTF8)
497 gu = grid_view_peek_utf8(s->grid, i, py);
499 if (screen_check_selection(s, i, py)) {
500 memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
501 tmpgc.data = gc->data;
502 tmpgc.flags = gc->flags &
503 ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
504 tmpgc.flags |= s->sel.cell.flags &
505 (GRID_FLAG_FG256|GRID_FLAG_BG256);
506 tty_cell(tty, &tmpgc, gu);
507 } else
508 tty_cell(tty, gc, gu);
511 if (sx >= tty->sx) {
512 tty_update_mode(tty, tty->mode);
513 return;
515 tty_reset(tty);
517 tty_cursor(tty, ox + sx, oy + py);
518 if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
519 tty_putcode(tty, TTYC_EL);
520 else {
521 for (i = sx; i < screen_size_x(s); i++)
522 tty_putc(tty, ' ');
524 tty_update_mode(tty, tty->mode);
527 void
528 tty_write(void (*cmdfn)(
529 struct tty *, const struct tty_ctx *), const struct tty_ctx *ctx)
531 struct window_pane *wp = ctx->wp;
532 struct client *c;
533 u_int i;
535 /* wp can be NULL if updating the screen but not the terminal. */
536 if (wp == NULL)
537 return;
539 if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
540 return;
541 if (wp->window->flags & WINDOW_HIDDEN || !window_pane_visible(wp))
542 return;
544 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
545 c = ARRAY_ITEM(&clients, i);
546 if (c == NULL || c->session == NULL)
547 continue;
548 if (c->flags & CLIENT_SUSPENDED)
549 continue;
551 if (c->session->curw->window == wp->window) {
552 if (c->tty.flags & TTY_FREEZE || c->tty.term == NULL)
553 continue;
554 cmdfn(&c->tty, ctx);
559 void
560 tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
562 struct window_pane *wp = ctx->wp;
563 struct screen *s = wp->screen;
564 u_int i;
566 if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
567 tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
568 return;
571 tty_reset(tty);
573 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
575 if (tty_term_has(tty->term, TTYC_ICH) ||
576 tty_term_has(tty->term, TTYC_ICH1))
577 tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
578 else if (tty_term_has(tty->term, TTYC_SMIR) &&
579 tty_term_has(tty->term, TTYC_RMIR)) {
580 tty_putcode(tty, TTYC_SMIR);
581 for (i = 0; i < ctx->num; i++)
582 tty_putc(tty, ' ');
583 tty_putcode(tty, TTYC_RMIR);
584 } else
585 tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
588 void
589 tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
591 struct window_pane *wp = ctx->wp;
592 struct screen *s = wp->screen;
594 if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
595 (!tty_term_has(tty->term, TTYC_DCH) &&
596 !tty_term_has(tty->term, TTYC_DCH1))) {
597 tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
598 return;
601 tty_reset(tty);
603 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
605 if (tty_term_has(tty->term, TTYC_DCH) ||
606 tty_term_has(tty->term, TTYC_DCH1))
607 tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
610 void
611 tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
613 struct window_pane *wp = ctx->wp;
614 struct screen *s = wp->screen;
616 if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
617 !tty_term_has(tty->term, TTYC_CSR) ||
618 !tty_term_has(tty->term, TTYC_IL1)) {
619 tty_redraw_region(tty, ctx);
620 return;
623 tty_reset(tty);
625 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
626 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
628 tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
631 void
632 tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
634 struct window_pane *wp = ctx->wp;
635 struct screen *s = wp->screen;
637 if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
638 !tty_term_has(tty->term, TTYC_CSR) ||
639 !tty_term_has(tty->term, TTYC_DL1)) {
640 tty_redraw_region(tty, ctx);
641 return;
644 tty_reset(tty);
646 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
647 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
649 tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
652 void
653 tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
655 struct window_pane *wp = ctx->wp;
656 struct screen *s = wp->screen;
657 u_int i;
659 tty_reset(tty);
661 tty_cursor_pane(tty, ctx, 0, ctx->ocy);
663 if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
664 tty_term_has(tty->term, TTYC_EL)) {
665 tty_putcode(tty, TTYC_EL);
666 } else {
667 for (i = 0; i < screen_size_x(s); i++)
668 tty_putc(tty, ' ');
672 void
673 tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
675 struct window_pane *wp = ctx->wp;
676 struct screen *s = wp->screen;
677 u_int i;
679 tty_reset(tty);
681 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
683 if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
684 tty_term_has(tty->term, TTYC_EL))
685 tty_putcode(tty, TTYC_EL);
686 else {
687 for (i = ctx->ocx; i < screen_size_x(s); i++)
688 tty_putc(tty, ' ');
692 void
693 tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
695 struct window_pane *wp = ctx->wp;
696 u_int i;
698 tty_reset(tty);
700 if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
701 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
702 tty_putcode(tty, TTYC_EL1);
703 } else {
704 tty_cursor_pane(tty, ctx, 0, ctx->ocy);
705 for (i = 0; i < ctx->ocx + 1; i++)
706 tty_putc(tty, ' ');
710 void
711 tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
713 struct window_pane *wp = ctx->wp;
714 struct screen *s = wp->screen;
716 if (ctx->ocy != ctx->orupper)
717 return;
719 if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
720 !tty_term_has(tty->term, TTYC_CSR) ||
721 !tty_term_has(tty->term, TTYC_RI)) {
722 tty_redraw_region(tty, ctx);
723 return;
726 tty_reset(tty);
728 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
729 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
731 tty_putcode(tty, TTYC_RI);
734 void
735 tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
737 struct window_pane *wp = ctx->wp;
738 struct screen *s = wp->screen;
740 if (ctx->ocy != ctx->orlower)
741 return;
743 if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
744 !tty_term_has(tty->term, TTYC_CSR)) {
745 tty_redraw_region(tty, ctx);
746 return;
750 * If this line wrapped naturally (ctx->num is nonzero), don't do
751 * anything - the cursor can just be moved to the last cell and wrap
752 * naturally.
754 if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
755 return;
757 tty_reset(tty);
759 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
760 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
762 tty_putc(tty, '\n');
765 void
766 tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
768 struct window_pane *wp = ctx->wp;
769 struct screen *s = wp->screen;
770 u_int i, j;
772 tty_reset(tty);
774 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
775 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
777 if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
778 tty_term_has(tty->term, TTYC_EL)) {
779 tty_putcode(tty, TTYC_EL);
780 if (ctx->ocy != screen_size_y(s) - 1) {
781 tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
782 for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
783 tty_putcode(tty, TTYC_EL);
784 if (i == screen_size_y(s) - 1)
785 continue;
786 tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
787 tty->cy++;
790 } else {
791 for (i = ctx->ocx; i < screen_size_x(s); i++)
792 tty_putc(tty, ' ');
793 for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
794 tty_cursor_pane(tty, ctx, 0, j);
795 for (i = 0; i < screen_size_x(s); i++)
796 tty_putc(tty, ' ');
801 void
802 tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
804 struct window_pane *wp = ctx->wp;
805 struct screen *s = wp->screen;
806 u_int i, j;
808 tty_reset(tty);
810 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
811 tty_cursor_pane(tty, ctx, 0, 0);
813 if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
814 tty_term_has(tty->term, TTYC_EL)) {
815 for (i = 0; i < ctx->ocy; i++) {
816 tty_putcode(tty, TTYC_EL);
817 tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
818 tty->cy++;
820 } else {
821 for (j = 0; j < ctx->ocy; j++) {
822 tty_cursor_pane(tty, ctx, 0, j);
823 for (i = 0; i < screen_size_x(s); i++)
824 tty_putc(tty, ' ');
827 for (i = 0; i <= ctx->ocx; i++)
828 tty_putc(tty, ' ');
831 void
832 tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
834 struct window_pane *wp = ctx->wp;
835 struct screen *s = wp->screen;
836 u_int i, j;
838 tty_reset(tty);
840 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
841 tty_cursor_pane(tty, ctx, 0, 0);
843 if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
844 tty_term_has(tty->term, TTYC_EL)) {
845 for (i = 0; i < screen_size_y(s); i++) {
846 tty_putcode(tty, TTYC_EL);
847 if (i != screen_size_y(s) - 1) {
848 tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
849 tty->cy++;
852 } else {
853 for (j = 0; j < screen_size_y(s); j++) {
854 tty_cursor_pane(tty, ctx, 0, j);
855 for (i = 0; i < screen_size_x(s); i++)
856 tty_putc(tty, ' ');
861 void
862 tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
864 struct window_pane *wp = ctx->wp;
865 struct screen *s = wp->screen;
866 u_int i, j;
868 tty_reset(tty);
870 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
872 for (j = 0; j < screen_size_y(s); j++) {
873 tty_cursor_pane(tty, ctx, 0, j);
874 for (i = 0; i < screen_size_x(s); i++)
875 tty_putc(tty, 'E');
879 void
880 tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
882 struct window_pane *wp = ctx->wp;
883 struct screen *s = wp->screen;
884 u_int cx;
886 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
888 /* Is the cursor in the very last position? */
889 if (ctx->ocx > wp->sx - ctx->last_width) {
890 if (wp->xoff != 0 || wp->sx != tty->sx) {
892 * The pane doesn't fill the entire line, the linefeed
893 * will already have happened, so just move the cursor.
895 tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
896 } else if (tty->cx < tty->sx) {
898 * The cursor isn't in the last position already, so
899 * move as far left as possinble and redraw the last
900 * cell to move into the last position.
902 cx = screen_size_x(s) - ctx->last_width;
903 tty_cursor_pane(tty, ctx, cx, ctx->ocy);
904 tty_cell(tty, &ctx->last_cell, &ctx->last_utf8);
906 } else
907 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
909 tty_cell(tty, ctx->cell, ctx->utf8);
912 void
913 tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
915 struct window_pane *wp = ctx->wp;
918 * Cannot rely on not being a partial character, so just redraw the
919 * whole line.
921 tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
924 void
925 tty_cell(
926 struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
928 u_int i;
930 /* Skip last character if terminal is stupid. */
931 if (tty->term->flags & TERM_EARLYWRAP &&
932 tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
933 return;
935 /* If this is a padding character, do nothing. */
936 if (gc->flags & GRID_FLAG_PADDING)
937 return;
939 /* Set the attributes. */
940 tty_attributes(tty, gc);
942 /* If not UTF-8, write directly. */
943 if (!(gc->flags & GRID_FLAG_UTF8)) {
944 if (gc->data < 0x20 || gc->data == 0x7f)
945 return;
946 tty_putc(tty, gc->data);
947 return;
950 /* If the terminal doesn't support UTF-8, write underscores. */
951 if (!(tty->flags & TTY_UTF8)) {
952 for (i = 0; i < gu->width; i++)
953 tty_putc(tty, '_');
954 return;
957 /* Otherwise, write UTF-8. */
958 tty_pututf8(tty, gu);
961 void
962 tty_reset(struct tty *tty)
964 struct grid_cell *gc = &tty->cell;
966 if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
967 return;
969 if (tty_term_has(tty->term, TTYC_RMACS) && gc->attr & GRID_ATTR_CHARSET)
970 tty_putcode(tty, TTYC_RMACS);
971 tty_putcode(tty, TTYC_SGR0);
972 memcpy(gc, &grid_default_cell, sizeof *gc);
975 /* Set region inside pane. */
976 void
977 tty_region_pane(
978 struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
980 struct window_pane *wp = ctx->wp;
982 tty_region(tty, wp->yoff + rupper, wp->yoff + rlower);
985 /* Set region at absolute position. */
986 void
987 tty_region(struct tty *tty, u_int rupper, u_int rlower)
989 if (tty->rlower == rlower && tty->rupper == rupper)
990 return;
991 if (!tty_term_has(tty->term, TTYC_CSR))
992 return;
994 tty->rupper = rupper;
995 tty->rlower = rlower;
998 * Some terminals (such as PuTTY) do not correctly reset the cursor to
999 * 0,0 if it is beyond the last column (they do not reset their wrap
1000 * flag so further output causes a line feed). As a workaround, do an
1001 * explicit move to 0 first.
1003 if (tty->cx >= tty->sx)
1004 tty_cursor(tty, 0, tty->cy);
1006 tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1007 tty_cursor(tty, 0, 0);
1010 /* Move cursor inside pane. */
1011 void
1012 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1014 struct window_pane *wp = ctx->wp;
1016 tty_cursor(tty, wp->xoff + cx, wp->yoff + cy);
1019 /* Move cursor to absolute position. */
1020 void
1021 tty_cursor(struct tty *tty, u_int cx, u_int cy)
1023 struct tty_term *term = tty->term;
1024 u_int thisx, thisy;
1025 int change;
1027 if (cx > tty->sx - 1)
1028 cx = tty->sx - 1;
1030 thisx = tty->cx;
1031 thisy = tty->cy;
1033 /* No change. */
1034 if (cx == thisx && cy == thisy)
1035 return;
1037 /* Very end of the line, just use absolute movement. */
1038 if (thisx > tty->sx - 1)
1039 goto absolute;
1041 /* Move to home position (0, 0). */
1042 if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1043 tty_putcode(tty, TTYC_HOME);
1044 goto out;
1047 /* Zero on the next line. */
1048 if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1049 tty_putc(tty, '\r');
1050 tty_putc(tty, '\n');
1051 goto out;
1054 /* Moving column or row. */
1055 if (cy == thisy) {
1057 * Moving column only, row staying the same.
1060 /* To left edge. */
1061 if (cx == 0) {
1062 tty_putc(tty, '\r');
1063 goto out;
1066 /* One to the left. */
1067 if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1068 tty_putcode(tty, TTYC_CUB1);
1069 goto out;
1072 /* One to the right. */
1073 if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1074 tty_putcode(tty, TTYC_CUF1);
1075 goto out;
1078 /* Calculate difference. */
1079 change = thisx - cx; /* +ve left, -ve right */
1082 * Use HPA if change is larger than absolute, otherwise move
1083 * the cursor with CUB/CUF.
1085 if (abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1086 tty_putcode1(tty, TTYC_HPA, cx);
1087 goto out;
1088 } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1089 tty_putcode1(tty, TTYC_CUB, change);
1090 goto out;
1091 } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1092 tty_putcode1(tty, TTYC_CUF, -change);
1093 goto out;
1095 } else if (cx == thisx) {
1097 * Moving row only, column staying the same.
1100 /* One above. */
1101 if (thisy != tty->rupper &&
1102 cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1103 tty_putcode(tty, TTYC_CUU1);
1104 goto out;
1107 /* One below. */
1108 if (thisy != tty->rlower &&
1109 cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1110 tty_putcode(tty, TTYC_CUD1);
1111 goto out;
1114 /* Calculate difference. */
1115 change = thisy - cy; /* +ve up, -ve down */
1118 * Try to use VPA if change is larger than absolute or if this
1119 * change would cross the scroll region, otherwise use CUU/CUD.
1121 if (abs(change) > cy ||
1122 (change < 0 && cy - change > tty->rlower) ||
1123 (change > 0 && cy - change < tty->rupper)) {
1124 if (tty_term_has(term, TTYC_VPA)) {
1125 tty_putcode1(tty, TTYC_VPA, cy);
1126 goto out;
1128 } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1129 tty_putcode1(tty, TTYC_CUU, change);
1130 goto out;
1131 } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1132 tty_putcode1(tty, TTYC_CUD, -change);
1133 goto out;
1137 absolute:
1138 /* Absolute movement. */
1139 tty_putcode2(tty, TTYC_CUP, cy, cx);
1141 out:
1142 tty->cx = cx;
1143 tty->cy = cy;
1146 void
1147 tty_attributes(struct tty *tty, const struct grid_cell *gc)
1149 struct grid_cell *tc = &tty->cell, gc2;
1150 u_char changed, new_attr;
1152 /* If the character is space, don't care about foreground. */
1153 if (gc->data == ' ' && !(gc->flags & GRID_FLAG_UTF8)) {
1154 memcpy(&gc2, gc, sizeof gc2);
1155 if (gc->attr & GRID_ATTR_REVERSE)
1156 gc2.bg = tc->bg;
1157 else
1158 gc2.fg = tc->fg;
1159 gc = &gc2;
1163 * If no setab, try to use the reverse attribute as a best-effort for a
1164 * non-default background. This is a bit of a hack but it doesn't do
1165 * any serious harm and makes a couple of applications happier.
1167 if (!tty_term_has(tty->term, TTYC_SETAB)) {
1168 if (gc != &gc2) {
1169 memcpy(&gc2, gc, sizeof gc2);
1170 gc = &gc2;
1173 if (gc->attr & GRID_ATTR_REVERSE) {
1174 if (gc->fg != 7 && gc->fg != 8)
1175 gc2.attr &= ~GRID_ATTR_REVERSE;
1176 } else {
1177 if (gc->bg != 0 && gc->bg != 8)
1178 gc2.attr |= GRID_ATTR_REVERSE;
1182 /* If any bits are being cleared, reset everything. */
1183 if (tc->attr & ~gc->attr)
1184 tty_reset(tty);
1187 * Set the colours. This may call tty_reset() (so it comes next) and
1188 * may add to (NOT remove) the desired attributes by changing new_attr.
1190 new_attr = gc->attr;
1191 tty_colours(tty, gc, &new_attr);
1193 /* Filter out attribute bits already set. */
1194 changed = new_attr & ~tc->attr;
1195 tc->attr = new_attr;
1197 /* Set the attributes. */
1198 if (changed & GRID_ATTR_BRIGHT)
1199 tty_putcode(tty, TTYC_BOLD);
1200 if (changed & GRID_ATTR_DIM)
1201 tty_putcode(tty, TTYC_DIM);
1202 if (changed & GRID_ATTR_ITALICS)
1203 tty_putcode(tty, TTYC_SMSO);
1204 if (changed & GRID_ATTR_UNDERSCORE)
1205 tty_putcode(tty, TTYC_SMUL);
1206 if (changed & GRID_ATTR_BLINK)
1207 tty_putcode(tty, TTYC_BLINK);
1208 if (changed & GRID_ATTR_REVERSE) {
1209 if (tty_term_has(tty->term, TTYC_REV))
1210 tty_putcode(tty, TTYC_REV);
1211 else if (tty_term_has(tty->term, TTYC_SMSO))
1212 tty_putcode(tty, TTYC_SMSO);
1214 if (changed & GRID_ATTR_HIDDEN)
1215 tty_putcode(tty, TTYC_INVIS);
1216 if (changed & GRID_ATTR_CHARSET)
1217 tty_putcode(tty, TTYC_SMACS);
1220 void
1221 tty_colours(struct tty *tty, const struct grid_cell *gc, u_char *attr)
1223 struct grid_cell *tc = &tty->cell;
1224 u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
1225 int have_ax, fg_default, bg_default;
1227 /* No changes? Nothing is necessary. */
1228 if (fg == tc->fg && bg == tc->bg &&
1229 ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1230 return;
1233 * Is either the default colour? This is handled specially because the
1234 * best solution might be to reset both colours to default, in which
1235 * case if only one is default need to fall onward to set the other
1236 * colour.
1238 fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
1239 bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1240 if (fg_default || bg_default) {
1242 * If don't have AX but do have op, send sgr0 (op can't
1243 * actually be used because it is sometimes the same as sgr0
1244 * and sometimes isn't). This resets both colours to default.
1246 * Otherwise, try to set the default colour only as needed.
1248 have_ax = tty_term_has(tty->term, TTYC_AX);
1249 if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1250 tty_reset(tty);
1251 else {
1252 if (fg_default &&
1253 (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1254 if (have_ax)
1255 tty_puts(tty, "\033[39m");
1256 else if (tc->fg != 7 ||
1257 tc->flags & GRID_FLAG_FG256)
1258 tty_putcode1(tty, TTYC_SETAF, 7);
1259 tc->fg = 8;
1260 tc->flags &= ~GRID_FLAG_FG256;
1262 if (bg_default &&
1263 (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1264 if (have_ax)
1265 tty_puts(tty, "\033[49m");
1266 else if (tc->bg != 0 ||
1267 tc->flags & GRID_FLAG_BG256)
1268 tty_putcode1(tty, TTYC_SETAB, 0);
1269 tc->bg = 8;
1270 tc->flags &= ~GRID_FLAG_BG256;
1275 /* Set the foreground colour. */
1276 if (!fg_default && (fg != tc->fg ||
1277 ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1278 tty_colours_fg(tty, gc, attr);
1281 * Set the background colour. This must come after the foreground as
1282 * tty_colour_fg() can call tty_reset().
1284 if (!bg_default && (bg != tc->bg ||
1285 ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1286 tty_colours_bg(tty, gc);
1289 void
1290 tty_colours_fg(struct tty *tty, const struct grid_cell *gc, u_char *attr)
1292 struct grid_cell *tc = &tty->cell;
1293 u_char fg = gc->fg;
1294 char s[32];
1296 /* Is this a 256-colour colour? */
1297 if (gc->flags & GRID_FLAG_FG256) {
1298 /* Try as 256 colours or translating to 88. */
1299 if (tty_try_256(tty, fg, "38") == 0)
1300 goto save_fg;
1301 if (tty_try_88(tty, fg, "38") == 0)
1302 goto save_fg;
1304 /* Translate to 16-colour palette, updating bold if needed. */
1305 fg = colour_256to16(fg);
1306 if (fg & 8) {
1307 fg &= 7;
1308 (*attr) |= GRID_ATTR_BRIGHT;
1309 } else
1310 tty_reset(tty); /* turn off bold */
1313 /* Is this an aixterm bright colour? */
1314 if (fg >= 90 && fg <= 97) {
1315 /* 16 colour terminals or above only. */
1316 if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
1317 xsnprintf(s, sizeof s, "\033[%dm", fg);
1318 tty_puts(tty, s);
1319 goto save_fg;
1321 fg -= 90;
1322 (*attr) |= GRID_ATTR_BRIGHT;
1325 /* Otherwise set the foreground colour. */
1326 tty_putcode1(tty, TTYC_SETAF, fg);
1328 save_fg:
1329 /* Save the new values in the terminal current cell. */
1330 tc->fg = fg;
1331 tc->flags &= ~GRID_FLAG_FG256;
1332 tc->flags |= gc->flags & GRID_FLAG_FG256;
1335 void
1336 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1338 struct grid_cell *tc = &tty->cell;
1339 u_char bg = gc->bg;
1340 char s[32];
1342 /* Is this a 256-colour colour? */
1343 if (gc->flags & GRID_FLAG_BG256) {
1344 /* Try as 256 colours or translating to 88. */
1345 if (tty_try_256(tty, bg, "48") == 0)
1346 goto save_bg;
1347 if (tty_try_88(tty, bg, "48") == 0)
1348 goto save_bg;
1351 * Translate to 16-colour palette. Bold background doesn't
1352 * exist portably, so just discard the bold bit if set.
1354 bg = colour_256to16(bg);
1355 if (bg & 8)
1356 bg &= 7;
1359 /* Is this an aixterm bright colour? */
1360 if (bg >= 100 && bg <= 107) {
1361 /* 16 colour terminals or above only. */
1362 if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
1363 xsnprintf(s, sizeof s, "\033[%dm", bg);
1364 tty_puts(tty, s);
1365 goto save_bg;
1367 bg -= 100;
1368 /* no such thing as a bold background */
1371 /* Otherwise set the background colour. */
1372 tty_putcode1(tty, TTYC_SETAB, bg);
1374 save_bg:
1375 /* Save the new values in the terminal current cell. */
1376 tc->bg = bg;
1377 tc->flags &= ~GRID_FLAG_BG256;
1378 tc->flags |= gc->flags & GRID_FLAG_BG256;
1382 tty_try_256(struct tty *tty, u_char colour, const char *type)
1384 char s[32];
1386 if (!(tty->term->flags & TERM_256COLOURS) &&
1387 !(tty->term_flags & TERM_256COLOURS))
1388 return (-1);
1390 xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1391 tty_puts(tty, s);
1392 return (0);
1396 tty_try_88(struct tty *tty, u_char colour, const char *type)
1398 char s[32];
1400 if (!(tty->term->flags & TERM_88COLOURS) &&
1401 !(tty->term_flags & TERM_88COLOURS))
1402 return (-1);
1403 colour = colour_256to88(colour);
1405 xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1406 tty_puts(tty, s);
1407 return (0);