Progress on transfer logic.

This commit is contained in:
Eugene Wang
2023-04-06 22:47:47 -04:00
parent fd44d9dddb
commit 242e3eba15
7 changed files with 145 additions and 44 deletions

View File

@@ -86,6 +86,7 @@ namespace WinFormSample
{
it.SubItems.Add("");
it.SubItems.Add("");
it.SubItems.Add("");
}
it.SubItems.Add(extended.Contains(c).ToString());
it.SubItems.Add(twain.QueryCapSupport(c).ToString());
@@ -130,9 +131,9 @@ namespace WinFormSample
return sival.ToString();
case TWTY.BOOL:
sts = forCurrent ?
twain.GetCapCurrent(cap, out usval) :
twain.GetCapDefault(cap, out usval);
return usval.ToString();
twain.GetCapCurrent(cap, out TW_BOOL tbval) :
twain.GetCapDefault(cap, out tbval);
return tbval.ToString();
case TWTY.FIX32:
sts = forCurrent ?
twain.GetCapCurrent(cap, out TW_FIX32 fxval) :

View File

@@ -97,7 +97,7 @@ namespace NTwain.Data
/// <summary>
/// TWAIN's boolean values.
/// </summary>
public enum BoolType : ushort
public enum TW_BOOL : ushort
{
/// <summary>
/// The false value (0).
@@ -511,7 +511,7 @@ namespace NTwain.Data
/// <returns></returns>
public override string ToString()
{
return $"{Left},{Top},{Right},{Bottom}";
return $"({Left},{Top}) ({Right},{Bottom})";
}
public bool Equals(TW_FRAME other)

View File

@@ -85,17 +85,22 @@ namespace NTwain
return (ushort)TWRC.SUCCESS;
}
bool _closeDsRequested;
private void HandleSourceMsg(MSG msg)
{
// the reason we post these to the background is
// if they're coming from UI message loop then
// this needs to return asap
switch (msg)
{
case MSG.XFERREADY:
case MSG.DEVICEEVENT:
case MSG.CLOSEDSOK:
_bgPendingMsgs.Add(msg);
break;
case MSG.CLOSEDSREQ:
// the reason we post these to the background is
// if they're coming from UI message loop then
// this needs to return asap
_closeDsRequested = true;
_bgPendingMsgs.Add(msg);
break;
}

View File

@@ -125,7 +125,8 @@ namespace NTwain
/// <summary>
/// Fires when <see cref="State"/> changes.
/// Fires when <see cref="State"/> changes.
/// This is not guaranteed to be raised on the UI thread.
/// </summary>
public event Action<TwainAppSession, STATE>? StateChanged;

View File

@@ -26,6 +26,7 @@ namespace NTwain
/// <summary>
/// Shows the TWAIN source selection UI for setting the default source.
/// Only included for completeness, not recommended for real app usage.
/// </summary>
public STS ShowUserSelect()
{
@@ -110,7 +111,7 @@ namespace NTwain
// already enabled :(
// TODO: should bring it down?
}
_closeDsRequested = false;
_userInterface = new TW_USERINTERFACE
{
ShowUI = (ushort)((showUI || uiOnly) ? 1 : 0),

View File

@@ -58,8 +58,8 @@ namespace NTwain
else if (xferAudio) GetCapCurrent(CAP.ACAP_XFERMECH, out audXferMech);
TW_PENDINGXFERS pending = default;
var rc = DGControl.PendingXfers.Get(ref _appIdentity, ref _currentDS, ref pending);
if (rc == TWRC.SUCCESS)
var sts = WrapInSTS(DGControl.PendingXfers.Get(ref _appIdentity, ref _currentDS, ref pending));
if (sts.RC == TWRC.SUCCESS)
{
do
{
@@ -73,37 +73,70 @@ namespace NTwain
catch { } // don't let consumer kill the loop if they have exception
});
switch (readyArgs.Cancel)
if (readyArgs.Cancel == CancelType.EndNow || _closeDsRequested)
{
case CancelType.SkipCurrent:
rc = DGControl.PendingXfers.EndXfer(ref _appIdentity, ref _currentDS, ref pending);
break;
case CancelType.EndNow:
rc = DGControl.PendingXfers.Reset(ref _appIdentity, ref _currentDS, ref pending);
break;
case CancelType.Graceful:
//??? oh boy
rc = DGControl.PendingXfers.EndXfer(ref _appIdentity, ref _currentDS, ref pending);
break;
default:
// normal capture
if (xferImage)
{
}
else if (xferAudio)
{
}
rc = DGControl.PendingXfers.EndXfer(ref _appIdentity, ref _currentDS, ref pending);
break;
sts = WrapInSTS(DGControl.PendingXfers.Reset(ref _appIdentity, ref _currentDS, ref pending));
if (sts.RC == TWRC.SUCCESS) State = STATE.S5;
break;
}
if (readyArgs.Cancel == CancelType.Graceful)
{
sts = WrapInSTS(DGControl.PendingXfers.StopFeeder(ref _appIdentity, ref _currentDS, ref pending));
}
} while (rc == TWRC.SUCCESS && pending.Count != 0);
if (readyArgs.Cancel != CancelType.SkipCurrent)
{
// transfer normally
if (xferImage)
{
switch (imgXferMech)
{
case TWSX.NATIVE:
TransferNativeImage();
break;
case TWSX.FILE:
TransferFileImage();
break;
case TWSX.MEMORY:
TransferMemoryImage();
break;
case TWSX.MEMFILE:
TransferMemoryFileImage();
break;
}
}
else if (xferAudio)
{
switch (audXferMech)
{
case TWSX.NATIVE:
TransferNativeAudio();
break;
case TWSX.FILE:
TransferFileAudio();
break;
}
}
}
sts = WrapInSTS(DGControl.PendingXfers.EndXfer(ref _appIdentity, ref _currentDS, ref pending));
if (sts.RC == TWRC.SUCCESS)
{
if (xferImage)
{
State = pending.Count == 0 ? STATE.S5 : STATE.S6;
}
else if (xferAudio)
{
State = STATE.S6;
}
}
} while (sts.RC == TWRC.SUCCESS && pending.Count != 0);
}
else
{
HandleNonSuccessXferCode(rc);
HandleNonSuccessXferCode(sts);
}
_uiThreadMarshaller.BeginInvoke(() =>
{
@@ -111,20 +144,70 @@ namespace NTwain
});
}
private void HandleNonSuccessXferCode(TWRC rc)
private void TransferFileAudio()
{
switch (rc)
}
private void TransferNativeAudio()
{
}
private void TransferMemoryFileImage()
{
}
private void TransferMemoryImage()
{
}
private void TransferFileImage()
{
}
private void TransferNativeImage()
{
}
private void HandleNonSuccessXferCode(STS sts)
{
switch (sts.RC)
{
case TWRC.SUCCESS:
case TWRC.XFERDONE:
case TWRC.CANCEL:
// ok to keep going
break;
case TWRC.CANCEL:
TW_PENDINGXFERS pending = default;
DGControl.PendingXfers.EndXfer(ref _appIdentity, ref _currentDS, ref pending);
break;
default:
var status = GetLastStatus(false);
var text = GetStatusText(status);
// TODO: raise error event
switch (sts.STATUS.ConditionCode)
{
case TWCC.DAMAGEDCORNER:
case TWCC.DOCTOODARK:
case TWCC.DOCTOOLIGHT:
case TWCC.FOCUSERROR:
case TWCC.NOMEDIA:
case TWCC.PAPERDOUBLEFEED:
case TWCC.PAPERJAM:
pending = default;
DGControl.PendingXfers.EndXfer(ref _appIdentity, ref _currentDS, ref pending);
break;
case TWCC.OPERATIONERROR:
GetCapCurrent(CAP.CAP_INDICATORS, out TW_BOOL showIndicator);
if (_userInterface.ShowUI == 0 && showIndicator == TW_BOOL.False)
{
// todo: alert user and drop to S4
}
break;
}
break;
}
}

View File

@@ -249,7 +249,17 @@ namespace NTwain
protected STS WrapInSTS(TWRC rc, bool dsmOnly = false)
{
if (rc != TWRC.FAILURE) return new STS { RC = rc };
return new STS { RC = rc, STATUS = GetLastStatus(dsmOnly) };
var sts = new STS { RC = rc, STATUS = GetLastStatus() };
if (sts.STATUS.ConditionCode == TWCC.BADDEST)
{
// TODO: the current ds is bad, should assume we're back in S3?
// needs the dest parameter to find out.
}
else if (sts.STATUS.ConditionCode == TWCC.BUMMER)
{
// TODO: notify with critical event to end the twain stuff
}
return sts;
}
/// <summary>