widget: store supported align and use to render flex widgets
[awesome.git] / utils / awsetbg
blob1dba709a5bbe6d9b058df8c1ba37ea59a364c8c4
1 #!/bin/sh
3 # Set wallpaper for awesome.
4 # Copyright (c) 2008 Julien Danjou <julien@danjou.info>
5 #
6 # Derived from fbsetbg:
7 # Copyright (c) 2003-2004 Han Boetes <han@mijncomputer.nl>
9 # Permission is hereby granted, free of charge, to any person obtaining
10 # a copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to
14 # permit persons to whom the Software is furnished to do so, subject to
15 # the following conditions:
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 # Portability notes:
29 # To guarantee this script works on all platforms that support awesome
30 # please keep the following restrictions in mind:
32 # - don't use if ! command;, use command; if [ $? -ne 0 ];
33 # - don't use [ -e file ] use [ -r file ]
34 # - don't use $(), use ``
35 # - don't use ~, use ${HOME}
36 # - don't use id -u or $UID, use whoami
37 # - don't use echo -e
38 # - getopts won't work on all platforms, but the config-file can
39 # compensate for that.
40 # - various software like grep/sed/perl may be not present or not
41 # the version you have. for example grep '\W' only works on gnu-grep.
42 # Keep this in mind, use bare basic defaults.
43 # - Do _NOT_ suggest to use #!/bin/bash. Not everybody uses bash.
45 # TODO purheps: xprop -root _WIN_WORKSPACE
46 # _NET_CURRENT_DESKTOP
48 if [ -z "$XDG_CACHE_HOME" ]
49 then
50 CONFIG_DIR="${HOME}/.cache/awesome"
51 else
52 CONFIG_DIR="${XDG_CACHE_HOME}/awesome"
55 # The wallpapersetter is selected in this order
56 wpsetters="${wpsetters:=Esetroot wmsetbg feh hsetroot chbg display qiv xv xsri xli xsetbg}" # broken icewmbg'
57 lastwallpaper="${CONFIG_DIR}/lastwallpaper"
60 WHOAMI=`whoami`
61 [ "$WHOAMI" = root ] && PATH=/bin:/usr/bin/:/usr/local/bin:/usr/X11R6/bin
63 command="`basename \"$0\"`"
66 # Functions
67 display_usage() {
68 cat << EOF
69 Usage: $command [-u/-U [wallpapersetter]] [-fFcCtTaA] /path/to/wallpaper
70 $command [-u/-U [wallpapersetter]] [-fFcCtTaA] -r/-R /path/to/wallpaperdirectory
71 $command [-lhip]
72 Use \`\`$command -h'' for a complete help message.
74 EOF
77 display_help() {
78 display_usage
79 cat << EOF
81 Options:
83 -f Set fullscreen wallpaper (default).
84 -c Set centered wallpaper.
85 -t Set tiled wallpaper.
86 -a Set maximized wallpaper, preserving aspect.
87 ( if your bgsetter doesn't support this
88 we fall back to -f )
89 -u Use specified wallpapersetter, use no argument to forget.
90 -r set random wallpaper from a directory
92 -F,-C,-T,-A,-U,-B,-R same as uncapsed but without remembering.
94 -h Display this help.
96 -l Set previous wallpaper.
98 -i Information about selected wallpaper command.
100 Files:
102 \$XDG_CACHE_HOME/awesome/lastwallpaper In this file the wallpaper you set
103 will be stored, for the -l option.
104 Environment variables:
105 wpsetters Wallpapersetters to use.
106 example:
107 wpsetters=feh awsetbg wallpaper.jpg
109 DISPLAY The display you want to set the wallpaper on.
110 example:
111 DISPLAY=:0.0 awsetbg -l
116 find_it() {
117 [ -n "$1" ] && hash $1 2> /dev/null
120 message() {
121 extra_args="-default okay"
122 if find_it gxmessage; then
123 gxmessage $extra_args -center "$command: $@" &
124 else
125 xmessage $extra_args -center "$command: $@" &
129 remembercommand() {
130 grep -vs "|${DISPLAY}$" ${lastwallpaper} > ${lastwallpaper}.tmp
131 mv -f ${lastwallpaper}.tmp ${lastwallpaper}
132 # Make dir/../../path/file.jpg work
133 case $wallpaper in
134 # no spaces allowed between the varname and '|'
135 /*) echo $option $option2"|$wallpaper|$style|"$DISPLAY >> $lastwallpaper ;;
136 *) echo $option $option2"|$PWD/$wallpaper|$style|"$DISPLAY >> $lastwallpaper ;;
137 esac
140 debugawsetbg() {
141 echo
142 echo $debugstory
143 echo $sad_esetroot_story
144 exit 0
147 if [ $# -eq 0 ]; then
148 message "no options given"
149 display_usage
150 exit 1
153 # create directory and last wallpaper file
154 if [ ! -d "${CONFIG_DIR}" ]; then
155 mkdir -p "${CONFIG_DIR}"
156 touch ${lastwallpaper}
159 unset debug setterfromcommandline
160 # Parse command-line options
161 while [ $# -gt 0 ]; do
162 case "$1" in
164 if find_it "$2"; then
165 grep -v wpsetters $lastwallpaper > ${lastwallpaper}.tmp
166 echo "wpsetters $2" >> ${lastwallpaper}.tmp
167 mv ${lastwallpaper}.tmp $lastwallpaper
168 WPSETTER=$2
169 setterfromcommandline=true
170 elif [ -z "$2" ]; then
171 grep -v wpsetters $lastwallpaper > ${lastwallpaper}.tmp
172 mv ${lastwallpaper}.tmp $lastwallpaper
173 message "wpsetter removed from historyfile"
174 exit 0
175 else
176 message "Couldn't find \"$2\" for wallpapersetter"
177 display_usage
178 exit 1
180 shift 2 ;;
182 if find_it "$2"; then
183 WPSETTER=$2
184 setterfromcommandline=true
185 else
186 message "Couldn't find \"$2\" for wallpapersetter"
187 display_usage
188 exit 1
190 shift 2 ;;
191 -i) debug=true
192 break ;;
193 -a) option='$aspect'
194 shift ;;
195 -f) option='$full'
196 shift ;;
197 -c) option='$center'
198 shift ;;
199 -t) option='$tile'
200 shift ;;
201 -A) option='$aspect'
202 remember=false
203 shift ;;
204 -F) option='$full'
205 remember=false
206 shift ;;
207 -C) option='$center'
208 remember=false
209 shift ;;
210 -T) option='$tile'
211 remember=false
212 shift ;;
213 -r) option2=$option
214 option=random
215 wallpaper=$2 # in this case it's a dir
216 break ;;
217 -R) option2=$option
218 option=random
219 wallpaper=$2 # in this case it's a dir
220 remember=false
221 break ;;
223 if [ -r "$lastwallpaper" ]; then
224 option=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f1`
225 option2=`echo $option|cut -d' ' -f2`
226 option=`echo $option|cut -d' ' -f1`
227 wallpaper=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f2`
228 if [ -z "$wallpaper" ]; then
229 option=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f1`
230 option2=`echo $option|cut -d' ' -f2`
231 option=`echo $option|cut -d' ' -f1`
232 wallpaper=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f2`
234 if [ -z "$wallpaper" ]; then
235 message "No previous wallpaper recorded for display ${DISPLAY}"
236 exit 1
238 else
239 message "No previous wallpaper recorded for display ${DISPLAY}"
240 exit 1
242 remember=false
243 break ;;
245 if [ -r "$lastwallpaper" ]; then
246 option=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f1`
247 option2=`echo $option|cut -d' ' -f2`
248 option=`echo $option|cut -d' ' -f1`
249 style=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f3`
250 wallpaper=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f2`
251 if [ -z "$wallpaper" ]; then
252 option=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f1`
253 option2=`echo $option|cut -d' ' -f2`
254 option=`echo $option|cut -d' ' -f1`
255 style=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f3`
256 wallpaper=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f2`
259 if [ "$style" != "style" -a -n "$wallpaper" ]; then
260 remember=false
261 break
263 style="style"
264 shift ;;
266 style="style"
267 shift ;;
268 -h) display_help ; exit 0 ;;
270 message "$command doesn't recognize -- gnu-longopts."
271 message 'Use $command -h for a help message.'
272 display_usage
273 exit 1 ;;
275 message "unrecognized option "\`"$1'"
276 display_usage
277 exit 1 ;;
279 if [ "$option" = random ]; then
280 option='$aspect'
281 elif [ ! -r "$1" ]; then
282 message "$1 isn't an existing wallpaper or a valid option."
283 display_usage
284 exit 1
285 elif [ -z "$1" ]; then
286 message 'No wallpaper to set'
287 display_usage
288 exit 1
289 else
290 wallpaper=$1
291 break
292 fi ;;
293 esac
294 done
296 # Find the default wallpapersetter
297 if [ "$setterfromcommandline" != true ]; then
298 if [ -r "$lastwallpaper" ]; then
299 wpsetters="`awk '/wpsetters/ {print $2}' $lastwallpaper` $wpsetters"
301 for wpsetter in $wpsetters; do
302 if find_it $wpsetter; then
303 if [ ! "$wpsetter" = Esetroot ]; then
304 WPSETTER=$wpsetter
305 break
306 elif ldd `which Esetroot`|grep libImlib 2>&1 > /dev/null; then
307 WPSETTER=$wpsetter
308 break
309 else
310 sad_esetroot_story="I also found Esetroot, but it doesn't have support for setting wallpapers. You need to install libimlib2 and rebuild Eterm to get it working."
313 done
316 standardrant=\
317 "$WPSETTER doesn't set the wallpaper properly. Transparency for awesome and
318 apps like aterm and xchat won't work right with it. Consider installing
319 feh, wmsetbg (from windowmaker) or Esetroot (from Eterm) and I'll use
320 them instead."
322 standardok=\
323 "$WPSETTER is a nice wallpapersetter. You won't have any problems."
325 case $WPSETTER in
326 chbg)
327 full='-once -mode maximize'
328 tile='-once -mode tile'
329 center='-once -mode center'
330 aspect='-once -mode smart -max_grow 100 -max_size 100'
331 debugstory="chbg supports all features but it doesn't report errors. I reported this bug to the chbg developers."
333 xsri)
334 full='--center-x --center-y --scale-width=100 --scale-height=100'
335 tile='--tile'
336 center='--center-x --center-y --color=black'
337 aspect='--center-x --center-y --scale-width=100 --scale-height=100 --keep-aspect --color=black'
338 debugstory=$standardok
340 display)
341 full="-sample `xwininfo -root 2> /dev/null|awk '/geom/{print $2}'` -window root"
342 tile='-window root'
343 center='-backdrop -window root'
344 aspect="`xwininfo -root 2> /dev/null|grep geom` -window root"
345 debugstory=$standardrant
347 Esetroot)
348 full='-scale'
349 tile=''
350 center='-c'
351 aspect='-fit'
352 debugstory=$standardok
354 wmsetbg)
355 full='-s -S'
356 tile='-t'
357 center='-b black -e'
358 aspect='-b black -a -S'
359 debugstory=$standardok
361 xsetbg)
362 tile='-border black'
363 center='-center -border black'
364 aspect='-fullscreen -border black'
365 full=$aspect #broken
366 debugstory="xsetbg is actually xli. The fillscreen option (-f) is broken, defaults to (-a). $standardrant"
368 xli)
369 tile='-onroot -quiet -border black'
370 center='-center -onroot -quiet -border black'
371 aspect='-fullscreen -onroot -quiet -border black'
372 full=$aspect #broken
373 debugstory="The fillscreen option (-f) is broken, defaults to (-a). $standardrant"
375 qiv)
376 full='--root_s'
377 tile='--root_t'
378 center='--root'
379 aspect='-m --root'
380 debugstory=$standardrant
383 full='-max -smooth -root -quit'
384 tile='-root -quit'
385 center='-rmode 5 -root -quit'
386 aspect='-maxpect -smooth -root -quit'
387 debugstory=$standardrant
389 feh)
390 full='--bg-scale'
391 tile='--bg-tile'
392 center='--bg-center'
393 aspect=$full
394 debugstory=$standardok
396 hsetroot)
397 full='-fill'
398 tile='-tile'
399 center='-center'
400 aspect='-full'
401 debugstory=$standardok
403 icewmbg)
404 tile='-s'
405 full=$tile
406 center=$tile
407 aspect=$tile
408 debugstory="icewmbg does support transparency, but only tiling. And I noticed odd
409 errormessages with aterm. Don't use it unless you have to."
412 message \
413 "I can't find an app to set the wallpaper with. You can install one in
414 many many ways but I will give you some simple advice: install Eterm and
415 you're set. Eterm provides Esetroot and thats a great wallpaper setter. I
416 recommend you install the package provided by your distro."
417 exit 1
419 esac
421 if [ "$debug" = true ]; then
422 debugawsetbg
423 exit 0
426 option=${option:='$full'}
427 option2=${option2:='$full'}
430 if [ -z "$DISPLAY" ]; then
431 message "You are not connected to an X session\nPerhaps you should set the DISPLAY environment variable?"
432 exit 1
436 # random wallpaper code
437 if [ "$option" = random ]; then
438 # Lets make one thing clear...
439 wallpaperdir="$wallpaper"
440 if [ -z "$wallpaperdir" ]; then
441 message "No random wallpaper directory specified."
442 exit 1
444 if [ -d "$wallpaperdir" ]; then
445 number_of_wallpapers=`ls "$wallpaperdir"|wc -l`
446 if find_it random_number; then
447 randomnumber=`random_number`
448 # some shells don't support $RANDOM
449 elif [ -z "$RANDOM" ]; then
450 randomnumber=`(echo $$ ;time ps; w ; date )2>&1 | cksum | cut -f1 -d' '`
451 else
452 randomnumber="$RANDOM"
454 wallpapernumber=`expr $randomnumber % $number_of_wallpapers + 1`
455 #remember previous wallpaper
456 if [ ! "$remember" = false ]; then
457 remembercommand
459 remember=false
460 #set -x
461 wallpaper="$wallpaperdir/`ls \"$wallpaperdir\"|sed -n ${wallpapernumber}p`"
462 option=$option2 # have to choose something...
463 else
464 message "Invalid random wallpaper directory specified."
465 exit 1
470 if [ ! -r "$wallpaper" ]; then
471 message "Can't find wallpaper $wallpaper"
472 exit 1
476 $WPSETTER `eval echo $option` "$wallpaper"
477 if [ $? -ne 0 ]; then
478 message "Something went wrong while setting the wallpaper.
479 Run '$WPSETTER "`eval echo $option` $wallpaper"' from an xterm to find out what."
480 exit 1
484 #remember previous wallpaper
485 if [ ! "$remember" = false ]; then
486 remembercommand