wallpaperctl/wallpaperctl.py

90 lines
3.0 KiB
Python
Executable File

#!/usr/bin/python3
import configparser
import os
import random
import sys
import subprocess
from pathlib import Path
def find_all_images(path):
IMAGE_TYPES = ['jpg', 'png', 'webp', 'jpeg']
if WALLPAPER_ENGINE == 'plasma':
IMAGE_TYPES.remove("webp")
images = []
for image_type in IMAGE_TYPES:
[images.append(image) for image in path.glob(f'**/*.{image_type}')]
return images
conf_file = f"{os.environ['HOME']}/.config/wallpaperctl/wallpaperctl.ini"
# Initialize config file
config = configparser.ConfigParser()
try:
config.read(conf_file)
X_DISPLAY = config['General']['X_SERVER']
WALLHAVEN_API_KEY = config['General']['WALLHAVEN_API_KEY']
WALLPAPER_ENGINE = config['General']['WALLPAPER_ENGINE']
ULTRA_WALL_DIR = Path(config['General']['ULTRA_WALL_DIR']).expanduser()
HOR_WALL_DIR = Path(config['General']['HOR_WALL_DIR']).expanduser()
VERT_WALL_DIR = Path(config['General']['VERT_WALL_DIR']).expanduser()
monitors = {}
for ID in config['Monitors'].keys():
monitors[ID] = config['Monitors'][ID]
except Exception as ex:
print('Please copy default config from /opt/wallpaperctl to $HOME/.config/wallpaperctl/wallpaperctl.ini')
# Load a list of the images (supports nested files using **)
ultra_images = [image for image in find_all_images(ULTRA_WALL_DIR)]
hor_images = [image for image in find_all_images(HOR_WALL_DIR)]
vert_images = [image for image in find_all_images(VERT_WALL_DIR)]
# Store selected wallpapers here
wallpapers = []
# Loop through defined monitors and allocate an approprate wallpaper
for mon in monitors:
match monitors[mon]:
case 'ultrawide':
wallpapers.append(random.choice(ultra_images))
case 'vertical':
wallpapers.append(random.choice(vert_images))
case 'horizontal':
wallpapers.append(random.choice(hor_images))
case _:
print(monitors[mon])
match WALLPAPER_ENGINE:
case 'feh':
cmd = "/usr/bin/feh --no-fehbg --bg-fill "
for wallpaper in wallpapers:
cmd += str(f"{wallpaper} ")
subprocess.call(cmd, shell=True)
case 'plasma':
# Write code to handle changing wallpapers on plasmashell
fuckingGarbageJS = """
var allDesktops = desktops();
"""
for wallpaper in wallpapers:
wallpaperIndex = wallpapers.index(wallpaper)
fuckingGarbageJS += f"""allDesktops[{wallpaperIndex}].wallpaperPlugin = "org.kde.image";
allDesktops[{wallpaperIndex}].currentConfigGroup = Array("Wallpaper", "org.kde.image", "General");
allDesktops[{wallpaperIndex}].writeConfig("Image", "file://{wallpaper}");
"""
cmd = f"qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript '{fuckingGarbageJS}'"
subprocess.call(cmd, shell=True)
case 'gnome':
# Write code to handle changing wallpapers on gnomeshell
cmd = "ls"
pass
case _:
print(f"Wallpaper engine {WALLPAPER_ENGINE} is not supported")
sys.exit()