Allow XInput joysticks to work while the game is unfocused

This commit is contained in:
Ricardo Luís Vaz Silva 2024-08-09 17:55:44 -03:00
parent 01e7859e1f
commit 1655f7e3b7
10 changed files with 114 additions and 6 deletions

View file

@ -120,16 +120,30 @@ bool M_LoadJoystickConfig(IJoystickConfig *joy)
{
return false;
}
assert(GameConfig);
value = GameConfig->GetValueForKey("Enabled");
if (value != NULL)
if (value)
{
joy->SetEnabled((bool)atoi(value));
}
if(joy->AllowsEnabledInBackground())
{
value = GameConfig->GetValueForKey("EnabledInBackground");
if (value)
{
joy->SetEnabledInBackground((bool)atoi(value));
}
}
value = GameConfig->GetValueForKey("Sensitivity");
if (value != NULL)
if (value)
{
joy->SetSensitivity((float)atof(value));
}
numaxes = joy->GetNumAxes();
for (int i = 0; i < numaxes; ++i)
{
@ -137,21 +151,21 @@ bool M_LoadJoystickConfig(IJoystickConfig *joy)
mysnprintf(key + axislen, countof(key) - axislen, "deadzone");
value = GameConfig->GetValueForKey(key);
if (value != NULL)
if (value)
{
joy->SetAxisDeadZone(i, (float)atof(value));
}
mysnprintf(key + axislen, countof(key) - axislen, "scale");
value = GameConfig->GetValueForKey(key);
if (value != NULL)
if (value)
{
joy->SetAxisScale(i, (float)atof(value));
}
mysnprintf(key + axislen, countof(key) - axislen, "map");
value = GameConfig->GetValueForKey(key);
if (value != NULL)
if (value)
{
EJoyAxis gameaxis = (EJoyAxis)atoi(value);
if (gameaxis < JOYAXIS_None || gameaxis >= NUM_JOYAXIS)
@ -185,6 +199,12 @@ void M_SaveJoystickConfig(IJoystickConfig *joy)
{
GameConfig->SetValueForKey("Enabled", "0");
}
if (!joy->AllowsEnabledInBackground() && joy->GetEnabledInBackground())
{
GameConfig->SetValueForKey("EnabledInBackground", "1");
}
if (!joy->IsSensitivityDefault())
{
mysnprintf(value, countof(value), "%g", joy->GetSensitivity());

View file

@ -39,6 +39,10 @@ struct IJoystickConfig
virtual bool GetEnabled() = 0;
virtual void SetEnabled(bool enabled) = 0;
virtual bool AllowsEnabledInBackground() = 0;
virtual bool GetEnabledInBackground() = 0;
virtual void SetEnabledInBackground(bool enabled) = 0;
// Used by the saver to not save properties that are at their defaults.
virtual bool IsSensitivityDefault() = 0;
virtual bool IsAxisDeadZoneDefault(int axis) = 0;

View file

@ -133,6 +133,26 @@ DEFINE_ACTION_FUNCTION(IJoystickConfig, SetEnabled)
return 0;
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, AllowsEnabledInBackground)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
ACTION_RETURN_BOOL(self->AllowsEnabledInBackground());
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, GetEnabledInBackground)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
ACTION_RETURN_BOOL(self->GetEnabledInBackground());
}
DEFINE_ACTION_FUNCTION(IJoystickConfig, SetEnabledInBackground)
{
PARAM_SELF_STRUCT_PROLOGUE(IJoystickConfig);
PARAM_BOOL(enabled);
self->SetEnabledInBackground(enabled);
return 0;
}
void UpdateJoystickMenu(IJoystickConfig *selected)
{

View file

@ -107,6 +107,10 @@ public:
virtual bool GetEnabled();
virtual void SetEnabled(bool enabled);
bool AllowsEnabledInBackground() { return false; }
bool GetEnabledInBackground() { return false; }
void SetEnabledInBackground(bool enabled) {}
virtual void SetDefaultConfig();
virtual FString GetIdentifier();

View file

@ -167,6 +167,10 @@ public:
Enabled = enabled;
}
bool AllowsEnabledInBackground() { return false; }
bool GetEnabledInBackground() { return false; }
void SetEnabledInBackground(bool enabled) {}
FString GetIdentifier()
{
char id[16];

View file

@ -183,6 +183,10 @@ public:
bool GetEnabled();
void SetEnabled(bool enabled);
bool AllowsEnabledInBackground() { return false; }
bool GetEnabledInBackground() { return false; }
void SetEnabledInBackground(bool enabled) {}
void SetDefaultConfig();
FString GetIdentifier();

View file

@ -117,6 +117,10 @@ public:
bool GetEnabled();
void SetEnabled(bool enabled);
bool AllowsEnabledInBackground() { return false; }
bool GetEnabledInBackground() { return false; }
void SetEnabledInBackground(bool enabled) {}
void SetDefaultConfig();
FString GetIdentifier();

View file

@ -65,6 +65,8 @@
#endif
#endif
extern bool AppActive;
// TYPES -------------------------------------------------------------------
typedef DWORD (WINAPI *XInputGetStateType)(DWORD index, XINPUT_STATE *state);
@ -105,6 +107,10 @@ public:
bool GetEnabled();
void SetEnabled(bool enabled);
bool AllowsEnabledInBackground() { return true; }
bool GetEnabledInBackground() { return EnabledInBackground; }
void SetEnabledInBackground(bool enabled) { EnabledInBackground = enabled; }
void SetDefaultConfig();
FString GetIdentifier();
@ -142,6 +148,7 @@ protected:
int LastButtons;
bool Connected;
bool Enabled;
bool EnabledInBackground;
void Attached();
void Detached();
@ -744,7 +751,10 @@ void FXInputManager::ProcessInput()
{
for (int i = 0; i < XUSER_MAX_COUNT; ++i)
{
Devices[i]->ProcessInput();
if(AppActive || Devices[i]->GetEnabledInBackground())
{
Devices[i]->ProcessInput();
}
}
}

View file

@ -243,6 +243,29 @@ class OptionMenuJoyEnable : OptionMenuItemOptionBase
}
}
class OptionMenuJoyEnableInBackground : OptionMenuItemOptionBase
{
JoystickConfig mJoy;
OptionMenuJoyEnableInBackground Init(String label, JoystickConfig joy)
{
Super.Init(label,"none","YesNo",null,0);
mJoy = joy;
return self;
}
override int GetSelection()
{
return mJoy.GetEnabledInBackground() ? 1 : 0;
}
override void SetSelection(int Selection)
{
mJoy.SetEnabledInBackground(Selection);
}
}
class OptionMenuItemJoyConfigMenu : OptionMenuItemSubmenu
{
JoystickConfig mJoy;
@ -279,12 +302,23 @@ class OptionMenuItemJoyConfigMenu : OptionMenuItemSubmenu
}
else
{
/*
it = new("OptionMenuItemStaticText").Init(joy.GetName(), false);
opt.mItems.Push(it);
it = new("OptionMenuItemStaticText").Init("", false);
opt.mItems.Push(it);
*/
it = new("OptionMenuJoyEnable").Init("$JOYMNU_JOYENABLE", joy);
opt.mItems.Push(it);
if(joy.AllowsEnabledInBackground())
{
it = new("OptionMenuJoyEnableInBackground").Init("$JOYMNU_JOYENABLEINBACKGROUND", joy);
opt.mItems.Push(it);
}
it = new("OptionMenuSliderJoySensitivity").Init("$JOYMNU_OVRSENS", 0, 2, 0.1, 3, joy);
opt.mItems.Push(it);
it = new("OptionMenuItemStaticText").Init(" ", false);

View file

@ -87,6 +87,10 @@ struct JoystickConfig native version("2.4")
native bool GetEnabled();
native void SetEnabled(bool enabled);
native bool AllowsEnabledInBackground();
native bool GetEnabledInBackground();
native void SetEnabledInBackground(bool enabled);
}
class Menu : Object native ui version("2.4")