#!/bin/sh
#Barry Kauler 2009
#Called from Xorg Wizard, via xinit, testing a video mode.
#091124 bug fixes, vert and horiz freqs did not display for vesa driver.
# 3jan11 shinobar: i18n
export TEXTDOMAIN=xorgwizard
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
LANGORG=$LANG
export LC_ALL=C

set -- `xvidtune -show` #'-display :0' not needed.
CLOCKHZ=`dc $2 1000000 \* p`
CLOCKHZINT=`echo -n "$CLOCKHZ" | cut -f 1 -d '.'`
if [ $CLOCKHZINT -ne 0 ];then
 XTEST=$3
 YTEST=$7
 VRTEST=`dc $CLOCKHZ $6 \/ ${10} \/ p`
 HSTEST=`dc $CLOCKHZ $6 \/ 1000 \/ p`
else
 #some video chips do not work with xvidtune.
 XRANDRINFO="`xrandr -q | grep '\*' | tr -s ' ' | grep '^ [0-9]' | head -n 1`"
 XTEST=`echo -n "$XRANDRINFO" | cut -f 2 -d ' ' | cut -f 1 -d 'x'`
 YTEST=`echo -n "$XRANDRINFO" | cut -f 2 -d ' ' | cut -f 2 -d 'x'`
 VRTEST=`echo -n "$XRANDRINFO" | cut -f 3 -d ' ' | cut -f 1 -d '*'`
 VRTESTINT=`echo -n "$VRTEST" | cut -f 1 -d '.'`
  
 #xrandr does not always return the vertical frequency...
 if [ $VRTESTINT -eq 0 -o ! $VRTEST ];then
  if [ "$MONCHOICES" = "" ];then #091124 exported from xorgwizard.
   mcPATTERN=" ${XTEST}x${YTEST}"'@'
   [ -f /tmp/ddcprobe.txt ] && VRTEST=`grep "$mcPATTERN" /tmp/ddcprobe.txt | tail -n 1 | cut -f 2 -d '@'` #091124
  else
   mcPATTERN='^'"${XTEST}x${YTEST}"'@'
   VRTEST=`echo "$MONCHOICES" | grep -m1 "$mcPATTERN" | cut -f 2 -d '@'`
  fi
  [ ! $VRTEST ] && VRTEST=60 #desparate last resort.
 fi
 # so we calc horiz freq theoretically...
 HSTEST=`gtf $XTEST $YTEST $VRTEST | head -n 2 | tail -n 1 | cut -f 1 -d ';' | cut -f 2 -d ':' | cut -f 2 -d ' '`
fi

#round off to two decimal places...
HSTEST=`printf "%.2f" $HSTEST`
VRTEST=`printf "%.2f" $VRTEST`

echo "XTEST=$XTEST
YTEST=$YTEST
VRTEST=$VRTEST
HSTEST=$HSTEST" > /tmp/xorgwizard_initrc_test_results #read by xorgwizard.
LC_ALL=
LANG=$LANGORG
#xmessage -font "8x16" -center -timeout 60 -buttons "OK:10" -default OK " 
TITLE=$(gettext "Puppy Xorg Video Wizard: testing X")
export MSG="$(gettext 'If you can see this, then X is working!')

$(gettext 'Current resolution'):  ${XTEST}x${YTEST} $(gettext 'pixels')
$(gettext 'Horizontal sync frequency'):  $HSTEST KHz
$(gettext 'Vertical refresh frequency'):  $VRTEST Hz ($(gettext 'times per second'))

$(gettext "Please click the 'OK' button, or if your mouse isn't working, just hit the ENTER key, or the combination CTRL-ALT-BACKSPACE.")"

BARMSG=$(gettext "If you don't do anything, this test will timeout in 60 seconds.")

STEP=6	# timeout is x10sec
export DIALOG="<window title=\"$TITLE\" $TITLEICON><vbox>
    <frame><text><input>echo -en \"\$MSG\"</input></text></frame>
	<progressbar><input>for i in \$(seq 0 10 100); do echo \$i; sleep $STEP; done;echo 100</input>
	  <label>$BARMSG</label>
      <action type=\"exit\">TIMEOUT</action></progressbar>
	<hbox><button ok></button></hbox>
	</vbox></window>" 
	eval $(gtkdialog3 -p DIALOG -c || echo "$DIALOG">&2)
[ "$EXIT" = "OK" ]
#when xmessage terminates, xinit should kill Xorg.
###END###
