event: Decouple user input checks from event_poll

This was done to generalize the usage of `event_poll`, which will now return
`true` only if a event has been processed/deferred before the timeout(if not
-1).

To do that, the `input_ready` calls have been extracted to the input.c
module(the `event_poll` call has been surrounded by `input_ready` calls,
resulting in the same behavior).

The `input_start`/`input_stop` calls still present in `event_poll` are
temporary: When the API becomes the only way to read user input, it will no
longer be necessary to start/stop the input stream.
This commit is contained in:
Thiago de Arruda 2014-06-20 10:36:58 -03:00
parent b00a37544c
commit ef4c5ccb21
2 changed files with 19 additions and 15 deletions

View File

@ -63,11 +63,6 @@ bool event_poll(int32_t ms)
{
uv_run_mode run_mode = UV_RUN_ONCE;
if (input_ready()) {
// If there's a pending input event to be consumed, do it now
return true;
}
static int recursive = 0;
if (!(recursive++)) {
@ -95,13 +90,16 @@ bool event_poll(int32_t ms)
run_mode = UV_RUN_NOWAIT;
}
bool events_processed;
do {
// Run one event loop iteration, blocking for events if run_mode is
// UV_RUN_ONCE
uv_run(uv_default_loop(), run_mode);
events_processed = event_process(false);
} while (
// Continue running if ...
!event_process(false) && // we didn't process any immediate events
!events_processed && // we didn't process any immediate events
!event_has_deferred() && // no events are waiting to be processed
run_mode != UV_RUN_NOWAIT && // ms != 0
!timer_data.timed_out); // we didn't get a timeout
@ -120,7 +118,7 @@ bool event_poll(int32_t ms)
event_process(false);
}
return input_ready() || event_has_deferred();
return !timer_data.timed_out && (events_processed || event_has_deferred());
}
bool event_has_deferred()

View File

@ -37,12 +37,6 @@ void input_init()
rstream_set_file(read_stream, read_cmd_fd);
}
// Check if there's pending input
bool input_ready()
{
return rstream_available(read_stream) > 0 || eof;
}
// Listen for input
void input_start()
{
@ -119,7 +113,7 @@ bool os_char_avail()
// In cooked mode we should get SIGINT, no need to check.
void os_breakcheck()
{
if (curr_tmode == TMODE_RAW && event_poll(0))
if (curr_tmode == TMODE_RAW && input_poll(0))
fill_input_buf(false);
}
@ -132,6 +126,11 @@ bool os_isatty(int fd)
return uv_guess_handle(fd) == UV_TTY;
}
static bool input_poll(int32_t ms)
{
return input_ready() || event_poll(ms) || input_ready();
}
// This is a replacement for the old `WaitForChar` function in os_unix.c
static InbufPollResult inbuf_poll(int32_t ms)
{
@ -139,7 +138,7 @@ static InbufPollResult inbuf_poll(int32_t ms)
return kInputAvail;
}
if (event_poll(ms)) {
if (input_poll(ms)) {
return eof && rstream_available(read_stream) == 0 ?
kInputEof :
kInputAvail;
@ -196,3 +195,10 @@ static int push_event_key(uint8_t *buf, int maxlen)
return buf_idx;
}
// Check if there's pending input
bool input_ready()
{
return rstream_available(read_stream) > 0 || eof;
}