blob: 98d4f30a2f331a784e4ba6ae56fd29bf53c7669e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <algorithm>
#include <atomic>
#include <list>
#include <mutex>
#include <utility>
#include "input_common/keyboard.h"
namespace InputCommon {
class KeyButton final : public Input::ButtonDevice {
public:
explicit KeyButton(std::atomic<bool>& status_) : status(status_) {}
~KeyButton() override = default;
bool GetStatus() const override {
return status.load();
}
friend class KeyButtonList;
private:
std::atomic<bool>& status;
};
struct KeyButtonPair {
explicit KeyButtonPair(int key_code_) : key_code(key_code_) {}
int key_code;
std::atomic<bool> status{false};
};
class KeyButtonList {
public:
KeyButtonPair& AddKeyButton(int key_code) {
std::lock_guard guard{mutex};
auto it = std::find_if(list.begin(), list.end(), [key_code](const KeyButtonPair& pair) {
return pair.key_code == key_code;
});
if (it == list.end()) {
return list.emplace_back(key_code);
}
return *it;
}
void ChangeKeyStatus(int key_code, bool pressed) {
std::lock_guard guard{mutex};
for (KeyButtonPair& pair : list) {
if (pair.key_code == key_code)
pair.status.store(pressed);
}
}
void ChangeAllKeyStatus(bool pressed) {
std::lock_guard guard{mutex};
for (KeyButtonPair& pair : list) {
pair.status.store(pressed);
}
}
private:
std::mutex mutex;
std::list<KeyButtonPair> list;
};
Keyboard::Keyboard() : key_button_list{std::make_shared<KeyButtonList>()} {}
std::unique_ptr<Input::ButtonDevice> Keyboard::Create(const Common::ParamPackage& params) {
int key_code = params.Get("code", 0);
auto& pair = key_button_list->AddKeyButton(key_code);
return std::make_unique<KeyButton>(pair.status);
}
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);
}
void Keyboard::ReleaseAllKeys() {
key_button_list->ChangeAllKeyStatus(false);
}
} // namespace InputCommon
|