home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 November - Disc 1 / PCNET_CD_2006_11_1.iso / linux / gparted-livecd-0.3.1-1.iso / gparted / usr / X11R6 / bin / fbsetbg < prev    next >
Encoding:
Text File  |  2006-07-03  |  15.6 KB  |  534 lines

  1. #!/bin/sh
  2. #
  3. # Set wallpaper for fluxbox.
  4. #
  5. # Copyright (c) 2003-2004 Han Boetes <han@mijncomputer.nl>
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining
  8. # a copy of this software and associated documentation files (the
  9. # "Software"), to deal in the Software without restriction, including
  10. # without limitation the rights to use, copy, modify, merge, publish,
  11. # distribute, sublicense, and/or sell copies of the Software, and to
  12. # permit persons to whom the Software is furnished to do so, subject to
  13. # the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be
  16. # included in all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. #
  26. # $Id: fbsetbg 4311 2006-04-17 09:47:40Z simonb $
  27.  
  28. # Portability notes:
  29. # To guarantee this script works on all platforms that support fluxbox
  30. # please keep the following restrictions in mind:
  31. #
  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.
  44.  
  45. # TODO purheps: xprop -root _WIN_WORKSPACE
  46. # _NET_CURRENT_DESKTOP
  47.  
  48. # The wallpapersetter is selected in this order
  49. wpsetters="${wpsetters:=Esetroot wmsetbg feh hsetroot chbg display qiv xv xsri xli xsetbg}" # broken icewmbg'
  50. lastwallpaper="${HOME}/.fluxbox/lastwallpaper"
  51.  
  52.  
  53. WHOAMI=`whoami`
  54. [ "$WHOAMI" = root ] && PATH=/bin:/usr/bin/:/usr/local/bin:/usr/X11R6/bin
  55.  
  56. command="`basename \"$0\"`"
  57.  
  58.  
  59. # Functions
  60. display_usage() {
  61.     cat << EOF
  62. Usage: $command [-u/-U [wallpapersetter]] [-fFcCtTaA /path/to/wallpaper]
  63.                 [-r/-R /path/to/wallpaperdirectory]
  64.                 [-b/-B fbsetrootoptions] [-l] [-h] [-i] [-p]
  65. Use \`\`$command -h'' for a complete help message.
  66.  
  67. EOF
  68. }
  69.  
  70. display_help() {
  71.     display_usage
  72.     cat << EOF
  73.  
  74. Options:
  75.  
  76.     -f  Set fullscreen wallpaper.
  77.     -c  Set centered wallpaper.
  78.     -t  Set tiled wallpaper.
  79.     -a  Set maximized wallpaper, preserving aspect.
  80.         ( if your bgsetter doesn't support this
  81.           we fall back to -f )
  82.     -u  Use specified wallpapersetter, use no argument to forget.
  83.     -b  Forward the options to fbsetroot.
  84.     -r  set random wallpaper from a directory
  85.  
  86.     -F,-C,-T,-A,-U,-B,-R same as uncapsed but without remembering.
  87.  
  88.     -h  Display this help.
  89.  
  90.     -l  Set previous wallpaper.
  91.  
  92.     -i  Information about selected wallpaper command.
  93.     -d  (deprecated, use -i) Debug info.
  94.     -p  Tips.
  95.  
  96.  
  97. Files:
  98.  
  99.     ~/.fluxbox/lastwallpaper     In this file the wallpaper you set
  100.                                  will be stored, for the -l option:
  101. Environment variables:
  102.     wpsetters   Wallpapersetters to use.
  103.                 example:
  104.                   wpsetters=feh fbsetbg wallpaper.jpg
  105.  
  106.     DISPLAY     The display you want to set the wallpaper on.
  107.                 example:
  108.                   DISPLAY=:0.0 fbsetbg -l
  109.  
  110. EOF
  111. }
  112.  
  113. display_tips() {
  114.     cat << EOF
  115. Common tips to use with $command:
  116.  
  117. 1) To replace all occurrences of bsetbg with $command in a file use this
  118.    command:
  119.  
  120.    perl -pi -e 's,([^f]|^)bsetbg,$command,' filename
  121.  
  122. 2) If you want the style to set the wallpaper and you want $command to
  123.    remember the previous wallpaper put this in your ~/.fluxbox/init
  124.  
  125.    session.screen0.rootCommand:    $command -l
  126.  
  127. 3) Use $command -i to find out what wallpapersetter $command will be
  128.    used and what $command thinks about it.
  129.  
  130. EOF
  131. }
  132.  
  133. # some which's have a reliable return code, some don't
  134. # Lets figure out which which we have.
  135. if which this_program_does_not_exist-no_really-aA1zZ9 >/dev/null 2>/dev/null; then
  136.     # can't rely on return value
  137.     find_it() {
  138.         file=`which $1 2> /dev/null`
  139.         if [ -x "$file" ]; then
  140.             if [ $# -gt 1 ]; then
  141.                 shift
  142.                 $*
  143.             fi
  144.             return 0
  145.         else
  146.             return 1
  147.         fi
  148.     }
  149. else
  150.     # can rely on return value
  151.     find_it() {
  152.         which $1 > /dev/null 2>&1 && shift && $*
  153.     }
  154. fi
  155.  
  156. message() {
  157.  
  158.     # This Terminal detection method is inaccurate. Looking for a
  159.     # replacement.
  160.  
  161.     # echo if we have terminal output, otherwise pop up a window
  162.     # if [ -t 1 ]; then
  163.     #     echo "$command: $@"
  164.     # else
  165.     extra_args="-default okay"
  166.         if find_it gxmessage; then
  167.             gxmessage $extra_args -center "$command: $@" &
  168.         else
  169.             xmessage $extra_args -center "$command: $@" &
  170.         fi
  171.     # fi
  172. }
  173.  
  174. remembercommand() {
  175.     grep -vs "|${DISPLAY}$" ${lastwallpaper} > ${lastwallpaper}.tmp
  176.     mv -f ${lastwallpaper}.tmp ${lastwallpaper}
  177.     if [ "$option" = fbsetroot ]; then
  178.         echo $option'|'$wallpaper'|'$DISPLAY >> $lastwallpaper
  179.         return
  180.     fi
  181.     # Make dir/../../path/file.jpg work
  182.     case $wallpaper in
  183.         # no spaces allowed between the varname and '|'
  184.         /*) echo $option'|'$wallpaper'|'$DISPLAY >> $lastwallpaper ;;
  185.         *)  echo $option'|'$PWD/$wallpaper'|'$DISPLAY >> $lastwallpaper ;;
  186.     esac
  187. }
  188.  
  189. debugfbsetbg() {
  190.     echo
  191.     echo $debugstory
  192.     echo $sad_esetroot_story
  193.     exit 0
  194. }
  195.  
  196. if [ $# -eq 0 ]; then
  197.     message "no options given"
  198.     display_usage
  199.     exit 1
  200. fi
  201.  
  202. # create directory and last wallpaper file 
  203. if [ ! -d  "$HOME/.fluxbox" ]; then
  204.     mkdir "$HOME/.fluxbox"
  205.     touch ${lastwallpaper}
  206. fi
  207.  
  208. unset debug setterfromcommandline
  209. # Parse command-line options
  210. while [ $# -gt 0 ]; do
  211.     case "$1" in
  212.         -u)
  213.             if find_it "$2"; then
  214.                 grep -v wpsetters $lastwallpaper > ${lastwallpaper}.tmp
  215.                 echo "wpsetters $2" >> ${lastwallpaper}.tmp
  216.                 mv ${lastwallpaper}.tmp $lastwallpaper
  217.                 WPSETTER=$2
  218.                 setterfromcommandline=true
  219.             elif [ -z "$2" ]; then
  220.                 grep -v wpsetters $lastwallpaper > ${lastwallpaper}.tmp
  221.                 mv ${lastwallpaper}.tmp $lastwallpaper
  222.                 message "wpsetter removed from historyfile"
  223.                 exit 0
  224.             else
  225.                 message "Couldn't find \"$2\" for wallpapersetter"
  226.                 display_usage
  227.                 exit 1
  228.             fi
  229.             shift 2 ;;
  230.         -U)
  231.             if find_it "$2"; then
  232.                 WPSETTER=$2
  233.                 setterfromcommandline=true
  234.             else
  235.                 message "Couldn't find \"$2\" for wallpapersetter"
  236.                 display_usage
  237.                 exit 1
  238.             fi
  239.             shift 2 ;;
  240.         -d|-i) debug=true
  241.             break ;;
  242.         -a) option='$aspect'
  243.             wallpaper=$2
  244.             break ;;
  245.         -f) option='$full'
  246.             wallpaper=$2
  247.             break ;;
  248.         -c) option='$center'
  249.             wallpaper=$2
  250.             break ;;
  251.         -t) option='$tile'
  252.             wallpaper=$2
  253.             break ;;
  254.         -A) option='$aspect'
  255.             wallpaper=$2
  256.             remember=false
  257.             break ;;
  258.         -F) option='$full'
  259.             wallpaper=$2
  260.             remember=false
  261.             break ;;
  262.         -C) option='$center'
  263.             wallpaper=$2
  264.             remember=false
  265.             break ;;
  266.         -T) option='$tile'
  267.             wallpaper=$2
  268.             remember=false
  269.             break ;;
  270.         -b) option=fbsetroot
  271.             shift
  272.             wallpaper=$*
  273.             break ;;
  274.         -B) option=fbsetroot
  275.             shift
  276.             wallpaper=$*
  277.             remember=false
  278.             break ;;
  279.         -r) option=random
  280.             wallpaper=$2 # in this case it's a dir
  281.             break ;;
  282.         -R) option=random
  283.             wallpaper=$2 # in this case it's a dir
  284.             remember=false
  285.             break ;;
  286.         -l)
  287.             if [ -r "$lastwallpaper" ]; then
  288.                 option=`grep "|${DISPLAY}$"    $lastwallpaper|cut -d'|' -f1`
  289.                 wallpaper=`grep "|${DISPLAY}$" $lastwallpaper|cut -d'|' -f2`
  290.                 if [ -z "$wallpaper" ]; then
  291.                     option=`grep "|${DISPLAY}.0$"    $lastwallpaper|cut -d'|' -f1`
  292.                     wallpaper=`grep "|${DISPLAY}.0$" $lastwallpaper|cut -d'|' -f2`
  293.                 fi
  294.                 if [ -z "$wallpaper" ]; then
  295.                     message "No previous wallpaper recorded for display ${DISPLAY}"
  296.                     exit 1
  297.                 fi
  298.             else
  299.                 message "No previous wallpaper recorded for display ${DISPLAY}"
  300.                 exit 1
  301.             fi
  302.             remember=false
  303.             break ;;
  304.         -p) display_tips ; exit 0 ;;
  305.         -h) display_help ; exit 0 ;;
  306.         --)
  307.             message "$command doesn't recognize -- gnu-longopts."
  308.             message 'Use $command -h for a help message.'
  309.             display_usage
  310.             exit 1 ;;
  311.         -*)
  312.             message "unrecognized option "\`"$1'"
  313.             display_usage
  314.             exit 1 ;;
  315.         *)
  316.             if [ "$option" = random ]; then
  317.                 option='$aspect'
  318.             elif [ ! -r "$1" ]; then
  319.                 message "$1 isn't an existing wallpaper or a valid option."
  320.                 display_usage
  321.                 exit 1
  322.             elif [ -z "$1" ]; then
  323.                 message 'No wallpaper to set'
  324.                 display_usage
  325.                 exit 1
  326.             else
  327.                 wallpaper=$1
  328.                 break
  329.             fi ;;
  330.     esac
  331. done
  332.  
  333. # Find the default wallpapersetter
  334. if [ "$setterfromcommandline" != true ]; then
  335.     if [ -r "$lastwallpaper" ]; then
  336.         wpsetters="`awk '/wpsetters/ {print $2}' $lastwallpaper` $wpsetters"
  337.     fi
  338.     for wpsetter in $wpsetters; do
  339.         if find_it $wpsetter; then
  340.             if [ ! "$wpsetter" = Esetroot ]; then
  341.                 WPSETTER=$wpsetter
  342.                 break
  343.             elif ldd `which Esetroot`|grep libImlib 2>&1 > /dev/null; then
  344.                 WPSETTER=$wpsetter
  345.                 break
  346.             else
  347.                 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."
  348.             fi
  349.         fi
  350.     done
  351. fi
  352.  
  353. standardrant=\
  354. "$WPSETTER doesn't set the wallpaper properly. Transparency for fluxbox and
  355. apps like aterm and xchat won't work right with it. Consider installing
  356. feh, wmsetbg (from windowmaker) or Esetroot (from Eterm) and I'll use
  357. them instead."
  358.  
  359. standardok=\
  360. "$WPSETTER is a nice wallpapersetter. You won't have any problems."
  361.  
  362. case $WPSETTER in
  363.     chbg)
  364.         full='-once -mode maximize'
  365.         tile='-once -mode tile'
  366.         center='-once -mode center'
  367.         aspect='-once -mode smart -max_grow 100 -max_size 100'
  368.         debugstory="chbg supports all features but it doesn't report errors. I reported this bug to the chbg developers."
  369.         ;;
  370.     xsri)
  371.         full='--center-x --center-y --scale-width=100 --scale-height=100'
  372.         tile='--tile'
  373.         center='--center-x --center-y --color=black'
  374.         aspect='--center-x --center-y --scale-width=100 --scale-height=100 --keep-aspect --color=black'
  375.         debugstory=$standardok
  376.         ;;
  377.     display)
  378.         full="`xwininfo -root 2> /dev/null|grep geom` -window root"
  379.         tile='-window root'
  380.         center='-backdrop -window root'
  381.         aspect=$full
  382.         debugstory=$standardrant
  383.         ;;
  384.     Esetroot)
  385.         full='-scale'
  386.         tile=''
  387.         center='-c'
  388.         aspect='-fit'
  389.         debugstory=$standardok
  390.         ;;
  391.     wmsetbg)
  392.         full='-s -S'
  393.         tile='-t'
  394.         center='-b black -e'
  395.         aspect='-b black -a -S'
  396.         debugstory=$standardok
  397.         ;;
  398.     xsetbg)
  399.         tile='-border black'
  400.         center='-center -border black'
  401.         aspect='-fullscreen -border black'
  402.         full=$aspect #broken
  403.         debugstory="xsetbg is actually xli. The fillscreen option (-f) is broken, defaults to (-a). $standardrant"
  404.         ;;
  405.     xli)
  406.         tile='-onroot -quiet -border black'
  407.         center='-center -onroot -quiet -border black'
  408.         aspect='-fullscreen -onroot -quiet -border black'
  409.         full=$aspect #broken
  410.         debugstory="The fillscreen option (-f) is broken, defaults to (-a). $standardrant"
  411.         ;;
  412.     qiv)
  413.         full='--root_s'
  414.         tile='--root_t'
  415.         center='--root'
  416.         aspect='-m --root'
  417.         debugstory=$standardrant
  418.         ;;
  419.     xv)
  420.         full='-max -smooth -root -quit'
  421.         tile='-root -quit'
  422.         center='-rmode 5 -root -quit'
  423.         aspect='-maxpect -smooth -root -quit'
  424.         debugstory=$standardrant
  425.         ;;
  426.     feh)
  427.         full='--bg-scale'
  428.         tile='--bg-tile'
  429.         center='--bg-center'
  430.         aspect=$full
  431.         debugstory=$standardok
  432.         ;;
  433.     hsetroot)
  434.         full='-fill'
  435.         tile='-tile'
  436.         center='-center'
  437.         aspect='-full'
  438.         debugstory=$standardok
  439.         ;;
  440.     icewmbg)
  441.         tile='-s'
  442.         full=$tile
  443.         center=$tile
  444.         aspect=$tile
  445.         debugstory="icewmbg does support transparency, but only tiling. And I noticed odd
  446. errormessages with aterm. Don't use it unless you have to."
  447.         ;;
  448.     '')
  449.         message \
  450. "I can't find an app to set the wallpaper with. You can install one in
  451. many many ways but I will give you some simple advice: install Eterm and
  452. you're set. Eterm provides Esetroot and thats a great wallpaper setter. I
  453. recommend you install the package provided by your distro."
  454.         exit 1
  455.         ;;
  456. esac
  457.  
  458. if [ "$debug" = true ]; then
  459.     debugfbsetbg
  460.     exit 0
  461. fi
  462.  
  463. option=${option:='$full'}
  464.  
  465.  
  466.  
  467. if [ -z "$DISPLAY" ]; then
  468.     message "You are not connected to an X session\nPerhaps you should set the DISPLAY environment variable?"
  469.     exit 1
  470. fi
  471.  
  472.  
  473. if [ "$option" = fbsetroot ]; then
  474.     fbsetroot $wallpaper
  475.     if [ ! "$remember" = false ]; then
  476.         remembercommand
  477.     fi
  478.     exit 0
  479. fi
  480.  
  481.  
  482. # random wallpaper code
  483. if [ "$option" = random ]; then
  484.     # Lets make one thing clear...
  485.     wallpaperdir="$wallpaper"
  486.     if [ -z "$wallpaperdir" ]; then
  487.         message "No random wallpaper directory specified."
  488.         exit 1
  489.     fi
  490.     if [ -d "$wallpaperdir" ]; then
  491.         number_of_wallpapers=`ls "$wallpaperdir"|wc -l`
  492.         if find_it random_number; then
  493.             randomnumber=`random_number`
  494.         # some shells don't support $RANDOM
  495.         elif [ -z "$RANDOM" ]; then
  496.             randomnumber=`(echo $$ ;time ps; w ; date )2>&1 | cksum | cut -f1 -d' '`
  497.         else
  498.             randomnumber="$RANDOM"
  499.         fi
  500.         wallpapernumber=`expr $randomnumber % $number_of_wallpapers + 1`
  501.         #remember previous wallpaper
  502.         if [ ! "$remember" = false ]; then
  503.             remembercommand
  504.         fi
  505.         remember=false
  506.         #set -x
  507.         wallpaper="$wallpaperdir/`ls \"$wallpaperdir\"|sed -n ${wallpapernumber}p`"
  508.         option='$aspect' # have to choose something...
  509.     else
  510.         message "Invalid random wallpaper directory specified."
  511.         exit 1
  512.     fi
  513. fi
  514.  
  515.  
  516. if [ ! -r "$wallpaper" ]; then
  517.     message "Can't find wallpaper $wallpaper"
  518.     exit 1
  519. fi
  520.  
  521.  
  522. $WPSETTER `eval echo $option` "$wallpaper"
  523. if [ $? -ne 0 ]; then
  524.     message "Something went wrong while setting the wallpaper.
  525. Run '$WPSETTER "`eval echo $option` $wallpaper"' from an xterm to find out what."
  526.     exit 1
  527. fi
  528.  
  529.  
  530. #remember previous wallpaper
  531. if [ ! "$remember" = false ]; then
  532.     remembercommand
  533. fi
  534.