2009-11-02 10:33:53 +01:00
|
|
|
#region License
|
2009-09-04 12:34:18 +02:00
|
|
|
//
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
//
|
|
|
|
// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
2009-02-22 11:43:35 +01:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Runtime.InteropServices;
|
2009-09-04 12:34:18 +02:00
|
|
|
using System.Reflection;
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
namespace OpenTK
|
|
|
|
{
|
2013-11-12 20:32:31 +01:00
|
|
|
/// <summary>
|
|
|
|
/// 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
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2013-10-13 22:35:20 +02:00
|
|
|
static bool runningOnWindows, runningOnUnix, runningOnX11, runningOnMacOS, runningOnLinux;
|
|
|
|
static bool runningOnMono;
|
2010-12-08 09:58:52 +01:00
|
|
|
volatile static bool initialized;
|
|
|
|
readonly static object InitLock = new object();
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2010-12-08 09:58:52 +01:00
|
|
|
#region Constructors
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2013-11-12 20:32:31 +01:00
|
|
|
Configuration() { }
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2010-12-08 09:00:44 +01:00
|
|
|
#region Public Methods
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
#region public static bool RunningOnWindows
|
|
|
|
|
|
|
|
/// <summary>Gets a System.Boolean indicating whether OpenTK is running on a Windows platform.</summary>
|
|
|
|
public static bool RunningOnWindows { get { return runningOnWindows; } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public static bool RunningOnX11
|
|
|
|
|
|
|
|
/// <summary>Gets a System.Boolean indicating whether OpenTK is running on an X11 platform.</summary>
|
2009-11-02 10:33:53 +01:00
|
|
|
public static bool RunningOnX11
|
|
|
|
{
|
|
|
|
get { return runningOnX11; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a <see cref="System.Boolean"/> indicating whether OpenTK is running on a Unix platform.
|
|
|
|
/// </summary>
|
|
|
|
public static bool RunningOnUnix
|
|
|
|
{
|
|
|
|
get { return runningOnUnix; }
|
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2013-09-30 20:47:29 +02:00
|
|
|
#region RunningOnSDL2
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a System.Boolean indicating whether OpenTK is running on the SDL2 backend.
|
|
|
|
/// </summary>
|
|
|
|
public static bool RunningOnSdl2
|
|
|
|
{
|
2013-11-12 20:32:31 +01:00
|
|
|
get;
|
|
|
|
private set;
|
2013-09-30 20:47:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
#region public static bool RunningOnLinux
|
|
|
|
|
|
|
|
/// <summary>Gets a System.Boolean indicating whether OpenTK is running on an X11 platform.</summary>
|
|
|
|
public static bool RunningOnLinux { get { return runningOnLinux; } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public static bool RunningOnMacOS
|
|
|
|
|
|
|
|
/// <summary>Gets a System.Boolean indicating whether OpenTK is running on a MacOS platform.</summary>
|
|
|
|
public static bool RunningOnMacOS { get { return runningOnMacOS; } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region public static bool RunningOnMono
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a System.Boolean indicating whether OpenTK is running on the Mono runtime.
|
|
|
|
/// </summary>
|
|
|
|
public static bool RunningOnMono { get { return runningOnMono; } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2013-10-13 22:35:20 +02:00
|
|
|
#region public static bool RunningOnAndroid
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a <c>System.Boolean</c> indicating whether
|
|
|
|
/// OpenTK is running on an Android device.
|
|
|
|
/// </summary>
|
|
|
|
public static bool RunningOnAndroid
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
#if ANDROID
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
#region --- Private Methods ---
|
|
|
|
|
|
|
|
#region private static string DetectUnixKernel()
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
|
|
|
struct utsname
|
|
|
|
{
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
|
|
|
public string sysname;
|
|
|
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
|
|
|
public string nodename;
|
|
|
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
|
|
|
public string release;
|
|
|
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
|
|
|
public string version;
|
|
|
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
|
|
|
public string machine;
|
|
|
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
|
|
|
|
public string extraJustInCase;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2009-11-16 15:28:37 +01:00
|
|
|
/// Detects the unix kernel by p/invoking uname (libc).
|
2009-02-22 11:43:35 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
private static string DetectUnixKernel()
|
|
|
|
{
|
|
|
|
Debug.Print("Size: {0}", Marshal.SizeOf(typeof(utsname)).ToString());
|
|
|
|
Debug.Flush();
|
|
|
|
utsname uts = new utsname();
|
|
|
|
uname(out uts);
|
|
|
|
|
|
|
|
Debug.WriteLine("System:");
|
|
|
|
Debug.Indent();
|
|
|
|
Debug.WriteLine(uts.sysname);
|
|
|
|
Debug.WriteLine(uts.nodename);
|
|
|
|
Debug.WriteLine(uts.release);
|
|
|
|
Debug.WriteLine(uts.version);
|
|
|
|
Debug.WriteLine(uts.machine);
|
|
|
|
Debug.Unindent();
|
|
|
|
|
|
|
|
return uts.sysname.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
[DllImport("libc")]
|
|
|
|
private static extern void uname(out utsname uname_struct);
|
|
|
|
|
2013-11-12 20:32:31 +01:00
|
|
|
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()
|
2013-09-27 14:41:37 +02:00
|
|
|
{
|
|
|
|
bool supported = false;
|
|
|
|
|
|
|
|
// Detect whether SDL2 is supported
|
2014-05-30 13:47:27 +02:00
|
|
|
// We require:
|
|
|
|
// - SDL2 version 2.0.0 or higher (previous beta
|
|
|
|
// versions are not ABI-compatible)
|
|
|
|
// - Successful SDL2 initialization (sometimes the
|
|
|
|
// binary exists but fails to initialize correctly)
|
|
|
|
var version = new Platform.SDL2.Version();
|
2013-09-27 14:41:37 +02:00
|
|
|
try
|
|
|
|
{
|
2014-05-30 13:47:27 +02:00
|
|
|
version = Platform.SDL2.SDL.Version;
|
|
|
|
if (version.Number >= 2000)
|
2013-09-27 14:41:37 +02:00
|
|
|
{
|
2014-05-30 13:47:27 +02:00
|
|
|
if (Platform.SDL2.SDL.WasInit(0))
|
2013-09-30 20:47:29 +02:00
|
|
|
{
|
|
|
|
supported = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-30 13:47:27 +02:00
|
|
|
// Attempt to initialize SDL2.
|
|
|
|
var flags =
|
|
|
|
Platform.SDL2.SystemFlags.VIDEO |
|
|
|
|
Platform.SDL2.SystemFlags.TIMER;
|
|
|
|
|
|
|
|
if (Platform.SDL2.SDL.Init(flags) == 0)
|
|
|
|
{
|
|
|
|
supported = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Print("SDL2 init failed with error: {0}",
|
|
|
|
Platform.SDL2.SDL.GetError());
|
|
|
|
}
|
2013-09-30 20:47:29 +02:00
|
|
|
}
|
2013-09-27 14:41:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Debug.Print("SDL2 init failed with exception: {0}", e);
|
|
|
|
}
|
2014-05-30 13:47:27 +02:00
|
|
|
|
|
|
|
if (!supported)
|
|
|
|
{
|
|
|
|
Debug.Print("SDL2 is not supported");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-30 13:50:11 +02:00
|
|
|
Debug.Print("SDL2 is supported. Version is {0}.{1}.{2}",
|
2014-05-30 13:47:27 +02:00
|
|
|
version.Major, version.Minor, version.Patch);
|
|
|
|
}
|
2013-09-27 14:41:37 +02:00
|
|
|
|
|
|
|
return supported;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:32:31 +01:00
|
|
|
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; }
|
|
|
|
}
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2010-12-08 09:00:44 +01:00
|
|
|
#endregion
|
|
|
|
|
2010-12-08 09:58:52 +01:00
|
|
|
#region Internal Methods
|
|
|
|
|
2013-11-12 20:32:31 +01:00
|
|
|
// Detects the underlying OS and runtime.
|
|
|
|
internal static void Init(ToolkitOptions options)
|
2010-12-08 09:58:52 +01:00
|
|
|
{
|
|
|
|
lock (InitLock)
|
|
|
|
{
|
|
|
|
if (!initialized)
|
|
|
|
{
|
2013-11-12 20:32:31 +01:00
|
|
|
#if ANDROID
|
|
|
|
runningOnMono = true;
|
|
|
|
runningOnAnroid = true;
|
|
|
|
#elif IPHONE
|
|
|
|
runningOnMono = true;
|
|
|
|
runningOnIOS = true;
|
|
|
|
#else
|
|
|
|
runningOnMono = DetectMono();
|
|
|
|
runningOnWindows = DetectWindows();
|
|
|
|
if (!runningOnWindows)
|
2010-12-08 09:58:52 +01:00
|
|
|
{
|
2013-11-12 20:32:31 +01:00
|
|
|
DetectUnix(out runningOnUnix, out runningOnLinux, out runningOnMacOS);
|
2010-12-08 09:58:52 +01:00
|
|
|
}
|
2013-11-12 20:32:31 +01:00
|
|
|
|
|
|
|
if (options.Backend == PlatformBackend.Default)
|
2013-09-27 14:41:37 +02:00
|
|
|
{
|
2013-11-12 20:32:31 +01:00
|
|
|
RunningOnSdl2 = DetectSdl2();
|
2013-09-27 14:41:37 +02:00
|
|
|
}
|
2013-12-08 12:38:06 +01:00
|
|
|
|
2014-04-27 22:41:24 +02:00
|
|
|
if ((runningOnLinux && !RunningOnSdl2) || options.Backend == PlatformBackend.PreferX11)
|
2010-12-08 09:58:52 +01:00
|
|
|
{
|
2013-11-12 20:32:31 +01:00
|
|
|
runningOnX11 = DetectX11();
|
2010-12-08 09:58:52 +01:00
|
|
|
}
|
|
|
|
|
2013-11-12 20:32:31 +01:00
|
|
|
initialized = true;
|
|
|
|
#endif
|
2010-12-08 09:58:52 +01:00
|
|
|
Debug.Print("Detected configuration: {0} / {1}",
|
|
|
|
RunningOnWindows ? "Windows" : RunningOnLinux ? "Linux" : RunningOnMacOS ? "MacOS" :
|
|
|
|
runningOnUnix ? "Unix" : RunningOnX11 ? "X11" : "Unknown Platform",
|
|
|
|
RunningOnMono ? "Mono" : ".Net");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
}
|