Add a mac-specific sdk/lib target without wia and twain

This will be needed to implement the ImageCaptureCore-based driver (as that requires a mac target)
This commit is contained in:
Ben Olden-Cooligan 2022-09-24 17:52:55 -07:00
parent ad7f327a0d
commit bd7d19b876
20 changed files with 84 additions and 46 deletions

View File

@ -1,5 +1,4 @@
using NAPS2.Scan;
using NAPS2.Wia;
namespace NAPS2.WinForms;
@ -9,7 +8,7 @@ public partial class FAdvancedScanSettings : FormBase
{
InitializeComponent();
AddEnumItems<WiaVersion>(cmbWiaVersion, FormatWiaVersion);
AddEnumItems<WiaApiVersion>(cmbWiaVersion, FormatWiaVersion);
AddEnumItems<TwainImpl>(cmbTwainImpl);
if (!Environment.Is64BitProcess)
{
@ -18,13 +17,13 @@ public partial class FAdvancedScanSettings : FormBase
}
// TODO: Standardize on this and put all formatters in some common location
private string FormatWiaVersion(WiaVersion value)
private string FormatWiaVersion(WiaApiVersion value)
{
return value switch
{
WiaVersion.Default => SettingsResources.WiaVersion_Default,
WiaVersion.Wia10 => SettingsResources.WiaVersion_Wia10,
WiaVersion.Wia20 => SettingsResources.WiaVersion_Wia20,
WiaApiVersion.Default => SettingsResources.WiaVersion_Default,
WiaApiVersion.Wia10 => SettingsResources.WiaVersion_Wia10,
WiaApiVersion.Wia20 => SettingsResources.WiaVersion_Wia20,
_ => value.ToString()
};
}
@ -86,7 +85,7 @@ public partial class FAdvancedScanSettings : FormBase
ScanProfile.WiaOffsetWidth = cbWiaOffsetWidth.Checked;
if (cmbWiaVersion.SelectedIndex != -1)
{
ScanProfile.WiaVersion = (WiaVersion)cmbWiaVersion.SelectedIndex;
ScanProfile.WiaVersion = (WiaApiVersion) cmbWiaVersion.SelectedIndex;
}
ScanProfile.ForcePageSize = cbForcePageSize.Checked;
ScanProfile.ForcePageSizeCrop = cbForcePageSizeCrop.Checked;

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6;net462</TargetFrameworks>
<TargetFrameworks>net6;net6-macos10.15;net462</TargetFrameworks>
<Nullable>enable</Nullable>
<GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources>
<RootNamespace>NAPS2</RootNamespace>
@ -14,6 +14,10 @@
<Import Project="..\NAPS2.Setup\CommonTargets.targets" />
<Import Project="..\NAPS2.Setup\LibUsers.targets" />
<PropertyGroup Condition="'$(TargetFramework)' == 'net6-macos10.15'">
<DefineConstants>MAC</DefineConstants>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NAPS2.Sdk\NAPS2.Sdk.csproj" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />

View File

@ -3,7 +3,9 @@ using NAPS2.ImportExport;
using NAPS2.Ocr;
using NAPS2.Scan.Exceptions;
using NAPS2.Scan.Internal;
#if !MAC
using NAPS2.Wia;
#endif
namespace NAPS2.Scan;
@ -128,7 +130,7 @@ internal class ScanPerformer : IScanPerformer
private void ShowOperation(ScanOperation op, ScanOptions scanOptions, ScanParams scanParams)
{
bool isWia10 = scanOptions.Driver == Driver.Wia && scanOptions.WiaOptions.WiaVersion == WiaVersion.Wia10;
bool isWia10 = scanOptions.Driver == Driver.Wia && scanOptions.WiaOptions.WiaApiVersion == WiaApiVersion.Wia10;
if (scanParams.NoUI || scanOptions.UseNativeUI && !isWia10)
{
return;
@ -170,7 +172,7 @@ internal class ScanPerformer : IScanPerformer
: Driver.Default,
WiaOptions =
{
WiaVersion = scanProfile.WiaVersion,
WiaApiVersion = scanProfile.WiaVersion,
OffsetWidth = scanProfile.WiaOffsetWidth
},
TwainOptions =
@ -269,11 +271,12 @@ internal class ScanPerformer : IScanPerformer
private async Task<ScanDevice?> PromptForDevice(ScanOptions options)
{
#if !MAC
// TODO: Not sure how best to handle this for console
if (options.Driver == Driver.Wia)
{
// WIA has a nice built-in device selection dialog, so use it
using var deviceManager = new WiaDeviceManager(options.WiaOptions.WiaVersion);
using var deviceManager = new WiaDeviceManager((WiaVersion) options.WiaOptions.WiaApiVersion);
try
{
var wiaDevice = deviceManager.PromptForDevice(options.DialogParent);
@ -292,6 +295,7 @@ internal class ScanPerformer : IScanPerformer
throw new ScanDriverUnknownException(ex);
}
}
#endif
// Other drivers do not, so use a generic dialog
var deviceList = await new ScanController(_scanningContext).GetDeviceList(options);

View File

@ -3,7 +3,6 @@ using System.Globalization;
using System.Reflection;
using System.Xml.Serialization;
using NAPS2.ImportExport;
using NAPS2.Wia;
namespace NAPS2.Scan;
@ -116,7 +115,7 @@ public class ScanProfile
public double WiaDelayBetweenScansSeconds { get; set; }
public WiaVersion WiaVersion { get; set; }
public WiaApiVersion WiaVersion { get; set; }
public bool FlipDuplexedPages { get; set; }

View File

@ -1,6 +1,8 @@
using NAPS2.EtoForms.Ui;
using NAPS2.Scan;
#if !MAC
using NAPS2.Wia;
#endif
namespace NAPS2.WinForms;
@ -63,6 +65,7 @@ public class DesktopScanController : IDesktopScanController
// No profile for the device we're scanning with, so prompt to create one
var editSettingsForm = _formFactory.Create<EditProfileForm>();
editSettingsForm.ScanProfile = _config.DefaultProfileSettings();
#if !MAC
try
{
// Populate the device field automatically (because we can do that!)
@ -73,6 +76,7 @@ public class DesktopScanController : IDesktopScanController
catch (WiaException)
{
}
#endif
editSettingsForm.ShowModal();
if (!editSettingsForm.Result)
{

View File

@ -19,9 +19,6 @@
<Reference Include="PdfSharpCore">
<HintPath>..\NAPS2.Setup\lib\PdfSharpCore.dll</HintPath>
</Reference>
<Reference Include="NTwain" Condition="'$(TargetFramework)' == 'net6'">
<HintPath>..\NAPS2.Setup\lib\NTwain.dll</HintPath>
</Reference>
<ProjectReference Include="..\NAPS2.Sdk\NAPS2.Sdk.csproj" />
<ProjectReference Include="..\NAPS2.Images.Gdi\NAPS2.Images.Gdi.csproj" Condition="$([MSBuild]::IsOSPlatform('Windows'))" />

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6;net462</TargetFrameworks>
<TargetFrameworks>net6;net6-macos10.15;net462</TargetFrameworks>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- TODO: Move ClientCreds etc out of NAPS2.Sdk -->
@ -29,6 +29,9 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Standalone|AnyCPU'">
<DefineConstants>STANDALONE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net6-macos10.15'">
<DefineConstants>MAC</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SharpZipLib" Version="1.3.3" />
@ -36,17 +39,14 @@
<Reference Include="PdfSharpCore">
<HintPath>..\NAPS2.Setup\lib\PdfSharpCore.dll</HintPath>
</Reference>
<Reference Include="NTwain" Condition="'$(TargetFramework)' == 'net6'">
<HintPath>..\NAPS2.Setup\lib\NTwain.dll</HintPath>
</Reference>
<PackageReference Include="Grpc.Tools" Version="2.47.0" PrivateAssets="all" />
<PackageReference Include="GrpcDotNetNamedPipes" Version="1.4.4" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="NAPS2.Wia" Version="1.0.1" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" Condition="'$(TargetFramework)' != 'net6-macos10.15'" />
<PackageReference Include="NAPS2.Wia" Version="1.0.1" Condition="'$(TargetFramework)' != 'net6-macos10.15'" />
<!-- Hide compatibility warnings for NTwain as it will only be actually used on Windows. -->
<!-- TODO: Does this actually work if we build a net6 framework-dependent app and try to use Twain? -->
<PackageReference Include="NTwain" Version="3.7.1" NoWarn="NU1701" Condition="'$(TargetFramework)' == 'net462'" />
<PackageReference Include="NTwain" Version="3.7.1" NoWarn="NU1701" Condition="'$(TargetFramework)' != 'net6-macos10.15'" />
<PackageReference Include="Nullable" Version="1.2.0" Condition="'$(TargetFramework)' == 'net462'" />
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
<PackageReference Include="System.Resources.Extensions" Version="6.0.0" />

View File

@ -8,8 +8,10 @@ using NAPS2.ImportExport.Pdf;
using NAPS2.Scan;
using NAPS2.Scan.Internal;
using NAPS2.Scan.Internal.Twain;
#if !MAC
using NAPS2.Scan.Internal.Wia;
using NAPS2.Wia;
#endif
using NAPS2.Serialization;
namespace NAPS2.Remoting.Worker;
@ -70,12 +72,15 @@ public class WorkerServiceImpl : WorkerService.WorkerServiceBase
public override Task<Wia10NativeUiResponse> Wia10NativeUi(Wia10NativeUiRequest request, ServerCallContext context)
{
#if MAC
throw new NotSupportedException();
#else
using var callRef = StartCall();
try
{
try
{
using var deviceManager = new WiaDeviceManager(WiaVersion.Wia10);
using var deviceManager = new WiaDeviceManager(Wia.WiaVersion.Wia10);
using var device = deviceManager.FindDevice(request.DeviceId);
var item = device.PromptToConfigure((IntPtr) request.Hwnd);
var response = new Wia10NativeUiResponse();
@ -100,6 +105,7 @@ public class WorkerServiceImpl : WorkerService.WorkerServiceBase
{
return Task.FromResult(new Wia10NativeUiResponse { Error = RemotingHelper.ToError(e) });
}
#endif
}
public override async Task<GetDeviceListResponse> GetDeviceList(GetDeviceListRequest request,

View File

@ -17,16 +17,18 @@ internal class ScanDriverFactory : IScanDriverFactory
{
switch (options.Driver)
{
#if !MAC
case Driver.Wia:
return new WiaScanDriver(_scanningContext);
case Driver.Sane:
return new SaneScanDriver(_scanningContext);
case Driver.Twain:
return options.TwainOptions.Adapter == TwainAdapter.Legacy
? new LegacyTwainScanDriver()
: new TwainScanDriver(_scanningContext);
#endif
case Driver.Sane:
return new SaneScanDriver(_scanningContext);
default:
throw new InvalidOperationException("Unknown driver. Should never happen.");
throw new InvalidOperationException($"Unsupported driver: {options.Driver}");
};
}
}

View File

@ -1,3 +1,4 @@
#if !MAC
using System.Threading;
using NAPS2.Scan.Exceptions;
using NTwain;
@ -77,4 +78,5 @@ public class LocalTwainSessionController : ITwainSessionController
var runner = new TwainSessionScanRunner(dsm, options, cancelToken, twainEvents);
await runner.Run();
}
}
}
#endif

View File

@ -1,3 +1,4 @@
#if !MAC
using NAPS2.Remoting.Worker;
namespace NAPS2.Scan.Internal.Twain;
@ -71,4 +72,5 @@ internal class TwainImageProcessor : ITwainEvents, IDisposable
{
_currentMemoryImage?.Dispose();
}
}
}
#endif

View File

@ -1,3 +1,4 @@
#if !MAC
using NAPS2.Images.Bitwise;
using NAPS2.Remoting.Worker;
using NTwain.Data;
@ -39,4 +40,5 @@ public static class TwainMemoryBufferReader
}
return true;
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using System.Reflection;
#if !MAC
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using NAPS2.Platform.Windows;
@ -58,4 +59,5 @@ internal class TwainScanDriver : IScanDriver
}
return new LocalTwainSessionController();
}
}
}
#endif

