Skip to content

Commit

Permalink
Merge pull request #2 from Dan0xE/windows_support
Browse files Browse the repository at this point in the history
Add Windows Support
  • Loading branch information
Colonial-Dev committed Sep 2, 2023
2 parents 3f3f931 + 5e63878 commit d7d4299
Showing 1 changed file with 70 additions and 23 deletions.
93 changes: 70 additions & 23 deletions src/wallpaper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::path::Path;
use anyhow::{Context, Result};
use std::env;

use anyhow::{Result, Context};
use std::path::Path;
use tokio::process::Command;

pub async fn set(path: impl AsRef<Path>, user_command: Option<&str>) -> Result<()> {
Expand All @@ -10,19 +9,28 @@ pub async fn set(path: impl AsRef<Path>, user_command: Option<&str>) -> Result<(
.to_str()
.context("Failed to convert wallpaper path to a UTF-8 string")?;

let desktop = env::var("XDG_CURRENT_DESKTOP")
.context("Failed to get XDG_CURRENT_DESKTOP environment variable")?;

log::debug!("XDG_CURRENT_DESKTOP is {desktop}.");
let os = env::consts::OS;

log::debug!("Setting wallpaper to image at path {path}.");

match user_command {
Some(command) => set_userdefined(path, command).await?,
None => match desktop.as_str() {
"GNOME" => set_gnome(path).await?,
"KDE" => set_kde(path).await?,
_ => panic!("Desktop {desktop} is not supported."),
},
match os {
"linux" => {
let desktop = env::var("XDG_CURRENT_DESKTOP")
.context("Failed to get XDG_CURRENT_DESKTOP environment variable")?;

match user_command {
Some(command) => set_userdefined(path, command).await?,
None => match desktop.as_str() {
"GNOME" => set_gnome(path).await?,
"KDE" => set_kde(path).await?,
_ => panic!("Desktop {desktop} is not supported."),
},
}
}
"windows" => {
set_windows(path).await?;
}
_ => panic!("Operating system not supported."),
}

Ok(())
Expand All @@ -40,18 +48,14 @@ async fn set_userdefined(path: &str, command: &str) -> Result<()> {

async fn set_gnome(path: &str) -> Result<()> {
let color_scheme = Command::new("gsettings")
.args([
"get",
"org.gnome.desktop.interface",
"color-scheme"
])
.args(["get", "org.gnome.desktop.interface", "color-scheme"])
.output()
.await
.context("Failed to get preferred color scheme from GSettings")?;

let uri = match String::from_utf8(color_scheme.stdout)?.trim() {
"'prefer-dark'" => "picture-uri-dark",
_ => "picture-uri"
_ => "picture-uri",
};

Command::new("gsettings")
Expand All @@ -68,14 +72,56 @@ async fn set_gnome(path: &str) -> Result<()> {
Ok(())
}

async fn set_windows(path: &str) -> Result<()> {
// From https://c-nergy.be/blog/?p=15291
//! IMPORTANT - DO NOT CHANGE THE FORMATTING OF THE POWERSHELL SCRIPT as this will BREAK the script. [more info: https://github.com/PowerShell/PowerShell/issues/2337]
let powershell_script = format!(
r#"
$code = @'
using System.Runtime.InteropServices;
namespace Win32 {{
public class Wallpaper {{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
public static void SetWallpaper(string thePath) {{
SystemParametersInfo(20, 0, thePath, 3);
}}
}}
}}
'@
add-type $code
# Apply the Change on the system
[Win32.Wallpaper]::SetWallpaper("{}")"#,
path
);

Command::new("powershell")
.args([
"-ExecutionPolicy",
"Bypass",
"-NoProfile",
"-Command",
&powershell_script,
])
.output()
.await
.context("PowerShell failed to update wallpaper")?;

Ok(())
}

async fn set_kde(path: &str) -> Result<()> {
// From https://superuser.com/questions/488232
Command::new("qdbus")
.args([
"org.kde.plasmashell",
"/PlasmaShell",
"org.kde.PlasmaShell.evaluateScript",
&format!(r#"'
&format!(
r#"'
var allDesktops = desktops();
print (allDesktops);
for (i=0;i<allDesktops.length;i++) {{
Expand All @@ -86,11 +132,12 @@ async fn set_kde(path: &str) -> Result<()> {
"General");
d.writeConfig("Image", "file://{path}")
}}
'"#)
'"#
),
])
.output()
.await
.context("Failed to set wallpaper with qdbus")?;

Ok(())
}
}

0 comments on commit d7d4299

Please sign in to comment.