Blame view

tools/background 2.53 KB
Georg Hopp authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/bin/bash

##
# This one generates a background image for all active xrandr displays
# and sets it via ImageMagick display. The intermediate image will
# be removed as soon as the background ist set.
#
# Author: Georg Hopp <ghopp@bigpoint.net>
# Date: Tue Apr 17 17:19:56 CEST 2012
# Version: 1.0.0
#

function rand() {
	local DD="/bin/dd"
	local OD="/usr/bin/od"
	local SED="/bin/sed"

	${DD} if=/dev/urandom bs=2 count=1 2>/dev/null | \
		${OD} -i | ${SED} '2d;s/^0\+ \+//'
}
22 23 24 25
function setroot() {
	DISPLAY=${1} /usr/bin/feh --bg-center ${2}
}
Georg Hopp authored
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
function background() {
	local SED="/bin/sed"
	local XRANDR="/usr/bin/xrandr"
	local TEMPFILE="/bin/tempfile"
	local IDENTIFY="/usr/bin/identify"
	local CONVERT="/usr/bin/convert"
	local ECHO="/bin/echo"
	local EXPR="/usr/bin/expr"
	local WC="/usr/bin/wc"
	local LS="/bin/ls"

	local BGDIR="${BGDIR:-${HOME}/images/backgrounds}"
	local MYDISP="${DISPLAY:-:0}"

	local XRANDR_EXP='
		/^Screen/{
			s/ //g
			s/^.*ent\([0-9x]*\).*$/\1/
		}
45
		/ conn/s/^.*cted[^0-9]*\([0-9x+]*\).*$/\1/
Georg Hopp authored
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
		t
		d'

	local RESOLUTIONS="`DISPLAY=${MYDISP} ${XRANDR} | ${SED} "${XRANDR_EXP}"`"
	local WHOLE="`${ECHO} "${RESOLUTIONS}" | ${SED} '1p;d'`"
	local SCREENS="`${ECHO} "${RESOLUTIONS}" | ${SED} '2,$p;d'`"

	local BGS="`${LS} -1b "${BGDIR}"`"
	local N_BGS=`${ECHO} "${BGS}" | ${WC} -l`

	local res img size ofs ofs_x ofs_y screen_asp image_asp image_geom cmd

	cmd="-size ${WHOLE} xc:black"

	for res in ${SCREENS}
	do
		img=`rand`
		img=`${EXPR} ${img} % ${N_BGS} + 1`
		img="${BGDIR}/`${ECHO} "${BGS}" | ${SED} ${img}'p;d'`"
65
Georg Hopp authored
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		size=${res%%+*}
		ofs=${res#*+*}
		ofs_x=${ofs%%+*}
		ofs_y=${ofs##*+}

		screen_asp=`eval ${EXPR} ${size/x/ '\*' 100 \/ }`
		image_asp=`eval "${IDENTIFY} -format \"%w '\*' 100 \/ %h\" ${img}"`
		image_asp=`eval ${EXPR} $image_asp`

		# decide wheter to scale up to screen height or width
		# based on previously computed asp's
		if [ ${image_asp} -lt ${screen_asp} ]
		then
			# scale on height!
			image_geom="x${size##*x}"
		else
			# scale on width!
			image_geom="${size%%x*}x"
		fi

		image_geom=`eval "${CONVERT} -scale ${image_geom} ${img} jpeg:- | \
			${IDENTIFY} -format \"%wx%h\" jpeg:-"`

		ofs_x=`${EXPR} $ofs_x + \( \( ${size%%x*} - ${image_geom%%x*} \) / 2 \)`
		ofs_y=`${EXPR} $ofs_y + \( \( ${size##*x} - ${image_geom##*x} \) / 2 \)`

		cmd="${cmd} ${img} -geometry ${image_geom}+${ofs_x}+${ofs_y} -composite"
	done

	img=`${TEMPFILE} -s '.jpg'`
	eval "${CONVERT} ${cmd} ${img}"
97
	setroot "${MYDISP}" "${img}"
Georg Hopp authored
98 99 100 101 102 103
	rm ${img}
}

test "X$(basename -- "$0")" = "Xbackground" && background "$@"

# vim: set ft=sh ts=4 sw=4: