fix: libinput dwt support (#196)

This commit is contained in:
Roland Fredenhagen 2022-10-22 22:17:27 +02:00 committed by GitHub
parent ae1b425141
commit c2f8d5d468
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -4,7 +4,7 @@ extern crate nix;
use anyhow::bail;
use derive_where::derive_where;
use evdev::uinput::{VirtualDevice, VirtualDeviceBuilder};
use evdev::{AttributeSet, Device, FetchEventsSynced, Key, RelativeAxisType};
use evdev::{AttributeSet, BusType, Device, FetchEventsSynced, InputId, Key, RelativeAxisType};
use nix::sys::inotify::{AddWatchFlags, InitFlags, Inotify};
use std::collections::HashMap;
use std::error::Error;
@ -38,7 +38,7 @@ static MOUSE_BTNS: [&str; 20] = [
];
// Credit: https://github.com/mooz/xkeysnail/blob/bf3c93b4fe6efd42893db4e6588e5ef1c4909cfb/xkeysnail/output.py#L10-L32
pub fn output_device() -> Result<VirtualDevice, Box<dyn Error>> {
pub fn output_device(bus_type: Option<BusType>) -> Result<VirtualDevice, Box<dyn Error>> {
let mut keys: AttributeSet<Key> = AttributeSet::new();
for code in Key::KEY_RESERVED.code()..Key::BTN_TRIGGER_HAPPY40.code() {
let key = Key::new(code);
@ -56,6 +56,8 @@ pub fn output_device() -> Result<VirtualDevice, Box<dyn Error>> {
relative_axes.insert(RelativeAxisType::REL_MISC);
let device = VirtualDeviceBuilder::new()?
// These are taken from https://docs.rs/evdev/0.12.0/src/evdev/uinput.rs.html#183-188
.input_id(InputId::new(bus_type.unwrap_or(BusType::BUS_USB), 0x1234, 0x5678, 0x111))
.name(&InputDevice::current_name())
.with_keys(&keys)?
.with_relative_axes(&relative_axes)?
@ -190,6 +192,10 @@ impl InputDevice {
fn device_name(&self) -> &str {
self.device.name().unwrap_or("<Unnamed device>")
}
pub fn bus_type(&self) -> BusType {
self.device.input_id().bus_type()
}
}
impl InputDevice {

View File

@ -105,14 +105,9 @@ fn main() -> anyhow::Result<()> {
let watch_config = watch.contains(&WatchTargets::Config);
// Event listeners
let output_device = match output_device() {
Ok(output_device) => output_device,
Err(e) => bail!("Failed to prepare an output device: {}", e),
};
let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty())?;
let timer_fd = timer.as_raw_fd();
let delay = Duration::from_millis(config.keypress_delay_ms);
let mut handler = EventHandler::new(output_device, timer, &config.default_mode, delay);
let mut input_devices = match get_input_devices(&device_filter, &ignore_filter, mouse, watch_devices) {
Ok(input_devices) => input_devices,
Err(e) => bail!("Failed to prepare input devices: {}", e),
@ -120,6 +115,11 @@ fn main() -> anyhow::Result<()> {
let device_watcher = device_watcher(watch_devices).context("Setting up device watcher")?;
let config_watcher = config_watcher(watch_config, &config_path).context("Setting up config watcher")?;
let watchers: Vec<_> = device_watcher.iter().chain(config_watcher.iter()).collect();
let output_device = match output_device(input_devices.values().next().map(InputDevice::bus_type)) {
Ok(output_device) => output_device,
Err(e) => bail!("Failed to prepare an output device: {}", e),
};
let mut handler = EventHandler::new(output_device, timer, &config.default_mode, delay);
// Main loop
loop {