Check for xwininfo before using it
[awesome.git] / utils / awsetbg
blob460fc8d3daa6eef864817f0f333e3310faf95115
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 habak feh hsetroot chbg fvwm-root display qiv xv xsri xli xsetbg wmsetbg xsetroot}" # 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] [-m #] /path/to/wallpaper
70 $command [-u/-U [wallpapersetter]] [-fFcCtTaA] [-m #] -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,-R same as lowercase but without remembering.
94 -h Display this help.
96 -l Set previous wallpaper.
98 -i Information about selected wallpaper command.
100 -m # Use montage to create a big image (image should
101 fit screen size). Requires montage(1).
102 # is the number of screen (columns in montage)
103 Files:
105 \$XDG_CACHE_HOME/awesome/lastwallpaper In this file the wallpaper you set
106 will be stored, for the -l option.
107 Environment variables:
108 wpsetters Wallpapersetters to use.
109 example:
110 wpsetters=feh awsetbg wallpaper.jpg
112 DISPLAY The display you want to set the wallpaper on.
113 example:
114 DISPLAY=:0.0 awsetbg -l
119 find_it() {
120 [ -n "$1" ] && which $1 1>/dev/null 2>&1
123 message() {
124 extra_args="-default okay"
125 if find_it gxmessage; then
126 gxmessage $extra_args -center "$command: $@" &
127 else
128 xmessage $extra_args -center "$command: $@" &
132 remembercommand() {
133 grep -vs "|${DISPLAY}$" ${lastwallpaper} > ${lastwallpaper}.tmp
134 mv -f ${lastwallpaper}.tmp ${lastwallpaper}
135 # Make dir/../../path/file.jpg work
136 case $wallpaper in
137 # no spaces allowed between the varname and '|'
138 /*) echo $option $option2"|$wallpaper|$style|"$DISPLAY >> $lastwallpaper ;;
139 *) echo $option $option2"|$PWD/$wallpaper|$style|"$DISPLAY >> $lastwallpaper ;;
140 esac
143 debugawsetbg() {
144 echo
145 echo $debugstory
146 echo $sad_esetroot_story
147 echo $sad_display_story
148 exit 0
151 if [ $# -eq 0 ]; then
152 message "no options given"
153 display_usage
154 exit 1
157 # create directory and last wallpaper file
158 if [ ! -d "${CONFIG_DIR}" ]; then
159 mkdir -p "${CONFIG_DIR}"
160 touch ${lastwallpaper}
163 unset debug setterfromcommandline
164 # Parse command-line options
165 while [ $# -gt 0 ]; do
166 case "$1" in
168 if find_it "$2"; then
169 grep -v wpsetters $lastwallpaper > ${lastwallpaper}.tmp
170 echo "wpsetters $2" >> ${lastwallpaper}.tmp
171 mv ${lastwallpaper}.tmp $lastwallpaper
172 WPSETTER=$2
173 setterfromcommandline=true
174 elif [ -z "$2" ]; then
175 grep -v wpsetters $lastwallpaper > ${lastwallpaper}.tmp
176 mv ${lastwallpaper}.tmp $lastwallpaper
177 message "wpsetter removed from historyfile"
178 exit 0
179 else
180 message "Couldn't find \"$2\" for wallpapersetter"
181 display_usage
182 exit 1
184 shift 2 ;;
186 if find_it "$2"; then
187 WPSETTER=$2
188 setterfromcommandline=true
189 else
190 message "Couldn't find \"$2\" for wallpapersetter"
191 display_usage
192 exit 1
194 shift 2 ;;
195 -i) debug=true
196 break ;;
197 -a) option='$aspect'
198 shift ;;
199 -f) option='$full'
200 shift ;;
201 -c) option='$center'
202 shift ;;
203 -t) option='$tile'
204 shift ;;
205 -A) option='$aspect'
206 remember=false
207 shift ;;
208 -F) option='$full'
209 remember=false
210 shift ;;
211 -C) option='$center'
212 remember=false
213 shift ;;
214 -T) option='$tile'
215 remember=false
216 shift ;;
218 montage=`which montage`
219 if test ! -z "$montage"; then
220 shift
221 cols="$1"; shift
222 fns=""
223 for i in `seq $cols`; do
224 fns="$fns $1";
225 done;
226 shift
227 $montage $fns -geometry +0+0 -tile ${cols}x $CONFIG_DIR/montage.png
228 wallpaper=$CONFIG_DIR/montage.png
229 else
230 shift
231 echo "montage not found. Falling back to single image"
234 -r) option2=$option
235 option=random
236 wallpaper=$2 # in this case it's a dir
237 break ;;
238 -R) option2=$option
239 option=random
240 wallpaper=$2 # in this case it's a dir
241 remember=false
242 break ;;
244 if [ -r "$lastwallpaper" ]; then
245 option=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f1`
246 option2=`echo $option|cut -d' ' -f2`
247 option=`echo $option|cut -d' ' -f1`
248 wallpaper=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f2`
249 if [ -z "$wallpaper" ]; then
250 option=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f1`
251 option2=`echo $option|cut -d' ' -f2`
252 option=`echo $option|cut -d' ' -f1`
253 wallpaper=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f2`
255 if [ -z "$wallpaper" ]; then
256 message "No previous wallpaper recorded for display ${DISPLAY}"
257 exit 1
259 else
260 message "No previous wallpaper recorded for display ${DISPLAY}"
261 exit 1
263 remember=false
264 break ;;
266 if [ -r "$lastwallpaper" ]; then
267 option=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f1`
268 option2=`echo $option|cut -d' ' -f2`
269 option=`echo $option|cut -d' ' -f1`
270 style=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f3`
271 wallpaper=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f2`
272 if [ -z "$wallpaper" ]; then
273 option=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f1`
274 option2=`echo $option|cut -d' ' -f2`
275 option=`echo $option|cut -d' ' -f1`
276 style=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f3`
277 wallpaper=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f2`
280 if [ "$style" != "style" -a -n "$wallpaper" ]; then
281 remember=false
282 break
284 style="style"
285 shift ;;
287 style="style"
288 shift ;;
289 -h) display_help ; exit 0 ;;
291 message "$command doesn't recognize -- gnu-longopts."
292 message 'Use $command -h for a help message.'
293 display_usage
294 exit 1 ;;
296 message "unrecognized option "\`"$1'"
297 display_usage
298 exit 1 ;;
300 if [ "$option" = random ]; then
301 option='$aspect'
302 elif [ ! -r "$1" ]; then
303 message "$1 isn't an existing wallpaper or a valid option."
304 display_usage
305 exit 1
306 elif [ -z "$1" ]; then
307 message 'No wallpaper to set'
308 display_usage
309 exit 1
310 else
311 wallpaper=$1
312 break
313 fi ;;
314 esac
315 done
317 # Find the default wallpapersetter
318 if [ "$setterfromcommandline" != true ]; then
319 if [ -r "$lastwallpaper" ]; then
320 wpsetters="`awk '/wpsetters/ {print $2}' $lastwallpaper` $wpsetters"
322 for wpsetter in $wpsetters; do
323 if find_it $wpsetter; then
324 case $wpsetter in
325 Esetroot)
326 if ldd `which Esetroot`|grep libImlib 2>&1 > /dev/null; then
327 WPSETTER=$wpsetter
328 break
329 else
330 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."
333 display)
334 if find_it xwininfo; then
335 WPSETTER=$wpsetter
336 break
337 else
338 sad_display_story="I also found display, but I need xwininfo for querying the root window geometry."
342 WPSETTER=$wpsetter
343 esac
345 done
348 standardrant=\
349 "$WPSETTER doesn't set the wallpaper properly. Transparency for
350 awesome and apps like aterm and xchat won't work right with
351 it. Consider installing feh, Esetroot
352 (from Eterm) or fvwm-root (from FVWM) and I'll use them instead."
354 standardok=\
355 "$WPSETTER is a nice wallpapersetter. You won't have any problems."
357 case $WPSETTER in
358 chbg)
359 full='-once -mode maximize'
360 tile='-once -mode tile'
361 center='-once -mode center'
362 aspect='-once -mode smart -max_grow 100 -max_size 100'
363 debugstory="chbg supports all features but it doesn't report errors. I reported this bug to the chbg developers."
365 xsri)
366 full='--center-x --center-y --scale-width=100 --scale-height=100'
367 tile='--tile'
368 center='--center-x --center-y --color=black'
369 aspect='--center-x --center-y --scale-width=100 --scale-height=100 --keep-aspect --color=black'
370 debugstory=$standardok
372 display)
373 full="-sample `xwininfo -root 2> /dev/null|awk '/geom/{print $2}'` -window root"
374 tile='-window root'
375 center='-backdrop -window root'
376 aspect="`xwininfo -root 2> /dev/null|grep geom` -window root"
377 debugstory=$standardrant
379 Esetroot)
380 full='-scale'
381 tile=''
382 center='-c'
383 aspect='-fit'
384 debugstory=$standardok
386 wmsetbg)
387 full='-s -S'
388 tile='-t'
389 center='-b black -e'
390 aspect='-b black -a -S'
391 debugstory=$standardok
393 xsetbg)
394 tile='-border black'
395 center='-center -border black'
396 aspect='-fullscreen -border black'
397 full=$aspect #broken
398 debugstory="xsetbg is actually xli. The fillscreen option (-f) is broken, defaults to (-a). $standardrant"
400 xli)
401 tile='-onroot -quiet -border black'
402 center='-center -onroot -quiet -border black'
403 aspect='-fullscreen -onroot -quiet -border black'
404 full=$aspect #broken
405 debugstory="The fillscreen option (-f) is broken, defaults to (-a). $standardrant"
407 qiv)
408 full='--root_s'
409 tile='--root_t'
410 center='--root'
411 aspect='-m --root'
412 debugstory=$standardrant
415 full='-max -smooth -root -quit'
416 tile='-root -quit'
417 center='-rmode 5 -root -quit'
418 aspect='-maxpect -smooth -root -quit'
419 debugstory=$standardrant
421 feh)
422 full='--bg-scale'
423 tile='--bg-tile'
424 center='--bg-center'
425 aspect=$full
426 debugstory=$standardok
428 hsetroot)
429 full='-fill'
430 tile='-tile'
431 center='-center'
432 aspect='-full'
433 debugstory=$standardok
435 habak)
436 full='-ms'
437 tile=''
438 center='-mC'
439 aspect='-mS'
440 debugstory=$standardok
442 fvwm-root)
443 tile='-r'
444 full=$tile
445 center=$tile
446 aspect=$tile
447 debugstory="fvwm-root can retain and publish the Pixmap, thus
448 supports transparency. But only these image formats are supported:
449 XBM, XPM, PNG, SVG and it is rather limited with options."
451 icewmbg)
452 tile='-s'
453 full=$tile
454 center=$tile
455 aspect=$tile
456 debugstory="icewmbg does support transparency, but only tiling. And I noticed odd
457 errormessages with aterm. Don't use it unless you have to."
459 xsetroot)
460 tile='-solid'
461 full=$tile
462 center=$tile
463 aspect=$tile
464 debugstory="I can't find an app to set the wallpaper with. Using xsetroot to set
465 a solid background. If you want to have a background image you should install Esetroot or feh."
466 wallpaper='#535d6c'
469 message \
470 "I can't find an app to set the wallpaper with. You can install one in
471 many many ways but I will give you some simple advice: install Eterm and
472 you're set. Eterm provides Esetroot and thats a great wallpaper setter. I
473 recommend you install the package provided by your distro."
474 exit 1
476 esac
478 if [ "$debug" = true ]; then
479 debugawsetbg
480 exit 0
483 option=${option:='$full'}
484 option2=${option2:='$full'}
487 if [ -z "$DISPLAY" ]; then
488 message "You are not connected to an X session\nPerhaps you should set the DISPLAY environment variable?"
489 exit 1
493 # random wallpaper code
494 if [ "$option" = random ]; then
495 # Lets make one thing clear...
496 wallpaperdir="$wallpaper"
497 if [ -z "$wallpaperdir" ]; then
498 message "No random wallpaper directory specified."
499 exit 1
501 if [ -d "$wallpaperdir" ]; then
502 number_of_wallpapers=`ls "$wallpaperdir"|wc -l`
503 if find_it random_number; then
504 randomnumber=`random_number`
505 # some shells don't support $RANDOM
506 elif [ -z "$RANDOM" ]; then
507 randomnumber=`(echo $$ ;time ps; w ; date )2>&1 | cksum | cut -f1 -d' '`
508 else
509 randomnumber="$RANDOM"
511 wallpapernumber=`expr $randomnumber % $number_of_wallpapers + 1`
512 #remember previous wallpaper
513 if [ ! "$remember" = false ]; then
514 remembercommand
516 remember=false
517 #set -x
518 wallpaper="$wallpaperdir/`ls \"$wallpaperdir\"|sed -n ${wallpapernumber}p`"
519 option=$option2 # have to choose something...
520 else
521 message "Invalid random wallpaper directory specified."
522 exit 1
527 if [ ! -r "$wallpaper" ] && [ $WPSETTER != "xsetroot" ]; then
528 message "Can't find wallpaper $wallpaper"
529 exit 1
533 $WPSETTER `eval echo $option` "$wallpaper"
534 if [ $? -ne 0 -a "$WPSETTER" != "display" ] ||
535 [ $? -ne 1 -a "$WPSETTER" = "display" ]; then
536 message "Something went wrong while setting the wallpaper.
537 Run '$WPSETTER "`eval echo $option` $wallpaper"' from an xterm to find out what."
538 exit 1
542 #remember previous wallpaper
543 if [ ! "$remember" = false ]; then
544 remembercommand