fix(tui): use uv_timer_t instead of TimeWatcher for input (#26435)

Avoid scheduling on main loop.

Fix #26425
This commit is contained in:
zeertzjq
2023-12-07 07:35:52 +08:00
committed by GitHub
parent 6b00b8a369
commit 3198598e69
3 changed files with 14 additions and 16 deletions

View File

@@ -156,14 +156,15 @@ void tinput_init(TermInput *input, Loop *loop)
rstream_init_fd(loop, &input->read_stream, input->in_fd, READ_STREAM_SIZE); rstream_init_fd(loop, &input->read_stream, input->in_fd, READ_STREAM_SIZE);
// initialize a timer handle for handling ESC with libtermkey // initialize a timer handle for handling ESC with libtermkey
time_watcher_init(loop, &input->timer_handle, input); uv_timer_init(&loop->uv, &input->timer_handle);
input->timer_handle.data = input;
} }
void tinput_destroy(TermInput *input) void tinput_destroy(TermInput *input)
{ {
map_destroy(int, &kitty_key_map); map_destroy(int, &kitty_key_map);
rbuffer_free(input->key_buffer); rbuffer_free(input->key_buffer);
time_watcher_close(&input->timer_handle, NULL); uv_close((uv_handle_t *)&input->timer_handle, NULL);
stream_close(&input->read_stream, NULL, NULL); stream_close(&input->read_stream, NULL, NULL);
termkey_destroy(input->tk); termkey_destroy(input->tk);
} }
@@ -176,7 +177,7 @@ void tinput_start(TermInput *input)
void tinput_stop(TermInput *input) void tinput_stop(TermInput *input)
{ {
rstream_stop(&input->read_stream); rstream_stop(&input->read_stream);
time_watcher_stop(&input->timer_handle); uv_timer_stop(&input->timer_handle);
} }
static void tinput_done_event(void **argv) static void tinput_done_event(void **argv)
@@ -466,17 +467,16 @@ static void tk_getkeys(TermInput *input, bool force)
if (input->ttimeout && input->ttimeoutlen >= 0) { if (input->ttimeout && input->ttimeoutlen >= 0) {
// Stop the current timer if already running // Stop the current timer if already running
time_watcher_stop(&input->timer_handle); uv_timer_stop(&input->timer_handle);
time_watcher_start(&input->timer_handle, tinput_timer_cb, uv_timer_start(&input->timer_handle, tinput_timer_cb, (uint64_t)input->ttimeoutlen, 0);
(uint64_t)input->ttimeoutlen, 0);
} else { } else {
tk_getkeys(input, true); tk_getkeys(input, true);
} }
} }
static void tinput_timer_cb(TimeWatcher *watcher, void *data) static void tinput_timer_cb(uv_timer_t *handle)
{ {
TermInput *input = (TermInput *)data; TermInput *input = handle->data;
// If the raw buffer is not empty, process the raw buffer first because it is // If the raw buffer is not empty, process the raw buffer first because it is
// processing an incomplete bracketed paster sequence. // processing an incomplete bracketed paster sequence.
if (rbuffer_size(input->read_stream.buffer)) { if (rbuffer_size(input->read_stream.buffer)) {
@@ -489,8 +489,8 @@ static void tinput_timer_cb(TimeWatcher *watcher, void *data)
/// Handle focus events. /// Handle focus events.
/// ///
/// If the upcoming sequence of bytes in the input stream matches the termcode /// If the upcoming sequence of bytes in the input stream matches the termcode
/// for "focus gained" or "focus lost", consume that sequence and schedule an /// for "focus gained" or "focus lost", consume that sequence and send an event
/// event on the main loop. /// to Nvim server.
/// ///
/// @param input the input stream /// @param input the input stream
/// @return true iff handle_focus_event consumed some input /// @return true iff handle_focus_event consumed some input
@@ -757,8 +757,8 @@ static void tinput_read_cb(Stream *stream, RBuffer *buf, size_t count_, void *da
int64_t ms = input->ttimeout int64_t ms = input->ttimeout
? (input->ttimeoutlen >= 0 ? input->ttimeoutlen : 0) : 0; ? (input->ttimeoutlen >= 0 ? input->ttimeoutlen : 0) : 0;
// Stop the current timer if already running // Stop the current timer if already running
time_watcher_stop(&input->timer_handle); uv_timer_stop(&input->timer_handle);
time_watcher_start(&input->timer_handle, tinput_timer_cb, (uint32_t)ms, 0); uv_timer_start(&input->timer_handle, tinput_timer_cb, (uint32_t)ms, 0);
return; return;
} }

View File

@@ -6,7 +6,6 @@
#include "nvim/event/loop.h" #include "nvim/event/loop.h"
#include "nvim/event/stream.h" #include "nvim/event/stream.h"
#include "nvim/event/time.h"
#include "nvim/rbuffer_defs.h" #include "nvim/rbuffer_defs.h"
#include "nvim/tui/input_defs.h" // IWYU pragma: export #include "nvim/tui/input_defs.h" // IWYU pragma: export
#include "nvim/tui/tui.h" #include "nvim/tui/tui.h"
@@ -33,7 +32,7 @@ typedef struct {
OptInt ttimeoutlen; OptInt ttimeoutlen;
TermKey *tk; TermKey *tk;
TermKey_Terminfo_Getstr_Hook *tk_ti_hook_fn; ///< libtermkey terminfo hook TermKey_Terminfo_Getstr_Hook *tk_ti_hook_fn; ///< libtermkey terminfo hook
TimeWatcher timer_handle; uv_timer_t timer_handle;
Loop *loop; Loop *loop;
Stream read_stream; Stream read_stream;
RBuffer *key_buffer; RBuffer *key_buffer;

View File

@@ -170,8 +170,7 @@ void tui_start(TUIData **tui_p, int *width, int *height, char **term, bool *rgb)
uv_timer_init(&tui->loop->uv, &tui->startup_delay_timer); uv_timer_init(&tui->loop->uv, &tui->startup_delay_timer);
tui->startup_delay_timer.data = tui; tui->startup_delay_timer.data = tui;
uv_timer_start(&tui->startup_delay_timer, after_startup_cb, uv_timer_start(&tui->startup_delay_timer, after_startup_cb, 100, 0);
100, 0);
*tui_p = tui; *tui_p = tui;
loop_poll_events(&main_loop, 1); loop_poll_events(&main_loop, 1);