commit 06dc4f8e5a7d1ca786f9bac52f9b1d3917351f12 Author: alterNERDtive Date: Thu Nov 2 17:16:32 2023 +0100 initial commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e87c589 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +all: install + +install: + mkdir -p ~/.local/bin + cp swaybg-swapper.py ~/.local/bin + cp systemd/swaybg-swapper.{service,timer} ~/.config/systemd/user/ + systemctl --user daemon-reload + systemctl --user enable --now swaybg-swapper.timer diff --git a/backgrounds.example.json b/backgrounds.example.json new file mode 100644 index 0000000..5fea2d1 --- /dev/null +++ b/backgrounds.example.json @@ -0,0 +1,17 @@ +{ + "DP-0": [ + { + "file": "test.png", + "transform": "fill", + "colour": "#000000" + }, + { "file": "empty.png" } + ], + "DP-1": [ + { "file": "foo.png" }, + { + "file": "bar.jpg", + "transform": "fit" + } + ] +} diff --git a/swaybg-swapper.py b/swaybg-swapper.py new file mode 100755 index 0000000..9cde748 --- /dev/null +++ b/swaybg-swapper.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +import json +import random +import subprocess +import time + +import os.path + +pidof = subprocess.run(["pidof", "swaybg"], capture_output=True) +pids = None +if pidof.returncode == 0: + pids = pidof.stdout[:-1] + +file = "~/.config/sway/backgrounds.json" +if not os.path.isfile(os.path.expanduser(file)): + print("Config file “{}” not found".format(file)) + exit(1) + +with open(os.path.expanduser(file)) as config_file: + config = json.load(config_file) + +cmd=["swaybg"] +for item in config: + output = config[item] + background = output[random.randrange(len(output))] + cmd.extend(["-o", item, "-i", os.path.expanduser(background["file"])]) + if "transform" in background: + cmd.extend(["-m", background["transform"]]) + else: + cmd.extend(["-m", "center"]) + if "colour" in background: + cmd.extend(["-c", background["colour"]]) + else: cmd.extend(["-c", "#000000"]) + +# avoid ugly flickering by +# 1. running new `swaybg` +# 2. waiting! +# 3. killing old `swaybg`(s) +subprocess.Popen(cmd) +time.sleep(1) +if pids is not None: + for pid in pids.split(): + subprocess.run(["kill", pid]) diff --git a/systemd/swaybg-swapper.service b/systemd/swaybg-swapper.service new file mode 100644 index 0000000..c30b532 --- /dev/null +++ b/systemd/swaybg-swapper.service @@ -0,0 +1,14 @@ +[Unit] +Description=Background swapper for sway/swaybg +PartOf=sway-session.target + +[Service] +Type=oneshot +ExecStart=python %h/.local/bin/swaybg-swapper.py +Restart=on-failure +RestartSec=1 +TimeoutStopSec=10 +KillMode=process + +[Install] +WantedBy=sway-session.target diff --git a/systemd/swaybg-swapper.timer b/systemd/swaybg-swapper.timer new file mode 100644 index 0000000..7d8a387 --- /dev/null +++ b/systemd/swaybg-swapper.timer @@ -0,0 +1,6 @@ +[Unit] +Description=Executes swaybg-swapper.service +[Timer] +OnUnitInactiveSec=60m +[Install] +WantedBy=timers.target diff --git a/timer.example.conf b/timer.example.conf new file mode 100644 index 0000000..f25dfdc --- /dev/null +++ b/timer.example.conf @@ -0,0 +1,2 @@ +[Timer] +OnUnitInactiveSec=60m