diff --git a/src/modules/joystick/JoystickModule.h b/src/modules/joystick/JoystickModule.h index 6cb844971..0671958e6 100644 --- a/src/modules/joystick/JoystickModule.h +++ b/src/modules/joystick/JoystickModule.h @@ -70,6 +70,16 @@ public: **/ virtual int getJoystickCount() const = 0; + /** + * Sets whether joystick input events are produced while the app doesn't have focus. + **/ + virtual void setBackgroundEvents(bool enable) = 0; + + /** + * Gets whether joystick input events are produced while the app doesn't have focus. + **/ + virtual bool hasBackgroundEvents() const = 0; + /** * Sets the virtual Gamepad mapping for a joystick input value for all * joystick devices with the specified joystick product GUID. diff --git a/src/modules/joystick/sdl/JoystickModule.cpp b/src/modules/joystick/sdl/JoystickModule.cpp index 576269249..cb0145c9a 100644 --- a/src/modules/joystick/sdl/JoystickModule.cpp +++ b/src/modules/joystick/sdl/JoystickModule.cpp @@ -95,6 +95,16 @@ int JoystickModule::getJoystickCount() const return (int) activeSticks.size(); } +void JoystickModule::setBackgroundEvents(bool enable) +{ + SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, enable ? "1" : "0"); +} + +bool JoystickModule::hasBackgroundEvents() const +{ + return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, false); +} + love::joystick::Joystick *JoystickModule::getJoystickFromID(int instanceid) { for (auto stick : activeSticks) diff --git a/src/modules/joystick/sdl/JoystickModule.h b/src/modules/joystick/sdl/JoystickModule.h index 0cef2f208..ab3f97d77 100644 --- a/src/modules/joystick/sdl/JoystickModule.h +++ b/src/modules/joystick/sdl/JoystickModule.h @@ -52,6 +52,9 @@ public: int getIndex(const love::joystick::Joystick *joystick) override; int getJoystickCount() const override; + void setBackgroundEvents(bool enable) override; + bool hasBackgroundEvents() const override; + bool setGamepadMapping(const std::string &guid, Joystick::GamepadInput gpinput, Joystick::JoystickInput joyinput) override; void loadGamepadMappings(const std::string &mappings) override; diff --git a/src/modules/joystick/wrap_JoystickModule.cpp b/src/modules/joystick/wrap_JoystickModule.cpp index b026b41f8..98c7bb531 100644 --- a/src/modules/joystick/wrap_JoystickModule.cpp +++ b/src/modules/joystick/wrap_JoystickModule.cpp @@ -65,6 +65,18 @@ int w_getJoystickCount(lua_State *L) return 1; } +int w_setBackgroundEvents(lua_State *L) +{ + instance()->setBackgroundEvents(luax_checkboolean(L, 1)); + return 0; +} + +int w_hasBackgroundEvents(lua_State *L) +{ + luax_pushboolean(L, instance()->hasBackgroundEvents()); + return 1; +} + int w_setGamepadMapping(lua_State *L) { // Only accept a GUID string. We don't accept a Joystick object because @@ -176,6 +188,8 @@ static const luaL_Reg functions[] = { { "getJoysticks", w_getJoysticks }, { "getJoystickCount", w_getJoystickCount }, + { "setBackgroundEvents", w_setBackgroundEvents }, + { "hasBackgroundEvents", w_hasBackgroundEvents }, { "setGamepadMapping", w_setGamepadMapping }, { "loadGamepadMappings", w_loadGamepadMappings }, { "saveGamepadMappings", w_saveGamepadMappings },