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>
31 void tty_read_callback(struct bufferevent
*, void *);
32 void tty_error_callback(struct bufferevent
*, short, void *);
34 int tty_try_256(struct tty
*, u_char
, const char *);
35 int tty_try_88(struct tty
*, u_char
, const char *);
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
*);
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 #define tty_use_acs(tty) \
50 (tty_term_has(tty->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
53 tty_init(struct tty
*tty
, int fd
, char *term
)
57 memset(tty
, 0, sizeof *tty
);
60 if (term
== NULL
|| *term
== '\0')
61 tty
->termname
= xstrdup("unknown");
63 tty
->termname
= xstrdup(term
);
66 if ((path
= ttyname(fd
)) == NULL
)
67 fatalx("ttyname failed");
68 tty
->path
= xstrdup(path
);
75 tty_resize(struct tty
*tty
)
80 if (ioctl(tty
->fd
, TIOCGWINSZ
, &ws
) != -1) {
91 if (sx
== tty
->sx
&& sy
== tty
->sy
)
99 tty
->rupper
= UINT_MAX
;
100 tty
->rlower
= UINT_MAX
;
103 * If the terminal has been started, reset the actual scroll region and
104 * cursor position, as this may not have happened.
106 if (tty
->flags
& TTY_STARTED
) {
107 tty_cursor(tty
, 0, 0);
108 tty_region(tty
, 0, tty
->sy
- 1);
115 tty_open(struct tty
*tty
, const char *overrides
, char **cause
)
120 if (debug_level
> 3) {
121 xsnprintf(out
, sizeof out
, "tmux-out-%ld.log", (long) getpid());
122 fd
= open(out
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
123 if (fd
!= -1 && fcntl(fd
, F_SETFD
, FD_CLOEXEC
) == -1)
124 fatal("fcntl failed");
128 tty
->term
= tty_term_find(tty
->termname
, tty
->fd
, overrides
, cause
);
129 if (tty
->term
== NULL
) {
133 tty
->flags
|= TTY_OPENED
;
135 tty
->flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
|TTY_ESCAPE
);
137 tty
->event
= bufferevent_new(
138 tty
->fd
, tty_read_callback
, NULL
, tty_error_callback
, tty
);
149 tty_read_callback(unused
struct bufferevent
*bufev
, void *data
)
151 struct tty
*tty
= data
;
153 while (tty_keys_next(tty
))
160 unused
struct bufferevent
*bufev
, unused
short what
, unused
void *data
)
165 tty_start_tty(struct tty
*tty
)
169 if (tty
->fd
== -1 || tcgetattr(tty
->fd
, &tty
->tio
) != 0)
172 setblocking(tty
->fd
, 0);
174 bufferevent_enable(tty
->event
, EV_READ
|EV_WRITE
);
176 memcpy(&tio
, &tty
->tio
, sizeof tio
);
177 tio
.c_iflag
&= ~(IXON
|IXOFF
|ICRNL
|INLCR
|IGNCR
|IMAXBEL
|ISTRIP
);
178 tio
.c_iflag
|= IGNBRK
;
179 tio
.c_oflag
&= ~(OPOST
|ONLCR
|OCRNL
|ONLRET
);
180 tio
.c_lflag
&= ~(IEXTEN
|ICANON
|ECHO
|ECHOE
|ECHONL
|ECHOCTL
|
181 ECHOPRT
|ECHOKE
|ECHOCTL
|ISIG
);
184 if (tcsetattr(tty
->fd
, TCSANOW
, &tio
) == 0)
185 tcflush(tty
->fd
, TCIOFLUSH
);
187 tty_putcode(tty
, TTYC_SMCUP
);
189 tty_putcode(tty
, TTYC_SGR0
);
190 memcpy(&tty
->cell
, &grid_default_cell
, sizeof tty
->cell
);
192 tty_putcode(tty
, TTYC_RMKX
);
193 if (tty_use_acs(tty
))
194 tty_putcode(tty
, TTYC_ENACS
);
195 tty_putcode(tty
, TTYC_CLEAR
);
197 tty_putcode(tty
, TTYC_CNORM
);
198 if (tty_term_has(tty
->term
, TTYC_KMOUS
))
199 tty_puts(tty
, "\033[?1000l");
204 tty
->rlower
= UINT_MAX
;
205 tty
->rupper
= UINT_MAX
;
207 tty
->mode
= MODE_CURSOR
;
209 tty
->flags
|= TTY_STARTED
;
213 tty_stop_tty(struct tty
*tty
)
217 if (!(tty
->flags
& TTY_STARTED
))
219 tty
->flags
&= ~TTY_STARTED
;
221 bufferevent_disable(tty
->event
, EV_READ
|EV_WRITE
);
224 * Be flexible about error handling and try not kill the server just
225 * because the fd is invalid. Things like ssh -t can easily leave us
228 if (ioctl(tty
->fd
, TIOCGWINSZ
, &ws
) == -1)
230 if (tcsetattr(tty
->fd
, TCSANOW
, &tty
->tio
) == -1)
233 tty_raw(tty
, tty_term_string2(tty
->term
, TTYC_CSR
, 0, ws
.ws_row
- 1));
234 if (tty_use_acs(tty
))
235 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMACS
));
236 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_SGR0
));
237 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMKX
));
238 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CLEAR
));
240 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CNORM
));
241 if (tty_term_has(tty
->term
, TTYC_KMOUS
))
242 tty_raw(tty
, "\033[?1000l");
244 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMCUP
));
246 setblocking(tty
->fd
, 1);
250 tty_close(struct tty
*tty
)
252 if (tty
->log_fd
!= -1) {
257 evtimer_del(&tty
->key_timer
);
260 if (tty
->flags
& TTY_OPENED
) {
261 bufferevent_free(tty
->event
);
263 tty_term_free(tty
->term
);
266 tty
->flags
&= ~TTY_OPENED
;
276 tty_free(struct tty
*tty
)
280 if (tty
->path
!= NULL
)
282 if (tty
->termname
!= NULL
)
283 xfree(tty
->termname
);
287 tty_raw(struct tty
*tty
, const char *s
)
289 write(tty
->fd
, s
, strlen(s
));
293 tty_putcode(struct tty
*tty
, enum tty_code_code code
)
295 tty_puts(tty
, tty_term_string(tty
->term
, code
));
299 tty_putcode1(struct tty
*tty
, enum tty_code_code code
, int a
)
303 tty_puts(tty
, tty_term_string1(tty
->term
, code
, a
));
307 tty_putcode2(struct tty
*tty
, enum tty_code_code code
, int a
, int b
)
311 tty_puts(tty
, tty_term_string2(tty
->term
, code
, a
, b
));
315 tty_puts(struct tty
*tty
, const char *s
)
319 bufferevent_write(tty
->event
, s
, strlen(s
));
321 if (tty
->log_fd
!= -1)
322 write(tty
->log_fd
, s
, strlen(s
));
326 tty_putc(struct tty
*tty
, u_char ch
)
331 if (tty
->cell
.attr
& GRID_ATTR_CHARSET
) {
332 acs
= tty_acs_get(tty
, ch
);
334 bufferevent_write(tty
->event
, acs
, strlen(acs
));
336 bufferevent_write(tty
->event
, &ch
, 1);
338 bufferevent_write(tty
->event
, &ch
, 1);
340 if (ch
>= 0x20 && ch
!= 0x7f) {
342 if (tty
->term
->flags
& TERM_EARLYWRAP
)
347 if (tty
->cy
!= tty
->rlower
)
353 if (tty
->log_fd
!= -1)
354 write(tty
->log_fd
, &ch
, 1);
358 tty_pututf8(struct tty
*tty
, const struct grid_utf8
*gu
)
362 size
= grid_utf8_size(gu
);
363 bufferevent_write(tty
->event
, gu
->data
, size
);
364 if (tty
->log_fd
!= -1)
365 write(tty
->log_fd
, gu
->data
, size
);
366 tty
->cx
+= gu
->width
;
370 tty_set_title(struct tty
*tty
, const char *title
)
372 if (strstr(tty
->termname
, "xterm") == NULL
&&
373 strstr(tty
->termname
, "rxvt") == NULL
&&
374 strcmp(tty
->termname
, "screen") != 0)
377 tty_puts(tty
, "\033]0;");
378 tty_puts(tty
, title
);
379 tty_putc(tty
, '\007');
383 tty_update_mode(struct tty
*tty
, int mode
)
387 if (tty
->flags
& TTY_NOCURSOR
)
388 mode
&= ~MODE_CURSOR
;
390 changed
= mode
^ tty
->mode
;
391 if (changed
& MODE_CURSOR
) {
392 if (mode
& MODE_CURSOR
)
393 tty_putcode(tty
, TTYC_CNORM
);
395 tty_putcode(tty
, TTYC_CIVIS
);
397 if (changed
& ALL_MOUSE_MODES
) {
398 if (mode
& ALL_MOUSE_MODES
) {
399 if (mode
& MODE_MOUSE_UTF8
)
400 tty_puts(tty
, "\033[?1005h");
401 if (mode
& MODE_MOUSE_ANY
)
402 tty_puts(tty
, "\033[?1003h");
403 else if (mode
& MODE_MOUSE_BUTTON
)
404 tty_puts(tty
, "\033[?1002h");
405 else if (mode
& MODE_MOUSE_STANDARD
)
406 tty_puts(tty
, "\033[?1000h");
408 if (tty
->mode
& MODE_MOUSE_ANY
)
409 tty_puts(tty
, "\033[?1003l");
410 else if (tty
->mode
& MODE_MOUSE_BUTTON
)
411 tty_puts(tty
, "\033[?1002l");
412 else if (tty
->mode
& MODE_MOUSE_STANDARD
)
413 tty_puts(tty
, "\033[?1000l");
414 if (tty
->mode
& MODE_MOUSE_UTF8
)
415 tty_puts(tty
, "\033[?1005l");
418 if (changed
& MODE_KKEYPAD
) {
419 if (mode
& MODE_KKEYPAD
)
420 tty_putcode(tty
, TTYC_SMKX
);
422 tty_putcode(tty
, TTYC_RMKX
);
429 struct tty
*tty
, enum tty_code_code code
, enum tty_code_code code1
, u_int n
)
431 if (tty_term_has(tty
->term
, code
))
432 tty_putcode1(tty
, code
, n
);
435 tty_putcode(tty
, code1
);
440 * Redraw scroll region using data from screen (already updated). Used when
441 * CSR not supported, or window is a pane that doesn't take up the full
442 * width of the terminal.
445 tty_redraw_region(struct tty
*tty
, const struct tty_ctx
*ctx
)
447 struct window_pane
*wp
= ctx
->wp
;
448 struct screen
*s
= wp
->screen
;
452 * If region is >= 50% of the screen, just schedule a window redraw. In
453 * most cases, this is likely to be followed by some more scrolling -
454 * without this, the entire pane ends up being redrawn many times which
455 * can be much more data.
457 if (ctx
->orupper
- ctx
->orlower
>= screen_size_y(s
) / 2) {
458 wp
->flags
|= PANE_REDRAW
;
462 if (ctx
->ocy
< ctx
->orupper
|| ctx
->ocy
> ctx
->orlower
) {
463 for (i
= ctx
->ocy
; i
< screen_size_y(s
); i
++)
464 tty_draw_line(tty
, s
, i
, wp
->xoff
, wp
->yoff
);
466 for (i
= ctx
->orupper
; i
<= ctx
->orlower
; i
++)
467 tty_draw_line(tty
, s
, i
, wp
->xoff
, wp
->yoff
);
472 tty_draw_line(struct tty
*tty
, struct screen
*s
, u_int py
, u_int ox
, u_int oy
)
474 const struct grid_cell
*gc
;
475 struct grid_line
*gl
;
476 struct grid_cell tmpgc
;
477 const struct grid_utf8
*gu
;
480 tty_update_mode(tty
, tty
->mode
& ~MODE_CURSOR
);
482 sx
= screen_size_x(s
);
483 if (sx
> s
->grid
->linedata
[s
->grid
->hsize
+ py
].cellsize
)
484 sx
= s
->grid
->linedata
[s
->grid
->hsize
+ py
].cellsize
;
489 * Don't move the cursor to the start permission if it will wrap there
494 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ py
- 1];
495 if (oy
+ py
== 0 || gl
== NULL
|| !(gl
->flags
& GRID_LINE_WRAPPED
) ||
496 tty
->cx
< tty
->sx
|| ox
!= 0 ||
497 (oy
+ py
!= tty
->cy
+ 1 && tty
->cy
!= s
->rlower
+ oy
))
498 tty_cursor(tty
, ox
, oy
+ py
);
500 for (i
= 0; i
< sx
; i
++) {
501 gc
= grid_view_peek_cell(s
->grid
, i
, py
);
504 if (gc
->flags
& GRID_FLAG_UTF8
)
505 gu
= grid_view_peek_utf8(s
->grid
, i
, py
);
507 if (screen_check_selection(s
, i
, py
)) {
508 memcpy(&tmpgc
, &s
->sel
.cell
, sizeof tmpgc
);
509 tmpgc
.data
= gc
->data
;
510 tmpgc
.flags
= gc
->flags
&
511 ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
512 tmpgc
.flags
|= s
->sel
.cell
.flags
&
513 (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
514 tty_cell(tty
, &tmpgc
, gu
);
516 tty_cell(tty
, gc
, gu
);
520 tty_update_mode(tty
, tty
->mode
);
525 tty_cursor(tty
, ox
+ sx
, oy
+ py
);
526 if (screen_size_x(s
) >= tty
->sx
&& tty_term_has(tty
->term
, TTYC_EL
))
527 tty_putcode(tty
, TTYC_EL
);
529 for (i
= sx
; i
< screen_size_x(s
); i
++)
532 tty_update_mode(tty
, tty
->mode
);
536 tty_write(void (*cmdfn
)(
537 struct tty
*, const struct tty_ctx
*), const struct tty_ctx
*ctx
)
539 struct window_pane
*wp
= ctx
->wp
;
543 /* wp can be NULL if updating the screen but not the terminal. */
547 if (wp
->window
->flags
& WINDOW_REDRAW
|| wp
->flags
& PANE_REDRAW
)
549 if (!window_pane_visible(wp
))
552 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
553 c
= ARRAY_ITEM(&clients
, i
);
554 if (c
== NULL
|| c
->session
== NULL
)
556 if (c
->flags
& CLIENT_SUSPENDED
)
559 if (c
->session
->curw
->window
== wp
->window
) {
560 if (c
->tty
.term
== NULL
)
562 if (c
->tty
.flags
& (TTY_FREEZE
|TTY_BACKOFF
))
570 tty_cmd_insertcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
572 struct window_pane
*wp
= ctx
->wp
;
573 struct screen
*s
= wp
->screen
;
576 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
) {
577 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, wp
->xoff
, wp
->yoff
);
583 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
585 if (tty_term_has(tty
->term
, TTYC_ICH
) ||
586 tty_term_has(tty
->term
, TTYC_ICH1
))
587 tty_emulate_repeat(tty
, TTYC_ICH
, TTYC_ICH1
, ctx
->num
);
588 else if (tty_term_has(tty
->term
, TTYC_SMIR
) &&
589 tty_term_has(tty
->term
, TTYC_RMIR
)) {
590 tty_putcode(tty
, TTYC_SMIR
);
591 for (i
= 0; i
< ctx
->num
; i
++)
593 tty_putcode(tty
, TTYC_RMIR
);
595 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, wp
->xoff
, wp
->yoff
);
599 tty_cmd_deletecharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
601 struct window_pane
*wp
= ctx
->wp
;
602 struct screen
*s
= wp
->screen
;
604 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
605 (!tty_term_has(tty
->term
, TTYC_DCH
) &&
606 !tty_term_has(tty
->term
, TTYC_DCH1
))) {
607 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, wp
->xoff
, wp
->yoff
);
613 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
615 if (tty_term_has(tty
->term
, TTYC_DCH
) ||
616 tty_term_has(tty
->term
, TTYC_DCH1
))
617 tty_emulate_repeat(tty
, TTYC_DCH
, TTYC_DCH1
, ctx
->num
);
621 tty_cmd_insertline(struct tty
*tty
, const struct tty_ctx
*ctx
)
623 struct window_pane
*wp
= ctx
->wp
;
624 struct screen
*s
= wp
->screen
;
626 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
627 !tty_term_has(tty
->term
, TTYC_CSR
) ||
628 !tty_term_has(tty
->term
, TTYC_IL1
)) {
629 tty_redraw_region(tty
, ctx
);
635 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
636 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
638 tty_emulate_repeat(tty
, TTYC_IL
, TTYC_IL1
, ctx
->num
);
642 tty_cmd_deleteline(struct tty
*tty
, const struct tty_ctx
*ctx
)
644 struct window_pane
*wp
= ctx
->wp
;
645 struct screen
*s
= wp
->screen
;
647 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
648 !tty_term_has(tty
->term
, TTYC_CSR
) ||
649 !tty_term_has(tty
->term
, TTYC_DL1
)) {
650 tty_redraw_region(tty
, ctx
);
656 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
657 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
659 tty_emulate_repeat(tty
, TTYC_DL
, TTYC_DL1
, ctx
->num
);
663 tty_cmd_clearline(struct tty
*tty
, const struct tty_ctx
*ctx
)
665 struct window_pane
*wp
= ctx
->wp
;
666 struct screen
*s
= wp
->screen
;
671 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
);
673 if (wp
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
674 tty_term_has(tty
->term
, TTYC_EL
)) {
675 tty_putcode(tty
, TTYC_EL
);
677 for (i
= 0; i
< screen_size_x(s
); i
++)
683 tty_cmd_clearendofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
685 struct window_pane
*wp
= ctx
->wp
;
686 struct screen
*s
= wp
->screen
;
691 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
693 if (wp
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
694 tty_term_has(tty
->term
, TTYC_EL
))
695 tty_putcode(tty
, TTYC_EL
);
697 for (i
= ctx
->ocx
; i
< screen_size_x(s
); i
++)
703 tty_cmd_clearstartofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
705 struct window_pane
*wp
= ctx
->wp
;
710 if (wp
->xoff
== 0 && tty_term_has(tty
->term
, TTYC_EL1
)) {
711 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
712 tty_putcode(tty
, TTYC_EL1
);
714 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
);
715 for (i
= 0; i
< ctx
->ocx
+ 1; i
++)
721 tty_cmd_reverseindex(struct tty
*tty
, const struct tty_ctx
*ctx
)
723 struct window_pane
*wp
= ctx
->wp
;
724 struct screen
*s
= wp
->screen
;
726 if (ctx
->ocy
!= ctx
->orupper
)
729 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
730 !tty_term_has(tty
->term
, TTYC_CSR
) ||
731 !tty_term_has(tty
->term
, TTYC_RI
)) {
732 tty_redraw_region(tty
, ctx
);
738 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
739 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
741 tty_putcode(tty
, TTYC_RI
);
745 tty_cmd_linefeed(struct tty
*tty
, const struct tty_ctx
*ctx
)
747 struct window_pane
*wp
= ctx
->wp
;
748 struct screen
*s
= wp
->screen
;
750 if (ctx
->ocy
!= ctx
->orlower
)
753 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
754 !tty_term_has(tty
->term
, TTYC_CSR
)) {
755 tty_redraw_region(tty
, ctx
);
760 * If this line wrapped naturally (ctx->num is nonzero), don't do
761 * anything - the cursor can just be moved to the last cell and wrap
764 if (ctx
->num
&& !(tty
->term
->flags
& TERM_EARLYWRAP
))
769 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
770 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
776 tty_cmd_clearendofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
778 struct window_pane
*wp
= ctx
->wp
;
779 struct screen
*s
= wp
->screen
;
784 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
785 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
787 if (wp
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
788 tty_term_has(tty
->term
, TTYC_EL
)) {
789 tty_putcode(tty
, TTYC_EL
);
790 if (ctx
->ocy
!= screen_size_y(s
) - 1) {
791 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
+ 1);
792 for (i
= ctx
->ocy
+ 1; i
< screen_size_y(s
); i
++) {
793 tty_putcode(tty
, TTYC_EL
);
794 if (i
== screen_size_y(s
) - 1)
796 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
801 for (i
= ctx
->ocx
; i
< screen_size_x(s
); i
++)
803 for (j
= ctx
->ocy
+ 1; j
< screen_size_y(s
); j
++) {
804 tty_cursor_pane(tty
, ctx
, 0, j
);
805 for (i
= 0; i
< screen_size_x(s
); i
++)
812 tty_cmd_clearstartofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
814 struct window_pane
*wp
= ctx
->wp
;
815 struct screen
*s
= wp
->screen
;
820 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
821 tty_cursor_pane(tty
, ctx
, 0, 0);
823 if (wp
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
824 tty_term_has(tty
->term
, TTYC_EL
)) {
825 for (i
= 0; i
< ctx
->ocy
; i
++) {
826 tty_putcode(tty
, TTYC_EL
);
827 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
831 for (j
= 0; j
< ctx
->ocy
; j
++) {
832 tty_cursor_pane(tty
, ctx
, 0, j
);
833 for (i
= 0; i
< screen_size_x(s
); i
++)
837 for (i
= 0; i
<= ctx
->ocx
; i
++)
842 tty_cmd_clearscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
844 struct window_pane
*wp
= ctx
->wp
;
845 struct screen
*s
= wp
->screen
;
850 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
851 tty_cursor_pane(tty
, ctx
, 0, 0);
853 if (wp
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
854 tty_term_has(tty
->term
, TTYC_EL
)) {
855 for (i
= 0; i
< screen_size_y(s
); i
++) {
856 tty_putcode(tty
, TTYC_EL
);
857 if (i
!= screen_size_y(s
) - 1) {
858 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
863 for (j
= 0; j
< screen_size_y(s
); j
++) {
864 tty_cursor_pane(tty
, ctx
, 0, j
);
865 for (i
= 0; i
< screen_size_x(s
); i
++)
872 tty_cmd_alignmenttest(struct tty
*tty
, const struct tty_ctx
*ctx
)
874 struct window_pane
*wp
= ctx
->wp
;
875 struct screen
*s
= wp
->screen
;
880 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
882 for (j
= 0; j
< screen_size_y(s
); j
++) {
883 tty_cursor_pane(tty
, ctx
, 0, j
);
884 for (i
= 0; i
< screen_size_x(s
); i
++)
890 tty_cmd_cell(struct tty
*tty
, const struct tty_ctx
*ctx
)
892 struct window_pane
*wp
= ctx
->wp
;
893 struct screen
*s
= wp
->screen
;
896 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
898 /* Is the cursor in the very last position? */
899 if (ctx
->ocx
> wp
->sx
- ctx
->last_width
) {
900 if (wp
->xoff
!= 0 || wp
->sx
!= tty
->sx
) {
902 * The pane doesn't fill the entire line, the linefeed
903 * will already have happened, so just move the cursor.
905 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
+ 1);
906 } else if (tty
->cx
< tty
->sx
) {
908 * The cursor isn't in the last position already, so
909 * move as far left as possinble and redraw the last
910 * cell to move into the last position.
912 cx
= screen_size_x(s
) - ctx
->last_width
;
913 tty_cursor_pane(tty
, ctx
, cx
, ctx
->ocy
);
914 tty_cell(tty
, &ctx
->last_cell
, &ctx
->last_utf8
);
917 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
919 tty_cell(tty
, ctx
->cell
, ctx
->utf8
);
923 tty_cmd_utf8character(struct tty
*tty
, const struct tty_ctx
*ctx
)
925 struct window_pane
*wp
= ctx
->wp
;
928 * Cannot rely on not being a partial character, so just redraw the
931 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, wp
->xoff
, wp
->yoff
);
936 struct tty
*tty
, const struct grid_cell
*gc
, const struct grid_utf8
*gu
)
940 /* Skip last character if terminal is stupid. */
941 if (tty
->term
->flags
& TERM_EARLYWRAP
&&
942 tty
->cy
== tty
->sy
- 1 && tty
->cx
== tty
->sx
- 1)
945 /* If this is a padding character, do nothing. */
946 if (gc
->flags
& GRID_FLAG_PADDING
)
949 /* Set the attributes. */
950 tty_attributes(tty
, gc
);
952 /* If not UTF-8, write directly. */
953 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
954 if (gc
->data
< 0x20 || gc
->data
== 0x7f)
956 tty_putc(tty
, gc
->data
);
960 /* If the terminal doesn't support UTF-8, write underscores. */
961 if (!(tty
->flags
& TTY_UTF8
)) {
962 for (i
= 0; i
< gu
->width
; i
++)
967 /* Otherwise, write UTF-8. */
968 tty_pututf8(tty
, gu
);
972 tty_reset(struct tty
*tty
)
974 struct grid_cell
*gc
= &tty
->cell
;
976 if (memcmp(gc
, &grid_default_cell
, sizeof *gc
) == 0)
979 if ((gc
->attr
& GRID_ATTR_CHARSET
) && tty_use_acs(tty
))
980 tty_putcode(tty
, TTYC_RMACS
);
981 tty_putcode(tty
, TTYC_SGR0
);
982 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
985 /* Set region inside pane. */
988 struct tty
*tty
, const struct tty_ctx
*ctx
, u_int rupper
, u_int rlower
)
990 struct window_pane
*wp
= ctx
->wp
;
992 tty_region(tty
, wp
->yoff
+ rupper
, wp
->yoff
+ rlower
);
995 /* Set region at absolute position. */
997 tty_region(struct tty
*tty
, u_int rupper
, u_int rlower
)
999 if (tty
->rlower
== rlower
&& tty
->rupper
== rupper
)
1001 if (!tty_term_has(tty
->term
, TTYC_CSR
))
1004 tty
->rupper
= rupper
;
1005 tty
->rlower
= rlower
;
1008 * Some terminals (such as PuTTY) do not correctly reset the cursor to
1009 * 0,0 if it is beyond the last column (they do not reset their wrap
1010 * flag so further output causes a line feed). As a workaround, do an
1011 * explicit move to 0 first.
1013 if (tty
->cx
>= tty
->sx
)
1014 tty_cursor(tty
, 0, tty
->cy
);
1016 tty_putcode2(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
1017 tty_cursor(tty
, 0, 0);
1020 /* Move cursor inside pane. */
1022 tty_cursor_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int cx
, u_int cy
)
1024 struct window_pane
*wp
= ctx
->wp
;
1026 tty_cursor(tty
, wp
->xoff
+ cx
, wp
->yoff
+ cy
);
1029 /* Move cursor to absolute position. */
1031 tty_cursor(struct tty
*tty
, u_int cx
, u_int cy
)
1033 struct tty_term
*term
= tty
->term
;
1037 if (cx
> tty
->sx
- 1)
1044 if (cx
== thisx
&& cy
== thisy
)
1047 /* Very end of the line, just use absolute movement. */
1048 if (thisx
> tty
->sx
- 1)
1051 /* Move to home position (0, 0). */
1052 if (cx
== 0 && cy
== 0 && tty_term_has(term
, TTYC_HOME
)) {
1053 tty_putcode(tty
, TTYC_HOME
);
1057 /* Zero on the next line. */
1058 if (cx
== 0 && cy
== thisy
+ 1 && thisy
!= tty
->rlower
) {
1059 tty_putc(tty
, '\r');
1060 tty_putc(tty
, '\n');
1064 /* Moving column or row. */
1067 * Moving column only, row staying the same.
1072 tty_putc(tty
, '\r');
1076 /* One to the left. */
1077 if (cx
== thisx
- 1 && tty_term_has(term
, TTYC_CUB1
)) {
1078 tty_putcode(tty
, TTYC_CUB1
);
1082 /* One to the right. */
1083 if (cx
== thisx
+ 1 && tty_term_has(term
, TTYC_CUF1
)) {
1084 tty_putcode(tty
, TTYC_CUF1
);
1088 /* Calculate difference. */
1089 change
= thisx
- cx
; /* +ve left, -ve right */
1092 * Use HPA if change is larger than absolute, otherwise move
1093 * the cursor with CUB/CUF.
1095 if ((u_int
) abs(change
) > cx
&& tty_term_has(term
, TTYC_HPA
)) {
1096 tty_putcode1(tty
, TTYC_HPA
, cx
);
1098 } else if (change
> 0 && tty_term_has(term
, TTYC_CUB
)) {
1099 tty_putcode1(tty
, TTYC_CUB
, change
);
1101 } else if (change
< 0 && tty_term_has(term
, TTYC_CUF
)) {
1102 tty_putcode1(tty
, TTYC_CUF
, -change
);
1105 } else if (cx
== thisx
) {
1107 * Moving row only, column staying the same.
1111 if (thisy
!= tty
->rupper
&&
1112 cy
== thisy
- 1 && tty_term_has(term
, TTYC_CUU1
)) {
1113 tty_putcode(tty
, TTYC_CUU1
);
1118 if (thisy
!= tty
->rlower
&&
1119 cy
== thisy
+ 1 && tty_term_has(term
, TTYC_CUD1
)) {
1120 tty_putcode(tty
, TTYC_CUD1
);
1124 /* Calculate difference. */
1125 change
= thisy
- cy
; /* +ve up, -ve down */
1128 * Try to use VPA if change is larger than absolute or if this
1129 * change would cross the scroll region, otherwise use CUU/CUD.
1131 if ((u_int
) abs(change
) > cy
||
1132 (change
< 0 && cy
- change
> tty
->rlower
) ||
1133 (change
> 0 && cy
- change
< tty
->rupper
)) {
1134 if (tty_term_has(term
, TTYC_VPA
)) {
1135 tty_putcode1(tty
, TTYC_VPA
, cy
);
1138 } else if (change
> 0 && tty_term_has(term
, TTYC_CUU
)) {
1139 tty_putcode1(tty
, TTYC_CUU
, change
);
1141 } else if (change
< 0 && tty_term_has(term
, TTYC_CUD
)) {
1142 tty_putcode1(tty
, TTYC_CUD
, -change
);
1148 /* Absolute movement. */
1149 tty_putcode2(tty
, TTYC_CUP
, cy
, cx
);
1157 tty_attributes(struct tty
*tty
, const struct grid_cell
*gc
)
1159 struct grid_cell
*tc
= &tty
->cell
, gc2
;
1162 memcpy(&gc2
, gc
, sizeof gc2
);
1165 * If no setab, try to use the reverse attribute as a best-effort for a
1166 * non-default background. This is a bit of a hack but it doesn't do
1167 * any serious harm and makes a couple of applications happier.
1169 if (!tty_term_has(tty
->term
, TTYC_SETAB
)) {
1170 if (gc2
.attr
& GRID_ATTR_REVERSE
) {
1171 if (gc2
.fg
!= 7 && gc2
.fg
!= 8)
1172 gc2
.attr
&= ~GRID_ATTR_REVERSE
;
1174 if (gc2
.bg
!= 0 && gc2
.bg
!= 8)
1175 gc2
.attr
|= GRID_ATTR_REVERSE
;
1179 /* Fix up the colours if necessary. */
1180 tty_check_fg(tty
, &gc2
);
1181 tty_check_bg(tty
, &gc2
);
1183 /* If any bits are being cleared, reset everything. */
1184 if (tc
->attr
& ~gc2
.attr
)
1188 * Set the colours. This may call tty_reset() (so it comes next) and
1189 * may add to (NOT remove) the desired attributes by changing new_attr.
1191 tty_colours(tty
, &gc2
);
1193 /* Filter out attribute bits already set. */
1194 changed
= gc2
.attr
& ~tc
->attr
;
1195 tc
->attr
= gc2
.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
) && tty_use_acs(tty
))
1217 tty_putcode(tty
, TTYC_SMACS
);
1221 tty_colours(struct tty
*tty
, const struct grid_cell
*gc
)
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)
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
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
))
1253 (tc
->fg
!= 8 || tc
->flags
& GRID_FLAG_FG256
)) {
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);
1260 tc
->flags
&= ~GRID_FLAG_FG256
;
1263 (tc
->bg
!= 8 || tc
->flags
& GRID_FLAG_BG256
)) {
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);
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
);
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
);
1290 tty_check_fg(struct tty
*tty
, struct grid_cell
*gc
)
1294 /* Is this a 256-colour colour? */
1295 if (gc
->flags
& GRID_FLAG_FG256
) {
1296 /* And not a 256 colour mode? */
1297 if (!(tty
->term
->flags
& TERM_88COLOURS
) &&
1298 !(tty
->term_flags
& TERM_88COLOURS
) &&
1299 !(tty
->term
->flags
& TERM_256COLOURS
) &&
1300 !(tty
->term_flags
& TERM_256COLOURS
)) {
1301 gc
->fg
= colour_256to16(gc
->fg
);
1304 gc
->attr
|= GRID_ATTR_BRIGHT
;
1306 gc
->attr
&= ~GRID_ATTR_BRIGHT
;
1307 gc
->flags
&= ~GRID_FLAG_FG256
;
1312 /* Is this an aixterm colour? */
1313 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
1314 if (gc
->fg
>= 90 && gc
->fg
<= 97 && colours
< 16) {
1316 gc
->attr
|= GRID_ATTR_BRIGHT
;
1321 tty_check_bg(struct tty
*tty
, struct grid_cell
*gc
)
1325 /* Is this a 256-colour colour? */
1326 if (gc
->flags
& GRID_FLAG_BG256
) {
1328 * And not a 256 colour mode? Translate to 16-colour
1329 * palette. Bold background doesn't exist portably, so just
1330 * discard the bold bit if set.
1332 if (!(tty
->term
->flags
& TERM_88COLOURS
) &&
1333 !(tty
->term_flags
& TERM_88COLOURS
) &&
1334 !(tty
->term
->flags
& TERM_256COLOURS
) &&
1335 !(tty
->term_flags
& TERM_256COLOURS
)) {
1336 gc
->bg
= colour_256to16(gc
->bg
);
1339 gc
->attr
&= ~GRID_ATTR_BRIGHT
;
1340 gc
->flags
&= ~GRID_FLAG_BG256
;
1345 /* Is this an aixterm colour? */
1346 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
1347 if (gc
->bg
>= 100 && gc
->bg
<= 107 && colours
< 16) {
1349 gc
->attr
|= GRID_ATTR_BRIGHT
;
1354 tty_colours_fg(struct tty
*tty
, const struct grid_cell
*gc
)
1356 struct grid_cell
*tc
= &tty
->cell
;
1360 /* Is this a 256-colour colour? */
1361 if (gc
->flags
& GRID_FLAG_FG256
) {
1362 /* Try as 256 colours or translating to 88. */
1363 if (tty_try_256(tty
, fg
, "38") == 0)
1365 if (tty_try_88(tty
, fg
, "38") == 0)
1367 /* Else already handled by tty_check_fg. */
1371 /* Is this an aixterm bright colour? */
1372 if (fg
>= 90 && fg
<= 97) {
1373 xsnprintf(s
, sizeof s
, "\033[%dm", fg
);
1378 /* Otherwise set the foreground colour. */
1379 tty_putcode1(tty
, TTYC_SETAF
, fg
);
1382 /* Save the new values in the terminal current cell. */
1384 tc
->flags
&= ~GRID_FLAG_FG256
;
1385 tc
->flags
|= gc
->flags
& GRID_FLAG_FG256
;
1389 tty_colours_bg(struct tty
*tty
, const struct grid_cell
*gc
)
1391 struct grid_cell
*tc
= &tty
->cell
;
1395 /* Is this a 256-colour colour? */
1396 if (gc
->flags
& GRID_FLAG_BG256
) {
1397 /* Try as 256 colours or translating to 88. */
1398 if (tty_try_256(tty
, bg
, "48") == 0)
1400 if (tty_try_88(tty
, bg
, "48") == 0)
1402 /* Else already handled by tty_check_bg. */
1406 /* Is this an aixterm bright colour? */
1407 if (bg
>= 100 && bg
<= 107) {
1408 /* 16 colour terminals or above only. */
1409 if (tty_term_number(tty
->term
, TTYC_COLORS
) >= 16) {
1410 xsnprintf(s
, sizeof s
, "\033[%dm", bg
);
1415 /* no such thing as a bold background */
1418 /* Otherwise set the background colour. */
1419 tty_putcode1(tty
, TTYC_SETAB
, bg
);
1422 /* Save the new values in the terminal current cell. */
1424 tc
->flags
&= ~GRID_FLAG_BG256
;
1425 tc
->flags
|= gc
->flags
& GRID_FLAG_BG256
;
1429 tty_try_256(struct tty
*tty
, u_char colour
, const char *type
)
1433 if (!(tty
->term
->flags
& TERM_256COLOURS
) &&
1434 !(tty
->term_flags
& TERM_256COLOURS
))
1437 xsnprintf(s
, sizeof s
, "\033[%s;5;%hhum", type
, colour
);
1443 tty_try_88(struct tty
*tty
, u_char colour
, const char *type
)
1447 if (!(tty
->term
->flags
& TERM_88COLOURS
) &&
1448 !(tty
->term_flags
& TERM_88COLOURS
))
1450 colour
= colour_256to88(colour
);
1452 xsnprintf(s
, sizeof s
, "\033[%s;5;%hhum", type
, colour
);