Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Dec 2, 2023
1 parent db851d5 commit d995d9a
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 48 deletions.
15 changes: 6 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ extern crate pretty_env_logger;
#[macro_use]
extern crate paris_log;

mod wallpaper;
mod reload;

mod util;
use crate::util::{
arguments::Cli,
Expand All @@ -28,12 +31,6 @@ use update_informer::{registry, Check};

use crate::util::arguments::Source;

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
use util::{reload::reload_apps_linux, wallpaper::set_wallaper};

#[cfg(target_os = "windows")]
use util::wallpaper::set_wallaper_windows;

pub struct Schemes {
pub light: Scheme,
pub dark: Scheme,
Expand Down Expand Up @@ -90,7 +87,7 @@ fn main() -> Result<(), Report> {

if config.config.reload_apps == Some(true) {
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
reload_apps_linux(&args, &config)?;
reload::unix::reload(&args, &config)?;
}

if config.config.set_wallpaper == Some(true) {
Expand All @@ -100,10 +97,10 @@ fn main() -> Result<(), Report> {
};

#[cfg(target_os = "windows")]
set_wallaper_windows(&path)?;
wallpaper::windows::set(&path)?;

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
set_wallaper(&config, &path)?;
wallpaper::unix::set(&config, &path)?;
}

if let Some(commands) = &config.config.run_after {
Expand Down
2 changes: 2 additions & 0 deletions src/reload/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
pub mod unix;
70 changes: 70 additions & 0 deletions src/reload/unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::util::{arguments::Cli, config::ConfigFile};
use color_eyre::{eyre::Result, Report};

use std::process::Command;

use crate::SchemesEnum;

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
pub fn reload(args: &Cli, config: &ConfigFile) -> Result<(), Report> {
if config.config.reload_apps_list.is_none() {
warn!("<d>The option <yellow><u>config.reload_apps<b></><d> is set to <u><green>TRUE</>, but <yellow><u>config.reload_apps_list</><d> is <u><red>EMPTY</>. Not restarting any apps...</>");
return Ok(());
}

let reload_apps_list = &config.config.reload_apps_list.as_ref().unwrap();

if reload_apps_list.waybar == Some(true) || reload_apps_list.waybar.is_none() {
reload_app("waybar", "SIGUSR2")?;
}

if reload_apps_list.kitty == Some(true) || reload_apps_list.waybar.is_none() {
reload_app("kitty", "SIGUSR1")?;
}

if reload_apps_list.dunst == Some(true) || reload_apps_list.waybar.is_none() {
reload_app("dunst", "SIGUSR2")?;
}

if reload_apps_list.gtk_theme == Some(true) || reload_apps_list.waybar.is_none() {
reload_gtk_theme(args)?;
}

Ok(())
}

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
pub fn reload_app(name: &str, signal: &str) -> Result<(), Report> {
info!("Restarting {}", name);
let mut kill = Command::new("pkill");
kill.arg(format!("-{}", signal));
kill.arg(name);

kill.spawn()?;
Ok(())
}

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
fn reload_gtk_theme(args: &Cli) -> Result<(), Report> {
let mode = match args.mode {
Some(SchemesEnum::Light) => "light",
Some(SchemesEnum::Dark) => "dark",
Some(SchemesEnum::Amoled) => "dark",
None => "dark",
};

info!("Setting gtk theme to adw-gtk3-{}", mode);

set_theme("")?;
set_theme(format!("adw-gtk3-{}", mode).as_str())?;
Ok(())
}

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
fn set_theme(theme: &str) -> Result<(), Report> {
Command::new("gsettings")
.args(["set", "org.gnome.desktop.interface", "gtk-theme", theme])
.spawn()?;

Ok(())
}
1 change: 0 additions & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ pub mod config;
pub mod image;
pub mod reload;
pub mod template;
pub mod wallpaper;
5 changes: 5 additions & 0 deletions src/wallpaper/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
pub mod unix;

#[cfg(target_os = "windows")]
pub mod windows;
41 changes: 3 additions & 38 deletions src/util/wallpaper.rs → src/wallpaper/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,14 @@ use color_eyre::Report;
use std::process::Command;
use std::process::Stdio;

use super::{
use crate::util::{
arguments::{Cli, Source},
config::{ConfigFile, WallpaperTool},
reload::reload_app,
};

#[cfg(target_os = "windows")]
use std::ffi::OsStr;
use std::io;
use std::iter;
use std::mem;
use std::os::windows::ffi::OsStrExt;
use winapi::ctypes::c_void;
use winapi::um::winuser::SystemParametersInfoW;
use winapi::um::winuser::SPIF_SENDCHANGE;
use winapi::um::winuser::SPIF_UPDATEINIFILE;
use winapi::um::winuser::SPI_SETDESKWALLPAPER;

#[cfg(target_os = "windows")]
pub fn set_wallaper_windows(path: &String) -> Result<(), Report> {
unsafe {
let path = OsStr::new(path)
.encode_wide()
// append null byte
.chain(iter::once(0))
.collect::<Vec<u16>>();
let successful = SystemParametersInfoW(
SPI_SETDESKWALLPAPER,
0,
path.as_ptr() as *mut c_void,
SPIF_UPDATEINIFILE | SPIF_SENDCHANGE,
) == 1;

if successful {
Ok(())
} else {
Err(io::Error::last_os_error().into())
}
}
}

#[cfg(any(target_os = "linux", target_os = "netbsd"))]
pub fn set_wallaper(config: &ConfigFile, path: &String) -> Result<(), Report> {
pub fn set(config: &ConfigFile, path: &String) -> Result<(), Report> {
let wallpaper_tool = match &config.config.wallpaper_tool {
Some(wallpaper_tool) => wallpaper_tool,
None => {
Expand Down Expand Up @@ -119,4 +84,4 @@ fn set_wallaper_feh(config: &ConfigFile, path: &String) -> Result<(), Report> {

cmd.spawn()?;
Ok(())
}
}
36 changes: 36 additions & 0 deletions src/wallpaper/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use color_eyre::Report;

#[cfg(target_os = "windows")]
use std::ffi::OsStr;
use std::io;
use std::iter;
use std::mem;
use std::os::windows::ffi::OsStrExt;
use winapi::ctypes::c_void;
use winapi::um::winuser::SystemParametersInfoW;
use winapi::um::winuser::SPIF_SENDCHANGE;
use winapi::um::winuser::SPIF_UPDATEINIFILE;
use winapi::um::winuser::SPI_SETDESKWALLPAPER;

#[cfg(target_os = "windows")]
pub fn set(path: &String) -> Result<(), Report> {
unsafe {
let path = OsStr::new(path)
.encode_wide()
// append null byte
.chain(iter::once(0))
.collect::<Vec<u16>>();
let successful = SystemParametersInfoW(
SPI_SETDESKWALLPAPER,
0,
path.as_ptr() as *mut c_void,
SPIF_UPDATEINIFILE | SPIF_SENDCHANGE,
) == 1;

if successful {
Ok(())
} else {
Err(io::Error::last_os_error().into())
}
}
}

0 comments on commit d995d9a

Please sign in to comment.