diff options
-rw-r--r-- | src/os/event.c | 14 | ||||
-rw-r--r-- | src/os/event.h | 9 | ||||
-rw-r--r-- | src/os/input.c | 47 | ||||
-rw-r--r-- | src/os/input.h | 3 |
4 files changed, 36 insertions, 37 deletions
diff --git a/src/os/event.c b/src/os/event.c index 1abde8d075..0a7d2c096c 100644 --- a/src/os/event.c +++ b/src/os/event.c @@ -19,15 +19,14 @@ void event_init() } /* Wait for some event */ -EventType event_poll(int32_t ms) +bool event_poll(int32_t ms) { bool timed_out; - EventType event; uv_run_mode run_mode = UV_RUN_ONCE; - if ((event = input_check()) != kEventNone) { + if (input_ready()) { /* If there's a pending input event to be consumed, do it now */ - return event; + return true; } input_start(); @@ -47,11 +46,12 @@ EventType event_poll(int32_t ms) } do { - /* Wait for some event */ + /* Run one event loop iteration, blocking for events if run_mode is + * UV_RUN_ONCE */ uv_run(uv_default_loop(), run_mode); } while ( /* Continue running if ... */ - (event = input_check()) == kEventNone && /* ... we have no input */ + !input_ready() && /* ... we have no input */ run_mode != UV_RUN_NOWAIT && /* ... ms != 0 */ !timed_out /* ... we didn't get a timeout */ ); @@ -64,7 +64,7 @@ EventType event_poll(int32_t ms) uv_timer_stop(&timer_req); } - return event; + return input_ready(); } /* Set a flag in the `event_poll` loop for signaling of a timeout */ diff --git a/src/os/event.h b/src/os/event.h index 230bb6fab9..874d7130bf 100644 --- a/src/os/event.h +++ b/src/os/event.h @@ -2,15 +2,10 @@ #define NEOVIM_OS_EVENT_H #include <stdint.h> - -typedef enum { - kEventNone, - kEventInput, - kEventEof -} EventType; +#include <stdbool.h> void event_init(void); -EventType event_poll(int32_t ms); +bool event_poll(int32_t ms); #endif diff --git a/src/os/input.c b/src/os/input.c index 0d344be11e..dd7d123536 100644 --- a/src/os/input.c +++ b/src/os/input.c @@ -16,6 +16,12 @@ #define READ_BUFFER_LENGTH 4096 +typedef enum { + kInputNone, + kInputAvail, + kInputEof +} InbufPollResult; + typedef struct { uv_buf_t uvbuf; uint32_t rpos, wpos, fpos; @@ -31,7 +37,7 @@ static uv_idle_t fread_idle; static uv_handle_type read_channel_type; static bool eof = false; -static EventType inbuf_poll(int32_t ms); +static InbufPollResult inbuf_poll(int32_t ms); static void stderr_switch(void); static void alloc_cb(uv_handle_t *, size_t, uv_buf_t *); static void read_cb(uv_stream_t *, ssize_t, const uv_buf_t *); @@ -53,17 +59,9 @@ void input_init() } /* Check if there's a pending input event */ -EventType input_check() +bool input_ready() { - if (rbuffer.rpos < rbuffer.wpos) { - return kEventInput; - } - - if (eof) { - return kEventEof; - } - - return kEventNone; + return rbuffer.rpos < rbuffer.wpos || eof; } /* Listen for input */ @@ -74,7 +72,7 @@ void input_start() rbuffer.uvbuf.base = (char *)(rbuffer.data + rbuffer.wpos); if (read_channel_type == UV_FILE) { - /* Just invoke the `fread_idle_cb` as soon as there are no pending events */ + /* Just invoke the `fread_idle_cb` as soon as the loop starts */ uv_idle_start(&fread_idle, fread_idle_cb); } else { /* Start reading */ @@ -126,14 +124,14 @@ uint32_t input_read(char *buf, uint32_t count) /* Low level input function. */ int mch_inchar(char_u *buf, int maxlen, long ms, int tb_change_cnt) { - EventType result; + InbufPollResult result; if (ms >= 0) { - if ((result = inbuf_poll(ms)) != kEventInput) { + if ((result = inbuf_poll(ms)) != kInputAvail) { return 0; } } else { - if ((result = inbuf_poll(p_ut)) != kEventInput) { + if ((result = inbuf_poll(p_ut)) != kInputAvail) { if (trigger_cursorhold() && maxlen >= 3 && !typebuf_changed(tb_change_cnt)) { buf[0] = K_SPECIAL; @@ -151,7 +149,7 @@ int mch_inchar(char_u *buf, int maxlen, long ms, int tb_change_cnt) if (typebuf_changed(tb_change_cnt)) return 0; - if (result == kEventEof) { + if (result == kInputEof) { read_error_exit(); return 0; } @@ -162,7 +160,7 @@ int mch_inchar(char_u *buf, int maxlen, long ms, int tb_change_cnt) /* Check if a character is available for reading */ bool mch_char_avail() { - return inbuf_poll(0) == kEventInput; + return inbuf_poll(0) == kInputAvail; } /* @@ -171,17 +169,24 @@ bool mch_char_avail() */ void mch_breakcheck() { - if (curr_tmode == TMODE_RAW && event_poll(0) == kEventInput) + if (curr_tmode == TMODE_RAW && event_poll(0)) fill_input_buf(FALSE); } /* This is a replacement for the old `WaitForChar` function in os_unix.c */ -static EventType inbuf_poll(int32_t ms) +static InbufPollResult inbuf_poll(int32_t ms) { if (input_available()) - return kEventInput; + return kInputAvail; + + if (event_poll(ms)) { + if (rbuffer.rpos == rbuffer.wpos && eof) { + return kInputEof; + } + return kInputAvail; + } - return event_poll(ms); + return kInputNone; } static void stderr_switch() diff --git a/src/os/input.h b/src/os/input.h index dd6a402cf9..a48769e843 100644 --- a/src/os/input.h +++ b/src/os/input.h @@ -4,11 +4,10 @@ #include <stdint.h> #include <stdbool.h> -#include "os/event.h" #include "types.h" void input_init(void); -EventType input_check(void); +bool input_ready(void); void input_start(void); void input_stop(void); uint32_t input_read(char *buf, uint32_t count); |