Skip to content

Commit

Permalink
Merge pull request #18596 from hrydgard/more-control-refactor
Browse files Browse the repository at this point in the history
More control refactor
  • Loading branch information
hrydgard committed Dec 21, 2023
2 parents 03b8826 + 16a31c2 commit 8601c5a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
35 changes: 23 additions & 12 deletions Core/ControlMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -165,6 +168,7 @@ void ControlMapper::ForceReleaseVKey(int vkey) {
}
}
}
KeyMap::UnlockMappings();
}

static int RotatePSPKeyCode(int x) {
Expand Down Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -502,6 +512,7 @@ void ControlMapper::Axis(const AxisInput *axes, size_t count) {
UpdatePSPState(opposite, now);
}
}
KeyMap::UnlockMappings();
}

void ControlMapper::Update(double now) {
Expand Down
27 changes: 19 additions & 8 deletions Core/KeyMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion Core/KeyMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);

Expand Down
2 changes: 0 additions & 2 deletions GPU/Common/DrawEngineCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions UI/ControlMappingScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 8601c5a

Please sign in to comment.