diff --git a/Terminal.Gui/ConsoleDrivers/FeatureDetection/ConsoleFeatureFinder.cs b/Terminal.Gui/ConsoleDrivers/FeatureDetection/ConsoleFeatureFinder.cs new file mode 100644 index 0000000000..1ce9c5af34 --- /dev/null +++ b/Terminal.Gui/ConsoleDrivers/FeatureDetection/ConsoleFeatureFinder.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Win32; + +namespace Terminal.Gui; + +/// +/// Attempts to determine information about the terminal and what features it +/// does/not support based on runtime operations e.g. registry etc +/// +internal class ConsoleFeatureFinder +{ + public ConsoleFeatureFinderResults GetResults () + { + var results = new ConsoleFeatureFinderResults (); + + PlatformID p = Environment.OSVersion.Platform; + results.IsWindows = p is PlatformID.Win32NT or PlatformID.Win32S or PlatformID.Win32Windows; + + if (results.IsWindows) + { + DetectWindowsSpecificFeatures (results.Windows); + } + + return results; + } + + private void DetectWindowsSpecificFeatures (WindowsFeatureSet windowsFeatures) + { + windowsFeatures.ConHostLegacyMode = IsLegacyConsoleEnabled (); + } + + bool IsLegacyConsoleEnabled () + { + try + { + using (RegistryKey key = Registry.CurrentUser.OpenSubKey (@"Console")) + { + if (key != null) + { + object value = key.GetValue ("ForceV2"); + if (value is int intValue) + { + return intValue == 0; // Legacy Mode enabled if ForceV2 is 0 + } + } + } + } + catch (Exception ex) + { + Logging.Warning ("Error reading registry: " + ex.Message); + } + + return false; // Assume new console mode if check fails + } +} \ No newline at end of file diff --git a/Terminal.Gui/ConsoleDrivers/FeatureDetection/ConsoleFeatureFinderResults.cs b/Terminal.Gui/ConsoleDrivers/FeatureDetection/ConsoleFeatureFinderResults.cs new file mode 100644 index 0000000000..fd9fa6e8e6 --- /dev/null +++ b/Terminal.Gui/ConsoleDrivers/FeatureDetection/ConsoleFeatureFinderResults.cs @@ -0,0 +1,16 @@ +namespace Terminal.Gui; + +/// +/// Results of console feature detection +/// +internal class ConsoleFeatureFinderResults +{ + public WindowsFeatureSet Windows { get; set; } = new WindowsFeatureSet(); + public bool IsWindows { get; set; } + + /// + public override string ToString () + { + return $"{nameof(IsWindows)}:{IsWindows} {nameof(Windows)}:{Windows}"; + } +} \ No newline at end of file diff --git a/Terminal.Gui/ConsoleDrivers/FeatureDetection/WindowsFeatureSet.cs b/Terminal.Gui/ConsoleDrivers/FeatureDetection/WindowsFeatureSet.cs new file mode 100644 index 0000000000..1d54ab5ac7 --- /dev/null +++ b/Terminal.Gui/ConsoleDrivers/FeatureDetection/WindowsFeatureSet.cs @@ -0,0 +1,15 @@ +using static Unix.Terminal.Curses; + +namespace Terminal.Gui; + +/// +/// Features specific to the windows operating system +/// +internal class WindowsFeatureSet +{ + + public bool ConHostLegacyMode { get; set; } + + + public override string ToString () { return $"{nameof(ConHostLegacyMode)}:{ConHostLegacyMode}"; } +} diff --git a/Terminal.Gui/ConsoleDrivers/V2/ApplicationV2.cs b/Terminal.Gui/ConsoleDrivers/V2/ApplicationV2.cs index 9baeba301f..f4500eef09 100644 --- a/Terminal.Gui/ConsoleDrivers/V2/ApplicationV2.cs +++ b/Terminal.Gui/ConsoleDrivers/V2/ApplicationV2.cs @@ -85,11 +85,15 @@ public override void Init (IConsoleDriver? driver = null, string? driverName = n private void CreateDriver (string? driverName) { - PlatformID p = Environment.OSVersion.Platform; bool definetlyWin = driverName?.Contains ("win") ?? false; bool definetlyNet = driverName?.Contains ("net") ?? false; + var finder = new ConsoleFeatureFinder (); + var result = finder.GetResults (); + + Logging.Logger.LogInformation ($"Feature detection results:{ result}"); + if (definetlyWin) { _coordinator = CreateWindowsSubcomponents (); @@ -98,7 +102,7 @@ private void CreateDriver (string? driverName) { _coordinator = CreateNetSubcomponents (); } - else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows) + else if (result.IsWindows) { _coordinator = CreateWindowsSubcomponents (); }