Keep keyboard state independent of acquired buttons (#5255)
* Keep keyboard state independent of acquired buttons * Review changes * Address review comments Co-authored-by: zhupengfei <zhupf321@gmail.com>
This commit is contained in:
parent
bf03fb83ae
commit
80c9f9abbb
1 changed files with 20 additions and 25 deletions
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under GPLv2 or any later version
|
// Licensed under GPLv2 or any later version
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
@ -12,10 +13,9 @@ namespace InputCommon {
|
||||||
|
|
||||||
class KeyButton final : public Input::ButtonDevice {
|
class KeyButton final : public Input::ButtonDevice {
|
||||||
public:
|
public:
|
||||||
explicit KeyButton(std::shared_ptr<KeyButtonList> key_button_list_)
|
explicit KeyButton(std::atomic<bool>& status_) : status(status_) {}
|
||||||
: key_button_list(std::move(key_button_list_)) {}
|
|
||||||
|
|
||||||
~KeyButton() override;
|
~KeyButton() override = default;
|
||||||
|
|
||||||
bool GetStatus() const override {
|
bool GetStatus() const override {
|
||||||
return status.load();
|
return status.load();
|
||||||
|
@ -24,40 +24,40 @@ public:
|
||||||
friend class KeyButtonList;
|
friend class KeyButtonList;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<KeyButtonList> key_button_list;
|
std::atomic<bool>& status;
|
||||||
std::atomic<bool> status{false};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct KeyButtonPair {
|
struct KeyButtonPair {
|
||||||
|
explicit KeyButtonPair(int key_code_) : key_code(key_code_) {}
|
||||||
int key_code;
|
int key_code;
|
||||||
KeyButton* key_button;
|
std::atomic<bool> status{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
class KeyButtonList {
|
class KeyButtonList {
|
||||||
public:
|
public:
|
||||||
void AddKeyButton(int key_code, KeyButton* key_button) {
|
KeyButtonPair& AddKeyButton(int key_code) {
|
||||||
std::lock_guard guard{mutex};
|
std::lock_guard guard{mutex};
|
||||||
list.push_back(KeyButtonPair{key_code, key_button});
|
auto it = std::find_if(list.begin(), list.end(), [key_code](const KeyButtonPair& pair) {
|
||||||
|
return pair.key_code == key_code;
|
||||||
|
});
|
||||||
|
if (it == list.end()) {
|
||||||
|
return list.emplace_back(key_code);
|
||||||
}
|
}
|
||||||
|
return *it;
|
||||||
void RemoveKeyButton(const KeyButton* key_button) {
|
|
||||||
std::lock_guard guard{mutex};
|
|
||||||
list.remove_if(
|
|
||||||
[key_button](const KeyButtonPair& pair) { return pair.key_button == key_button; });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChangeKeyStatus(int key_code, bool pressed) {
|
void ChangeKeyStatus(int key_code, bool pressed) {
|
||||||
std::lock_guard guard{mutex};
|
std::lock_guard guard{mutex};
|
||||||
for (const KeyButtonPair& pair : list) {
|
for (KeyButtonPair& pair : list) {
|
||||||
if (pair.key_code == key_code)
|
if (pair.key_code == key_code)
|
||||||
pair.key_button->status.store(pressed);
|
pair.status.store(pressed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChangeAllKeyStatus(bool pressed) {
|
void ChangeAllKeyStatus(bool pressed) {
|
||||||
std::lock_guard guard{mutex};
|
std::lock_guard guard{mutex};
|
||||||
for (const KeyButtonPair& pair : list) {
|
for (KeyButtonPair& pair : list) {
|
||||||
pair.key_button->status.store(pressed);
|
pair.status.store(pressed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,15 +68,10 @@ private:
|
||||||
|
|
||||||
Keyboard::Keyboard() : key_button_list{std::make_shared<KeyButtonList>()} {}
|
Keyboard::Keyboard() : key_button_list{std::make_shared<KeyButtonList>()} {}
|
||||||
|
|
||||||
KeyButton::~KeyButton() {
|
|
||||||
key_button_list->RemoveKeyButton(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_ptr<Input::ButtonDevice> Keyboard::Create(const Common::ParamPackage& params) {
|
std::unique_ptr<Input::ButtonDevice> Keyboard::Create(const Common::ParamPackage& params) {
|
||||||
int key_code = params.Get("code", 0);
|
int key_code = params.Get("code", 0);
|
||||||
std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list);
|
auto& pair = key_button_list->AddKeyButton(key_code);
|
||||||
key_button_list->AddKeyButton(key_code, button.get());
|
return std::make_unique<KeyButton>(pair.status);
|
||||||
return std::move(button);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Keyboard::PressKey(int key_code) {
|
void Keyboard::PressKey(int key_code) {
|
||||||
|
|
Loading…
Reference in a new issue