tony / oxwm

Branch: dd010d0
Switch branches/tags
master
tonybtw Added layout names into bar, and in config files to be configurable. Updated test config and default config to include ExchangeClient, and SmartMoveWin 7858063 2025-10-27
368 lines · 10.6 KB Raw Blame History
1 use crate::bar::{BlockCommand, BlockConfig};
2 use crate::errors::ConfigError;
3 use crate::keyboard::handlers::Key;
4 use crate::keyboard::keycodes;
5 use crate::keyboard::{Arg, KeyAction};
6 use serde::Deserialize;
7 use x11rb::protocol::xproto::{KeyButMask, Keycode};
8
9 #[derive(Debug, Deserialize)]
10 pub enum ModKey {
11 Mod1,
12 Mod2,
13 Mod3,
14 Mod4,
15 Mod5,
16 Shift,
17 Control,
18 }
19
20 impl ModKey {
21 fn to_keybut_mask(&self) -> KeyButMask {
22 match self {
23 ModKey::Mod1 => KeyButMask::MOD1,
24 ModKey::Mod2 => KeyButMask::MOD2,
25 ModKey::Mod3 => KeyButMask::MOD3,
26 ModKey::Mod4 => KeyButMask::MOD4,
27 ModKey::Mod5 => KeyButMask::MOD5,
28 ModKey::Shift => KeyButMask::SHIFT,
29 ModKey::Control => KeyButMask::CONTROL,
30 }
31 }
32 }
33
34 #[rustfmt::skip]
35 #[derive(Debug, Deserialize)]
36 pub enum KeyData {
37 Return,
38 Q,
39 Escape,
40 Space,
41 Tab,
42 Backspace,
43 Delete,
44 F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
45 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, R, S, T, U, V, W, X, Y, Z,
46 Key0,
47 Key1,
48 Key2,
49 Key3,
50 Key4,
51 Key5,
52 Key6,
53 Key7,
54 Key8,
55 Key9,
56 Left,
57 Right,
58 Up,
59 Down,
60 Home,
61 End,
62 PageUp,
63 PageDown,
64 Insert,
65 Minus,
66 Equal,
67 BracketLeft,
68 BracketRight,
69 Semicolon,
70 Apostrophe,
71 Grave,
72 Backslash,
73 Comma,
74 Period,
75 Slash,
76 }
77
78 impl KeyData {
79 fn to_keycode(&self) -> Keycode {
80 match self {
81 KeyData::Return => keycodes::RETURN,
82 KeyData::Q => keycodes::Q,
83 KeyData::Escape => keycodes::ESCAPE,
84 KeyData::Space => keycodes::SPACE,
85 KeyData::Tab => keycodes::TAB,
86 KeyData::Backspace => keycodes::BACKSPACE,
87 KeyData::Delete => keycodes::DELETE,
88 KeyData::F1 => keycodes::F1,
89 KeyData::F2 => keycodes::F2,
90 KeyData::F3 => keycodes::F3,
91 KeyData::F4 => keycodes::F4,
92 KeyData::F5 => keycodes::F5,
93 KeyData::F6 => keycodes::F6,
94 KeyData::F7 => keycodes::F7,
95 KeyData::F8 => keycodes::F8,
96 KeyData::F9 => keycodes::F9,
97 KeyData::F10 => keycodes::F10,
98 KeyData::F11 => keycodes::F11,
99 KeyData::F12 => keycodes::F12,
100 KeyData::A => keycodes::A,
101 KeyData::B => keycodes::B,
102 KeyData::C => keycodes::C,
103 KeyData::D => keycodes::D,
104 KeyData::E => keycodes::E,
105 KeyData::F => keycodes::F,
106 KeyData::G => keycodes::G,
107 KeyData::H => keycodes::H,
108 KeyData::I => keycodes::I,
109 KeyData::J => keycodes::J,
110 KeyData::K => keycodes::K,
111 KeyData::L => keycodes::L,
112 KeyData::M => keycodes::M,
113 KeyData::N => keycodes::N,
114 KeyData::O => keycodes::O,
115 KeyData::P => keycodes::P,
116 KeyData::R => keycodes::R,
117 KeyData::S => keycodes::S,
118 KeyData::T => keycodes::T,
119 KeyData::U => keycodes::U,
120 KeyData::V => keycodes::V,
121 KeyData::W => keycodes::W,
122 KeyData::X => keycodes::X,
123 KeyData::Y => keycodes::Y,
124 KeyData::Z => keycodes::Z,
125 KeyData::Key0 => keycodes::KEY_0,
126 KeyData::Key1 => keycodes::KEY_1,
127 KeyData::Key2 => keycodes::KEY_2,
128 KeyData::Key3 => keycodes::KEY_3,
129 KeyData::Key4 => keycodes::KEY_4,
130 KeyData::Key5 => keycodes::KEY_5,
131 KeyData::Key6 => keycodes::KEY_6,
132 KeyData::Key7 => keycodes::KEY_7,
133 KeyData::Key8 => keycodes::KEY_8,
134 KeyData::Key9 => keycodes::KEY_9,
135 KeyData::Left => keycodes::LEFT,
136 KeyData::Right => keycodes::RIGHT,
137 KeyData::Up => keycodes::UP,
138 KeyData::Down => keycodes::DOWN,
139 KeyData::Home => keycodes::HOME,
140 KeyData::End => keycodes::END,
141 KeyData::PageUp => keycodes::PAGE_UP,
142 KeyData::PageDown => keycodes::PAGE_DOWN,
143 KeyData::Insert => keycodes::INSERT,
144 KeyData::Minus => keycodes::MINUS,
145 KeyData::Equal => keycodes::EQUAL,
146 KeyData::BracketLeft => keycodes::LEFT_BRACKET,
147 KeyData::BracketRight => keycodes::RIGHT_BRACKET,
148 KeyData::Semicolon => keycodes::SEMICOLON,
149 KeyData::Apostrophe => keycodes::APOSTROPHE,
150 KeyData::Grave => keycodes::GRAVE,
151 KeyData::Backslash => keycodes::BACKSLASH,
152 KeyData::Comma => keycodes::COMMA,
153 KeyData::Period => keycodes::PERIOD,
154 KeyData::Slash => keycodes::SLASH,
155 }
156 }
157 }
158
159 pub fn parse_config(input: &str) -> Result<crate::Config, ConfigError> {
160 let config_data: ConfigData = ron::from_str(input)?;
161 config_data_to_config(config_data)
162 }
163
164 #[derive(Debug, Deserialize)]
165 struct LayoutSymbolOverrideData {
166 name: String,
167 symbol: String,
168 }
169
170 #[derive(Debug, Deserialize)]
171 struct ConfigData {
172 border_width: u32,
173 border_focused: u32,
174 border_unfocused: u32,
175 font: String,
176
177 gaps_enabled: bool,
178 gap_inner_horizontal: u32,
179 gap_inner_vertical: u32,
180 gap_outer_horizontal: u32,
181 gap_outer_vertical: u32,
182
183 terminal: String,
184 modkey: ModKey,
185
186 tags: Vec<String>,
187 #[serde(default)]
188 layout_symbols: Vec<LayoutSymbolOverrideData>,
189 keybindings: Vec<KeybindingData>,
190 status_blocks: Vec<StatusBlockData>,
191
192 scheme_normal: ColorSchemeData,
193 scheme_occupied: ColorSchemeData,
194 scheme_selected: ColorSchemeData,
195 }
196
197 #[derive(Debug, Deserialize)]
198 struct KeybindingData {
199 modifiers: Vec<ModKey>,
200 key: KeyData,
201 action: KeyAction,
202 #[serde(default)]
203 arg: ArgData,
204 }
205
206 #[derive(Debug, Deserialize)]
207 #[serde(untagged)]
208 enum ArgData {
209 None,
210 String(String),
211 Int(i32),
212 Array(Vec<String>),
213 }
214
215 impl Default for ArgData {
216 fn default() -> Self {
217 ArgData::None
218 }
219 }
220
221 #[derive(Debug, Deserialize)]
222 struct StatusBlockData {
223 format: String,
224 command: String,
225 #[serde(default)]
226 command_arg: Option<String>,
227 #[serde(default)]
228 battery_formats: Option<BatteryFormats>,
229 interval_secs: u64,
230 color: u32,
231 underline: bool,
232 }
233
234 #[derive(Debug, Deserialize)]
235 struct BatteryFormats {
236 charging: String,
237 discharging: String,
238 full: String,
239 }
240
241 #[derive(Debug, Deserialize)]
242 struct ColorSchemeData {
243 foreground: u32,
244 background: u32,
245 underline: u32,
246 }
247
248 fn config_data_to_config(data: ConfigData) -> Result<crate::Config, ConfigError> {
249 let modkey = data.modkey.to_keybut_mask();
250
251 let mut keybindings = Vec::new();
252 for kb_data in data.keybindings {
253 let modifiers = kb_data
254 .modifiers
255 .iter()
256 .map(|m| m.to_keybut_mask())
257 .collect();
258
259 let key = kb_data.key.to_keycode();
260 let action = kb_data.action;
261 let arg = arg_data_to_arg(kb_data.arg)?;
262
263 keybindings.push(Key::new(modifiers, key, action, arg));
264 }
265
266 let mut status_blocks = Vec::new();
267 for block_data in data.status_blocks {
268 let command = match block_data.command.as_str() {
269 "DateTime" => {
270 let fmt = block_data
271 .command_arg
272 .ok_or_else(|| ConfigError::MissingCommandArg {
273 command: "DateTime".to_string(),
274 field: "command_arg".to_string(),
275 })?;
276 BlockCommand::DateTime(fmt)
277 }
278 "Shell" => {
279 let cmd = block_data
280 .command_arg
281 .ok_or_else(|| ConfigError::MissingCommandArg {
282 command: "Shell".to_string(),
283 field: "command_arg".to_string(),
284 })?;
285 BlockCommand::Shell(cmd)
286 }
287 "Ram" => BlockCommand::Ram,
288 "Static" => {
289 let text = block_data.command_arg.unwrap_or_default();
290 BlockCommand::Static(text)
291 }
292 "Battery" => {
293 let formats =
294 block_data
295 .battery_formats
296 .ok_or_else(|| ConfigError::MissingCommandArg {
297 command: "Battery".to_string(),
298 field: "battery_formats".to_string(),
299 })?;
300 BlockCommand::Battery {
301 format_charging: formats.charging,
302 format_discharging: formats.discharging,
303 format_full: formats.full,
304 }
305 }
306 _ => return Err(ConfigError::UnknownBlockCommand(block_data.command)),
307 };
308
309 status_blocks.push(BlockConfig {
310 format: block_data.format,
311 command,
312 interval_secs: block_data.interval_secs,
313 color: block_data.color,
314 underline: block_data.underline,
315 });
316 }
317
318 let layout_symbols = data
319 .layout_symbols
320 .into_iter()
321 .map(|l| crate::LayoutSymbolOverride {
322 name: l.name,
323 symbol: l.symbol,
324 })
325 .collect();
326
327 Ok(crate::Config {
328 border_width: data.border_width,
329 border_focused: data.border_focused,
330 border_unfocused: data.border_unfocused,
331 font: data.font,
332 gaps_enabled: data.gaps_enabled,
333 gap_inner_horizontal: data.gap_inner_horizontal,
334 gap_inner_vertical: data.gap_inner_vertical,
335 gap_outer_horizontal: data.gap_outer_horizontal,
336 gap_outer_vertical: data.gap_outer_vertical,
337 terminal: data.terminal,
338 modkey,
339 tags: data.tags,
340 layout_symbols,
341 keybindings,
342 status_blocks,
343 scheme_normal: crate::ColorScheme {
344 foreground: data.scheme_normal.foreground,
345 background: data.scheme_normal.background,
346 underline: data.scheme_normal.underline,
347 },
348 scheme_occupied: crate::ColorScheme {
349 foreground: data.scheme_occupied.foreground,
350 background: data.scheme_occupied.background,
351 underline: data.scheme_occupied.underline,
352 },
353 scheme_selected: crate::ColorScheme {
354 foreground: data.scheme_selected.foreground,
355 background: data.scheme_selected.background,
356 underline: data.scheme_selected.underline,
357 },
358 })
359 }
360
361 fn arg_data_to_arg(data: ArgData) -> Result<Arg, ConfigError> {
362 match data {
363 ArgData::None => Ok(Arg::None),
364 ArgData::String(s) => Ok(Arg::Str(s)),
365 ArgData::Int(n) => Ok(Arg::Int(n)),
366 ArgData::Array(arr) => Ok(Arg::Array(arr)),
367 }
368 }