From b02abbe150466527c15a2438965dcc713cac60ab Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Thu, 28 Feb 2008 13:39:42 +0000 Subject: [PATCH] Initial commit. Can detect if we are running on Windows, Linux or OSX. --- Source/OpenTK/Configuration.cs | 126 +++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 Source/OpenTK/Configuration.cs diff --git a/Source/OpenTK/Configuration.cs b/Source/OpenTK/Configuration.cs new file mode 100644 index 00000000..7e4de1cd --- /dev/null +++ b/Source/OpenTK/Configuration.cs @@ -0,0 +1,126 @@ +#region --- License --- +/* Licensed under the MIT/X11 license. + * Copyright (c) 2006-2008 the OpenTK Team. + * This notice may not be removed from any source distribution. + * See license.txt for licensing detailed licensing details. + */ +#endregion + +using System; +using System.Collections.Generic; +using System.Text; +using System.Diagnostics; +using System.IO; + +namespace OpenTK +{ + /// + /// Contains configuration options for OpenTK. + internal static class Configuration + { + static bool runningOnWindows, runningOnX11, runningOnOSX; + + #region --- Constructors --- + + static Configuration() + { + if (System.Environment.OSVersion.Platform == PlatformID.Win32NT || + System.Environment.OSVersion.Platform == PlatformID.Win32S || + System.Environment.OSVersion.Platform == PlatformID.Win32Windows || + System.Environment.OSVersion.Platform == PlatformID.WinCE) + runningOnWindows = true; + else if (System.Environment.OSVersion.Platform == PlatformID.Unix || + System.Environment.OSVersion.Platform == (PlatformID)4) + { + // Distinguish between Unix and Mac OS X kernels. + switch (DetectUnixKernel()) + { + case "Unix": + case "Linux": + runningOnX11 = true; + break; + + case "Darwin": + runningOnOSX = true; + break; + + default: + throw new PlatformNotSupportedException( + DetectUnixKernel() + ": Unknown Unix platform. Please report this error at http://www.opentk.com."); + } + } + else + throw new PlatformNotSupportedException("Unknown platform. Please report this error at http://www.opentk.com."); + } + + #endregion + + #region --- Public Methods --- + + #region internal static bool RunningOnWindows + + /// Gets a System.Boolean indicating whether OpenTK is running on a Windows platform. + internal static bool RunningOnWindows { get { return runningOnWindows; } } + + #endregion + + #region internal static bool RunningOnX11 + + /// Gets a System.Boolean indicating whether OpenTK is running on an X11 platform. + internal static bool RunningOnX11 { get { return runningOnX11; } } + + #endregion + + #region internal static bool RunningOnOSX + + /// Gets a System.Boolean indicating whether OpenTK is running on an OSX platform. + internal static bool RunningOnOSX { get { return runningOnOSX; } } + + #endregion + + #region --- Private Methods --- + + #region private static string DetectUnixKernel() + + /// + /// Executes "uname" which returns a string representing the name of the + /// underlying Unix kernel. + /// + /// "Unix", "Linux", "Darwin" or null. + /// Source code from "Mono: A Developer's Notebook" + private static string DetectUnixKernel() + { + ProcessStartInfo startInfo = new ProcessStartInfo(); + startInfo.Arguments = "-s"; + startInfo.RedirectStandardOutput = true; + startInfo.RedirectStandardError = true; + startInfo.UseShellExecute = false; + foreach (string unameprog in new string[] { "/usr/bin/uname", "/bin/uname", "uname" }) + { + try + { + startInfo.FileName = unameprog; + Process uname = Process.Start(startInfo); + StreamReader stdout = uname.StandardOutput; + return stdout.ReadLine().Trim(); + } + catch (System.IO.FileNotFoundException) + { + // The requested executable doesn't exist, try next one. + continue; + } + catch (System.ComponentModel.Win32Exception) + { + continue; + } + } + return null; + } + + #endregion + + #endregion + + #endregion + } +}