diff options
| author | 2017-01-21 11:53:03 +0200 | |
|---|---|---|
| committer | 2017-03-01 23:30:57 +0200 | |
| commit | 38e800f70d122051e12ac9f22e23d84b97fec424 (patch) | |
| tree | 0f52b5731a4fd8e9f2a39e8e75d1fa18ce8b9107 /src/input_common/keyboard.cpp | |
| parent | HID: use AnalogDevice (diff) | |
| download | yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.tar.gz yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.tar.xz yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.zip | |
InputCommon: add Keyboard
Diffstat (limited to 'src/input_common/keyboard.cpp')
| -rw-r--r-- | src/input_common/keyboard.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/input_common/keyboard.cpp b/src/input_common/keyboard.cpp new file mode 100644 index 000000000..a8fc01f2e --- /dev/null +++ b/src/input_common/keyboard.cpp | |||
| @@ -0,0 +1,82 @@ | |||
| 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 "input_common/keyboard.h" | ||
| 9 | |||
| 10 | namespace InputCommon { | ||
| 11 | |||
| 12 | class KeyButton final : public Input::ButtonDevice { | ||
| 13 | public: | ||
| 14 | explicit KeyButton(std::shared_ptr<KeyButtonList> key_button_list_) | ||
| 15 | : key_button_list(key_button_list_) {} | ||
| 16 | |||
| 17 | ~KeyButton(); | ||
| 18 | |||
| 19 | bool GetStatus() const override { | ||
| 20 | return status.load(); | ||
| 21 | } | ||
| 22 | |||
| 23 | friend class KeyButtonList; | ||
| 24 | |||
| 25 | private: | ||
| 26 | std::shared_ptr<KeyButtonList> key_button_list; | ||
| 27 | std::atomic<bool> status{false}; | ||
| 28 | }; | ||
| 29 | |||
| 30 | struct KeyButtonPair { | ||
| 31 | int key_code; | ||
| 32 | KeyButton* key_button; | ||
| 33 | }; | ||
| 34 | |||
| 35 | class KeyButtonList { | ||
| 36 | public: | ||
| 37 | void AddKeyButton(int key_code, KeyButton* key_button) { | ||
| 38 | std::lock_guard<std::mutex> guard(mutex); | ||
| 39 | list.push_back(KeyButtonPair{key_code, key_button}); | ||
| 40 | } | ||
| 41 | |||
| 42 | void RemoveKeyButton(const KeyButton* key_button) { | ||
| 43 | std::lock_guard<std::mutex> guard(mutex); | ||
| 44 | list.remove_if( | ||
| 45 | [key_button](const KeyButtonPair& pair) { return pair.key_button == key_button; }); | ||
| 46 | } | ||
| 47 | |||
| 48 | void ChangeKeyStatus(int key_code, bool pressed) { | ||
| 49 | std::lock_guard<std::mutex> guard(mutex); | ||
| 50 | for (const KeyButtonPair& pair : list) { | ||
| 51 | if (pair.key_code == key_code) | ||
| 52 | pair.key_button->status.store(pressed); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | private: | ||
| 57 | std::mutex mutex; | ||
| 58 | std::list<KeyButtonPair> list; | ||
| 59 | }; | ||
| 60 | |||
| 61 | Keyboard::Keyboard() : key_button_list{std::make_shared<KeyButtonList>()} {} | ||
| 62 | |||
| 63 | KeyButton::~KeyButton() { | ||
| 64 | key_button_list->RemoveKeyButton(this); | ||
| 65 | } | ||
| 66 | |||
| 67 | std::unique_ptr<Input::ButtonDevice> Keyboard::Create(const Common::ParamPackage& params) { | ||
| 68 | int key_code = params.Get("code", 0); | ||
| 69 | std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list); | ||
| 70 | key_button_list->AddKeyButton(key_code, button.get()); | ||
| 71 | return std::move(button); | ||
| 72 | } | ||
| 73 | |||
| 74 | void Keyboard::PressKey(int key_code) { | ||
| 75 | key_button_list->ChangeKeyStatus(key_code, true); | ||
| 76 | } | ||
| 77 | |||
| 78 | void Keyboard::ReleaseKey(int key_code) { | ||
| 79 | key_button_list->ChangeKeyStatus(key_code, false); | ||
| 80 | } | ||
| 81 | |||
| 82 | } // namespace InputCommon | ||