[hyena] more extensive probing



commit 855eec7a993ad2de672ee9ab35f38d47ea49aa79
Author: Aaron Bockover <abockover novell com>
Date:   Sun Feb 7 12:52:56 2010 -0500

    more extensive probing
    
    Renamed PlatformUtil to PlatformDetection, and implemented more
    extensive probing. Detects Windows, Linux, Unix, and Mac OS X,
    and will detect environments like Moblin, GNOME, etc. later.

 src/Hyena/Hyena.csproj               |    2 +-
 src/Hyena/Hyena/PlatformDetection.cs |   90 ++++++++++++++++++++++++++++++++++
 src/Hyena/Hyena/PlatformUtil.cs      |   45 -----------------
 src/Hyena/Makefile.am                |    2 +-
 4 files changed, 92 insertions(+), 47 deletions(-)
---
diff --git a/src/Hyena/Hyena.csproj b/src/Hyena/Hyena.csproj
index b9f6ba1..b18081c 100644
--- a/src/Hyena/Hyena.csproj
+++ b/src/Hyena/Hyena.csproj
@@ -105,7 +105,6 @@
     <Compile Include="Hyena.Query\QueryOrder.cs" />
     <Compile Include="Hyena\Log.cs" />
     <Compile Include="Hyena\CryptoUtil.cs" />
-    <Compile Include="Hyena\PlatformUtil.cs" />
     <Compile Include="Hyena.Query\IntegerKeyedObjectQueryValue.cs" />
     <Compile Include="Hyena\ConsoleCrayon.cs" />
     <Compile Include="Hyena.Data\ISelectable.cs" />
@@ -151,6 +150,7 @@
     <Compile Include="Hyena/Hyena/XdgBaseDirectorySpec.cs" />
     <Compile Include="System.Web\Helpers.cs" />
     <Compile Include="System.Web\HttpUtility.cs" />
+    <Compile Include="Hyena\PlatformDetection.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Mono.Posix" />
diff --git a/src/Hyena/Hyena/PlatformDetection.cs b/src/Hyena/Hyena/PlatformDetection.cs
new file mode 100644
index 0000000..42418ee
--- /dev/null
+++ b/src/Hyena/Hyena/PlatformDetection.cs
@@ -0,0 +1,90 @@
+//
+// PlatformUtil.cs
+//
+// Author:
+//   Aaron Bockover <abockover novell com>
+//
+// Copyright 2010 Novell, Inc.
+//
+// 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.
+//
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace Hyena
+{
+    public static class PlatformDetection
+    {
+        public static readonly bool IsMac;
+        public static readonly bool IsWindows;
+        public static readonly bool IsLinux;
+        public static readonly bool IsUnix;
+        public static readonly bool IsPosix;
+        public static readonly bool IsMoblin;
+
+        public static readonly string PosixSystemName;
+
+        [DllImport ("libc")]
+        private static extern int uname (IntPtr utsname);
+
+        static PlatformDetection ()
+        {
+            // From http://www.mono-project.com/FAQ:_Technical
+            int p = (int)Environment.OSVersion.Platform;
+            IsUnix = p == 4 || p == 6 || p == 128;
+            IsWindows = p < 4;
+
+            if (IsWindows) {
+                return;
+            }
+
+            // uname expects a pointer to a utsname structure, but we are
+            // tricky here - this structure's first field is the field we
+            // care about (char sysname []); the size of the structure is
+            // unknown, as it varies on all platforms. Darwin uses only
+            // the five POSIX fields, each 256 bytes, so the total size is
+            // total size is 5 * 256 = 1280 bytes. Arbitrarily using 8192.
+            var utsname = IntPtr.Zero;
+            try {
+                utsname = Marshal.AllocHGlobal (8192);
+                if (uname (utsname) == 0) {
+                    PosixSystemName = Marshal.PtrToStringAnsi (utsname);
+                }
+            } catch {
+            } finally {
+                if (utsname != IntPtr.Zero) {
+                    Marshal.FreeHGlobal (utsname);
+                }
+            }
+
+            if (PosixSystemName == null) {
+                return;
+            }
+
+            switch (PosixSystemName) {
+                case "Darwin": IsMac = true; break;
+                case "Linux": IsLinux = true; break;
+            }
+
+            // FIXME: probe the root X11 window for Moblin properties
+        }
+    }
+}
diff --git a/src/Hyena/Makefile.am b/src/Hyena/Makefile.am
index 0217eb9..0c2a464 100644
--- a/src/Hyena/Makefile.am
+++ b/src/Hyena/Makefile.am
@@ -67,7 +67,7 @@ SOURCES =  \
 	Hyena.Query/QueryOrder.cs \
 	Hyena/Log.cs \
 	Hyena/CryptoUtil.cs \
-	Hyena/PlatformUtil.cs \
+	Hyena/PlatformDetection.cs \
 	Hyena.Query/IntegerKeyedObjectQueryValue.cs \
 	Hyena/ConsoleCrayon.cs \
 	Hyena.Data/ISelectable.cs \



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]