From 306a67f7a2da03b04e954ed912c73d6d4ad3222d Mon Sep 17 00:00:00 2001 From: Eugene Wang <8755753+soukoku@users.noreply.github.com> Date: Sun, 2 Apr 2023 13:41:16 -0400 Subject: [PATCH] Moved triplet success logic back to twainsession to reduce dependencies and identity copying in windows. --- samples/Console32/Program.cs | 4 +- samples/WinForm32/Form1.cs | 12 +-- src/NTwain/Triplets/ControlDATs/Callback.cs | 27 ++---- src/NTwain/Triplets/ControlDATs/Callback2.cs | 27 ++---- .../Triplets/ControlDATs/CustomDsData.cs | 28 ++---- .../Triplets/ControlDATs/DeviceEvent.cs | 22 ++--- src/NTwain/Triplets/ControlDATs/EntryPoint.cs | 19 ++-- src/NTwain/Triplets/ControlDATs/Identity.cs | 75 ++++----------- src/NTwain/Triplets/ControlDATs/Parent.cs | 64 +++---------- src/NTwain/Triplets/ControlDATs/Status.cs | 25 ++--- src/NTwain/Triplets/ControlDATs/StatusUtf8.cs | 17 +--- .../Triplets/ControlDATs/UserInterface.cs | 51 +++------- src/NTwain/Triplets/ControlDATs/XferGroup.cs | 26 ++--- src/NTwain/Triplets/DGAudio.cs | 5 +- src/NTwain/Triplets/DGControl.cs | 28 +++--- src/NTwain/Triplets/DGImage.cs | 5 +- src/NTwain/Triplets/TripletBase.cs | 25 ----- src/NTwain/TwainSession.Callbacks.cs | 4 +- src/NTwain/TwainSession.PropEvents.cs | 12 +-- src/NTwain/TwainSession.Sources.cs | 59 +++++++++++- src/NTwain/TwainSession.cs | 96 +++++++++++++++++-- 21 files changed, 278 insertions(+), 353 deletions(-) delete mode 100644 src/NTwain/Triplets/TripletBase.cs diff --git a/samples/Console32/Program.cs b/samples/Console32/Program.cs index 530bb93..d4d98c7 100644 --- a/samples/Console32/Program.cs +++ b/samples/Console32/Program.cs @@ -18,13 +18,13 @@ namespace SampleConsole twain.StateChanged += Twain_StateChanged; var hwnd = IntPtr.Zero; // required for windows - var rc = twain.DGControl.Parent.OpenDSM(ref hwnd); + var rc = twain.OpenDSM(hwnd); Debug.WriteLine($"OpenDSM={rc}"); if (rc == STS.SUCCESS) { Debug.WriteLine($"CloseDSM={rc}"); - rc = twain.DGControl.Parent.CloseDSM(ref hwnd); + rc = twain.CloseDSM(); } } diff --git a/samples/WinForm32/Form1.cs b/samples/WinForm32/Form1.cs index ddf6d60..fdc68a1 100644 --- a/samples/WinForm32/Form1.cs +++ b/samples/WinForm32/Form1.cs @@ -45,7 +45,7 @@ namespace WinForm32 var hwnd = this.Handle; - var rc = twain.DGControl.Parent.OpenDSM(ref hwnd); + var rc = twain.OpenDSM(hwnd); Debug.WriteLine($"OpenDSM={rc}"); } @@ -59,7 +59,7 @@ namespace WinForm32 private void btnSelect_Click(object sender, EventArgs e) { - twain.DGControl.Identity.UserSelect(); + twain.ShowUserSelect(); } private void btnEnumSources_Click(object sender, EventArgs e) @@ -75,7 +75,7 @@ namespace WinForm32 { if (listSources.SelectedItem is TW_IDENTITY_LEGACY ds) { - twain.DGControl.Identity.Set(ds); + twain.SetDefaultSource(ds); } } @@ -85,20 +85,20 @@ namespace WinForm32 { twain.TryStepdown(STATE.S3); - twain.DGControl.Identity.OpenDS(ds); + twain.OpenSource(ds); } } private void btnClose_Click(object sender, EventArgs e) { - twain.DGControl.Identity.CloseDS(); + twain.CloseSource(); } private void btnOpenDef_Click(object sender, EventArgs e) { twain.TryStepdown(STATE.S3); - twain.DGControl.Identity.OpenDS(twain.DefaultSource); + twain.OpenSource(twain.DefaultSource); } private void btnShowSettings_Click(object sender, EventArgs e) diff --git a/src/NTwain/Triplets/ControlDATs/Callback.cs b/src/NTwain/Triplets/ControlDATs/Callback.cs index 44fcea4..0bcab0c 100644 --- a/src/NTwain/Triplets/ControlDATs/Callback.cs +++ b/src/NTwain/Triplets/ControlDATs/Callback.cs @@ -6,49 +6,38 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class Callback : TripletBase + public class Callback { - public Callback(TwainSession session) : base(session) - { - } - /// /// Registers the callback function. /// /// /// - public STS RegisterCallback(ref TW_CALLBACK data) - { - return DoIt(MSG.REGISTER_CALLBACK, ref data); - } - - STS DoIt(MSG msg, ref TW_CALLBACK data) + public STS RegisterCallback(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, ref TW_CALLBACK data) { var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; - var ds = Session.CurrentSource; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { - rc = (STS)WinLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK, msg, ref data); + rc = (STS)WinLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK, MSG.REGISTER_CALLBACK, ref data); } else { - rc = (STS)WinNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK, msg, ref data); + rc = (STS)WinNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK, MSG.REGISTER_CALLBACK, ref data); } } else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; - TW_IDENTITY_MACOSX ds = Session.CurrentSource; + TW_IDENTITY_MACOSX app2 = app; + TW_IDENTITY_MACOSX ds2 = ds; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK, msg, ref data); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.CALLBACK, MSG.REGISTER_CALLBACK, ref data); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK, msg, ref data); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.CALLBACK, MSG.REGISTER_CALLBACK, ref data); } } return rc; diff --git a/src/NTwain/Triplets/ControlDATs/Callback2.cs b/src/NTwain/Triplets/ControlDATs/Callback2.cs index bfb5017..bdcd3c7 100644 --- a/src/NTwain/Triplets/ControlDATs/Callback2.cs +++ b/src/NTwain/Triplets/ControlDATs/Callback2.cs @@ -6,49 +6,38 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class Callback2 : TripletBase + public class Callback2 { - public Callback2(TwainSession session) : base(session) - { - } - /// /// Registers the callback function. /// /// /// - public STS RegisterCallback(ref TW_CALLBACK2 data) - { - return DoIt(MSG.REGISTER_CALLBACK, ref data); - } - - STS DoIt(MSG msg, ref TW_CALLBACK2 data) + public STS RegisterCallback(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, ref TW_CALLBACK2 data) { var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; - var ds = Session.CurrentSource; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { - rc = (STS)WinLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK2, msg, ref data); + rc = (STS)WinLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK2, MSG.REGISTER_CALLBACK, ref data); } else { - rc = (STS)WinNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK2, msg, ref data); + rc = (STS)WinNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK2, MSG.REGISTER_CALLBACK, ref data); } } else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; - TW_IDENTITY_MACOSX ds = Session.CurrentSource; + TW_IDENTITY_MACOSX app2 = app; + TW_IDENTITY_MACOSX ds2 = ds; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK2, msg, ref data); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.CALLBACK2, MSG.REGISTER_CALLBACK, ref data); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CALLBACK2, msg, ref data); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.CALLBACK2, MSG.REGISTER_CALLBACK, ref data); } } return rc; diff --git a/src/NTwain/Triplets/ControlDATs/CustomDsData.cs b/src/NTwain/Triplets/ControlDATs/CustomDsData.cs index f08e8d8..e841082 100644 --- a/src/NTwain/Triplets/ControlDATs/CustomDsData.cs +++ b/src/NTwain/Triplets/ControlDATs/CustomDsData.cs @@ -6,21 +6,17 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class CustomDsData : TripletBase + public class CustomDsData { - public CustomDsData(TwainSession session) : base(session) - { - } - /// /// Loads the custom DS data. /// /// /// - public STS Get(out TW_CUSTOMDSDATA data) + public STS Get(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, out TW_CUSTOMDSDATA data) { data = default; - return DoIt(MSG.GET, ref data); + return DoIt(ref app, ref ds, MSG.GET, ref data); } /// @@ -28,18 +24,14 @@ namespace NTwain.Triplets.ControlDATs /// /// /// - public STS Set(ref TW_CUSTOMDSDATA data) - { - return DoIt(MSG.SET, ref data); - } + public STS Set(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, ref TW_CUSTOMDSDATA data) + => DoIt(ref app, ref ds, MSG.SET, ref data); - STS DoIt(MSG msg, ref TW_CUSTOMDSDATA data) + static STS DoIt(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, MSG msg, ref TW_CUSTOMDSDATA data) { var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; - var ds = Session.CurrentSource; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { rc = (STS)WinLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CUSTOMDSDATA, msg, ref data); @@ -51,15 +43,15 @@ namespace NTwain.Triplets.ControlDATs } else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; - TW_IDENTITY_MACOSX ds = Session.CurrentSource; + TW_IDENTITY_MACOSX app2 = app; + TW_IDENTITY_MACOSX ds2 = ds; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CUSTOMDSDATA, msg, ref data); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.CUSTOMDSDATA, msg, ref data); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.CUSTOMDSDATA, msg, ref data); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.CUSTOMDSDATA, msg, ref data); } } return rc; diff --git a/src/NTwain/Triplets/ControlDATs/DeviceEvent.cs b/src/NTwain/Triplets/ControlDATs/DeviceEvent.cs index 97e9274..3e62bea 100644 --- a/src/NTwain/Triplets/ControlDATs/DeviceEvent.cs +++ b/src/NTwain/Triplets/ControlDATs/DeviceEvent.cs @@ -6,30 +6,24 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class DeviceEvent : TripletBase + public class DeviceEvent { - public DeviceEvent(TwainSession session) : base(session) - { - } - /// /// Gets the device event detail. /// /// /// - public STS Get(out TW_DEVICEEVENT data) + public STS Get(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, out TW_DEVICEEVENT data) { data = default; - return DoIt(MSG.GET, ref data); + return DoIt(ref app, ref ds, MSG.GET, ref data); } - STS DoIt(MSG msg, ref TW_DEVICEEVENT data) + static STS DoIt(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, MSG msg, ref TW_DEVICEEVENT data) { var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; - var ds = Session.CurrentSource; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { rc = (STS)WinLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.DEVICEEVENT, msg, ref data); @@ -41,15 +35,15 @@ namespace NTwain.Triplets.ControlDATs } else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; - TW_IDENTITY_MACOSX ds = Session.CurrentSource; + TW_IDENTITY_MACOSX app2 = app; + TW_IDENTITY_MACOSX ds2 = ds; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.DEVICEEVENT, msg, ref data); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.DEVICEEVENT, msg, ref data); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.DEVICEEVENT, msg, ref data); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.DEVICEEVENT, msg, ref data); } } return rc; diff --git a/src/NTwain/Triplets/ControlDATs/EntryPoint.cs b/src/NTwain/Triplets/ControlDATs/EntryPoint.cs index 78ecf23..659423a 100644 --- a/src/NTwain/Triplets/ControlDATs/EntryPoint.cs +++ b/src/NTwain/Triplets/ControlDATs/EntryPoint.cs @@ -8,22 +8,18 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class EntryPoint : TripletBase + public class EntryPoint { - public EntryPoint(TwainSession session) : base(session) - { - } - /// /// Loads and opens the DSM. /// /// /// - public STS Get(out TW_ENTRYPOINT_DELEGATES entry) + public STS Get(ref TW_IDENTITY_LEGACY app, out TW_ENTRYPOINT_DELEGATES entry) { entry = default; TW_ENTRYPOINT rawentry = default; - var rc = DoIt(MSG.GET, ref rawentry); + var rc = DoIt(ref app, MSG.GET, ref rawentry); if (rc == STS.SUCCESS) { entry.Size = rawentry.Size; @@ -48,12 +44,11 @@ namespace NTwain.Triplets.ControlDATs return rc; } - STS DoIt(MSG msg, ref TW_ENTRYPOINT entry) + static STS DoIt(ref TW_IDENTITY_LEGACY app, MSG msg, ref TW_ENTRYPOINT entry) { var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { rc = (STS)WinLegacyDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.ENTRYPOINT, msg, ref entry); @@ -69,14 +64,14 @@ namespace NTwain.Triplets.ControlDATs //} else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; + TW_IDENTITY_MACOSX app2 = app; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.ENTRYPOINT, msg, ref entry); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, IntPtr.Zero, DG.CONTROL, DAT.ENTRYPOINT, msg, ref entry); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.ENTRYPOINT, msg, ref entry); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, IntPtr.Zero, DG.CONTROL, DAT.ENTRYPOINT, msg, ref entry); } } return rc; diff --git a/src/NTwain/Triplets/ControlDATs/Identity.cs b/src/NTwain/Triplets/ControlDATs/Identity.cs index 221a109..569be39 100644 --- a/src/NTwain/Triplets/ControlDATs/Identity.cs +++ b/src/NTwain/Triplets/ControlDATs/Identity.cs @@ -7,59 +7,32 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class Identity : TripletBase + public class Identity { - public Identity(TwainSession session) : base(session) - { - } - /// /// Loads and opens the specified data source. /// /// /// - public STS OpenDS(TW_IDENTITY_LEGACY ds) // not a ref on purpose - { - STS rc; - if ((rc = DoIt(MSG.OPENDS, ref ds)) == STS.SUCCESS) - { - Session.CurrentSource = ds; - Session.RegisterCallback(); - Session.State = STATE.S4; - } - return rc; - } + public STS OpenDS(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds) + => DoIt(ref app, MSG.OPENDS, ref ds); /// /// Closes the currently open data source. /// /// - public STS CloseDS() - { - STS rc; - var ds = Session.CurrentSource; - if ((rc = DoIt(MSG.CLOSEDS, ref ds)) == STS.SUCCESS) - { - Session.State = STATE.S3; - Session.CurrentSource = default; - } - return rc; - } + public STS CloseDS(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds) + => DoIt(ref app, MSG.CLOSEDS, ref ds); /// /// Opens the TWAIN data source selector dialog /// to choose the default data source. /// /// - public STS UserSelect() + public STS UserSelect(ref TW_IDENTITY_LEGACY app, out TW_IDENTITY_LEGACY ds) { - STS rc; - var ds = Session.DefaultSource; - if ((rc = DoIt(MSG.USERSELECT, ref ds)) == STS.SUCCESS) - { - Session.DefaultSource = ds; - } - return rc; + ds = default; + return DoIt(ref app, MSG.USERSELECT, ref ds); } /// @@ -67,10 +40,10 @@ namespace NTwain.Triplets.ControlDATs /// /// /// - public STS GetDefault(out TW_IDENTITY_LEGACY ds) + public STS GetDefault(ref TW_IDENTITY_LEGACY app, out TW_IDENTITY_LEGACY ds) { ds = default; - return DoIt(MSG.GETDEFAULT, ref ds); + return DoIt(ref app, MSG.GETDEFAULT, ref ds); } /// @@ -78,15 +51,8 @@ namespace NTwain.Triplets.ControlDATs /// /// /// - public STS Set(TW_IDENTITY_LEGACY ds) - { - STS rc; - if ((rc = DoIt(MSG.SET, ref ds)) == STS.SUCCESS) - { - Session.DefaultSource = ds; - } - return rc; - } + public STS Set(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds) + => DoIt(ref app, MSG.SET, ref ds); /// /// Gets the first available data source in an enumerating fashion @@ -94,10 +60,10 @@ namespace NTwain.Triplets.ControlDATs /// /// /// - public STS GetFirst(out TW_IDENTITY_LEGACY ds) + public STS GetFirst(ref TW_IDENTITY_LEGACY app, out TW_IDENTITY_LEGACY ds) { ds = default; - return DoIt(MSG.GETFIRST, ref ds); + return DoIt(ref app, MSG.GETFIRST, ref ds); } /// @@ -106,19 +72,18 @@ namespace NTwain.Triplets.ControlDATs /// /// /// - public STS GetNext(out TW_IDENTITY_LEGACY ds) + public STS GetNext(ref TW_IDENTITY_LEGACY app, out TW_IDENTITY_LEGACY ds) { ds = default; - return DoIt(MSG.GETNEXT, ref ds); + return DoIt(ref app, MSG.GETNEXT, ref ds); } - STS DoIt(MSG msg, ref TW_IDENTITY_LEGACY ds) + static STS DoIt(ref TW_IDENTITY_LEGACY app, MSG msg, ref TW_IDENTITY_LEGACY ds) { var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { rc = (STS)WinLegacyDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, msg, ref ds); @@ -130,15 +95,15 @@ namespace NTwain.Triplets.ControlDATs } else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; + TW_IDENTITY_MACOSX app2 = app; TW_IDENTITY_MACOSX osxds = ds; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, msg, ref osxds); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, msg, ref osxds); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, msg, ref osxds); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, msg, ref osxds); } ds = osxds; } diff --git a/src/NTwain/Triplets/ControlDATs/Parent.cs b/src/NTwain/Triplets/ControlDATs/Parent.cs index 8dd32a8..639d6a3 100644 --- a/src/NTwain/Triplets/ControlDATs/Parent.cs +++ b/src/NTwain/Triplets/ControlDATs/Parent.cs @@ -7,68 +7,31 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class Parent : TripletBase + public class Parent { - public Parent(TwainSession session) : base(session) - { - } - /// /// Loads and opens the DSM. /// + /// /// Required if on Windows. /// - public STS OpenDSM(ref IntPtr hwnd) - { - STS rc; - if ((rc = DoIt(MSG.OPENDSM, ref hwnd)) == STS.SUCCESS) - { - Session._hwnd = hwnd; - - // get default source - if (Session.DGControl.Identity.GetDefault(out TW_IDENTITY_LEGACY ds) == STS.SUCCESS) - { - Session.DefaultSource = ds; - } - - // determine memory mgmt routines used - if (((DG)Session.AppIdentity.SupportedGroups & DG.DSM2) == DG.DSM2) - { - if (Session.DGControl.EntryPoint.Get(out TW_ENTRYPOINT_DELEGATES entry) == STS.SUCCESS) - { - Session._entryPoint = entry; - } - } - Session.State = STATE.S3; - } - return rc; - } + public STS OpenDSM(ref TW_IDENTITY_LEGACY app, IntPtr hwnd) + => DoIt(ref app, MSG.OPENDSM, hwnd); /// /// Closes the DSM. /// + /// /// Required if on Windows. /// - public STS CloseDSM(ref IntPtr hwnd) - { - STS rc; - if ((rc = DoIt(MSG.CLOSEDSM, ref hwnd)) == STS.SUCCESS) - { - Session.State = STATE.S2; - Session._entryPoint = default; - Session.DefaultSource = default; - Session._hwnd = IntPtr.Zero; - } - return rc; - } + public STS CloseDSM(ref TW_IDENTITY_LEGACY app, IntPtr hwnd) + => DoIt(ref app, MSG.CLOSEDSM, hwnd); - - STS DoIt(MSG msg, ref IntPtr hwnd) + static STS DoIt(ref TW_IDENTITY_LEGACY app, MSG msg, IntPtr hwnd) { var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { rc = (STS)WinLegacyDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd); @@ -77,7 +40,6 @@ namespace NTwain.Triplets.ControlDATs { rc = (STS)WinNewDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd); } - if (rc == STS.SUCCESS) Session.AppIdentity = app; } //else if (TwainPlatform.IsLinux) //{ @@ -87,18 +49,14 @@ namespace NTwain.Triplets.ControlDATs //} else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; + TW_IDENTITY_MACOSX app2 = app; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd); - } - if (rc == STS.SUCCESS) - { - Session.AppIdentity = app; + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd); } } return rc; diff --git a/src/NTwain/Triplets/ControlDATs/Status.cs b/src/NTwain/Triplets/ControlDATs/Status.cs index 3cf047b..44f464e 100644 --- a/src/NTwain/Triplets/ControlDATs/Status.cs +++ b/src/NTwain/Triplets/ControlDATs/Status.cs @@ -7,24 +7,19 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class Status : TripletBase + public class Status { - public Status(TwainSession session) : base(session) - { - } - /// /// Gets the current status for the DSM. /// /// /// - public STS GetForDSM(out TW_STATUS status) + public STS GetForDSM(ref TW_IDENTITY_LEGACY app, out TW_STATUS status) { status = default; var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { rc = (STS)WinLegacyDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); @@ -36,14 +31,14 @@ namespace NTwain.Triplets.ControlDATs } else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; + TW_IDENTITY_MACOSX app2 = app; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, IntPtr.Zero, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, IntPtr.Zero, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } } return rc; @@ -54,14 +49,12 @@ namespace NTwain.Triplets.ControlDATs /// /// /// - public STS GetForDS(out TW_STATUS status) + public STS GetForDS(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, out TW_STATUS status) { status = default; - var ds = Session.CurrentSource; var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { rc = (STS)WinLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); @@ -73,15 +66,15 @@ namespace NTwain.Triplets.ControlDATs } else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; + TW_IDENTITY_MACOSX app2 = app; TW_IDENTITY_MACOSX osxds = ds; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, ref osxds, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, ref osxds, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, ref osxds, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, ref osxds, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } } return rc; diff --git a/src/NTwain/Triplets/ControlDATs/StatusUtf8.cs b/src/NTwain/Triplets/ControlDATs/StatusUtf8.cs index d4f5324..361ebd5 100644 --- a/src/NTwain/Triplets/ControlDATs/StatusUtf8.cs +++ b/src/NTwain/Triplets/ControlDATs/StatusUtf8.cs @@ -7,13 +7,8 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class StatusUtf8 : TripletBase + public class StatusUtf8 { - public StatusUtf8(TwainSession session) : base(session) - { - - } - /// /// Gets the extended text info for a previously received . /// If this is called you should try to extract the string value from it once @@ -23,13 +18,12 @@ namespace NTwain.Triplets.ControlDATs /// /// /// - public STS Get(TW_STATUS status, out TW_STATUSUTF8 extendedStatus) + public STS Get(ref TW_IDENTITY_LEGACY app, TW_STATUS status, out TW_STATUSUTF8 extendedStatus) { extendedStatus = new TW_STATUSUTF8 { Status = status }; var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { rc = (STS)WinLegacyDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.STATUSUTF8, MSG.GET, ref extendedStatus); @@ -41,15 +35,14 @@ namespace NTwain.Triplets.ControlDATs } else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; - TW_IDENTITY_MACOSX ds = Session.CurrentSource; + TW_IDENTITY_MACOSX app2 = app; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.STATUSUTF8, MSG.GET, ref extendedStatus); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, IntPtr.Zero, DG.CONTROL, DAT.STATUSUTF8, MSG.GET, ref extendedStatus); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, IntPtr.Zero, DG.CONTROL, DAT.STATUSUTF8, MSG.GET, ref extendedStatus); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, IntPtr.Zero, DG.CONTROL, DAT.STATUSUTF8, MSG.GET, ref extendedStatus); } } return rc; diff --git a/src/NTwain/Triplets/ControlDATs/UserInterface.cs b/src/NTwain/Triplets/ControlDATs/UserInterface.cs index ab8ff3b..d71ba2d 100644 --- a/src/NTwain/Triplets/ControlDATs/UserInterface.cs +++ b/src/NTwain/Triplets/ControlDATs/UserInterface.cs @@ -6,64 +6,37 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class UserInterface : TripletBase + public class UserInterface { - public UserInterface(TwainSession session) : base(session) - { - } - /// /// Disables source to bring state down to . /// /// /// - public STS DisableDS(ref TW_USERINTERFACE data) - { - var rc = DoIt(MSG.DISABLEDS, ref data); - if (rc == STS.SUCCESS) - { - Session.State = STATE.S4; - } - return rc; - } + public STS DisableDS(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, ref TW_USERINTERFACE data) + => DoIt(ref app, ref ds, MSG.DISABLEDS, ref data); /// /// Enables source to bring state up to . /// /// /// - public STS EnableDS(ref TW_USERINTERFACE data) - { - var rc = DoIt(MSG.ENABLEDS, ref data); - if (rc == STS.SUCCESS || (data.ShowUI == 0 && rc == STS.CHECKSTATUS)) - { - Session.State = STATE.S5; - } - return rc; - } + public STS EnableDS(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, ref TW_USERINTERFACE data) + => DoIt(ref app, ref ds, MSG.ENABLEDS, ref data); /// /// Enables source to bring state up to . /// /// /// - public STS EnableDSUIOnly(ref TW_USERINTERFACE data) - { - var rc = DoIt(MSG.ENABLEDSUIONLY, ref data); - if (rc == STS.SUCCESS) - { - Session.State = STATE.S5; - } - return rc; - } + public STS EnableDSUIOnly(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, ref TW_USERINTERFACE data) + => DoIt(ref app, ref ds, MSG.ENABLEDSUIONLY, ref data); - STS DoIt(MSG msg, ref TW_USERINTERFACE data) + static STS DoIt(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, MSG msg, ref TW_USERINTERFACE data) { var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; - var ds = Session.CurrentSource; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { rc = (STS)WinLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.USERINTERFACE, msg, ref data); @@ -75,15 +48,15 @@ namespace NTwain.Triplets.ControlDATs } else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; - TW_IDENTITY_MACOSX ds = Session.CurrentSource; + TW_IDENTITY_MACOSX app2 = app; + TW_IDENTITY_MACOSX ds2 = ds; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.USERINTERFACE, msg, ref data); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.USERINTERFACE, msg, ref data); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.USERINTERFACE, msg, ref data); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.USERINTERFACE, msg, ref data); } } return rc; diff --git a/src/NTwain/Triplets/ControlDATs/XferGroup.cs b/src/NTwain/Triplets/ControlDATs/XferGroup.cs index e44a092..fcb3c06 100644 --- a/src/NTwain/Triplets/ControlDATs/XferGroup.cs +++ b/src/NTwain/Triplets/ControlDATs/XferGroup.cs @@ -6,21 +6,17 @@ namespace NTwain.Triplets.ControlDATs /// /// Contains calls used with and . /// - public class XferGroup : TripletBase + public class XferGroup { - public XferGroup(TwainSession session) : base(session) - { - } - /// /// Gets the transfer group used. /// /// /// - public STS Get(out DG data) + public STS Get(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, out DG data) { data = default; - return DoIt(MSG.GET, ref data); + return DoIt(ref app, ref ds, MSG.GET, ref data); } /// @@ -28,18 +24,16 @@ namespace NTwain.Triplets.ControlDATs /// /// /// - public STS Set(DG data) + public STS Set(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, DG data) { - return DoIt(MSG.SET, ref data); + return DoIt(ref app, ref ds, MSG.SET, ref data); } - STS DoIt(MSG msg, ref DG data) + static STS DoIt(ref TW_IDENTITY_LEGACY app, ref TW_IDENTITY_LEGACY ds, MSG msg, ref DG data) { var rc = STS.FAILURE; if (TwainPlatform.IsWindows) { - var app = Session.AppIdentity; - var ds = Session.CurrentSource; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { rc = (STS)WinLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.XFERGROUP, msg, ref data); @@ -51,15 +45,15 @@ namespace NTwain.Triplets.ControlDATs } else if (TwainPlatform.IsMacOSX) { - TW_IDENTITY_MACOSX app = Session.AppIdentity; - TW_IDENTITY_MACOSX ds = Session.CurrentSource; + TW_IDENTITY_MACOSX app2 = app; + TW_IDENTITY_MACOSX ds2 = ds; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.XFERGROUP, msg, ref data); + rc = (STS)OSXLegacyDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.XFERGROUP, msg, ref data); } else { - rc = (STS)OSXNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.XFERGROUP, msg, ref data); + rc = (STS)OSXNewDSM.DSM_Entry(ref app2, ref ds2, DG.CONTROL, DAT.XFERGROUP, msg, ref data); } } return rc; diff --git a/src/NTwain/Triplets/DGAudio.cs b/src/NTwain/Triplets/DGAudio.cs index ffa2a39..97b99cb 100644 --- a/src/NTwain/Triplets/DGAudio.cs +++ b/src/NTwain/Triplets/DGAudio.cs @@ -5,10 +5,7 @@ namespace NTwain.Triplets /// /// Contains calls used with . /// - public class DGAudio : TripletBase + public class DGAudio { - public DGAudio(TwainSession session) : base(session) - { - } } } \ No newline at end of file diff --git a/src/NTwain/Triplets/DGControl.cs b/src/NTwain/Triplets/DGControl.cs index 62d6e31..6142b2e 100644 --- a/src/NTwain/Triplets/DGControl.cs +++ b/src/NTwain/Triplets/DGControl.cs @@ -5,44 +5,40 @@ namespace NTwain.Triplets /// /// Contains calls used with . /// - public class DGControl : TripletBase + public class DGControl { - public DGControl(TwainSession session) : base(session) - { - } - private Parent? _parent; - public Parent Parent => _parent ??= new Parent(Session); + public Parent Parent => _parent ??= new Parent(); private EntryPoint? _entryPoint; - public EntryPoint EntryPoint => _entryPoint ??= new EntryPoint(Session); + public EntryPoint EntryPoint => _entryPoint ??= new EntryPoint(); private Identity? _identity; - public Identity Identity => _identity ??= new Identity(Session); + public Identity Identity => _identity ??= new Identity(); private Status? _status; - public Status Status => _status ??= new Status(Session); + public Status Status => _status ??= new Status(); private StatusUtf8? _statusUtf8; - public StatusUtf8 StatusUtf8 => _statusUtf8 ??= new StatusUtf8(Session); + public StatusUtf8 StatusUtf8 => _statusUtf8 ??= new StatusUtf8(); private CustomDsData? _customDsData; - public CustomDsData CustomDsData => _customDsData ??= new CustomDsData(Session); + public CustomDsData CustomDsData => _customDsData ??= new CustomDsData(); private DeviceEvent? _deviceEvent; - public DeviceEvent DeviceEvent => _deviceEvent ??= new DeviceEvent(Session); + public DeviceEvent DeviceEvent => _deviceEvent ??= new DeviceEvent(); private Callback? _callback; - public Callback Callback => _callback ??= new Callback(Session); + public Callback Callback => _callback ??= new Callback(); private Callback2? _callback2; - public Callback2 Callback2 => _callback2 ??= new Callback2(Session); + public Callback2 Callback2 => _callback2 ??= new Callback2(); private XferGroup? _xferGroup; - public XferGroup XferGroup => _xferGroup ??= new XferGroup(Session); + public XferGroup XferGroup => _xferGroup ??= new XferGroup(); private UserInterface? _userInterface; - public UserInterface UserInterface => _userInterface ??= new UserInterface(Session); + public UserInterface UserInterface => _userInterface ??= new UserInterface(); } } \ No newline at end of file diff --git a/src/NTwain/Triplets/DGImage.cs b/src/NTwain/Triplets/DGImage.cs index 4685194..a8d7abd 100644 --- a/src/NTwain/Triplets/DGImage.cs +++ b/src/NTwain/Triplets/DGImage.cs @@ -5,10 +5,7 @@ namespace NTwain.Triplets /// /// Contains calls used with . /// - public class DGImage : TripletBase + public class DGImage { - public DGImage(TwainSession session) : base(session) - { - } } } \ No newline at end of file diff --git a/src/NTwain/Triplets/TripletBase.cs b/src/NTwain/Triplets/TripletBase.cs deleted file mode 100644 index 926879f..0000000 --- a/src/NTwain/Triplets/TripletBase.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; - -namespace NTwain.Triplets -{ - /// - /// Base class for grouping triplet operations messages. - /// - public abstract class TripletBase - { - /// - /// Initializes a new instance of the class. - /// - /// The session. - /// - public TripletBase(TwainSession session) - { - Session = session ?? throw new ArgumentNullException(nameof(session)); - } - - /// - /// Gets the twain session. - /// - public TwainSession Session { get; } - } -} diff --git a/src/NTwain/TwainSession.Callbacks.cs b/src/NTwain/TwainSession.Callbacks.cs index d901fca..84b85fd 100644 --- a/src/NTwain/TwainSession.Callbacks.cs +++ b/src/NTwain/TwainSession.Callbacks.cs @@ -36,13 +36,13 @@ namespace NTwain if (_appIdentity.ProtocolMajor > 2 || (_appIdentity.ProtocolMajor >= 2 && _appIdentity.ProtocolMinor >= 2)) { var cb2 = new TW_CALLBACK2 { CallBackProc = cbPtr }; - rc = DGControl.Callback2.RegisterCallback(ref cb2); + rc = DGControl.Callback2.RegisterCallback(ref _appIdentity, ref _currentDS, ref cb2); } if (rc != STS.SUCCESS) { // always try old callback var cb = new TW_CALLBACK { CallBackProc = cbPtr }; - DGControl.Callback.RegisterCallback(ref cb); + DGControl.Callback.RegisterCallback(ref _appIdentity, ref _currentDS, ref cb); } } diff --git a/src/NTwain/TwainSession.PropEvents.cs b/src/NTwain/TwainSession.PropEvents.cs index f83b3a9..e1a2e6f 100644 --- a/src/NTwain/TwainSession.PropEvents.cs +++ b/src/NTwain/TwainSession.PropEvents.cs @@ -2,7 +2,6 @@ using System; using System.Runtime.InteropServices; using TWAINWorkingGroup; -using static System.Collections.Specialized.BitVector32; namespace NTwain { @@ -38,16 +37,11 @@ namespace NTwain TW_IDENTITY_LEGACY _currentDS; /// - /// Gets the default data source. + /// Gets/sets the default data source. /// public TW_IDENTITY_LEGACY DefaultSource { get => _defaultDS; - internal set - { - _defaultDS = value; - DefaultSourceChanged?.Invoke(this, value); - } } TW_IDENTITY_LEGACY _defaultDS; @@ -78,7 +72,7 @@ namespace NTwain { get { - var sts = DGControl.CustomDsData.Get(out TW_CUSTOMDSDATA data); + var sts = DGControl.CustomDsData.Get(ref _appIdentity, ref _currentDS, out TW_CUSTOMDSDATA data); if (sts == STS.SUCCESS) { if (data.hData != IntPtr.Zero && data.InfoLength > 0) @@ -111,7 +105,7 @@ namespace NTwain var lockedPtr = Lock(data.hData); Marshal.Copy(value, 0, lockedPtr, value.Length); Unlock(data.hData); - var sts = DGControl.CustomDsData.Set(ref data); + var sts = DGControl.CustomDsData.Set(ref _appIdentity, ref _currentDS, ref data); } finally { diff --git a/src/NTwain/TwainSession.Sources.cs b/src/NTwain/TwainSession.Sources.cs index 6335235..2c28235 100644 --- a/src/NTwain/TwainSession.Sources.cs +++ b/src/NTwain/TwainSession.Sources.cs @@ -14,14 +14,57 @@ namespace NTwain /// public IEnumerable GetSources() { - var rc = DGControl.Identity.GetFirst(out TW_IDENTITY_LEGACY ds); + var rc = DGControl.Identity.GetFirst(ref _appIdentity, out TW_IDENTITY_LEGACY ds); while (rc == STS.SUCCESS) { yield return ds; - rc = DGControl.Identity.GetNext(out ds); + rc = DGControl.Identity.GetNext(ref _appIdentity, out ds); } } + /// + /// Shows the TWAIN source selection UI for setting the default source. + /// + public void ShowUserSelect() + { + if (DGControl.Identity.UserSelect(ref _appIdentity, out TW_IDENTITY_LEGACY ds) == STS.SUCCESS) + { + _defaultDS = ds; + DefaultSourceChanged?.Invoke(this, ds); + } + } + + public void OpenSource(TW_IDENTITY_LEGACY source) + { + if (DGControl.Identity.OpenDS(ref _appIdentity, ref source) == STS.SUCCESS) + { + RegisterCallback(); + CurrentSource = source; + State = STATE.S4; + } + } + + public void CloseSource() + { + if (DGControl.Identity.CloseDS(ref _appIdentity, ref _currentDS) == STS.SUCCESS) + { + State = STATE.S3; + CurrentSource = default; + } + } + + public STS SetDefaultSource(TW_IDENTITY_LEGACY source) + { + var rc = DGControl.Identity.Set(ref _appIdentity, ref source); + if (rc == STS.SUCCESS) + { + _defaultDS = source; + DefaultSourceChanged?.Invoke(this, source); + } + return rc; + } + + /// /// Enables the currently open source. /// @@ -36,11 +79,14 @@ namespace NTwain ShowUI = (ushort)((showUI || uiOnly) ? 1 : 0), hParent = _hwnd, }; - var rc = uiOnly ? DGControl.UserInterface.EnableDSUIOnly(ref ui) : DGControl.UserInterface.EnableDS(ref ui); + var rc = uiOnly ? + DGControl.UserInterface.EnableDSUIOnly(ref _appIdentity, ref _currentDS, ref ui) : + DGControl.UserInterface.EnableDS(ref _appIdentity, ref _currentDS, ref ui); if (rc == STS.SUCCESS || (!uiOnly && !showUI && rc == STS.CHECKSTATUS)) { // keep it around for disable use _userInterface = ui; + State = STATE.S5; }; return rc; } @@ -51,7 +97,12 @@ namespace NTwain /// public STS DisableSource() { - return DGControl.UserInterface.DisableDS(ref _userInterface); + var rc = DGControl.UserInterface.DisableDS(ref _appIdentity, ref _currentDS, ref _userInterface); + if (rc == STS.SUCCESS) + { + State = STATE.S4; + } + return rc; } } } diff --git a/src/NTwain/TwainSession.cs b/src/NTwain/TwainSession.cs index 0911bf3..1d15f17 100644 --- a/src/NTwain/TwainSession.cs +++ b/src/NTwain/TwainSession.cs @@ -1,6 +1,5 @@ using NTwain.Triplets; using System; -using System.Collections; using System.Diagnostics; using System.Text; using TWAINWorkingGroup; @@ -78,9 +77,9 @@ namespace NTwain } }; - DGControl = new DGControl(this); - DGImage = new DGImage(this); - DGAudio = new DGAudio(this); + DGControl = new DGControl(); + DGImage = new DGImage(); + DGAudio = new DGAudio(); _legacyCallbackDelegate = LegacyCallbackHandler; _osxCallbackDelegate = OSXCallbackHandler; @@ -89,6 +88,88 @@ namespace NTwain internal IntPtr _hwnd; internal TW_USERINTERFACE _userInterface; + /// + /// Loads and opens the TWAIN data source manager. + /// + /// Required if on Windows. + /// + public STS OpenDSM(IntPtr hwnd) + { + var rc = DGControl.Parent.OpenDSM(ref _appIdentity, hwnd); + if (rc == STS.SUCCESS) + { + _hwnd = hwnd; + State = STATE.S3; + // get default source + if (DGControl.Identity.GetDefault(ref _appIdentity, out TW_IDENTITY_LEGACY ds) == STS.SUCCESS) + { + _defaultDS = ds; + DefaultSourceChanged?.Invoke(this, _defaultDS); + } + + // determine memory mgmt routines used + if (((DG)AppIdentity.SupportedGroups & DG.DSM2) == DG.DSM2) + { + DGControl.EntryPoint.Get(ref _appIdentity, out _entryPoint); + } + } + return rc; + } + + + /// + /// Closes the TWAIN data source manager. + /// + /// + public STS CloseDSM() + { + var rc = DGControl.Parent.CloseDSM(ref _appIdentity, _hwnd); + if (rc == STS.SUCCESS) + { + State = STATE.S2; + _entryPoint = default; + _defaultDS = default; + DefaultSourceChanged?.Invoke(this, _defaultDS); + _hwnd = IntPtr.Zero; + } + return rc; + } + + /// + /// Gets the last status code if an operation did not return success. + /// This can only be done once after an error. + /// + /// true to get status for dsm operation error, false to get status for ds operation error, + /// + public TW_STATUS GetLastStatus(bool forDsmOnly) + { + if (forDsmOnly) + { + DGControl.Status.GetForDSM(ref _appIdentity, out TW_STATUS status); + return status; + } + else + { + DGControl.Status.GetForDS(ref _appIdentity, ref _currentDS, out TW_STATUS status); + return status; + } + } + + /// + /// Tries to get string representation of a previously gotten status + /// from if possible. + /// + /// + /// + public string? GetStatusText(TW_STATUS status) + { + if (DGControl.StatusUtf8.Get(ref _appIdentity, status, out TW_STATUSUTF8 extendedStatus) == STS.SUCCESS) + { + return extendedStatus.ReadAndFree(this); + } + return null; + } + /// /// Tries to bring the TWAIN session down to some state. /// @@ -104,14 +185,13 @@ namespace NTwain switch (State) { case STATE.S5: - DGControl.UserInterface.DisableDS(ref _userInterface); + DisableSource(); break; case STATE.S4: - DGControl.Identity.CloseDS(); + CloseSource(); break; case STATE.S3: - // shouldn't care about handle when closing really - DGControl.Parent.CloseDSM(ref _hwnd); + CloseDSM(); break; } }