tony / oxwm

Branch: f188c77
Switch branches/tags
master
tonybtw Fixed box::leak issue. f188c77 2025-10-15
315 lines · 9.8 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 pub fn parse_config(input: &str) -> Result<crate::Config, ConfigError> {
10 let config_data: ConfigData = ron::from_str(input)?;
11 config_data_to_config(config_data)
12 }
13
14 #[derive(Debug, Deserialize)]
15 struct ConfigData {
16 border_width: u32,
17 border_focused: u32,
18 border_unfocused: u32,
19 font: String,
20
21 gaps_enabled: bool,
22 gap_inner_horizontal: u32,
23 gap_inner_vertical: u32,
24 gap_outer_horizontal: u32,
25 gap_outer_vertical: u32,
26
27 terminal: String,
28 modkey: String,
29
30 tags: Vec<String>,
31 keybindings: Vec<KeybindingData>,
32 status_blocks: Vec<StatusBlockData>,
33
34 scheme_normal: ColorSchemeData,
35 scheme_occupied: ColorSchemeData,
36 scheme_selected: ColorSchemeData,
37 }
38
39 #[derive(Debug, Deserialize)]
40 struct KeybindingData {
41 modifiers: Vec<String>,
42 key: String,
43 action: String,
44 #[serde(default)]
45 arg: ArgData,
46 }
47
48 #[derive(Debug, Deserialize)]
49 #[serde(untagged)]
50 enum ArgData {
51 None,
52 String(String),
53 Int(i32),
54 Array(Vec<String>),
55 }
56
57 impl Default for ArgData {
58 fn default() -> Self {
59 ArgData::None
60 }
61 }
62
63 #[derive(Debug, Deserialize)]
64 struct StatusBlockData {
65 format: String,
66 command: String,
67 #[serde(default)]
68 command_arg: Option<String>,
69 #[serde(default)]
70 battery_formats: Option<BatteryFormats>,
71 interval_secs: u64,
72 color: u32,
73 underline: bool,
74 }
75
76 #[derive(Debug, Deserialize)]
77 struct BatteryFormats {
78 charging: String,
79 discharging: String,
80 full: String,
81 }
82
83 #[derive(Debug, Deserialize)]
84 struct ColorSchemeData {
85 foreground: u32,
86 background: u32,
87 underline: u32,
88 }
89
90 fn config_data_to_config(data: ConfigData) -> Result<crate::Config, ConfigError> {
91 let modkey = parse_modkey(&data.modkey)?;
92
93 let mut keybindings = Vec::new();
94 for kb_data in data.keybindings {
95 let modifiers = kb_data
96 .modifiers
97 .iter()
98 .map(|s| parse_modkey(s))
99 .collect::<Result<Vec<_>, _>>()?;
100
101 let key = string_to_keycode(&kb_data.key)?;
102 let action = parse_key_action(&kb_data.action)?;
103 let arg = arg_data_to_arg(kb_data.arg)?;
104
105 keybindings.push(Key::new(modifiers, key, action, arg));
106 }
107
108 let mut status_blocks = Vec::new();
109 for block_data in data.status_blocks {
110 let command = match block_data.command.as_str() {
111 "DateTime" => {
112 let fmt = block_data
113 .command_arg
114 .ok_or_else(|| ConfigError::MissingCommandArg {
115 command: "DateTime".to_string(),
116 field: "command_arg".to_string(),
117 })?;
118 BlockCommand::DateTime(fmt)
119 }
120 "Shell" => {
121 let cmd = block_data
122 .command_arg
123 .ok_or_else(|| ConfigError::MissingCommandArg {
124 command: "Shell".to_string(),
125 field: "command_arg".to_string(),
126 })?;
127 BlockCommand::Shell(cmd)
128 }
129 "Ram" => BlockCommand::Ram,
130 "Static" => {
131 let text = block_data.command_arg.unwrap_or_default();
132 BlockCommand::Static(text)
133 }
134 "Battery" => {
135 let formats =
136 block_data
137 .battery_formats
138 .ok_or_else(|| ConfigError::MissingCommandArg {
139 command: "Battery".to_string(),
140 field: "battery_formats".to_string(),
141 })?;
142 BlockCommand::Battery {
143 format_charging: formats.charging,
144 format_discharging: formats.discharging,
145 format_full: formats.full,
146 }
147 }
148 _ => return Err(ConfigError::UnknownBlockCommand(block_data.command)),
149 };
150
151 status_blocks.push(BlockConfig {
152 format: block_data.format,
153 command,
154 interval_secs: block_data.interval_secs,
155 color: block_data.color,
156 underline: block_data.underline,
157 });
158 }
159
160 Ok(crate::Config {
161 border_width: data.border_width,
162 border_focused: data.border_focused,
163 border_unfocused: data.border_unfocused,
164 font: data.font,
165 gaps_enabled: data.gaps_enabled,
166 gap_inner_horizontal: data.gap_inner_horizontal,
167 gap_inner_vertical: data.gap_inner_vertical,
168 gap_outer_horizontal: data.gap_outer_horizontal,
169 gap_outer_vertical: data.gap_outer_vertical,
170 terminal: data.terminal,
171 modkey,
172 tags: data.tags,
173 keybindings,
174 status_blocks,
175 scheme_normal: crate::ColorScheme {
176 foreground: data.scheme_normal.foreground,
177 background: data.scheme_normal.background,
178 underline: data.scheme_normal.underline,
179 },
180 scheme_occupied: crate::ColorScheme {
181 foreground: data.scheme_occupied.foreground,
182 background: data.scheme_occupied.background,
183 underline: data.scheme_occupied.underline,
184 },
185 scheme_selected: crate::ColorScheme {
186 foreground: data.scheme_selected.foreground,
187 background: data.scheme_selected.background,
188 underline: data.scheme_selected.underline,
189 },
190 })
191 }
192
193 fn parse_modkey(s: &str) -> Result<KeyButMask, ConfigError> {
194 match s {
195 "Mod1" => Ok(KeyButMask::MOD1),
196 "Mod2" => Ok(KeyButMask::MOD2),
197 "Mod3" => Ok(KeyButMask::MOD3),
198 "Mod4" => Ok(KeyButMask::MOD4),
199 "Mod5" => Ok(KeyButMask::MOD5),
200 "Shift" => Ok(KeyButMask::SHIFT),
201 "Control" => Ok(KeyButMask::CONTROL),
202 _ => Err(ConfigError::InvalidModkey(s.to_string())),
203 }
204 }
205
206 fn string_to_keycode(s: &str) -> Result<Keycode, ConfigError> {
207 match s.to_lowercase().as_str() {
208 "return" => Ok(keycodes::RETURN),
209 "q" => Ok(keycodes::Q),
210 "escape" => Ok(keycodes::ESCAPE),
211 "space" => Ok(keycodes::SPACE),
212 "tab" => Ok(keycodes::TAB),
213 "backspace" => Ok(keycodes::BACKSPACE),
214 "delete" => Ok(keycodes::DELETE),
215
216 "f1" => Ok(keycodes::F1),
217 "f2" => Ok(keycodes::F2),
218 "f3" => Ok(keycodes::F3),
219 "f4" => Ok(keycodes::F4),
220 "f5" => Ok(keycodes::F5),
221 "f6" => Ok(keycodes::F6),
222 "f7" => Ok(keycodes::F7),
223 "f8" => Ok(keycodes::F8),
224 "f9" => Ok(keycodes::F9),
225 "f10" => Ok(keycodes::F10),
226 "f11" => Ok(keycodes::F11),
227 "f12" => Ok(keycodes::F12),
228
229 "a" => Ok(keycodes::A),
230 "b" => Ok(keycodes::B),
231 "c" => Ok(keycodes::C),
232 "d" => Ok(keycodes::D),
233 "e" => Ok(keycodes::E),
234 "f" => Ok(keycodes::F),
235 "g" => Ok(keycodes::G),
236 "h" => Ok(keycodes::H),
237 "i" => Ok(keycodes::I),
238 "j" => Ok(keycodes::J),
239 "k" => Ok(keycodes::K),
240 "l" => Ok(keycodes::L),
241 "m" => Ok(keycodes::M),
242 "n" => Ok(keycodes::N),
243 "o" => Ok(keycodes::O),
244 "p" => Ok(keycodes::P),
245 "r" => Ok(keycodes::R),
246 "s" => Ok(keycodes::S),
247 "t" => Ok(keycodes::T),
248 "u" => Ok(keycodes::U),
249 "v" => Ok(keycodes::V),
250 "w" => Ok(keycodes::W),
251 "x" => Ok(keycodes::X),
252 "y" => Ok(keycodes::Y),
253 "z" => Ok(keycodes::Z),
254
255 "0" => Ok(keycodes::KEY_0),
256 "1" => Ok(keycodes::KEY_1),
257 "2" => Ok(keycodes::KEY_2),
258 "3" => Ok(keycodes::KEY_3),
259 "4" => Ok(keycodes::KEY_4),
260 "5" => Ok(keycodes::KEY_5),
261 "6" => Ok(keycodes::KEY_6),
262 "7" => Ok(keycodes::KEY_7),
263 "8" => Ok(keycodes::KEY_8),
264 "9" => Ok(keycodes::KEY_9),
265
266 "left" => Ok(keycodes::LEFT),
267 "right" => Ok(keycodes::RIGHT),
268 "up" => Ok(keycodes::UP),
269 "down" => Ok(keycodes::DOWN),
270 "home" => Ok(keycodes::HOME),
271 "end" => Ok(keycodes::END),
272 "pageup" => Ok(keycodes::PAGE_UP),
273 "pagedown" => Ok(keycodes::PAGE_DOWN),
274 "insert" => Ok(keycodes::INSERT),
275
276 "minus" | "-" => Ok(keycodes::MINUS),
277 "equal" | "=" => Ok(keycodes::EQUAL),
278 "bracketleft" | "[" => Ok(keycodes::LEFT_BRACKET),
279 "bracketright" | "]" => Ok(keycodes::RIGHT_BRACKET),
280 "semicolon" | ";" => Ok(keycodes::SEMICOLON),
281 "apostrophe" | "'" => Ok(keycodes::APOSTROPHE),
282 "grave" | "`" => Ok(keycodes::GRAVE),
283 "backslash" | "\\" => Ok(keycodes::BACKSLASH),
284 "comma" | "," => Ok(keycodes::COMMA),
285 "period" | "." => Ok(keycodes::PERIOD),
286 "slash" | "/" => Ok(keycodes::SLASH),
287
288 _ => Err(ConfigError::UnknownKey(s.to_string())),
289 }
290 }
291
292 fn parse_key_action(s: &str) -> Result<crate::keyboard::KeyAction, ConfigError> {
293 match s {
294 "Spawn" => Ok(KeyAction::Spawn),
295 "KillClient" => Ok(KeyAction::KillClient),
296 "FocusStack" => Ok(KeyAction::FocusStack),
297 "Quit" => Ok(KeyAction::Quit),
298 "Restart" => Ok(KeyAction::Restart),
299 "ViewTag" => Ok(KeyAction::ViewTag),
300 "MoveToTag" => Ok(KeyAction::MoveToTag),
301 "ToggleGaps" => Ok(KeyAction::ToggleGaps),
302 "ToggleFullScreen" => Ok(KeyAction::ToggleFullScreen),
303 "ToggleFloating" => Ok(KeyAction::ToggleFloating),
304 _ => Err(ConfigError::UnknownAction(s.to_string())),
305 }
306 }
307
308 fn arg_data_to_arg(data: ArgData) -> Result<Arg, ConfigError> {
309 match data {
310 ArgData::None => Ok(Arg::None),
311 ArgData::String(s) => Ok(Arg::Str(s)),
312 ArgData::Int(n) => Ok(Arg::Int(n)),
313 ArgData::Array(arr) => Ok(Arg::Array(arr)),
314 }
315 }