Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only trigger a hotkey action once per press #325

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion headers/gamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "BoardConfig.h"
#include <string.h>

#include "enums.pb.h"
#include "gamepad/GamepadDebouncer.h"
#include "gamepad/GamepadState.h"
#include "gamepad/descriptors/HIDDescriptors.h"
Expand Down Expand Up @@ -66,7 +67,7 @@ class Gamepad {
void save();
void debounce();

GamepadHotkey hotkey();
void hotkey();

/**
* @brief Flag to indicate analog trigger support.
Expand Down Expand Up @@ -168,6 +169,7 @@ class Gamepad {
void releaseAllKeys(void);
void pressKey(uint8_t code);
uint8_t getModifier(uint8_t code);
void processHotkeyIfNewAction(GamepadHotkey action);

GamepadOptions& options;

Expand All @@ -179,6 +181,8 @@ class Gamepad {
HotkeyEntry hotkeyF2Down;
HotkeyEntry hotkeyF2Left;
HotkeyEntry hotkeyF2Right;

GamepadHotkey lastAction = HOTKEY_NONE;
};

#endif
60 changes: 34 additions & 26 deletions src/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,10 @@ void Gamepad::save()
Storage::getInstance().save();
}

GamepadHotkey Gamepad::hotkey()
void Gamepad::hotkey()
{
if (options.lockHotkeys) return HOTKEY_NONE;
if (options.lockHotkeys) return;

static GamepadHotkey lastAction = HOTKEY_NONE;
GamepadHotkey action = HOTKEY_NONE;
if (pressedF1())
{
Expand All @@ -291,33 +290,42 @@ GamepadHotkey Gamepad::hotkey()
state.dpad = 0;
state.buttons &= ~(f2Mask);
}
} else {
// no hotkey pressed, set reset last action so we can process the next press
lastAction = HOTKEY_NONE;
}
processHotkeyIfNewAction(action);
}

switch (action) {
case HOTKEY_NONE : return action;
case HOTKEY_DPAD_DIGITAL : options.dpadMode = DPAD_MODE_DIGITAL; break;
case HOTKEY_DPAD_LEFT_ANALOG : options.dpadMode = DPAD_MODE_LEFT_ANALOG; break;
case HOTKEY_DPAD_RIGHT_ANALOG : options.dpadMode = DPAD_MODE_RIGHT_ANALOG; break;
case HOTKEY_HOME_BUTTON : state.buttons |= GAMEPAD_MASK_A1; break; // Press the Home button
case HOTKEY_CAPTURE_BUTTON :
break;
case HOTKEY_SOCD_UP_PRIORITY : options.socdMode = SOCD_MODE_UP_PRIORITY; break;
case HOTKEY_SOCD_NEUTRAL : options.socdMode = SOCD_MODE_NEUTRAL; break;
case HOTKEY_SOCD_LAST_INPUT : options.socdMode = SOCD_MODE_SECOND_INPUT_PRIORITY; break;
case HOTKEY_SOCD_FIRST_INPUT : options.socdMode = SOCD_MODE_FIRST_INPUT_PRIORITY; break;
case HOTKEY_SOCD_BYPASS : options.socdMode = SOCD_MODE_BYPASS; break;
case HOTKEY_INVERT_X_AXIS : break;
case HOTKEY_INVERT_Y_AXIS :
if (lastAction != HOTKEY_INVERT_Y_AXIS)
options.invertYAxis = !options.invertYAxis;
break;
}
/**
* @brief Take a hotkey action if it hasn't already been taken, modifying state/options appropriately.
*/
void Gamepad::processHotkeyIfNewAction(GamepadHotkey action)
{
if (action != lastAction) {
switch (action) {
case HOTKEY_NONE : return;
case HOTKEY_DPAD_DIGITAL : options.dpadMode = DPAD_MODE_DIGITAL; break;
case HOTKEY_DPAD_LEFT_ANALOG : options.dpadMode = DPAD_MODE_LEFT_ANALOG; break;
case HOTKEY_DPAD_RIGHT_ANALOG : options.dpadMode = DPAD_MODE_RIGHT_ANALOG; break;
case HOTKEY_HOME_BUTTON : state.buttons |= GAMEPAD_MASK_A1; break; // Press the Home button
case HOTKEY_CAPTURE_BUTTON :
break;
case HOTKEY_SOCD_UP_PRIORITY : options.socdMode = SOCD_MODE_UP_PRIORITY; break;
case HOTKEY_SOCD_NEUTRAL : options.socdMode = SOCD_MODE_NEUTRAL; break;
case HOTKEY_SOCD_LAST_INPUT : options.socdMode = SOCD_MODE_SECOND_INPUT_PRIORITY; break;
case HOTKEY_SOCD_FIRST_INPUT : options.socdMode = SOCD_MODE_FIRST_INPUT_PRIORITY; break;
case HOTKEY_SOCD_BYPASS : options.socdMode = SOCD_MODE_BYPASS; break;
case HOTKEY_INVERT_X_AXIS : break;
case HOTKEY_INVERT_Y_AXIS :
if (lastAction != HOTKEY_INVERT_Y_AXIS)
options.invertYAxis = !options.invertYAxis;
break;
}

GamepadHotkey hotkey = action;
if (hotkey != GamepadHotkey::HOTKEY_NONE)
lastAction = action;
save();

return hotkey;
}
}


Expand Down