Attempt to resurrect my old triplet ideas with new data types and native methods.

This commit is contained in:
Eugene Wang
2023-04-01 12:57:07 -04:00
parent 260ae9fdef
commit e68525775d
17 changed files with 479 additions and 10 deletions

View File

@@ -14,7 +14,7 @@ namespace TWAINWorkingGroup
{
static TwainPlatform()
{
Is64bit = IntPtr.Size == 8;
Is32bit = IntPtr.Size == 4;
#if NETFRAMEWORK
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
@@ -64,7 +64,14 @@ namespace TWAINWorkingGroup
/// <summary>
/// Whether the code is running in 64bit or 32bit.
/// </summary>
public static bool Is64bit { get; }
public static bool Is32bit { get; }
/// <summary>
/// Whether to use the older DSM lib on Windows and Mac.
/// On Windows it only takes effect when running in 32bit.
/// Defaults to false.
/// </summary>
public static bool PreferLegacyDSM { get; set; }
}
/// <summary>

View File

@@ -0,0 +1,85 @@
using System;
using TWAINWorkingGroup;
namespace NTwain.Triplets
{
/// <summary>
/// Contains calls used with <see cref="DG.CONTROL"/> and <see cref="DAT.PARENT"/>.
/// </summary>
public class DATParent : TripletBase
{
public DATParent(TwainSession session) : base(session)
{
}
/// <summary>
/// Loads and opens the DSM.
/// </summary>
/// <param name="hwnd">Required if on Windows.</param>
/// <returns></returns>
public STS OpenDSM(ref IntPtr hwnd)
{
STS rc;
if ((rc = DoIt(MSG.OPENDSM, ref hwnd)) == STS.SUCCESS)
{
Session.State = STATE.S3;
}
return rc;
}
/// <summary>
/// Closes the DSM.
/// </summary>
/// <param name="hwnd">Required if on Windows.</param>
/// <returns></returns>
public STS CloseDSM(ref IntPtr hwnd)
{
STS rc;
if ((rc = DoIt(MSG.CLOSEDSM, ref hwnd)) == STS.SUCCESS)
{
Session.State = STATE.S2;
}
return rc;
}
STS DoIt(MSG msg, ref IntPtr hwnd)
{
var rc = STS.FAILURE;
if (TwainPlatform.IsWindows)
{
var app = Session._appIdentityLegacy;
if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM)
{
rc = (STS)NativeMethods.WindowsTwain32DsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
}
else
{
rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
}
if (rc == STS.SUCCESS) Session._appIdentity = app;
}
else if (TwainPlatform.IsLinux)
{
var app = Session._appIdentityLegacy;
rc = (STS)NativeMethods.LinuxDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
if (rc == STS.SUCCESS) Session._appIdentity = app;
}
else if (TwainPlatform.IsMacOSX)
{
var app = Session._appIdentityOSX;
if (TwainPlatform.PreferLegacyDSM)
{
rc = (STS)NativeMethods.MacosxTwainDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
}
else
{
rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
}
if (rc == STS.SUCCESS) Session._appIdentityOSX = app;
}
return rc;
}
}
}

View File

@@ -0,0 +1,14 @@
using TWAINWorkingGroup;
namespace NTwain.Triplets
{
/// <summary>
/// Contains calls used with <see cref="DG.AUDIO"/>.
/// </summary>
public class DGAudio : TripletBase
{
public DGAudio(TwainSession session) : base(session)
{
}
}
}

View File

@@ -0,0 +1,16 @@
namespace NTwain.Triplets
{
/// <summary>
/// Contains calls used with <see cref="DG.CONTROL"/>.
/// </summary>
public class DGControl : TripletBase
{
public DGControl(TwainSession session) : base(session)
{
}
private DATParent? _parent;
public DATParent Parent => _parent ??= new DATParent(Session);
}
}

View File

@@ -0,0 +1,14 @@
using TWAINWorkingGroup;
namespace NTwain.Triplets
{
/// <summary>
/// Contains calls used with <see cref="DG.IMAGE"/>.
/// </summary>
public class DGImage : TripletBase
{
public DGImage(TwainSession session) : base(session)
{
}
}
}

View File

@@ -0,0 +1,22 @@
All TWAIN operations are done through the a combination of
Data Group (DG), Data Argument Type (DAT), and Message (MSG)
triplets. Rather than letting consumers of this lib deal
with all the combinations themselves and risk passing the
wrong thing, all valid triplet combinations are simply
made available under this namespace.
Example:
To get the status of the DS, just use the
"Get" method (represents MSG), in the
"Status" property (represnts DAT), in the
"DGControl" class (represents DG).
or better explained in code:
DGControl.Status.Get(...)
Only triplets usable by the
application-side are defined here.
Due to the number of things involved, the properties are
lazy-loaded until used.

View File

@@ -0,0 +1,25 @@
using System;
namespace NTwain.Triplets
{
/// <summary>
/// Base class for grouping triplet operations messages.
/// </summary>
public abstract class TripletBase
{
/// <summary>
/// Initializes a new instance of the <see cref="TripletBase" /> class.
/// </summary>
/// <param name="session">The session.</param>
/// <exception cref="System.ArgumentNullException"></exception>
public TripletBase(TwainSession session)
{
Session = session ?? throw new ArgumentNullException("session");
}
/// <summary>
/// Gets the twain session.
/// </summary>
public TwainSession Session { get; }
}
}

View File

@@ -1,4 +1,5 @@
using System;
using NTwain.Triplets;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@@ -42,7 +43,7 @@ namespace NTwain
public STATE State
{
get => _state;
private set
internal set
{
if (_state != value)
{
@@ -53,8 +54,21 @@ namespace NTwain
}
/// <summary>
/// Fired when state changes.
/// Fired when <see cref="State"/> changes.
/// </summary>
public event Action<TwainSession, STATE>? StateChanged;
/// <summary>
/// TWAIN triplet API calls with <see cref="DG.CONTROL"/>.
/// </summary>
public DGControl DGControl { get; }
/// <summary>
/// TWAIN triplet API calls with <see cref="DG.IMAGE"/>.
/// </summary>
public DGImage DGImage { get; }
/// <summary>
/// TWAIN triplet API calls with <see cref="DG.AUDIO"/>.
/// </summary>
public DGAudio DGAudio { get; }
}
}

View File

@@ -1,4 +1,5 @@
using System;
using NTwain.Triplets;
using System;
using System.Diagnostics;
using System.Text;
using TWAINWorkingGroup;
@@ -62,7 +63,7 @@ namespace NTwain
ProductName = productName,
ProtocolMajor = (ushort)TWON_PROTOCOL.MAJOR,
ProtocolMinor = (ushort)TWON_PROTOCOL.MINOR,
SupportedGroups = (uint)(supportedTypes | DG.APP2),
SupportedGroups = (uint)(supportedTypes | DG.CONTROL | DG.APP2),
Version = new TW_VERSION
{
Country = appCountry,
@@ -74,6 +75,10 @@ namespace NTwain
};
if (TwainPlatform.IsLinux) _appIdentity = _appIdentityLegacy;
else if (TwainPlatform.IsMacOSX) _appIdentityOSX = _appIdentityLegacy;
DGControl = new DGControl(this);
DGImage = new DGImage(this);
DGAudio = new DGAudio(this);
}
}
}