From 1a2284234aa4c7ba71c6a2c451ec38b5c5476ec7 Mon Sep 17 00:00:00 2001 From: obsidrielle <1049617132@qq.com> Date: Mon, 12 May 2025 22:36:16 +0800 Subject: [PATCH 1/5] Feat: switch to video background and colorgen --- .../scripts/color_generation/switchwall.sh | 141 ++++++++++++++++-- 1 file changed, 131 insertions(+), 10 deletions(-) diff --git a/.config/ags/scripts/color_generation/switchwall.sh b/.config/ags/scripts/color_generation/switchwall.sh index b41304112..2caa3ccab 100755 --- a/.config/ags/scripts/color_generation/switchwall.sh +++ b/.config/ags/scripts/color_generation/switchwall.sh @@ -3,6 +3,81 @@ XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" CONFIG_DIR="$XDG_CONFIG_HOME/ags" +THUMBNAIL_DIR="/tmp/mpvpaper_thumbnails" +CUSTOM_DIR="$XDG_CONFIG_HOME/hypr/custom" +RESTORE_SCRIPT_DIR="$CUSTOM_DIR/scripts" +RESTORE_SCRIPT="$RESTORE_SCRIPT_DIR/__restore_video_wallpaper.sh" +EXECS_CONF="$CUSTOM_DIR/execs.conf" + +mkdir -p "$THUMBNAIL_DIR" +mkdir -p "$RESTORE_SCRIPT_DIR" + +is_video() { + local extension="${1##*.}" + [[ "$extension" == "mp4" || "$extension" == "mkv" || "$extension" == "webm" ]] && return 0 || return 1 +} + +kill_existing_mpvpaper() { + # Abort all mpvpapers' instance + pkill -f -9 mpvpaper || true +} + +create_restore_script() { + local video_path=$1 + local video_opts=${2:-"no-audio loop"} + + cat > "$RESTORE_SCRIPT" << EOF +#!/bin/bash +# Generated by switchwall.sh - Don't modify it by yourself. +# Time: $(date) + +pkill -f -9 mpvpaper + +for monitor in \$(hyprctl monitors -j | jq -r '.[] | .name'); do + mpvpaper -f -o "$video_opts" "\$monitor" "$video_path" & + sleep 0.1 +done +EOF + + chmod +x "$RESTORE_SCRIPT" +} + +ensure_restore_exec() { + local restore_line="exec-once=bash $RESTORE_SCRIPT" + + if [ ! -f "$EXECS_CONF" ]; then + echo "$restore_line" >> "$EXECS_CONF" + fi + + if ! grep -q "$restore_line" "$EXECS_CONF"; then + echo "$restore_line" >> "$EXECS_CONF" + fi +} + +remove_restore() { + if [ -f "$RESTORE_SCRIPT" ]; then + rm "$RESTORE_SCRIPT" + fi + + if [ -f "$EXECS_CONF" ]; then + grep -v "exec-once=bash $RESTORE_SCRIPT" "$EXECS_CONF" > "$EXECS_CONF.tmp" + mv "$EXECS_CONF.tmp" "$EXECS_CONF" + fi +} + +save_video_config() { + local path=$1 + local opts=$2 + local exec_cmd=$3 + + echo "VIDEO_PATH=$path" > "$VIDEO_CONFIG" + echo "VIDEO_OPTS=$opts" >> "$VIDEO_CONFIG" + + if [ -n "$exec_cmd" ]; then + echo "EXEC_CMD=$exec_cmd" >> "$VIDEO_CONFIG" + fi +} + switch() { imgpath=$1 read scale screenx screeny screensizey < <(hyprctl monitors -j | jq '.[] | select(.focused) | .scale, .x, .y, .height' | xargs) @@ -17,16 +92,65 @@ switch() { exit 0 fi - # agsv1 run-js "wallpaper.set('')" - # sleep 0.1 && agsv1 run-js "wallpaper.set('${imgpath}')" & - swww img "$imgpath" --transition-step 100 --transition-fps 120 \ - --transition-type grow --transition-angle 30 --transition-duration 1 \ - --transition-pos "$cursorposx, $cursorposy_inverted" + kill_existing_mpvpaper + + if is_video "$imgpath"; then + missing_deps=() + if ! command -v mpvpaper &> /dev/null; then + missing_deps+=("mpvpaper") + fi + + if ! command -v ffmpeg &> /dev/null; then + missing_deps+=("ffmpeg") + fi + + if [ ${#missing_deps[@]} -gt 0 ]; then + echo "Missing deps: ${missing_deps[*]}" + echo "Arch: " + echo " yay -S ${missing_deps[*]}" + exit 0 + fi + + local video_path=$1 + local video_opts=${2:-"no-audio loop hwdec=auto scale=bilinear interpolation=no video-sync=display-resample panscan=1.0 video-scale-x=1.0 video-scale-y=1.0 video-align-x=0.5 video-align-y=0.5"} + + monitors=$(hyprctl monitors -j | jq -r '.[] | .name') + + for monitor in $monitors; do + mpvpaper -o "$video_opts" "$monitor" "$video_path" & + sleep 0.1 + done + + # We take the first frame of video to colorgen + thumbnail="$THUMBNAIL_DIR/$(basename $"imgpath").jpg" + ffmpeg -y -i "$imgpath" -vframes 1 "$thumbnail" 2>/dev/null + + if [ -f "$thumbnail" ]; then + "$CONFIG_DIR"/scripts/color_generation/colorgen.sh "$thumbnail" --apply --smart + create_restore_script "$video_path" "$video_opts" + ensure_restore_exec + else + echo "Cannot create image to colorgen" + fi + else + # agsv1 run-js "wallpaper.set('')" + # sleep 0.1 && agsv1 run-js "wallpaper.set('${imgpath}')" & + swww img "$imgpath" --transition-step 100 --transition-fps 120 \ + --transition-type grow --transition-angle 30 --transition-duration 1 \ + --transition-pos "$cursorposx, $cursorposy_inverted" + + "$CONFIG_DIR"/scripts/color_generation/colorgen.sh "$imgpath" --apply --smart + remove_restore + fi } if [ "$1" == "--noswitch" ]; then - imgpath=$(swww query | awk -F 'image: ' '{print $2}') - # imgpath=$(agsv1 run-js 'wallpaper.get(0)') + if pgrep -f mpvpaper > /dev/null; then + imgpath=$(ps -eo cmd | grep mpvpaper | grep -v grep | awk '{for(i=NF;i>0;i--) if($i!~/^-/) {print $i; break}}') + else + imgpath=$(swww query | awk -F 'image: ' '{print $2}') + # imgpath=$(agsv1 run-js 'wallpaper.get(0)') + fi elif [[ "$1" ]]; then switch "$1" else @@ -35,6 +159,3 @@ else cd "$(xdg-user-dir PICTURES)/Wallpapers" || cd "$(xdg-user-dir PICTURES)" || return 1 switch "$(yad --width 1200 --height 800 --file --add-preview --large-preview --title='Choose wallpaper')" fi - -# Generate colors for ags n stuff -"$CONFIG_DIR"/scripts/color_generation/colorgen.sh "${imgpath}" --apply --smart From cd9167344f9d8954051c6f8152655181e32bd6e1 Mon Sep 17 00:00:00 2001 From: obsidrielle <1049617132@qq.com> Date: Tue, 13 May 2025 08:39:30 +0800 Subject: [PATCH 2/5] Feat: switch to video background and colorgen --- .config/ags/scripts/color_generation/switchwall.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.config/ags/scripts/color_generation/switchwall.sh b/.config/ags/scripts/color_generation/switchwall.sh index 2caa3ccab..144616f59 100755 --- a/.config/ags/scripts/color_generation/switchwall.sh +++ b/.config/ags/scripts/color_generation/switchwall.sh @@ -49,6 +49,7 @@ ensure_restore_exec() { echo "$restore_line" >> "$EXECS_CONF" fi + # Add `exec-once = bash $RESTORE_SCRIPT` if not exists if ! grep -q "$restore_line" "$EXECS_CONF"; then echo "$restore_line" >> "$EXECS_CONF" fi @@ -59,9 +60,10 @@ remove_restore() { rm "$RESTORE_SCRIPT" fi + # Delete `exec-once = bach $RESTORE_SCRIPT` if exists if [ -f "$EXECS_CONF" ]; then grep -v "exec-once=bash $RESTORE_SCRIPT" "$EXECS_CONF" > "$EXECS_CONF.tmp" - mv "$EXECS_CONF.tmp" "$EXECS_CONF" + mv "$EXECS_CONF.tmp" "$EXECS_CONF" fi } From a544f09114c62a0845f1469bbaeb93a5c0a6e59d Mon Sep 17 00:00:00 2001 From: obsidrielle <1049617132@qq.com> Date: Wed, 14 May 2025 10:35:11 +0800 Subject: [PATCH 3/5] Refactor: rewrite startup script without modifying config --- .../scripts/color_generation/switchwall.sh | 50 ++++--------------- .../scripts/__restore_video_wallpaper.sh | 2 + .config/hypr/hyprland.conf | 1 - .config/hypr/hyprland/execs.conf | 1 + 4 files changed, 13 insertions(+), 41 deletions(-) create mode 100644 .config/hypr/custom/scripts/__restore_video_wallpaper.sh diff --git a/.config/ags/scripts/color_generation/switchwall.sh b/.config/ags/scripts/color_generation/switchwall.sh index 144616f59..783288d70 100755 --- a/.config/ags/scripts/color_generation/switchwall.sh +++ b/.config/ags/scripts/color_generation/switchwall.sh @@ -9,6 +9,8 @@ RESTORE_SCRIPT_DIR="$CUSTOM_DIR/scripts" RESTORE_SCRIPT="$RESTORE_SCRIPT_DIR/__restore_video_wallpaper.sh" EXECS_CONF="$CUSTOM_DIR/execs.conf" +VIDEO_OPTS="no-audio loop hwdec=auto scale=bilinear interpolation=no video-sync=display-resample panscan=1.0 video-scale-x=1.0 video-scale-y=1.0 video-align-x=0.5 video-align-y=0.5" + mkdir -p "$THUMBNAIL_DIR" mkdir -p "$RESTORE_SCRIPT_DIR" @@ -24,7 +26,6 @@ kill_existing_mpvpaper() { create_restore_script() { local video_path=$1 - local video_opts=${2:-"no-audio loop"} cat > "$RESTORE_SCRIPT" << EOF #!/bin/bash @@ -34,7 +35,7 @@ create_restore_script() { pkill -f -9 mpvpaper for monitor in \$(hyprctl monitors -j | jq -r '.[] | .name'); do - mpvpaper -f -o "$video_opts" "\$monitor" "$video_path" & + mpvpaper -o "$VIDEO_OPTS" "\$monitor" "$video_path" & sleep 0.1 done EOF @@ -42,42 +43,13 @@ EOF chmod +x "$RESTORE_SCRIPT" } -ensure_restore_exec() { - local restore_line="exec-once=bash $RESTORE_SCRIPT" - - if [ ! -f "$EXECS_CONF" ]; then - echo "$restore_line" >> "$EXECS_CONF" - fi - - # Add `exec-once = bash $RESTORE_SCRIPT` if not exists - if ! grep -q "$restore_line" "$EXECS_CONF"; then - echo "$restore_line" >> "$EXECS_CONF" - fi -} - remove_restore() { - if [ -f "$RESTORE_SCRIPT" ]; then - rm "$RESTORE_SCRIPT" - fi + cat > "$RESTORE_SCRIPT.tmp" << EOF +#!/bin/bash +# The content of this script will be generated by switchwall.sh - Don't modify it by yourself. +EOF - # Delete `exec-once = bach $RESTORE_SCRIPT` if exists - if [ -f "$EXECS_CONF" ]; then - grep -v "exec-once=bash $RESTORE_SCRIPT" "$EXECS_CONF" > "$EXECS_CONF.tmp" - mv "$EXECS_CONF.tmp" "$EXECS_CONF" - fi -} - -save_video_config() { - local path=$1 - local opts=$2 - local exec_cmd=$3 - - echo "VIDEO_PATH=$path" > "$VIDEO_CONFIG" - echo "VIDEO_OPTS=$opts" >> "$VIDEO_CONFIG" - - if [ -n "$exec_cmd" ]; then - echo "EXEC_CMD=$exec_cmd" >> "$VIDEO_CONFIG" - fi + mv "$RESTORE_SCRIPT.tmp" $RESTORE_SCRIPT } switch() { @@ -114,12 +86,11 @@ switch() { fi local video_path=$1 - local video_opts=${2:-"no-audio loop hwdec=auto scale=bilinear interpolation=no video-sync=display-resample panscan=1.0 video-scale-x=1.0 video-scale-y=1.0 video-align-x=0.5 video-align-y=0.5"} - + monitors=$(hyprctl monitors -j | jq -r '.[] | .name') for monitor in $monitors; do - mpvpaper -o "$video_opts" "$monitor" "$video_path" & + mpvpaper -o "$VIDEO_OPTS" "$monitor" "$video_path" & sleep 0.1 done @@ -130,7 +101,6 @@ switch() { if [ -f "$thumbnail" ]; then "$CONFIG_DIR"/scripts/color_generation/colorgen.sh "$thumbnail" --apply --smart create_restore_script "$video_path" "$video_opts" - ensure_restore_exec else echo "Cannot create image to colorgen" fi diff --git a/.config/hypr/custom/scripts/__restore_video_wallpaper.sh b/.config/hypr/custom/scripts/__restore_video_wallpaper.sh new file mode 100644 index 000000000..ea4b4fbd3 --- /dev/null +++ b/.config/hypr/custom/scripts/__restore_video_wallpaper.sh @@ -0,0 +1,2 @@ +#!/bin/bash +# The content of this script will be generated by switchwall.sh - Don't modify it by yourself. diff --git a/.config/hypr/hyprland.conf b/.config/hypr/hyprland.conf index aa7c076c8..b34a3fb97 100644 --- a/.config/hypr/hyprland.conf +++ b/.config/hypr/hyprland.conf @@ -15,4 +15,3 @@ source=~/.config/hypr/custom/execs.conf source=~/.config/hypr/custom/general.conf source=~/.config/hypr/custom/rules.conf source=~/.config/hypr/custom/keybinds.conf - diff --git a/.config/hypr/hyprland/execs.conf b/.config/hypr/hyprland/execs.conf index 232f7e1da..1c3ab3b14 100644 --- a/.config/hypr/hyprland/execs.conf +++ b/.config/hypr/hyprland/execs.conf @@ -1,5 +1,6 @@ # Bar, wallpaper exec-once = swww-daemon --format xrgb +exec-once = bash ~/.config/hypr/custom/scripts/__restore_video_wallpaper.sh exec-once = /usr/lib/geoclue-2.0/demos/agent & gammastep exec-once = agsv1 & From 8c62520666e9f60b713e8fbff3b04fd535fcf132 Mon Sep 17 00:00:00 2001 From: obsidrielle <1049617132@qq.com> Date: Wed, 14 May 2025 10:45:01 +0800 Subject: [PATCH 4/5] Refactor: consistently use temporary files and mv (atomic operation) --- .config/ags/scripts/color_generation/switchwall.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.config/ags/scripts/color_generation/switchwall.sh b/.config/ags/scripts/color_generation/switchwall.sh index 783288d70..1d295ff48 100755 --- a/.config/ags/scripts/color_generation/switchwall.sh +++ b/.config/ags/scripts/color_generation/switchwall.sh @@ -27,7 +27,7 @@ kill_existing_mpvpaper() { create_restore_script() { local video_path=$1 - cat > "$RESTORE_SCRIPT" << EOF + cat > "$RESTORE_SCRIPT.tmp" << EOF #!/bin/bash # Generated by switchwall.sh - Don't modify it by yourself. # Time: $(date) @@ -40,6 +40,7 @@ for monitor in \$(hyprctl monitors -j | jq -r '.[] | .name'); do done EOF + mv "$RESTORE_SCRIPT.tmp" $RESTORE_SCRIPT chmod +x "$RESTORE_SCRIPT" } From def2d6f3839671e2c5369fcf947066e5d259a1f6 Mon Sep 17 00:00:00 2001 From: obsidrielle <1049617132@qq.com> Date: Wed, 14 May 2025 12:01:51 +0800 Subject: [PATCH 5/5] Style: remove unused variables and args --- .config/ags/scripts/color_generation/switchwall.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.config/ags/scripts/color_generation/switchwall.sh b/.config/ags/scripts/color_generation/switchwall.sh index 1d295ff48..e3ab24ded 100755 --- a/.config/ags/scripts/color_generation/switchwall.sh +++ b/.config/ags/scripts/color_generation/switchwall.sh @@ -7,7 +7,6 @@ THUMBNAIL_DIR="/tmp/mpvpaper_thumbnails" CUSTOM_DIR="$XDG_CONFIG_HOME/hypr/custom" RESTORE_SCRIPT_DIR="$CUSTOM_DIR/scripts" RESTORE_SCRIPT="$RESTORE_SCRIPT_DIR/__restore_video_wallpaper.sh" -EXECS_CONF="$CUSTOM_DIR/execs.conf" VIDEO_OPTS="no-audio loop hwdec=auto scale=bilinear interpolation=no video-sync=display-resample panscan=1.0 video-scale-x=1.0 video-scale-y=1.0 video-align-x=0.5 video-align-y=0.5" @@ -40,7 +39,7 @@ for monitor in \$(hyprctl monitors -j | jq -r '.[] | .name'); do done EOF - mv "$RESTORE_SCRIPT.tmp" $RESTORE_SCRIPT + mv "$RESTORE_SCRIPT.tmp" "$RESTORE_SCRIPT" chmod +x "$RESTORE_SCRIPT" } @@ -50,7 +49,7 @@ remove_restore() { # The content of this script will be generated by switchwall.sh - Don't modify it by yourself. EOF - mv "$RESTORE_SCRIPT.tmp" $RESTORE_SCRIPT + mv "$RESTORE_SCRIPT.tmp" "$RESTORE_SCRIPT" } switch() { @@ -96,12 +95,12 @@ switch() { done # We take the first frame of video to colorgen - thumbnail="$THUMBNAIL_DIR/$(basename $"imgpath").jpg" + thumbnail="$THUMBNAIL_DIR/$(basename "$imgpath").jpg" ffmpeg -y -i "$imgpath" -vframes 1 "$thumbnail" 2>/dev/null if [ -f "$thumbnail" ]; then "$CONFIG_DIR"/scripts/color_generation/colorgen.sh "$thumbnail" --apply --smart - create_restore_script "$video_path" "$video_opts" + create_restore_script "$video_path" else echo "Cannot create image to colorgen" fi