diff --git a/src/logid/Configuration.cpp b/src/logid/Configuration.cpp index c287441..a5fad09 100644 --- a/src/logid/Configuration.cpp +++ b/src/logid/Configuration.cpp @@ -96,6 +96,46 @@ Configuration::Configuration(const std::string& config_file) catch(const SettingNotFoundException &e) { logPrintf(WARN, "No devices listed in config file."); } + + try { + auto& ignore = root.lookup("ignore"); + if(ignore.getType() == libconfig::Setting::TypeInt) { + _ignore_list.insert((int)ignore); + } else if(ignore.isList() || ignore.isArray()) { + int ignore_count = ignore.getLength(); + for(int i = 0; i < ignore_count; i++) { + if(ignore[i].getType() != libconfig::Setting::TypeInt) { + logPrintf(WARN, "Line %d: ignore must refer to device PIDs", + ignore[i].getSourceLine()); + if(ignore.isArray()) + break; + } else + _ignore_list.insert((int)ignore[i]); + } + } + } catch(const SettingNotFoundException& e) { + // May be called blacklist + try { + auto& ignore = root.lookup("blacklist"); + if(ignore.getType() == libconfig::Setting::TypeInt) { + _ignore_list.insert((int)ignore); + } else if(ignore.isList() || ignore.isArray()) { + int ignore_count = ignore.getLength(); + for(int i = 0; i < ignore_count; i++) { + if(ignore[i].getType() != libconfig::Setting::TypeInt) { + logPrintf(WARN, "Line %d: blacklist must refer to " + "device PIDs", + ignore[i].getSourceLine()); + if(ignore.isArray()) + break; + } else + _ignore_list.insert((int)ignore[i]); + } + } + } catch(const SettingNotFoundException& e) { + // Ignore + } + } } libconfig::Setting& Configuration::getSetting(const std::string& path) @@ -112,6 +152,11 @@ std::string Configuration::getDevice(const std::string& name) return it->second; } +bool Configuration::isIgnored(uint16_t pid) const +{ + return _ignore_list.find(pid) != _ignore_list.end(); +} + Configuration::DeviceNotFound::DeviceNotFound(std::string name) : _name (std::move(name)) { diff --git a/src/logid/Configuration.h b/src/logid/Configuration.h index 7a196ed..03eb62d 100644 --- a/src/logid/Configuration.h +++ b/src/logid/Configuration.h @@ -23,6 +23,7 @@ #include #include #include +#include #define LOGID_DEFAULT_IO_TIMEOUT std::chrono::seconds(2) #define LOGID_DEFAULT_WORKER_COUNT 4 @@ -36,6 +37,7 @@ namespace logid Configuration() = default; libconfig::Setting& getSetting(const std::string& path); std::string getDevice(const std::string& name); + bool isIgnored(uint16_t pid) const; class DeviceNotFound : public std::exception { @@ -50,6 +52,7 @@ namespace logid int workerCount() const; private: std::map _device_paths; + std::set _ignore_list; std::chrono::milliseconds _io_timeout = LOGID_DEFAULT_IO_TIMEOUT; int _worker_threads = LOGID_DEFAULT_WORKER_COUNT; libconfig::Config _config; diff --git a/src/logid/DeviceManager.cpp b/src/logid/DeviceManager.cpp index 40185df..e8016d4 100644 --- a/src/logid/DeviceManager.cpp +++ b/src/logid/DeviceManager.cpp @@ -32,6 +32,17 @@ void DeviceManager::addDevice(std::string path) { bool defaultExists = true; bool isReceiver = false; + + // Check if device is ignored before continuing + { + raw::RawDevice raw_dev(path); + if(global_config->isIgnored(raw_dev.productId())) { + logPrintf(DEBUG, "%s: Device 0x%04x ignored.", + path.c_str(), raw_dev.productId()); + return; + } + } + try { hidpp::Device device(path, hidpp::DefaultDevice); isReceiver = device.version() == std::make_tuple(1, 0); diff --git a/src/logid/Receiver.cpp b/src/logid/Receiver.cpp index 499cd9b..15595e5 100644 --- a/src/logid/Receiver.cpp +++ b/src/logid/Receiver.cpp @@ -34,6 +34,13 @@ void Receiver::addDevice(hidpp::DeviceConnectionEvent event) { std::unique_lock lock(_devices_change); try { + // Check if device is ignored before continuing + if(global_config->isIgnored(event.pid)) { + logPrintf(DEBUG, "%s:%d: Device 0x%04x ignored.", + _path.c_str(), event.index, event.pid); + return; + } + auto dev = _devices.find(event.index); if(dev != _devices.end()) { if(event.linkEstablished)