Support animated GIF wallpapers via awww

swaybg only ever renders one frame, so an animated GIF (detected by its
magic bytes, not the .gif extension - the active wallpaper file is
always literally named wallpaper.png regardless of real format) now
goes through awww instead, which was already installed. Found that
awww img's --transition-type none - the obvious flag for an instant
switch - silently kills the render loop that also drives GIF frame
advancement; --transition-step 255 gives the same instant switch
without that side effect.

New `wallpaper.py start` picks the right backend for whatever's
currently active and replaces the hardcoded swaybg autostart line in
hyprland.lua, so a GIF wallpaper survives a restart correctly too.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dujsk9jYfxUVLtb5B8eC94
This commit is contained in:
nico
2026-09-29 01:33:03 +02:00
parent 823277f535
commit 15f5da4509
5 changed files with 60 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""Manages the desktop wallpaper (~/.config/hypr/wallpaper.png, loaded by swaybg).
"""Manages the desktop wallpaper (~/.config/hypr/wallpaper.png, loaded by swaybg/awww).
wallpaper.py list JSON array of previously used wallpapers (newest first),
each {id, path, added, current}; "path" is directly usable
@@ -7,9 +7,19 @@
wallpaper.py apply <path> archive the current wallpaper, make <path> the new one
wallpaper.py pick open a native file picker, then apply the chosen file
wallpaper.py remove <id> delete one archived wallpaper (refuses the current one)
wallpaper.py start (re)start whichever daemon the current wallpaper needs,
without touching it - called on Hyprland startup
The picker button in the ThemeWidget calls "list" to fill its gallery of recommendations,
"pick" for "choose a new file", and "apply" again when a gallery entry is clicked.
Static images go through swaybg, same as always. An animated GIF needs an actual
compositing daemon instead - swaybg only ever shows one frame - so those go through
awww (already installed; a swww-like wlroots wallpaper daemon that also does gifs).
Which one applies is decided by sniffing the file's magic bytes, not its extension:
ACTIVE is always literally named wallpaper.png regardless of the real format inside
(pre-existing, swaybg/Qt image loading both sniff content anyway), so a fixed ".gif"
check on that path would never match.
"""
import hashlib
import json
@@ -24,6 +34,14 @@ ARCHIVE = os.path.expanduser("~/.local/share/quickshell/wallpapers")
MANIFEST = os.path.join(ARCHIVE, "manifest.json")
def is_gif(path):
try:
with open(path, "rb") as f:
return f.read(6) in (b"GIF87a", b"GIF89a")
except OSError:
return False
def load_manifest():
try:
return json.load(open(MANIFEST))
@@ -53,18 +71,35 @@ def archive_current(entries):
return
os.makedirs(ARCHIVE, exist_ok=True)
next_id = max([e["id"] for e in entries], default=0) + 1
ext = os.path.splitext(ACTIVE)[1] or ".png"
ext = ".gif" if is_gif(ACTIVE) else (os.path.splitext(ACTIVE)[1] or ".png")
dest = os.path.join(ARCHIVE, f"{next_id:04d}{ext}")
shutil.copy2(ACTIVE, dest)
entries.append({"id": next_id, "file": os.path.basename(dest), "sha256": digest,
"added": time.strftime("%Y-%m-%d %H:%M")})
def restart_swaybg():
subprocess.run(["pkill", "-x", "swaybg"], check=False, capture_output=True)
subprocess.Popen(["swaybg", "-i", ACTIVE, "-m", "fill"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
start_new_session=True)
def restart_wallpaper_daemon():
if is_gif(ACTIVE):
subprocess.run(["pkill", "-x", "swaybg"], check=False, capture_output=True)
if subprocess.run(["awww", "query"], capture_output=True, timeout=2).returncode != 0:
subprocess.Popen(["awww-daemon"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
start_new_session=True)
for _ in range(20): # wait for the daemon's socket, up to ~2s
time.sleep(0.1)
if subprocess.run(["awww", "query"], capture_output=True, timeout=2).returncode == 0:
break
time.sleep(0.3) # query succeeding doesn't mean it's ready for `img` yet
# --transition-step 255 for an instant switch, same end result as
# --transition-type none - but "none" takes a code path in awww that also kills
# the render loop driving GIF frame advancement, leaving it stuck on one frame.
subprocess.run(["awww", "img", ACTIVE, "--resize", "crop", "--transition-step", "255"],
check=False, capture_output=True, timeout=10)
else:
subprocess.run(["pkill", "-x", "awww-daemon"], check=False, capture_output=True)
subprocess.run(["pkill", "-x", "swaybg"], check=False, capture_output=True)
subprocess.Popen(["swaybg", "-i", ACTIVE, "-m", "fill"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
start_new_session=True)
def cmd_list():
@@ -84,13 +119,13 @@ def cmd_apply(path):
archive_current(entries)
shutil.copy2(path, ACTIVE)
save_manifest(entries)
restart_swaybg()
restart_wallpaper_daemon()
def cmd_pick():
result = subprocess.run(
["zenity", "--file-selection", "--title=Wallpaper wählen",
"--file-filter=Images | *.png *.jpg *.jpeg *.webp *.bmp"],
"--file-filter=Images | *.png *.jpg *.jpeg *.webp *.bmp *.gif"],
capture_output=True, text=True)
path = result.stdout.strip()
if result.returncode != 0 or not path:
@@ -111,6 +146,11 @@ def cmd_remove(id_str):
save_manifest([e for e in entries if e["id"] != wanted])
def cmd_start():
if os.path.isfile(ACTIVE):
restart_wallpaper_daemon()
def main():
args = sys.argv[1:]
if args[:1] == ["list"]:
@@ -121,6 +161,8 @@ def main():
cmd_pick()
elif args[:1] == ["remove"] and len(args) == 2:
cmd_remove(args[1])
elif args[:1] == ["start"]:
cmd_start()
else:
sys.exit(__doc__)