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)
1370 /* UTF-8 terminal and a UTF-8 character - fine. */
1371 if (tty
->client
->flags
& CLIENT_UTF8
)
1373 memcpy(&new, gc
, sizeof new);
1375 /* See if this can be mapped to an ACS character. */
1376 c
= tty_acs_reverse_get(tty
, gc
->data
.data
, gc
->data
.size
);
1378 utf8_set(&new.data
, c
);
1379 new.attr
|= GRID_ATTR_CHARSET
;
1383 /* Replace by the right number of underscores. */
1384 new.data
.size
= gc
->data
.width
;
1385 if (new.data
.size
> UTF8_SIZE
)
1386 new.data
.size
= UTF8_SIZE
;
1387 memset(new.data
.data
, '_', new.data
.size
);
1392 * Check if a single character is obstructed by the overlay and return a
1396 tty_check_overlay(struct tty
*tty
, u_int px
, u_int py
)
1398 struct overlay_ranges r
;
1401 * A unit width range will always return nx[2] == 0 from a check, even
1402 * with multiple overlays, so it's sufficient to check just the first
1405 tty_check_overlay_range(tty
, px
, py
, 1, &r
);
1406 if (r
.nx
[0] + r
.nx
[1] == 0)
1411 /* Return parts of the input range which are visible. */
1413 tty_check_overlay_range(struct tty
*tty
, u_int px
, u_int py
, u_int nx
,
1414 struct overlay_ranges
*r
)
1416 struct client
*c
= tty
->client
;
1418 if (c
->overlay_check
== NULL
) {
1428 c
->overlay_check(c
, c
->overlay_data
, px
, py
, nx
, r
);
1432 tty_draw_line(struct tty
*tty
, struct screen
*s
, u_int px
, u_int py
, u_int nx
,
1433 u_int atx
, u_int aty
, const struct grid_cell
*defaults
,
1434 struct colour_palette
*palette
)
1436 struct grid
*gd
= s
->grid
;
1437 struct grid_cell gc
, last
;
1438 const struct grid_cell
*gcp
;
1439 struct grid_line
*gl
;
1440 struct client
*c
= tty
->client
;
1441 struct overlay_ranges r
;
1442 u_int i
, j
, ux
, sx
, width
, hidden
, eux
, nxx
;
1444 int flags
, cleared
= 0, wrapped
= 0;
1448 log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__
,
1449 px
, py
, nx
, atx
, aty
);
1450 log_debug("%s: defaults: fg=%d, bg=%d", __func__
, defaults
->fg
,
1454 * py is the line in the screen to draw.
1455 * px is the start x and nx is the width to draw.
1456 * atx,aty is the line on the terminal to draw it.
1459 flags
= (tty
->flags
& TTY_NOCURSOR
);
1460 tty
->flags
|= TTY_NOCURSOR
;
1461 tty_update_mode(tty
, tty
->mode
, s
);
1463 tty_region_off(tty
);
1464 tty_margin_off(tty
);
1467 * Clamp the width to cellsize - note this is not cellused, because
1468 * there may be empty background cells after it (from BCE).
1470 sx
= screen_size_x(s
);
1473 cellsize
= grid_get_line(gd
, gd
->hsize
+ py
)->cellsize
;
1485 gl
= grid_get_line(gd
, gd
->hsize
+ py
- 1);
1487 (~gl
->flags
& GRID_LINE_WRAPPED
) ||
1489 tty
->cx
< tty
->sx
||
1494 tty_term_has(tty
->term
, TTYC_EL1
) &&
1495 !tty_fake_bce(tty
, defaults
, 8) &&
1496 c
->overlay_check
== NULL
) {
1497 tty_default_attributes(tty
, defaults
, palette
, 8,
1499 tty_cursor(tty
, nx
- 1, aty
);
1500 tty_putcode(tty
, TTYC_EL1
);
1504 log_debug("%s: wrapped line %u", __func__
, aty
);
1508 memcpy(&last
, &grid_default_cell
, sizeof last
);
1512 for (i
= 0; i
< sx
; i
++) {
1513 grid_view_get_cell(gd
, px
+ i
, py
, &gc
);
1514 gcp
= tty_check_codeset(tty
, &gc
);
1516 (!tty_check_overlay(tty
, atx
+ ux
+ width
, aty
) ||
1517 (gcp
->attr
& GRID_ATTR_CHARSET
) ||
1518 gcp
->flags
!= last
.flags
||
1519 gcp
->attr
!= last
.attr
||
1520 gcp
->fg
!= last
.fg
||
1521 gcp
->bg
!= last
.bg
||
1522 gcp
->us
!= last
.us
||
1523 gcp
->link
!= last
.link
||
1524 ux
+ width
+ gcp
->data
.width
> nx
||
1525 (sizeof buf
) - len
< gcp
->data
.size
)) {
1526 tty_attributes(tty
, &last
, defaults
, palette
,
1528 if (last
.flags
& GRID_FLAG_CLEARED
) {
1529 log_debug("%s: %zu cleared", __func__
, len
);
1530 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
,
1533 if (!wrapped
|| atx
!= 0 || ux
!= 0)
1534 tty_cursor(tty
, atx
+ ux
, aty
);
1535 tty_putn(tty
, buf
, len
, width
);
1544 if (gcp
->flags
& GRID_FLAG_SELECTED
)
1545 screen_select_cell(s
, &last
, gcp
);
1547 memcpy(&last
, gcp
, sizeof last
);
1549 tty_check_overlay_range(tty
, atx
+ ux
, aty
, gcp
->data
.width
,
1552 for (j
= 0; j
< OVERLAY_MAX_RANGES
; j
++)
1554 hidden
= gcp
->data
.width
- hidden
;
1555 if (hidden
!= 0 && hidden
== gcp
->data
.width
) {
1556 if (~gcp
->flags
& GRID_FLAG_PADDING
)
1557 ux
+= gcp
->data
.width
;
1558 } else if (hidden
!= 0 || ux
+ gcp
->data
.width
> nx
) {
1559 if (~gcp
->flags
& GRID_FLAG_PADDING
) {
1560 tty_attributes(tty
, &last
, defaults
, palette
,
1562 for (j
= 0; j
< OVERLAY_MAX_RANGES
; j
++) {
1565 /* Effective width drawn so far. */
1566 eux
= r
.px
[j
] - atx
;
1568 tty_cursor(tty
, r
.px
[j
], aty
);
1572 tty_repeat_space(tty
, r
.nx
[j
]);
1577 } else if (gcp
->attr
& GRID_ATTR_CHARSET
) {
1578 tty_attributes(tty
, &last
, defaults
, palette
,
1580 tty_cursor(tty
, atx
+ ux
, aty
);
1581 for (j
= 0; j
< gcp
->data
.size
; j
++)
1582 tty_putc(tty
, gcp
->data
.data
[j
]);
1583 ux
+= gcp
->data
.width
;
1584 } else if (~gcp
->flags
& GRID_FLAG_PADDING
) {
1585 memcpy(buf
+ len
, gcp
->data
.data
, gcp
->data
.size
);
1586 len
+= gcp
->data
.size
;
1587 width
+= gcp
->data
.width
;
1590 if (len
!= 0 && ((~last
.flags
& GRID_FLAG_CLEARED
) || last
.bg
!= 8)) {
1591 tty_attributes(tty
, &last
, defaults
, palette
, s
->hyperlinks
);
1592 if (last
.flags
& GRID_FLAG_CLEARED
) {
1593 log_debug("%s: %zu cleared (end)", __func__
, len
);
1594 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
, width
,
1597 if (!wrapped
|| atx
!= 0 || ux
!= 0)
1598 tty_cursor(tty
, atx
+ ux
, aty
);
1599 tty_putn(tty
, buf
, len
, width
);
1604 if (!cleared
&& ux
< nx
) {
1605 log_debug("%s: %u to end of line (%zu cleared)", __func__
,
1607 tty_default_attributes(tty
, defaults
, palette
, 8,
1609 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
, nx
- ux
, 8);
1612 tty
->flags
= (tty
->flags
& ~TTY_NOCURSOR
) | flags
;
1613 tty_update_mode(tty
, tty
->mode
, s
);
1617 /* Update context for client. */
1619 tty_set_client_cb(struct tty_ctx
*ttyctx
, struct client
*c
)
1621 struct window_pane
*wp
= ttyctx
->arg
;
1623 if (c
->session
->curw
->window
!= wp
->window
)
1625 if (wp
->layout_cell
== NULL
)
1628 /* Set the properties relevant to the current client. */
1629 ttyctx
->bigger
= tty_window_offset(&c
->tty
, &ttyctx
->wox
, &ttyctx
->woy
,
1630 &ttyctx
->wsx
, &ttyctx
->wsy
);
1632 ttyctx
->yoff
= ttyctx
->ryoff
= wp
->yoff
;
1633 if (status_at_line(c
) == 0)
1634 ttyctx
->yoff
+= status_line_size(c
);
1640 tty_draw_images(struct client
*c
, struct window_pane
*wp
, struct screen
*s
)
1643 struct tty_ctx ttyctx
;
1645 TAILQ_FOREACH(im
, &s
->images
, entry
) {
1646 memset(&ttyctx
, 0, sizeof ttyctx
);
1648 /* Set the client independent properties. */
1649 ttyctx
.ocx
= im
->px
;
1650 ttyctx
.ocy
= im
->py
;
1652 ttyctx
.orlower
= s
->rlower
;
1653 ttyctx
.orupper
= s
->rupper
;
1655 ttyctx
.xoff
= ttyctx
.rxoff
= wp
->xoff
;
1661 ttyctx
.set_client_cb
= tty_set_client_cb
;
1662 ttyctx
.allow_invisible_panes
= 1;
1663 tty_write_one(tty_cmd_sixelimage
, c
, &ttyctx
);
1669 tty_sync_start(struct tty
*tty
)
1671 if (tty
->flags
& TTY_BLOCK
)
1673 if (tty
->flags
& TTY_SYNCING
)
1675 tty
->flags
|= TTY_SYNCING
;
1677 if (tty_term_has(tty
->term
, TTYC_SYNC
)) {
1678 log_debug("%s sync start", tty
->client
->name
);
1679 tty_putcode_i(tty
, TTYC_SYNC
, 1);
1684 tty_sync_end(struct tty
*tty
)
1686 if (tty
->flags
& TTY_BLOCK
)
1688 if (~tty
->flags
& TTY_SYNCING
)
1690 tty
->flags
&= ~TTY_SYNCING
;
1692 if (tty_term_has(tty
->term
, TTYC_SYNC
)) {
1693 log_debug("%s sync end", tty
->client
->name
);
1694 tty_putcode_i(tty
, TTYC_SYNC
, 2);
1699 tty_client_ready(const struct tty_ctx
*ctx
, struct client
*c
)
1701 if (c
->session
== NULL
|| c
->tty
.term
== NULL
)
1703 if (c
->flags
& CLIENT_SUSPENDED
)
1707 * If invisible panes are allowed (used for passthrough), don't care if
1708 * redrawing or frozen.
1710 if (ctx
->allow_invisible_panes
)
1713 if (c
->flags
& CLIENT_REDRAWWINDOW
)
1715 if (c
->tty
.flags
& TTY_FREEZE
)
1721 tty_write(void (*cmdfn
)(struct tty
*, const struct tty_ctx
*),
1722 struct tty_ctx
*ctx
)
1727 if (ctx
->set_client_cb
== NULL
)
1729 TAILQ_FOREACH(c
, &clients
, entry
) {
1730 if (tty_client_ready(ctx
, c
)) {
1731 state
= ctx
->set_client_cb(ctx
, c
);
1736 cmdfn(&c
->tty
, ctx
);
1742 /* Only write to the incoming tty instead of every client. */
1744 tty_write_one(void (*cmdfn
)(struct tty
*, const struct tty_ctx
*),
1745 struct client
*c
, struct tty_ctx
*ctx
)
1747 if (ctx
->set_client_cb
== NULL
)
1749 if ((ctx
->set_client_cb(ctx
, c
)) == 1)
1750 cmdfn(&c
->tty
, ctx
);
1755 tty_cmd_insertcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1757 struct client
*c
= tty
->client
;
1760 !tty_full_width(tty
, ctx
) ||
1761 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1762 (!tty_term_has(tty
->term
, TTYC_ICH
) &&
1763 !tty_term_has(tty
->term
, TTYC_ICH1
)) ||
1764 c
->overlay_check
!= NULL
) {
1765 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
1769 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1770 ctx
->s
->hyperlinks
);
1772 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1774 tty_emulate_repeat(tty
, TTYC_ICH
, TTYC_ICH1
, ctx
->num
);
1778 tty_cmd_deletecharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1780 struct client
*c
= tty
->client
;
1783 !tty_full_width(tty
, ctx
) ||
1784 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1785 (!tty_term_has(tty
->term
, TTYC_DCH
) &&
1786 !tty_term_has(tty
->term
, TTYC_DCH1
)) ||
1787 c
->overlay_check
!= NULL
) {
1788 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
1792 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1793 ctx
->s
->hyperlinks
);
1795 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1797 tty_emulate_repeat(tty
, TTYC_DCH
, TTYC_DCH1
, ctx
->num
);
1801 tty_cmd_clearcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1803 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1804 ctx
->s
->hyperlinks
);
1806 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, ctx
->ocx
, ctx
->num
, ctx
->bg
);
1810 tty_cmd_insertline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1812 struct client
*c
= tty
->client
;
1815 !tty_full_width(tty
, ctx
) ||
1816 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1817 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1818 !tty_term_has(tty
->term
, TTYC_IL1
) ||
1821 c
->overlay_check
!= NULL
) {
1822 tty_redraw_region(tty
, ctx
);
1826 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1827 ctx
->s
->hyperlinks
);
1829 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1830 tty_margin_off(tty
);
1831 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1833 tty_emulate_repeat(tty
, TTYC_IL
, TTYC_IL1
, ctx
->num
);
1834 tty
->cx
= tty
->cy
= UINT_MAX
;
1838 tty_cmd_deleteline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1840 struct client
*c
= tty
->client
;
1843 !tty_full_width(tty
, ctx
) ||
1844 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1845 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1846 !tty_term_has(tty
->term
, TTYC_DL1
) ||
1849 c
->overlay_check
!= NULL
) {
1850 tty_redraw_region(tty
, ctx
);
1854 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1855 ctx
->s
->hyperlinks
);
1857 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1858 tty_margin_off(tty
);
1859 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1861 tty_emulate_repeat(tty
, TTYC_DL
, TTYC_DL1
, ctx
->num
);
1862 tty
->cx
= tty
->cy
= UINT_MAX
;
1866 tty_cmd_clearline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1868 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1869 ctx
->s
->hyperlinks
);
1871 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, 0, ctx
->sx
, ctx
->bg
);
1875 tty_cmd_clearendofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1877 u_int nx
= ctx
->sx
- ctx
->ocx
;
1879 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1880 ctx
->s
->hyperlinks
);
1882 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, ctx
->ocx
, nx
, ctx
->bg
);
1886 tty_cmd_clearstartofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1888 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1889 ctx
->s
->hyperlinks
);
1891 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, 0, ctx
->ocx
+ 1, ctx
->bg
);
1895 tty_cmd_reverseindex(struct tty
*tty
, const struct tty_ctx
*ctx
)
1897 struct client
*c
= tty
->client
;
1899 if (ctx
->ocy
!= ctx
->orupper
)
1903 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1904 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1905 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1906 (!tty_term_has(tty
->term
, TTYC_RI
) &&
1907 !tty_term_has(tty
->term
, TTYC_RIN
)) ||
1910 c
->overlay_check
!= NULL
) {
1911 tty_redraw_region(tty
, ctx
);
1915 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1916 ctx
->s
->hyperlinks
);
1918 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1919 tty_margin_pane(tty
, ctx
);
1920 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
1922 if (tty_term_has(tty
->term
, TTYC_RI
))
1923 tty_putcode(tty
, TTYC_RI
);
1925 tty_putcode_i(tty
, TTYC_RIN
, 1);
1929 tty_cmd_linefeed(struct tty
*tty
, const struct tty_ctx
*ctx
)
1931 struct client
*c
= tty
->client
;
1933 if (ctx
->ocy
!= ctx
->orlower
)
1937 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1938 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1939 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1942 c
->overlay_check
!= NULL
) {
1943 tty_redraw_region(tty
, ctx
);
1947 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1948 ctx
->s
->hyperlinks
);
1950 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1951 tty_margin_pane(tty
, ctx
);
1954 * If we want to wrap a pane while using margins, the cursor needs to
1955 * be exactly on the right of the region. If the cursor is entirely off
1956 * the edge - move it back to the right. Some terminals are funny about
1957 * this and insert extra spaces, so only use the right if margins are
1960 if (ctx
->xoff
+ ctx
->ocx
> tty
->rright
) {
1961 if (!tty_use_margin(tty
))
1962 tty_cursor(tty
, 0, ctx
->yoff
+ ctx
->ocy
);
1964 tty_cursor(tty
, tty
->rright
, ctx
->yoff
+ ctx
->ocy
);
1966 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1968 tty_putc(tty
, '\n');
1972 tty_cmd_scrollup(struct tty
*tty
, const struct tty_ctx
*ctx
)
1974 struct client
*c
= tty
->client
;
1978 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1979 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1980 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1983 c
->overlay_check
!= NULL
) {
1984 tty_redraw_region(tty
, ctx
);
1988 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1989 ctx
->s
->hyperlinks
);
1991 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1992 tty_margin_pane(tty
, ctx
);
1994 if (ctx
->num
== 1 || !tty_term_has(tty
->term
, TTYC_INDN
)) {
1995 if (!tty_use_margin(tty
))
1996 tty_cursor(tty
, 0, tty
->rlower
);
1998 tty_cursor(tty
, tty
->rright
, tty
->rlower
);
1999 for (i
= 0; i
< ctx
->num
; i
++)
2000 tty_putc(tty
, '\n');
2002 if (tty
->cy
== UINT_MAX
)
2003 tty_cursor(tty
, 0, 0);
2005 tty_cursor(tty
, 0, tty
->cy
);
2006 tty_putcode_i(tty
, TTYC_INDN
, ctx
->num
);
2011 tty_cmd_scrolldown(struct tty
*tty
, const struct tty_ctx
*ctx
)
2014 struct client
*c
= tty
->client
;
2017 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
2018 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
2019 !tty_term_has(tty
->term
, TTYC_CSR
) ||
2020 (!tty_term_has(tty
->term
, TTYC_RI
) &&
2021 !tty_term_has(tty
->term
, TTYC_RIN
)) ||
2024 c
->overlay_check
!= NULL
) {
2025 tty_redraw_region(tty
, ctx
);
2029 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
2030 ctx
->s
->hyperlinks
);
2032 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
2033 tty_margin_pane(tty
, ctx
);
2034 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
2036 if (tty_term_has(tty
->term
, TTYC_RIN
))
2037 tty_putcode_i(tty
, TTYC_RIN
, ctx
->num
);
2039 for (i
= 0; i
< ctx
->num
; i
++)
2040 tty_putcode(tty
, TTYC_RI
);
2045 tty_cmd_clearendofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
2047 u_int px
, py
, nx
, ny
;
2049 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
2050 ctx
->s
->hyperlinks
);
2052 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
2053 tty_margin_off(tty
);
2058 ny
= ctx
->sy
- ctx
->ocy
- 1;
2060 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
2063 nx
= ctx
->sx
- ctx
->ocx
;
2066 tty_clear_pane_line(tty
, ctx
, py
, px
, nx
, ctx
->bg
);
2070 tty_cmd_clearstartofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
2072 u_int px
, py
, nx
, ny
;
2074 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
2075 ctx
->s
->hyperlinks
);
2077 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
2078 tty_margin_off(tty
);
2085 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
2091 tty_clear_pane_line(tty
, ctx
, py
, px
, nx
, ctx
->bg
);
2095 tty_cmd_clearscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
2097 u_int px
, py
, nx
, ny
;
2099 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
2100 ctx
->s
->hyperlinks
);
2102 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
2103 tty_margin_off(tty
);
2110 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
2114 tty_cmd_alignmenttest(struct tty
*tty
, const struct tty_ctx
*ctx
)
2119 ctx
->redraw_cb(ctx
);
2123 tty_attributes(tty
, &grid_default_cell
, &ctx
->defaults
, ctx
->palette
,
2124 ctx
->s
->hyperlinks
);
2126 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
2127 tty_margin_off(tty
);
2129 for (j
= 0; j
< ctx
->sy
; j
++) {
2130 tty_cursor_pane(tty
, ctx
, 0, j
);
2131 for (i
= 0; i
< ctx
->sx
; i
++)
2137 tty_cmd_cell(struct tty
*tty
, const struct tty_ctx
*ctx
)
2139 const struct grid_cell
*gcp
= ctx
->cell
;
2140 struct screen
*s
= ctx
->s
;
2141 struct overlay_ranges r
;
2142 u_int px
, py
, i
, vis
= 0;
2144 px
= ctx
->xoff
+ ctx
->ocx
- ctx
->wox
;
2145 py
= ctx
->yoff
+ ctx
->ocy
- ctx
->woy
;
2146 if (!tty_is_visible(tty
, ctx
, ctx
->ocx
, ctx
->ocy
, 1, 1) ||
2147 (gcp
->data
.width
== 1 && !tty_check_overlay(tty
, px
, py
)))
2150 /* Handle partially obstructed wide characters. */
2151 if (gcp
->data
.width
> 1) {
2152 tty_check_overlay_range(tty
, px
, py
, gcp
->data
.width
, &r
);
2153 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++)
2155 if (vis
< gcp
->data
.width
) {
2156 tty_draw_line(tty
, s
, s
->cx
, s
->cy
, gcp
->data
.width
,
2157 px
, py
, &ctx
->defaults
, ctx
->palette
);
2162 if (ctx
->xoff
+ ctx
->ocx
- ctx
->wox
> tty
->sx
- 1 &&
2163 ctx
->ocy
== ctx
->orlower
&&
2164 tty_full_width(tty
, ctx
))
2165 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
2167 tty_margin_off(tty
);
2168 tty_cursor_pane_unless_wrap(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
2170 tty_cell(tty
, ctx
->cell
, &ctx
->defaults
, ctx
->palette
,
2171 ctx
->s
->hyperlinks
);
2174 tty_invalidate(tty
);
2178 tty_cmd_cells(struct tty
*tty
, const struct tty_ctx
*ctx
)
2180 struct overlay_ranges r
;
2181 u_int i
, px
, py
, cx
;
2182 char *cp
= ctx
->ptr
;
2184 if (!tty_is_visible(tty
, ctx
, ctx
->ocx
, ctx
->ocy
, ctx
->num
, 1))
2188 (ctx
->xoff
+ ctx
->ocx
< ctx
->wox
||
2189 ctx
->xoff
+ ctx
->ocx
+ ctx
->num
> ctx
->wox
+ ctx
->wsx
)) {
2190 if (!ctx
->wrapped
||
2191 !tty_full_width(tty
, ctx
) ||
2192 (tty
->term
->flags
& TERM_NOAM
) ||
2193 ctx
->xoff
+ ctx
->ocx
!= 0 ||
2194 ctx
->yoff
+ ctx
->ocy
!= tty
->cy
+ 1 ||
2195 tty
->cx
< tty
->sx
||
2196 tty
->cy
== tty
->rlower
)
2197 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
2199 ctx
->redraw_cb(ctx
);
2203 tty_margin_off(tty
);
2204 tty_cursor_pane_unless_wrap(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
2205 tty_attributes(tty
, ctx
->cell
, &ctx
->defaults
, ctx
->palette
, ctx
->s
->hyperlinks
);
2207 /* Get tty position from pane position for overlay check. */
2208 px
= ctx
->xoff
+ ctx
->ocx
- ctx
->wox
;
2209 py
= ctx
->yoff
+ ctx
->ocy
- ctx
->woy
;
2211 tty_check_overlay_range(tty
, px
, py
, ctx
->num
, &r
);
2212 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++) {
2215 /* Convert back to pane position for printing. */
2216 cx
= r
.px
[i
] - ctx
->xoff
+ ctx
->wox
;
2217 tty_cursor_pane_unless_wrap(tty
, ctx
, cx
, ctx
->ocy
);
2218 tty_putn(tty
, cp
+ r
.px
[i
] - px
, r
.nx
[i
], r
.nx
[i
]);
2223 tty_cmd_setselection(struct tty
*tty
, const struct tty_ctx
*ctx
)
2225 tty_set_selection(tty
, ctx
->ptr2
, ctx
->ptr
, ctx
->num
);
2229 tty_set_selection(struct tty
*tty
, const char *flags
, const char *buf
,
2235 if (~tty
->flags
& TTY_STARTED
)
2237 if (!tty_term_has(tty
->term
, TTYC_MS
))
2240 size
= 4 * ((len
+ 2) / 3) + 1; /* storage for base64 */
2241 encoded
= xmalloc(size
);
2243 b64_ntop(buf
, len
, encoded
, size
);
2244 tty
->flags
|= TTY_NOBLOCK
;
2245 tty_putcode_ss(tty
, TTYC_MS
, flags
, encoded
);
2251 tty_cmd_rawstring(struct tty
*tty
, const struct tty_ctx
*ctx
)
2253 tty
->flags
|= TTY_NOBLOCK
;
2254 tty_add(tty
, ctx
->ptr
, ctx
->num
);
2255 tty_invalidate(tty
);
2260 tty_cmd_sixelimage(struct tty
*tty
, const struct tty_ctx
*ctx
)
2262 struct image
*im
= ctx
->ptr
;
2263 struct sixel_image
*si
= im
->data
;
2264 struct sixel_image
*new;
2267 u_int cx
= ctx
->ocx
, cy
= ctx
->ocy
, sx
, sy
;
2268 u_int i
, j
, x
, y
, rx
, ry
;
2271 if ((~tty
->term
->flags
& TERM_SIXEL
) &&
2272 !tty_term_has(tty
->term
, TTYC_SXL
))
2274 if (tty
->xpixel
== 0 || tty
->ypixel
== 0)
2277 sixel_size_in_cells(si
, &sx
, &sy
);
2278 log_debug("%s: image is %ux%u", __func__
, sx
, sy
);
2279 if (!tty_clamp_area(tty
, ctx
, cx
, cy
, sx
, sy
, &i
, &j
, &x
, &y
, &rx
, &ry
))
2281 log_debug("%s: clamping to %u,%u-%u,%u", __func__
, i
, j
, rx
, ry
);
2283 if (fallback
== 1) {
2284 data
= xstrdup(im
->fallback
);
2285 size
= strlen(data
);
2287 new = sixel_scale(si
, tty
->xpixel
, tty
->ypixel
, i
, j
, rx
, ry
, 0);
2291 data
= sixel_print(new, si
, &size
);
2294 log_debug("%s: %zu bytes: %s", __func__
, size
, data
);
2295 tty_region_off(tty
);
2296 tty_margin_off(tty
);
2297 tty_cursor(tty
, x
, y
);
2299 tty
->flags
|= TTY_NOBLOCK
;
2300 tty_add(tty
, data
, size
);
2301 tty_invalidate(tty
);
2311 tty_cmd_syncstart(struct tty
*tty
, const struct tty_ctx
*ctx
)
2313 if (ctx
->num
== 0x11) {
2315 * This is an overlay and a command that moves the cursor so
2316 * start synchronized updates.
2318 tty_sync_start(tty
);
2319 } else if (~ctx
->num
& 0x10) {
2321 * This is a pane. If there is an overlay, always start;
2322 * otherwise, only if requested.
2324 if (ctx
->num
|| tty
->client
->overlay_draw
!= NULL
)
2325 tty_sync_start(tty
);
2330 tty_cell(struct tty
*tty
, const struct grid_cell
*gc
,
2331 const struct grid_cell
*defaults
, struct colour_palette
*palette
,
2332 struct hyperlinks
*hl
)
2334 const struct grid_cell
*gcp
;
2336 /* Skip last character if terminal is stupid. */
2337 if ((tty
->term
->flags
& TERM_NOAM
) &&
2338 tty
->cy
== tty
->sy
- 1 &&
2339 tty
->cx
== tty
->sx
- 1)
2342 /* If this is a padding character, do nothing. */
2343 if (gc
->flags
& GRID_FLAG_PADDING
)
2346 /* Check the output codeset and apply attributes. */
2347 gcp
= tty_check_codeset(tty
, gc
);
2348 tty_attributes(tty
, gcp
, defaults
, palette
, hl
);
2350 /* If it is a single character, write with putc to handle ACS. */
2351 if (gcp
->data
.size
== 1) {
2352 tty_attributes(tty
, gcp
, defaults
, palette
, hl
);
2353 if (*gcp
->data
.data
< 0x20 || *gcp
->data
.data
== 0x7f)
2355 tty_putc(tty
, *gcp
->data
.data
);
2359 /* Write the data. */
2360 tty_putn(tty
, gcp
->data
.data
, gcp
->data
.size
, gcp
->data
.width
);
2364 tty_reset(struct tty
*tty
)
2366 struct grid_cell
*gc
= &tty
->cell
;
2368 if (!grid_cells_equal(gc
, &grid_default_cell
)) {
2370 tty_putcode_ss(tty
, TTYC_HLS
, "", "");
2371 if ((gc
->attr
& GRID_ATTR_CHARSET
) && tty_acs_needed(tty
))
2372 tty_putcode(tty
, TTYC_RMACS
);
2373 tty_putcode(tty
, TTYC_SGR0
);
2374 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2376 memcpy(&tty
->last_cell
, &grid_default_cell
, sizeof tty
->last_cell
);
2380 tty_invalidate(struct tty
*tty
)
2382 memcpy(&tty
->cell
, &grid_default_cell
, sizeof tty
->cell
);
2383 memcpy(&tty
->last_cell
, &grid_default_cell
, sizeof tty
->last_cell
);
2385 tty
->cx
= tty
->cy
= UINT_MAX
;
2386 tty
->rupper
= tty
->rleft
= UINT_MAX
;
2387 tty
->rlower
= tty
->rright
= UINT_MAX
;
2389 if (tty
->flags
& TTY_STARTED
) {
2390 if (tty_use_margin(tty
))
2391 tty_putcode(tty
, TTYC_ENMG
);
2392 tty_putcode(tty
, TTYC_SGR0
);
2394 tty
->mode
= ALL_MODES
;
2395 tty_update_mode(tty
, MODE_CURSOR
, NULL
);
2397 tty_cursor(tty
, 0, 0);
2398 tty_region_off(tty
);
2399 tty_margin_off(tty
);
2401 tty
->mode
= MODE_CURSOR
;
2404 /* Turn off margin. */
2406 tty_region_off(struct tty
*tty
)
2408 tty_region(tty
, 0, tty
->sy
- 1);
2411 /* Set region inside pane. */
2413 tty_region_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int rupper
,
2416 tty_region(tty
, ctx
->yoff
+ rupper
- ctx
->woy
,
2417 ctx
->yoff
+ rlower
- ctx
->woy
);
2420 /* Set region at absolute position. */
2422 tty_region(struct tty
*tty
, u_int rupper
, u_int rlower
)
2424 if (tty
->rlower
== rlower
&& tty
->rupper
== rupper
)
2426 if (!tty_term_has(tty
->term
, TTYC_CSR
))
2429 tty
->rupper
= rupper
;
2430 tty
->rlower
= rlower
;
2433 * Some terminals (such as PuTTY) do not correctly reset the cursor to
2434 * 0,0 if it is beyond the last column (they do not reset their wrap
2435 * flag so further output causes a line feed). As a workaround, do an
2436 * explicit move to 0 first.
2438 if (tty
->cx
>= tty
->sx
) {
2439 if (tty
->cy
== UINT_MAX
)
2440 tty_cursor(tty
, 0, 0);
2442 tty_cursor(tty
, 0, tty
->cy
);
2445 tty_putcode_ii(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
2446 tty
->cx
= tty
->cy
= UINT_MAX
;
2449 /* Turn off margin. */
2451 tty_margin_off(struct tty
*tty
)
2453 tty_margin(tty
, 0, tty
->sx
- 1);
2456 /* Set margin inside pane. */
2458 tty_margin_pane(struct tty
*tty
, const struct tty_ctx
*ctx
)
2460 tty_margin(tty
, ctx
->xoff
- ctx
->wox
,
2461 ctx
->xoff
+ ctx
->sx
- 1 - ctx
->wox
);
2464 /* Set margin at absolute position. */
2466 tty_margin(struct tty
*tty
, u_int rleft
, u_int rright
)
2468 if (!tty_use_margin(tty
))
2470 if (tty
->rleft
== rleft
&& tty
->rright
== rright
)
2473 tty_putcode_ii(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
2476 tty
->rright
= rright
;
2478 if (rleft
== 0 && rright
== tty
->sx
- 1)
2479 tty_putcode(tty
, TTYC_CLMG
);
2481 tty_putcode_ii(tty
, TTYC_CMG
, rleft
, rright
);
2482 tty
->cx
= tty
->cy
= UINT_MAX
;
2486 * Move the cursor, unless it would wrap itself when the next character is
2490 tty_cursor_pane_unless_wrap(struct tty
*tty
, const struct tty_ctx
*ctx
,
2493 if (!ctx
->wrapped
||
2494 !tty_full_width(tty
, ctx
) ||
2495 (tty
->term
->flags
& TERM_NOAM
) ||
2496 ctx
->xoff
+ cx
!= 0 ||
2497 ctx
->yoff
+ cy
!= tty
->cy
+ 1 ||
2498 tty
->cx
< tty
->sx
||
2499 tty
->cy
== tty
->rlower
)
2500 tty_cursor_pane(tty
, ctx
, cx
, cy
);
2502 log_debug("%s: will wrap at %u,%u", __func__
, tty
->cx
, tty
->cy
);
2505 /* Move cursor inside pane. */
2507 tty_cursor_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int cx
, u_int cy
)
2509 tty_cursor(tty
, ctx
->xoff
+ cx
- ctx
->wox
, ctx
->yoff
+ cy
- ctx
->woy
);
2512 /* Move cursor to absolute position. */
2514 tty_cursor(struct tty
*tty
, u_int cx
, u_int cy
)
2516 struct tty_term
*term
= tty
->term
;
2520 if (tty
->flags
& TTY_BLOCK
)
2527 * If in the automargin space, and want to be there, do not move.
2528 * Otherwise, force the cursor to be in range (and complain).
2530 if (cx
== thisx
&& cy
== thisy
&& cx
== tty
->sx
)
2532 if (cx
> tty
->sx
- 1) {
2533 log_debug("%s: x too big %u > %u", __func__
, cx
, tty
->sx
- 1);
2538 if (cx
== thisx
&& cy
== thisy
)
2541 /* Currently at the very end of the line - use absolute movement. */
2542 if (thisx
> tty
->sx
- 1)
2545 /* Move to home position (0, 0). */
2546 if (cx
== 0 && cy
== 0 && tty_term_has(term
, TTYC_HOME
)) {
2547 tty_putcode(tty
, TTYC_HOME
);
2551 /* Zero on the next line. */
2552 if (cx
== 0 && cy
== thisy
+ 1 && thisy
!= tty
->rlower
&&
2553 (!tty_use_margin(tty
) || tty
->rleft
== 0)) {
2554 tty_putc(tty
, '\r');
2555 tty_putc(tty
, '\n');
2559 /* Moving column or row. */
2562 * Moving column only, row staying the same.
2566 if (cx
== 0 && (!tty_use_margin(tty
) || tty
->rleft
== 0)) {
2567 tty_putc(tty
, '\r');
2571 /* One to the left. */
2572 if (cx
== thisx
- 1 && tty_term_has(term
, TTYC_CUB1
)) {
2573 tty_putcode(tty
, TTYC_CUB1
);
2577 /* One to the right. */
2578 if (cx
== thisx
+ 1 && tty_term_has(term
, TTYC_CUF1
)) {
2579 tty_putcode(tty
, TTYC_CUF1
);
2583 /* Calculate difference. */
2584 change
= thisx
- cx
; /* +ve left, -ve right */
2587 * Use HPA if change is larger than absolute, otherwise move
2588 * the cursor with CUB/CUF.
2590 if ((u_int
) abs(change
) > cx
&& tty_term_has(term
, TTYC_HPA
)) {
2591 tty_putcode_i(tty
, TTYC_HPA
, cx
);
2593 } else if (change
> 0 &&
2594 tty_term_has(term
, TTYC_CUB
) &&
2595 !tty_use_margin(tty
)) {
2596 if (change
== 2 && tty_term_has(term
, TTYC_CUB1
)) {
2597 tty_putcode(tty
, TTYC_CUB1
);
2598 tty_putcode(tty
, TTYC_CUB1
);
2601 tty_putcode_i(tty
, TTYC_CUB
, change
);
2603 } else if (change
< 0 &&
2604 tty_term_has(term
, TTYC_CUF
) &&
2605 !tty_use_margin(tty
)) {
2606 tty_putcode_i(tty
, TTYC_CUF
, -change
);
2609 } else if (cx
== thisx
) {
2611 * Moving row only, column staying the same.
2615 if (thisy
!= tty
->rupper
&&
2616 cy
== thisy
- 1 && tty_term_has(term
, TTYC_CUU1
)) {
2617 tty_putcode(tty
, TTYC_CUU1
);
2622 if (thisy
!= tty
->rlower
&&
2623 cy
== thisy
+ 1 && tty_term_has(term
, TTYC_CUD1
)) {
2624 tty_putcode(tty
, TTYC_CUD1
);
2628 /* Calculate difference. */
2629 change
= thisy
- cy
; /* +ve up, -ve down */
2632 * Try to use VPA if change is larger than absolute or if this
2633 * change would cross the scroll region, otherwise use CUU/CUD.
2635 if ((u_int
) abs(change
) > cy
||
2636 (change
< 0 && cy
- change
> tty
->rlower
) ||
2637 (change
> 0 && cy
- change
< tty
->rupper
)) {
2638 if (tty_term_has(term
, TTYC_VPA
)) {
2639 tty_putcode_i(tty
, TTYC_VPA
, cy
);
2642 } else if (change
> 0 && tty_term_has(term
, TTYC_CUU
)) {
2643 tty_putcode_i(tty
, TTYC_CUU
, change
);
2645 } else if (change
< 0 && tty_term_has(term
, TTYC_CUD
)) {
2646 tty_putcode_i(tty
, TTYC_CUD
, -change
);
2652 /* Absolute movement. */
2653 tty_putcode_ii(tty
, TTYC_CUP
, cy
, cx
);
2661 tty_hyperlink(struct tty
*tty
, const struct grid_cell
*gc
,
2662 struct hyperlinks
*hl
)
2664 const char *uri
, *id
;
2666 if (gc
->link
== tty
->cell
.link
)
2668 tty
->cell
.link
= gc
->link
;
2673 if (gc
->link
== 0 || !hyperlinks_get(hl
, gc
->link
, &uri
, NULL
, &id
))
2674 tty_putcode_ss(tty
, TTYC_HLS
, "", "");
2676 tty_putcode_ss(tty
, TTYC_HLS
, id
, uri
);
2680 tty_attributes(struct tty
*tty
, const struct grid_cell
*gc
,
2681 const struct grid_cell
*defaults
, struct colour_palette
*palette
,
2682 struct hyperlinks
*hl
)
2684 struct grid_cell
*tc
= &tty
->cell
, gc2
;
2687 /* Copy cell and update default colours. */
2688 memcpy(&gc2
, gc
, sizeof gc2
);
2689 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2691 gc2
.fg
= defaults
->fg
;
2693 gc2
.bg
= defaults
->bg
;
2696 /* Ignore cell if it is the same as the last one. */
2697 if (gc2
.attr
== tty
->last_cell
.attr
&&
2698 gc2
.fg
== tty
->last_cell
.fg
&&
2699 gc2
.bg
== tty
->last_cell
.bg
&&
2700 gc2
.us
== tty
->last_cell
.us
&&
2701 gc2
.link
== tty
->last_cell
.link
)
2705 * If no setab, try to use the reverse attribute as a best-effort for a
2706 * non-default background. This is a bit of a hack but it doesn't do
2707 * any serious harm and makes a couple of applications happier.
2709 if (!tty_term_has(tty
->term
, TTYC_SETAB
)) {
2710 if (gc2
.attr
& GRID_ATTR_REVERSE
) {
2711 if (gc2
.fg
!= 7 && !COLOUR_DEFAULT(gc2
.fg
))
2712 gc2
.attr
&= ~GRID_ATTR_REVERSE
;
2714 if (gc2
.bg
!= 0 && !COLOUR_DEFAULT(gc2
.bg
))
2715 gc2
.attr
|= GRID_ATTR_REVERSE
;
2719 /* Fix up the colours if necessary. */
2720 tty_check_fg(tty
, palette
, &gc2
);
2721 tty_check_bg(tty
, palette
, &gc2
);
2722 tty_check_us(tty
, palette
, &gc2
);
2725 * If any bits are being cleared or the underline colour is now default,
2728 if ((tc
->attr
& ~gc2
.attr
) || (tc
->us
!= gc2
.us
&& gc2
.us
== 0))
2732 * Set the colours. This may call tty_reset() (so it comes next) and
2733 * may add to (NOT remove) the desired attributes.
2735 tty_colours(tty
, &gc2
);
2737 /* Filter out attribute bits already set. */
2738 changed
= gc2
.attr
& ~tc
->attr
;
2739 tc
->attr
= gc2
.attr
;
2741 /* Set the attributes. */
2742 if (changed
& GRID_ATTR_BRIGHT
)
2743 tty_putcode(tty
, TTYC_BOLD
);
2744 if (changed
& GRID_ATTR_DIM
)
2745 tty_putcode(tty
, TTYC_DIM
);
2746 if (changed
& GRID_ATTR_ITALICS
)
2747 tty_set_italics(tty
);
2748 if (changed
& GRID_ATTR_ALL_UNDERSCORE
) {
2749 if ((changed
& GRID_ATTR_UNDERSCORE
) ||
2750 !tty_term_has(tty
->term
, TTYC_SMULX
))
2751 tty_putcode(tty
, TTYC_SMUL
);
2752 else if (changed
& GRID_ATTR_UNDERSCORE_2
)
2753 tty_putcode_i(tty
, TTYC_SMULX
, 2);
2754 else if (changed
& GRID_ATTR_UNDERSCORE_3
)
2755 tty_putcode_i(tty
, TTYC_SMULX
, 3);
2756 else if (changed
& GRID_ATTR_UNDERSCORE_4
)
2757 tty_putcode_i(tty
, TTYC_SMULX
, 4);
2758 else if (changed
& GRID_ATTR_UNDERSCORE_5
)
2759 tty_putcode_i(tty
, TTYC_SMULX
, 5);
2761 if (changed
& GRID_ATTR_BLINK
)
2762 tty_putcode(tty
, TTYC_BLINK
);
2763 if (changed
& GRID_ATTR_REVERSE
) {
2764 if (tty_term_has(tty
->term
, TTYC_REV
))
2765 tty_putcode(tty
, TTYC_REV
);
2766 else if (tty_term_has(tty
->term
, TTYC_SMSO
))
2767 tty_putcode(tty
, TTYC_SMSO
);
2769 if (changed
& GRID_ATTR_HIDDEN
)
2770 tty_putcode(tty
, TTYC_INVIS
);
2771 if (changed
& GRID_ATTR_STRIKETHROUGH
)
2772 tty_putcode(tty
, TTYC_SMXX
);
2773 if (changed
& GRID_ATTR_OVERLINE
)
2774 tty_putcode(tty
, TTYC_SMOL
);
2775 if ((changed
& GRID_ATTR_CHARSET
) && tty_acs_needed(tty
))
2776 tty_putcode(tty
, TTYC_SMACS
);
2778 /* Set hyperlink if any. */
2779 tty_hyperlink(tty
, gc
, hl
);
2781 memcpy(&tty
->last_cell
, &gc2
, sizeof tty
->last_cell
);
2785 tty_colours(struct tty
*tty
, const struct grid_cell
*gc
)
2787 struct grid_cell
*tc
= &tty
->cell
;
2789 /* No changes? Nothing is necessary. */
2790 if (gc
->fg
== tc
->fg
&& gc
->bg
== tc
->bg
&& gc
->us
== tc
->us
)
2794 * Is either the default colour? This is handled specially because the
2795 * best solution might be to reset both colours to default, in which
2796 * case if only one is default need to fall onward to set the other
2799 if (COLOUR_DEFAULT(gc
->fg
) || COLOUR_DEFAULT(gc
->bg
)) {
2801 * If don't have AX, send sgr0. This resets both colours to default.
2802 * Otherwise, try to set the default colour only as needed.
2804 if (!tty_term_flag(tty
->term
, TTYC_AX
))
2807 if (COLOUR_DEFAULT(gc
->fg
) && !COLOUR_DEFAULT(tc
->fg
)) {
2808 tty_puts(tty
, "\033[39m");
2811 if (COLOUR_DEFAULT(gc
->bg
) && !COLOUR_DEFAULT(tc
->bg
)) {
2812 tty_puts(tty
, "\033[49m");
2818 /* Set the foreground colour. */
2819 if (!COLOUR_DEFAULT(gc
->fg
) && gc
->fg
!= tc
->fg
)
2820 tty_colours_fg(tty
, gc
);
2823 * Set the background colour. This must come after the foreground as
2824 * tty_colours_fg() can call tty_reset().
2826 if (!COLOUR_DEFAULT(gc
->bg
) && gc
->bg
!= tc
->bg
)
2827 tty_colours_bg(tty
, gc
);
2829 /* Set the underscore colour. */
2830 if (gc
->us
!= tc
->us
)
2831 tty_colours_us(tty
, gc
);
2835 tty_check_fg(struct tty
*tty
, struct colour_palette
*palette
,
2836 struct grid_cell
*gc
)
2843 * Perform substitution if this pane has a palette. If the bright
2844 * attribute is set and Nobr is not present, use the bright entry in
2845 * the palette by changing to the aixterm colour
2847 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2850 gc
->attr
& GRID_ATTR_BRIGHT
&&
2851 !tty_term_has(tty
->term
, TTYC_NOBR
))
2853 if ((c
= colour_palette_get(palette
, c
)) != -1)
2857 /* Is this a 24-bit colour? */
2858 if (gc
->fg
& COLOUR_FLAG_RGB
) {
2859 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2860 if (tty
->term
->flags
& TERM_RGBCOLOURS
)
2862 colour_split_rgb(gc
->fg
, &r
, &g
, &b
);
2863 gc
->fg
= colour_find_rgb(r
, g
, b
);
2866 /* How many colours does this terminal have? */
2867 if (tty
->term
->flags
& TERM_256COLOURS
)
2870 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
2872 /* Is this a 256-colour colour? */
2873 if (gc
->fg
& COLOUR_FLAG_256
) {
2874 /* And not a 256 colour mode? */
2875 if (colours
< 256) {
2876 gc
->fg
= colour_256to16(gc
->fg
);
2886 /* Is this an aixterm colour? */
2887 if (gc
->fg
>= 90 && gc
->fg
<= 97 && colours
< 16) {
2889 gc
->attr
|= GRID_ATTR_BRIGHT
;
2894 tty_check_bg(struct tty
*tty
, struct colour_palette
*palette
,
2895 struct grid_cell
*gc
)
2901 /* Perform substitution if this pane has a palette. */
2902 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2903 if ((c
= colour_palette_get(palette
, gc
->bg
)) != -1)
2907 /* Is this a 24-bit colour? */
2908 if (gc
->bg
& COLOUR_FLAG_RGB
) {
2909 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2910 if (tty
->term
->flags
& TERM_RGBCOLOURS
)
2912 colour_split_rgb(gc
->bg
, &r
, &g
, &b
);
2913 gc
->bg
= colour_find_rgb(r
, g
, b
);
2916 /* How many colours does this terminal have? */
2917 if (tty
->term
->flags
& TERM_256COLOURS
)
2920 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
2922 /* Is this a 256-colour colour? */
2923 if (gc
->bg
& COLOUR_FLAG_256
) {
2925 * And not a 256 colour mode? Translate to 16-colour
2926 * palette. Bold background doesn't exist portably, so just
2927 * discard the bold bit if set.
2929 if (colours
< 256) {
2930 gc
->bg
= colour_256to16(gc
->bg
);
2940 /* Is this an aixterm colour? */
2941 if (gc
->bg
>= 90 && gc
->bg
<= 97 && colours
< 16)
2946 tty_check_us(__unused
struct tty
*tty
, struct colour_palette
*palette
,
2947 struct grid_cell
*gc
)
2951 /* Perform substitution if this pane has a palette. */
2952 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2953 if ((c
= colour_palette_get(palette
, gc
->us
)) != -1)
2957 /* Convert underscore colour if only RGB can be supported. */
2958 if (!tty_term_has(tty
->term
, TTYC_SETULC1
)) {
2959 if ((c
= colour_force_rgb (gc
->us
)) == -1)
2967 tty_colours_fg(struct tty
*tty
, const struct grid_cell
*gc
)
2969 struct grid_cell
*tc
= &tty
->cell
;
2973 * If the current colour is an aixterm bright colour and the new is not,
2974 * reset because some terminals do not clear bright correctly.
2976 if (tty
->cell
.fg
>= 90 &&
2977 tty
->cell
.bg
<= 97 &&
2978 (gc
->fg
< 90 || gc
->fg
> 97))
2981 /* Is this a 24-bit or 256-colour colour? */
2982 if (gc
->fg
& COLOUR_FLAG_RGB
|| gc
->fg
& COLOUR_FLAG_256
) {
2983 if (tty_try_colour(tty
, gc
->fg
, "38") == 0)
2985 /* Should not get here, already converted in tty_check_fg. */
2989 /* Is this an aixterm bright colour? */
2990 if (gc
->fg
>= 90 && gc
->fg
<= 97) {
2991 if (tty
->term
->flags
& TERM_256COLOURS
) {
2992 xsnprintf(s
, sizeof s
, "\033[%dm", gc
->fg
);
2995 tty_putcode_i(tty
, TTYC_SETAF
, gc
->fg
- 90 + 8);
2999 /* Otherwise set the foreground colour. */
3000 tty_putcode_i(tty
, TTYC_SETAF
, gc
->fg
);
3003 /* Save the new values in the terminal current cell. */
3008 tty_colours_bg(struct tty
*tty
, const struct grid_cell
*gc
)
3010 struct grid_cell
*tc
= &tty
->cell
;
3013 /* Is this a 24-bit or 256-colour colour? */
3014 if (gc
->bg
& COLOUR_FLAG_RGB
|| gc
->bg
& COLOUR_FLAG_256
) {
3015 if (tty_try_colour(tty
, gc
->bg
, "48") == 0)
3017 /* Should not get here, already converted in tty_check_bg. */
3021 /* Is this an aixterm bright colour? */
3022 if (gc
->bg
>= 90 && gc
->bg
<= 97) {
3023 if (tty
->term
->flags
& TERM_256COLOURS
) {
3024 xsnprintf(s
, sizeof s
, "\033[%dm", gc
->bg
+ 10);
3027 tty_putcode_i(tty
, TTYC_SETAB
, gc
->bg
- 90 + 8);
3031 /* Otherwise set the background colour. */
3032 tty_putcode_i(tty
, TTYC_SETAB
, gc
->bg
);
3035 /* Save the new values in the terminal current cell. */
3040 tty_colours_us(struct tty
*tty
, const struct grid_cell
*gc
)
3042 struct grid_cell
*tc
= &tty
->cell
;
3046 /* Clear underline colour. */
3047 if (COLOUR_DEFAULT(gc
->us
)) {
3048 tty_putcode(tty
, TTYC_OL
);
3053 * If this is not an RGB colour, use Setulc1 if it exists, otherwise
3056 if (~gc
->us
& COLOUR_FLAG_RGB
) {
3058 if ((~c
& COLOUR_FLAG_256
) && (c
>= 90 && c
<= 97))
3060 tty_putcode_i(tty
, TTYC_SETULC1
, c
& ~COLOUR_FLAG_256
);
3065 * Setulc and setal follows the ncurses(3) one argument "direct colour"
3066 * capability format. Calculate the colour value.
3068 colour_split_rgb(gc
->us
, &r
, &g
, &b
);
3069 c
= (65536 * r
) + (256 * g
) + b
;
3072 * Write the colour. Only use setal if the RGB flag is set because the
3073 * non-RGB version may be wrong.
3075 if (tty_term_has(tty
->term
, TTYC_SETULC
))
3076 tty_putcode_i(tty
, TTYC_SETULC
, c
);
3077 else if (tty_term_has(tty
->term
, TTYC_SETAL
) &&
3078 tty_term_has(tty
->term
, TTYC_RGB
))
3079 tty_putcode_i(tty
, TTYC_SETAL
, c
);
3082 /* Save the new values in the terminal current cell. */
3087 tty_try_colour(struct tty
*tty
, int colour
, const char *type
)
3091 if (colour
& COLOUR_FLAG_256
) {
3092 if (*type
== '3' && tty_term_has(tty
->term
, TTYC_SETAF
))
3093 tty_putcode_i(tty
, TTYC_SETAF
, colour
& 0xff);
3094 else if (tty_term_has(tty
->term
, TTYC_SETAB
))
3095 tty_putcode_i(tty
, TTYC_SETAB
, colour
& 0xff);
3099 if (colour
& COLOUR_FLAG_RGB
) {
3100 colour_split_rgb(colour
& 0xffffff, &r
, &g
, &b
);
3101 if (*type
== '3' && tty_term_has(tty
->term
, TTYC_SETRGBF
))
3102 tty_putcode_iii(tty
, TTYC_SETRGBF
, r
, g
, b
);
3103 else if (tty_term_has(tty
->term
, TTYC_SETRGBB
))
3104 tty_putcode_iii(tty
, TTYC_SETRGBB
, r
, g
, b
);
3112 tty_window_default_style(struct grid_cell
*gc
, struct window_pane
*wp
)
3114 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
3115 gc
->fg
= wp
->palette
.fg
;
3116 gc
->bg
= wp
->palette
.bg
;
3120 tty_default_colours(struct grid_cell
*gc
, struct window_pane
*wp
)
3122 struct options
*oo
= wp
->options
;
3123 struct format_tree
*ft
;
3125 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
3127 if (wp
->flags
& PANE_STYLECHANGED
) {
3128 log_debug("%%%u: style changed", wp
->id
);
3129 wp
->flags
&= ~PANE_STYLECHANGED
;
3131 ft
= format_create(NULL
, NULL
, FORMAT_PANE
|wp
->id
,
3133 format_defaults(ft
, NULL
, NULL
, NULL
, wp
);
3134 tty_window_default_style(&wp
->cached_active_gc
, wp
);
3135 style_add(&wp
->cached_active_gc
, oo
, "window-active-style", ft
);
3136 tty_window_default_style(&wp
->cached_gc
, wp
);
3137 style_add(&wp
->cached_gc
, oo
, "window-style", ft
);
3142 if (wp
== wp
->window
->active
&& wp
->cached_active_gc
.fg
!= 8)
3143 gc
->fg
= wp
->cached_active_gc
.fg
;
3145 gc
->fg
= wp
->cached_gc
.fg
;
3149 if (wp
== wp
->window
->active
&& wp
->cached_active_gc
.bg
!= 8)
3150 gc
->bg
= wp
->cached_active_gc
.bg
;
3152 gc
->bg
= wp
->cached_gc
.bg
;
3157 tty_default_attributes(struct tty
*tty
, const struct grid_cell
*defaults
,
3158 struct colour_palette
*palette
, u_int bg
, struct hyperlinks
*hl
)
3160 struct grid_cell gc
;
3162 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
3164 tty_attributes(tty
, &gc
, defaults
, palette
, hl
);
3168 tty_clipboard_query_callback(__unused
int fd
, __unused
short events
, void *data
)
3170 struct tty
*tty
= data
;
3171 struct client
*c
= tty
->client
;
3173 c
->flags
&= ~CLIENT_CLIPBOARDBUFFER
;
3174 free(c
->clipboard_panes
);
3175 c
->clipboard_panes
= NULL
;
3176 c
->clipboard_npanes
= 0;
3178 tty
->flags
&= ~TTY_OSC52QUERY
;
3182 tty_clipboard_query(struct tty
*tty
)
3184 struct timeval tv
= { .tv_sec
= TTY_QUERY_TIMEOUT
};
3186 if ((~tty
->flags
& TTY_STARTED
) || (tty
->flags
& TTY_OSC52QUERY
))
3188 tty_putcode_ss(tty
, TTYC_MS
, "", "?");
3190 tty
->flags
|= TTY_OSC52QUERY
;
3191 evtimer_set(&tty
->clipboard_timer
, tty_clipboard_query_callback
, tty
);
3192 evtimer_add(&tty
->clipboard_timer
, &tv
);