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