use crate::{
DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult,
types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::path::Path;
use std::process::Command;
use tracing::{debug, info};
#[derive(Debug)]
pub struct OsWallpaperSetDriver;
#[async_trait::async_trait]
impl Driver for OsWallpaperSetDriver {
fn name(&self) -> &str {
"os_wallpaper_set"
}
fn description(&self) -> &str {
"Set the desktop wallpaper"
}
fn usage_hint(&self) -> &str {
"Use this skill to change the desktop wallpaper. Provide a path to an image file."
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![DriverParameter {
name: "path".to_string(),
param_type: "string".to_string(),
description: "Path to the image file (jpg, png, etc.)".to_string(),
required: true,
default: None,
example: Some(Value::String("/home/user/wallpaper.jpg".to_string())),
enum_values: None,
}];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "os_wallpaper_set",
"parameters": {
"path": "/home/user/wallpaper.jpg"
}
}));
}
fn example_output(&self) -> String {
return "Wallpaper set to /home/user/wallpaper.jpg".to_string();
}
fn category(&self) -> DriverCategory {
return DriverCategory::OperatingSystemBasis;
}
async fn execute(
&self,
parameters: &HashMap<String, Value>,
_callback: Option<&dyn DriverCallback>,
_context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing os_wallpaper_set driver");
let path = parameters.get("path").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("path"))?;
if !Path::new(path).exists() {
debug!("File not found: {}", path);
return Err(DriverError::execution(format!("File not found: {}", path)));
}
info!("Setting wallpaper to: {}", path);
set_wallpaper(path)?;
info!("Wallpaper set successfully");
return Ok(format!("Wallpaper set to {}", path));
}
}
#[cfg(target_os = "windows")]
fn set_wallpaper(path: &str) -> DriverResult<()> {
debug!("Setting wallpaper on Windows");
let _ = crate::common::hidden_cmd("powershell")
.args([
"-Command",
&format!("Set-ItemProperty -Path 'HKCU:\\Control Panel\\Desktop' -Name Wallpaper -Value '{}'; RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", path)
])
.output();
return Ok(());
}
#[cfg(target_os = "linux")]
fn set_wallpaper(path: &str) -> DriverResult<()> {
debug!("Setting wallpaper on Linux");
let _ = crate::common::hidden_cmd("gsettings").args(["set", "org.gnome.desktop.background", "picture-uri", &format!("file://{}", path)]).output();
let _ = crate::common::hidden_cmd("gsettings").args(["set", "org.gnome.desktop.background", "picture-uri-dark", &format!("file://{}", path)]).output();
let _ = crate::common::hidden_cmd("feh").args(["--bg-scale", path]).output();
let _ = crate::common::hidden_cmd("nitrogen").args(["--set-scaled", path]).output();
return Ok(());
}
#[cfg(target_os = "macos")]
fn set_wallpaper(path: &str) -> DriverResult<()> {
debug!("Setting wallpaper on macOS");
let _ =
crate::common::hidden_cmd("osascript").args(["-e", &format!("tell application \"Finder\" to set desktop picture to POSIX file \"{}\"", path)]).output();
return Ok(());
}
#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
fn set_wallpaper(_path: &str) -> DriverResult<()> {
debug!("Setting wallpaper not supported on this platform");
return Err(DriverError::execution("Setting wallpaper is not supported on this platform"));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_os_wallpaper_set_metadata() {
let driver = OsWallpaperSetDriver;
assert_eq!(driver.name(), "os_wallpaper_set");
assert_eq!(driver.category(), DriverCategory::OperatingSystemBasis);
}
}