diff options
| author | 2021-09-20 17:18:40 -0500 | |
|---|---|---|
| committer | 2021-11-24 20:30:22 -0600 | |
| commit | 5a785ed794fff8c944283271bf25cb835c11700a (patch) | |
| tree | dc99f3fb595a57e9629661d3002dad724914275b /src/input_common/keyboard.cpp | |
| parent | input_common: Move touch and analog from button. Move udp protocol (diff) | |
| download | yuzu-5a785ed794fff8c944283271bf25cb835c11700a.tar.gz yuzu-5a785ed794fff8c944283271bf25cb835c11700a.tar.xz yuzu-5a785ed794fff8c944283271bf25cb835c11700a.zip | |
input_common: Rewrite keyboard
Diffstat (limited to 'src/input_common/keyboard.cpp')
| -rw-r--r-- | src/input_common/keyboard.cpp | 121 |
1 files changed, 0 insertions, 121 deletions
diff --git a/src/input_common/keyboard.cpp b/src/input_common/keyboard.cpp deleted file mode 100644 index 8261e76fd..000000000 --- a/src/input_common/keyboard.cpp +++ /dev/null | |||
| @@ -1,121 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <atomic> | ||
| 6 | #include <list> | ||
| 7 | #include <mutex> | ||
| 8 | #include <utility> | ||
| 9 | #include "input_common/keyboard.h" | ||
| 10 | |||
| 11 | namespace InputCommon { | ||
| 12 | |||
| 13 | class KeyButton final : public Input::ButtonDevice { | ||
| 14 | public: | ||
| 15 | explicit KeyButton(std::shared_ptr<KeyButtonList> key_button_list_, bool toggle_) | ||
| 16 | : key_button_list(std::move(key_button_list_)), toggle(toggle_) {} | ||
| 17 | |||
| 18 | ~KeyButton() override; | ||
| 19 | |||
| 20 | bool GetStatus() const override { | ||
| 21 | if (toggle) { | ||
| 22 | return toggled_status.load(std::memory_order_relaxed); | ||
| 23 | } | ||
| 24 | return status.load(); | ||
| 25 | } | ||
| 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 | |||
| 40 | friend class KeyButtonList; | ||
| 41 | |||
| 42 | private: | ||
| 43 | std::shared_ptr<KeyButtonList> key_button_list; | ||
| 44 | std::atomic<bool> status{false}; | ||
| 45 | std::atomic<bool> toggled_status{false}; | ||
| 46 | bool lock{false}; | ||
| 47 | const bool toggle; | ||
| 48 | }; | ||
| 49 | |||
| 50 | struct KeyButtonPair { | ||
| 51 | int key_code; | ||
| 52 | KeyButton* key_button; | ||
| 53 | }; | ||
| 54 | |||
| 55 | class KeyButtonList { | ||
| 56 | public: | ||
| 57 | void AddKeyButton(int key_code, KeyButton* key_button) { | ||
| 58 | std::lock_guard guard{mutex}; | ||
| 59 | list.push_back(KeyButtonPair{key_code, key_button}); | ||
| 60 | } | ||
| 61 | |||
| 62 | void RemoveKeyButton(const KeyButton* key_button) { | ||
| 63 | std::lock_guard guard{mutex}; | ||
| 64 | list.remove_if( | ||
| 65 | [key_button](const KeyButtonPair& pair) { return pair.key_button == key_button; }); | ||
| 66 | } | ||
| 67 | |||
| 68 | void ChangeKeyStatus(int key_code, bool pressed) { | ||
| 69 | std::lock_guard guard{mutex}; | ||
| 70 | for (const KeyButtonPair& pair : list) { | ||
| 71 | if (pair.key_code == key_code) { | ||
| 72 | pair.key_button->status.store(pressed); | ||
| 73 | if (pressed) { | ||
| 74 | pair.key_button->ToggleButton(); | ||
| 75 | } else { | ||
| 76 | pair.key_button->UnlockButton(); | ||
| 77 | } | ||
| 78 | pair.key_button->TriggerOnChange(); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | void ChangeAllKeyStatus(bool pressed) { | ||
| 84 | std::lock_guard guard{mutex}; | ||
| 85 | for (const KeyButtonPair& pair : list) { | ||
| 86 | pair.key_button->status.store(pressed); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | private: | ||
| 91 | std::mutex mutex; | ||
| 92 | std::list<KeyButtonPair> list; | ||
| 93 | }; | ||
| 94 | |||
| 95 | Keyboard::Keyboard() : key_button_list{std::make_shared<KeyButtonList>()} {} | ||
| 96 | |||
| 97 | KeyButton::~KeyButton() { | ||
| 98 | key_button_list->RemoveKeyButton(this); | ||
| 99 | } | ||
| 100 | |||
| 101 | std::unique_ptr<Input::ButtonDevice> Keyboard::Create(const Common::ParamPackage& params) { | ||
| 102 | const int key_code = params.Get("code", 0); | ||
| 103 | const bool toggle = params.Get("toggle", false); | ||
| 104 | std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list, toggle); | ||
| 105 | key_button_list->AddKeyButton(key_code, button.get()); | ||
| 106 | return button; | ||
| 107 | } | ||
| 108 | |||
| 109 | void Keyboard::PressKey(int key_code) { | ||
| 110 | key_button_list->ChangeKeyStatus(key_code, true); | ||
| 111 | } | ||
| 112 | |||
| 113 | void Keyboard::ReleaseKey(int key_code) { | ||
| 114 | key_button_list->ChangeKeyStatus(key_code, false); | ||
| 115 | } | ||
| 116 | |||
| 117 | void Keyboard::ReleaseAllKeys() { | ||
| 118 | key_button_list->ChangeAllKeyStatus(false); | ||
| 119 | } | ||
| 120 | |||
| 121 | } // namespace InputCommon | ||