4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
22 #include <netinet/in.h>
35 static int tty_log_fd
= -1;
37 static int tty_client_ready(struct client
*);
39 static void tty_set_italics(struct tty
*);
40 static int tty_try_colour(struct tty
*, int, const char *);
41 static void tty_force_cursor_colour(struct tty
*, int);
42 static void tty_cursor_pane(struct tty
*, const struct tty_ctx
*, u_int
,
44 static void tty_cursor_pane_unless_wrap(struct tty
*,
45 const struct tty_ctx
*, u_int
, u_int
);
46 static void tty_invalidate(struct tty
*);
47 static void tty_colours(struct tty
*, const struct grid_cell
*);
48 static void tty_check_fg(struct tty
*, struct colour_palette
*,
50 static void tty_check_bg(struct tty
*, struct colour_palette
*,
52 static void tty_check_us(struct tty
*, struct colour_palette
*,
54 static void tty_colours_fg(struct tty
*, const struct grid_cell
*);
55 static void tty_colours_bg(struct tty
*, const struct grid_cell
*);
56 static void tty_colours_us(struct tty
*, const struct grid_cell
*);
58 static void tty_region_pane(struct tty
*, const struct tty_ctx
*, u_int
,
60 static void tty_region(struct tty
*, u_int
, u_int
);
61 static void tty_margin_pane(struct tty
*, const struct tty_ctx
*);
62 static void tty_margin(struct tty
*, u_int
, u_int
);
63 static int tty_large_region(struct tty
*, const struct tty_ctx
*);
64 static int tty_fake_bce(const struct tty
*, const struct grid_cell
*,
66 static void tty_redraw_region(struct tty
*, const struct tty_ctx
*);
67 static void tty_emulate_repeat(struct tty
*, enum tty_code_code
,
68 enum tty_code_code
, u_int
);
69 static void tty_repeat_space(struct tty
*, u_int
);
70 static void tty_draw_pane(struct tty
*, const struct tty_ctx
*, u_int
);
71 static void tty_default_attributes(struct tty
*, const struct grid_cell
*,
72 struct colour_palette
*, u_int
, struct hyperlinks
*);
73 static int tty_check_overlay(struct tty
*, u_int
, u_int
);
74 static void tty_check_overlay_range(struct tty
*, u_int
, u_int
, u_int
,
75 struct overlay_ranges
*);
77 #define tty_use_margin(tty) \
78 (tty->term->flags & TERM_DECSLRM)
79 #define tty_full_width(tty, ctx) \
80 ((ctx)->xoff == 0 && (ctx)->sx >= (tty)->sx)
82 #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
83 #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
84 #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
86 #define TTY_QUERY_TIMEOUT 5
93 xsnprintf(name
, sizeof name
, "tmux-out-%ld.log", (long)getpid());
95 tty_log_fd
= open(name
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
96 if (tty_log_fd
!= -1 && fcntl(tty_log_fd
, F_SETFD
, FD_CLOEXEC
) == -1)
97 fatal("fcntl failed");
101 tty_init(struct tty
*tty
, struct client
*c
)
106 memset(tty
, 0, sizeof *tty
);
109 tty
->cstyle
= SCREEN_CURSOR_DEFAULT
;
112 if (tcgetattr(c
->fd
, &tty
->tio
) != 0)
118 tty_resize(struct tty
*tty
)
120 struct client
*c
= tty
->client
;
122 u_int sx
, sy
, xpixel
, ypixel
;
124 if (ioctl(c
->fd
, TIOCGWINSZ
, &ws
) != -1) {
130 xpixel
= ws
.ws_xpixel
/ sx
;
136 ypixel
= ws
.ws_ypixel
/ sy
;
143 log_debug("%s: %s now %ux%u (%ux%u)", __func__
, c
->name
, sx
, sy
,
145 tty_set_size(tty
, sx
, sy
, xpixel
, ypixel
);
150 tty_set_size(struct tty
*tty
, u_int sx
, u_int sy
, u_int xpixel
, u_int ypixel
)
154 tty
->xpixel
= xpixel
;
155 tty
->ypixel
= ypixel
;
159 tty_read_callback(__unused
int fd
, __unused
short events
, void *data
)
161 struct tty
*tty
= data
;
162 struct client
*c
= tty
->client
;
163 const char *name
= c
->name
;
164 size_t size
= EVBUFFER_LENGTH(tty
->in
);
167 nread
= evbuffer_read(tty
->in
, c
->fd
, -1);
168 if (nread
== 0 || nread
== -1) {
170 log_debug("%s: read closed", name
);
172 log_debug("%s: read error: %s", name
, strerror(errno
));
173 event_del(&tty
->event_in
);
174 server_client_lost(tty
->client
);
177 log_debug("%s: read %d bytes (already %zu)", name
, nread
, size
);
179 while (tty_keys_next(tty
))
184 tty_timer_callback(__unused
int fd
, __unused
short events
, void *data
)
186 struct tty
*tty
= data
;
187 struct client
*c
= tty
->client
;
188 struct timeval tv
= { .tv_usec
= TTY_BLOCK_INTERVAL
};
190 log_debug("%s: %zu discarded", c
->name
, tty
->discarded
);
192 c
->flags
|= CLIENT_ALLREDRAWFLAGS
;
193 c
->discarded
+= tty
->discarded
;
195 if (tty
->discarded
< TTY_BLOCK_STOP(tty
)) {
196 tty
->flags
&= ~TTY_BLOCK
;
201 evtimer_add(&tty
->timer
, &tv
);
205 tty_block_maybe(struct tty
*tty
)
207 struct client
*c
= tty
->client
;
208 size_t size
= EVBUFFER_LENGTH(tty
->out
);
209 struct timeval tv
= { .tv_usec
= TTY_BLOCK_INTERVAL
};
212 tty
->flags
&= ~TTY_NOBLOCK
;
213 else if (tty
->flags
& TTY_NOBLOCK
)
216 if (size
< TTY_BLOCK_START(tty
))
219 if (tty
->flags
& TTY_BLOCK
)
221 tty
->flags
|= TTY_BLOCK
;
223 log_debug("%s: can't keep up, %zu discarded", c
->name
, size
);
225 evbuffer_drain(tty
->out
, size
);
226 c
->discarded
+= size
;
229 evtimer_add(&tty
->timer
, &tv
);
234 tty_write_callback(__unused
int fd
, __unused
short events
, void *data
)
236 struct tty
*tty
= data
;
237 struct client
*c
= tty
->client
;
238 size_t size
= EVBUFFER_LENGTH(tty
->out
);
241 nwrite
= evbuffer_write(tty
->out
, c
->fd
);
244 log_debug("%s: wrote %d bytes (of %zu)", c
->name
, nwrite
, size
);
247 if ((size_t)nwrite
>= c
->redraw
)
251 log_debug("%s: waiting for redraw, %zu bytes left", c
->name
,
253 } else if (tty_block_maybe(tty
))
256 if (EVBUFFER_LENGTH(tty
->out
) != 0)
257 event_add(&tty
->event_out
, NULL
);
261 tty_open(struct tty
*tty
, char **cause
)
263 struct client
*c
= tty
->client
;
265 tty
->term
= tty_term_create(tty
, c
->term_name
, c
->term_caps
,
266 c
->term_ncaps
, &c
->term_features
, cause
);
267 if (tty
->term
== NULL
) {
271 tty
->flags
|= TTY_OPENED
;
273 tty
->flags
&= ~(TTY_NOCURSOR
|TTY_FREEZE
|TTY_BLOCK
|TTY_TIMER
);
275 event_set(&tty
->event_in
, c
->fd
, EV_PERSIST
|EV_READ
,
276 tty_read_callback
, tty
);
277 tty
->in
= evbuffer_new();
279 fatal("out of memory");
281 event_set(&tty
->event_out
, c
->fd
, EV_WRITE
, tty_write_callback
, tty
);
282 tty
->out
= evbuffer_new();
283 if (tty
->out
== NULL
)
284 fatal("out of memory");
286 evtimer_set(&tty
->timer
, tty_timer_callback
, tty
);
296 tty_start_timer_callback(__unused
int fd
, __unused
short events
, void *data
)
298 struct tty
*tty
= data
;
299 struct client
*c
= tty
->client
;
301 log_debug("%s: start timer fired", c
->name
);
302 if ((tty
->flags
& (TTY_HAVEDA
|TTY_HAVEXDA
)) == 0)
303 tty_update_features(tty
);
304 tty
->flags
|= (TTY_HAVEDA
|TTY_HAVEXDA
);
308 tty_start_tty(struct tty
*tty
)
310 struct client
*c
= tty
->client
;
312 struct timeval tv
= { .tv_sec
= TTY_QUERY_TIMEOUT
};
314 setblocking(c
->fd
, 0);
315 event_add(&tty
->event_in
, NULL
);
317 memcpy(&tio
, &tty
->tio
, sizeof tio
);
318 tio
.c_iflag
&= ~(IXON
|IXOFF
|ICRNL
|INLCR
|IGNCR
|IMAXBEL
|ISTRIP
);
319 tio
.c_iflag
|= IGNBRK
;
320 tio
.c_oflag
&= ~(OPOST
|ONLCR
|OCRNL
|ONLRET
);
321 tio
.c_lflag
&= ~(IEXTEN
|ICANON
|ECHO
|ECHOE
|ECHONL
|ECHOCTL
|ECHOPRT
|
325 if (tcsetattr(c
->fd
, TCSANOW
, &tio
) == 0)
326 tcflush(c
->fd
, TCOFLUSH
);
328 tty_putcode(tty
, TTYC_SMCUP
);
330 tty_putcode(tty
, TTYC_SMKX
);
331 tty_putcode(tty
, TTYC_CLEAR
);
333 if (tty_acs_needed(tty
)) {
334 log_debug("%s: using capabilities for ACS", c
->name
);
335 tty_putcode(tty
, TTYC_ENACS
);
337 log_debug("%s: using UTF-8 for ACS", c
->name
);
339 tty_putcode(tty
, TTYC_CNORM
);
340 if (tty_term_has(tty
->term
, TTYC_KMOUS
)) {
341 tty_puts(tty
, "\033[?1000l\033[?1002l\033[?1003l");
342 tty_puts(tty
, "\033[?1006l\033[?1005l");
345 evtimer_set(&tty
->start_timer
, tty_start_timer_callback
, tty
);
346 evtimer_add(&tty
->start_timer
, &tv
);
348 tty
->flags
|= TTY_STARTED
;
351 if (tty
->ccolour
!= -1)
352 tty_force_cursor_colour(tty
, -1);
354 tty
->mouse_drag_flag
= 0;
355 tty
->mouse_drag_update
= NULL
;
356 tty
->mouse_drag_release
= NULL
;
360 tty_send_requests(struct tty
*tty
)
362 if (~tty
->flags
& TTY_STARTED
)
365 if (tty
->term
->flags
& TERM_VT100LIKE
) {
366 if (~tty
->flags
& TTY_HAVEDA
)
367 tty_puts(tty
, "\033[>c");
368 if (~tty
->flags
& TTY_HAVEXDA
)
369 tty_puts(tty
, "\033[>q");
371 tty
->flags
|= (TTY_HAVEDA
|TTY_HAVEXDA
);
375 tty_stop_tty(struct tty
*tty
)
377 struct client
*c
= tty
->client
;
380 if (!(tty
->flags
& TTY_STARTED
))
382 tty
->flags
&= ~TTY_STARTED
;
384 evtimer_del(&tty
->start_timer
);
386 event_del(&tty
->timer
);
387 tty
->flags
&= ~TTY_BLOCK
;
389 event_del(&tty
->event_in
);
390 event_del(&tty
->event_out
);
393 * Be flexible about error handling and try not kill the server just
394 * because the fd is invalid. Things like ssh -t can easily leave us
397 if (ioctl(c
->fd
, TIOCGWINSZ
, &ws
) == -1)
399 if (tcsetattr(c
->fd
, TCSANOW
, &tty
->tio
) == -1)
402 tty_raw(tty
, tty_term_string2(tty
->term
, TTYC_CSR
, 0, ws
.ws_row
- 1));
403 if (tty_acs_needed(tty
))
404 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMACS
));
405 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_SGR0
));
406 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMKX
));
407 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CLEAR
));
408 if (tty
->cstyle
!= SCREEN_CURSOR_DEFAULT
) {
409 if (tty_term_has(tty
->term
, TTYC_SE
))
410 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_SE
));
411 else if (tty_term_has(tty
->term
, TTYC_SS
))
412 tty_raw(tty
, tty_term_string1(tty
->term
, TTYC_SS
, 0));
414 if (tty
->mode
& MODE_BRACKETPASTE
)
415 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSBP
));
416 if (tty
->ccolour
!= -1)
417 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CR
));
419 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_CNORM
));
420 if (tty_term_has(tty
->term
, TTYC_KMOUS
)) {
421 tty_raw(tty
, "\033[?1000l\033[?1002l\033[?1003l");
422 tty_raw(tty
, "\033[?1006l\033[?1005l");
425 if (tty
->term
->flags
& TERM_VT100LIKE
)
426 tty_raw(tty
, "\033[?7727l");
427 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSFCS
));
428 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSEKS
));
430 if (tty_use_margin(tty
))
431 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_DSMG
));
432 tty_raw(tty
, tty_term_string(tty
->term
, TTYC_RMCUP
));
434 setblocking(c
->fd
, 1);
438 tty_close(struct tty
*tty
)
440 if (event_initialized(&tty
->key_timer
))
441 evtimer_del(&tty
->key_timer
);
444 if (tty
->flags
& TTY_OPENED
) {
445 evbuffer_free(tty
->in
);
446 event_del(&tty
->event_in
);
447 evbuffer_free(tty
->out
);
448 event_del(&tty
->event_out
);
450 tty_term_free(tty
->term
);
453 tty
->flags
&= ~TTY_OPENED
;
458 tty_free(struct tty
*tty
)
464 tty_update_features(struct tty
*tty
)
466 struct client
*c
= tty
->client
;
468 if (tty_apply_features(tty
->term
, c
->term_features
))
469 tty_term_apply_overrides(tty
->term
);
471 if (tty_use_margin(tty
))
472 tty_putcode(tty
, TTYC_ENMG
);
473 if (options_get_number(global_options
, "extended-keys"))
474 tty_puts(tty
, tty_term_string(tty
->term
, TTYC_ENEKS
));
475 if (options_get_number(global_options
, "focus-events"))
476 tty_puts(tty
, tty_term_string(tty
->term
, TTYC_ENFCS
));
477 if (tty
->term
->flags
& TERM_VT100LIKE
)
478 tty_puts(tty
, "\033[?7727h");
482 tty_raw(struct tty
*tty
, const char *s
)
484 struct client
*c
= tty
->client
;
489 for (i
= 0; i
< 5; i
++) {
490 n
= write(c
->fd
, s
, slen
);
496 } else if (n
== -1 && errno
!= EAGAIN
)
503 tty_putcode(struct tty
*tty
, enum tty_code_code code
)
505 tty_puts(tty
, tty_term_string(tty
->term
, code
));
509 tty_putcode1(struct tty
*tty
, enum tty_code_code code
, int a
)
513 tty_puts(tty
, tty_term_string1(tty
->term
, code
, a
));
517 tty_putcode2(struct tty
*tty
, enum tty_code_code code
, int a
, int b
)
521 tty_puts(tty
, tty_term_string2(tty
->term
, code
, a
, b
));
525 tty_putcode3(struct tty
*tty
, enum tty_code_code code
, int a
, int b
, int c
)
527 if (a
< 0 || b
< 0 || c
< 0)
529 tty_puts(tty
, tty_term_string3(tty
->term
, code
, a
, b
, c
));
533 tty_putcode_ptr1(struct tty
*tty
, enum tty_code_code code
, const void *a
)
536 tty_puts(tty
, tty_term_ptr1(tty
->term
, code
, a
));
540 tty_putcode_ptr2(struct tty
*tty
, enum tty_code_code code
, const void *a
,
543 if (a
!= NULL
&& b
!= NULL
)
544 tty_puts(tty
, tty_term_ptr2(tty
->term
, code
, a
, b
));
548 tty_add(struct tty
*tty
, const char *buf
, size_t len
)
550 struct client
*c
= tty
->client
;
552 if (tty
->flags
& TTY_BLOCK
) {
553 tty
->discarded
+= len
;
557 evbuffer_add(tty
->out
, buf
, len
);
558 log_debug("%s: %.*s", c
->name
, (int)len
, buf
);
561 if (tty_log_fd
!= -1)
562 write(tty_log_fd
, buf
, len
);
563 if (tty
->flags
& TTY_STARTED
)
564 event_add(&tty
->event_out
, NULL
);
568 tty_puts(struct tty
*tty
, const char *s
)
571 tty_add(tty
, s
, strlen(s
));
575 tty_putc(struct tty
*tty
, u_char ch
)
579 if ((tty
->term
->flags
& TERM_NOAM
) &&
580 ch
>= 0x20 && ch
!= 0x7f &&
581 tty
->cy
== tty
->sy
- 1 &&
582 tty
->cx
+ 1 >= tty
->sx
)
585 if (tty
->cell
.attr
& GRID_ATTR_CHARSET
) {
586 acs
= tty_acs_get(tty
, ch
);
588 tty_add(tty
, acs
, strlen(acs
));
590 tty_add(tty
, &ch
, 1);
592 tty_add(tty
, &ch
, 1);
594 if (ch
>= 0x20 && ch
!= 0x7f) {
595 if (tty
->cx
>= tty
->sx
) {
597 if (tty
->cy
!= tty
->rlower
)
601 * On !am terminals, force the cursor position to where
602 * we think it should be after a line wrap - this means
603 * it works on sensible terminals as well.
605 if (tty
->term
->flags
& TERM_NOAM
)
606 tty_putcode2(tty
, TTYC_CUP
, tty
->cy
, tty
->cx
);
613 tty_putn(struct tty
*tty
, const void *buf
, size_t len
, u_int width
)
615 if ((tty
->term
->flags
& TERM_NOAM
) &&
616 tty
->cy
== tty
->sy
- 1 &&
617 tty
->cx
+ len
>= tty
->sx
)
618 len
= tty
->sx
- tty
->cx
- 1;
620 tty_add(tty
, buf
, len
);
621 if (tty
->cx
+ width
> tty
->sx
) {
622 tty
->cx
= (tty
->cx
+ width
) - tty
->sx
;
623 if (tty
->cx
<= tty
->sx
)
626 tty
->cx
= tty
->cy
= UINT_MAX
;
632 tty_set_italics(struct tty
*tty
)
636 if (tty_term_has(tty
->term
, TTYC_SITM
)) {
637 s
= options_get_string(global_options
, "default-terminal");
638 if (strcmp(s
, "screen") != 0 && strncmp(s
, "screen-", 7) != 0) {
639 tty_putcode(tty
, TTYC_SITM
);
643 tty_putcode(tty
, TTYC_SMSO
);
647 tty_set_title(struct tty
*tty
, const char *title
)
649 if (!tty_term_has(tty
->term
, TTYC_TSL
) ||
650 !tty_term_has(tty
->term
, TTYC_FSL
))
653 tty_putcode(tty
, TTYC_TSL
);
654 tty_puts(tty
, title
);
655 tty_putcode(tty
, TTYC_FSL
);
659 tty_set_path(struct tty
*tty
, const char *title
)
661 if (!tty_term_has(tty
->term
, TTYC_SWD
) ||
662 !tty_term_has(tty
->term
, TTYC_FSL
))
665 tty_putcode(tty
, TTYC_SWD
);
666 tty_puts(tty
, title
);
667 tty_putcode(tty
, TTYC_FSL
);
671 tty_force_cursor_colour(struct tty
*tty
, int c
)
677 c
= colour_force_rgb(c
);
678 if (c
== tty
->ccolour
)
681 tty_putcode(tty
, TTYC_CR
);
683 colour_split_rgb(c
, &r
, &g
, &b
);
684 xsnprintf(s
, sizeof s
, "rgb:%02hhx/%02hhx/%02hhx", r
, g
, b
);
685 tty_putcode_ptr1(tty
, TTYC_CS
, s
);
691 tty_update_cursor(struct tty
*tty
, int mode
, struct screen
*s
)
693 enum screen_cursor_style cstyle
;
694 int ccolour
, changed
, cmode
= mode
;
696 /* Set cursor colour if changed. */
698 ccolour
= s
->ccolour
;
699 if (s
->ccolour
== -1)
700 ccolour
= s
->default_ccolour
;
701 tty_force_cursor_colour(tty
, ccolour
);
704 /* If cursor is off, set as invisible. */
705 if (~cmode
& MODE_CURSOR
) {
706 if (tty
->mode
& MODE_CURSOR
)
707 tty_putcode(tty
, TTYC_CIVIS
);
711 /* Check if blinking or very visible flag changed or style changed. */
713 cstyle
= tty
->cstyle
;
716 if (cstyle
== SCREEN_CURSOR_DEFAULT
) {
717 if (~cmode
& MODE_CURSOR_BLINKING_SET
) {
718 if (s
->default_mode
& MODE_CURSOR_BLINKING
)
719 cmode
|= MODE_CURSOR_BLINKING
;
721 cmode
&= ~MODE_CURSOR_BLINKING
;
723 cstyle
= s
->default_cstyle
;
727 /* If nothing changed, do nothing. */
728 changed
= cmode
^ tty
->mode
;
729 if ((changed
& CURSOR_MODES
) == 0 && cstyle
== tty
->cstyle
)
733 * Set cursor style. If an explicit style has been set with DECSCUSR,
734 * set it if supported, otherwise send cvvis for blinking styles.
736 * If no style, has been set (SCREEN_CURSOR_DEFAULT), then send cvvis
737 * if either the blinking or very visible flags are set.
739 tty_putcode(tty
, TTYC_CNORM
);
741 case SCREEN_CURSOR_DEFAULT
:
742 if (tty
->cstyle
!= SCREEN_CURSOR_DEFAULT
) {
743 if (tty_term_has(tty
->term
, TTYC_SE
))
744 tty_putcode(tty
, TTYC_SE
);
746 tty_putcode1(tty
, TTYC_SS
, 0);
748 if (cmode
& (MODE_CURSOR_BLINKING
|MODE_CURSOR_VERY_VISIBLE
))
749 tty_putcode(tty
, TTYC_CVVIS
);
751 case SCREEN_CURSOR_BLOCK
:
752 if (tty_term_has(tty
->term
, TTYC_SS
)) {
753 if (cmode
& MODE_CURSOR_BLINKING
)
754 tty_putcode1(tty
, TTYC_SS
, 1);
756 tty_putcode1(tty
, TTYC_SS
, 2);
757 } else if (cmode
& MODE_CURSOR_BLINKING
)
758 tty_putcode(tty
, TTYC_CVVIS
);
760 case SCREEN_CURSOR_UNDERLINE
:
761 if (tty_term_has(tty
->term
, TTYC_SS
)) {
762 if (cmode
& MODE_CURSOR_BLINKING
)
763 tty_putcode1(tty
, TTYC_SS
, 3);
765 tty_putcode1(tty
, TTYC_SS
, 4);
766 } else if (cmode
& MODE_CURSOR_BLINKING
)
767 tty_putcode(tty
, TTYC_CVVIS
);
769 case SCREEN_CURSOR_BAR
:
770 if (tty_term_has(tty
->term
, TTYC_SS
)) {
771 if (cmode
& MODE_CURSOR_BLINKING
)
772 tty_putcode1(tty
, TTYC_SS
, 5);
774 tty_putcode1(tty
, TTYC_SS
, 6);
775 } else if (cmode
& MODE_CURSOR_BLINKING
)
776 tty_putcode(tty
, TTYC_CVVIS
);
779 tty
->cstyle
= cstyle
;
784 tty_update_mode(struct tty
*tty
, int mode
, struct screen
*s
)
786 struct tty_term
*term
= tty
->term
;
787 struct client
*c
= tty
->client
;
790 if (tty
->flags
& TTY_NOCURSOR
)
791 mode
&= ~MODE_CURSOR
;
793 if (tty_update_cursor(tty
, mode
, s
) & MODE_CURSOR_BLINKING
)
794 mode
|= MODE_CURSOR_BLINKING
;
796 mode
&= ~MODE_CURSOR_BLINKING
;
798 changed
= mode
^ tty
->mode
;
799 if (log_get_level() != 0 && changed
!= 0) {
800 log_debug("%s: current mode %s", c
->name
,
801 screen_mode_to_string(tty
->mode
));
802 log_debug("%s: setting mode %s", c
->name
,
803 screen_mode_to_string(mode
));
806 if ((changed
& ALL_MOUSE_MODES
) && tty_term_has(term
, TTYC_KMOUS
)) {
808 * If the mouse modes have changed, clear then all and apply
809 * again. There are differences in how terminals track the
812 tty_puts(tty
, "\033[?1006l\033[?1000l\033[?1002l\033[?1003l");
813 if (mode
& ALL_MOUSE_MODES
)
814 tty_puts(tty
, "\033[?1006h");
815 if (mode
& MODE_MOUSE_ALL
)
816 tty_puts(tty
, "\033[?1000h\033[?1002h\033[?1003h");
817 else if (mode
& MODE_MOUSE_BUTTON
)
818 tty_puts(tty
, "\033[?1000h\033[?1002h");
819 else if (mode
& MODE_MOUSE_STANDARD
)
820 tty_puts(tty
, "\033[?1000h");
822 if (changed
& MODE_BRACKETPASTE
) {
823 if (mode
& MODE_BRACKETPASTE
)
824 tty_putcode(tty
, TTYC_ENBP
);
826 tty_putcode(tty
, TTYC_DSBP
);
832 tty_emulate_repeat(struct tty
*tty
, enum tty_code_code code
,
833 enum tty_code_code code1
, u_int n
)
835 if (tty_term_has(tty
->term
, code
))
836 tty_putcode1(tty
, code
, n
);
839 tty_putcode(tty
, code1
);
844 tty_repeat_space(struct tty
*tty
, u_int n
)
849 memset(s
, ' ', sizeof s
);
851 while (n
> sizeof s
) {
852 tty_putn(tty
, s
, sizeof s
, sizeof s
);
856 tty_putn(tty
, s
, n
, n
);
859 /* Is this window bigger than the terminal? */
861 tty_window_bigger(struct tty
*tty
)
863 struct client
*c
= tty
->client
;
864 struct window
*w
= c
->session
->curw
->window
;
866 return (tty
->sx
< w
->sx
|| tty
->sy
- status_line_size(c
) < w
->sy
);
869 /* What offset should this window be drawn at? */
871 tty_window_offset(struct tty
*tty
, u_int
*ox
, u_int
*oy
, u_int
*sx
, u_int
*sy
)
881 /* What offset should this window be drawn at? */
883 tty_window_offset1(struct tty
*tty
, u_int
*ox
, u_int
*oy
, u_int
*sx
, u_int
*sy
)
885 struct client
*c
= tty
->client
;
886 struct window
*w
= c
->session
->curw
->window
;
887 struct window_pane
*wp
= server_client_get_pane(c
);
890 lines
= status_line_size(c
);
892 if (tty
->sx
>= w
->sx
&& tty
->sy
- lines
>= w
->sy
) {
898 c
->pan_window
= NULL
;
903 *sy
= tty
->sy
- lines
;
905 if (c
->pan_window
== w
) {
908 else if (c
->pan_ox
+ *sx
> w
->sx
)
909 c
->pan_ox
= w
->sx
- *sx
;
913 else if (c
->pan_oy
+ *sy
> w
->sy
)
914 c
->pan_oy
= w
->sy
- *sy
;
919 if (~wp
->screen
->mode
& MODE_CURSOR
) {
923 cx
= wp
->xoff
+ wp
->screen
->cx
;
924 cy
= wp
->yoff
+ wp
->screen
->cy
;
928 else if (cx
> w
->sx
- *sx
)
935 else if (cy
> w
->sy
- *sy
)
941 c
->pan_window
= NULL
;
945 /* Update stored offsets for a window and redraw if necessary. */
947 tty_update_window_offset(struct window
*w
)
951 TAILQ_FOREACH(c
, &clients
, entry
) {
952 if (c
->session
!= NULL
&&
953 c
->session
->curw
!= NULL
&&
954 c
->session
->curw
->window
== w
)
955 tty_update_client_offset(c
);
959 /* Update stored offsets for a client and redraw if necessary. */
961 tty_update_client_offset(struct client
*c
)
963 u_int ox
, oy
, sx
, sy
;
965 if (~c
->flags
& CLIENT_TERMINAL
)
968 c
->tty
.oflag
= tty_window_offset1(&c
->tty
, &ox
, &oy
, &sx
, &sy
);
969 if (ox
== c
->tty
.oox
&&
975 log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
976 __func__
, c
->name
, c
->tty
.oox
, c
->tty
.ooy
, c
->tty
.osx
, c
->tty
.osy
,
984 c
->flags
|= (CLIENT_REDRAWWINDOW
|CLIENT_REDRAWSTATUS
);
988 * Is the region large enough to be worth redrawing once later rather than
989 * probably several times now? Currently yes if it is more than 50% of the
993 tty_large_region(__unused
struct tty
*tty
, const struct tty_ctx
*ctx
)
995 return (ctx
->orlower
- ctx
->orupper
>= ctx
->sy
/ 2);
999 * Return if BCE is needed but the terminal doesn't have it - it'll need to be
1003 tty_fake_bce(const struct tty
*tty
, const struct grid_cell
*gc
, u_int bg
)
1005 if (tty_term_flag(tty
->term
, TTYC_BCE
))
1007 if (!COLOUR_DEFAULT(bg
) || !COLOUR_DEFAULT(gc
->bg
))
1013 * Redraw scroll region using data from screen (already updated). Used when
1014 * CSR not supported, or window is a pane that doesn't take up the full
1015 * width of the terminal.
1018 tty_redraw_region(struct tty
*tty
, const struct tty_ctx
*ctx
)
1020 struct client
*c
= tty
->client
;
1024 * If region is large, schedule a redraw. In most cases this is likely
1025 * to be followed by some more scrolling.
1027 if (tty_large_region(tty
, ctx
)) {
1028 log_debug("%s: %s large redraw", __func__
, c
->name
);
1029 ctx
->redraw_cb(ctx
);
1033 for (i
= ctx
->orupper
; i
<= ctx
->orlower
; i
++)
1034 tty_draw_pane(tty
, ctx
, i
);
1037 /* Is this position visible in the pane? */
1039 tty_is_visible(__unused
struct tty
*tty
, const struct tty_ctx
*ctx
, u_int px
,
1040 u_int py
, u_int nx
, u_int ny
)
1042 u_int xoff
= ctx
->rxoff
+ px
, yoff
= ctx
->ryoff
+ py
;
1047 if (xoff
+ nx
<= ctx
->wox
|| xoff
>= ctx
->wox
+ ctx
->wsx
||
1048 yoff
+ ny
<= ctx
->woy
|| yoff
>= ctx
->woy
+ ctx
->wsy
)
1053 /* Clamp line position to visible part of pane. */
1055 tty_clamp_line(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int px
, u_int py
,
1056 u_int nx
, u_int
*i
, u_int
*x
, u_int
*rx
, u_int
*ry
)
1058 u_int xoff
= ctx
->rxoff
+ px
;
1060 if (!tty_is_visible(tty
, ctx
, px
, py
, nx
, 1))
1062 *ry
= ctx
->yoff
+ py
- ctx
->woy
;
1064 if (xoff
>= ctx
->wox
&& xoff
+ nx
<= ctx
->wox
+ ctx
->wsx
) {
1067 *x
= ctx
->xoff
+ px
- ctx
->wox
;
1069 } else if (xoff
< ctx
->wox
&& xoff
+ nx
> ctx
->wox
+ ctx
->wsx
) {
1070 /* Both left and right not visible. */
1074 } else if (xoff
< ctx
->wox
) {
1075 /* Left not visible. */
1076 *i
= ctx
->wox
- (ctx
->xoff
+ px
);
1080 /* Right not visible. */
1082 *x
= (ctx
->xoff
+ px
) - ctx
->wox
;
1083 *rx
= ctx
->wsx
- *x
;
1086 fatalx("%s: x too big, %u > %u", __func__
, *rx
, nx
);
1093 tty_clear_line(struct tty
*tty
, const struct grid_cell
*defaults
, u_int py
,
1094 u_int px
, u_int nx
, u_int bg
)
1096 struct client
*c
= tty
->client
;
1097 struct overlay_ranges r
;
1100 log_debug("%s: %s, %u at %u,%u", __func__
, c
->name
, nx
, px
, py
);
1102 /* Nothing to clear. */
1106 /* If genuine BCE is available, can try escape sequences. */
1107 if (c
->overlay_check
== NULL
&& !tty_fake_bce(tty
, defaults
, bg
)) {
1108 /* Off the end of the line, use EL if available. */
1109 if (px
+ nx
>= tty
->sx
&& tty_term_has(tty
->term
, TTYC_EL
)) {
1110 tty_cursor(tty
, px
, py
);
1111 tty_putcode(tty
, TTYC_EL
);
1115 /* At the start of the line. Use EL1. */
1116 if (px
== 0 && tty_term_has(tty
->term
, TTYC_EL1
)) {
1117 tty_cursor(tty
, px
+ nx
- 1, py
);
1118 tty_putcode(tty
, TTYC_EL1
);
1122 /* Section of line. Use ECH if possible. */
1123 if (tty_term_has(tty
->term
, TTYC_ECH
)) {
1124 tty_cursor(tty
, px
, py
);
1125 tty_putcode1(tty
, TTYC_ECH
, nx
);
1131 * Couldn't use an escape sequence, use spaces. Clear only the visible
1132 * bit if there is an overlay.
1134 tty_check_overlay_range(tty
, px
, py
, nx
, &r
);
1135 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++) {
1138 tty_cursor(tty
, r
.px
[i
], py
);
1139 tty_repeat_space(tty
, r
.nx
[i
]);
1143 /* Clear a line, adjusting to visible part of pane. */
1145 tty_clear_pane_line(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int py
,
1146 u_int px
, u_int nx
, u_int bg
)
1148 struct client
*c
= tty
->client
;
1151 log_debug("%s: %s, %u at %u,%u", __func__
, c
->name
, nx
, px
, py
);
1153 if (tty_clamp_line(tty
, ctx
, px
, py
, nx
, &i
, &x
, &rx
, &ry
))
1154 tty_clear_line(tty
, &ctx
->defaults
, ry
, x
, rx
, bg
);
1157 /* Clamp area position to visible part of pane. */
1159 tty_clamp_area(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int px
, u_int py
,
1160 u_int nx
, u_int ny
, u_int
*i
, u_int
*j
, u_int
*x
, u_int
*y
, u_int
*rx
,
1163 u_int xoff
= ctx
->rxoff
+ px
, yoff
= ctx
->ryoff
+ py
;
1165 if (!tty_is_visible(tty
, ctx
, px
, py
, nx
, ny
))
1168 if (xoff
>= ctx
->wox
&& xoff
+ nx
<= ctx
->wox
+ ctx
->wsx
) {
1171 *x
= ctx
->xoff
+ px
- ctx
->wox
;
1173 } else if (xoff
< ctx
->wox
&& xoff
+ nx
> ctx
->wox
+ ctx
->wsx
) {
1174 /* Both left and right not visible. */
1178 } else if (xoff
< ctx
->wox
) {
1179 /* Left not visible. */
1180 *i
= ctx
->wox
- (ctx
->xoff
+ px
);
1184 /* Right not visible. */
1186 *x
= (ctx
->xoff
+ px
) - ctx
->wox
;
1187 *rx
= ctx
->wsx
- *x
;
1190 fatalx("%s: x too big, %u > %u", __func__
, *rx
, nx
);
1192 if (yoff
>= ctx
->woy
&& yoff
+ ny
<= ctx
->woy
+ ctx
->wsy
) {
1195 *y
= ctx
->yoff
+ py
- ctx
->woy
;
1197 } else if (yoff
< ctx
->woy
&& yoff
+ ny
> ctx
->woy
+ ctx
->wsy
) {
1198 /* Both top and bottom not visible. */
1202 } else if (yoff
< ctx
->woy
) {
1203 /* Top not visible. */
1204 *j
= ctx
->woy
- (ctx
->yoff
+ py
);
1208 /* Bottom not visible. */
1210 *y
= (ctx
->yoff
+ py
) - ctx
->woy
;
1211 *ry
= ctx
->wsy
- *y
;
1214 fatalx("%s: y too big, %u > %u", __func__
, *ry
, ny
);
1219 /* Clear an area, adjusting to visible part of pane. */
1221 tty_clear_area(struct tty
*tty
, const struct grid_cell
*defaults
, u_int py
,
1222 u_int ny
, u_int px
, u_int nx
, u_int bg
)
1224 struct client
*c
= tty
->client
;
1228 log_debug("%s: %s, %u,%u at %u,%u", __func__
, c
->name
, nx
, ny
, px
, py
);
1230 /* Nothing to clear. */
1231 if (nx
== 0 || ny
== 0)
1234 /* If genuine BCE is available, can try escape sequences. */
1235 if (c
->overlay_check
== NULL
&& !tty_fake_bce(tty
, defaults
, bg
)) {
1236 /* Use ED if clearing off the bottom of the terminal. */
1238 px
+ nx
>= tty
->sx
&&
1239 py
+ ny
>= tty
->sy
&&
1240 tty_term_has(tty
->term
, TTYC_ED
)) {
1241 tty_cursor(tty
, 0, py
);
1242 tty_putcode(tty
, TTYC_ED
);
1247 * On VT420 compatible terminals we can use DECFRA if the
1248 * background colour isn't default (because it doesn't work
1251 if ((tty
->term
->flags
& TERM_DECFRA
) && !COLOUR_DEFAULT(bg
)) {
1252 xsnprintf(tmp
, sizeof tmp
, "\033[32;%u;%u;%u;%u$x",
1253 py
+ 1, px
+ 1, py
+ ny
, px
+ nx
);
1258 /* Full lines can be scrolled away to clear them. */
1260 px
+ nx
>= tty
->sx
&&
1262 tty_term_has(tty
->term
, TTYC_CSR
) &&
1263 tty_term_has(tty
->term
, TTYC_INDN
)) {
1264 tty_region(tty
, py
, py
+ ny
- 1);
1265 tty_margin_off(tty
);
1266 tty_putcode1(tty
, TTYC_INDN
, ny
);
1271 * If margins are supported, can just scroll the area off to
1276 tty_term_has(tty
->term
, TTYC_CSR
) &&
1277 tty_use_margin(tty
) &&
1278 tty_term_has(tty
->term
, TTYC_INDN
)) {
1279 tty_region(tty
, py
, py
+ ny
- 1);
1280 tty_margin(tty
, px
, px
+ nx
- 1);
1281 tty_putcode1(tty
, TTYC_INDN
, ny
);
1286 /* Couldn't use an escape sequence, loop over the lines. */
1287 for (yy
= py
; yy
< py
+ ny
; yy
++)
1288 tty_clear_line(tty
, defaults
, yy
, px
, nx
, bg
);
1291 /* Clear an area in a pane. */
1293 tty_clear_pane_area(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int py
,
1294 u_int ny
, u_int px
, u_int nx
, u_int bg
)
1296 u_int i
, j
, x
, y
, rx
, ry
;
1298 if (tty_clamp_area(tty
, ctx
, px
, py
, nx
, ny
, &i
, &j
, &x
, &y
, &rx
, &ry
))
1299 tty_clear_area(tty
, &ctx
->defaults
, y
, ry
, x
, rx
, bg
);
1303 tty_draw_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int py
)
1305 struct screen
*s
= ctx
->s
;
1306 u_int nx
= ctx
->sx
, i
, x
, rx
, ry
;
1308 log_debug("%s: %s %u %d", __func__
, tty
->client
->name
, py
, ctx
->bigger
);
1311 tty_draw_line(tty
, s
, 0, py
, nx
, ctx
->xoff
, ctx
->yoff
+ py
,
1312 &ctx
->defaults
, ctx
->palette
);
1315 if (tty_clamp_line(tty
, ctx
, 0, py
, nx
, &i
, &x
, &rx
, &ry
)) {
1316 tty_draw_line(tty
, s
, i
, py
, rx
, x
, ry
, &ctx
->defaults
,
1321 static const struct grid_cell
*
1322 tty_check_codeset(struct tty
*tty
, const struct grid_cell
*gc
)
1324 static struct grid_cell
new;
1327 /* Characters less than 0x7f are always fine, no matter what. */
1328 if (gc
->data
.size
== 1 && *gc
->data
.data
< 0x7f)
1331 /* UTF-8 terminal and a UTF-8 character - fine. */
1332 if (tty
->client
->flags
& CLIENT_UTF8
)
1334 memcpy(&new, gc
, sizeof new);
1336 /* See if this can be mapped to an ACS character. */
1337 c
= tty_acs_reverse_get(tty
, gc
->data
.data
, gc
->data
.size
);
1339 utf8_set(&new.data
, c
);
1340 new.attr
|= GRID_ATTR_CHARSET
;
1344 /* Replace by the right number of underscores. */
1345 new.data
.size
= gc
->data
.width
;
1346 if (new.data
.size
> UTF8_SIZE
)
1347 new.data
.size
= UTF8_SIZE
;
1348 memset(new.data
.data
, '_', new.data
.size
);
1353 * Check if a single character is obstructed by the overlay and return a
1357 tty_check_overlay(struct tty
*tty
, u_int px
, u_int py
)
1359 struct overlay_ranges r
;
1362 * A unit width range will always return nx[2] == 0 from a check, even
1363 * with multiple overlays, so it's sufficient to check just the first
1366 tty_check_overlay_range(tty
, px
, py
, 1, &r
);
1367 if (r
.nx
[0] + r
.nx
[1] == 0)
1372 /* Return parts of the input range which are visible. */
1374 tty_check_overlay_range(struct tty
*tty
, u_int px
, u_int py
, u_int nx
,
1375 struct overlay_ranges
*r
)
1377 struct client
*c
= tty
->client
;
1379 if (c
->overlay_check
== NULL
) {
1389 c
->overlay_check(c
, c
->overlay_data
, px
, py
, nx
, r
);
1393 tty_draw_line(struct tty
*tty
, struct screen
*s
, u_int px
, u_int py
, u_int nx
,
1394 u_int atx
, u_int aty
, const struct grid_cell
*defaults
,
1395 struct colour_palette
*palette
)
1397 struct grid
*gd
= s
->grid
;
1398 struct grid_cell gc
, last
;
1399 const struct grid_cell
*gcp
;
1400 struct grid_line
*gl
;
1401 struct client
*c
= tty
->client
;
1402 struct overlay_ranges r
;
1403 u_int i
, j
, ux
, sx
, width
, hidden
, eux
, nxx
;
1405 int flags
, cleared
= 0, wrapped
= 0;
1409 log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__
,
1410 px
, py
, nx
, atx
, aty
);
1411 log_debug("%s: defaults: fg=%d, bg=%d", __func__
, defaults
->fg
,
1415 * py is the line in the screen to draw.
1416 * px is the start x and nx is the width to draw.
1417 * atx,aty is the line on the terminal to draw it.
1420 flags
= (tty
->flags
& TTY_NOCURSOR
);
1421 tty
->flags
|= TTY_NOCURSOR
;
1422 tty_update_mode(tty
, tty
->mode
, s
);
1424 tty_region_off(tty
);
1425 tty_margin_off(tty
);
1428 * Clamp the width to cellsize - note this is not cellused, because
1429 * there may be empty background cells after it (from BCE).
1431 sx
= screen_size_x(s
);
1434 cellsize
= grid_get_line(gd
, gd
->hsize
+ py
)->cellsize
;
1446 gl
= grid_get_line(gd
, gd
->hsize
+ py
- 1);
1448 (~gl
->flags
& GRID_LINE_WRAPPED
) ||
1450 tty
->cx
< tty
->sx
||
1455 tty_term_has(tty
->term
, TTYC_EL1
) &&
1456 !tty_fake_bce(tty
, defaults
, 8) &&
1457 c
->overlay_check
== NULL
) {
1458 tty_default_attributes(tty
, defaults
, palette
, 8,
1460 tty_cursor(tty
, nx
- 1, aty
);
1461 tty_putcode(tty
, TTYC_EL1
);
1465 log_debug("%s: wrapped line %u", __func__
, aty
);
1469 memcpy(&last
, &grid_default_cell
, sizeof last
);
1473 for (i
= 0; i
< sx
; i
++) {
1474 grid_view_get_cell(gd
, px
+ i
, py
, &gc
);
1475 gcp
= tty_check_codeset(tty
, &gc
);
1477 (!tty_check_overlay(tty
, atx
+ ux
+ width
, aty
) ||
1478 (gcp
->attr
& GRID_ATTR_CHARSET
) ||
1479 gcp
->flags
!= last
.flags
||
1480 gcp
->attr
!= last
.attr
||
1481 gcp
->fg
!= last
.fg
||
1482 gcp
->bg
!= last
.bg
||
1483 gcp
->us
!= last
.us
||
1484 gcp
->link
!= last
.link
||
1485 ux
+ width
+ gcp
->data
.width
> nx
||
1486 (sizeof buf
) - len
< gcp
->data
.size
)) {
1487 tty_attributes(tty
, &last
, defaults
, palette
,
1489 if (last
.flags
& GRID_FLAG_CLEARED
) {
1490 log_debug("%s: %zu cleared", __func__
, len
);
1491 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
,
1494 if (!wrapped
|| atx
!= 0 || ux
!= 0)
1495 tty_cursor(tty
, atx
+ ux
, aty
);
1496 tty_putn(tty
, buf
, len
, width
);
1505 if (gcp
->flags
& GRID_FLAG_SELECTED
)
1506 screen_select_cell(s
, &last
, gcp
);
1508 memcpy(&last
, gcp
, sizeof last
);
1510 tty_check_overlay_range(tty
, atx
+ ux
, aty
, gcp
->data
.width
,
1513 for (j
= 0; j
< OVERLAY_MAX_RANGES
; j
++)
1515 hidden
= gcp
->data
.width
- hidden
;
1516 if (hidden
!= 0 && hidden
== gcp
->data
.width
) {
1517 if (~gcp
->flags
& GRID_FLAG_PADDING
)
1518 ux
+= gcp
->data
.width
;
1519 } else if (hidden
!= 0 || ux
+ gcp
->data
.width
> nx
) {
1520 if (~gcp
->flags
& GRID_FLAG_PADDING
) {
1521 tty_attributes(tty
, &last
, defaults
, palette
,
1523 for (j
= 0; j
< OVERLAY_MAX_RANGES
; j
++) {
1526 /* Effective width drawn so far. */
1527 eux
= r
.px
[j
] - atx
;
1529 tty_cursor(tty
, r
.px
[j
], aty
);
1533 tty_repeat_space(tty
, r
.nx
[j
]);
1538 } else if (gcp
->attr
& GRID_ATTR_CHARSET
) {
1539 tty_attributes(tty
, &last
, defaults
, palette
,
1541 tty_cursor(tty
, atx
+ ux
, aty
);
1542 for (j
= 0; j
< gcp
->data
.size
; j
++)
1543 tty_putc(tty
, gcp
->data
.data
[j
]);
1544 ux
+= gcp
->data
.width
;
1545 } else if (~gcp
->flags
& GRID_FLAG_PADDING
) {
1546 memcpy(buf
+ len
, gcp
->data
.data
, gcp
->data
.size
);
1547 len
+= gcp
->data
.size
;
1548 width
+= gcp
->data
.width
;
1551 if (len
!= 0 && ((~last
.flags
& GRID_FLAG_CLEARED
) || last
.bg
!= 8)) {
1552 tty_attributes(tty
, &last
, defaults
, palette
, s
->hyperlinks
);
1553 if (last
.flags
& GRID_FLAG_CLEARED
) {
1554 log_debug("%s: %zu cleared (end)", __func__
, len
);
1555 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
, width
,
1558 if (!wrapped
|| atx
!= 0 || ux
!= 0)
1559 tty_cursor(tty
, atx
+ ux
, aty
);
1560 tty_putn(tty
, buf
, len
, width
);
1565 if (!cleared
&& ux
< nx
) {
1566 log_debug("%s: %u to end of line (%zu cleared)", __func__
,
1568 tty_default_attributes(tty
, defaults
, palette
, 8,
1570 tty_clear_line(tty
, defaults
, aty
, atx
+ ux
, nx
- ux
, 8);
1573 tty
->flags
= (tty
->flags
& ~TTY_NOCURSOR
) | flags
;
1574 tty_update_mode(tty
, tty
->mode
, s
);
1578 tty_sync_start(struct tty
*tty
)
1580 if (tty
->flags
& TTY_BLOCK
)
1582 if (tty
->flags
& TTY_SYNCING
)
1584 tty
->flags
|= TTY_SYNCING
;
1586 if (tty_term_has(tty
->term
, TTYC_SYNC
)) {
1587 log_debug("%s sync start", tty
->client
->name
);
1588 tty_putcode1(tty
, TTYC_SYNC
, 1);
1593 tty_sync_end(struct tty
*tty
)
1595 if (tty
->flags
& TTY_BLOCK
)
1597 if (~tty
->flags
& TTY_SYNCING
)
1599 tty
->flags
&= ~TTY_SYNCING
;
1601 if (tty_term_has(tty
->term
, TTYC_SYNC
)) {
1602 log_debug("%s sync end", tty
->client
->name
);
1603 tty_putcode1(tty
, TTYC_SYNC
, 2);
1608 tty_client_ready(struct client
*c
)
1610 if (c
->session
== NULL
|| c
->tty
.term
== NULL
)
1612 if (c
->flags
& (CLIENT_REDRAWWINDOW
|CLIENT_SUSPENDED
))
1614 if (c
->tty
.flags
& TTY_FREEZE
)
1620 tty_write(void (*cmdfn
)(struct tty
*, const struct tty_ctx
*),
1621 struct tty_ctx
*ctx
)
1626 if (ctx
->set_client_cb
== NULL
)
1628 TAILQ_FOREACH(c
, &clients
, entry
) {
1629 if (ctx
->allow_invisible_panes
) {
1630 if (c
->session
== NULL
||
1631 c
->tty
.term
== NULL
||
1632 c
->flags
& CLIENT_SUSPENDED
)
1635 if (!tty_client_ready(c
))
1637 state
= ctx
->set_client_cb(ctx
, c
);
1643 cmdfn(&c
->tty
, ctx
);
1648 tty_cmd_insertcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1650 struct client
*c
= tty
->client
;
1653 !tty_full_width(tty
, ctx
) ||
1654 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1655 (!tty_term_has(tty
->term
, TTYC_ICH
) &&
1656 !tty_term_has(tty
->term
, TTYC_ICH1
)) ||
1657 c
->overlay_check
!= NULL
) {
1658 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
1662 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1663 ctx
->s
->hyperlinks
);
1665 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1667 tty_emulate_repeat(tty
, TTYC_ICH
, TTYC_ICH1
, ctx
->num
);
1671 tty_cmd_deletecharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1673 struct client
*c
= tty
->client
;
1676 !tty_full_width(tty
, ctx
) ||
1677 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1678 (!tty_term_has(tty
->term
, TTYC_DCH
) &&
1679 !tty_term_has(tty
->term
, TTYC_DCH1
)) ||
1680 c
->overlay_check
!= NULL
) {
1681 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
1685 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1686 ctx
->s
->hyperlinks
);
1688 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1690 tty_emulate_repeat(tty
, TTYC_DCH
, TTYC_DCH1
, ctx
->num
);
1694 tty_cmd_clearcharacter(struct tty
*tty
, const struct tty_ctx
*ctx
)
1696 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1697 ctx
->s
->hyperlinks
);
1699 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, ctx
->ocx
, ctx
->num
, ctx
->bg
);
1703 tty_cmd_insertline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1705 struct client
*c
= tty
->client
;
1708 !tty_full_width(tty
, ctx
) ||
1709 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1710 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1711 !tty_term_has(tty
->term
, TTYC_IL1
) ||
1714 c
->overlay_check
!= NULL
) {
1715 tty_redraw_region(tty
, ctx
);
1719 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1720 ctx
->s
->hyperlinks
);
1722 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1723 tty_margin_off(tty
);
1724 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1726 tty_emulate_repeat(tty
, TTYC_IL
, TTYC_IL1
, ctx
->num
);
1727 tty
->cx
= tty
->cy
= UINT_MAX
;
1731 tty_cmd_deleteline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1733 struct client
*c
= tty
->client
;
1736 !tty_full_width(tty
, ctx
) ||
1737 tty_fake_bce(tty
, &ctx
->defaults
, ctx
->bg
) ||
1738 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1739 !tty_term_has(tty
->term
, TTYC_DL1
) ||
1742 c
->overlay_check
!= NULL
) {
1743 tty_redraw_region(tty
, ctx
);
1747 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1748 ctx
->s
->hyperlinks
);
1750 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1751 tty_margin_off(tty
);
1752 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1754 tty_emulate_repeat(tty
, TTYC_DL
, TTYC_DL1
, ctx
->num
);
1755 tty
->cx
= tty
->cy
= UINT_MAX
;
1759 tty_cmd_clearline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1761 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1762 ctx
->s
->hyperlinks
);
1764 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, 0, ctx
->sx
, ctx
->bg
);
1768 tty_cmd_clearendofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1770 u_int nx
= ctx
->sx
- ctx
->ocx
;
1772 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1773 ctx
->s
->hyperlinks
);
1775 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, ctx
->ocx
, nx
, ctx
->bg
);
1779 tty_cmd_clearstartofline(struct tty
*tty
, const struct tty_ctx
*ctx
)
1781 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1782 ctx
->s
->hyperlinks
);
1784 tty_clear_pane_line(tty
, ctx
, ctx
->ocy
, 0, ctx
->ocx
+ 1, ctx
->bg
);
1788 tty_cmd_reverseindex(struct tty
*tty
, const struct tty_ctx
*ctx
)
1790 struct client
*c
= tty
->client
;
1792 if (ctx
->ocy
!= ctx
->orupper
)
1796 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1797 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1798 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1799 (!tty_term_has(tty
->term
, TTYC_RI
) &&
1800 !tty_term_has(tty
->term
, TTYC_RIN
)) ||
1803 c
->overlay_check
!= NULL
) {
1804 tty_redraw_region(tty
, ctx
);
1808 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1809 ctx
->s
->hyperlinks
);
1811 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1812 tty_margin_pane(tty
, ctx
);
1813 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
1815 if (tty_term_has(tty
->term
, TTYC_RI
))
1816 tty_putcode(tty
, TTYC_RI
);
1818 tty_putcode1(tty
, TTYC_RIN
, 1);
1822 tty_cmd_linefeed(struct tty
*tty
, const struct tty_ctx
*ctx
)
1824 struct client
*c
= tty
->client
;
1826 if (ctx
->ocy
!= ctx
->orlower
)
1830 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1831 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1832 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1835 c
->overlay_check
!= NULL
) {
1836 tty_redraw_region(tty
, ctx
);
1840 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1841 ctx
->s
->hyperlinks
);
1843 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1844 tty_margin_pane(tty
, ctx
);
1847 * If we want to wrap a pane while using margins, the cursor needs to
1848 * be exactly on the right of the region. If the cursor is entirely off
1849 * the edge - move it back to the right. Some terminals are funny about
1850 * this and insert extra spaces, so only use the right if margins are
1853 if (ctx
->xoff
+ ctx
->ocx
> tty
->rright
) {
1854 if (!tty_use_margin(tty
))
1855 tty_cursor(tty
, 0, ctx
->yoff
+ ctx
->ocy
);
1857 tty_cursor(tty
, tty
->rright
, ctx
->yoff
+ ctx
->ocy
);
1859 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
1861 tty_putc(tty
, '\n');
1865 tty_cmd_scrollup(struct tty
*tty
, const struct tty_ctx
*ctx
)
1867 struct client
*c
= tty
->client
;
1871 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1872 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1873 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1876 c
->overlay_check
!= NULL
) {
1877 tty_redraw_region(tty
, ctx
);
1881 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1882 ctx
->s
->hyperlinks
);
1884 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1885 tty_margin_pane(tty
, ctx
);
1887 if (ctx
->num
== 1 || !tty_term_has(tty
->term
, TTYC_INDN
)) {
1888 if (!tty_use_margin(tty
))
1889 tty_cursor(tty
, 0, tty
->rlower
);
1891 tty_cursor(tty
, tty
->rright
, tty
->rlower
);
1892 for (i
= 0; i
< ctx
->num
; i
++)
1893 tty_putc(tty
, '\n');
1895 if (tty
->cy
== UINT_MAX
)
1896 tty_cursor(tty
, 0, 0);
1898 tty_cursor(tty
, 0, tty
->cy
);
1899 tty_putcode1(tty
, TTYC_INDN
, ctx
->num
);
1904 tty_cmd_scrolldown(struct tty
*tty
, const struct tty_ctx
*ctx
)
1907 struct client
*c
= tty
->client
;
1910 (!tty_full_width(tty
, ctx
) && !tty_use_margin(tty
)) ||
1911 tty_fake_bce(tty
, &ctx
->defaults
, 8) ||
1912 !tty_term_has(tty
->term
, TTYC_CSR
) ||
1913 (!tty_term_has(tty
->term
, TTYC_RI
) &&
1914 !tty_term_has(tty
->term
, TTYC_RIN
)) ||
1917 c
->overlay_check
!= NULL
) {
1918 tty_redraw_region(tty
, ctx
);
1922 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1923 ctx
->s
->hyperlinks
);
1925 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
1926 tty_margin_pane(tty
, ctx
);
1927 tty_cursor_pane(tty
, ctx
, ctx
->ocx
, ctx
->orupper
);
1929 if (tty_term_has(tty
->term
, TTYC_RIN
))
1930 tty_putcode1(tty
, TTYC_RIN
, ctx
->num
);
1932 for (i
= 0; i
< ctx
->num
; i
++)
1933 tty_putcode(tty
, TTYC_RI
);
1938 tty_cmd_clearendofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
1940 u_int px
, py
, nx
, ny
;
1942 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1943 ctx
->s
->hyperlinks
);
1945 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
1946 tty_margin_off(tty
);
1951 ny
= ctx
->sy
- ctx
->ocy
- 1;
1953 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
1956 nx
= ctx
->sx
- ctx
->ocx
;
1959 tty_clear_pane_line(tty
, ctx
, py
, px
, nx
, ctx
->bg
);
1963 tty_cmd_clearstartofscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
1965 u_int px
, py
, nx
, ny
;
1967 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1968 ctx
->s
->hyperlinks
);
1970 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
1971 tty_margin_off(tty
);
1978 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
1984 tty_clear_pane_line(tty
, ctx
, py
, px
, nx
, ctx
->bg
);
1988 tty_cmd_clearscreen(struct tty
*tty
, const struct tty_ctx
*ctx
)
1990 u_int px
, py
, nx
, ny
;
1992 tty_default_attributes(tty
, &ctx
->defaults
, ctx
->palette
, ctx
->bg
,
1993 ctx
->s
->hyperlinks
);
1995 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
1996 tty_margin_off(tty
);
2003 tty_clear_pane_area(tty
, ctx
, py
, ny
, px
, nx
, ctx
->bg
);
2007 tty_cmd_alignmenttest(struct tty
*tty
, const struct tty_ctx
*ctx
)
2012 ctx
->redraw_cb(ctx
);
2016 tty_attributes(tty
, &grid_default_cell
, &ctx
->defaults
, ctx
->palette
,
2017 ctx
->s
->hyperlinks
);
2019 tty_region_pane(tty
, ctx
, 0, ctx
->sy
- 1);
2020 tty_margin_off(tty
);
2022 for (j
= 0; j
< ctx
->sy
; j
++) {
2023 tty_cursor_pane(tty
, ctx
, 0, j
);
2024 for (i
= 0; i
< ctx
->sx
; i
++)
2030 tty_cmd_cell(struct tty
*tty
, const struct tty_ctx
*ctx
)
2032 const struct grid_cell
*gcp
= ctx
->cell
;
2033 struct screen
*s
= ctx
->s
;
2034 struct overlay_ranges r
;
2035 u_int px
, py
, i
, vis
= 0;
2037 px
= ctx
->xoff
+ ctx
->ocx
- ctx
->wox
;
2038 py
= ctx
->yoff
+ ctx
->ocy
- ctx
->woy
;
2039 if (!tty_is_visible(tty
, ctx
, ctx
->ocx
, ctx
->ocy
, 1, 1) ||
2040 (gcp
->data
.width
== 1 && !tty_check_overlay(tty
, px
, py
)))
2043 /* Handle partially obstructed wide characters. */
2044 if (gcp
->data
.width
> 1) {
2045 tty_check_overlay_range(tty
, px
, py
, gcp
->data
.width
, &r
);
2046 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++)
2048 if (vis
< gcp
->data
.width
) {
2049 tty_draw_line(tty
, s
, s
->cx
, s
->cy
, gcp
->data
.width
,
2050 px
, py
, &ctx
->defaults
, ctx
->palette
);
2055 if (ctx
->xoff
+ ctx
->ocx
- ctx
->wox
> tty
->sx
- 1 &&
2056 ctx
->ocy
== ctx
->orlower
&&
2057 tty_full_width(tty
, ctx
))
2058 tty_region_pane(tty
, ctx
, ctx
->orupper
, ctx
->orlower
);
2060 tty_margin_off(tty
);
2061 tty_cursor_pane_unless_wrap(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
2063 tty_cell(tty
, ctx
->cell
, &ctx
->defaults
, ctx
->palette
,
2064 ctx
->s
->hyperlinks
);
2068 tty_cmd_cells(struct tty
*tty
, const struct tty_ctx
*ctx
)
2070 struct overlay_ranges r
;
2071 u_int i
, px
, py
, cx
;
2072 char *cp
= ctx
->ptr
;
2074 if (!tty_is_visible(tty
, ctx
, ctx
->ocx
, ctx
->ocy
, ctx
->num
, 1))
2078 (ctx
->xoff
+ ctx
->ocx
< ctx
->wox
||
2079 ctx
->xoff
+ ctx
->ocx
+ ctx
->num
> ctx
->wox
+ ctx
->wsx
)) {
2080 if (!ctx
->wrapped
||
2081 !tty_full_width(tty
, ctx
) ||
2082 (tty
->term
->flags
& TERM_NOAM
) ||
2083 ctx
->xoff
+ ctx
->ocx
!= 0 ||
2084 ctx
->yoff
+ ctx
->ocy
!= tty
->cy
+ 1 ||
2085 tty
->cx
< tty
->sx
||
2086 tty
->cy
== tty
->rlower
)
2087 tty_draw_pane(tty
, ctx
, ctx
->ocy
);
2089 ctx
->redraw_cb(ctx
);
2093 tty_margin_off(tty
);
2094 tty_cursor_pane_unless_wrap(tty
, ctx
, ctx
->ocx
, ctx
->ocy
);
2095 tty_attributes(tty
, ctx
->cell
, &ctx
->defaults
, ctx
->palette
, ctx
->s
->hyperlinks
);
2097 /* Get tty position from pane position for overlay check. */
2098 px
= ctx
->xoff
+ ctx
->ocx
- ctx
->wox
;
2099 py
= ctx
->yoff
+ ctx
->ocy
- ctx
->woy
;
2101 tty_check_overlay_range(tty
, px
, py
, ctx
->num
, &r
);
2102 for (i
= 0; i
< OVERLAY_MAX_RANGES
; i
++) {
2105 /* Convert back to pane position for printing. */
2106 cx
= r
.px
[i
] - ctx
->xoff
+ ctx
->wox
;
2107 tty_cursor_pane_unless_wrap(tty
, ctx
, cx
, ctx
->ocy
);
2108 tty_putn(tty
, cp
+ r
.px
[i
] - px
, r
.nx
[i
], r
.nx
[i
]);
2113 tty_cmd_setselection(struct tty
*tty
, const struct tty_ctx
*ctx
)
2115 tty_set_selection(tty
, ctx
->ptr2
, ctx
->ptr
, ctx
->num
);
2119 tty_set_selection(struct tty
*tty
, const char *flags
, const char *buf
,
2125 if (~tty
->flags
& TTY_STARTED
)
2127 if (!tty_term_has(tty
->term
, TTYC_MS
))
2130 size
= 4 * ((len
+ 2) / 3) + 1; /* storage for base64 */
2131 encoded
= xmalloc(size
);
2133 b64_ntop(buf
, len
, encoded
, size
);
2134 tty
->flags
|= TTY_NOBLOCK
;
2135 tty_putcode_ptr2(tty
, TTYC_MS
, flags
, encoded
);
2141 tty_cmd_rawstring(struct tty
*tty
, const struct tty_ctx
*ctx
)
2143 tty
->flags
|= TTY_NOBLOCK
;
2144 tty_add(tty
, ctx
->ptr
, ctx
->num
);
2145 tty_invalidate(tty
);
2149 tty_cmd_syncstart(struct tty
*tty
, const struct tty_ctx
*ctx
)
2151 if (ctx
->num
== 0x11) {
2153 * This is an overlay and a command that moves the cursor so
2154 * start synchronized updates.
2156 tty_sync_start(tty
);
2157 } else if (~ctx
->num
& 0x10) {
2159 * This is a pane. If there is an overlay, always start;
2160 * otherwise, only if requested.
2162 if (ctx
->num
|| tty
->client
->overlay_draw
!= NULL
)
2163 tty_sync_start(tty
);
2168 tty_cell(struct tty
*tty
, const struct grid_cell
*gc
,
2169 const struct grid_cell
*defaults
, struct colour_palette
*palette
,
2170 struct hyperlinks
*hl
)
2172 const struct grid_cell
*gcp
;
2174 /* Skip last character if terminal is stupid. */
2175 if ((tty
->term
->flags
& TERM_NOAM
) &&
2176 tty
->cy
== tty
->sy
- 1 &&
2177 tty
->cx
== tty
->sx
- 1)
2180 /* If this is a padding character, do nothing. */
2181 if (gc
->flags
& GRID_FLAG_PADDING
)
2184 /* Check the output codeset and apply attributes. */
2185 gcp
= tty_check_codeset(tty
, gc
);
2186 tty_attributes(tty
, gcp
, defaults
, palette
, hl
);
2188 /* If it is a single character, write with putc to handle ACS. */
2189 if (gcp
->data
.size
== 1) {
2190 tty_attributes(tty
, gcp
, defaults
, palette
, hl
);
2191 if (*gcp
->data
.data
< 0x20 || *gcp
->data
.data
== 0x7f)
2193 tty_putc(tty
, *gcp
->data
.data
);
2197 /* Write the data. */
2198 tty_putn(tty
, gcp
->data
.data
, gcp
->data
.size
, gcp
->data
.width
);
2202 tty_reset(struct tty
*tty
)
2204 struct grid_cell
*gc
= &tty
->cell
;
2206 if (!grid_cells_equal(gc
, &grid_default_cell
)) {
2208 tty_putcode_ptr2(tty
, TTYC_HLS
, "", "");
2209 if ((gc
->attr
& GRID_ATTR_CHARSET
) && tty_acs_needed(tty
))
2210 tty_putcode(tty
, TTYC_RMACS
);
2211 tty_putcode(tty
, TTYC_SGR0
);
2212 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2214 memcpy(&tty
->last_cell
, &grid_default_cell
, sizeof tty
->last_cell
);
2218 tty_invalidate(struct tty
*tty
)
2220 memcpy(&tty
->cell
, &grid_default_cell
, sizeof tty
->cell
);
2221 memcpy(&tty
->last_cell
, &grid_default_cell
, sizeof tty
->last_cell
);
2223 tty
->cx
= tty
->cy
= UINT_MAX
;
2224 tty
->rupper
= tty
->rleft
= UINT_MAX
;
2225 tty
->rlower
= tty
->rright
= UINT_MAX
;
2227 if (tty
->flags
& TTY_STARTED
) {
2228 if (tty_use_margin(tty
))
2229 tty_putcode(tty
, TTYC_ENMG
);
2230 tty_putcode(tty
, TTYC_SGR0
);
2232 tty
->mode
= ALL_MODES
;
2233 tty_update_mode(tty
, MODE_CURSOR
, NULL
);
2235 tty_cursor(tty
, 0, 0);
2236 tty_region_off(tty
);
2237 tty_margin_off(tty
);
2239 tty
->mode
= MODE_CURSOR
;
2242 /* Turn off margin. */
2244 tty_region_off(struct tty
*tty
)
2246 tty_region(tty
, 0, tty
->sy
- 1);
2249 /* Set region inside pane. */
2251 tty_region_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int rupper
,
2254 tty_region(tty
, ctx
->yoff
+ rupper
- ctx
->woy
,
2255 ctx
->yoff
+ rlower
- ctx
->woy
);
2258 /* Set region at absolute position. */
2260 tty_region(struct tty
*tty
, u_int rupper
, u_int rlower
)
2262 if (tty
->rlower
== rlower
&& tty
->rupper
== rupper
)
2264 if (!tty_term_has(tty
->term
, TTYC_CSR
))
2267 tty
->rupper
= rupper
;
2268 tty
->rlower
= rlower
;
2271 * Some terminals (such as PuTTY) do not correctly reset the cursor to
2272 * 0,0 if it is beyond the last column (they do not reset their wrap
2273 * flag so further output causes a line feed). As a workaround, do an
2274 * explicit move to 0 first.
2276 if (tty
->cx
>= tty
->sx
) {
2277 if (tty
->cy
== UINT_MAX
)
2278 tty_cursor(tty
, 0, 0);
2280 tty_cursor(tty
, 0, tty
->cy
);
2283 tty_putcode2(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
2284 tty
->cx
= tty
->cy
= UINT_MAX
;
2287 /* Turn off margin. */
2289 tty_margin_off(struct tty
*tty
)
2291 tty_margin(tty
, 0, tty
->sx
- 1);
2294 /* Set margin inside pane. */
2296 tty_margin_pane(struct tty
*tty
, const struct tty_ctx
*ctx
)
2298 tty_margin(tty
, ctx
->xoff
- ctx
->wox
,
2299 ctx
->xoff
+ ctx
->sx
- 1 - ctx
->wox
);
2302 /* Set margin at absolute position. */
2304 tty_margin(struct tty
*tty
, u_int rleft
, u_int rright
)
2306 if (!tty_use_margin(tty
))
2308 if (tty
->rleft
== rleft
&& tty
->rright
== rright
)
2311 tty_putcode2(tty
, TTYC_CSR
, tty
->rupper
, tty
->rlower
);
2314 tty
->rright
= rright
;
2316 if (rleft
== 0 && rright
== tty
->sx
- 1)
2317 tty_putcode(tty
, TTYC_CLMG
);
2319 tty_putcode2(tty
, TTYC_CMG
, rleft
, rright
);
2320 tty
->cx
= tty
->cy
= UINT_MAX
;
2324 * Move the cursor, unless it would wrap itself when the next character is
2328 tty_cursor_pane_unless_wrap(struct tty
*tty
, const struct tty_ctx
*ctx
,
2331 if (!ctx
->wrapped
||
2332 !tty_full_width(tty
, ctx
) ||
2333 (tty
->term
->flags
& TERM_NOAM
) ||
2334 ctx
->xoff
+ cx
!= 0 ||
2335 ctx
->yoff
+ cy
!= tty
->cy
+ 1 ||
2336 tty
->cx
< tty
->sx
||
2337 tty
->cy
== tty
->rlower
)
2338 tty_cursor_pane(tty
, ctx
, cx
, cy
);
2340 log_debug("%s: will wrap at %u,%u", __func__
, tty
->cx
, tty
->cy
);
2343 /* Move cursor inside pane. */
2345 tty_cursor_pane(struct tty
*tty
, const struct tty_ctx
*ctx
, u_int cx
, u_int cy
)
2347 tty_cursor(tty
, ctx
->xoff
+ cx
- ctx
->wox
, ctx
->yoff
+ cy
- ctx
->woy
);
2350 /* Move cursor to absolute position. */
2352 tty_cursor(struct tty
*tty
, u_int cx
, u_int cy
)
2354 struct tty_term
*term
= tty
->term
;
2358 if (tty
->flags
& TTY_BLOCK
)
2365 * If in the automargin space, and want to be there, do not move.
2366 * Otherwise, force the cursor to be in range (and complain).
2368 if (cx
== thisx
&& cy
== thisy
&& cx
== tty
->sx
)
2370 if (cx
> tty
->sx
- 1) {
2371 log_debug("%s: x too big %u > %u", __func__
, cx
, tty
->sx
- 1);
2376 if (cx
== thisx
&& cy
== thisy
)
2379 /* Currently at the very end of the line - use absolute movement. */
2380 if (thisx
> tty
->sx
- 1)
2383 /* Move to home position (0, 0). */
2384 if (cx
== 0 && cy
== 0 && tty_term_has(term
, TTYC_HOME
)) {
2385 tty_putcode(tty
, TTYC_HOME
);
2389 /* Zero on the next line. */
2390 if (cx
== 0 && cy
== thisy
+ 1 && thisy
!= tty
->rlower
&&
2391 (!tty_use_margin(tty
) || tty
->rleft
== 0)) {
2392 tty_putc(tty
, '\r');
2393 tty_putc(tty
, '\n');
2397 /* Moving column or row. */
2400 * Moving column only, row staying the same.
2404 if (cx
== 0 && (!tty_use_margin(tty
) || tty
->rleft
== 0)) {
2405 tty_putc(tty
, '\r');
2409 /* One to the left. */
2410 if (cx
== thisx
- 1 && tty_term_has(term
, TTYC_CUB1
)) {
2411 tty_putcode(tty
, TTYC_CUB1
);
2415 /* One to the right. */
2416 if (cx
== thisx
+ 1 && tty_term_has(term
, TTYC_CUF1
)) {
2417 tty_putcode(tty
, TTYC_CUF1
);
2421 /* Calculate difference. */
2422 change
= thisx
- cx
; /* +ve left, -ve right */
2425 * Use HPA if change is larger than absolute, otherwise move
2426 * the cursor with CUB/CUF.
2428 if ((u_int
) abs(change
) > cx
&& tty_term_has(term
, TTYC_HPA
)) {
2429 tty_putcode1(tty
, TTYC_HPA
, cx
);
2431 } else if (change
> 0 &&
2432 tty_term_has(term
, TTYC_CUB
) &&
2433 !tty_use_margin(tty
)) {
2434 if (change
== 2 && tty_term_has(term
, TTYC_CUB1
)) {
2435 tty_putcode(tty
, TTYC_CUB1
);
2436 tty_putcode(tty
, TTYC_CUB1
);
2439 tty_putcode1(tty
, TTYC_CUB
, change
);
2441 } else if (change
< 0 &&
2442 tty_term_has(term
, TTYC_CUF
) &&
2443 !tty_use_margin(tty
)) {
2444 tty_putcode1(tty
, TTYC_CUF
, -change
);
2447 } else if (cx
== thisx
) {
2449 * Moving row only, column staying the same.
2453 if (thisy
!= tty
->rupper
&&
2454 cy
== thisy
- 1 && tty_term_has(term
, TTYC_CUU1
)) {
2455 tty_putcode(tty
, TTYC_CUU1
);
2460 if (thisy
!= tty
->rlower
&&
2461 cy
== thisy
+ 1 && tty_term_has(term
, TTYC_CUD1
)) {
2462 tty_putcode(tty
, TTYC_CUD1
);
2466 /* Calculate difference. */
2467 change
= thisy
- cy
; /* +ve up, -ve down */
2470 * Try to use VPA if change is larger than absolute or if this
2471 * change would cross the scroll region, otherwise use CUU/CUD.
2473 if ((u_int
) abs(change
) > cy
||
2474 (change
< 0 && cy
- change
> tty
->rlower
) ||
2475 (change
> 0 && cy
- change
< tty
->rupper
)) {
2476 if (tty_term_has(term
, TTYC_VPA
)) {
2477 tty_putcode1(tty
, TTYC_VPA
, cy
);
2480 } else if (change
> 0 && tty_term_has(term
, TTYC_CUU
)) {
2481 tty_putcode1(tty
, TTYC_CUU
, change
);
2483 } else if (change
< 0 && tty_term_has(term
, TTYC_CUD
)) {
2484 tty_putcode1(tty
, TTYC_CUD
, -change
);
2490 /* Absolute movement. */
2491 tty_putcode2(tty
, TTYC_CUP
, cy
, cx
);
2499 tty_hyperlink(struct tty
*tty
, const struct grid_cell
*gc
,
2500 struct hyperlinks
*hl
)
2502 const char *uri
, *id
;
2504 if (gc
->link
== tty
->cell
.link
)
2506 tty
->cell
.link
= gc
->link
;
2511 if (gc
->link
== 0 || !hyperlinks_get(hl
, gc
->link
, &uri
, NULL
, &id
))
2512 tty_putcode_ptr2(tty
, TTYC_HLS
, "", "");
2514 tty_putcode_ptr2(tty
, TTYC_HLS
, id
, uri
);
2518 tty_attributes(struct tty
*tty
, const struct grid_cell
*gc
,
2519 const struct grid_cell
*defaults
, struct colour_palette
*palette
,
2520 struct hyperlinks
*hl
)
2522 struct grid_cell
*tc
= &tty
->cell
, gc2
;
2525 /* Copy cell and update default colours. */
2526 memcpy(&gc2
, gc
, sizeof gc2
);
2527 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2529 gc2
.fg
= defaults
->fg
;
2531 gc2
.bg
= defaults
->bg
;
2534 /* Ignore cell if it is the same as the last one. */
2535 if (gc2
.attr
== tty
->last_cell
.attr
&&
2536 gc2
.fg
== tty
->last_cell
.fg
&&
2537 gc2
.bg
== tty
->last_cell
.bg
&&
2538 gc2
.us
== tty
->last_cell
.us
&&
2539 gc2
.link
== tty
->last_cell
.link
)
2543 * If no setab, try to use the reverse attribute as a best-effort for a
2544 * non-default background. This is a bit of a hack but it doesn't do
2545 * any serious harm and makes a couple of applications happier.
2547 if (!tty_term_has(tty
->term
, TTYC_SETAB
)) {
2548 if (gc2
.attr
& GRID_ATTR_REVERSE
) {
2549 if (gc2
.fg
!= 7 && !COLOUR_DEFAULT(gc2
.fg
))
2550 gc2
.attr
&= ~GRID_ATTR_REVERSE
;
2552 if (gc2
.bg
!= 0 && !COLOUR_DEFAULT(gc2
.bg
))
2553 gc2
.attr
|= GRID_ATTR_REVERSE
;
2557 /* Fix up the colours if necessary. */
2558 tty_check_fg(tty
, palette
, &gc2
);
2559 tty_check_bg(tty
, palette
, &gc2
);
2560 tty_check_us(tty
, palette
, &gc2
);
2563 * If any bits are being cleared or the underline colour is now default,
2566 if ((tc
->attr
& ~gc2
.attr
) || (tc
->us
!= gc2
.us
&& gc2
.us
== 0))
2570 * Set the colours. This may call tty_reset() (so it comes next) and
2571 * may add to (NOT remove) the desired attributes.
2573 tty_colours(tty
, &gc2
);
2575 /* Filter out attribute bits already set. */
2576 changed
= gc2
.attr
& ~tc
->attr
;
2577 tc
->attr
= gc2
.attr
;
2579 /* Set the attributes. */
2580 if (changed
& GRID_ATTR_BRIGHT
)
2581 tty_putcode(tty
, TTYC_BOLD
);
2582 if (changed
& GRID_ATTR_DIM
)
2583 tty_putcode(tty
, TTYC_DIM
);
2584 if (changed
& GRID_ATTR_ITALICS
)
2585 tty_set_italics(tty
);
2586 if (changed
& GRID_ATTR_ALL_UNDERSCORE
) {
2587 if ((changed
& GRID_ATTR_UNDERSCORE
) ||
2588 !tty_term_has(tty
->term
, TTYC_SMULX
))
2589 tty_putcode(tty
, TTYC_SMUL
);
2590 else if (changed
& GRID_ATTR_UNDERSCORE_2
)
2591 tty_putcode1(tty
, TTYC_SMULX
, 2);
2592 else if (changed
& GRID_ATTR_UNDERSCORE_3
)
2593 tty_putcode1(tty
, TTYC_SMULX
, 3);
2594 else if (changed
& GRID_ATTR_UNDERSCORE_4
)
2595 tty_putcode1(tty
, TTYC_SMULX
, 4);
2596 else if (changed
& GRID_ATTR_UNDERSCORE_5
)
2597 tty_putcode1(tty
, TTYC_SMULX
, 5);
2599 if (changed
& GRID_ATTR_BLINK
)
2600 tty_putcode(tty
, TTYC_BLINK
);
2601 if (changed
& GRID_ATTR_REVERSE
) {
2602 if (tty_term_has(tty
->term
, TTYC_REV
))
2603 tty_putcode(tty
, TTYC_REV
);
2604 else if (tty_term_has(tty
->term
, TTYC_SMSO
))
2605 tty_putcode(tty
, TTYC_SMSO
);
2607 if (changed
& GRID_ATTR_HIDDEN
)
2608 tty_putcode(tty
, TTYC_INVIS
);
2609 if (changed
& GRID_ATTR_STRIKETHROUGH
)
2610 tty_putcode(tty
, TTYC_SMXX
);
2611 if (changed
& GRID_ATTR_OVERLINE
)
2612 tty_putcode(tty
, TTYC_SMOL
);
2613 if ((changed
& GRID_ATTR_CHARSET
) && tty_acs_needed(tty
))
2614 tty_putcode(tty
, TTYC_SMACS
);
2616 /* Set hyperlink if any. */
2617 tty_hyperlink(tty
, gc
, hl
);
2619 memcpy(&tty
->last_cell
, &gc2
, sizeof tty
->last_cell
);
2623 tty_colours(struct tty
*tty
, const struct grid_cell
*gc
)
2625 struct grid_cell
*tc
= &tty
->cell
;
2628 /* No changes? Nothing is necessary. */
2629 if (gc
->fg
== tc
->fg
&& gc
->bg
== tc
->bg
&& gc
->us
== tc
->us
)
2633 * Is either the default colour? This is handled specially because the
2634 * best solution might be to reset both colours to default, in which
2635 * case if only one is default need to fall onward to set the other
2638 if (COLOUR_DEFAULT(gc
->fg
) || COLOUR_DEFAULT(gc
->bg
)) {
2640 * If don't have AX but do have op, send sgr0 (op can't
2641 * actually be used because it is sometimes the same as sgr0
2642 * and sometimes isn't). This resets both colours to default.
2644 * Otherwise, try to set the default colour only as needed.
2646 have_ax
= tty_term_flag(tty
->term
, TTYC_AX
);
2647 if (!have_ax
&& tty_term_has(tty
->term
, TTYC_OP
))
2650 if (COLOUR_DEFAULT(gc
->fg
) && !COLOUR_DEFAULT(tc
->fg
)) {
2652 tty_puts(tty
, "\033[39m");
2653 else if (tc
->fg
!= 7)
2654 tty_putcode1(tty
, TTYC_SETAF
, 7);
2657 if (COLOUR_DEFAULT(gc
->bg
) && !COLOUR_DEFAULT(tc
->bg
)) {
2659 tty_puts(tty
, "\033[49m");
2660 else if (tc
->bg
!= 0)
2661 tty_putcode1(tty
, TTYC_SETAB
, 0);
2667 /* Set the foreground colour. */
2668 if (!COLOUR_DEFAULT(gc
->fg
) && gc
->fg
!= tc
->fg
)
2669 tty_colours_fg(tty
, gc
);
2672 * Set the background colour. This must come after the foreground as
2673 * tty_colour_fg() can call tty_reset().
2675 if (!COLOUR_DEFAULT(gc
->bg
) && gc
->bg
!= tc
->bg
)
2676 tty_colours_bg(tty
, gc
);
2678 /* Set the underscore colour. */
2679 if (gc
->us
!= tc
->us
)
2680 tty_colours_us(tty
, gc
);
2684 tty_check_fg(struct tty
*tty
, struct colour_palette
*palette
,
2685 struct grid_cell
*gc
)
2692 * Perform substitution if this pane has a palette. If the bright
2693 * attribute is set and Nobr is not present, use the bright entry in
2694 * the palette by changing to the aixterm colour
2696 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2699 gc
->attr
& GRID_ATTR_BRIGHT
&&
2700 !tty_term_has(tty
->term
, TTYC_NOBR
))
2702 if ((c
= colour_palette_get(palette
, c
)) != -1)
2706 /* Is this a 24-bit colour? */
2707 if (gc
->fg
& COLOUR_FLAG_RGB
) {
2708 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2709 if (tty
->term
->flags
& TERM_RGBCOLOURS
)
2711 colour_split_rgb(gc
->fg
, &r
, &g
, &b
);
2712 gc
->fg
= colour_find_rgb(r
, g
, b
);
2715 /* How many colours does this terminal have? */
2716 if (tty
->term
->flags
& TERM_256COLOURS
)
2719 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
2721 /* Is this a 256-colour colour? */
2722 if (gc
->fg
& COLOUR_FLAG_256
) {
2723 /* And not a 256 colour mode? */
2724 if (colours
< 256) {
2725 gc
->fg
= colour_256to16(gc
->fg
);
2735 /* Is this an aixterm colour? */
2736 if (gc
->fg
>= 90 && gc
->fg
<= 97 && colours
< 16) {
2738 gc
->attr
|= GRID_ATTR_BRIGHT
;
2743 tty_check_bg(struct tty
*tty
, struct colour_palette
*palette
,
2744 struct grid_cell
*gc
)
2750 /* Perform substitution if this pane has a palette. */
2751 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2752 if ((c
= colour_palette_get(palette
, gc
->bg
)) != -1)
2756 /* Is this a 24-bit colour? */
2757 if (gc
->bg
& COLOUR_FLAG_RGB
) {
2758 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2759 if (tty
->term
->flags
& TERM_RGBCOLOURS
)
2761 colour_split_rgb(gc
->bg
, &r
, &g
, &b
);
2762 gc
->bg
= colour_find_rgb(r
, g
, b
);
2765 /* How many colours does this terminal have? */
2766 if (tty
->term
->flags
& TERM_256COLOURS
)
2769 colours
= tty_term_number(tty
->term
, TTYC_COLORS
);
2771 /* Is this a 256-colour colour? */
2772 if (gc
->bg
& COLOUR_FLAG_256
) {
2774 * And not a 256 colour mode? Translate to 16-colour
2775 * palette. Bold background doesn't exist portably, so just
2776 * discard the bold bit if set.
2778 if (colours
< 256) {
2779 gc
->bg
= colour_256to16(gc
->bg
);
2789 /* Is this an aixterm colour? */
2790 if (gc
->bg
>= 90 && gc
->bg
<= 97 && colours
< 16)
2795 tty_check_us(__unused
struct tty
*tty
, struct colour_palette
*palette
,
2796 struct grid_cell
*gc
)
2800 /* Perform substitution if this pane has a palette. */
2801 if (~gc
->flags
& GRID_FLAG_NOPALETTE
) {
2802 if ((c
= colour_palette_get(palette
, gc
->us
)) != -1)
2806 /* Underscore colour is set as RGB so convert a 256 colour to RGB. */
2807 if (gc
->us
& COLOUR_FLAG_256
)
2808 gc
->us
= colour_256toRGB (gc
->us
);
2812 tty_colours_fg(struct tty
*tty
, const struct grid_cell
*gc
)
2814 struct grid_cell
*tc
= &tty
->cell
;
2817 /* Is this a 24-bit or 256-colour colour? */
2818 if (gc
->fg
& COLOUR_FLAG_RGB
|| gc
->fg
& COLOUR_FLAG_256
) {
2819 if (tty_try_colour(tty
, gc
->fg
, "38") == 0)
2821 /* Should not get here, already converted in tty_check_fg. */
2825 /* Is this an aixterm bright colour? */
2826 if (gc
->fg
>= 90 && gc
->fg
<= 97) {
2827 if (tty
->term
->flags
& TERM_256COLOURS
) {
2828 xsnprintf(s
, sizeof s
, "\033[%dm", gc
->fg
);
2831 tty_putcode1(tty
, TTYC_SETAF
, gc
->fg
- 90 + 8);
2835 /* Otherwise set the foreground colour. */
2836 tty_putcode1(tty
, TTYC_SETAF
, gc
->fg
);
2839 /* Save the new values in the terminal current cell. */
2844 tty_colours_bg(struct tty
*tty
, const struct grid_cell
*gc
)
2846 struct grid_cell
*tc
= &tty
->cell
;
2849 /* Is this a 24-bit or 256-colour colour? */
2850 if (gc
->bg
& COLOUR_FLAG_RGB
|| gc
->bg
& COLOUR_FLAG_256
) {
2851 if (tty_try_colour(tty
, gc
->bg
, "48") == 0)
2853 /* Should not get here, already converted in tty_check_bg. */
2857 /* Is this an aixterm bright colour? */
2858 if (gc
->bg
>= 90 && gc
->bg
<= 97) {
2859 if (tty
->term
->flags
& TERM_256COLOURS
) {
2860 xsnprintf(s
, sizeof s
, "\033[%dm", gc
->bg
+ 10);
2863 tty_putcode1(tty
, TTYC_SETAB
, gc
->bg
- 90 + 8);
2867 /* Otherwise set the background colour. */
2868 tty_putcode1(tty
, TTYC_SETAB
, gc
->bg
);
2871 /* Save the new values in the terminal current cell. */
2876 tty_colours_us(struct tty
*tty
, const struct grid_cell
*gc
)
2878 struct grid_cell
*tc
= &tty
->cell
;
2882 /* Clear underline colour. */
2884 tty_putcode(tty
, TTYC_OL
);
2888 /* Must be an RGB colour - this should never happen. */
2889 if (~gc
->us
& COLOUR_FLAG_RGB
)
2893 * Setulc and setal follows the ncurses(3) one argument "direct colour"
2894 * capability format. Calculate the colour value.
2896 colour_split_rgb(gc
->us
, &r
, &g
, &b
);
2897 c
= (65536 * r
) + (256 * g
) + b
;
2900 * Write the colour. Only use setal if the RGB flag is set because the
2901 * non-RGB version may be wrong.
2903 if (tty_term_has(tty
->term
, TTYC_SETULC
))
2904 tty_putcode1(tty
, TTYC_SETULC
, c
);
2905 else if (tty_term_has(tty
->term
, TTYC_SETAL
) &&
2906 tty_term_has(tty
->term
, TTYC_RGB
))
2907 tty_putcode1(tty
, TTYC_SETAL
, c
);
2910 /* Save the new values in the terminal current cell. */
2915 tty_try_colour(struct tty
*tty
, int colour
, const char *type
)
2919 if (colour
& COLOUR_FLAG_256
) {
2920 if (*type
== '3' && tty_term_has(tty
->term
, TTYC_SETAF
))
2921 tty_putcode1(tty
, TTYC_SETAF
, colour
& 0xff);
2922 else if (tty_term_has(tty
->term
, TTYC_SETAB
))
2923 tty_putcode1(tty
, TTYC_SETAB
, colour
& 0xff);
2927 if (colour
& COLOUR_FLAG_RGB
) {
2928 colour_split_rgb(colour
& 0xffffff, &r
, &g
, &b
);
2929 if (*type
== '3' && tty_term_has(tty
->term
, TTYC_SETRGBF
))
2930 tty_putcode3(tty
, TTYC_SETRGBF
, r
, g
, b
);
2931 else if (tty_term_has(tty
->term
, TTYC_SETRGBB
))
2932 tty_putcode3(tty
, TTYC_SETRGBB
, r
, g
, b
);
2940 tty_window_default_style(struct grid_cell
*gc
, struct window_pane
*wp
)
2942 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2943 gc
->fg
= wp
->palette
.fg
;
2944 gc
->bg
= wp
->palette
.bg
;
2948 tty_default_colours(struct grid_cell
*gc
, struct window_pane
*wp
)
2950 struct options
*oo
= wp
->options
;
2951 struct format_tree
*ft
;
2953 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
2955 if (wp
->flags
& PANE_STYLECHANGED
) {
2956 log_debug("%%%u: style changed", wp
->id
);
2957 wp
->flags
&= ~PANE_STYLECHANGED
;
2959 ft
= format_create(NULL
, NULL
, FORMAT_PANE
|wp
->id
,
2961 format_defaults(ft
, NULL
, NULL
, NULL
, wp
);
2962 tty_window_default_style(&wp
->cached_active_gc
, wp
);
2963 style_add(&wp
->cached_active_gc
, oo
, "window-active-style", ft
);
2964 tty_window_default_style(&wp
->cached_gc
, wp
);
2965 style_add(&wp
->cached_gc
, oo
, "window-style", ft
);
2970 if (wp
== wp
->window
->active
&& wp
->cached_active_gc
.fg
!= 8)
2971 gc
->fg
= wp
->cached_active_gc
.fg
;
2973 gc
->fg
= wp
->cached_gc
.fg
;
2977 if (wp
== wp
->window
->active
&& wp
->cached_active_gc
.bg
!= 8)
2978 gc
->bg
= wp
->cached_active_gc
.bg
;
2980 gc
->bg
= wp
->cached_gc
.bg
;
2985 tty_default_attributes(struct tty
*tty
, const struct grid_cell
*defaults
,
2986 struct colour_palette
*palette
, u_int bg
, struct hyperlinks
*hl
)
2988 struct grid_cell gc
;
2990 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
2992 tty_attributes(tty
, &gc
, defaults
, palette
, hl
);
2996 tty_clipboard_query_callback(__unused
int fd
, __unused
short events
, void *data
)
2998 struct tty
*tty
= data
;
2999 struct client
*c
= tty
->client
;
3001 c
->flags
&= ~CLIENT_CLIPBOARDBUFFER
;
3002 free(c
->clipboard_panes
);
3003 c
->clipboard_panes
= NULL
;
3004 c
->clipboard_npanes
= 0;
3006 tty
->flags
&= ~TTY_OSC52QUERY
;
3010 tty_clipboard_query(struct tty
*tty
)
3012 struct timeval tv
= { .tv_sec
= TTY_QUERY_TIMEOUT
};
3014 if ((~tty
->flags
& TTY_STARTED
) || (tty
->flags
& TTY_OSC52QUERY
))
3016 tty_putcode_ptr2(tty
, TTYC_MS
, "", "?");
3018 tty
->flags
|= TTY_OSC52QUERY
;
3019 evtimer_set(&tty
->clipboard_timer
, tty_clipboard_query_callback
, tty
);
3020 evtimer_add(&tty
->clipboard_timer
, &tv
);