use libudev to handle device disconnect/reconnect
This change has substantially bumped up the daemon's overall robustness, as the code now ensures that the controller will only start once the /dev/input/eventXX file is set up, which was causing all sorts of issues in the past. Additionally, this change enables the daemon to run as a proper background task that _doesn't_ constantly die / need to be restarted, which removes the need for any janky udev "on add" rules, and instead, a simple systemd user service will suffice.
This commit is contained in:
@@ -35,7 +35,7 @@ pub struct DialController {
|
||||
active_mode: ActiveMode,
|
||||
|
||||
new_mode: Arc<Mutex<Option<usize>>>,
|
||||
meta_mode: Box<dyn ControlMode>, // always MetaMode
|
||||
meta_mode: Box<dyn ControlMode>, // concrete type is always `MetaMode`
|
||||
}
|
||||
|
||||
impl DialController {
|
||||
@@ -60,12 +60,6 @@ impl DialController {
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> Result<()> {
|
||||
let initial_mode = match self.active_mode {
|
||||
ActiveMode::Normal(i) => i,
|
||||
ActiveMode::Meta => 0,
|
||||
};
|
||||
self.modes[initial_mode].on_start(self.device.haptics())?;
|
||||
|
||||
loop {
|
||||
let evt = self.device.next_event()?;
|
||||
let haptics = self.device.haptics();
|
||||
@@ -80,13 +74,22 @@ impl DialController {
|
||||
ActiveMode::Meta => &mut self.meta_mode,
|
||||
};
|
||||
|
||||
// TODO: press and hold (+ rotate?) to switch between modes
|
||||
|
||||
match evt.kind {
|
||||
DialEventKind::Ignored => {}
|
||||
|
||||
DialEventKind::Connect => {
|
||||
eprintln!("Dial Connected");
|
||||
mode.on_start(haptics)?
|
||||
}
|
||||
DialEventKind::Disconnect => {
|
||||
eprintln!("Dial Disconnected");
|
||||
mode.on_end(haptics)?
|
||||
}
|
||||
|
||||
DialEventKind::ButtonPress => mode.on_btn_press(haptics)?,
|
||||
DialEventKind::ButtonRelease => mode.on_btn_release(haptics)?,
|
||||
DialEventKind::Dial(delta) => mode.on_dial(haptics, delta)?,
|
||||
|
||||
DialEventKind::ButtonLongPress => {
|
||||
eprintln!("long press!");
|
||||
if !matches!(self.active_mode, ActiveMode::Meta) {
|
||||
|
||||
Reference in New Issue
Block a user