client: handle tags assignment in awful (FS#441)
[awesome.git] / utils / awsetbg
blob5d6f6e11c98ec8108e5ab21f907862057ca77109
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 wmsetbg feh hsetroot chbg display qiv xv xsri xli xsetbg 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 uncapsed 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 exit 0
150 if [ $# -eq 0 ]; then
151 message "no options given"
152 display_usage
153 exit 1
156 # create directory and last wallpaper file
157 if [ ! -d "${CONFIG_DIR}" ]; then
158 mkdir -p "${CONFIG_DIR}"
159 touch ${lastwallpaper}
162 unset debug setterfromcommandline
163 # Parse command-line options
164 while [ $# -gt 0 ]; do
165 case "$1" in
167 if find_it "$2"; then
168 grep -v wpsetters $lastwallpaper > ${lastwallpaper}.tmp
169 echo "wpsetters $2" >> ${lastwallpaper}.tmp
170 mv ${lastwallpaper}.tmp $lastwallpaper
171 WPSETTER=$2
172 setterfromcommandline=true
173 elif [ -z "$2" ]; then
174 grep -v wpsetters $lastwallpaper > ${lastwallpaper}.tmp
175 mv ${lastwallpaper}.tmp $lastwallpaper
176 message "wpsetter removed from historyfile"
177 exit 0
178 else
179 message "Couldn't find \"$2\" for wallpapersetter"
180 display_usage
181 exit 1
183 shift 2 ;;
185 if find_it "$2"; then
186 WPSETTER=$2
187 setterfromcommandline=true
188 else
189 message "Couldn't find \"$2\" for wallpapersetter"
190 display_usage
191 exit 1
193 shift 2 ;;
194 -i) debug=true
195 break ;;
196 -a) option='$aspect'
197 shift ;;
198 -f) option='$full'
199 shift ;;
200 -c) option='$center'
201 shift ;;
202 -t) option='$tile'
203 shift ;;
204 -A) option='$aspect'
205 remember=false
206 shift ;;
207 -F) option='$full'
208 remember=false
209 shift ;;
210 -C) option='$center'
211 remember=false
212 shift ;;
213 -T) option='$tile'
214 remember=false
215 shift ;;
217 montage=`which montage`
218 if test ! -z "$montage"; then
219 shift
220 cols="$1"; shift
221 fns=""
222 for i in `seq $cols`; do
223 fns="$fns $1";
224 done;
225 shift
226 $montage $fns -geometry +0+0 -tile ${cols}x $CONFIG_DIR/montage.png
227 wallpaper=$CONFIG_DIR/montage.png
228 else
229 shift
230 echo "montage not found. Falling back to single image"
233 -r) option2=$option
234 option=random
235 wallpaper=$2 # in this case it's a dir
236 break ;;
237 -R) option2=$option
238 option=random
239 wallpaper=$2 # in this case it's a dir
240 remember=false
241 break ;;
243 if [ -r "$lastwallpaper" ]; then
244 option=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f1`
245 option2=`echo $option|cut -d' ' -f2`
246 option=`echo $option|cut -d' ' -f1`
247 wallpaper=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f2`
248 if [ -z "$wallpaper" ]; then
249 option=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f1`
250 option2=`echo $option|cut -d' ' -f2`
251 option=`echo $option|cut -d' ' -f1`
252 wallpaper=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f2`
254 if [ -z "$wallpaper" ]; then
255 message "No previous wallpaper recorded for display ${DISPLAY}"
256 exit 1
258 else
259 message "No previous wallpaper recorded for display ${DISPLAY}"
260 exit 1
262 remember=false
263 break ;;
265 if [ -r "$lastwallpaper" ]; then
266 option=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f1`
267 option2=`echo $option|cut -d' ' -f2`
268 option=`echo $option|cut -d' ' -f1`
269 style=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f3`
270 wallpaper=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f2`
271 if [ -z "$wallpaper" ]; then
272 option=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f1`
273 option2=`echo $option|cut -d' ' -f2`
274 option=`echo $option|cut -d' ' -f1`
275 style=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f3`
276 wallpaper=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f2`
279 if [ "$style" != "style" -a -n "$wallpaper" ]; then
280 remember=false
281 break
283 style="style"
284 shift ;;
286 style="style"
287 shift ;;
288 -h) display_help ; exit 0 ;;
290 message "$command doesn't recognize -- gnu-longopts."
291 message 'Use $command -h for a help message.'
292 display_usage
293 exit 1 ;;
295 message "unrecognized option "\`"$1'"
296 display_usage
297 exit 1 ;;
299 if [ "$option" = random ]; then
300 option='$aspect'
301 elif [ ! -r "$1" ]; then
302 message "$1 isn't an existing wallpaper or a valid option."
303 display_usage
304 exit 1
305 elif [ -z "$1" ]; then
306 message 'No wallpaper to set'
307 display_usage
308 exit 1
309 else
310 wallpaper=$1
311 break
312 fi ;;
313 esac
314 done
316 # Find the default wallpapersetter
317 if [ "$setterfromcommandline" != true ]; then
318 if [ -r "$lastwallpaper" ]; then
319 wpsetters="`awk '/wpsetters/ {print $2}' $lastwallpaper` $wpsetters"
321 for wpsetter in $wpsetters; do
322 if find_it $wpsetter; then
323 if [ ! "$wpsetter" = Esetroot ]; then
324 WPSETTER=$wpsetter
325 break
326 elif 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 done
336 standardrant=\
337 "$WPSETTER doesn't set the wallpaper properly. Transparency for awesome and
338 apps like aterm and xchat won't work right with it. Consider installing
339 feh, wmsetbg (from windowmaker) or Esetroot (from Eterm) and I'll use
340 them instead."
342 standardok=\
343 "$WPSETTER is a nice wallpapersetter. You won't have any problems."
345 case $WPSETTER in
346 chbg)
347 full='-once -mode maximize'
348 tile='-once -mode tile'
349 center='-once -mode center'
350 aspect='-once -mode smart -max_grow 100 -max_size 100'
351 debugstory="chbg supports all features but it doesn't report errors. I reported this bug to the chbg developers."
353 xsri)
354 full='--center-x --center-y --scale-width=100 --scale-height=100'
355 tile='--tile'
356 center='--center-x --center-y --color=black'
357 aspect='--center-x --center-y --scale-width=100 --scale-height=100 --keep-aspect --color=black'
358 debugstory=$standardok
360 display)
361 full="-sample `xwininfo -root 2> /dev/null|awk '/geom/{print $2}'` -window root"
362 tile='-window root'
363 center='-backdrop -window root'
364 aspect="`xwininfo -root 2> /dev/null|grep geom` -window root"
365 debugstory=$standardrant
367 Esetroot)
368 full='-scale'
369 tile=''
370 center='-c'
371 aspect='-fit'
372 debugstory=$standardok
374 wmsetbg)
375 full='-s -S'
376 tile='-t'
377 center='-b black -e'
378 aspect='-b black -a -S'
379 debugstory=$standardok
381 xsetbg)
382 tile='-border black'
383 center='-center -border black'
384 aspect='-fullscreen -border black'
385 full=$aspect #broken
386 debugstory="xsetbg is actually xli. The fillscreen option (-f) is broken, defaults to (-a). $standardrant"
388 xli)
389 tile='-onroot -quiet -border black'
390 center='-center -onroot -quiet -border black'
391 aspect='-fullscreen -onroot -quiet -border black'
392 full=$aspect #broken
393 debugstory="The fillscreen option (-f) is broken, defaults to (-a). $standardrant"
395 qiv)
396 full='--root_s'
397 tile='--root_t'
398 center='--root'
399 aspect='-m --root'
400 debugstory=$standardrant
403 full='-max -smooth -root -quit'
404 tile='-root -quit'
405 center='-rmode 5 -root -quit'
406 aspect='-maxpect -smooth -root -quit'
407 debugstory=$standardrant
409 feh)
410 full='--bg-scale'
411 tile='--bg-tile'
412 center='--bg-center'
413 aspect=$full
414 debugstory=$standardok
416 hsetroot)
417 full='-fill'
418 tile='-tile'
419 center='-center'
420 aspect='-full'
421 debugstory=$standardok
423 habak)
424 full='-ms'
425 tile=''
426 center='-mC'
427 aspect='-mS'
428 debugstory=$standardok
430 icewmbg)
431 tile='-s'
432 full=$tile
433 center=$tile
434 aspect=$tile
435 debugstory="icewmbg does support transparency, but only tiling. And I noticed odd
436 errormessages with aterm. Don't use it unless you have to."
438 xsetroot)
439 tile='-solid'
440 full=$tile
441 center=$tile
442 aspect=$tile
443 debugstory="I can't find an app to set the wallpaper with. Using xsetroot to set
444 a solid background. If you want to have a background image you should install Esetroot or feh."
445 wallpaper='#535d6c'
448 message \
449 "I can't find an app to set the wallpaper with. You can install one in
450 many many ways but I will give you some simple advice: install Eterm and
451 you're set. Eterm provides Esetroot and thats a great wallpaper setter. I
452 recommend you install the package provided by your distro."
453 exit 1
455 esac
457 if [ "$debug" = true ]; then
458 debugawsetbg
459 exit 0
462 option=${option:='$full'}
463 option2=${option2:='$full'}
466 if [ -z "$DISPLAY" ]; then
467 message "You are not connected to an X session\nPerhaps you should set the DISPLAY environment variable?"
468 exit 1
472 # random wallpaper code
473 if [ "$option" = random ]; then
474 # Lets make one thing clear...
475 wallpaperdir="$wallpaper"
476 if [ -z "$wallpaperdir" ]; then
477 message "No random wallpaper directory specified."
478 exit 1
480 if [ -d "$wallpaperdir" ]; then
481 number_of_wallpapers=`ls "$wallpaperdir"|wc -l`
482 if find_it random_number; then
483 randomnumber=`random_number`
484 # some shells don't support $RANDOM
485 elif [ -z "$RANDOM" ]; then
486 randomnumber=`(echo $$ ;time ps; w ; date )2>&1 | cksum | cut -f1 -d' '`
487 else
488 randomnumber="$RANDOM"
490 wallpapernumber=`expr $randomnumber % $number_of_wallpapers + 1`
491 #remember previous wallpaper
492 if [ ! "$remember" = false ]; then
493 remembercommand
495 remember=false
496 #set -x
497 wallpaper="$wallpaperdir/`ls \"$wallpaperdir\"|sed -n ${wallpapernumber}p`"
498 option=$option2 # have to choose something...
499 else
500 message "Invalid random wallpaper directory specified."
501 exit 1
506 if [ ! -r "$wallpaper" ] && [ $WPSETTER != "xsetroot" ]; then
507 message "Can't find wallpaper $wallpaper"
508 exit 1
512 $WPSETTER `eval echo $option` "$wallpaper"
513 if [ $? -ne 0 ]; then
514 message "Something went wrong while setting the wallpaper.
515 Run '$WPSETTER "`eval echo $option` $wallpaper"' from an xterm to find out what."
516 exit 1
520 #remember previous wallpaper
521 if [ ! "$remember" = false ]; then
522 remembercommand