Skip to content

Commit

Permalink
Add Virtual Desktop support (#4)
Browse files Browse the repository at this point in the history
Resolves: #3

Signed-off-by: Thibault Meyer <meyer.thibault@gmail.com>
  • Loading branch information
thibaultmeyer authored Jan 24, 2024
1 parent fdfadac commit 8c2ca74
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ winit = "0.28.7"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["wincon", "winuser"] }
winver = "1.0.0"
winvd = "0.0.45"
43 changes: 39 additions & 4 deletions src/bingwallpaper/bingwallpaperchanger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ use std::process::Command;
use std::time::SystemTime;

use chrono::{DateTime, Utc};

#[cfg(target_os = "windows")]
use winapi::ctypes::c_void;
#[cfg(target_os = "windows")]
use winapi::um::winuser;
#[cfg(target_os = "windows")]
use winvd::{get_desktop_count, get_desktops};
#[cfg(target_os = "windows")]
use winver::WindowsVersion;

use crate::bingwallpaper::{BingAPIClient, BingWallpaperConfiguration};

Expand Down Expand Up @@ -202,15 +205,47 @@ impl BingWallpaperChanger {
/// Changes the wallpaper with the given picture on Windows.
#[cfg(target_os = "windows")]
fn change_wallpaper_windows(&self) {
let image_path = CString::new(String::from(&self.configuration.target_filename)).unwrap();
let win_version = WindowsVersion::detect().unwrap();

if win_version >= WindowsVersion::new(10, 0, 22621) && get_desktop_count().unwrap() > 1 {
if let Err(error) = self.change_wallpaper_windows_virtualdesktop() {
println!("Something goes wrong with Virtual Desktop API. Fallback to legacy Windows API\n{:?}", error);
self.change_wallpaper_windows_winuser();
}
} else {
self.change_wallpaper_windows_winuser();
}
}

/// Changes the wallpaper with the given picture on Windows using the Virtual Desktop API.
#[cfg(target_os = "windows")]
fn change_wallpaper_windows_virtualdesktop(&self) -> Result<(), String> {
if let Ok(detected_desktops) = get_desktops() {
for desktop in detected_desktops {
if let Err(error) = desktop.set_wallpaper(&self.configuration.target_filename) {
return Err(format!(
"Can't change Virtual Desktop wallpaper for #{:?}\n{:?}",
desktop.get_id().unwrap(),
error));
}
}

Ok(())
} else {
Err("Can't detect Virtual Desktop ".to_string())
}
}

/// Changes the wallpaper with the given picture on Windows using the legacy Windows API.
#[cfg(target_os = "windows")]
fn change_wallpaper_windows_winuser(&self) {
let image_path = CString::new(String::from(&self.configuration.target_filename)).unwrap();
unsafe {
winuser::SystemParametersInfoA(
winuser::SPI_SETDESKWALLPAPER,
0,
image_path.as_ptr() as *mut c_void,
winuser::SPIF_UPDATEINIFILE,
);
winuser::SPIF_UPDATEINIFILE);
}
}
}

0 comments on commit 8c2ca74

Please sign in to comment.