Skip to content

Commit

Permalink
Merge pull request #170 from LostLuma/smart-glfw-workaround
Browse files Browse the repository at this point in the history
Fix the GLFW mouse workaround applying when reducing FPS via keybind
  • Loading branch information
LostLuma authored Feb 23, 2024
2 parents 1050182 + 075d503 commit 39d8233
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
21 changes: 9 additions & 12 deletions platforms/common/src/main/java/dynamic_fps/impl/DynamicFPSMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public static boolean disabledByUser() {
return isKeybindDisabled;
}

public static WindowObserver getWindow() {
if (window != null) {
return window;
}

throw new RuntimeException("Accessed window too early!");
}

public static boolean isDisabled() {
return isKeybindDisabled || !modConfig.enabled() || ModCompat.getInstance().isDisabled();
}
Expand Down Expand Up @@ -207,24 +215,13 @@ public static void handleStateChange(PowerState previous, PowerState current) {
Config before = config;
config = modConfig.get(current);

GLFW.applyWorkaround(); // Apply mouse hover fix if required
hasRenderedLastFrame = false; // Render next frame w/o delay

if (config.runGarbageCollector()) {
System.gc();
}

if (GLFW.useHoverEventWorkaround()) {
if (!current.windowActive) {
minecraft.mouseHandler.releaseMouse();
} else {
// Grabbing the mouse only works while Minecraft
// Agrees that the window is focused. The mod is
// A little too fast for this, so we schedule it
// For the next client tick (before next frame).
minecraft.tell(minecraft.mouseHandler::grabMouse);
}
}

for (SoundSource source : SoundSource.values()) {
((DuckSoundEngine) minecraft.getSoundManager().soundEngine).dynamic_fps$updateVolume(before, source);
}
Expand Down
14 changes: 6 additions & 8 deletions platforms/common/src/main/java/dynamic_fps/impl/PowerState.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,31 @@ public enum PowerState {
/**
* Window is currently focused.
*/
FOCUSED(false, true),
FOCUSED(false),

/**
* Mouse positioned over unfocused window.
*/
HOVERED(true, false),
HOVERED(true),

/**
* Another application is focused.
*/
UNFOCUSED(true, false),
UNFOCUSED(true),

/**
* User hasn't sent input for some time.
*/
ABANDONED(true, true),
ABANDONED(true),

/**
* Window minimized or otherwise hidden.
*/
INVISIBLE(true, false);
INVISIBLE(true);

public final boolean configurable;
public final boolean windowActive;

private PowerState(boolean configurable, boolean windowActive) {
private PowerState(boolean configurable) {
this.configurable = configurable;
this.windowActive = windowActive;
}
}
21 changes: 19 additions & 2 deletions platforms/common/src/main/java/dynamic_fps/impl/compat/GLFW.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
package dynamic_fps.impl.compat;

import dynamic_fps.impl.DynamicFPSMod;
import net.minecraft.client.Minecraft;

public class GLFW {
private static final Minecraft minecraft = Minecraft.getInstance();
private static final boolean enterEventBroken = isEnterEventBroken();

/**
* Whether to use a workaround for the cursor enter event not working.
* Apply a workaround for the cursor enter event not working if needed.
*
* This fixes an issue when running GLFW version 3.3.x or earlier where
* the cursor enter event will only work if the window is not capturing
* The mouse cursor. Since this is often not the case when switching windows
* Dynamic FPS releases and captures the cursor in tandem with window focus.
*/
public static boolean useHoverEventWorkaround() {
public static void applyWorkaround() {
if (!useWorkaround()) {
return;
}

if (!DynamicFPSMod.getWindow().isFocused()) {
minecraft.mouseHandler.releaseMouse();
} else {
// Grabbing the mouse only works while Minecraft
// Agrees that the window is focused. The mod is
// A little too fast for this, so we schedule it
// For the next client tick (before next frame).
minecraft.tell(minecraft.mouseHandler::grabMouse);
}
}

private static boolean useWorkaround() {
return enterEventBroken && minecraft.screen == null && !minecraft.options.pauseOnLostFocus;
}

Expand Down

0 comments on commit 39d8233

Please sign in to comment.