Skip to content

Commit

Permalink
Fix mac build
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Sep 5, 2024
1 parent 497b6bd commit 6456f1c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAPS2.Sdk/Platform/Windows/Win32MessagePump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace NAPS2.Platform.Windows;

[System.Runtime.Versioning.SupportedOSPlatform("windows")]
internal class Win32MessagePump : IInvoker, IDisposable
{
private const string WND_CLASS_NAME = "MPWndClass";
Expand Down
5 changes: 4 additions & 1 deletion NAPS2.Sdk/Platform/Windows/Win32TwainHandleManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#if !MAC
using NAPS2.Scan.Internal.Twain;
using NTwain;

namespace NAPS2.Platform.Windows;

[System.Runtime.Versioning.SupportedOSPlatform("windows")]
internal class Win32TwainHandleManager : TwainHandleManager
{
private readonly Win32MessagePump _messagePump;
Expand Down Expand Up @@ -72,4 +74,5 @@ public override void Dispose()
Win32.EnableWindow(_disabledWindow, true);
}
}
}
}
#endif
30 changes: 24 additions & 6 deletions NAPS2.Sdk/Remoting/Worker/WorkerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,32 @@ namespace NAPS2.Remoting.Worker;
/// </summary>
public static class WorkerServer
{
#pragma warning disable CS1998
public static async Task Run(ScanningContext scanningContext, CancellationToken cancellationToken = default)
{
try
{
var messagePump = Win32MessagePump.Create();
messagePump.Logger = scanningContext.Logger;
Invoker.Current = messagePump;
TwainHandleManager.Factory = () => new Win32TwainHandleManager(messagePump);
var tcs = new TaskCompletionSource<bool>();
var run = async () => { await tcs.Task; };
var stop = () => tcs.SetResult(true);

#if !MAC
#if NET6_0_OR_GREATER
if (OperatingSystem.IsWindows())
{
#endif
var messagePump = Win32MessagePump.Create();
messagePump.Logger = scanningContext.Logger;
Invoker.Current = messagePump;
#pragma warning disable CA1416
TwainHandleManager.Factory = () => new Win32TwainHandleManager(messagePump);
run = async () => messagePump.RunMessageLoop();
stop = () => messagePump.Dispose();
#pragma warning restore CA1416
#if NET6_0_OR_GREATER
}
#endif
#endif

var server =
new NamedPipeServer(string.Format(WorkerFactory.PIPE_NAME_FORMAT, Process.GetCurrentProcess().Id));
Expand All @@ -31,14 +49,14 @@ public static async Task Run(ScanningContext scanningContext, CancellationToken
#else
new LocalTwainController(scanningContext));
#endif
serviceImpl.OnStop += (_, _) => messagePump.Dispose();
serviceImpl.OnStop += (_, _) => stop();
WorkerService.BindService(server.ServiceBinder, serviceImpl);
cancellationToken.Register(() => serviceImpl.Stop());
server.Start();
try
{
Console.WriteLine(@"ready");
messagePump.RunMessageLoop();
await run();
}
finally
{
Expand Down
4 changes: 3 additions & 1 deletion NAPS2.Sdk/Scan/Internal/Twain/TwainHandleManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !MAC
using NTwain;

namespace NAPS2.Scan.Internal.Twain;
Expand All @@ -20,4 +21,5 @@ public virtual MessageLoopHook CreateMessageLoopHook(IntPtr dialogParent = defau
public virtual void Dispose()
{
}
}
}
#endif

0 comments on commit 6456f1c

Please sign in to comment.