Skip to content

Commit 8649ac2

Browse files
committed
Add option for Wayland compositor
1 parent a5623ca commit 8649ac2

7 files changed

Lines changed: 73 additions & 31 deletions

File tree

src/main/java/net/vulkanmod/config/Config.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
5-
import net.vulkanmod.config.video.VideoModeManager;
65
import net.vulkanmod.config.video.VideoModeSet;
76

87
import java.io.FileReader;
@@ -13,7 +12,7 @@
1312
import java.util.Collections;
1413

1514
public class Config {
16-
public VideoModeSet.VideoMode videoMode = VideoModeManager.getFirstAvailable().getVideoMode();
15+
public VideoModeSet.VideoMode videoMode = VideoModeSet.getDummy().getVideoMode();
1716
public int windowMode = 0;
1817

1918
public int advCulling = 2;
@@ -22,6 +21,7 @@ public class Config {
2221
public boolean uniqueOpaqueLayer = true;
2322
public boolean entityCulling = true;
2423
public int device = -1;
24+
public boolean useWayland = false;
2525

2626
public int ambientOcclusion = 1;
2727
public int frameQueueSize = 2;

src/main/java/net/vulkanmod/config/Platform.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
package net.vulkanmod.config;
22

3+
import net.minecraft.Util;
34
import org.apache.commons.lang3.SystemUtils;
45
import org.lwjgl.glfw.GLFW;
5-
import org.lwjgl.system.Configuration;
66

7+
import static net.vulkanmod.Initializer.CONFIG;
78
import static net.vulkanmod.Initializer.LOGGER;
89
import static org.lwjgl.glfw.GLFW.*;
910

1011
public abstract class Platform {
11-
private static final int activePlat = getSupportedPlat();
12-
private static final String activeDE = determineDE();
12+
public static final Util.OS OS = Util.getPlatform();
13+
private static int activePlat;
14+
private static String activeDE;
1315

1416
public static void init() {
17+
activePlat = getSupportedPlat();
18+
activeDE = determineDE();
19+
1520
GLFW.glfwInitHint(GLFW_PLATFORM, activePlat);
1621
LOGGER.info("Selecting Platform: {}", getStringFromPlat());
1722
LOGGER.info("GLFW: {}", GLFW.glfwGetVersionString());
1823
GLFW.glfwInit();
1924
}
2025

21-
//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)
22-
private static int determineDisplayServer() {
23-
24-
//Return Null platform if not on Linux (i.e. no X11 or Wayland)
26+
// 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)
27+
private static int determineLinuxDisplayServer() {
28+
// Return Null platform if not on Linux (i.e. no X11 or Wayland)
2529
String xdgSessionType = System.getenv("XDG_SESSION_TYPE");
26-
if (xdgSessionType == null) return GLFW_ANY_PLATFORM; //Likely Android
30+
if (xdgSessionType == null) return GLFW_ANY_PLATFORM; // Likely Android
2731
return switch (xdgSessionType) {
28-
case "wayland" -> GLFW_PLATFORM_WAYLAND; //Wayland
29-
case "x11" -> GLFW_PLATFORM_X11; //X11
32+
case "wayland" -> {
33+
if (CONFIG.useWayland) {
34+
yield GLFW_PLATFORM_WAYLAND;
35+
}
36+
else {
37+
yield GLFW_PLATFORM_X11;
38+
}
39+
}
40+
case "x11" -> GLFW_PLATFORM_X11;
3041
default -> GLFW_ANY_PLATFORM; //Either unknown Platform or Display Server
3142
};
3243
}
@@ -35,7 +46,7 @@ private static int getSupportedPlat() {
3546
//Switch statement would be ideal, but couldn't find a good way of implementing it, so fell back to basic if statements/branches
3647
if (SystemUtils.IS_OS_WINDOWS) return GLFW_PLATFORM_WIN32;
3748
if (SystemUtils.IS_OS_MAC_OSX) return GLFW_PLATFORM_COCOA;
38-
if (SystemUtils.IS_OS_LINUX) return determineDisplayServer(); //Linux Or Android
49+
if (SystemUtils.IS_OS_LINUX) return determineLinuxDisplayServer(); //Linux Or Android
3950

4051
return GLFW_ANY_PLATFORM; //Unknown platform
4152
}
@@ -61,10 +72,26 @@ private static String determineDE() {
6172
return "N/A";
6273
}
6374

75+
public static boolean isWindows() {
76+
return OS == Util.OS.LINUX;
77+
}
78+
79+
public static boolean isLinux() {
80+
return OS == Util.OS.LINUX;
81+
}
82+
83+
public static boolean isMacOS() {
84+
return OS == Util.OS.OSX;
85+
}
86+
6487
public static int getActivePlat() {
6588
return activePlat;
6689
}
6790

91+
public static boolean isAndroid() {
92+
return activePlat == GLFW_ANY_PLATFORM;
93+
}
94+
6895
//Allows platform specific checks to be handled
6996
public static boolean isWayLand() {
7097
return activePlat == GLFW_PLATFORM_WAYLAND;
@@ -74,18 +101,6 @@ public static boolean isX11() {
74101
return activePlat == GLFW_PLATFORM_X11;
75102
}
76103

77-
public static boolean isWindows() {
78-
return activePlat == GLFW_PLATFORM_WIN32;
79-
}
80-
81-
public static boolean isMacOS() {
82-
return activePlat == GLFW_PLATFORM_COCOA;
83-
}
84-
85-
public static boolean isAndroid() {
86-
return activePlat == GLFW_ANY_PLATFORM;
87-
}
88-
89104
//Desktop Environment Names: https://wiki.archlinux.org/title/Xdg-utils#Usage
90105
public static boolean isGnome() {
91106
return activeDE.contains("gnome");

src/main/java/net/vulkanmod/config/option/Options.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.minecraft.server.level.ParticleStatus;
77
import net.vulkanmod.Initializer;
88
import net.vulkanmod.config.Config;
9+
import net.vulkanmod.config.Platform;
910
import net.vulkanmod.config.gui.*;
1011
import net.vulkanmod.config.video.*;
1112
import net.vulkanmod.render.chunk.WorldRenderer;
@@ -58,6 +59,7 @@ public static List<OptionPage> getOptionPages() {
5859
}
5960

6061
public static OptionBlock[] getVideoOpts() {
62+
VideoModeManager.checkConfigVideoMode(config);
6163
VideoModeManager.selectBestMonitor(window);
6264
var resolutions = VideoModeManager.getVideoResolutions();
6365

@@ -374,6 +376,11 @@ public static OptionBlock[] getOtherOpts() {
374376
() -> config.textureAnimations)
375377
}),
376378
new OptionBlock("", new Option<?>[]{
379+
new SwitchOption(Component.translatable("vulkanmod.options.wayland"),
380+
v -> config.useWayland = v,
381+
() -> config.useWayland)
382+
.setActivationFn(Platform::isLinux)
383+
.setTooltip(v -> Component.translatable("vulkanmod.options.wayland.tooltip")),
377384
new CyclingOption<>(Component.translatable("vulkanmod.options.deviceSelector"),
378385
IntStream.range(-1, DeviceManager.suitableDevices.size())
379386
.boxed()

src/main/java/net/vulkanmod/config/video/VideoModeManager.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package net.vulkanmod.config.video;
22

33
import com.mojang.blaze3d.platform.Monitor;
4-
import com.mojang.blaze3d.platform.ScreenManager;
54
import com.mojang.blaze3d.platform.Window;
65
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
76
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
8-
import net.minecraft.client.Minecraft;
97
import net.vulkanmod.Initializer;
8+
import net.vulkanmod.config.Config;
109
import org.lwjgl.glfw.GLFW;
1110
import org.lwjgl.glfw.GLFWVidMode;
1211

@@ -57,7 +56,7 @@ public static VideoModeSet getFirstAvailable() {
5756
if (videoModeSets != null)
5857
return videoModeSets[videoModeSets.length - 1];
5958
else
60-
return VideoModeSet.getDummy();
59+
return null;
6160
}
6261

6362
public static VideoModeSet.VideoMode getOsVideoMode() {
@@ -123,10 +122,22 @@ public static void selectBestMonitor(Window window) {
123122
selectedMonitor = findBestMonitor(window).getMonitor();
124123

125124
if (selectedMonitor == 0L) {
125+
// Fallback to primary in case of null handle
126126
selectedMonitor = GLFW.glfwGetPrimaryMonitor();
127127
}
128128
}
129129

130+
public static void checkConfigVideoMode(Config config) {
131+
var videoMode = config.videoMode;
132+
if (videoMode.width <= 0 || videoMode.height <= 0 || videoMode.refreshRate <= 0) {
133+
videoMode = getFirstAvailable().getVideoMode();
134+
config.videoMode = videoMode;
135+
config.write();
136+
}
137+
}
138+
139+
140+
130141
public static Monitor findBestMonitor(final Window window) {
131142
long windowMonitor = GLFW.glfwGetWindowMonitor(window.handle());
132143
if (windowMonitor != 0L) {

src/main/java/net/vulkanmod/config/video/VideoModeSet.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
import java.util.List;
66

77
public class VideoModeSet {
8+
public static VideoModeSet DUMMY;
9+
810
public final int width;
911
public final int height;
1012
public final int bitDepth;
1113
List<Integer> refreshRates = new ObjectArrayList<>();
1214

1315
public static VideoModeSet getDummy() {
14-
var set = new VideoModeSet(-1, -1, -1);
15-
set.addRefreshRate(-1);
16-
return set;
16+
if (DUMMY == null) {
17+
var set = new VideoModeSet(-1, -1, -1);
18+
set.addRefreshRate(-1);
19+
DUMMY = set;
20+
}
21+
return DUMMY;
1722
}
1823

1924
public VideoModeSet(int width, int height, int bitDepth) {

src/main/java/net/vulkanmod/mixin/window/WindowMixin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public void updateDisplay(@Nullable TracyFrameCapture tracyFrameCapture) {
113113
@Overwrite
114114
private void setMode() {
115115
Config config = Initializer.CONFIG;
116+
VideoModeManager.checkConfigVideoMode(config);
116117

117118
if (this.fullscreen) {
118119
config.windowMode = WindowMode.EXCLUSIVE_FULLSCREEN.mode;

src/main/resources/assets/vulkanmod/lang/en_us.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
"vulkanmod.options.uniqueOpaqueLayer": "Unique opaque layer",
4747
"vulkanmod.options.uniqueOpaqueLayer.tooltip": "Use a unique render layer for opaque terrain to improve performance.",
4848

49+
"vulkanmod.options.wayland": "Use Wayland",
50+
"vulkanmod.options.wayland.tooltip": "Use Linux Wayland compositor if available (Experimental). Requires restart.",
51+
4952
"vulkanmod.options.windowMode": "Window Mode",
5053
"vulkanmod.options.windowMode.windowed": "Windowed",
5154
"vulkanmod.options.windowMode.windowedFullscreen": "Windowed Fullscreen",

0 commit comments

Comments
 (0)