/*
 * $VER: ToasterGrab 2.0.0 (22.7.94)
 *
 * Arexx program for the ImageFX 2.0 image processing system.
 * Written by Thomas Krehbiel
 *
 * Grab a frame from the Video Toaster frame buffer.
 *
 */

OPTIONS RESULTS

toastport   = 'ToasterARexx.port'
fsdev       = 'RAM:'
fspath      = fsdev||'FrameStore'
fsname      = 'TempIFX'
fsnum       = 998
fsfullname  = fspath||'/'fsnum||'.FS.'||fsname

GetMain
IF rc = 0 THEN DO
   RequestResponse 'Current buffer will be destroyed.'
   IF rc ~= 0 THEN EXIT
   END

IF (~SHOW('L', toastport)) THEN
	ADDLIB(toastport, -1)

IF ~SHOW('P', toastport) THEN DO
   RequestNotify 'Switcher is not running.'
   EXIT
   END

IF ~EXISTS(fspath) THEN
   ADDRESS COMMAND 'Makedir' fspath

RequestThree '"Select FrameBuffer To Grab:"' 'DV1' 'DV2' 'Cancel'
IF rc ~= 0 THEN EXIT

Switcher(TOSW)
SELECT
   WHEN result = 'DV1' THEN DO
      Switcher(MDV1)
      Switcher(PDV1)
      END
   WHEN result = 'DV2' THEN DO
      Switcher(MDV2)
      Switcher(PDV2)
      END
   OTHERWISE
      NOP
   END

oldfsdev = Switcher(FSNM)
IF oldfspath = 'RAM:' THEN
   Switcher(FSBD)
ELSE DO
   IF Switcher(FSDV, fsdev) ~= SWITCHER THEN DO
      Switcher(TOWB)
      ScreenToFront
      RequestNotify 'Could not change FrameStore device.'
      CALL Abort
      END
   END

i = Switcher(FMXI, fsnum)
IF i = 1 THEN DO
   Switcher(TOWB)
   ScreenToFront
   RequestResponse 'FrameStore' fsnum 'already exists.  Delete it?'
   IF rc ~= 0 THEN CALL Abort

   IF Switcher(FMDL, fsnum) ~= SWITCHER THEN DO
      Switcher(TOWB)
      ScreenToFront
      RequestNotify 'Could not delete FrameStore' fsnum
      CALL Abort
      END

   END

IF Switcher(FMSV, fsnum, fsname) ~= SWITCHER THEN DO
   Switcher(TOWB)
   ScreenToFront
   RequestNotify 'Could not save temporary file.'
   CALL Abort
   END

Switcher(TOWB)
ScreenToFront

LoadBufferAs 'FrameStore' fsfullname
IF rc ~= 0 THEN DO
   CALL Abort
   END

/* fall through */

Abort:

   IF EXISTS(fsfullname) THEN
      ADDRESS COMMAND 'Delete' fsfullname

   IF oldfsdev ~= 'OLDFSDEV' THEN
      Switcher(FSDV, oldfsdev)

   EXIT
