Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1452 chris 1
#region License
2
//
3
// The Open Toolkit Library License
4
//
5
// Copyright (c) 2006 - 2009 the Open Toolkit library.
6
//
7
// Permission is hereby granted, free of charge, to any person obtaining a copy
8
// of this software and associated documentation files (the "Software"), to deal
9
// in the Software without restriction, including without limitation the rights to 
10
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
// the Software, and to permit persons to whom the Software is furnished to do
12
// so, subject to the following conditions:
13
//
14
// The above copyright notice and this permission notice shall be included in all
15
// copies or substantial portions of the Software.
16
//
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
// OTHER DEALINGS IN THE SOFTWARE.
25
//
26
#endregion
27
 
28
using System;
29
using System.Collections.Generic;
30
using System.Text;
31
using System.Diagnostics;
32
using System.IO;
33
using System.Runtime.InteropServices;
34
using System.Reflection;
35
 
36
namespace OpenTK
37
{
38
    /// <summary>Provides information about the underlying OS and runtime.</summary>
39
    public static class Configuration
40
    {
41
        static bool runningOnWindows, runningOnUnix, runningOnX11, runningOnMacOS, runningOnLinux, runningOnMono;
42
 
43
        #region --- Constructors ---
44
 
45
        // Detects the underlying OS and runtime.
46
        static Configuration()
47
        {
48
            if (System.Environment.OSVersion.Platform == PlatformID.Win32NT ||
49
                System.Environment.OSVersion.Platform == PlatformID.Win32S ||
50
                System.Environment.OSVersion.Platform == PlatformID.Win32Windows ||
51
                System.Environment.OSVersion.Platform == PlatformID.WinCE)
52
                runningOnWindows = true;
53
            else if (System.Environment.OSVersion.Platform == PlatformID.Unix ||
54
                     System.Environment.OSVersion.Platform == (PlatformID)4)
55
            {
56
                // Distinguish between Linux, Mac OS X and other Unix operating systems.
57
                string kernel_name = DetectUnixKernel();
58
                switch (kernel_name)
59
                {
60
                    case null:
61
                    case "":
62
                        throw new PlatformNotSupportedException(
63
                            "Unknown platform. Please file a bug report at http://www.opentk.com/node/add/project-issue/opentk");
64
 
65
                    case "Linux":
66
                        runningOnLinux = runningOnUnix = true;
67
                        break;
68
 
69
                    case "Darwin":
70
                        runningOnMacOS = runningOnUnix = true;
71
                        break;
72
 
73
                    default:
74
                        runningOnUnix = true;
75
                        break;
76
                }
77
            }
78
            else
79
                throw new PlatformNotSupportedException("Unknown platform. Please report this error at http://www.opentk.com.");
80
 
81
            // Detect whether X is present.
82
            // Hack: it seems that this check will cause X to initialize itself on Mac OS X Leopard and newer.
83
            // We don't want that (we'll be using the native interfaces anyway), so we'll avoid this check
84
            // when we detect Mac OS X.
85
            if (!RunningOnMacOS)
86
            {
87
                try { runningOnX11 = OpenTK.Platform.X11.API.DefaultDisplay != IntPtr.Zero; }
88
                catch { }
89
            }
90
 
91
            // Detect the Mono runtime (code taken from http://mono.wikia.com/wiki/Detecting_if_program_is_running_in_Mono).
92
            Type t = Type.GetType("Mono.Runtime");
93
            if (t != null)
94
                runningOnMono = true;
95
 
96
            Debug.Print("Detected configuration: {0} / {1}",
97
                RunningOnWindows ? "Windows" : RunningOnLinux ? "Linux" : RunningOnMacOS ? "MacOS" :
98
                runningOnUnix ? "Unix" : RunningOnX11 ? "X11" : "Unknown Platform",
99
                RunningOnMono ? "Mono" : ".Net");
100
        }
101
 
102
        #endregion
103
 
104
        #region --- Public Methods ---
105
 
106
        #region public static bool RunningOnWindows
107
 
108
        /// <summary>Gets a System.Boolean indicating whether OpenTK is running on a Windows platform.</summary>
109
        public static bool RunningOnWindows { get { return runningOnWindows; } }
110
 
111
        #endregion
112
 
113
        #region public static bool RunningOnX11
114
 
115
        /// <summary>Gets a System.Boolean indicating whether OpenTK is running on an X11 platform.</summary>
116
        public static bool RunningOnX11
117
        {
118
            get { return runningOnX11; }
119
        }
120
 
121
        /// <summary>
122
        /// Gets a <see cref="System.Boolean"/> indicating whether OpenTK is running on a Unix platform.
123
        /// </summary>
124
        public static bool RunningOnUnix
125
        {
126
            get { return runningOnUnix; }
127
        }
128
 
129
        #endregion
130
 
131
        #region public static bool RunningOnLinux
132
 
133
        /// <summary>Gets a System.Boolean indicating whether OpenTK is running on an X11 platform.</summary>
134
        public static bool RunningOnLinux { get { return runningOnLinux; } }
135
 
136
        #endregion
137
 
138
        #region public static bool RunningOnMacOS
139
 
140
        /// <summary>Gets a System.Boolean indicating whether OpenTK is running on a MacOS platform.</summary>
141
        public static bool RunningOnMacOS { get { return runningOnMacOS; } }
142
 
143
        #endregion
144
 
145
        #region public static bool RunningOnMono
146
 
147
        /// <summary>
148
        /// Gets a System.Boolean indicating whether OpenTK is running on the Mono runtime.
149
        /// </summary>
150
        public static bool RunningOnMono { get { return runningOnMono; } }
151
 
152
        #endregion
153
 
154
        #region --- Private Methods ---
155
 
156
        #region private static string DetectUnixKernel()
157
 
158
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
159
        struct utsname
160
        {
161
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
162
            public string sysname;
163
 
164
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
165
            public string nodename;
166
 
167
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
168
            public string release;
169
 
170
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
171
            public string version;
172
 
173
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
174
            public string machine;
175
 
176
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
177
            public string extraJustInCase;
178
 
179
        }
180
 
181
        /// <summary>
182
        /// Detects the unix kernel by p/invoking uname (libc).
183
        /// </summary>
184
        /// <returns></returns>
185
        private static string DetectUnixKernel()
186
        {
187
            Debug.Print("Size: {0}", Marshal.SizeOf(typeof(utsname)).ToString());
188
            Debug.Flush();
189
            utsname uts = new utsname();
190
            uname(out uts);
191
 
192
            Debug.WriteLine("System:");
193
            Debug.Indent();
194
            Debug.WriteLine(uts.sysname);
195
            Debug.WriteLine(uts.nodename);
196
            Debug.WriteLine(uts.release);
197
            Debug.WriteLine(uts.version);
198
            Debug.WriteLine(uts.machine);
199
            Debug.Unindent();
200
 
201
            return uts.sysname.ToString();
202
        }
203
 
204
        [DllImport("libc")]
205
        private static extern void uname(out utsname uname_struct);
206
 
207
        #endregion
208
 
209
        #endregion
210
 
211
        #endregion
212
    }
213
}