1
0
Fork 0
forked from wry/wry

config: add window-rule infrastructure

This commit is contained in:
Julian Orth 2025-05-01 17:49:21 +02:00
parent a6257910bb
commit 59f8acdfde
26 changed files with 1829 additions and 38 deletions

View file

@ -28,6 +28,7 @@ use {
status::MessageFormat,
theme::Color,
video::{ColorSpace, Format, GfxApi, TearingMode, TransferFunction, Transform, VrrMode},
window::WindowType,
xwayland::XScalingMode,
},
std::{
@ -241,6 +242,20 @@ pub struct ClientMatch {
pub exe_regex: Option<String>,
}
#[derive(Debug, Clone)]
pub struct WindowRule {
pub name: Option<String>,
pub match_: WindowMatch,
pub action: Option<Action>,
pub latch: Option<Action>,
}
#[derive(Default, Debug, Clone)]
pub struct WindowMatch {
pub generic: GenericMatch<Self>,
pub types: Option<WindowType>,
}
#[derive(Debug, Clone)]
pub enum DrmDeviceMatch {
Any(Vec<DrmDeviceMatch>),
@ -439,6 +454,7 @@ pub struct Config {
pub named_actions: Vec<NamedAction>,
pub max_action_depth: u64,
pub client_rules: Vec<ClientRule>,
pub window_rules: Vec<WindowRule>,
}
#[derive(Debug, Error)]

View file

@ -39,6 +39,9 @@ mod tearing;
mod theme;
mod ui_drag;
mod vrr;
mod window_match;
mod window_rule;
mod window_type;
mod xwayland;
#[derive(Debug, Error)]

View file

@ -32,6 +32,7 @@ use {
theme::ThemeParser,
ui_drag::UiDragParser,
vrr::VrrParser,
window_rule::WindowRulesParser,
xwayland::XwaylandParser,
},
spanned::SpannedErrorExt,
@ -121,7 +122,14 @@ impl Parser for ConfigParser<'_> {
ui_drag_val,
xwayland_val,
),
(color_management_val, float_val, actions_val, max_action_depth_val, client_rules_val),
(
color_management_val,
float_val,
actions_val,
max_action_depth_val,
client_rules_val,
window_rules_val,
),
) = ext.extract((
(
opt(val("keymap")),
@ -165,6 +173,7 @@ impl Parser for ConfigParser<'_> {
opt(val("actions")),
recover(opt(int("max-action-depth"))),
opt(val("clients")),
opt(val("windows")),
),
))?;
let mut keymap = None;
@ -428,6 +437,13 @@ impl Parser for ConfigParser<'_> {
Err(e) => log::warn!("Could not parse the client rules: {}", self.0.error(e)),
}
}
let mut window_rules = vec![];
if let Some(value) = window_rules_val {
match value.parse(&mut WindowRulesParser(self.0)) {
Ok(v) => window_rules = v,
Err(e) => log::warn!("Could not parse the window rules: {}", self.0.error(e)),
}
}
Ok(Config {
keymap,
repeat_rate,
@ -463,6 +479,7 @@ impl Parser for ConfigParser<'_> {
named_actions,
max_action_depth,
client_rules,
window_rules,
})
}
}

View file

