1
0
Fork 0
forked from wry/wry

config: add client-rule infrastructure

This commit is contained in:
Julian Orth 2025-05-04 18:02:17 +02:00
parent 17e715cde4
commit fd2163d658
32 changed files with 1804 additions and 27 deletions

View file

@ -8,6 +8,8 @@ use {
pub mod action;
mod actions;
mod client_match;
mod client_rule;
mod color;
pub mod color_management;
pub mod config;

View file

@ -132,6 +132,7 @@ impl ActionParser<'_> {
"pin-float" => SetFloatPinned(true),
"unpin-float" => SetFloatPinned(false),
"toggle-float-pinned" => ToggleFloatPinned,
"kill-client" => KillClient,
_ => {
return Err(
ActionParserError::UnknownSimpleAction(string.to_string()).spanned(span)

View file

@ -0,0 +1,104 @@
use {
crate::{
config::{
ClientMatch, GenericMatch, MatchExactly,
context::Context,
extractor::{Extractor, ExtractorError, arr, n32, opt, str, val},
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
},
toml::{
toml_span::{DespanExt, Span, Spanned},
toml_value::Value,
},
},
indexmap::IndexMap,
thiserror::Error,
};
#[derive(Debug, Error)]
pub enum ClientMatchParserError {
#[error(transparent)]
Expected(#[from] UnexpectedDataType),
#[error(transparent)]
Extract(#[from] ExtractorError),
}
pub struct ClientMatchParser<'a>(pub &'a Context<'a>);
impl Parser for ClientMatchParser<'_> {
type Value = ClientMatch;
type Error = ClientMatchParserError;
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 ((name, not_val, all_val, any_val, exactly_val),) = ext.extract(((
opt(str("name")),
opt(val("not")),
opt(arr("all")),
opt(arr("any")),
opt(val("exactly")),
),))?;
let mut not = None;
if let Some(value) = not_val {
not = Some(Box::new(value.parse(&mut ClientMatchParser(self.0))?));
}
macro_rules! list {
($val:expr) => {{
let mut list = None;
if let Some(value) = $val {
let mut res = vec![];
for value in value.value {
res.push(value.parse(&mut ClientMatchParser(self.0))?);
}
list = Some(res);
}
list
}};
}
let all = list!(all_val);
let any = list!(any_val);
let mut exactly = None;
if let Some(value) = exactly_val {
exactly = Some(value.parse(&mut ClientMatchExactlyParser(self.0))?);
}
Ok(ClientMatch {
generic: GenericMatch {
name: name.despan_into(),
not,
all,
any,
exactly,
},
})
}
}
pub struct ClientMatchExactlyParser<'a>(pub &'a Context<'a>);
impl Parser for ClientMatchExactlyParser<'_> {
type Value = MatchExactly<ClientMatch>;
type Error = ClientMatchParserError;
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 (num, list_val) = ext.extract((n32("num"), arr("list")))?;
let mut list = vec![];
for el in list_val.value {
list.push(el.parse(&mut ClientMatchParser(self.0))?);
}
Ok(MatchExactly {
num: num.value as _,
list,
})
}
}

View file

@ -0,0 +1,104 @@
use {
crate::{
config::{
ClientMatch, ClientRule,
context::Context,
extractor::{Extractor, ExtractorError, opt, str, val},
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
parsers::{
action::{ActionParser, ActionParserError},
client_match::{ClientMatchParser, ClientMatchParserError},
},
spanned::SpannedErrorExt,
},
toml::{
toml_span::{DespanExt, Span, Spanned},
toml_value::Value,
},
},
indexmap::IndexMap,
thiserror::Error,
};
#[derive(Debug, Error)]
pub enum ClientRuleParserError {
#[error(transparent)]
Expected(#[from] UnexpectedDataType),
#[error(transparent)]
Extract(#[from] ExtractorError),
#[error(transparent)]
Match(#[from] ClientMatchParserError),
#[error(transparent)]
Action(ActionParserError),
#[error(transparent)]
Latch(ActionParserError),
}
pub struct ClientRuleParser<'a>(pub &'a Context<'a>);
impl Parser for ClientRuleParser<'_> {
type Value = ClientRule;
type Error = ClientRuleParserError;
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 (name, match_val, action_val, latch_val) = ext.extract((
opt(str("name")),
opt(val("match")),
opt(val("action")),
opt(val("latch")),
))?;
let mut action = None;
if let Some(value) = action_val {
action = Some(
value
.parse(&mut ActionParser(self.0))
.map_spanned_err(ClientRuleParserError::Action)?,
);
}
let mut latch = None;
if let Some(value) = latch_val {
latch = Some(
value
.parse(&mut ActionParser(self.0))
.map_spanned_err(ClientRuleParserError::Latch)?,
);
}
let match_ = match match_val {
None => ClientMatch::default(),
Some(m) => m.parse_map(&mut ClientMatchParser(self.0))?,
};
Ok(ClientRule {
name: name.despan_into(),
match_,
action,
latch,
})
}
}
pub struct ClientRulesParser<'a>(pub &'a Context<'a>);
impl Parser for ClientRulesParser<'_> {
type Value = Vec<ClientRule>;
type Error = ClientRuleParserError;
const EXPECTED: &'static [DataType] = &[DataType::Array];
fn parse_array(&mut self, _span: Span, array: &[Spanned<Value>]) -> ParseResult<Self> {
let mut res = vec![];
for el in array {
match el.parse(&mut ClientRuleParser(self.0)) {
Ok(o) => res.push(o),
Err(e) => {
log::warn!("Could not parse client rule: {}", self.0.error(e));
}
}
}
Ok(res)
}
}

View file

@ -8,6 +8,7 @@ use {
parsers::{
action::ActionParser,
actions::ActionsParser,
client_rule::ClientRulesParser,
color_management::ColorManagementParser,
connector::ConnectorsParser,
drm_device::DrmDevicesParser,
@ -120,7 +121,7 @@ impl Parser for ConfigParser<'_> {
ui_drag_val,
xwayland_val,
),
(color_management_val, float_val, actions_val, max_action_depth_val),
(color_management_val, float_val, actions_val, max_action_depth_val, client_rules_val),
) = ext.extract((
(
opt(val("keymap")),
@ -163,6 +164,7 @@ impl Parser for ConfigParser<'_> {
opt(val("float")),
opt(val("actions")),
recover(opt(int("max-action-depth"))),
opt(val("clients")),
),
))?;
let mut keymap = None;
@ -419,6 +421,13 @@ impl Parser for ConfigParser<'_> {
}
max_action_depth = value.value as _;
}
let mut client_rules = vec![];
if let Some(value) = client_rules_val {
match value.parse(&mut ClientRulesParser(self.0)) {
Ok(v) => client_rules = v,
Err(e) => log::warn!("Could not parse the client rules: {}", self.0.error(e)),
}
}
Ok(Config {
keymap,
repeat_rate,
@ -453,6 +462,7 @@ impl Parser for ConfigParser<'_> {
float,
named_actions,
max_action_depth,
client_rules,
})
}
}

View file

@ -28,7 +28,7 @@ pub struct OutputMatchParser<'a>(pub &'a Context<'a>);
impl Parser for OutputMatchParser<'_> {
type Value = OutputMatch;
type Error = OutputMatchParserError;
const EXPECTED: &'static [DataType] = &[DataType::Table, DataType::Table];
const EXPECTED: &'static [DataType] = &[DataType::Table, DataType::Array];
fn parse_array(&mut self, _span: Span, array: &[Spanned<Value>]) -> ParseResult<Self> {
let mut res = vec![];