mabox-tools/bin/mbwallpaper

119 lines
3.3 KiB
Plaintext
Raw Normal View History

2021-05-14 19:35:19 +02:00
#!/bin/bash
2021-05-30 01:51:41 +02:00
CONFIG_DIR="$HOME/.config/mbwallpaper"
CONFIG_FILE="$CONFIG_DIR/mbwallpaper.conf"
2021-05-14 19:35:19 +02:00
WALLPAPERS_LIST="$CONFIG_DIR/wplist"
# Make config directory if not exist
mkdir -p $CONFIG_DIR
# If config file not exist create one with defaults
if [ ! -f $CONFIG_FILE ]; then
cat <<EOF > ${CONFIG_FILE}
# Directory with wallpapers
wallpaper_dir=/usr/share/backgrounds/
# Rotate time in seconds
2021-05-28 03:32:29 +02:00
interval=10
2021-05-14 19:35:19 +02:00
# Wallpaper setter program: nitrogen or feh
setter=nitrogen
EOF
fi
# read config variables from file
source <(grep = $CONFIG_FILE)
# decide which setter and command use to set wallpaper
case "$setter" in
2021-05-23 13:18:24 +02:00
nitrogen) command="nitrogen --set-zoom-fill --save";;
2021-05-14 19:35:19 +02:00
feh) command="feh --bg-fill" ;;
2021-05-28 03:32:29 +02:00
pcmanfm) command="pcmanfm -w" ;;
2021-05-14 19:35:19 +02:00
*) echo "Unknown wallpaper setter. Please check your config file $CONFIG_FILE";exit 1;;
esac
2022-02-12 13:37:36 +01:00
#check if wallpaper dir was changed, if so delete wallpaper list
if [ ! -f "$CONFIG_DIR/.lastdir" ]; then
echo "$wallpaper_dir" > "$CONFIG_DIR/.lastdir"
else
lastdir=$(<$CONFIG_DIR/.lastdir)
[[ "$wallpaper_dir" != "$lastdir" ]] && rm "$WALLPAPERS_LIST" && echo "$wallpaper_dir" > "$CONFIG_DIR/.lastdir"
fi
2021-05-28 03:32:29 +02:00
checkwplist() {
2021-05-14 19:35:19 +02:00
# If list does not exist or is empty
if [[ ! -f $WALLPAPERS_LIST || $(wc -l <$WALLPAPERS_LIST) -lt 1 ]]; then
# Prepare list with wallpapers (only PNG and JPG)
2024-09-10 06:55:24 +02:00
ls -1 "$wallpaper_dir"/*{.jpg,.png,.avif} > "$WALLPAPERS_LIST"
2021-05-14 19:35:19 +02:00
fi
2021-05-28 03:32:29 +02:00
}
one() {
checkwplist
line=$(shuf -n 1 $WALLPAPERS_LIST)
#delete it from list
grep -v "$line" "$WALLPAPERS_LIST" > "$CONFIG_DIR/tmpfile"; mv "$CONFIG_DIR/tmpfile" "$WALLPAPERS_LIST"
#run command to set wallpaper
${command} ${line}
exit 0
}
choose() {
2021-05-30 01:51:41 +02:00
case $LANG in
pl*)
TITLE="Wybierz tapetę"
2024-09-10 06:55:24 +02:00
TEXT="\n<b>Strzałki</b> / <b>kółko myszy</b> poprzednia/następna.\n<b>Enter</b> by ustawić tapetę.\n<b>Esc</b> by wyjść."
2021-05-30 01:51:41 +02:00
;;
*)
TITLE="Select Wallpaper"
2024-09-10 06:55:24 +02:00
TEXT="\n<b>Arrows</b> / <b>mousewheel</b> prev/next.\n<b>Enter</b> to set wallpaper.\n<b>Esc</b> to quit."
2021-05-30 01:51:41 +02:00
;;
esac
2021-05-28 03:32:29 +02:00
SCREENSIZE=$(wmctrl -d |grep "*" | awk -F' ' '{print $4}')
W="${SCREENSIZE%x*}"
H="${SCREENSIZE#*x}"
2024-09-16 00:44:33 +02:00
FW=$((W/4))
FH=$((H/4))
X=$((FW*3/2))
2021-05-28 03:32:29 +02:00
Y=$((H-FH-40))
2024-09-10 06:55:24 +02:00
notify-send.sh -t 12000 -i ~/.config/mabox/wpicon.png "$TITLE" "$TEXT"
feh -B "#4D4D4D" -N -x --scale-down -E ${FH} -y ${FW} -^ "Enter=select, Arrows=next/prev Esc=quit" -g ${FW}x${FH}+${X}+${Y} ${wallpaper_dir} -A "${command} '%f'"
2021-05-28 03:32:29 +02:00
}
slideshow() {
while true
do
checkwplist
2021-05-14 19:35:19 +02:00
# if wallpapers list have more than 0 lines
while [ $(wc -l <$WALLPAPERS_LIST) -gt 0 ]
do
# get random line
line=$(shuf -n 1 $WALLPAPERS_LIST)
#delete it from list
grep -v "$line" "$WALLPAPERS_LIST" > "$CONFIG_DIR/tmpfile"; mv "$CONFIG_DIR/tmpfile" "$WALLPAPERS_LIST"
#run command to set wallpaper
${command} ${line}
sleep ${interval}
done
done
2021-05-28 03:32:29 +02:00
}
2024-09-10 06:55:24 +02:00
changedir(){
wdir=${1/$HOME/\~}
#notify-send.sh "dirrrrrrr" "$wdir"
sd "wallpaper_dir=.*$" "wallpaper_dir=${wdir}" ${CONFIG_FILE}
}
2021-05-28 03:32:29 +02:00
usage() {
grep "^#:" $0 | while read DOC; do printf '%s\n' "${DOC###:}"; done
exit
}
case "$1" in
-o|one) one;;
-c|choose) choose;;
-h|--help) usage;;
2024-09-10 06:55:24 +02:00
-s) slideshow;;
-d|changedir) changedir "$2";;
2021-05-28 03:32:29 +02:00
esac
exit 0