Merge pull request #18596 from hrydgard/more-control-refactor

More control refactor
This commit is contained in:
Henrik Rydgård authored and GitHub committed 2023-12-21 13:40:41 +01:00
commit 8601c5a3bc
5 files changed
+51 -25

No files matched your search

+23 -12
View File
@@ -154,8 +154,11 @@ void ControlMapper::UpdateAnalogOutput(int stick) {
}
void ControlMapper::ForceReleaseVKey(int vkey) {
// Note: This one is called from an onVKey_ handler, which already holds mutex_.
KeyMap::LockMappings();
std::vector<KeyMap::MultiInputMapping> multiMappings;
if (KeyMap::InputMappingsFromPspButton(vkey, &multiMappings, true)) {
if (KeyMap::InputMappingsFromPspButtonNoLock(vkey, &multiMappings, true)) {
double now = time_now_d();
for (const auto &entry : multiMappings) {
for (const auto &mapping : entry.mappings) {
@@ -165,6 +168,7 @@ void ControlMapper::ForceReleaseVKey(int vkey) {
}
}
}
KeyMap::UnlockMappings();
}
static int RotatePSPKeyCode(int x) {
@@ -251,7 +255,7 @@ void ControlMapper::SwapMappingIfEnabled(uint32_t *vkey) {
}
// Can only be called from Key or Axis.
// mutex_ should be locked.
// mutex_ should be locked, and also KeyMap::LockMappings().
// TODO: We should probably make a batched version of this.
bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping, double now) {
// Instead of taking an input key and finding what it outputs, we loop through the OUTPUTS and
@@ -268,6 +272,7 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping, double no
// For the PSP's button inputs, we just go through and put the flags together.
uint32_t buttonMask = 0;
uint32_t changedButtonMask = 0;
std::vector<MultiInputMapping> inputMappings;
for (int i = 0; i < 32; i++) {
uint32_t mask = 1 << i;
if (!(mask & CTRL_MASK_USER)) {
@@ -281,9 +286,7 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping, double no
}
SwapMappingIfEnabled(&mappingBit);
std::vector<MultiInputMapping> inputMappings;
if (!KeyMap::InputMappingsFromPspButton(mappingBit, &inputMappings, false))
if (!KeyMap::InputMappingsFromPspButtonNoLock(mappingBit, &inputMappings, false))
continue;
// If a mapping could consist of a combo, we could trivially check it here.
@@ -316,12 +319,11 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping, double no
// Note that virtual keys include the analog directions, as they are driven by them.
for (int i = 0; i < VIRTKEY_COUNT; i++) {
int vkId = i + VIRTKEY_FIRST;
std::vector<MultiInputMapping> inputMappings;
uint32_t idForMapping = vkId;
SwapMappingIfEnabled(&idForMapping);
if (!KeyMap::InputMappingsFromPspButton(idForMapping, &inputMappings, false))
if (!KeyMap::InputMappingsFromPspButtonNoLock(idForMapping, &inputMappings, false))
continue;
// If a mapping could consist of a combo, we could trivially check it here.
@@ -423,15 +425,16 @@ bool ControlMapper::Key(const KeyInput &key, bool *pauseTrigger) {
// Claim that we handled this. Prevents volume key repeats from popping up the volume control on Android.
return true;
}
double now = time_now_d();
if (key.deviceId < DEVICE_ID_COUNT) {
deviceTimestamps_[(int)key.deviceId] = now;
}
double now = time_now_d();
InputMapping mapping(key.deviceId, key.keyCode);
std::lock_guard<std::mutex> guard(mutex_);
if (key.deviceId < DEVICE_ID_COUNT) {
deviceTimestamps_[(int)key.deviceId] = now;
}
if (key.flags & KEY_DOWN) {
curInput_[mapping] = { 1.0f, now };
} else if (key.flags & KEY_UP) {
@@ -448,10 +451,15 @@ bool ControlMapper::Key(const KeyInput &key, bool *pauseTrigger) {
}
}
return UpdatePSPState(mapping, now);
KeyMap::LockMappings();
bool retval = UpdatePSPState(mapping, now);
KeyMap::UnlockMappings();
return retval;
}
void ControlMapper::ToggleSwapAxes() {
std::lock_guard<std::mutex> guard(mutex_);
swapAxes_ = !swapAxes_;
updatePSPButtons_(0, CTRL_LEFT | CTRL_RIGHT | CTRL_UP | CTRL_DOWN);
@@ -480,6 +488,8 @@ void ControlMapper::Axis(const AxisInput *axes, size_t count) {
double now = time_now_d();
std::lock_guard<std::mutex> guard(mutex_);
KeyMap::LockMappings();
for (size_t i = 0; i < count; i++) {
const AxisInput &axis = axes[i];
size_t deviceIndex = (size_t)axis.deviceId; // this wraps -1 up high, so will get rejected on the next line.
@@ -502,6 +512,7 @@ void ControlMapper::Axis(const AxisInput *axes, size_t count) {
UpdatePSPState(opposite, now);
}
}
KeyMap::UnlockMappings();
}
void ControlMapper::Update(double now) {
+19 -8
View File
@@ -504,12 +504,9 @@ const char* GetPspButtonNameCharPointer(int btn) {
return nullptr;
}
std::vector<KeyMap_IntStrPair> GetMappableKeys() {
std::vector<KeyMap_IntStrPair> temp;
for (size_t i = 0; i < ARRAY_SIZE(psp_button_names); i++) {
temp.push_back(psp_button_names[i]);
}
return temp;
const KeyMap::KeyMap_IntStrPair *GetMappableKeys(size_t *count) {
*count = ARRAY_SIZE(psp_button_names);
return psp_button_names;
}
bool InputMappingToPspButton(const InputMapping &mapping, std::vector<int> *pspButtons) {
@@ -527,13 +524,14 @@ bool InputMappingToPspButton(const InputMapping &mapping, std::vector<int> *pspB
return found;
}
bool InputMappingsFromPspButton(int btn, std::vector<MultiInputMapping> *mappings, bool ignoreMouse) {
std::lock_guard<std::recursive_mutex> guard(g_controllerMapLock);
// This is the main workhorse of the ControlMapper.
bool InputMappingsFromPspButtonNoLock(int btn, std::vector<MultiInputMapping> *mappings, bool ignoreMouse) {
auto iter = g_controllerMap.find(btn);
if (iter == g_controllerMap.end()) {
return false;
}
bool mapped = false;
mappings->clear();
for (auto &iter2 : iter->second) {
bool ignore = ignoreMouse && iter2.HasMouse();
if (mappings && !ignore) {
@@ -544,6 +542,19 @@ bool InputMappingsFromPspButton(int btn, std::vector<MultiInputMapping> *mapping
return mapped;
}
bool InputMappingsFromPspButton(int btn, std::vector<MultiInputMapping> *mappings, bool ignoreMouse) {
std::lock_guard<std::recursive_mutex> guard(g_controllerMapLock);
return InputMappingsFromPspButtonNoLock(btn, mappings, ignoreMouse);
}
void LockMappings() {
g_controllerMapLock.lock();
}
void UnlockMappings() {
g_controllerMapLock.unlock();
}
bool PspButtonHasMappings(int btn) {
std::lock_guard<std::recursive_mutex> guard(g_controllerMapLock);
auto iter = g_controllerMap.find(btn);
+6 -1
View File
@@ -172,7 +172,7 @@ namespace KeyMap {
const char *GetVirtKeyName(int vkey);
const char *GetPspButtonNameCharPointer(int btn);
std::vector<KeyMap_IntStrPair> GetMappableKeys();
const KeyMap_IntStrPair *GetMappableKeys(size_t *count);
// Use to translate input mappings to and from PSP buttons. You should have already translated
// your platform's keys to InputMapping keys.
@@ -181,6 +181,11 @@ namespace KeyMap {
bool InputMappingToPspButton(const InputMapping &mapping, std::vector<int> *pspButtons);
bool InputMappingsFromPspButton(int btn, std::vector<MultiInputMapping> *keys, bool ignoreMouse);
// Careful with these.
bool InputMappingsFromPspButtonNoLock(int btn, std::vector<MultiInputMapping> *keys, bool ignoreMouse);
void LockMappings();
void UnlockMappings();
// Simplified check.
bool PspButtonHasMappings(int btn);
-2
View File
@@ -846,8 +846,6 @@ int DrawEngineCommon::ExtendNonIndexedPrim(const uint32_t *cmd, const uint32_t *
seenPrims_ |= seenPrims;
_dbg_assert_(cmd != start);
int totalCount = offset - dv.vertexCount;
dv.vertexCount = offset;
dv.indexUpperBound = dv.vertexCount - 1;
+3 -2
View File
@@ -274,7 +274,8 @@ void ControlMappingScreen::CreateViews() {
root_->Add(leftColumn);
root_->Add(rightScroll_);
std::vector<KeyMap::KeyMap_IntStrPair> mappableKeys = KeyMap::GetMappableKeys();
size_t numMappableKeys = 0;
const KeyMap::KeyMap_IntStrPair *mappableKeys = KeyMap::GetMappableKeys(&numMappableKeys);
struct Cat {
const char *catName;
@@ -291,7 +292,7 @@ void ControlMappingScreen::CreateViews() {
int curCat = -1;
CollapsibleSection *curSection = nullptr;
for (size_t i = 0; i < mappableKeys.size(); i++) {
for (size_t i = 0; i < numMappableKeys; i++) {
if (curCat < (int)ARRAY_SIZE(cats) && mappableKeys[i].key == cats[curCat + 1].firstKey) {
if (curCat >= 0 && !cats[curCat].openByDefault) {
curSection->SetOpen(false);