Refactored environment config detection

This significantly cleans up the startup sequence on all platforms:
- X11 is not detected on non-Linux platforms unless the user explicitly
requests it
- Supports selection of platform abstractions (SDL) vs native
implementations.
- Returns correct flags on Android and iOS.

This contains a semantic change: OpenTK.Configuration will not return
correct values until OpenTK.Toolkit.Init() has been called, either
directly or indirectly (e.g. by creating a window.)
This commit is contained in:
Stefanos A. 2013-11-12 20:32:31 +01:00
parent 7503a87b3f
commit 663864f083

View file

@ -35,23 +35,22 @@ using System.Reflection;
namespace OpenTK namespace OpenTK
{ {
/// <summary>Provides information about the underlying OS and runtime.</summary> /// <summary>
public static class Configuration /// Provides information about the underlying OS and runtime.
/// You must call <c>Toolkit.Init</c> before accessing members
/// of this class.
/// </summary>
public sealed class Configuration
{ {
static bool runningOnWindows, runningOnUnix, runningOnX11, runningOnMacOS, runningOnLinux; static bool runningOnWindows, runningOnUnix, runningOnX11, runningOnMacOS, runningOnLinux;
static bool runningOnMono; static bool runningOnMono;
static bool runningOnAndroid; static bool runningOnAndroid;
static bool? sdl2supported;
volatile static bool initialized; volatile static bool initialized;
readonly static object InitLock = new object(); readonly static object InitLock = new object();
#region Constructors #region Constructors
// Detects the underlying OS and runtime. Configuration() { }
static Configuration()
{
Init();
}
#endregion #endregion
@ -89,7 +88,8 @@ namespace OpenTK
/// </summary> /// </summary>
public static bool RunningOnSdl2 public static bool RunningOnSdl2
{ {
get { return Sdl2Supported; } get;
private set;
} }
#endregion #endregion
@ -190,7 +190,14 @@ namespace OpenTK
[DllImport("libc")] [DllImport("libc")]
private static extern void uname(out utsname uname_struct); private static extern void uname(out utsname uname_struct);
private static bool DetectSdl2() static bool DetectMono()
{
// Detect the Mono runtime (code taken from http://mono.wikia.com/wiki/Detecting_if_program_is_running_in_Mono).
Type t = Type.GetType("Mono.Runtime");
return t != null;
}
static bool DetectSdl2()
{ {
bool supported = false; bool supported = false;
@ -224,6 +231,48 @@ namespace OpenTK
return supported; return supported;
} }
static void DetectUnix(out bool unix, out bool linux, out bool macos)
{
unix = linux = macos = false;
string kernel_name = DetectUnixKernel();
switch (kernel_name)
{
case null:
case "":
throw new PlatformNotSupportedException(
"Unknown platform. Please file a bug report at http://www.opentk.com");
case "Linux":
linux = unix = true;
break;
case "Darwin":
macos = unix = true;
break;
default:
unix = true;
break;
}
}
static bool DetectWindows()
{
return
System.Environment.OSVersion.Platform == PlatformID.Win32NT ||
System.Environment.OSVersion.Platform == PlatformID.Win32S ||
System.Environment.OSVersion.Platform == PlatformID.Win32Windows ||
System.Environment.OSVersion.Platform == PlatformID.WinCE;
}
static bool DetectX11()
{
// Detect whether X is present.
try { return OpenTK.Platform.X11.API.DefaultDisplay != IntPtr.Zero; }
catch { return false; }
}
#endregion #endregion
#endregion #endregion
@ -232,78 +281,42 @@ namespace OpenTK
#region Internal Methods #region Internal Methods
internal static bool Sdl2Supported // Detects the underlying OS and runtime.
{ internal static void Init(ToolkitOptions options)
get
{
return DetectSdl2();
}
}
internal static void Init()
{ {
lock (InitLock) lock (InitLock)
{ {
if (!initialized) if (!initialized)
{ {
if (System.Environment.OSVersion.Platform == PlatformID.Win32NT || #if ANDROID
System.Environment.OSVersion.Platform == PlatformID.Win32S || runningOnMono = true;
System.Environment.OSVersion.Platform == PlatformID.Win32Windows || runningOnAnroid = true;
System.Environment.OSVersion.Platform == PlatformID.WinCE) #elif IPHONE
runningOnMono = true;
runningOnIOS = true;
#else
runningOnMono = DetectMono();
runningOnWindows = DetectWindows();
if (!runningOnWindows)
{ {
runningOnWindows = true; DetectUnix(out runningOnUnix, out runningOnLinux, out runningOnMacOS);
}
else if (System.Environment.OSVersion.Platform == PlatformID.Unix ||
System.Environment.OSVersion.Platform == (PlatformID)4)
{
// Distinguish between Linux, Mac OS X and other Unix operating systems.
string kernel_name = DetectUnixKernel();
switch (kernel_name)
{
case null:
case "":
throw new PlatformNotSupportedException(
"Unknown platform. Please file a bug report at http://www.opentk.com/node/add/project-issue/opentk");
case "Linux":
runningOnLinux = runningOnUnix = true;
break;
case "Darwin":
runningOnMacOS = runningOnUnix = true;
break;
default:
runningOnUnix = true;
break;
}
}
else
{
throw new PlatformNotSupportedException("Unknown platform. Please report this error at http://www.opentk.com.");
} }
// Detect whether X is present. if (options.Backend == PlatformBackend.Default)
// Hack: it seems that this check will cause X to initialize itself on Mac OS X Leopard and newer.
// We don't want that (we'll be using the native interfaces anyway), so we'll avoid this check
// when we detect Mac OS X.
if (!RunningOnMacOS)
{ {
try { runningOnX11 = OpenTK.Platform.X11.API.DefaultDisplay != IntPtr.Zero; } RunningOnSdl2 = DetectSdl2();
catch { } }
else if (runningOnLinux || options.Backend == PlatformBackend.PreferX11)
{
runningOnX11 = DetectX11();
} }
// Detect the Mono runtime (code taken from http://mono.wikia.com/wiki/Detecting_if_program_is_running_in_Mono). initialized = true;
Type t = Type.GetType("Mono.Runtime"); #endif
if (t != null)
runningOnMono = true;
Debug.Print("Detected configuration: {0} / {1}", Debug.Print("Detected configuration: {0} / {1}",
RunningOnWindows ? "Windows" : RunningOnLinux ? "Linux" : RunningOnMacOS ? "MacOS" : RunningOnWindows ? "Windows" : RunningOnLinux ? "Linux" : RunningOnMacOS ? "MacOS" :
runningOnUnix ? "Unix" : RunningOnX11 ? "X11" : "Unknown Platform", runningOnUnix ? "Unix" : RunningOnX11 ? "X11" : "Unknown Platform",
RunningOnMono ? "Mono" : ".Net"); RunningOnMono ? "Mono" : ".Net");
initialized = true;
} }
} }
} }