-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathWindowMixin.java
More file actions
248 lines (203 loc) · 8.23 KB
/
WindowMixin.java
File metadata and controls
248 lines (203 loc) · 8.23 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package net.vulkanmod.mixin.window;
import com.mojang.blaze3d.platform.*;
import com.mojang.blaze3d.systems.RenderSystem;
import net.vulkanmod.Initializer;
import net.vulkanmod.config.Config;
import net.vulkanmod.config.Platform;
import net.vulkanmod.config.video.VideoModeManager;
import net.vulkanmod.config.option.Options;
import net.vulkanmod.config.video.VideoModeSet;
import net.vulkanmod.config.video.WindowMode;
import net.vulkanmod.vulkan.Renderer;
import net.vulkanmod.vulkan.VRenderSystem;
import net.vulkanmod.vulkan.Vulkan;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GLCapabilities;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import static org.lwjgl.glfw.GLFW.*;
@Mixin(Window.class)
public abstract class WindowMixin {
@Final @Shadow private long window;
@Shadow private boolean vsync;
@Shadow protected abstract void updateFullscreen(boolean bl);
@Shadow private boolean fullscreen;
@Shadow @Final private static Logger LOGGER;
@Shadow private int windowedX;
@Shadow private int windowedY;
@Shadow private int windowedWidth;
@Shadow private int windowedHeight;
@Shadow private int x;
@Shadow private int y;
@Shadow private int width;
@Shadow private int height;
@Shadow private int framebufferWidth;
@Shadow private int framebufferHeight;
@Shadow public abstract int getWidth();
@Shadow public abstract int getHeight();
@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwWindowHint(II)V"))
private void redirect(int hint, int value) { }
@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwMakeContextCurrent(J)V"))
private void redirect2(long window) { }
@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL;createCapabilities()Lorg/lwjgl/opengl/GLCapabilities;"))
private GLCapabilities redirect2() {
return null;
}
// Vulkan device not initialized yet
@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/RenderSystem;maxSupportedTextureSize()I"))
private int redirect3() {
return 0;
}
@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwSetWindowSizeLimits(JIIII)V"))
private void redirect4(long window, int minwidth, int minheight, int maxwidth, int maxheight) { }
@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwCreateWindow(IILjava/lang/CharSequence;JJ)J"))
private void vulkanHint(WindowEventHandler windowEventHandler, ScreenManager screenManager, DisplayData displayData, String string, String string2, CallbackInfo ci) {
GLFW.glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
//Fix Gnome Client-Side Decorators
boolean b = (Platform.isGnome() | Platform.isWeston() | Platform.isGeneric()) && Platform.isWayLand();
GLFW.glfwWindowHint(GLFW_DECORATED, (b ? GLFW_FALSE : GLFW_TRUE));
}
@Inject(method = "<init>", at = @At(value = "RETURN"))
private void getHandle(WindowEventHandler windowEventHandler, ScreenManager screenManager, DisplayData displayData, String string, String string2, CallbackInfo ci) {
VRenderSystem.setWindow(this.window);
}
/**
* @author
*/
@Overwrite
public void updateVsync(boolean vsync) {
this.vsync = vsync;
Vulkan.setVsync(vsync);
}
/**
* @author
*/
@Overwrite
public void toggleFullScreen() {
this.fullscreen = !this.fullscreen;
Options.fullscreenDirty = true;
}
/**
* @author
*/
@Overwrite
public void updateDisplay() {
RenderSystem.flipFrame(this.window);
if (Options.fullscreenDirty) {
Options.fullscreenDirty = false;
this.updateFullscreen(this.vsync);
}
}
private boolean wasOnFullscreen = false;
/**
* @author
*/
@Overwrite
private void setMode() {
Config config = Initializer.CONFIG;
// Try to recover a monitor pointer
long monitor = 0;
for (var m : VideoModeManager.getMonitors()) {
var name = glfwGetMonitorName(m);
if (name.equals(config.monitor)) {
monitor = m;
break;
}
}
// if the save monitor is not present / there was no saved monitor fallback
if (monitor == 0) {
monitor = glfwGetPrimaryMonitor();
}
VideoModeManager.setSelectedMonitor(monitor); // refresh video mode manager
if (this.fullscreen) {
{
VideoModeSet.VideoMode videoMode = config.videoMode;
boolean supported;
VideoModeSet set = VideoModeManager.getFromVideoMode(videoMode);
if (set != null) {
supported = set.hasRefreshRate(videoMode.refreshRate);
}
else {
supported = false;
}
if(!supported) {
LOGGER.error("Resolution not supported, using first available as fallback");
videoMode = VideoModeManager.getFirstAvailable().getVideoMode();
}
if (!this.wasOnFullscreen) {
this.windowedX = this.x;
this.windowedY = this.y;
this.windowedWidth = this.width;
this.windowedHeight = this.height;
}
this.x = 0;
this.y = 0;
this.width = videoMode.width;
this.height = videoMode.height;
GLFW.glfwSetWindowMonitor(this.window, monitor, this.x, this.y, this.width, this.height, videoMode.refreshRate);
this.wasOnFullscreen = true;
}
}
else if (config.windowMode == WindowMode.WINDOWED_FULLSCREEN.mode) {
VideoModeSet.VideoMode videoMode = VideoModeManager.getOsVideoMode();
if (!this.wasOnFullscreen) {
this.windowedX = this.x;
this.windowedY = this.y;
this.windowedWidth = this.width;
this.windowedHeight = this.height;
}
int width = videoMode.width;
int height = videoMode.height;
GLFW.glfwSetWindowAttrib(this.window, GLFW_DECORATED, GLFW_FALSE);
GLFW.glfwSetWindowMonitor(this.window, 0L, 0, 0, width, height, -1);
this.width = width;
this.height = height;
this.wasOnFullscreen = true;
} else {
this.x = this.windowedX;
this.y = this.windowedY;
this.width = this.windowedWidth;
this.height = this.windowedHeight;
GLFW.glfwSetWindowMonitor(this.window, 0L, this.x, this.y, this.width, this.height, -1);
GLFW.glfwSetWindowAttrib(this.window, GLFW_DECORATED, GLFW_TRUE);
this.wasOnFullscreen = false;
}
}
/**
* @author
* @reason
*/
@Overwrite
private void onFramebufferResize(long window, int width, int height) {
if (window == this.window) {
int prevWidth = this.getWidth();
int prevHeight = this.getHeight();
if(width > 0 && height > 0) {
this.framebufferWidth = width;
this.framebufferHeight = height;
// if (this.framebufferWidth != prevWidth || this.framebufferHeight != prevHeight) {
// this.eventHandler.resizeDisplay();
// }
Renderer.scheduleSwapChainUpdate();
}
}
}
/**
* @author
* @reason
*/
@Overwrite
private void onResize(long window, int width, int height) {
this.width = width;
this.height = height;
if(width > 0 && height > 0)
Renderer.scheduleSwapChainUpdate();
}
}