diff options
author | bunnei <bunneidev@gmail.com> | 2017-03-17 19:59:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-17 19:59:39 +0100 |
commit | 423ab5e2bcf5a522e5f412447c05f648df57a14c (patch) | |
tree | 1e60eaeffa59229254a47f885d2fe2cbbdc1a5c0 /src/input_common/keyboard.cpp | |
parent | Merge pull request #2618 from wwylele/log-less-filename (diff) | |
parent | qt/config_input: don't connect for null button (diff) | |
download | yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.gz yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.bz2 yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.lz yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.xz yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.zst yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.zip |
Diffstat (limited to '')
-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 @@ +// Copyright 2017 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include <atomic> +#include <list> +#include <mutex> +#include "input_common/keyboard.h" + +namespace InputCommon { + +class KeyButton final : public Input::ButtonDevice { +public: + explicit KeyButton(std::shared_ptr<KeyButtonList> key_button_list_) + : key_button_list(key_button_list_) {} + + ~KeyButton(); + + bool GetStatus() const override { + return status.load(); + } + + friend class KeyButtonList; + +private: + std::shared_ptr<KeyButtonList> key_button_list; + std::atomic<bool> status{false}; +}; + +struct KeyButtonPair { + int key_code; + KeyButton* key_button; +}; + +class KeyButtonList { +public: + void AddKeyButton(int key_code, KeyButton* key_button) { + std::lock_guard<std::mutex> guard(mutex); + list.push_back(KeyButtonPair{key_code, key_button}); + } + + void RemoveKeyButton(const KeyButton* key_button) { + std::lock_guard<std::mutex> guard(mutex); + list.remove_if( + [key_button](const KeyButtonPair& pair) { return pair.key_button == key_button; }); + } + + void ChangeKeyStatus(int key_code, bool pressed) { + std::lock_guard<std::mutex> guard(mutex); + for (const KeyButtonPair& pair : list) { + if (pair.key_code == key_code) + pair.key_button->status.store(pressed); + } + } + +private: + std::mutex mutex; + std::list<KeyButtonPair> list; +}; + +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) { + int key_code = params.Get("code", 0); + std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list); + key_button_list->AddKeyButton(key_code, button.get()); + return std::move(button); +} + +void Keyboard::PressKey(int key_code) { + key_button_list->ChangeKeyStatus(key_code, true); +} + +void Keyboard::ReleaseKey(int key_code) { + key_button_list->ChangeKeyStatus(key_code, false); +} + +} // namespace InputCommon |