001package net.gdface.utils;
002
003/**
004 * copy from jna-4.1.0.jar com.sun.jna.Platform but 'is64Bit' method
005 * @author guyadong
006 *
007 */
008public class Platform {
009    public static final int UNSPECIFIED = -1;
010    public static final int MAC = 0;
011    public static final int LINUX = 1;
012    public static final int WINDOWS = 2;
013    public static final int SOLARIS = 3;
014    public static final int FREEBSD = 4;
015    public static final int OPENBSD = 5;
016    public static final int WINDOWSCE = 6;
017    public static final int AIX = 7;
018    public static final int ANDROID = 8;
019    public static final int GNU = 9;
020    public static final int KFREEBSD = 10;
021    public static final int NETBSD = 11;
022
023    /** Whether read-only (final) fields within Structures are supported. */
024    public static final boolean RO_FIELDS;
025    /** Whether this platform provides NIO Buffers. */
026    public static final boolean HAS_BUFFERS;
027    /** Whether this platform provides the AWT Component class; also false if
028     * running headless.
029     */
030    public static final boolean HAS_AWT;
031    /** Canonical name of this platform's math library. */
032    public static final String MATH_LIBRARY_NAME;
033    /** Canonical name of this platform's C runtime library. */
034    public static final String C_LIBRARY_NAME;
035    /** Whether in-DLL callbacks are supported. */
036    public static final boolean HAS_DLL_CALLBACKS;
037    /** Canonical resource prefix for the current platform.  This value is
038     * used to load bundled native libraries from the class path.
039     */
040    public static final String RESOURCE_PREFIX;
041
042    private static final int osType;
043    /** Current platform architecture. */
044    public static final String ARCH;
045
046    static {
047        String osName = System.getProperty("os.name");
048        if (osName.startsWith("Linux")) {
049            if ("dalvik".equals(System.getProperty("java.vm.name").toLowerCase())) {
050                osType = ANDROID;
051                // Native libraries on android must be bundled with the APK
052                System.setProperty("jna.nounpack", "true");
053            }
054            else {
055                osType = LINUX;
056            }
057        }
058        else if (osName.startsWith("AIX")) {
059            osType = AIX;
060        }
061        else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) {
062            osType = MAC;
063        }
064        else if (osName.startsWith("Windows CE")) {
065            osType = WINDOWSCE;
066        }
067        else if (osName.startsWith("Windows")) {
068            osType = WINDOWS;
069        }
070        else if (osName.startsWith("Solaris") || osName.startsWith("SunOS")) {
071            osType = SOLARIS;
072        }
073        else if (osName.startsWith("FreeBSD")) {
074            osType = FREEBSD;
075        }
076        else if (osName.startsWith("OpenBSD")) {
077            osType = OPENBSD;
078        }
079        else if (osName.equalsIgnoreCase("gnu")) {
080            osType = GNU;
081        }
082        else if (osName.equalsIgnoreCase("gnu/kfreebsd")) {
083            osType = KFREEBSD;
084        }
085        else if (osName.equalsIgnoreCase("netbsd")) {
086            osType = NETBSD;
087        }
088        else {
089            osType = UNSPECIFIED;
090        }
091        boolean hasBuffers = false;
092        try {
093            Class.forName("java.nio.Buffer");
094            hasBuffers = true;
095        }
096        catch(ClassNotFoundException e) {
097        }
098        // NOTE: we used to do Class.forName("java.awt.Component"), but that
099        // has the unintended side effect of actually loading AWT native libs,
100        // which can be problematic
101        HAS_AWT = osType != WINDOWSCE && osType != ANDROID && osType != AIX;
102        HAS_BUFFERS = hasBuffers;
103        RO_FIELDS = osType != WINDOWSCE;
104        C_LIBRARY_NAME = osType == WINDOWS ? "msvcrt" : osType == WINDOWSCE ? "coredll" : "c";
105        MATH_LIBRARY_NAME = osType == WINDOWS ? "msvcrt" : osType == WINDOWSCE ? "coredll" : "m";
106        HAS_DLL_CALLBACKS = osType == WINDOWS;
107        RESOURCE_PREFIX = getNativeLibraryResourcePrefix();
108        ARCH = System.getProperty("os.arch").toLowerCase().trim();
109    }
110    private Platform() { }
111    public static final int getOSType() {
112        return osType;
113    }
114    public static final boolean isMac() {
115        return osType == MAC;
116    }
117    public static final boolean isAndroid() {
118        return osType == ANDROID;
119    }
120    public static final boolean isLinux() {
121        return osType == LINUX;
122    }
123    public static final boolean isAIX() {
124        return osType == AIX;
125    }
126    /** @deprecated */
127    public static final boolean isAix() {
128        return isAIX();
129    }
130    public static final boolean isWindowsCE() {
131        return osType == WINDOWSCE;
132    }
133    /** Returns true for any windows variant. */
134    public static final boolean isWindows() {
135        return osType == WINDOWS || osType == WINDOWSCE;
136    }
137    public static final boolean isSolaris() {
138        return osType == SOLARIS;
139    }
140    public static final boolean isFreeBSD() {
141        return osType == FREEBSD;
142    }
143    public static final boolean isOpenBSD() {
144        return osType == OPENBSD;
145    }
146    public static final boolean isNetBSD() {
147        return osType == NETBSD;
148    }
149    public static final boolean isGNU() {
150        return osType == GNU;
151    }
152    public static final boolean iskFreeBSD() {
153        return osType == KFREEBSD;
154    }
155    public static final boolean isX11() {
156        // TODO: check filesystem for /usr/X11 or some other X11-specific test
157        return !Platform.isWindows() && !Platform.isMac();
158    }
159    public static final boolean hasRuntimeExec() {
160        if (isWindowsCE() && "J9".equals(System.getProperty("java.vm.name")))
161            return false;
162        return true;
163    }
164    public static final boolean isIntel() {
165        if (ARCH.equals("i386")
166            || ARCH.startsWith("i686")
167            || ARCH.equals("x86")
168            || ARCH.equals("x86_64")
169            || ARCH.equals("amd64")) {
170            return true;
171        } 
172        return false;
173    }
174
175    public static final boolean isPPC() {
176        if (ARCH.equals("ppc")
177            || ARCH.equals("ppc64")
178            || ARCH.equals("powerpc")
179            || ARCH.equals("powerpc64")) {
180            return true;
181        } 
182        return false;
183    }
184
185    public static final boolean isARM() {
186        return ARCH.startsWith("arm");
187    }
188
189    public static final boolean isSPARC() {
190        return ARCH.startsWith("sparc");
191    }
192
193    /** Generate a canonical String prefix based on the current OS 
194        type/arch/name.
195    */
196    public static String getNativeLibraryResourcePrefix() {
197        return getNativeLibraryResourcePrefix(getOSType(), System.getProperty("os.arch"), System.getProperty("os.name"));
198    }
199
200    /** Generate a canonical String prefix based on the given OS
201        type/arch/name.
202        @param osType from {@link #getOSType()}
203        @param arch from <code>os.arch</code> System property
204        @param name from <code>os.name</code> System property
205    */
206    static String getNativeLibraryResourcePrefix(int osType, String arch, String name) {
207        String osPrefix;
208        arch = arch.toLowerCase().trim();
209        if ("powerpc".equals(arch)) {
210            arch = "ppc";
211        }
212        else if ("powerpc64".equals(arch)) {
213            arch = "ppc64";
214        }
215        else if ("i386".equals(arch)) {
216            arch = "x86";
217        }
218        else if ("x86_64".equals(arch) || "amd64".equals(arch)) {
219            arch = "x86-64";
220        }
221        switch(osType) {
222        case Platform.ANDROID:
223            if (arch.startsWith("arm")) {
224                arch = "arm";
225            }
226            osPrefix = "android-" + arch;
227            break;
228        case Platform.WINDOWS:
229            osPrefix = "win32-" + arch;
230            break;
231        case Platform.WINDOWSCE:
232            osPrefix = "w32ce-" + arch;
233            break;
234        case Platform.MAC:
235            osPrefix = "darwin";
236            break;
237        case Platform.LINUX:
238            osPrefix = "linux-" + arch;
239            break;
240        case Platform.SOLARIS:
241            osPrefix = "sunos-" + arch;
242            break;
243        case Platform.FREEBSD:
244            osPrefix = "freebsd-" + arch;
245            break;
246        case Platform.OPENBSD:
247            osPrefix = "openbsd-" + arch;
248            break;
249        case Platform.NETBSD:
250            osPrefix = "netbsd-" + arch;
251            break;
252        case Platform.KFREEBSD:
253            osPrefix = "kfreebsd-" + arch;
254            break;
255        default:
256            osPrefix = name.toLowerCase();
257            int space = osPrefix.indexOf(" ");
258            if (space != -1) {
259                osPrefix = osPrefix.substring(0, space);
260            }
261            osPrefix += "-" + arch;
262            break;
263        }
264        return osPrefix;
265    }
266}