#!/bin/sh
#
# Play a DVD or VCD on a TV using atitvout to control the video card
# and a second X session on VT8 to achieve the correct resolution.
#
# Developed by Stuart Prescott http://www.nanonanonano.net/
# Copyright Stuart Prescott 2005
# Released under the GNU GPL v2
#
# Created:        20050108
# Last Modified:  20050110
#
#----------------------------------------
#
#TODO: 
# * run the media player inside an xterm on the new screen so that
#   console output is available
# * run an xterm in the new screen anyway so you can do some diagnosis if you
#   need!
# * should this script be SUID root with only the startx command being run as
#   the real user? over-reliance on sudo is a little disconcerting.

#what's your favourite player?
PLAYER=/usr/bin/xine
#Start xine in fullsreen mode and use the slow but failsafe Xshm driver
PLAYEROPTS="-pf -V xshm "

#FIXME: this is ugly shell scripting at its best, but it is also 
#incompatible between xine and mplayer :(
if [ "x$1" == "xvcd" -o "x$2" == "xvcd" ]
then
  source=vcd
  xsource=vcd://
else
  source=dvd
  xsource=dvd:/
fi

if [ "x$1" != "xPLAY" ]
then
  #FIXME: is this clumsy? use the same cookie on two sessions?
  export XAUTHORITY=$HOME/.Xauthority.vt8
  sudo startx $0 PLAY $source -- :1 -layout LayoutTV -xf86config /etc/X11/XF86Config-4 
  # need to re-init the video card in a text session or it gets very confused
  sudo chvt 1
  sudo atitvout -f t
  #sleep 1
  sudo atitvout -f l
  sudo chvt 7
else
  sudo chvt 8
  sudo atitvout -f detect
  sudo atitvout pal
  sudo atitvout -f t
  $PLAYER $PLAYEROPTS $xsource
  sudo atitvout -f l
  sleep 1
fi

exit
