change structure
This commit is contained in:
1 parent
76d1992a55
commit
fa0746edba
6 files changed
+143
-131
No files matched your search
Executable
+63
@@ -0,0 +1,63 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Function to get the relative path based on the current script's location
|
||||
get_relative_path() {
|
||||
local path=$1
|
||||
local level=$2
|
||||
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
for ((i = 1; i <= level; i++)); do
|
||||
script_dir="$(dirname "$script_dir")"
|
||||
done
|
||||
|
||||
echo "$script_dir/$path"
|
||||
}
|
||||
|
||||
# Assign relative paths to variables
|
||||
GENERATOR=$(get_relative_path "../material-colors/generate.py")
|
||||
SETTINGS_FILE=$(get_relative_path "../assets/settings.json")
|
||||
|
||||
# Function to read a value from the JSON file
|
||||
get_json_value() {
|
||||
local key=$1
|
||||
jq -r --arg key "$key" '.[$key]' $SETTINGS_FILE
|
||||
}
|
||||
|
||||
# Function to update a value in the JSON file
|
||||
update_json_value() {
|
||||
local key=$1
|
||||
local value=$2
|
||||
jq --arg key "$key" --arg value "$value" \
|
||||
'.[$key] = $value' $SETTINGS_FILE >"$SETTINGS_FILE.tmp" && mv "$SETTINGS_FILE.tmp" $SETTINGS_FILE
|
||||
}
|
||||
|
||||
toggle() {
|
||||
local color_scheme=$(get_json_value "color-scheme")
|
||||
local generation_scheme=$(get_json_value "generation-scheme")
|
||||
|
||||
if [ "$color_scheme" == "dark" ]; then
|
||||
python -O $GENERATOR -R --color-scheme light --scheme $generation_scheme
|
||||
update_json_value "color-scheme" "light"
|
||||
elif [ "$color_scheme" == "light" ]; then
|
||||
python -O $GENERATOR -R --color-scheme dark --scheme $generation_scheme
|
||||
update_json_value "color-scheme" "dark"
|
||||
else
|
||||
echo "Unknown color scheme: $color_scheme"
|
||||
fi
|
||||
}
|
||||
|
||||
set() {
|
||||
local new_scheme=$1
|
||||
local generation_scheme=$(get_json_value "generation-scheme")
|
||||
python -O $GENERATOR -R --color-scheme $new_scheme --scheme $generation_scheme
|
||||
update_json_value "color-scheme" $new_scheme
|
||||
}
|
||||
|
||||
# Process arguments
|
||||
if [[ "$1" == "--toggle" ]]; then
|
||||
toggle
|
||||
elif [[ "$1" == "--set" ]]; then
|
||||
set $2
|
||||
else
|
||||
echo "Usage: $0 --toggle | --set <color-scheme>"
|
||||
fi
|
||||
@@ -0,0 +1,271 @@
|
||||
import os
|
||||
import sys
|
||||
import importlib.util
|
||||
import argparse
|
||||
import fcntl
|
||||
import subprocess
|
||||
import asyncio
|
||||
import random as _random
|
||||
import colorsys
|
||||
import json
|
||||
import inspect
|
||||
|
||||
|
||||
def get_relative_path(path: str, level: int = 1) -> str:
|
||||
prev_globals = inspect.stack()[level][0].f_globals
|
||||
file_var = (
|
||||
os.path.dirname(os.path.abspath(prev_globals["__file__"]))
|
||||
if "__file__" in prev_globals
|
||||
else os.getcwd()
|
||||
)
|
||||
return os.path.join(file_var, path)
|
||||
|
||||
|
||||
lock_file_path = "/tmp/wallpaper.lock"
|
||||
settings_file_path = get_relative_path("../assets/settings.json")
|
||||
|
||||
|
||||
def acquire_lock():
|
||||
global lock_file
|
||||
lock_file = open(lock_file_path, "w")
|
||||
try:
|
||||
fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
lock_file.write(str(os.getpid()))
|
||||
lock_file.flush()
|
||||
except IOError:
|
||||
print("Another instance of the script is already running.")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def release_lock():
|
||||
fcntl.flock(lock_file, fcntl.LOCK_UN)
|
||||
lock_file.close()
|
||||
os.remove(lock_file_path)
|
||||
|
||||
|
||||
def hue_to_numeric_hex(hue):
|
||||
hue = hue / 360.0
|
||||
rgb = colorsys.hls_to_rgb(hue, 0.5, 1.0)
|
||||
hex_color_str = "#{:02x}{:02x}{:02x}".format(
|
||||
int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255)
|
||||
)
|
||||
numeric_hex_color = int(hex_color_str.lstrip("#"), 16)
|
||||
return numeric_hex_color
|
||||
|
||||
|
||||
def load_settings():
|
||||
default_settings = {
|
||||
"color-scheme": "dark",
|
||||
"custom-color": "none",
|
||||
"generation-scheme": "tonalSpot",
|
||||
}
|
||||
|
||||
if not os.path.exists(settings_file_path):
|
||||
os.makedirs(os.path.dirname(settings_file_path), exist_ok=True)
|
||||
with open(settings_file_path, "w") as file:
|
||||
json.dump(default_settings, file, indent=4)
|
||||
|
||||
with open(settings_file_path, "r") as file:
|
||||
return json.load(file)
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument("-I", "--image", type=str, help="Image")
|
||||
group.add_argument(
|
||||
"-P",
|
||||
"--prev",
|
||||
help="Use last used wallpaper for color generation",
|
||||
action="store_true",
|
||||
)
|
||||
group.add_argument(
|
||||
"-R", "--random", help="Random image from folder", action="store_true"
|
||||
)
|
||||
parser.add_argument("-n", "--notify", help="Send notifications", action="store_true")
|
||||
parser.add_argument(
|
||||
"--status", type=str, help="Status file", default="/tmp/wallpaper.status"
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
random = args.random
|
||||
prev = args.prev
|
||||
image = args.image
|
||||
notify = args.notify
|
||||
status = args.status
|
||||
|
||||
HOME = os.path.expanduser("~")
|
||||
|
||||
module_path = get_relative_path("./material-colors")
|
||||
sys.path.append(module_path)
|
||||
|
||||
module_name = "generate"
|
||||
file_path = f"{module_path}/{module_name}.py"
|
||||
|
||||
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
||||
GENERATOR = importlib.util.module_from_spec(spec) # type: ignore
|
||||
spec.loader.exec_module(GENERATOR) # type: ignore
|
||||
|
||||
cache_file = f"{HOME}/.cache/current_wallpaper"
|
||||
square = f"{HOME}/.cache/square_wallpaper.png"
|
||||
png = f"{HOME}/.cache/current_wallpaper.png"
|
||||
|
||||
|
||||
def current_state(state_str: str):
|
||||
with open(status, "w") as f:
|
||||
f.write(state_str)
|
||||
|
||||
|
||||
def send_notify(label: str, desc: str):
|
||||
if not notify:
|
||||
return
|
||||
subprocess.run(["notify-send", label, desc])
|
||||
|
||||
|
||||
def state(name: str | None, label: str | None, desc: str | None):
|
||||
if name is not None:
|
||||
current_state(name)
|
||||
if label is not None:
|
||||
send_notify(label, desc or "")
|
||||
|
||||
|
||||
def join(*args):
|
||||
return os.path.join(*args)
|
||||
|
||||
|
||||
async def main():
|
||||
state("init", None, None)
|
||||
|
||||
settings = load_settings()
|
||||
color_scheme = settings["color-scheme"]
|
||||
custom_color = settings["custom-color"]
|
||||
generation_scheme = settings["generation-scheme"]
|
||||
swww_animation = "wipe"
|
||||
wallpaper_engine = "swww"
|
||||
|
||||
new_wallpaper = f"{HOME}/Pictures/wallpapers/example-1.jpg"
|
||||
# new_wallpaper = get_relative_path("../assets/wallpapers/1.png")
|
||||
|
||||
if random:
|
||||
files = [
|
||||
f
|
||||
for f in os.listdir(f"{HOME}/Pictures/wallpapers/")
|
||||
if f.endswith((".png", ".jpg", ".jpeg"))
|
||||
]
|
||||
if files:
|
||||
new_wallpaper = join(f"{HOME}/Pictures/wallpapers", _random.choice(files))
|
||||
elif prev:
|
||||
try:
|
||||
with open(cache_file) as f:
|
||||
new_wallpaper = f.read().strip()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
elif image is not None:
|
||||
new_wallpaper = os.path.abspath(image)
|
||||
|
||||
print(f":: Wallpaper {new_wallpaper}")
|
||||
|
||||
with open(cache_file, "w") as f:
|
||||
f.write(new_wallpaper)
|
||||
|
||||
with_image = f"with image {new_wallpaper}"
|
||||
|
||||
# -----------------------------------------------------
|
||||
# Set the new wallpaper
|
||||
# -----------------------------------------------------
|
||||
|
||||
transition_type = swww_animation
|
||||
|
||||
state("changing", "Changing wallpaper...", with_image)
|
||||
print(":: Changing wallpaper...")
|
||||
|
||||
if wallpaper_engine == "swww":
|
||||
print(":: Using swww")
|
||||
|
||||
cursor_pos = subprocess.getoutput("hyprctl cursorpos").replace(", ", ",")
|
||||
|
||||
subprocess.run(
|
||||
[
|
||||
"swww",
|
||||
"img",
|
||||
new_wallpaper,
|
||||
"--transition-bezier",
|
||||
".43,1.19,1,.4",
|
||||
"--transition-fps",
|
||||
"60",
|
||||
"--transition-type",
|
||||
transition_type,
|
||||
"--transition-duration",
|
||||
"0.7",
|
||||
"--transition-pos",
|
||||
cursor_pos,
|
||||
]
|
||||
)
|
||||
|
||||
else:
|
||||
print(":: Wallpaper Engine disabled")
|
||||
|
||||
# -----------------------------------------------------
|
||||
# Generate colors
|
||||
# -----------------------------------------------------
|
||||
state("colors", "Generating colors...", with_image)
|
||||
print(":: Generate colors")
|
||||
_starts_with = lambda x: str(x).startswith("-")
|
||||
|
||||
GENERATOR.main(
|
||||
color_scheme,
|
||||
new_wallpaper,
|
||||
(
|
||||
hue_to_numeric_hex(int(custom_color.lstrip("-")))
|
||||
if _starts_with(custom_color)
|
||||
else (int(custom_color.lstrip("#"), 16) if custom_color != "none" else None)
|
||||
),
|
||||
scheme_name=generation_scheme,
|
||||
)
|
||||
|
||||
# Generate square and png versions
|
||||
await asyncio.gather(square_image(new_wallpaper), png_image(new_wallpaper))
|
||||
|
||||
state("finish", "Wallpaper procedure complete!", with_image)
|
||||
|
||||
|
||||
async def png_image(wallpaper: str):
|
||||
if wallpaper.lower().endswith((".jpg", ".jpeg")):
|
||||
state(None, "Converting jpg to png...", f"with image {wallpaper}")
|
||||
print(":: Converting jpg to png...")
|
||||
cmd = f"magick {wallpaper}[0] {png}"
|
||||
else:
|
||||
cmd = f"cp -f {wallpaper} {png}"
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
|
||||
if process.returncode != 0:
|
||||
print(f":: Error while processing image: {stderr.decode()}")
|
||||
else:
|
||||
print(":: JPG successfully converted to PNG!")
|
||||
|
||||
|
||||
async def square_image(wallpaper):
|
||||
state(None, "Creating square version...", f"with image {wallpaper}")
|
||||
print(":: Creating square version")
|
||||
cmd = f"magick {wallpaper}[0] -gravity Center -extent 1:1 {square}"
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
|
||||
if process.returncode != 0:
|
||||
print(f":: Error while processing image: {stderr.decode()}")
|
||||
else:
|
||||
print(":: Square image created!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
acquire_lock()
|
||||
try:
|
||||
asyncio.run(main())
|
||||
finally:
|
||||
release_lock()
|
||||
Executable
+83
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from signal import SIGINT, SIGTERM, signal
|
||||
from threading import Event
|
||||
import setproctitle
|
||||
|
||||
from pywayland.client.display import Display # type: ignore
|
||||
from pywayland.protocol.idle_inhibit_unstable_v1.zwp_idle_inhibit_manager_v1 import ( # type: ignore
|
||||
ZwpIdleInhibitManagerV1,
|
||||
)
|
||||
from pywayland.protocol.wayland.wl_compositor import WlCompositor # type: ignore
|
||||
from pywayland.protocol.wayland.wl_registry import WlRegistryProxy # type: ignore
|
||||
from pywayland.protocol.wayland.wl_surface import WlSurface # type: ignore
|
||||
|
||||
|
||||
@dataclass
|
||||
class GlobalRegistry:
|
||||
surface: WlSurface | None = None
|
||||
inhibit_manager: ZwpIdleInhibitManagerV1 | None = None
|
||||
|
||||
|
||||
def handle_registry_global(
|
||||
wl_registry: WlRegistryProxy, id_num: int, iface_name: str, version: int
|
||||
) -> None:
|
||||
global_registry: GlobalRegistry = wl_registry.user_data or GlobalRegistry()
|
||||
|
||||
if iface_name == "wl_compositor":
|
||||
compositor = wl_registry.bind(id_num, WlCompositor, version)
|
||||
global_registry.surface = compositor.create_surface() # type: ignore
|
||||
elif iface_name == "zwp_idle_inhibit_manager_v1":
|
||||
global_registry.inhibit_manager = wl_registry.bind(
|
||||
id_num, ZwpIdleInhibitManagerV1, version
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
done = Event()
|
||||
signal(SIGINT, lambda _, __: done.set())
|
||||
signal(SIGTERM, lambda _, __: done.set())
|
||||
|
||||
global_registry = GlobalRegistry()
|
||||
|
||||
display = Display()
|
||||
display.connect()
|
||||
|
||||
registry = display.get_registry() # type: ignore
|
||||
registry.user_data = global_registry
|
||||
registry.dispatcher["global"] = handle_registry_global
|
||||
|
||||
def shutdown() -> None:
|
||||
display.dispatch()
|
||||
display.roundtrip()
|
||||
display.disconnect()
|
||||
|
||||
display.dispatch()
|
||||
display.roundtrip()
|
||||
|
||||
if global_registry.surface is None or global_registry.inhibit_manager is None:
|
||||
print("Wayland seems not to support idle_inhibit_unstable_v1 protocol.")
|
||||
shutdown()
|
||||
sys.exit(1)
|
||||
|
||||
inhibitor = global_registry.inhibit_manager.create_inhibitor( # type: ignore
|
||||
global_registry.surface
|
||||
)
|
||||
|
||||
display.dispatch()
|
||||
display.roundtrip()
|
||||
|
||||
print("Inhibiting idle...")
|
||||
done.wait()
|
||||
print("Shutting down...")
|
||||
|
||||
inhibitor.destroy()
|
||||
|
||||
shutdown()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
setproctitle.setproctitle("wayland-idle-inhibitor.py")
|
||||
main()
|
||||
Reference in new issue
Block a user