-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackgroundchooser
executable file
·187 lines (162 loc) · 5.84 KB
/
backgroundchooser
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
#!/bin/bash
# backgroundchooser
# Chooses a random background file from a dir with backrgrounds.
# Copyright (C) 2014-2015 Alexander Swen <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Alexander Swen
# CHANGELOG:
# 2018-01-25 A.Swen Created.
# 2018-11-04 A.Swen Added:
# - Suggest feature to support other tools (see examples).
# - Link feature to support i3lock from within the i3 config.
# - Function to transform JPG's to PNG files (because
# https://github.com/i3/i3lock/issues/68).
# - Choice to remove the original JPG file after converion.
# This script requires the tools 'feh' and 'convert' (from Imagemagick)
## USAGE EXAMPLES:
# This script could be run from a users cron
# 15 * * * * /data/git/alex/scripts/backgroundchooser -d /data/backgrounds
# It could be launched each time i3 starts by adding this to the i3 config:
# exec --no-startup-id /data/git/alex/scripts/backgroundchooser --dir /data/backgrounds
# It can also be used to set a random background before i3lock is started:
# bindsym $win+l "exec --no-startup-id /data/git/alex/scripts/backgroundchooser --dir /data/backgrounds --link /tmp/background.png; exec --no-startup-id i3lock -tei /tmp/background.png -c 000000"
# I also use it just before my laptop goes to sleep. I changed /usr/share/acpi-support/screenblank
# which has a line with i3lock (I commented all other lock optiones)
# su "$XUSER" -s /bin/sh -c "/usr/bin/i3lock -tei $(/data/git/alex/scripts/backgroundchooser --dir /data/backgrounds --suggest) -c 000000"
# SETTINGS
date=$(date +%Y%m%d)
me=$(basename $0)
mydir=$(dirname $0)
# Case insensitive list of filetypes
filetypes='*.png'
# FUNCTIONS
die () {
rc=$1
shift
printf '%s\n' "=====================" >&2
printf '%s\n' "==== FATAL ERROR ====" >&2
printf '%s\n\n' "=====================" >&2
printf '%s\n\n' "$@" >&2
exit $rc
}
usage () {
printf '%s\n' "===============" >&2
printf '%s\n' "==== USAGE ====" >&2
printf '%s\n\n' "===============" >&2
printf '%s\n' "Usage: ${me} [--dir|-d (directory with backgrounds)] [--suggest|-s] [--link|-l] [--remove_jpg|-r]" >&2
printf '%s\n\n' "example: ${me} --dir /data/backgrounds --link" >&2
exit 1
}
get_options () {
while [[ $# -gt 0 ]]; do
case "$1" in
--dir|-d)
shift
export backgrounds_dir="$1"
shift
;;
--link|-l)
shift
export link=1
export linktarget="$1"
shift
;;
--remove_jpg|-r)
shift
export remove_jpg=1
;;
--suggest|-s)
shift
export suggest=1
;;
-h|--help)
usage
;;
*)
usage
;;
esac
done
}
convert_to_png () {
[ -x /usr/bin/convert ] || die 20 "Convert not found."
old_background="$background"
background="$(echo $background|sed -e 's?\(^.*\.\)\(jp[e*]g\)$?\1png?')"
[ "$background" = "$old_background" ] && die 14 "Old and new background are the same."
[ -f "background" ] && die 15 "A file with the name $background already exists."
/usr/bin/convert "$old_background" "$background" || die 25 "Converting the background to png format failed."
[ $remove_jpg -eq 1 ] && rm "$old_background"
}
log () { printf '%s %s\n' "$(date +%F' '%T)" "$@"; }
log_warn () { printf '%s %s\n' "$(date +%F' '%T)" "$@" >&2; }
# SCRIPT
link=0
suggest=0
remove_jpg=0
if [[ $# -gt 0 ]];then
get_options $@
fi
# Switch to backgrounds dir or die
[ -n "$backgrounds_dir" ] || backgrounds_dir=$(pwd)
# Build an array with background files
shopt -s nocaseglob
[ -d "$backgrounds_dir" ] && cd "$backgrounds_dir" || die "$backgrounds_dir not found."
background_files=($filetypes)
# The background selection
if [ ${#background_files[*]} -gt 1 ];then
# Pick a random background
background_file="${background_files[$(($RANDOM%${#background_files[*]}))]}"
background="${backgrounds_dir}/${background_file}"
if [[ "$background" =~ ^(.*\.(jpeg|jpg)$) ]] ;then
convert_to_png "$background"
fi
elif [ ${#background_files[*]} -eq 1 ];then
background="$backgrounds_dir/${background_files[0]}"
if [[ "$background" =~ ^(.*\.(jpeg|jpg)$) ]] ;then
convert_to_png "$background"
fi
else
if [ $suggest -eq 1 ];then
# If nothing to be found just throw the default
log_warn "No backgrounds found, falling back to default mosquito."
background=/home/alex/mug.png
else
die 1 "No suitable backgrounds found in $backgrounds_dir."
fi
fi
# Use the background
if [ $suggest -eq 1 ];then
echo $background
elif [ $link -eq 1 ];then
if [ -z "$linktarget" ];then
die 10 "\$linktarget not set"
else
log "Linking $background to $linktarget."
ln -sf "$background" "$linktarget"
fi
else
# Set that background
log "I have chosen $background"
if [ "$XDG_CURRENT_DESKTOP" == "i3" ];then
feh --bg-fill $background
elif [ "$XDG_CURRENT_DESKTOP" == "GNOME" ];then
gsettings set org.gnome.desktop.background picture-uri $background
gsettings set org.gnome.desktop.background picture-uri-dark $background
else
gsettings set org.gnome.desktop.background picture-uri $background
gsettings set org.gnome.desktop.background picture-uri-dark $background
#die 12 "I don't yet know how to set a background on $XDG_CURRENT_DESKTOP"
fi
fi
exit 0
# END