fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 1 | //! The TOML config. Parsing and validation only, the compositor applies it. |
57f44a63d99f | atagen | 2026-10-16 05:34 UTC | 2 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 3 | use monolith_ipc::Command; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 4 | use regex::Regex; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 5 | use serde::Deserialize; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 6 | use std::collections::BTreeMap; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 7 | use std::fmt; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 8 | use std::path::{Path, PathBuf}; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 9 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 10 | pub const DEFAULT: &str = include_str!("../default.toml"); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 11 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 12 | #[derive(Debug, Clone)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 13 | pub struct Config { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 14 | pub general: General, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 15 | pub output: Outputs, |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 16 | pub decoration: Decoration, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 17 | pub animation: Animation, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 18 | pub keyboard: Keyboard, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 19 | pub binds: Vec<Bind>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 20 | pub rules: Vec<Rule>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 21 | pub startup: Vec<Vec<String>>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 22 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 23 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 24 | #[derive(Deserialize, Debug, Clone, PartialEq)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 25 | #[serde(default, deny_unknown_fields)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 26 | pub struct General { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 27 | /// What `Mod` means in binds. |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 28 | #[serde(rename = "mod")] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 29 | pub mod_key: Modifier, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 30 | pub gaps: Gaps, |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 31 | /// What a window move does when it meets a neighbour. |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 32 | #[serde(rename = "move")] |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 33 | pub move_mode: MoveMode, |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 34 | } |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 35 | |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 36 | #[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)] |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 37 | #[serde(rename_all = "lowercase")] |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 38 | pub enum MoveMode { |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 39 | /// Trade places with it. |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 40 | #[default] |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 41 | Swap, |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 42 | /// Join its lane, stacking with it if it is alone. |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 43 | Merge, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 44 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 45 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 46 | impl Default for General { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 47 | fn default() -> Self { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 48 | Self { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 49 | mod_key: Modifier::Super, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 50 | gaps: Gaps::default(), |
4444c779eb48 | atagen | 2026-10-16 12:07 UTC | 51 | move_mode: MoveMode::Swap, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 52 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 53 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 54 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 55 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 56 | #[derive(Deserialize, Debug, Clone, Copy, PartialEq)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 57 | #[serde(default, deny_unknown_fields)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 58 | pub struct Gaps { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 59 | pub inner: f64, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 60 | pub outer: f64, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 61 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 62 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 63 | impl Default for Gaps { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 64 | fn default() -> Self { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 65 | Self { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 66 | inner: 8.0, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 67 | outer: 8.0, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 68 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 69 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 70 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 71 | |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 72 | #[derive(Deserialize, Debug, Clone, PartialEq, Default)] |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 73 | #[serde(default, deny_unknown_fields)] |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 74 | pub struct Outputs { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 75 | pub vrr: Vrr, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 76 | } |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 77 | |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 78 | #[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)] |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 79 | #[serde(rename_all = "lowercase")] |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 80 | pub enum Vrr { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 81 | Off, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 82 | On, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 83 | /// Only while a fullscreen window is shown, where a game sets the pace. |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 84 | #[default] |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 85 | Fullscreen, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 86 | } |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 87 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 88 | #[derive(Deserialize, Debug, Clone, PartialEq)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 89 | #[serde(default, deny_unknown_fields)] |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 90 | pub struct Decoration { |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 91 | /// Border width in logical pixels, drawn in the gap around each window. |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 92 | pub border: u32, |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 93 | pub focused: Color, |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 94 | pub unfocused: Color, |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 95 | } |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 96 | |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 97 | impl Default for Decoration { |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 98 | fn default() -> Self { |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 99 | Self { |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 100 | border: 2, |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 101 | focused: Color([0.53, 0.75, 0.82, 1.0]), |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 102 | unfocused: Color([0.23, 0.26, 0.32, 1.0]), |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 103 | } |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 104 | } |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 105 | } |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 106 | |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 107 | /// Straight RGBA from `#rrggbb` or `#rrggbbaa`. |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 108 | #[derive(Debug, Clone, Copy, PartialEq)] |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 109 | pub struct Color(pub [f32; 4]); |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 110 | |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 111 | impl<'de> Deserialize<'de> for Color { |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 112 | fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> { |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 113 | let s = String::deserialize(d)?; |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 114 | let hex = s.strip_prefix('#').unwrap_or(&s); |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 115 | let bad = |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 116 | || serde::de::Error::custom(format!("bad colour {s:?}, use #rrggbb or #rrggbbaa")); |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 117 | if !matches!(hex.len(), 6 | 8) || !hex.is_ascii() { |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 118 | return Err(bad()); |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 119 | } |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 120 | let mut c = [1.0f32; 4]; |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 121 | for (i, v) in c.iter_mut().enumerate().take(hex.len() / 2) { |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 122 | let byte = u8::from_str_radix(&hex[i * 2..i * 2 + 2], 16).map_err(|_| bad())?; |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 123 | *v = f32::from(byte) / 255.0; |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 124 | } |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 125 | Ok(Self(c)) |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 126 | } |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 127 | } |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 128 | |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 129 | #[derive(Deserialize, Debug, Clone, PartialEq)] |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 130 | #[serde(default, deny_unknown_fields)] |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 131 | pub struct Animation { |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 132 | pub enabled: bool, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 133 | pub style: AnimationStyle, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 134 | /// Every phase of a planned transition lasts this long. |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 135 | pub phase_ms: u64, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 136 | /// A cubic bezier, as in CSS. |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 137 | pub curve: [f64; 4], |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 138 | } |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 139 | |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 140 | impl Default for Animation { |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 141 | fn default() -> Self { |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 142 | Self { |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 143 | enabled: true, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 144 | style: AnimationStyle::Orthogonal, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 145 | phase_ms: 160, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 146 | curve: [0.45, 0.0, 0.2, 1.0], |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 147 | } |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 148 | } |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 149 | } |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 150 | |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 151 | #[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)] |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 152 | #[serde(rename_all = "lowercase")] |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 153 | pub enum AnimationStyle { |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 154 | Orthogonal, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 155 | Direct, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 156 | } |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 157 | |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 158 | #[derive(Deserialize, Debug, Clone, PartialEq)] |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 159 | #[serde(default, deny_unknown_fields)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 160 | pub struct Keyboard { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 161 | pub rules: String, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 162 | pub model: String, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 163 | pub layout: String, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 164 | pub variant: String, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 165 | pub options: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 166 | pub repeat_delay: i32, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 167 | pub repeat_rate: i32, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 168 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 169 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 170 | impl Default for Keyboard { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 171 | fn default() -> Self { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 172 | Self { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 173 | rules: String::new(), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 174 | model: String::new(), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 175 | layout: String::new(), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 176 | variant: String::new(), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 177 | options: None, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 178 | repeat_delay: 200, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 179 | repeat_rate: 25, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 180 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 181 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 182 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 183 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 184 | #[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 185 | #[serde(rename_all = "lowercase")] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 186 | pub enum Modifier { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 187 | Super, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 188 | Alt, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 189 | Ctrl, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 190 | Shift, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 191 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 192 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 193 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 194 | pub struct Mods { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 195 | pub logo: bool, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 196 | pub alt: bool, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 197 | pub ctrl: bool, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 198 | pub shift: bool, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 199 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 200 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 201 | impl Mods { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 202 | fn set(&mut self, m: Modifier) { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 203 | match m { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 204 | Modifier::Super => self.logo = true, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 205 | Modifier::Alt => self.alt = true, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 206 | Modifier::Ctrl => self.ctrl = true, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 207 | Modifier::Shift => self.shift = true, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 208 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 209 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 210 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 211 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 212 | #[derive(Debug, Clone, PartialEq)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 213 | pub struct Bind { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 214 | pub mods: Mods, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 215 | /// An xkb keysym name, resolved by the compositor. |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 216 | pub key: String, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 217 | pub command: Command, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 218 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 219 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 220 | #[derive(Debug, Clone)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 221 | pub struct Rule { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 222 | pub app_id: Option<Regex>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 223 | pub title: Option<Regex>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 224 | pub tag: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 225 | pub output: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 226 | pub floating: Option<bool>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 227 | pub sticky: Option<bool>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 228 | pub scratchpad: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 229 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 230 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 231 | /// Everything the matching rules say about a window, later rules winning. |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 232 | #[derive(Debug, Clone, Default, PartialEq)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 233 | pub struct Placement { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 234 | pub tag: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 235 | pub output: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 236 | pub floating: Option<bool>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 237 | pub sticky: Option<bool>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 238 | pub scratchpad: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 239 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 240 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 241 | #[derive(Debug)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 242 | pub enum Error { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 243 | Io(PathBuf, std::io::Error), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 244 | Toml(toml::de::Error), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 245 | Bind(String, String), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 246 | Regex(String, regex::Error), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 247 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 248 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 249 | impl fmt::Display for Error { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 250 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 251 | match self { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 252 | Error::Io(p, e) => write!(f, "{}: {e}", p.display()), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 253 | Error::Toml(e) => write!(f, "{e}"), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 254 | Error::Bind(k, e) => write!(f, "bind {k:?}: {e}"), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 255 | Error::Regex(r, e) => write!(f, "rule pattern {r:?}: {e}"), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 256 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 257 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 258 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 259 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 260 | impl std::error::Error for Error {} |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 261 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 262 | #[derive(Deserialize)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 263 | #[serde(deny_unknown_fields)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 264 | struct Raw { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 265 | #[serde(default)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 266 | startup: Vec<Vec<String>>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 267 | #[serde(default)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 268 | general: General, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 269 | #[serde(default)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 270 | keyboard: Keyboard, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 271 | #[serde(default)] |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 272 | animation: Animation, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 273 | #[serde(default)] |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 274 | decoration: Decoration, |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 275 | #[serde(default)] |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 276 | output: Outputs, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 277 | #[serde(default)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 278 | binds: BTreeMap<String, Words>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 279 | #[serde(default)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 280 | rules: Vec<RawRule>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 281 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 282 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 283 | #[derive(Deserialize)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 284 | #[serde(untagged)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 285 | enum Words { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 286 | Line(String), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 287 | List(Vec<String>), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 288 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 289 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 290 | #[derive(Deserialize)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 291 | #[serde(deny_unknown_fields)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 292 | struct RawRule { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 293 | app_id: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 294 | title: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 295 | tag: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 296 | output: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 297 | floating: Option<bool>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 298 | sticky: Option<bool>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 299 | scratchpad: Option<String>, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 300 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 301 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 302 | impl Config { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 303 | /// `$MONOLITH_CONFIG`, else `$XDG_CONFIG_HOME/monolith/config.toml`. |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 304 | pub fn path() -> Option<PathBuf> { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 305 | if let Some(p) = std::env::var_os("MONOLITH_CONFIG") { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 306 | return Some(p.into()); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 307 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 308 | let base = std::env::var_os("XDG_CONFIG_HOME") |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 309 | .map(PathBuf::from) |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 310 | .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".config")))?; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 311 | Some(base.join("monolith/config.toml")) |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 312 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 313 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 314 | /// Loads `path`, or the built-in default when it does not exist. |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 315 | pub fn load(path: Option<&Path>) -> Result<Self, Error> { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 316 | match path { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 317 | Some(p) => match std::fs::read_to_string(p) { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 318 | Ok(text) => Self::parse(&text), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 319 | Err(e) if e.kind() == std::io::ErrorKind::NotFound => Self::parse(DEFAULT), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 320 | Err(e) => Err(Error::Io(p.to_owned(), e)), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 321 | }, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 322 | None => Self::parse(DEFAULT), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 323 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 324 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 325 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 326 | pub fn parse(text: &str) -> Result<Self, Error> { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 327 | let raw: Raw = toml::from_str(text).map_err(Error::Toml)?; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 328 | let mod_key = raw.general.mod_key; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 329 | let binds = raw |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 330 | .binds |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 331 | .into_iter() |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 332 | .map(|(combo, words)| parse_bind(&combo, words, mod_key)) |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 333 | .collect::<Result<_, _>>()?; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 334 | let regex = |p: Option<String>| { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 335 | p.map(|p| Regex::new(&p).map_err(|e| Error::Regex(p, e))) |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 336 | .transpose() |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 337 | }; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 338 | let rules = raw |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 339 | .rules |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 340 | .into_iter() |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 341 | .map(|r| { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 342 | Ok(Rule { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 343 | app_id: regex(r.app_id)?, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 344 | title: regex(r.title)?, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 345 | tag: r.tag, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 346 | output: r.output, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 347 | floating: r.floating, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 348 | sticky: r.sticky, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 349 | scratchpad: r.scratchpad, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 350 | }) |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 351 | }) |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 352 | .collect::<Result<_, Error>>()?; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 353 | Ok(Self { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 354 | general: raw.general, |
2af1d8c55876 | atagen | 2026-10-16 11:05 UTC | 355 | animation: raw.animation, |
7ce6ae4e5a85 | atagen | 2026-10-16 12:16 UTC | 356 | decoration: raw.decoration, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 357 | output: raw.output, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 358 | keyboard: raw.keyboard, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 359 | binds, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 360 | rules, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 361 | startup: raw.startup, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 362 | }) |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 363 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 364 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 365 | pub fn placement(&self, app_id: &str, title: &str) -> Placement { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 366 | let mut p = Placement::default(); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 367 | let hit = |re: &Option<Regex>, s: &str| re.as_ref().is_none_or(|re| re.is_match(s)); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 368 | for r in &self.rules { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 369 | if !hit(&r.app_id, app_id) || !hit(&r.title, title) { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 370 | continue; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 371 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 372 | p.tag = r.tag.clone().or(p.tag); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 373 | p.output = r.output.clone().or(p.output); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 374 | p.floating = r.floating.or(p.floating); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 375 | p.sticky = r.sticky.or(p.sticky); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 376 | p.scratchpad = r.scratchpad.clone().or(p.scratchpad); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 377 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 378 | p |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 379 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 380 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 381 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 382 | fn parse_bind(combo: &str, words: Words, mod_key: Modifier) -> Result<Bind, Error> { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 383 | let bad = |m: String| Error::Bind(combo.to_owned(), m); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 384 | let parts: Vec<&str> = combo.split('+').map(str::trim).collect(); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 385 | let Some((&key, mods)) = parts.split_last().filter(|(k, _)| !k.is_empty()) else { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 386 | return Err(bad("no key".into())); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 387 | }; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 388 | let mut set = Mods::default(); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 389 | for m in mods { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 390 | let m = match m.to_ascii_lowercase().as_str() { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 391 | "mod" => mod_key, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 392 | "super" | "logo" | "win" => Modifier::Super, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 393 | "alt" => Modifier::Alt, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 394 | "ctrl" | "control" => Modifier::Ctrl, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 395 | "shift" => Modifier::Shift, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 396 | other => return Err(bad(format!("unknown modifier {other}"))), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 397 | }; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 398 | set.set(m); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 399 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 400 | let words: Vec<String> = match words { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 401 | Words::Line(s) => s.split_whitespace().map(str::to_owned).collect(), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 402 | Words::List(v) => v, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 403 | }; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 404 | let command = Command::parse(&words).map_err(|e| bad(e.0))?; |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 405 | Ok(Bind { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 406 | mods: set, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 407 | key: key.to_owned(), |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 408 | command, |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 409 | }) |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 410 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 411 | |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 412 | #[cfg(test)] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 413 | mod tests { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 414 | #[test] |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 415 | fn the_default_config_parses() { |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 416 | let c = super::Config::parse(super::DEFAULT).unwrap(); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 417 | assert!(c.binds.len() > 40); |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 418 | } |
fe835aed6c8d | atagen | 2026-10-16 10:05 UTC | 419 | } |