@ -0,0 +1,113 @@
use {
crate::{
config::{
GenericMatch, MatchExactly, WindowMatch,
context::Context,
extractor::{Extractor, ExtractorError, arr, n32, opt, str, val},
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
parsers::window_type::{WindowTypeParser, WindowTypeParserError},
},
toml::{
toml_span::{DespanExt, Span, Spanned},
toml_value::Value,
},
},
indexmap::IndexMap,
thiserror::Error,
};
#[derive(Debug, Error)]
pub enum WindowMatchParserError {
#[error(transparent)]
Expected(#[from] UnexpectedDataType),
#[error(transparent)]
Extract(#[from] ExtractorError),
#[error(transparent)]
WindowTypes(#[from] WindowTypeParserError),
}
pub struct WindowMatchParser<'a>(pub &'a Context<'a>);
impl Parser for WindowMatchParser<'_> {
type Value = WindowMatch;
type Error = WindowMatchParserError;
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, types_val),) = ext.extract(((
opt(str("name")),
opt(val("not")),
opt(arr("all")),
opt(arr("any")),
opt(val("exactly")),
opt(val("types")),
),))?;
let mut not = None;
if let Some(value) = not_val {
not = Some(Box::new(value.parse(&mut WindowMatchParser(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 WindowMatchParser(self.0))?);
}
list = Some(res);
}
list
}};
}
let all = list!(all_val);
let any = list!(any_val);
let mut types = None;
if let Some(value) = types_val {
types = Some(value.parse_map(&mut WindowTypeParser)?);
}
let mut exactly = None;
if let Some(value) = exactly_val {
exactly = Some(value.parse(&mut WindowMatchExactlyParser(self.0))?);
}
Ok(WindowMatch {
generic: GenericMatch {
name: name.despan_into(),
not,
all,
any,
exactly,
},
types,
})
}
}
pub struct WindowMatchExactlyParser<'a>(pub &'a Context<'a>);
impl Parser for WindowMatchExactlyParser<'_> {
type Value = MatchExactly<WindowMatch>;
type Error = WindowMatchParserError;
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 WindowMatchParser(self.0))?);
}
Ok(MatchExactly {
num: num.value as _,
list,
})
}
}

View file

@ -0,0 +1,104 @@
use {
crate::{
config::{
WindowMatch, WindowRule,
context::Context,
extractor::{Extractor, ExtractorError, opt, str, val},
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
parsers::{
action::{ActionParser, ActionParserError},
window_match::{WindowMatchParser, WindowMatchParserError},
},
spanned::SpannedErrorExt,
},
toml::{
toml_span::{DespanExt, Span, Spanned},
toml_value::Value,
},
},
indexmap::IndexMap,
thiserror::Error,
};
#[derive(Debug, Error)]
pub enum WindowRuleParserError {
#[error(transparent)]
Expected(#[from] UnexpectedDataType),
#[error(transparent)]
Extract(#[from] ExtractorError),
#[error(transparent)]
Match(#[from] WindowMatchParserError),
#[error(transparent)]
Action(ActionParserError),
#[error(transparent)]
Latch(ActionParserError),
}
pub struct WindowRuleParser<'a>(pub &'a Context<'a>);
impl Parser for WindowRuleParser<'_> {
type Value = WindowRule;
type Error = WindowRuleParserError;
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(WindowRuleParserError::Action)?,
);
}
let mut latch = None;
if let Some(value) = latch_val {
latch = Some(
value
.parse(&mut ActionParser(self.0))
.map_spanned_err(WindowRuleParserError::Latch)?,
);
}
let match_ = match match_val {
None => WindowMatch::default(),
Some(m) => m.parse_map(&mut WindowMatchParser(self.0))?,
};
Ok(WindowRule {
name: name.despan_into(),
match_,
action,
latch,
})
}
}
pub struct WindowRulesParser<'a>(pub &'a Context<'a>);
impl Parser for WindowRulesParser<'_> {
type Value = Vec<WindowRule>;
type Error = WindowRuleParserError;
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 WindowRuleParser(self.0)) {
Ok(o) => res.push(o),
Err(e) => {
log::warn!("Could not parse window rule: {}", self.0.error(e));
}
}
}
Ok(res)
}
}

View file

