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 *);
39 void tty_colours(struct tty
*, const struct grid_cell
*);
40 void tty_check_fg(struct tty
*, struct grid_cell
*);
41 void tty_check_bg(struct tty
*, struct grid_cell
*);
42 void tty_colours_fg(struct tty
*, const struct grid_cell
*);
43 void tty_colours_bg(struct tty
*, const struct grid_cell
*);
45 int tty_large_region(struct tty
*, const struct tty_ctx
*);
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_repeat_space(struct tty
*, u_int
);
50 void tty_cell(struct tty
*, const struct grid_cell
*);
52 #define tty_use_acs(tty) \
53 (tty_term_has((tty)->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
55 #define tty_pane_full_width(tty, ctx) \
56 ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
59 tty_init(struct tty
*tty
, struct client
*c
, int fd
, char *term
)
63 memset(tty
, 0, sizeof *tty
);
66 if (term
== NULL
|| *term
== '\0')
67 tty
->termname
= xstrdup("unknown");
69 tty
->termname
= xstrdup(term
);
73 if ((path
= ttyname(fd
)) == NULL
)
74 fatalx("ttyname failed");
75 tty
->path
= xstrdup(path
);
77 tty
->ccolour
= xstrdup("");
84 tty_resize(struct tty
*tty
)
89 if (ioctl(tty
->fd
, TIOCGWINSZ
, &ws
) != -1) {
100 if (!tty_set_size(tty
, sx
, sy
))
106 tty
->rupper
= UINT_MAX
;
107 tty
->rlower
= UINT_MAX
;
110 * If the terminal has been started, reset the actual scroll region and
111 * cursor position, as this may not have happened.
113 if (tty
->flags
& TTY_STARTED
) {
114 tty_cursor(tty
, 0, 0);
115 tty_region(tty
, 0, tty
->sy
- 1);
122 tty_set_size(struct tty
*tty
, u_int sx
, u_int sy
) {
123 if (sx
== tty
->sx
&& sy
== tty
->sy
)
131 tty_open(struct tty
*tty
, const char *overrides
, char **cause
)
136 if (debug_level
> 3) {
137 xsnprintf(out
, sizeof out
, "tmux-out-%ld.log", (long) getpid());
138 fd
= open(out
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
139 if (fd
!= -1 && fcntl(fd
, F_SETFD
, FD_CLOEXEC
) == -1)
140 fatal("fcntl failed");
144 tty
->term
= tty_term_find(tty
->termname
, tty
->fd
, overrides
, cause
);
145 if (tty
->term
== NULL
) {
149 tty
->flags
|= TTY_OPENED
;
151 tty
->flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
|TTY_TIMER
);
153 tty
->event
= bufferevent_new(
154 tty
->fd
, tty_read_callback
, NULL
, tty_error_callback
, tty
);
164 tty_read_callback(unused
struct bufferevent
*bufev
, void *data
)
166 struct tty
*tty
= data
;
168 while (tty_keys_next(tty
))
174 unused
struct bufferevent
*bufev
, unused
short what
, unused
void *data
)
179 tty_init_termios(int fd
, struct termios
*orig_tio
, struct bufferevent
*bufev
)
183 if (fd
== -1 || tcgetattr(fd
, orig_tio
) != 0)
189 bufferevent_enable(bufev
, EV_READ
|EV_WRITE
);
191 memcpy(&tio
, orig_tio
, sizeof tio
);
192 tio
.c_iflag
&= ~(IXON
|IXOFF
|ICRNL
|INLCR
|IGNCR
|IMAXBEL
|ISTRIP
);
193 tio
.c_iflag
|= IGNBRK
;
194 tio
.c_oflag
&= ~(OPOST
|ONLCR
|OCRNL
|ONLRET
);
195 tio
.c_lflag
&= ~(IEXTEN
|ICANON
|ECHO
|ECHOE
|ECHONL
|ECHOCTL
|
196 ECHOPRT
|ECHOKE
|ECHOCTL
|ISIG
);
199 if (tcsetattr(fd
, TCSANOW
, &tio
) == 0)
200 tcflush(fd
, TCIOFLUSH
);
204 tty_start_tty(struct tty
*tty
)
206 tty_init_termios(tty
->fd
, &tty
->tio
, tty
->event
);
208 tty_putcode(tty
, TTYC_SMCUP
);
210 tty_putcode(tty
, TTYC_SGR0
);
211 memcpy(&tty
->cell
, &grid_default_cell
, sizeof tty
->cell
);
213 tty_putcode(tty
, TTYC_RMKX
);
214 if (tty_use_acs(tty
))
215 tty_putcode(tty
, TTYC_ENACS
);
216 tty_putcode(tty
, TTYC_CLEAR
);
218 tty_putcode(tty
, TTYC_CNORM
);
219 if (tty_term_has(tty
->term
, TTYC_KMOUS
))
220 tty_puts(tty
, "\033[?1000l\033[?1006l\033[?1005l");
222 if (tty_term_has(tty
->term
, TTYC_XT
))
223 tty_puts(tty
, "\033[c\033[>4;1m\033[?1004h");
228 tty
->rlower
= UINT_MAX
;
229 tty
->rupper
= UINT_MAX
;
231 tty
->mode
= MODE_CURSOR
;
233 tty
->flags
|= TTY_STARTED
;
235 tty_force_cursor_colour(tty
, "");
239 tty_set_class(struct tty
*tty
, u_int
class)
247 tty_stop_tty(struct tty
*tty
)
251 if (!(tty
->flags
& TTY_STARTED
))
253 tty
->flags
&= ~TTY_STARTED
;
255 bufferevent_disable(tty
->event
, EV_READ
|EV_WRITE
);
258 * Be flexible about error handling and try not kill the server just
259 * because the fd is invalid. Things like ssh -t can easily leave us
262 if (ioctl(tty
->fd
, TIOCGWINSZ
, &ws
) == -1)
264 if (tcsetattr(tty
->fd
, TCSANOW
, &tty
->tio
) == -1)
267 tty_raw(tty
, tty_term_string2(tty
->term
, TTYC_CSR
, 0, ws
.ws_row
- 1));
268 if (tty_use_acs(tty
))
269 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMACS
));
270 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_SGR0
));
271 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMKX
));
272 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CLEAR
));
273 if (tty_term_has(tty
->term
, TTYC_CS1
) && tty
->cstyle
!= 0) {
274 if (tty_term_has(tty
->term
, TTYC_CSR1
))
275 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CSR1
));
277 tty_raw(tty
, tty_term_string1(tty
->term
, TTYC_CS1
, 0));
279 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CR
));
281 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CNORM
));
282 if (tty_term_has(tty
->term
, TTYC_KMOUS
))
283 tty_raw(tty
, "\033[?1000l\033[?1006l\033[?1005l");
285 if (tty_term_has(tty
->term
, TTYC_XT
))
286 tty_raw(tty
, "\033[>4m\033[?1004l");
288 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMCUP
));
290 setblocking(tty
->fd
, 1);
294 tty_close(struct tty
*tty
)
296 if (tty
->log_fd
!= -1) {
301 if (event_initialized(&tty
->key_timer
))
302 evtimer_del(&tty
->key_timer
);
305 if (tty
->flags
& TTY_OPENED
) {
306 bufferevent_free(tty
->event
);
308 tty_term_free(tty
->term
);
311 tty
->flags
&= ~TTY_OPENED
;
321 tty_free(struct tty
*tty
)
326 if (tty
->path
!= NULL
)
328 if (tty
->termname
!= NULL
)
333 tty_raw(struct tty
*tty
, const char *s
)
339 for (i
= 0; i
< 5; i
++) {
340 n
= write(tty
->fd
, s
, slen
);
346 } else if (n
== -1 && errno
!= EAGAIN
)
353 tty_putcode(struct tty
*tty
, enum tty_code_code code
)
355 tty_puts(tty
, tty_term_string(tty
->term
, code
));
359 tty_putcode1(struct tty
*tty
, enum tty_code_code code
, int a
)
363 tty_puts(tty
, tty_term_string1(tty
->term
, code
, a
));
367 tty_putcode2(struct tty
*tty
, enum tty_code_code code
, int a
, int b
)
371 tty_puts(tty
, tty_term_string2(tty
->term
, code
, a
, b
));
375 tty_putcode_ptr1(struct tty
*tty
, enum tty_code_code code
, const void *a
)
378 tty_puts(tty
, tty_term_ptr1(tty
->term
, code
, a
));
382 tty_putcode_ptr2(struct tty
*tty
, enum tty_code_code code
, const void *a
, const void *b
)
384 if (a
!= NULL
&& b
!= NULL
)
385 tty_puts(tty
, tty_term_ptr2(tty
->term
, code
, a
, b
));
389 tty_puts(struct tty
*tty
, const char *s
)
393 bufferevent_write(tty
->event
, s
, strlen(s
));
395 if (tty
->log_fd
!= -1)
396 write(tty
->log_fd
, s
, strlen(s
));
400 tty_putc(struct tty
*tty
, u_char ch
)
405 if (tty
->cell
.attr
& GRID_ATTR_CHARSET
) {
406 acs
= tty_acs_get(tty
, ch
);
408 bufferevent_write(tty
->event
, acs
, strlen(acs
));
410 bufferevent_write(tty
->event
, &ch
, 1);
412 bufferevent_write(tty
->event
, &ch
, 1);
414 if (ch
>= 0x20 && ch
!= 0x7f) {
416 if (tty
->term
->flags
& TERM_EARLYWRAP
)
421 if (tty
->cy
!= tty
->rlower
)
427 if (tty
->log_fd
!= -1)
428 write(tty
->log_fd
, &ch
, 1);
432 tty_putn(struct tty
*tty
, const void *buf
, size_t len
, u_int width
)
434 bufferevent_write(tty
->event
, buf
, len
);
435 if (tty
->log_fd
!= -1)
436 write(tty
->log_fd
, buf
, len
);
441 tty_set_title(struct tty
*tty
, const char *title
)
443 if (!tty_term_has(tty
->term
, TTYC_TSL
) ||
444 !tty_term_has(tty
->term
, TTYC_FSL
))
447 tty_putcode(tty
, TTYC_TSL
);
448 tty_puts(tty
, title
);
449 tty_putcode(tty
, TTYC_FSL
);
453 tty_force_cursor_colour(struct tty
*tty
, const char *ccolour
)
455 if (*ccolour
== '\0')
456 tty_putcode(tty
, TTYC_CR
);
458 tty_putcode_ptr1(tty
, TTYC_CC
, ccolour
);
460 tty
->ccolour
= xstrdup(ccolour
);
464 tty_update_mode(struct tty
*tty
, int mode
, struct screen
*s
)
468 if (strcmp(s
->ccolour
, tty
->ccolour
))
469 tty_force_cursor_colour(tty
, s
->ccolour
);
471 if (tty
->flags
& TTY_NOCURSOR
)
472 mode
&= ~MODE_CURSOR
;
474 changed
= mode
^ tty
->mode
;
475 if (changed
& MODE_CURSOR
) {
476 if (mode
& MODE_CURSOR
)
477 tty_putcode(tty
, TTYC_CNORM
);
479 tty_putcode(tty
, TTYC_CIVIS
);
481 if (tty
->cstyle
!= s
->cstyle
) {
482 if (tty_term_has(tty
->term
, TTYC_CS1
)) {
483 if (s
->cstyle
== 0 &&
484 tty_term_has(tty
->term
, TTYC_CSR1
))
485 tty_putcode(tty
, TTYC_CSR1
);
487 tty_putcode1(tty
, TTYC_CS1
, s
->cstyle
);
489 tty
->cstyle
= s
->cstyle
;
491 if (changed
& (ALL_MOUSE_MODES
|MODE_MOUSE_UTF8
)) {
492 if (mode
& ALL_MOUSE_MODES
) {
494 * Enable the UTF-8 (1005) extension if configured to.
495 * Enable the SGR (1006) extension unconditionally, as
496 * this is safe from misinterpretation. Do it in this
497 * order, because in some terminals it's the last one
498 * that takes effect and SGR is the preferred one.
500 if (mode
& MODE_MOUSE_UTF8
)
501 tty_puts(tty
, "\033[?1005h");
503 tty_puts(tty
, "\033[?1005l");
504 tty_puts(tty
, "\033[?1006h");
506 if (mode
& MODE_MOUSE_ANY
)
507 tty_puts(tty
, "\033[?1003h");
508 else if (mode
& MODE_MOUSE_BUTTON
)
509 tty_puts(tty
, "\033[?1002h");
510 else if (mode
& MODE_MOUSE_STANDARD
)
511 tty_puts(tty
, "\033[?1000h");
513 if (tty
->mode
& MODE_MOUSE_ANY
)
514 tty_puts(tty
, "\033[?1003l");
515 else if (tty
->mode
& MODE_MOUSE_BUTTON
)
516 tty_puts(tty
, "\033[?1002l");
517 else if (tty
->mode
& MODE_MOUSE_STANDARD
)
518 tty_puts(tty
, "\033[?1000l");
520 tty_puts(tty
, "\033[?1006l");
521 if (tty
->mode
& MODE_MOUSE_UTF8
)
522 tty_puts(tty
, "\033[?1005l");
525 if (changed
& MODE_KKEYPAD
) {
526 if (mode
& MODE_KKEYPAD
)
527 tty_putcode(tty
, TTYC_SMKX
);
529 tty_putcode(tty
, TTYC_RMKX
);
531 if (changed
& MODE_BRACKETPASTE
) {
532 if (mode
& MODE_BRACKETPASTE
)
533 tty_puts(tty
, "\033[?2004h");
535 tty_puts(tty
, "\033[?2004l");
542 struct tty
*tty
, enum tty_code_code code
, enum tty_code_code code1
, u_int n
)
544 if (tty_term_has(tty
->term
, code
))
545 tty_putcode1(tty
, code
, n
);
548 tty_putcode(tty
, code1
);
553 tty_repeat_space(struct tty
*tty
, u_int n
)
560 * Is the region large enough to be worth redrawing once later rather than
561 * probably several times now? Currently yes if it is more than 50% of the
565 tty_large_region(unused
struct tty
*tty
, const struct tty_ctx
*ctx
)
567 struct window_pane
*wp
= ctx
->wp
;
569 return (ctx
->orlower
- ctx
->orupper
>= screen_size_y(wp
->screen
) / 2);
573 * Redraw scroll region using data from screen (already updated). Used when
574 * CSR not supported, or window is a pane that doesn't take up the full
575 * width of the terminal.
578 tty_redraw_region(struct tty
*tty
, const struct tty_ctx
*ctx
)
580 struct window_pane
*wp
= ctx
->wp
;
581 struct screen
*s
= wp
->screen
;
585 * If region is large, schedule a window redraw. In most cases this is
586 * likely to be followed by some more scrolling.
588 if (tty_large_region(tty
, ctx
)) {
589 wp
->flags
|= PANE_REDRAW
;
593 if (ctx
->ocy
< ctx
->orupper
|| ctx
->ocy
> ctx
->orlower
) {
594 for (i
= ctx
->ocy
; i
< screen_size_y(s
); i
++)
595 tty_draw_line(tty
, s
, i
, ctx
->xoff
, ctx
->yoff
);
597 for (i
= ctx
->orupper
; i
<= ctx
->orlower
; i
++)
598 tty_draw_line(tty
, s
, i
, ctx
->xoff
, ctx
->yoff
);
603 tty_draw_line(struct tty
*tty
, struct screen
*s
, u_int py
, u_int ox
, u_int oy
)
605 const struct grid_cell
*gc
;
606 struct grid_line
*gl
;
607 struct grid_cell tmpgc
;
611 tty_update_mode(tty
, tty
->mode
& ~MODE_CURSOR
, s
);
613 sx
= screen_size_x(s
);
614 if (sx
> s
->grid
->linedata
[s
->grid
->hsize
+ py
].cellsize
)
615 sx
= s
->grid
->linedata
[s
->grid
->hsize
+ py
].cellsize
;
620 * Don't move the cursor to the start permission if it will wrap there
625 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ py
- 1];
626 if (oy
+ py
== 0 || gl
== NULL
|| !(gl
->flags
& GRID_LINE_WRAPPED
) ||
627 tty
->cx
< tty
->sx
|| ox
!= 0 ||
628 (oy
+ py
!= tty
->cy
+ 1 && tty
->cy
!= s
->rlower
+ oy
))
629 tty_cursor(tty
, ox
, oy
+ py
);
631 for (i
= 0; i
< sx
; i
++) {
632 gc
= grid_view_peek_cell(s
->grid
, i
, py
);
633 if (screen_check_selection(s
, i
, py
)) {
634 memcpy(&tmpgc
, &s
->sel
.cell
, sizeof tmpgc
);
635 grid_cell_get(gc
, &ud
);
636 grid_cell_set(&tmpgc
, &ud
);
637 tmpgc
.flags
= gc
->flags
&
638 ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
639 tmpgc
.flags
|= s
->sel
.cell
.flags
&
640 (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
641 tty_cell(tty
, &tmpgc
);
647 tty_update_mode(tty
, tty
->mode
, s
);
652 tty_cursor(tty
, ox
+ sx
, oy
+ py
);
653 if (sx
!= screen_size_x(s
) && ox
+ screen_size_x(s
) >= tty
->sx
&&
654 tty_term_has(tty
->term
, TTYC_EL
))
655 tty_putcode(tty
, TTYC_EL
);
657 tty_repeat_space(tty
, screen_size_x(s
) - sx
);
658 tty_update_mode(tty
, tty
->mode
, s
);
663 void (*cmdfn
)(struct tty
*, const struct tty_ctx
*), struct tty_ctx
*ctx
)
665 struct window_pane
*wp
= ctx
->wp
;
669 /* wp can be NULL if updating the screen but not the terminal. */
673 if (wp
->window
->flags
& WINDOW_REDRAW
|| wp
->flags
& PANE_REDRAW
)
675 if (!window_pane_visible(wp
) || wp
->flags
& PANE_DROP
)
678 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
679 c
= ARRAY_ITEM(&clients
, i
);
680 if (c
== NULL
|| c
->session
== NULL
|| c
->tty
.term
== NULL
)
682 if (c
->flags
& CLIENT_SUSPENDED
)
684 if (c
->tty
.flags
& TTY_FREEZE
)
686 if (c
->session
->curw
->window
!= wp
->window
)
689 ctx
->xoff
= wp
->xoff
;
690 ctx
->yoff
= wp
->yoff
;
691 if (status_at_line(c
) == 0)
699 tty_cmd_insertcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
701 struct window_pane
*wp
= ctx
->wp
;
703 if (!tty_pane_full_width(tty
, ctx
)) {
704 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, ctx
->xoff
, ctx
->yoff
);
710 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
712 if (tty_term_has(tty
->term
, TTYC_ICH
) ||
713 tty_term_has(tty
->term
, TTYC_ICH1
))
714 tty_emulate_repeat(tty
, TTYC_ICH
, TTYC_ICH1
, ctx
->num
);
716 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, ctx
->xoff
, ctx
->yoff
);
720 tty_cmd_deletecharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
722 struct window_pane
*wp
= ctx
->wp
;
724 if (!tty_pane_full_width(tty
, ctx
) ||
725 (!tty_term_has(tty
->term
, TTYC_DCH
) &&
726 !tty_term_has(tty
->term
, TTYC_DCH1
))) {
727 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, ctx
->xoff
, ctx
->yoff
);
733 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
735 if (tty_term_has(tty
->term
, TTYC_DCH
) ||
736 tty_term_has(tty
->term
, TTYC_DCH1
))
737 tty_emulate_repeat(tty
, TTYC_DCH
, TTYC_DCH1
, ctx
->num
);
741 tty_cmd_clearcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
747 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
749 if (tty_term_has(tty
->term
, TTYC_ECH
))
750 tty_putcode1(tty
, TTYC_ECH
, ctx
->num
);
752 for (i
= 0; i
< ctx
->num
; i
++)
758 tty_cmd_insertline(struct tty
*tty
, const struct tty_ctx
*ctx
)
760 if (!tty_pane_full_width(tty
, ctx
) ||
761 !tty_term_has(tty
->term
, TTYC_CSR
) ||
762 !tty_term_has(tty
->term
, TTYC_IL1
)) {
763 tty_redraw_region(tty
, ctx
);
769 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
770 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
772 tty_emulate_repeat(tty
, TTYC_IL
, TTYC_IL1
, ctx
->num
);
776 tty_cmd_deleteline(struct tty
*tty
, const struct tty_ctx
*ctx
)
778 if (!tty_pane_full_width(tty
, ctx
) ||
779 !tty_term_has(tty
->term
, TTYC_CSR
) ||
780 !tty_term_has(tty
->term
, TTYC_DL1
)) {
781 tty_redraw_region(tty
, ctx
);
787 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
788 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
790 tty_emulate_repeat(tty
, TTYC_DL
, TTYC_DL1
, ctx
->num
);
794 tty_cmd_clearline(struct tty
*tty
, const struct tty_ctx
*ctx
)
796 struct window_pane
*wp
= ctx
->wp
;
797 struct screen
*s
= wp
->screen
;
801 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
);
803 if (tty_pane_full_width(tty
, ctx
) && tty_term_has(tty
->term
, TTYC_EL
))
804 tty_putcode(tty
, TTYC_EL
);
806 tty_repeat_space(tty
, screen_size_x(s
));
810 tty_cmd_clearendofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
812 struct window_pane
*wp
= ctx
->wp
;
813 struct screen
*s
= wp
->screen
;
817 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
819 if (tty_pane_full_width(tty
, ctx
) && tty_term_has(tty
->term
, TTYC_EL
))
820 tty_putcode(tty
, TTYC_EL
);
822 tty_repeat_space(tty
, screen_size_x(s
) - ctx
->ocx
);
826 tty_cmd_clearstartofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
830 if (ctx
->xoff
== 0 && tty_term_has(tty
->term
, TTYC_EL1
)) {
831 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
832 tty_putcode(tty
, TTYC_EL1
);
834 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
);
835 tty_repeat_space(tty
, ctx
->ocx
+ 1);
840 tty_cmd_reverseindex(struct tty
*tty
, const struct tty_ctx
*ctx
)
842 if (ctx
->ocy
!= ctx
->orupper
)
845 if (!tty_pane_full_width(tty
, ctx
) ||
846 !tty_term_has(tty
->term
, TTYC_CSR
) ||
847 !tty_term_has(tty
->term
, TTYC_RI
)) {
848 tty_redraw_region(tty
, ctx
);
854 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
855 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
857 tty_putcode(tty
, TTYC_RI
);
861 tty_cmd_linefeed(struct tty
*tty
, const struct tty_ctx
*ctx
)
863 struct window_pane
*wp
= ctx
->wp
;
865 if (ctx
->ocy
!= ctx
->orlower
)
868 if (!tty_pane_full_width(tty
, ctx
) ||
869 !tty_term_has(tty
->term
, TTYC_CSR
)) {
870 if (tty_large_region(tty
, ctx
))
871 wp
->flags
|= PANE_REDRAW
;
873 tty_redraw_region(tty
, ctx
);
878 * If this line wrapped naturally (ctx->num is nonzero), don't do
879 * anything - the cursor can just be moved to the last cell and wrap
882 if (ctx
->num
&& !(tty
->term
->flags
& TERM_EARLYWRAP
))
887 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
888 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
894 tty_cmd_clearendofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
896 struct window_pane
*wp
= ctx
->wp
;
897 struct screen
*s
= wp
->screen
;
902 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
903 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
905 if (tty_pane_full_width(tty
, ctx
) && tty_term_has(tty
->term
, TTYC_EL
)) {
906 tty_putcode(tty
, TTYC_EL
);
907 if (ctx
->ocy
!= screen_size_y(s
) - 1) {
908 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
+ 1);
909 for (i
= ctx
->ocy
+ 1; i
< screen_size_y(s
); i
++) {
910 tty_putcode(tty
, TTYC_EL
);
911 if (i
== screen_size_y(s
) - 1)
913 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
918 tty_repeat_space(tty
, screen_size_x(s
) - ctx
->ocx
);
919 for (j
= ctx
->ocy
+ 1; j
< screen_size_y(s
); j
++) {
920 tty_cursor_pane(tty
, ctx
, 0, j
);
921 tty_repeat_space(tty
, screen_size_x(s
));
927 tty_cmd_clearstartofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
929 struct window_pane
*wp
= ctx
->wp
;
930 struct screen
*s
= wp
->screen
;
935 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
936 tty_cursor_pane(tty
, ctx
, 0, 0);
938 if (tty_pane_full_width(tty
, ctx
) && tty_term_has(tty
->term
, TTYC_EL
)) {
939 for (i
= 0; i
< ctx
->ocy
; i
++) {
940 tty_putcode(tty
, TTYC_EL
);
941 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
945 for (j
= 0; j
< ctx
->ocy
; j
++) {
946 tty_cursor_pane(tty
, ctx
, 0, j
);
947 tty_repeat_space(tty
, screen_size_x(s
));
950 tty_repeat_space(tty
, ctx
->ocx
+ 1);
954 tty_cmd_clearscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
956 struct window_pane
*wp
= ctx
->wp
;
957 struct screen
*s
= wp
->screen
;
962 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
963 tty_cursor_pane(tty
, ctx
, 0, 0);
965 if (tty_pane_full_width(tty
, ctx
) && tty_term_has(tty
->term
, TTYC_EL
)) {
966 for (i
= 0; i
< screen_size_y(s
); i
++) {
967 tty_putcode(tty
, TTYC_EL
);
968 if (i
!= screen_size_y(s
) - 1) {
969 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
974 for (j
= 0; j
< screen_size_y(s
); j
++) {
975 tty_cursor_pane(tty
, ctx
, 0, j
);
976 tty_repeat_space(tty
, screen_size_x(s
));
982 tty_cmd_alignmenttest(struct tty
*tty
, const struct tty_ctx
*ctx
)
984 struct window_pane
*wp
= ctx
->wp
;
985 struct screen
*s
= wp
->screen
;
990 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
992 for (j
= 0; j
< screen_size_y(s
); j
++) {
993 tty_cursor_pane(tty
, ctx
, 0, j
);
994 for (i
= 0; i
< screen_size_x(s
); i
++)
1000 tty_cmd_cell(struct tty
*tty
, const struct tty_ctx
*ctx
)
1002 struct window_pane
*wp
= ctx
->wp
;
1003 struct screen
*s
= wp
->screen
;
1007 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1009 /* Is the cursor in the very last position? */
1010 width
= grid_cell_width(ctx
->cell
);
1011 if (ctx
->ocx
> wp
->sx
- width
) {
1012 if (ctx
->xoff
!= 0 || wp
->sx
!= tty
->sx
) {
1014 * The pane doesn't fill the entire line, the linefeed
1015 * will already have happened, so just move the cursor.
1017 if (ctx
->ocy
!= wp
->yoff
+ wp
->screen
->rlower
)
1018 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
+ 1);
1020 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
);
1021 } else if (tty
->cx
< tty
->sx
) {
1023 * The cursor isn't in the last position already, so
1024 * move as far left as possible and redraw the last
1025 * cell to move into the last position.
1027 cx
= screen_size_x(s
) - grid_cell_width(&ctx
->last_cell
);
1028 tty_cursor_pane(tty
, ctx
, cx
, ctx
->ocy
);
1029 tty_cell(tty
, &ctx
->last_cell
);
1032 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1034 tty_cell(tty
, ctx
->cell
);
1038 tty_cmd_utf8character(struct tty
*tty
, const struct tty_ctx
*ctx
)
1040 struct window_pane
*wp
= ctx
->wp
;
1043 * Cannot rely on not being a partial character, so just redraw the
1046 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, ctx
->xoff
, ctx
->yoff
);
1050 tty_cmd_setselection(struct tty
*tty
, const struct tty_ctx
*ctx
)
1055 if (!tty_term_has(tty
->term
, TTYC_MS
))
1058 off
= 4 * ((ctx
->num
+ 2) / 3) + 1; /* storage for base64 */
1061 b64_ntop(ctx
->ptr
, ctx
->num
, buf
, off
);
1062 tty_putcode_ptr2(tty
, TTYC_MS
, "", buf
);
1068 tty_cmd_rawstring(struct tty
*tty
, const struct tty_ctx
*ctx
)
1071 u_char
*str
= ctx
->ptr
;
1073 for (i
= 0; i
< ctx
->num
; i
++)
1074 tty_putc(tty
, str
[i
]);
1076 tty
->cx
= tty
->cy
= UINT_MAX
;
1077 tty
->rupper
= tty
->rlower
= UINT_MAX
;
1080 tty_cursor(tty
, 0, 0);
1084 tty_cell(struct tty
*tty
, const struct grid_cell
*gc
)
1086 struct utf8_data ud
;
1089 /* Skip last character if terminal is stupid. */
1090 if (tty
->term
->flags
& TERM_EARLYWRAP
&&
1091 tty
->cy
== tty
->sy
- 1 && tty
->cx
== tty
->sx
- 1)
1094 /* If this is a padding character, do nothing. */
1095 if (gc
->flags
& GRID_FLAG_PADDING
)
1098 /* Set the attributes. */
1099 tty_attributes(tty
, gc
);
1101 /* Get the cell and if ASCII write with putc to do ACS translation. */
1102 grid_cell_get(gc
, &ud
);
1104 if (*ud
.data
< 0x20 || *ud
.data
== 0x7f)
1106 tty_putc(tty
, *ud
.data
);
1110 /* If not UTF-8, write _. */
1111 if (!(tty
->flags
& TTY_UTF8
)) {
1112 for (i
= 0; i
< ud
.width
; i
++)
1117 /* Write the data. */
1118 tty_putn(tty
, ud
.data
, ud
.size
, ud
.width
);
1122 tty_reset(struct tty
*tty
)
1124 struct grid_cell
*gc
= &tty
->cell
;
1126 if (memcmp(gc
, &grid_default_cell
, sizeof *gc
) == 0)
1129 if ((gc
->attr
& GRID_ATTR_CHARSET
) && tty_use_acs(tty
))
1130 tty_putcode(tty
, TTYC_RMACS
);
1131 tty_putcode(tty
, TTYC_SGR0
);
1132 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
1135 /* Set region inside pane. */
1138 struct tty
*tty
, const struct tty_ctx
*ctx
, u_int rupper
, u_int rlower
)
1140 tty_region(tty
, ctx
->yoff
+ rupper
, ctx
->yoff
+ rlower
);
1143 /* Set region at absolute position. */
1145 tty_region(struct tty
*tty
, u_int rupper
, u_int rlower
)
1147 if (tty
->rlower
== rlower
&& tty
->rupper
== rupper
)
1149 if (!tty_term_has(tty
->term
, TTYC_CSR
))
1152 tty
->rupper
= rupper
;
1153 tty
->rlower
= rlower
;
1156 * Some terminals (such as PuTTY) do not correctly reset the cursor to
1157 * 0,0 if it is beyond the last column (they do not reset their wrap
1158 * flag so further output causes a line feed). As a workaround, do an
1159 * explicit move to 0 first.
1161 if (tty
->cx
>= tty
->sx
)
1162 tty_cursor(tty
, 0, tty
->cy
);
1164 tty_putcode2(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
1165 tty_cursor(tty
, 0, 0);
1168 /* Move cursor inside pane. */
1170 tty_cursor_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int cx
, u_int cy
)
1172 tty_cursor(tty
, ctx
->xoff
+ cx
, ctx
->yoff
+ cy
);
1175 /* Move cursor to absolute position. */
1177 tty_cursor(struct tty
*tty
, u_int cx
, u_int cy
)
1179 struct tty_term
*term
= tty
->term
;
1183 if (cx
> tty
->sx
- 1)
1190 if (cx
== thisx
&& cy
== thisy
)
1193 /* Very end of the line, just use absolute movement. */
1194 if (thisx
> tty
->sx
- 1)
1197 /* Move to home position (0, 0). */
1198 if (cx
== 0 && cy
== 0 && tty_term_has(term
, TTYC_HOME
)) {
1199 tty_putcode(tty
, TTYC_HOME
);
1203 /* Zero on the next line. */
1204 if (cx
== 0 && cy
== thisy
+ 1 && thisy
!= tty
->rlower
) {
1205 tty_putc(tty
, '\r');
1206 tty_putc(tty
, '\n');
1210 /* Moving column or row. */
1213 * Moving column only, row staying the same.
1218 tty_putc(tty
, '\r');
1222 /* One to the left. */
1223 if (cx
== thisx
- 1 && tty_term_has(term
, TTYC_CUB1
)) {
1224 tty_putcode(tty
, TTYC_CUB1
);
1228 /* One to the right. */
1229 if (cx
== thisx
+ 1 && tty_term_has(term
, TTYC_CUF1
)) {
1230 tty_putcode(tty
, TTYC_CUF1
);
1234 /* Calculate difference. */
1235 change
= thisx
- cx
; /* +ve left, -ve right */
1238 * Use HPA if change is larger than absolute, otherwise move
1239 * the cursor with CUB/CUF.
1241 if ((u_int
) abs(change
) > cx
&& tty_term_has(term
, TTYC_HPA
)) {
1242 tty_putcode1(tty
, TTYC_HPA
, cx
);
1244 } else if (change
> 0 && tty_term_has(term
, TTYC_CUB
)) {
1245 tty_putcode1(tty
, TTYC_CUB
, change
);
1247 } else if (change
< 0 && tty_term_has(term
, TTYC_CUF
)) {
1248 tty_putcode1(tty
, TTYC_CUF
, -change
);
1251 } else if (cx
== thisx
) {
1253 * Moving row only, column staying the same.
1257 if (thisy
!= tty
->rupper
&&
1258 cy
== thisy
- 1 && tty_term_has(term
, TTYC_CUU1
)) {
1259 tty_putcode(tty
, TTYC_CUU1
);
1264 if (thisy
!= tty
->rlower
&&
1265 cy
== thisy
+ 1 && tty_term_has(term
, TTYC_CUD1
)) {
1266 tty_putcode(tty
, TTYC_CUD1
);
1270 /* Calculate difference. */
1271 change
= thisy
- cy
; /* +ve up, -ve down */
1274 * Try to use VPA if change is larger than absolute or if this
1275 * change would cross the scroll region, otherwise use CUU/CUD.
1277 if ((u_int
) abs(change
) > cy
||
1278 (change
< 0 && cy
- change
> tty
->rlower
) ||
1279 (change
> 0 && cy
- change
< tty
->rupper
)) {
1280 if (tty_term_has(term
, TTYC_VPA
)) {
1281 tty_putcode1(tty
, TTYC_VPA
, cy
);
1284 } else if (change
> 0 && tty_term_has(term
, TTYC_CUU
)) {
1285 tty_putcode1(tty
, TTYC_CUU
, change
);
1287 } else if (change
< 0 && tty_term_has(term
, TTYC_CUD
)) {
1288 tty_putcode1(tty
, TTYC_CUD
, -change
);
1294 /* Absolute movement. */
1295 tty_putcode2(tty
, TTYC_CUP
, cy
, cx
);
1303 tty_attributes(struct tty
*tty
, const struct grid_cell
*gc
)
1305 struct grid_cell
*tc
= &tty
->cell
, gc2
;
1308 memcpy(&gc2
, gc
, sizeof gc2
);
1311 * If no setab, try to use the reverse attribute as a best-effort for a
1312 * non-default background. This is a bit of a hack but it doesn't do
1313 * any serious harm and makes a couple of applications happier.
1315 if (!tty_term_has(tty
->term
, TTYC_SETAB
)) {
1316 if (gc2
.attr
& GRID_ATTR_REVERSE
) {
1317 if (gc2
.fg
!= 7 && gc2
.fg
!= 8)
1318 gc2
.attr
&= ~GRID_ATTR_REVERSE
;
1320 if (gc2
.bg
!= 0 && gc2
.bg
!= 8)
1321 gc2
.attr
|= GRID_ATTR_REVERSE
;
1325 /* Fix up the colours if necessary. */
1326 tty_check_fg(tty
, &gc2
);
1327 tty_check_bg(tty
, &gc2
);
1329 /* If any bits are being cleared, reset everything. */
1330 if (tc
->attr
& ~gc2
.attr
)
1334 * Set the colours. This may call tty_reset() (so it comes next) and
1335 * may add to (NOT remove) the desired attributes by changing new_attr.
1337 tty_colours(tty
, &gc2
);
1339 /* Filter out attribute bits already set. */
1340 changed
= gc2
.attr
& ~tc
->attr
;
1341 tc
->attr
= gc2
.attr
;
1343 /* Set the attributes. */
1344 if (changed
& GRID_ATTR_BRIGHT
)
1345 tty_putcode(tty
, TTYC_BOLD
);
1346 if (changed
& GRID_ATTR_DIM
)
1347 tty_putcode(tty
, TTYC_DIM
);
1348 if (changed
& GRID_ATTR_ITALICS
)
1350 if (tty_term_has(tty
->term
, TTYC_SITM
))
1351 tty_putcode(tty
, TTYC_SITM
);
1353 tty_putcode(tty
, TTYC_SMSO
);
1355 if (changed
& GRID_ATTR_UNDERSCORE
)
1356 tty_putcode(tty
, TTYC_SMUL
);
1357 if (changed
& GRID_ATTR_BLINK
)
1358 tty_putcode(tty
, TTYC_BLINK
);
1359 if (changed
& GRID_ATTR_REVERSE
) {
1360 if (tty_term_has(tty
->term
, TTYC_REV
))
1361 tty_putcode(tty
, TTYC_REV
);
1362 else if (tty_term_has(tty
->term
, TTYC_SMSO
))
1363 tty_putcode(tty
, TTYC_SMSO
);
1365 if (changed
& GRID_ATTR_HIDDEN
)
1366 tty_putcode(tty
, TTYC_INVIS
);
1367 if ((changed
& GRID_ATTR_CHARSET
) && tty_use_acs(tty
))
1368 tty_putcode(tty
, TTYC_SMACS
);
1372 tty_colours(struct tty
*tty
, const struct grid_cell
*gc
)
1374 struct grid_cell
*tc
= &tty
->cell
;
1375 u_char fg
= gc
->fg
, bg
= gc
->bg
, flags
= gc
->flags
;
1376 int have_ax
, fg_default
, bg_default
;
1378 /* No changes? Nothing is necessary. */
1379 if (fg
== tc
->fg
&& bg
== tc
->bg
&&
1380 ((flags
^ tc
->flags
) & (GRID_FLAG_FG256
|GRID_FLAG_BG256
)) == 0)
1384 * Is either the default colour? This is handled specially because the
1385 * best solution might be to reset both colours to default, in which
1386 * case if only one is default need to fall onward to set the other
1389 fg_default
= (fg
== 8 && !(flags
& GRID_FLAG_FG256
));
1390 bg_default
= (bg
== 8 && !(flags
& GRID_FLAG_BG256
));
1391 if (fg_default
|| bg_default
) {
1393 * If don't have AX but do have op, send sgr0 (op can't
1394 * actually be used because it is sometimes the same as sgr0
1395 * and sometimes isn't). This resets both colours to default.
1397 * Otherwise, try to set the default colour only as needed.
1399 have_ax
= tty_term_has(tty
->term
, TTYC_AX
);
1400 if (!have_ax
&& tty_term_has(tty
->term
, TTYC_OP
))
1404 (tc
->fg
!= 8 || tc
->flags
& GRID_FLAG_FG256
)) {
1406 tty_puts(tty
, "\033[39m");
1407 else if (tc
->fg
!= 7 ||
1408 tc
->flags
& GRID_FLAG_FG256
)
1409 tty_putcode1(tty
, TTYC_SETAF
, 7);
1411 tc
->flags
&= ~GRID_FLAG_FG256
;
1414 (tc
->bg
!= 8 || tc
->flags
& GRID_FLAG_BG256
)) {
1416 tty_puts(tty
, "\033[49m");
1417 else if (tc
->bg
!= 0 ||
1418 tc
->flags
& GRID_FLAG_BG256
)
1419 tty_putcode1(tty
, TTYC_SETAB
, 0);
1421 tc
->flags
&= ~GRID_FLAG_BG256
;
1426 /* Set the foreground colour. */
1427 if (!fg_default
&& (fg
!= tc
->fg
||
1428 ((flags
& GRID_FLAG_FG256
) != (tc
->flags
& GRID_FLAG_FG256
))))
1429 tty_colours_fg(tty
, gc
);
1432 * Set the background colour. This must come after the foreground as
1433 * tty_colour_fg() can call tty_reset().
1435 if (!bg_default
&& (bg
!= tc
->bg
||
1436 ((flags
& GRID_FLAG_BG256
) != (tc
->flags
& GRID_FLAG_BG256
))))
1437 tty_colours_bg(tty
, gc
);
1441 tty_check_fg(struct tty
*tty
, struct grid_cell
*gc
)
1445 /* Is this a 256-colour colour? */
1446 if (gc
->flags
& GRID_FLAG_FG256
) {
1447 /* And not a 256 colour mode? */
1448 if (!(tty
->term
->flags
& TERM_256COLOURS
) &&
1449 !(tty
->term_flags
& TERM_256COLOURS
)) {
1450 gc
->fg
= colour_256to16(gc
->fg
);
1453 gc
->attr
|= GRID_ATTR_BRIGHT
;
1455 gc
->attr
&= ~GRID_ATTR_BRIGHT
;
1456 gc
->flags
&= ~GRID_FLAG_FG256
;
1461 /* Is this an aixterm colour? */
1462 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
1463 if (gc
->fg
>= 90 && gc
->fg
<= 97 && colours
< 16) {
1465 gc
->attr
|= GRID_ATTR_BRIGHT
;
1470 tty_check_bg(struct tty
*tty
, struct grid_cell
*gc
)
1474 /* Is this a 256-colour colour? */
1475 if (gc
->flags
& GRID_FLAG_BG256
) {
1477 * And not a 256 colour mode? Translate to 16-colour
1478 * palette. Bold background doesn't exist portably, so just
1479 * discard the bold bit if set.
1481 if (!(tty
->term
->flags
& TERM_256COLOURS
) &&
1482 !(tty
->term_flags
& TERM_256COLOURS
)) {
1483 gc
->bg
= colour_256to16(gc
->bg
);
1486 gc
->attr
&= ~GRID_ATTR_BRIGHT
;
1487 gc
->flags
&= ~GRID_FLAG_BG256
;
1492 /* Is this an aixterm colour? */
1493 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
1494 if (gc
->bg
>= 90 && gc
->bg
<= 97 && colours
< 16) {
1496 gc
->attr
|= GRID_ATTR_BRIGHT
;
1501 tty_colours_fg(struct tty
*tty
, const struct grid_cell
*gc
)
1503 struct grid_cell
*tc
= &tty
->cell
;
1507 /* Is this a 256-colour colour? */
1508 if (gc
->flags
& GRID_FLAG_FG256
) {
1509 /* Try as 256 colours. */
1510 if (tty_try_256(tty
, fg
, "38") == 0)
1512 /* Else already handled by tty_check_fg. */
1516 /* Is this an aixterm bright colour? */
1517 if (fg
>= 90 && fg
<= 97) {
1518 xsnprintf(s
, sizeof s
, "\033[%dm", fg
);
1523 /* Otherwise set the foreground colour. */
1524 tty_putcode1(tty
, TTYC_SETAF
, fg
);
1527 /* Save the new values in the terminal current cell. */
1529 tc
->flags
&= ~GRID_FLAG_FG256
;
1530 tc
->flags
|= gc
->flags
& GRID_FLAG_FG256
;
1534 tty_colours_bg(struct tty
*tty
, const struct grid_cell
*gc
)
1536 struct grid_cell
*tc
= &tty
->cell
;
1540 /* Is this a 256-colour colour? */
1541 if (gc
->flags
& GRID_FLAG_BG256
) {
1542 /* Try as 256 colours. */
1543 if (tty_try_256(tty
, bg
, "48") == 0)
1545 /* Else already handled by tty_check_bg. */
1549 /* Is this an aixterm bright colour? */
1550 if (bg
>= 90 && bg
<= 97) {
1551 /* 16 colour terminals or above only. */
1552 if (tty_term_number(tty
->term
, TTYC_COLORS
) >= 16) {
1553 xsnprintf(s
, sizeof s
, "\033[%dm", bg
+ 10);
1558 /* no such thing as a bold background */
1561 /* Otherwise set the background colour. */
1562 tty_putcode1(tty
, TTYC_SETAB
, bg
);
1565 /* Save the new values in the terminal current cell. */
1567 tc
->flags
&= ~GRID_FLAG_BG256
;
1568 tc
->flags
|= gc
->flags
& GRID_FLAG_BG256
;
1572 tty_try_256(struct tty
*tty
, u_char colour
, const char *type
)
1576 if (!(tty
->term
->flags
& TERM_256COLOURS
) &&
1577 !(tty
->term_flags
& TERM_256COLOURS
))
1580 xsnprintf(s
, sizeof s
, "\033[%s;5;%hhum", type
, colour
);
1586 tty_bell(struct tty
*tty
)
1588 tty_putcode(tty
, TTYC_BEL
);