summaryrefslogtreecommitdiff
path: root/src/input_common
diff options
context:
space:
mode:
authorGravatar wwylele2017-01-21 11:53:03 +0200
committerGravatar wwylele2017-03-01 23:30:57 +0200
commit38e800f70d122051e12ac9f22e23d84b97fec424 (patch)
tree0f52b5731a4fd8e9f2a39e8e75d1fa18ce8b9107 /src/input_common
parentHID: use AnalogDevice (diff)
downloadyuzu-38e800f70d122051e12ac9f22e23d84b97fec424.tar.gz
yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.tar.xz
yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.zip
InputCommon: add Keyboard
Diffstat (limited to '')
-rw-r--r--src/input_common/CMakeLists.txt15
-rw-r--r--src/input_common/keyboard.cpp82
-rw-r--r--src/input_common/keyboard.h45
-rw-r--r--src/input_common/main.cpp35
-rw-r--r--src/input_common/main.h25
5 files changed, 202 insertions, 0 deletions
diff --git a/src/input_common/CMakeLists.txt b/src/input_common/CMakeLists.txt
new file mode 100644
index 000000000..ac1ad45a9
--- /dev/null
+++ b/src/input_common/CMakeLists.txt
@@ -0,0 +1,15 @@
1set(SRCS
2 keyboard.cpp
3 main.cpp
4 )
5
6set(HEADERS
7 keyboard.h
8 main.h
9 )
10
11create_directory_groups(${SRCS} ${HEADERS})
12
13add_library(input_common STATIC ${SRCS} ${HEADERS})
14target_link_libraries(input_common common core)
15
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
10namespace InputCommon {
11
12class KeyButton final : public Input::ButtonDevice {
13public:
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
25private:
26 std::shared_ptr<KeyButtonList> key_button_list;
27 std::atomic<bool> status{false};
28};
29
30struct KeyButtonPair {
31 int key_code;
32 KeyButton* key_button;
33};
34
35class KeyButtonList {
36public:
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
56private:
57 std::mutex mutex;
58 std::list<KeyButtonPair> list;
59};
60
61Keyboard::Keyboard() : key_button_list{std::make_shared<KeyButtonList>()} {}
62
63KeyButton::~KeyButton() {
64 key_button_list->RemoveKeyButton(this);
65}
66
67std::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
74void Keyboard::PressKey(int key_code) {
75 key_button_list->ChangeKeyStatus(key_code, true);
76}
77
78void Keyboard::ReleaseKey(int key_code) {
79 key_button_list->ChangeKeyStatus(key_code, false);
80}
81
82} // namespace InputCommon
diff --git a/src/input_common/keyboard.h b/src/input_common/keyboard.h
new file mode 100644
index 000000000..76359aa30
--- /dev/null
+++ b/src/input_common/keyboard.h
@@ -0,0 +1,45 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include "core/frontend/input.h"
9
10namespace InputCommon {
11
12class KeyButtonList;
13
14/**
15 * A button device factory representing a keyboard. It receives keyboard events and forward them
16 * to all button devices it created.
17 */
18class Keyboard final : public Input::Factory<Input::ButtonDevice> {
19public:
20 Keyboard();
21
22 /**
23 * Creates a button device from a keyboard key
24 * @param params contains parameters for creating the device:
25 * - "code": the code of the key to bind with the button
26 */
27 std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override;
28
29 /**
30 * Sets the status of all buttons bound with the key to pressed
31 * @param key_code the code of the key to press
32 */
33 void PressKey(int key_code);
34
35 /**
36 * Sets the status of all buttons bound with the key to released
37 * @param key_code the code of the key to release
38 */
39 void ReleaseKey(int key_code);
40
41private:
42 std::shared_ptr<KeyButtonList> key_button_list;
43};
44
45} // namespace InputCommon
diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp
new file mode 100644
index 000000000..ff25220b4
--- /dev/null
+++ b/src/input_common/main.cpp
@@ -0,0 +1,35 @@
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 <memory>
6#include "common/param_package.h"
7#include "input_common/keyboard.h"
8#include "input_common/main.h"
9
10namespace InputCommon {
11
12static std::shared_ptr<Keyboard> keyboard;
13
14void Init() {
15 keyboard = std::make_shared<InputCommon::Keyboard>();
16 Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard);
17}
18
19void Shutdown() {
20 Input::UnregisterFactory<Input::ButtonDevice>("keyboard");
21 keyboard.reset();
22}
23
24Keyboard* GetKeyboard() {
25 return keyboard.get();
26}
27
28std::string GenerateKeyboardParam(int key_code) {
29 Common::ParamPackage param{
30 {"engine", "keyboard"}, {"code", std::to_string(key_code)},
31 };
32 return param.Serialize();
33}
34
35} // namespace InputCommon
diff --git a/src/input_common/main.h b/src/input_common/main.h
new file mode 100644
index 000000000..a490dd829
--- /dev/null
+++ b/src/input_common/main.h
@@ -0,0 +1,25 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <string>
8
9namespace InputCommon {
10
11/// Initializes and registers all built-in input device factories.
12void Init();
13
14/// Unresisters all build-in input device factories and shut them down.
15void Shutdown();
16
17class Keyboard;
18
19/// Gets the keyboard button device factory.
20Keyboard* GetKeyboard();
21
22/// Generates a serialized param package for creating a keyboard button device
23std::string GenerateKeyboardParam(int key_code);
24
25} // namespace InputCommon