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
);
73 tty
->ccolour
= xstrdup("");
80 tty_resize(struct tty
*tty
)
85 if (ioctl(tty
->fd
, TIOCGWINSZ
, &ws
) != -1) {
96 if (sx
== tty
->sx
&& sy
== tty
->sy
)
104 tty
->rupper
= UINT_MAX
;
105 tty
->rlower
= UINT_MAX
;
108 * If the terminal has been started, reset the actual scroll region and
109 * cursor position, as this may not have happened.
111 if (tty
->flags
& TTY_STARTED
) {
112 tty_cursor(tty
, 0, 0);
113 tty_region(tty
, 0, tty
->sy
- 1);
120 tty_open(struct tty
*tty
, const char *overrides
, char **cause
)
125 if (debug_level
> 3) {
126 xsnprintf(out
, sizeof out
, "tmux-out-%ld.log", (long) getpid());
127 fd
= open(out
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
128 if (fd
!= -1 && fcntl(fd
, F_SETFD
, FD_CLOEXEC
) == -1)
129 fatal("fcntl failed");
133 tty
->term
= tty_term_find(tty
->termname
, tty
->fd
, overrides
, cause
);
134 if (tty
->term
== NULL
) {
138 tty
->flags
|= TTY_OPENED
;
140 tty
->flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
|TTY_ESCAPE
);
142 tty
->event
= bufferevent_new(
143 tty
->fd
, tty_read_callback
, NULL
, tty_error_callback
, tty
);
154 tty_read_callback(unused
struct bufferevent
*bufev
, void *data
)
156 struct tty
*tty
= data
;
158 while (tty_keys_next(tty
))
165 unused
struct bufferevent
*bufev
, unused
short what
, unused
void *data
)
170 tty_start_tty(struct tty
*tty
)
174 if (tty
->fd
== -1 || tcgetattr(tty
->fd
, &tty
->tio
) != 0)
177 setblocking(tty
->fd
, 0);
179 bufferevent_enable(tty
->event
, EV_READ
|EV_WRITE
);
181 memcpy(&tio
, &tty
->tio
, sizeof tio
);
182 tio
.c_iflag
&= ~(IXON
|IXOFF
|ICRNL
|INLCR
|IGNCR
|IMAXBEL
|ISTRIP
);
183 tio
.c_iflag
|= IGNBRK
;
184 tio
.c_oflag
&= ~(OPOST
|ONLCR
|OCRNL
|ONLRET
);
185 tio
.c_lflag
&= ~(IEXTEN
|ICANON
|ECHO
|ECHOE
|ECHONL
|ECHOCTL
|
186 ECHOPRT
|ECHOKE
|ECHOCTL
|ISIG
);
189 if (tcsetattr(tty
->fd
, TCSANOW
, &tio
) == 0)
190 tcflush(tty
->fd
, TCIOFLUSH
);
192 tty_putcode(tty
, TTYC_SMCUP
);
194 tty_putcode(tty
, TTYC_SGR0
);
195 memcpy(&tty
->cell
, &grid_default_cell
, sizeof tty
->cell
);
197 tty_putcode(tty
, TTYC_RMKX
);
198 if (tty_use_acs(tty
))
199 tty_putcode(tty
, TTYC_ENACS
);
200 tty_putcode(tty
, TTYC_CLEAR
);
202 tty_putcode(tty
, TTYC_CNORM
);
203 if (tty_term_has(tty
->term
, TTYC_KMOUS
))
204 tty_puts(tty
, "\033[?1000l");
209 tty
->rlower
= UINT_MAX
;
210 tty
->rupper
= UINT_MAX
;
212 tty
->mode
= MODE_CURSOR
;
214 tty
->flags
|= TTY_STARTED
;
216 tty_force_cursor_colour(tty
, "");
220 tty_stop_tty(struct tty
*tty
)
224 if (!(tty
->flags
& TTY_STARTED
))
226 tty
->flags
&= ~TTY_STARTED
;
228 bufferevent_disable(tty
->event
, EV_READ
|EV_WRITE
);
231 * Be flexible about error handling and try not kill the server just
232 * because the fd is invalid. Things like ssh -t can easily leave us
235 if (ioctl(tty
->fd
, TIOCGWINSZ
, &ws
) == -1)
237 if (tcsetattr(tty
->fd
, TCSANOW
, &tty
->tio
) == -1)
240 setblocking(tty
->fd
, 1);
242 tty_raw(tty
, tty_term_string2(tty
->term
, TTYC_CSR
, 0, ws
.ws_row
- 1));
243 if (tty_use_acs(tty
))
244 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMACS
));
245 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_SGR0
));
246 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMKX
));
247 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CLEAR
));
248 if (tty_term_has(tty
->term
, TTYC_CS1
) && tty
->cstyle
!= 0) {
249 if (tty_term_has(tty
->term
, TTYC_CSR1
))
250 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CSR1
));
252 tty_raw(tty
, tty_term_string1(tty
->term
, TTYC_CS1
, 0));
254 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CR
));
256 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CNORM
));
257 if (tty_term_has(tty
->term
, TTYC_KMOUS
))
258 tty_raw(tty
, "\033[?1000l");
260 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMCUP
));
264 tty_close(struct tty
*tty
)
266 if (tty
->log_fd
!= -1) {
271 evtimer_del(&tty
->key_timer
);
274 if (tty
->flags
& TTY_OPENED
) {
275 bufferevent_free(tty
->event
);
277 tty_term_free(tty
->term
);
280 tty
->flags
&= ~TTY_OPENED
;
290 tty_free(struct tty
*tty
)
295 if (tty
->path
!= NULL
)
297 if (tty
->termname
!= NULL
)
298 xfree(tty
->termname
);
302 tty_raw(struct tty
*tty
, const char *s
)
304 write(tty
->fd
, s
, strlen(s
));
308 tty_putcode(struct tty
*tty
, enum tty_code_code code
)
310 tty_puts(tty
, tty_term_string(tty
->term
, code
));
314 tty_putcode1(struct tty
*tty
, enum tty_code_code code
, int a
)
318 tty_puts(tty
, tty_term_string1(tty
->term
, code
, a
));
322 tty_putcode2(struct tty
*tty
, enum tty_code_code code
, int a
, int b
)
326 tty_puts(tty
, tty_term_string2(tty
->term
, code
, a
, b
));
330 tty_putcode_ptr1(struct tty
*tty
, enum tty_code_code code
, const void *a
)
333 tty_puts(tty
, tty_term_ptr1(tty
->term
, code
, a
));
337 tty_putcode_ptr2(struct tty
*tty
, enum tty_code_code code
, const void *a
, const void *b
)
339 if (a
!= NULL
&& b
!= NULL
)
340 tty_puts(tty
, tty_term_ptr2(tty
->term
, code
, a
, b
));
344 tty_puts(struct tty
*tty
, const char *s
)
348 bufferevent_write(tty
->event
, s
, strlen(s
));
350 if (tty
->log_fd
!= -1)
351 write(tty
->log_fd
, s
, strlen(s
));
355 tty_putc(struct tty
*tty
, u_char ch
)
360 if (tty
->cell
.attr
& GRID_ATTR_CHARSET
) {
361 acs
= tty_acs_get(tty
, ch
);
363 bufferevent_write(tty
->event
, acs
, strlen(acs
));
365 bufferevent_write(tty
->event
, &ch
, 1);
367 bufferevent_write(tty
->event
, &ch
, 1);
369 if (ch
>= 0x20 && ch
!= 0x7f) {
371 if (tty
->term
->flags
& TERM_EARLYWRAP
)
376 if (tty
->cy
!= tty
->rlower
)
382 if (tty
->log_fd
!= -1)
383 write(tty
->log_fd
, &ch
, 1);
387 tty_pututf8(struct tty
*tty
, const struct grid_utf8
*gu
)
391 size
= grid_utf8_size(gu
);
392 bufferevent_write(tty
->event
, gu
->data
, size
);
393 if (tty
->log_fd
!= -1)
394 write(tty
->log_fd
, gu
->data
, size
);
395 tty
->cx
+= gu
->width
;
399 tty_set_title(struct tty
*tty
, const char *title
)
401 if (!tty_term_has(tty
->term
, TTYC_TSL
) ||
402 !tty_term_has(tty
->term
, TTYC_FSL
))
405 tty_putcode(tty
, TTYC_TSL
);
406 tty_puts(tty
, title
);
407 tty_putcode(tty
, TTYC_FSL
);
411 tty_force_cursor_colour(struct tty
*tty
, const char *ccolour
)
413 if (*ccolour
== '\0')
414 tty_putcode(tty
, TTYC_CR
);
416 tty_putcode_ptr1(tty
, TTYC_CC
, ccolour
);
418 tty
->ccolour
= xstrdup(ccolour
);
422 tty_update_mode(struct tty
*tty
, int mode
, struct screen
*s
)
426 if (strcmp(s
->ccolour
, tty
->ccolour
))
427 tty_force_cursor_colour(tty
, s
->ccolour
);
429 if (tty
->flags
& TTY_NOCURSOR
)
430 mode
&= ~MODE_CURSOR
;
432 changed
= mode
^ tty
->mode
;
433 if (changed
& MODE_CURSOR
) {
434 if (mode
& MODE_CURSOR
)
435 tty_putcode(tty
, TTYC_CNORM
);
437 tty_putcode(tty
, TTYC_CIVIS
);
439 if (tty
->cstyle
!= s
->cstyle
) {
440 if (tty_term_has(tty
->term
, TTYC_CS1
)) {
441 if (s
->cstyle
== 0 &&
442 tty_term_has(tty
->term
, TTYC_CSR1
))
443 tty_putcode(tty
, TTYC_CSR1
);
445 tty_putcode1(tty
, TTYC_CS1
, s
->cstyle
);
447 tty
->cstyle
= s
->cstyle
;
449 if (changed
& ALL_MOUSE_MODES
) {
450 if (mode
& ALL_MOUSE_MODES
) {
451 if (mode
& MODE_MOUSE_UTF8
)
452 tty_puts(tty
, "\033[?1005h");
453 if (mode
& MODE_MOUSE_ANY
)
454 tty_puts(tty
, "\033[?1003h");
455 else if (mode
& MODE_MOUSE_BUTTON
)
456 tty_puts(tty
, "\033[?1002h");
457 else if (mode
& MODE_MOUSE_STANDARD
)
458 tty_puts(tty
, "\033[?1000h");
460 if (tty
->mode
& MODE_MOUSE_ANY
)
461 tty_puts(tty
, "\033[?1003l");
462 else if (tty
->mode
& MODE_MOUSE_BUTTON
)
463 tty_puts(tty
, "\033[?1002l");
464 else if (tty
->mode
& MODE_MOUSE_STANDARD
)
465 tty_puts(tty
, "\033[?1000l");
466 if (tty
->mode
& MODE_MOUSE_UTF8
)
467 tty_puts(tty
, "\033[?1005l");
470 if (changed
& MODE_KKEYPAD
) {
471 if (mode
& MODE_KKEYPAD
)
472 tty_putcode(tty
, TTYC_SMKX
);
474 tty_putcode(tty
, TTYC_RMKX
);
481 struct tty
*tty
, enum tty_code_code code
, enum tty_code_code code1
, u_int n
)
483 if (tty_term_has(tty
->term
, code
))
484 tty_putcode1(tty
, code
, n
);
487 tty_putcode(tty
, code1
);
492 * Redraw scroll region using data from screen (already updated). Used when
493 * CSR not supported, or window is a pane that doesn't take up the full
494 * width of the terminal.
497 tty_redraw_region(struct tty
*tty
, const struct tty_ctx
*ctx
)
499 struct window_pane
*wp
= ctx
->wp
;
500 struct screen
*s
= wp
->screen
;
504 * If region is >= 50% of the screen, just schedule a window redraw. In
505 * most cases, this is likely to be followed by some more scrolling -
506 * without this, the entire pane ends up being redrawn many times which
507 * can be much more data.
509 if (ctx
->orlower
- ctx
->orupper
>= screen_size_y(s
) / 2) {
510 wp
->flags
|= PANE_REDRAW
;
514 if (ctx
->ocy
< ctx
->orupper
|| ctx
->ocy
> ctx
->orlower
) {
515 for (i
= ctx
->ocy
; i
< screen_size_y(s
); i
++)
516 tty_draw_line(tty
, s
, i
, ctx
->xoff
, ctx
->yoff
);
518 for (i
= ctx
->orupper
; i
<= ctx
->orlower
; i
++)
519 tty_draw_line(tty
, s
, i
, ctx
->xoff
, ctx
->yoff
);
524 tty_draw_line(struct tty
*tty
, struct screen
*s
, u_int py
, u_int ox
, u_int oy
)
526 const struct grid_cell
*gc
;
527 struct grid_line
*gl
;
528 struct grid_cell tmpgc
;
529 const struct grid_utf8
*gu
;
532 tty_update_mode(tty
, tty
->mode
& ~MODE_CURSOR
, s
);
534 sx
= screen_size_x(s
);
535 if (sx
> s
->grid
->linedata
[s
->grid
->hsize
+ py
].cellsize
)
536 sx
= s
->grid
->linedata
[s
->grid
->hsize
+ py
].cellsize
;
541 * Don't move the cursor to the start permission if it will wrap there
546 gl
= &s
->grid
->linedata
[s
->grid
->hsize
+ py
- 1];
547 if (oy
+ py
== 0 || gl
== NULL
|| !(gl
->flags
& GRID_LINE_WRAPPED
) ||
548 tty
->cx
< tty
->sx
|| ox
!= 0 ||
549 (oy
+ py
!= tty
->cy
+ 1 && tty
->cy
!= s
->rlower
+ oy
))
550 tty_cursor(tty
, ox
, oy
+ py
);
552 for (i
= 0; i
< sx
; i
++) {
553 gc
= grid_view_peek_cell(s
->grid
, i
, py
);
556 if (gc
->flags
& GRID_FLAG_UTF8
)
557 gu
= grid_view_peek_utf8(s
->grid
, i
, py
);
559 if (screen_check_selection(s
, i
, py
)) {
560 memcpy(&tmpgc
, &s
->sel
.cell
, sizeof tmpgc
);
561 tmpgc
.data
= gc
->data
;
562 tmpgc
.flags
= gc
->flags
&
563 ~(GRID_FLAG_FG256
|GRID_FLAG_BG256
);
564 tmpgc
.flags
|= s
->sel
.cell
.flags
&
565 (GRID_FLAG_FG256
|GRID_FLAG_BG256
);
566 tty_cell(tty
, &tmpgc
, gu
);
568 tty_cell(tty
, gc
, gu
);
572 tty_update_mode(tty
, tty
->mode
, s
);
577 tty_cursor(tty
, ox
+ sx
, oy
+ py
);
578 if (screen_size_x(s
) >= tty
->sx
&& tty_term_has(tty
->term
, TTYC_EL
))
579 tty_putcode(tty
, TTYC_EL
);
581 for (i
= sx
; i
< screen_size_x(s
); i
++)
584 tty_update_mode(tty
, tty
->mode
, s
);
589 void (*cmdfn
)(struct tty
*, const struct tty_ctx
*), struct tty_ctx
*ctx
)
591 struct window_pane
*wp
= ctx
->wp
;
597 /* wp can be NULL if updating the screen but not the terminal. */
601 if (wp
->window
->flags
& WINDOW_REDRAW
|| wp
->flags
& PANE_REDRAW
)
603 if (!window_pane_visible(wp
))
606 for (i
= 0; i
< ARRAY_LENGTH(&clients
); i
++) {
607 c
= ARRAY_ITEM(&clients
, i
);
608 if (c
== NULL
|| c
->session
== NULL
)
610 if (c
->flags
& CLIENT_SUSPENDED
)
614 if (s
->curw
->window
== wp
->window
) {
615 if (c
->tty
.term
== NULL
)
617 if (c
->tty
.flags
& (TTY_FREEZE
|TTY_BACKOFF
))
621 ctx
->xoff
= wp
->xoff
;
622 ctx
->yoff
= wp
->yoff
;
623 if (status_at_line(c
) == 0)
632 tty_cmd_insertcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
634 struct window_pane
*wp
= ctx
->wp
;
635 struct screen
*s
= wp
->screen
;
638 if (ctx
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
) {
639 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, ctx
->xoff
, ctx
->yoff
);
645 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
647 if (tty_term_has(tty
->term
, TTYC_ICH
) ||
648 tty_term_has(tty
->term
, TTYC_ICH1
))
649 tty_emulate_repeat(tty
, TTYC_ICH
, TTYC_ICH1
, ctx
->num
);
650 else if (tty_term_has(tty
->term
, TTYC_SMIR
) &&
651 tty_term_has(tty
->term
, TTYC_RMIR
)) {
652 tty_putcode(tty
, TTYC_SMIR
);
653 for (i
= 0; i
< ctx
->num
; i
++)
655 tty_putcode(tty
, TTYC_RMIR
);
657 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, ctx
->xoff
, ctx
->yoff
);
661 tty_cmd_deletecharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
663 struct window_pane
*wp
= ctx
->wp
;
664 struct screen
*s
= wp
->screen
;
666 if (ctx
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
667 (!tty_term_has(tty
->term
, TTYC_DCH
) &&
668 !tty_term_has(tty
->term
, TTYC_DCH1
))) {
669 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, ctx
->xoff
, ctx
->yoff
);
675 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
677 if (tty_term_has(tty
->term
, TTYC_DCH
) ||
678 tty_term_has(tty
->term
, TTYC_DCH1
))
679 tty_emulate_repeat(tty
, TTYC_DCH
, TTYC_DCH1
, ctx
->num
);
683 tty_cmd_insertline(struct tty
*tty
, const struct tty_ctx
*ctx
)
685 struct window_pane
*wp
= ctx
->wp
;
686 struct screen
*s
= wp
->screen
;
688 if (ctx
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
689 !tty_term_has(tty
->term
, TTYC_CSR
) ||
690 !tty_term_has(tty
->term
, TTYC_IL1
)) {
691 tty_redraw_region(tty
, ctx
);
697 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
698 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
700 tty_emulate_repeat(tty
, TTYC_IL
, TTYC_IL1
, ctx
->num
);
704 tty_cmd_deleteline(struct tty
*tty
, const struct tty_ctx
*ctx
)
706 struct window_pane
*wp
= ctx
->wp
;
707 struct screen
*s
= wp
->screen
;
709 if (ctx
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
710 !tty_term_has(tty
->term
, TTYC_CSR
) ||
711 !tty_term_has(tty
->term
, TTYC_DL1
)) {
712 tty_redraw_region(tty
, ctx
);
718 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
719 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
721 tty_emulate_repeat(tty
, TTYC_DL
, TTYC_DL1
, ctx
->num
);
725 tty_cmd_clearline(struct tty
*tty
, const struct tty_ctx
*ctx
)
727 struct window_pane
*wp
= ctx
->wp
;
728 struct screen
*s
= wp
->screen
;
733 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
);
735 if (ctx
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
736 tty_term_has(tty
->term
, TTYC_EL
)) {
737 tty_putcode(tty
, TTYC_EL
);
739 for (i
= 0; i
< screen_size_x(s
); i
++)
745 tty_cmd_clearendofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
747 struct window_pane
*wp
= ctx
->wp
;
748 struct screen
*s
= wp
->screen
;
753 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
755 if (ctx
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
756 tty_term_has(tty
->term
, TTYC_EL
))
757 tty_putcode(tty
, TTYC_EL
);
759 for (i
= ctx
->ocx
; i
< screen_size_x(s
); i
++)
765 tty_cmd_clearstartofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
771 if (ctx
->xoff
== 0 && tty_term_has(tty
->term
, TTYC_EL1
)) {
772 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
773 tty_putcode(tty
, TTYC_EL1
);
775 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
);
776 for (i
= 0; i
< ctx
->ocx
+ 1; i
++)
782 tty_cmd_reverseindex(struct tty
*tty
, const struct tty_ctx
*ctx
)
784 struct window_pane
*wp
= ctx
->wp
;
785 struct screen
*s
= wp
->screen
;
787 if (ctx
->ocy
!= ctx
->orupper
)
790 if (ctx
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
791 !tty_term_has(tty
->term
, TTYC_CSR
) ||
792 !tty_term_has(tty
->term
, TTYC_RI
)) {
793 tty_redraw_region(tty
, ctx
);
799 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
800 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
802 tty_putcode(tty
, TTYC_RI
);
806 tty_cmd_linefeed(struct tty
*tty
, const struct tty_ctx
*ctx
)
808 struct window_pane
*wp
= ctx
->wp
;
809 struct screen
*s
= wp
->screen
;
811 if (ctx
->ocy
!= ctx
->orlower
)
814 if (ctx
->xoff
!= 0 || screen_size_x(s
) < tty
->sx
||
815 !tty_term_has(tty
->term
, TTYC_CSR
)) {
816 tty_redraw_region(tty
, ctx
);
821 * If this line wrapped naturally (ctx->num is nonzero), don't do
822 * anything - the cursor can just be moved to the last cell and wrap
825 if (ctx
->num
&& !(tty
->term
->flags
& TERM_EARLYWRAP
))
830 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
831 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
837 tty_cmd_clearendofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
839 struct window_pane
*wp
= ctx
->wp
;
840 struct screen
*s
= wp
->screen
;
845 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
846 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
848 if (ctx
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
849 tty_term_has(tty
->term
, TTYC_EL
)) {
850 tty_putcode(tty
, TTYC_EL
);
851 if (ctx
->ocy
!= screen_size_y(s
) - 1) {
852 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
+ 1);
853 for (i
= ctx
->ocy
+ 1; i
< screen_size_y(s
); i
++) {
854 tty_putcode(tty
, TTYC_EL
);
855 if (i
== screen_size_y(s
) - 1)
857 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
862 for (i
= ctx
->ocx
; i
< screen_size_x(s
); i
++)
864 for (j
= ctx
->ocy
+ 1; j
< screen_size_y(s
); j
++) {
865 tty_cursor_pane(tty
, ctx
, 0, j
);
866 for (i
= 0; i
< screen_size_x(s
); i
++)
873 tty_cmd_clearstartofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
875 struct window_pane
*wp
= ctx
->wp
;
876 struct screen
*s
= wp
->screen
;
881 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
882 tty_cursor_pane(tty
, ctx
, 0, 0);
884 if (ctx
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
885 tty_term_has(tty
->term
, TTYC_EL
)) {
886 for (i
= 0; i
< ctx
->ocy
; i
++) {
887 tty_putcode(tty
, TTYC_EL
);
888 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
892 for (j
= 0; j
< ctx
->ocy
; j
++) {
893 tty_cursor_pane(tty
, ctx
, 0, j
);
894 for (i
= 0; i
< screen_size_x(s
); i
++)
898 for (i
= 0; i
<= ctx
->ocx
; i
++)
903 tty_cmd_clearscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
905 struct window_pane
*wp
= ctx
->wp
;
906 struct screen
*s
= wp
->screen
;
911 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
912 tty_cursor_pane(tty
, ctx
, 0, 0);
914 if (ctx
->xoff
== 0 && screen_size_x(s
) >= tty
->sx
&&
915 tty_term_has(tty
->term
, TTYC_EL
)) {
916 for (i
= 0; i
< screen_size_y(s
); i
++) {
917 tty_putcode(tty
, TTYC_EL
);
918 if (i
!= screen_size_y(s
) - 1) {
919 tty_emulate_repeat(tty
, TTYC_CUD
, TTYC_CUD1
, 1);
924 for (j
= 0; j
< screen_size_y(s
); j
++) {
925 tty_cursor_pane(tty
, ctx
, 0, j
);
926 for (i
= 0; i
< screen_size_x(s
); i
++)
933 tty_cmd_alignmenttest(struct tty
*tty
, const struct tty_ctx
*ctx
)
935 struct window_pane
*wp
= ctx
->wp
;
936 struct screen
*s
= wp
->screen
;
941 tty_region_pane(tty
, ctx
, 0, screen_size_y(s
) - 1);
943 for (j
= 0; j
< screen_size_y(s
); j
++) {
944 tty_cursor_pane(tty
, ctx
, 0, j
);
945 for (i
= 0; i
< screen_size_x(s
); i
++)
951 tty_cmd_cell(struct tty
*tty
, const struct tty_ctx
*ctx
)
953 struct window_pane
*wp
= ctx
->wp
;
954 struct screen
*s
= wp
->screen
;
957 const struct grid_cell
*gc
= ctx
->cell
;
958 const struct grid_utf8
*gu
= ctx
->utf8
;
960 if (gc
->flags
& GRID_FLAG_UTF8
)
965 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
967 /* Is the cursor in the very last position? */
968 if (ctx
->ocx
> wp
->sx
- width
) {
969 if (ctx
->xoff
!= 0 || wp
->sx
!= tty
->sx
) {
971 * The pane doesn't fill the entire line, the linefeed
972 * will already have happened, so just move the cursor.
974 tty_cursor_pane(tty
, ctx
, 0, ctx
->ocy
+ 1);
975 } else if (tty
->cx
< tty
->sx
) {
977 * The cursor isn't in the last position already, so
978 * move as far left as possible and redraw the last
979 * cell to move into the last position.
981 if (ctx
->last_cell
.flags
& GRID_FLAG_UTF8
)
982 cx
= screen_size_x(s
) - ctx
->last_utf8
.width
;
984 cx
= screen_size_x(s
) - 1;
985 tty_cursor_pane(tty
, ctx
, cx
, ctx
->ocy
);
986 tty_cell(tty
, &ctx
->last_cell
, &ctx
->last_utf8
);
989 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
991 tty_cell(tty
, ctx
->cell
, ctx
->utf8
);
995 tty_cmd_utf8character(struct tty
*tty
, const struct tty_ctx
*ctx
)
997 struct window_pane
*wp
= ctx
->wp
;
1000 * Cannot rely on not being a partial character, so just redraw the
1003 tty_draw_line(tty
, wp
->screen
, ctx
->ocy
, ctx
->xoff
, ctx
->yoff
);
1007 tty_cmd_setselection(struct tty
*tty
, const struct tty_ctx
*ctx
)
1012 if (!tty_term_has(tty
->term
, TTYC_MS
))
1015 off
= 4 * ((ctx
->num
+ 2) / 3) + 1; /* storage for base64 */
1018 b64_ntop(ctx
->ptr
, ctx
->num
, buf
, off
);
1019 tty_putcode_ptr2(tty
, TTYC_MS
, "", buf
);
1025 tty_cmd_rawstring(struct tty
*tty
, const struct tty_ctx
*ctx
)
1028 u_char
*str
= ctx
->ptr
;
1030 for (i
= 0; i
< ctx
->num
; i
++)
1031 tty_putc(tty
, str
[i
]);
1036 struct tty
*tty
, const struct grid_cell
*gc
, const struct grid_utf8
*gu
)
1040 /* Skip last character if terminal is stupid. */
1041 if (tty
->term
->flags
& TERM_EARLYWRAP
&&
1042 tty
->cy
== tty
->sy
- 1 && tty
->cx
== tty
->sx
- 1)
1045 /* If this is a padding character, do nothing. */
1046 if (gc
->flags
& GRID_FLAG_PADDING
)
1049 /* Set the attributes. */
1050 tty_attributes(tty
, gc
);
1052 /* If not UTF-8, write directly. */
1053 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
1054 if (gc
->data
< 0x20 || gc
->data
== 0x7f)
1056 tty_putc(tty
, gc
->data
);
1060 /* If the terminal doesn't support UTF-8, write underscores. */
1061 if (!(tty
->flags
& TTY_UTF8
)) {
1062 for (i
= 0; i
< gu
->width
; i
++)
1067 /* Otherwise, write UTF-8. */
1068 tty_pututf8(tty
, gu
);
1072 tty_reset(struct tty
*tty
)
1074 struct grid_cell
*gc
= &tty
->cell
;
1076 if (memcmp(gc
, &grid_default_cell
, sizeof *gc
) == 0)
1079 if ((gc
->attr
& GRID_ATTR_CHARSET
) && tty_use_acs(tty
))
1080 tty_putcode(tty
, TTYC_RMACS
);
1081 tty_putcode(tty
, TTYC_SGR0
);
1082 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
1085 /* Set region inside pane. */
1088 struct tty
*tty
, const struct tty_ctx
*ctx
, u_int rupper
, u_int rlower
)
1090 tty_region(tty
, ctx
->yoff
+ rupper
, ctx
->yoff
+ rlower
);
1093 /* Set region at absolute position. */
1095 tty_region(struct tty
*tty
, u_int rupper
, u_int rlower
)
1097 if (tty
->rlower
== rlower
&& tty
->rupper
== rupper
)
1099 if (!tty_term_has(tty
->term
, TTYC_CSR
))
1102 tty
->rupper
= rupper
;
1103 tty
->rlower
= rlower
;
1106 * Some terminals (such as PuTTY) do not correctly reset the cursor to
1107 * 0,0 if it is beyond the last column (they do not reset their wrap
1108 * flag so further output causes a line feed). As a workaround, do an
1109 * explicit move to 0 first.
1111 if (tty
->cx
>= tty
->sx
)
1112 tty_cursor(tty
, 0, tty
->cy
);
1114 tty_putcode2(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
1115 tty_cursor(tty
, 0, 0);
1118 /* Move cursor inside pane. */
1120 tty_cursor_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int cx
, u_int cy
)
1122 tty_cursor(tty
, ctx
->xoff
+ cx
, ctx
->yoff
+ cy
);
1125 /* Move cursor to absolute position. */
1127 tty_cursor(struct tty
*tty
, u_int cx
, u_int cy
)
1129 struct tty_term
*term
= tty
->term
;
1133 if (cx
> tty
->sx
- 1)
1140 if (cx
== thisx
&& cy
== thisy
)
1143 /* Very end of the line, just use absolute movement. */
1144 if (thisx
> tty
->sx
- 1)
1147 /* Move to home position (0, 0). */
1148 if (cx
== 0 && cy
== 0 && tty_term_has(term
, TTYC_HOME
)) {
1149 tty_putcode(tty
, TTYC_HOME
);
1153 /* Zero on the next line. */
1154 if (cx
== 0 && cy
== thisy
+ 1 && thisy
!= tty
->rlower
) {
1155 tty_putc(tty
, '\r');
1156 tty_putc(tty
, '\n');
1160 /* Moving column or row. */
1163 * Moving column only, row staying the same.
1168 tty_putc(tty
, '\r');
1172 /* One to the left. */
1173 if (cx
== thisx
- 1 && tty_term_has(term
, TTYC_CUB1
)) {
1174 tty_putcode(tty
, TTYC_CUB1
);
1178 /* One to the right. */
1179 if (cx
== thisx
+ 1 && tty_term_has(term
, TTYC_CUF1
)) {
1180 tty_putcode(tty
, TTYC_CUF1
);
1184 /* Calculate difference. */
1185 change
= thisx
- cx
; /* +ve left, -ve right */
1188 * Use HPA if change is larger than absolute, otherwise move
1189 * the cursor with CUB/CUF.
1191 if ((u_int
) abs(change
) > cx
&& tty_term_has(term
, TTYC_HPA
)) {
1192 tty_putcode1(tty
, TTYC_HPA
, cx
);
1194 } else if (change
> 0 && tty_term_has(term
, TTYC_CUB
)) {
1195 tty_putcode1(tty
, TTYC_CUB
, change
);
1197 } else if (change
< 0 && tty_term_has(term
, TTYC_CUF
)) {
1198 tty_putcode1(tty
, TTYC_CUF
, -change
);
1201 } else if (cx
== thisx
) {
1203 * Moving row only, column staying the same.
1207 if (thisy
!= tty
->rupper
&&
1208 cy
== thisy
- 1 && tty_term_has(term
, TTYC_CUU1
)) {
1209 tty_putcode(tty
, TTYC_CUU1
);
1214 if (thisy
!= tty
->rlower
&&
1215 cy
== thisy
+ 1 && tty_term_has(term
, TTYC_CUD1
)) {
1216 tty_putcode(tty
, TTYC_CUD1
);
1220 /* Calculate difference. */
1221 change
= thisy
- cy
; /* +ve up, -ve down */
1224 * Try to use VPA if change is larger than absolute or if this
1225 * change would cross the scroll region, otherwise use CUU/CUD.
1227 if ((u_int
) abs(change
) > cy
||
1228 (change
< 0 && cy
- change
> tty
->rlower
) ||
1229 (change
> 0 && cy
- change
< tty
->rupper
)) {
1230 if (tty_term_has(term
, TTYC_VPA
)) {
1231 tty_putcode1(tty
, TTYC_VPA
, cy
);
1234 } else if (change
> 0 && tty_term_has(term
, TTYC_CUU
)) {
1235 tty_putcode1(tty
, TTYC_CUU
, change
);
1237 } else if (change
< 0 && tty_term_has(term
, TTYC_CUD
)) {
1238 tty_putcode1(tty
, TTYC_CUD
, -change
);
1244 /* Absolute movement. */
1245 tty_putcode2(tty
, TTYC_CUP
, cy
, cx
);
1253 tty_attributes(struct tty
*tty
, const struct grid_cell
*gc
)
1255 struct grid_cell
*tc
= &tty
->cell
, gc2
;
1258 memcpy(&gc2
, gc
, sizeof gc2
);
1261 * If no setab, try to use the reverse attribute as a best-effort for a
1262 * non-default background. This is a bit of a hack but it doesn't do
1263 * any serious harm and makes a couple of applications happier.
1265 if (!tty_term_has(tty
->term
, TTYC_SETAB
)) {
1266 if (gc2
.attr
& GRID_ATTR_REVERSE
) {
1267 if (gc2
.fg
!= 7 && gc2
.fg
!= 8)
1268 gc2
.attr
&= ~GRID_ATTR_REVERSE
;
1270 if (gc2
.bg
!= 0 && gc2
.bg
!= 8)
1271 gc2
.attr
|= GRID_ATTR_REVERSE
;
1275 /* Fix up the colours if necessary. */
1276 tty_check_fg(tty
, &gc2
);
1277 tty_check_bg(tty
, &gc2
);
1279 /* If any bits are being cleared, reset everything. */
1280 if (tc
->attr
& ~gc2
.attr
)
1284 * Set the colours. This may call tty_reset() (so it comes next) and
1285 * may add to (NOT remove) the desired attributes by changing new_attr.
1287 tty_colours(tty
, &gc2
);
1289 /* Filter out attribute bits already set. */
1290 changed
= gc2
.attr
& ~tc
->attr
;
1291 tc
->attr
= gc2
.attr
;
1293 /* Set the attributes. */
1294 if (changed
& GRID_ATTR_BRIGHT
)
1295 tty_putcode(tty
, TTYC_BOLD
);
1296 if (changed
& GRID_ATTR_DIM
)
1297 tty_putcode(tty
, TTYC_DIM
);
1298 if (changed
& GRID_ATTR_ITALICS
)
1300 if (tty_term_has(tty
->term
, TTYC_SITM
))
1301 tty_putcode(tty
, TTYC_SITM
);
1303 tty_putcode(tty
, TTYC_SMSO
);
1305 if (changed
& GRID_ATTR_UNDERSCORE
)
1306 tty_putcode(tty
, TTYC_SMUL
);
1307 if (changed
& GRID_ATTR_BLINK
)
1308 tty_putcode(tty
, TTYC_BLINK
);
1309 if (changed
& GRID_ATTR_REVERSE
) {
1310 if (tty_term_has(tty
->term
, TTYC_REV
))
1311 tty_putcode(tty
, TTYC_REV
);
1312 else if (tty_term_has(tty
->term
, TTYC_SMSO
))
1313 tty_putcode(tty
, TTYC_SMSO
);
1315 if (changed
& GRID_ATTR_HIDDEN
)
1316 tty_putcode(tty
, TTYC_INVIS
);
1317 if ((changed
& GRID_ATTR_CHARSET
) && tty_use_acs(tty
))
1318 tty_putcode(tty
, TTYC_SMACS
);
1322 tty_colours(struct tty
*tty
, const struct grid_cell
*gc
)
1324 struct grid_cell
*tc
= &tty
->cell
;
1325 u_char fg
= gc
->fg
, bg
= gc
->bg
, flags
= gc
->flags
;
1326 int have_ax
, fg_default
, bg_default
;
1328 /* No changes? Nothing is necessary. */
1329 if (fg
== tc
->fg
&& bg
== tc
->bg
&&
1330 ((flags
^ tc
->flags
) & (GRID_FLAG_FG256
|GRID_FLAG_BG256
)) == 0)
1334 * Is either the default colour? This is handled specially because the
1335 * best solution might be to reset both colours to default, in which
1336 * case if only one is default need to fall onward to set the other
1339 fg_default
= (fg
== 8 && !(flags
& GRID_FLAG_FG256
));
1340 bg_default
= (bg
== 8 && !(flags
& GRID_FLAG_BG256
));
1341 if (fg_default
|| bg_default
) {
1343 * If don't have AX but do have op, send sgr0 (op can't
1344 * actually be used because it is sometimes the same as sgr0
1345 * and sometimes isn't). This resets both colours to default.
1347 * Otherwise, try to set the default colour only as needed.
1349 have_ax
= tty_term_has(tty
->term
, TTYC_AX
);
1350 if (!have_ax
&& tty_term_has(tty
->term
, TTYC_OP
))
1354 (tc
->fg
!= 8 || tc
->flags
& GRID_FLAG_FG256
)) {
1356 tty_puts(tty
, "\033[39m");
1357 else if (tc
->fg
!= 7 ||
1358 tc
->flags
& GRID_FLAG_FG256
)
1359 tty_putcode1(tty
, TTYC_SETAF
, 7);
1361 tc
->flags
&= ~GRID_FLAG_FG256
;
1364 (tc
->bg
!= 8 || tc
->flags
& GRID_FLAG_BG256
)) {
1366 tty_puts(tty
, "\033[49m");
1367 else if (tc
->bg
!= 0 ||
1368 tc
->flags
& GRID_FLAG_BG256
)
1369 tty_putcode1(tty
, TTYC_SETAB
, 0);
1371 tc
->flags
&= ~GRID_FLAG_BG256
;
1376 /* Set the foreground colour. */
1377 if (!fg_default
&& (fg
!= tc
->fg
||
1378 ((flags
& GRID_FLAG_FG256
) != (tc
->flags
& GRID_FLAG_FG256
))))
1379 tty_colours_fg(tty
, gc
);
1382 * Set the background colour. This must come after the foreground as
1383 * tty_colour_fg() can call tty_reset().
1385 if (!bg_default
&& (bg
!= tc
->bg
||
1386 ((flags
& GRID_FLAG_BG256
) != (tc
->flags
& GRID_FLAG_BG256
))))
1387 tty_colours_bg(tty
, gc
);
1391 tty_check_fg(struct tty
*tty
, struct grid_cell
*gc
)
1395 /* Is this a 256-colour colour? */
1396 if (gc
->flags
& GRID_FLAG_FG256
) {
1397 /* And not a 256 colour mode? */
1398 if (!(tty
->term
->flags
& TERM_88COLOURS
) &&
1399 !(tty
->term_flags
& TERM_88COLOURS
) &&
1400 !(tty
->term
->flags
& TERM_256COLOURS
) &&
1401 !(tty
->term_flags
& TERM_256COLOURS
)) {
1402 gc
->fg
= colour_256to16(gc
->fg
);
1405 gc
->attr
|= GRID_ATTR_BRIGHT
;
1407 gc
->attr
&= ~GRID_ATTR_BRIGHT
;
1408 gc
->flags
&= ~GRID_FLAG_FG256
;
1413 /* Is this an aixterm colour? */
1414 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
1415 if (gc
->fg
>= 90 && gc
->fg
<= 97 && colours
< 16) {
1417 gc
->attr
|= GRID_ATTR_BRIGHT
;
1422 tty_check_bg(struct tty
*tty
, struct grid_cell
*gc
)
1426 /* Is this a 256-colour colour? */
1427 if (gc
->flags
& GRID_FLAG_BG256
) {
1429 * And not a 256 colour mode? Translate to 16-colour
1430 * palette. Bold background doesn't exist portably, so just
1431 * discard the bold bit if set.
1433 if (!(tty
->term
->flags
& TERM_88COLOURS
) &&
1434 !(tty
->term_flags
& TERM_88COLOURS
) &&
1435 !(tty
->term
->flags
& TERM_256COLOURS
) &&
1436 !(tty
->term_flags
& TERM_256COLOURS
)) {
1437 gc
->bg
= colour_256to16(gc
->bg
);
1440 gc
->attr
&= ~GRID_ATTR_BRIGHT
;
1441 gc
->flags
&= ~GRID_FLAG_BG256
;
1446 /* Is this an aixterm colour? */
1447 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
1448 if (gc
->bg
>= 90 && gc
->bg
<= 97 && colours
< 16) {
1450 gc
->attr
|= GRID_ATTR_BRIGHT
;
1455 tty_colours_fg(struct tty
*tty
, const struct grid_cell
*gc
)
1457 struct grid_cell
*tc
= &tty
->cell
;
1461 /* Is this a 256-colour colour? */
1462 if (gc
->flags
& GRID_FLAG_FG256
) {
1463 /* Try as 256 colours or translating to 88. */
1464 if (tty_try_256(tty
, fg
, "38") == 0)
1466 if (tty_try_88(tty
, fg
, "38") == 0)
1468 /* Else already handled by tty_check_fg. */
1472 /* Is this an aixterm bright colour? */
1473 if (fg
>= 90 && fg
<= 97) {
1474 xsnprintf(s
, sizeof s
, "\033[%dm", fg
);
1479 /* Otherwise set the foreground colour. */
1480 tty_putcode1(tty
, TTYC_SETAF
, fg
);
1483 /* Save the new values in the terminal current cell. */
1485 tc
->flags
&= ~GRID_FLAG_FG256
;
1486 tc
->flags
|= gc
->flags
& GRID_FLAG_FG256
;
1490 tty_colours_bg(struct tty
*tty
, const struct grid_cell
*gc
)
1492 struct grid_cell
*tc
= &tty
->cell
;
1496 /* Is this a 256-colour colour? */
1497 if (gc
->flags
& GRID_FLAG_BG256
) {
1498 /* Try as 256 colours or translating to 88. */
1499 if (tty_try_256(tty
, bg
, "48") == 0)
1501 if (tty_try_88(tty
, bg
, "48") == 0)
1503 /* Else already handled by tty_check_bg. */
1507 /* Is this an aixterm bright colour? */
1508 if (bg
>= 90 && bg
<= 97) {
1509 /* 16 colour terminals or above only. */
1510 if (tty_term_number(tty
->term
, TTYC_COLORS
) >= 16) {
1511 xsnprintf(s
, sizeof s
, "\033[%dm", bg
+ 10);
1516 /* no such thing as a bold background */
1519 /* Otherwise set the background colour. */
1520 tty_putcode1(tty
, TTYC_SETAB
, bg
);
1523 /* Save the new values in the terminal current cell. */
1525 tc
->flags
&= ~GRID_FLAG_BG256
;
1526 tc
->flags
|= gc
->flags
& GRID_FLAG_BG256
;
1530 tty_try_256(struct tty
*tty
, u_char colour
, const char *type
)
1534 if (!(tty
->term
->flags
& TERM_256COLOURS
) &&
1535 !(tty
->term_flags
& TERM_256COLOURS
))
1538 xsnprintf(s
, sizeof s
, "\033[%s;5;%hhum", type
, colour
);
1544 tty_try_88(struct tty
*tty
, u_char colour
, const char *type
)
1548 if (!(tty
->term
->flags
& TERM_88COLOURS
) &&
1549 !(tty
->term_flags
& TERM_88COLOURS
))
1551 colour
= colour_256to88(colour
);
1553 xsnprintf(s
, sizeof s
, "\033[%s;5;%hhum", type
, colour
);
1559 tty_bell(struct tty
*tty
)
1561 tty_putcode(tty
, TTYC_BEL
);