feat(wallpaper): detect wpaperd wallpapers from state links

wpaperd mirrors the current wallpaper of every output as a symlink
under $XDG_STATE_HOME/wpaperd/wallpapers, rewritten on each change.
Identify the wpaperd process in the runtime provider scan and resolve
the newest link, joining relative targets with $HOME.
This commit is contained in:
Muush authored and Xuc Pan committed 2026-09-24 13:13:12 +08:00
1 parent bb6476a864
commit 3dc3b62477
1 file changed
+41 -4
+41 -4
View File
@@ -276,12 +276,13 @@ fn detect_runtime_provider() -> Option<PathBuf> {
.and_then(OsStr::to_str)
.unwrap_or_default();
let candidate = match executable {
"swaybg" => argument_after(&arguments, "-i"),
"xwallpaper" => arguments.last().cloned(),
"mpvpaper" => arguments.last().cloned(),
"swaybg" => argument_after(&arguments, "-i").map(PathBuf::from),
"xwallpaper" => arguments.last().map(PathBuf::from),
"mpvpaper" => arguments.last().map(PathBuf::from),
"wpaperd" => wpaperd_link(),
_ => None,
};
if let Some(path) = candidate.and_then(|value| resolve_candidate(PathBuf::from(value))) {
if let Some(path) = candidate.and_then(resolve_candidate) {
return Some(path);
}
}
@@ -303,6 +304,36 @@ fn argument_after(arguments: &[String], option: &str) -> Option<String> {
.map(|pair| pair[1].clone())
}
/// wpaperd >= 1.1.0 mirrors the current wallpaper of every output as a
/// symlink under `$XDG_STATE_HOME/wpaperd/wallpapers`, rewritten on each
/// change. The newest link wins; leftovers from earlier runs are older.
fn wpaperd_link() -> Option<PathBuf> {
std::fs::read_dir(state_home().join("wpaperd/wallpapers"))
.ok()?
.flatten()
.filter_map(|entry| {
let target = link_target(&entry.path())?;
let modified = std::fs::symlink_metadata(entry.path())
.ok()?
.modified()
.ok()?;
Some((modified, target))
})
.max_by_key(|(modified, _)| *modified)
.map(|(_, target)| target)
}
fn link_target(link: &Path) -> Option<PathBuf> {
let target = std::fs::read_link(link).ok()?;
Some(if target.is_absolute() {
target
} else {
// wpaperd stores config-relative targets, resolved against its
// working directory, which is the user's home in practice.
home_dir().join(target)
})
}
fn wallpaper_from_value(value: &str, provider: &'static str) -> Option<Wallpaper> {
let value = value.trim().trim_matches(['\'', '"']);
if value.is_empty() {
@@ -545,6 +576,12 @@ fn config_home() -> PathBuf {
.unwrap_or_else(|| home_dir().join(".config"))
}
fn state_home() -> PathBuf {
std::env::var_os("XDG_STATE_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| home_dir().join(".local/state"))
}
fn home_dir() -> PathBuf {
std::env::var_os("HOME")
.map(PathBuf::from)