#!/usr/bin/moat
#
# Simple moat GUI for mplayer on IRIX
# 
# (c) 2004 - Marcus Herbert <m.herbert@das-werk.de>
#

global videofile

set version "1.4a"
set binary  "/opt/MPlayer/bin/mplayer"
set pid     "NaN"

xtAppInitialize -class XMPlayer

xmMainWindow   .main        managed
xmPanedWindow  .main.window managed

xmRowColumn  .main.window.top managed -orientation horizontal
xmLabel      .main.window.top.videolabel managed -labelString "Choose video/audio file: "
xmText       .main.window.top.txt    managed
xmPushButton .main.window.top.button managed

xmRowColumn    .main.window.vo managed -orientation horizontal -radioBehavior true
xmLabel        .main.window.vo.label managed -labelString "Video out:"
xmToggleButton .main.window.vo.gl  managed
xmToggleButton .main.window.vo.gl2 managed
xmToggleButton .main.window.vo.sdl managed
xmToggleButton .main.window.vo.x11 managed

xmRowColumn  .main.window.bottom managed -orientation horizontal
xmPushButton .main.window.bottom.play managed
xmPushButton .main.window.bottom.stop managed
xmPushButton .main.window.bottom.quit managed

.main.window.top.button setValues -labelString "Browse"
.main.window.vo.gl  setValues -labelString "OpenGL"
.main.window.vo.gl2 setValues -labelString "GL 2"
.main.window.vo.sdl setValues -labelString "SDL" -set true
.main.window.vo.x11 setValues -labelString "X11"
.main.window.bottom.play setValues -labelString "Play"
.main.window.bottom.stop setValues -labelString "Stop"
.main.window.bottom.quit setValues -labelString "Quit"	
.main.window.top.button  activateCallback videoChooser
.main.window.bottom.play activateCallback videoPlay
.main.window.bottom.stop activateCallback \
  { if {$pid != "NaN"} { catch { exec kill $pid } } }
.main.window.bottom.quit activateCallback  \
  { if {$pid != "NaN"} { catch { exec kill $pid } }
    exit 0 
  }

proc videoChooser {} {
  if { [llength [info commands .videoChooser]] > 0 } {
    .videoChooser manageChild
  } else {
    xmFileSelectionDialog .videoChooser managed \
      -dialogStyle dialog_full_application_modal
    .videoChooser okCallback {
      .main.window.top.txt setValues -value "%value"
      .videoChooser unmanageChild }
    .videoChooser cancelCallback ".videoChooser unmanageChild"
  }
}

proc videoPlay {} {
  global binary videofile pid

  set videoout "-vo sdl"

  .main.window.top.txt getValues -value videofile
  .main.window.vo.gl getValues -set dummy
  if { $dummy == "true"} { set videoout "-vo gl -vf format=RGB24" }
  .main.window.vo.gl2 getValues -set dummy   
  if { $dummy == "true"} { set videoout "-vo gl2" }
  .main.window.vo.sdl getValues -set dummy   
  if { $dummy == "true"} { set videoout "-vo sdl" }
  .main.window.vo.x11 getValues -set dummy   
  if { $dummy == "true"} { set videoout "-vo x11" }  

  if {$pid != "NaN"} {
    catch {exec kill $pid}
  }
  set pid [exec /bin/sh -c "$binary $videoout '$videofile' " >/dev/null 2>/dev/null & ]

  return $pid
}

# Main program
#
. realizeWidget

if { $argc > 0 } {
  .main.window.top.txt setValues -value "$argv"
  videoPlay
}

. mainLoop
