From e16f51fcf691f18df96c70db45401b179cd12797 Mon Sep 17 00:00:00 2001 From: jD91mZM2 Date: Mon, 25 May 2020 18:08:30 +0200 Subject: [PATCH] Make --not-when-audio handle audio silently stopping Fixes https://github.com/jD91mZM2/xidlehook/issues/43 --- xidlehook-core/src/lib.rs | 41 ++++++++++++++++------------- xidlehook-core/src/modules/mod.rs | 9 ++++++- xidlehook-core/src/modules/pulse.rs | 2 +- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/xidlehook-core/src/lib.rs b/xidlehook-core/src/lib.rs index c7ffa67..428bb20 100644 --- a/xidlehook-core/src/lib.rs +++ b/xidlehook-core/src/lib.rs @@ -14,7 +14,6 @@ clippy::get_unwrap, clippy::integer_arithmetic, clippy::integer_division, - clippy::pedantic, clippy::print_stdout, )] #![allow( @@ -208,6 +207,24 @@ where absolute_time: Duration, force: bool, ) -> Result { + macro_rules! handle { + ($progress:expr) => { + match $progress { + Progress::Continue => (), + Progress::Abort => { + trace!("Module requested abort of chain."); + self.abort()?; + return Ok(Progress::Abort); + }, + Progress::Reset => { + trace!("Module requested reset of chain."); + self.reset()?; + return Ok(Progress::Reset); + }, + Progress::Stop => return Ok(Progress::Stop), + } + }; + } trace!("Activating timer {}", index); let timer_info = TimerInfo { @@ -219,15 +236,7 @@ where match self.module.pre_timer(timer_info) { Ok(_) if force => (), - - Ok(Progress::Continue) => (), - Ok(Progress::Abort) => { - trace!("Module requested abort of chain."); - self.abort()?; - return Ok(Progress::Abort); - }, - Ok(Progress::Stop) => return Ok(Progress::Stop), - + Ok(progress) => handle!(progress), Err(err) => { self.module.warning(&err)?; }, @@ -241,14 +250,7 @@ where self.base_idle_time = absolute_time; match self.module.post_timer(timer_info) { - Ok(Progress::Continue) => (), - Ok(Progress::Abort) => { - trace!("Module requested abort of chain."); - self.abort()?; - return Ok(Progress::Abort); - }, - Ok(Progress::Stop) => return Ok(Progress::Stop), - + Ok(progress) => handle!(progress), Err(err) => { self.module.warning(&err)?; }, @@ -290,7 +292,7 @@ where ); if self.aborted { - // This chain was aborted, so don't pursue it + trace!("This chain was aborted, I won't pursue it"); return Ok(Some(max_sleep)); } @@ -319,6 +321,7 @@ where match self.trigger(self.next_index, absolute_time, false)? { Progress::Continue => (), Progress::Abort => return Ok(Some(max_sleep)), + Progress::Reset => return Ok(Some(max_sleep)), Progress::Stop => return Ok(None), } // From now on, `relative_time` is invalid. Don't use it. diff --git a/xidlehook-core/src/modules/mod.rs b/xidlehook-core/src/modules/mod.rs index bd94fb2..c04fb3d 100644 --- a/xidlehook-core/src/modules/mod.rs +++ b/xidlehook-core/src/modules/mod.rs @@ -11,8 +11,15 @@ use log::warn; pub enum Progress { /// Continue the program, no action taken. Continue, - /// Abort this chain, don't pursue it any longer. + /// Abort this chain, don't pursue it any longer. The timers won't be + /// checked again, until the user is active. Abort, + /// Like abort, but is immediately ready to check timers again. See + /// https://github.com/jD91mZM2/xidlehook/issues/43 for a quick description + /// of why this exists: When an application goes out of fullscreen, you + /// don't want to re-check it all the time. But an application can stop + /// playing audio without user interaction. + Reset, /// Stop the program completely. Use this sparingly. Stop, } diff --git a/xidlehook-core/src/modules/pulse.rs b/xidlehook-core/src/modules/pulse.rs index 6c7461b..bafb0fc 100644 --- a/xidlehook-core/src/modules/pulse.rs +++ b/xidlehook-core/src/modules/pulse.rs @@ -174,7 +174,7 @@ impl Module for NotWhenAudio { if players == 0 { Ok(Progress::Continue) } else { - Ok(Progress::Abort) + Ok(Progress::Reset) } } }