tui: improve support for tmux, GNU Screen

Wrapping it in DCS allows the get_bg features to work with tmux, GNU Screen.
This commit is contained in:
erw7 2021-03-11 00:24:33 +09:00
parent 300e71de9c
commit c266c2f36e
2 changed files with 18 additions and 6 deletions

View File

@ -31,7 +31,9 @@
#define CSI 0x9b // Control Sequence Introducer #define CSI 0x9b // Control Sequence Introducer
#define CSI_STR "\233" #define CSI_STR "\233"
#define DCS 0x90 // Device Control String #define DCS 0x90 // Device Control String
#define DCS_STR "\033P"
#define STERM 0x9c // String Terminator #define STERM 0x9c // String Terminator
#define STERM_STR "\033\\"
#define POUND 0xA3 #define POUND 0xA3

View File

@ -55,7 +55,11 @@
#define STARTS_WITH(str, prefix) (strlen(str) >= (sizeof(prefix) - 1) \ #define STARTS_WITH(str, prefix) (strlen(str) >= (sizeof(prefix) - 1) \
&& 0 == memcmp((str), (prefix), sizeof(prefix) - 1)) && 0 == memcmp((str), (prefix), sizeof(prefix) - 1))
#define TMUX_WRAP(is_tmux, seq) ((is_tmux) \ #define TMUX_WRAP(is_tmux, seq) ((is_tmux) \
? "\x1bPtmux;\x1b" seq "\x1b\\" : seq) ? DCS_STR "tmux;\x1b" seq STERM_STR : seq)
#define SCREEN_TMUX_WRAP(is_screen, is_tmux, seq) \
((is_screen) \
? DCS_STR seq STERM_STR : (is_tmux) \
? DCS_STR "tmux;\x1b" seq STERM_STR : seq)
#define LINUXSET0C "\x1b[?0c" #define LINUXSET0C "\x1b[?0c"
#define LINUXSET1C "\x1b[?1c" #define LINUXSET1C "\x1b[?1c"
@ -297,6 +301,12 @@ static void terminfo_start(UI *ui)
data->invis, sizeof data->invis); data->invis, sizeof data->invis);
// Set 't_Co' from the result of unibilium & fix_terminfo. // Set 't_Co' from the result of unibilium & fix_terminfo.
t_colors = unibi_get_num(data->ut, unibi_max_colors); t_colors = unibi_get_num(data->ut, unibi_max_colors);
// Ask the terminal to send us the background color.
// If get_bg is sent at the same time after enter_ca_mode, tmux will not send
// get_bg to the host terminal. To avoid this, send get_bg before
// enter_ca_mode.
data->input.waiting_for_bg_response = 5;
unibi_out_ext(ui, data->unibi_ext.get_bg);
// Enter alternate screen, save title, and clear. // Enter alternate screen, save title, and clear.
// NOTE: Do this *before* changing terminal settings. #6433 // NOTE: Do this *before* changing terminal settings. #6433
unibi_out(ui, unibi_enter_ca_mode); unibi_out(ui, unibi_enter_ca_mode);
@ -304,9 +314,6 @@ static void terminfo_start(UI *ui)
unibi_out_ext(ui, data->unibi_ext.save_title); unibi_out_ext(ui, data->unibi_ext.save_title);
unibi_out(ui, unibi_keypad_xmit); unibi_out(ui, unibi_keypad_xmit);
unibi_out(ui, unibi_clear_screen); unibi_out(ui, unibi_clear_screen);
// Ask the terminal to send us the background color.
data->input.waiting_for_bg_response = 5;
unibi_out_ext(ui, data->unibi_ext.get_bg);
// Enable bracketed paste // Enable bracketed paste
unibi_out_ext(ui, data->unibi_ext.enable_bracketed_paste); unibi_out_ext(ui, data->unibi_ext.enable_bracketed_paste);
@ -328,6 +335,7 @@ static void terminfo_start(UI *ui)
uv_pipe_init(&data->write_loop, &data->output_handle.pipe, 0); uv_pipe_init(&data->write_loop, &data->output_handle.pipe, 0);
uv_pipe_open(&data->output_handle.pipe, data->out_fd); uv_pipe_open(&data->output_handle.pipe, data->out_fd);
} }
flush_buf(ui); flush_buf(ui);
} }
@ -1772,8 +1780,10 @@ static void patch_terminfo_bugs(TUIData *data, const char *term,
#define XTERM_SETAB_16 \ #define XTERM_SETAB_16 \
"\x1b[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e39%;m" "\x1b[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e39%;m"
data->unibi_ext.get_bg = (int)unibi_add_ext_str(ut, "ext.get_bg", data->unibi_ext.get_bg =
"\x1b]11;?\x07"); (int)unibi_add_ext_str(ut, "ext.get_bg",
SCREEN_TMUX_WRAP((screen && !tmux), tmux,
"\x1b]11;?\x07"));
// Terminals with 256-colour SGR support despite what terminfo says. // Terminals with 256-colour SGR support despite what terminfo says.
if (unibi_get_num(ut, unibi_max_colors) < 256) { if (unibi_get_num(ut, unibi_max_colors) < 256) {