Moved OS getting utils to its own Platform enum.

This commit is contained in:
coolGi
2023-03-26 16:03:02 +10:30
parent cb88a48d78
commit f4c1c9ebbf
4 changed files with 119 additions and 16 deletions
@@ -1,6 +1,6 @@
package com.seibel.lod.core.config.gui;
import org.lwjgl.system.*;
import com.seibel.lod.core.jar.Platform;
import org.lwjgl.system.jawt.JAWT;
import org.lwjgl.system.macosx.*;
@@ -18,6 +18,12 @@ import static org.lwjgl.system.macosx.ObjCRuntime.*;
// Some of the code is from https://github.com/LWJGL/lwjgl3/blob/master/modules/samples/src/test/java/org/lwjgl/demo/system/jawt/EmbeddedFrameUtil.java
// which is licensed under https://www.lwjgl.org/license
/**
* Some utils for embeding awt and swing items into lwjgl windows
*
* @author Ran
* @author coolGi
*/
public final class EmbeddedFrameUtil {
private static final int JAVA_VERSION;
@@ -45,10 +51,10 @@ public final class EmbeddedFrameUtil {
switch (Platform.get()) {
case LINUX:
return "sun.awt.X11.XEmbeddedFrame";
case MACOSX:
return "sun.lwawt.macosx.CViewEmbeddedFrame";
case WINDOWS:
return "sun.awt.windows.WEmbeddedFrame";
case MACOS:
return "sun.lwawt.macosx.CViewEmbeddedFrame";
default:
throw new IllegalStateException();
}
@@ -58,11 +64,11 @@ public final class EmbeddedFrameUtil {
switch (Platform.get()) {
case LINUX:
return glfwGetX11Window(window);
case MACOSX:
long objc_msgSend = ObjCRuntime.getLibrary().getFunctionAddress("objc_msgSend");
return invokePPP(glfwGetCocoaWindow(window), sel_getUid("contentView"), objc_msgSend);
case WINDOWS:
return glfwGetWin32Window(window);
case MACOS:
long objc_msgSend = ObjCRuntime.getLibrary().getFunctionAddress("objc_msgSend");
return invokePPP(glfwGetCocoaWindow(window), sel_getUid("contentView"), objc_msgSend);
default:
throw new IllegalStateException();
}
@@ -19,12 +19,15 @@ public class DarkModeDetector {
private static final String DARK_THEME_CMD = REGQUERY_UTIL + "\"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize\"" + " /v AppsUseLightTheme";
public static boolean isDarkMode() {
switch (JarUtils.getOperatingSystem()) {
switch (Platform.get()) {
case WINDOWS:
return isWindowsDarkMode();
case MACOS:
return isMacOsDarkMode();
case LINUX:
// Most Unix(-like) distros also use a lot of the same things as Linux (like desktop environments and window managers)
case BSD:
case UNIX:
return checkLinuxDark();
default:
return false;
@@ -105,17 +105,17 @@ public class JarUtils {
}
/** Please use the Platform enum instead */
@Deprecated
public enum OperatingSystem {WINDOWS, MACOS, LINUX, NONE} // Easy to use enum for the 3 main os's
/** Please use the Platform enum instead */
@Deprecated
public static OperatingSystem getOperatingSystem() { // Get the os and turn it into that enum
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
return OperatingSystem.WINDOWS;
} else if (os.contains("mac")) {
return OperatingSystem.MACOS;
} else if (os.contains("nix") || os.contains("nux")) {
return OperatingSystem.LINUX;
} else {
return OperatingSystem.NONE;
switch (Platform.get()) {
case WINDOWS: return OperatingSystem.WINDOWS;
case LINUX: return OperatingSystem.LINUX;
case MACOS: return OperatingSystem.MACOS;
default: return OperatingSystem.NONE;
}
}
}
@@ -0,0 +1,94 @@
package com.seibel.lod.core.jar;
/**
* A simple OS getting util based off LWJGL's Platform at org.lwjgl.system.Platform. <br>
* This version includes some extra utils that we need and removes stuff which we don't.
*
* @author coolGi
*/
public enum Platform {
WINDOWS("Windows", false),
LINUX("Linux", true),
MACOS("macOS", true),
BSD("BSD", true),
UNIX("Unix", true);
public enum Architecture {
X86(false),
X64(true),
ARM32(false),
ARM64(true);
static final Architecture current;
final boolean is64Bit;
static {
String osArch = System.getProperty("os.arch");
boolean is64Bit = osArch.contains("64") || osArch.startsWith("armv8");
current = osArch.startsWith("arm") || osArch.startsWith("aarch64")
? (is64Bit ? ARM64 : ARM32)
: (is64Bit ? X64 : X86);
}
Architecture(boolean is64Bit) {
this.is64Bit = is64Bit;
}
}
private static final Platform current;
static {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("windows")) { // Proper name is "Windows"
current = WINDOWS;
} else if (osName.contains("linux")) { // Proper name is "Linux"
current = LINUX;
} else if (osName.contains("mac") || osName.contains("darwin")) { // For MacOS it should either output "Mac OS X" or "Darwin" depending on the version of MacOS
current = MACOS;
} else if (osName.startsWith("bsd")) { // Depending on the BSD distro this will be different
current = BSD;
} else if (osName.startsWith("unix")) { // In case you are running some very niece OS which didnt change their OS name from Unix
current = UNIX;
} else {
// We only test on Windows, Linux and MacOS
// If you use a different os (eg, BSD, Unix) then it may or may not work
// Otherwise, good luck
throw new LinkageError("Unknown platform: " + osName);
}
}
private final String name;
private final boolean isUnix;
Platform(String name, boolean isUnix) {
this.name = name;
this.isUnix = isUnix;
}
/** Returns the platform name. */
public String getName() {
return name;
}
/** Returns weather the OS is Unix or Unix-Like OS */
public boolean isUnix() {
return isUnix;
}
/** Returns the platform on which the library is running. */
public static Platform get() {
return current;
}
/** Returns the architecture on which the library is running. */
public static Architecture getArchitecture() {
return Architecture.current;
}
@Override
public String toString() {
return this.getName();
}
}