Skip to content

Commit

Permalink
Remove wpaperd, replace with swaybg command and config output.
Browse files Browse the repository at this point in the history
  • Loading branch information
fyodordev committed May 6, 2023
1 parent aacbe1b commit f7b8101
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 192 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "rwpspread"
name = "swaybg-spread"
version = "0.1.1"
edition = "2021"

Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod parser;
mod splitter;
mod outputs;
mod wpaperd;

use std::process;
use colored::Colorize;
Expand Down
24 changes: 15 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,29 @@ struct Args {
#[arg(short, long)]
image: String,

/// Use wpaperd integration
#[arg(short, long)]
wpaperd: bool,

/// Force Resplit even if cache exists
#[arg(short, long)]
force_resplit: bool,

/// Don't downscale base image, even if it's bigger than needed
#[arg(short, long)]
dont_downscale: bool,
no_downscale: bool,

/// Don't downscale base image, even if it's bigger than needed
#[arg(short, long)]
dont_set: bool,

/// Don't downscale base image, even if it's bigger than needed
#[arg(short, long)]
silent: bool,
}

pub struct Config {
pub image_path: PathBuf,
pub with_wpaperd: bool,
pub force_resplit: bool,
pub dont_downscale: bool,
pub no_downscale: bool,
pub dont_set: bool,
pub silent: bool,
}

impl Config {
Expand All @@ -46,9 +51,10 @@ impl Config {
// construct
Ok(Self {
image_path: in_path,
with_wpaperd: args.wpaperd,
force_resplit: args.force_resplit,
dont_downscale: args.dont_downscale
no_downscale: args.no_downscale,
dont_set: args.dont_set,
silent: args.silent,
})
}

Expand Down
120 changes: 59 additions & 61 deletions src/splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use glob::glob;
use std::fs::remove_file;
use std::path::Path;
use crate::Config;
use crate::wpaperd::{WpaperdConfig, cmd_wrapper};
use crate::outputs::Monitor;
use std::process::{Command, Stdio};

pub struct ResultPaper {
pub monitor_name: String,
Expand Down Expand Up @@ -50,77 +50,45 @@ impl Splitter {
config
);

// check if we need to generate wpaperd config
if config.with_wpaperd {
// create new wpaperd instance
let wpaperd = WpaperdConfig::new(
format!(
"{}/.config/wpaperd/wallpaper.toml",
var("HOME").unwrap()
),
self.hash.clone()
);

// check caches
let caches_present = self.check_caches();
// check caches
let caches_present = self.check_caches();
// TODO: self.result_papers not being set if cache exists.

// do we need to resplit
if
config.force_resplit ||
! caches_present
{
// cleanup caches first
self.cleanup_cache();

// we need to resplit
self.result_papers = self.perform_split(
img,
config,
format!("{}/.cache/",var("HOME").unwrap())
).map_err(
|err| err.to_string()
)?;
}

//check wpaper config hash
let wpaperd_present = wpaperd.check_existing().map_err(
|err| err.to_string()
)?;

// do we need to rebuild config
// also always rebuild when force resplit was set
if
config.force_resplit ||
! wpaperd_present
{
// yes we do
wpaperd.build(&self.result_papers).map_err(
|err| err.to_string()
)?;
}

// finally, run wrapper
cmd_wrapper().map_err(
|err| err.to_string()
)?;
// do we need to resplit
if
true
// config.force_resplit ||
// ! caches_present
{
// cleanup caches first
self.cleanup_cache();

// no wpaperd to worry about, just split
} else {
// just split
// we need to resplit
self.result_papers = self.perform_split(
img,
config,
format!("{}/",var("PWD").unwrap())
format!("{}/.cache/",var("HOME").unwrap())
).map_err(
|err| err.to_string()
)?;
}

// return
for monitor in &self.result_papers {
if (!config.silent) {
// Output in sway config style.
println!("output {} bg {} fill", monitor.monitor_name.to_string(), monitor.image_full_path.to_string());
}
if (!config.dont_set) {
// Set background for this monitor.
cmd_setbg(&monitor)?;
}
}

Ok(())
}

// do the actual splitting
// TODO: Very slow for some reason (especially resizing and saving), try to optimize.
fn perform_split(&self, mut img: DynamicImage, config: &Config, save_path: String) -> Result<Vec<ResultPaper>, String> {
/*
Calculate Overall Size
Expand All @@ -147,7 +115,7 @@ impl Splitter {
// either if user doesn't deny
// or if image is too small
if
config.dont_downscale == false
config.no_downscale == false
|| img.dimensions().0 < overall_width
|| img.dimensions().1 < overall_height
{
Expand Down Expand Up @@ -178,7 +146,7 @@ impl Splitter {
),
image: cropped_image
}
)
);
}

// save our result images
Expand Down Expand Up @@ -208,7 +176,7 @@ impl Splitter {
format!(
"# {:?}{:?}{:?}\n",
img_hash,
compute(config.dont_downscale.to_string()),
compute(config.no_downscale.to_string()),
compute(hash_string.as_bytes())
)
}
Expand All @@ -230,6 +198,7 @@ impl Splitter {
}

fn check_caches(&self) -> bool {

// what we search for
let base_format = format!(
"{}/.cache/rwps_{}",
Expand All @@ -255,3 +224,32 @@ impl Splitter {
true
}
}


fn cmd_setbg(wallpaper: &ResultPaper) -> Result<(), String> {

let sway_socket = var("SWAYSOCK")
.map_err(|_| "MY_VAR environment variable not set".to_string())?;

Command::new("swaymsg")
.arg("-s")
.arg(&sway_socket)
.arg("output")
.arg(&wallpaper.monitor_name)
.arg("bg")
.arg(&wallpaper.image_full_path)
.arg("fill")
.stdout(Stdio::null())
.status()
//.and_then(|exit_status| exit_status.exit_ok().map_err(|e| e.to_string()))
//.map_err(|err| err.to_string())
.map_or_else(
|err| Err(err.to_string()),
|exit_status| if exit_status.success() {
Ok(())
} else {
Err("Exit with error".to_string())
}
)
}

106 changes: 0 additions & 106 deletions src/wpaperd.rs

This file was deleted.

0 comments on commit f7b8101

Please sign in to comment.