| 1 | use anyhow::{Context, Result}; |
| 2 | use std::path::PathBuf; |
| 3 | |
| 4 | fn main() -> Result<()> { |
| 5 | let args: Vec<String> = std::env::args().collect(); |
| 6 | |
| 7 | let mut custom_config_path: Option<PathBuf> = None; |
| 8 | |
| 9 | match args.get(1).map(|s| s.as_str()) { |
| 10 | Some("--version") => { |
| 11 | println!("oxwm {}", env!("CARGO_PKG_VERSION")); |
| 12 | return Ok(()); |
| 13 | } |
| 14 | Some("--help") => { |
| 15 | print_help(); |
| 16 | return Ok(()); |
| 17 | } |
| 18 | Some("--init") => { |
| 19 | init_config()?; |
| 20 | return Ok(()); |
| 21 | } |
| 22 | Some("--config") => { |
| 23 | if let Some(path) = args.get(2) { |
| 24 | custom_config_path = Some(PathBuf::from(path)); |
| 25 | } else { |
| 26 | eprintln!("Error: --config requires a path argument"); |
| 27 | std::process::exit(1); |
| 28 | } |
| 29 | } |
| 30 | _ => {} |
| 31 | } |
| 32 | |
| 33 | let config = load_config(custom_config_path)?; |
| 34 | |
| 35 | let mut wm = oxwm::window_manager::WindowManager::new(config)?; |
| 36 | let should_restart = wm.run()?; |
| 37 | |
| 38 | drop(wm); |
| 39 | |
| 40 | if should_restart { |
| 41 | use std::os::unix::process::CommandExt; |
| 42 | let err = std::process::Command::new(&args[0]).args(&args[1..]).exec(); |
| 43 | eprintln!("Failed to restart: {}", err); |
| 44 | } |
| 45 | |
| 46 | Ok(()) |
| 47 | } |
| 48 | |
| 49 | fn load_config(custom_path: Option<PathBuf>) -> Result<oxwm::Config> { |
| 50 | let config_path = if let Some(path) = custom_path { |
| 51 | path |
| 52 | } else { |
| 53 | let config_dir = get_config_path(); |
| 54 | let lua_path = config_dir.join("config.lua"); |
| 55 | |
| 56 | if !lua_path.exists() { |
| 57 | let ron_path = config_dir.join("config.ron"); |
| 58 | let had_ron_config = ron_path.exists(); |
| 59 | |
| 60 | println!("No config found at {:?}", config_dir); |
| 61 | println!("Creating default Lua config..."); |
| 62 | init_config()?; |
| 63 | |
| 64 | if had_ron_config { |
| 65 | println!("\n NOTICE: OXWM has migrated to Lua configuration."); |
| 66 | println!(" Your old config.ron has been preserved, but is no longer used."); |
| 67 | println!(" Your settings have been reset to defaults."); |
| 68 | println!(" Please manually port your configuration to the new Lua format."); |
| 69 | println!(" See the new config.lua template for examples.\n"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | lua_path |
| 74 | }; |
| 75 | |
| 76 | let config_str = |
| 77 | std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?; |
| 78 | |
| 79 | let config_dir = config_path.parent(); |
| 80 | oxwm::config::parse_lua_config(&config_str, config_dir) |
| 81 | .with_context(|| "Failed to parse Lua config") |
| 82 | } |
| 83 | |
| 84 | fn init_config() -> Result<()> { |
| 85 | let config_dir = get_config_path(); |
| 86 | std::fs::create_dir_all(&config_dir)?; |
| 87 | |
| 88 | let config_template = include_str!("../../templates/config.lua"); |
| 89 | let config_path = config_dir.join("config.lua"); |
| 90 | std::fs::write(&config_path, config_template)?; |
| 91 | |
| 92 | let oxwm_lua_template = include_str!("../../templates/oxwm.lua"); |
| 93 | let oxwm_lua_path = config_dir.join("oxwm.lua"); |
| 94 | std::fs::write(&oxwm_lua_path, oxwm_lua_template)?; |
| 95 | |
| 96 | println!("✓ Config created at {:?}", config_path); |
| 97 | println!("✓ LSP definitions installed at {:?}", oxwm_lua_path); |
| 98 | println!(" Edit the file and reload with Mod+Shift+R"); |
| 99 | println!(" No compilation needed - changes take effect immediately!"); |
| 100 | |
| 101 | Ok(()) |
| 102 | } |
| 103 | |
| 104 | fn get_config_path() -> PathBuf { |
| 105 | dirs::config_dir() |
| 106 | .expect("Could not find config directory") |
| 107 | .join("oxwm") |
| 108 | } |
| 109 | |
| 110 | fn print_help() { |
| 111 | println!("OXWM - A dynamic window manager written in Rust\n"); |
| 112 | println!("USAGE:"); |
| 113 | println!(" oxwm [OPTIONS]\n"); |
| 114 | println!("OPTIONS:"); |
| 115 | println!(" --init Create default config in ~/.config/oxwm/config.lua"); |
| 116 | println!(" --config <PATH> Use custom config file"); |
| 117 | println!(" --version Print version information"); |
| 118 | println!(" --help Print this help message\n"); |
| 119 | println!("CONFIG:"); |
| 120 | println!(" Location: ~/.config/oxwm/config.lua"); |
| 121 | println!(" Edit the config file and use Mod+Shift+R to reload"); |
| 122 | println!(" No compilation needed - instant hot-reload!"); |
| 123 | println!(" LSP support included with oxwm.lua type definitions\n"); |
| 124 | println!("FIRST RUN:"); |
| 125 | println!(" Run 'oxwm --init' to create a config file"); |
| 126 | println!(" Or just start oxwm and it will create one automatically\n"); |
| 127 | } |