aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/UserScripts/WallpaperSelect.sh
blob: ff8469925487ca850da677569667091a72f00545 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
# /* ---- 💫 https://github.com/JaKooLit 💫 ---- */ 
# This script for selecting wallpapers (SUPER W)

# WALLPAPERS PATH
terminal=kitty
wallDIR="$HOME/Pictures/wallpapers"
SCRIPTSDIR="$HOME/.config/hypr/scripts"
wallpaper_current="$HOME/.config/hypr/wallpaper_effects/.wallpaper_current"

# Directory for swaync
iDIR="$HOME/.config/swaync/images"
iDIRi="$HOME/.config/swaync/icons"

# swww transition config
FPS=60
TYPE="any"
DURATION=2
BEZIER=".43,1.19,1,.4"
SWWW_PARAMS="--transition-fps $FPS --transition-type $TYPE --transition-duration $DURATION --transition-bezier $BEZIER"

# Check if package bc exists
if ! command -v bc &>/dev/null; then
    notify-send -i "$iDIR/ja.png" "bc missing" "Install package bc first"
    exit 1
fi

# Variables
rofi_theme="$HOME/.config/rofi/config-wallpaper.rasi"
focused_monitor=$(hyprctl monitors -j | jq -r '.[] | select(.focused) | .name')

# Ensure focused_monitor is detected
if [[ -z "$focused_monitor" ]]; then
    notify-send -i "$iDIR/ja.png" "E-R-R-O-R" "Could not detect focused monitor"
    exit 1
fi

# Monitor details
scale_factor=$(hyprctl monitors -j | jq -r --arg mon "$focused_monitor" '.[] | select(.name == $mon) | .scale')
monitor_height=$(hyprctl monitors -j | jq -r --arg mon "$focused_monitor" '.[] | select(.name == $mon) | .height')

icon_size=$(echo "scale=1; ($monitor_height * 3) / ($scale_factor * 150)" | bc)
adjusted_icon_size=$(echo "$icon_size" | awk '{if ($1 < 15) $1 = 20; if ($1 > 25) $1 = 25; print $1}')
rofi_override="element-icon{size:${adjusted_icon_size}%;}"

# Kill existing wallpaper daemons
kill_wallpaper_for_video() {	
	killall swww
	killall mpvpaper
    pkill swaybg
    pkill hyprpaper
}

kill_wallpaper_for_image() {
	killall mpvpaper
    pkill swaybg
    pkill hyprpaper
}

