Skip to content

Commit

Permalink
more optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
nnyyxxxx committed Oct 17, 2024
1 parent 356d377 commit ab351f4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 43 deletions.
56 changes: 56 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ edition = "2021"
gtk = { version = "0.9.2", package = "gtk4" }
glib = "0.18.0"
shellexpand = "3.1.0"
lazy_static = "1.4.0"
parking_lot = "0.12.1"

[profile.release]
lto = true
codegen-units = 1
panic = "abort"
strip = true
75 changes: 32 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ mod gui;

use gtk::{prelude::*, Application};
use std::process::Command;
use std::sync::Once;
use lazy_static::lazy_static;
use parking_lot::Mutex;

lazy_static! {
static ref MONITORS: Mutex<Vec<String>> = Mutex::new(Vec::new());
}

static INIT: Once = Once::new();

fn main() {
let app = Application::builder()
Expand All @@ -13,45 +22,27 @@ fn main() {
}

pub fn set_wallpaper(path: &str) {
println!("Attempting to set wallpaper: {}", path);
let monitors = get_monitors();
println!("Found monitors: {:?}", monitors);
INIT.call_once(|| {
*MONITORS.lock() = get_monitors();
});

let preload_command = format!("hyprctl hyprpaper preload \"{}\"", path);
println!("Preloading wallpaper: {}", preload_command);
let preload_output = Command::new("sh")
.arg("-c")
.arg(&preload_command)
.output()
.expect("Failed to execute preload command");

if !preload_output.status.success() {
eprintln!(
"Failed to preload wallpaper: {:?}",
String::from_utf8_lossy(&preload_output.stderr)
);
if !execute_command(&preload_command) {
return;
}

println!("Wallpaper preloaded successfully");

for monitor in monitors {
for monitor in MONITORS.lock().iter() {
let set_command = format!("hyprctl hyprpaper wallpaper \"{},{}\"", monitor, path);
println!("Executing command: {}", set_command);
let output = Command::new("sh")
.arg("-c")
.arg(&set_command)
.output()
.expect("Failed to execute set wallpaper command");
execute_command(&set_command);
}
}

if !output.status.success() {
eprintln!(
"Failed to set wallpaper for {}: {:?}",
monitor,
String::from_utf8_lossy(&output.stderr)
);
} else {
println!("Successfully set wallpaper for {}", monitor);
fn execute_command(command: &str) -> bool {
match Command::new("sh").arg("-c").arg(command).status() {
Ok(status) if status.success() => true,
_ => {
eprintln!("Failed to execute command: {}", command);
false
}
}
}
Expand All @@ -62,16 +53,14 @@ fn get_monitors() -> Vec<String> {
.output()
.expect("Failed to execute hyprctl monitors");

let output_str = String::from_utf8_lossy(&output.stdout);
let mut monitors = Vec::new();

for line in output_str.lines() {
if line.starts_with("Monitor ") {
if let Some(monitor_name) = line.split_whitespace().nth(1) {
monitors.push(monitor_name.to_string());
String::from_utf8_lossy(&output.stdout)
.lines()
.filter_map(|line| {
if line.starts_with("Monitor ") {
line.split_whitespace().nth(1).map(String::from)
} else {
None
}
}
}

monitors
})
.collect()
}

0 comments on commit ab351f4

Please sign in to comment.