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>
36 static int tty_log_fd
= -1;
38 static void tty_set_italics(struct tty
*);
39 static int tty_try_colour(struct tty
*, int, const char *);
40 static void tty_force_cursor_colour(struct tty
*, int);
41 static void tty_cursor_pane(struct tty
*, const struct tty_ctx
*, u_int
,
43 static void tty_cursor_pane_unless_wrap(struct tty
*,
44 const struct tty_ctx
*, u_int
, u_int
);
45 static void tty_colours(struct tty
*, const struct grid_cell
*);
46 static void tty_check_fg(struct tty
*, struct colour_palette
*,
48 static void tty_check_bg(struct tty
*, struct colour_palette
*,
50 static void tty_check_us(struct tty
*, struct colour_palette
*,
52 static void tty_colours_fg(struct tty
*, const struct grid_cell
*);
53 static void tty_colours_bg(struct tty
*, const struct grid_cell
*);
54 static void tty_colours_us(struct tty
*, const struct grid_cell
*);
56 static void tty_region_pane(struct tty
*, const struct tty_ctx
*, u_int
,
58 static void tty_region(struct tty
*, u_int
, u_int
);
59 static void tty_margin_pane(struct tty
*, const struct tty_ctx
*);
60 static void tty_margin(struct tty
*, u_int
, u_int
);
61 static int tty_large_region(struct tty
*, const struct tty_ctx
*);
62 static int tty_fake_bce(const struct tty
*, const struct grid_cell
*,
64 static void tty_redraw_region(struct tty
*, const struct tty_ctx
*);
65 static void tty_emulate_repeat(struct tty
*, enum tty_code_code
,
66 enum tty_code_code
, u_int
);
67 static void tty_repeat_space(struct tty
*, u_int
);
68 static void tty_draw_pane(struct tty
*, const struct tty_ctx
*, u_int
);
69 static void tty_default_attributes(struct tty
*, const struct grid_cell
*,
70 struct colour_palette
*, u_int
, struct hyperlinks
*);
71 static int tty_check_overlay(struct tty
*, u_int
, u_int
);
72 static void tty_check_overlay_range(struct tty
*, u_int
, u_int
, u_int
,
73 struct overlay_ranges
*);
76 static void tty_write_one(void (*)(struct tty
*, const struct tty_ctx
*),
77 struct client
*, struct tty_ctx
*);
80 #define tty_use_margin(tty) \
81 (tty->term->flags & TERM_DECSLRM)
82 #define tty_full_width(tty, ctx) \
83 ((ctx)->xoff == 0 && (ctx)->sx >= (tty)->sx)
85 #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
86 #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
87 #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
89 #define TTY_QUERY_TIMEOUT 5
90 #define TTY_REQUEST_LIMIT 30
97 xsnprintf(name
, sizeof name
, "tmux-out-%ld.log", (long)getpid());
99 tty_log_fd
= open(name
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
100 if (tty_log_fd
!= -1 && fcntl(tty_log_fd
, F_SETFD
, FD_CLOEXEC
) == -1)
101 fatal("fcntl failed");
105 tty_init(struct tty
*tty
, struct client
*c
)
110 memset(tty
, 0, sizeof *tty
);
113 tty
->cstyle
= SCREEN_CURSOR_DEFAULT
;
115 tty
->fg
= tty
->bg
= -1;
117 if (tcgetattr(c
->fd
, &tty
->tio
) != 0)
123 tty_resize(struct tty
*tty
)
125 struct client
*c
= tty
->client
;
127 u_int sx
, sy
, xpixel
, ypixel
;
129 if (ioctl(c
->fd
, TIOCGWINSZ
, &ws
) != -1) {
135 xpixel
= ws
.ws_xpixel
/ sx
;
141 ypixel
= ws
.ws_ypixel
/ sy
;
143 if ((xpixel
== 0 || ypixel
== 0) &&
145 !(tty
->flags
& TTY_WINSIZEQUERY
) &&
146 (tty
->term
->flags
& TERM_VT100LIKE
)) {
147 tty_puts(tty
, "\033[18t\033[14t");
148 tty
->flags
|= TTY_WINSIZEQUERY
;
156 log_debug("%s: %s now %ux%u (%ux%u)", __func__
, c
->name
, sx
, sy
,
158 tty_set_size(tty
, sx
, sy
, xpixel
, ypixel
);
163 tty_set_size(struct tty
*tty
, u_int sx
, u_int sy
, u_int xpixel
, u_int ypixel
)
167 tty
->xpixel
= xpixel
;
168 tty
->ypixel
= ypixel
;
172 tty_read_callback(__unused
int fd
, __unused
short events
, void *data
)
174 struct tty
*tty
= data
;
175 struct client
*c
= tty
->client
;
176 const char *name
= c
->name
;
177 size_t size
= EVBUFFER_LENGTH(tty
->in
);
180 nread
= evbuffer_read(tty
->in
, c
->fd
, -1);
181 if (nread
== 0 || nread
== -1) {
183 log_debug("%s: read closed", name
);
185 log_debug("%s: read error: %s", name
, strerror(errno
));
186 event_del(&tty
->event_in
);
187 server_client_lost(tty
->client
);
190 log_debug("%s: read %d bytes (already %zu)", name
, nread
, size
);
192 while (tty_keys_next(tty
))
197 tty_timer_callback(__unused
int fd
, __unused
short events
, void *data
)
199 struct tty
*tty
= data
;
200 struct client
*c
= tty
->client
;
201 struct timeval tv
= { .tv_usec
= TTY_BLOCK_INTERVAL
};
203 log_debug("%s: %zu discarded", c
->name
, tty
->discarded
);
205 c
->flags
|= CLIENT_ALLREDRAWFLAGS
;
206 c
->discarded
+= tty
->discarded
;
208 if (tty
->discarded
< TTY_BLOCK_STOP(tty
)) {
209 tty
->flags
&= ~TTY_BLOCK
;
214 evtimer_add(&tty
->timer
, &tv
);
218 tty_block_maybe(struct tty
*tty
)
220 struct client
*c
= tty
->client
;
221 size_t size
= EVBUFFER_LENGTH(tty
->out
);
222 struct timeval tv
= { .tv_usec
= TTY_BLOCK_INTERVAL
};
225 tty
->flags
&= ~TTY_NOBLOCK
;
226 else if (tty
->flags
& TTY_NOBLOCK
)
229 if (size
< TTY_BLOCK_START(tty
))
232 if (tty
->flags
& TTY_BLOCK
)
234 tty
->flags
|= TTY_BLOCK
;
236 log_debug("%s: can't keep up, %zu discarded", c
->name
, size
);
238 evbuffer_drain(tty
->out
, size
);
239 c
->discarded
+= size
;
242 evtimer_add(&tty
->timer
, &tv
);
247 tty_write_callback(__unused
int fd
, __unused
short events
, void *data
)
249 struct tty
*tty
= data
;
250 struct client
*c
= tty
->client
;
251 size_t size
= EVBUFFER_LENGTH(tty
->out
);
254 nwrite
= evbuffer_write(tty
->out
, c
->fd
);
257 log_debug("%s: wrote %d bytes (of %zu)", c
->name
, nwrite
, size
);
260 if ((size_t)nwrite
>= c
->redraw
)
264 log_debug("%s: waiting for redraw, %zu bytes left", c
->name
,
266 } else if (tty_block_maybe(tty
))
269 if (EVBUFFER_LENGTH(tty
->out
) != 0)
270 event_add(&tty
->event_out
, NULL
);
274 tty_open(struct tty
*tty
, char **cause
)
276 struct client
*c
= tty
->client
;
278 tty
->term
= tty_term_create(tty
, c
->term_name
, c
->term_caps
,
279 c
->term_ncaps
, &c
->term_features
, cause
);
280 if (tty
->term
== NULL
) {
284 tty
->flags
|= TTY_OPENED
;
286 tty
->flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
|TTY_BLOCK
|TTY_TIMER
);
288 event_set(&tty
->event_in
, c
->fd
, EV_PERSIST
|EV_READ
,
289 tty_read_callback
, tty
);
290 tty
->in
= evbuffer_new();
292 fatal("out of memory");
294 event_set(&tty
->event_out
, c
->fd
, EV_WRITE
, tty_write_callback
, tty
);
295 tty
->out
= evbuffer_new();
296 if (tty
->out
== NULL
)
297 fatal("out of memory");
299 evtimer_set(&tty
->timer
, tty_timer_callback
, tty
);
308 tty_start_timer_callback(__unused
int fd
, __unused
short events
, void *data
)
310 struct tty
*tty
= data
;
311 struct client
*c
= tty
->client
;
313 log_debug("%s: start timer fired", c
->name
);
314 if ((tty
->flags
& (TTY_HAVEDA
|TTY_HAVEDA2
|TTY_HAVEXDA
)) == 0)
315 tty_update_features(tty
);
316 tty
->flags
|= TTY_ALL_REQUEST_FLAGS
;
320 tty_start_tty(struct tty
*tty
)
322 struct client
*c
= tty
->client
;
324 struct timeval tv
= { .tv_sec
= TTY_QUERY_TIMEOUT
};
326 setblocking(c
->fd
, 0);
327 event_add(&tty
->event_in
, NULL
);
329 memcpy(&tio
, &tty
->tio
, sizeof tio
);
330 tio
.c_iflag
&= ~(IXON
|IXOFF
|ICRNL
|INLCR
|IGNCR
|IMAXBEL
|ISTRIP
);
331 tio
.c_iflag
|= IGNBRK
;
332 tio
.c_oflag
&= ~(OPOST
|ONLCR
|OCRNL
|ONLRET
);
333 tio
.c_lflag
&= ~(IEXTEN
|ICANON
|ECHO
|ECHOE
|ECHONL
|ECHOCTL
|ECHOPRT
|
337 if (tcsetattr(c
->fd
, TCSANOW
, &tio
) == 0)
338 tcflush(c
->fd
, TCOFLUSH
);
340 tty_putcode(tty
, TTYC_SMCUP
);
342 tty_putcode(tty
, TTYC_SMKX
);
343 tty_putcode(tty
, TTYC_CLEAR
);
345 if (tty_acs_needed(tty
)) {
346 log_debug("%s: using capabilities for ACS", c
->name
);
347 tty_putcode(tty
, TTYC_ENACS
);
349 log_debug("%s: using UTF-8 for ACS", c
->name
);
351 tty_putcode(tty
, TTYC_CNORM
);
352 if (tty_term_has(tty
->term
, TTYC_KMOUS
)) {
353 tty_puts(tty
, "\033[?1000l\033[?1002l\033[?1003l");
354 tty_puts(tty
, "\033[?1006l\033[?1005l");
356 if (tty_term_has(tty
->term
, TTYC_ENBP
))
357 tty_putcode(tty
, TTYC_ENBP
);
359 evtimer_set(&tty
->start_timer
, tty_start_timer_callback
, tty
);
360 evtimer_add(&tty
->start_timer
, &tv
);
362 tty
->flags
|= TTY_STARTED
;
365 if (tty
->ccolour
!= -1)
366 tty_force_cursor_colour(tty
, -1);
368 tty
->mouse_drag_flag
= 0;
369 tty
->mouse_drag_update
= NULL
;
370 tty
->mouse_drag_release
= NULL
;
374 tty_send_requests(struct tty
*tty
)
376 if (~tty
->flags
& TTY_STARTED
)
379 if (tty
->term
->flags
& TERM_VT100LIKE
) {
380 if (~tty
->term
->flags
& TTY_HAVEDA
)
381 tty_puts(tty
, "\033[c");
382 if (~tty
->flags
& TTY_HAVEDA2
)
383 tty_puts(tty
, "\033[>c");
384 if (~tty
->flags
& TTY_HAVEXDA
)
385 tty_puts(tty
, "\033[>q");
386 tty_puts(tty
, "\033]10;?\033\\");
387 tty_puts(tty
, "\033]11;?\033\\");
389 tty
->flags
|= TTY_ALL_REQUEST_FLAGS
;
390 tty
->last_requests
= time(NULL
);
394 tty_repeat_requests(struct tty
*tty
)
396 time_t t
= time(NULL
);
398 if (~tty
->flags
& TTY_STARTED
)
401 if (t
- tty
->last_requests
<= TTY_REQUEST_LIMIT
)
403 tty
->last_requests
= t
;
405 if (tty
->term
->flags
& TERM_VT100LIKE
) {
406 tty_puts(tty
, "\033]10;?\033\\");
407 tty_puts(tty
, "\033]11;?\033\\");
412 tty_stop_tty(struct tty
*tty
)
414 struct client
*c
= tty
->client
;
417 if (!(tty
->flags
& TTY_STARTED
))
419 tty
->flags
&= ~TTY_STARTED
;
421 evtimer_del(&tty
->start_timer
);
423 event_del(&tty
->timer
);
424 tty
->flags
&= ~TTY_BLOCK
;
426 event_del(&tty
->event_in
);
427 event_del(&tty
->event_out
);
430 * Be flexible about error handling and try not kill the server just
431 * because the fd is invalid. Things like ssh -t can easily leave us
434 if (ioctl(c
->fd
, TIOCGWINSZ
, &ws
) == -1)
436 if (tcsetattr(c
->fd
, TCSANOW
, &tty
->tio
) == -1)
439 tty_raw(tty
, tty_term_string_ii(tty
->term
, TTYC_CSR
, 0, ws
.ws_row
- 1));
440 if (tty_acs_needed(tty
))
441 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMACS
));
442 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_SGR0
));
443 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMKX
));
444 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CLEAR
));
445 if (tty
->cstyle
!= SCREEN_CURSOR_DEFAULT
) {
446 if (tty_term_has(tty
->term
, TTYC_SE
))
447 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_SE
));
448 else if (tty_term_has(tty
->term
, TTYC_SS
))
449 tty_raw(tty
, tty_term_string_i(tty
->term
, TTYC_SS
, 0));
451 if (tty
->ccolour
!= -1)
452 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CR
));
454 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CNORM
));
455 if (tty_term_has(tty
->term
, TTYC_KMOUS
)) {
456 tty_raw(tty
, "\033[?1000l\033[?1002l\033[?1003l");
457 tty_raw(tty
, "\033[?1006l\033[?1005l");
459 if (tty_term_has(tty
->term
, TTYC_DSBP
))
460 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSBP
));
462 if (tty
->term
->flags
& TERM_VT100LIKE
)
463 tty_raw(tty
, "\033[?7727l");
464 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSFCS
));
465 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSEKS
));
467 if (tty_use_margin(tty
))
468 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSMG
));
469 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMCUP
));
471 setblocking(c
->fd
, 1);
475 tty_close(struct tty
*tty
)
477 if (event_initialized(&tty
->key_timer
))
478 evtimer_del(&tty
->key_timer
);
481 if (tty
->flags
& TTY_OPENED
) {
482 evbuffer_free(tty
->in
);
483 event_del(&tty
->event_in
);
484 evbuffer_free(tty
->out
);
485 event_del(&tty
->event_out
);
487 tty_term_free(tty
->term
);
490 tty
->flags
&= ~TTY_OPENED
;
495 tty_free(struct tty
*tty
)
501 tty_update_features(struct tty
*tty
)
503 struct client
*c
= tty
->client
;
505 if (tty_apply_features(tty
->term
, c
->term_features
))
506 tty_term_apply_overrides(tty
->term
);
508 if (tty_use_margin(tty
))
509 tty_putcode(tty
, TTYC_ENMG
);
510 if (options_get_number(global_options
, "extended-keys"))
511 tty_puts(tty
, tty_term_string(tty
->term
, TTYC_ENEKS
));
512 if (options_get_number(global_options
, "focus-events"))
513 tty_puts(tty
, tty_term_string(tty
->term
, TTYC_ENFCS
));
514 if (tty
->term
->flags
& TERM_VT100LIKE
)
515 tty_puts(tty
, "\033[?7727h");
518 * Features might have changed since the first draw during attach. For
519 * example, this happens when DA responses are received.
521 server_redraw_client(c
);
527 tty_raw(struct tty
*tty
, const char *s
)
529 struct client
*c
= tty
->client
;
534 for (i
= 0; i
< 5; i
++) {
535 n
= write(c
->fd
, s
, slen
);
541 } else if (n
== -1 && errno
!= EAGAIN
)
548 tty_putcode(struct tty
*tty
, enum tty_code_code code
)
550 tty_puts(tty
, tty_term_string(tty
->term
, code
));
554 tty_putcode_i(struct tty
*tty
, enum tty_code_code code
, int a
)
558 tty_puts(tty
, tty_term_string_i(tty
->term
, code
, a
));
562 tty_putcode_ii(struct tty
*tty
, enum tty_code_code code
, int a
, int b
)
566 tty_puts(tty
, tty_term_string_ii(tty
->term
, code
, a
, b
));
570 tty_putcode_iii(struct tty
*tty
, enum tty_code_code code
, int a
, int b
, int c
)
572 if (a
< 0 || b
< 0 || c
< 0)
574 tty_puts(tty
, tty_term_string_iii(tty
->term
, code
, a
, b
, c
));
578 tty_putcode_s(struct tty
*tty
, enum tty_code_code code
, const char *a
)
581 tty_puts(tty
, tty_term_string_s(tty
->term
, code
, a
));
585 tty_putcode_ss(struct tty
*tty
, enum tty_code_code code
, const char *a
,
588 if (a
!= NULL
&& b
!= NULL
)
589 tty_puts(tty
, tty_term_string_ss(tty
->term
, code
, a
, b
));
593 tty_add(struct tty
*tty
, const char *buf
, size_t len
)
595 struct client
*c
= tty
->client
;
597 if (tty
->flags
& TTY_BLOCK
) {
598 tty
->discarded
+= len
;
602 evbuffer_add(tty
->out
, buf
, len
);
603 log_debug("%s: %.*s", c
->name
, (int)len
, buf
);
606 if (tty_log_fd
!= -1)
607 write(tty_log_fd
, buf
, len
);
608 if (tty
->flags
& TTY_STARTED
)
609 event_add(&tty
->event_out
, NULL
);
613 tty_puts(struct tty
*tty
, const char *s
)
616 tty_add(tty
, s
, strlen(s
));
620 tty_putc(struct tty
*tty
, u_char ch
)
624 if ((tty
->term
->flags
& TERM_NOAM
) &&
625 ch
>= 0x20 && ch
!= 0x7f &&
626 tty
->cy
== tty
->sy
- 1 &&
627 tty
->cx
+ 1 >= tty
->sx
)
630 if (tty
->cell
.attr
& GRID_ATTR_CHARSET
) {
631 acs
= tty_acs_get(tty
, ch
);
633 tty_add(tty
, acs
, strlen(acs
));
635 tty_add(tty
, &ch
, 1);
637 tty_add(tty
, &ch
, 1);
639 if (ch
>= 0x20 && ch
!= 0x7f) {
640 if (tty
->cx
>= tty
->sx
) {
642 if (tty
->cy
!= tty
->rlower
)
646 * On !am terminals, force the cursor position to where
647 * we think it should be after a line wrap - this means
648 * it works on sensible terminals as well.
650 if (tty
->term
->flags
& TERM_NOAM
)
651 tty_putcode_ii(tty
, TTYC_CUP
, tty
->cy
, tty
->cx
);
658 tty_putn(struct tty
*tty
, const void *buf
, size_t len
, u_int width
)
660 if ((tty
->term
->flags
& TERM_NOAM
) &&
661 tty
->cy
== tty
->sy
- 1 &&
662 tty
->cx
+ len
>= tty
->sx
)
663 len
= tty
->sx
- tty
->cx
- 1;
665 tty_add(tty
, buf
, len
);
666 if (tty
->cx
+ width
> tty
->sx
) {
667 tty
->cx
= (tty
->cx
+ width
) - tty
->sx
;
668 if (tty
->cx
<= tty
->sx
)
671 tty
->cx
= tty
->cy
= UINT_MAX
;
677 tty_set_italics(struct tty
*tty
)
681 if (tty_term_has(tty
->term
, TTYC_SITM
)) {
682 s
= options_get_string(global_options
, "default-terminal");
683 if (strcmp(s
, "screen") != 0 && strncmp(s
, "screen-", 7) != 0) {
684 tty_putcode(tty
, TTYC_SITM
);
688 tty_putcode(tty
, TTYC_SMSO
);
692 tty_set_title(struct tty
*tty
, const char *title
)
694 if (!tty_term_has(tty
->term
, TTYC_TSL
) ||
695 !tty_term_has(tty
->term
, TTYC_FSL
))
698 tty_putcode(tty
, TTYC_TSL
);
699 tty_puts(tty
, title
);
700 tty_putcode(tty
, TTYC_FSL
);
704 tty_set_path(struct tty
*tty
, const char *title
)
706 if (!tty_term_has(tty
->term
, TTYC_SWD
) ||
707 !tty_term_has(tty
->term
, TTYC_FSL
))
710 tty_putcode(tty
, TTYC_SWD
);
711 tty_puts(tty
, title
);
712 tty_putcode(tty
, TTYC_FSL
);
716 tty_force_cursor_colour(struct tty
*tty
, int c
)
722 c
= colour_force_rgb(c
);
723 if (c
== tty
->ccolour
)
726 tty_putcode(tty
, TTYC_CR
);
728 colour_split_rgb(c
, &r
, &g
, &b
);
729 xsnprintf(s
, sizeof s
, "rgb:%02hhx/%02hhx/%02hhx", r
, g
, b
);
730 tty_putcode_s(tty
, TTYC_CS
, s
);
736 tty_update_cursor(struct tty
*tty
, int mode
, struct screen
*s
)
738 enum screen_cursor_style cstyle
;
739 int ccolour
, changed
, cmode
= mode
;
741 /* Set cursor colour if changed. */
743 ccolour
= s
->ccolour
;
744 if (s
->ccolour
== -1)
745 ccolour
= s
->default_ccolour
;
746 tty_force_cursor_colour(tty
, ccolour
);
749 /* If cursor is off, set as invisible. */
750 if (~cmode
& MODE_CURSOR
) {
751 if (tty
->mode
& MODE_CURSOR
)
752 tty_putcode(tty
, TTYC_CIVIS
);
756 /* Check if blinking or very visible flag changed or style changed. */
758 cstyle
= tty
->cstyle
;
761 if (cstyle
== SCREEN_CURSOR_DEFAULT
) {
762 if (~cmode
& MODE_CURSOR_BLINKING_SET
) {
763 if (s
->default_mode
& MODE_CURSOR_BLINKING
)
764 cmode
|= MODE_CURSOR_BLINKING
;
766 cmode
&= ~MODE_CURSOR_BLINKING
;
768 cstyle
= s
->default_cstyle
;
772 /* If nothing changed, do nothing. */
773 changed
= cmode
^ tty
->mode
;
774 if ((changed
& CURSOR_MODES
) == 0 && cstyle
== tty
->cstyle
)
778 * Set cursor style. If an explicit style has been set with DECSCUSR,
779 * set it if supported, otherwise send cvvis for blinking styles.
781 * If no style, has been set (SCREEN_CURSOR_DEFAULT), then send cvvis
782 * if either the blinking or very visible flags are set.
784 tty_putcode(tty
, TTYC_CNORM
);
786 case SCREEN_CURSOR_DEFAULT
:
787 if (tty
->cstyle
!= SCREEN_CURSOR_DEFAULT
) {
788 if (tty_term_has(tty
->term
, TTYC_SE
))
789 tty_putcode(tty
, TTYC_SE
);
791 tty_putcode_i(tty
, TTYC_SS
, 0);
793 if (cmode
& (MODE_CURSOR_BLINKING
|MODE_CURSOR_VERY_VISIBLE
))
794 tty_putcode(tty
, TTYC_CVVIS
);
796 case SCREEN_CURSOR_BLOCK
:
797 if (tty_term_has(tty
->term
, TTYC_SS
)) {
798 if (cmode
& MODE_CURSOR_BLINKING
)
799 tty_putcode_i(tty
, TTYC_SS
, 1);
801 tty_putcode_i(tty
, TTYC_SS
, 2);
802 } else if (cmode
& MODE_CURSOR_BLINKING
)
803 tty_putcode(tty
, TTYC_CVVIS
);
805 case SCREEN_CURSOR_UNDERLINE
:
806 if (tty_term_has(tty
->term
, TTYC_SS
)) {
807 if (cmode
& MODE_CURSOR_BLINKING
)
808 tty_putcode_i(tty
, TTYC_SS
, 3);
810 tty_putcode_i(tty
, TTYC_SS
, 4);
811 } else if (cmode
& MODE_CURSOR_BLINKING
)
812 tty_putcode(tty
, TTYC_CVVIS
);
814 case SCREEN_CURSOR_BAR
:
815 if (tty_term_has(tty
->term
, TTYC_SS
)) {
816 if (cmode
& MODE_CURSOR_BLINKING
)
817 tty_putcode_i(tty
, TTYC_SS
, 5);
819 tty_putcode_i(tty
, TTYC_SS
, 6);
820 } else if (cmode
& MODE_CURSOR_BLINKING
)
821 tty_putcode(tty
, TTYC_CVVIS
);
824 tty
->cstyle
= cstyle
;
829 tty_update_mode(struct tty
*tty
, int mode
, struct screen
*s
)
831 struct tty_term
*term
= tty
->term
;
832 struct client
*c
= tty
->client
;
835 if (tty
->flags
& TTY_NOCURSOR
)
836 mode
&= ~MODE_CURSOR
;
838 if (tty_update_cursor(tty
, mode
, s
) & MODE_CURSOR_BLINKING
)
839 mode
|= MODE_CURSOR_BLINKING
;
841 mode
&= ~MODE_CURSOR_BLINKING
;
843 changed
= mode
^ tty
->mode
;
844 if (log_get_level() != 0 && changed
!= 0) {
845 log_debug("%s: current mode %s", c
->name
,
846 screen_mode_to_string(tty
->mode
));
847 log_debug("%s: setting mode %s", c
->name
,
848 screen_mode_to_string(mode
));
851 if ((changed
& ALL_MOUSE_MODES
) && tty_term_has(term
, TTYC_KMOUS
)) {
853 * If the mouse modes have changed, clear then all and apply
854 * again. There are differences in how terminals track the
857 tty_puts(tty
, "\033[?1006l\033[?1000l\033[?1002l\033[?1003l");
858 if (mode
& ALL_MOUSE_MODES
)
859 tty_puts(tty
, "\033[?1006h");
860 if (mode
& MODE_MOUSE_ALL
)
861 tty_puts(tty
, "\033[?1000h\033[?1002h\033[?1003h");
862 else if (mode
& MODE_MOUSE_BUTTON
)
863 tty_puts(tty
, "\033[?1000h\033[?1002h");
864 else if (mode
& MODE_MOUSE_STANDARD
)
865 tty_puts(tty
, "\033[?1000h");
871 tty_emulate_repeat(struct tty
*tty
, enum tty_code_code code
,
872 enum tty_code_code code1
, u_int n
)
874 if (tty_term_has(tty
->term
, code
))
875 tty_putcode_i(tty
, code
, n
);
878 tty_putcode(tty
, code1
);
883 tty_repeat_space(struct tty
*tty
, u_int n
)
888 memset(s
, ' ', sizeof s
);
890 while (n
> sizeof s
) {
891 tty_putn(tty
, s
, sizeof s
, sizeof s
);
895 tty_putn(tty
, s
, n
, n
);
898 /* Is this window bigger than the terminal? */
900 tty_window_bigger(struct tty
*tty
)
902 struct client
*c
= tty
->client
;
903 struct window
*w
= c
->session
->curw
->window
;
905 return (tty
->sx
< w
->sx
|| tty
->sy
- status_line_size(c
) < w
->sy
);
908 /* What offset should this window be drawn at? */
910 tty_window_offset(struct tty
*tty
, u_int
*ox
, u_int
*oy
, u_int
*sx
, u_int
*sy
)
920 /* What offset should this window be drawn at? */
922 tty_window_offset1(struct tty
*tty
, u_int
*ox
, u_int
*oy
, u_int
*sx
, u_int
*sy
)
924 struct client
*c
= tty
->client
;
925 struct window
*w
= c
->session
->curw
->window
;
926 struct window_pane
*wp
= server_client_get_pane(c
);
929 lines
= status_line_size(c
);
931 if (tty
->sx
>= w
->sx
&& tty
->sy
- lines
>= w
->sy
) {
937 c
->pan_window
= NULL
;
942 *sy
= tty
->sy
- lines
;
944 if (c
->pan_window
== w
) {
947 else if (c
->pan_ox
+ *sx
> w
->sx
)
948 c
->pan_ox
= w
->sx
- *sx
;
952 else if (c
->pan_oy
+ *sy
> w
->sy
)
953 c
->pan_oy
= w
->sy
- *sy
;
958 if (~wp
->screen
->mode
& MODE_CURSOR
) {
962 cx
= wp
->xoff
+ wp
->screen
->cx
;
963 cy
= wp
->yoff
+ wp
->screen
->cy
;
967 else if (cx
> w
->sx
- *sx
)
974 else if (cy
> w
->sy
- *sy
)
980 c
->pan_window
= NULL
;
984 /* Update stored offsets for a window and redraw if necessary. */
986 tty_update_window_offset(struct window
*w
)
990 TAILQ_FOREACH(c
, &clients
, entry
) {
991 if (c
->session
!= NULL
&&
992 c
->session
->curw
!= NULL
&&
993 c
->session
->curw
->window
== w
)
994 tty_update_client_offset(c
);
998 /* Update stored offsets for a client and redraw if necessary. */
1000 tty_update_client_offset(struct client
*c
)
1002 u_int ox
, oy
, sx
, sy
;
1004 if (~c
->flags
& CLIENT_TERMINAL
)
1007 c
->tty
.oflag
= tty_window_offset1(&c
->tty
, &ox
, &oy
, &sx
, &sy
);
1008 if (ox
== c
->tty
.oox
&&
1014 log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
1015 __func__
, c
->name
, c
->tty
.oox
, c
->tty
.ooy
, c
->tty
.osx
, c
->tty
.osy
,
1023 c
->flags
|= (CLIENT_REDRAWWINDOW
|CLIENT_REDRAWSTATUS
);
1027 * Is the region large enough to be worth redrawing once later rather than
1028 * probably several times now? Currently yes if it is more than 50% of the
1032 tty_large_region(__unused
struct tty
*tty
, const struct tty_ctx
*ctx
)
1034 return (ctx
->orlower
- ctx
->orupper
>= ctx
->sy
/ 2);
1038 * Return if BCE is needed but the terminal doesn't have it - it'll need to be
1042 tty_fake_bce(const struct tty
*tty
, const struct grid_cell
*gc
, u_int bg
)
1044 if (tty_term_flag(tty
->term
, TTYC_BCE
))
1046 if (!COLOUR_DEFAULT(bg
) || !COLOUR_DEFAULT(gc
->bg
))
1052 * Redraw scroll region using data from screen (already updated). Used when
1053 * CSR not supported, or window is a pane that doesn't take up the full
1054 * width of the terminal.
1057 tty_redraw_region(struct tty
*tty
, const struct tty_ctx
*ctx
)
1059 struct client
*c
= tty
->client
;
1063 * If region is large, schedule a redraw. In most cases this is likely
1064 * to be followed by some more scrolling.
1066 if (tty_large_region(tty
, ctx
)) {
1067 log_debug("%s: %s large redraw", __func__
, c
->name
);
1068 ctx
->redraw_cb(ctx
);
1072 for (i
= ctx
->orupper
; i
<= ctx
->orlower
; i
++)
1073 tty_draw_pane(tty
, ctx
, i
);
1076 /* Is this position visible in the pane? */
1078 tty_is_visible(__unused
struct tty
*tty
, const struct tty_ctx
*ctx
, u_int px
,
1079 u_int py
, u_int nx
, u_int ny
)
1081 u_int xoff
= ctx
->rxoff
+ px
, yoff
= ctx
->ryoff
+ py
;
1086 if (xoff
+ nx
<= ctx
->wox
|| xoff
>= ctx
->wox
+ ctx
->wsx
||
1087 yoff
+ ny
<= ctx
->woy
|| yoff
>= ctx
->woy
+ ctx
->wsy
)
1092 /* Clamp line position to visible part of pane. */
1094 tty_clamp_line(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int px
, u_int py
,
1095 u_int nx
, u_int
*i
, u_int
*x
, u_int
*rx
, u_int
*ry
)
1097 u_int xoff
= ctx
->rxoff
+ px
;
1099 if (!tty_is_visible(tty
, ctx
, px
, py
, nx
, 1))
1101 *ry
= ctx
->yoff
+ py
- ctx
->woy
;
1103 if (xoff
>= ctx
->wox
&& xoff
+ nx
<= ctx
->wox
+ ctx
->wsx
) {
1106 *x
= ctx
->xoff
+ px
- ctx
->wox
;
1108 } else if (xoff
< ctx
->wox
&& xoff
+ nx
> ctx
->wox
+ ctx
->wsx
) {
1109 /* Both left and right not visible. */
1113 } else if (xoff
< ctx
->wox
) {
1114 /* Left not visible. */
1115 *i
= ctx
->wox
- (ctx
->xoff
+ px
);
1119 /* Right not visible. */
1121 *x
= (ctx
->xoff
+ px
) - ctx
->wox
;
1122 *rx
= ctx
->wsx
- *x
;
1125 fatalx("%s: x too big, %u > %u", __func__
, *rx
, nx
);
1132 tty_clear_line(struct tty
*tty
, const struct grid_cell
*defaults
, u_int py
,
1133 u_int px
, u_int nx
, u_int bg
)
1135 struct client
*c
= tty
->client
;
1136 struct overlay_ranges r
;
1139 log_debug("%s: %s, %u at %u,%u", __func__
, c
->name
, nx
, px
, py
);
1141 /* Nothing to clear. */
1145 /* If genuine BCE is available, can try escape sequences. */
1146 if (c
->overlay_check
== NULL
&& !tty_fake_bce(tty
, defaults
, bg
)) {
1147 /* Off the end of the line, use EL if available. */
1148 if (px
+ nx
>= tty
->sx
&& tty_term_has(tty
->term
, TTYC_EL
)) {
1149 tty_cursor(tty
, px
, py
);
1150 tty_putcode(tty
, TTYC_EL
);
1154 /* At the start of the line. Use EL1. */
1155 if (px
== 0 && tty_term_has(tty
->term
, TTYC_EL1
)) {
1156 tty_cursor(tty
, px
+ nx
- 1, py
);
1157 tty_putcode(tty
, TTYC_EL1
);
1161 /* Section of line. Use ECH if possible. */
1162 if (tty_term_has(tty
->term
, TTYC_ECH
)) {
1163 tty_cursor(tty
, px
, py
);
1164 tty_putcode_i(tty
, TTYC_ECH
, nx
);
1170 * Couldn't use an escape sequence, use spaces. Clear only the visible
1171 * bit if there is an overlay.
1173 tty_check_overlay_range(tty
, px
, py
, nx
, &r
);
1174 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++) {
1177 tty_cursor(tty
, r
.px
[i
], py
);
1178 tty_repeat_space(tty
, r
.nx
[i
]);
1182 /* Clear a line, adjusting to visible part of pane. */
1184 tty_clear_pane_line(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int py
,
1185 u_int px
, u_int nx
, u_int bg
)
1187 struct client
*c
= tty
->client
;
1190 log_debug("%s: %s, %u at %u,%u", __func__
, c
->name
, nx
, px
, py
);
1192 if (tty_clamp_line(tty
, ctx
, px
, py
, nx
, &i
, &x
, &rx
, &ry
))
1193 tty_clear_line(tty
, &ctx
->defaults
, ry
, x
, rx
, bg
);
1196 /* Clamp area position to visible part of pane. */
1198 tty_clamp_area(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int px
, u_int py
,
1199 u_int nx
, u_int ny
, u_int
*i
, u_int
*j
, u_int
*x
, u_int
*y
, u_int
*rx
,
1202 u_int xoff
= ctx
->rxoff
+ px
, yoff
= ctx
->ryoff
+ py
;
1204 if (!tty_is_visible(tty
, ctx
, px
, py
, nx
, ny
))
1207 if (xoff
>= ctx
->wox
&& xoff
+ nx
<= ctx
->wox
+ ctx
->wsx
) {
1210 *x
= ctx
->xoff
+ px
- ctx
->wox
;
1212 } else if (xoff
< ctx
->wox
&& xoff
+ nx
> ctx
->wox
+ ctx
->wsx
) {
1213 /* Both left and right not visible. */
1217 } else if (xoff
< ctx
->wox
) {
1218 /* Left not visible. */
1219 *i
= ctx
->wox
- (ctx
->xoff
+ px
);
1223 /* Right not visible. */
1225 *x
= (ctx
->xoff
+ px
) - ctx
->wox
;
1226 *rx
= ctx
->wsx
- *x
;
1229 fatalx("%s: x too big, %u > %u", __func__
, *rx
, nx
);
1231 if (yoff
>= ctx
->woy
&& yoff
+ ny
<= ctx
->woy
+ ctx
->wsy
) {
1234 *y
= ctx
->yoff
+ py
- ctx
->woy
;
1236 } else if (yoff
< ctx
->woy
&& yoff
+ ny
> ctx
->woy
+ ctx
->wsy
) {
1237 /* Both top and bottom not visible. */
1241 } else if (yoff
< ctx
->woy
) {
1242 /* Top not visible. */
1243 *j
= ctx
->woy
- (ctx
->yoff
+ py
);
1247 /* Bottom not visible. */
1249 *y
= (ctx
->yoff
+ py
) - ctx
->woy
;
1250 *ry
= ctx
->wsy
- *y
;
1253 fatalx("%s: y too big, %u > %u", __func__
, *ry
, ny
);
1258 /* Clear an area, adjusting to visible part of pane. */
1260 tty_clear_area(struct tty
*tty
, const struct grid_cell
*defaults
, u_int py
,
1261 u_int ny
, u_int px
, u_int nx
, u_int bg
)
1263 struct client
*c
= tty
->client
;
1267 log_debug("%s: %s, %u,%u at %u,%u", __func__
, c
->name
, nx
, ny
, px
, py
);
1269 /* Nothing to clear. */
1270 if (nx
== 0 || ny
== 0)
1273 /* If genuine BCE is available, can try escape sequences. */
1274 if (c
->overlay_check
== NULL
&& !tty_fake_bce(tty
, defaults
, bg
)) {
1275 /* Use ED if clearing off the bottom of the terminal. */
1277 px
+ nx
>= tty
->sx
&&
1278 py
+ ny
>= tty
->sy
&&
1279 tty_term_has(tty
->term
, TTYC_ED
)) {
1280 tty_cursor(tty
, 0, py
);
1281 tty_putcode(tty
, TTYC_ED
);
1286 * On VT420 compatible terminals we can use DECFRA if the
1287 * background colour isn't default (because it doesn't work
1290 if ((tty
->term
->flags
& TERM_DECFRA
) && !COLOUR_DEFAULT(bg
)) {
1291 xsnprintf(tmp
, sizeof tmp
, "\033[32;%u;%u;%u;%u$x",
1292 py
+ 1, px
+ 1, py
+ ny
, px
+ nx
);
1297 /* Full lines can be scrolled away to clear them. */
1299 px
+ nx
>= tty
->sx
&&
1301 tty_term_has(tty
->term
, TTYC_CSR
) &&
1302 tty_term_has(tty
->term
, TTYC_INDN
)) {
1303 tty_region(tty
, py
, py
+ ny
- 1);
1304 tty_margin_off(tty
);
1305 tty_putcode_i(tty
, TTYC_INDN
, ny
);
1310 * If margins are supported, can just scroll the area off to
1315 tty_term_has(tty
->term
, TTYC_CSR
) &&
1316 tty_use_margin(tty
) &&
1317 tty_term_has(tty
->term
, TTYC_INDN
)) {
1318 tty_region(tty
, py
, py
+ ny
- 1);
1319 tty_margin(tty
, px
, px
+ nx
- 1);
1320 tty_putcode_i(tty
, TTYC_INDN
, ny
);
1325 /* Couldn't use an escape sequence, loop over the lines. */
1326 for (yy
= py
; yy
< py
+ ny
; yy
++)
1327 tty_clear_line(tty
, defaults
, yy
, px
, nx
, bg
);
1330 /* Clear an area in a pane. */
1332 tty_clear_pane_area(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int py
,
1333 u_int ny
, u_int px
, u_int nx
, u_int bg
)
1335 u_int i
, j
, x
, y
, rx
, ry
;
1337 if (tty_clamp_area(tty
, ctx
, px
, py
, nx
, ny
, &i
, &j
, &x
, &y
, &rx
, &ry
))
1338 tty_clear_area(tty
, &ctx
->defaults
, y
, ry
, x
, rx
, bg
);
1342 tty_draw_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int py
)
1344 struct screen
*s
= ctx
->s
;
1345 u_int nx
= ctx
->sx
, i
, x
, rx
, ry
;
1347 log_debug("%s: %s %u %d", __func__
, tty
->client
->name
, py
, ctx
->bigger
);
1350 tty_draw_line(tty
, s
, 0, py
, nx
, ctx
->xoff
, ctx
->yoff
+ py
,
1351 &ctx
->defaults
, ctx
->palette
);
1354 if (tty_clamp_line(tty
, ctx
, 0, py
, nx
, &i
, &x
, &rx
, &ry
)) {
1355 tty_draw_line(tty
, s
, i
, py
, rx
, x
, ry
, &ctx
->defaults
,
1360 static const struct grid_cell
*
1361 tty_check_codeset(struct tty
*tty
, const struct grid_cell
*gc
)
1363 static struct grid_cell
new;
1366 /* Characters less than 0x7f are always fine, no matter what. */
1367 if (gc
->data
.size
== 1 && *gc
->data
.data
< 0x7f)
1369 if (gc
->flags
& GRID_FLAG_TAB
)
1372 /* UTF-8 terminal and a UTF-8 character - fine. */
1373 if (tty
->client
->flags
& CLIENT_UTF8
)
1375 memcpy(&new, gc
, sizeof new);
1377 /* See if this can be mapped to an ACS character. */
1378 c
= tty_acs_reverse_get(tty
, gc
->data
.data
, gc
->data
.size
);
1380 utf8_set(&new.data
, c
);
1381 new.attr
|= GRID_ATTR_CHARSET
;
1385 /* Replace by the right number of underscores. */
1386 new.data
.size
= gc
->data
.width
;
1387 if (new.data
.size
> UTF8_SIZE
)
1388 new.data
.size
= UTF8_SIZE
;
1389 memset(new.data
.data
, '_', new.data
.size
);
1394 * Check if a single character is obstructed by the overlay and return a
1398 tty_check_overlay(struct tty
*tty
, u_int px
, u_int py
)
1400 struct overlay_ranges r
;
1403 * A unit width range will always return nx[2] == 0 from a check, even
1404 * with multiple overlays, so it's sufficient to check just the first
1407 tty_check_overlay_range(tty
, px
, py
, 1, &r
);
1408 if (r
.nx
[0] + r
.nx
[1] == 0)
1413 /* Return parts of the input range which are visible. */
1415 tty_check_overlay_range(struct tty
*tty
, u_int px
, u_int py
, u_int nx
,
1416 struct overlay_ranges
*r
)
1418 struct client
*c
= tty
->client
;
1420 if (c
->overlay_check
== NULL
) {
1430 c
->overlay_check(c
, c
->overlay_data
, px
, py
, nx
, r
);
1434 tty_draw_line(struct tty
*tty
, struct screen
*s
, u_int px
, u_int py
, u_int nx
,
1435 u_int atx
, u_int aty
, const struct grid_cell
*defaults
,
1436 struct colour_palette
*palette
)
1438 struct grid
*gd
= s
->grid
;
1439 struct grid_cell gc
, last
;
1440 const struct grid_cell
*gcp
;
1441 struct grid_line
*gl
;
1442 struct client
*c
= tty
->client
;
1443 struct overlay_ranges r
;
1444 u_int i
, j
, ux
, sx
, width
, hidden
, eux
, nxx
;
1446 int flags
, cleared
= 0, wrapped
= 0;
1450 log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__
,
1451 px
, py
, nx
, atx
, aty
);
1452 log_debug("%s: defaults: fg=%d, bg=%d", __func__
, defaults
->fg
,
1456 * py is the line in the screen to draw.
1457 * px is the start x and nx is the width to draw.
1458 * atx,aty is the line on the terminal to draw it.
1461 flags
= (tty
->flags
& TTY_NOCURSOR
);
1462 tty
->flags
|= TTY_NOCURSOR
;
1463 tty_update_mode(tty
, tty
->mode
, s
);
1465 tty_region_off(tty
);
1466 tty_margin_off(tty
);
1469 * Clamp the width to cellsize - note this is not cellused, because
1470 * there may be empty background cells after it (from BCE).
1472 sx
= screen_size_x(s
);
1475 cellsize
= grid_get_line(gd
, gd
->hsize
+ py
)->cellsize
;
1487 gl
= grid_get_line(gd
, gd
->hsize
+ py
- 1);
1489 (~gl
->flags
& GRID_LINE_WRAPPED
) ||
1491 tty
->cx
< tty
->sx
||
1496 tty_term_has(tty
->term
, TTYC_EL1
) &&
1497 !tty_fake_bce(tty
, defaults
, 8) &&
1498 c
->overlay_check
== NULL
) {
1499 tty_default_attributes(tty
, defaults
, palette
, 8,
1501 tty_cursor(tty
, nx
- 1, aty
);
1502 tty_putcode(tty
, TTYC_EL1
);
1506 log_debug("%s: wrapped line %u", __func__
, aty
);
1510 memcpy(&last
, &grid_default_cell
, sizeof last
);
1514 for (i
= 0; i
< sx
; i
++) {
1515 grid_view_get_cell(gd
, px
+ i
, py
, &gc
);
1516 gcp
= tty_check_codeset(tty
, &gc
);
1518 (!tty_check_overlay(tty
, atx
+ ux
+ width
, aty
) ||
1519 (gcp
->attr
& GRID_ATTR_CHARSET
) ||
1520 gcp
->flags
!= last
.flags
||
1521 gcp
->attr
!= last
.attr
||
1522 gcp
->fg
!= last
.fg
||
1523 gcp
->bg
!= last
.bg
||
1524 gcp
->us
!= last
.us
||
1525 gcp
->link
!= last
.link
||
1526 ux
+ width
+ gcp
->data
.width
> nx
||
1527 (sizeof buf
) - len
< gcp
->data
.size
)) {
1528 tty_attributes(tty
, &last
, defaults
, palette
,
1530 if (last
.flags
& GRID_FLAG_CLEARED
) {
1531 log_debug("%s: %zu cleared", __func__
, len
);
1532 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
,
1535 if (!wrapped
|| atx
!= 0 || ux
!= 0)
1536 tty_cursor(tty
, atx
+ ux
, aty
);
1537 tty_putn(tty
, buf
, len
, width
);
1546 if (gcp
->flags
& GRID_FLAG_SELECTED
)
1547 screen_select_cell(s
, &last
, gcp
);
1549 memcpy(&last
, gcp
, sizeof last
);
1551 tty_check_overlay_range(tty
, atx
+ ux
, aty
, gcp
->data
.width
,
1554 for (j
= 0; j
< OVERLAY_MAX_RANGES
; j
++)
1556 hidden
= gcp
->data
.width
- hidden
;
1557 if (hidden
!= 0 && hidden
== gcp
->data
.width
) {
1558 if (~gcp
->flags
& GRID_FLAG_PADDING
)
1559 ux
+= gcp
->data
.width
;
1560 } else if (hidden
!= 0 || ux
+ gcp
->data
.width
> nx
) {
1561 if (~gcp
->flags
& GRID_FLAG_PADDING
) {
1562 tty_attributes(tty
, &last
, defaults
, palette
,
1564 for (j
= 0; j
< OVERLAY_MAX_RANGES
; j
++) {
1567 /* Effective width drawn so far. */
1568 eux
= r
.px
[j
] - atx
;
1570 tty_cursor(tty
, r
.px
[j
], aty
);
1574 tty_repeat_space(tty
, r
.nx
[j
]);
1579 } else if (gcp
->attr
& GRID_ATTR_CHARSET
) {
1580 tty_attributes(tty
, &last
, defaults
, palette
,
1582 tty_cursor(tty
, atx
+ ux
, aty
);
1583 for (j
= 0; j
< gcp
->data
.size
; j
++)
1584 tty_putc(tty
, gcp
->data
.data
[j
]);
1585 ux
+= gcp
->data
.width
;
1586 } else if (~gcp
->flags
& GRID_FLAG_PADDING
) {
1587 memcpy(buf
+ len
, gcp
->data
.data
, gcp
->data
.size
);
1588 len
+= gcp
->data
.size
;
1589 width
+= gcp
->data
.width
;
1592 if (len
!= 0 && ((~last
.flags
& GRID_FLAG_CLEARED
) || last
.bg
!= 8)) {
1593 tty_attributes(tty
, &last
, defaults
, palette
, s
->hyperlinks
);
1594 if (last
.flags
& GRID_FLAG_CLEARED
) {
1595 log_debug("%s: %zu cleared (end)", __func__
, len
);
1596 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
, width
,
1599 if (!wrapped
|| atx
!= 0 || ux
!= 0)
1600 tty_cursor(tty
, atx
+ ux
, aty
);
1601 tty_putn(tty
, buf
, len
, width
);
1606 if (!cleared
&& ux
< nx
) {
1607 log_debug("%s: %u to end of line (%zu cleared)", __func__
,
1609 tty_default_attributes(tty
, defaults
, palette
, 8,
1611 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
, nx
- ux
, 8);
1614 tty
->flags
= (tty
->flags
& ~TTY_NOCURSOR
) | flags
;
1615 tty_update_mode(tty
, tty
->mode
, s
);
1619 /* Update context for client. */
1621 tty_set_client_cb(struct tty_ctx
*ttyctx
, struct client
*c
)
1623 struct window_pane
*wp
= ttyctx
->arg
;
1625 if (c
->session
->curw
->window
!= wp
->window
)
1627 if (wp
->layout_cell
== NULL
)
1630 /* Set the properties relevant to the current client. */
1631 ttyctx
->bigger
= tty_window_offset(&c
->tty
, &ttyctx
->wox
, &ttyctx
->woy
,
1632 &ttyctx
->wsx
, &ttyctx
->wsy
);
1634 ttyctx
->yoff
= ttyctx
->ryoff
= wp
->yoff
;
1635 if (status_at_line(c
) == 0)
1636 ttyctx
->yoff
+= status_line_size(c
);
1642 tty_draw_images(struct client
*c
, struct window_pane
*wp
, struct screen
*s
)
1645 struct tty_ctx ttyctx
;
1647 TAILQ_FOREACH(im
, &s
->images
, entry
) {
1648 memset(&ttyctx
, 0, sizeof ttyctx
);
1650 /* Set the client independent properties. */
1651 ttyctx
.ocx
= im
->px
;
1652 ttyctx
.ocy
= im
->py
;
1654 ttyctx
.orlower
= s
->rlower
;
1655 ttyctx
.orupper
= s
->rupper
;
1657 ttyctx
.xoff
= ttyctx
.rxoff
= wp
->xoff
;
1663 ttyctx
.set_client_cb
= tty_set_client_cb
;
1664 ttyctx
.allow_invisible_panes
= 1;
1665 tty_write_one(tty_cmd_sixelimage
, c
, &ttyctx
);
1671 tty_sync_start(struct tty
*tty
)
1673 if (tty
->flags
& TTY_BLOCK
)
1675 if (tty
->flags
& TTY_SYNCING
)
1677 tty
->flags
|= TTY_SYNCING
;
1679 if (tty_term_has(tty
->term
, TTYC_SYNC
)) {
1680 log_debug("%s sync start", tty
->client
->name
);
1681 tty_putcode_i(tty
, TTYC_SYNC
, 1);
1686 tty_sync_end(struct tty
*tty
)
1688 if (tty
->flags
& TTY_BLOCK
)
1690 if (~tty
->flags
& TTY_SYNCING
)
1692 tty
->flags
&= ~TTY_SYNCING
;
1694 if (tty_term_has(tty
->term
, TTYC_SYNC
)) {
1695 log_debug("%s sync end", tty
->client
->name
);
1696 tty_putcode_i(tty
, TTYC_SYNC
, 2);
1701 tty_client_ready(const struct tty_ctx
*ctx
, struct client
*c
)
1703 if (c
->session
== NULL
|| c
->tty
.term
== NULL
)
1705 if (c
->flags
& CLIENT_SUSPENDED
)
1709 * If invisible panes are allowed (used for passthrough), don't care if
1710 * redrawing or frozen.
1712 if (ctx
->allow_invisible_panes
)
1715 if (c
->flags
& CLIENT_REDRAWWINDOW
)
1717 if (c
->tty
.flags
& TTY_FREEZE
)
1723 tty_write(void (*cmdfn
)(struct tty
*, const struct tty_ctx
*),
1724 struct tty_ctx
*ctx
)
1729 if (ctx
->set_client_cb
== NULL
)
1731 TAILQ_FOREACH(c
, &clients
, entry
) {
1732 if (tty_client_ready(ctx
, c
)) {
1733 state
= ctx
->set_client_cb(ctx
, c
);
1738 cmdfn(&c
->tty
, ctx
);
1744 /* Only write to the incoming tty instead of every client. */
1746 tty_write_one(void (*cmdfn
)(struct tty
*, const struct tty_ctx
*),
1747 struct client
*c
, struct tty_ctx
*ctx
)
1749 if (ctx
->set_client_cb
== NULL
)
1751 if ((ctx
->set_client_cb(ctx
, c
)) == 1)
1752 cmdfn(&c
->tty
, ctx
);
1757 tty_cmd_insertcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1759 struct client
*c
= tty
->client
;
1762 !tty_full_width(tty
, ctx
) ||
1763 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1764 (!tty_term_has(tty
->term
, TTYC_ICH
) &&
1765 !tty_term_has(tty
->term
, TTYC_ICH1
)) ||
1766 c
->overlay_check
!= NULL
) {
1767 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
1771 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1772 ctx
->s
->hyperlinks
);
1774 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1776 tty_emulate_repeat(tty
, TTYC_ICH
, TTYC_ICH1
, ctx
->num
);
1780 tty_cmd_deletecharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1782 struct client
*c
= tty
->client
;
1785 !tty_full_width(tty
, ctx
) ||
1786 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1787 (!tty_term_has(tty
->term
, TTYC_DCH
) &&
1788 !tty_term_has(tty
->term
, TTYC_DCH1
)) ||
1789 c
->overlay_check
!= NULL
) {
1790 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
1794 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1795 ctx
->s
->hyperlinks
);
1797 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1799 tty_emulate_repeat(tty
, TTYC_DCH
, TTYC_DCH1
, ctx
->num
);
1803 tty_cmd_clearcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1805 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1806 ctx
->s
->hyperlinks
);
1808 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, ctx
->ocx
, ctx
->num
, ctx
->bg
);
1812 tty_cmd_insertline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1814 struct client
*c
= tty
->client
;
1817 !tty_full_width(tty
, ctx
) ||
1818 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1819 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1820 !tty_term_has(tty
->term
, TTYC_IL1
) ||
1823 c
->overlay_check
!= NULL
) {
1824 tty_redraw_region(tty
, ctx
);
1828 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1829 ctx
->s
->hyperlinks
);
1831 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1832 tty_margin_off(tty
);
1833 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1835 tty_emulate_repeat(tty
, TTYC_IL
, TTYC_IL1
, ctx
->num
);
1836 tty
->cx
= tty
->cy
= UINT_MAX
;
1840 tty_cmd_deleteline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1842 struct client
*c
= tty
->client
;
1845 !tty_full_width(tty
, ctx
) ||
1846 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1847 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1848 !tty_term_has(tty
->term
, TTYC_DL1
) ||
1851 c
->overlay_check
!= NULL
) {
1852 tty_redraw_region(tty
, ctx
);
1856 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1857 ctx
->s
->hyperlinks
);
1859 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1860 tty_margin_off(tty
);
1861 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1863 tty_emulate_repeat(tty
, TTYC_DL
, TTYC_DL1
, ctx
->num
);
1864 tty
->cx
= tty
->cy
= UINT_MAX
;
1868 tty_cmd_clearline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1870 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1871 ctx
->s
->hyperlinks
);
1873 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, 0, ctx
->sx
, ctx
->bg
);
1877 tty_cmd_clearendofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1879 u_int nx
= ctx
->sx
- ctx
->ocx
;
1881 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1882 ctx
->s
->hyperlinks
);
1884 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, ctx
->ocx
, nx
, ctx
->bg
);
1888 tty_cmd_clearstartofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1890 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1891 ctx
->s
->hyperlinks
);
1893 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, 0, ctx
->ocx
+ 1, ctx
->bg
);
1897 tty_cmd_reverseindex(struct tty
*tty
, const struct tty_ctx
*ctx
)
1899 struct client
*c
= tty
->client
;
1901 if (ctx
->ocy
!= ctx
->orupper
)
1905 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1906 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1907 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1908 (!tty_term_has(tty
->term
, TTYC_RI
) &&
1909 !tty_term_has(tty
->term
, TTYC_RIN
)) ||
1912 c
->overlay_check
!= NULL
) {
1913 tty_redraw_region(tty
, ctx
);
1917 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1918 ctx
->s
->hyperlinks
);
1920 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1921 tty_margin_pane(tty
, ctx
);
1922 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
1924 if (tty_term_has(tty
->term
, TTYC_RI
))
1925 tty_putcode(tty
, TTYC_RI
);
1927 tty_putcode_i(tty
, TTYC_RIN
, 1);
1931 tty_cmd_linefeed(struct tty
*tty
, const struct tty_ctx
*ctx
)
1933 struct client
*c
= tty
->client
;
1935 if (ctx
->ocy
!= ctx
->orlower
)
1939 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1940 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1941 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1944 c
->overlay_check
!= NULL
) {
1945 tty_redraw_region(tty
, ctx
);
1949 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1950 ctx
->s
->hyperlinks
);
1952 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1953 tty_margin_pane(tty
, ctx
);
1956 * If we want to wrap a pane while using margins, the cursor needs to
1957 * be exactly on the right of the region. If the cursor is entirely off
1958 * the edge - move it back to the right. Some terminals are funny about
1959 * this and insert extra spaces, so only use the right if margins are
1962 if (ctx
->xoff
+ ctx
->ocx
> tty
->rright
) {
1963 if (!tty_use_margin(tty
))
1964 tty_cursor(tty
, 0, ctx
->yoff
+ ctx
->ocy
);
1966 tty_cursor(tty
, tty
->rright
, ctx
->yoff
+ ctx
->ocy
);
1968 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1970 tty_putc(tty
, '\n');
1974 tty_cmd_scrollup(struct tty
*tty
, const struct tty_ctx
*ctx
)
1976 struct client
*c
= tty
->client
;
1980 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1981 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1982 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1985 c
->overlay_check
!= NULL
) {
1986 tty_redraw_region(tty
, ctx
);
1990 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1991 ctx
->s
->hyperlinks
);
1993 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1994 tty_margin_pane(tty
, ctx
);
1996 if (ctx
->num
== 1 || !tty_term_has(tty
->term
, TTYC_INDN
)) {
1997 if (!tty_use_margin(tty
))
1998 tty_cursor(tty
, 0, tty
->rlower
);
2000 tty_cursor(tty
, tty
->rright
, tty
->rlower
);
2001 for (i
= 0; i
< ctx
->num
; i
++)
2002 tty_putc(tty
, '\n');
2004 if (tty
->cy
== UINT_MAX
)
2005 tty_cursor(tty
, 0, 0);
2007 tty_cursor(tty
, 0, tty
->cy
);
2008 tty_putcode_i(tty
, TTYC_INDN
, ctx
->num
);
2013 tty_cmd_scrolldown(struct tty
*tty
, const struct tty_ctx
*ctx
)
2016 struct client
*c
= tty
->client
;
2019 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
2020 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
2021 !tty_term_has(tty
->term
, TTYC_CSR
) ||
2022 (!tty_term_has(tty
->term
, TTYC_RI
) &&
2023 !tty_term_has(tty
->term
, TTYC_RIN
)) ||
2026 c
->overlay_check
!= NULL
) {
2027 tty_redraw_region(tty
, ctx
);
2031 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
2032 ctx
->s
->hyperlinks
);
2034 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
2035 tty_margin_pane(tty
, ctx
);
2036 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
2038 if (tty_term_has(tty
->term
, TTYC_RIN
))
2039 tty_putcode_i(tty
, TTYC_RIN
, ctx
->num
);
2041 for (i
= 0; i
< ctx
->num
; i
++)
2042 tty_putcode(tty
, TTYC_RI
);
2047 tty_cmd_clearendofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
2049 u_int px
, py
, nx
, ny
;
2051 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
2052 ctx
->s
->hyperlinks
);
2054 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
2055 tty_margin_off(tty
);
2060 ny
= ctx
->sy
- ctx
->ocy
- 1;
2062 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
2065 nx
= ctx
->sx
- ctx
->ocx
;
2068 tty_clear_pane_line(tty
, ctx
, py
, px
, nx
, ctx
->bg
);
2072 tty_cmd_clearstartofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
2074 u_int px
, py
, nx
, ny
;
2076 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
2077 ctx
->s
->hyperlinks
);
2079 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
2080 tty_margin_off(tty
);
2087 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
2093 tty_clear_pane_line(tty
, ctx
, py
, px
, nx
, ctx
->bg
);
2097 tty_cmd_clearscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
2099 u_int px
, py
, nx
, ny
;
2101 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
2102 ctx
->s
->hyperlinks
);
2104 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
2105 tty_margin_off(tty
);
2112 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
2116 tty_cmd_alignmenttest(struct tty
*tty
, const struct tty_ctx
*ctx
)
2121 ctx
->redraw_cb(ctx
);
2125 tty_attributes(tty
, &grid_default_cell
, &ctx
->defaults
, ctx
->palette
,
2126 ctx
->s
->hyperlinks
);
2128 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
2129 tty_margin_off(tty
);
2131 for (j
= 0; j
< ctx
->sy
; j
++) {
2132 tty_cursor_pane(tty
, ctx
, 0, j
);
2133 for (i
= 0; i
< ctx
->sx
; i
++)
2139 tty_cmd_cell(struct tty
*tty
, const struct tty_ctx
*ctx
)
2141 const struct grid_cell
*gcp
= ctx
->cell
;
2142 struct screen
*s
= ctx
->s
;
2143 struct overlay_ranges r
;
2144 u_int px
, py
, i
, vis
= 0;
2146 px
= ctx
->xoff
+ ctx
->ocx
- ctx
->wox
;
2147 py
= ctx
->yoff
+ ctx
->ocy
- ctx
->woy
;
2148 if (!tty_is_visible(tty
, ctx
, ctx
->ocx
, ctx
->ocy
, 1, 1) ||
2149 (gcp
->data
.width
== 1 && !tty_check_overlay(tty
, px
, py
)))
2152 /* Handle partially obstructed wide characters. */
2153 if (gcp
->data
.width
> 1) {
2154 tty_check_overlay_range(tty
, px
, py
, gcp
->data
.width
, &r
);
2155 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++)
2157 if (vis
< gcp
->data
.width
) {
2158 tty_draw_line(tty
, s
, s
->cx
, s
->cy
, gcp
->data
.width
,
2159 px
, py
, &ctx
->defaults
, ctx
->palette
);
2164 if (ctx
->xoff
+ ctx
->ocx
- ctx
->wox
> tty
->sx
- 1 &&
2165 ctx
->ocy
== ctx
->orlower
&&
2166 tty_full_width(tty
, ctx
))
2167 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
2169 tty_margin_off(tty
);
2170 tty_cursor_pane_unless_wrap(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
2172 tty_cell(tty
, ctx
->cell
, &ctx
->defaults
, ctx
->palette
,
2173 ctx
->s
->hyperlinks
);
2176 tty_invalidate(tty
);
2180 tty_cmd_cells(struct tty
*tty
, const struct tty_ctx
*ctx
)
2182 struct overlay_ranges r
;
2183 u_int i
, px
, py
, cx
;
2184 char *cp
= ctx
->ptr
;
2186 if (!tty_is_visible(tty
, ctx
, ctx
->ocx
, ctx
->ocy
, ctx
->num
, 1))
2190 (ctx
->xoff
+ ctx
->ocx
< ctx
->wox
||
2191 ctx
->xoff
+ ctx
->ocx
+ ctx
->num
> ctx
->wox
+ ctx
->wsx
)) {
2192 if (!ctx
->wrapped
||
2193 !tty_full_width(tty
, ctx
) ||
2194 (tty
->term
->flags
& TERM_NOAM
) ||
2195 ctx
->xoff
+ ctx
->ocx
!= 0 ||
2196 ctx
->yoff
+ ctx
->ocy
!= tty
->cy
+ 1 ||
2197 tty
->cx
< tty
->sx
||
2198 tty
->cy
== tty
->rlower
)
2199 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
2201 ctx
->redraw_cb(ctx
);
2205 tty_margin_off(tty
);
2206 tty_cursor_pane_unless_wrap(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
2207 tty_attributes(tty
, ctx
->cell
, &ctx
->defaults
, ctx
->palette
, ctx
->s
->hyperlinks
);
2209 /* Get tty position from pane position for overlay check. */
2210 px
= ctx
->xoff
+ ctx
->ocx
- ctx
->wox
;
2211 py
= ctx
->yoff
+ ctx
->ocy
- ctx
->woy
;
2213 tty_check_overlay_range(tty
, px
, py
, ctx
->num
, &r
);
2214 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++) {
2217 /* Convert back to pane position for printing. */
2218 cx
= r
.px
[i
] - ctx
->xoff
+ ctx
->wox
;
2219 tty_cursor_pane_unless_wrap(tty
, ctx
, cx
, ctx
->ocy
);
2220 tty_putn(tty
, cp
+ r
.px
[i
] - px
, r
.nx
[i
], r
.nx
[i
]);
2225 tty_cmd_setselection(struct tty
*tty
, const struct tty_ctx
*ctx
)
2227 tty_set_selection(tty
, ctx
->ptr2
, ctx
->ptr
, ctx
->num
);
2231 tty_set_selection(struct tty
*tty
, const char *flags
, const char *buf
,
2237 if (~tty
->flags
& TTY_STARTED
)
2239 if (!tty_term_has(tty
->term
, TTYC_MS
))
2242 size
= 4 * ((len
+ 2) / 3) + 1; /* storage for base64 */
2243 encoded
= xmalloc(size
);
2245 b64_ntop(buf
, len
, encoded
, size
);
2246 tty
->flags
|= TTY_NOBLOCK
;
2247 tty_putcode_ss(tty
, TTYC_MS
, flags
, encoded
);
2253 tty_cmd_rawstring(struct tty
*tty
, const struct tty_ctx
*ctx
)
2255 tty
->flags
|= TTY_NOBLOCK
;
2256 tty_add(tty
, ctx
->ptr
, ctx
->num
);
2257 tty_invalidate(tty
);
2262 tty_cmd_sixelimage(struct tty
*tty
, const struct tty_ctx
*ctx
)
2264 struct image
*im
= ctx
->ptr
;
2265 struct sixel_image
*si
= im
->data
;
2266 struct sixel_image
*new;
2269 u_int cx
= ctx
->ocx
, cy
= ctx
->ocy
, sx
, sy
;
2270 u_int i
, j
, x
, y
, rx
, ry
;
2273 if ((~tty
->term
->flags
& TERM_SIXEL
) &&
2274 !tty_term_has(tty
->term
, TTYC_SXL
))
2276 if (tty
->xpixel
== 0 || tty
->ypixel
== 0)
2279 sixel_size_in_cells(si
, &sx
, &sy
);
2280 log_debug("%s: image is %ux%u", __func__
, sx
, sy
);
2281 if (!tty_clamp_area(tty
, ctx
, cx
, cy
, sx
, sy
, &i
, &j
, &x
, &y
, &rx
, &ry
))
2283 log_debug("%s: clamping to %u,%u-%u,%u", __func__
, i
, j
, rx
, ry
);
2285 if (fallback
== 1) {
2286 data
= xstrdup(im
->fallback
);
2287 size
= strlen(data
);
2289 new = sixel_scale(si
, tty
->xpixel
, tty
->ypixel
, i
, j
, rx
, ry
, 0);
2293 data
= sixel_print(new, si
, &size
);
2296 log_debug("%s: %zu bytes: %s", __func__
, size
, data
);
2297 tty_region_off(tty
);
2298 tty_margin_off(tty
);
2299 tty_cursor(tty
, x
, y
);
2301 tty
->flags
|= TTY_NOBLOCK
;
2302 tty_add(tty
, data
, size
);
2303 tty_invalidate(tty
);
2313 tty_cmd_syncstart(struct tty
*tty
, const struct tty_ctx
*ctx
)
2315 if (ctx
->num
== 0x11) {
2317 * This is an overlay and a command that moves the cursor so
2318 * start synchronized updates.
2320 tty_sync_start(tty
);
2321 } else if (~ctx
->num
& 0x10) {
2323 * This is a pane. If there is an overlay, always start;
2324 * otherwise, only if requested.
2326 if (ctx
->num
|| tty
->client
->overlay_draw
!= NULL
)
2327 tty_sync_start(tty
);
2332 tty_cell(struct tty
*tty
, const struct grid_cell
*gc
,
2333 const struct grid_cell
*defaults
, struct colour_palette
*palette
,
2334 struct hyperlinks
*hl
)
2336 const struct grid_cell
*gcp
;
2338 /* Skip last character if terminal is stupid. */
2339 if ((tty
->term
->flags
& TERM_NOAM
) &&
2340 tty
->cy
== tty
->sy
- 1 &&
2341 tty
->cx
== tty
->sx
- 1)
2344 /* If this is a padding character, do nothing. */
2345 if (gc
->flags
& GRID_FLAG_PADDING
)
2348 /* Check the output codeset and apply attributes. */
2349 gcp
= tty_check_codeset(tty
, gc
);
2350 tty_attributes(tty
, gcp
, defaults
, palette
, hl
);
2352 /* If it is a single character, write with putc to handle ACS. */
2353 if (gcp
->data
.size
== 1) {
2354 tty_attributes(tty
, gcp
, defaults
, palette
, hl
);
2355 if (*gcp
->data
.data
< 0x20 || *gcp
->data
.data
== 0x7f)
2357 tty_putc(tty
, *gcp
->data
.data
);
2361 /* Write the data. */
2362 tty_putn(tty
, gcp
->data
.data
, gcp
->data
.size
, gcp
->data
.width
);
2366 tty_reset(struct tty
*tty
)
2368 struct grid_cell
*gc
= &tty
->cell
;
2370 if (!grid_cells_equal(gc
, &grid_default_cell
)) {
2372 tty_putcode_ss(tty
, TTYC_HLS
, "", "");
2373 if ((gc
->attr
& GRID_ATTR_CHARSET
) && tty_acs_needed(tty
))
2374 tty_putcode(tty
, TTYC_RMACS
);
2375 tty_putcode(tty
, TTYC_SGR0
);
2376 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2378 memcpy(&tty
->last_cell
, &grid_default_cell
, sizeof tty
->last_cell
);
2382 tty_invalidate(struct tty
*tty
)
2384 memcpy(&tty
->cell
, &grid_default_cell
, sizeof tty
->cell
);
2385 memcpy(&tty
->last_cell
, &grid_default_cell
, sizeof tty
->last_cell
);
2387 tty
->cx
= tty
->cy
= UINT_MAX
;
2388 tty
->rupper
= tty
->rleft
= UINT_MAX
;
2389 tty
->rlower
= tty
->rright
= UINT_MAX
;
2391 if (tty
->flags
& TTY_STARTED
) {
2392 if (tty_use_margin(tty
))
2393 tty_putcode(tty
, TTYC_ENMG
);
2394 tty_putcode(tty
, TTYC_SGR0
);
2396 tty
->mode
= ALL_MODES
;
2397 tty_update_mode(tty
, MODE_CURSOR
, NULL
);
2399 tty_cursor(tty
, 0, 0);
2400 tty_region_off(tty
);
2401 tty_margin_off(tty
);
2403 tty
->mode
= MODE_CURSOR
;
2406 /* Turn off margin. */
2408 tty_region_off(struct tty
*tty
)
2410 tty_region(tty
, 0, tty
->sy
- 1);
2413 /* Set region inside pane. */
2415 tty_region_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int rupper
,
2418 tty_region(tty
, ctx
->yoff
+ rupper
- ctx
->woy
,
2419 ctx
->yoff
+ rlower
- ctx
->woy
);
2422 /* Set region at absolute position. */
2424 tty_region(struct tty
*tty
, u_int rupper
, u_int rlower
)
2426 if (tty
->rlower
== rlower
&& tty
->rupper
== rupper
)
2428 if (!tty_term_has(tty
->term
, TTYC_CSR
))
2431 tty
->rupper
= rupper
;
2432 tty
->rlower
= rlower
;
2435 * Some terminals (such as PuTTY) do not correctly reset the cursor to
2436 * 0,0 if it is beyond the last column (they do not reset their wrap
2437 * flag so further output causes a line feed). As a workaround, do an
2438 * explicit move to 0 first.
2440 if (tty
->cx
>= tty
->sx
) {
2441 if (tty
->cy
== UINT_MAX
)
2442 tty_cursor(tty
, 0, 0);
2444 tty_cursor(tty
, 0, tty
->cy
);
2447 tty_putcode_ii(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
2448 tty
->cx
= tty
->cy
= UINT_MAX
;
2451 /* Turn off margin. */
2453 tty_margin_off(struct tty
*tty
)
2455 tty_margin(tty
, 0, tty
->sx
- 1);
2458 /* Set margin inside pane. */
2460 tty_margin_pane(struct tty
*tty
, const struct tty_ctx
*ctx
)
2462 tty_margin(tty
, ctx
->xoff
- ctx
->wox
,
2463 ctx
->xoff
+ ctx
->sx
- 1 - ctx
->wox
);
2466 /* Set margin at absolute position. */
2468 tty_margin(struct tty
*tty
, u_int rleft
, u_int rright
)
2470 if (!tty_use_margin(tty
))
2472 if (tty
->rleft
== rleft
&& tty
->rright
== rright
)
2475 tty_putcode_ii(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
2478 tty
->rright
= rright
;
2480 if (rleft
== 0 && rright
== tty
->sx
- 1)
2481 tty_putcode(tty
, TTYC_CLMG
);
2483 tty_putcode_ii(tty
, TTYC_CMG
, rleft
, rright
);
2484 tty
->cx
= tty
->cy
= UINT_MAX
;
2488 * Move the cursor, unless it would wrap itself when the next character is
2492 tty_cursor_pane_unless_wrap(struct tty
*tty
, const struct tty_ctx
*ctx
,
2495 if (!ctx
->wrapped
||
2496 !tty_full_width(tty
, ctx
) ||
2497 (tty
->term
->flags
& TERM_NOAM
) ||
2498 ctx
->xoff
+ cx
!= 0 ||
2499 ctx
->yoff
+ cy
!= tty
->cy
+ 1 ||
2500 tty
->cx
< tty
->sx
||
2501 tty
->cy
== tty
->rlower
)
2502 tty_cursor_pane(tty
, ctx
, cx
, cy
);
2504 log_debug("%s: will wrap at %u,%u", __func__
, tty
->cx
, tty
->cy
);
2507 /* Move cursor inside pane. */
2509 tty_cursor_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int cx
, u_int cy
)
2511 tty_cursor(tty
, ctx
->xoff
+ cx
- ctx
->wox
, ctx
->yoff
+ cy
- ctx
->woy
);
2514 /* Move cursor to absolute position. */
2516 tty_cursor(struct tty
*tty
, u_int cx
, u_int cy
)
2518 struct tty_term
*term
= tty
->term
;
2522 if (tty
->flags
& TTY_BLOCK
)
2529 * If in the automargin space, and want to be there, do not move.
2530 * Otherwise, force the cursor to be in range (and complain).
2532 if (cx
== thisx
&& cy
== thisy
&& cx
== tty
->sx
)
2534 if (cx
> tty
->sx
- 1) {
2535 log_debug("%s: x too big %u > %u", __func__
, cx
, tty
->sx
- 1);
2540 if (cx
== thisx
&& cy
== thisy
)
2543 /* Currently at the very end of the line - use absolute movement. */
2544 if (thisx
> tty
->sx
- 1)
2547 /* Move to home position (0, 0). */
2548 if (cx
== 0 && cy
== 0 && tty_term_has(term
, TTYC_HOME
)) {
2549 tty_putcode(tty
, TTYC_HOME
);
2553 /* Zero on the next line. */
2554 if (cx
== 0 && cy
== thisy
+ 1 && thisy
!= tty
->rlower
&&
2555 (!tty_use_margin(tty
) || tty
->rleft
== 0)) {
2556 tty_putc(tty
, '\r');
2557 tty_putc(tty
, '\n');
2561 /* Moving column or row. */
2564 * Moving column only, row staying the same.
2568 if (cx
== 0 && (!tty_use_margin(tty
) || tty
->rleft
== 0)) {
2569 tty_putc(tty
, '\r');
2573 /* One to the left. */
2574 if (cx
== thisx
- 1 && tty_term_has(term
, TTYC_CUB1
)) {
2575 tty_putcode(tty
, TTYC_CUB1
);
2579 /* One to the right. */
2580 if (cx
== thisx
+ 1 && tty_term_has(term
, TTYC_CUF1
)) {
2581 tty_putcode(tty
, TTYC_CUF1
);
2585 /* Calculate difference. */
2586 change
= thisx
- cx
; /* +ve left, -ve right */
2589 * Use HPA if change is larger than absolute, otherwise move
2590 * the cursor with CUB/CUF.
2592 if ((u_int
) abs(change
) > cx
&& tty_term_has(term
, TTYC_HPA
)) {
2593 tty_putcode_i(tty
, TTYC_HPA
, cx
);
2595 } else if (change
> 0 &&
2596 tty_term_has(term
, TTYC_CUB
) &&
2597 !tty_use_margin(tty
)) {
2598 if (change
== 2 && tty_term_has(term
, TTYC_CUB1
)) {
2599 tty_putcode(tty
, TTYC_CUB1
);
2600 tty_putcode(tty
, TTYC_CUB1
);
2603 tty_putcode_i(tty
, TTYC_CUB
, change
);
2605 } else if (change
< 0 &&
2606 tty_term_has(term
, TTYC_CUF
) &&
2607 !tty_use_margin(tty
)) {
2608 tty_putcode_i(tty
, TTYC_CUF
, -change
);
2611 } else if (cx
== thisx
) {
2613 * Moving row only, column staying the same.
2617 if (thisy
!= tty
->rupper
&&
2618 cy
== thisy
- 1 && tty_term_has(term
, TTYC_CUU1
)) {
2619 tty_putcode(tty
, TTYC_CUU1
);
2624 if (thisy
!= tty
->rlower
&&
2625 cy
== thisy
+ 1 && tty_term_has(term
, TTYC_CUD1
)) {
2626 tty_putcode(tty
, TTYC_CUD1
);
2630 /* Calculate difference. */
2631 change
= thisy
- cy
; /* +ve up, -ve down */
2634 * Try to use VPA if change is larger than absolute or if this
2635 * change would cross the scroll region, otherwise use CUU/CUD.
2637 if ((u_int
) abs(change
) > cy
||
2638 (change
< 0 && cy
- change
> tty
->rlower
) ||
2639 (change
> 0 && cy
- change
< tty
->rupper
)) {
2640 if (tty_term_has(term
, TTYC_VPA
)) {
2641 tty_putcode_i(tty
, TTYC_VPA
, cy
);
2644 } else if (change
> 0 && tty_term_has(term
, TTYC_CUU
)) {
2645 tty_putcode_i(tty
, TTYC_CUU
, change
);
2647 } else if (change
< 0 && tty_term_has(term
, TTYC_CUD
)) {
2648 tty_putcode_i(tty
, TTYC_CUD
, -change
);
2654 /* Absolute movement. */
2655 tty_putcode_ii(tty
, TTYC_CUP
, cy
, cx
);
2663 tty_hyperlink(struct tty
*tty
, const struct grid_cell
*gc
,
2664 struct hyperlinks
*hl
)
2666 const char *uri
, *id
;
2668 if (gc
->link
== tty
->cell
.link
)
2670 tty
->cell
.link
= gc
->link
;
2675 if (gc
->link
== 0 || !hyperlinks_get(hl
, gc
->link
, &uri
, NULL
, &id
))
2676 tty_putcode_ss(tty
, TTYC_HLS
, "", "");
2678 tty_putcode_ss(tty
, TTYC_HLS
, id
, uri
);
2682 tty_attributes(struct tty
*tty
, const struct grid_cell
*gc
,
2683 const struct grid_cell
*defaults
, struct colour_palette
*palette
,
2684 struct hyperlinks
*hl
)
2686 struct grid_cell
*tc
= &tty
->cell
, gc2
;
2689 /* Copy cell and update default colours. */
2690 memcpy(&gc2
, gc
, sizeof gc2
);
2691 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2693 gc2
.fg
= defaults
->fg
;
2695 gc2
.bg
= defaults
->bg
;
2698 /* Ignore cell if it is the same as the last one. */
2699 if (gc2
.attr
== tty
->last_cell
.attr
&&
2700 gc2
.fg
== tty
->last_cell
.fg
&&
2701 gc2
.bg
== tty
->last_cell
.bg
&&
2702 gc2
.us
== tty
->last_cell
.us
&&
2703 gc2
.link
== tty
->last_cell
.link
)
2707 * If no setab, try to use the reverse attribute as a best-effort for a
2708 * non-default background. This is a bit of a hack but it doesn't do
2709 * any serious harm and makes a couple of applications happier.
2711 if (!tty_term_has(tty
->term
, TTYC_SETAB
)) {
2712 if (gc2
.attr
& GRID_ATTR_REVERSE
) {
2713 if (gc2
.fg
!= 7 && !COLOUR_DEFAULT(gc2
.fg
))
2714 gc2
.attr
&= ~GRID_ATTR_REVERSE
;
2716 if (gc2
.bg
!= 0 && !COLOUR_DEFAULT(gc2
.bg
))
2717 gc2
.attr
|= GRID_ATTR_REVERSE
;
2721 /* Fix up the colours if necessary. */
2722 tty_check_fg(tty
, palette
, &gc2
);
2723 tty_check_bg(tty
, palette
, &gc2
);
2724 tty_check_us(tty
, palette
, &gc2
);
2727 * If any bits are being cleared or the underline colour is now default,
2730 if ((tc
->attr
& ~gc2
.attr
) || (tc
->us
!= gc2
.us
&& gc2
.us
== 0))
2734 * Set the colours. This may call tty_reset() (so it comes next) and
2735 * may add to (NOT remove) the desired attributes.
2737 tty_colours(tty
, &gc2
);
2739 /* Filter out attribute bits already set. */
2740 changed
= gc2
.attr
& ~tc
->attr
;
2741 tc
->attr
= gc2
.attr
;
2743 /* Set the attributes. */
2744 if (changed
& GRID_ATTR_BRIGHT
)
2745 tty_putcode(tty
, TTYC_BOLD
);
2746 if (changed
& GRID_ATTR_DIM
)
2747 tty_putcode(tty
, TTYC_DIM
);
2748 if (changed
& GRID_ATTR_ITALICS
)
2749 tty_set_italics(tty
);
2750 if (changed
& GRID_ATTR_ALL_UNDERSCORE
) {
2751 if ((changed
& GRID_ATTR_UNDERSCORE
) ||
2752 !tty_term_has(tty
->term
, TTYC_SMULX
))
2753 tty_putcode(tty
, TTYC_SMUL
);
2754 else if (changed
& GRID_ATTR_UNDERSCORE_2
)
2755 tty_putcode_i(tty
, TTYC_SMULX
, 2);
2756 else if (changed
& GRID_ATTR_UNDERSCORE_3
)
2757 tty_putcode_i(tty
, TTYC_SMULX
, 3);
2758 else if (changed
& GRID_ATTR_UNDERSCORE_4
)
2759 tty_putcode_i(tty
, TTYC_SMULX
, 4);
2760 else if (changed
& GRID_ATTR_UNDERSCORE_5
)
2761 tty_putcode_i(tty
, TTYC_SMULX
, 5);
2763 if (changed
& GRID_ATTR_BLINK
)
2764 tty_putcode(tty
, TTYC_BLINK
);
2765 if (changed
& GRID_ATTR_REVERSE
) {
2766 if (tty_term_has(tty
->term
, TTYC_REV
))
2767 tty_putcode(tty
, TTYC_REV
);
2768 else if (tty_term_has(tty
->term
, TTYC_SMSO
))
2769 tty_putcode(tty
, TTYC_SMSO
);
2771 if (changed
& GRID_ATTR_HIDDEN
)
2772 tty_putcode(tty
, TTYC_INVIS
);
2773 if (changed
& GRID_ATTR_STRIKETHROUGH
)
2774 tty_putcode(tty
, TTYC_SMXX
);
2775 if (changed
& GRID_ATTR_OVERLINE
)
2776 tty_putcode(tty
, TTYC_SMOL
);
2777 if ((changed
& GRID_ATTR_CHARSET
) && tty_acs_needed(tty
))
2778 tty_putcode(tty
, TTYC_SMACS
);
2780 /* Set hyperlink if any. */
2781 tty_hyperlink(tty
, gc
, hl
);
2783 memcpy(&tty
->last_cell
, &gc2
, sizeof tty
->last_cell
);
2787 tty_colours(struct tty
*tty
, const struct grid_cell
*gc
)
2789 struct grid_cell
*tc
= &tty
->cell
;
2791 /* No changes? Nothing is necessary. */
2792 if (gc
->fg
== tc
->fg
&& gc
->bg
== tc
->bg
&& gc
->us
== tc
->us
)
2796 * Is either the default colour? This is handled specially because the
2797 * best solution might be to reset both colours to default, in which
2798 * case if only one is default need to fall onward to set the other
2801 if (COLOUR_DEFAULT(gc
->fg
) || COLOUR_DEFAULT(gc
->bg
)) {
2803 * If don't have AX, send sgr0. This resets both colours to default.
2804 * Otherwise, try to set the default colour only as needed.
2806 if (!tty_term_flag(tty
->term
, TTYC_AX
))
2809 if (COLOUR_DEFAULT(gc
->fg
) && !COLOUR_DEFAULT(tc
->fg
)) {
2810 tty_puts(tty
, "\033[39m");
2813 if (COLOUR_DEFAULT(gc
->bg
) && !COLOUR_DEFAULT(tc
->bg
)) {
2814 tty_puts(tty
, "\033[49m");
2820 /* Set the foreground colour. */
2821 if (!COLOUR_DEFAULT(gc
->fg
) && gc
->fg
!= tc
->fg
)
2822 tty_colours_fg(tty
, gc
);
2825 * Set the background colour. This must come after the foreground as
2826 * tty_colours_fg() can call tty_reset().
2828 if (!COLOUR_DEFAULT(gc
->bg
) && gc
->bg
!= tc
->bg
)
2829 tty_colours_bg(tty
, gc
);
2831 /* Set the underscore colour. */
2832 if (gc
->us
!= tc
->us
)
2833 tty_colours_us(tty
, gc
);
2837 tty_check_fg(struct tty
*tty
, struct colour_palette
*palette
,
2838 struct grid_cell
*gc
)
2845 * Perform substitution if this pane has a palette. If the bright
2846 * attribute is set and Nobr is not present, use the bright entry in
2847 * the palette by changing to the aixterm colour
2849 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2852 gc
->attr
& GRID_ATTR_BRIGHT
&&
2853 !tty_term_has(tty
->term
, TTYC_NOBR
))
2855 if ((c
= colour_palette_get(palette
, c
)) != -1)
2859 /* Is this a 24-bit colour? */
2860 if (gc
->fg
& COLOUR_FLAG_RGB
) {
2861 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2862 if (tty
->term
->flags
& TERM_RGBCOLOURS
)
2864 colour_split_rgb(gc
->fg
, &r
, &g
, &b
);
2865 gc
->fg
= colour_find_rgb(r
, g
, b
);
2868 /* How many colours does this terminal have? */
2869 if (tty
->term
->flags
& TERM_256COLOURS
)
2872 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
2874 /* Is this a 256-colour colour? */
2875 if (gc
->fg
& COLOUR_FLAG_256
) {
2876 /* And not a 256 colour mode? */
2877 if (colours
< 256) {
2878 gc
->fg
= colour_256to16(gc
->fg
);
2888 /* Is this an aixterm colour? */
2889 if (gc
->fg
>= 90 && gc
->fg
<= 97 && colours
< 16) {
2891 gc
->attr
|= GRID_ATTR_BRIGHT
;
2896 tty_check_bg(struct tty
*tty
, struct colour_palette
*palette
,
2897 struct grid_cell
*gc
)
2903 /* Perform substitution if this pane has a palette. */
2904 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2905 if ((c
= colour_palette_get(palette
, gc
->bg
)) != -1)
2909 /* Is this a 24-bit colour? */
2910 if (gc
->bg
& COLOUR_FLAG_RGB
) {
2911 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2912 if (tty
->term
->flags
& TERM_RGBCOLOURS
)
2914 colour_split_rgb(gc
->bg
, &r
, &g
, &b
);
2915 gc
->bg
= colour_find_rgb(r
, g
, b
);
2918 /* How many colours does this terminal have? */
2919 if (tty
->term
->flags
& TERM_256COLOURS
)
2922 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
2924 /* Is this a 256-colour colour? */
2925 if (gc
->bg
& COLOUR_FLAG_256
) {
2927 * And not a 256 colour mode? Translate to 16-colour
2928 * palette. Bold background doesn't exist portably, so just
2929 * discard the bold bit if set.
2931 if (colours
< 256) {
2932 gc
->bg
= colour_256to16(gc
->bg
);
2942 /* Is this an aixterm colour? */
2943 if (gc
->bg
>= 90 && gc
->bg
<= 97 && colours
< 16)
2948 tty_check_us(__unused
struct tty
*tty
, struct colour_palette
*palette
,
2949 struct grid_cell
*gc
)
2953 /* Perform substitution if this pane has a palette. */
2954 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2955 if ((c
= colour_palette_get(palette
, gc
->us
)) != -1)
2959 /* Convert underscore colour if only RGB can be supported. */
2960 if (!tty_term_has(tty
->term
, TTYC_SETULC1
)) {
2961 if ((c
= colour_force_rgb (gc
->us
)) == -1)
2969 tty_colours_fg(struct tty
*tty
, const struct grid_cell
*gc
)
2971 struct grid_cell
*tc
= &tty
->cell
;
2975 * If the current colour is an aixterm bright colour and the new is not,
2976 * reset because some terminals do not clear bright correctly.
2978 if (tty
->cell
.fg
>= 90 &&
2979 tty
->cell
.bg
<= 97 &&
2980 (gc
->fg
< 90 || gc
->fg
> 97))
2983 /* Is this a 24-bit or 256-colour colour? */
2984 if (gc
->fg
& COLOUR_FLAG_RGB
|| gc
->fg
& COLOUR_FLAG_256
) {
2985 if (tty_try_colour(tty
, gc
->fg
, "38") == 0)
2987 /* Should not get here, already converted in tty_check_fg. */
2991 /* Is this an aixterm bright colour? */
2992 if (gc
->fg
>= 90 && gc
->fg
<= 97) {
2993 if (tty
->term
->flags
& TERM_256COLOURS
) {
2994 xsnprintf(s
, sizeof s
, "\033[%dm", gc
->fg
);
2997 tty_putcode_i(tty
, TTYC_SETAF
, gc
->fg
- 90 + 8);
3001 /* Otherwise set the foreground colour. */
3002 tty_putcode_i(tty
, TTYC_SETAF
, gc
->fg
);
3005 /* Save the new values in the terminal current cell. */
3010 tty_colours_bg(struct tty
*tty
, const struct grid_cell
*gc
)
3012 struct grid_cell
*tc
= &tty
->cell
;
3015 /* Is this a 24-bit or 256-colour colour? */
3016 if (gc
->bg
& COLOUR_FLAG_RGB
|| gc
->bg
& COLOUR_FLAG_256
) {
3017 if (tty_try_colour(tty
, gc
->bg
, "48") == 0)
3019 /* Should not get here, already converted in tty_check_bg. */
3023 /* Is this an aixterm bright colour? */
3024 if (gc
->bg
>= 90 && gc
->bg
<= 97) {
3025 if (tty
->term
->flags
& TERM_256COLOURS
) {
3026 xsnprintf(s
, sizeof s
, "\033[%dm", gc
->bg
+ 10);
3029 tty_putcode_i(tty
, TTYC_SETAB
, gc
->bg
- 90 + 8);
3033 /* Otherwise set the background colour. */
3034 tty_putcode_i(tty
, TTYC_SETAB
, gc
->bg
);
3037 /* Save the new values in the terminal current cell. */
3042 tty_colours_us(struct tty
*tty
, const struct grid_cell
*gc
)
3044 struct grid_cell
*tc
= &tty
->cell
;
3048 /* Clear underline colour. */
3049 if (COLOUR_DEFAULT(gc
->us
)) {
3050 tty_putcode(tty
, TTYC_OL
);
3055 * If this is not an RGB colour, use Setulc1 if it exists, otherwise
3058 if (~gc
->us
& COLOUR_FLAG_RGB
) {
3060 if ((~c
& COLOUR_FLAG_256
) && (c
>= 90 && c
<= 97))
3062 tty_putcode_i(tty
, TTYC_SETULC1
, c
& ~COLOUR_FLAG_256
);
3067 * Setulc and setal follows the ncurses(3) one argument "direct colour"
3068 * capability format. Calculate the colour value.
3070 colour_split_rgb(gc
->us
, &r
, &g
, &b
);
3071 c
= (65536 * r
) + (256 * g
) + b
;
3074 * Write the colour. Only use setal if the RGB flag is set because the
3075 * non-RGB version may be wrong.
3077 if (tty_term_has(tty
->term
, TTYC_SETULC
))
3078 tty_putcode_i(tty
, TTYC_SETULC
, c
);
3079 else if (tty_term_has(tty
->term
, TTYC_SETAL
) &&
3080 tty_term_has(tty
->term
, TTYC_RGB
))
3081 tty_putcode_i(tty
, TTYC_SETAL
, c
);
3084 /* Save the new values in the terminal current cell. */
3089 tty_try_colour(struct tty
*tty
, int colour
, const char *type
)
3093 if (colour
& COLOUR_FLAG_256
) {
3094 if (*type
== '3' && tty_term_has(tty
->term
, TTYC_SETAF
))
3095 tty_putcode_i(tty
, TTYC_SETAF
, colour
& 0xff);
3096 else if (tty_term_has(tty
->term
, TTYC_SETAB
))
3097 tty_putcode_i(tty
, TTYC_SETAB
, colour
& 0xff);
3101 if (colour
& COLOUR_FLAG_RGB
) {
3102 colour_split_rgb(colour
& 0xffffff, &r
, &g
, &b
);
3103 if (*type
== '3' && tty_term_has(tty
->term
, TTYC_SETRGBF
))
3104 tty_putcode_iii(tty
, TTYC_SETRGBF
, r
, g
, b
);
3105 else if (tty_term_has(tty
->term
, TTYC_SETRGBB
))
3106 tty_putcode_iii(tty
, TTYC_SETRGBB
, r
, g
, b
);
3114 tty_window_default_style(struct grid_cell
*gc
, struct window_pane
*wp
)
3116 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
3117 gc
->fg
= wp
->palette
.fg
;
3118 gc
->bg
= wp
->palette
.bg
;
3122 tty_default_colours(struct grid_cell
*gc
, struct window_pane
*wp
)
3124 struct options
*oo
= wp
->options
;
3125 struct format_tree
*ft
;
3127 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
3129 if (wp
->flags
& PANE_STYLECHANGED
) {
3130 log_debug("%%%u: style changed", wp
->id
);
3131 wp
->flags
&= ~PANE_STYLECHANGED
;
3133 ft
= format_create(NULL
, NULL
, FORMAT_PANE
|wp
->id
,
3135 format_defaults(ft
, NULL
, NULL
, NULL
, wp
);
3136 tty_window_default_style(&wp
->cached_active_gc
, wp
);
3137 style_add(&wp
->cached_active_gc
, oo
, "window-active-style", ft
);
3138 tty_window_default_style(&wp
->cached_gc
, wp
);
3139 style_add(&wp
->cached_gc
, oo
, "window-style", ft
);
3144 if (wp
== wp
->window
->active
&& wp
->cached_active_gc
.fg
!= 8)
3145 gc
->fg
= wp
->cached_active_gc
.fg
;
3147 gc
->fg
= wp
->cached_gc
.fg
;
3151 if (wp
== wp
->window
->active
&& wp
->cached_active_gc
.bg
!= 8)
3152 gc
->bg
= wp
->cached_active_gc
.bg
;
3154 gc
->bg
= wp
->cached_gc
.bg
;
3159 tty_default_attributes(struct tty
*tty
, const struct grid_cell
*defaults
,
3160 struct colour_palette
*palette
, u_int bg
, struct hyperlinks
*hl
)
3162 struct grid_cell gc
;
3164 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
3166 tty_attributes(tty
, &gc
, defaults
, palette
, hl
);
3170 tty_clipboard_query_callback(__unused
int fd
, __unused
short events
, void *data
)
3172 struct tty
*tty
= data
;
3173 struct client
*c
= tty
->client
;
3175 c
->flags
&= ~CLIENT_CLIPBOARDBUFFER
;
3176 free(c
->clipboard_panes
);
3177 c
->clipboard_panes
= NULL
;
3178 c
->clipboard_npanes
= 0;
3180 tty
->flags
&= ~TTY_OSC52QUERY
;
3184 tty_clipboard_query(struct tty
*tty
)
3186 struct timeval tv
= { .tv_sec
= TTY_QUERY_TIMEOUT
};
3188 if ((~tty
->flags
& TTY_STARTED
) || (tty
->flags
& TTY_OSC52QUERY
))
3190 tty_putcode_ss(tty
, TTYC_MS
, "", "?");
3192 tty
->flags
|= TTY_OSC52QUERY
;
3193 evtimer_set(&tty
->clipboard_timer
, tty_clipboard_query_callback
, tty
);
3194 evtimer_add(&tty
->clipboard_timer
, &tv
);