# Retrieve wallpapers (both images & videos)
mapfile -d '' PICS < <(find -L "${wallDIR}" -type f \( \
    -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" -o \
    -iname "*.bmp" -o -iname "*.tiff" -o -iname "*.webp" -o \
    -iname "*.mp4" -o -iname "*.mkv" -o -iname "*.mov" -o -iname "*.webm" \) -print0)

RANDOM_PIC="${PICS[$((RANDOM % ${#PICS[@]}))]}"
RANDOM_PIC_NAME=". random"

# Rofi command
rofi_command="rofi -i -show -dmenu -config $rofi_theme -theme-str $rofi_override"

# Sorting Wallpapers
menu() {
    IFS=$'\n' sorted_options=($(sort <<<"${PICS[*]}"))
    
    printf "%s\x00icon\x1f%s\n" "$RANDOM_PIC_NAME" "$RANDOM_PIC"
    
    for pic_path in "${sorted_options[@]}"; do
        pic_name=$(basename "$pic_path")
        printf "%s\x00icon\x1f%s\n" "$(echo "$pic_name" | cut -d. -f1)" "$pic_path"
    done
}

# Offer SDDM Sequioa Wallpaper Option (only for non-video wallpapers)
set_sddm_wallpaper() {
    sleep 1
    sddm_sequoia="/usr/share/sddm/themes/sequoia_2"

    if [ -d "$sddm_sequoia" ]; then

        # Check if yad is running to avoid multiple notifications
        if pidof yad > /dev/null; then
            killall yad
        fi
        
        if yad --info --text="Set current wallpaper as SDDM background?\n\nNOTE: This only applies to SEQUOIA SDDM Theme" \
        --text-align=left \
        --title="SDDM Background" \
        --timeout=5 \
        --timeout-indicator=right \
        --button="yes:0" \
        --button="no:1"; then

            # Check if terminal exists
            if ! command -v "$terminal" &>/dev/null; then
            notify-send -i "$iDIR/ja.png" "Missing $terminal" "Install $terminal to enable setting of wallpaper background"
                exit 1
            fi

            # Open terminal to enter password
            $terminal -e bash -c "echo 'Enter your password to set wallpaper as SDDM Background'; \
            sudo cp -r $wallpaper_current '$sddm_sequoia/backgrounds/default' && \
            notify-send -i '$iDIR/ja.png' 'SDDM' 'Background SET'"
        fi
    fi
}

# setting up $HOME/.config/hypr/UserConfigs/Startup_Apps.conf
modify_startup_config() {
    local selected_file="$1"

    # Path to the configuration file
    local startup_config="$HOME/.config/hypr/UserConfigs/Startup_Apps.conf"

    # Check if it's a live wallpaper (video)
    if [[ "$selected_file" =~ \.(mp4|mkv|mov|webm)$ ]]; then
        # Comment out the line for swww-daemon
        sed -i '/^\s*exec-once\s*=\s*swww-daemon\s*--format\s*xrgb.*$/s/^/\#/' "$startup_config"

        # Update the livewallpaper variable with the selected video path (using $HOME)
        selected_file="${selected_file/#$HOME/\$HOME}"  # Replace /home/user with $HOME
        sed -i "s|^\$livewallpaper=.*|\$livewallpaper=\"$selected_file\"|" "$startup_config"

        # Uncomment the mpvpaper line and set it with the live wallpaper path
        sed -i '/^\s*#\s*exec-once\s*=\s*mpvpaper\s*.*$/s/^#\s*//; s|mpvpaper\s*'.*'|mpvpaper '*' -o \"no-audio --loop\" \"\$livewallpaper\"|' "$startup_config"

        echo "Configured for live wallpaper (video)."
    else
        sed -i 's/^#\(exec-once = swww-daemon.*\)$/\1/' "$startup_config"

        # Comment out the mpvpaper line 
        sed -i '/^exec-once = mpvpaper\s*'.*'/s/^/#/' "$startup_config"
    fi
}

# Apply Image Wallpaper
apply_image_wallpaper() {
    local image_path="$1"

    kill_wallpaper_for_image

    # Start swww-daemon if not running
    if ! pgrep -x "swww-daemon" > /dev/null; then
        echo "Starting swww-daemon..."
        swww-daemon --format xrgb &
    fi

    # Apply static image wallpaper using swww
    sleep 1
    swww img -o "$focused_monitor" "$image_path" $SWWW_PARAMS

    # Run additional scripts
    "$SCRIPTSDIR/WallustSwww.sh"
    wait $!
    sleep 2
    "$SCRIPTSDIR/Refresh.sh"
    sleep 1

    # set SDDM wallpaper
    set_sddm_wallpaper
}

apply_video_wallpaper() {
    local video_path="$1"

    # Check if mpvpaper is installed
    if ! command -v mpvpaper &> /dev/null; then
        notify-send -i "$iDIR/ja.png" "E-R-R-O-R" "mpvpaper not found"
        return 1  
    fi
    kill_wallpaper_for_video

    # Apply video wallpaper using mpvpaper, adding a unique argument
    mpvpaper '*' -o "load-scripts=no no-audio --loop --unique-wallpaper-process" "$video_path" &
}

# Main function
main() {
    choice=$(menu | $rofi_command)
    choice=$(echo "$choice" | xargs)
    RANDOM_PIC_NAME=$(echo "$RANDOM_PIC_NAME" | xargs)

    if [[ -z "$choice" ]]; then
        echo "No choice selected. Exiting."
        exit 0
    fi

    # Handle random selection correctly
    if [[ "$choice" == "$RANDOM_PIC_NAME" ]]; then
        choice=$(basename "$RANDOM_PIC")
    fi

    # Find the selected file
    selected_file=""
    for pic in "${PICS[@]}"; do
        if [[ "$(basename "$pic")" == "$choice"* ]]; then
            selected_file="$pic"
            break
        fi
    done

    if [[ -z "$selected_file" ]]; then
        echo "File not found."
        exit 1
    fi

	# Modify the Startup_Apps.conf file based on wallpaper type
    modify_startup_config "$selected_file"

    # **CHECK FIRST** if it's a video or an image **before calling any function**
    if [[ "$selected_file" =~ \.(mp4|mkv|mov|webm|MP4|MKV|MOV|WEBM)$ ]]; then
        apply_video_wallpaper "$selected_file"
    else
        apply_image_wallpaper "$selected_file"
    fi
}

# Check if rofi is already running
if pidof rofi > /dev/null; then
    pkill rofi
fi

main
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage