Skip to content

Commit ade3c03

Browse files
committed
Fix and simplify platform detection
1 parent 5160742 commit ade3c03

30 files changed

Lines changed: 89 additions & 240 deletions

Terminal.Gui/Drivers/AnsiDriver/AnsiComponentFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public override ISizeMonitor CreateSizeMonitor (IOutput consoleOutput, IOutputBu
7878
/// </summary>
7979
internal static Func<Size?> CreateNativeSizeQuery ()
8080
{
81-
if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows))
81+
if (OperatingSystem.IsWindows ())
8282
{
8383
return () =>
8484
{

Terminal.Gui/Drivers/AnsiDriver/AnsiInput.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public AnsiInput ()
9494
}
9595

9696
// Initialize platform-specific input helpers
97-
if (PlatformDetection.IsWindows ())
97+
if (OperatingSystem.IsWindows ())
9898
{
9999
_windowsVTInput = new WindowsVTInputHelper ();
100100

@@ -111,7 +111,7 @@ public AnsiInput ()
111111
}
112112
_platform = AnsiPlatform.WindowsVT;
113113
}
114-
else if (PlatformDetection.IsUnixLike ())
114+
else
115115
{
116116
try
117117
{
@@ -139,10 +139,6 @@ public AnsiInput ()
139139
$"Failed to enable Unix raw input mode. libc not available: {ex.Message}. Running in degraded mode.");
140140
}
141141
}
142-
else
143-
{
144-
Trace.Lifecycle (nameof (AnsiInput), "Init", "Unknown OS platform. Terminal input will not work. Running in degraded mode.");
145-
}
146142

147143
// NOTE: Output operations (alternate buffer, cursor visibility, mouse events)
148144
// NOTE: are handled by ANSIOutput, not here. ANSIInput only handles input.

Terminal.Gui/Drivers/AnsiDriver/AnsiOutput.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public AnsiOutput (AppModel appModel = AppModel.FullScreen)
9494
}
9595

9696
// Initialize platform-specific output helpers
97-
if (PlatformDetection.IsWindows ())
97+
if (OperatingSystem.IsWindows ())
9898
{
9999
_windowsVTOutput = new WindowsVTOutputHelper ();
100100

@@ -111,7 +111,7 @@ public AnsiOutput (AppModel appModel = AppModel.FullScreen)
111111
}
112112
_platform = AnsiPlatform.WindowsVT;
113113
}
114-
else if (PlatformDetection.IsUnixLike ())
114+
else if (!OperatingSystem.IsWindows ())
115115
{
116116
// duplicate the controlling terminal output fd so we don't mess with it.
117117
// When stdout is redirected this is /dev/tty rather than STDOUT_FILENO,

Terminal.Gui/Drivers/DotNetDriver/NetOutput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void Dispose () { }
181181
/// <inheritdoc/>
182182
public void Suspend ()
183183
{
184-
if (PlatformDetection.IsWindows () && !IsAttachedToTerminal)
184+
if (OperatingSystem.IsWindows () && !IsAttachedToTerminal)
185185
{
186186
return;
187187
}

Terminal.Gui/Drivers/DriverImpl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ private static IClipboard CreateClipboard ()
205205
PlatformID p = Environment.OSVersion.Platform;
206206

207207
return CreateClipboard (() => p is PlatformID.Win32NT or PlatformID.Win32S or PlatformID.Win32Windows,
208-
() => RuntimeInformation.IsOSPlatform (OSPlatform.OSX),
208+
OperatingSystem.IsMacOS,
209209
PlatformDetection.IsWSL,
210-
PlatformDetection.IsLinux,
210+
OperatingSystem.IsLinux,
211211
() => new WindowsClipboard (),
212212
() => new MacOSXClipboard (),
213213
() => new WSLClipboard (),

Terminal.Gui/Drivers/PlatformDetection.cs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,6 @@ namespace Terminal.Gui.Drivers;
77
/// </summary>
88
public static class PlatformDetection
99
{
10-
/// <summary>
11-
/// Determines whether the current operating system is Linux.
12-
/// </summary>
13-
/// <remarks>
14-
/// This method returns <see langword="true"/> only when running on a Linux
15-
/// distribution. Other Unix-like platforms such as macOS and FreeBSD
16-
/// return <see langword="false"/>.
17-
/// </remarks>
18-
/// <returns>
19-
/// <see langword="true"/> if the operating system is Linux;
20-
/// otherwise, <see langword="false"/>.
21-
/// </returns>
22-
public static bool IsLinux () => RuntimeInformation.IsOSPlatform (OSPlatform.Linux);
23-
24-
/// <summary>
25-
/// Determines whether the current operating system is macOS.
26-
/// </summary>
27-
/// <returns>true if the current operating system is macOS; otherwise, false.</returns>
28-
public static bool IsMac () => RuntimeInformation.IsOSPlatform (OSPlatform.OSX);
29-
30-
/// <summary>
31-
/// Determines whether the current operating system is a Unix-like platform.
32-
/// </summary>
33-
/// <remarks>
34-
/// Unix-like platforms include operating systems that derive from or
35-
/// closely follow traditional UNIX and POSIX design principles.
36-
/// On .NET, this currently includes Linux, macOS (Darwin), and FreeBSD.
37-
/// </remarks>
38-
/// <returns>
39-
/// <see langword="true"/> if the operating system is Linux, macOS, or FreeBSD;
40-
/// otherwise, <see langword="false"/>.
41-
/// </returns>
42-
public static bool IsUnixLike () =>
43-
RuntimeInformation.IsOSPlatform (OSPlatform.Linux)
44-
|| RuntimeInformation.IsOSPlatform (OSPlatform.OSX)
45-
|| RuntimeInformation.IsOSPlatform (OSPlatform.FreeBSD);
46-
47-
48-
/// <summary>Returns the <see cref="TuiPlatform"/> for the current operating system.</summary>
49-
public static TuiPlatform GetCurrentPlatform ()
50-
{
51-
if (IsWindows ())
52-
{
53-
return TuiPlatform.Windows;
54-
}
55-
56-
if (IsMac ())
57-
{
58-
return TuiPlatform.Macos;
59-
}
60-
61-
return TuiPlatform.Linux;
62-
}
63-
64-
/// <summary>
65-
/// Determines if the current platform is Windows.
66-
/// </summary>
67-
/// <returns></returns>
68-
public static bool IsWindows () => RuntimeInformation.IsOSPlatform (OSPlatform.Windows);
69-
7010
/// <summary>
7111
/// Determines if the current platform is WSL (Windows Subsystem for Linux).
7212
/// </summary>

Terminal.Gui/Drivers/TerminalDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private static void EnsureInitialized ()
186186

187187
try
188188
{
189-
if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows))
189+
if (OperatingSystem.IsWindows ())
190190
{
191191
InitializeWindows ();
192192
}
Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Runtime.InteropServices;
23

34
namespace Terminal.Gui.Drivers;
@@ -36,58 +37,30 @@ private static int GetSuspendSignal ()
3637
return _suspendSignal;
3738
}
3839

39-
nint buf = Marshal.AllocHGlobal (8192);
40-
41-
if (uname (buf) != 0)
40+
if (OperatingSystem.IsMacOS () ||
41+
OperatingSystem.IsFreeBSD () ||
42+
RuntimeInformation.IsOSPlatform (OSPlatform.Create ("NETBSD")) ||
43+
RuntimeInformation.IsOSPlatform (OSPlatform.Create ("OPENBSD")))
4244
{
43-
Marshal.FreeHGlobal (buf);
44-
_suspendSignal = -1;
45-
46-
return _suspendSignal;
45+
_suspendSignal = 18;
4746
}
48-
49-
try
47+
else if (OperatingSystem.IsLinux ())
5048
{
51-
switch (Marshal.PtrToStringAnsi (buf))
52-
{
53-
case "Darwin":
54-
case "DragonFly":
55-
case "FreeBSD":
56-
case "NetBSD":
57-
case "OpenBSD":
58-
_suspendSignal = 18;
59-
60-
break;
61-
62-
case "Linux":
63-
// TODO: should fetch the machine name and
64-
// if it is MIPS return 24
65-
_suspendSignal = 20;
66-
67-
break;
68-
69-
case "Solaris":
70-
_suspendSignal = 24;
71-
72-
break;
73-
74-
default:
75-
_suspendSignal = -1;
76-
77-
break;
78-
}
79-
80-
return _suspendSignal;
49+
_suspendSignal = 20;
8150
}
82-
finally
51+
else if (RuntimeInformation.IsOSPlatform (OSPlatform.Create ("SOLARIS")) ||
52+
RuntimeInformation.IsOSPlatform (OSPlatform.Create ("ILLUMOS")))
8353
{
84-
Marshal.FreeHGlobal (buf);
54+
_suspendSignal = 24;
8555
}
56+
else
57+
{
58+
_suspendSignal = -1;
59+
}
60+
61+
return _suspendSignal;
8662
}
8763

8864
[DllImport ("libc", SetLastError = true)]
8965
private static extern int killpg (int pgrp, int sig);
90-
91-
[DllImport ("libc")]
92-
private static extern int uname (nint buf);
9366
}

Terminal.Gui/Drivers/UnixHelpers/UnixIOHelper.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ public struct WinSize
181181
/// Get window/terminal size using ioctl.
182182
/// Platform-specific constant (different on Darwin/BSD vs Linux).
183183
/// </summary>
184-
public static readonly uint TIOCGWINSZ = RuntimeInformation.IsOSPlatform (OSPlatform.OSX) || RuntimeInformation.IsOSPlatform (OSPlatform.FreeBSD)
185-
? 0x40087468u // Darwin/BSD
186-
: 0x5413u; // Linux
184+
public static readonly uint TIOCGWINSZ = OperatingSystem.IsLinux () ? 0x5413u : 0x40087468u;
187185

188186
/// <summary>
189187
/// I/O control operations on file descriptors.
@@ -196,7 +194,7 @@ public struct WinSize
196194
public static extern int ioctl (int fd, uint request, out WinSize ws);
197195

198196
/// <summary>
199-
/// ioctl definition for Darwin/FreeBSD on ARM64.
197+
/// ioctl definition for Darwin on ARM64.
200198
/// See https://github.com/dotnet/runtime/issues/48796#issuecomment-3695794860.
201199
/// </summary>
202200
/// <param name="fd">File descriptor</param>
@@ -435,7 +433,7 @@ public static bool TryGetTerminalSize (out Size size)
435433
WinSize ws;
436434

437435
if (RuntimeInformation.OSArchitecture == Architecture.Arm64
438-
&& (RuntimeInformation.IsOSPlatform (OSPlatform.OSX) || RuntimeInformation.IsOSPlatform (OSPlatform.FreeBSD)))
436+
&& OperatingSystem.IsMacOS ())
439437
{
440438
ioctlResult = ioctl_arm64 (fd,
441439
TIOCGWINSZ,

Terminal.Gui/Drivers/UnixHelpers/UnixRawModeHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public bool TryEnable ()
5858
}
5959

6060
// Only attempt on Unix-like platforms
61-
if (!PlatformDetection.IsUnixLike ())
61+
if (OperatingSystem.IsWindows ())
6262
{
6363
return false;
6464
}

0 commit comments

Comments
 (0)