tony / oxwm

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