summaryrefslogtreecommitdiffstats
path: root/src/input_common
diff options
context:
space:
mode:
authorwwylele <wwylele@gmail.com>2017-01-21 10:53:03 +0100
committerwwylele <wwylele@gmail.com>2017-03-01 22:30:57 +0100
commit38e800f70d122051e12ac9f22e23d84b97fec424 (patch)
tree0f52b5731a4fd8e9f2a39e8e75d1fa18ce8b9107 /src/input_common
parentHID: use AnalogDevice (diff)
downloadyuzu-38e800f70d122051e12ac9f22e23d84b97fec424.tar
yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.tar.gz
yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.tar.bz2
yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.tar.lz
yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.tar.xz
yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.tar.zst
yuzu-38e800f70d122051e12ac9f22e23d84b97fec424.zip
Diffstat (limited to 'src/input_common')
-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 @@
+set(SRCS
+ keyboard.cpp
+ main.cpp
+ )
+
+set(HEADERS
+ keyboard.h
+ main.h
+ )
+
+create_directory_groups(${SRCS} ${HEADERS})
+
+add_library(input_common STATIC ${SRCS} ${HEADERS})
+target_link_libraries(input_common common core)
+
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
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 @@
+// Copyright 2017 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <memory>
+#include "core/frontend/input.h"
+
+namespace InputCommon {
+
+class KeyButtonList;
+
+/**
+ * A button device factory representing a keyboard. It receives keyboard events and forward them
+ * to all button devices it created.
+ */
+class Keyboard final : public Input::Factory<Input::ButtonDevice> {
+public:
+ Keyboard();
+
+ /**
+ * Creates a button device from a keyboard key
+ * @param params contains parameters for creating the device:
+ * - "code": the code of the key to bind with the button
+ */
+ std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override;
+
+ /**
+ * Sets the status of all buttons bound with the key to pressed
+ * @param key_code the code of the key to press
+ */
+ void PressKey(int key_code);
+
+ /**
+ * Sets the status of all buttons bound with the key to released
+ * @param key_code the code of the key to release
+ */
+ void ReleaseKey(int key_code);
+
+private:
+ std::shared_ptr<KeyButtonList> key_button_list;
+};
+
+} // 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 @@
+// Copyright 2017 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <memory>
+#include "common/param_package.h"
+#include "input_common/keyboard.h"
+#include "input_common/main.h"
+
+namespace InputCommon {
+
+static std::shared_ptr<Keyboard> keyboard;
+
+void Init() {
+ keyboard = std::make_shared<InputCommon::Keyboard>();
+ Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard);
+}
+
+void Shutdown() {
+ Input::UnregisterFactory<Input::ButtonDevice>("keyboard");
+ keyboard.reset();
+}
+
+Keyboard* GetKeyboard() {
+ return keyboard.get();
+}
+
+std::string GenerateKeyboardParam(int key_code) {
+ Common::ParamPackage param{
+ {"engine", "keyboard"}, {"code", std::to_string(key_code)},
+ };
+ return param.Serialize();
+}
+
+} // 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 @@
+// Copyright 2017 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <string>
+
+namespace InputCommon {
+
+/// Initializes and registers all built-in input device factories.
+void Init();
+
+/// Unresisters all build-in input device factories and shut them down.
+void Shutdown();
+
+class Keyboard;
+
+/// Gets the keyboard button device factory.
+Keyboard* GetKeyboard();
+
+/// Generates a serialized param package for creating a keyboard button device
+std::string GenerateKeyboardParam(int key_code);
+
+} // namespace InputCommon