2016-05-02 02:54:25 +00:00
|
|
|
// Implementation file for the low level input library.
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2019-10-13 22:50:48 +00:00
|
|
|
|
2019-05-05 10:09:25 +00:00
|
|
|
#include <cstring>
|
2006-08-10 19:02:46 +00:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-05-07 04:22:28 +00:00
|
|
|
#include <sys/time.h>
|
2016-05-02 02:54:25 +00:00
|
|
|
#include <sys/types.h>
|
2017-02-11 02:47:02 +00:00
|
|
|
|
2019-10-13 22:50:48 +00:00
|
|
|
#include <cwchar>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <deque>
|
|
|
|
#include <list>
|
2016-05-02 02:54:25 +00:00
|
|
|
#include <memory>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <type_traits>
|
2018-02-19 02:39:03 +00:00
|
|
|
#include <utility>
|
2006-01-19 12:22:07 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
2015-07-25 15:14:25 +00:00
|
|
|
#include "env.h"
|
2016-05-02 02:54:25 +00:00
|
|
|
#include "env_universal_common.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2019-05-05 00:32:40 +00:00
|
|
|
#include "global_safety.h"
|
2016-05-02 02:54:25 +00:00
|
|
|
#include "input_common.h"
|
2011-12-27 05:05:25 +00:00
|
|
|
#include "iothread.h"
|
2016-11-23 04:24:03 +00:00
|
|
|
#include "wutil.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-07 04:22:28 +00:00
|
|
|
/// Time in milliseconds to wait for another byte to be available for reading
|
2018-06-18 05:01:32 +00:00
|
|
|
/// after \x1B is read before assuming that escape key was pressed, and not an
|
2016-05-07 04:22:28 +00:00
|
|
|
/// escape sequence.
|
2019-02-07 11:08:24 +00:00
|
|
|
#define WAIT_ON_ESCAPE_DEFAULT 30
|
2016-01-15 05:46:53 +00:00
|
|
|
static int wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
/// Callback function for handling interrupts on reading.
|
2019-03-16 20:52:07 +00:00
|
|
|
static interrupt_func_t interrupt_handler;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-03-16 20:52:07 +00:00
|
|
|
void input_common_init(interrupt_func_t func) { interrupt_handler = func; }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
/// Internal function used by input_common_readch to read one byte from fd 0. This function should
|
|
|
|
/// only be called by input_common_readch().
|
2019-06-02 22:41:23 +00:00
|
|
|
char_event_t input_event_queue_t::readb() {
|
2019-04-29 05:08:40 +00:00
|
|
|
for (;;) {
|
2012-11-19 00:30:30 +00:00
|
|
|
fd_set fdset;
|
2014-04-30 00:03:00 +00:00
|
|
|
int fd_max = 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
int ioport = iothread_port();
|
|
|
|
int res;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(0, &fdset);
|
2016-05-02 02:54:25 +00:00
|
|
|
if (ioport > 0) {
|
2012-11-19 00:30:30 +00:00
|
|
|
FD_SET(ioport, &fdset);
|
2019-03-13 05:38:42 +00:00
|
|
|
fd_max = std::max(fd_max, ioport);
|
2014-04-30 00:03:00 +00:00
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
|
|
|
|
// Get our uvar notifier.
|
2020-01-16 23:14:21 +00:00
|
|
|
universal_notifier_t& notifier = universal_notifier_t::default_notifier();
|
2016-05-02 02:54:25 +00:00
|
|
|
|
|
|
|
// Get the notification fd (possibly none).
|
2014-04-30 22:50:03 +00:00
|
|
|
int notifier_fd = notifier.notification_fd();
|
2016-05-02 02:54:25 +00:00
|
|
|
if (notifier_fd > 0) {
|
2014-04-30 22:50:03 +00:00
|
|
|
FD_SET(notifier_fd, &fdset);
|
2019-03-13 05:38:42 +00:00
|
|
|
fd_max = std::max(fd_max, notifier_fd);
|
2014-04-30 22:50:03 +00:00
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
|
|
|
|
// Get its suggested delay (possibly none).
|
2014-04-30 00:03:00 +00:00
|
|
|
struct timeval tv = {};
|
|
|
|
const unsigned long usecs_delay = notifier.usec_delay_between_polls();
|
2016-05-02 02:54:25 +00:00
|
|
|
if (usecs_delay > 0) {
|
2014-04-30 00:03:00 +00:00
|
|
|
unsigned long usecs_per_sec = 1000000;
|
2019-11-19 01:08:16 +00:00
|
|
|
tv.tv_sec = static_cast<int>(usecs_delay / usecs_per_sec);
|
|
|
|
tv.tv_usec = static_cast<int>(usecs_delay % usecs_per_sec);
|
2014-04-30 00:03:00 +00:00
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
|
2019-11-19 02:34:50 +00:00
|
|
|
res = select(fd_max + 1, &fdset, nullptr, nullptr, usecs_delay > 0 ? &tv : nullptr);
|
2016-05-02 02:54:25 +00:00
|
|
|
if (res == -1) {
|
2016-10-23 03:32:25 +00:00
|
|
|
if (errno == EINTR || errno == EAGAIN) {
|
|
|
|
if (interrupt_handler) {
|
2019-03-16 20:52:07 +00:00
|
|
|
if (auto interrupt_evt = interrupt_handler()) {
|
|
|
|
return *interrupt_evt;
|
2019-06-02 22:41:23 +00:00
|
|
|
} else if (auto mc = pop_discard_timeouts()) {
|
2019-03-15 08:11:15 +00:00
|
|
|
return *mc;
|
|
|
|
}
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
2016-10-23 03:32:25 +00:00
|
|
|
} else {
|
2019-03-16 19:35:49 +00:00
|
|
|
// The terminal has been closed.
|
2019-03-16 21:08:00 +00:00
|
|
|
return char_event_type_t::eof;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
} else {
|
|
|
|
// Check to see if we want a universal variable barrier.
|
2014-05-14 06:16:53 +00:00
|
|
|
bool barrier_from_poll = notifier.poll();
|
|
|
|
bool barrier_from_readability = false;
|
2016-05-02 02:54:25 +00:00
|
|
|
if (notifier_fd > 0 && FD_ISSET(notifier_fd, &fdset)) {
|
2014-05-14 06:16:53 +00:00
|
|
|
barrier_from_readability = notifier.notification_fd_became_readable(notifier_fd);
|
2014-04-30 22:50:03 +00:00
|
|
|
}
|
2016-05-02 02:54:25 +00:00
|
|
|
if (barrier_from_poll || barrier_from_readability) {
|
2019-04-29 05:05:03 +00:00
|
|
|
if (env_universal_barrier()) {
|
|
|
|
// A variable change may have triggered a repaint, etc.
|
2019-06-02 22:41:23 +00:00
|
|
|
if (auto mc = pop_discard_timeouts()) {
|
2019-04-29 05:05:03 +00:00
|
|
|
return *mc;
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 00:03:00 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
if (FD_ISSET(STDIN_FILENO, &fdset)) {
|
2019-04-29 05:08:40 +00:00
|
|
|
unsigned char arr[1];
|
2016-05-02 02:54:25 +00:00
|
|
|
if (read_blocked(0, arr, 1) != 1) {
|
2019-03-16 19:35:49 +00:00
|
|
|
// The teminal has been closed.
|
2019-03-16 21:08:00 +00:00
|
|
|
return char_event_type_t::eof;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
// We read from stdin, so don't loop.
|
2019-04-29 05:08:40 +00:00
|
|
|
return arr[0];
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-06-23 23:19:13 +00:00
|
|
|
|
|
|
|
// Check for iothread completions only if there is no data to be read from the stdin.
|
|
|
|
// This gives priority to the foreground.
|
|
|
|
if (ioport > 0 && FD_ISSET(ioport, &fdset)) {
|
|
|
|
iothread_service_completion();
|
|
|
|
if (auto mc = pop_discard_timeouts()) {
|
|
|
|
return *mc;
|
|
|
|
}
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-04-29 05:08:40 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 02:54:25 +00:00
|
|
|
// Update the wait_on_escape_ms value in response to the fish_escape_delay_ms user variable being
|
|
|
|
// set.
|
2020-01-16 23:14:21 +00:00
|
|
|
void update_wait_on_escape_ms(const environment_t& vars) {
|
2018-09-18 04:26:21 +00:00
|
|
|
auto escape_time_ms = vars.get(L"fish_escape_delay_ms");
|
2016-05-02 02:54:25 +00:00
|
|
|
if (escape_time_ms.missing_or_empty()) {
|
2016-01-15 05:46:53 +00:00
|
|
|
wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-28 07:25:41 +00:00
|
|
|
long tmp = fish_wcstol(escape_time_ms->as_string().c_str());
|
2016-11-23 04:24:03 +00:00
|
|
|
if (errno || tmp < 10 || tmp >= 5000) {
|
2019-03-12 21:06:01 +00:00
|
|
|
std::fwprintf(stderr,
|
2019-05-05 10:09:25 +00:00
|
|
|
L"ignoring fish_escape_delay_ms: value '%ls' "
|
|
|
|
L"is not an integer or is < 10 or >= 5000 ms\n",
|
|
|
|
escape_time_ms->as_string().c_str());
|
2016-05-02 02:54:25 +00:00
|
|
|
} else {
|
2019-11-19 01:08:16 +00:00
|
|
|
wait_on_escape_ms = static_cast<int>(tmp);
|
2016-01-15 05:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 22:41:23 +00:00
|
|
|
char_event_t input_event_queue_t::pop() {
|
|
|
|
auto result = queue_.front();
|
|
|
|
queue_.pop_front();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
maybe_t<char_event_t> input_event_queue_t::pop_discard_timeouts() {
|
|
|
|
while (has_lookahead()) {
|
|
|
|
auto evt = pop();
|
|
|
|
if (!evt.is_timeout()) {
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none();
|
|
|
|
}
|
|
|
|
|
|
|
|
char_event_t input_event_queue_t::readch() {
|
2019-05-05 00:32:40 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2019-06-02 22:41:23 +00:00
|
|
|
if (auto mc = pop_discard_timeouts()) {
|
2019-03-15 08:11:15 +00:00
|
|
|
return *mc;
|
|
|
|
}
|
|
|
|
wchar_t res;
|
|
|
|
mbstate_t state = {};
|
2019-11-26 00:36:13 +00:00
|
|
|
while (true) {
|
2019-03-16 21:08:00 +00:00
|
|
|
auto evt = readb();
|
2019-03-16 23:48:23 +00:00
|
|
|
if (!evt.is_char()) {
|
2019-03-16 21:08:00 +00:00
|
|
|
return evt;
|
2019-03-16 19:35:49 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-03-16 21:08:00 +00:00
|
|
|
wint_t b = evt.get_char();
|
2019-03-15 08:11:15 +00:00
|
|
|
if (MB_CUR_MAX == 1) {
|
|
|
|
return b; // single-byte locale, all values are legal
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-03-15 08:11:15 +00:00
|
|
|
char bb = b;
|
|
|
|
size_t sz = std::mbrtowc(&res, &bb, 1, &state);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-03-15 08:11:15 +00:00
|
|
|
switch (sz) {
|
2019-11-19 01:08:16 +00:00
|
|
|
case static_cast<size_t>(-1): {
|
2019-03-15 08:11:15 +00:00
|
|
|
std::memset(&state, '\0', sizeof(state));
|
|
|
|
debug(2, L"Illegal input");
|
2019-03-16 22:49:35 +00:00
|
|
|
return char_event_type_t::check_exit;
|
2016-07-12 03:31:30 +00:00
|
|
|
}
|
2019-11-19 01:08:16 +00:00
|
|
|
case static_cast<size_t>(-2): {
|
2019-03-15 08:11:15 +00:00
|
|
|
break;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-03-15 08:11:15 +00:00
|
|
|
case 0: {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-05 10:09:25 +00:00
|
|
|
default: {
|
|
|
|
return res;
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-03-15 08:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 22:41:23 +00:00
|
|
|
char_event_t input_event_queue_t::readch_timed(bool dequeue_timeouts) {
|
2019-03-15 08:11:15 +00:00
|
|
|
char_event_t result{char_event_type_t::timeout};
|
2019-06-02 22:41:23 +00:00
|
|
|
if (has_lookahead()) {
|
|
|
|
result = pop();
|
2016-05-02 02:54:25 +00:00
|
|
|
} else {
|
2019-03-15 08:11:15 +00:00
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(STDIN_FILENO, &fds);
|
|
|
|
struct timeval tm = {wait_on_escape_ms / 1000, 1000 * (wait_on_escape_ms % 1000)};
|
2019-11-19 02:34:50 +00:00
|
|
|
if (select(1, &fds, nullptr, nullptr, &tm) > 0) {
|
2019-06-02 22:41:23 +00:00
|
|
|
result = readch();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-15 08:11:15 +00:00
|
|
|
// If we got a timeout, either through dequeuing or creating, ensure it stays on the queue.
|
|
|
|
if (result.is_timeout()) {
|
2019-06-02 22:41:23 +00:00
|
|
|
if (!dequeue_timeouts) queue_.push_front(char_event_type_t::timeout);
|
2019-03-15 08:11:15 +00:00
|
|
|
return char_event_type_t::timeout;
|
|
|
|
}
|
2019-03-16 23:48:23 +00:00
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 05:54:21 +00:00
|
|
|
void input_event_queue_t::push_back(const char_event_t& ch) { queue_.push_back(ch); }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-12-27 05:54:21 +00:00
|
|
|
void input_event_queue_t::push_front(const char_event_t& ch) { queue_.push_front(ch); }
|