Window .java

com.mojang.blaze3d.platform.Window (class, 763 lines): members: getPreferredFullscreenVideoMode, setPreferredFullscreenVideoMode, getAppropriateLineWidth.

com.mojang.blaze3d.platformclass763 linesclient23.4 KB

Implements

API Surface (41)

String getPlatform()
boolean shouldClose()
void handleEvent(final SDL_Event event)
void setIcon(final PackMetadataResources resources, final IconSet iconSet)
String getErrorSection()
void setErrorSection(final String string)
void close()
Window.FramebufferSize queryFramebufferSize()
void updateFullscreenIfChanged()
Optional<VideoMode> getPreferredFullscreenVideoMode()
void setPreferredFullscreenVideoMode(final Optional<VideoMode> preferredFullscreenVideoMode)
void changeFullscreenVideoMode()
boolean isExclusiveFullscreen()
void setExclusiveFullscreen(final boolean exclusiveFullscreen)
void setWindowed(final int width, final int height)
int calculateScale(final int maxScale, final boolean enforceUnicode)
void setTitle(final String title)
void setWindowMaxSize(final int width, final int height)
long handle()
void setFullscreen(final boolean fullscreen)
boolean isIconified()
boolean isFocused()
int getWidth()
void setWidth(final int width)
int getHeight()
void setHeight(final int height)
int getScreenWidth()
int getScreenHeight()
int getGuiScaledWidth()
int getGuiScaledHeight()
int getX()
int getY()
int getGuiScale()
void setGuiScale(final int guiScale)
float getPixelDensity()
void setWindowCloseCallback(final Runnable task)
void setAllowCursorChanges(final boolean value)
void setQuitShortcuts(final boolean value)
void selectCursor(final CursorType cursor)
float getAppropriateLineWidth()
record FramebufferSize(int width, int height)

Fields & Constants (40)

LOGGER
MIN_WINDOW_WIDTH
MIN_WINDOW_HEIGHT
BASE_WIDTH
BASE_HEIGHT
BORDERLESS_FULLSCREEN_PADDING
eventHandler
monitorManager
handle
windowedX
windowedY
windowedWidth
windowedHeight
preferredFullscreenVideoMode
fullscreenRequested
fullscreen
x
y
width
height
framebufferWidth
framebufferHeight
guiScaledWidth
guiScaledHeight
guiScale
errorSection
dirty
iconified
focused
shouldClose

…and 10 more (see full source).

String literals (11)

"Startup"
"Failed to query initial window position: "
"unknown platform"
"Failed to create window: "
"<no error>"
"Window resize"
"Window Dimensions"
"Failed to query window size in pixels: "
"enter"
"leave"
"SDL_WINDOWS_CLOSE_ON_ALT_F4"

Full Source

