Skip to content

Commit

Permalink
Make --not-when-audio handle audio silently stopping
Browse files Browse the repository at this point in the history
Fixes #43
  • Loading branch information
jD91mZM2 committed May 25, 2020
1 parent 4768b68 commit e16f51f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
41 changes: 22 additions & 19 deletions xidlehook-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
clippy::get_unwrap,
clippy::integer_arithmetic,
clippy::integer_division,
clippy::pedantic,
clippy::print_stdout,
)]
#![allow(
Expand Down Expand Up @@ -208,6 +207,24 @@ where
absolute_time: Duration,
force: bool,
) -> Result<Progress> {
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 {
Expand All @@ -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)?;
},
Expand All @@ -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)?;
},
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion xidlehook-core/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion xidlehook-core/src/modules/pulse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl Module for NotWhenAudio {
if players == 0 {
Ok(Progress::Continue)
} else {
Ok(Progress::Abort)
Ok(Progress::Reset)
}
}
}

0 comments on commit e16f51f

Please sign in to comment.