| 1 | package com.mojang.blaze3d.platform;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 6 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 7 | import java.nio.IntBuffer;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 | import org.lwjgl.sdl.SDLStdinc;
|
| 12 | import org.lwjgl.sdl.SDLVideo;
|
| 13 | import org.slf4j.Logger;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class MonitorManager {
|
| 17 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 18 | private final Int2ObjectMap<Monitor> monitors = new Int2ObjectOpenHashMap<>();
|
| 19 |
|
| 20 | public MonitorManager() {
|
| 21 | IntBuffer displays = SDLVideo.SDL_GetDisplays();
|
| 22 | if (displays != null) {
|
| 23 | try {
|
| 24 | for (int i = 0; i < displays.limit(); i++) {
|
| 25 | this.addDisplay(displays.get(i));
|
| 26 | }
|
| 27 | } finally {
|
| 28 | SDLStdinc.SDL_free(displays);
|
| 29 | }
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | public void onDisplayConnected(final int id) {
|
| 34 | RenderSystem.assertOnRenderThread();
|
| 35 | Monitor monitor = this.addDisplay(id);
|
| 36 | if (monitor != null) {
|
| 37 | LOGGER.debug("Monitor {} connected. Current monitors: {}", monitor, this.monitors);
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | public void onDisplayDisconnected(final int id) {
|
| 42 | RenderSystem.assertOnRenderThread();
|
| 43 | Monitor monitor = this.monitors.remove(id);
|
| 44 | LOGGER.debug("Monitor {} disconnected. Current monitors: {}", monitor, this.monitors);
|
| 45 | }
|
| 46 |
|
| 47 | public void onDisplayModeChanged(final int id) {
|
| 48 | RenderSystem.assertOnRenderThread();
|
| 49 | Monitor monitor = this.addDisplay(id);
|
| 50 | if (monitor != null) {
|
| 51 | LOGGER.debug("Monitor {} mode changed. Current monitors: {}", monitor, this.monitors);
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | private @Nullable Monitor addDisplay(final int id) {
|
| 56 | Monitor monitor = Monitor.tryCreate(id);
|
| 57 | if (monitor != null) {
|
| 58 | this.monitors.put(id, monitor);
|
| 59 | }
|
| 60 |
|
| 61 | return monitor;
|
| 62 | }
|
| 63 |
|
| 64 | public @Nullable Monitor getMonitor(final int id) {
|
| 65 | return this.monitors.get(id);
|
| 66 | }
|
| 67 |
|
| 68 | public @Nullable Monitor findBestMonitor(final Window window) {
|
| 69 | return this.getMonitor(SDLVideo.SDL_GetDisplayForWindow(window.handle()));
|
| 70 | }
|
| 71 | }
|
| 72 |
|