| 1 | use anyhow::Result; |
| 2 | use std::path::PathBuf; |
| 3 | |
| 4 | fn main() -> Result<()> { |
| 5 | let args: Vec<String> = std::env::args().collect(); |
| 6 | |
| 7 | match args.get(1).map(|s| s.as_str()) { |
| 8 | Some("--version") => { |
| 9 | println!("oxwm {}", env!("CARGO_PKG_VERSION")); |
| 10 | return Ok(()); |
| 11 | } |
| 12 | Some("--help") => { |
| 13 | print_help(); |
| 14 | return Ok(()); |
| 15 | } |
| 16 | Some("--init") => { |
| 17 | init_config()?; |
| 18 | return Ok(()); |
| 19 | } |
| 20 | _ => {} |
| 21 | } |
| 22 | |
| 23 | let config = load_config()?; |
| 24 | |
| 25 | let mut wm = oxwm::window_manager::WindowManager::new(config)?; |
| 26 | let should_restart = wm.run()?; |
| 27 | |
| 28 | drop(wm); |
| 29 | |
| 30 | if should_restart { |
| 31 | use std::os::unix::process::CommandExt; |
| 32 | let err = std::process::Command::new(&args[0]).args(&args[1..]).exec(); |
| 33 | eprintln!("Failed to restart: {}", err); |
| 34 | } |
| 35 | |
| 36 | Ok(()) |
| 37 | } |
| 38 | |
| 39 | fn load_config() -> Result<oxwm::Config> { |
| 40 | let config_path = get_config_path().join("config.ron"); |
| 41 | |
| 42 | if !config_path.exists() { |
| 43 | println!("No config found at {:?}", config_path); |
| 44 | println!("Creating default config..."); |
| 45 | init_config()?; |
| 46 | } |
| 47 | |
| 48 | let config_str = std::fs::read_to_string(&config_path) |
| 49 | .map_err(|e| anyhow::anyhow!("Failed to read config file: {}", e))?; |
| 50 | |
| 51 | oxwm::config::parse_config(&config_str) |
| 52 | .map_err(|e| anyhow::anyhow!("Failed to parse config: {}", e)) |
| 53 | } |
| 54 | |
| 55 | fn init_config() -> Result<()> { |
| 56 | let config_dir = get_config_path(); |
| 57 | std::fs::create_dir_all(&config_dir)?; |
| 58 | |
| 59 | let config_template = include_str!("../../templates/config.ron"); |
| 60 | let config_path = config_dir.join("config.ron"); |
| 61 | |
| 62 | std::fs::write(&config_path, config_template)?; |
| 63 | |
| 64 | println!("✓ Config created at {:?}", config_path); |
| 65 | println!(" Edit the file and reload with Mod+Shift+R"); |
| 66 | println!(" No compilation needed - changes take effect immediately!"); |
| 67 | |
| 68 | Ok(()) |
| 69 | } |
| 70 | |
| 71 | fn get_config_path() -> PathBuf { |
| 72 | dirs::config_dir() |
| 73 | .expect("Could not find config directory") |
| 74 | .join("oxwm") |
| 75 | } |
| 76 | |
| 77 | fn print_help() { |
| 78 | println!("OXWM - A dynamic window manager written in Rust\n"); |
| 79 | println!("USAGE:"); |
| 80 | println!(" oxwm [OPTIONS]\n"); |
| 81 | println!("OPTIONS:"); |
| 82 | println!(" --init Create default config in ~/.config/oxwm/config.ron"); |
| 83 | println!(" --version Print version information"); |
| 84 | println!(" --help Print this help message\n"); |
| 85 | println!("CONFIG:"); |
| 86 | println!(" Location: ~/.config/oxwm/config.ron"); |
| 87 | println!(" Edit the config file and use Mod+Shift+R to reload"); |
| 88 | println!(" No compilation needed - instant hot-reload!\n"); |
| 89 | println!("FIRST RUN:"); |
| 90 | println!(" Run 'oxwm --init' to create a config file"); |
| 91 | println!(" Or just start oxwm and it will create one automatically\n"); |
| 92 | } |