-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathPlatform.java
More file actions
110 lines (88 loc) · 3.84 KB
/
Platform.java
File metadata and controls
110 lines (88 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package net.vulkanmod.config;
import org.apache.commons.lang3.SystemUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.system.Configuration;
import static net.vulkanmod.Initializer.LOGGER;
import static org.lwjgl.glfw.GLFW.*;
public abstract class Platform {
private static final int activePlat = getSupportedPlat();
private static final String activeDE = determineDE();
public static final boolean skipWaylandPatches = shouldSkipWaylandPatches();
public static void init() {
// Increase stack size to 256 KB to prevent out of stack error on nvidia driver
Configuration.STACK_SIZE.set(256);
GLFW.glfwInitHint(GLFW_PLATFORM, activePlat);
LOGGER.info("Selecting Platform: {}", getStringFromPlat(activePlat));
LOGGER.info("GLFW: {}", GLFW.glfwGetVersionString());
GLFW.glfwInit();
}
//Actually detect the currently active Display Server (if both Wayland and X11 are present on the system and/or GLFW is compiled to support both)
private static int determineDisplayServer() {
//Return Null platform if not on Linux (i.e. no X11 or Wayland)
String xdgSessionType = System.getenv("XDG_SESSION_TYPE");
if (xdgSessionType == null) return GLFW_ANY_PLATFORM; //Likely Android
return switch (xdgSessionType) {
case "wayland" -> GLFW_PLATFORM_WAYLAND; //Wayland
case "x11" -> GLFW_PLATFORM_X11; //X11
default -> GLFW_ANY_PLATFORM; //Either unknown Platform or Display Server
};
}
private static int getSupportedPlat() {
//Switch statement would be ideal, but couldn't find a good way of implementing it, so fell back to basic if statements/branches
if (SystemUtils.IS_OS_WINDOWS) return GLFW_PLATFORM_WIN32;
if (SystemUtils.IS_OS_MAC_OSX) return GLFW_PLATFORM_COCOA;
if (SystemUtils.IS_OS_LINUX) return determineDisplayServer(); //Linux Or Android
return GLFW_ANY_PLATFORM; //Unknown platform
}
private static String getStringFromPlat(int plat) {
return switch (plat) {
case GLFW_PLATFORM_WIN32 -> "WIN32";
case GLFW_PLATFORM_WAYLAND -> "WAYLAND";
case GLFW_PLATFORM_X11 -> "X11";
case GLFW_PLATFORM_COCOA -> "MACOS";
case GLFW_ANY_PLATFORM -> "ANDROID";
default -> throw new IllegalStateException("Unexpected value: " + plat);
};
}
private static String determineDE() {
String xdgSessionDesktop = System.getenv("XDG_SESSION_DESKTOP");
String xdgCurrentDesktop = System.getenv("XDG_CURRENT_DESKTOP");
if (xdgSessionDesktop != null)
return xdgSessionDesktop.toLowerCase();
if (xdgCurrentDesktop != null)
return xdgCurrentDesktop.toLowerCase();
return "N/A";
}
private static boolean shouldSkipWaylandPatches() {
return System.getProperties().containsKey("vkmod.wayland.compat.skip");
}
public static int getActivePlat() {
return activePlat;
}
//Allows platform specific checks to be handled
public static boolean isWayLand() {
return activePlat == GLFW_PLATFORM_WAYLAND;
}
public static boolean isX11() {
return activePlat == GLFW_PLATFORM_X11;
}
public static boolean isWindows() {
return activePlat == GLFW_PLATFORM_WIN32;
}
public static boolean isMacOS() {
return activePlat == GLFW_PLATFORM_COCOA;
}
public static boolean isAndroid() {
return activePlat == GLFW_ANY_PLATFORM;
}
//Desktop Environment Names: https://wiki.archlinux.org/title/Xdg-utils#Usage
public static boolean isGnome() {
return activeDE.contains("gnome");
}
public static boolean isWeston() {
return activeDE.contains("weston");
}
public static boolean isGeneric() {
return activeDE.contains("generic");
}
}