2017-01-21 12:53:03 +03:00
|
|
|
// 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>
|
2018-08-03 04:51:08 +03:00
|
|
|
#include <utility>
|
2017-01-21 12:53:03 +03:00
|
|
|
#include "input_common/keyboard.h"
|
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
|
|
|
|
class KeyButton final : public Input::ButtonDevice {
|
|
|
|
public:
|
2021-03-06 04:21:04 +03:00
|
|
|
explicit KeyButton(std::shared_ptr<KeyButtonList> key_button_list_, bool toggle_)
|
|
|
|
: key_button_list(std::move(key_button_list_)), toggle(toggle_) {}
|
2017-01-21 12:53:03 +03:00
|
|
|
|
2018-08-03 04:41:51 +03:00
|
|
|
~KeyButton() override;
|
2017-01-21 12:53:03 +03:00
|
|
|
|
|
|
|
bool GetStatus() const override {
|
2021-03-06 04:21:04 +03:00
|
|
|
if (toggle) {
|
2021-03-06 22:27:02 +03:00
|
|
|
return toggled_status.load(std::memory_order_relaxed);
|
2021-03-06 04:21:04 +03:00
|
|
|
}
|
2017-01-21 12:53:03 +03:00
|
|
|
return status.load();
|
|
|
|
}
|
|
|
|
|
2021-03-06 04:21:04 +03:00
|
|
|
void ToggleButton() {
|
2021-03-06 22:27:02 +03:00
|
|
|
if (lock) {
|
|
|
|
return;
|
2021-03-06 04:21:04 +03:00
|
|
|
}
|
2021-03-06 22:27:02 +03:00
|
|
|
lock = true;
|
|
|
|
const bool old_toggle_status = toggled_status.load();
|
|
|
|
toggled_status.store(!old_toggle_status);
|
2021-03-06 04:21:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnlockButton() {
|
|
|
|
lock = false;
|
|
|
|
}
|
|
|
|
|
2017-01-21 12:53:03 +03:00
|
|
|
friend class KeyButtonList;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<KeyButtonList> key_button_list;
|
|
|
|
std::atomic<bool> status{false};
|
2021-03-06 04:21:04 +03:00
|
|
|
std::atomic<bool> toggled_status{false};
|
2021-03-06 22:27:02 +03:00
|
|
|
bool lock{false};
|
2021-03-06 04:21:04 +03:00
|
|
|
const bool toggle;
|
2017-01-21 12:53:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct KeyButtonPair {
|
|
|
|
int key_code;
|
|
|
|
KeyButton* key_button;
|
|
|
|
};
|
|
|
|
|
|
|
|
class KeyButtonList {
|
|
|
|
public:
|
|
|
|
void AddKeyButton(int key_code, KeyButton* key_button) {
|
2019-04-01 19:29:59 +03:00
|
|
|
std::lock_guard guard{mutex};
|
2017-01-21 12:53:03 +03:00
|
|
|
list.push_back(KeyButtonPair{key_code, key_button});
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveKeyButton(const KeyButton* key_button) {
|
2019-04-01 19:29:59 +03:00
|
|
|
std::lock_guard guard{mutex};
|
2017-01-21 12:53:03 +03:00
|
|
|
list.remove_if(
|
|
|
|
[key_button](const KeyButtonPair& pair) { return pair.key_button == key_button; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChangeKeyStatus(int key_code, bool pressed) {
|
2019-04-01 19:29:59 +03:00
|
|
|
std::lock_guard guard{mutex};
|
2017-01-21 12:53:03 +03:00
|
|
|
for (const KeyButtonPair& pair : list) {
|
2020-10-14 09:51:14 +03:00
|
|
|
if (pair.key_code == key_code) {
|
2017-01-21 12:53:03 +03:00
|
|
|
pair.key_button->status.store(pressed);
|
2021-03-06 04:21:04 +03:00
|
|
|
if (pressed) {
|
|
|
|
pair.key_button->ToggleButton();
|
|
|
|
} else {
|
|
|
|
pair.key_button->UnlockButton();
|
|
|
|
}
|
2021-05-30 07:35:46 +03:00
|
|
|
pair.key_button->TriggerOnChange();
|
2020-10-14 09:51:14 +03:00
|
|
|
}
|
2017-01-21 12:53:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:41:25 +03:00
|
|
|
void ChangeAllKeyStatus(bool pressed) {
|
2019-04-01 19:29:59 +03:00
|
|
|
std::lock_guard guard{mutex};
|
2017-03-17 22:41:25 +03:00
|
|
|
for (const KeyButtonPair& pair : list) {
|
|
|
|
pair.key_button->status.store(pressed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 12:53:03 +03:00
|
|
|
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) {
|
2020-10-14 09:51:14 +03:00
|
|
|
const int key_code = params.Get("code", 0);
|
2021-03-06 04:21:04 +03:00
|
|
|
const bool toggle = params.Get("toggle", false);
|
|
|
|
std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list, toggle);
|
2017-01-21 12:53:03 +03:00
|
|
|
key_button_list->AddKeyButton(key_code, button.get());
|
2020-06-19 16:29:36 +03:00
|
|
|
return button;
|
2017-01-21 12:53:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:41:25 +03:00
|
|
|
void Keyboard::ReleaseAllKeys() {
|
|
|
|
key_button_list->ChangeAllKeyStatus(false);
|
|
|
|
}
|
|
|
|
|
2017-01-21 12:53:03 +03:00
|
|
|
} // namespace InputCommon
|