Add love.joystick.setBackgroundEvents(bool) and hasBackgroundEvents.

Resolves #2270.
This commit is contained in:
Sasha Szpakowski
2026-03-30 21:13:13 -03:00
parent 9d3b1c7c73
commit b0a2cb4d7d
4 changed files with 37 additions and 0 deletions
+10
View File
@@ -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.
@@ -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)
@@ -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;
@@ -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 },