finish? lock functionality
This commit is contained in:
parent
652c26c712
commit
f3538fe267
3 changed files with 55 additions and 19 deletions
|
@ -24,11 +24,17 @@ pub struct Programs {
|
||||||
pub command: String,
|
pub command: String,
|
||||||
pub arguments: Vec<String>
|
pub arguments: Vec<String>
|
||||||
}
|
}
|
||||||
|
#[derive(Serialize,Deserialize,Clone,Debug,Default)]
|
||||||
|
pub struct LockConf {
|
||||||
|
pub blur: f32,
|
||||||
|
pub scale: f32,
|
||||||
|
}
|
||||||
#[derive(Serialize,Deserialize,Default)]
|
#[derive(Serialize,Deserialize,Default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub title_length: usize,
|
pub title_length: usize,
|
||||||
pub window_icons: Vec<WindowIcon>,
|
pub window_icons: Vec<WindowIcon>,
|
||||||
pub wallpaper_path: String,
|
pub wallpaper_path: String,
|
||||||
pub programs: HashMap<String, Programs>,
|
pub programs: HashMap<String, Programs>,
|
||||||
|
pub lock: LockConf,
|
||||||
pub profiles: HashMap<String, Profile>
|
pub profiles: HashMap<String, Profile>
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{env::consts, fs::{self, exists}, io, ops::Sub, path::{Path, PathBuf}};
|
use std::{env::consts, fs::{self, exists, DirBuilder}, io, ops::Sub, path::{Path, PathBuf}};
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
use image::imageops::GaussianBlurParameters;
|
use image::{imageops::GaussianBlurParameters, DynamicImage};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use sha2::{Sha256, Digest};
|
use sha2::{Sha256, Digest};
|
||||||
|
@ -9,7 +9,7 @@ use swayipc::Connection;
|
||||||
use xdg::BaseDirectories;
|
use xdg::BaseDirectories;
|
||||||
use shellexpand::tilde;
|
use shellexpand::tilde;
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::{Config, LockConf};
|
||||||
|
|
||||||
#[derive(Serialize,Deserialize,Debug)]
|
#[derive(Serialize,Deserialize,Debug)]
|
||||||
pub struct WallpaperCache {
|
pub struct WallpaperCache {
|
||||||
|
@ -17,11 +17,33 @@ pub struct WallpaperCache {
|
||||||
pub hash: String
|
pub hash: String
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lock_screen(config: &Config, mut sway_connection: Connection) {
|
pub fn generate_image(orig_img_data: DynamicImage,gen_image_path: PathBuf, lock_conf: LockConf) {// Result<(), ImageError> {
|
||||||
|
//let img_data = image::open(image_path).unwrap();
|
||||||
|
let w = orig_img_data.width() as f32 * lock_conf.scale;
|
||||||
|
let h = orig_img_data.height() as f32 * lock_conf.scale;
|
||||||
|
let blurred_img_data = orig_img_data
|
||||||
|
.resize(w as u32, h as u32, image::imageops::FilterType::Nearest)
|
||||||
|
.blur_advanced(GaussianBlurParameters::new_from_sigma(lock_conf.blur));
|
||||||
|
//let gen_image_path = cache_dir.clone().unwrap().join(format!("sway-profiles-rs/lock/{}.jpg",output.name));
|
||||||
|
match blurred_img_data.save(gen_image_path) {
|
||||||
|
Ok(_) => println!("Image saved successfully"),
|
||||||
|
Err(e) => println!("error: {:?}",e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lock_screen(config: &Config, mut sway_connection: Connection, force_bg_render: bool) -> Result<Vec<Result<(), swayipc::Error>>, swayipc::Error> {
|
||||||
let wallpaper_root_path = tilde(&config.wallpaper_path).to_string();
|
let wallpaper_root_path = tilde(&config.wallpaper_path).to_string();
|
||||||
let wallpaper_root_path = Path::new(&wallpaper_root_path);
|
let wallpaper_root_path = Path::new(&wallpaper_root_path);
|
||||||
let cache_dir = BaseDirectories::with_prefix("sway-profiles-rs").cache_home;
|
let cache_dir = BaseDirectories::with_prefix("sway-profiles-rs").cache_home;
|
||||||
let wp_cache_dir = cache_dir.clone().unwrap().join("sway-profiles-rs/lock/");
|
let wp_cache_dir = cache_dir.clone().unwrap().join("sway-profiles-rs/lock/");
|
||||||
|
match DirBuilder::new().recursive(true).create(&wp_cache_dir) {
|
||||||
|
Ok(o) => {
|
||||||
|
println!("created directory ({:?})",o)
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
println!("error: {:?}",e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut wp_hash_json: Vec<WallpaperCache> = vec![];
|
let mut wp_hash_json: Vec<WallpaperCache> = vec![];
|
||||||
let hashes_json_path = wp_cache_dir.join("hashes.json");
|
let hashes_json_path = wp_cache_dir.join("hashes.json");
|
||||||
|
@ -43,16 +65,19 @@ pub fn lock_screen(config: &Config, mut sway_connection: Connection) {
|
||||||
let hash = format!("{:x}",sha256.finalize());
|
let hash = format!("{:x}",sha256.finalize());
|
||||||
wp_hash_array.push(WallpaperCache { display: output.name.clone(), hash: hash.clone() });
|
wp_hash_array.push(WallpaperCache { display: output.name.clone(), hash: hash.clone() });
|
||||||
|
|
||||||
if let Some(output_saved_hash) = wp_hash_json.iter().find(|x|x.display == output.name) && output_saved_hash.hash.ne(&hash) {
|
// if hashes_json_path.exists() {
|
||||||
let img_data = image::open(image_path).unwrap();
|
let wallpaper_data = image::open(&image_path).unwrap();
|
||||||
let blurred_img_data = img_data
|
let wallpaper_cache_path = cache_dir.as_ref().unwrap().join(format!("sway-profiles-rs/lock/{}.jpg",output.name));
|
||||||
.resize((img_data.width() as f64 * 0.75) as u32, (img_data.height() as f64 * 0.75) as u32, image::imageops::FilterType::Nearest)
|
match wp_hash_json.iter().find(|x|x.display == output.name) {
|
||||||
.blur_advanced(GaussianBlurParameters::new_from_sigma(10.0));
|
Some (saved_hash) => {
|
||||||
let blurred_image_path = cache_dir.clone().unwrap().join(format!("sway-profiles-rs/lock/{}.jpg",output.name));
|
if !(saved_hash.hash == hash && wallpaper_cache_path.exists()) || force_bg_render {
|
||||||
let _ = blurred_img_data.save(blurred_image_path);
|
generate_image(wallpaper_data,wallpaper_cache_path, config.lock.clone())
|
||||||
|
};
|
||||||
|
},
|
||||||
|
None => generate_image(wallpaper_data, wallpaper_cache_path, config.lock.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let gtklock_modules_dir = "/usr/lib/".to_owned() + consts::ARCH + "-linux-gnu/gtklock/";
|
let gtklock_modules_dir = format!("/usr/lib/{}-linux-gnu/gtklock/",consts::ARCH);//.to_owned() + consts::ARCH + "-linux-gnu/gtklock/";
|
||||||
let gktlock_modules_path = Path::new(>klock_modules_dir);
|
let gktlock_modules_path = Path::new(>klock_modules_dir);
|
||||||
let mut gtklock_args: Vec<String> = vec![];
|
let mut gtklock_args: Vec<String> = vec![];
|
||||||
for file in WalkDir::new(gktlock_modules_path).into_iter() {//.find(|x|x.as_ref().unwrap().file_name().to_str().unwrap().ends_with("-module.so")) {
|
for file in WalkDir::new(gktlock_modules_path).into_iter() {//.find(|x|x.as_ref().unwrap().file_name().to_str().unwrap().ends_with("-module.so")) {
|
||||||
|
@ -64,10 +89,11 @@ pub fn lock_screen(config: &Config, mut sway_connection: Connection) {
|
||||||
}
|
}
|
||||||
let new_json = json!(wp_hash_array);
|
let new_json = json!(wp_hash_array);
|
||||||
let _ = std::fs::write::<PathBuf, String>(hashes_json_path, new_json.to_string());
|
let _ = std::fs::write::<PathBuf, String>(hashes_json_path, new_json.to_string());
|
||||||
let mut gtklock_command = "exec gtklock".to_owned();
|
let mut gtklock_command = "exec echo gtklock".to_owned();
|
||||||
for a in gtklock_args {
|
for a in gtklock_args {
|
||||||
gtklock_command = gtklock_command + " " + &a;
|
gtklock_command = gtklock_command + " " + &a;
|
||||||
}
|
}
|
||||||
println!("{:?}",gtklock_command);
|
println!("{:?}",gtklock_command);
|
||||||
let _ = sway_connection.run_command(gtklock_command);
|
// let _ = sway_connection.run_command(gtklock_command);
|
||||||
|
sway_connection.run_command(gtklock_command)
|
||||||
}
|
}
|
14
src/main.rs
14
src/main.rs
|
@ -1,7 +1,5 @@
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
use clap::{Parser,Subcommand,ArgAction};
|
use clap::{Parser,Subcommand,ArgAction};
|
||||||
use swayipc::{Connection, Event, EventType, Fallible};
|
use swayipc::{Connection, Event, EventType, Fallible};
|
||||||
|
|
||||||
|
@ -54,7 +52,10 @@ enum Commands {
|
||||||
program: String
|
program: String
|
||||||
},
|
},
|
||||||
/// Set up blurred wallpaper for screen lock, and load gtklock modules
|
/// Set up blurred wallpaper for screen lock, and load gtklock modules
|
||||||
Lock,
|
Lock {
|
||||||
|
#[arg(short, long, action = ArgAction::SetTrue)]
|
||||||
|
force_render_background: Option<bool>,
|
||||||
|
},
|
||||||
//Rename,
|
//Rename,
|
||||||
Profile,
|
Profile,
|
||||||
Shortcuts {
|
Shortcuts {
|
||||||
|
@ -110,8 +111,11 @@ fn main() -> Fallible<()> {
|
||||||
let _ = sway_connection.run_command(swaymsg_command);
|
let _ = sway_connection.run_command(swaymsg_command);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Commands::Lock => {
|
Commands::Lock { force_render_background } => {
|
||||||
lock_screen(&config, sway_connection);
|
match lock_screen(&config, sway_connection, force_render_background.unwrap()) {
|
||||||
|
Ok(o) => println!("Screen locked successfully{:?}",o.first().unwrap()),
|
||||||
|
Err(e) => println!("{:?}",e),
|
||||||
|
};
|
||||||
},
|
},
|
||||||
//Commands::Rename => todo!(),
|
//Commands::Rename => todo!(),
|
||||||
Commands::Profile => {
|
Commands::Profile => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue