From 4e70c3dec13e33a055accb64c900fa569088e62c Mon Sep 17 00:00:00 2001 From: nyx Date: Fri, 18 Oct 2024 02:29:07 -0400 Subject: [PATCH 1/7] implement cli arguments --- Cargo.lock | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/gui.rs | 27 +++++++++++++ src/main.rs | 28 ++++++++++++- 4 files changed, 169 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 3c878cb..e94a070 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,55 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "autocfg" version = "1.4.0" @@ -130,6 +179,52 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.79", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + [[package]] name = "crossbeam-channel" version = "0.5.13" @@ -711,6 +806,7 @@ checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" name = "hyprwall" version = "0.1.1" dependencies = [ + "clap", "crossbeam-channel", "gdk", "gio 0.18.4", @@ -735,6 +831,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "lazy_static" version = "1.5.0" @@ -1162,6 +1264,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "1.0.109" @@ -1315,6 +1423,12 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "version-compare" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 9f188d6..0453ef9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ categories = ["gui"] edition = "2021" [dependencies] +clap = { version = "4.3", features = ["derive"] } gtk = { version = "0.9.2", package = "gtk4" } glib = { version = "0.18.0", features = ["v2_68"] } gio = "0.18.0" diff --git a/src/gui.rs b/src/gui.rs index cba3475..6c595bb 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -431,3 +431,30 @@ pub fn custom_error_popup(title: &str, text: &str, modal: bool) { dialog.show(); } + +pub fn load_last_wallpaper() -> Option { + let config_path = shellexpand::tilde(CONFIG_FILE).into_owned(); + fs::File::open(config_path).ok().and_then(|mut file| { + let mut contents = String::new(); + file.read_to_string(&mut contents).ok()?; + contents + .lines() + .find(|line| line.starts_with("last_wallpaper = ")) + .map(|line| line.trim_start_matches("last_wallpaper = ").to_string()) + }) +} + +pub fn save_last_wallpaper(path: &str) { + let config_path = shellexpand::tilde(CONFIG_FILE).into_owned(); + if let Ok(mut contents) = fs::read_to_string(&config_path) { + if let Some(line) = contents + .lines() + .find(|line| line.starts_with("last_wallpaper = ")) + { + contents = contents.replace(line, &format!("last_wallpaper = {}", path)); + } else { + contents.push_str(&format!("\nlast_wallpaper = {}", path)); + } + let _ = fs::write(config_path, contents); + } +} diff --git a/src/main.rs b/src/main.rs index 161bc73..fd0d8ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod gui; +use clap::Parser; use gtk::{prelude::*, Application}; use lazy_static::lazy_static; use parking_lot::Mutex; @@ -20,10 +21,24 @@ pub enum WallpaperBackend { Feh, } +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Cli { + #[arg(long, help = "Restore the last selected wallpaper")] + restore: bool, +} + fn main() { + let cli = Cli::parse(); + let rt = Runtime::new().expect("Failed to create Tokio runtime"); let _guard = rt.enter(); + if cli.restore { + restore_last_wallpaper(); + return; + } + let app = Application::builder() .application_id("nnyyxxxx.hyprwall") .build(); @@ -35,7 +50,10 @@ fn main() { pub fn set_wallpaper(path: String) { glib::spawn_future_local(async move { match set_wallpaper_internal(&path).await { - Ok(_) => println!("Wallpaper set successfully"), + Ok(_) => { + println!("Wallpaper set successfully"); + gui::save_last_wallpaper(&path); + } Err(e) => { eprintln!("Error setting wallpaper: {}", e); gui::custom_error_popup("Error setting wallpaper", &e, true); @@ -250,3 +268,11 @@ async fn drop_all_wallpapers(backend: WallpaperBackend) { _ => {} } } + +fn restore_last_wallpaper() { + if let Some(last_wallpaper) = gui::load_last_wallpaper() { + set_wallpaper(last_wallpaper); + } else { + eprintln!("No last wallpaper found to restore"); + } +} From 5bf2f8934a247336bf6cad7b12a8238ce1b6d404 Mon Sep 17 00:00:00 2001 From: nyx Date: Fri, 18 Oct 2024 02:35:00 -0400 Subject: [PATCH 2/7] fix problems with restoring --- src/main.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index fd0d8ee..1ff4383 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,9 +31,6 @@ struct Cli { fn main() { let cli = Cli::parse(); - let rt = Runtime::new().expect("Failed to create Tokio runtime"); - let _guard = rt.enter(); - if cli.restore { restore_last_wallpaper(); return; @@ -271,7 +268,16 @@ async fn drop_all_wallpapers(backend: WallpaperBackend) { fn restore_last_wallpaper() { if let Some(last_wallpaper) = gui::load_last_wallpaper() { - set_wallpaper(last_wallpaper); + let rt = Runtime::new().expect("Failed to create Tokio runtime"); + match rt.block_on(set_wallpaper_internal(&last_wallpaper)) { + Ok(_) => { + println!("Wallpaper restored successfully"); + gui::save_last_wallpaper(&last_wallpaper); + } + Err(e) => { + eprintln!("Error restoring wallpaper: {}", e); + } + } } else { eprintln!("No last wallpaper found to restore"); } From 1518b53863cf58ca8a5350a8eee6928242d5cd35 Mon Sep 17 00:00:00 2001 From: nyx Date: Fri, 18 Oct 2024 02:40:02 -0400 Subject: [PATCH 3/7] forgot to bump ver LOL --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e94a070..c3772c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -804,7 +804,7 @@ checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" [[package]] name = "hyprwall" -version = "0.1.1" +version = "0.1.2" dependencies = [ "clap", "crossbeam-channel", diff --git a/Cargo.toml b/Cargo.toml index 0453ef9..242f422 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hyprwall" -version = "0.1.1" +version = "0.1.2" authors = ["Nyx "] license = "GPL-2.0" description = "GUI for setting wallpapers with Hyprpaper, written in blazingly fast Rust!" From 4a90c808e962c20c4a72728cbe443b1da7d5f421 Mon Sep 17 00:00:00 2001 From: nyx Date: Fri, 18 Oct 2024 02:50:36 -0400 Subject: [PATCH 4/7] more implementation --- Cargo.lock | 4 ++-- src/gui.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 20 +++++++++++++++++++- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c3772c4..dff5be8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -845,9 +845,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.160" +version = "0.2.161" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0b21006cd1874ae9e650973c565615676dc4a274c965bb0a73796dac838ce4f" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" [[package]] name = "libredox" diff --git a/src/gui.rs b/src/gui.rs index 6c595bb..9acfdea 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -160,7 +160,16 @@ pub fn build_ui(app: &Application) { backend_combo.append(Some("swww"), "Swww"); backend_combo.append(Some("wallutils"), "Wallutils"); backend_combo.append(Some("feh"), "Feh"); - backend_combo.set_active_id(Some("hyprpaper")); + + let current_backend = *crate::CURRENT_BACKEND.lock(); + let backend_id = match current_backend { + WallpaperBackend::Hyprpaper => "hyprpaper", + WallpaperBackend::Swaybg => "swaybg", + WallpaperBackend::Swww => "swww", + WallpaperBackend::Wallutils => "wallutils", + WallpaperBackend::Feh => "feh", + }; + backend_combo.set_active_id(Some(backend_id)); backend_combo.connect_changed(|combo| { if let Some(active_id) = combo.active_id() { @@ -458,3 +467,44 @@ pub fn save_last_wallpaper(path: &str) { let _ = fs::write(config_path, contents); } } + +pub fn save_wallpaper_backend(backend: &WallpaperBackend) { + let config_path = shellexpand::tilde(CONFIG_FILE).into_owned(); + if let Ok(mut contents) = fs::read_to_string(&config_path) { + let backend_str = match backend { + WallpaperBackend::Hyprpaper => "hyprpaper", + WallpaperBackend::Swaybg => "swaybg", + WallpaperBackend::Swww => "swww", + WallpaperBackend::Wallutils => "wallutils", + WallpaperBackend::Feh => "feh", + }; + if let Some(line) = contents.lines().find(|line| line.starts_with("backend = ")) { + contents = contents.replace(line, &format!("backend = {}", backend_str)); + } else { + contents.push_str(&format!("\nbackend = {}", backend_str)); + } + let _ = fs::write(config_path, contents); + } +} + +pub fn load_wallpaper_backend() -> Option { + let config_path = shellexpand::tilde(CONFIG_FILE).into_owned(); + fs::File::open(config_path).ok().and_then(|mut file| { + let mut contents = String::new(); + file.read_to_string(&mut contents).ok()?; + contents + .lines() + .find(|line| line.starts_with("backend = ")) + .and_then(|line| { + let backend_str = line.trim_start_matches("backend = "); + match backend_str { + "hyprpaper" => Some(WallpaperBackend::Hyprpaper), + "swaybg" => Some(WallpaperBackend::Swaybg), + "swww" => Some(WallpaperBackend::Swww), + "wallutils" => Some(WallpaperBackend::Wallutils), + "feh" => Some(WallpaperBackend::Feh), + _ => None, + } + }) + }) +} diff --git a/src/main.rs b/src/main.rs index 1ff4383..b787435 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,11 @@ struct Cli { fn main() { let cli = Cli::parse(); + let rt = Runtime::new().expect("Failed to create Tokio runtime"); + let _guard = rt.enter(); + + load_wallpaper_backend(); + if cli.restore { restore_last_wallpaper(); return; @@ -65,13 +70,19 @@ async fn set_wallpaper_internal(path: &str) -> Result<(), String> { println!("Attempting to set wallpaper: {}", path); let backend = *CURRENT_BACKEND.lock(); - match backend { + let result = match backend { WallpaperBackend::Hyprpaper => set_hyprpaper_wallpaper(path).await, WallpaperBackend::Swaybg => set_swaybg_wallpaper(path).await, WallpaperBackend::Swww => set_swww_wallpaper(path).await, WallpaperBackend::Wallutils => set_wallutils_wallpaper(path).await, WallpaperBackend::Feh => set_feh_wallpaper(path).await, + }; + + if result.is_ok() { + gui::save_wallpaper_backend(&backend); } + + result } async fn set_hyprpaper_wallpaper(path: &str) -> Result<(), String> { @@ -234,6 +245,7 @@ pub fn set_wallpaper_backend(backend: WallpaperBackend) { drop_all_wallpapers(previous_backend).await; kill_previous_backend(previous_backend).await; }); + gui::save_wallpaper_backend(&backend); } async fn kill_previous_backend(backend: WallpaperBackend) { @@ -282,3 +294,9 @@ fn restore_last_wallpaper() { eprintln!("No last wallpaper found to restore"); } } + +pub fn load_wallpaper_backend() { + if let Some(backend) = gui::load_wallpaper_backend() { + *CURRENT_BACKEND.lock() = backend; + } +} From 0b26ee77ca91bf3db0c42671a61b567248a979e6 Mon Sep 17 00:00:00 2001 From: nyx Date: Fri, 18 Oct 2024 03:53:21 -0400 Subject: [PATCH 5/7] fix swaybg issues --- src/main.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index b787435..78af4ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,8 +106,19 @@ async fn set_hyprpaper_wallpaper(path: &str) -> Result<(), String> { } async fn set_swaybg_wallpaper(path: &str) -> Result<(), String> { - let command = format!("swaybg -i \"{}\" -m fill", path); - spawn_background_process(&command).await + let command = format!("swaybg -i \"{}\" -m fill &", path); + TokioCommand::new("sh") + .arg("-c") + .arg(&command) + .spawn() + .map_err(|e| format!("Failed to start swaybg: {}", e))?; + + tokio::time::sleep(tokio::time::Duration::from_millis(500)).await; + if is_process_running("swaybg").await { + Ok(()) + } else { + Err("swaybg failed to start or crashed immediately".to_string()) + } } async fn set_swww_wallpaper(path: &str) -> Result<(), String> { From 5170df7872d9d763cbb5790ef0243e8a49cb62f4 Mon Sep 17 00:00:00 2001 From: nyx Date: Fri, 18 Oct 2024 03:55:30 -0400 Subject: [PATCH 6/7] add to md --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 0db7fda..e237d63 100644 --- a/readme.md +++ b/readme.md @@ -16,6 +16,7 @@ An unofficial GUI for setting wallpapers with multiple backends, built with GTK4 - **High capacity** - Hyprwall can handle a large number of wallpapers (over 1000 at one time!) without any issues. - **Multiple monitors** - Hyprwall supports setting wallpapers on **Multiple** monitors at once. - **True async** - Hyprwall is built to be asynchronous, it uses tokio to run commands in this manner massively improving performance. +- **Cli args** - Hyprwall supports command line arguments, to view these type **`hyprwall --help`**, **--restore** is one of them, if you wish you can restore your last used wallpaper in the gui with this argument. - **Supports swaybg, swww, wallutils, feh, and hyprpaper** - Hyprwall supports a variety of wallpaper backends, so you can use it with your preferred wallpaper tool. ## Requirements From 2038b40a4f174118411543fa59f5c3926d79eba1 Mon Sep 17 00:00:00 2001 From: nyx Date: Fri, 18 Oct 2024 04:10:14 -0400 Subject: [PATCH 7/7] use -r for small arg --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 78af4ac..a6d828c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ pub enum WallpaperBackend { #[derive(Parser)] #[command(author, version, about, long_about = None)] struct Cli { - #[arg(long, help = "Restore the last selected wallpaper")] + #[arg(short = 'r', long, help = "Restore the last selected wallpaper")] restore: bool, }