00001 #region License
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #endregion
00027
00028 using System;
00029 using System.Collections.Generic;
00030 using System.Text;
00031 using System.Diagnostics;
00032 using System.IO;
00033 using System.Runtime.InteropServices;
00034 using System.Reflection;
00035
00036 namespace OpenTK
00037 {
00039 public static class Configuration
00040 {
00041 static bool runningOnWindows, runningOnUnix, runningOnX11, runningOnMacOS, runningOnLinux, runningOnMono;
00042
00043 #region --- Constructors ---
00044
00045
00046 static Configuration()
00047 {
00048 if (System.Environment.OSVersion.Platform == PlatformID.Win32NT ||
00049 System.Environment.OSVersion.Platform == PlatformID.Win32S ||
00050 System.Environment.OSVersion.Platform == PlatformID.Win32Windows ||
00051 System.Environment.OSVersion.Platform == PlatformID.WinCE)
00052 runningOnWindows = true;
00053 else if (System.Environment.OSVersion.Platform == PlatformID.Unix ||
00054 System.Environment.OSVersion.Platform == (PlatformID)4)
00055 {
00056
00057 string kernel_name = DetectUnixKernel();
00058 switch (kernel_name)
00059 {
00060 case null:
00061 case "":
00062 throw new PlatformNotSupportedException(
00063 "Unknown platform. Please file a bug report at http://www.opentk.com/node/add/project-issue/opentk");
00064
00065 case "Linux":
00066 runningOnLinux = runningOnUnix = true;
00067 break;
00068
00069 case "Darwin":
00070 runningOnMacOS = runningOnUnix = true;
00071 break;
00072
00073 default:
00074 runningOnUnix = true;
00075 break;
00076 }
00077 }
00078 else
00079 throw new PlatformNotSupportedException("Unknown platform. Please report this error at http://www.opentk.com.");
00080
00081
00082
00083
00084
00085 if (!RunningOnMacOS)
00086 {
00087 try { runningOnX11 = OpenTK.Platform.X11.API.DefaultDisplay != IntPtr.Zero; }
00088 catch { }
00089 }
00090
00091
00092 Type t = Type.GetType("Mono.Runtime");
00093 if (t != null)
00094 runningOnMono = true;
00095
00096 Debug.Print("Detected configuration: {0} / {1}",
00097 RunningOnWindows ? "Windows" : RunningOnLinux ? "Linux" : RunningOnMacOS ? "MacOS" :
00098 runningOnUnix ? "Unix" : RunningOnX11 ? "X11" : "Unknown Platform",
00099 RunningOnMono ? "Mono" : ".Net");
00100 }
00101
00102 #endregion
00103
00104 #region --- Public Methods ---
00105
00106 #region public static bool RunningOnWindows
00107
00109 public static bool RunningOnWindows { get { return runningOnWindows; } }
00110
00111 #endregion
00112
00113 #region public static bool RunningOnX11
00114
00116 public static bool RunningOnX11
00117 {
00118 get { return runningOnX11; }
00119 }
00120
00124 public static bool RunningOnUnix
00125 {
00126 get { return runningOnUnix; }
00127 }
00128
00129 #endregion
00130
00131 #region public static bool RunningOnLinux
00132
00134 public static bool RunningOnLinux { get { return runningOnLinux; } }
00135
00136 #endregion
00137
00138 #region public static bool RunningOnMacOS
00139
00141 public static bool RunningOnMacOS { get { return runningOnMacOS; } }
00142
00143 #endregion
00144
00145 #region public static bool RunningOnMono
00146
00150 public static bool RunningOnMono { get { return runningOnMono; } }
00151
00152 #endregion
00153
00154 #region --- Private Methods ---
00155
00156 #region private static string DetectUnixKernel()
00157
00158 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
00159 struct utsname
00160 {
00161 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
00162 public string sysname;
00163
00164 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
00165 public string nodename;
00166
00167 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
00168 public string release;
00169
00170 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
00171 public string version;
00172
00173 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
00174 public string machine;
00175
00176 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
00177 public string extraJustInCase;
00178
00179 }
00180
00185 private static string DetectUnixKernel()
00186 {
00187 Debug.Print("Size: {0}", Marshal.SizeOf(typeof(utsname)).ToString());
00188 Debug.Flush();
00189 utsname uts = new utsname();
00190 uname(out uts);
00191
00192 Debug.WriteLine("System:");
00193 Debug.Indent();
00194 Debug.WriteLine(uts.sysname);
00195 Debug.WriteLine(uts.nodename);
00196 Debug.WriteLine(uts.release);
00197 Debug.WriteLine(uts.version);
00198 Debug.WriteLine(uts.machine);
00199 Debug.Unindent();
00200
00201 return uts.sysname.ToString();
00202 }
00203
00204 [DllImport("libc")]
00205 private static extern void uname(out utsname uname_struct);
00206
00207 #endregion
00208
00209 #endregion
00210
00211 #endregion
00212 }
00213 }