5.1 KiB
MatterControl Linux Migration Notes
Current State
The project is a C# .NET 6.0 desktop application for 3D printing control. It currently targets Windows but has cross-platform foundations.
Build Errors on Linux
error NETSDK1100: To build a project targeting Windows on this operating system, set the EnableWindowsTargeting property to true.
Affected projects:
MatterControl.Winforms/MatterControl.Winforms.csprojSubmodules/agg-sharp/PlatformWin32/PlatformWin32.csprojSubmodules/agg-sharp/Tests/TestInvoker/TestInvoker.csproj
Architecture Overview
Platform Abstraction Layer
The project uses a provider pattern for platform-specific functionality:
-
Window Providers (in
Program.cs:167-172):MatterHackers.GlfwProvider.GlfwWindowProvider- Cross-platform (GLFW)MatterHackers.MatterControl.WinformsSingleWindowProvider- Windows-only (WinForms)
-
Platform Features (
Program.cs:276):MatterHackers.MatterControl.WindowsPlatformsFeatures- Windows implementation
Key Projects
| Project | Target | Notes |
|---|---|---|
MatterControl.csproj |
net6.0-windows |
Main entry point, needs net6.0 |
MatterControlLib.csproj |
net6.0-windows |
Core library, needs net6.0 |
MatterControl.Winforms.csproj |
net6.0-windows |
WinForms provider, Windows-only |
GlfwProvider.csproj |
net6.0 |
Already cross-platform! |
PlatformWin32.csproj |
net6.0-windows |
Windows-only |
GLFW Provider (Cross-Platform)
Located at Submodules/agg-sharp/Glfw/GlfwProvider.csproj - already targets net6.0 and provides cross-platform windowing via GLFW library.
Windows-Specific Code in Program.cs
// P/Invoke - Windows only
[DllImport("kernel32.dll")]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
[DllImport("Shcore.dll")]
static extern int SetProcessDpiAwareness(int PROCESS_DPI_AWARENESS);
These are used for:
- Preventing system sleep during prints (
KeepAwake) - High-DPI display support
Windows-Specific Code in WindowsPlatformsFeatures.cs
System.Media.SoundPlayer- Windows-only audioWindowsFormsClipboard- WinForms clipboardWinformsEventSink.AllowInspector- Debug inspectorInspectForm- WinForms debug form
Migration Strategy
Phase 1: Project File Changes
-
Change target frameworks from
net6.0-windowstonet6.0:MatterControl.csprojMatterControlLib.csproj
-
Use conditional project references:
<ProjectReference Condition="'$(OS)' == 'Windows_NT'" Include="MatterControl.Winforms.csproj" /> -
Remove or conditionally exclude:
PrinterDriverInstaller(Windows driver installer)MatterControl.Winformsreference on Linux
Phase 2: Code Changes
-
Program.cs - Add platform detection:
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { AggContext.Config.ProviderTypes.SystemWindowProvider = "MatterHackers.GlfwProvider.GlfwWindowProvider, MatterHackers.GlfwProvider"; // Use Linux platform features } -
Create LinuxPlatformFeatures.cs:
- Implement
INativePlatformFeaturesfor Linux - Handle clipboard via X11/Wayland or cross-platform library
- Stub out Windows-specific features (sound, camera)
- Implement
-
Conditional P/Invoke:
static void KeepAwake(bool keepAwake) { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return; // Windows implementation }
Phase 3: Dependencies
-
GLFW native library: Need
libglfw.soon Linux# Debian/Ubuntu sudo apt-get install libglfw3 # Or via Nix pkgs.glfw -
OpenGL: Need OpenGL drivers
sudo apt-get install libgl1-mesa-dev -
Serial ports: Already have
System.IO.Portspackage (cross-platform)
Phase 4: shell.nix Updates
{
pkgs ? import <nixpkgs> { },
}:
pkgs.mkShell {
packages = with pkgs; [
dotnet-sdk_8
glfw
libGL
xorg.libX11
xorg.libXcursor
xorg.libXrandr
xorg.libXinerama
xorg.libXi
];
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
pkgs.glfw
pkgs.libGL
];
}
Files to Create
MatterControl.Linux/LinuxPlatformFeatures.cs- Linux platform implementationMatterControl.Linux/MatterControl.Linux.csproj- Linux-specific project (optional)
Files to Modify
MatterControl.csproj- Change TFM, conditional referencesMatterControlLib/MatterControlLib.csproj- Change TFMProgram.cs- Platform detection, conditional codeshell.nix- Add native dependencies
Alternative: Minimal Changes Approach
Instead of full platform abstraction, use #if directives:
#if WINDOWS
// Windows-specific code
#else
// Linux/cross-platform code
#endif
And build with:
dotnet build -p:DefineConstants=LINUX
Open Questions
- Should the GLFW provider be the default for all platforms?
- What Linux-specific features need implementation (file dialogs, etc.)?
- Is the old
PlatformGtkworth updating or should we focus on GLFW? - Should we create a separate solution file for Linux builds?