using Avalonia; using Avalonia.OpenGL.Egl; using System.CommandLine; namespace GSS2.Test; public partial class Program { private enum RenderingMode { X11, DRM } private static string[]? _args = null; private static readonly RootCommand _rootCommand = new("Run UI application"); private static readonly Option _fullscreen = new("--fullscreen") { Description = "Run in fullscreen mode" }; private static readonly Option _debug = new Option("--debug") { Description = "Wait debugger attach on launch", Hidden = true }; private static readonly Option _renderingMode = new("--rendering-mode") { Description = "Rendering mode to use", DefaultValueFactory = (_) => RenderingMode.X11 }; private static readonly Option _hideConsole = new("--hide-console") { Description = "Suppress console output", DefaultValueFactory = (result) => result.GetRequiredValue(_renderingMode) == RenderingMode.DRM }; private static readonly Command _cameraTuningCommand = new("camera-tuning") { Description = "Tune camera parameters" }; private static readonly Option _cameraTuningOutputFile = new("--output") { Description = "Output image file", Required = false }; private static readonly Option _cameraTuningGainR = new("--gain-r") { Description = "Color gain (red channel) to start from", DefaultValueFactory = (_) => 1 }; private static readonly Option _cameraTuningGainG = new("--gain-g") { Description = "Color gain (green channel) to start from", DefaultValueFactory = (_) => 1 }; private static readonly Option _cameraTuningGainB = new("--gain-b") { Description = "Color gain (blue channel) to start from", DefaultValueFactory = (_) => 1 }; private static readonly Option _cameraTuningThreshold = new("--threshold") { Description = "Threshold to stop on", DefaultValueFactory = (_) => 0.1 }; private static readonly Option _cameraTuningSteps = new("--steps") { Description = "Maximum steps count", DefaultValueFactory = (_) => 10 }; private static readonly Command _captureCommand = new("capture") { Description = "Capture image from camera" }; private static readonly Option _captureOutputFile = new("--output") { Description = "Output image file", DefaultValueFactory = (_) => new FileInfo("image.png") }; private static readonly Option _captureIntencityWhite = new("--intencity-white") { Description = "Illuminator intencity for white (from 0 to 1)", DefaultValueFactory = (_) => 1 }; private static readonly Option _captureIntencityUv365nm = new("--intencity-uv365nm") { Description = "Illuminator intencity for UV 365 nm (from 0 to 1)", DefaultValueFactory = (_) => 0 }; private static readonly Option _captureIntencityUv254nm = new("--intencity-uv254nm") { Description = "Illuminator intencity for UV 254 nm (from 0 to 1)", DefaultValueFactory = (_) => 0 }; private static readonly Command _imageStorageClearCommand = new("image-storage-clear") { Description = "Clear image storage from unused images" }; public static event ConsoleCancelEventHandler? ShutdownRequested; [STAThread] public static int Main(string[] args) { _args = args; _rootCommand.SetAction(Ui); _rootCommand.Add(_fullscreen); _rootCommand.Add(_debug); _rootCommand.Add(_renderingMode); _rootCommand.Add(_hideConsole); _rootCommand.Add(_cameraTuningCommand); _cameraTuningCommand.SetAction(CameraTuning); _cameraTuningCommand.Add(_cameraTuningOutputFile); _cameraTuningCommand.Add(_cameraTuningGainR); _cameraTuningCommand.Add(_cameraTuningGainG); _cameraTuningCommand.Add(_cameraTuningGainB); _cameraTuningCommand.Add(_cameraTuningThreshold); _cameraTuningCommand.Add(_cameraTuningSteps); _rootCommand.Add(_captureCommand); _captureCommand.SetAction(Capture); _captureCommand.Add(_captureOutputFile); _captureCommand.Add(_captureIntencityWhite); _captureCommand.Add(_captureIntencityUv365nm); _captureCommand.Add(_captureIntencityUv254nm); _rootCommand.Add(_imageStorageClearCommand); _imageStorageClearCommand.SetAction(ImageStorageClear); Console.CancelKeyPress += new ConsoleCancelEventHandler(OnProgramShutdown); Console.WriteLine("Press Ctrl+C to shut down."); var parseResult = _rootCommand.Parse(_args); if (parseResult.Errors.Count() != 0) { foreach (var parseError in parseResult.Errors) Console.Error.WriteLine(parseError.Message); return 1; } return parseResult.Invoke(); } private static void OnProgramShutdown(object? sender, ConsoleCancelEventArgs ea) { ShutdownRequested?.Invoke(sender, ea); ea.Cancel = true; } }