@@ -0,0 +1,321 @@ |
| 1 | +//! Lua configuration loader for gar ecosystem integration |
| 2 | +//! |
| 3 | +//! Reads `gar.notification` table from `~/.config/gar/init.lua` |
| 4 | + |
| 5 | +use anyhow::{anyhow, Result}; |
| 6 | +use mlua::{Lua, Table, Value}; |
| 7 | +use std::path::Path; |
| 8 | +use tracing::{debug, info, warn}; |
| 9 | + |
| 10 | +use super::{ |
| 11 | + AnimationConfig, AppearanceConfig, ColorConfig, Config, GeneralConfig, |
| 12 | + GeometryConfig, HistoryConfig, TimeoutConfig, |
| 13 | +}; |
| 14 | + |
| 15 | +/// Convert mlua Error to anyhow Error |
| 16 | +fn lua_err(e: mlua::Error) -> anyhow::Error { |
| 17 | + anyhow!("Lua error: {}", e) |
| 18 | +} |
| 19 | + |
| 20 | +/// Setup stub functions for gar's Lua API |
| 21 | +/// These no-ops allow garnotify to execute gar's init.lua without errors |
| 22 | +fn setup_gar_stubs(lua: &Lua, gar: &Table) -> Result<()> { |
| 23 | + // gar.set(key, value) - configuration setter |
| 24 | + let set_fn = lua |
| 25 | + .create_function(|_, (_key, _value): (String, Value)| Ok(())) |
| 26 | + .map_err(lua_err)?; |
| 27 | + gar.set("set", set_fn).map_err(lua_err)?; |
| 28 | + |
| 29 | + // gar.bind(keyspec, callback) - keybinding |
| 30 | + let bind_fn = lua |
| 31 | + .create_function(|_, (_keyspec, _callback): (String, Value)| Ok(())) |
| 32 | + .map_err(lua_err)?; |
| 33 | + gar.set("bind", bind_fn).map_err(lua_err)?; |
| 34 | + |
| 35 | + // gar.exec(cmd) - execute command |
| 36 | + let exec_fn = lua |
| 37 | + .create_function(|_, _cmd: String| Ok(())) |
| 38 | + .map_err(lua_err)?; |
| 39 | + gar.set("exec", exec_fn).map_err(lua_err)?; |
| 40 | + |
| 41 | + // gar.exec_once(cmd) - execute command once at startup |
| 42 | + let exec_once_fn = lua |
| 43 | + .create_function(|_, _cmd: String| Ok(())) |
| 44 | + .map_err(lua_err)?; |
| 45 | + gar.set("exec_once", exec_once_fn).map_err(lua_err)?; |
| 46 | + |
| 47 | + // gar.rule(match, actions) - window rules |
| 48 | + let rule_fn = lua |
| 49 | + .create_function(|_, (_match_table, _actions_table): (Value, Value)| Ok(())) |
| 50 | + .map_err(lua_err)?; |
| 51 | + gar.set("rule", rule_fn).map_err(lua_err)?; |
| 52 | + |
| 53 | + // gar.picom_rule(config) - picom window rules |
| 54 | + let picom_rule_fn = lua |
| 55 | + .create_function(|_, _config: Value| Ok(())) |
| 56 | + .map_err(lua_err)?; |
| 57 | + gar.set("picom_rule", picom_rule_fn).map_err(lua_err)?; |
| 58 | + |
| 59 | + // Window management stubs |
| 60 | + for name in &[ |
| 61 | + "focus", |
| 62 | + "swap", |
| 63 | + "close", |
| 64 | + "toggle_floating", |
| 65 | + "equalize", |
| 66 | + "reload", |
| 67 | + "exit", |
| 68 | + "focus_monitor", |
| 69 | + "move_to_monitor", |
| 70 | + ] { |
| 71 | + let stub = lua.create_function(|_, _: Value| Ok(())).map_err(lua_err)?; |
| 72 | + gar.set(*name, stub).map_err(lua_err)?; |
| 73 | + } |
| 74 | + |
| 75 | + // Workspace stubs |
| 76 | + let workspace_fn = lua |
| 77 | + .create_function(|_, _n: Value| Ok(())) |
| 78 | + .map_err(lua_err)?; |
| 79 | + gar.set("workspace", workspace_fn).map_err(lua_err)?; |
| 80 | + |
| 81 | + let workspace_next_fn = lua.create_function(|_, ()| Ok(())).map_err(lua_err)?; |
| 82 | + gar.set("workspace_next", workspace_next_fn) |
| 83 | + .map_err(lua_err)?; |
| 84 | + |
| 85 | + let workspace_prev_fn = lua.create_function(|_, ()| Ok(())).map_err(lua_err)?; |
| 86 | + gar.set("workspace_prev", workspace_prev_fn) |
| 87 | + .map_err(lua_err)?; |
| 88 | + |
| 89 | + let move_fn = lua |
| 90 | + .create_function(|_, _n: Value| Ok(())) |
| 91 | + .map_err(lua_err)?; |
| 92 | + gar.set("move", move_fn).map_err(lua_err)?; |
| 93 | + |
| 94 | + let move_to_workspace_fn = lua |
| 95 | + .create_function(|_, _n: Value| Ok(())) |
| 96 | + .map_err(lua_err)?; |
| 97 | + gar.set("move_to_workspace", move_to_workspace_fn) |
| 98 | + .map_err(lua_err)?; |
| 99 | + |
| 100 | + let resize_fn = lua |
| 101 | + .create_function(|_, (_direction, _amount): (Value, Value)| Ok(())) |
| 102 | + .map_err(lua_err)?; |
| 103 | + gar.set("resize", resize_fn).map_err(lua_err)?; |
| 104 | + |
| 105 | + Ok(()) |
| 106 | +} |
| 107 | + |
| 108 | +/// Load configuration from gar's init.lua file |
| 109 | +pub fn load_from_lua<P: AsRef<Path>>(path: P) -> Result<Option<Config>> { |
| 110 | + let path = path.as_ref(); |
| 111 | + |
| 112 | + if !path.exists() { |
| 113 | + debug!("Lua config file not found: {}", path.display()); |
| 114 | + return Ok(None); |
| 115 | + } |
| 116 | + |
| 117 | + info!("Loading config from {}", path.display()); |
| 118 | + |
| 119 | + let lua = Lua::new(); |
| 120 | + let content = std::fs::read_to_string(path)?; |
| 121 | + |
| 122 | + // Create the gar global table with stub functions |
| 123 | + let gar = lua.create_table().map_err(lua_err)?; |
| 124 | + setup_gar_stubs(&lua, &gar)?; |
| 125 | + lua.globals().set("gar", gar).map_err(lua_err)?; |
| 126 | + |
| 127 | + // Execute the config file |
| 128 | + lua.load(&content) |
| 129 | + .set_name(path.to_string_lossy()) |
| 130 | + .exec() |
| 131 | + .map_err(|e| anyhow!("Failed to execute {}: {}", path.display(), e))?; |
| 132 | + |
| 133 | + // Try to get gar.notification |
| 134 | + let gar: Table = lua.globals().get("gar").map_err(lua_err)?; |
| 135 | + let notification_value: Value = gar.get("notification").map_err(lua_err)?; |
| 136 | + |
| 137 | + match notification_value { |
| 138 | + Value::Table(table) => { |
| 139 | + let config = parse_notification_config(&table)?; |
| 140 | + Ok(Some(config)) |
| 141 | + } |
| 142 | + Value::Nil => { |
| 143 | + debug!("No gar.notification table found in config"); |
| 144 | + Ok(None) |
| 145 | + } |
| 146 | + _ => { |
| 147 | + warn!("gar.notification is not a table"); |
| 148 | + Ok(None) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/// Parse the gar.notification table into Config |
| 154 | +fn parse_notification_config(table: &Table) -> Result<Config> { |
| 155 | + let mut config = Config::default(); |
| 156 | + |
| 157 | + // General settings |
| 158 | + if let Ok(monitor) = table.get::<String>("monitor") { |
| 159 | + config.general.monitor = monitor; |
| 160 | + } |
| 161 | + if let Ok(follow_mouse) = table.get::<bool>("follow_mouse") { |
| 162 | + config.general.follow_mouse = follow_mouse; |
| 163 | + } |
| 164 | + |
| 165 | + // Geometry settings |
| 166 | + if let Ok(position) = table.get::<String>("position") { |
| 167 | + config.geometry.position = position; |
| 168 | + } |
| 169 | + if let Ok(width) = table.get::<u32>("width") { |
| 170 | + config.geometry.width = width; |
| 171 | + } |
| 172 | + if let Ok(max_height) = table.get::<u32>("max_height") { |
| 173 | + config.geometry.max_height = max_height; |
| 174 | + } |
| 175 | + if let Ok(offset_x) = table.get::<i32>("offset_x") { |
| 176 | + config.geometry.offset_x = offset_x; |
| 177 | + } |
| 178 | + if let Ok(offset_y) = table.get::<i32>("offset_y") { |
| 179 | + config.geometry.offset_y = offset_y; |
| 180 | + } |
| 181 | + if let Ok(gap) = table.get::<i32>("gap") { |
| 182 | + config.geometry.gap = gap; |
| 183 | + } |
| 184 | + if let Ok(max_visible) = table.get::<u32>("max_visible") { |
| 185 | + config.geometry.max_visible = max_visible; |
| 186 | + } |
| 187 | + |
| 188 | + // Timeout settings |
| 189 | + if let Ok(timeout) = table.get::<i32>("timeout") { |
| 190 | + // Single timeout applies to normal urgency |
| 191 | + config.timeouts.normal = timeout; |
| 192 | + } |
| 193 | + if let Ok(timeouts) = table.get::<Table>("timeouts") { |
| 194 | + if let Ok(low) = timeouts.get::<i32>("low") { |
| 195 | + config.timeouts.low = low; |
| 196 | + } |
| 197 | + if let Ok(normal) = timeouts.get::<i32>("normal") { |
| 198 | + config.timeouts.normal = normal; |
| 199 | + } |
| 200 | + if let Ok(critical) = timeouts.get::<i32>("critical") { |
| 201 | + config.timeouts.critical = critical; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + // Appearance settings |
| 206 | + if let Ok(font) = table.get::<String>("font") { |
| 207 | + config.appearance.font = font; |
| 208 | + } |
| 209 | + if let Ok(title_font) = table.get::<String>("title_font") { |
| 210 | + config.appearance.title_font = title_font; |
| 211 | + } |
| 212 | + if let Ok(icon_size) = table.get::<u32>("icon_size") { |
| 213 | + config.appearance.icon_size = icon_size; |
| 214 | + } |
| 215 | + if let Ok(padding) = table.get::<u32>("padding") { |
| 216 | + config.appearance.padding = padding; |
| 217 | + } |
| 218 | + if let Ok(corner_radius) = table.get::<u32>("corner_radius") { |
| 219 | + config.appearance.corner_radius = corner_radius; |
| 220 | + } |
| 221 | + if let Ok(border_width) = table.get::<u32>("border_width") { |
| 222 | + config.appearance.border_width = border_width; |
| 223 | + } |
| 224 | + |
| 225 | + // Colors - can be flat or nested |
| 226 | + if let Ok(background) = table.get::<String>("background") { |
| 227 | + config.appearance.colors.background = background.clone(); |
| 228 | + config.appearance.colors.low_background = background.clone(); |
| 229 | + } |
| 230 | + if let Ok(foreground) = table.get::<String>("foreground") { |
| 231 | + config.appearance.colors.foreground = foreground.clone(); |
| 232 | + config.appearance.colors.low_foreground = foreground.clone(); |
| 233 | + } |
| 234 | + if let Ok(border) = table.get::<String>("border") { |
| 235 | + config.appearance.colors.border = border.clone(); |
| 236 | + config.appearance.colors.low_border = border.clone(); |
| 237 | + } |
| 238 | + |
| 239 | + // Nested colors table |
| 240 | + if let Ok(colors) = table.get::<Table>("colors") { |
| 241 | + parse_colors(&colors, &mut config.appearance.colors); |
| 242 | + } |
| 243 | + |
| 244 | + // Animation settings |
| 245 | + if let Ok(animations) = table.get::<Table>("animation") { |
| 246 | + if let Ok(enabled) = animations.get::<bool>("enabled") { |
| 247 | + config.animation.enabled = enabled; |
| 248 | + } |
| 249 | + if let Ok(fade_in) = animations.get::<u32>("fade_in") { |
| 250 | + config.animation.fade_in = fade_in; |
| 251 | + } |
| 252 | + if let Ok(fade_out) = animations.get::<u32>("fade_out") { |
| 253 | + config.animation.fade_out = fade_out; |
| 254 | + } |
| 255 | + if let Ok(slide) = animations.get::<String>("slide") { |
| 256 | + config.animation.slide = slide; |
| 257 | + } |
| 258 | + if let Ok(slide_distance) = animations.get::<u32>("slide_distance") { |
| 259 | + config.animation.slide_distance = slide_distance; |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + // History settings |
| 264 | + if let Ok(history) = table.get::<Table>("history") { |
| 265 | + if let Ok(max_length) = history.get::<usize>("max_length") { |
| 266 | + config.history.max_length = max_length; |
| 267 | + } |
| 268 | + if let Ok(persist) = history.get::<bool>("persist") { |
| 269 | + config.history.persist = persist; |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + debug!( |
| 274 | + "Parsed notification config: position={}, width={}", |
| 275 | + config.geometry.position, config.geometry.width |
| 276 | + ); |
| 277 | + Ok(config) |
| 278 | +} |
| 279 | + |
| 280 | +/// Parse colors table |
| 281 | +fn parse_colors(table: &Table, colors: &mut ColorConfig) { |
| 282 | + if let Ok(bg) = table.get::<String>("background") { |
| 283 | + colors.background = bg; |
| 284 | + } |
| 285 | + if let Ok(fg) = table.get::<String>("foreground") { |
| 286 | + colors.foreground = fg; |
| 287 | + } |
| 288 | + if let Ok(border) = table.get::<String>("border") { |
| 289 | + colors.border = border; |
| 290 | + } |
| 291 | + |
| 292 | + // Low urgency |
| 293 | + if let Ok(low_bg) = table.get::<String>("low_background") { |
| 294 | + colors.low_background = low_bg; |
| 295 | + } |
| 296 | + if let Ok(low_fg) = table.get::<String>("low_foreground") { |
| 297 | + colors.low_foreground = low_fg; |
| 298 | + } |
| 299 | + if let Ok(low_border) = table.get::<String>("low_border") { |
| 300 | + colors.low_border = low_border; |
| 301 | + } |
| 302 | + |
| 303 | + // Critical urgency |
| 304 | + if let Ok(crit_bg) = table.get::<String>("critical_background") { |
| 305 | + colors.critical_background = crit_bg; |
| 306 | + } |
| 307 | + if let Ok(crit_fg) = table.get::<String>("critical_foreground") { |
| 308 | + colors.critical_foreground = crit_fg; |
| 309 | + } |
| 310 | + if let Ok(crit_border) = table.get::<String>("critical_border") { |
| 311 | + colors.critical_border = crit_border; |
| 312 | + } |
| 313 | +} |
| 314 | + |
| 315 | +/// Get the path to gar's init.lua |
| 316 | +pub fn gar_config_path() -> std::path::PathBuf { |
| 317 | + dirs::config_dir() |
| 318 | + .unwrap_or_else(|| std::path::PathBuf::from("~/.config")) |
| 319 | + .join("gar") |
| 320 | + .join("init.lua") |
| 321 | +} |