Window.java763 lines · Minecraft 26.3 · verbatim
1
package com.mojang.blaze3d.platform;
2
3
import com.mojang.blaze3d.platform.cursor.CursorType;
4
import com.mojang.blaze3d.systems.RenderSystem;
5
import com.mojang.logging.LogUtils;
6
import com.mojang.renderpearl.api.device.GpuBackend;
7
import java.io.IOException;
8
import java.io.InputStream;
9
import java.nio.IntBuffer;
10
import java.util.ArrayList;
11
import java.util.List;
12
import java.util.Objects;
13
import java.util.Optional;
14
import net.fabricmc.api.EnvType;
15
import net.fabricmc.api.Environment;
16
import net.minecraft.CrashReport;
17
import net.minecraft.CrashReportCategory;
18
import net.minecraft.ReportedException;
19
import net.minecraft.client.Minecraft;
20
import net.minecraft.client.input.InputQuirks;
21
import net.minecraft.client.main.SilentInitException;
22
import net.minecraft.server.packs.PackMetadataResources;
23
import net.minecraft.server.packs.resources.IoSupplier;
24
import net.minecraft.util.Util;
25
import net.minecraft.util.Util.OS;
26
import org.jspecify.annotations.Nullable;
27
import org.lwjgl.sdl.SDLError;
28
import org.lwjgl.sdl.SDLHints;
29
import org.lwjgl.sdl.SDLPlatform;
30
import org.lwjgl.sdl.SDLSurface;
31
import org.lwjgl.sdl.SDLVideo;
32
import org.lwjgl.sdl.SDL_DisplayMode;
33
import org.lwjgl.sdl.SDL_Event;
34
import org.lwjgl.sdl.SDL_Rect;
35
import org.lwjgl.sdl.SDL_Surface;
36
import org.lwjgl.system.MemoryStack;
37
import org.slf4j.Logger;
38
39
@Environment(EnvType.CLIENT)
40
public final class Window implements AutoCloseable {
41
	private static final Logger LOGGER = LogUtils.getLogger();
42
	public static final int MIN_WINDOW_WIDTH = 320;
43
	public static final int MIN_WINDOW_HEIGHT = 240;
44
	public static final int BASE_WIDTH = 320;
45
	public static final int BASE_HEIGHT = 240;
46
	private static final int BORDERLESS_FULLSCREEN_PADDING = 1;
47
	private final WindowEventHandler eventHandler;
48
	private final MonitorManager monitorManager;
49
	private final long handle;
50
	private int windowedX;
51
	private int windowedY;
52
	private int windowedWidth;
53
	private int windowedHeight;
54
	private Optional<VideoMode> preferredFullscreenVideoMode;
55
	private boolean fullscreenRequested;
56
	private boolean fullscreen;
57
	private int x;
58
	private int y;
59
	private int width;
60
	private int height;
61
	private int framebufferWidth;
62
	private int framebufferHeight;
63
	private int guiScaledWidth;
64
	private int guiScaledHeight;
65
	private int guiScale;
66
	private String errorSection = "Startup";
67
	private boolean dirty;
68
	private boolean iconified;
69
	private boolean focused = true;
70
	private boolean shouldClose;
71
	private @Nullable Runnable closeCallback;
72
	private boolean allowCursorChanges;
73
	private boolean quitShortcuts;
74
	private CursorType currentCursor = CursorType.DEFAULT;
75
	private boolean exclusiveFullscreen;
76
	private boolean borderlessFullscreen;
77
78
	public Window(
79
		final WindowEventHandler eventHandler,
80
		final DisplayData displayData,
81
		final @Nullable String fullscreenVideoModeString,
82
		final boolean exclusiveFullscreen,
83
		final String title,
84
		final MonitorManager monitorManager,
85
		final GpuBackend backend
86
	) {
87
		this(eventHandler, displayData, fullscreenVideoModeString, exclusiveFullscreen, title, monitorManager, backend, 0);
88
	}
89
90
	public Window(
91
		final WindowEventHandler eventHandler,
92
		final DisplayData displayData,
93
		final @Nullable String fullscreenVideoModeString,
94
		final boolean exclusiveFullscreen,
95
		final String title,
96
		final MonitorManager monitorManager,
97
		final GpuBackend backend,
98
		final int maximumSize
99
	) {
100
		this.monitorManager = monitorManager;
101
		this.exclusiveFullscreen = exclusiveFullscreen;
102
		this.eventHandler = eventHandler;
103
		Optional<VideoMode> optionsMode = VideoMode.read(fullscreenVideoModeString);
104
		if (optionsMode.isPresent()) {
105
			this.preferredFullscreenVideoMode = optionsMode;
106
		} else if (displayData.fullscreenWidth().isPresent() && displayData.fullscreenHeight().isPresent()) {
107
			this.preferredFullscreenVideoMode = Optional.of(
108
				new VideoMode(displayData.fullscreenWidth().getAsInt(), displayData.fullscreenHeight().getAsInt(), 8, 8, 8, 60)
109
			);
110
		} else {
111
			this.preferredFullscreenVideoMode = Optional.empty();
112
		}
113
114
		this.fullscreenRequested = displayData.isFullscreen();
115
		Monitor initialMonitor = monitorManager.getMonitor(SDLVideo.SDL_GetPrimaryDisplay());
116
		this.windowedWidth = this.width = allowedWindowMinSize(displayData.width(), 320);
117
		this.windowedHeight = this.height = allowedWindowMinSize(displayData.height(), 240);
118
		this.handle = this.createWindow(backend, this.width, this.height, title);
119
		this.setWindowMaxSize(maximumSize, maximumSize);
120
		MacosUtil.disableCloseWindowMenuItem();
121
		if (initialMonitor != null) {
122
			this.windowedX = this.x = initialMonitor.x() + (initialMonitor.w() - this.width) / 2;
123
			this.windowedY = this.y = initialMonitor.y() + (initialMonitor.h() - this.height) / 2;
124
		} else {
125
			try (MemoryStack stack = MemoryStack.stackPush()) {
126
				IntBuffer actualX = stack.mallocInt(1);
127
				IntBuffer actualY = stack.mallocInt(1);
128
				if (!SDLVideo.SDL_GetWindowPosition(this.handle, actualX, actualY)) {
129
					throw new IllegalStateException("Failed to query initial window position: " + SDLError.SDL_GetError());
130
				}
131
132
				this.windowedX = this.x = actualX.get(0);
133
				this.windowedY = this.y = actualY.get(0);
134
			}
135
		}
136
137
		this.setMode();
138
		this.refreshFramebufferSize();
139
	}
140
141
	public static String getPlatform() {
142
		String platform = SDLPlatform.SDL_GetPlatform();
143
		return platform == null ? "unknown platform" : platform;
144
	}
145
146
	private static @Nullable SDL_Surface createIconSurface(final NativeImage image) {
147
		int pitch = image.getWidth() * 4;
148
		return SDLSurface.SDL_CreateSurfaceFrom(image.getWidth(), image.getHeight(), 376840196, image.getPixelBytes(), pitch);
149
	}
150
151
	private long createWindow(final GpuBackend backend, final int width, final int height, final String title) {
152
		long flags = 8224L;
153
		long windowHandle = backend.createWindow(title, width, height, 8224L);
154
		if (windowHandle == 0L) {
155
			throw new IllegalStateException("Failed to create window: " + Objects.requireNonNullElse(SDLError.SDL_GetError(), "<no error>"));
156
		}
157
158
		SDLVideo.SDL_SetWindowMinimumSize(windowHandle, 320, 240);
159
		LOGGER.info("Created window using SDL video driver: {}", SDLVideo.SDL_GetCurrentVideoDriver());
160
		return windowHandle;
161
	}
162
163
	public @Nullable VideoMode getActiveVideoMode() {
164
		RenderSystem.assertOnRenderThread();
165
		SDL_DisplayMode mode = this.getActiveDisplayMode();
166
		return mode == null ? null : new VideoMode(mode);
167
	}
168
169
	private @Nullable SDL_DisplayMode getActiveDisplayMode() {
170
		SDL_DisplayMode windowMode = SDLVideo.SDL_GetWindowFullscreenMode(this.handle);
171
		if (windowMode != null) {
172
			return windowMode;
173
		}
174
175
		int displayId = SDLVideo.SDL_GetDisplayForWindow(this.handle);
176
		return displayId == 0 ? null : SDLVideo.SDL_GetCurrentDisplayMode(displayId);
177
	}
178
179
	public boolean shouldClose() {
180
		return this.shouldClose;
181
	}
182
183
	public void handleEvent(final SDL_Event event) {
184
		switch (event.type()) {
185
			case 256:
186
			case 528:
187
				this.onQuitRequested();
188
				break;
189
			case 257:
190
				this.requestClose();
191
				break;
192
			case 338:
193
				this.monitorManager.onDisplayConnected(event.display().displayID());
194
				break;
195
			case 339:
196
				this.monitorManager.onDisplayDisconnected(event.display().displayID());
197
				break;
198
			case 342:
199
				this.onDisplayModeChanged(event.display().displayID());
200
				break;
201
			case 517:
202
				this.onMove(event.window().data1(), event.window().data2());
203
				break;
204
			case 518:
205
				this.onResize(event.window().data1(), event.window().data2());
206
				break;
207
			case 519:
208
				this.onFramebufferResize(event.window().data1(), event.window().data2());
209
				break;
210
			case 521:
211
				this.onIconified(true);
212
				break;
213
			case 522:
214
			case 523:
215
				this.onIconified(false);
216
				break;
217
			case 524:
218
			case 525:
219
				this.eventHandler.cursorEntered();
220
				break;
221
			case 526:
222
				this.onFocus(true);
223
				break;
224
			case 527:
225
				this.onFocus(false);
226
				break;
227
			case 531:
228
				this.eventHandler.framebufferSizeChanged();
229
				break;
230
			case 535:
231
			case 536:
232
				this.updateFullscreenState();
233
		}
234
	}
235
236
	private void onDisplayModeChanged(final int displayId) {
237
		this.monitorManager.onDisplayModeChanged(displayId);
238
		this.refreshFramebufferSize();
239
		this.eventHandler.framebufferSizeChanged();
240
	}
241
242
	private void onQuitRequested() {
243
		if (this.quitShortcuts || !InputQuirks.isQuitShortcutDown()) {
244
			this.requestClose();
245
		}
246
	}
247
248
	private void updateFullscreenState() {
249
		boolean newFullscreen = this.isWindowFullscreen();
250
		if (this.fullscreen != newFullscreen) {
251
			this.fullscreen = newFullscreen;
252
			this.eventHandler.fullscreenStateChanged(newFullscreen);
253
		}
254
	}
255
256
	private void requestClose() {
257
		this.shouldClose = true;
258
		if (this.closeCallback != null) {
259
			this.closeCallback.run();
260
		}
261
	}
262
263
	public void setIcon(final PackMetadataResources resources, final IconSet iconSet) throws IOException {
264
		OS platform = Util.getPlatform();
265
		switch (platform) {
266
			case WINDOWS:
267
			case LINUX:
268
			case SOLARIS:
269
			case OSX:
270
				this.setIcon(iconSet.getStandardIcons(resources));
271
				break;
272
			default:
273
				LOGGER.warn("Not setting icon for unrecognized platform: {}", platform);
274
		}
275
	}
276
277
	private void setIcon(final List<IoSupplier<InputStream>> iconStreams) throws IOException {
278
		if (!iconStreams.isEmpty()) {
279
			List<NativeImage> images = new ArrayList<>(iconStreams.size());
280
281
			try {
282
				SDL_Surface primarySurface = createIconSurface(iconStreams.getFirst(), images);
283
				if (primarySurface != null) {
284
					for (IoSupplier<InputStream> iconStream : iconStreams.subList(1, iconStreams.size())) {
285
						SDL_Surface surface = createIconSurface(iconStream, images);
286
						if (surface != null) {
287
							if (!SDLSurface.SDL_AddSurfaceAlternateImage(primarySurface, surface)) {
288
								LOGGER.warn("Failed to add {}x{} icon as alternate window icon image: {}", surface.w(), surface.h(), SDLError.SDL_GetError());
289
							}
290
291
							SDLSurface.SDL_DestroySurface(surface);
292
						}
293
					}
294
295
					if (!SDLVideo.SDL_SetWindowIcon(this.handle, primarySurface)) {
296
						LOGGER.warn("Failed to set window icon: {}", SDLError.SDL_GetError());
297
					}
298
299
					SDLSurface.SDL_DestroySurface(primarySurface);
300
					return;
301
				}
302
			} finally {
303
				images.forEach(NativeImage::close);
304
			}
305
		}
306
	}
307
308
	private static @Nullable SDL_Surface createIconSurface(final IoSupplier<InputStream> iconStream, final List<NativeImage> images) throws IOException {
309
		NativeImage image = NativeImage.read(iconStream.get());
310
		images.add(image);
311
		SDL_Surface surface = createIconSurface(image);
312
		if (surface == null) {
313
			LOGGER.warn("Failed to create SDL surface for {}x{} icon: {}", image.getWidth(), image.getHeight(), SDLError.SDL_GetError());
314
		}
315
316
		return surface;
317
	}
318
319
	public String getErrorSection() {
320
		return this.errorSection;
321
	}
322
323
	public void setErrorSection(final String string) {
324
		this.errorSection = string;
325
	}
326
327
	@Override
328
	public void close() {
329
		RenderSystem.assertOnRenderThread();
330
		SDLVideo.SDL_DestroyWindow(this.handle);
331
	}
332
333
	private void onMove(final int x, final int y) {
334
		this.x = x;
335
		this.y = y;
336
		if (!this.isWindowFullscreen()) {
337
			this.windowedX = x;
338
			this.windowedY = y;
339
		}
340
	}
341
342
	private void onResize(final int newWidth, final int newHeight) {
343
		this.width = newWidth;
344
		this.height = newHeight;
345
		if (!this.isWindowFullscreen()) {
346
			this.windowedWidth = allowedWindowMinSize(newWidth, 320);
347
			this.windowedHeight = allowedWindowMinSize(newHeight, 240);
348
		}
349
350
		this.updateWindowMouseGrab();
351
		if (Minecraft.getInstance().mouseHandler.isMouseGrabbed()) {
352
			double xpos = this.getScreenWidth() / 2.0;
353
			double ypos = this.getScreenHeight() / 2.0;
354
			InputConstants.grabMouse(this, xpos, ypos);
355
		}
356
	}
357
358
	private void onFramebufferResize(final int newWidth, final int newHeight) {
359
		if (newWidth > 0 && newHeight > 0) {
360
			int oldWidth = this.getWidth();
361
			int oldHeight = this.getHeight();
362
			this.framebufferWidth = newWidth - this.framebufferWidthPadding();
363
			this.framebufferHeight = newHeight;
364
365
			try {
366
				this.eventHandler.framebufferSizeChanged();
367
			} catch (Exception e) {
368
				CrashReport report = CrashReport.forThrowable(e, "Window resize");
369
				CrashReportCategory windowSizeDetails = report.addCategory("Window Dimensions");
370
				windowSizeDetails.setDetail("Old", oldWidth + "x" + oldHeight);
371
				windowSizeDetails.setDetail("New", newWidth + "x" + newHeight);
372
				throw new ReportedException(report);
373
			}
374
		}
375
	}
376
377
	private void refreshFramebufferSize() {
378
		Window.FramebufferSize size = this.queryFramebufferSize();
379
		this.framebufferWidth = size.width() - this.framebufferWidthPadding();
380
		this.framebufferHeight = size.height();
381
	}
382
383
	private int framebufferWidthPadding() {
384
		return this.borderlessFullscreen ? 1 : 0;
385
	}
386
387
	public Window.FramebufferSize queryFramebufferSize() {
388
		try (MemoryStack stack = MemoryStack.stackPush()) {
389
			IntBuffer outWidth = stack.mallocInt(1);
390
			IntBuffer outHeight = stack.mallocInt(1);
391
			if (!SDLVideo.SDL_GetWindowSizeInPixels(this.handle, outWidth, outHeight)) {
392
				throw new IllegalStateException("Failed to query window size in pixels: " + SDLError.SDL_GetError());
393
			} else {
394
				return new Window.FramebufferSize(Math.max(outWidth.get(0), 1), Math.max(outHeight.get(0), 1));
395
			}
396
		}
397
	}
398
399
	private void onFocus(final boolean focused) {
400
		this.focused = focused;
401
	}
402
403
	private void onIconified(final boolean iconified) {
404
		this.iconified = iconified;
405
		Minecraft.getInstance().invalidateSurfaceConfiguration();
406
	}
407
408
	public void updateFullscreenIfChanged() {
409
		RenderSystem.assertOnRenderThread();
410
		if (this.fullscreenRequested != this.fullscreen) {
411
			this.setMode();
412
			this.eventHandler.framebufferSizeChanged();
413
		}
414
	}
415
416
	public Optional<VideoMode> getPreferredFullscreenVideoMode() {
417
		return this.preferredFullscreenVideoMode;
418
	}
419
420
	public void setPreferredFullscreenVideoMode(final Optional<VideoMode> preferredFullscreenVideoMode) {
421
		boolean changed = !preferredFullscreenVideoMode.equals(this.preferredFullscreenVideoMode);
422
		this.preferredFullscreenVideoMode = preferredFullscreenVideoMode;
423
		if (changed) {
424
			this.dirty = true;
425
		}
426
	}
427
428
	public void changeFullscreenVideoMode() {
429
		RenderSystem.assertOnRenderThread();
430
		if (this.fullscreenRequested && this.dirty) {
431
			this.dirty = false;
432
			this.setMode();
433
			this.eventHandler.framebufferSizeChanged();
434
		}
435
	}
436
437
	private void setMode() {
438
		RenderSystem.assertOnRenderThread();
439
		if (this.fullscreenRequested && !this.fullscreen) {
440
			this.windowedX = this.x;
441
			this.windowedY = this.y;
442
			this.windowedWidth = allowedWindowMinSize(this.width, 320);
443
			this.windowedHeight = allowedWindowMinSize(this.height, 240);
444
		}
445
446
		boolean success = this.fullscreenRequested ? this.applyFullscreen() : this.applyWindowed();
447
		if (!success) {
448
			LOGGER.error("Couldn't {} fullscreen: {}", this.fullscreenRequested ? "enter" : "leave", SDLError.SDL_GetError());
449
			this.fullscreenRequested = this.fullscreen;
450
			this.eventHandler.fullscreenStateChanged(this.fullscreen);
451
		} else {
452
			this.syncWindow();
453
			this.updateFullscreenState();
454
			this.refreshFramebufferSize();
455
			if (!this.isExclusiveFullscreen()) {
456
				this.updateWindowMouseGrab();
457
			}
458
459
			if (this.exclusiveFullscreen && this.fullscreen && !this.isExclusiveFullscreen()) {
460
				LOGGER.info("Exclusive fullscreen request resolved to borderless desktop");
461
			}
462
		}
463
	}
464
465
	private void updateWindowMouseGrab() {
466
		boolean shouldGrabMouse = this.fullscreen && this.isExclusiveFullscreen();
467
		if (!SDLVideo.SDL_SetWindowMouseGrab(this.handle, shouldGrabMouse)) {
468
			LOGGER.warn("Failed to update window mouse grab state: {}", SDLError.SDL_GetError());
469
		}
470
	}
471
472
	private boolean isWindowFullscreen() {
473
		return this.borderlessFullscreen || (SDLVideo.SDL_GetWindowFlags(this.handle) & 1L) != 0L;
474
	}
475
476
	public boolean isExclusiveFullscreen() {
477
		return this.isWindowFullscreen() && SDLVideo.SDL_GetWindowFullscreenMode(this.handle) != null;
478
	}
479
480
	private boolean applyFullscreen() {
481
		if (this.useBorderlessFullscreenWindow()) {
482
			return this.applyBorderlessFullscreenWindow();
483
		}
484
485
		this.leaveBorderlessFullscreenWindow();
486
		return this.applyFullscreenMode() && SDLVideo.SDL_SetWindowFullscreen(this.handle, true);
487
	}
488
489
	private boolean useBorderlessFullscreenWindow() {
490
		return Util.getPlatform() == OS.WINDOWS && !this.exclusiveFullscreen;
491
	}
492
493
	private boolean applySdlBorderlessFullscreen() {
494
		this.leaveBorderlessFullscreenWindow();
495
		return this.applyBorderlessFullscreen() && SDLVideo.SDL_SetWindowFullscreen(this.handle, true);
496
	}
497
498
	private void leaveBorderlessFullscreenWindow() {
499
		if (this.borderlessFullscreen) {
500
			this.borderlessFullscreen = false;
501
			if (!SDLVideo.SDL_SetWindowBordered(this.handle, true)) {
502
				LOGGER.warn("Failed to restore window decorations: {}", SDLError.SDL_GetError());
503
			}
504
		}
505
	}
506
507
	private boolean applyFullscreenMode() {
508
		return !this.exclusiveFullscreen ? this.applyBorderlessFullscreen() : this.applyExclusiveFullscreen();
509
	}
510
511
	private boolean applyExclusiveFullscreen() {
512
		Monitor monitor = this.monitorManager.findBestMonitor(this);
513
		if (monitor == null) {
514
			LOGGER.warn("Failed to find suitable monitor for exclusive fullscreen, falling back to borderless fullscreen");
515
			return this.applyBorderlessFullscreen();
516
		}
517
518
		VideoMode videoMode = monitor.getPreferredVideoMode(this.preferredFullscreenVideoMode);
519
		LOGGER.info("Exclusive target {} on monitor {}", videoMode, monitor);
520
521
		try (MemoryStack stack = MemoryStack.stackPush()) {
522
			SDL_DisplayMode mode = SDL_DisplayMode.malloc(stack);
523
			if (SDLVideo.SDL_GetClosestFullscreenDisplayMode(monitor.id(), videoMode.getWidth(), videoMode.getHeight(), videoMode.getRefreshRate(), true, mode)) {
524
				return SDLVideo.SDL_SetWindowFullscreenMode(this.handle, mode);
525
			}
526
527
			LOGGER.warn("No matching exclusive fullscreen mode found for {}, falling back to borderless fullscreen", videoMode);
528
			return this.applyBorderlessFullscreen();
529
		}
530
	}
531
532
	private boolean applyWindowed() {
533
		this.leaveBorderlessFullscreenWindow();
534
		return !SDLVideo.SDL_SetWindowFullscreen(this.handle, false)
535
			? false
536
			: this.setWindowSizeAndPosition(
537
				this.windowedX, this.windowedY, allowedWindowMinSize(this.windowedWidth, 320), allowedWindowMinSize(this.windowedHeight, 240)
538
			);
539
	}
540
541
	private boolean applyBorderlessFullscreenWindow() {
542
		Monitor monitor = this.monitorManager.findBestMonitor(this);
543
		if (monitor == null) {
544
			LOGGER.warn("Failed to find suitable monitor for borderless fullscreen, falling back to SDL borderless fullscreen");
545
			return this.applySdlBorderlessFullscreen();
546
		}
547
548
		try (MemoryStack stack = MemoryStack.stackPush()) {
549
			SDL_Rect bounds = SDL_Rect.malloc(stack);
550
			if (!SDLVideo.SDL_GetDisplayBounds(monitor.id(), bounds)) {
551
				LOGGER.warn("Failed to query bounds of monitor {}, falling back to SDL borderless fullscreen: {}", monitor, SDLError.SDL_GetError());
552
				return this.applySdlBorderlessFullscreen();
553
			}
554
555
			if (this.applyBorderlessFullscreen() && SDLVideo.SDL_SetWindowFullscreen(this.handle, false)) {
556
				this.restoreWindow();
557
				this.borderlessFullscreen = true;
558
				if (!SDLVideo.SDL_SetWindowBordered(this.handle, false)) {
559
					LOGGER.warn("Failed to remove window decorations for borderless fullscreen: {}", SDLError.SDL_GetError());
560
				}
561
562
				return this.setWindowSizeAndPosition(bounds.x(), bounds.y(), bounds.w() + 1, bounds.h());
563
			} else {
564
				return false;
565
			}
566
		}
567
	}
568
569
	private void restoreWindow() {
570
		if (!SDLVideo.SDL_RestoreWindow(this.handle)) {
571
			LOGGER.warn("Failed to restore window before entering fullscreen: {}", SDLError.SDL_GetError());
572
		} else {
573
			this.syncWindow();
574
		}
575
	}
576
577
	private void syncWindow() {
578
		if (!SDLVideo.SDL_SyncWindow(this.handle)) {
579
			LOGGER.warn("Failed to synchronize SDL window: {}", SDLError.SDL_GetError());
580
		}
581
	}
582
583
	private boolean setWindowSizeAndPosition(final int windowX, final int windowY, final int windowWidth, final int windowHeight) {
584
		this.x = windowX;
585
		this.y = windowY;
586
		this.width = windowWidth;
587
		this.height = windowHeight;
588
		if (!SDLVideo.SDL_SetWindowSize(this.handle, this.width, this.height)) {
589
			return false;
590
		}
591
592
		if (!SDLVideo.SDL_SetWindowPosition(this.handle, this.x, this.y)) {
593
			LOGGER.debug("Window manager declined window positioning: {}", SDLError.SDL_GetError());
594
		}
595
596
		return true;
597
	}
598
599
	private boolean applyBorderlessFullscreen() {
600
		return SDLVideo.SDL_SetWindowFullscreenMode(this.handle, null);
601
	}
602
603
	public void setExclusiveFullscreen(final boolean exclusiveFullscreen) {
604
		if (this.exclusiveFullscreen != exclusiveFullscreen) {
605
			this.exclusiveFullscreen = exclusiveFullscreen;
606
			if (this.fullscreenRequested) {
607
				this.setMode();
608
				this.eventHandler.framebufferSizeChanged();
609
			}
610
		}
611
	}
612
613
	public void setWindowed(final int width, final int height) {
614
		this.windowedWidth = allowedWindowMinSize(width, 320);
615
		this.windowedHeight = allowedWindowMinSize(height, 240);
616
		this.fullscreenRequested = false;
617
		this.setMode();
618
	}
619
620
	public int calculateScale(final int maxScale, final boolean enforceUnicode) {
621
		int scale = 1;
622
623
		while (
624
			scale != maxScale
625
				&& scale < this.framebufferWidth
626
				&& scale < this.framebufferHeight
627
				&& this.framebufferWidth / (scale + 1) >= 320
628
				&& this.framebufferHeight / (scale + 1) >= 240
629
		) {
630
			scale++;
631
		}
632
633
		if (enforceUnicode && scale % 2 != 0) {
634
			scale++;
635
		}
636
637
		return scale;
638
	}
639
640
	public void setTitle(final String title) {
641
		SDLVideo.SDL_SetWindowTitle(this.handle, title);
642
	}
643
644
	public void setWindowMaxSize(final int width, final int height) {
645
		SDLVideo.SDL_SetWindowMaximumSize(this.handle, width, height);
646
	}
647
648
	public long handle() {
649
		return this.handle;
650
	}
651
652
	public void setFullscreen(final boolean fullscreen) {
653
		this.fullscreenRequested = fullscreen;
654
	}
655
656
	public boolean isIconified() {
657
		return this.iconified;
658
	}
659
660
	public boolean isFocused() {
661
		return this.focused;
662
	}
663
664
	public int getWidth() {
665
		return this.framebufferWidth;
666
	}
667
668
	public void setWidth(final int width) {
669
		this.framebufferWidth = width;
670
	}
671
672
	public int getHeight() {
673
		return this.framebufferHeight;
674
	}
675
676
	public void setHeight(final int height) {
677
		this.framebufferHeight = height;
678
	}
679
680
	public int getScreenWidth() {
681
		return this.width;
682
	}
683
684
	public int getScreenHeight() {
685
		return this.height;
686
	}
687
688
	public int getGuiScaledWidth() {
689
		return this.guiScaledWidth;
690
	}
691
692
	public int getGuiScaledHeight() {
693
		return this.guiScaledHeight;
694
	}
695
696
	public int getX() {
697
		return this.x;
698
	}
699
700
	public int getY() {
701
		return this.y;
702
	}
703
704
	public int getGuiScale() {
705
		return this.guiScale;
706
	}
707
708
	public void setGuiScale(final int guiScale) {
709
		this.guiScale = guiScale;
710
		this.guiScaledWidth = (int)Math.ceil((double)this.framebufferWidth / guiScale);
711
		this.guiScaledHeight = (int)Math.ceil((double)this.framebufferHeight / guiScale);
712
	}
713
714
	public float getPixelDensity() {
715
		float density = SDLVideo.SDL_GetWindowPixelDensity(this.handle);
716
		return density > 0.0F ? density : 1.0F;
717
	}
718
719
	public @Nullable Monitor findBestMonitor() {
720
		return this.monitorManager.findBestMonitor(this);
721
	}
722
723
	public void setWindowCloseCallback(final Runnable task) {
724
		this.closeCallback = task;
725
	}
726
727
	public void setAllowCursorChanges(final boolean value) {
728
		this.allowCursorChanges = value;
729
	}
730
731
	public void setQuitShortcuts(final boolean value) {
732
		this.quitShortcuts = value;
733
		SDLHints.SDL_SetHint("SDL_WINDOWS_CLOSE_ON_ALT_F4", value ? "1" : "0");
734
	}
735
736
	public void selectCursor(final CursorType cursor) {
737
		CursorType effectiveCursor = this.allowCursorChanges ? cursor : CursorType.DEFAULT;
738
		if (this.currentCursor != effectiveCursor) {
739
			this.currentCursor = effectiveCursor;
740
			effectiveCursor.select();
741
		}
742
	}
743
744
	public float getAppropriateLineWidth() {
745
		return Math.max(2.5F, this.getWidth() / 1920.0F * 2.5F);
746
	}
747
748
	private static int allowedWindowMinSize(final int size, final int minSize) {
749
		return Math.max(size, minSize);
750
	}
751
752
	@Environment(EnvType.CLIENT)
753
	public record FramebufferSize(int width, int height) {
754
	}
755
756
	@Environment(EnvType.CLIENT)
757
	public static class WindowInitFailed extends SilentInitException {
758
		public WindowInitFailed(final String message) {
759
			super(message);
760
		}
761
	}
762
}
763

Decompiled Minecraft 26.3 source of Window (com/mojang/blaze3d/platform/Window.java) with Mojang official mappings, stored verbatim on the site.