Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cycle wallpaper and theme #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
121 changes: 121 additions & 0 deletions Configs/.local/lib/hyde/cyclewallpaperandtheme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#! /bin/env bash

scrDir=$(dirname "$(realpath "$0")")
source "$scrDir/globalcontrol.sh"

# Paths to scripts and directories
WALL_SCRIPT="$scrDir/swwwallpaper.sh"
THEME_SCRIPT="$scrDir/themeswitch.sh"
LOG_FILE="$HOME/.cache/cyclewallpaperandtheme.log"
WALL_CYCLE_FILE="$HOME/.local/share/cyclewallpapercount"
THEME_CYCLE_FILE="$HOME/.local/share/cyclethemecount"
THEME_CHANGE_FLAG_FILE="$HOME/.local/share/cyclethemeflag"
THEMES_DIR="$hydeConfDir/themes"
HYDE_CONFIG="$hydeConfDir/hyde.conf"

# Log messages to file
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$LOG_FILE"
}

# Safely read a counter file or initialize it
read_or_initialize_counter() {
local file="$1"
[ ! -f "$file" ] && echo 0 > "$file"
cat "$file"
}

# Increment and save a counter
increment_counter() {
local count="$1"
local max="$2"
local file="$3"
echo $(( (count + 1) % max )) > "$file"
}

# Cycle to the next theme
cycle_theme() {
# Get theme list
themes=()
while IFS= read -r theme; do
themes+=("$theme")
done < <(find "$THEMES_DIR" -mindepth 1 -maxdepth 1 -type d | sort)
themes=("${themes[@]##*/}") # Strip path to get only names

# Read and increment theme counter
theme_count=$(read_or_initialize_counter "$THEME_CYCLE_FILE")
next_theme="${themes[$theme_count]}"
increment_counter "$theme_count" "${#themes[@]}" "$THEME_CYCLE_FILE"

# Apply theme and wait for stability
"$THEME_SCRIPT" -s "$next_theme" >> "$LOG_FILE" 2>&1
log "Theme changed to: $next_theme"
sleep 3
}

# Cycle to the next wallpaper
cycle_wallpaper() {
# Determine current theme and wallpaper directory
current_theme=$(grep '^hydeTheme=' "$HYDE_CONFIG" | sed -E 's/^hydeTheme="([^"]+)"/\1/')
if [ -z "$current_theme" ]; then
log "ERROR: Unable to determine current theme from $HYDE_CONFIG."
exit 1
fi

wallpaper_dir="$THEMES_DIR/$current_theme/wallpapers"
if [ ! -d "$wallpaper_dir" ]; then
log "ERROR: Wallpaper directory for theme '$current_theme' does not exist: $wallpaper_dir"
exit 1
fi

# Get wallpaper list
wallpapers=("$wallpaper_dir"/*)
total_wallpapers=${#wallpapers[@]}
if [ "$total_wallpapers" -eq 0 ]; then
log "ERROR: No wallpapers found in $wallpaper_dir"
exit 1
fi

# Read and increment wallpaper counter
wall_count=$(read_or_initialize_counter "$WALL_CYCLE_FILE")
next_wallpaper="${wallpapers[$wall_count]}"
increment_counter "$wall_count" "$total_wallpapers" "$WALL_CYCLE_FILE"

# Set wallpaper
"$WALL_SCRIPT" -s "$next_wallpaper" >> "$LOG_FILE" 2>&1
if [ -n "$DM_SCRIPT" ] && [ -x "$DM_SCRIPT" ]; then
"$DM_SCRIPT" "$next_wallpaper" >> "$LOG_FILE" 2>&1
else
log "No Display Manager script provided or script is not executable. Skipping Display Manager wallpaper update."
fi
log "Wallpaper changed to: $next_wallpaper"

# If all wallpapers are cycled, flag theme change
if [ "$wall_count" -eq $((total_wallpapers - 1)) ]; then
touch "$THEME_CHANGE_FLAG_FILE"
echo 0 > "$WALL_CYCLE_FILE"
log "Theme change flagged for next run."
fi
}

# Main execution
while [[ $# -gt 0 ]]; do
case "$1" in
--dm-script)
DM_SCRIPT="$2"
shift 2
;;
*)
log "Unknown argument: $1"
exit 1
;;
esac
done

if [ -f "$THEME_CHANGE_FLAG_FILE" ]; then
cycle_theme
rm -f "$THEME_CHANGE_FLAG_FILE"
fi

cycle_wallpaper
log "Cycle operation completed."
29 changes: 29 additions & 0 deletions Configs/.local/lib/hyde/setsddmcornerswallpaper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#! /bin/env bash

# Configuration
SDDM_THEME_CONFIG="/usr/share/sddm/themes/Corners/theme.conf"
BACKGROUND_DIR="/usr/share/sddm/themes/Corners/backgrounds"
WALLPAPER_NAME="customwallpaper"

# Log messages
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') $1"
}

# Validate input
if [ -z "$1" ]; then
log "ERROR: No wallpaper path provided."
exit 1
fi

WALLPAPER_PATH="$1"
if [ ! -f "$WALLPAPER_PATH" ]; then
log "ERROR: Wallpaper file does not exist: $WALLPAPER_PATH"
exit 1
fi

# Update SDDM wallpaper
cp -f "$WALLPAPER_PATH" "$BACKGROUND_DIR/$WALLPAPER_NAME"
sed -i "s|^Background=.*|Background=\"backgrounds/$WALLPAPER_NAME\"|" "$SDDM_THEME_CONFIG"

log "SDDM wallpaper updated to: $BACKGROUND_DIR/$WALLPAPER_NAME"