From 1ee5668ff13e70c85e9bbf3ca68a30a61d656d12 Mon Sep 17 00:00:00 2001 From: Eugene Wang Date: Sun, 18 Nov 2018 12:30:59 -0500 Subject: [PATCH] Added metrics triplet. --- src/NTwain/Data/TwainTypes.cs | 2 +- src/NTwain/Data/TwainTypesExtended.cs | 4 -- src/NTwain/Threading/ThreadDispatcher.cs | 23 +++++++++ src/NTwain/Triplets/Control/Metrics.cs | 52 ++++++++++++++++++++ src/NTwain/Triplets/DGControl.cs | 6 +++ src/NTwain/Triplets/NativeMethods.Linux32.cs | 9 ++++ src/NTwain/Triplets/NativeMethods.Linux64.cs | 9 ++++ src/NTwain/Triplets/NativeMethods.Mac32.cs | 9 ++++ src/NTwain/Triplets/NativeMethods.Mac64.cs | 9 ++++ src/NTwain/Triplets/NativeMethods.Win32.cs | 9 ++++ src/NTwain/Triplets/NativeMethods.Win64.cs | 9 ++++ 11 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 src/NTwain/Threading/ThreadDispatcher.cs create mode 100644 src/NTwain/Triplets/Control/Metrics.cs diff --git a/src/NTwain/Data/TwainTypes.cs b/src/NTwain/Data/TwainTypes.cs index 2a6c5a2..2eb7b4c 100644 --- a/src/NTwain/Data/TwainTypes.cs +++ b/src/NTwain/Data/TwainTypes.cs @@ -423,7 +423,7 @@ namespace NTwain.Data [StructLayout(LayoutKind.Sequential, Pack = 2)] partial struct TW_METRICS { - TW_UINT32 _sizeOf; + internal TW_UINT32 SizeOf; TW_UINT32 _imageCount; TW_UINT32 _sheetCount; } diff --git a/src/NTwain/Data/TwainTypesExtended.cs b/src/NTwain/Data/TwainTypesExtended.cs index 1f835d8..0f410cc 100644 --- a/src/NTwain/Data/TwainTypesExtended.cs +++ b/src/NTwain/Data/TwainTypesExtended.cs @@ -1937,10 +1937,6 @@ namespace NTwain.Data /// public partial struct TW_METRICS { - /// - /// Set by the application. Specifies the number of bytes in the structure - /// - public uint SizeOf => _sizeOf; /// /// The number of images made available for transfer by the driver. This is not /// necessarily the same as the number of images actually transferred, since the diff --git a/src/NTwain/Threading/ThreadDispatcher.cs b/src/NTwain/Threading/ThreadDispatcher.cs new file mode 100644 index 0000000..04dc497 --- /dev/null +++ b/src/NTwain/Threading/ThreadDispatcher.cs @@ -0,0 +1,23 @@ +//using System; +//using System.Collections.Generic; +//using System.Linq; +//using System.Text; +//using System.Threading; + +//namespace NTwain.Threading +//{ +// /// +// /// A base thread-marshalling class that does no marshalling. +// /// +// public class ThreadDispatcher +// { +// /// +// /// Invokes an action and returns after it's done. +// /// +// /// +// public virtual void Invoke(Action action) +// { +// action(); SynchronizationContext.Current +// } +// } +//} diff --git a/src/NTwain/Triplets/Control/Metrics.cs b/src/NTwain/Triplets/Control/Metrics.cs new file mode 100644 index 0000000..61034be --- /dev/null +++ b/src/NTwain/Triplets/Control/Metrics.cs @@ -0,0 +1,52 @@ +using NTwain.Data; +using NTwain.Internals; +using System.Runtime.InteropServices; + +namespace NTwain.Triplets.Control +{ + /// + /// Represents . + /// + public sealed class Metrics : BaseTriplet + { + internal Metrics(TwainSession session) : base(session) { } + + /// + /// Reads information relating to the last time DG_CONTROL / DAT_USERINTERFACE / MSG_ENABLEDS was sent. + /// An application calls this to get final counts after scanning. This is + /// necessary because some metrics cannot be detected during scanning, such as blank images + /// discarded at the very end of a session. + /// + /// + /// + public ReturnCode Get(ref TW_METRICS metrics) + { + metrics.SizeOf = (uint)Marshal.SizeOf(typeof(TW_METRICS)); + + if (Is32Bit) + { + if (IsWin) + return NativeMethods.DsmWin32(Session.Config.App32, Session.CurrentSource.Identity32, + DataGroups.Control, DataArgumentType.Metrics, Message.Get, ref metrics); + if (IsLinux) + return NativeMethods.DsmLinux32(Session.Config.App32, Session.CurrentSource.Identity32, + DataGroups.Control, DataArgumentType.Metrics, Message.Get, ref metrics); + if (IsMac) + return NativeMethods.DsmMac32(Session.Config.App32, Session.CurrentSource.Identity32, + DataGroups.Control, DataArgumentType.Metrics, Message.Get, ref metrics); + } + + if (IsWin) + return NativeMethods.DsmWin64(Session.Config.App32, Session.CurrentSource.Identity32, + DataGroups.Control, DataArgumentType.Metrics, Message.Get, ref metrics); + if (IsLinux) + return NativeMethods.DsmLinux64(Session.Config.App32, Session.CurrentSource.Identity32, + DataGroups.Control, DataArgumentType.Metrics, Message.Get, ref metrics); + if (IsMac) + return NativeMethods.DsmMac64(Session.Config.App32, Session.CurrentSource.Identity32, + DataGroups.Control, DataArgumentType.Metrics, Message.Get, ref metrics); + + return ReturnCode.Failure; + } + } +} \ No newline at end of file diff --git a/src/NTwain/Triplets/DGControl.cs b/src/NTwain/Triplets/DGControl.cs index c394d55..c6d53a4 100644 --- a/src/NTwain/Triplets/DGControl.cs +++ b/src/NTwain/Triplets/DGControl.cs @@ -86,5 +86,11 @@ namespace NTwain.Triplets /// Gets the operations defined for DAT_XFERGROUP. /// public XferGroup XferGroup => _xferGroup ?? (_xferGroup = new XferGroup(Session)); + + Metrics _metrics; + /// + /// Gets the operations defined for DAT_METRICS. + /// + public Metrics Metrics => _metrics ?? (_metrics = new Metrics(Session)); } } diff --git a/src/NTwain/Triplets/NativeMethods.Linux32.cs b/src/NTwain/Triplets/NativeMethods.Linux32.cs index cd61952..2780857 100644 --- a/src/NTwain/Triplets/NativeMethods.Linux32.cs +++ b/src/NTwain/Triplets/NativeMethods.Linux32.cs @@ -279,5 +279,14 @@ namespace NTwain.Triplets Message msg, ref TW_MEMORY data); + [DllImport(LinuxDll, EntryPoint = EntryName)] + public static extern ReturnCode DsmLinux32( + [In, Out]TW_IDENTITY origin, + [In, Out]TW_IDENTITY destination, + DataGroups dg, + DataArgumentType dat, + Message msg, + ref TW_METRICS data); + } } diff --git a/src/NTwain/Triplets/NativeMethods.Linux64.cs b/src/NTwain/Triplets/NativeMethods.Linux64.cs index 91f5306..d8a5b33 100644 --- a/src/NTwain/Triplets/NativeMethods.Linux64.cs +++ b/src/NTwain/Triplets/NativeMethods.Linux64.cs @@ -279,5 +279,14 @@ namespace NTwain.Triplets Message msg, ref TW_MEMORY data); + [DllImport(LinuxDll, EntryPoint = EntryName)] + public static extern ReturnCode DsmLinux64( + [In, Out]TW_IDENTITY origin, + [In, Out]TW_IDENTITY destination, + DataGroups dg, + DataArgumentType dat, + Message msg, + ref TW_METRICS data); + } } diff --git a/src/NTwain/Triplets/NativeMethods.Mac32.cs b/src/NTwain/Triplets/NativeMethods.Mac32.cs index a224f32..c8523e6 100644 --- a/src/NTwain/Triplets/NativeMethods.Mac32.cs +++ b/src/NTwain/Triplets/NativeMethods.Mac32.cs @@ -279,5 +279,14 @@ namespace NTwain.Triplets Message msg, ref TW_MEMORY data); + [DllImport(Mac32Dll, EntryPoint = EntryName)] + public static extern ReturnCode DsmMac32( + [In, Out]TW_IDENTITY origin, + [In, Out]TW_IDENTITY destination, + DataGroups dg, + DataArgumentType dat, + Message msg, + ref TW_METRICS data); + } } diff --git a/src/NTwain/Triplets/NativeMethods.Mac64.cs b/src/NTwain/Triplets/NativeMethods.Mac64.cs index f5c014f..4b50cc1 100644 --- a/src/NTwain/Triplets/NativeMethods.Mac64.cs +++ b/src/NTwain/Triplets/NativeMethods.Mac64.cs @@ -279,5 +279,14 @@ namespace NTwain.Triplets Message msg, ref TW_MEMORY data); + [DllImport(Mac64Dll, EntryPoint = EntryName)] + public static extern ReturnCode DsmMac64( + [In, Out]TW_IDENTITY origin, + [In, Out]TW_IDENTITY destination, + DataGroups dg, + DataArgumentType dat, + Message msg, + ref TW_METRICS data); + } } diff --git a/src/NTwain/Triplets/NativeMethods.Win32.cs b/src/NTwain/Triplets/NativeMethods.Win32.cs index da7ad20..71d7de6 100644 --- a/src/NTwain/Triplets/NativeMethods.Win32.cs +++ b/src/NTwain/Triplets/NativeMethods.Win32.cs @@ -279,5 +279,14 @@ namespace NTwain.Triplets Message msg, ref TW_MEMORY data); + [DllImport(WinDll, EntryPoint = EntryName)] + public static extern ReturnCode DsmWin32( + [In, Out]TW_IDENTITY origin, + [In, Out]TW_IDENTITY destination, + DataGroups dg, + DataArgumentType dat, + Message msg, + ref TW_METRICS data); + } } diff --git a/src/NTwain/Triplets/NativeMethods.Win64.cs b/src/NTwain/Triplets/NativeMethods.Win64.cs index 0a1dad9..15cd87b 100644 --- a/src/NTwain/Triplets/NativeMethods.Win64.cs +++ b/src/NTwain/Triplets/NativeMethods.Win64.cs @@ -279,5 +279,14 @@ namespace NTwain.Triplets Message msg, ref TW_MEMORY data); + [DllImport(WinDll, EntryPoint = EntryName)] + public static extern ReturnCode DsmWin64( + [In, Out]TW_IDENTITY origin, + [In, Out]TW_IDENTITY destination, + DataGroups dg, + DataArgumentType dat, + Message msg, + ref TW_METRICS data); + } }