| 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 modifiers_static: &'static [KeyButMask] = Box::leak(modifiers.into_boxed_slice()); |
| 102 | |
| 103 | let key = string_to_keycode(&kb_data.key)?; |
| 104 | let action = parse_key_action(&kb_data.action)?; |
| 105 | let arg = arg_data_to_arg(kb_data.arg)?; |
| 106 | |
| 107 | keybindings.push(Key::new(modifiers_static, key, action, arg)); |
| 108 | } |
| 109 | |
| 110 | let mut status_blocks = Vec::new(); |
| 111 | for block_data in data.status_blocks { |
| 112 | let format_static: &'static str = Box::leak(block_data.format.into_boxed_str()); |
| 113 | |
| 114 | let command = match block_data.command.as_str() { |
| 115 | "DateTime" => { |
| 116 | let fmt = block_data |
| 117 | .command_arg |
| 118 | .ok_or_else(|| ConfigError::MissingCommandArg { |
| 119 | command: "DateTime".to_string(), |
| 120 | field: "command_arg".to_string(), |
| 121 | })?; |
| 122 | let fmt_static: &'static str = Box::leak(fmt.into_boxed_str()); |
| 123 | BlockCommand::DateTime(fmt_static) |
| 124 | } |
| 125 | "Shell" => { |
| 126 | let cmd = block_data |
| 127 | .command_arg |
| 128 | .ok_or_else(|| ConfigError::MissingCommandArg { |
| 129 | command: "Shell".to_string(), |
| 130 | field: "command_arg".to_string(), |
| 131 | })?; |
| 132 | let cmd_static: &'static str = Box::leak(cmd.into_boxed_str()); |
| 133 | BlockCommand::Shell(cmd_static) |
| 134 | } |
| 135 | "Ram" => BlockCommand::Ram, |
| 136 | "Static" => { |
| 137 | let text = block_data.command_arg.unwrap_or_default(); |
| 138 | let text_static: &'static str = Box::leak(text.into_boxed_str()); |
| 139 | BlockCommand::Static(text_static) |
| 140 | } |
| 141 | "Battery" => { |
| 142 | let formats = |
| 143 | block_data |
| 144 | .battery_formats |
| 145 | .ok_or_else(|| ConfigError::MissingCommandArg { |
| 146 | command: "Battery".to_string(), |
| 147 | field: "battery_formats".to_string(), |
| 148 | })?; |
| 149 | BlockCommand::Battery { |
| 150 | format_charging: Box::leak(formats.charging.into_boxed_str()), |
| 151 | format_discharging: Box::leak(formats.discharging.into_boxed_str()), |
| 152 | format_full: Box::leak(formats.full.into_boxed_str()), |
| 153 | } |
| 154 | } |
| 155 | _ => return Err(ConfigError::UnknownBlockCommand(block_data.command)), |
| 156 | }; |
| 157 | |
| 158 | status_blocks.push(BlockConfig { |
| 159 | format: format_static, |
| 160 | command, |
| 161 | interval_secs: block_data.interval_secs, |
| 162 | color: block_data.color, |
| 163 | underline: block_data.underline, |
| 164 | }); |
| 165 | } |
| 166 | |
| 167 | Ok(crate::Config { |
| 168 | border_width: data.border_width, |
| 169 | border_focused: data.border_focused, |
| 170 | border_unfocused: data.border_unfocused, |
| 171 | font: data.font, |
| 172 | gaps_enabled: data.gaps_enabled, |
| 173 | gap_inner_horizontal: data.gap_inner_horizontal, |
| 174 | gap_inner_vertical: data.gap_inner_vertical, |
| 175 | gap_outer_horizontal: data.gap_outer_horizontal, |
| 176 | gap_outer_vertical: data.gap_outer_vertical, |
| 177 | terminal: data.terminal, |
| 178 | modkey, |
| 179 | tags: data.tags, |
| 180 | keybindings, |
| 181 | status_blocks, |
| 182 | scheme_normal: crate::ColorScheme { |
| 183 | foreground: data.scheme_normal.foreground, |
| 184 | background: data.scheme_normal.background, |
| 185 | underline: data.scheme_normal.underline, |
| 186 | }, |
| 187 | scheme_occupied: crate::ColorScheme { |
| 188 | foreground: data.scheme_occupied.foreground, |
| 189 | background: data.scheme_occupied.background, |
| 190 | underline: data.scheme_occupied.underline, |
| 191 | }, |
| 192 | scheme_selected: crate::ColorScheme { |
| 193 | foreground: data.scheme_selected.foreground, |
| 194 | background: data.scheme_selected.background, |
| 195 | underline: data.scheme_selected.underline, |
| 196 | }, |
| 197 | }) |
| 198 | } |
| 199 | |
| 200 | fn parse_modkey(s: &str) -> Result<KeyButMask, ConfigError> { |
| 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 | _ => Err(ConfigError::InvalidModkey(s.to_string())), |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | fn string_to_keycode(s: &str) -> Result<Keycode, ConfigError> { |
| 214 | match s.to_lowercase().as_str() { |
| 215 | "return" => Ok(keycodes::RETURN), |
| 216 | "q" => Ok(keycodes::Q), |
| 217 | "escape" => Ok(keycodes::ESCAPE), |
| 218 | "space" => Ok(keycodes::SPACE), |
| 219 | "tab" => Ok(keycodes::TAB), |
| 220 | "backspace" => Ok(keycodes::BACKSPACE), |
| 221 | "delete" => Ok(keycodes::DELETE), |
| 222 | |
| 223 | "f1" => Ok(keycodes::F1), |
| 224 | "f2" => Ok(keycodes::F2), |
| 225 | "f3" => Ok(keycodes::F3), |
| 226 | "f4" => Ok(keycodes::F4), |
| 227 | "f5" => Ok(keycodes::F5), |
| 228 | "f6" => Ok(keycodes::F6), |
| 229 | "f7" => Ok(keycodes::F7), |
| 230 | "f8" => Ok(keycodes::F8), |
| 231 | "f9" => Ok(keycodes::F9), |
| 232 | "f10" => Ok(keycodes::F10), |
| 233 | "f11" => Ok(keycodes::F11), |
| 234 | "f12" => Ok(keycodes::F12), |
| 235 | |
| 236 | "a" => Ok(keycodes::A), |
| 237 | "b" => Ok(keycodes::B), |
| 238 | "c" => Ok(keycodes::C), |
| 239 | "d" => Ok(keycodes::D), |
| 240 | "e" => Ok(keycodes::E), |
| 241 | "f" => Ok(keycodes::F), |
| 242 | "g" => Ok(keycodes::G), |
| 243 | "h" => Ok(keycodes::H), |
| 244 | "i" => Ok(keycodes::I), |
| 245 | "j" => Ok(keycodes::J), |
| 246 | "k" => Ok(keycodes::K), |
| 247 | "l" => Ok(keycodes::L), |
| 248 | "m" => Ok(keycodes::M), |
| 249 | "n" => Ok(keycodes::N), |
| 250 | "o" => Ok(keycodes::O), |
| 251 | "p" => Ok(keycodes::P), |
| 252 | "r" => Ok(keycodes::R), |
| 253 | "s" => Ok(keycodes::S), |
| 254 | "t" => Ok(keycodes::T), |
| 255 | "u" => Ok(keycodes::U), |
| 256 | "v" => Ok(keycodes::V), |
| 257 | "w" => Ok(keycodes::W), |
| 258 | "x" => Ok(keycodes::X), |
| 259 | "y" => Ok(keycodes::Y), |
| 260 | "z" => Ok(keycodes::Z), |
| 261 | |
| 262 | "0" => Ok(keycodes::KEY_0), |
| 263 | "1" => Ok(keycodes::KEY_1), |
| 264 | "2" => Ok(keycodes::KEY_2), |
| 265 | "3" => Ok(keycodes::KEY_3), |
| 266 | "4" => Ok(keycodes::KEY_4), |
| 267 | "5" => Ok(keycodes::KEY_5), |
| 268 | "6" => Ok(keycodes::KEY_6), |
| 269 | "7" => Ok(keycodes::KEY_7), |
| 270 | "8" => Ok(keycodes::KEY_8), |
| 271 | "9" => Ok(keycodes::KEY_9), |
| 272 | |
| 273 | "left" => Ok(keycodes::LEFT), |
| 274 | "right" => Ok(keycodes::RIGHT), |
| 275 | "up" => Ok(keycodes::UP), |
| 276 | "down" => Ok(keycodes::DOWN), |
| 277 | "home" => Ok(keycodes::HOME), |
| 278 | "end" => Ok(keycodes::END), |
| 279 | "pageup" => Ok(keycodes::PAGE_UP), |
| 280 | "pagedown" => Ok(keycodes::PAGE_DOWN), |
| 281 | "insert" => Ok(keycodes::INSERT), |
| 282 | |
| 283 | "minus" | "-" => Ok(keycodes::MINUS), |
| 284 | "equal" | "=" => Ok(keycodes::EQUAL), |
| 285 | "bracketleft" | "[" => Ok(keycodes::LEFT_BRACKET), |
| 286 | "bracketright" | "]" => Ok(keycodes::RIGHT_BRACKET), |
| 287 | "semicolon" | ";" => Ok(keycodes::SEMICOLON), |
| 288 | "apostrophe" | "'" => Ok(keycodes::APOSTROPHE), |
| 289 | "grave" | "`" => Ok(keycodes::GRAVE), |
| 290 | "backslash" | "\\" => Ok(keycodes::BACKSLASH), |
| 291 | "comma" | "," => Ok(keycodes::COMMA), |
| 292 | "period" | "." => Ok(keycodes::PERIOD), |
| 293 | "slash" | "/" => Ok(keycodes::SLASH), |
| 294 | |
| 295 | _ => Err(ConfigError::UnknownKey(s.to_string())), |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | fn parse_key_action(s: &str) -> Result<crate::keyboard::KeyAction, ConfigError> { |
| 300 | match s { |
| 301 | "Spawn" => Ok(KeyAction::Spawn), |
| 302 | "KillClient" => Ok(KeyAction::KillClient), |
| 303 | "FocusStack" => Ok(KeyAction::FocusStack), |
| 304 | "Quit" => Ok(KeyAction::Quit), |
| 305 | "Restart" => Ok(KeyAction::Restart), |
| 306 | "ViewTag" => Ok(KeyAction::ViewTag), |
| 307 | "MoveToTag" => Ok(KeyAction::MoveToTag), |
| 308 | "ToggleGaps" => Ok(KeyAction::ToggleGaps), |
| 309 | "ToggleFullScreen" => Ok(KeyAction::ToggleFullScreen), |
| 310 | "ToggleFloating" => Ok(KeyAction::ToggleFloating), |
| 311 | _ => Err(ConfigError::UnknownAction(s.to_string())), |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | fn arg_data_to_arg(data: ArgData) -> Result<crate::keyboard::Arg, ConfigError> { |
| 316 | match data { |
| 317 | ArgData::None => Ok(Arg::None), |
| 318 | ArgData::String(s) => { |
| 319 | let static_str: &'static str = Box::leak(s.into_boxed_str()); |
| 320 | Ok(Arg::Str(static_str)) |
| 321 | } |
| 322 | ArgData::Int(n) => Ok(Arg::Int(n)), |
| 323 | ArgData::Array(arr) => { |
| 324 | let static_strs: Vec<&'static str> = arr |
| 325 | .into_iter() |
| 326 | .map(|s| Box::leak(s.into_boxed_str()) as &'static str) |
| 327 | .collect(); |
| 328 | let static_slice: &'static [&'static str] = Box::leak(static_strs.into_boxed_slice()); |
| 329 | Ok(Arg::Array(static_slice)) |
| 330 | } |
| 331 | } |
| 332 | } |