View File

@ -1,3 +1,4 @@
#if !MAC
using System.Threading;
using Google.Protobuf;
using NAPS2.Remoting.Worker;
@ -372,4 +373,5 @@ internal class TwainSessionScanRunner
source.Capabilities.ICapXResolution.SetValue(_options.Dpi);
source.Capabilities.ICapYResolution.SetValue(_options.Dpi);
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using NAPS2.Wia;
#if !MAC
using NAPS2.Wia;
namespace NAPS2.Scan.Internal.Wia;
@ -39,4 +40,5 @@ public static class WiaExtensions
Log.ErrorException("Error setting property", e);
}
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using System.Threading;
#if !MAC
using System.Threading;
using NAPS2.Scan.Exceptions;
using NAPS2.Wia;
@ -17,7 +18,7 @@ internal class WiaScanDriver : IScanDriver
{
return Task.Run(() =>
{
using var deviceManager = new WiaDeviceManager(options.WiaOptions.WiaVersion);
using var deviceManager = new WiaDeviceManager((WiaVersion) options.WiaOptions.WiaApiVersion);
return deviceManager.GetDeviceInfos().Select(deviceInfo =>
{
using (deviceInfo)
@ -36,13 +37,14 @@ internal class WiaScanDriver : IScanDriver
var context = new WiaScanContext(_scanningContext, options, cancelToken, scanEvents, callback);
try
{
var version = (WiaVersion) options.WiaOptions.WiaApiVersion;
try
{
context.Scan(options.WiaOptions.WiaVersion);
context.Scan(version);
}
catch (WiaException e) when
(e.ErrorCode == Hresult.E_INVALIDARG &&
options.WiaOptions.WiaVersion == WiaVersion.Default &&
version == WiaVersion.Default &&
NativeWiaObject.DefaultWiaVersion == WiaVersion.Wia20
&& !options.UseNativeUI)
{
@ -362,4 +364,5 @@ internal class WiaScanDriver : IScanDriver
}
}
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using NAPS2.Scan.Exceptions;
#if !MAC
using NAPS2.Scan.Exceptions;
using NAPS2.Wia;
namespace NAPS2.Scan.Internal.Wia;
@ -37,4 +38,5 @@ public class WiaScanErrors
}
throw new ScanDriverUnknownException(e);
}
}
}
#endif

View File

@ -1,10 +1,8 @@
using NAPS2.Wia;
namespace NAPS2.Scan;
namespace NAPS2.Scan;
public class WiaOptions
{
public WiaVersion WiaVersion { get; set; }
public WiaApiVersion WiaApiVersion { get; set; }
public bool OffsetWidth { get; set; }
}

View File

@ -0,0 +1,8 @@
namespace NAPS2.Scan;
public enum WiaApiVersion
{
Default,
Wia10,
Wia20
}

Binary file not shown.