1
0
Fork 0
forked from wry/wry

idle: add a grace period

This commit is contained in:
Julian Orth 2025-01-26 12:29:20 +01:00
parent 1ad3d11616
commit e8be15a26c
29 changed files with 405 additions and 79 deletions

View file

@ -177,7 +177,10 @@ impl ActionParser<'_> {
.extract(val("idle"))?
.parse_map(&mut IdleParser(self.0))
.map_spanned_err(ActionParserError::ConfigureIdle)?;
Ok(Action::ConfigureIdle { idle })
Ok(Action::ConfigureIdle {
idle: idle.timeout,
grace_period: idle.grace_period,
})
}
fn parse_configure_output(&mut self, ext: &mut Extractor<'_>) -> ParseResult<Self> {

View file

@ -294,9 +294,13 @@ impl Parser for ConfigParser<'_> {
}
}
let mut idle = None;
let mut grace_period = None;
if let Some(value) = idle_val {
match value.parse(&mut IdleParser(self.0)) {
Ok(v) => idle = Some(v),
Ok(v) => {
idle = v.timeout;
grace_period = v.grace_period;
}
Err(e) => {
log::warn!("Could not parse the idle timeout: {}", self.0.error(e));
}
@ -384,6 +388,7 @@ impl Parser for ConfigParser<'_> {
render_device,
inputs,
idle,
grace_period,
focus_follows_mouse: focus_follows_mouse.despan().unwrap_or(true),
window_management_key,
vrr,

View file

@ -2,7 +2,7 @@ use {
crate::{
config::{
context::Context,
extractor::{n64, opt, Extractor, ExtractorError},
extractor::{n64, opt, val, Extractor, ExtractorError},
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
},
toml::{
@ -25,7 +25,45 @@ pub enum IdleParserError {
pub struct IdleParser<'a>(pub &'a Context<'a>);
pub struct Idle {
pub timeout: Option<Duration>,
pub grace_period: Option<Duration>,
}
impl Parser for IdleParser<'_> {
type Value = Idle;
type Error = IdleParserError;
const EXPECTED: &'static [DataType] = &[DataType::Table];
fn parse_table(
&mut self,
span: Span,
table: &IndexMap<Spanned<String>, Spanned<Value>>,
) -> ParseResult<Self> {
let mut ext = Extractor::new(self.0, span, table);
let (minutes, seconds, grace_period_val) = ext.extract((
opt(n64("minutes")),
opt(n64("seconds")),
opt(val("grace-period")),
))?;
let mut timeout = None;
if minutes.is_some() || seconds.is_some() {
timeout = Some(parse_duration(&minutes, &seconds));
}
let mut grace_period = None;
if let Some(gp) = grace_period_val {
grace_period = Some(gp.parse(&mut GracePeriodParser(self.0))?);
}
Ok(Idle {
timeout,
grace_period,
})
}
}
struct GracePeriodParser<'a>(pub &'a Context<'a>);
impl Parser for GracePeriodParser<'_> {
type Value = Duration;
type Error = IdleParserError;
const EXPECTED: &'static [DataType] = &[DataType::Table];
@ -37,9 +75,13 @@ impl Parser for IdleParser<'_> {
) -> ParseResult<Self> {
let mut ext = Extractor::new(self.0, span, table);
let (minutes, seconds) = ext.extract((opt(n64("minutes")), opt(n64("seconds"))))?;
let idle = Duration::from_secs(
minutes.despan().unwrap_or_default() * 60 + seconds.despan().unwrap_or_default(),
);
Ok(idle)
let grace_period = parse_duration(&minutes, &seconds);
Ok(grace_period)
}
}
fn parse_duration(minutes: &Option<Spanned<u64>>, seconds: &Option<Spanned<u64>>) -> Duration {
Duration::from_secs(
minutes.despan().unwrap_or_default() * 60 + seconds.despan().unwrap_or_default(),
)
}