| 1 | use mlua::{Lua, Table, Value}; |
| 2 | use std::cell::RefCell; |
| 3 | use std::rc::Rc; |
| 4 | |
| 5 | use crate::bar::BlockConfig; |
| 6 | use crate::errors::ConfigError; |
| 7 | use crate::keyboard::handlers::{Arg, KeyAction, KeyBinding, KeyPress}; |
| 8 | use crate::keyboard::keysyms::{self, Keysym}; |
| 9 | use crate::ColorScheme; |
| 10 | use x11rb::protocol::xproto::KeyButMask; |
| 11 | |
| 12 | #[derive(Clone)] |
| 13 | pub struct ConfigBuilder { |
| 14 | pub border_width: u32, |
| 15 | pub border_focused: u32, |
| 16 | pub border_unfocused: u32, |
| 17 | pub font: String, |
| 18 | pub gaps_enabled: bool, |
| 19 | pub smartgaps_enabled: bool, |
| 20 | pub gap_inner_horizontal: u32, |
| 21 | pub gap_inner_vertical: u32, |
| 22 | pub gap_outer_horizontal: u32, |
| 23 | pub gap_outer_vertical: u32, |
| 24 | pub terminal: String, |
| 25 | pub modkey: KeyButMask, |
| 26 | pub tags: Vec<String>, |
| 27 | pub layout_symbols: Vec<crate::LayoutSymbolOverride>, |
| 28 | pub keybindings: Vec<KeyBinding>, |
| 29 | pub status_blocks: Vec<BlockConfig>, |
| 30 | pub scheme_normal: ColorScheme, |
| 31 | pub scheme_occupied: ColorScheme, |
| 32 | pub scheme_selected: ColorScheme, |
| 33 | pub autostart: Vec<String>, |
| 34 | } |
| 35 | |
| 36 | impl Default for ConfigBuilder { |
| 37 | fn default() -> Self { |
| 38 | Self { |
| 39 | border_width: 2, |
| 40 | border_focused: 0x6dade3, |
| 41 | border_unfocused: 0xbbbbbb, |
| 42 | font: "monospace:style=Bold:size=10".to_string(), |
| 43 | gaps_enabled: true, |
| 44 | smartgaps_enabled: true, |
| 45 | gap_inner_horizontal: 5, |
| 46 | gap_inner_vertical: 5, |
| 47 | gap_outer_horizontal: 5, |
| 48 | gap_outer_vertical: 5, |
| 49 | terminal: "st".to_string(), |
| 50 | modkey: KeyButMask::MOD4, |
| 51 | tags: vec!["1".into(), "2".into(), "3".into()], |
| 52 | layout_symbols: Vec::new(), |
| 53 | keybindings: Vec::new(), |
| 54 | status_blocks: Vec::new(), |
| 55 | scheme_normal: ColorScheme { |
| 56 | foreground: 0xffffff, |
| 57 | background: 0x000000, |
| 58 | underline: 0x444444, |
| 59 | }, |
| 60 | scheme_occupied: ColorScheme { |
| 61 | foreground: 0xffffff, |
| 62 | background: 0x000000, |
| 63 | underline: 0x444444, |
| 64 | }, |
| 65 | scheme_selected: ColorScheme { |
| 66 | foreground: 0xffffff, |
| 67 | background: 0x000000, |
| 68 | underline: 0x444444, |
| 69 | }, |
| 70 | autostart: Vec::new(), |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | type SharedBuilder = Rc<RefCell<ConfigBuilder>>; |
| 76 | |
| 77 | pub fn register_api(lua: &Lua) -> Result<SharedBuilder, ConfigError> { |
| 78 | let builder = Rc::new(RefCell::new(ConfigBuilder::default())); |
| 79 | |
| 80 | let oxwm_table = lua.create_table()?; |
| 81 | |
| 82 | register_spawn(&lua, &oxwm_table, builder.clone())?; |
| 83 | register_key_module(&lua, &oxwm_table, builder.clone())?; |
| 84 | register_gaps_module(&lua, &oxwm_table, builder.clone())?; |
| 85 | register_border_module(&lua, &oxwm_table, builder.clone())?; |
| 86 | register_client_module(&lua, &oxwm_table)?; |
| 87 | register_layout_module(&lua, &oxwm_table)?; |
| 88 | register_tag_module(&lua, &oxwm_table)?; |
| 89 | register_bar_module(&lua, &oxwm_table, builder.clone())?; |
| 90 | register_misc(&lua, &oxwm_table, builder.clone())?; |
| 91 | |
| 92 | lua.globals().set("oxwm", oxwm_table)?; |
| 93 | |
| 94 | Ok(builder) |
| 95 | } |
| 96 | |
| 97 | fn register_spawn(lua: &Lua, parent: &Table, _builder: SharedBuilder) -> Result<(), ConfigError> { |
| 98 | let spawn = lua.create_function(|lua, cmd: Value| { |
| 99 | create_action_table(lua, "Spawn", cmd) |
| 100 | })?; |
| 101 | let spawn_terminal = lua.create_function(|lua, ()| { |
| 102 | create_action_table(lua, "SpawnTerminal", Value::Nil) |
| 103 | })?; |
| 104 | parent.set("spawn", spawn)?; |
| 105 | parent.set("spawn_terminal", spawn_terminal)?; |
| 106 | Ok(()) |
| 107 | } |
| 108 | |
| 109 | fn register_key_module(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<(), ConfigError> { |
| 110 | let key_table = lua.create_table()?; |
| 111 | |
| 112 | let builder_clone = builder.clone(); |
| 113 | let bind = lua.create_function(move |lua, (mods, key, action): (Value, String, Value)| { |
| 114 | let modifiers = parse_modifiers_value(lua, mods)?; |
| 115 | let keysym = parse_keysym(&key)?; |
| 116 | let (key_action, arg) = parse_action_value(lua, action)?; |
| 117 | |
| 118 | let binding = KeyBinding::single_key(modifiers, keysym, key_action, arg); |
| 119 | builder_clone.borrow_mut().keybindings.push(binding); |
| 120 | |
| 121 | Ok(()) |
| 122 | })?; |
| 123 | |
| 124 | let builder_clone = builder.clone(); |
| 125 | let chord = lua.create_function(move |lua, (keys, action): (Table, Value)| { |
| 126 | let mut key_presses = Vec::new(); |
| 127 | |
| 128 | for i in 1..=keys.len()? { |
| 129 | let key_spec: Table = keys.get(i)?; |
| 130 | let mods: Value = key_spec.get(1)?; |
| 131 | let key: String = key_spec.get(2)?; |
| 132 | |
| 133 | let modifiers = parse_modifiers_value(lua, mods)?; |
| 134 | let keysym = parse_keysym(&key)?; |
| 135 | |
| 136 | key_presses.push(KeyPress { modifiers, keysym }); |
| 137 | } |
| 138 | |
| 139 | let (key_action, arg) = parse_action_value(lua, action)?; |
| 140 | let binding = KeyBinding::new(key_presses, key_action, arg); |
| 141 | builder_clone.borrow_mut().keybindings.push(binding); |
| 142 | |
| 143 | Ok(()) |
| 144 | })?; |
| 145 | |
| 146 | key_table.set("bind", bind)?; |
| 147 | key_table.set("chord", chord)?; |
| 148 | parent.set("key", key_table)?; |
| 149 | Ok(()) |
| 150 | } |
| 151 | |
| 152 | fn register_gaps_module(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<(), ConfigError> { |
| 153 | let gaps_table = lua.create_table()?; |
| 154 | |
| 155 | let builder_clone = builder.clone(); |
| 156 | let set_enabled = lua.create_function(move |_, enabled: bool| { |
| 157 | builder_clone.borrow_mut().gaps_enabled = enabled; |
| 158 | Ok(()) |
| 159 | })?; |
| 160 | |
| 161 | let builder_clone = builder.clone(); |
| 162 | let enable = lua.create_function(move |_, ()| { |
| 163 | builder_clone.borrow_mut().gaps_enabled = true; |
| 164 | Ok(()) |
| 165 | })?; |
| 166 | |
| 167 | let builder_clone = builder.clone(); |
| 168 | let disable = lua.create_function(move |_, ()| { |
| 169 | builder_clone.borrow_mut().gaps_enabled = false; |
| 170 | Ok(()) |
| 171 | })?; |
| 172 | |
| 173 | let builder_clone = builder.clone(); |
| 174 | let set_inner = lua.create_function(move |_, (h, v): (u32, u32)| { |
| 175 | let mut b = builder_clone.borrow_mut(); |
| 176 | b.gap_inner_horizontal = h; |
| 177 | b.gap_inner_vertical = v; |
| 178 | Ok(()) |
| 179 | })?; |
| 180 | |
| 181 | let builder_clone = builder.clone(); |
| 182 | let set_outer = lua.create_function(move |_, (h, v): (u32, u32)| { |
| 183 | let mut b = builder_clone.borrow_mut(); |
| 184 | b.gap_outer_horizontal = h; |
| 185 | b.gap_outer_vertical = v; |
| 186 | Ok(()) |
| 187 | })?; |
| 188 | |
| 189 | let builder_clone = builder.clone(); |
| 190 | let set_smart = lua.create_function(move |_, enabled: bool| { |
| 191 | builder_clone.borrow_mut().smartgaps_enabled = enabled; |
| 192 | Ok(()) |
| 193 | })?; |
| 194 | |
| 195 | gaps_table.set("set_enabled", set_enabled)?; |
| 196 | gaps_table.set("enable", enable)?; |
| 197 | gaps_table.set("disable", disable)?; |
| 198 | gaps_table.set("set_inner", set_inner)?; |
| 199 | gaps_table.set("set_outer", set_outer)?; |
| 200 | gaps_table.set("set_smart", set_smart)?; |
| 201 | parent.set("gaps", gaps_table)?; |
| 202 | Ok(()) |
| 203 | } |
| 204 | |
| 205 | fn register_border_module(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<(), ConfigError> { |
| 206 | let border_table = lua.create_table()?; |
| 207 | |
| 208 | let builder_clone = builder.clone(); |
| 209 | let set_width = lua.create_function(move |_, width: u32| { |
| 210 | builder_clone.borrow_mut().border_width = width; |
| 211 | Ok(()) |
| 212 | })?; |
| 213 | |
| 214 | let builder_clone = builder.clone(); |
| 215 | let set_focused_color = lua.create_function(move |_, color: Value| { |
| 216 | let color_u32 = parse_color_value(color)?; |
| 217 | builder_clone.borrow_mut().border_focused = color_u32; |
| 218 | Ok(()) |
| 219 | })?; |
| 220 | |
| 221 | let builder_clone = builder.clone(); |
| 222 | let set_unfocused_color = lua.create_function(move |_, color: Value| { |
| 223 | let color_u32 = parse_color_value(color)?; |
| 224 | builder_clone.borrow_mut().border_unfocused = color_u32; |
| 225 | Ok(()) |
| 226 | })?; |
| 227 | |
| 228 | border_table.set("set_width", set_width)?; |
| 229 | border_table.set("set_focused_color", set_focused_color)?; |
| 230 | border_table.set("set_unfocused_color", set_unfocused_color)?; |
| 231 | parent.set("border", border_table)?; |
| 232 | Ok(()) |
| 233 | } |
| 234 | |
| 235 | fn register_client_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> { |
| 236 | let client_table = lua.create_table()?; |
| 237 | |
| 238 | let kill = lua.create_function(|lua, ()| { |
| 239 | create_action_table(lua, "KillClient", Value::Nil) |
| 240 | })?; |
| 241 | |
| 242 | let toggle_fullscreen = lua.create_function(|lua, ()| { |
| 243 | create_action_table(lua, "ToggleFullScreen", Value::Nil) |
| 244 | })?; |
| 245 | |
| 246 | let toggle_floating = lua.create_function(|lua, ()| { |
| 247 | create_action_table(lua, "ToggleFloating", Value::Nil) |
| 248 | })?; |
| 249 | |
| 250 | let focus_stack = lua.create_function(|lua, dir: i32| { |
| 251 | create_action_table(lua, "FocusStack", Value::Integer(dir as i64)) |
| 252 | })?; |
| 253 | |
| 254 | let focus_direction = lua.create_function(|lua, dir: String| { |
| 255 | let dir_int = direction_string_to_int(&dir)?; |
| 256 | create_action_table(lua, "FocusDirection", Value::Integer(dir_int)) |
| 257 | })?; |
| 258 | |
| 259 | let swap_direction = lua.create_function(|lua, dir: String| { |
| 260 | let dir_int = direction_string_to_int(&dir)?; |
| 261 | create_action_table(lua, "SwapDirection", Value::Integer(dir_int)) |
| 262 | })?; |
| 263 | |
| 264 | let exchange = lua.create_function(|lua, ()| { |
| 265 | create_action_table(lua, "ExchangeClient", Value::Nil) |
| 266 | })?; |
| 267 | |
| 268 | client_table.set("kill", kill)?; |
| 269 | client_table.set("toggle_fullscreen", toggle_fullscreen)?; |
| 270 | client_table.set("toggle_floating", toggle_floating)?; |
| 271 | client_table.set("focus_stack", focus_stack)?; |
| 272 | client_table.set("focus_direction", focus_direction)?; |
| 273 | client_table.set("swap_direction", swap_direction)?; |
| 274 | client_table.set("exchange", exchange)?; |
| 275 | |
| 276 | parent.set("client", client_table)?; |
| 277 | Ok(()) |
| 278 | } |
| 279 | |
| 280 | fn register_layout_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> { |
| 281 | let layout_table = lua.create_table()?; |
| 282 | |
| 283 | let cycle = lua.create_function(|lua, ()| { |
| 284 | create_action_table(lua, "CycleLayout", Value::Nil) |
| 285 | })?; |
| 286 | |
| 287 | let set = lua.create_function(|lua, name: String| { |
| 288 | create_action_table(lua, "ChangeLayout", Value::String(lua.create_string(&name)?)) |
| 289 | })?; |
| 290 | |
| 291 | layout_table.set("cycle", cycle)?; |
| 292 | layout_table.set("set", set)?; |
| 293 | parent.set("layout", layout_table)?; |
| 294 | Ok(()) |
| 295 | } |
| 296 | |
| 297 | fn register_tag_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> { |
| 298 | let tag_table = lua.create_table()?; |
| 299 | |
| 300 | let view = lua.create_function(|lua, idx: i32| { |
| 301 | create_action_table(lua, "ViewTag", Value::Integer(idx as i64)) |
| 302 | })?; |
| 303 | |
| 304 | let move_to = lua.create_function(|lua, idx: i32| { |
| 305 | create_action_table(lua, "MoveToTag", Value::Integer(idx as i64)) |
| 306 | })?; |
| 307 | |
| 308 | tag_table.set("view", view)?; |
| 309 | tag_table.set("move_to", move_to)?; |
| 310 | parent.set("tag", tag_table)?; |
| 311 | Ok(()) |
| 312 | } |
| 313 | |
| 314 | fn register_bar_module(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<(), ConfigError> { |
| 315 | let bar_table = lua.create_table()?; |
| 316 | |
| 317 | let builder_clone = builder.clone(); |
| 318 | let set_font = lua.create_function(move |_, font: String| { |
| 319 | builder_clone.borrow_mut().font = font; |
| 320 | Ok(()) |
| 321 | })?; |
| 322 | |
| 323 | let block_table = lua.create_table()?; |
| 324 | |
| 325 | let ram = lua.create_function(|lua, config: Table| { |
| 326 | create_block_config(lua, config, "Ram", None) |
| 327 | })?; |
| 328 | |
| 329 | let datetime = lua.create_function(|lua, config: Table| { |
| 330 | let date_format: String = config.get("date_format") |
| 331 | .map_err(|_| mlua::Error::RuntimeError("oxwm.bar.block.datetime: 'date_format' field is required (e.g., '%H:%M')".into()))?; |
| 332 | create_block_config(lua, config, "DateTime", Some(Value::String(lua.create_string(&date_format)?))) |
| 333 | })?; |
| 334 | |
| 335 | let shell = lua.create_function(|lua, config: Table| { |
| 336 | let command: String = config.get("command") |
| 337 | .map_err(|_| mlua::Error::RuntimeError("oxwm.bar.block.shell: 'command' field is required".into()))?; |
| 338 | create_block_config(lua, config, "Shell", Some(Value::String(lua.create_string(&command)?))) |
| 339 | })?; |
| 340 | |
| 341 | let static_block = lua.create_function(|lua, config: Table| { |
| 342 | let text: String = config.get("text") |
| 343 | .map_err(|_| mlua::Error::RuntimeError("oxwm.bar.block.static: 'text' field is required".into()))?; |
| 344 | create_block_config(lua, config, "Static", Some(Value::String(lua.create_string(&text)?))) |
| 345 | })?; |
| 346 | |
| 347 | let battery = lua.create_function(|lua, config: Table| { |
| 348 | let charging: String = config.get("charging") |
| 349 | .map_err(|_| mlua::Error::RuntimeError("oxwm.bar.block.battery: 'charging' field is required".into()))?; |
| 350 | let discharging: String = config.get("discharging") |
| 351 | .map_err(|_| mlua::Error::RuntimeError("oxwm.bar.block.battery: 'discharging' field is required".into()))?; |
| 352 | let full: String = config.get("full") |
| 353 | .map_err(|_| mlua::Error::RuntimeError("oxwm.bar.block.battery: 'full' field is required".into()))?; |
| 354 | |
| 355 | let formats_table = lua.create_table()?; |
| 356 | formats_table.set("charging", charging)?; |
| 357 | formats_table.set("discharging", discharging)?; |
| 358 | formats_table.set("full", full)?; |
| 359 | |
| 360 | create_block_config(lua, config, "Battery", Some(Value::Table(formats_table))) |
| 361 | })?; |
| 362 | |
| 363 | block_table.set("ram", ram)?; |
| 364 | block_table.set("datetime", datetime)?; |
| 365 | block_table.set("shell", shell)?; |
| 366 | block_table.set("static", static_block)?; |
| 367 | block_table.set("battery", battery)?; |
| 368 | |
| 369 | // Deprecated add_block() function for backwards compatibility |
| 370 | // This allows old configs to still work, but users should migrate to set_blocks() |
| 371 | let builder_clone = builder.clone(); |
| 372 | let add_block = lua.create_function(move |_, (format, block_type, arg, interval, color, underline): (String, String, Value, u64, Value, Option<bool>)| -> mlua::Result<()> { |
| 373 | eprintln!("WARNING: oxwm.bar.add_block() is deprecated. Please migrate to oxwm.bar.set_blocks() with block constructors."); |
| 374 | eprintln!("See the migration guide for details."); |
| 375 | |
| 376 | let cmd = match block_type.as_str() { |
| 377 | "DateTime" => { |
| 378 | let fmt = if let Value::String(s) = arg { |
| 379 | s.to_str()?.to_string() |
| 380 | } else { |
| 381 | return Err(mlua::Error::RuntimeError("DateTime block requires format string as third argument".into())); |
| 382 | }; |
| 383 | crate::bar::BlockCommand::DateTime(fmt) |
| 384 | } |
| 385 | "Shell" => { |
| 386 | let cmd_str = if let Value::String(s) = arg { |
| 387 | s.to_str()?.to_string() |
| 388 | } else { |
| 389 | return Err(mlua::Error::RuntimeError("Shell block requires command string as third argument".into())); |
| 390 | }; |
| 391 | crate::bar::BlockCommand::Shell(cmd_str) |
| 392 | } |
| 393 | "Ram" => crate::bar::BlockCommand::Ram, |
| 394 | "Static" => { |
| 395 | let text = if let Value::String(s) = arg { |
| 396 | s.to_str()?.to_string() |
| 397 | } else { |
| 398 | String::new() |
| 399 | }; |
| 400 | crate::bar::BlockCommand::Static(text) |
| 401 | } |
| 402 | "Battery" => { |
| 403 | return Err(mlua::Error::RuntimeError( |
| 404 | "Battery block is not supported with add_block(). Please use oxwm.bar.set_blocks() with oxwm.bar.block.battery()".into() |
| 405 | )); |
| 406 | } |
| 407 | _ => return Err(mlua::Error::RuntimeError(format!("Unknown block type '{}'", block_type))), |
| 408 | }; |
| 409 | |
| 410 | let color_u32 = parse_color_value(color)?; |
| 411 | |
| 412 | let block = crate::bar::BlockConfig { |
| 413 | format, |
| 414 | command: cmd, |
| 415 | interval_secs: interval, |
| 416 | color: color_u32, |
| 417 | underline: underline.unwrap_or(false), |
| 418 | }; |
| 419 | |
| 420 | builder_clone.borrow_mut().status_blocks.push(block); |
| 421 | Ok(()) |
| 422 | })?; |
| 423 | |
| 424 | let builder_clone = builder.clone(); |
| 425 | let set_blocks = lua.create_function(move |_, blocks: Table| { |
| 426 | use crate::bar::BlockCommand; |
| 427 | |
| 428 | let mut block_configs = Vec::new(); |
| 429 | |
| 430 | for i in 1..=blocks.len()? { |
| 431 | let block_table: Table = blocks.get(i)?; |
| 432 | let block_type: String = block_table.get("__block_type")?; |
| 433 | let format: String = block_table.get("format").unwrap_or_default(); |
| 434 | let interval: u64 = block_table.get("interval")?; |
| 435 | let color_val: Value = block_table.get("color")?; |
| 436 | let underline: bool = block_table.get("underline").unwrap_or(false); |
| 437 | let arg: Option<Value> = block_table.get("__arg").ok(); |
| 438 | |
| 439 | let cmd = match block_type.as_str() { |
| 440 | "DateTime" => { |
| 441 | let fmt = arg.and_then(|v| { |
| 442 | if let Value::String(s) = v { |
| 443 | s.to_str().ok().map(|s| s.to_string()) |
| 444 | } else { |
| 445 | None |
| 446 | } |
| 447 | }).ok_or_else(|| mlua::Error::RuntimeError("DateTime block missing format".into()))?; |
| 448 | BlockCommand::DateTime(fmt) |
| 449 | } |
| 450 | "Shell" => { |
| 451 | let cmd_str = arg.and_then(|v| { |
| 452 | if let Value::String(s) = v { |
| 453 | s.to_str().ok().map(|s| s.to_string()) |
| 454 | } else { |
| 455 | None |
| 456 | } |
| 457 | }).ok_or_else(|| mlua::Error::RuntimeError("Shell block missing command".into()))?; |
| 458 | BlockCommand::Shell(cmd_str) |
| 459 | } |
| 460 | "Ram" => BlockCommand::Ram, |
| 461 | "Static" => { |
| 462 | let text = arg.and_then(|v| { |
| 463 | if let Value::String(s) = v { |
| 464 | s.to_str().ok().map(|s| s.to_string()) |
| 465 | } else { |
| 466 | None |
| 467 | } |
| 468 | }).unwrap_or_default(); |
| 469 | BlockCommand::Static(text) |
| 470 | } |
| 471 | "Battery" => { |
| 472 | let formats = arg.and_then(|v| { |
| 473 | if let Value::Table(t) = v { |
| 474 | Some(t) |
| 475 | } else { |
| 476 | None |
| 477 | } |
| 478 | }).ok_or_else(|| mlua::Error::RuntimeError("Battery block missing formats".into()))?; |
| 479 | |
| 480 | let charging: String = formats.get("charging")?; |
| 481 | let discharging: String = formats.get("discharging")?; |
| 482 | let full: String = formats.get("full")?; |
| 483 | |
| 484 | BlockCommand::Battery { |
| 485 | format_charging: charging, |
| 486 | format_discharging: discharging, |
| 487 | format_full: full, |
| 488 | } |
| 489 | } |
| 490 | _ => return Err(mlua::Error::RuntimeError(format!("Unknown block type '{}'", block_type))), |
| 491 | }; |
| 492 | |
| 493 | let color_u32 = parse_color_value(color_val)?; |
| 494 | |
| 495 | let block = crate::bar::BlockConfig { |
| 496 | format, |
| 497 | command: cmd, |
| 498 | interval_secs: interval, |
| 499 | color: color_u32, |
| 500 | underline, |
| 501 | }; |
| 502 | |
| 503 | block_configs.push(block); |
| 504 | } |
| 505 | |
| 506 | builder_clone.borrow_mut().status_blocks = block_configs; |
| 507 | Ok(()) |
| 508 | })?; |
| 509 | |
| 510 | let builder_clone = builder.clone(); |
| 511 | let set_scheme_normal = lua.create_function(move |_, (fg, bg, ul): (Value, Value, Value)| { |
| 512 | let foreground = parse_color_value(fg)?; |
| 513 | let background = parse_color_value(bg)?; |
| 514 | let underline = parse_color_value(ul)?; |
| 515 | |
| 516 | builder_clone.borrow_mut().scheme_normal = ColorScheme { |
| 517 | foreground, |
| 518 | background, |
| 519 | underline, |
| 520 | }; |
| 521 | Ok(()) |
| 522 | })?; |
| 523 | |
| 524 | let builder_clone = builder.clone(); |
| 525 | let set_scheme_occupied = lua.create_function(move |_, (fg, bg, ul): (Value, Value, Value)| { |
| 526 | let foreground = parse_color_value(fg)?; |
| 527 | let background = parse_color_value(bg)?; |
| 528 | let underline = parse_color_value(ul)?; |
| 529 | |
| 530 | builder_clone.borrow_mut().scheme_occupied = ColorScheme { |
| 531 | foreground, |
| 532 | background, |
| 533 | underline, |
| 534 | }; |
| 535 | Ok(()) |
| 536 | })?; |
| 537 | |
| 538 | let builder_clone = builder.clone(); |
| 539 | let set_scheme_selected = lua.create_function(move |_, (fg, bg, ul): (Value, Value, Value)| { |
| 540 | let foreground = parse_color_value(fg)?; |
| 541 | let background = parse_color_value(bg)?; |
| 542 | let underline = parse_color_value(ul)?; |
| 543 | |
| 544 | builder_clone.borrow_mut().scheme_selected = ColorScheme { |
| 545 | foreground, |
| 546 | background, |
| 547 | underline, |
| 548 | }; |
| 549 | Ok(()) |
| 550 | })?; |
| 551 | |
| 552 | bar_table.set("set_font", set_font)?; |
| 553 | bar_table.set("block", block_table)?; |
| 554 | bar_table.set("add_block", add_block)?; // Deprecated, for backwards compatibility |
| 555 | bar_table.set("set_blocks", set_blocks)?; |
| 556 | bar_table.set("set_scheme_normal", set_scheme_normal)?; |
| 557 | bar_table.set("set_scheme_occupied", set_scheme_occupied)?; |
| 558 | bar_table.set("set_scheme_selected", set_scheme_selected)?; |
| 559 | parent.set("bar", bar_table)?; |
| 560 | Ok(()) |
| 561 | } |
| 562 | |
| 563 | fn register_misc(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<(), ConfigError> { |
| 564 | let builder_clone = builder.clone(); |
| 565 | let set_terminal = lua.create_function(move |_, term: String| { |
| 566 | builder_clone.borrow_mut().terminal = term; |
| 567 | Ok(()) |
| 568 | })?; |
| 569 | |
| 570 | let builder_clone = builder.clone(); |
| 571 | let set_modkey = lua.create_function(move |_, modkey_str: String| { |
| 572 | let modkey = parse_modkey_string(&modkey_str) |
| 573 | .map_err(|e| mlua::Error::RuntimeError(format!("{}", e)))?; |
| 574 | builder_clone.borrow_mut().modkey = modkey; |
| 575 | Ok(()) |
| 576 | })?; |
| 577 | |
| 578 | let builder_clone = builder.clone(); |
| 579 | let set_tags = lua.create_function(move |_, tags: Vec<String>| { |
| 580 | builder_clone.borrow_mut().tags = tags; |
| 581 | Ok(()) |
| 582 | })?; |
| 583 | |
| 584 | let quit = lua.create_function(|lua, ()| { |
| 585 | create_action_table(lua, "Quit", Value::Nil) |
| 586 | })?; |
| 587 | |
| 588 | let restart = lua.create_function(|lua, ()| { |
| 589 | create_action_table(lua, "Restart", Value::Nil) |
| 590 | })?; |
| 591 | |
| 592 | let recompile = lua.create_function(|lua, ()| { |
| 593 | create_action_table(lua, "Recompile", Value::Nil) |
| 594 | })?; |
| 595 | |
| 596 | let toggle_gaps = lua.create_function(|lua, ()| { |
| 597 | create_action_table(lua, "ToggleGaps", Value::Nil) |
| 598 | })?; |
| 599 | |
| 600 | let set_master_factor = lua.create_function(|lua, delta: i32| { |
| 601 | create_action_table(lua, "SetMasterFactor", Value::Integer(delta as i64)) |
| 602 | })?; |
| 603 | |
| 604 | let inc_num_master = lua.create_function(|lua, delta: i32| { |
| 605 | create_action_table(lua, "IncNumMaster", Value::Integer(delta as i64)) |
| 606 | })?; |
| 607 | |
| 608 | let show_keybinds = lua.create_function(|lua, ()| { |
| 609 | create_action_table(lua, "ShowKeybindOverlay", Value::Nil) |
| 610 | })?; |
| 611 | |
| 612 | let focus_monitor = lua.create_function(|lua, idx: i32| { |
| 613 | create_action_table(lua, "FocusMonitor", Value::Integer(idx as i64)) |
| 614 | })?; |
| 615 | |
| 616 | let builder_clone = builder.clone(); |
| 617 | let set_layout_symbol = lua.create_function(move |_, (name, symbol): (String, String)| { |
| 618 | builder_clone.borrow_mut().layout_symbols.push(crate::LayoutSymbolOverride { |
| 619 | name, |
| 620 | symbol, |
| 621 | }); |
| 622 | Ok(()) |
| 623 | })?; |
| 624 | |
| 625 | let builder_clone = builder.clone(); |
| 626 | let autostart = lua.create_function(move |_, cmd: String| { |
| 627 | builder_clone.borrow_mut().autostart.push(cmd); |
| 628 | Ok(()) |
| 629 | })?; |
| 630 | |
| 631 | parent.set("set_terminal", set_terminal)?; |
| 632 | parent.set("set_modkey", set_modkey)?; |
| 633 | parent.set("set_tags", set_tags)?; |
| 634 | parent.set("set_layout_symbol", set_layout_symbol)?; |
| 635 | parent.set("autostart", autostart)?; |
| 636 | parent.set("quit", quit)?; |
| 637 | parent.set("restart", restart)?; |
| 638 | parent.set("recompile", recompile)?; |
| 639 | parent.set("toggle_gaps", toggle_gaps)?; |
| 640 | parent.set("set_master_factor", set_master_factor)?; |
| 641 | parent.set("inc_num_master", inc_num_master)?; |
| 642 | parent.set("show_keybinds", show_keybinds)?; |
| 643 | parent.set("focus_monitor", focus_monitor)?; |
| 644 | Ok(()) |
| 645 | } |
| 646 | |
| 647 | fn parse_modifiers_value(_lua: &Lua, value: Value) -> mlua::Result<Vec<KeyButMask>> { |
| 648 | match value { |
| 649 | Value::Table(t) => { |
| 650 | let mut mods = Vec::new(); |
| 651 | for i in 1..=t.len()? { |
| 652 | let mod_str: String = t.get(i)?; |
| 653 | let mask = parse_modkey_string(&mod_str) |
| 654 | .map_err(|e| mlua::Error::RuntimeError(format!("oxwm.key.bind: invalid modifier - {}", e)))?; |
| 655 | mods.push(mask); |
| 656 | } |
| 657 | Ok(mods) |
| 658 | } |
| 659 | Value::String(s) => { |
| 660 | let s_str = s.to_str()?; |
| 661 | let mask = parse_modkey_string(&s_str) |
| 662 | .map_err(|e| mlua::Error::RuntimeError(format!("oxwm.key.bind: invalid modifier - {}", e)))?; |
| 663 | Ok(vec![mask]) |
| 664 | } |
| 665 | _ => Err(mlua::Error::RuntimeError( |
| 666 | "oxwm.key.bind: first argument must be a table of modifiers like {\"Mod4\"} or {\"Mod4\", \"Shift\"}".into(), |
| 667 | )), |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | fn parse_modkey_string(s: &str) -> Result<KeyButMask, ConfigError> { |
| 672 | match s { |
| 673 | "Mod1" => Ok(KeyButMask::MOD1), |
| 674 | "Mod2" => Ok(KeyButMask::MOD2), |
| 675 | "Mod3" => Ok(KeyButMask::MOD3), |
| 676 | "Mod4" => Ok(KeyButMask::MOD4), |
| 677 | "Mod5" => Ok(KeyButMask::MOD5), |
| 678 | "Shift" => Ok(KeyButMask::SHIFT), |
| 679 | "Control" => Ok(KeyButMask::CONTROL), |
| 680 | _ => Err(ConfigError::InvalidModkey(format!("'{}' is not a valid modifier. Use one of: Mod1, Mod4, Shift, Control", s))), |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | fn parse_keysym(key: &str) -> mlua::Result<Keysym> { |
| 685 | keysyms::keysym_from_str(key) |
| 686 | .ok_or_else(|| mlua::Error::RuntimeError(format!("unknown key '{}'. valid keys include: Return, Space, A-Z, 0-9, F1-F12, Left, Right, Up, Down, etc. check oxwm.lua type definitions for the complete list", key))) |
| 687 | } |
| 688 | |
| 689 | fn parse_action_value(_lua: &Lua, value: Value) -> mlua::Result<(KeyAction, Arg)> { |
| 690 | match value { |
| 691 | Value::Function(_) => { |
| 692 | Err(mlua::Error::RuntimeError( |
| 693 | "action must be a function call, not a function reference. did you forget ()? example: oxwm.spawn('st') not oxwm.spawn".into() |
| 694 | )) |
| 695 | } |
| 696 | Value::Table(t) => { |
| 697 | if let Ok(action_name) = t.get::<String>("__action") { |
| 698 | let action = string_to_action(&action_name)?; |
| 699 | let arg = if let Ok(arg_val) = t.get::<Value>("__arg") { |
| 700 | value_to_arg(arg_val)? |
| 701 | } else { |
| 702 | Arg::None |
| 703 | }; |
| 704 | return Ok((action, arg)); |
| 705 | } |
| 706 | |
| 707 | Err(mlua::Error::RuntimeError( |
| 708 | "action must be a table returned by oxwm functions like oxwm.spawn(), oxwm.client.kill(), oxwm.quit(), etc.".into(), |
| 709 | )) |
| 710 | } |
| 711 | _ => Err(mlua::Error::RuntimeError( |
| 712 | "action must be a table returned by oxwm functions like oxwm.spawn(), oxwm.client.kill(), oxwm.quit(), etc.".into(), |
| 713 | )), |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | fn string_to_action(s: &str) -> mlua::Result<KeyAction> { |
| 718 | match s { |
| 719 | "Spawn" => Ok(KeyAction::Spawn), |
| 720 | "SpawnTerminal" => Ok(KeyAction::SpawnTerminal), |
| 721 | "KillClient" => Ok(KeyAction::KillClient), |
| 722 | "FocusStack" => Ok(KeyAction::FocusStack), |
| 723 | "FocusDirection" => Ok(KeyAction::FocusDirection), |
| 724 | "SwapDirection" => Ok(KeyAction::SwapDirection), |
| 725 | "Quit" => Ok(KeyAction::Quit), |
| 726 | "Restart" => Ok(KeyAction::Restart), |
| 727 | "Recompile" => Ok(KeyAction::Recompile), |
| 728 | "ViewTag" => Ok(KeyAction::ViewTag), |
| 729 | "ToggleGaps" => Ok(KeyAction::ToggleGaps), |
| 730 | "SetMasterFactor" => Ok(KeyAction::SetMasterFactor), |
| 731 | "IncNumMaster" => Ok(KeyAction::IncNumMaster), |
| 732 | "ToggleFullScreen" => Ok(KeyAction::ToggleFullScreen), |
| 733 | "ToggleFloating" => Ok(KeyAction::ToggleFloating), |
| 734 | "ChangeLayout" => Ok(KeyAction::ChangeLayout), |
| 735 | "CycleLayout" => Ok(KeyAction::CycleLayout), |
| 736 | "MoveToTag" => Ok(KeyAction::MoveToTag), |
| 737 | "FocusMonitor" => Ok(KeyAction::FocusMonitor), |
| 738 | "ExchangeClient" => Ok(KeyAction::ExchangeClient), |
| 739 | "ShowKeybindOverlay" => Ok(KeyAction::ShowKeybindOverlay), |
| 740 | _ => Err(mlua::Error::RuntimeError(format!("unknown action '{}'. this is an internal error, please report it", s))), |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | fn value_to_arg(value: Value) -> mlua::Result<Arg> { |
| 745 | match value { |
| 746 | Value::Nil => Ok(Arg::None), |
| 747 | Value::String(s) => Ok(Arg::Str(s.to_str()?.to_string())), |
| 748 | Value::Integer(i) => Ok(Arg::Int(i as i32)), |
| 749 | Value::Number(n) => Ok(Arg::Int(n as i32)), |
| 750 | Value::Table(t) => { |
| 751 | let mut arr = Vec::new(); |
| 752 | for i in 1..=t.len()? { |
| 753 | let item: String = t.get(i)?; |
| 754 | arr.push(item); |
| 755 | } |
| 756 | Ok(Arg::Array(arr)) |
| 757 | } |
| 758 | _ => Ok(Arg::None), |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | fn create_action_table(lua: &Lua, action_name: &str, arg: Value) -> mlua::Result<Table> { |
| 763 | let table = lua.create_table()?; |
| 764 | table.set("__action", action_name)?; |
| 765 | table.set("__arg", arg)?; |
| 766 | Ok(table) |
| 767 | } |
| 768 | |
| 769 | fn direction_string_to_int(dir: &str) -> mlua::Result<i64> { |
| 770 | match dir { |
| 771 | "up" => Ok(0), |
| 772 | "down" => Ok(1), |
| 773 | "left" => Ok(2), |
| 774 | "right" => Ok(3), |
| 775 | _ => Err(mlua::Error::RuntimeError( |
| 776 | format!("invalid direction '{}'. must be one of: up, down, left, right", dir) |
| 777 | )), |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | fn parse_color_value(value: Value) -> mlua::Result<u32> { |
| 782 | match value { |
| 783 | Value::Integer(i) => Ok(i as u32), |
| 784 | Value::Number(n) => Ok(n as u32), |
| 785 | Value::String(s) => { |
| 786 | let s = s.to_str()?; |
| 787 | if s.starts_with('#') { |
| 788 | u32::from_str_radix(&s[1..], 16) |
| 789 | .map_err(|e| mlua::Error::RuntimeError(format!("invalid hex color '{}': {}. use format like #ff0000 or 0xff0000", s, e))) |
| 790 | } else if s.starts_with("0x") { |
| 791 | u32::from_str_radix(&s[2..], 16) |
| 792 | .map_err(|e| mlua::Error::RuntimeError(format!("invalid hex color '{}': {}. use format like 0xff0000 or #ff0000", s, e))) |
| 793 | } else { |
| 794 | s.parse::<u32>() |
| 795 | .map_err(|e| mlua::Error::RuntimeError(format!("invalid color '{}': {}. use hex format like 0xff0000 or #ff0000", s, e))) |
| 796 | } |
| 797 | } |
| 798 | _ => Err(mlua::Error::RuntimeError( |
| 799 | "color must be a number (0xff0000) or string ('#ff0000' or '0xff0000')".into(), |
| 800 | )), |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | fn create_block_config(lua: &Lua, config: Table, block_type: &str, arg: Option<Value>) -> mlua::Result<Table> { |
| 805 | let table = lua.create_table()?; |
| 806 | table.set("__block_type", block_type)?; |
| 807 | |
| 808 | let format: String = config.get("format").unwrap_or_default(); |
| 809 | let interval: u64 = config.get("interval")?; |
| 810 | let color: Value = config.get("color")?; |
| 811 | let underline: bool = config.get("underline").unwrap_or(false); |
| 812 | |
| 813 | table.set("format", format)?; |
| 814 | table.set("interval", interval)?; |
| 815 | table.set("color", color)?; |
| 816 | table.set("underline", underline)?; |
| 817 | |
| 818 | if let Some(arg_val) = arg { |
| 819 | table.set("__arg", arg_val)?; |
| 820 | } |
| 821 | |
| 822 | Ok(table) |
| 823 | } |