-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButtonComboInfoHold.cpp
More file actions
88 lines (74 loc) · 4.95 KB
/
Copy pathButtonComboInfoHold.cpp
File metadata and controls
88 lines (74 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "ButtonComboInfoHold.h"
#include <logger.h>
ButtonComboInfoHold::ButtonComboInfoHold(std::string label,
const ButtonComboModule_ControllerTypes controllerMask,
const ButtonComboModule_Buttons combo,
const uint32_t targetDurationInMs,
const ButtonComboModule_ComboCallback callback,
void *context,
const bool observer) : ButtonComboInfoIF(std::move(label),
controllerMask,
combo,
callback,
context,
observer),
mTargetDurationInMs(targetDurationInMs) {
DEBUG_FUNCTION_LINE_INFO("Created ButtonComboInfoHold: \"%s\", combo: %08X, targetDurationInMs: %d ms, controllerMask: %08X", mLabel.c_str(), mCombo, mTargetDurationInMs, mControllerMask);
}
ButtonComboInfoHold::~ButtonComboInfoHold() {
DEBUG_FUNCTION_LINE_INFO("Deleted ButtonComboInfoHold: \"%s\", combo: %08X, targetDurationInMs: %d ms, controllerMask: %08X", mLabel.c_str(), mCombo, mTargetDurationInMs, mControllerMask);
}
int ButtonComboInfoHold::UpdateInput(const ButtonComboModule_ControllerTypes controller, const std::span<uint32_t> pressedButtons) {
if ((mControllerMask & controller) == 0) {
return -1;
}
const auto chanIndex = ControllerTypeToChanIndex(controller);
if (chanIndex < 0 || static_cast<uint32_t>(chanIndex) >= std::size(mHoldInformation)) {
DEBUG_FUNCTION_LINE_WARN("ChanIndex is out of bounds %d", chanIndex);
return -1;
}
int activatedIndex = -1;
auto &holdInformation = mHoldInformation[chanIndex];
const auto latestButtonPress = pressedButtons.back();
DEBUG_FUNCTION_LINE_VERBOSE("[HOLD ] Check button combo %08X on controller %08X (lastItem im pressedButtons (size %d) is %08X) for %s [%p]", mCombo, controller, pressedButtons.size(), latestButtonPress, mLabel.c_str(), getHandle().handle);
const bool prevButtonsIncludedCombo = (holdInformation.prevButtonCombo & mCombo) == mCombo; // Make sure the combo can't be triggered on releasing
const bool buttonsPressedChanged = holdInformation.prevButtonCombo != latestButtonPress; // Avoid "holding" the combo
const bool buttonsPressedMatchCombo = latestButtonPress == mCombo; // detect the actual combo
holdInformation.prevButtonCombo = latestButtonPress;
if (!(buttonsPressedChanged && prevButtonsIncludedCombo) && (holdInformation.holdStartedAt > 0 || buttonsPressedChanged) && buttonsPressedMatchCombo) {
if (holdInformation.holdStartedAt == 0) {
holdInformation.holdStartedAt = OSGetTime();
}
const auto intervalInMs = static_cast<uint32_t>(OSTicksToMilliseconds(OSGetTime() - holdInformation.holdStartedAt));
if (intervalInMs > mTargetDurationInMs && !holdInformation.callbackTriggered) {
if (mCallback != nullptr) {
DEBUG_FUNCTION_LINE("Calling callback [%p](controller: %08X context: %p) for \"%s\" [handle: %p], hold %08X for %d ms", mCallback, controller, mContext, mLabel.c_str(), getHandle().handle, mCombo, intervalInMs);
mCallback(controller, getHandle(), mContext);
activatedIndex = pressedButtons.size() - 1;
} else {
DEBUG_FUNCTION_LINE_WARN("Callback was null for combo %p", getHandle().handle);
}
holdInformation.callbackTriggered = true;
}
} else {
holdInformation.callbackTriggered = false;
holdInformation.holdStartedAt = 0;
}
return activatedIndex;
}
ButtonComboModule_Error ButtonComboInfoHold::setHoldDuration(const uint32_t holdDurationInMs) {
DEBUG_FUNCTION_LINE("Setting holdDurationInMs to %d for %s [%p]", holdDurationInMs, mLabel.c_str(), getHandle().handle);
mTargetDurationInMs = holdDurationInMs;
return BUTTON_COMBO_MODULE_ERROR_SUCCESS;
}
ButtonComboModule_ButtonComboInfoEx ButtonComboInfoHold::getComboInfoEx() const {
return {.type = mIsObserver ? BUTTON_COMBO_MODULE_COMBO_TYPE_HOLD_OBSERVER : BUTTON_COMBO_MODULE_COMBO_TYPE_HOLD,
.basicCombo = {.controllerMask = mControllerMask, .combo = mCombo},
.optionalHoldForXMs = mTargetDurationInMs};
}
void ButtonComboInfoHold::resetPrevInput() {
for (uint32_t i = 0; i < std::size(mHoldInformation); ++i) {
mHoldInformation[i] = {};
mHoldInformation[i].prevButtonCombo = 0xFFFFFFFF;
}
}