@ -0,0 +1,53 @@
use {
crate::{
config::parser::{DataType, ParseResult, Parser, UnexpectedDataType},
toml::{
toml_span::{Span, Spanned, SpannedExt},
toml_value::Value,
},
},
jay_config::{window, window::WindowType},
thiserror::Error,
};
#[derive(Debug, Error)]
pub enum WindowTypeParserError {
#[error(transparent)]
Expected(#[from] UnexpectedDataType),
#[error("Unknown window type `{}`", .0)]
UnknownWindowType(String),
}
pub struct WindowTypeParser;
impl Parser for WindowTypeParser {
type Value = WindowType;
type Error = WindowTypeParserError;
const EXPECTED: &'static [DataType] = &[DataType::Array, DataType::String];
fn parse_string(&mut self, span: Span, string: &str) -> ParseResult<Self> {
let ty = match string {
"none" => WindowType(0),
"any" => WindowType(!0),
"container" => window::CONTAINER,
"placeholder" => window::PLACEHOLDER,
"xdg-toplevel" => window::XDG_TOPLEVEL,
"x-window" => window::X_WINDOW,
"client-window" => window::CLIENT_WINDOW,
_ => {
return Err(
WindowTypeParserError::UnknownWindowType(string.to_owned()).spanned(span)
);
}
};
Ok(ty)
}
fn parse_array(&mut self, _span: Span, array: &[Spanned<Value>]) -> ParseResult<Self> {
let mut ty = WindowType(0);
for el in array {
ty |= el.parse(&mut WindowTypeParser)?;
}
Ok(ty)
}
}

View file

@ -14,7 +14,7 @@ use {
config::{
Action, ClientRule, Config, ConfigConnector, ConfigDrmDevice, ConfigKeymap,
ConnectorMatch, DrmDeviceMatch, Exec, Input, InputMatch, Output, OutputMatch, Shortcut,
SimpleCommand, Status, Theme, parse_config,
SimpleCommand, Status, Theme, WindowRule, parse_config,
},
rules::{MatcherTemp, RuleMapper},
},
@ -47,6 +47,7 @@ use {
on_new_connector, on_new_drm_device, set_direct_scanout_enabled, set_gfx_api,
set_tearing_mode, set_vrr_cursor_hz, set_vrr_mode,
},
window::Window,
xwayland::set_x_scaling_mode,
},
run_on_drop::on_drop,
@ -100,24 +101,39 @@ impl Action {
}};
}
let s = state.persistent.seat;
macro_rules! window_or_seat {
($name:ident, $expr:expr) => {{
let state = state.clone();
B::new(move || {
if let Some($name) = state.window.get() {
if let Some($name) = $name {
$expr;
}
} else {
let $name = s;
$expr;
}
})
}};
}
match self {
Action::SimpleCommand { cmd } => match cmd {
SimpleCommand::Focus(dir) => B::new(move || s.focus(dir)),
SimpleCommand::Move(dir) => B::new(move || s.move_(dir)),
SimpleCommand::Split(axis) => B::new(move || s.create_split(axis)),
SimpleCommand::ToggleSplit => B::new(move || s.toggle_split()),
SimpleCommand::SetSplit(b) => B::new(move || s.set_split(b)),
SimpleCommand::ToggleMono => B::new(move || s.toggle_mono()),
SimpleCommand::SetMono(b) => B::new(move || s.set_mono(b)),
SimpleCommand::ToggleFullscreen => B::new(move || s.toggle_fullscreen()),
SimpleCommand::SetFullscreen(b) => B::new(move || s.set_fullscreen(b)),
SimpleCommand::Move(dir) => window_or_seat!(s, s.move_(dir)),
SimpleCommand::Split(axis) => window_or_seat!(s, s.create_split(axis)),
SimpleCommand::ToggleSplit => window_or_seat!(s, s.toggle_split()),
SimpleCommand::SetSplit(b) => window_or_seat!(s, s.set_split(b)),
SimpleCommand::ToggleMono => window_or_seat!(s, s.toggle_mono()),
SimpleCommand::SetMono(b) => window_or_seat!(s, s.set_mono(b)),
SimpleCommand::ToggleFullscreen => window_or_seat!(s, s.toggle_fullscreen()),
SimpleCommand::SetFullscreen(b) => window_or_seat!(s, s.set_fullscreen(b)),
SimpleCommand::FocusParent => B::new(move || s.focus_parent()),
SimpleCommand::Close => B::new(move || s.close()),
SimpleCommand::Close => window_or_seat!(s, s.close()),
SimpleCommand::DisablePointerConstraint => {
B::new(move || s.disable_pointer_constraint())
}
SimpleCommand::ToggleFloating => B::new(move || s.toggle_floating()),
SimpleCommand::SetFloating(b) => B::new(move || s.set_floating(b)),
SimpleCommand::ToggleFloating => window_or_seat!(s, s.toggle_floating()),
SimpleCommand::SetFloating(b) => window_or_seat!(s, s.set_floating(b)),
SimpleCommand::Quit => B::new(quit),
SimpleCommand::ReloadConfigToml => {
let persistent = state.persistent.clone();
@ -133,8 +149,10 @@ impl Action {
B::new(move || set_float_above_fullscreen(bool))
}
SimpleCommand::ToggleFloatAboveFullscreen => B::new(toggle_float_above_fullscreen),
SimpleCommand::SetFloatPinned(pinned) => B::new(move || s.set_float_pinned(pinned)),
SimpleCommand::ToggleFloatPinned => B::new(move || s.toggle_float_pinned()),
SimpleCommand::SetFloatPinned(pinned) => {
window_or_seat!(s, s.set_float_pinned(pinned))
}
SimpleCommand::ToggleFloatPinned => window_or_seat!(s, s.toggle_float_pinned()),
SimpleCommand::KillClient => client_action!(c, c.kill()),
},
Action::Multi { actions } => {
@ -153,7 +171,7 @@ impl Action {
}
Action::MoveToWorkspace { name } => {
let workspace = get_workspace(&name);
B::new(move || s.set_workspace(workspace))
window_or_seat!(s, s.set_workspace(workspace))
}
Action::ConfigureConnector { con } => B::new(move || {
for c in connectors() {
@ -689,6 +707,8 @@ struct State {
action_depth: Cell<u64>,
client: Cell<Option<Client>>,
window: Cell<Option<Option<Window>>>,
}
impl Drop for State {
@ -897,13 +917,23 @@ impl State {
fn with_client(&self, client: Client, check: bool, f: impl FnOnce()) {
let mut opt = Some(client);
if check && client.does_not_exist() {
if client.0 == 0 || (check && client.does_not_exist()) {
opt = None;
}
self.client.set(opt);
f();
self.client.set(None);
}
fn with_window(&self, window: Window, check: bool, f: impl FnOnce()) {
let mut w = Some(window);
if check && !window.exists() {
w = None;
}
self.window.set(Some(w));
f();
self.window.set(None);
}
}
#[derive(Eq, PartialEq, Hash)]
@ -922,6 +952,7 @@ struct PersistentState {
actions: RefCell<AHashMap<Rc<String>, Rc<dyn Fn()>>>,
client_rules: Cell<Vec<MatcherTemp<ClientRule>>>,
client_rule_mapper: RefCell<Option<RuleMapper<ClientRule>>>,
window_rules: Cell<Vec<MatcherTemp<WindowRule>>>,
}
fn load_config(initial_load: bool, persistent: &Rc<PersistentState>) {
@ -1003,10 +1034,13 @@ fn load_config(initial_load: bool, persistent: &Rc<PersistentState>) {
action_depth_max: config.max_action_depth,
action_depth: Cell::new(0),
client: Default::default(),
window: Default::default(),
});
let (client_rules, client_rule_mapper) = state.create_rules(&config.client_rules);
persistent.client_rules.set(client_rules);
*state.persistent.client_rule_mapper.borrow_mut() = Some(client_rule_mapper);
let (window_rules, _) = state.create_rules(&config.window_rules);
persistent.window_rules.set(window_rules);
state.set_status(&config.status);
persistent.actions.borrow_mut().clear();
for a in config.named_actions {
@ -1231,6 +1265,7 @@ pub fn configure() {
actions: Default::default(),
client_rules: Default::default(),
client_rule_mapper: Default::default(),
window_rules: Default::default(),
});
{
let p = persistent.clone();

View file

@ -1,10 +1,13 @@
use {
crate::{
State,
config::{ClientMatch, ClientRule, GenericMatch},
config::{ClientMatch, ClientRule, GenericMatch, WindowMatch, WindowRule},
},
ahash::{AHashMap, AHashSet},
jay_config::client::{ClientCriterion, ClientMatcher},
jay_config::{
client::{ClientCriterion, ClientMatcher},
window::{WindowCriterion, WindowMatcher},
},
std::{mem::ManuallyDrop, rc::Rc},
};
@ -195,6 +198,131 @@ impl Rule for ClientRule {
}
}
impl Rule for WindowRule {
type Match = WindowMatch;
type Matcher = WindowMatcher;
type Criterion<'a> = WindowCriterion<'a>;
const NAME_UPPER: &str = "Window";
const NAME_LOWER: &str = "window";
fn name(&self) -> Option<&str> {
self.name.as_deref()
}
fn match_(&self) -> &Self::Match {
&self.match_
}
fn generic(m: &Self::Match) -> &GenericMatch<Self::Match> {
&m.generic
}
fn map_custom(
_state: &Rc<State>,
all: &mut Vec<MatcherTemp<Self>>,
match_: &Self::Match,
) -> Option<()> {
let m = |c: WindowCriterion<'_>| MatcherTemp(c.to_matcher());
#[expect(unused_macros)]
macro_rules! value {
($ty:ident, $field:ident) => {
if let Some(value) = &match_.$field {
all.push(m(WindowCriterion::$ty(value)));
}
};
}
#[expect(unused_macros)]
macro_rules! bool {
($ty:ident, $field:ident) => {
if let Some(value) = &match_.$field {
let crit = WindowCriterion::$ty;
let matcher = match value {
false => m(WindowCriterion::Not(&crit)),
true => m(crit),
};
all.push(matcher);
}
};
}
if let Some(value) = &match_.types {
all.push(m(WindowCriterion::Types(*value)));
}
Some(())
}
fn create(c: Self::Criterion<'_>) -> Self::Matcher {
c.to_matcher()
}
fn destroy(m: Self::Matcher) {
m.destroy();
}
fn bind(&self, state: &Rc<State>, matcher: Self::Matcher) {
let state = state.clone();
macro_rules! latch {
($g:ident, $client:ident, $win:ident) => {
let g = $g.clone();
let state = state.clone();
$win.latch(move || {
state.with_client($client, true, || {
state.with_window(*$win, true, || g());
});
});
};
}
if let Some(action) = &self.action {
let f = action.clone().into_fn(&state);
if let Some(action) = &self.latch {
let g = action.clone().into_rc_fn(&state);
matcher.bind(move |win| {
let client = win.client();
state.with_client(client, false, || {
state.with_window(*win, false, &f);
});
latch!(g, client, win);
});
} else {
matcher.bind(move |win| {
let client = win.client();
state.with_client(client, false, || {
state.with_window(*win, false, &f);
});
});
}
} else {
if let Some(action) = &self.latch {
let g = action.clone().into_rc_fn(&state);
matcher.bind(move |win| {
let client = win.client();
latch!(g, client, win);
});
}
}
}
fn gen_matcher(m: Self::Matcher) -> Self::Criterion<'static> {
WindowCriterion::Matcher(m)
}
fn gen_not<'a, 'b: 'a>(m: &'a Self::Criterion<'b>) -> Self::Criterion<'a> {
WindowCriterion::Not(m)
}
fn gen_all<'a, 'b: 'a>(m: &'a [Self::Criterion<'b>]) -> Self::Criterion<'a> {
WindowCriterion::All(m)
}
fn gen_any<'a, 'b: 'a>(m: &'a [Self::Criterion<'b>]) -> Self::Criterion<'a> {
WindowCriterion::Any(m)
}
fn gen_exactly<'a, 'b: 'a>(n: usize, m: &'a [Self::Criterion<'b>]) -> Self::Criterion<'a> {
WindowCriterion::Exactly(n, m)
}
}
pub struct RuleMapper<R>
where
R: Rule,