4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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>
35 static int tty_log_fd
= -1;
37 static int tty_client_ready(struct client
*);
39 static void tty_set_italics(struct tty
*);
40 static int tty_try_colour(struct tty
*, int, const char *);
41 static void tty_force_cursor_colour(struct tty
*, int);
42 static void tty_cursor_pane(struct tty
*, const struct tty_ctx
*, u_int
,
44 static void tty_cursor_pane_unless_wrap(struct tty
*,
45 const struct tty_ctx
*, u_int
, u_int
);
46 static void tty_invalidate(struct tty
*);
47 static void tty_colours(struct tty
*, const struct grid_cell
*);
48 static void tty_check_fg(struct tty
*, struct colour_palette
*,
50 static void tty_check_bg(struct tty
*, struct colour_palette
*,
52 static void tty_check_us(struct tty
*, struct colour_palette
*,
54 static void tty_colours_fg(struct tty
*, const struct grid_cell
*);
55 static void tty_colours_bg(struct tty
*, const struct grid_cell
*);
56 static void tty_colours_us(struct tty
*, const struct grid_cell
*);
58 static void tty_region_pane(struct tty
*, const struct tty_ctx
*, u_int
,
60 static void tty_region(struct tty
*, u_int
, u_int
);
61 static void tty_margin_pane(struct tty
*, const struct tty_ctx
*);
62 static void tty_margin(struct tty
*, u_int
, u_int
);
63 static int tty_large_region(struct tty
*, const struct tty_ctx
*);
64 static int tty_fake_bce(const struct tty
*, const struct grid_cell
*,
66 static void tty_redraw_region(struct tty
*, const struct tty_ctx
*);
67 static void tty_emulate_repeat(struct tty
*, enum tty_code_code
,
68 enum tty_code_code
, u_int
);
69 static void tty_repeat_space(struct tty
*, u_int
);
70 static void tty_draw_pane(struct tty
*, const struct tty_ctx
*, u_int
);
71 static void tty_default_attributes(struct tty
*, const struct grid_cell
*,
72 struct colour_palette
*, u_int
);
73 static int tty_check_overlay(struct tty
*, u_int
, u_int
);
74 static void tty_check_overlay_range(struct tty
*, u_int
, u_int
, u_int
,
75 struct overlay_ranges
*);
77 #define tty_use_margin(tty) \
78 (tty->term->flags & TERM_DECSLRM)
79 #define tty_full_width(tty, ctx) \
80 ((ctx)->xoff == 0 && (ctx)->sx >= (tty)->sx)
82 #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
83 #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
84 #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
91 xsnprintf(name
, sizeof name
, "tmux-out-%ld.log", (long)getpid());
93 tty_log_fd
= open(name
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
94 if (tty_log_fd
!= -1 && fcntl(tty_log_fd
, F_SETFD
, FD_CLOEXEC
) == -1)
95 fatal("fcntl failed");
99 tty_init(struct tty
*tty
, struct client
*c
)
104 memset(tty
, 0, sizeof *tty
);
107 tty
->cstyle
= SCREEN_CURSOR_DEFAULT
;
110 if (tcgetattr(c
->fd
, &tty
->tio
) != 0)
116 tty_resize(struct tty
*tty
)
118 struct client
*c
= tty
->client
;
120 u_int sx
, sy
, xpixel
, ypixel
;
122 if (ioctl(c
->fd
, TIOCGWINSZ
, &ws
) != -1) {
128 xpixel
= ws
.ws_xpixel
/ sx
;
134 ypixel
= ws
.ws_ypixel
/ sy
;
141 log_debug("%s: %s now %ux%u (%ux%u)", __func__
, c
->name
, sx
, sy
,
143 tty_set_size(tty
, sx
, sy
, xpixel
, ypixel
);
148 tty_set_size(struct tty
*tty
, u_int sx
, u_int sy
, u_int xpixel
, u_int ypixel
)
152 tty
->xpixel
= xpixel
;
153 tty
->ypixel
= ypixel
;
157 tty_read_callback(__unused
int fd
, __unused
short events
, void *data
)
159 struct tty
*tty
= data
;
160 struct client
*c
= tty
->client
;
161 const char *name
= c
->name
;
162 size_t size
= EVBUFFER_LENGTH(tty
->in
);
165 nread
= evbuffer_read(tty
->in
, c
->fd
, -1);
166 if (nread
== 0 || nread
== -1) {
168 log_debug("%s: read closed", name
);
170 log_debug("%s: read error: %s", name
, strerror(errno
));
171 event_del(&tty
->event_in
);
172 server_client_lost(tty
->client
);
175 log_debug("%s: read %d bytes (already %zu)", name
, nread
, size
);
177 while (tty_keys_next(tty
))
182 tty_timer_callback(__unused
int fd
, __unused
short events
, void *data
)
184 struct tty
*tty
= data
;
185 struct client
*c
= tty
->client
;
186 struct timeval tv
= { .tv_usec
= TTY_BLOCK_INTERVAL
};
188 log_debug("%s: %zu discarded", c
->name
, tty
->discarded
);
190 c
->flags
|= CLIENT_ALLREDRAWFLAGS
;
191 c
->discarded
+= tty
->discarded
;
193 if (tty
->discarded
< TTY_BLOCK_STOP(tty
)) {
194 tty
->flags
&= ~TTY_BLOCK
;
199 evtimer_add(&tty
->timer
, &tv
);
203 tty_block_maybe(struct tty
*tty
)
205 struct client
*c
= tty
->client
;
206 size_t size
= EVBUFFER_LENGTH(tty
->out
);
207 struct timeval tv
= { .tv_usec
= TTY_BLOCK_INTERVAL
};
210 tty
->flags
&= ~TTY_NOBLOCK
;
211 else if (tty
->flags
& TTY_NOBLOCK
)
214 if (size
< TTY_BLOCK_START(tty
))
217 if (tty
->flags
& TTY_BLOCK
)
219 tty
->flags
|= TTY_BLOCK
;
221 log_debug("%s: can't keep up, %zu discarded", c
->name
, size
);
223 evbuffer_drain(tty
->out
, size
);
224 c
->discarded
+= size
;
227 evtimer_add(&tty
->timer
, &tv
);
232 tty_write_callback(__unused
int fd
, __unused
short events
, void *data
)
234 struct tty
*tty
= data
;
235 struct client
*c
= tty
->client
;
236 size_t size
= EVBUFFER_LENGTH(tty
->out
);
239 nwrite
= evbuffer_write(tty
->out
, c
->fd
);
242 log_debug("%s: wrote %d bytes (of %zu)", c
->name
, nwrite
, size
);
245 if ((size_t)nwrite
>= c
->redraw
)
249 log_debug("%s: waiting for redraw, %zu bytes left", c
->name
,
251 } else if (tty_block_maybe(tty
))
254 if (EVBUFFER_LENGTH(tty
->out
) != 0)
255 event_add(&tty
->event_out
, NULL
);
259 tty_open(struct tty
*tty
, char **cause
)
261 struct client
*c
= tty
->client
;
263 tty
->term
= tty_term_create(tty
, c
->term_name
, c
->term_caps
,
264 c
->term_ncaps
, &c
->term_features
, cause
);
265 if (tty
->term
== NULL
) {
269 tty
->flags
|= TTY_OPENED
;
271 tty
->flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
|TTY_BLOCK
|TTY_TIMER
);
273 event_set(&tty
->event_in
, c
->fd
, EV_PERSIST
|EV_READ
,
274 tty_read_callback
, tty
);
275 tty
->in
= evbuffer_new();
277 fatal("out of memory");
279 event_set(&tty
->event_out
, c
->fd
, EV_WRITE
, tty_write_callback
, tty
);
280 tty
->out
= evbuffer_new();
281 if (tty
->out
== NULL
)
282 fatal("out of memory");
284 evtimer_set(&tty
->timer
, tty_timer_callback
, tty
);
294 tty_start_timer_callback(__unused
int fd
, __unused
short events
, void *data
)
296 struct tty
*tty
= data
;
297 struct client
*c
= tty
->client
;
299 log_debug("%s: start timer fired", c
->name
);
300 if ((tty
->flags
& (TTY_HAVEDA
|TTY_HAVEXDA
)) == 0)
301 tty_update_features(tty
);
302 tty
->flags
|= (TTY_HAVEDA
|TTY_HAVEXDA
);
306 tty_start_tty(struct tty
*tty
)
308 struct client
*c
= tty
->client
;
310 struct timeval tv
= { .tv_sec
= 3 };
312 setblocking(c
->fd
, 0);
313 event_add(&tty
->event_in
, NULL
);
315 memcpy(&tio
, &tty
->tio
, sizeof tio
);
316 tio
.c_iflag
&= ~(IXON
|IXOFF
|ICRNL
|INLCR
|IGNCR
|IMAXBEL
|ISTRIP
);
317 tio
.c_iflag
|= IGNBRK
;
318 tio
.c_oflag
&= ~(OPOST
|ONLCR
|OCRNL
|ONLRET
);
319 tio
.c_lflag
&= ~(IEXTEN
|ICANON
|ECHO
|ECHOE
|ECHONL
|ECHOCTL
|ECHOPRT
|
323 if (tcsetattr(c
->fd
, TCSANOW
, &tio
) == 0)
324 tcflush(c
->fd
, TCOFLUSH
);
326 tty_putcode(tty
, TTYC_SMCUP
);
328 tty_putcode(tty
, TTYC_SMKX
);
329 tty_putcode(tty
, TTYC_CLEAR
);
331 if (tty_acs_needed(tty
)) {
332 log_debug("%s: using capabilities for ACS", c
->name
);
333 tty_putcode(tty
, TTYC_ENACS
);
335 log_debug("%s: using UTF-8 for ACS", c
->name
);
337 tty_putcode(tty
, TTYC_CNORM
);
338 if (tty_term_has(tty
->term
, TTYC_KMOUS
)) {
339 tty_puts(tty
, "\033[?1000l\033[?1002l\033[?1003l");
340 tty_puts(tty
, "\033[?1006l\033[?1005l");
343 evtimer_set(&tty
->start_timer
, tty_start_timer_callback
, tty
);
344 evtimer_add(&tty
->start_timer
, &tv
);
346 tty
->flags
|= TTY_STARTED
;
349 if (tty
->ccolour
!= -1)
350 tty_force_cursor_colour(tty
, -1);
352 tty
->mouse_drag_flag
= 0;
353 tty
->mouse_drag_update
= NULL
;
354 tty
->mouse_drag_release
= NULL
;
358 tty_send_requests(struct tty
*tty
)
360 if (~tty
->flags
& TTY_STARTED
)
363 if (tty
->term
->flags
& TERM_VT100LIKE
) {
364 if (~tty
->flags
& TTY_HAVEDA
)
365 tty_puts(tty
, "\033[>c");
366 if (~tty
->flags
& TTY_HAVEXDA
)
367 tty_puts(tty
, "\033[>q");
369 tty
->flags
|= (TTY_HAVEDA
|TTY_HAVEXDA
);
373 tty_stop_tty(struct tty
*tty
)
375 struct client
*c
= tty
->client
;
378 if (!(tty
->flags
& TTY_STARTED
))
380 tty
->flags
&= ~TTY_STARTED
;
382 evtimer_del(&tty
->start_timer
);
384 event_del(&tty
->timer
);
385 tty
->flags
&= ~TTY_BLOCK
;
387 event_del(&tty
->event_in
);
388 event_del(&tty
->event_out
);
391 * Be flexible about error handling and try not kill the server just
392 * because the fd is invalid. Things like ssh -t can easily leave us
395 if (ioctl(c
->fd
, TIOCGWINSZ
, &ws
) == -1)
397 if (tcsetattr(c
->fd
, TCSANOW
, &tty
->tio
) == -1)
400 tty_raw(tty
, tty_term_string2(tty
->term
, TTYC_CSR
, 0, ws
.ws_row
- 1));
401 if (tty_acs_needed(tty
))
402 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMACS
));
403 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_SGR0
));
404 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMKX
));
405 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CLEAR
));
406 if (tty
->cstyle
!= SCREEN_CURSOR_DEFAULT
) {
407 if (tty_term_has(tty
->term
, TTYC_SE
))
408 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_SE
));
409 else if (tty_term_has(tty
->term
, TTYC_SS
))
410 tty_raw(tty
, tty_term_string1(tty
->term
, TTYC_SS
, 0));
412 if (tty
->mode
& MODE_BRACKETPASTE
)
413 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSBP
));
414 if (tty
->ccolour
!= -1)
415 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CR
));
417 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CNORM
));
418 if (tty_term_has(tty
->term
, TTYC_KMOUS
)) {
419 tty_raw(tty
, "\033[?1000l\033[?1002l\033[?1003l");
420 tty_raw(tty
, "\033[?1006l\033[?1005l");
423 if (tty
->term
->flags
& TERM_VT100LIKE
)
424 tty_raw(tty
, "\033[?7727l");
425 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSFCS
));
426 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSEKS
));
428 if (tty_use_margin(tty
))
429 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSMG
));
430 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMCUP
));
432 setblocking(c
->fd
, 1);
436 tty_close(struct tty
*tty
)
438 if (event_initialized(&tty
->key_timer
))
439 evtimer_del(&tty
->key_timer
);
442 if (tty
->flags
& TTY_OPENED
) {
443 evbuffer_free(tty
->in
);
444 event_del(&tty
->event_in
);
445 evbuffer_free(tty
->out
);
446 event_del(&tty
->event_out
);
448 tty_term_free(tty
->term
);
451 tty
->flags
&= ~TTY_OPENED
;
456 tty_free(struct tty
*tty
)
462 tty_update_features(struct tty
*tty
)
464 struct client
*c
= tty
->client
;
466 if (tty_apply_features(tty
->term
, c
->term_features
))
467 tty_term_apply_overrides(tty
->term
);
469 if (tty_use_margin(tty
))
470 tty_putcode(tty
, TTYC_ENMG
);
471 if (options_get_number(global_options
, "extended-keys"))
472 tty_puts(tty
, tty_term_string(tty
->term
, TTYC_ENEKS
));
473 if (options_get_number(global_options
, "focus-events"))
474 tty_puts(tty
, tty_term_string(tty
->term
, TTYC_ENFCS
));
475 if (tty
->term
->flags
& TERM_VT100LIKE
)
476 tty_puts(tty
, "\033[?7727h");
480 tty_raw(struct tty
*tty
, const char *s
)
482 struct client
*c
= tty
->client
;
487 for (i
= 0; i
< 5; i
++) {
488 n
= write(c
->fd
, s
, slen
);
494 } else if (n
== -1 && errno
!= EAGAIN
)
501 tty_putcode(struct tty
*tty
, enum tty_code_code code
)
503 tty_puts(tty
, tty_term_string(tty
->term
, code
));
507 tty_putcode1(struct tty
*tty
, enum tty_code_code code
, int a
)
511 tty_puts(tty
, tty_term_string1(tty
->term
, code
, a
));
515 tty_putcode2(struct tty
*tty
, enum tty_code_code code
, int a
, int b
)
519 tty_puts(tty
, tty_term_string2(tty
->term
, code
, a
, b
));
523 tty_putcode3(struct tty
*tty
, enum tty_code_code code
, int a
, int b
, int c
)
525 if (a
< 0 || b
< 0 || c
< 0)
527 tty_puts(tty
, tty_term_string3(tty
->term
, code
, a
, b
, c
));
531 tty_putcode_ptr1(struct tty
*tty
, enum tty_code_code code
, const void *a
)
534 tty_puts(tty
, tty_term_ptr1(tty
->term
, code
, a
));
538 tty_putcode_ptr2(struct tty
*tty
, enum tty_code_code code
, const void *a
,
541 if (a
!= NULL
&& b
!= NULL
)
542 tty_puts(tty
, tty_term_ptr2(tty
->term
, code
, a
, b
));
546 tty_add(struct tty
*tty
, const char *buf
, size_t len
)
548 struct client
*c
= tty
->client
;
550 if (tty
->flags
& TTY_BLOCK
) {
551 tty
->discarded
+= len
;
555 evbuffer_add(tty
->out
, buf
, len
);
556 log_debug("%s: %.*s", c
->name
, (int)len
, buf
);
559 if (tty_log_fd
!= -1)
560 write(tty_log_fd
, buf
, len
);
561 if (tty
->flags
& TTY_STARTED
)
562 event_add(&tty
->event_out
, NULL
);
566 tty_puts(struct tty
*tty
, const char *s
)
569 tty_add(tty
, s
, strlen(s
));
573 tty_putc(struct tty
*tty
, u_char ch
)
577 if ((tty
->term
->flags
& TERM_NOAM
) &&
578 ch
>= 0x20 && ch
!= 0x7f &&
579 tty
->cy
== tty
->sy
- 1 &&
580 tty
->cx
+ 1 >= tty
->sx
)
583 if (tty
->cell
.attr
& GRID_ATTR_CHARSET
) {
584 acs
= tty_acs_get(tty
, ch
);
586 tty_add(tty
, acs
, strlen(acs
));
588 tty_add(tty
, &ch
, 1);
590 tty_add(tty
, &ch
, 1);
592 if (ch
>= 0x20 && ch
!= 0x7f) {
593 if (tty
->cx
>= tty
->sx
) {
595 if (tty
->cy
!= tty
->rlower
)
599 * On !am terminals, force the cursor position to where
600 * we think it should be after a line wrap - this means
601 * it works on sensible terminals as well.
603 if (tty
->term
->flags
& TERM_NOAM
)
604 tty_putcode2(tty
, TTYC_CUP
, tty
->cy
, tty
->cx
);
611 tty_putn(struct tty
*tty
, const void *buf
, size_t len
, u_int width
)
613 if ((tty
->term
->flags
& TERM_NOAM
) &&
614 tty
->cy
== tty
->sy
- 1 &&
615 tty
->cx
+ len
>= tty
->sx
)
616 len
= tty
->sx
- tty
->cx
- 1;
618 tty_add(tty
, buf
, len
);
619 if (tty
->cx
+ width
> tty
->sx
) {
620 tty
->cx
= (tty
->cx
+ width
) - tty
->sx
;
621 if (tty
->cx
<= tty
->sx
)
624 tty
->cx
= tty
->cy
= UINT_MAX
;
630 tty_set_italics(struct tty
*tty
)
634 if (tty_term_has(tty
->term
, TTYC_SITM
)) {
635 s
= options_get_string(global_options
, "default-terminal");
636 if (strcmp(s
, "screen") != 0 && strncmp(s
, "screen-", 7) != 0) {
637 tty_putcode(tty
, TTYC_SITM
);
641 tty_putcode(tty
, TTYC_SMSO
);
645 tty_set_title(struct tty
*tty
, const char *title
)
647 if (!tty_term_has(tty
->term
, TTYC_TSL
) ||
648 !tty_term_has(tty
->term
, TTYC_FSL
))
651 tty_putcode(tty
, TTYC_TSL
);
652 tty_puts(tty
, title
);
653 tty_putcode(tty
, TTYC_FSL
);
657 tty_force_cursor_colour(struct tty
*tty
, int c
)
663 c
= colour_force_rgb(c
);
664 if (c
== tty
->ccolour
)
667 tty_putcode(tty
, TTYC_CR
);
669 colour_split_rgb(c
, &r
, &g
, &b
);
670 xsnprintf(s
, sizeof s
, "rgb:%02hhx/%02hhx/%02hhx", r
, g
, b
);
671 tty_putcode_ptr1(tty
, TTYC_CS
, s
);
677 tty_update_cursor(struct tty
*tty
, int mode
, struct screen
*s
)
679 enum screen_cursor_style cstyle
;
680 int ccolour
, changed
, cmode
= mode
;
682 /* Set cursor colour if changed. */
684 ccolour
= s
->ccolour
;
685 if (s
->ccolour
== -1)
686 ccolour
= s
->default_ccolour
;
687 tty_force_cursor_colour(tty
, ccolour
);
690 /* If cursor is off, set as invisible. */
691 if (~cmode
& MODE_CURSOR
) {
692 if (tty
->mode
& MODE_CURSOR
)
693 tty_putcode(tty
, TTYC_CIVIS
);
697 /* Check if blinking or very visible flag changed or style changed. */
699 cstyle
= tty
->cstyle
;
702 if (cstyle
== SCREEN_CURSOR_DEFAULT
) {
703 if (~cmode
& MODE_CURSOR_BLINKING_SET
) {
704 if (s
->default_mode
& MODE_CURSOR_BLINKING
)
705 cmode
|= MODE_CURSOR_BLINKING
;
707 cmode
&= ~MODE_CURSOR_BLINKING
;
709 cstyle
= s
->default_cstyle
;
713 /* If nothing changed, do nothing. */
714 changed
= cmode
^ tty
->mode
;
715 if ((changed
& CURSOR_MODES
) == 0 && cstyle
== tty
->cstyle
)
719 * Set cursor style. If an explicit style has been set with DECSCUSR,
720 * set it if supported, otherwise send cvvis for blinking styles.
722 * If no style, has been set (SCREEN_CURSOR_DEFAULT), then send cvvis
723 * if either the blinking or very visible flags are set.
725 tty_putcode(tty
, TTYC_CNORM
);
727 case SCREEN_CURSOR_DEFAULT
:
728 if (tty
->cstyle
!= SCREEN_CURSOR_DEFAULT
) {
729 if (tty_term_has(tty
->term
, TTYC_SE
))
730 tty_putcode(tty
, TTYC_SE
);
732 tty_putcode1(tty
, TTYC_SS
, 0);
734 if (cmode
& (MODE_CURSOR_BLINKING
|MODE_CURSOR_VERY_VISIBLE
))
735 tty_putcode(tty
, TTYC_CVVIS
);
737 case SCREEN_CURSOR_BLOCK
:
738 if (tty_term_has(tty
->term
, TTYC_SS
)) {
739 if (cmode
& MODE_CURSOR_BLINKING
)
740 tty_putcode1(tty
, TTYC_SS
, 1);
742 tty_putcode1(tty
, TTYC_SS
, 2);
743 } else if (cmode
& MODE_CURSOR_BLINKING
)
744 tty_putcode(tty
, TTYC_CVVIS
);
746 case SCREEN_CURSOR_UNDERLINE
:
747 if (tty_term_has(tty
->term
, TTYC_SS
)) {
748 if (cmode
& MODE_CURSOR_BLINKING
)
749 tty_putcode1(tty
, TTYC_SS
, 3);
751 tty_putcode1(tty
, TTYC_SS
, 4);
752 } else if (cmode
& MODE_CURSOR_BLINKING
)
753 tty_putcode(tty
, TTYC_CVVIS
);
755 case SCREEN_CURSOR_BAR
:
756 if (tty_term_has(tty
->term
, TTYC_SS
)) {
757 if (cmode
& MODE_CURSOR_BLINKING
)
758 tty_putcode1(tty
, TTYC_SS
, 5);
760 tty_putcode1(tty
, TTYC_SS
, 6);
761 } else if (cmode
& MODE_CURSOR_BLINKING
)
762 tty_putcode(tty
, TTYC_CVVIS
);
765 tty
->cstyle
= cstyle
;
770 tty_update_mode(struct tty
*tty
, int mode
, struct screen
*s
)
772 struct tty_term
*term
= tty
->term
;
773 struct client
*c
= tty
->client
;
776 if (tty
->flags
& TTY_NOCURSOR
)
777 mode
&= ~MODE_CURSOR
;
779 if (tty_update_cursor(tty
, mode
, s
) & MODE_CURSOR_BLINKING
)
780 mode
|= MODE_CURSOR_BLINKING
;
782 mode
&= ~MODE_CURSOR_BLINKING
;
784 changed
= mode
^ tty
->mode
;
785 if (log_get_level() != 0 && changed
!= 0) {
786 log_debug("%s: current mode %s", c
->name
,
787 screen_mode_to_string(tty
->mode
));
788 log_debug("%s: setting mode %s", c
->name
,
789 screen_mode_to_string(mode
));
792 if ((changed
& ALL_MOUSE_MODES
) && tty_term_has(term
, TTYC_KMOUS
)) {
794 * If the mouse modes have changed, clear then all and apply
795 * again. There are differences in how terminals track the
798 tty_puts(tty
, "\033[?1006l\033[?1000l\033[?1002l\033[?1003l");
799 if (mode
& ALL_MOUSE_MODES
)
800 tty_puts(tty
, "\033[?1006h");
801 if (mode
& MODE_MOUSE_ALL
)
802 tty_puts(tty
, "\033[?1000h\033[?1002h\033[?1003h");
803 if (mode
& MODE_MOUSE_BUTTON
)
804 tty_puts(tty
, "\033[?1000h\033[?1002h");
805 else if (mode
& MODE_MOUSE_STANDARD
)
806 tty_puts(tty
, "\033[?1000h");
808 if (changed
& MODE_BRACKETPASTE
) {
809 if (mode
& MODE_BRACKETPASTE
)
810 tty_putcode(tty
, TTYC_ENBP
);
812 tty_putcode(tty
, TTYC_DSBP
);
818 tty_emulate_repeat(struct tty
*tty
, enum tty_code_code code
,
819 enum tty_code_code code1
, u_int n
)
821 if (tty_term_has(tty
->term
, code
))
822 tty_putcode1(tty
, code
, n
);
825 tty_putcode(tty
, code1
);
830 tty_repeat_space(struct tty
*tty
, u_int n
)
835 memset(s
, ' ', sizeof s
);
837 while (n
> sizeof s
) {
838 tty_putn(tty
, s
, sizeof s
, sizeof s
);
842 tty_putn(tty
, s
, n
, n
);
845 /* Is this window bigger than the terminal? */
847 tty_window_bigger(struct tty
*tty
)
849 struct client
*c
= tty
->client
;
850 struct window
*w
= c
->session
->curw
->window
;
852 return (tty
->sx
< w
->sx
|| tty
->sy
- status_line_size(c
) < w
->sy
);
855 /* What offset should this window be drawn at? */
857 tty_window_offset(struct tty
*tty
, u_int
*ox
, u_int
*oy
, u_int
*sx
, u_int
*sy
)
867 /* What offset should this window be drawn at? */
869 tty_window_offset1(struct tty
*tty
, u_int
*ox
, u_int
*oy
, u_int
*sx
, u_int
*sy
)
871 struct client
*c
= tty
->client
;
872 struct window
*w
= c
->session
->curw
->window
;
873 struct window_pane
*wp
= server_client_get_pane(c
);
876 lines
= status_line_size(c
);
878 if (tty
->sx
>= w
->sx
&& tty
->sy
- lines
>= w
->sy
) {
884 c
->pan_window
= NULL
;
889 *sy
= tty
->sy
- lines
;
891 if (c
->pan_window
== w
) {
894 else if (c
->pan_ox
+ *sx
> w
->sx
)
895 c
->pan_ox
= w
->sx
- *sx
;
899 else if (c
->pan_oy
+ *sy
> w
->sy
)
900 c
->pan_oy
= w
->sy
- *sy
;
905 if (~wp
->screen
->mode
& MODE_CURSOR
) {
909 cx
= wp
->xoff
+ wp
->screen
->cx
;
910 cy
= wp
->yoff
+ wp
->screen
->cy
;
914 else if (cx
> w
->sx
- *sx
)
921 else if (cy
> w
->sy
- *sy
)
927 c
->pan_window
= NULL
;
931 /* Update stored offsets for a window and redraw if necessary. */
933 tty_update_window_offset(struct window
*w
)
937 TAILQ_FOREACH(c
, &clients
, entry
) {
938 if (c
->session
!= NULL
&&
939 c
->session
->curw
!= NULL
&&
940 c
->session
->curw
->window
== w
)
941 tty_update_client_offset(c
);
945 /* Update stored offsets for a client and redraw if necessary. */
947 tty_update_client_offset(struct client
*c
)
949 u_int ox
, oy
, sx
, sy
;
951 if (~c
->flags
& CLIENT_TERMINAL
)
954 c
->tty
.oflag
= tty_window_offset1(&c
->tty
, &ox
, &oy
, &sx
, &sy
);
955 if (ox
== c
->tty
.oox
&&
961 log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
962 __func__
, c
->name
, c
->tty
.oox
, c
->tty
.ooy
, c
->tty
.osx
, c
->tty
.osy
,
970 c
->flags
|= (CLIENT_REDRAWWINDOW
|CLIENT_REDRAWSTATUS
);
974 * Is the region large enough to be worth redrawing once later rather than
975 * probably several times now? Currently yes if it is more than 50% of the
979 tty_large_region(__unused
struct tty
*tty
, const struct tty_ctx
*ctx
)
981 return (ctx
->orlower
- ctx
->orupper
>= ctx
->sy
/ 2);
985 * Return if BCE is needed but the terminal doesn't have it - it'll need to be
989 tty_fake_bce(const struct tty
*tty
, const struct grid_cell
*gc
, u_int bg
)
991 if (tty_term_flag(tty
->term
, TTYC_BCE
))
993 if (!COLOUR_DEFAULT(bg
) || !COLOUR_DEFAULT(gc
->bg
))
999 * Redraw scroll region using data from screen (already updated). Used when
1000 * CSR not supported, or window is a pane that doesn't take up the full
1001 * width of the terminal.
1004 tty_redraw_region(struct tty
*tty
, const struct tty_ctx
*ctx
)
1006 struct client
*c
= tty
->client
;
1010 * If region is large, schedule a redraw. In most cases this is likely
1011 * to be followed by some more scrolling.
1013 if (tty_large_region(tty
, ctx
)) {
1014 log_debug("%s: %s large redraw", __func__
, c
->name
);
1015 ctx
->redraw_cb(ctx
);
1019 for (i
= ctx
->orupper
; i
<= ctx
->orlower
; i
++)
1020 tty_draw_pane(tty
, ctx
, i
);
1023 /* Is this position visible in the pane? */
1025 tty_is_visible(__unused
struct tty
*tty
, const struct tty_ctx
*ctx
, u_int px
,
1026 u_int py
, u_int nx
, u_int ny
)
1028 u_int xoff
= ctx
->rxoff
+ px
, yoff
= ctx
->ryoff
+ py
;
1033 if (xoff
+ nx
<= ctx
->wox
|| xoff
>= ctx
->wox
+ ctx
->wsx
||
1034 yoff
+ ny
<= ctx
->woy
|| yoff
>= ctx
->woy
+ ctx
->wsy
)
1039 /* Clamp line position to visible part of pane. */
1041 tty_clamp_line(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int px
, u_int py
,
1042 u_int nx
, u_int
*i
, u_int
*x
, u_int
*rx
, u_int
*ry
)
1044 u_int xoff
= ctx
->rxoff
+ px
;
1046 if (!tty_is_visible(tty
, ctx
, px
, py
, nx
, 1))
1048 *ry
= ctx
->yoff
+ py
- ctx
->woy
;
1050 if (xoff
>= ctx
->wox
&& xoff
+ nx
<= ctx
->wox
+ ctx
->wsx
) {
1053 *x
= ctx
->xoff
+ px
- ctx
->wox
;
1055 } else if (xoff
< ctx
->wox
&& xoff
+ nx
> ctx
->wox
+ ctx
->wsx
) {
1056 /* Both left and right not visible. */
1060 } else if (xoff
< ctx
->wox
) {
1061 /* Left not visible. */
1062 *i
= ctx
->wox
- (ctx
->xoff
+ px
);
1066 /* Right not visible. */
1068 *x
= (ctx
->xoff
+ px
) - ctx
->wox
;
1069 *rx
= ctx
->wsx
- *x
;
1072 fatalx("%s: x too big, %u > %u", __func__
, *rx
, nx
);
1079 tty_clear_line(struct tty
*tty
, const struct grid_cell
*defaults
, u_int py
,
1080 u_int px
, u_int nx
, u_int bg
)
1082 struct client
*c
= tty
->client
;
1083 struct overlay_ranges r
;
1086 log_debug("%s: %s, %u at %u,%u", __func__
, c
->name
, nx
, px
, py
);
1088 /* Nothing to clear. */
1092 /* If genuine BCE is available, can try escape sequences. */
1093 if (c
->overlay_check
== NULL
&& !tty_fake_bce(tty
, defaults
, bg
)) {
1094 /* Off the end of the line, use EL if available. */
1095 if (px
+ nx
>= tty
->sx
&& tty_term_has(tty
->term
, TTYC_EL
)) {
1096 tty_cursor(tty
, px
, py
);
1097 tty_putcode(tty
, TTYC_EL
);
1101 /* At the start of the line. Use EL1. */
1102 if (px
== 0 && tty_term_has(tty
->term
, TTYC_EL1
)) {
1103 tty_cursor(tty
, px
+ nx
- 1, py
);
1104 tty_putcode(tty
, TTYC_EL1
);
1108 /* Section of line. Use ECH if possible. */
1109 if (tty_term_has(tty
->term
, TTYC_ECH
)) {
1110 tty_cursor(tty
, px
, py
);
1111 tty_putcode1(tty
, TTYC_ECH
, nx
);
1117 * Couldn't use an escape sequence, use spaces. Clear only the visible
1118 * bit if there is an overlay.
1120 tty_check_overlay_range(tty
, px
, py
, nx
, &r
);
1121 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++) {
1124 tty_cursor(tty
, r
.px
[i
], py
);
1125 tty_repeat_space(tty
, r
.nx
[i
]);
1129 /* Clear a line, adjusting to visible part of pane. */
1131 tty_clear_pane_line(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int py
,
1132 u_int px
, u_int nx
, u_int bg
)
1134 struct client
*c
= tty
->client
;
1137 log_debug("%s: %s, %u at %u,%u", __func__
, c
->name
, nx
, px
, py
);
1139 if (tty_clamp_line(tty
, ctx
, px
, py
, nx
, &i
, &x
, &rx
, &ry
))
1140 tty_clear_line(tty
, &ctx
->defaults
, ry
, x
, rx
, bg
);
1143 /* Clamp area position to visible part of pane. */
1145 tty_clamp_area(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int px
, u_int py
,
1146 u_int nx
, u_int ny
, u_int
*i
, u_int
*j
, u_int
*x
, u_int
*y
, u_int
*rx
,
1149 u_int xoff
= ctx
->rxoff
+ px
, yoff
= ctx
->ryoff
+ py
;
1151 if (!tty_is_visible(tty
, ctx
, px
, py
, nx
, ny
))
1154 if (xoff
>= ctx
->wox
&& xoff
+ nx
<= ctx
->wox
+ ctx
->wsx
) {
1157 *x
= ctx
->xoff
+ px
- ctx
->wox
;
1159 } else if (xoff
< ctx
->wox
&& xoff
+ nx
> ctx
->wox
+ ctx
->wsx
) {
1160 /* Both left and right not visible. */
1164 } else if (xoff
< ctx
->wox
) {
1165 /* Left not visible. */
1166 *i
= ctx
->wox
- (ctx
->xoff
+ px
);
1170 /* Right not visible. */
1172 *x
= (ctx
->xoff
+ px
) - ctx
->wox
;
1173 *rx
= ctx
->wsx
- *x
;
1176 fatalx("%s: x too big, %u > %u", __func__
, *rx
, nx
);
1178 if (yoff
>= ctx
->woy
&& yoff
+ ny
<= ctx
->woy
+ ctx
->wsy
) {
1181 *y
= ctx
->yoff
+ py
- ctx
->woy
;
1183 } else if (yoff
< ctx
->woy
&& yoff
+ ny
> ctx
->woy
+ ctx
->wsy
) {
1184 /* Both top and bottom not visible. */
1188 } else if (yoff
< ctx
->woy
) {
1189 /* Top not visible. */
1190 *j
= ctx
->woy
- (ctx
->yoff
+ py
);
1194 /* Bottom not visible. */
1196 *y
= (ctx
->yoff
+ py
) - ctx
->woy
;
1197 *ry
= ctx
->wsy
- *y
;
1200 fatalx("%s: y too big, %u > %u", __func__
, *ry
, ny
);
1205 /* Clear an area, adjusting to visible part of pane. */
1207 tty_clear_area(struct tty
*tty
, const struct grid_cell
*defaults
, u_int py
,
1208 u_int ny
, u_int px
, u_int nx
, u_int bg
)
1210 struct client
*c
= tty
->client
;
1214 log_debug("%s: %s, %u,%u at %u,%u", __func__
, c
->name
, nx
, ny
, px
, py
);
1216 /* Nothing to clear. */
1217 if (nx
== 0 || ny
== 0)
1220 /* If genuine BCE is available, can try escape sequences. */
1221 if (c
->overlay_check
== NULL
&& !tty_fake_bce(tty
, defaults
, bg
)) {
1222 /* Use ED if clearing off the bottom of the terminal. */
1224 px
+ nx
>= tty
->sx
&&
1225 py
+ ny
>= tty
->sy
&&
1226 tty_term_has(tty
->term
, TTYC_ED
)) {
1227 tty_cursor(tty
, 0, py
);
1228 tty_putcode(tty
, TTYC_ED
);
1233 * On VT420 compatible terminals we can use DECFRA if the
1234 * background colour isn't default (because it doesn't work
1237 if ((tty
->term
->flags
& TERM_DECFRA
) && !COLOUR_DEFAULT(bg
)) {
1238 xsnprintf(tmp
, sizeof tmp
, "\033[32;%u;%u;%u;%u$x",
1239 py
+ 1, px
+ 1, py
+ ny
, px
+ nx
);
1244 /* Full lines can be scrolled away to clear them. */
1246 px
+ nx
>= tty
->sx
&&
1248 tty_term_has(tty
->term
, TTYC_CSR
) &&
1249 tty_term_has(tty
->term
, TTYC_INDN
)) {
1250 tty_region(tty
, py
, py
+ ny
- 1);
1251 tty_margin_off(tty
);
1252 tty_putcode1(tty
, TTYC_INDN
, ny
);
1257 * If margins are supported, can just scroll the area off to
1262 tty_term_has(tty
->term
, TTYC_CSR
) &&
1263 tty_use_margin(tty
) &&
1264 tty_term_has(tty
->term
, TTYC_INDN
)) {
1265 tty_region(tty
, py
, py
+ ny
- 1);
1266 tty_margin(tty
, px
, px
+ nx
- 1);
1267 tty_putcode1(tty
, TTYC_INDN
, ny
);
1272 /* Couldn't use an escape sequence, loop over the lines. */
1273 for (yy
= py
; yy
< py
+ ny
; yy
++)
1274 tty_clear_line(tty
, defaults
, yy
, px
, nx
, bg
);
1277 /* Clear an area in a pane. */
1279 tty_clear_pane_area(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int py
,
1280 u_int ny
, u_int px
, u_int nx
, u_int bg
)
1282 u_int i
, j
, x
, y
, rx
, ry
;
1284 if (tty_clamp_area(tty
, ctx
, px
, py
, nx
, ny
, &i
, &j
, &x
, &y
, &rx
, &ry
))
1285 tty_clear_area(tty
, &ctx
->defaults
, y
, ry
, x
, rx
, bg
);
1289 tty_draw_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int py
)
1291 struct screen
*s
= ctx
->s
;
1292 u_int nx
= ctx
->sx
, i
, x
, rx
, ry
;
1294 log_debug("%s: %s %u %d", __func__
, tty
->client
->name
, py
, ctx
->bigger
);
1297 tty_draw_line(tty
, s
, 0, py
, nx
, ctx
->xoff
, ctx
->yoff
+ py
,
1298 &ctx
->defaults
, ctx
->palette
);
1301 if (tty_clamp_line(tty
, ctx
, 0, py
, nx
, &i
, &x
, &rx
, &ry
)) {
1302 tty_draw_line(tty
, s
, i
, py
, rx
, x
, ry
, &ctx
->defaults
,
1307 static const struct grid_cell
*
1308 tty_check_codeset(struct tty
*tty
, const struct grid_cell
*gc
)
1310 static struct grid_cell
new;
1313 /* Characters less than 0x7f are always fine, no matter what. */
1314 if (gc
->data
.size
== 1 && *gc
->data
.data
< 0x7f)
1317 /* UTF-8 terminal and a UTF-8 character - fine. */
1318 if (tty
->client
->flags
& CLIENT_UTF8
)
1320 memcpy(&new, gc
, sizeof new);
1322 /* See if this can be mapped to an ACS character. */
1323 c
= tty_acs_reverse_get(tty
, gc
->data
.data
, gc
->data
.size
);
1325 utf8_set(&new.data
, c
);
1326 new.attr
|= GRID_ATTR_CHARSET
;
1330 /* Replace by the right number of underscores. */
1331 new.data
.size
= gc
->data
.width
;
1332 if (new.data
.size
> UTF8_SIZE
)
1333 new.data
.size
= UTF8_SIZE
;
1334 memset(new.data
.data
, '_', new.data
.size
);
1339 * Check if a single character is obstructed by the overlay and return a
1343 tty_check_overlay(struct tty
*tty
, u_int px
, u_int py
)
1345 struct overlay_ranges r
;
1348 * A unit width range will always return nx[2] == 0 from a check, even
1349 * with multiple overlays, so it's sufficient to check just the first
1352 tty_check_overlay_range(tty
, px
, py
, 1, &r
);
1353 if (r
.nx
[0] + r
.nx
[1] == 0)
1358 /* Return parts of the input range which are visible. */
1360 tty_check_overlay_range(struct tty
*tty
, u_int px
, u_int py
, u_int nx
,
1361 struct overlay_ranges
*r
)
1363 struct client
*c
= tty
->client
;
1365 if (c
->overlay_check
== NULL
) {
1375 c
->overlay_check(c
, c
->overlay_data
, px
, py
, nx
, r
);
1379 tty_draw_line(struct tty
*tty
, struct screen
*s
, u_int px
, u_int py
, u_int nx
,
1380 u_int atx
, u_int aty
, const struct grid_cell
*defaults
,
1381 struct colour_palette
*palette
)
1383 struct grid
*gd
= s
->grid
;
1384 struct grid_cell gc
, last
;
1385 const struct grid_cell
*gcp
;
1386 struct grid_line
*gl
;
1387 struct client
*c
= tty
->client
;
1388 struct overlay_ranges r
;
1389 u_int i
, j
, ux
, sx
, width
, hidden
, eux
, nxx
;
1391 int flags
, cleared
= 0, wrapped
= 0;
1395 log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__
,
1396 px
, py
, nx
, atx
, aty
);
1397 log_debug("%s: defaults: fg=%d, bg=%d", __func__
, defaults
->fg
,
1401 * py is the line in the screen to draw.
1402 * px is the start x and nx is the width to draw.
1403 * atx,aty is the line on the terminal to draw it.
1406 flags
= (tty
->flags
& TTY_NOCURSOR
);
1407 tty
->flags
|= TTY_NOCURSOR
;
1408 tty_update_mode(tty
, tty
->mode
, s
);
1410 tty_region_off(tty
);
1411 tty_margin_off(tty
);
1414 * Clamp the width to cellsize - note this is not cellused, because
1415 * there may be empty background cells after it (from BCE).
1417 sx
= screen_size_x(s
);
1420 cellsize
= grid_get_line(gd
, gd
->hsize
+ py
)->cellsize
;
1432 gl
= grid_get_line(gd
, gd
->hsize
+ py
- 1);
1434 (~gl
->flags
& GRID_LINE_WRAPPED
) ||
1436 tty
->cx
< tty
->sx
||
1441 tty_term_has(tty
->term
, TTYC_EL1
) &&
1442 !tty_fake_bce(tty
, defaults
, 8) &&
1443 c
->overlay_check
== NULL
) {
1444 tty_default_attributes(tty
, defaults
, palette
, 8);
1445 tty_cursor(tty
, nx
- 1, aty
);
1446 tty_putcode(tty
, TTYC_EL1
);
1450 log_debug("%s: wrapped line %u", __func__
, aty
);
1454 memcpy(&last
, &grid_default_cell
, sizeof last
);
1458 for (i
= 0; i
< sx
; i
++) {
1459 grid_view_get_cell(gd
, px
+ i
, py
, &gc
);
1460 gcp
= tty_check_codeset(tty
, &gc
);
1462 (!tty_check_overlay(tty
, atx
+ ux
+ width
, aty
) ||
1463 (gcp
->attr
& GRID_ATTR_CHARSET
) ||
1464 gcp
->flags
!= last
.flags
||
1465 gcp
->attr
!= last
.attr
||
1466 gcp
->fg
!= last
.fg
||
1467 gcp
->bg
!= last
.bg
||
1468 gcp
->us
!= last
.us
||
1469 ux
+ width
+ gcp
->data
.width
> nx
||
1470 (sizeof buf
) - len
< gcp
->data
.size
)) {
1471 tty_attributes(tty
, &last
, defaults
, palette
);
1472 if (last
.flags
& GRID_FLAG_CLEARED
) {
1473 log_debug("%s: %zu cleared", __func__
, len
);
1474 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
,
1477 if (!wrapped
|| atx
!= 0 || ux
!= 0)
1478 tty_cursor(tty
, atx
+ ux
, aty
);
1479 tty_putn(tty
, buf
, len
, width
);
1488 if (gcp
->flags
& GRID_FLAG_SELECTED
)
1489 screen_select_cell(s
, &last
, gcp
);
1491 memcpy(&last
, gcp
, sizeof last
);
1493 tty_check_overlay_range(tty
, atx
+ ux
, aty
, gcp
->data
.width
,
1496 for (j
= 0; j
< OVERLAY_MAX_RANGES
; j
++)
1498 hidden
= gcp
->data
.width
- hidden
;
1499 if (hidden
!= 0 && hidden
== gcp
->data
.width
) {
1500 if (~gcp
->flags
& GRID_FLAG_PADDING
)
1501 ux
+= gcp
->data
.width
;
1502 } else if (hidden
!= 0 || ux
+ gcp
->data
.width
> nx
) {
1503 if (~gcp
->flags
& GRID_FLAG_PADDING
) {
1504 tty_attributes(tty
, &last
, defaults
, palette
);
1505 for (j
= 0; j
< OVERLAY_MAX_RANGES
; j
++) {
1508 /* Effective width drawn so far. */
1509 eux
= r
.px
[j
] - atx
;
1511 tty_cursor(tty
, r
.px
[j
], aty
);
1515 tty_repeat_space(tty
, r
.nx
[j
]);
1520 } else if (gcp
->attr
& GRID_ATTR_CHARSET
) {
1521 tty_attributes(tty
, &last
, defaults
, palette
);
1522 tty_cursor(tty
, atx
+ ux
, aty
);
1523 for (j
= 0; j
< gcp
->data
.size
; j
++)
1524 tty_putc(tty
, gcp
->data
.data
[j
]);
1525 ux
+= gcp
->data
.width
;
1526 } else if (~gcp
->flags
& GRID_FLAG_PADDING
) {
1527 memcpy(buf
+ len
, gcp
->data
.data
, gcp
->data
.size
);
1528 len
+= gcp
->data
.size
;
1529 width
+= gcp
->data
.width
;
1532 if (len
!= 0 && ((~last
.flags
& GRID_FLAG_CLEARED
) || last
.bg
!= 8)) {
1533 tty_attributes(tty
, &last
, defaults
, palette
);
1534 if (last
.flags
& GRID_FLAG_CLEARED
) {
1535 log_debug("%s: %zu cleared (end)", __func__
, len
);
1536 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
, width
,
1539 if (!wrapped
|| atx
!= 0 || ux
!= 0)
1540 tty_cursor(tty
, atx
+ ux
, aty
);
1541 tty_putn(tty
, buf
, len
, width
);
1546 if (!cleared
&& ux
< nx
) {
1547 log_debug("%s: %u to end of line (%zu cleared)", __func__
,
1549 tty_default_attributes(tty
, defaults
, palette
, 8);
1550 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
, nx
- ux
, 8);
1553 tty
->flags
= (tty
->flags
& ~TTY_NOCURSOR
) | flags
;
1554 tty_update_mode(tty
, tty
->mode
, s
);
1558 tty_sync_start(struct tty
*tty
)
1560 if (tty
->flags
& TTY_BLOCK
)
1562 if (tty
->flags
& TTY_SYNCING
)
1564 tty
->flags
|= TTY_SYNCING
;
1566 if (tty_term_has(tty
->term
, TTYC_SYNC
)) {
1567 log_debug("%s sync start", tty
->client
->name
);
1568 tty_putcode1(tty
, TTYC_SYNC
, 1);
1573 tty_sync_end(struct tty
*tty
)
1575 if (tty
->flags
& TTY_BLOCK
)
1577 if (~tty
->flags
& TTY_SYNCING
)
1579 tty
->flags
&= ~TTY_SYNCING
;
1581 if (tty_term_has(tty
->term
, TTYC_SYNC
)) {
1582 log_debug("%s sync end", tty
->client
->name
);
1583 tty_putcode1(tty
, TTYC_SYNC
, 2);
1588 tty_client_ready(struct client
*c
)
1590 if (c
->session
== NULL
|| c
->tty
.term
== NULL
)
1592 if (c
->flags
& (CLIENT_REDRAWWINDOW
|CLIENT_SUSPENDED
))
1594 if (c
->tty
.flags
& TTY_FREEZE
)
1600 tty_write(void (*cmdfn
)(struct tty
*, const struct tty_ctx
*),
1601 struct tty_ctx
*ctx
)
1606 if (ctx
->set_client_cb
== NULL
)
1608 TAILQ_FOREACH(c
, &clients
, entry
) {
1609 if (!tty_client_ready(c
))
1611 state
= ctx
->set_client_cb(ctx
, c
);
1616 cmdfn(&c
->tty
, ctx
);
1621 tty_cmd_insertcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1623 struct client
*c
= tty
->client
;
1626 !tty_full_width(tty
, ctx
) ||
1627 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1628 (!tty_term_has(tty
->term
, TTYC_ICH
) &&
1629 !tty_term_has(tty
->term
, TTYC_ICH1
)) ||
1630 c
->overlay_check
!= NULL
) {
1631 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
1635 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1637 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1639 tty_emulate_repeat(tty
, TTYC_ICH
, TTYC_ICH1
, ctx
->num
);
1643 tty_cmd_deletecharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1645 struct client
*c
= tty
->client
;
1648 !tty_full_width(tty
, ctx
) ||
1649 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1650 (!tty_term_has(tty
->term
, TTYC_DCH
) &&
1651 !tty_term_has(tty
->term
, TTYC_DCH1
)) ||
1652 c
->overlay_check
!= NULL
) {
1653 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
1657 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1659 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1661 tty_emulate_repeat(tty
, TTYC_DCH
, TTYC_DCH1
, ctx
->num
);
1665 tty_cmd_clearcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1667 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1669 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, ctx
->ocx
, ctx
->num
, ctx
->bg
);
1673 tty_cmd_insertline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1675 struct client
*c
= tty
->client
;
1678 !tty_full_width(tty
, ctx
) ||
1679 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1680 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1681 !tty_term_has(tty
->term
, TTYC_IL1
) ||
1684 c
->overlay_check
!= NULL
) {
1685 tty_redraw_region(tty
, ctx
);
1689 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1691 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1692 tty_margin_off(tty
);
1693 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1695 tty_emulate_repeat(tty
, TTYC_IL
, TTYC_IL1
, ctx
->num
);
1696 tty
->cx
= tty
->cy
= UINT_MAX
;
1700 tty_cmd_deleteline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1702 struct client
*c
= tty
->client
;
1705 !tty_full_width(tty
, ctx
) ||
1706 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1707 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1708 !tty_term_has(tty
->term
, TTYC_DL1
) ||
1711 c
->overlay_check
!= NULL
) {
1712 tty_redraw_region(tty
, ctx
);
1716 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1718 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1719 tty_margin_off(tty
);
1720 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1722 tty_emulate_repeat(tty
, TTYC_DL
, TTYC_DL1
, ctx
->num
);
1723 tty
->cx
= tty
->cy
= UINT_MAX
;
1727 tty_cmd_clearline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1729 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1731 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, 0, ctx
->sx
, ctx
->bg
);
1735 tty_cmd_clearendofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1737 u_int nx
= ctx
->sx
- ctx
->ocx
;
1739 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1741 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, ctx
->ocx
, nx
, ctx
->bg
);
1745 tty_cmd_clearstartofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1747 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1749 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, 0, ctx
->ocx
+ 1, ctx
->bg
);
1753 tty_cmd_reverseindex(struct tty
*tty
, const struct tty_ctx
*ctx
)
1755 struct client
*c
= tty
->client
;
1757 if (ctx
->ocy
!= ctx
->orupper
)
1761 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1762 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1763 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1764 (!tty_term_has(tty
->term
, TTYC_RI
) &&
1765 !tty_term_has(tty
->term
, TTYC_RIN
)) ||
1768 c
->overlay_check
!= NULL
) {
1769 tty_redraw_region(tty
, ctx
);
1773 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1775 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1776 tty_margin_pane(tty
, ctx
);
1777 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
1779 if (tty_term_has(tty
->term
, TTYC_RI
))
1780 tty_putcode(tty
, TTYC_RI
);
1782 tty_putcode1(tty
, TTYC_RIN
, 1);
1786 tty_cmd_linefeed(struct tty
*tty
, const struct tty_ctx
*ctx
)
1788 struct client
*c
= tty
->client
;
1790 if (ctx
->ocy
!= ctx
->orlower
)
1794 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1795 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1796 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1799 c
->overlay_check
!= NULL
) {
1800 tty_redraw_region(tty
, ctx
);
1804 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1806 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1807 tty_margin_pane(tty
, ctx
);
1810 * If we want to wrap a pane while using margins, the cursor needs to
1811 * be exactly on the right of the region. If the cursor is entirely off
1812 * the edge - move it back to the right. Some terminals are funny about
1813 * this and insert extra spaces, so only use the right if margins are
1816 if (ctx
->xoff
+ ctx
->ocx
> tty
->rright
) {
1817 if (!tty_use_margin(tty
))
1818 tty_cursor(tty
, 0, ctx
->yoff
+ ctx
->ocy
);
1820 tty_cursor(tty
, tty
->rright
, ctx
->yoff
+ ctx
->ocy
);
1822 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1824 tty_putc(tty
, '\n');
1828 tty_cmd_scrollup(struct tty
*tty
, const struct tty_ctx
*ctx
)
1830 struct client
*c
= tty
->client
;
1834 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1835 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1836 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1839 c
->overlay_check
!= NULL
) {
1840 tty_redraw_region(tty
, ctx
);
1844 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1846 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1847 tty_margin_pane(tty
, ctx
);
1849 if (ctx
->num
== 1 || !tty_term_has(tty
->term
, TTYC_INDN
)) {
1850 if (!tty_use_margin(tty
))
1851 tty_cursor(tty
, 0, tty
->rlower
);
1853 tty_cursor(tty
, tty
->rright
, tty
->rlower
);
1854 for (i
= 0; i
< ctx
->num
; i
++)
1855 tty_putc(tty
, '\n');
1857 if (tty
->cy
== UINT_MAX
)
1858 tty_cursor(tty
, 0, 0);
1860 tty_cursor(tty
, 0, tty
->cy
);
1861 tty_putcode1(tty
, TTYC_INDN
, ctx
->num
);
1866 tty_cmd_scrolldown(struct tty
*tty
, const struct tty_ctx
*ctx
)
1869 struct client
*c
= tty
->client
;
1872 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1873 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1874 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1875 (!tty_term_has(tty
->term
, TTYC_RI
) &&
1876 !tty_term_has(tty
->term
, TTYC_RIN
)) ||
1879 c
->overlay_check
!= NULL
) {
1880 tty_redraw_region(tty
, ctx
);
1884 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1886 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1887 tty_margin_pane(tty
, ctx
);
1888 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
1890 if (tty_term_has(tty
->term
, TTYC_RIN
))
1891 tty_putcode1(tty
, TTYC_RIN
, ctx
->num
);
1893 for (i
= 0; i
< ctx
->num
; i
++)
1894 tty_putcode(tty
, TTYC_RI
);
1899 tty_cmd_clearendofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
1901 u_int px
, py
, nx
, ny
;
1903 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1905 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
1906 tty_margin_off(tty
);
1911 ny
= ctx
->sy
- ctx
->ocy
- 1;
1913 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
1916 nx
= ctx
->sx
- ctx
->ocx
;
1919 tty_clear_pane_line(tty
, ctx
, py
, px
, nx
, ctx
->bg
);
1923 tty_cmd_clearstartofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
1925 u_int px
, py
, nx
, ny
;
1927 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1929 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
1930 tty_margin_off(tty
);
1937 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
1943 tty_clear_pane_line(tty
, ctx
, py
, px
, nx
, ctx
->bg
);
1947 tty_cmd_clearscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
1949 u_int px
, py
, nx
, ny
;
1951 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
);
1953 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
1954 tty_margin_off(tty
);
1961 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
1965 tty_cmd_alignmenttest(struct tty
*tty
, const struct tty_ctx
*ctx
)
1970 ctx
->redraw_cb(ctx
);
1974 tty_attributes(tty
, &grid_default_cell
, &ctx
->defaults
, ctx
->palette
);
1976 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
1977 tty_margin_off(tty
);
1979 for (j
= 0; j
< ctx
->sy
; j
++) {
1980 tty_cursor_pane(tty
, ctx
, 0, j
);
1981 for (i
= 0; i
< ctx
->sx
; i
++)
1987 tty_cmd_cell(struct tty
*tty
, const struct tty_ctx
*ctx
)
1989 const struct grid_cell
*gcp
= ctx
->cell
;
1990 struct screen
*s
= ctx
->s
;
1991 struct overlay_ranges r
;
1992 u_int px
, py
, i
, vis
= 0;
1994 px
= ctx
->xoff
+ ctx
->ocx
- ctx
->wox
;
1995 py
= ctx
->yoff
+ ctx
->ocy
- ctx
->woy
;
1996 if (!tty_is_visible(tty
, ctx
, ctx
->ocx
, ctx
->ocy
, 1, 1) ||
1997 (gcp
->data
.width
== 1 && !tty_check_overlay(tty
, px
, py
)))
2000 /* Handle partially obstructed wide characters. */
2001 if (gcp
->data
.width
> 1) {
2002 tty_check_overlay_range(tty
, px
, py
, gcp
->data
.width
, &r
);
2003 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++)
2005 if (vis
< gcp
->data
.width
) {
2006 tty_draw_line(tty
, s
, s
->cx
, s
->cy
, gcp
->data
.width
,
2007 px
, py
, &ctx
->defaults
, ctx
->palette
);
2012 if (ctx
->xoff
+ ctx
->ocx
- ctx
->wox
> tty
->sx
- 1 &&
2013 ctx
->ocy
== ctx
->orlower
&&
2014 tty_full_width(tty
, ctx
))
2015 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
2017 tty_margin_off(tty
);
2018 tty_cursor_pane_unless_wrap(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
2020 tty_cell(tty
, ctx
->cell
, &ctx
->defaults
, ctx
->palette
);
2024 tty_cmd_cells(struct tty
*tty
, const struct tty_ctx
*ctx
)
2026 struct overlay_ranges r
;
2027 u_int i
, px
, py
, cx
;
2028 char *cp
= ctx
->ptr
;
2030 if (!tty_is_visible(tty
, ctx
, ctx
->ocx
, ctx
->ocy
, ctx
->num
, 1))
2034 (ctx
->xoff
+ ctx
->ocx
< ctx
->wox
||
2035 ctx
->xoff
+ ctx
->ocx
+ ctx
->num
> ctx
->wox
+ ctx
->wsx
)) {
2036 if (!ctx
->wrapped
||
2037 !tty_full_width(tty
, ctx
) ||
2038 (tty
->term
->flags
& TERM_NOAM
) ||
2039 ctx
->xoff
+ ctx
->ocx
!= 0 ||
2040 ctx
->yoff
+ ctx
->ocy
!= tty
->cy
+ 1 ||
2041 tty
->cx
< tty
->sx
||
2042 tty
->cy
== tty
->rlower
)
2043 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
2045 ctx
->redraw_cb(ctx
);
2049 tty_margin_off(tty
);
2050 tty_cursor_pane_unless_wrap(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
2051 tty_attributes(tty
, ctx
->cell
, &ctx
->defaults
, ctx
->palette
);
2053 /* Get tty position from pane position for overlay check. */
2054 px
= ctx
->xoff
+ ctx
->ocx
- ctx
->wox
;
2055 py
= ctx
->yoff
+ ctx
->ocy
- ctx
->woy
;
2057 tty_check_overlay_range(tty
, px
, py
, ctx
->num
, &r
);
2058 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++) {
2061 /* Convert back to pane position for printing. */
2062 cx
= r
.px
[i
] - ctx
->xoff
+ ctx
->wox
;
2063 tty_cursor_pane_unless_wrap(tty
, ctx
, cx
, ctx
->ocy
);
2064 tty_putn(tty
, cp
+ r
.px
[i
] - px
, r
.nx
[i
], r
.nx
[i
]);
2069 tty_cmd_setselection(struct tty
*tty
, const struct tty_ctx
*ctx
)
2071 tty_set_selection(tty
, ctx
->ptr
, ctx
->num
);
2075 tty_set_selection(struct tty
*tty
, const char *buf
, size_t len
)
2080 if (~tty
->flags
& TTY_STARTED
)
2082 if (!tty_term_has(tty
->term
, TTYC_MS
))
2085 size
= 4 * ((len
+ 2) / 3) + 1; /* storage for base64 */
2086 encoded
= xmalloc(size
);
2088 b64_ntop(buf
, len
, encoded
, size
);
2089 tty
->flags
|= TTY_NOBLOCK
;
2090 tty_putcode_ptr2(tty
, TTYC_MS
, "", encoded
);
2096 tty_cmd_rawstring(struct tty
*tty
, const struct tty_ctx
*ctx
)
2098 tty
->flags
|= TTY_NOBLOCK
;
2099 tty_add(tty
, ctx
->ptr
, ctx
->num
);
2100 tty_invalidate(tty
);
2104 tty_cmd_syncstart(struct tty
*tty
, const struct tty_ctx
*ctx
)
2106 if (ctx
->num
== 0x11) {
2108 * This is an overlay and a command that moves the cursor so
2109 * start synchronized updates.
2111 tty_sync_start(tty
);
2112 } else if (~ctx
->num
& 0x10) {
2114 * This is a pane. If there is an overlay, always start;
2115 * otherwise, only if requested.
2117 if (ctx
->num
|| tty
->client
->overlay_draw
!= NULL
)
2118 tty_sync_start(tty
);
2123 tty_cell(struct tty
*tty
, const struct grid_cell
*gc
,
2124 const struct grid_cell
*defaults
, struct colour_palette
*palette
)
2126 const struct grid_cell
*gcp
;
2128 /* Skip last character if terminal is stupid. */
2129 if ((tty
->term
->flags
& TERM_NOAM
) &&
2130 tty
->cy
== tty
->sy
- 1 &&
2131 tty
->cx
== tty
->sx
- 1)
2134 /* If this is a padding character, do nothing. */
2135 if (gc
->flags
& GRID_FLAG_PADDING
)
2138 /* Check the output codeset and apply attributes. */
2139 gcp
= tty_check_codeset(tty
, gc
);
2140 tty_attributes(tty
, gcp
, defaults
, palette
);
2142 /* If it is a single character, write with putc to handle ACS. */
2143 if (gcp
->data
.size
== 1) {
2144 tty_attributes(tty
, gcp
, defaults
, palette
);
2145 if (*gcp
->data
.data
< 0x20 || *gcp
->data
.data
== 0x7f)
2147 tty_putc(tty
, *gcp
->data
.data
);
2151 /* Write the data. */
2152 tty_putn(tty
, gcp
->data
.data
, gcp
->data
.size
, gcp
->data
.width
);
2156 tty_reset(struct tty
*tty
)
2158 struct grid_cell
*gc
= &tty
->cell
;
2160 if (!grid_cells_equal(gc
, &grid_default_cell
)) {
2161 if ((gc
->attr
& GRID_ATTR_CHARSET
) && tty_acs_needed(tty
))
2162 tty_putcode(tty
, TTYC_RMACS
);
2163 tty_putcode(tty
, TTYC_SGR0
);
2164 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2166 memcpy(&tty
->last_cell
, &grid_default_cell
, sizeof tty
->last_cell
);
2170 tty_invalidate(struct tty
*tty
)
2172 memcpy(&tty
->cell
, &grid_default_cell
, sizeof tty
->cell
);
2173 memcpy(&tty
->last_cell
, &grid_default_cell
, sizeof tty
->last_cell
);
2175 tty
->cx
= tty
->cy
= UINT_MAX
;
2176 tty
->rupper
= tty
->rleft
= UINT_MAX
;
2177 tty
->rlower
= tty
->rright
= UINT_MAX
;
2179 if (tty
->flags
& TTY_STARTED
) {
2180 if (tty_use_margin(tty
))
2181 tty_putcode(tty
, TTYC_ENMG
);
2182 tty_putcode(tty
, TTYC_SGR0
);
2184 tty
->mode
= ALL_MODES
;
2185 tty_update_mode(tty
, MODE_CURSOR
, NULL
);
2187 tty_cursor(tty
, 0, 0);
2188 tty_region_off(tty
);
2189 tty_margin_off(tty
);
2191 tty
->mode
= MODE_CURSOR
;
2194 /* Turn off margin. */
2196 tty_region_off(struct tty
*tty
)
2198 tty_region(tty
, 0, tty
->sy
- 1);
2201 /* Set region inside pane. */
2203 tty_region_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int rupper
,
2206 tty_region(tty
, ctx
->yoff
+ rupper
- ctx
->woy
,
2207 ctx
->yoff
+ rlower
- ctx
->woy
);
2210 /* Set region at absolute position. */
2212 tty_region(struct tty
*tty
, u_int rupper
, u_int rlower
)
2214 if (tty
->rlower
== rlower
&& tty
->rupper
== rupper
)
2216 if (!tty_term_has(tty
->term
, TTYC_CSR
))
2219 tty
->rupper
= rupper
;
2220 tty
->rlower
= rlower
;
2223 * Some terminals (such as PuTTY) do not correctly reset the cursor to
2224 * 0,0 if it is beyond the last column (they do not reset their wrap
2225 * flag so further output causes a line feed). As a workaround, do an
2226 * explicit move to 0 first.
2228 if (tty
->cx
>= tty
->sx
) {
2229 if (tty
->cy
== UINT_MAX
)
2230 tty_cursor(tty
, 0, 0);
2232 tty_cursor(tty
, 0, tty
->cy
);
2235 tty_putcode2(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
2236 tty
->cx
= tty
->cy
= UINT_MAX
;
2239 /* Turn off margin. */
2241 tty_margin_off(struct tty
*tty
)
2243 tty_margin(tty
, 0, tty
->sx
- 1);
2246 /* Set margin inside pane. */
2248 tty_margin_pane(struct tty
*tty
, const struct tty_ctx
*ctx
)
2250 tty_margin(tty
, ctx
->xoff
- ctx
->wox
,
2251 ctx
->xoff
+ ctx
->sx
- 1 - ctx
->wox
);
2254 /* Set margin at absolute position. */
2256 tty_margin(struct tty
*tty
, u_int rleft
, u_int rright
)
2258 if (!tty_use_margin(tty
))
2260 if (tty
->rleft
== rleft
&& tty
->rright
== rright
)
2263 tty_putcode2(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
2266 tty
->rright
= rright
;
2268 if (rleft
== 0 && rright
== tty
->sx
- 1)
2269 tty_putcode(tty
, TTYC_CLMG
);
2271 tty_putcode2(tty
, TTYC_CMG
, rleft
, rright
);
2272 tty
->cx
= tty
->cy
= UINT_MAX
;
2276 * Move the cursor, unless it would wrap itself when the next character is
2280 tty_cursor_pane_unless_wrap(struct tty
*tty
, const struct tty_ctx
*ctx
,
2283 if (!ctx
->wrapped
||
2284 !tty_full_width(tty
, ctx
) ||
2285 (tty
->term
->flags
& TERM_NOAM
) ||
2286 ctx
->xoff
+ cx
!= 0 ||
2287 ctx
->yoff
+ cy
!= tty
->cy
+ 1 ||
2288 tty
->cx
< tty
->sx
||
2289 tty
->cy
== tty
->rlower
)
2290 tty_cursor_pane(tty
, ctx
, cx
, cy
);
2292 log_debug("%s: will wrap at %u,%u", __func__
, tty
->cx
, tty
->cy
);
2295 /* Move cursor inside pane. */
2297 tty_cursor_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int cx
, u_int cy
)
2299 tty_cursor(tty
, ctx
->xoff
+ cx
- ctx
->wox
, ctx
->yoff
+ cy
- ctx
->woy
);
2302 /* Move cursor to absolute position. */
2304 tty_cursor(struct tty
*tty
, u_int cx
, u_int cy
)
2306 struct tty_term
*term
= tty
->term
;
2310 if (tty
->flags
& TTY_BLOCK
)
2317 * If in the automargin space, and want to be there, do not move.
2318 * Otherwise, force the cursor to be in range (and complain).
2320 if (cx
== thisx
&& cy
== thisy
&& cx
== tty
->sx
)
2322 if (cx
> tty
->sx
- 1) {
2323 log_debug("%s: x too big %u > %u", __func__
, cx
, tty
->sx
- 1);
2328 if (cx
== thisx
&& cy
== thisy
)
2331 /* Currently at the very end of the line - use absolute movement. */
2332 if (thisx
> tty
->sx
- 1)
2335 /* Move to home position (0, 0). */
2336 if (cx
== 0 && cy
== 0 && tty_term_has(term
, TTYC_HOME
)) {
2337 tty_putcode(tty
, TTYC_HOME
);
2341 /* Zero on the next line. */
2342 if (cx
== 0 && cy
== thisy
+ 1 && thisy
!= tty
->rlower
&&
2343 (!tty_use_margin(tty
) || tty
->rleft
== 0)) {
2344 tty_putc(tty
, '\r');
2345 tty_putc(tty
, '\n');
2349 /* Moving column or row. */
2352 * Moving column only, row staying the same.
2356 if (cx
== 0 && (!tty_use_margin(tty
) || tty
->rleft
== 0)) {
2357 tty_putc(tty
, '\r');
2361 /* One to the left. */
2362 if (cx
== thisx
- 1 && tty_term_has(term
, TTYC_CUB1
)) {
2363 tty_putcode(tty
, TTYC_CUB1
);
2367 /* One to the right. */
2368 if (cx
== thisx
+ 1 && tty_term_has(term
, TTYC_CUF1
)) {
2369 tty_putcode(tty
, TTYC_CUF1
);
2373 /* Calculate difference. */
2374 change
= thisx
- cx
; /* +ve left, -ve right */
2377 * Use HPA if change is larger than absolute, otherwise move
2378 * the cursor with CUB/CUF.
2380 if ((u_int
) abs(change
) > cx
&& tty_term_has(term
, TTYC_HPA
)) {
2381 tty_putcode1(tty
, TTYC_HPA
, cx
);
2383 } else if (change
> 0 &&
2384 tty_term_has(term
, TTYC_CUB
) &&
2385 !tty_use_margin(tty
)) {
2386 if (change
== 2 && tty_term_has(term
, TTYC_CUB1
)) {
2387 tty_putcode(tty
, TTYC_CUB1
);
2388 tty_putcode(tty
, TTYC_CUB1
);
2391 tty_putcode1(tty
, TTYC_CUB
, change
);
2393 } else if (change
< 0 &&
2394 tty_term_has(term
, TTYC_CUF
) &&
2395 !tty_use_margin(tty
)) {
2396 tty_putcode1(tty
, TTYC_CUF
, -change
);
2399 } else if (cx
== thisx
) {
2401 * Moving row only, column staying the same.
2405 if (thisy
!= tty
->rupper
&&
2406 cy
== thisy
- 1 && tty_term_has(term
, TTYC_CUU1
)) {
2407 tty_putcode(tty
, TTYC_CUU1
);
2412 if (thisy
!= tty
->rlower
&&
2413 cy
== thisy
+ 1 && tty_term_has(term
, TTYC_CUD1
)) {
2414 tty_putcode(tty
, TTYC_CUD1
);
2418 /* Calculate difference. */
2419 change
= thisy
- cy
; /* +ve up, -ve down */
2422 * Try to use VPA if change is larger than absolute or if this
2423 * change would cross the scroll region, otherwise use CUU/CUD.
2425 if ((u_int
) abs(change
) > cy
||
2426 (change
< 0 && cy
- change
> tty
->rlower
) ||
2427 (change
> 0 && cy
- change
< tty
->rupper
)) {
2428 if (tty_term_has(term
, TTYC_VPA
)) {
2429 tty_putcode1(tty
, TTYC_VPA
, cy
);
2432 } else if (change
> 0 && tty_term_has(term
, TTYC_CUU
)) {
2433 tty_putcode1(tty
, TTYC_CUU
, change
);
2435 } else if (change
< 0 && tty_term_has(term
, TTYC_CUD
)) {
2436 tty_putcode1(tty
, TTYC_CUD
, -change
);
2442 /* Absolute movement. */
2443 tty_putcode2(tty
, TTYC_CUP
, cy
, cx
);
2451 tty_attributes(struct tty
*tty
, const struct grid_cell
*gc
,
2452 const struct grid_cell
*defaults
, struct colour_palette
*palette
)
2454 struct grid_cell
*tc
= &tty
->cell
, gc2
;
2457 /* Copy cell and update default colours. */
2458 memcpy(&gc2
, gc
, sizeof gc2
);
2459 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2461 gc2
.fg
= defaults
->fg
;
2463 gc2
.bg
= defaults
->bg
;
2466 /* Ignore cell if it is the same as the last one. */
2467 if (gc2
.attr
== tty
->last_cell
.attr
&&
2468 gc2
.fg
== tty
->last_cell
.fg
&&
2469 gc2
.bg
== tty
->last_cell
.bg
&&
2470 gc2
.us
== tty
->last_cell
.us
)
2474 * If no setab, try to use the reverse attribute as a best-effort for a
2475 * non-default background. This is a bit of a hack but it doesn't do
2476 * any serious harm and makes a couple of applications happier.
2478 if (!tty_term_has(tty
->term
, TTYC_SETAB
)) {
2479 if (gc2
.attr
& GRID_ATTR_REVERSE
) {
2480 if (gc2
.fg
!= 7 && !COLOUR_DEFAULT(gc2
.fg
))
2481 gc2
.attr
&= ~GRID_ATTR_REVERSE
;
2483 if (gc2
.bg
!= 0 && !COLOUR_DEFAULT(gc2
.bg
))
2484 gc2
.attr
|= GRID_ATTR_REVERSE
;
2488 /* Fix up the colours if necessary. */
2489 tty_check_fg(tty
, palette
, &gc2
);
2490 tty_check_bg(tty
, palette
, &gc2
);
2491 tty_check_us(tty
, palette
, &gc2
);
2494 * If any bits are being cleared or the underline colour is now default,
2497 if ((tc
->attr
& ~gc2
.attr
) || (tc
->us
!= gc2
.us
&& gc2
.us
== 0))
2501 * Set the colours. This may call tty_reset() (so it comes next) and
2502 * may add to (NOT remove) the desired attributes.
2504 tty_colours(tty
, &gc2
);
2506 /* Filter out attribute bits already set. */
2507 changed
= gc2
.attr
& ~tc
->attr
;
2508 tc
->attr
= gc2
.attr
;
2510 /* Set the attributes. */
2511 if (changed
& GRID_ATTR_BRIGHT
)
2512 tty_putcode(tty
, TTYC_BOLD
);
2513 if (changed
& GRID_ATTR_DIM
)
2514 tty_putcode(tty
, TTYC_DIM
);
2515 if (changed
& GRID_ATTR_ITALICS
)
2516 tty_set_italics(tty
);
2517 if (changed
& GRID_ATTR_ALL_UNDERSCORE
) {
2518 if ((changed
& GRID_ATTR_UNDERSCORE
) ||
2519 !tty_term_has(tty
->term
, TTYC_SMULX
))
2520 tty_putcode(tty
, TTYC_SMUL
);
2521 else if (changed
& GRID_ATTR_UNDERSCORE_2
)
2522 tty_putcode1(tty
, TTYC_SMULX
, 2);
2523 else if (changed
& GRID_ATTR_UNDERSCORE_3
)
2524 tty_putcode1(tty
, TTYC_SMULX
, 3);
2525 else if (changed
& GRID_ATTR_UNDERSCORE_4
)
2526 tty_putcode1(tty
, TTYC_SMULX
, 4);
2527 else if (changed
& GRID_ATTR_UNDERSCORE_5
)
2528 tty_putcode1(tty
, TTYC_SMULX
, 5);
2530 if (changed
& GRID_ATTR_BLINK
)
2531 tty_putcode(tty
, TTYC_BLINK
);
2532 if (changed
& GRID_ATTR_REVERSE
) {
2533 if (tty_term_has(tty
->term
, TTYC_REV
))
2534 tty_putcode(tty
, TTYC_REV
);
2535 else if (tty_term_has(tty
->term
, TTYC_SMSO
))
2536 tty_putcode(tty
, TTYC_SMSO
);
2538 if (changed
& GRID_ATTR_HIDDEN
)
2539 tty_putcode(tty
, TTYC_INVIS
);
2540 if (changed
& GRID_ATTR_STRIKETHROUGH
)
2541 tty_putcode(tty
, TTYC_SMXX
);
2542 if (changed
& GRID_ATTR_OVERLINE
)
2543 tty_putcode(tty
, TTYC_SMOL
);
2544 if ((changed
& GRID_ATTR_CHARSET
) && tty_acs_needed(tty
))
2545 tty_putcode(tty
, TTYC_SMACS
);
2547 memcpy(&tty
->last_cell
, &gc2
, sizeof tty
->last_cell
);
2551 tty_colours(struct tty
*tty
, const struct grid_cell
*gc
)
2553 struct grid_cell
*tc
= &tty
->cell
;
2556 /* No changes? Nothing is necessary. */
2557 if (gc
->fg
== tc
->fg
&& gc
->bg
== tc
->bg
&& gc
->us
== tc
->us
)
2561 * Is either the default colour? This is handled specially because the
2562 * best solution might be to reset both colours to default, in which
2563 * case if only one is default need to fall onward to set the other
2566 if (COLOUR_DEFAULT(gc
->fg
) || COLOUR_DEFAULT(gc
->bg
)) {
2568 * If don't have AX but do have op, send sgr0 (op can't
2569 * actually be used because it is sometimes the same as sgr0
2570 * and sometimes isn't). This resets both colours to default.
2572 * Otherwise, try to set the default colour only as needed.
2574 have_ax
= tty_term_flag(tty
->term
, TTYC_AX
);
2575 if (!have_ax
&& tty_term_has(tty
->term
, TTYC_OP
))
2578 if (COLOUR_DEFAULT(gc
->fg
) && !COLOUR_DEFAULT(tc
->fg
)) {
2580 tty_puts(tty
, "\033[39m");
2581 else if (tc
->fg
!= 7)
2582 tty_putcode1(tty
, TTYC_SETAF
, 7);
2585 if (COLOUR_DEFAULT(gc
->bg
) && !COLOUR_DEFAULT(tc
->bg
)) {
2587 tty_puts(tty
, "\033[49m");
2588 else if (tc
->bg
!= 0)
2589 tty_putcode1(tty
, TTYC_SETAB
, 0);
2595 /* Set the foreground colour. */
2596 if (!COLOUR_DEFAULT(gc
->fg
) && gc
->fg
!= tc
->fg
)
2597 tty_colours_fg(tty
, gc
);
2600 * Set the background colour. This must come after the foreground as
2601 * tty_colour_fg() can call tty_reset().
2603 if (!COLOUR_DEFAULT(gc
->bg
) && gc
->bg
!= tc
->bg
)
2604 tty_colours_bg(tty
, gc
);
2606 /* Set the underscore colour. */
2607 if (gc
->us
!= tc
->us
)
2608 tty_colours_us(tty
, gc
);
2612 tty_check_fg(struct tty
*tty
, struct colour_palette
*palette
,
2613 struct grid_cell
*gc
)
2620 * Perform substitution if this pane has a palette. If the bright
2621 * attribute is set, use the bright entry in the palette by changing to
2622 * the aixterm colour.
2624 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2626 if (c
< 8 && gc
->attr
& GRID_ATTR_BRIGHT
)
2628 if ((c
= colour_palette_get(palette
, c
)) != -1)
2632 /* Is this a 24-bit colour? */
2633 if (gc
->fg
& COLOUR_FLAG_RGB
) {
2634 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2635 if (tty
->term
->flags
& TERM_RGBCOLOURS
)
2637 colour_split_rgb(gc
->fg
, &r
, &g
, &b
);
2638 gc
->fg
= colour_find_rgb(r
, g
, b
);
2641 /* How many colours does this terminal have? */
2642 if (tty
->term
->flags
& TERM_256COLOURS
)
2645 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
2647 /* Is this a 256-colour colour? */
2648 if (gc
->fg
& COLOUR_FLAG_256
) {
2649 /* And not a 256 colour mode? */
2650 if (colours
< 256) {
2651 gc
->fg
= colour_256to16(gc
->fg
);
2661 /* Is this an aixterm colour? */
2662 if (gc
->fg
>= 90 && gc
->fg
<= 97 && colours
< 16) {
2664 gc
->attr
|= GRID_ATTR_BRIGHT
;
2669 tty_check_bg(struct tty
*tty
, struct colour_palette
*palette
,
2670 struct grid_cell
*gc
)
2676 /* Perform substitution if this pane has a palette. */
2677 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2678 if ((c
= colour_palette_get(palette
, gc
->bg
)) != -1)
2682 /* Is this a 24-bit colour? */
2683 if (gc
->bg
& COLOUR_FLAG_RGB
) {
2684 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2685 if (tty
->term
->flags
& TERM_RGBCOLOURS
)
2687 colour_split_rgb(gc
->bg
, &r
, &g
, &b
);
2688 gc
->bg
= colour_find_rgb(r
, g
, b
);
2691 /* How many colours does this terminal have? */
2692 if (tty
->term
->flags
& TERM_256COLOURS
)
2695 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
2697 /* Is this a 256-colour colour? */
2698 if (gc
->bg
& COLOUR_FLAG_256
) {
2700 * And not a 256 colour mode? Translate to 16-colour
2701 * palette. Bold background doesn't exist portably, so just
2702 * discard the bold bit if set.
2704 if (colours
< 256) {
2705 gc
->bg
= colour_256to16(gc
->bg
);
2715 /* Is this an aixterm colour? */
2716 if (gc
->bg
>= 90 && gc
->bg
<= 97 && colours
< 16)
2721 tty_check_us(__unused
struct tty
*tty
, struct colour_palette
*palette
,
2722 struct grid_cell
*gc
)
2726 /* Perform substitution if this pane has a palette. */
2727 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2728 if ((c
= colour_palette_get(palette
, gc
->us
)) != -1)
2732 /* Underscore colour is set as RGB so convert a 256 colour to RGB. */
2733 if (gc
->us
& COLOUR_FLAG_256
)
2734 gc
->us
= colour_256toRGB (gc
->us
);
2738 tty_colours_fg(struct tty
*tty
, const struct grid_cell
*gc
)
2740 struct grid_cell
*tc
= &tty
->cell
;
2743 /* Is this a 24-bit or 256-colour colour? */
2744 if (gc
->fg
& COLOUR_FLAG_RGB
|| gc
->fg
& COLOUR_FLAG_256
) {
2745 if (tty_try_colour(tty
, gc
->fg
, "38") == 0)
2747 /* Should not get here, already converted in tty_check_fg. */
2751 /* Is this an aixterm bright colour? */
2752 if (gc
->fg
>= 90 && gc
->fg
<= 97) {
2753 if (tty
->term
->flags
& TERM_256COLOURS
) {
2754 xsnprintf(s
, sizeof s
, "\033[%dm", gc
->fg
);
2757 tty_putcode1(tty
, TTYC_SETAF
, gc
->fg
- 90 + 8);
2761 /* Otherwise set the foreground colour. */
2762 tty_putcode1(tty
, TTYC_SETAF
, gc
->fg
);
2765 /* Save the new values in the terminal current cell. */
2770 tty_colours_bg(struct tty
*tty
, const struct grid_cell
*gc
)
2772 struct grid_cell
*tc
= &tty
->cell
;
2775 /* Is this a 24-bit or 256-colour colour? */
2776 if (gc
->bg
& COLOUR_FLAG_RGB
|| gc
->bg
& COLOUR_FLAG_256
) {
2777 if (tty_try_colour(tty
, gc
->bg
, "48") == 0)
2779 /* Should not get here, already converted in tty_check_bg. */
2783 /* Is this an aixterm bright colour? */
2784 if (gc
->bg
>= 90 && gc
->bg
<= 97) {
2785 if (tty
->term
->flags
& TERM_256COLOURS
) {
2786 xsnprintf(s
, sizeof s
, "\033[%dm", gc
->bg
+ 10);
2789 tty_putcode1(tty
, TTYC_SETAB
, gc
->bg
- 90 + 8);
2793 /* Otherwise set the background colour. */
2794 tty_putcode1(tty
, TTYC_SETAB
, gc
->bg
);
2797 /* Save the new values in the terminal current cell. */
2802 tty_colours_us(struct tty
*tty
, const struct grid_cell
*gc
)
2804 struct grid_cell
*tc
= &tty
->cell
;
2808 /* Clear underline colour. */
2810 tty_putcode(tty
, TTYC_OL
);
2814 /* Must be an RGB colour - this should never happen. */
2815 if (~gc
->us
& COLOUR_FLAG_RGB
)
2819 * Setulc and setal follows the ncurses(3) one argument "direct colour"
2820 * capability format. Calculate the colour value.
2822 colour_split_rgb(gc
->us
, &r
, &g
, &b
);
2823 c
= (65536 * r
) + (256 * g
) + b
;
2826 * Write the colour. Only use setal if the RGB flag is set because the
2827 * non-RGB version may be wrong.
2829 if (tty_term_has(tty
->term
, TTYC_SETULC
))
2830 tty_putcode1(tty
, TTYC_SETULC
, c
);
2831 else if (tty_term_has(tty
->term
, TTYC_SETAL
) &&
2832 tty_term_has(tty
->term
, TTYC_RGB
))
2833 tty_putcode1(tty
, TTYC_SETAL
, c
);
2836 /* Save the new values in the terminal current cell. */
2841 tty_try_colour(struct tty
*tty
, int colour
, const char *type
)
2845 if (colour
& COLOUR_FLAG_256
) {
2846 if (*type
== '3' && tty_term_has(tty
->term
, TTYC_SETAF
))
2847 tty_putcode1(tty
, TTYC_SETAF
, colour
& 0xff);
2848 else if (tty_term_has(tty
->term
, TTYC_SETAB
))
2849 tty_putcode1(tty
, TTYC_SETAB
, colour
& 0xff);
2853 if (colour
& COLOUR_FLAG_RGB
) {
2854 colour_split_rgb(colour
& 0xffffff, &r
, &g
, &b
);
2855 if (*type
== '3' && tty_term_has(tty
->term
, TTYC_SETRGBF
))
2856 tty_putcode3(tty
, TTYC_SETRGBF
, r
, g
, b
);
2857 else if (tty_term_has(tty
->term
, TTYC_SETRGBB
))
2858 tty_putcode3(tty
, TTYC_SETRGBB
, r
, g
, b
);
2866 tty_window_default_style(struct grid_cell
*gc
, struct window_pane
*wp
)
2868 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2869 gc
->fg
= wp
->palette
.fg
;
2870 gc
->bg
= wp
->palette
.bg
;
2874 tty_default_colours(struct grid_cell
*gc
, struct window_pane
*wp
)
2876 struct options
*oo
= wp
->options
;
2877 struct format_tree
*ft
;
2879 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2881 if (wp
->flags
& PANE_STYLECHANGED
) {
2882 log_debug("%%%u: style changed", wp
->id
);
2883 wp
->flags
&= ~PANE_STYLECHANGED
;
2885 ft
= format_create(NULL
, NULL
, FORMAT_PANE
|wp
->id
,
2887 format_defaults(ft
, NULL
, NULL
, NULL
, wp
);
2888 tty_window_default_style(&wp
->cached_active_gc
, wp
);
2889 style_add(&wp
->cached_active_gc
, oo
, "window-active-style", ft
);
2890 tty_window_default_style(&wp
->cached_gc
, wp
);
2891 style_add(&wp
->cached_gc
, oo
, "window-style", ft
);
2896 if (wp
== wp
->window
->active
&& wp
->cached_active_gc
.fg
!= 8)
2897 gc
->fg
= wp
->cached_active_gc
.fg
;
2899 gc
->fg
= wp
->cached_gc
.fg
;
2903 if (wp
== wp
->window
->active
&& wp
->cached_active_gc
.bg
!= 8)
2904 gc
->bg
= wp
->cached_active_gc
.bg
;
2906 gc
->bg
= wp
->cached_gc
.bg
;
2911 tty_default_attributes(struct tty
*tty
, const struct grid_cell
*defaults
,
2912 struct colour_palette
*palette
, u_int bg
)
2914 struct grid_cell gc
;
2916 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
2918 tty_attributes(tty
, &gc
, defaults
, palette
);