Tag clients more intelligently (FS#700)
[awesome.git] / utils / awsetbg
blobbc833c37dfbae8b4dea2697ce4839ef84df66305
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 if [ $# -eq 0 ]; then
222 echo "-m option need a columns argument"
223 exit # no more args
224 else
225 cols="$1"; shift
226 fns=""
227 i=$cols
228 while [ $i -gt 0 ]; do
229 fns="$fns $1"
230 i=$(($i - 1))
231 done
232 shift
233 $montage $fns -geometry +0+0 -tile ${cols}x $CONFIG_DIR/montage.png
234 wallpaper=$CONFIG_DIR/montage.png
236 else
237 shift
238 echo "montage not found. Falling back to single image"
241 -r) option2=$option
242 option=random
243 wallpaper=$2 # in this case it's a dir
244 break ;;
245 -R) option2=$option
246 option=random
247 wallpaper=$2 # in this case it's a dir
248 remember=false
249 break ;;
251 if [ -r "$lastwallpaper" ]; then
252 option=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f1`
253 option2=`echo $option|cut -d' ' -f2`
254 option=`echo $option|cut -d' ' -f1`
255 wallpaper=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f2`
256 if [ -z "$wallpaper" ]; then
257 option=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f1`
258 option2=`echo $option|cut -d' ' -f2`
259 option=`echo $option|cut -d' ' -f1`
260 wallpaper=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f2`
262 if [ -z "$wallpaper" ]; then
263 message "No previous wallpaper recorded for display ${DISPLAY}"
264 exit 1
266 else
267 message "No previous wallpaper recorded for display ${DISPLAY}"
268 exit 1
270 remember=false
271 break ;;
273 if [ -r "$lastwallpaper" ]; then
274 option=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f1`
275 option2=`echo $option|cut -d' ' -f2`
276 option=`echo $option|cut -d' ' -f1`
277 style=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f3`
278 wallpaper=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f2`
279 if [ -z "$wallpaper" ]; then
280 option=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f1`
281 option2=`echo $option|cut -d' ' -f2`
282 option=`echo $option|cut -d' ' -f1`
283 style=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f3`
284 wallpaper=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f2`
287 if [ "$style" != "style" -a -n "$wallpaper" ]; then
288 remember=false
289 break
291 style="style"
292 shift ;;
294 style="style"
295 shift ;;
296 -h) display_help ; exit 0 ;;
298 message "$command doesn't recognize -- gnu-longopts."
299 message 'Use $command -h for a help message.'
300 display_usage
301 exit 1 ;;
303 message "unrecognized option "\`"$1'"
304 display_usage
305 exit 1 ;;
307 if [ "$option" = random ]; then
308 option='$aspect'
309 elif [ ! -r "$1" ]; then
310 message "$1 isn't an existing wallpaper or a valid option."
311 display_usage
312 exit 1
313 elif [ -z "$1" ]; then
314 message 'No wallpaper to set'
315 display_usage
316 exit 1
317 else
318 wallpaper=$1
319 break
320 fi ;;
321 esac
322 done
324 # Find the default wallpapersetter
325 if [ "$setterfromcommandline" != true ]; then
326 if [ -r "$lastwallpaper" ]; then
327 wpsetters="`awk '/wpsetters/ {print $2}' $lastwallpaper` $wpsetters"
329 for wpsetter in $wpsetters; do
330 if find_it $wpsetter; then
331 case $wpsetter in
332 Esetroot)
333 if ldd `which Esetroot`|grep libImlib 2>&1 > /dev/null; then
334 WPSETTER=$wpsetter
335 break
336 else
337 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."
340 display)
341 if find_it xwininfo; then
342 WPSETTER=$wpsetter
343 break
344 else
345 sad_display_story="I also found display, but I need xwininfo for querying the root window geometry."
349 WPSETTER=$wpsetter
350 break
351 esac
353 done
356 standardrant=\
357 "$WPSETTER doesn't set the wallpaper properly. Transparency for
358 awesome and apps like aterm and xchat won't work right with
359 it. Consider installing feh, Esetroot
360 (from Eterm) or fvwm-root (from FVWM) and I'll use them instead."
362 standardok=\
363 "$WPSETTER is a nice wallpapersetter. You won't have any problems."
365 case $WPSETTER in
366 chbg)
367 full='-once -mode maximize'
368 tile='-once -mode tile'
369 center='-once -mode center'
370 aspect='-once -mode smart -max_grow 100 -max_size 100'
371 debugstory="chbg supports all features but it doesn't report errors. I reported this bug to the chbg developers."
373 xsri)
374 full='--center-x --center-y --scale-width=100 --scale-height=100'
375 tile='--tile'
376 center='--center-x --center-y --color=black'
377 aspect='--center-x --center-y --scale-width=100 --scale-height=100 --keep-aspect --color=black'
378 debugstory=$standardok
380 display)
381 full="-sample `xwininfo -root 2> /dev/null|awk '/geom/{print $2}'` -window root"
382 tile='-window root'
383 center='-backdrop -window root'
384 aspect="`xwininfo -root 2> /dev/null|grep geom` -window root"
385 debugstory=$standardrant
387 Esetroot)
388 full='-scale'
389 tile=''
390 center='-c'
391 aspect='-fit'
392 debugstory=$standardok
394 wmsetbg)
395 full='-s -S'
396 tile='-t'
397 center='-b black -e'
398 aspect='-b black -a -S'
399 debugstory=$standardok
401 xsetbg)
402 tile='-border black'
403 center='-center -border black'
404 aspect='-fullscreen -border black'
405 full=$aspect #broken
406 debugstory="xsetbg is actually xli. The fillscreen option (-f) is broken, defaults to (-a). $standardrant"
408 xli)
409 tile='-onroot -quiet -border black'
410 center='-center -onroot -quiet -border black'
411 aspect='-fullscreen -onroot -quiet -border black'
412 full=$aspect #broken
413 debugstory="The fillscreen option (-f) is broken, defaults to (-a). $standardrant"
415 qiv)
416 full='--root_s'
417 tile='--root_t'
418 center='--root'
419 aspect='-m --root'
420 debugstory=$standardrant
423 full='-max -smooth -root -quit'
424 tile='-root -quit'
425 center='-rmode 5 -root -quit'
426 aspect='-maxpect -smooth -root -quit'
427 debugstory=$standardrant
429 feh)
430 full='--bg-scale'
431 tile='--bg-tile'
432 center='--bg-center'
433 aspect=$full
434 debugstory=$standardok
436 hsetroot)
437 full='-fill'
438 tile='-tile'
439 center='-center'
440 aspect='-full'
441 debugstory=$standardok
443 habak)
444 full='-ms'
445 tile=''
446 center='-mC'
447 aspect='-mS'
448 debugstory=$standardok
450 fvwm-root)
451 tile='-r'
452 full=$tile
453 center=$tile
454 aspect=$tile
455 debugstory="fvwm-root can retain and publish the Pixmap, thus
456 supports transparency. But only these image formats are supported:
457 XBM, XPM, PNG, SVG and it is rather limited with options."
459 icewmbg)
460 tile='-s'
461 full=$tile
462 center=$tile
463 aspect=$tile
464 debugstory="icewmbg does support transparency, but only tiling. And I noticed odd
465 errormessages with aterm. Don't use it unless you have to."
467 xsetroot)
468 tile='-solid'
469 full=$tile
470 center=$tile
471 aspect=$tile
472 debugstory="I can't find an app to set the wallpaper with. Using xsetroot to set
473 a solid background. If you want to have a background image you should install Esetroot or feh."
474 wallpaper='#535d6c'
477 message \
478 "I can't find an app to set the wallpaper with. You can install one in
479 many many ways but I will give you some simple advice: install Eterm and
480 you're set. Eterm provides Esetroot and thats a great wallpaper setter. I
481 recommend you install the package provided by your distro."
482 exit 1
484 esac
486 if [ "$debug" = true ]; then
487 debugawsetbg
488 exit 0
491 option=${option:='$full'}
492 option2=${option2:='$full'}
495 if [ -z "$DISPLAY" ]; then
496 message "You are not connected to an X session\nPerhaps you should set the DISPLAY environment variable?"
497 exit 1
501 # random wallpaper code
502 if [ "$option" = random ]; then
503 # Lets make one thing clear...
504 wallpaperdir="$wallpaper"
505 if [ -z "$wallpaperdir" ]; then
506 message "No random wallpaper directory specified."
507 exit 1
509 if [ -d "$wallpaperdir" ]; then
510 number_of_wallpapers=`ls "$wallpaperdir"|wc -l`
511 if find_it random_number; then
512 randomnumber=`random_number`
513 # some shells don't support $RANDOM
514 elif [ -z "$RANDOM" ]; then
515 randomnumber=`(echo $$ ;time ps; w ; date )2>&1 | cksum | cut -f1 -d' '`
516 else
517 randomnumber="$RANDOM"
519 wallpapernumber=`expr $randomnumber % $number_of_wallpapers + 1`
520 #remember previous wallpaper
521 if [ ! "$remember" = false ]; then
522 remembercommand
524 remember=false
525 #set -x
526 wallpaper="$wallpaperdir/`ls \"$wallpaperdir\"|sed -n ${wallpapernumber}p`"
527 option=$option2 # have to choose something...
528 else
529 message "Invalid random wallpaper directory specified."
530 exit 1
535 if [ ! -r "$wallpaper" ] && [ $WPSETTER != "xsetroot" ]; then
536 message "Can't find wallpaper $wallpaper"
537 exit 1
541 $WPSETTER `eval echo $option` "$wallpaper"
542 if [ $? -ne 0 -a "$WPSETTER" != "display" ] ||
543 [ $? -ne 1 -a "$WPSETTER" = "display" ]; then
544 message "Something went wrong while setting the wallpaper.
545 Run '$WPSETTER "`eval echo $option` $wallpaper"' from an xterm to find out what."
546 exit 1
550 #remember previous wallpaper
551 if [ ! "$remember" = false ]; then
552 remembercommand