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 <netinet/in.h>
34 void tty_read_callback(struct bufferevent
*, void *);
35 void tty_error_callback(struct bufferevent
*, short, void *);
37 int tty_try_256(struct tty
*, u_char
, const char *);
38 int tty_try_88(struct tty
*, u_char
, const char *);
40 void tty_colours(struct tty
*, const struct grid_cell
*);
41 void tty_check_fg(struct tty
*, struct grid_cell
*);
42 void tty_check_bg(struct tty
*, struct grid_cell
*);
43 void tty_colours_fg(struct tty
*, const struct grid_cell
*);
44 void tty_colours_bg(struct tty
*, const struct grid_cell
*);
46 void tty_redraw_region(struct tty
*, const struct tty_ctx
*);
47 void tty_emulate_repeat(
48 struct tty
*, enum tty_code_code
, enum tty_code_code
, u_int
);
49 void tty_cell(struct tty
*,
50 const struct grid_cell
*, const struct grid_utf8
*);
52 #define tty_use_acs(tty) \
53 (tty_term_has(tty->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
56 tty_init(struct tty
*tty
, int fd
, char *term
)
60 memset(tty
, 0, sizeof *tty
);
63 if (term
== NULL
|| *term
== '\0')
64 tty
->termname
= xstrdup("unknown");
66 tty
->termname
= xstrdup(term
);
69 if ((path
= ttyname(fd
)) == NULL
)
70 fatalx("ttyname failed");
71 tty
->path
= xstrdup(path
);
78 tty_resize(struct tty
*tty
)
83 if (ioctl(tty
->fd
, TIOCGWINSZ
, &ws
) != -1) {
94 if (sx
== tty
->sx
&& sy
== tty
->sy
)
102 tty
->rupper
= UINT_MAX
;
103 tty
->rlower
= UINT_MAX
;
106 * If the terminal has been started, reset the actual scroll region and
107 * cursor position, as this may not have happened.
109 if (tty
->flags
& TTY_STARTED
) {
110 tty_cursor(tty
, 0, 0);
111 tty_region(tty
, 0, tty
->sy
- 1);
118 tty_open(struct tty
*tty
, const char *overrides
, char **cause
)
123 if (debug_level
> 3) {
124 xsnprintf(out
, sizeof out
, "tmux-out-%ld.log", (long) getpid());
125 fd
= open(out
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
126 if (fd
!= -1 && fcntl(fd
, F_SETFD
, FD_CLOEXEC
) == -1)
127 fatal("fcntl failed");
131 tty
->term
= tty_term_find(tty
->termname
, tty
->fd
, overrides
, cause
);
132 if (tty
->term
== NULL
) {
136 tty
->flags
|= TTY_OPENED
;
138 tty
->flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
|TTY_ESCAPE
);
140 tty
->event
= bufferevent_new(
141 tty
->fd
, tty_read_callback
, NULL
, tty_error_callback
, tty
);
152 tty_read_callback(unused
struct bufferevent
*bufev
, void *data
)
154 struct tty
*tty
= data
;
156 while (tty_keys_next(tty
))
163 unused
struct bufferevent
*bufev
, unused
short what
, unused
void *data
)
168 tty_start_tty(struct tty
*tty
)
172 if (tty
->fd
== -1 || tcgetattr(tty
->fd
, &tty
->tio
) != 0)
175 setblocking(tty
->fd
, 0);
177 bufferevent_enable(tty
->event
, EV_READ
|EV_WRITE
);
179 memcpy(&tio
, &tty
->tio
, sizeof tio
);
180 tio
.c_iflag
&= ~(IXON
|IXOFF
|ICRNL
|INLCR
|IGNCR
|IMAXBEL
|ISTRIP
);
181 tio
.c_iflag
|= IGNBRK
;
182 tio
.c_oflag
&= ~(OPOST
|ONLCR
|OCRNL
|ONLRET
);
183 tio
.c_lflag
&= ~(IEXTEN
|ICANON
|ECHO
|ECHOE
|ECHONL
|ECHOCTL
|
184 ECHOPRT
|ECHOKE
|ECHOCTL
|ISIG
);
187 if (tcsetattr(tty
->fd
, TCSANOW
, &tio
) == 0)
188 tcflush(tty
->fd
, TCIOFLUSH
);
190 tty_putcode(tty
, TTYC_SMCUP
);
192 tty_putcode(tty
, TTYC_SGR0
);
193 memcpy(&tty
->cell
, &grid_default_cell
, sizeof tty
->cell
);
195 tty_putcode(tty
, TTYC_RMKX
);
196 if (tty_use_acs(tty
))
197 tty_putcode(tty
, TTYC_ENACS
);
198 tty_putcode(tty
, TTYC_CLEAR
);
200 tty_putcode(tty
, TTYC_CNORM
);
201 if (tty_term_has(tty
->term
, TTYC_KMOUS
))
202 tty_puts(tty
, "\033[?1000l");
207 tty
->rlower
= UINT_MAX
;
208 tty
->rupper
= UINT_MAX
;
210 tty
->mode
= MODE_CURSOR
;
212 tty
->flags
|= TTY_STARTED
;
216 tty_stop_tty(struct tty
*tty
)
220 if (!(tty
->flags
& TTY_STARTED
))
222 tty
->flags
&= ~TTY_STARTED
;
224 bufferevent_disable(tty
->event
, EV_READ
|EV_WRITE
);
227 * Be flexible about error handling and try not kill the server just
228 * because the fd is invalid. Things like ssh -t can easily leave us
231 if (ioctl(tty
->fd
, TIOCGWINSZ
, &ws
) == -1)
233 if (tcsetattr(tty
->fd
, TCSANOW
, &tty
->tio
) == -1)
236 setblocking(tty
->fd
, 1);
238 tty_raw(tty
, tty_term_string2(tty
->term
, TTYC_CSR
, 0, ws
.ws_row
- 1));
239 if (tty_use_acs(tty
))
240 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMACS
));
241 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_SGR0
));
242 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMKX
));
243 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CLEAR
));
245 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CNORM
));
246 if (tty_term_has(tty
->term
, TTYC_KMOUS
))
247 tty_raw(tty
, "\033[?1000l");
249 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMCUP
));
253 tty_close(struct tty
*tty
)
255 if (tty
->log_fd
!= -1) {
260 evtimer_del(&tty
->key_timer
);
263 if (tty
->flags
& TTY_OPENED
) {
264 bufferevent_free(tty
->event
);
266 tty_term_free(tty
->term
);
269 tty
->flags
&= ~TTY_OPENED
;
279 tty_free(struct tty
*tty
)
283 if (tty
->path
!= NULL
)
285 if (tty
->termname
!= NULL
)
286 xfree(tty
->termname
);
290 tty_raw(struct tty
*tty
, const char *s
)
292 write(tty
->fd
, s
, strlen(s
));
296 tty_putcode(struct tty
*tty
, enum tty_code_code code
)
298 tty_puts(tty
, tty_term_string(tty
->term
, code
));
302 tty_putcode1(struct tty
*tty
, enum tty_code_code code
, int a
)
306 tty_puts(tty
, tty_term_string1(tty
->term
, code
, a
));
310 tty_putcode2(struct tty
*tty
, enum tty_code_code code
, int a
, int b
)
314 tty_puts(tty
, tty_term_string2(tty
->term
, code
, a
, b
));
318 tty_putcode_ptr2(struct tty
*tty
, enum tty_code_code code
, const void *a
, const void *b
)
320 if (a
!= NULL
&& b
!= NULL
)
321 tty_puts(tty
, tty_term_ptr2(tty
->term
, code
, a
, b
));
325 tty_puts(struct tty
*tty
, const char *s
)
329 bufferevent_write(tty
->event
, s
, strlen(s
));
331 if (tty
->log_fd
!= -1)
332 write(tty
->log_fd
, s
, strlen(s
));
336 tty_putc(struct tty
*tty
, u_char ch
)
341 if (tty
->cell
.attr
& GRID_ATTR_CHARSET
) {
342 acs
= tty_acs_get(tty
, ch
);
344 bufferevent_write(tty
->event
, acs
, strlen(acs
));
346 bufferevent_write(tty
->event
, &ch
, 1);
348 bufferevent_write(tty
->event
, &ch
, 1);
350 if (ch
>= 0x20 && ch
!= 0x7f) {
352 if (tty
->term
->flags
& TERM_EARLYWRAP
)
357 if (tty
->cy
!= tty
->rlower
)
363 if (tty
->log_fd
!= -1)
364 write(tty
->log_fd
, &ch
, 1);
368 tty_pututf8(struct tty
*tty
, const struct grid_utf8
*gu
)
372 size
= grid_utf8_size(gu
);
373 bufferevent_write(tty
->event
, gu
->data
, size
);
374 if (tty
->log_fd
!= -1)
375 write(tty
->log_fd
, gu
->data
, size
);
376 tty
->cx
+= gu
->width
;
380 tty_set_title(struct tty
*tty
, const char *title
)
382 if (!tty_term_has(tty
->term
, TTYC_TSL
) ||
383 !tty_term_has(tty
->term
, TTYC_FSL
))
386 tty_putcode(tty
, TTYC_TSL
);
387 tty_puts(tty
, title
);
388 tty_putcode(tty
, TTYC_FSL
);
392 tty_update_mode(struct tty
*tty
, int mode
)
396 if (tty
->flags
& TTY_NOCURSOR
)
397 mode
&= ~MODE_CURSOR
;
399 changed
= mode
^ tty
->mode
;
400 if (changed
& MODE_CURSOR
) {
401 if (mode
& MODE_CURSOR
)
402 tty_putcode(tty
, TTYC_CNORM
);
404 tty_putcode(tty
, TTYC_CIVIS
);
406 if (changed
& ALL_MOUSE_MODES
) {
407 if (mode
& ALL_MOUSE_MODES
) {
408 if (mode
& MODE_MOUSE_UTF8
)
409 tty_puts(tty
, "\033[?1005h");
410 if (mode
& MODE_MOUSE_ANY
)
411 tty_puts(tty
, "\033[?1003h");
412 else if (mode
& MODE_MOUSE_BUTTON
)
413 tty_puts(tty
, "\033[?1002h");
414 else if (mode
& MODE_MOUSE_STANDARD
)
415 tty_puts(tty
, "\033[?1000h");
417 if (tty
->mode
& MODE_MOUSE_ANY
)
418 tty_puts(tty
, "\033[?1003l");
419 else if (tty
->mode
& MODE_MOUSE_BUTTON
)
420 tty_puts(tty
, "\033[?1002l");
421 else if (tty
->mode
& MODE_MOUSE_STANDARD
)
422 tty_puts(tty
, "\033[?1000l");
423 if (tty
->mode
& MODE_MOUSE_UTF8
)
424 tty_puts(tty
, "\033[?1005l");
427 if (changed
& MODE_KKEYPAD
) {
428 if (mode
& MODE_KKEYPAD
)
429 tty_putcode(tty
, TTYC_SMKX
);
431 tty_putcode(tty
, TTYC_RMKX
);
438 struct tty
*tty
, enum tty_code_code code
, enum tty_code_code code1
, u_int n
)
440 if (tty_term_has(tty
->term
, code
))
441 tty_putcode1(tty
, code
, n
);
444 tty_putcode(tty
, code1
);
449 * Redraw scroll region using data from screen (already updated). Used when
450 * CSR not supported, or window is a pane that doesn't take up the full
451 * width of the terminal.
454 tty_redraw_region(struct tty
*tty
, const struct tty_ctx
*ctx
)
456 struct window_pane
*wp
= ctx
->wp
;
457 struct screen
*s
= wp
->screen
;
461 * If region is >= 50% of the screen, just schedule a window redraw. In
462 * most cases, this is likely to be followed by some more scrolling -
463 * without this, the entire pane ends up being redrawn many times which
464 * can be much more data.
466 if (ctx
->orlower
- ctx
->orupper
>= screen_size_y(s
) / 2) {
467 wp
->flags
|= PANE_REDRAW
;
471 if (ctx
->ocy
< ctx
->orupper
|| ctx
->ocy
> ctx
->orlower
) {
472 for (i
= ctx
->ocy
; i
< screen_size_y(s
); i
++)
473 tty_draw_line(tty
, s
, i
, wp
->xoff
, wp
->yoff
);
475 for (i
= ctx
->orupper
; i
<= ctx
->orlower
; i
++)
476 tty_draw_line(tty
, s
, i
, wp
->xoff
, wp
->yoff
);
481 tty_draw_line(struct tty
*tty
, struct screen
*s
, u_int py
, u_int ox
, u_int oy
)
483 const struct grid_cell
*gc
;
484 struct grid_line
*gl
;
485 struct grid_cell tmpgc
;
486 const struct grid_utf8
*gu
;
489 tty_update_mode(tty
, tty
->mode
& ~MODE_CURSOR
);
491 sx
= screen_size_x(s
);
492 if (sx
> s
->grid
->linedata
[s
->grid
->hsize
+ py
].cellsize
)
493 sx
= s
->grid
->linedata
[s
->grid
->hsize
+ py
].cellsize
;
498 * Don't move the cursor to the start permission if it will wrap there
503 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ py
- 1];
504 if (oy
+ py
== 0 || gl
== NULL
|| !(gl
->flags
& GRID_LINE_WRAPPED
) ||
505 tty
->cx
< tty
->sx
|| ox
!= 0 ||
506 (oy
+ py
!= tty
->cy
+ 1 && tty
->cy
!= s
->rlower
+ oy
))
507 tty_cursor(tty
, ox
, oy
+ py
);
509 for (i
= 0; i
< sx
; i
++) {
510 gc
= grid_view_peek_cell(s
->grid
, i
, py
);
513 if (gc
->flags
& GRID_FLAG_UTF8
)
514 gu
= grid_view_peek_utf8(s
->grid
, i
, py
);
516 if (screen_check_selection(s
, i
, py
)) {
517 memcpy(&tmpgc
, &s
->sel
.cell
, sizeof tmpgc
);
518 tmpgc
.data
= gc
->data
;
519 tmpgc
.flags
= gc
->flags
&
520 ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
521 tmpgc
.flags
|= s
->sel
.cell
.flags
&
522 (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
523 tty_cell(tty
, &tmpgc
, gu
);
525 tty_cell(tty
, gc
, gu
);
529 tty_update_mode(tty
, tty
->mode
);
534 tty_cursor(tty
, ox
+ sx
, oy
+ py
);
535 if (screen_size_x(s
) >= tty
->sx
&& tty_term_has(tty
->term
, TTYC_EL
))
536 tty_putcode(tty
, TTYC_EL
);
538 for (i
= sx
; i
< screen_size_x(s
); i
++)
541 tty_update_mode(tty
, tty
->mode
);
545 tty_write(void (*cmdfn
)(
546 struct tty
*, const struct tty_ctx
*), const struct tty_ctx
*ctx
)
548 struct window_pane
*wp
= ctx
->wp
;
552 /* wp can be NULL if updating the screen but not the terminal. */
556 if (wp
->window
->flags
& WINDOW_REDRAW
|| wp
->flags
& PANE_REDRAW
)
558 if (!window_pane_visible(wp
))
561 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
562 c
= ARRAY_ITEM(&clients
, i
);
563 if (c
== NULL
|| c
->session
== NULL
)
565 if (c
->flags
& CLIENT_SUSPENDED
)
568 if (c
->session
->curw
->window
== wp
->window
) {
569 if (c
->tty
.term
== NULL
)
571 if (c
->tty
.flags
& (TTY_FREEZE
|TTY_BACKOFF
))
579 tty_cmd_insertcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
581 struct window_pane
*wp
= ctx
->wp
;
582 struct screen
*s
= wp
->screen
;
585 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
) {
586 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, wp
->xoff
, wp
->yoff
);
592 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
594 if (tty_term_has(tty
->term
, TTYC_ICH
) ||
595 tty_term_has(tty
->term
, TTYC_ICH1
))
596 tty_emulate_repeat(tty
, TTYC_ICH
, TTYC_ICH1
, ctx
->num
);
597 else if (tty_term_has(tty
->term
, TTYC_SMIR
) &&
598 tty_term_has(tty
->term
, TTYC_RMIR
)) {
599 tty_putcode(tty
, TTYC_SMIR
);
600 for (i
= 0; i
< ctx
->num
; i
++)
602 tty_putcode(tty
, TTYC_RMIR
);
604 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, wp
->xoff
, wp
->yoff
);
608 tty_cmd_deletecharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
610 struct window_pane
*wp
= ctx
->wp
;
611 struct screen
*s
= wp
->screen
;
613 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
614 (!tty_term_has(tty
->term
, TTYC_DCH
) &&
615 !tty_term_has(tty
->term
, TTYC_DCH1
))) {
616 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, wp
->xoff
, wp
->yoff
);
622 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
624 if (tty_term_has(tty
->term
, TTYC_DCH
) ||
625 tty_term_has(tty
->term
, TTYC_DCH1
))
626 tty_emulate_repeat(tty
, TTYC_DCH
, TTYC_DCH1
, ctx
->num
);
630 tty_cmd_insertline(struct tty
*tty
, const struct tty_ctx
*ctx
)
632 struct window_pane
*wp
= ctx
->wp
;
633 struct screen
*s
= wp
->screen
;
635 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
636 !tty_term_has(tty
->term
, TTYC_CSR
) ||
637 !tty_term_has(tty
->term
, TTYC_IL1
)) {
638 tty_redraw_region(tty
, ctx
);
644 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
645 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
647 tty_emulate_repeat(tty
, TTYC_IL
, TTYC_IL1
, ctx
->num
);
651 tty_cmd_deleteline(struct tty
*tty
, const struct tty_ctx
*ctx
)
653 struct window_pane
*wp
= ctx
->wp
;
654 struct screen
*s
= wp
->screen
;
656 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
657 !tty_term_has(tty
->term
, TTYC_CSR
) ||
658 !tty_term_has(tty
->term
, TTYC_DL1
)) {
659 tty_redraw_region(tty
, ctx
);
665 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
666 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
668 tty_emulate_repeat(tty
, TTYC_DL
, TTYC_DL1
, ctx
->num
);
672 tty_cmd_clearline(struct tty
*tty
, const struct tty_ctx
*ctx
)
674 struct window_pane
*wp
= ctx
->wp
;
675 struct screen
*s
= wp
->screen
;
680 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
);
682 if (wp
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
683 tty_term_has(tty
->term
, TTYC_EL
)) {
684 tty_putcode(tty
, TTYC_EL
);
686 for (i
= 0; i
< screen_size_x(s
); i
++)
692 tty_cmd_clearendofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
694 struct window_pane
*wp
= ctx
->wp
;
695 struct screen
*s
= wp
->screen
;
700 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
702 if (wp
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
703 tty_term_has(tty
->term
, TTYC_EL
))
704 tty_putcode(tty
, TTYC_EL
);
706 for (i
= ctx
->ocx
; i
< screen_size_x(s
); i
++)
712 tty_cmd_clearstartofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
714 struct window_pane
*wp
= ctx
->wp
;
719 if (wp
->xoff
== 0 && tty_term_has(tty
->term
, TTYC_EL1
)) {
720 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
721 tty_putcode(tty
, TTYC_EL1
);
723 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
);
724 for (i
= 0; i
< ctx
->ocx
+ 1; i
++)
730 tty_cmd_reverseindex(struct tty
*tty
, const struct tty_ctx
*ctx
)
732 struct window_pane
*wp
= ctx
->wp
;
733 struct screen
*s
= wp
->screen
;
735 if (ctx
->ocy
!= ctx
->orupper
)
738 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
739 !tty_term_has(tty
->term
, TTYC_CSR
) ||
740 !tty_term_has(tty
->term
, TTYC_RI
)) {
741 tty_redraw_region(tty
, ctx
);
747 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
748 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
750 tty_putcode(tty
, TTYC_RI
);
754 tty_cmd_linefeed(struct tty
*tty
, const struct tty_ctx
*ctx
)
756 struct window_pane
*wp
= ctx
->wp
;
757 struct screen
*s
= wp
->screen
;
759 if (ctx
->ocy
!= ctx
->orlower
)
762 if (wp
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
763 !tty_term_has(tty
->term
, TTYC_CSR
)) {
764 tty_redraw_region(tty
, ctx
);
769 * If this line wrapped naturally (ctx->num is nonzero), don't do
770 * anything - the cursor can just be moved to the last cell and wrap
773 if (ctx
->num
&& !(tty
->term
->flags
& TERM_EARLYWRAP
))
778 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
779 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
785 tty_cmd_clearendofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
787 struct window_pane
*wp
= ctx
->wp
;
788 struct screen
*s
= wp
->screen
;
793 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
794 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
796 if (wp
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
797 tty_term_has(tty
->term
, TTYC_EL
)) {
798 tty_putcode(tty
, TTYC_EL
);
799 if (ctx
->ocy
!= screen_size_y(s
) - 1) {
800 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
+ 1);
801 for (i
= ctx
->ocy
+ 1; i
< screen_size_y(s
); i
++) {
802 tty_putcode(tty
, TTYC_EL
);
803 if (i
== screen_size_y(s
) - 1)
805 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
810 for (i
= ctx
->ocx
; i
< screen_size_x(s
); i
++)
812 for (j
= ctx
->ocy
+ 1; j
< screen_size_y(s
); j
++) {
813 tty_cursor_pane(tty
, ctx
, 0, j
);
814 for (i
= 0; i
< screen_size_x(s
); i
++)
821 tty_cmd_clearstartofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
823 struct window_pane
*wp
= ctx
->wp
;
824 struct screen
*s
= wp
->screen
;
829 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
830 tty_cursor_pane(tty
, ctx
, 0, 0);
832 if (wp
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
833 tty_term_has(tty
->term
, TTYC_EL
)) {
834 for (i
= 0; i
< ctx
->ocy
; i
++) {
835 tty_putcode(tty
, TTYC_EL
);
836 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
840 for (j
= 0; j
< ctx
->ocy
; j
++) {
841 tty_cursor_pane(tty
, ctx
, 0, j
);
842 for (i
= 0; i
< screen_size_x(s
); i
++)
846 for (i
= 0; i
<= ctx
->ocx
; i
++)
851 tty_cmd_clearscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
853 struct window_pane
*wp
= ctx
->wp
;
854 struct screen
*s
= wp
->screen
;
859 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
860 tty_cursor_pane(tty
, ctx
, 0, 0);
862 if (wp
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
863 tty_term_has(tty
->term
, TTYC_EL
)) {
864 for (i
= 0; i
< screen_size_y(s
); i
++) {
865 tty_putcode(tty
, TTYC_EL
);
866 if (i
!= screen_size_y(s
) - 1) {
867 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 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
++)
881 tty_cmd_alignmenttest(struct tty
*tty
, const struct tty_ctx
*ctx
)
883 struct window_pane
*wp
= ctx
->wp
;
884 struct screen
*s
= wp
->screen
;
889 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
891 for (j
= 0; j
< screen_size_y(s
); j
++) {
892 tty_cursor_pane(tty
, ctx
, 0, j
);
893 for (i
= 0; i
< screen_size_x(s
); i
++)
899 tty_cmd_cell(struct tty
*tty
, const struct tty_ctx
*ctx
)
901 struct window_pane
*wp
= ctx
->wp
;
902 struct screen
*s
= wp
->screen
;
905 const struct grid_cell
*gc
= ctx
->cell
;
906 const struct grid_utf8
*gu
= ctx
->utf8
;
908 if (gc
->flags
& GRID_FLAG_UTF8
)
913 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
915 /* Is the cursor in the very last position? */
916 if (ctx
->ocx
> wp
->sx
- width
) {
917 if (wp
->xoff
!= 0 || wp
->sx
!= tty
->sx
) {
919 * The pane doesn't fill the entire line, the linefeed
920 * will already have happened, so just move the cursor.
922 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
+ 1);
923 } else if (tty
->cx
< tty
->sx
) {
925 * The cursor isn't in the last position already, so
926 * move as far left as possible and redraw the last
927 * cell to move into the last position.
929 cx
= screen_size_x(s
) - width
;
930 tty_cursor_pane(tty
, ctx
, cx
, ctx
->ocy
);
931 tty_cell(tty
, &ctx
->last_cell
, &ctx
->last_utf8
);
934 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
936 tty_cell(tty
, ctx
->cell
, ctx
->utf8
);
940 tty_cmd_utf8character(struct tty
*tty
, const struct tty_ctx
*ctx
)
942 struct window_pane
*wp
= ctx
->wp
;
945 * Cannot rely on not being a partial character, so just redraw the
948 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, wp
->xoff
, wp
->yoff
);
952 tty_cmd_setselection(struct tty
*tty
, const struct tty_ctx
*ctx
)
957 if (!tty_term_has(tty
->term
, TTYC_MS
))
960 off
= 4 * ((ctx
->num
+ 2) / 3) + 1; /* storage for base64 */
963 b64_ntop(ctx
->ptr
, ctx
->num
, buf
, off
);
964 tty_putcode_ptr2(tty
, TTYC_MS
, "", buf
);
970 tty_cmd_rawstring(struct tty
*tty
, const struct tty_ctx
*ctx
)
973 u_char
*str
= ctx
->ptr
;
975 for (i
= 0; i
< ctx
->num
; i
++)
976 tty_putc(tty
, str
[i
]);
981 struct tty
*tty
, const struct grid_cell
*gc
, const struct grid_utf8
*gu
)
985 /* Skip last character if terminal is stupid. */
986 if (tty
->term
->flags
& TERM_EARLYWRAP
&&
987 tty
->cy
== tty
->sy
- 1 && tty
->cx
== tty
->sx
- 1)
990 /* If this is a padding character, do nothing. */
991 if (gc
->flags
& GRID_FLAG_PADDING
)
994 /* Set the attributes. */
995 tty_attributes(tty
, gc
);
997 /* If not UTF-8, write directly. */
998 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
999 if (gc
->data
< 0x20 || gc
->data
== 0x7f)
1001 tty_putc(tty
, gc
->data
);
1005 /* If the terminal doesn't support UTF-8, write underscores. */
1006 if (!(tty
->flags
& TTY_UTF8
)) {
1007 for (i
= 0; i
< gu
->width
; i
++)
1012 /* Otherwise, write UTF-8. */
1013 tty_pututf8(tty
, gu
);
1017 tty_reset(struct tty
*tty
)
1019 struct grid_cell
*gc
= &tty
->cell
;
1021 if (memcmp(gc
, &grid_default_cell
, sizeof *gc
) == 0)
1024 if ((gc
->attr
& GRID_ATTR_CHARSET
) && tty_use_acs(tty
))
1025 tty_putcode(tty
, TTYC_RMACS
);
1026 tty_putcode(tty
, TTYC_SGR0
);
1027 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
1030 /* Set region inside pane. */
1033 struct tty
*tty
, const struct tty_ctx
*ctx
, u_int rupper
, u_int rlower
)
1035 struct window_pane
*wp
= ctx
->wp
;
1037 tty_region(tty
, wp
->yoff
+ rupper
, wp
->yoff
+ rlower
);
1040 /* Set region at absolute position. */
1042 tty_region(struct tty
*tty
, u_int rupper
, u_int rlower
)
1044 if (tty
->rlower
== rlower
&& tty
->rupper
== rupper
)
1046 if (!tty_term_has(tty
->term
, TTYC_CSR
))
1049 tty
->rupper
= rupper
;
1050 tty
->rlower
= rlower
;
1053 * Some terminals (such as PuTTY) do not correctly reset the cursor to
1054 * 0,0 if it is beyond the last column (they do not reset their wrap
1055 * flag so further output causes a line feed). As a workaround, do an
1056 * explicit move to 0 first.
1058 if (tty
->cx
>= tty
->sx
)
1059 tty_cursor(tty
, 0, tty
->cy
);
1061 tty_putcode2(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
1062 tty_cursor(tty
, 0, 0);
1065 /* Move cursor inside pane. */
1067 tty_cursor_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int cx
, u_int cy
)
1069 struct window_pane
*wp
= ctx
->wp
;
1071 tty_cursor(tty
, wp
->xoff
+ cx
, wp
->yoff
+ cy
);
1074 /* Move cursor to absolute position. */
1076 tty_cursor(struct tty
*tty
, u_int cx
, u_int cy
)
1078 struct tty_term
*term
= tty
->term
;
1082 if (cx
> tty
->sx
- 1)
1089 if (cx
== thisx
&& cy
== thisy
)
1092 /* Very end of the line, just use absolute movement. */
1093 if (thisx
> tty
->sx
- 1)
1096 /* Move to home position (0, 0). */
1097 if (cx
== 0 && cy
== 0 && tty_term_has(term
, TTYC_HOME
)) {
1098 tty_putcode(tty
, TTYC_HOME
);
1102 /* Zero on the next line. */
1103 if (cx
== 0 && cy
== thisy
+ 1 && thisy
!= tty
->rlower
) {
1104 tty_putc(tty
, '\r');
1105 tty_putc(tty
, '\n');
1109 /* Moving column or row. */
1112 * Moving column only, row staying the same.
1117 tty_putc(tty
, '\r');
1121 /* One to the left. */
1122 if (cx
== thisx
- 1 && tty_term_has(term
, TTYC_CUB1
)) {
1123 tty_putcode(tty
, TTYC_CUB1
);
1127 /* One to the right. */
1128 if (cx
== thisx
+ 1 && tty_term_has(term
, TTYC_CUF1
)) {
1129 tty_putcode(tty
, TTYC_CUF1
);
1133 /* Calculate difference. */
1134 change
= thisx
- cx
; /* +ve left, -ve right */
1137 * Use HPA if change is larger than absolute, otherwise move
1138 * the cursor with CUB/CUF.
1140 if ((u_int
) abs(change
) > cx
&& tty_term_has(term
, TTYC_HPA
)) {
1141 tty_putcode1(tty
, TTYC_HPA
, cx
);
1143 } else if (change
> 0 && tty_term_has(term
, TTYC_CUB
)) {
1144 tty_putcode1(tty
, TTYC_CUB
, change
);
1146 } else if (change
< 0 && tty_term_has(term
, TTYC_CUF
)) {
1147 tty_putcode1(tty
, TTYC_CUF
, -change
);
1150 } else if (cx
== thisx
) {
1152 * Moving row only, column staying the same.
1156 if (thisy
!= tty
->rupper
&&
1157 cy
== thisy
- 1 && tty_term_has(term
, TTYC_CUU1
)) {
1158 tty_putcode(tty
, TTYC_CUU1
);
1163 if (thisy
!= tty
->rlower
&&
1164 cy
== thisy
+ 1 && tty_term_has(term
, TTYC_CUD1
)) {
1165 tty_putcode(tty
, TTYC_CUD1
);
1169 /* Calculate difference. */
1170 change
= thisy
- cy
; /* +ve up, -ve down */
1173 * Try to use VPA if change is larger than absolute or if this
1174 * change would cross the scroll region, otherwise use CUU/CUD.
1176 if ((u_int
) abs(change
) > cy
||
1177 (change
< 0 && cy
- change
> tty
->rlower
) ||
1178 (change
> 0 && cy
- change
< tty
->rupper
)) {
1179 if (tty_term_has(term
, TTYC_VPA
)) {
1180 tty_putcode1(tty
, TTYC_VPA
, cy
);
1183 } else if (change
> 0 && tty_term_has(term
, TTYC_CUU
)) {
1184 tty_putcode1(tty
, TTYC_CUU
, change
);
1186 } else if (change
< 0 && tty_term_has(term
, TTYC_CUD
)) {
1187 tty_putcode1(tty
, TTYC_CUD
, -change
);
1193 /* Absolute movement. */
1194 tty_putcode2(tty
, TTYC_CUP
, cy
, cx
);
1202 tty_attributes(struct tty
*tty
, const struct grid_cell
*gc
)
1204 struct grid_cell
*tc
= &tty
->cell
, gc2
;
1207 memcpy(&gc2
, gc
, sizeof gc2
);
1210 * If no setab, try to use the reverse attribute as a best-effort for a
1211 * non-default background. This is a bit of a hack but it doesn't do
1212 * any serious harm and makes a couple of applications happier.
1214 if (!tty_term_has(tty
->term
, TTYC_SETAB
)) {
1215 if (gc2
.attr
& GRID_ATTR_REVERSE
) {
1216 if (gc2
.fg
!= 7 && gc2
.fg
!= 8)
1217 gc2
.attr
&= ~GRID_ATTR_REVERSE
;
1219 if (gc2
.bg
!= 0 && gc2
.bg
!= 8)
1220 gc2
.attr
|= GRID_ATTR_REVERSE
;
1224 /* Fix up the colours if necessary. */
1225 tty_check_fg(tty
, &gc2
);
1226 tty_check_bg(tty
, &gc2
);
1228 /* If any bits are being cleared, reset everything. */
1229 if (tc
->attr
& ~gc2
.attr
)
1233 * Set the colours. This may call tty_reset() (so it comes next) and
1234 * may add to (NOT remove) the desired attributes by changing new_attr.
1236 tty_colours(tty
, &gc2
);
1238 /* Filter out attribute bits already set. */
1239 changed
= gc2
.attr
& ~tc
->attr
;
1240 tc
->attr
= gc2
.attr
;
1242 /* Set the attributes. */
1243 if (changed
& GRID_ATTR_BRIGHT
)
1244 tty_putcode(tty
, TTYC_BOLD
);
1245 if (changed
& GRID_ATTR_DIM
)
1246 tty_putcode(tty
, TTYC_DIM
);
1247 if (changed
& GRID_ATTR_ITALICS
)
1249 if (tty_term_has(tty
->term
, TTYC_SITM
))
1250 tty_putcode(tty
, TTYC_SITM
);
1252 tty_putcode(tty
, TTYC_SMSO
);
1254 if (changed
& GRID_ATTR_UNDERSCORE
)
1255 tty_putcode(tty
, TTYC_SMUL
);
1256 if (changed
& GRID_ATTR_BLINK
)
1257 tty_putcode(tty
, TTYC_BLINK
);
1258 if (changed
& GRID_ATTR_REVERSE
) {
1259 if (tty_term_has(tty
->term
, TTYC_REV
))
1260 tty_putcode(tty
, TTYC_REV
);
1261 else if (tty_term_has(tty
->term
, TTYC_SMSO
))
1262 tty_putcode(tty
, TTYC_SMSO
);
1264 if (changed
& GRID_ATTR_HIDDEN
)
1265 tty_putcode(tty
, TTYC_INVIS
);
1266 if ((changed
& GRID_ATTR_CHARSET
) && tty_use_acs(tty
))
1267 tty_putcode(tty
, TTYC_SMACS
);
1271 tty_colours(struct tty
*tty
, const struct grid_cell
*gc
)
1273 struct grid_cell
*tc
= &tty
->cell
;
1274 u_char fg
= gc
->fg
, bg
= gc
->bg
, flags
= gc
->flags
;
1275 int have_ax
, fg_default
, bg_default
;
1277 /* No changes? Nothing is necessary. */
1278 if (fg
== tc
->fg
&& bg
== tc
->bg
&&
1279 ((flags
^ tc
->flags
) & (GRID_FLAG_FG256
|GRID_FLAG_BG256
)) == 0)
1283 * Is either the default colour? This is handled specially because the
1284 * best solution might be to reset both colours to default, in which
1285 * case if only one is default need to fall onward to set the other
1288 fg_default
= (fg
== 8 && !(flags
& GRID_FLAG_FG256
));
1289 bg_default
= (bg
== 8 && !(flags
& GRID_FLAG_BG256
));
1290 if (fg_default
|| bg_default
) {
1292 * If don't have AX but do have op, send sgr0 (op can't
1293 * actually be used because it is sometimes the same as sgr0
1294 * and sometimes isn't). This resets both colours to default.
1296 * Otherwise, try to set the default colour only as needed.
1298 have_ax
= tty_term_has(tty
->term
, TTYC_AX
);
1299 if (!have_ax
&& tty_term_has(tty
->term
, TTYC_OP
))
1303 (tc
->fg
!= 8 || tc
->flags
& GRID_FLAG_FG256
)) {
1305 tty_puts(tty
, "\033[39m");
1306 else if (tc
->fg
!= 7 ||
1307 tc
->flags
& GRID_FLAG_FG256
)
1308 tty_putcode1(tty
, TTYC_SETAF
, 7);
1310 tc
->flags
&= ~GRID_FLAG_FG256
;
1313 (tc
->bg
!= 8 || tc
->flags
& GRID_FLAG_BG256
)) {
1315 tty_puts(tty
, "\033[49m");
1316 else if (tc
->bg
!= 0 ||
1317 tc
->flags
& GRID_FLAG_BG256
)
1318 tty_putcode1(tty
, TTYC_SETAB
, 0);
1320 tc
->flags
&= ~GRID_FLAG_BG256
;
1325 /* Set the foreground colour. */
1326 if (!fg_default
&& (fg
!= tc
->fg
||
1327 ((flags
& GRID_FLAG_FG256
) != (tc
->flags
& GRID_FLAG_FG256
))))
1328 tty_colours_fg(tty
, gc
);
1331 * Set the background colour. This must come after the foreground as
1332 * tty_colour_fg() can call tty_reset().
1334 if (!bg_default
&& (bg
!= tc
->bg
||
1335 ((flags
& GRID_FLAG_BG256
) != (tc
->flags
& GRID_FLAG_BG256
))))
1336 tty_colours_bg(tty
, gc
);
1340 tty_check_fg(struct tty
*tty
, struct grid_cell
*gc
)
1344 /* Is this a 256-colour colour? */
1345 if (gc
->flags
& GRID_FLAG_FG256
) {
1346 /* And not a 256 colour mode? */
1347 if (!(tty
->term
->flags
& TERM_88COLOURS
) &&
1348 !(tty
->term_flags
& TERM_88COLOURS
) &&
1349 !(tty
->term
->flags
& TERM_256COLOURS
) &&
1350 !(tty
->term_flags
& TERM_256COLOURS
)) {
1351 gc
->fg
= colour_256to16(gc
->fg
);
1354 gc
->attr
|= GRID_ATTR_BRIGHT
;
1356 gc
->attr
&= ~GRID_ATTR_BRIGHT
;
1357 gc
->flags
&= ~GRID_FLAG_FG256
;
1362 /* Is this an aixterm colour? */
1363 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
1364 if (gc
->fg
>= 90 && gc
->fg
<= 97 && colours
< 16) {
1366 gc
->attr
|= GRID_ATTR_BRIGHT
;
1371 tty_check_bg(struct tty
*tty
, struct grid_cell
*gc
)
1375 /* Is this a 256-colour colour? */
1376 if (gc
->flags
& GRID_FLAG_BG256
) {
1378 * And not a 256 colour mode? Translate to 16-colour
1379 * palette. Bold background doesn't exist portably, so just
1380 * discard the bold bit if set.
1382 if (!(tty
->term
->flags
& TERM_88COLOURS
) &&
1383 !(tty
->term_flags
& TERM_88COLOURS
) &&
1384 !(tty
->term
->flags
& TERM_256COLOURS
) &&
1385 !(tty
->term_flags
& TERM_256COLOURS
)) {
1386 gc
->bg
= colour_256to16(gc
->bg
);
1389 gc
->attr
&= ~GRID_ATTR_BRIGHT
;
1390 gc
->flags
&= ~GRID_FLAG_BG256
;
1395 /* Is this an aixterm colour? */
1396 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
1397 if (gc
->bg
>= 100 && gc
->bg
<= 107 && colours
< 16) {
1399 gc
->attr
|= GRID_ATTR_BRIGHT
;
1404 tty_colours_fg(struct tty
*tty
, const struct grid_cell
*gc
)
1406 struct grid_cell
*tc
= &tty
->cell
;
1410 /* Is this a 256-colour colour? */
1411 if (gc
->flags
& GRID_FLAG_FG256
) {
1412 /* Try as 256 colours or translating to 88. */
1413 if (tty_try_256(tty
, fg
, "38") == 0)
1415 if (tty_try_88(tty
, fg
, "38") == 0)
1417 /* Else already handled by tty_check_fg. */
1421 /* Is this an aixterm bright colour? */
1422 if (fg
>= 90 && fg
<= 97) {
1423 xsnprintf(s
, sizeof s
, "\033[%dm", fg
);
1428 /* Otherwise set the foreground colour. */
1429 tty_putcode1(tty
, TTYC_SETAF
, fg
);
1432 /* Save the new values in the terminal current cell. */
1434 tc
->flags
&= ~GRID_FLAG_FG256
;
1435 tc
->flags
|= gc
->flags
& GRID_FLAG_FG256
;
1439 tty_colours_bg(struct tty
*tty
, const struct grid_cell
*gc
)
1441 struct grid_cell
*tc
= &tty
->cell
;
1445 /* Is this a 256-colour colour? */
1446 if (gc
->flags
& GRID_FLAG_BG256
) {
1447 /* Try as 256 colours or translating to 88. */
1448 if (tty_try_256(tty
, bg
, "48") == 0)
1450 if (tty_try_88(tty
, bg
, "48") == 0)
1452 /* Else already handled by tty_check_bg. */
1456 /* Is this an aixterm bright colour? */
1457 if (bg
>= 100 && bg
<= 107) {
1458 /* 16 colour terminals or above only. */
1459 if (tty_term_number(tty
->term
, TTYC_COLORS
) >= 16) {
1460 xsnprintf(s
, sizeof s
, "\033[%dm", bg
);
1465 /* no such thing as a bold background */
1468 /* Otherwise set the background colour. */
1469 tty_putcode1(tty
, TTYC_SETAB
, bg
);
1472 /* Save the new values in the terminal current cell. */
1474 tc
->flags
&= ~GRID_FLAG_BG256
;
1475 tc
->flags
|= gc
->flags
& GRID_FLAG_BG256
;
1479 tty_try_256(struct tty
*tty
, u_char colour
, const char *type
)
1483 if (!(tty
->term
->flags
& TERM_256COLOURS
) &&
1484 !(tty
->term_flags
& TERM_256COLOURS
))
1487 xsnprintf(s
, sizeof s
, "\033[%s;5;%hhum", type
, colour
);
1493 tty_try_88(struct tty
*tty
, u_char colour
, const char *type
)
1497 if (!(tty
->term
->flags
& TERM_88COLOURS
) &&
1498 !(tty
->term_flags
& TERM_88COLOURS
))
1500 colour
= colour_256to88(colour
);
1502 xsnprintf(s
, sizeof s
, "\033[%s;5;%hhum", type
, colour
);