From c3f54ff2329d79bdbb273678b5123cf0b1cd090c Mon Sep 17 00:00:00 2001 From: german77 Date: Mon, 20 Sep 2021 19:43:16 -0500 Subject: core/hid: Add emulated controllers --- src/core/hid/emulated_controller.h | 232 +++++++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 src/core/hid/emulated_controller.h (limited to 'src/core/hid/emulated_controller.h') diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h new file mode 100644 index 000000000..94db9b00b --- /dev/null +++ b/src/core/hid/emulated_controller.h @@ -0,0 +1,232 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include + +#include "common/input.h" +#include "common/param_package.h" +#include "common/point.h" +#include "common/quaternion.h" +#include "common/settings.h" +#include "common/vector_math.h" +#include "core/hid/hid_types.h" +#include "core/hid/motion_input.h" + +namespace Core::HID { + +struct ControllerMotionInfo { + Input::MotionStatus raw_status; + MotionInput emulated{}; +}; + +using ButtonDevices = + std::array, Settings::NativeButton::NumButtons>; +using StickDevices = + std::array, Settings::NativeAnalog::NumAnalogs>; +using ControllerMotionDevices = + std::array, Settings::NativeMotion::NumMotions>; +using TriggerDevices = + std::array, Settings::NativeTrigger::NumTriggers>; +using BatteryDevices = std::array, 2>; + +using ButtonParams = std::array; +using StickParams = std::array; +using ControllerMotionParams = std::array; +using TriggerParams = std::array; +using BatteryParams = std::array; + +using ButtonValues = std::array; +using SticksValues = std::array; +using TriggerValues = std::array; +using ControllerMotionValues = std::array; +using ColorValues = std::array; +using BatteryValues = std::array; +using VibrationValues = std::array; + +struct AnalogSticks { + AnalogStickState left; + AnalogStickState right; +}; + +struct ControllerColors { + NpadControllerColor fullkey; + NpadControllerColor left; + NpadControllerColor right; +}; + +struct BatteryLevelState { + NpadPowerInfo dual; + NpadPowerInfo left; + NpadPowerInfo right; +}; + +struct ControllerMotion { + bool is_at_rest; + Common::Vec3f accel{}; + Common::Vec3f gyro{}; + Common::Vec3f rotation{}; + std::array orientation{}; +}; + +using MotionState = std::array; + +struct ControllerStatus { + // Data from input_common + ButtonValues button_values{}; + SticksValues stick_values{}; + ControllerMotionValues motion_values{}; + TriggerValues trigger_values{}; + ColorValues color_values{}; + BatteryValues battery_values{}; + VibrationValues vibration_values{}; + + // Data for Nintendo devices + NpadButtonState npad_button_state{}; + DebugPadButton debug_pad_button_state{}; + AnalogSticks analog_stick_state{}; + MotionState motion_state{}; + NpadGcTriggerState gc_trigger_state{}; + ControllerColors colors_state{}; + BatteryLevelState battery_state{}; +}; +enum class ControllerTriggerType { + Button, + Stick, + Trigger, + Motion, + Color, + Battery, + Vibration, + Connected, + Disconnected, + Type, + All, +}; + +struct ControllerUpdateCallback { + std::function on_change; +}; + +class EmulatedController { +public: + /** + * TODO: Write description + * + * @param npad_id_type + */ + explicit EmulatedController(NpadIdType npad_id_type_); + ~EmulatedController(); + + YUZU_NON_COPYABLE(EmulatedController); + YUZU_NON_MOVEABLE(EmulatedController); + + static NpadType MapSettingsTypeToNPad(Settings::ControllerType type); + static Settings::ControllerType MapNPadToSettingsType(NpadType type); + + /// Gets the NpadIdType for this controller. + NpadIdType GetNpadIdType() const; + + /// Sets the NpadType for this controller. + void SetNpadType(NpadType npad_type_); + + /// Gets the NpadType for this controller. + NpadType GetNpadType() const; + + void Connect(); + void Disconnect(); + + bool IsConnected() const; + bool IsVibrationEnabled() const; + + void ReloadFromSettings(); + void ReloadInput(); + void UnloadInput(); + + void EnableConfiguration(); + void DisableConfiguration(); + bool IsConfiguring() const; + void SaveCurrentConfig(); + void RestoreConfig(); + + std::vector GetMappedDevices() const; + + Common::ParamPackage GetButtonParam(std::size_t index) const; + Common::ParamPackage GetStickParam(std::size_t index) const; + Common::ParamPackage GetMotionParam(std::size_t index) const; + + void SetButtonParam(std::size_t index, Common::ParamPackage param); + void SetStickParam(std::size_t index, Common::ParamPackage param); + void SetMotionParam(std::size_t index, Common::ParamPackage param); + + ButtonValues GetButtonsValues() const; + SticksValues GetSticksValues() const; + TriggerValues GetTriggersValues() const; + ControllerMotionValues GetMotionValues() const; + ColorValues GetColorsValues() const; + BatteryValues GetBatteryValues() const; + + NpadButtonState GetNpadButtons() const; + DebugPadButton GetDebugPadButtons() const; + AnalogSticks GetSticks() const; + NpadGcTriggerState GetTriggers() const; + MotionState GetMotions() const; + ControllerColors GetColors() const; + BatteryLevelState GetBattery() const; + + bool SetVibration(std::size_t device_index, VibrationValue vibration); + int TestVibration(std::size_t device_index); + + int SetCallback(ControllerUpdateCallback update_callback); + void DeleteCallback(int key); + +private: + /** + * Sets the status of a button. Applies toggle properties to the output. + * + * @param A CallbackStatus and a button index number + */ + void SetButton(Input::CallbackStatus callback, std::size_t index); + void SetStick(Input::CallbackStatus callback, std::size_t index); + void SetTrigger(Input::CallbackStatus callback, std::size_t index); + void SetMotion(Input::CallbackStatus callback, std::size_t index); + void SetBattery(Input::CallbackStatus callback, std::size_t index); + + /** + * Triggers a callback that something has changed + * + * @param Input type of the trigger + */ + void TriggerOnChange(ControllerTriggerType type); + + NpadIdType npad_id_type; + NpadType npad_type{NpadType::None}; + bool is_connected{false}; + bool is_configuring{false}; + bool is_vibration_enabled{true}; + f32 motion_sensitivity{0.01f}; + + ButtonParams button_params; + StickParams stick_params; + ControllerMotionParams motion_params; + TriggerParams trigger_params; + BatteryParams battery_params; + + ButtonDevices button_devices; + StickDevices stick_devices; + ControllerMotionDevices motion_devices; + TriggerDevices trigger_devices; + BatteryDevices battery_devices; + // VibrationDevices vibration_devices; + + mutable std::mutex mutex; + std::unordered_map callback_list; + int last_callback_key = 0; + ControllerStatus controller; +}; + +} // namespace Core::HID -- cgit v1.2.3