From aed62dd72ed5f8645dae35595625e661535b77ae Mon Sep 17 00:00:00 2001 From: hesam-init Date: Sun, 25 Feb 2024 11:10:05 +0330 Subject: [PATCH] feat: dynamic wallpaper daemon --- hyprland/.config/environments/env.sh | 3 +- hyprland/.config/hypr/conf/autostart.conf | 3 +- .../hypr/conf/environments/default.conf | 2 +- hyprland/.config/hypr/conf/keybinding.conf | 2 +- .../.config/hypr/scripts/swaybg-random.sh | 30 +++++++++++++++++++ hyprland/.config/hypr/scripts/swww-launch.sh | 9 ------ hyprland/.config/hypr/scripts/swww-random.sh | 4 +++ .../.config/hypr/scripts/wallpaper-daemon.sh | 27 +++++++++++++++++ .../rofi/wallpaper/type-1/wallpaper.sh | 30 ++++++++++++++++++- 9 files changed, 95 insertions(+), 15 deletions(-) create mode 100755 hyprland/.config/hypr/scripts/swaybg-random.sh delete mode 100755 hyprland/.config/hypr/scripts/swww-launch.sh create mode 100755 hyprland/.config/hypr/scripts/wallpaper-daemon.sh diff --git a/hyprland/.config/environments/env.sh b/hyprland/.config/environments/env.sh index fe7ead6..b0f4495 100755 --- a/hyprland/.config/environments/env.sh +++ b/hyprland/.config/environments/env.sh @@ -6,7 +6,8 @@ export WAYBAR_DIR="$HOME/.config/waybar/$WAYBAR_THEME" export WAYBAR_LAUNCHER="waybar -c $WAYBAR_DIR/config.jsonc -s $WAYBAR_DIR/style.css" export WAYBAR_INOTIFY_EVENTS="-e close_write,move,create" -# swww env : +# wallpaper configs : export WALLPAPERS_DIR="$HOME/Wallpapers" +export WALLPAPER_DAEMON="swaybg" # swaybg or swww export SWWW_FPS=144 export SWWW_DURATION=2 diff --git a/hyprland/.config/hypr/conf/autostart.conf b/hyprland/.config/hypr/conf/autostart.conf index 0dc2588..54a4c91 100644 --- a/hyprland/.config/hypr/conf/autostart.conf +++ b/hyprland/.config/hypr/conf/autostart.conf @@ -11,8 +11,7 @@ exec-once = sleep 1 && /usr/bin/wireplumber & exec-once = wl-paste --watch cliphist store & # wallpaper : -exec-once = swaybg -i ~/Wallpapers/dark-blue-windows-3840x2160-14391.png & -# exec-once = ~/.config/hypr/scripts/swww-launch.sh & +exec-once = ~/.config/hypr/scripts/wallpaper-daemon.sh & # status bar : exec-once = ~/.config/hypr/scripts/waybar-launch.sh & diff --git a/hyprland/.config/hypr/conf/environments/default.conf b/hyprland/.config/hypr/conf/environments/default.conf index 8281ddf..fc68cfd 100644 --- a/hyprland/.config/hypr/conf/environments/default.conf +++ b/hyprland/.config/hypr/conf/environments/default.conf @@ -12,7 +12,7 @@ $terminal = alacritty -o "window.opacity=0" $fileManager = thunar $network_manager = $scr_path/nmtui-launch.sh $screenshot = $scr_path/hyprshot.sh -$swww_random = $scr_path/swww-random.sh +$random_wallpaper = $scr_path/wallpaper-daemon.sh $waybar_toggle = $scr_path/waybar-toggle.sh $volume_control = $scr_path/volume-control.sh $rofi_menu = $scr_path/rofi-launch.sh d diff --git a/hyprland/.config/hypr/conf/keybinding.conf b/hyprland/.config/hypr/conf/keybinding.conf index 0c22a24..0ba848b 100644 --- a/hyprland/.config/hypr/conf/keybinding.conf +++ b/hyprland/.config/hypr/conf/keybinding.conf @@ -33,7 +33,7 @@ bind = $mainMod, PRINT, exec, $screenshot -m window bind = , PRINT, exec, $screenshot -m output # Wallpaper -bind = $mainMod, Y, exec, $swww_random +bind = $mainMod, Y, exec, $random_wallpaper # Audio control binde=,XF86AudioRaiseVolume,exec, $volume_control u diff --git a/hyprland/.config/hypr/scripts/swaybg-random.sh b/hyprland/.config/hypr/scripts/swaybg-random.sh new file mode 100755 index 0000000..79093a3 --- /dev/null +++ b/hyprland/.config/hypr/scripts/swaybg-random.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +source $HOME/.config/environments/env.sh + +files=($(find "${WALLPAPERS_DIR}" -type f)) + +if [ ${#files[@]} -eq 0 ]; then + echo "No files found in the directory: ${WALLPAPERS_DIR}" + exit 1 +fi + +image_files=() +for file in "${files[@]}"; do + if file -b --mime-type "$file" | grep -q "^image/"; then + image_files+=("$file") + fi +done + +if [ ${#image_files[@]} -eq 0 ]; then + echo "No image files found in the directory: ${WALLPAPERS_DIR}" + exit 1 +fi + +random_pic=${image_files[$RANDOM % ${#image_files[@]}]} + +if [[ $(pidof swaybg) ]]; then + pkill swaybg +fi + +swaybg -i "$random_pic" diff --git a/hyprland/.config/hypr/scripts/swww-launch.sh b/hyprland/.config/hypr/scripts/swww-launch.sh deleted file mode 100755 index 07a666b..0000000 --- a/hyprland/.config/hypr/scripts/swww-launch.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash - -if [[ $(pidof swaybg) ]]; then - pkill swaybg -fi - -swww query || swww init - -exec $HOME/.config/hypr/scripts/swww-random.sh diff --git a/hyprland/.config/hypr/scripts/swww-random.sh b/hyprland/.config/hypr/scripts/swww-random.sh index d731a98..3ca023d 100755 --- a/hyprland/.config/hypr/scripts/swww-random.sh +++ b/hyprland/.config/hypr/scripts/swww-random.sh @@ -2,6 +2,10 @@ source $HOME/.config/environments/env.sh +if [[ ! $(pidof swww-daemon) ]]; then + swww init +fi + files=($(find "${WALLPAPERS_DIR}" -type f)) if [ ${#files[@]} -eq 0 ]; then diff --git a/hyprland/.config/hypr/scripts/wallpaper-daemon.sh b/hyprland/.config/hypr/scripts/wallpaper-daemon.sh new file mode 100755 index 0000000..2a1f9cb --- /dev/null +++ b/hyprland/.config/hypr/scripts/wallpaper-daemon.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +source $HOME/.config/environments/env.sh + +if [ -z "$WALLPAPER_DAEMON" ]; then + echo "WALLPAPER_DAEMON is not set in the environment." + exit 1 +fi + +case $WALLPAPER_DAEMON in +"swaybg") + if [[ $(pidof swww-daemon) ]]; then + pkill swww-daemon + fi + exec $HOME/.config/hypr/scripts/swaybg-random.sh + ;; +"swww") + if [[ $(pidof swaybg) ]]; then + pkill swaybg + fi + exec $HOME/.config/hypr/scripts/swww-random.sh + ;; +*) + echo "Unknown value for WALLPAPER_DAEMON: $WALLPAPER_DAEMON" + exit 1 + ;; +esac diff --git a/hyprland/.config/rofi/wallpaper/type-1/wallpaper.sh b/hyprland/.config/rofi/wallpaper/type-1/wallpaper.sh index 0fa98ac..80d7332 100755 --- a/hyprland/.config/rofi/wallpaper/type-1/wallpaper.sh +++ b/hyprland/.config/rofi/wallpaper/type-1/wallpaper.sh @@ -18,7 +18,35 @@ show_image_preview() { set_wallpaper() { local wallpaper="$WALLPAPERS_DIR/$1" if [ -f "$wallpaper" ]; then - swww img "$wallpaper" --transition-fps "$SWWW_FPS" --transition-type any --transition-duration "$SWWW_DURATION" + + case $WALLPAPER_DAEMON in + "swaybg") + if [[ $(pidof swww-daemon) ]]; then + pkill swww-daemon + fi + + if [[ $(pidof swaybg) ]]; then + pkill swaybg + fi + + swaybg -i "$wallpaper" + ;; + "swww") + if [[ ! $(pidof swww-daemon) ]]; then + swww init + fi + + if [[ $(pidof swaybg) ]]; then + pkill swaybg + fi + + swww img "$wallpaper" --transition-fps "$SWWW_FPS" --transition-type any --transition-duration "$SWWW_DURATION" + ;; + *) + echo "Unknown value for WALLPAPER_DAEMON: $WALLPAPER_DAEMON" + exit 1 + ;; + esac echo "Wallpaper set to: $wallpaper" else echo "Error: Wallpaper file not found: $wallpaper"