Files
planetx-wallpaper/functions.sh
T
2026-10-16 16:21:51 -05:00

120 lines
5.6 KiB
Bash

#!/usr/bin/env bash
function downloadClouds() { #Download latest clouds image from matteason.co.uk if newer than the local copy exists
echo downloading latest cloud image if newer...
local sourceImage="${1:?Error output file not provided (arg 1)}"
local httpStatus curlStatus
local etagFile="${sourceImage}.etag"
if [ ! -d "${sourceImage%/*}" ]; then # If the source directory does not exist, return an error code
echo Could not find source image directory for clouds image
return 1
fi
# TODO return sample image if the source image does not exist, or if the download fails
# if [ ! -e "$sourceImage" ]; then # If the image path does not exist, return an error code
# echo Cloud source image path does not exist
# return 1
# fi
# Download the image saving the http status code and curl status code for error checking
httpStatus=$(curl -s --show-error -R \
--write-out "%{http_code}" \
--etag-compare "$etagFile" \
--etag-save "$etagFile" \
-o "$sourceImage" \
"https://clouds.matteason.co.uk/images/8192x4096/clouds.jpg" )
curlStatus="$?"
if (( curlStatus != 0 )); then # return an error code if curl failed to download the image
echo "Cloud image request failed, curl exit status: $curlStatus" >&2
return "$curlStatus"
fi
case "$httpStatus" in # Report the status of the download based on the HTTP status code returned by curl
200) echo "Cloud image successfully downloaded"; return 0 ;;
304) echo "Cloud image already up to date"; return 0 ;;
"") echo "Cloud image successfully downloaded or already up to date"; return 0 ;;
*) echo "Error: failed to download clouds image, HTTP status: $httpStatus" >&2; return 1 ;;
esac
}
function downloadStations() { # Download latest stations data from celestrak.org and format it to only include the names of the stations
echo downloading latest stations data...
curl -s --show-error -L -o "$STATIONS_TLE" "https://celestrak.org/NORAD/elements/gp.php?GROUP=stations&FORMAT=tle" && echo stations data downloaded successfully || echo Error: failed to download stations data
sed -i 's/\r//' "$STATIONS_TLE" #Strip carriage returns from file if they exist
# sed -n '1~3p' "$STATIONS_TLE" > "$STATIONS_FINAL" && echo stations data formatted successfully || echo Error: failed to format stations data
# sed -n '1~3p' stations.tle | sed 's/[[:space:]]*$//' > stations
sed -n '1~3p' stations.tle > stations
}
function connectedMonitors() { # returns list of connected monitors
monitors=$(hyprctl monitors -j | jq ".[].name")
echo "${monitors//$'\n'/ }"
}
function cacheImages() { # Validate that the images exist, cache them if they do, return a path to the temp image
# TODO make functional
local monitor="${1:?Error monitor name not provided (arg 1)}"
local imageDir="${2:?Error image directory not provided (arg 2)}"
local imagePath="$imageDir/$monitor-render.png"
if [ -e "$imagePath" ]; then # If the Image exists, move it to a temp file and set it as the wallpaper
echo image exists for "$monitor", caching old walpaper...
mv "$imagePath" "$imagePath.tmp"
noctalia msg wallpaper-set "$monitor" "$imagePath.tmp" 1>/dev/null && echo "$monitor walpaper cached" || echo "Warning: not connected to monitor $monitor, wallpaper will not be set."
# TODO return here
elif [ -e "$imagePath.tmp" ]; then # If temp exists but not the original, move temp back to original
# TODO report work
mv "$imagePath.tmp" "$monitor"
noctalia msg wallpaper-set "$monitor" "$imagePath.tmp" 1>/dev/null && echo "$monitor walpaper cached" || echo "Warning: not connected to monitor $monitor, wallpaper will not be set."
# TODO return here
else
echo Error: no previous image exists, using sample image instead
noctalia msg wallpaper-set eDP-1 "$imagePath/sample-render.png" 1>/dev/null || return 1
# TODO return 1 instead of exiting, and handle the error in the calling function
fi
# TODO handle this case
}
function old_cacheImages() { # Validate that the images exist and cache them if they do, otherwise use a sample image
# Noctalia must see an image path update in order to change the active wallpaper
# So set a tmp file with the current image, upodate to that tmp file, and then switch back once the image is rendered.
# TODO: only render monitors that currectly exist.
if [ -e "$OUTPUT_IMG_ULTRAWIDE" ]; then # Check for ultrawide image
echo ultrawide image exists, caching old walpaper...
mv "$OUTPUT_IMG_ULTRAWIDE" "$OUTPUT_IMG_ULTRAWIDE.tmp"
noctalia msg wallpaper-set DP-2 "$OUTPUT_IMG_ULTRAWIDE.tmp" 1>/dev/null && echo ultrawide walpaper cached || echo Warning: not connected to monitor DP-2, wallpaper will not be set.
elif [ -e "$OUTPUT_IMG_ULTRAWIDE.tmp" ]; then # If temp exists but not the original, move temp back to original
mv "$OUTPUT_IMG_ULTRAWIDE.tmp" "$OUTPUT_IMG_ULTRAWIDE"
else
echo Error: no builtin image exists, using sample image instead
noctalia msg wallpaper-set eDP-1 "test.png" 1>/dev/null || exit 1
fi
if [ -e "$OUTPUT_IMG_EDP" ]; then # Check for builtin image
echo builtin image exists, caching old walpaper...
mv "$OUTPUT_IMG_EDP" "$OUTPUT_IMG_EDP.tmp"
noctalia msg wallpaper-set eDP-1 "$OUTPUT_IMG_EDP.tmp" 1>/dev/null || exit 1
elif [ -e "$OUTPUT_IMG_EDP.tmp" ]; then # If temp exists but not the original, move temp back to original
mv "$OUTPUT_IMG_EDP.tmp" "$OUTPUT_IMG_EDP"
else # Use a smample image if no original or temp exists
echo Error: no builtin image exists, using sample image instead
noctalia msg wallpaper-set eDP-1 "test.png" 1>/dev/null || exit 1
fi
}
function renderWallpaper() {
NATIVE_RES="$1"
MONITOR_NAME="$2"
XPLANET_ARGS=$3
OUTPUT_RES="$"
}
function cleanup() { # Cleanup temp files if no errors occurred
rm "$OUTPUT_IMG_ULTRAWIDE.tmp"
rm "$OUTPUT_IMG_EDP.tmp"
}