Skip to content

Commit

Permalink
Fix SANE environment vars
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Oct 23, 2022
1 parent 6dc5426 commit 83dd49c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion NAPS2.App.Mac/NAPS2.App.Mac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<Import Project="..\NAPS2.Setup\SdkUsers.targets" />

<ItemGroup>
<PackageReference Include="NAPS2.Sane.Binaries" Version="1.0.0" />
<PackageReference Include="NAPS2.Sane.Binaries" Version="1.0.4" />
<PackageReference Include="NAPS2.Tesseract.Binaries" Version="1.0.2" />

<ProjectReference Include="..\NAPS2.Lib.Mac\NAPS2.Lib.Mac.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion NAPS2.Sdk.ScannerTests/NAPS2.Sdk.ScannerTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\NAPS2.Sdk.Tests\NAPS2.Sdk.Tests.csproj" />

<PackageReference Include="NAPS2.Sane.Binaries" Version="1.0.0" />
<PackageReference Include="NAPS2.Sane.Binaries" Version="1.0.4" />
<PackageReference Include="NAPS2.Wia" Version="1.0.1" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Sdk/Platform/ISystemCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public interface ISystemCompat
IDisposable? FileReadLock(string path);

IDisposable? FileWriteLock(string path);

void SetEnv(string name, string value);
}
2 changes: 2 additions & 0 deletions NAPS2.Sdk/Platform/LinuxSystemCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class LinuxSystemCompat : ISystemCompat

public string GetLoadError() => LinuxInterop.dlerror();

public void SetEnv(string name, string value) => throw new NotSupportedException();

public IDisposable FileReadLock(string path) => new FileStream(path, FileMode.Open, FileAccess.Read);

public IDisposable FileWriteLock(string path) => new FileStream(path, FileMode.Open, FileAccess.Write);
Expand Down
3 changes: 3 additions & 0 deletions NAPS2.Sdk/Platform/Mac/MacInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ public static class MacInterop

[DllImport("libSystem.dylib")]
public static extern IntPtr dlsym(IntPtr handle, string symbol);

[DllImport("libSystem.dylib")]
public static extern int setenv(string name, string value, int overwrite);
}
6 changes: 4 additions & 2 deletions NAPS2.Sdk/Platform/MacSystemCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ public string[] LibrarySearchPaths

public string PdfiumLibraryName => "libpdfium.dylib";

public string[] SaneLibraryDeps => new[] { "libusb.dylib", "libjpeg.dylib" };
public string[] SaneLibraryDeps => new[] { "libusb-1.0.0.dylib", "libjpeg.62.dylib" };

public string SaneLibraryName => "libsane.dylib";
public string SaneLibraryName => "libsane.1.dylib";

public IntPtr LoadLibrary(string path) => MacInterop.dlopen(path, RTLD_LAZY | RTLD_GLOBAL);

public IntPtr LoadSymbol(IntPtr libraryHandle, string symbol) => MacInterop.dlsym(libraryHandle, symbol);

public string GetLoadError() => MacInterop.dlerror();

public void SetEnv(string name, string value) => MacInterop.setenv(name, value, 1);

public IDisposable FileReadLock(string path) => new FileStream(path, FileMode.Open, FileAccess.Read);

public IDisposable FileWriteLock(string path) => new FileStream(path, FileMode.Open, FileAccess.Write);
Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Sdk/Platform/WindowsSystemCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public abstract class WindowsSystemCompat : ISystemCompat

public abstract IntPtr LoadSymbol(IntPtr libraryHandle, string symbol);

public void SetEnv(string name, string value) => throw new NotSupportedException();

public IDisposable? FileReadLock(string path) => null;

public IDisposable? FileWriteLock(string path) => null;
Expand Down
27 changes: 27 additions & 0 deletions NAPS2.Sdk/Scan/Internal/Sane/Native/SaneNativeLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ public class SaneNativeLibrary : Unmanaged.NativeLibrary
var libraryPath = FindPath(PlatformCompat.System.SaneLibraryName, testRoot);
var libraryDeps = PlatformCompat.System.SaneLibraryDeps
?.Select(path => FindPath(path, testRoot)).ToArray();
if (libraryDeps != null)
{
// If we're using a bundled SANE, we will need to manually set the environment
// variables to the appropriate folders.
var backendsFolder = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(libraryPath)!, "sane"));
var configFolder =
Path.GetFullPath(Path.Combine(Path.GetDirectoryName(libraryPath)!, "..", "_config", "sane"));
// We can't use Environment.SetEnvironmentVariable as that will just change the .NET
// env and won't be visible to SANE. Instead we use setenv which is technically not
// thread-safe but in practice should be fine here.
PlatformCompat.System.SetEnv("LD_LIBRARY_PATH", backendsFolder);
PlatformCompat.System.SetEnv("SANE_CONFIG_DIR", configFolder);
// Note: We can add SANE debug variables here
// PlatformCompat.System.SetEnv("SANE_DEBUG_DLL", "255");
}
var nativeLib = new SaneNativeLibrary(libraryPath, libraryDeps);
return nativeLib;
});
Expand All @@ -22,25 +37,37 @@ private SaneNativeLibrary(string libraryPath, string[]? libraryDeps)
}

public delegate SaneStatus sane_init_delegate(out int version_code, IntPtr authorize);

public delegate void sane_exit_delegate();

public delegate SaneStatus sane_get_devices_delegate(out IntPtr device_list, int local_only);

public delegate SaneStatus sane_open_delegate(string name, out IntPtr handle);

public delegate void sane_close_delegate(IntPtr handle);

public delegate IntPtr sane_get_option_descriptor_delegate(IntPtr handle, int n);

public delegate SaneStatus sane_control_option_delegate(IntPtr handle, int n, SaneOptionAction a, IntPtr v,
out SaneOptionSetInfo i);

public delegate SaneStatus sane_get_parameters_delegate(IntPtr handle, out SaneReadParameters p);

public delegate SaneStatus sane_start_delegate(IntPtr handle);

public delegate SaneStatus sane_read_delegate(IntPtr handle, byte[] buf, int maxlen, out int len);

public delegate void sane_cancel_delegate(IntPtr handle);

public sane_init_delegate sane_init => Load<sane_init_delegate>();
public sane_exit_delegate sane_exit => Load<sane_exit_delegate>();
public sane_get_devices_delegate sane_get_devices => Load<sane_get_devices_delegate>();
public sane_open_delegate sane_open => Load<sane_open_delegate>();
public sane_close_delegate sane_close => Load<sane_close_delegate>();

public sane_get_option_descriptor_delegate sane_get_option_descriptor =>
Load<sane_get_option_descriptor_delegate>();

public sane_control_option_delegate sane_control_option => Load<sane_control_option_delegate>();
public sane_get_parameters_delegate sane_get_parameters => Load<sane_get_parameters_delegate>();
public sane_start_delegate sane_start => Load<sane_start_delegate>();
Expand Down

0 comments on commit 83dd49c

Please sign in to comment.