#!/bin/sh
# fsplay - wrapper around mplayer for easily playing video files or discs
#          at full-screen size in X or Linux framebuffer
#
# by Rob Funk <rfunk@funknet.net>
# May 2002 - March 2003

# Sometimes a -0.2 delay helps synchronize sound and video when playing DVDs
delay=0

# Extra video filters (-vop), separate with commas and no spaces
# "pp=lb:a" does deinterlacing
vop="pp=lb:a"

# Try these audio outputs in order:
audout=sdl,esd,alsa,oss

# xvidix, vesa, and other video drivers require root.
# I have myself in the "video" group, and have this line in /etc/sudoers:
# %video	ALL = NOPASSWD: /usr/bin/mplayer
# But default to no root; this will be set to "sudo" later if necessary
sudo=

# Do they want help?
case "$1" in
-h|-help|--help|"-?")
	echo "Usage:  $0 [-aspect ratio] [mplayer-opts ...] [file ...]" 2>&1
	echo "        ratio is of the forms 4:3, 1.33:1, or just 1.33" 2>&1
	echo "        default is to fill the screen" 2>&1
	echo "        VCD or DVD title 1 played if no files given" 2>&1
	exit 0
	;;
esac

# see if we can get the size of an X screen
xres=`xwininfo -root 2>/dev/null | \
	sed -ne 's/^  -geometry \([0-9]*x[0-9]*\).*/\1/p'`
if [ "$xres" ]; then
	# OK, we have an X display
	# is it local or remote?
	# (remote probably won't work very well unless it's really localhost)
	case "$DISPLAY" in
	:*)
		vidout=xvidix,xv,x11
		# xvidix requires root; not sure if xv does
		sudo=sudo
		;;
	*)
		vidout=x11
		;;
	esac

	width=`echo $xres | sed -e 's/x.*//'`
	height=`echo $xres | sed -e 's/.*x//'`
	# kill xscreensaver if it's running
	xscreensaver-command -exit || true
elif [ -r /proc/fb ]; then
	vidout=fbdev
	# Do we have access to the framebuffer already?
	if [ -r /dev/fb0 -a -w /dev/fb0 ]; then
		# yes, no need for root
		sudo=
	else
		sudo=sudo
	fi

	# OK, we're sitting at a framebuffer console and need to know
	# its resolution.  Let's base it on the number of text lines/columns!
	# This is really only practical for standard sizes.
	# This list is incomplete; unlisted sizes default to 640x480.
	# resize is actually an X utility; eval'ing its output sets env vars.
	eval `resize -u`
	case "${COLUMNS}x${LINES}" in
	128x48)
		width=1024
		height=768
		;;
	100x37)
		width=800
		height=600
		;;
	80x30)
		width=640
		height=480
		;;
	*)
		width=640
		height=480
		;;
	esac
else
	echo "Sorry, I can only deal with X11 and framebuffer output" 1>&2
	echo "(But you may consider adding vesa or svgalib support to this script!)" 1>&2
	exit 1
fi

case "$1" in
-aspect)
	case "$2" in
	[1-9]*:[1-9]*)
		newheight=`echo "$2 $width * r / p" | tr : " " | dc`
		;;
	[1-9]|[1-9].[0-9]*)
		newheight=`echo "$2 $width r / p" | dc`
		;;
	"")
		echo "Error: -aspect needs an aspect ratio, e.g.:" 1>&2
		echo "       -aspect 4:3" 1>&2
		echo "       -aspect 1.33" 1>&2
		exit 2
		;;
	esac
	if [ ! "$newheight" -gt "$height" ]; then
		echo "Warning: Using height of $height instead of $newheight" \
			2>&1
		height="$newheight"
	fi
	# Save in case we need them later...
	aspect="$1"
	ratio="$2"
	shift; shift
	;;
esac

# xvidix at least doesn't seem to like scaling;
# it scales on its own anyway, though aspect ratio may need work.
# Of course, mplayer may fall through xvidix to a driver that can scale,
# but I don't know how to detect whether it will do that.
case "$vidout" in
*xvidix*|*xv*)
	scale=
	oldopts="$aspect $ratio"
	;;
*)
	scale="scale=$width:$height"
	;;
esac
# combine scaling with any output filters defined at the top
if [ "$vop" -a "$scale" ]; then
	vop="$vop,$scale"
elif [ "$scale" ]; then
	vop="$scale"
fi
if [ "$vop" ]; then
	vopopt="-vop $vop"
fi

echo "vidout=$vidout"
echo "width=$width"
echo "height=$height"
echo "vop=$vop"

exec $sudo mplayer \
	-vo $vidout \
	-zoom $vopopt \
	-ao $audout \
	-delay $delay \
	-fs \
	-framedrop \
	-double \
	-cache 4096 \
	-alang en,EN \
	$oldopts \
	"$@" \
	-vcd 1 -dvd 1

