fix(keyboard): correct device-path config behavior

This commit is contained in:
asas1asas200 2022-08-24 14:08:34 +08:00
parent 5944989a8a
commit 8b03e38594
1 changed files with 21 additions and 11 deletions

View File

@ -137,7 +137,11 @@ waybar::modules::KeyboardState::KeyboardState(const std::string& id, const Bar&
if (config_["device-path"].isString()) { if (config_["device-path"].isString()) {
std::string dev_path = config_["device-path"].asString(); std::string dev_path = config_["device-path"].asString();
tryAddDevice(dev_path); tryAddDevice(dev_path);
} else { if (libinput_devices_.empty()) {
spdlog::error("keyboard-state: Cannot find device {}", dev_path);
}
}
DIR* dev_dir = opendir(devices_path_.c_str()); DIR* dev_dir = opendir(devices_path_.c_str());
if (dev_dir == nullptr) { if (dev_dir == nullptr) {
throw errno_error(errno, "Failed to open " + devices_path_); throw errno_error(errno, "Failed to open " + devices_path_);
@ -148,7 +152,7 @@ waybar::modules::KeyboardState::KeyboardState(const std::string& id, const Bar&
std::string dev_path = devices_path_ + ep->d_name; std::string dev_path = devices_path_ + ep->d_name;
tryAddDevice(dev_path); tryAddDevice(dev_path);
} }
}
if (libinput_devices_.empty()) { if (libinput_devices_.empty()) {
throw errno_error(errno, "Failed to find keyboard device"); throw errno_error(errno, "Failed to find keyboard device");
} }
@ -241,7 +245,13 @@ auto waybar::modules::KeyboardState::update() -> void {
int numl = 0, capsl = 0, scrolll = 0; int numl = 0, capsl = 0, scrolll = 0;
try { try {
std::string dev_path = libinput_devices_.begin()->first; std::string dev_path;
if (config_["device-path"].isString() &&
libinput_devices_.find(config_["device-path"].asString()) != libinput_devices_.end()) {
dev_path = config_["device-path"].asString();
} else {
dev_path = libinput_devices_.begin()->first;
}
int fd = openFile(dev_path, O_NONBLOCK | O_CLOEXEC | O_RDONLY); int fd = openFile(dev_path, O_NONBLOCK | O_CLOEXEC | O_RDONLY);
auto dev = openDevice(fd); auto dev = openDevice(fd);
numl = libevdev_get_event_value(dev, EV_LED, LED_NUML); numl = libevdev_get_event_value(dev, EV_LED, LED_NUML);