diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2019-02-17 14:47:40 +0100 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2019-02-17 14:47:40 +0100 |
commit | 6bd6927656500c50787b16de1fadf1bc46b7aefa (patch) | |
tree | 39b7291f434431e40e0a2d526de3026fc166226a /src | |
parent | dc9dd8d664f2ffec3d199f96e4605cb0729497ec (diff) | |
parent | 7e9b99d55be68938689b9f593e200054533c3783 (diff) | |
download | rneovim-6bd6927656500c50787b16de1fadf1bc46b7aefa.tar.gz rneovim-6bd6927656500c50787b16de1fadf1bc46b7aefa.tar.bz2 rneovim-6bd6927656500c50787b16de1fadf1bc46b7aefa.zip |
Merge #5027 'TUI: detect background color'
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/option.c | 2 | ||||
-rw-r--r-- | src/nvim/tui/input.c | 65 | ||||
-rw-r--r-- | src/nvim/tui/tui.c | 7 |
3 files changed, 72 insertions, 2 deletions
diff --git a/src/nvim/option.c b/src/nvim/option.c index fc1fab834e..b85ec6dbcc 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -883,7 +883,7 @@ set_options_default ( /// @param name The name of the option /// @param val The value of the option /// @param allocated If true, do not copy default as it was already allocated. -static void set_string_default(const char *name, char *val, bool allocated) +void set_string_default(const char *name, char *val, bool allocated) FUNC_ATTR_NONNULL_ALL { int opt_idx = findoption(name); diff --git a/src/nvim/tui/input.c b/src/nvim/tui/input.c index a0df80a8e8..e0dd4022ed 100644 --- a/src/nvim/tui/input.c +++ b/src/nvim/tui/input.c @@ -7,8 +7,10 @@ #include "nvim/api/vim.h" #include "nvim/api/private/helpers.h" #include "nvim/ascii.h" +#include "nvim/charset.h" #include "nvim/main.h" #include "nvim/aucmd.h" +#include "nvim/option.h" #include "nvim/os/os.h" #include "nvim/os/input.h" #include "nvim/event/rstream.h" @@ -352,6 +354,66 @@ static bool handle_forced_escape(TermInput *input) return false; } +static void set_bg_deferred(void **argv) +{ + char *bgvalue = argv[0]; + set_string_default("bg", bgvalue, false); + if (!option_was_set("bg")) { + set_option_value("bg", 0, bgvalue, 0); + } +} + +static bool handle_background_color(TermInput *input) +{ + size_t count = 0; + size_t component = 0; + uint16_t rgb[] = { 0, 0, 0 }; + uint16_t rgb_max[] = { 0, 0, 0 }; + bool eat_backslash = false; + bool done = false; + bool bad = false; + if (rbuffer_size(input->read_stream.buffer) >= 9 + && !rbuffer_cmp(input->read_stream.buffer, "\x1b]11;rgb:", 9)) { + rbuffer_consumed(input->read_stream.buffer, 9); + RBUFFER_EACH(input->read_stream.buffer, c, i) { + count = i + 1; + if (eat_backslash) { + done = true; + break; + } else if (c == '\x07') { + done = true; + break; + } else if (c == '\x1b') { + eat_backslash = true; + } else if (bad) { + // ignore + } else if (c == '/') { + if (component < 3) { + component++; + } + } else if (ascii_isxdigit(c)) { + if (component < 3 && rgb_max[component] != 0xffff) { + rgb_max[component] = (uint16_t)((rgb_max[component] << 4) | 0xf); + rgb[component] = (uint16_t)((rgb[component] << 4) | hex2nr(c)); + } + } else { + bad = true; + } + } + rbuffer_consumed(input->read_stream.buffer, count); + if (done && !bad && rgb_max[0] && rgb_max[1] && rgb_max[2]) { + double r = (double)rgb[0] / (double)rgb_max[0]; + double g = (double)rgb[1] / (double)rgb_max[1]; + double b = (double)rgb[2] / (double)rgb_max[2]; + double luminance = (0.299 * r) + (0.587 * g) + (0.114 * b); // CCIR 601 + char *bgvalue = luminance < 0.5 ? "dark" : "light"; + loop_schedule(&main_loop, event_create(set_bg_deferred, 1, bgvalue)); + } + return true; + } + return false; +} + static void read_cb(Stream *stream, RBuffer *buf, size_t count_, void *data, bool eof) { @@ -381,7 +443,8 @@ static void read_cb(Stream *stream, RBuffer *buf, size_t count_, void *data, do { if (handle_focus_event(input) || handle_bracketed_paste(input) - || handle_forced_escape(input)) { + || handle_forced_escape(input) + || handle_background_color(input)) { continue; } diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c index bbf2306f90..0fc153e8ce 100644 --- a/src/nvim/tui/tui.c +++ b/src/nvim/tui/tui.c @@ -123,6 +123,7 @@ typedef struct { int set_cursor_style, reset_cursor_style; int save_title, restore_title; int enter_undercurl_mode, exit_undercurl_mode, set_underline_color; + int get_bg; } unibi_ext; char *space_buf; } TUIData; @@ -211,6 +212,7 @@ static void terminfo_start(UI *ui) data->unibi_ext.reset_scroll_region = -1; data->unibi_ext.set_cursor_style = -1; data->unibi_ext.reset_cursor_style = -1; + data->unibi_ext.get_bg = -1; data->out_fd = 1; data->out_isatty = os_isatty(data->out_fd); @@ -281,6 +283,8 @@ static void terminfo_start(UI *ui) unibi_out_ext(ui, data->unibi_ext.save_title); unibi_out(ui, unibi_keypad_xmit); unibi_out(ui, unibi_clear_screen); + // Ask the terminal to send us the background color. + unibi_out_ext(ui, data->unibi_ext.get_bg); // Enable bracketed paste unibi_out_ext(ui, data->unibi_ext.enable_bracketed_paste); @@ -1648,6 +1652,9 @@ static void patch_terminfo_bugs(TUIData *data, const char *term, #define XTERM_SETAB_16 \ "\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", + "\x1b]11;?\x07"); + // Terminals with 256-colour SGR support despite what terminfo says. if (unibi_get_num(ut, unibi_max_colors) < 256) { // See http://fedoraproject.org/wiki/Features/256_Color_Terminals |