From 38e800f70d122051e12ac9f22e23d84b97fec424 Mon Sep 17 00:00:00 2001 From: wwylele Date: Sat, 21 Jan 2017 11:53:03 +0200 Subject: InputCommon: add Keyboard --- src/input_common/CMakeLists.txt | 15 ++++++++ src/input_common/keyboard.cpp | 82 +++++++++++++++++++++++++++++++++++++++++ src/input_common/keyboard.h | 45 ++++++++++++++++++++++ src/input_common/main.cpp | 35 ++++++++++++++++++ src/input_common/main.h | 25 +++++++++++++ 5 files changed, 202 insertions(+) create mode 100644 src/input_common/CMakeLists.txt create mode 100644 src/input_common/keyboard.cpp create mode 100644 src/input_common/keyboard.h create mode 100644 src/input_common/main.cpp create mode 100644 src/input_common/main.h (limited to 'src/input_common') 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 +#include +#include +#include "input_common/keyboard.h" + +namespace InputCommon { + +class KeyButton final : public Input::ButtonDevice { +public: + explicit KeyButton(std::shared_ptr key_button_list_) + : key_button_list(key_button_list_) {} + + ~KeyButton(); + + bool GetStatus() const override { + return status.load(); + } + + friend class KeyButtonList; + +private: + std::shared_ptr key_button_list; + std::atomic status{false}; +}; + +struct KeyButtonPair { + int key_code; + KeyButton* key_button; +}; + +class KeyButtonList { +public: + void AddKeyButton(int key_code, KeyButton* key_button) { + std::lock_guard guard(mutex); + list.push_back(KeyButtonPair{key_code, key_button}); + } + + void RemoveKeyButton(const KeyButton* key_button) { + std::lock_guard 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 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 list; +}; + +Keyboard::Keyboard() : key_button_list{std::make_shared()} {} + +KeyButton::~KeyButton() { + key_button_list->RemoveKeyButton(this); +} + +std::unique_ptr Keyboard::Create(const Common::ParamPackage& params) { + int key_code = params.Get("code", 0); + std::unique_ptr button = std::make_unique(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 +#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 { +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 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 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 +#include "common/param_package.h" +#include "input_common/keyboard.h" +#include "input_common/main.h" + +namespace InputCommon { + +static std::shared_ptr keyboard; + +void Init() { + keyboard = std::make_shared(); + Input::RegisterFactory("keyboard", keyboard); +} + +void Shutdown() { + Input::UnregisterFactory("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 + +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 -- cgit v1.2.3