Query devices

This commit is contained in:
hodasemi 2022-01-30 15:35:02 +01:00
parent 6e5565d45c
commit 1244041861
3 changed files with 120 additions and 2 deletions

View file

@ -16,4 +16,5 @@ strum_macros = "*"
serde = { version = "*", features = ["derive"] }
anyhow = "*"
nalgebra = { version = "*", features = ["serde-serialize"] }
rfd = "*"
rfd = "*"
evdev = { git = "http://www.gavania.de/hodasemi/evdev-rs" }

104
src/device.rs Normal file
View file

@ -0,0 +1,104 @@
use evdev::{Device as EvDevice, EventType, InputEventKind};
use std::{collections::HashMap, path::PathBuf};
pub struct Device {
device: EvDevice,
path: PathBuf,
supported_events: HashMap<EventType, Vec<InputEventKind>>,
}
impl Device {
pub fn query() -> Vec<Device> {
evdev::enumerate()
.map(|(path, device)| {
let supported_events = device
.supported_events()
.iter()
.map(|event| {
let input_kind: Vec<InputEventKind> = match event {
evdev::EventType::KEY => device
.supported_keys()
.map(|keys| {
keys.iter().map(|key| InputEventKind::Key(key)).collect()
})
.unwrap_or(Vec::new()),
evdev::EventType::RELATIVE => device
.supported_relative_axes()
.map(|axes| {
axes.iter()
.map(|rel_axis| InputEventKind::RelAxis(rel_axis))
.collect()
})
.unwrap_or(Vec::new()),
evdev::EventType::ABSOLUTE => device
.supported_absolute_axes()
.map(|axes| {
axes.iter()
.map(|abs_axis| InputEventKind::AbsAxis(abs_axis))
.collect()
})
.unwrap_or(Vec::new()),
evdev::EventType::SWITCH => device
.supported_switches()
.map(|switches| {
switches
.iter()
.map(|switch| InputEventKind::Switch(switch))
.collect()
})
.unwrap_or(Vec::new()),
evdev::EventType::MISC => device
.misc_properties()
.map(|miscs| {
miscs
.iter()
.map(|misc| InputEventKind::Misc(misc))
.collect()
})
.unwrap_or(Vec::new()),
evdev::EventType::SOUND => device
.supported_sounds()
.map(|sounds| {
sounds
.iter()
.map(|sound| InputEventKind::Sound(sound))
.collect()
})
.unwrap_or(Vec::new()),
evdev::EventType::FORCEFEEDBACK => device
.supported_forcefeedback_effects()
.map(|ffs| {
ffs.iter()
.map(|ff| InputEventKind::ForceFeedback(ff))
.collect()
})
.unwrap_or(Vec::new()),
_ => Vec::new(),
};
(event, input_kind)
})
.collect();
Device {
device,
path,
supported_events,
}
})
.collect()
}
}
impl std::fmt::Debug for Device {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Device")
.field("name", &self.device.name())
.field("path", &self.path)
.field("supported_events", &self.supported_events)
.finish()
}
}

View file

@ -3,6 +3,7 @@ use editor_state::GraphEditorState;
mod graph_node_ui;
mod color_hex_utils;
mod device;
mod editor_state;
mod graph_editor_egui;
mod graph_impls;
@ -13,11 +14,23 @@ mod node_finder;
mod node_types;
mod param_ui;
use device::Device;
pub type SVec<T> = smallvec::SmallVec<[T; 4]>;
#[derive(Default)]
struct App {
pub state: GraphEditorState,
pub devices: Vec<Device>,
}
impl App {
fn new() -> Self {
Self {
state: GraphEditorState::default(),
devices: dbg!(Device::query()),
}
}
}
impl epi::App for App {
@ -37,5 +50,5 @@ fn main() {
..Default::default()
};
eframe::run_native(Box::new(App::default()), options);
eframe::run_native(Box::new(App::new()), options);
}