From 237d63069919f87af984739988335e8444b4d066 Mon Sep 17 00:00:00 2001 From: Benjamin Schulte Date: Wed, 13 Jan 2021 18:50:37 +0100 Subject: [PATCH] Make using SDL 2.0.14 RawInput configurable on instantiation --- .../studiohartman/jamepad/Configuration.java | 19 +++++++++++++++ .../jamepad/ControllerManager.java | 24 +++++++++++-------- 2 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/studiohartman/jamepad/Configuration.java diff --git a/src/main/java/com/studiohartman/jamepad/Configuration.java b/src/main/java/com/studiohartman/jamepad/Configuration.java new file mode 100644 index 0000000..1304f03 --- /dev/null +++ b/src/main/java/com/studiohartman/jamepad/Configuration.java @@ -0,0 +1,19 @@ +package com.studiohartman.jamepad; + +/** + * Class defining the configuration of a {@link ControllerManager}. + * + * @author Benjamin Schulte + */ +public class Configuration { + /** + * The max number of controllers the ControllerManager should deal with + */ + public int maxNumControllers = 4; + + /** + * Use RawInput implementation instead of XInput on Windows, if applicable. Enable this if you + * need to use more than four XInput controllers at once. Comes with drawbacks. + */ + public boolean useRawInput = false; +} diff --git a/src/main/java/com/studiohartman/jamepad/ControllerManager.java b/src/main/java/com/studiohartman/jamepad/ControllerManager.java index e890ceb..b28b513 100644 --- a/src/main/java/com/studiohartman/jamepad/ControllerManager.java +++ b/src/main/java/com/studiohartman/jamepad/ControllerManager.java @@ -29,6 +29,7 @@ public class ControllerManager { SDL_Event event; */ + private final Configuration configuration; private String mappingsPath; private boolean isInitialized; private ControllerIndex[] controllers; @@ -38,28 +39,29 @@ public class ControllerManager { * https://github.com/gabomdq/SDL_GameControllerDB */ public ControllerManager() { - this(4, "/gamecontrollerdb.txt"); + this(new Configuration(), "/gamecontrollerdb.txt"); } /** * Constructor. Uses built-in mappings from here: https://github.com/gabomdq/SDL_GameControllerDB * - * @param maxNumControllers The number of controllers this ControllerManager can deal with + * @param configuration see {@link Configuration and its fields} */ - public ControllerManager(int maxNumControllers) { - this(maxNumControllers, "/gamecontrollerdb.txt"); + public ControllerManager(Configuration configuration) { + this(configuration, "/gamecontrollerdb.txt"); } /** * Constructor. * * @param mappingsPath The path to a file containing SDL controller mappings. - * @param maxNumControllers The number of controller this ControllerManager can deal with + * @param configuration see {@link Configuration and its fields} */ - public ControllerManager(int maxNumControllers, String mappingsPath) { + public ControllerManager(Configuration configuration, String mappingsPath) { + this.configuration = configuration; this.mappingsPath = mappingsPath; isInitialized = false; - controllers = new ControllerIndex[maxNumControllers]; + controllers = new ControllerIndex[configuration.maxNumControllers]; new SharedLibraryLoader().load("jamepad"); } @@ -76,7 +78,7 @@ public void initSDLGamepad() throws IllegalStateException { } //Initialize SDL - if (!nativeInitSDLGamepad()) { + if (!nativeInitSDLGamepad(!configuration.useRawInput)) { throw new IllegalStateException("Failed to initialize SDL in native method!"); } else { isInitialized = true; @@ -97,8 +99,10 @@ public void initSDLGamepad() throws IllegalStateException { controllers[i] = new ControllerIndex(i); } } - private native boolean nativeInitSDLGamepad(); /* - SDL_SetHint(SDL_HINT_JOYSTICK_RAWINPUT, "0"); + private native boolean nativeInitSDLGamepad(boolean disableRawInput); /* + if (disableRawInput) { + SDL_SetHint(SDL_HINT_JOYSTICK_RAWINPUT, "0"); + } if (SDL_Init(SDL_INIT_EVENTS | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) != 0) { printf("NATIVE METHOD: SDL_Init failed: %s\n", SDL_GetError());