summaryrefslogtreecommitdiff
path: root/src/input_common/keyboard.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/keyboard.cpp')
-rw-r--r--src/input_common/keyboard.cpp31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/input_common/keyboard.cpp b/src/input_common/keyboard.cpp
index 24a6f7a33..c467ff4c5 100644
--- a/src/input_common/keyboard.cpp
+++ b/src/input_common/keyboard.cpp
@@ -12,20 +12,39 @@ namespace InputCommon {
12 12
13class KeyButton final : public Input::ButtonDevice { 13class KeyButton final : public Input::ButtonDevice {
14public: 14public:
15 explicit KeyButton(std::shared_ptr<KeyButtonList> key_button_list_) 15 explicit KeyButton(std::shared_ptr<KeyButtonList> key_button_list_, bool toggle_)
16 : key_button_list(std::move(key_button_list_)) {} 16 : key_button_list(std::move(key_button_list_)), toggle(toggle_) {}
17 17
18 ~KeyButton() override; 18 ~KeyButton() override;
19 19
20 bool GetStatus() const override { 20 bool GetStatus() const override {
21 if (toggle) {
22 return toggled_status.load(std::memory_order_relaxed);
23 }
21 return status.load(); 24 return status.load();
22 } 25 }
23 26
27 void ToggleButton() {
28 if (lock) {
29 return;
30 }
31 lock = true;
32 const bool old_toggle_status = toggled_status.load();
33 toggled_status.store(!old_toggle_status);
34 }
35
36 void UnlockButton() {
37 lock = false;
38 }
39
24 friend class KeyButtonList; 40 friend class KeyButtonList;
25 41
26private: 42private:
27 std::shared_ptr<KeyButtonList> key_button_list; 43 std::shared_ptr<KeyButtonList> key_button_list;
28 std::atomic<bool> status{false}; 44 std::atomic<bool> status{false};
45 std::atomic<bool> toggled_status{false};
46 bool lock{false};
47 const bool toggle;
29}; 48};
30 49
31struct KeyButtonPair { 50struct KeyButtonPair {
@@ -51,6 +70,11 @@ public:
51 for (const KeyButtonPair& pair : list) { 70 for (const KeyButtonPair& pair : list) {
52 if (pair.key_code == key_code) { 71 if (pair.key_code == key_code) {
53 pair.key_button->status.store(pressed); 72 pair.key_button->status.store(pressed);
73 if (pressed) {
74 pair.key_button->ToggleButton();
75 } else {
76 pair.key_button->UnlockButton();
77 }
54 } 78 }
55 } 79 }
56 } 80 }
@@ -75,7 +99,8 @@ KeyButton::~KeyButton() {
75 99
76std::unique_ptr<Input::ButtonDevice> Keyboard::Create(const Common::ParamPackage& params) { 100std::unique_ptr<Input::ButtonDevice> Keyboard::Create(const Common::ParamPackage& params) {
77 const int key_code = params.Get("code", 0); 101 const int key_code = params.Get("code", 0);
78 std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list); 102 const bool toggle = params.Get("toggle", false);
103 std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list, toggle);
79 key_button_list->AddKeyButton(key_code, button.get()); 104 key_button_list->AddKeyButton(key_code, button.get());
80 return button; 105 return button;
81} 106}