diff --git a/src/logid/Configuration.cpp b/src/logid/Configuration.cpp index 7a16266..a94265a 100644 --- a/src/logid/Configuration.cpp +++ b/src/logid/Configuration.cpp @@ -25,6 +25,7 @@ using namespace logid; using namespace libconfig; +using namespace std::chrono; Configuration::Configuration(const std::string& config_file) { @@ -49,6 +50,23 @@ Configuration::Configuration(const std::string& config_file) return; } + _io_timeout = LOGID_DEFAULT_RAWDEVICE_TIMEOUT; + try { + auto& timeout = root["io_timeout"]; + if(timeout.isNumber()) { + auto t = timeout.getType(); + if(timeout.getType() == libconfig::Setting::TypeFloat) + _io_timeout = duration_cast( + duration(timeout)); + else + _io_timeout = milliseconds((int)timeout); + } else + logPrintf(WARN, "Line %d: io_timeout must be a number.", + timeout.getSourceLine()); + } catch(const SettingNotFoundException& e) { + // Ignore + } + for(int i = 0; i < devices->getLength(); i++) { const Setting &device = (*devices)[i]; std::string name; @@ -90,3 +108,8 @@ const char * Configuration::DeviceNotFound::what() const noexcept { return _name.c_str(); } + +std::chrono::milliseconds Configuration::ioTimeout() const +{ + return _io_timeout; +} diff --git a/src/logid/Configuration.h b/src/logid/Configuration.h index 2d2166a..d083ef9 100644 --- a/src/logid/Configuration.h +++ b/src/logid/Configuration.h @@ -22,6 +22,9 @@ #include #include #include +#include + +#define LOGID_DEFAULT_RAWDEVICE_TIMEOUT std::chrono::seconds(2) namespace logid { @@ -41,8 +44,11 @@ namespace logid private: std::string _name; }; + + std::chrono::milliseconds ioTimeout() const; private: std::map _device_paths; + std::chrono::milliseconds _io_timeout; libconfig::Config _config; }; diff --git a/src/logid/backend/raw/RawDevice.cpp b/src/logid/backend/raw/RawDevice.cpp index 680bdd2..2905c38 100644 --- a/src/logid/backend/raw/RawDevice.cpp +++ b/src/logid/backend/raw/RawDevice.cpp @@ -22,6 +22,7 @@ #include "../dj/defs.h" #include "../../util/log.h" #include "../hidpp/Report.h" +#include "../../Configuration.h" #include #include @@ -258,8 +259,15 @@ int RawDevice::_readReport(std::vector& report, std::size_t maxDataLeng int ret; report.resize(maxDataLength); - timeval timeout = { duration_cast(HIDPP_IO_TIMEOUT).count(), - duration_cast(HIDPP_IO_TIMEOUT).count() }; + timeval timeout{}; + timeout.tv_sec = duration_cast(global_config->ioTimeout()) + .count(); + timeout.tv_usec = duration_cast( + global_config->ioTimeout()).count() % + duration_cast(seconds(1)).count(); + + auto timeout_ms = duration_cast( + global_config->ioTimeout()).count(); fd_set fds; do { @@ -269,7 +277,7 @@ int RawDevice::_readReport(std::vector& report, std::size_t maxDataLeng ret = select(std::max(_fd, _pipe[0]) + 1, &fds, nullptr, nullptr, - (HIDPP_IO_TIMEOUT.count() > 0 ? nullptr : &timeout)); + (timeout_ms > 0 ? nullptr : &timeout)); } while(ret == -1 && errno == EINTR); if(ret == -1) diff --git a/src/logid/backend/raw/RawDevice.h b/src/logid/backend/raw/RawDevice.h index d12020a..3cb1823 100644 --- a/src/logid/backend/raw/RawDevice.h +++ b/src/logid/backend/raw/RawDevice.h @@ -30,8 +30,6 @@ #include "defs.h" #include "../../util/mutex_queue.h" -#define HIDPP_IO_TIMEOUT std::chrono::seconds(2) - namespace logid { namespace backend { namespace raw