diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-05-16 21:19:00 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-10-30 09:52:23 +0100 |
commit | 5a3373ab70f54b8f7a0f1c16b6a80dd4a8e87f1f (patch) | |
tree | dd3e1cee612c1e62662cd6c789162bddce28e4c9 | |
parent | e595dcb94c0ea8aacc92a0090cc6a9f52f1b728f (diff) |
[sway] build script to enable auto-changing wallpapers
-rwxr-xr-x | .config/sway/bin/wallpaper.py | 75 | ||||
-rwxr-xr-x | .config/sway/bin/wallpaper.sh | 46 |
2 files changed, 121 insertions, 0 deletions
diff --git a/.config/sway/bin/wallpaper.py b/.config/sway/bin/wallpaper.py new file mode 100755 index 0000000..9784625 --- /dev/null +++ b/.config/sway/bin/wallpaper.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python + + +import sys, os +import argparse, json +import random, subprocess, signal, time + + +""" +" CLI +""" + +parser = argparse.ArgumentParser( + prog='wallpaper', + description='Cycle through wallpapers' +) + +parser.add_argument( + '--config', + required=True, + help='{<weight>: { find: <path>, exclude: <path> }, <weight>:...}' +) + +parser.add_argument( + '--interval', + default=600 +) + +args = parser.parse_args() + +config = json.loads(args.config) + + +""" +" Program +""" + +pid_running = -1 +while True: + probability_number = random.randrange(start=1, stop=100, step=1) + + # build "find" command based on args.config + # see https://stackoverflow.com/questions/16489449/select-element-from-array-with-probability-proportional-to-its-value/16490300#16490300 + find_cmd = '' + accumulative = 0 + for weight, wallpaper_path in config.items(): + lower_bound = accumulative + accumulative += int(weight) + upper_bound = accumulative + + if lower_bound <= probability_number and probability_number <= upper_bound: + find_cmd = 'find "{find}" -type f'.format(find=wallpaper_path['find']) + + if 'exclude' in wallpaper_path.keys(): + find_cmd += ' -not -path "{exclude}"'.format(exclude=wallpaper_path['exclude']) + + break + + # run swaybg + swaybg_cmd = f'swaybg -i $({find_cmd} | shuf -n1) -m fill &' + subprocess.run(swaybg_cmd, shell=True) + + # wait until new instance of swaybg has booted up properly + time.sleep(5) + + # kill the previous process + # the new swaybg process takes its place automatically + if pid_running != -1: + os.kill(pid_running, signal.SIGTERM) + + # set pid for next iteration + pid_running = int(subprocess.check_output(['pidof', 'swaybg'])) + + # wait $INTERVAL seconds + time.sleep(int(args.interval)) diff --git a/.config/sway/bin/wallpaper.sh b/.config/sway/bin/wallpaper.sh new file mode 100755 index 0000000..a035c03 --- /dev/null +++ b/.config/sway/bin/wallpaper.sh @@ -0,0 +1,46 @@ +#!/bin/sh + +WALLPAPER_PATH="$HOME/Images/Wallpaper" +INTERVAL=600 + +while getopts 'p::t::' OPTION; do + case "$OPTION" in + p) + WALLPAPER_PATH="$OPTARG" + ;; + t) + INTERVAL="$OPTARG" + ;; + ?) + echo "Usage: {$0} [--path=] [--time=]" + exit 1 + ;; + esac +done + +RUNNING_PID=-1 +while true; do + # run swaybg + RANDOM_NUMBER=$(shuf -i 1-10 -n 1) + if [ $RANDOM_NUMBER -le 2 ]; then + swaybg -i "$(find "$WALLPAPER_PATH" -type f | shuf -n1)" -m fill & + else + swaybg -i "$(find "$WALLPAPER_PATH" -type f -not -path "*/hyper-pokemon/*" | shuf -n1)" -m fill & + fi + + # get pid of new spawned instance + NEW_PID=$! + + # wait until new instance of swaybg has booted up properly + sleep 5 + + # kill the previous process + # the new swaybg process takes its place automatically + kill $RUNNING_PID + + # set pid for next iteration + RUNNING_PID=$NEW_PID + + # wait $INTERVAL seconds + sleep $INTERVAL +done |