| 1 | package com.mojang.blaze3d.platform;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableList.Builder;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.util.HexFormat;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Locale;
|
| 9 | import java.util.Objects;
|
| 10 | import java.util.Optional;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 | import org.lwjgl.PointerBuffer;
|
| 15 | import org.lwjgl.sdl.SDLError;
|
| 16 | import org.lwjgl.sdl.SDLStdinc;
|
| 17 | import org.lwjgl.sdl.SDLVideo;
|
| 18 | import org.lwjgl.sdl.SDL_DisplayMode;
|
| 19 | import org.lwjgl.sdl.SDL_Rect;
|
| 20 | import org.lwjgl.system.MemoryStack;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public record Monitor(String name, int id, List<VideoMode> videoModes, VideoMode currentMode, int x, int y, int w, int h) {
|
| 25 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 26 | private static final HexFormat HEX_FORMAT = HexFormat.of().withUpperCase();
|
| 27 |
|
| 28 | public static @Nullable Monitor tryCreate(final int id) {
|
| 29 | String name = queryMonitorName(id);
|
| 30 | Builder<VideoMode> videoModes = ImmutableList.builder();
|
| 31 |
|
| 32 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 33 | PointerBuffer modes = SDLVideo.SDL_GetFullscreenDisplayModes(id);
|
| 34 | if (modes == null) {
|
| 35 | LOGGER.warn("Failed to query video modes of monitor {}: {}", name, SDLError.SDL_GetError());
|
| 36 | return null;
|
| 37 | }
|
| 38 |
|
| 39 | try {
|
| 40 | for (int i = 0; i < modes.limit(); i++) {
|
| 41 | VideoMode mode = new VideoMode(SDL_DisplayMode.create(modes.get(i)));
|
| 42 | if (mode.getRedBits() >= 8 && mode.getGreenBits() >= 8 && mode.getBlueBits() >= 8) {
|
| 43 | videoModes.add(mode);
|
| 44 | }
|
| 45 | }
|
| 46 | } finally {
|
| 47 | SDLStdinc.SDL_free(modes);
|
| 48 | }
|
| 49 |
|
| 50 | SDL_Rect var14 = SDL_Rect.malloc(stack);
|
| 51 | if (!SDLVideo.SDL_GetDisplayBounds(id, var14)) {
|
| 52 | LOGGER.warn("Failed to query monitor bounds of {}: {}", name, SDLError.SDL_GetError());
|
| 53 | return null;
|
| 54 | } else {
|
| 55 | SDL_DisplayMode currentMode = SDLVideo.SDL_GetDesktopDisplayMode(id);
|
| 56 | if (currentMode == null) {
|
| 57 | LOGGER.warn("Failed to query current desktop video mode of monitor {}: {}", name, SDLError.SDL_GetError());
|
| 58 | return null;
|
| 59 | } else {
|
| 60 | return new Monitor(name, id, videoModes.build(), new VideoMode(currentMode), var14.x(), var14.y(), var14.w(), var14.h());
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | private static String queryMonitorName(final int id) {
|
| 67 | String monitorName = Objects.requireNonNullElse(SDLVideo.SDL_GetDisplayName(id), "unknown");
|
| 68 | return monitorName + "[0x" + HEX_FORMAT.toHexDigits(id) + "]";
|
| 69 | }
|
| 70 |
|
| 71 | public VideoMode getPreferredVideoMode(final Optional<VideoMode> expectedMode) {
|
| 72 | return expectedMode.filter(this.videoModes::contains).orElse(this.currentMode);
|
| 73 | }
|
| 74 |
|
| 75 | public int indexOfMode(final VideoMode expectedMode) {
|
| 76 | return this.videoModes.indexOf(expectedMode);
|
| 77 | }
|
| 78 |
|
| 79 | public VideoMode mode(final int mode) {
|
| 80 | return this.videoModes.get(mode);
|
| 81 | }
|
| 82 |
|
| 83 | public int modeCount() {
|
| 84 | return this.videoModes.size();
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public String toString() {
|
| 89 | return String.format(Locale.ROOT, "%s(%s at (%d,%d))", this.name, this.currentMode, this.x, this.y);
|
| 90 | }
|
| 91 | }
|
| 92 |
|