From bd5db3dc677ecd84cdece648d0addbd39e80b62a Mon Sep 17 00:00:00 2001 From: Daniel Prilik Date: Sun, 1 Nov 2020 23:30:12 -0500 Subject: [PATCH] clean up notification when terminated --- Cargo.lock | 4 +-- Cargo.toml | 2 +- src/dial_device.rs | 25 ++++++++--------- src/main.rs | 67 ++++++++++++++++++++++++++++++++++------------ 4 files changed, 64 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 97b4bf9..e195bac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -145,7 +145,7 @@ dependencies = [ [[package]] name = "evdev-rs" version = "0.4.0" -source = "git+https://github.com/ndesh26/evdev-rs.git#8e995b8bfc1f8ed844ae47edaa4286c99e8c88b5" +source = "git+https://github.com/ndesh26/evdev-rs.git?rev=8e995b8bf#8e995b8bfc1f8ed844ae47edaa4286c99e8c88b5" dependencies = [ "bitflags 1.2.1", "evdev-sys", @@ -156,7 +156,7 @@ dependencies = [ [[package]] name = "evdev-sys" version = "0.2.1" -source = "git+https://github.com/ndesh26/evdev-rs.git#8e995b8bfc1f8ed844ae47edaa4286c99e8c88b5" +source = "git+https://github.com/ndesh26/evdev-rs.git?rev=8e995b8bf#8e995b8bfc1f8ed844ae47edaa4286c99e8c88b5" dependencies = [ "cc", "libc", diff --git a/Cargo.toml b/Cargo.toml index 696c104..46889d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] directories = "3.0" # master includes a PR that implements `Send` for `Device` and `UInputDevice` -evdev-rs = { git = "https://github.com/ndesh26/evdev-rs.git" } +evdev-rs = { git = "https://github.com/ndesh26/evdev-rs.git", rev = "8e995b8bf" } hidapi = { version = "1.2.3", default-features = false, features = ["linux-shared-hidraw"] } notify-rust = "4" signal-hook = "0.1.16" diff --git a/src/dial_device.rs b/src/dial_device.rs index 58fedd2..7a37884 100644 --- a/src/dial_device.rs +++ b/src/dial_device.rs @@ -2,7 +2,7 @@ use std::fs; use std::sync::mpsc; use std::time::Duration; -use evdev_rs::{Device, InputEvent}; +use evdev_rs::{Device, InputEvent, ReadStatus}; use hidapi::{HidApi, HidDevice}; use crate::error::Error; @@ -10,7 +10,7 @@ use crate::error::Error; pub struct DialDevice { long_press_timeout: Duration, haptics: DialHaptics, - events: mpsc::Receiver, + events: mpsc::Receiver>, possible_long_press: bool, } @@ -75,17 +75,10 @@ impl DialDevice { std::thread::spawn({ let events = events_tx; - move || { - loop { - let (_axis_status, axis_evt) = axis - .next_event(evdev_rs::ReadFlag::NORMAL) - .expect("Error::Evdev"); - // assert!(matches!(axis_status, ReadStatus::Success)); - let event = DialEvent::from_raw_evt(axis_evt.clone()) - .expect("Error::UnexpectedEvt(axis_evt)"); - - events.send(event).expect("failed to send axis event"); - } + move || loop { + events + .send(axis.next_event(evdev_rs::ReadFlag::NORMAL)) + .expect("failed to send axis event"); } }); @@ -108,7 +101,10 @@ impl DialDevice { }; let event = match evt { - Ok(event) => { + Ok(Ok((_event_status, event))) => { + // assert!(matches!(axis_status, ReadStatus::Success)); + let event = + DialEvent::from_raw_evt(event.clone()).ok_or(Error::UnexpectedEvt(event))?; match event.kind { DialEventKind::ButtonPress => self.possible_long_press = true, DialEventKind::ButtonRelease => self.possible_long_press = false, @@ -116,6 +112,7 @@ impl DialDevice { } event } + Ok(Err(e)) => return Err(Error::Evdev(e)), Err(mpsc::RecvTimeoutError::Timeout) => { self.possible_long_press = false; DialEvent { diff --git a/src/main.rs b/src/main.rs index 72f4cf3..1b6335b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,8 @@ mod fake_input; pub type DynResult = Result>; +use std::sync::mpsc; + use crate::controller::DialController; use crate::dial_device::DialDevice; use crate::error::Error; @@ -17,22 +19,9 @@ use notify_rust::{Hint, Notification, Timeout}; use signal_hook::{iterator::Signals, SIGINT, SIGTERM}; fn main() { - if let Err(e) = true_main() { - println!("{}", e); - } -} - -fn true_main() -> DynResult<()> { - println!("Started"); - - let cfg = config::Config::from_disk()?; - - let dial = DialDevice::new(std::time::Duration::from_millis(750))?; - println!("Found the dial"); - - std::thread::spawn(move || { - // TODO: use this persistent notification for the meta mode. + let (kill_notif_tx, kill_notif_rx) = mpsc::channel::>(); + let handle = std::thread::spawn(move || { let active_notification = Notification::new() .hint(Hint::Resident(true)) .hint(Hint::Category("device".into())) @@ -43,11 +32,55 @@ fn true_main() -> DynResult<()> { .show() .expect("failed to send notification"); + let kill_notif = kill_notif_rx.recv(); + + active_notification.close(); + + let (msg, icon) = match kill_notif { + Ok(None) => { + // shutdown immediately + std::process::exit(1); + } + Ok(Some((msg, icon))) => (msg, icon), + Err(_) => ("Unexpected Error".into(), "dialog-error"), + }; + + Notification::new() + .hint(Hint::Transient(true)) + .hint(Hint::Category("device".into())) + .timeout(Timeout::Milliseconds(100)) + .summary("Surface Dial") + .body(&msg) + .icon(icon) + .show() + .unwrap(); + + std::process::exit(1); + }); + + if let Err(e) = true_main(kill_notif_tx.clone()) { + println!("{}", e); + } + + kill_notif_tx.send(None).unwrap(); // silently shut down + handle.join().unwrap(); +} + +fn true_main(kill_notif_tx: mpsc::Sender>) -> DynResult<()> { + println!("Started"); + + let cfg = config::Config::from_disk()?; + + let dial = DialDevice::new(std::time::Duration::from_millis(750))?; + println!("Found the dial"); + + std::thread::spawn(move || { let signals = Signals::new(&[SIGTERM, SIGINT]).unwrap(); for sig in signals.forever() { eprintln!("received signal {:?}", sig); - active_notification.close(); - std::process::exit(1); + kill_notif_tx + .send(Some(("Terminated!".into(), "dialog-warning"))) + .unwrap(); } });