mirror of https://github.com/k0kubun/xremap
Implement xremap's VirtualDevice
parent
d21a3e7650
commit
f058c44433
@ -1,13 +1,45 @@
|
||||
use evdev::uinput::{VirtualDevice, VirtualDeviceBuilder};
|
||||
use evdev::Device;
|
||||
use evdev::{AttributeSet, Key, RelativeAxisType};
|
||||
use std::error::Error;
|
||||
|
||||
pub fn build_device(base_device: &Device) -> Result<VirtualDevice, Box<dyn Error>> {
|
||||
let builder = VirtualDeviceBuilder::new()?.name("xremap");
|
||||
let device = match base_device.supported_keys() {
|
||||
Some(keys) => builder.with_keys(keys)?,
|
||||
None => builder,
|
||||
static MOUSE_BTNS: [&str; 13] = [
|
||||
"BTN_0",
|
||||
"BTN_1",
|
||||
"BTN_2",
|
||||
"BTN_3",
|
||||
"BTN_4",
|
||||
"BTN_5",
|
||||
"BTN_6",
|
||||
"BTN_7",
|
||||
"BTN_8",
|
||||
"BTN_9",
|
||||
"BTN_LEFT",
|
||||
"BTN_MIDDLE",
|
||||
"BTN_RIGHT",
|
||||
];
|
||||
|
||||
// Credit: https://github.com/mooz/xkeysnail/blob/master/xkeysnail/output.py#L10-L32
|
||||
pub fn build_device() -> 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);
|
||||
let name = format!("{:?}", key);
|
||||
if name.starts_with("KEY_") || MOUSE_BTNS.contains(&&**&name) {
|
||||
keys.insert(key);
|
||||
}
|
||||
}
|
||||
.build()?;
|
||||
|
||||
let mut relative_axes: AttributeSet<RelativeAxisType> = AttributeSet::new();
|
||||
relative_axes.insert(RelativeAxisType::REL_X);
|
||||
relative_axes.insert(RelativeAxisType::REL_Y);
|
||||
relative_axes.insert(RelativeAxisType::REL_HWHEEL);
|
||||
relative_axes.insert(RelativeAxisType::REL_WHEEL);
|
||||
relative_axes.insert(RelativeAxisType::REL_MISC);
|
||||
|
||||
let device = VirtualDeviceBuilder::new()?
|
||||
.name("xremap")
|
||||
.with_keys(&keys)?
|
||||
.with_relative_axes(&relative_axes)?
|
||||
.build()?;
|
||||
Ok(device)
|
||||
}
|
||||
|
Loading…
Reference in New Issue