From 3974895e08fc133c4e000c2a654f401662325718 Mon Sep 17 00:00:00 2001 From: wwylele Date: Fri, 20 Jan 2017 21:52:32 +0200 Subject: Input: add device and factory template --- src/core/frontend/input.h | 97 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/core/frontend/input.h (limited to 'src/core/frontend/input.h') diff --git a/src/core/frontend/input.h b/src/core/frontend/input.h new file mode 100644 index 000000000..a0c51df0c --- /dev/null +++ b/src/core/frontend/input.h @@ -0,0 +1,97 @@ +// Copyright 2017 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include +#include +#include +#include "common/logging/log.h" +#include "common/param_package.h" + +namespace Input { + +/// An abstract class template for an input device (a button, an analog input, etc.). +template +class InputDevice { +public: + virtual ~InputDevice() = default; + virtual StatusType GetStatus() const { + return {}; + } +}; + +/// An abstract class template for a factory that can create input devices. +template +class Factory { +public: + virtual ~Factory() = default; + virtual std::unique_ptr Create(const Common::ParamPackage&) = 0; +}; + +namespace Impl { + +template +using FactoryListType = std::unordered_map>>; + +template +struct FactoryList { + static FactoryListType list; +}; + +template +FactoryListType FactoryList::list; + +} // namespace Impl + +/** + * Registers an input device factory. + * @tparam InputDeviceType the type of input devices the factory can create + * @param name the name of the factory. Will be used to match the "engine" parameter when creating + * a device + * @param factory the factory object to register + */ +template +void RegisterFactory(const std::string& name, std::shared_ptr> factory) { + auto pair = std::make_pair(name, std::move(factory)); + if (!Impl::FactoryList::list.insert(std::move(pair)).second) { + LOG_ERROR(Input, "Factory %s already registered", name.c_str()); + } +} + +/** + * Unregisters an input device factory. + * @tparam InputDeviceType the type of input devices the factory can create + * @param name the name of the factory to unregister + */ +template +void UnregisterFactory(const std::string& name) { + if (Impl::FactoryList::list.erase(name) == 0) { + LOG_ERROR(Input, "Factory %s not registered", name.c_str()); + } +} + +/** + * Create an input device from given paramters. + * @tparam InputDeviceType the type of input devices to create + * @param params a serialized ParamPackage string contains all parameters for creating the device + */ +template +std::unique_ptr CreateDevice(const std::string& params) { + const Common::ParamPackage package(params); + const std::string engine = package.Get("engine", "null"); + const auto& factory_list = Impl::FactoryList::list; + const auto pair = factory_list.find(engine); + if (pair == factory_list.end()) { + if (engine != "null") { + LOG_ERROR(Input, "Unknown engine name: %s", engine.c_str()); + } + return std::make_unique(); + } + return pair->second->Create(package); +} + +} // namespace Input -- cgit v1.2.3 From 1d1329af23221be31c244889609415e0fb0b2641 Mon Sep 17 00:00:00 2001 From: wwylele Date: Fri, 20 Jan 2017 22:46:39 +0200 Subject: HID: use ButtonDevice --- src/core/frontend/input.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/core/frontend/input.h') diff --git a/src/core/frontend/input.h b/src/core/frontend/input.h index a0c51df0c..63e64ac67 100644 --- a/src/core/frontend/input.h +++ b/src/core/frontend/input.h @@ -94,4 +94,10 @@ std::unique_ptr CreateDevice(const std::string& params) { return pair->second->Create(package); } +/** + * A button device is an input device that returns bool as status. + * true for pressed; false for released. + */ +using ButtonDevice = InputDevice; + } // namespace Input -- cgit v1.2.3 From 70420272ca63425b52844632c6be3d3691446468 Mon Sep 17 00:00:00 2001 From: wwylele Date: Fri, 20 Jan 2017 23:58:03 +0200 Subject: HID: use AnalogDevice --- src/core/frontend/input.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/core/frontend/input.h') diff --git a/src/core/frontend/input.h b/src/core/frontend/input.h index 63e64ac67..0a5713dc0 100644 --- a/src/core/frontend/input.h +++ b/src/core/frontend/input.h @@ -100,4 +100,11 @@ std::unique_ptr CreateDevice(const std::string& params) { */ using ButtonDevice = InputDevice; +/** + * An analog device is an input device that returns a tuple of x and y coordinates as status. The + * coordinates are within the unit circle. x+ is defined as right direction, and y+ is defined as up + * direction + */ +using AnalogDevice = InputDevice>; + } // namespace Input -- cgit v1.2.3