1
0
forked from Clones/Controlify

fix bugs:

fix reconnecting controllers are unidentified
fix some mod keybinds failing to register
fix crash with unmapped joysticks
This commit is contained in:
isXander
2023-05-02 12:30:34 +01:00
parent 4e076631a4
commit 681eabd90a
12 changed files with 57 additions and 10 deletions

View File

@ -298,6 +298,8 @@ public class Controlify implements ControlifyApi {
Controller.CONTROLLERS.values().stream().filter(controller -> controller.joystickId() == jid).findAny().ifPresent(controller -> {
Controller.remove(controller);
controller.hidInfo().ifPresent(controllerHIDService::unconsumeController);
setCurrentController(Controller.CONTROLLERS.values().stream().findFirst().orElse(null));
LOGGER.info("Controller disconnected: " + controller.name());
this.setInputMode(currentController == null ? InputMode.KEYBOARD_MOUSE : InputMode.CONTROLLER);

View File

@ -17,6 +17,13 @@ import java.util.Optional;
* Anything that is asked for from this API is safe to use, even if it is not in the API package.
*/
public interface ControlifyApi {
/**
* The controller that is currently enabled and in use.
* If there is no controller disconnected or disabled, this will return {@link Optional#empty()}.
* This is the controller that is used for {@link dev.isxander.controlify.api.event.ControlifyEvents#ACTIVE_CONTROLLER_TICKED}
*/
@NotNull Optional<Controller<?, ?>> getCurrentController();
/**
* @deprecated Use {@link #getCurrentController()} instead.
* @return the controller currently in use. If disabled, this will return {@link Controller#DUMMY}
@ -24,10 +31,8 @@ public interface ControlifyApi {
@Deprecated
@NotNull Controller<?, ?> currentController();
@NotNull Optional<Controller<?, ?>> getCurrentController();
/**
* Get the current input mode for the game.
* The last input received: a controller or keyboard/mouse.
*/
@NotNull InputMode currentInputMode();
boolean setInputMode(@NotNull InputMode mode);

View File

@ -1,8 +1,9 @@
package dev.isxander.controlify.api.bind;
import dev.isxander.controlify.controller.Controller;
import org.jetbrains.annotations.NotNull;
@FunctionalInterface
public interface BindingSupplier {
ControllerBinding onController(Controller<?, ?> controller);
ControllerBinding onController(@NotNull Controller<?, ?> controller);
}

View File

@ -417,7 +417,12 @@ public class ControllerBindings<T extends ControllerState> {
continue;
try {
var identifier = new ResourceLocation("fabric-key-binding-api-v1", keyMapping.getName());
var idPath = keyMapping.getName()
.toLowerCase()
.replaceAll("[^a-z0-9/._-]", "_")
.trim();
var identifier = new ResourceLocation("fabric-key-binding-api-v1", idPath);
BooleanSupplier toggleOverride = () -> false;
if (keyMapping instanceof ToggleKeyMapping toggleKeyMapping) {
toggleOverride = ((ToggleKeyMappingAccessor) toggleKeyMapping).getNeedsToggle();

View File

@ -16,6 +16,7 @@ import org.libsdl.SDL;
import org.lwjgl.glfw.GLFW;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
public abstract class AbstractController<S extends ControllerState, C extends ControllerConfig> implements Controller<S, C>, RumbleCapable {
@ -24,6 +25,7 @@ public abstract class AbstractController<S extends ControllerState, C extends Co
private final String uid;
private final String guid;
private final ControllerType type;
private final ControllerHIDService.ControllerHIDInfo hidInfo;
protected ControllerBindings<S> bindings;
protected C config, defaultConfig;
@ -34,6 +36,8 @@ public abstract class AbstractController<S extends ControllerState, C extends Co
if (!GLFW.glfwJoystickPresent(joystickId))
throw new IllegalArgumentException("Joystick " + joystickId + " is not present and cannot be initialised!");
this.hidInfo = hidInfo;
this.joystickId = joystickId;
this.guid = GLFW.glfwGetJoystickGUID(joystickId);
@ -121,6 +125,11 @@ public abstract class AbstractController<S extends ControllerState, C extends Co
}
}
@Override
public Optional<ControllerHIDService.ControllerHIDInfo> hidInfo() {
return Optional.of(hidInfo);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -48,6 +48,8 @@ public interface Controller<S extends ControllerState, C extends ControllerConfi
RumbleManager rumbleManager();
boolean canRumble();
Optional<ControllerHIDService.ControllerHIDInfo> hidInfo();
default boolean canBeUsed() {
return true;
}
@ -156,6 +158,11 @@ public interface Controller<S extends ControllerState, C extends ControllerConfi
return ControllerType.UNKNOWN;
}
@Override
public Optional<ControllerHIDService.ControllerHIDInfo> hidInfo() {
return Optional.empty();
}
@Override
public String name() {
return "DUMMY";

View File

@ -105,6 +105,10 @@ public class ControllerHIDService {
}
}
public void unconsumeController(ControllerHIDInfo hid) {
hid.hidDevice.ifPresent(device -> attachedDevices.remove(device.getPath()));
}
private boolean isController(HidDevice device) {
boolean isControllerType = ControllerType.getTypeMap().containsKey(new HIDIdentifier(device.getVendorId(), device.getProductId()));
boolean isGenericDesktopControlOrGameControl = device.getUsagePage() == 0x1 || device.getUsagePage() == 0x5;

View File

@ -6,6 +6,7 @@ import com.google.gson.JsonElement;
import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.bindings.ControllerBindings;
import dev.isxander.controlify.controller.ControllerType;
import dev.isxander.controlify.controller.hid.ControllerHIDService;
import dev.isxander.controlify.controller.joystick.mapping.JoystickMapping;
import dev.isxander.controlify.controller.joystick.mapping.RPJoystickMapping;
import dev.isxander.controlify.rumble.RumbleCapable;
@ -14,6 +15,7 @@ import dev.isxander.controlify.rumble.RumbleSource;
import org.lwjgl.glfw.GLFW;
import java.util.List;
import java.util.Optional;
public class CompoundJoystickController implements JoystickController<JoystickConfig>, RumbleCapable {
private final String uid;
@ -177,4 +179,9 @@ public class CompoundJoystickController implements JoystickController<JoystickCo
private int getHatCountForJoystick(int joystick) {
return GLFW.glfwGetJoystickHats(joystick).capacity();
}
@Override
public Optional<ControllerHIDService.ControllerHIDInfo> hidInfo() {
return Optional.empty();
}
}

View File

@ -20,7 +20,7 @@ public class UnmappedJoystickMapping implements JoystickMapping {
this.axes[i] = new UnmappedAxis(i, new GenericRenderer.Axis(Integer.toString(i + 1)));
}
this.buttons = new UnmappedButton[axisCount];
this.buttons = new UnmappedButton[buttonCount];
for (int i = 0; i < buttonCount; i++) {
this.buttons[i] = new UnmappedButton(i, new GenericRenderer.Button(Integer.toString(i + 1)));
}