Merge branch 'yuzu-emu:master' into convert_legacy
commit
e49184e606
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1 +1 @@
|
||||
Subproject commit 25f9ed87ff6947d9576fc9d79dee0784e638ac58
|
||||
Subproject commit 2e9821423a237a1206e3c09020778faacfe430be
|
@ -1 +1 @@
|
||||
Subproject commit 1d66483ad2b93f0e00e175f9480c771af90003a7
|
||||
Subproject commit 75d9d125ee655ef80f3bfcd97ae5a805931042b8
|
@ -1,18 +0,0 @@
|
||||
# Exports:
|
||||
# LIBUNICORN_FOUND
|
||||
# LIBUNICORN_INCLUDE_DIR
|
||||
# LIBUNICORN_LIBRARY
|
||||
|
||||
find_path(LIBUNICORN_INCLUDE_DIR
|
||||
unicorn/unicorn.h
|
||||
HINTS $ENV{UNICORNDIR}
|
||||
PATH_SUFFIXES include)
|
||||
|
||||
find_library(LIBUNICORN_LIBRARY
|
||||
NAMES unicorn
|
||||
HINTS $ENV{UNICORNDIR})
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(unicorn DEFAULT_MSG
|
||||
LIBUNICORN_LIBRARY LIBUNICORN_INCLUDE_DIR)
|
||||
mark_as_advanced(LIBUNICORN_INCLUDE_DIR LIBUNICORN_LIBRARY)
|
@ -0,0 +1,366 @@
|
||||
// Copyright 2017 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include "common/logging/log.h"
|
||||
#include "common/param_package.h"
|
||||
#include "common/uuid.h"
|
||||
|
||||
namespace Common::Input {
|
||||
|
||||
// Type of data that is expected to recieve or send
|
||||
enum class InputType {
|
||||
None,
|
||||
Battery,
|
||||
Button,
|
||||
Stick,
|
||||
Analog,
|
||||
Trigger,
|
||||
Motion,
|
||||
Touch,
|
||||
Color,
|
||||
Vibration,
|
||||
Nfc,
|
||||
Ir,
|
||||
};
|
||||
|
||||
// Internal battery charge level
|
||||
enum class BatteryLevel : u32 {
|
||||
None,
|
||||
Empty,
|
||||
Critical,
|
||||
Low,
|
||||
Medium,
|
||||
Full,
|
||||
Charging,
|
||||
};
|
||||
|
||||
enum class PollingMode {
|
||||
// Constant polling of buttons, analogs and motion data
|
||||
Active,
|
||||
// Only update on button change, digital analogs
|
||||
Pasive,
|
||||
// Enable near field communication polling
|
||||
NFC,
|
||||
// Enable infrared camera polling
|
||||
IR,
|
||||
};
|
||||
|
||||
// Vibration reply from the controller
|
||||
enum class VibrationError {
|
||||
None,
|
||||
NotSupported,
|
||||
Disabled,
|
||||
Unknown,
|
||||
};
|
||||
|
||||
// Polling mode reply from the controller
|
||||
enum class PollingError {
|
||||
None,
|
||||
NotSupported,
|
||||
Unknown,
|
||||
};
|
||||
|
||||
// Hint for amplification curve to be used
|
||||
enum class VibrationAmplificationType {
|
||||
Linear,
|
||||
Exponential,
|
||||
};
|
||||
|
||||
// Analog properties for calibration
|
||||
struct AnalogProperties {
|
||||
// Anything below this value will be detected as zero
|
||||
float deadzone{};
|
||||
// Anyting above this values will be detected as one
|
||||
float range{1.0f};
|
||||
// Minimum value to be detected as active
|
||||
float threshold{0.5f};
|
||||
// Drift correction applied to the raw data
|
||||
float offset{};
|
||||
// Invert direction of the sensor data
|
||||
bool inverted{};
|
||||
};
|
||||
|
||||
// Single analog sensor data
|
||||
struct AnalogStatus {
|
||||
float value{};
|
||||
float raw_value{};
|
||||
AnalogProperties properties{};
|
||||
};
|
||||
|
||||
// Button data
|
||||
struct ButtonStatus {
|
||||
Common::UUID uuid{};
|
||||
bool value{};
|
||||
bool inverted{};
|
||||
bool toggle{};
|
||||
bool locked{};
|
||||
};
|
||||
|
||||
// Internal battery data
|
||||
using BatteryStatus = BatteryLevel;
|
||||
|
||||
// Analog and digital joystick data
|
||||
struct StickStatus {
|
||||
Common::UUID uuid{};
|
||||
AnalogStatus x{};
|
||||
AnalogStatus y{};
|
||||
bool left{};
|
||||
bool right{};
|
||||
bool up{};
|
||||
bool down{};
|
||||
};
|
||||
|
||||
// Analog and digital trigger data
|
||||
struct TriggerStatus {
|
||||
Common::UUID uuid{};
|
||||
AnalogStatus analog{};
|
||||
ButtonStatus pressed{};
|
||||
};
|
||||
|
||||
// 3D vector representing motion input
|
||||
struct MotionSensor {
|
||||
AnalogStatus x{};
|
||||
AnalogStatus y{};
|
||||
AnalogStatus z{};
|
||||
};
|
||||
|
||||
// Motion data used to calculate controller orientation
|
||||
struct MotionStatus {
|
||||
// Gyroscope vector measurement in radians/s.
|
||||
MotionSensor gyro{};
|
||||
// Acceleration vector measurement in G force
|
||||
MotionSensor accel{};
|
||||
// Time since last measurement in microseconds
|
||||
u64 delta_timestamp{};
|
||||
// Request to update after reading the value
|
||||
bool force_update{};
|
||||
};
|
||||
|
||||
// Data of a single point on a touch screen
|
||||
struct TouchStatus {
|
||||
ButtonStatus pressed{};
|
||||
AnalogStatus x{};
|
||||
AnalogStatus y{};
|
||||
int id{};
|
||||
};
|
||||
|
||||
// Physical controller color in RGB format
|
||||
struct BodyColorStatus {
|
||||
u32 body{};
|
||||
u32 buttons{};
|
||||
};
|
||||
|
||||
// HD rumble data
|
||||
struct VibrationStatus {
|
||||
f32 low_amplitude{};
|
||||
f32 low_frequency{};
|
||||
f32 high_amplitude{};
|
||||
f32 high_frequency{};
|
||||
VibrationAmplificationType type;
|
||||
};
|
||||
|
||||
// Physical controller LED pattern
|
||||
struct LedStatus {
|
||||
bool led_1{};
|
||||
bool led_2{};
|
||||
bool led_3{};
|
||||
bool led_4{};
|
||||
};
|
||||
|
||||
// List of buttons to be passed to Qt that can be translated
|
||||
enum class ButtonNames {
|
||||
Undefined,
|
||||
Invalid,
|
||||
// This will display the engine name instead of the button name
|
||||
Engine,
|
||||
// This will display the button by value instead of the button name
|
||||
Value,
|
||||
ButtonLeft,
|
||||
ButtonRight,
|
||||
ButtonDown,
|
||||
ButtonUp,
|
||||
TriggerZ,
|
||||
TriggerR,
|
||||
TriggerL,
|
||||
ButtonA,
|
||||
ButtonB,
|
||||
ButtonX,
|
||||
ButtonY,
|
||||
ButtonStart,
|
||||
|
||||
// DS4 button names
|
||||
L1,
|
||||
L2,
|
||||
L3,
|
||||
R1,
|
||||
R2,
|
||||
R3,
|
||||
Circle,
|
||||
Cross,
|
||||
Square,
|
||||
Triangle,
|
||||
Share,
|
||||
Options,
|
||||
};
|
||||
|
||||
// Callback data consisting of an input type and the equivalent data status
|
||||
struct CallbackStatus {
|
||||
InputType type{InputType::None};
|
||||
ButtonStatus button_status{};
|
||||
StickStatus stick_status{};
|
||||
AnalogStatus analog_status{};
|
||||
TriggerStatus trigger_status{};
|
||||
MotionStatus motion_status{};
|
||||
TouchStatus touch_status{};
|
||||
BodyColorStatus color_status{};
|
||||
BatteryStatus battery_status{};
|
||||
VibrationStatus vibration_status{};
|
||||
};
|
||||
|
||||
// Triggered once every input change
|
||||
struct InputCallback {
|
||||
std::function<void(const CallbackStatus&)> on_change;
|
||||
};
|
||||
|
||||
/// An abstract class template for an input device (a button, an analog input, etc.).
|
||||
class InputDevice {
|
||||
public:
|
||||
virtual ~InputDevice() = default;
|
||||
|
||||
// Request input device to update if necessary
|
||||
virtual void SoftUpdate() {}
|
||||
|
||||
// Force input device to update data regardless of the current state
|
||||
virtual void ForceUpdate() {}
|
||||
|
||||
// Sets the function to be triggered when input changes
|
||||
void SetCallback(InputCallback callback_) {
|
||||
callback = std::move(callback_);
|
||||
}
|
||||
|
||||
// Triggers the function set in the callback
|
||||
void TriggerOnChange(const CallbackStatus& status) {
|
||||
if (callback.on_change) {
|
||||
callback.on_change(status);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
InputCallback callback;
|
||||
};
|
||||
|
||||
/// An abstract class template for an output device (rumble, LED pattern, polling mode).
|
||||
class OutputDevice {
|
||||
public:
|
||||
virtual ~OutputDevice() = default;
|
||||
|
||||
virtual void SetLED([[maybe_unused]] const LedStatus& led_status) {}
|
||||
|
||||
virtual VibrationError SetVibration([[maybe_unused]] const VibrationStatus& vibration_status) {
|
||||
return VibrationError::NotSupported;
|
||||
}
|
||||
|
||||
virtual PollingError SetPollingMode([[maybe_unused]] PollingMode polling_mode) {
|
||||
return PollingError::NotSupported;
|
||||
}
|
||||
};
|
||||
|
||||
/// An abstract class template for a factory that can create input devices.
|
||||
template <typename InputDeviceType>
|
||||
class Factory {
|
||||
public:
|
||||
virtual ~Factory() = default;
|
||||
virtual std::unique_ptr<InputDeviceType> Create(const Common::ParamPackage&) = 0;
|
||||
};
|
||||
|
||||
namespace Impl {
|
||||
|
||||
template <typename InputDeviceType>
|
||||
using FactoryListType = std::unordered_map<std::string, std::shared_ptr<Factory<InputDeviceType>>>;
|
||||
|
||||
template <typename InputDeviceType>
|
||||
struct FactoryList {
|
||||
static FactoryListType<InputDeviceType> list;
|
||||
};
|
||||
|
||||
template <typename InputDeviceType>
|
||||
FactoryListType<InputDeviceType> FactoryList<InputDeviceType>::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 <typename InputDeviceType>
|
||||
void RegisterFactory(const std::string& name, std::shared_ptr<Factory<InputDeviceType>> factory) {
|
||||
auto pair = std::make_pair(name, std::move(factory));
|
||||
if (!Impl::FactoryList<InputDeviceType>::list.insert(std::move(pair)).second) {
|
||||
LOG_ERROR(Input, "Factory '{}' already registered", name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 <typename InputDeviceType>
|
||||
void UnregisterFactory(const std::string& name) {
|
||||
if (Impl::FactoryList<InputDeviceType>::list.erase(name) == 0) {
|
||||
LOG_ERROR(Input, "Factory '{}' not registered", name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an input device from given paramters.
|
||||
* @tparam InputDeviceType the type of input devices to create
|
||||
* @param params a serialized ParamPackage string that contains all parameters for creating the
|
||||
* device
|
||||
*/
|
||||
template <typename InputDeviceType>
|
||||
std::unique_ptr<InputDeviceType> CreateDeviceFromString(const std::string& params) {
|
||||
const Common::ParamPackage package(params);
|
||||
const std::string engine = package.Get("engine", "null");
|
||||
const auto& factory_list = Impl::FactoryList<InputDeviceType>::list;
|
||||
const auto pair = factory_list.find(engine);
|
||||
if (pair == factory_list.end()) {
|
||||
if (engine != "null") {
|
||||
LOG_ERROR(Input, "Unknown engine name: {}", engine);
|
||||
}
|
||||
return std::make_unique<InputDeviceType>();
|
||||
}
|
||||
return pair->second->Create(package);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an input device from given paramters.
|
||||
* @tparam InputDeviceType the type of input devices to create
|
||||
* @param A ParamPackage that contains all parameters for creating the device
|
||||
*/
|
||||
template <typename InputDeviceType>
|
||||
std::unique_ptr<InputDeviceType> CreateDevice(const Common::ParamPackage package) {
|
||||
const std::string engine = package.Get("engine", "null");
|
||||
const auto& factory_list = Impl::FactoryList<InputDeviceType>::list;
|
||||
const auto pair = factory_list.find(engine);
|
||||
if (pair == factory_list.end()) {
|
||||
if (engine != "null") {
|
||||
LOG_ERROR(Input, "Unknown engine name: {}", engine);
|
||||
}
|
||||
return std::make_unique<InputDeviceType>();
|
||||
}
|
||||
return pair->second->Create(package);
|
||||
}
|
||||
|
||||
} // namespace Common::Input
|
@ -1,217 +0,0 @@
|
||||
// Copyright 2017 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include "common/logging/log.h"
|
||||
#include "common/param_package.h"
|
||||
#include "common/quaternion.h"
|
||||
#include "common/vector_math.h"
|
||||
|
||||
namespace Input {
|
||||
|
||||
enum class AnalogDirection : u8 {
|
||||
RIGHT,
|
||||
LEFT,
|
||||
UP,
|
||||
DOWN,
|
||||
};
|
||||
struct AnalogProperties {
|
||||
float deadzone;
|
||||
float range;
|
||||
float threshold;
|
||||
};
|
||||
template <typename StatusType>
|
||||
struct InputCallback {
|
||||
std::function<void(StatusType)> on_change;
|
||||
};
|
||||
|
||||
/// An abstract class template for an input device (a button, an analog input, etc.).
|
||||
template <typename StatusType>
|
||||
class InputDevice {
|
||||
public:
|
||||
virtual ~InputDevice() = default;
|
||||
virtual StatusType GetStatus() const {
|
||||
return {};
|
||||
}
|
||||
virtual StatusType GetRawStatus() const {
|
||||
return GetStatus();
|
||||
}
|
||||
virtual AnalogProperties GetAnalogProperties() const {
|
||||
return {};
|
||||
}
|
||||
virtual bool GetAnalogDirectionStatus([[maybe_unused]] AnalogDirection direction) const {
|
||||
return {};
|
||||
}
|
||||
virtual bool SetRumblePlay([[maybe_unused]] f32 amp_low, [[maybe_unused]] f32 freq_low,
|
||||
[[maybe_unused]] f32 amp_high,
|
||||
[[maybe_unused]] f32 freq_high) const {
|
||||
return {};
|
||||
}
|
||||
void SetCallback(InputCallback<StatusType> callback_) {
|
||||
callback = std::move(callback_);
|
||||
}
|
||||
void TriggerOnChange() {
|
||||
if (callback.on_change) {
|
||||
callback.on_change(GetStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
InputCallback<StatusType> callback;
|
||||
};
|
||||
|
||||
/// An abstract class template for a factory that can create input devices.
|
||||
template <typename InputDeviceType>
|
||||
class Factory {
|
||||
public:
|
||||
virtual ~Factory() = default;
|
||||
virtual std::unique_ptr<InputDeviceType> Create(const Common::ParamPackage&) = 0;
|
||||
};
|
||||
|
||||
namespace Impl {
|
||||
|
||||
template <typename InputDeviceType>
|
||||
using FactoryListType = std::unordered_map<std::string, std::shared_ptr<Factory<InputDeviceType>>>;
|
||||
|
||||
template <typename InputDeviceType>
|
||||
struct FactoryList {
|
||||
static FactoryListType<InputDeviceType> list;
|
||||
};
|
||||
|
||||
template <typename InputDeviceType>
|
||||
FactoryListType<InputDeviceType> FactoryList<InputDeviceType>::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 <typename InputDeviceType>
|
||||
void RegisterFactory(const std::string& name, std::shared_ptr<Factory<InputDeviceType>> factory) {
|
||||
auto pair = std::make_pair(name, std::move(factory));
|
||||
if (!Impl::FactoryList<InputDeviceType>::list.insert(std::move(pair)).second) {
|
||||
LOG_ERROR(Input, "Factory '{}' already registered", name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 <typename InputDeviceType>
|
||||
void UnregisterFactory(const std::string& name) {
|
||||
if (Impl::FactoryList<InputDeviceType>::list.erase(name) == 0) {
|
||||
LOG_ERROR(Input, "Factory '{}' not registered", name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 <typename InputDeviceType>
|
||||
std::unique_ptr<InputDeviceType> CreateDevice(const std::string& params) {
|
||||
const Common::ParamPackage package(params);
|
||||
const std::string engine = package.Get("engine", "null");
|
||||
const auto& factory_list = Impl::FactoryList<InputDeviceType>::list;
|
||||
const auto pair = factory_list.find(engine);
|
||||
if (pair == factory_list.end()) {
|
||||
if (engine != "null") {
|
||||
LOG_ERROR(Input, "Unknown engine name: {}", engine);
|
||||
}
|
||||
return std::make_unique<InputDeviceType>();
|
||||
}
|
||||
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<bool>;
|
||||
|
||||
/**
|
||||
* 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<std::tuple<float, float>>;
|
||||
|
||||
/**
|
||||
* A vibration device is an input device that returns an unsigned byte as status.
|
||||
* It represents whether the vibration device supports vibration or not.
|
||||
* If the status returns 1, it supports vibration. Otherwise, it does not support vibration.
|
||||
*/
|
||||
using VibrationDevice = InputDevice<u8>;
|
||||
|
||||
/**
|
||||
* A motion status is an object that returns a tuple of accelerometer state vector,
|
||||
* gyroscope state vector, rotation state vector, orientation state matrix and quaterion state
|
||||
* vector.
|
||||
*
|
||||
* For both 3D vectors:
|
||||
* x+ is the same direction as RIGHT on D-pad.
|
||||
* y+ is normal to the touch screen, pointing outward.
|
||||
* z+ is the same direction as UP on D-pad.
|
||||
*
|
||||
* For accelerometer state vector
|
||||
* Units: g (gravitational acceleration)
|
||||
*
|
||||
* For gyroscope state vector:
|
||||
* Orientation is determined by right-hand rule.
|
||||
* Units: deg/sec
|
||||
*
|
||||
* For rotation state vector
|
||||
* Units: rotations
|
||||
*
|
||||
* For orientation state matrix
|
||||
* x vector
|
||||
* y vector
|
||||
* z vector
|
||||
*
|
||||
* For quaternion state vector
|
||||
* xyz vector
|
||||
* w float
|
||||
*/
|
||||
using MotionStatus = std::tuple<Common::Vec3<float>, Common::Vec3<float>, Common::Vec3<float>,
|
||||
std::array<Common::Vec3f, 3>, Common::Quaternion<f32>>;
|
||||
|
||||
/**
|
||||
* A motion device is an input device that returns a motion status object
|
||||
*/
|
||||
using MotionDevice = InputDevice<MotionStatus>;
|
||||
|
||||
/**
|
||||
* A touch status is an object that returns an array of 16 tuple elements of two floats and a bool.
|
||||
* The floats are x and y coordinates in the range 0.0 - 1.0, and the bool indicates whether it is
|
||||
* pressed.
|
||||
*/
|
||||
using TouchStatus = std::array<std::tuple<float, float, bool>, 16>;
|
||||
|
||||
/**
|
||||
* A touch device is an input device that returns a touch status object
|
||||
*/
|
||||
using TouchDevice = InputDevice<TouchStatus>;
|
||||
|
||||
/**
|
||||
* A mouse device is an input device that returns a tuple of two floats and four ints.
|
||||
* The first two floats are X and Y device coordinates of the mouse (from 0-1).
|
||||
* The s32s are the mouse wheel.
|
||||
*/
|
||||
using MouseDevice = InputDevice<std::tuple<float, float, s32, s32>>;
|
||||
|
||||
} // namespace Input
|
@ -0,0 +1,232 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included
|
||||
|
||||
#include "common/settings.h"
|
||||
#include "core/hid/emulated_console.h"
|
||||
#include "core/hid/input_converter.h"
|
||||
|
||||
namespace Core::HID {
|
||||
EmulatedConsole::EmulatedConsole() = default;
|
||||
|
||||
EmulatedConsole::~EmulatedConsole() = default;
|
||||
|
||||
void EmulatedConsole::ReloadFromSettings() {
|
||||
// Using first motion device from player 1. No need to assign any unique config at the moment
|
||||
const auto& player = Settings::values.players.GetValue()[0];
|
||||
motion_params = Common::ParamPackage(player.motions[0]);
|
||||
|
||||
ReloadInput();
|
||||
}
|
||||
|
||||
void EmulatedConsole::SetTouchParams() {
|
||||
// TODO(german77): Support any number of fingers
|
||||
std::size_t index = 0;
|
||||
|
||||
// Hardcode mouse, touchscreen and cemuhook parameters
|
||||
if (!Settings::values.mouse_enabled) {
|
||||
// We can't use mouse as touch if native mouse is enabled
|
||||
touch_params[index++] = Common::ParamPackage{"engine:mouse,axis_x:10,axis_y:11,button:0"};
|
||||
}
|
||||
touch_params[index++] = Common::ParamPackage{"engine:touch,axis_x:0,axis_y:1,button:0"};
|
||||
touch_params[index++] = Common::ParamPackage{"engine:touch,axis_x:2,axis_y:3,button:1"};
|
||||
touch_params[index++] =
|
||||
Common::ParamPackage{"engine:cemuhookudp,axis_x:17,axis_y:18,button:65536"};
|
||||
touch_params[index++] =
|
||||
Common::ParamPackage{"engine:cemuhookudp,axis_x:19,axis_y:20,button:131072"};
|
||||
|
||||
const auto button_index =
|
||||
static_cast<u64>(Settings::values.touch_from_button_map_index.GetValue());
|
||||
const auto& touch_buttons = Settings::values.touch_from_button_maps[button_index].buttons;
|
||||
|
||||
// Map the rest of the fingers from touch from button configuration
|
||||
for (const auto& config_entry : touch_buttons) {
|
||||
if (index >= touch_params.size()) {
|
||||
continue;
|
||||
}
|
||||
Common::ParamPackage params{config_entry};
|
||||
Common::ParamPackage touch_button_params;
|
||||
const int x = params.Get("x", 0);
|
||||
const int y = params.Get("y", 0);
|
||||
params.Erase("x");
|
||||
params.Erase("y");
|
||||
touch_button_params.Set("engine", "touch_from_button");
|
||||
touch_button_params.Set("button", params.Serialize());
|
||||
touch_button_params.Set("x", x);
|
||||
touch_button_params.Set("y", y);
|
||||
touch_button_params.Set("touch_id", static_cast<int>(index));
|
||||
touch_params[index] = touch_button_params;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
void EmulatedConsole::ReloadInput() {
|
||||
// If you load any device here add the equivalent to the UnloadInput() function
|
||||
SetTouchParams();
|
||||
|
||||
motion_devices = Common::Input::CreateDevice<Common::Input::InputDevice>(motion_params);
|
||||
if (motion_devices) {
|
||||
motion_devices->SetCallback({
|
||||
.on_change =
|
||||
[this](const Common::Input::CallbackStatus& callback) { SetMotion(callback); },
|
||||
});
|
||||
}
|
||||
|
||||
// Unique index for identifying touch device source
|
||||
std::size_t index = 0;
|
||||
for (auto& touch_device : touch_devices) {
|
||||
touch_device = Common::Input::CreateDevice<Common::Input::InputDevice>(touch_params[index]);
|
||||
if (!touch_device) {
|
||||
continue;
|
||||
}
|
||||
touch_device->SetCallback({
|
||||
.on_change =
|
||||
[this, index](const Common::Input::CallbackStatus& callback) {
|
||||
SetTouch(callback, index);
|
||||
},
|
||||
});
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
void EmulatedConsole::UnloadInput() {
|
||||
motion_devices.reset();
|
||||
for (auto& touch : touch_devices) {
|
||||
touch.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void EmulatedConsole::EnableConfiguration() {
|
||||
is_configuring = true;
|
||||
SaveCurrentConfig();
|
||||
}
|
||||
|
||||
void EmulatedConsole::DisableConfiguration() {
|
||||
is_configuring = false;
|
||||
}
|
||||
|
||||
bool EmulatedConsole::IsConfiguring() const {
|
||||
return is_configuring;
|
||||
}
|
||||
|
||||
void EmulatedConsole::SaveCurrentConfig() {
|
||||
if (!is_configuring) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void EmulatedConsole::RestoreConfig() {
|
||||
if (!is_configuring) {
|
||||
return;
|
||||
}
|
||||
ReloadFromSettings();
|
||||
}
|
||||
|
||||
Common::ParamPackage EmulatedConsole::GetMotionParam() const {
|
||||
return motion_params;
|
||||
}
|
||||
|
||||
void EmulatedConsole::SetMotionParam(Common::ParamPackage param) {
|
||||
motion_params = param;
|
||||
ReloadInput();
|
||||
}
|
||||
|
||||
void EmulatedConsole::SetMotion(const Common::Input::CallbackStatus& callback) {
|
||||
std::lock_guard lock{mutex};
|
||||
auto& raw_status = console.motion_values.raw_status;
|
||||
auto& emulated = console.motion_values.emulated;
|
||||
|
||||
raw_status = TransformToMotion(callback);
|
||||
emulated.SetAcceleration(Common::Vec3f{
|
||||
raw_status.accel.x.value,
|
||||
raw_status.accel.y.value,
|
||||
raw_status.accel.z.value,
|
||||
});
|
||||
emulated.SetGyroscope(Common::Vec3f{
|
||||
raw_status.gyro.x.value,
|
||||
raw_status.gyro.y.value,
|
||||
raw_status.gyro.z.value,
|
||||
});
|
||||
emulated.UpdateRotation(raw_status.delta_timestamp);
|
||||
emulated.UpdateOrientation(raw_status.delta_timestamp);
|
||||
|
||||
if (is_configuring) {
|
||||
TriggerOnChange(ConsoleTriggerType::Motion);
|
||||
return;
|
||||
}
|
||||
|
||||
auto& motion = console.motion_state;
|
||||
motion.accel = emulated.GetAcceleration();
|
||||
motion.gyro = emulated.GetGyroscope();
|
||||
motion.rotation = emulated.GetGyroscope();
|
||||
motion.orientation = emulated.GetOrientation();
|
||||
motion.quaternion = emulated.GetQuaternion();
|
||||
motion.is_at_rest = !emulated.IsMoving(motion_sensitivity);
|
||||
|
||||
TriggerOnChange(ConsoleTriggerType::Motion);
|
||||
}
|
||||
|
||||
void EmulatedConsole::SetTouch(const Common::Input::CallbackStatus& callback, std::size_t index) {
|
||||
if (index >= console.touch_values.size()) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard lock{mutex};
|
||||
|
||||
console.touch_values[index] = TransformToTouch(callback);
|
||||
|
||||
if (is_configuring) {
|
||||
TriggerOnChange(ConsoleTriggerType::Touch);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO(german77): Remap touch id in sequential order
|
||||
console.touch_state[index] = {
|
||||
.position = {console.touch_values[index].x.value, console.touch_values[index].y.value},
|
||||
.id = static_cast<u32>(console.touch_values[index].id),
|
||||
.pressed = console.touch_values[index].pressed.value,
|
||||
};
|
||||
|
||||
TriggerOnChange(ConsoleTriggerType::Touch);
|
||||
}
|
||||
|
||||
ConsoleMotionValues EmulatedConsole::GetMotionValues() const {
|
||||
return console.motion_values;
|
||||
}
|
||||
|
||||
TouchValues EmulatedConsole::GetTouchValues() const {
|
||||
return console.touch_values;
|
||||
}
|
||||
|
||||
ConsoleMotion EmulatedConsole::GetMotion() const {
|
||||
return console.motion_state;
|
||||
}
|
||||
|
||||
TouchFingerState EmulatedConsole::GetTouch() const {
|
||||
return console.touch_state;
|
||||
}
|
||||
|
||||
void EmulatedConsole::TriggerOnChange(ConsoleTriggerType type) {
|
||||
for (const auto& poller_pair : callback_list) {
|
||||
const ConsoleUpdateCallback& poller = poller_pair.second;
|
||||
if (poller.on_change) {
|
||||
poller.on_change(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int EmulatedConsole::SetCallback(ConsoleUpdateCallback update_callback) {
|
||||
std::lock_guard lock{mutex};
|
||||
callback_list.insert_or_assign(last_callback_key, update_callback);
|
||||
return last_callback_key++;
|
||||
}
|
||||
|
||||
void EmulatedConsole::DeleteCallback(int key) {
|
||||
std::lock_guard lock{mutex};
|
||||
const auto& iterator = callback_list.find(key);
|
||||
if (iterator == callback_list.end()) {
|
||||
LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
|
||||
return;
|
||||
}
|
||||
callback_list.erase(iterator);
|
||||
}
|
||||
} // namespace Core::HID
|
@ -0,0 +1,190 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "common/common_types.h"
|
||||
#include "common/input.h"
|
||||
#include "common/param_package.h"
|
||||
#include "common/point.h"
|
||||
#include "common/quaternion.h"
|
||||
#include "common/vector_math.h"
|
||||
#include "core/hid/hid_types.h"
|
||||
#include "core/hid/motion_input.h"
|
||||
|
||||
namespace Core::HID {
|
||||
|
||||
struct ConsoleMotionInfo {
|
||||
Common::Input::MotionStatus raw_status{};
|
||||
MotionInput emulated{};
|
||||
};
|
||||
|
||||
using ConsoleMotionDevices = std::unique_ptr<Common::Input::InputDevice>;
|
||||
using TouchDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, 16>;
|
||||
|
||||
using ConsoleMotionParams = Common::ParamPackage;
|
||||
using TouchParams = std::array<Common::ParamPackage, 16>;
|
||||
|
||||
using ConsoleMotionValues = ConsoleMotionInfo;
|
||||
using TouchValues = std::array<Common::Input::TouchStatus, 16>;
|
||||
|
||||
struct TouchFinger {
|
||||
u64 last_touch{};
|
||||
Common::Point<float> position{};
|
||||
u32 id{};
|
||||
TouchAttribute attribute{};
|
||||
bool pressed{};
|
||||
};
|
||||
|
||||
// Contains all motion related data that is used on the services
|
||||
struct ConsoleMotion {
|
||||
Common::Vec3f accel{};
|
||||
Common::Vec3f gyro{};
|
||||
Common::Vec3f rotation{};
|
||||
std::array<Common::Vec3f, 3> orientation{};
|
||||
Common::Quaternion<f32> quaternion{};
|
||||
bool is_at_rest{};
|
||||
};
|
||||
|
||||
using TouchFingerState = std::array<TouchFinger, 16>;
|
||||
|
||||
struct ConsoleStatus {
|
||||
// Data from input_common
|
||||
ConsoleMotionValues motion_values{};
|
||||
TouchValues touch_values{};
|
||||
|
||||
// Data for HID services
|
||||
ConsoleMotion motion_state{};
|
||||
TouchFingerState touch_state{};
|
||||
};
|
||||
|
||||
enum class ConsoleTriggerType {
|
||||
Motion,
|
||||
Touch,
|
||||
All,
|
||||
};
|
||||
|
||||
struct ConsoleUpdateCallback {
|
||||
std::function<void(ConsoleTriggerType)> on_change;
|
||||
};
|
||||
|
||||
class EmulatedConsole {
|
||||
public:
|
||||
/**
|
||||
* Contains all input data within the emulated switch console tablet such as touch and motion
|
||||
*/
|
||||
explicit EmulatedConsole();
|
||||
~EmulatedConsole();
|
||||
|
||||
YUZU_NON_COPYABLE(EmulatedConsole);
|
||||
YUZU_NON_MOVEABLE(EmulatedConsole);
|
||||
|
||||
/// Removes all callbacks created from input devices
|
||||
void UnloadInput();
|
||||
|
||||
/**
|
||||
* Sets the emulated console into configuring mode
|
||||
* This prevents the modification of the HID state of the emulated console by input commands
|
||||
*/
|
||||
void EnableConfiguration();
|
||||
|
||||
/// Returns the emulated console into normal mode, allowing the modification of the HID state
|
||||
void DisableConfiguration();
|
||||
|
||||
/// Returns true if the emulated console is in configuring mode
|
||||
bool IsConfiguring() const;
|
||||
|
||||
/// Reload all input devices
|
||||
void ReloadInput();
|
||||
|
||||
/// Overrides current mapped devices with the stored configuration and reloads all input devices
|
||||
void ReloadFromSettings();
|
||||
|
||||
/// Saves the current mapped configuration
|
||||
void SaveCurrentConfig();
|
||||
|
||||
/// Reverts any mapped changes made that weren't saved
|
||||
void RestoreConfig();
|
||||
|
||||
// Returns the current mapped motion device
|
||||
Common::ParamPackage GetMotionParam() const;
|
||||
|
||||
/**
|
||||
* Updates the current mapped motion device
|
||||
* @param param ParamPackage with controller data to be mapped
|
||||
*/
|
||||
void SetMotionParam(Common::ParamPackage param);
|
||||
|
||||
/// Returns the latest status of motion input from the console with parameters
|
||||
ConsoleMotionValues GetMotionValues() const;
|
||||
|
||||
/// Returns the latest status of touch input from the console with parameters
|
||||
TouchValues GetTouchValues() const;
|
||||
|
||||
/// Returns the latest status of motion input from the console
|
||||
ConsoleMotion GetMotion() const;
|
||||
|
||||
/// Returns the latest status of touch input from the console
|
||||
TouchFingerState GetTouch() const;
|
||||
|
||||
/**
|
||||
* Adds a callback to the list of events
|
||||
* @param update_callback A ConsoleUpdateCallback that will be triggered
|
||||
* @return an unique key corresponding to the callback index in the list
|
||||
*/
|
||||
int SetCallback(ConsoleUpdateCallback update_callback);
|
||||
|
||||
/**
|
||||
* Removes a callback from the list stopping any future events to this object
|
||||
* @param key Key corresponding to the callback index in the list
|
||||
*/
|
||||
void DeleteCallback(int key);
|
||||
|
||||
private:
|
||||
/// Creates and stores the touch params
|
||||
void SetTouchParams();
|
||||
|
||||
/**
|
||||
* Updates the motion status of the console
|
||||
* @param callback A CallbackStatus containing gyro and accelerometer data
|
||||
*/
|
||||
void SetMotion(const Common::Input::CallbackStatus& callback);
|
||||
|
||||
/**
|
||||
* Updates the touch status of the console
|
||||
* @param callback A CallbackStatus containing the touch position
|
||||
* @param index Finger ID to be updated
|
||||
*/
|
||||
void SetTouch(const Common::Input::CallbackStatus& callback, std::size_t index);
|
||||
|
||||
/**
|
||||
* Triggers a callback that something has changed on the console status
|
||||
* @param type Input type of the event to trigger
|
||||
*/
|
||||
void TriggerOnChange(ConsoleTriggerType type);
|
||||
|
||||
bool is_configuring{false};
|
||||
f32 motion_sensitivity{0.01f};
|
||||
|
||||
ConsoleMotionParams motion_params;
|
||||
TouchParams touch_params;
|
||||
|
||||
ConsoleMotionDevices motion_devices;
|
||||
TouchDevices touch_devices;
|
||||
|
||||
mutable std::mutex mutex;
|
||||
std::unordered_map<int, ConsoleUpdateCallback> callback_list;
|
||||
int last_callback_key = 0;
|
||||
|
||||
// Stores the current status of all console input
|
||||
ConsoleStatus console;
|
||||
};
|
||||
|
||||
} // namespace Core::HID
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,411 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "common/common_types.h"
|
||||
#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 {
|
||||
const std::size_t max_emulated_controllers = 2;
|
||||
struct ControllerMotionInfo {
|
||||
Common::Input::MotionStatus raw_status{};
|
||||
MotionInput emulated{};
|
||||
};
|
||||
|
||||
using ButtonDevices =
|
||||
std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeButton::NumButtons>;
|
||||
using StickDevices =
|
||||
std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeAnalog::NumAnalogs>;
|
||||
using ControllerMotionDevices =
|
||||
std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeMotion::NumMotions>;
|
||||
using TriggerDevices =
|
||||
std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeTrigger::NumTriggers>;
|
||||
using BatteryDevices =
|
||||
std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
|
||||
using OutputDevices =
|
||||
std::array<std::unique_ptr<Common::Input::OutputDevice>, max_emulated_controllers>;
|
||||
|
||||
using ButtonParams = std::array<Common::ParamPackage, Settings::NativeButton::NumButtons>;
|
||||
using StickParams = std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs>;
|
||||
using ControllerMotionParams = std::array<Common::ParamPackage, Settings::NativeMotion::NumMotions>;
|
||||
using TriggerParams = std::array<Common::ParamPackage, Settings::NativeTrigger::NumTriggers>;
|
||||
using BatteryParams = std::array<Common::ParamPackage, max_emulated_controllers>;
|
||||
using OutputParams = std::array<Common::ParamPackage, max_emulated_controllers>;
|
||||
|
||||
using ButtonValues = std::array<Common::Input::ButtonStatus, Settings::NativeButton::NumButtons>;
|
||||
using SticksValues = std::array<Common::Input::StickStatus, Settings::NativeAnalog::NumAnalogs>;
|
||||
using TriggerValues =
|
||||
std::array<Common::Input::TriggerStatus, Settings::NativeTrigger::NumTriggers>;
|
||||
using ControllerMotionValues = std::array<ControllerMotionInfo, Settings::NativeMotion::NumMotions>;
|
||||
using ColorValues = std::array<Common::Input::BodyColorStatus, max_emulated_controllers>;
|
||||
using BatteryValues = std::array<Common::Input::BatteryStatus, max_emulated_controllers>;
|
||||
using VibrationValues = std::array<Common::Input::VibrationStatus, max_emulated_controllers>;
|
||||
|
||||
struct AnalogSticks {
|
||||
AnalogStickState left{};
|
||||
AnalogStickState right{};
|
||||
};
|
||||
|
||||
struct ControllerColors {
|
||||
NpadControllerColor fullkey{};
|
||||
NpadControllerColor left{};
|
||||
NpadControllerColor right{};
|
||||
};
|
||||
|
||||
struct BatteryLevelState {
|
||||
NpadPowerInfo dual{};
|
||||
NpadPowerInfo left{};
|
||||
NpadPowerInfo right{};
|
||||
};
|
||||
|
||||
struct ControllerMotion {
|
||||
Common::Vec3f accel{};
|
||||
Common::Vec3f gyro{};
|
||||
Common::Vec3f rotation{};
|
||||
std::array<Common::Vec3f, 3> orientation{};
|
||||
bool is_at_rest{};
|
||||
};
|
||||
|
||||
enum EmulatedDeviceIndex : u8 {
|
||||
LeftIndex,
|
||||
RightIndex,
|
||||
DualIndex,
|
||||
AllDevices,
|
||||
};
|
||||
|
||||
using MotionState = std::array<ControllerMotion, 2>;
|
||||
|
||||
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 HID serices
|
||||
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<void(ControllerTriggerType)> on_change;
|
||||
bool is_npad_service;
|
||||
};
|
||||
|
||||
class EmulatedController {
|
||||
public:
|
||||
/**
|
||||
* Contains all input data (buttons, joysticks, vibration, and motion) within this controller.
|
||||
* @param npad_id_type npad id type for this specific controller
|
||||
*/
|
||||
explicit EmulatedController(NpadIdType npad_id_type_);
|
||||
~EmulatedController();
|
||||
|
||||
YUZU_NON_COPYABLE(EmulatedController);
|
||||
YUZU_NON_MOVEABLE(EmulatedController);
|
||||
|
||||
/// Converts the controller type from settings to npad type
|
||||
static NpadStyleIndex MapSettingsTypeToNPad(Settings::ControllerType type);
|
||||
|
||||
/// Converts npad type to the equivalent of controller type from settings
|
||||
static Settings::ControllerType MapNPadToSettingsType(NpadStyleIndex type);
|
||||
|
||||
/// Gets the NpadIdType for this controller
|
||||
NpadIdType GetNpadIdType() const;
|
||||
|
||||
/// Sets the NpadStyleIndex for this controller
|
||||
void SetNpadStyleIndex(NpadStyleIndex npad_type_);
|
||||
|
||||
/**
|
||||
* Gets the NpadStyleIndex for this controller
|
||||
* @param get_temporary_value If true tmp_npad_type will be returned
|
||||
* @return NpadStyleIndex set on the controller
|
||||
*/
|
||||
NpadStyleIndex GetNpadStyleIndex(bool get_temporary_value = false) const;
|
||||
|
||||
/**
|
||||
* Sets the supported controller types. Disconnects the controller if current type is not
|
||||
* supported
|
||||
* @param supported_styles bitflag with supported types
|
||||
*/
|
||||
void SetSupportedNpadStyleTag(NpadStyleTag supported_styles);
|
||||
|
||||
/// Sets the connected status to true
|
||||
void Connect();
|
||||
|
||||
/// Sets the connected status to false
|
||||
void Disconnect();
|
||||
|
||||
/**
|
||||
* Is the emulated connected
|
||||
* @param get_temporary_value If true tmp_is_connected will be returned
|
||||
* @return true if the controller has the connected status
|
||||
*/
|
||||
bool IsConnected(bool get_temporary_value = false) const;
|
||||
|
||||
/// Returns true if vibration is enabled
|
||||
bool IsVibrationEnabled() const;
|
||||
|
||||
/// Removes all callbacks created from input devices
|
||||
void UnloadInput();
|
||||
|
||||
/**
|
||||
* Sets the emulated controller into configuring mode
|
||||
* This prevents the modification of the HID state of the emulated controller by input commands
|
||||
*/
|
||||
void EnableConfiguration();
|
||||
|
||||
/// Returns the emulated controller into normal mode, allowing the modification of the HID state
|
||||
void DisableConfiguration();
|
||||
|
||||
/// Returns true if the emulated controller is in configuring mode
|
||||
bool IsConfiguring() const;
|
||||
|
||||
/// Reload all input devices
|
||||
void ReloadInput();
|
||||
|
||||
/// Overrides current mapped devices with the stored configuration and reloads all input devices
|
||||
void ReloadFromSettings();
|
||||
|
||||
/// Saves the current mapped configuration
|
||||
void SaveCurrentConfig();
|
||||
|
||||
/// Reverts any mapped changes made that weren't saved
|
||||
void RestoreConfig();
|
||||
|
||||
/// Returns a vector of mapped devices from the mapped button and stick parameters
|
||||
std::vector<Common::ParamPackage> GetMappedDevices(EmulatedDeviceIndex device_index) const;
|
||||
|
||||
// Returns the current mapped button device
|
||||
Common::ParamPackage GetButtonParam(std::size_t index) const;
|
||||
|
||||
// Returns the current mapped stick device
|
||||
Common::ParamPackage GetStickParam(std::size_t index) const;
|
||||
|
||||
// Returns the current mapped motion device
|
||||
Common::ParamPackage GetMotionParam(std::size_t index) const;
|
||||
|
||||
/**
|
||||
* Updates the current mapped button device
|
||||
* @param param ParamPackage with controller data to be mapped
|
||||
*/
|
||||
void SetButtonParam(std::size_t index, Common::ParamPackage param);
|
||||
|
||||
/**
|
||||
* Updates the current mapped stick device
|
||||
* @param param ParamPackage with controller data to be mapped
|
||||
*/
|
||||
void SetStickParam(std::size_t index, Common::ParamPackage param);
|
||||
|
||||
/**
|
||||
* Updates the current mapped motion device
|
||||
* @param param ParamPackage with controller data to be mapped
|
||||
*/
|
||||
void SetMotionParam(std::size_t index, Common::ParamPackage param);
|
||||
|
||||
/// Returns the latest button status from the controller with parameters
|
||||
ButtonValues GetButtonsValues() const;
|
||||
|
||||
/// Returns the latest analog stick status from the controller with parameters
|
||||
SticksValues GetSticksValues() const;
|
||||
|
||||
/// Returns the latest trigger status from the controller with parameters
|
||||
TriggerValues GetTriggersValues() const;
|
||||
|
||||
/// Returns the latest motion status from the controller with parameters
|
||||
ControllerMotionValues GetMotionValues() const;
|
||||
|
||||
/// Returns the latest color status from the controller with parameters
|
||||
ColorValues GetColorsValues() const;
|
||||
|
||||
/// Returns the latest battery status from the controller with parameters
|
||||
BatteryValues GetBatteryValues() const;
|
||||
|
||||
/// Returns the latest status of button input for the npad service
|
||||
NpadButtonState GetNpadButtons() const;
|
||||
|
||||
/// Returns the latest status of button input for the debug pad service
|
||||
DebugPadButton GetDebugPadButtons() const;
|
||||
|
||||
/// Returns the latest status of stick input from the mouse
|
||||
AnalogSticks GetSticks() const;
|
||||
|
||||
/// Returns the latest status of trigger input from the mouse
|
||||
NpadGcTriggerState GetTriggers() const;
|
||||
|
||||
/// Returns the latest status of motion input from the mouse
|
||||
MotionState GetMotions() const;
|
||||
|
||||
/// Returns the latest color value from the controller
|
||||
ControllerColors GetColors() const;
|
||||
|
||||
/// Returns the latest battery status from the controller
|
||||
BatteryLevelState GetBattery() const;
|
||||
|
||||
/**
|
||||
* Sends a specific vibration to the output device
|
||||
* @return returns true if vibration had no errors
|
||||
*/
|
||||
bool SetVibration(std::size_t device_index, VibrationValue vibration);
|
||||
|
||||
/**
|
||||
* Sends a small vibration to the output device
|
||||
* @return returns true if SetVibration was successfull
|
||||
*/
|
||||
bool TestVibration(std::size_t device_index);
|
||||
|
||||
/// Returns the led pattern corresponding to this emulated controller
|
||||
LedPattern GetLedPattern() const;
|
||||
|
||||
/// Asks the output device to change the player led pattern
|
||||
void SetLedPattern();
|
||||
|
||||
/**
|
||||
* Adds a callback to the list of events
|
||||
* @param update_callback A ConsoleUpdateCallback that will be triggered
|
||||
* @return an unique key corresponding to the callback index in the list
|
||||
*/
|
||||
int SetCallback(ControllerUpdateCallback update_callback);
|
||||
|
||||
/**
|
||||
* Removes a callback from the list stopping any future events to this object
|
||||
* @param key Key corresponding to the callback index in the list
|
||||
*/
|
||||
void DeleteCallback(int key);
|
||||
|
||||
private:
|
||||
/// creates input devices from params
|
||||
void LoadDevices();
|
||||
|
||||
/// Set the params for TAS devices
|
||||
void LoadTASParams();
|
||||
|
||||
/**
|
||||
* Checks the current controller type against the supported_style_tag
|
||||
* @return true if the controller is supported
|
||||
*/
|
||||
bool IsControllerSupported() const;
|
||||
|
||||
/**
|
||||
* Updates the button status of the controller
|
||||
* @param callback A CallbackStatus containing the button status
|
||||
* @param index Button ID of the to be updated
|
||||
*/
|
||||
void SetButton(const Common::Input::CallbackStatus& callback, std::size_t index,
|
||||
Common::UUID uuid);
|
||||
|
||||
/**
|
||||
* Updates the analog stick status of the controller
|
||||
* @param callback A CallbackStatus containing the analog stick status
|
||||
* @param index stick ID of the to be updated
|
||||
*/
|
||||
void SetStick(const Common::Input::CallbackStatus& callback, std::size_t index,
|
||||
Common::UUID uuid);
|
||||
|
||||
/**
|
||||
* Updates the trigger status of the controller
|
||||
* @param callback A CallbackStatus containing the trigger status
|
||||
* @param index trigger ID of the to be updated
|
||||
*/
|
||||
void SetTrigger(const Common::Input::CallbackStatus& callback, std::size_t index,
|
||||
Common::UUID uuid);
|
||||
|
||||
/**
|
||||
* Updates the motion status of the controller
|
||||
* @param callback A CallbackStatus containing gyro and accelerometer data
|
||||
* @param index motion ID of the to be updated
|
||||
*/
|
||||
void SetMotion(const Common::Input::CallbackStatus& callback, std::size_t index);
|
||||
|
||||
/**
|
||||
* Updates the battery status of the controller
|
||||
* @param callback A CallbackStatus containing the battery status
|
||||
* @param index Button ID of the to be updated
|
||||
*/
|
||||
void SetBattery(const Common::Input::CallbackStatus& callback, std::size_t index);
|
||||
|
||||
/**
|
||||
* Triggers a callback that something has changed on the controller status
|
||||
* @param type Input type of the event to trigger
|
||||
* @param is_service_update indicates if this event should only be sent to HID services
|
||||
*/
|
||||
void TriggerOnChange(ControllerTriggerType type, bool is_service_update);
|
||||
|
||||
NpadIdType npad_id_type;
|
||||
NpadStyleIndex npad_type{NpadStyleIndex::None};
|
||||
NpadStyleTag supported_style_tag{NpadStyleSet::All};
|
||||
bool is_connected{false};
|
||||
bool is_configuring{false};
|
||||
f32 motion_sensitivity{0.01f};
|
||||
bool force_update_motion{false};
|
||||
|
||||
// Temporary values to avoid doing changes while the controller is in configuring mode
|
||||
NpadStyleIndex tmp_npad_type{NpadStyleIndex::None};
|
||||
bool tmp_is_connected{false};
|
||||
|
||||
ButtonParams button_params;
|
||||
StickParams stick_params;
|
||||
ControllerMotionParams motion_params;
|
||||
TriggerParams trigger_params;
|
||||
BatteryParams battery_params;
|
||||
OutputParams output_params;
|
||||
|
||||
ButtonDevices button_devices;
|
||||
StickDevices stick_devices;
|
||||
ControllerMotionDevices motion_devices;
|
||||
TriggerDevices trigger_devices;
|
||||
BatteryDevices battery_devices;
|
||||
OutputDevices output_devices;
|
||||
|
||||
// TAS related variables
|
||||
ButtonParams tas_button_params;
|
||||
StickParams tas_stick_params;
|
||||
ButtonDevices tas_button_devices;
|
||||
StickDevices tas_stick_devices;
|
||||
|
||||
mutable std::mutex mutex;
|
||||
std::unordered_map<int, ControllerUpdateCallback> callback_list;
|
||||
int last_callback_key = 0;
|
||||
|
||||
// Stores the current status of all controller input
|
||||
ControllerStatus controller;
|
||||
};
|
||||
|
||||
} // namespace Core::HID
|
@ -0,0 +1,459 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included
|
||||
|
||||
#include <algorithm>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "core/hid/emulated_devices.h"
|
||||
#include "core/hid/input_converter.h"
|
||||
|
||||
namespace Core::HID {
|
||||
|
||||
EmulatedDevices::EmulatedDevices() = default;
|
||||
|
||||
EmulatedDevices::~EmulatedDevices() = default;
|
||||
|
||||
void EmulatedDevices::ReloadFromSettings() {
|
||||
ReloadInput();
|
||||
}
|
||||
|
||||
void EmulatedDevices::ReloadInput() {
|
||||
// If you load any device here add the equivalent to the UnloadInput() function
|
||||
std::size_t key_index = 0;
|
||||
for (auto& mouse_device : mouse_button_devices) {
|
||||
Common::ParamPackage mouse_params;
|
||||
mouse_params.Set("engine", "mouse");
|
||||
mouse_params.Set("button", static_cast<int>(key_index));
|
||||
mouse_device = Common::Input::CreateDevice<Common::Input::InputDevice>(mouse_params);
|
||||
key_index++;
|
||||
}
|
||||
|
||||
mouse_stick_device = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
|
||||
"engine:mouse,axis_x:0,axis_y:1");
|
||||
|
||||
// First two axis are reserved for mouse position
|
||||
key_index = 2;
|
||||
for (auto& mouse_device : mouse_analog_devices) {
|
||||
Common::ParamPackage mouse_params;
|
||||
mouse_params.Set("engine", "mouse");
|
||||
mouse_params.Set("axis", static_cast<int>(key_index));
|
||||
mouse_device = Common::Input::CreateDevice<Common::Input::InputDevice>(mouse_params);
|
||||
key_index++;
|
||||
}
|
||||
|
||||
key_index = 0;
|
||||
for (auto& keyboard_device : keyboard_devices) {
|
||||
// Keyboard keys are only mapped on port 1, pad 0
|
||||
Common::ParamPackage keyboard_params;
|
||||
keyboard_params.Set("engine", "keyboard");
|
||||
keyboard_params.Set("button", static_cast<int>(key_index));
|
||||
keyboard_params.Set("port", 1);
|
||||
keyboard_params.Set("pad", 0);
|
||||
keyboard_device = Common::Input::CreateDevice<Common::Input::InputDevice>(keyboard_params);
|
||||
key_index++;
|
||||
}
|
||||
|
||||
key_index = 0;
|
||||
for (auto& keyboard_device : keyboard_modifier_devices) {
|
||||
// Keyboard moddifiers are only mapped on port 1, pad 1
|
||||
Common::ParamPackage keyboard_params;
|
||||
keyboard_params.Set("engine", "keyboard");
|
||||
keyboard_params.Set("button", static_cast<int>(key_index));
|
||||
keyboard_params.Set("port", 1);
|
||||
keyboard_params.Set("pad", 1);
|
||||
keyboard_device = Common::Input::CreateDevice<Common::Input::InputDevice>(keyboard_params);
|
||||
key_index++;
|
||||
}
|
||||
|
||||
for (std::size_t index = 0; index < mouse_button_devices.size(); ++index) {
|
||||
if (!mouse_button_devices[index]) {
|
||||
continue;
|
||||
}
|
||||
mouse_button_devices[index]->SetCallback({
|
||||
.on_change =
|
||||
[this, index](const Common::Input::CallbackStatus& callback) {
|
||||
SetMouseButton(callback, index);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (std::size_t index = 0; index < mouse_analog_devices.size(); ++index) {
|
||||
if (!mouse_analog_devices[index]) {
|
||||
continue;
|
||||
}
|
||||
mouse_analog_devices[index]->SetCallback({
|
||||
.on_change =
|
||||
[this, index](const Common::Input::CallbackStatus& callback) {
|
||||
SetMouseAnalog(callback, index);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (mouse_stick_device) {
|
||||
mouse_stick_device->SetCallback({
|
||||
.on_change =
|
||||
[this](const Common::Input::CallbackStatus& callback) { SetMouseStick(callback); },
|
||||
});
|
||||
}
|
||||
|
||||
for (std::size_t index = 0; index < keyboard_devices.size(); ++index) {
|
||||
if (!keyboard_devices[index]) {
|
||||
continue;
|
||||
}
|
||||
keyboard_devices[index]->SetCallback({
|
||||
.on_change =
|
||||
[this, index](const Common::Input::CallbackStatus& callback) {
|
||||
SetKeyboardButton(callback, index);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (std::size_t index = 0; index < keyboard_modifier_devices.size(); ++index) {
|
||||
if (!keyboard_modifier_devices[index]) {
|
||||
continue;
|
||||
}
|
||||
keyboard_modifier_devices[index]->SetCallback({
|
||||
.on_change =
|
||||
[this, index](const Common::Input::CallbackStatus& callback) {
|
||||
SetKeyboardModifier(callback, index);
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void EmulatedDevices::UnloadInput() {
|
||||
for (auto& button : mouse_button_devices) {
|
||||
button.reset();
|
||||
}
|
||||
for (auto& analog : mouse_analog_devices) {
|
||||
analog.reset();
|
||||
}
|
||||
mouse_stick_device.reset();
|
||||
for (auto& button : keyboard_devices) {
|
||||
button.reset();
|
||||
}
|
||||
for (auto& button : keyboard_modifier_devices) {
|
||||
button.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void EmulatedDevices::EnableConfiguration() {
|
||||
is_configuring = true;
|
||||
SaveCurrentConfig();
|
||||
}
|
||||
|
||||
void EmulatedDevices::DisableConfiguration() {
|
||||
is_configuring = false;
|
||||
}
|
||||
|
||||
bool EmulatedDevices::IsConfiguring() const {
|
||||
return is_configuring;
|
||||
}
|
||||
|
||||
void EmulatedDevices::SaveCurrentConfig() {
|
||||
if (!is_configuring) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void EmulatedDevices::RestoreConfig() {
|
||||
if (!is_configuring) {
|
||||
return;
|
||||
}
|
||||
ReloadFromSettings();
|
||||
}
|
||||
|
||||
void EmulatedDevices::SetKeyboardButton(const Common::Input::CallbackStatus& callback,
|
||||
std::size_t index) {
|
||||
if (index >= device_status.keyboard_values.size()) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard lock{mutex};
|
||||
bool value_changed = false;
|
||||
const auto new_status = TransformToButton(callback);
|
||||
auto& current_status = device_status.keyboard_values[index];
|
||||
current_status.toggle = new_status.toggle;
|
||||
|
||||
// Update button status with current status
|
||||
if (!current_status.toggle) {
|
||||
current_status.locked = false;
|
||||
if (current_status.value != new_status.value) {
|
||||
current_status.value = new_status.value;
|
||||
value_changed = true;
|
||||
}
|
||||
} else {
|
||||
// Toggle button and lock status
|
||||
if (new_status.value && !current_status.locked) {
|
||||
current_status.locked = true;
|
||||
current_status.value = !current_status.value;
|
||||
value_changed = true;
|
||||
}
|
||||
|
||||
// Unlock button, ready for next press
|
||||
if (!new_status.value && current_status.locked) {
|
||||
current_status.locked = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!value_changed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_configuring) {
|
||||
TriggerOnChange(DeviceTriggerType::Keyboard);
|
||||
return;
|
||||
}
|
||||
|
||||
// Index should be converted from NativeKeyboard to KeyboardKeyIndex
|
||||
UpdateKey(index, current_status.value);
|
||||
|
||||
TriggerOnChange(DeviceTriggerType::Keyboard);
|
||||
}
|
||||
|
||||
void EmulatedDevices::UpdateKey(std::size_t key_index, bool status) {
|
||||
constexpr std::size_t KEYS_PER_BYTE = 8;
|
||||
auto& entry = device_status.keyboard_state.key[key_index / KEYS_PER_BYTE];
|
||||
const u8 mask = static_cast<u8>(1 << (key_index % KEYS_PER_BYTE));
|
||||
if (status) {
|
||||
entry = entry | mask;
|
||||
} else {
|
||||
entry = static_cast<u8>(entry & ~mask);
|
||||
}
|
||||
}
|
||||
|
||||
void EmulatedDevices::SetKeyboardModifier(const Common::Input::CallbackStatus& callback,
|
||||
std::size_t index) {
|
||||
if (index >= device_status.keyboard_moddifier_values.size()) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard lock{mutex};
|
||||
bool value_changed = false;
|
||||
const auto new_status = TransformToButton(callback);
|
||||
auto& current_status = device_status.keyboard_moddifier_values[index];
|
||||
current_status.toggle = new_status.toggle;
|
||||
|
||||
// Update button status with current
|
||||
if (!current_status.toggle) {
|
||||
current_status.locked = false;
|
||||
if (current_status.value != new_status.value) {
|
||||
current_status.value = new_status.value;
|
||||
value_changed = true;
|
||||
}
|
||||
} else {
|
||||
// Toggle button and lock status
|
||||
if (new_status.value && !current_status.locked) {
|
||||
current_status.locked = true;
|
||||
current_status.value = !current_status.value;
|
||||
value_changed = true;
|
||||
}
|
||||
|
||||
// Unlock button ready for next press
|
||||
if (!new_status.value && current_status.locked) {
|
||||
current_status.locked = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!value_changed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_configuring) {
|
||||
TriggerOnChange(DeviceTriggerType::KeyboardModdifier);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (index) {
|
||||
case Settings::NativeKeyboard::LeftControl:
|
||||
case Settings::NativeKeyboard::RightControl:
|
||||
device_status.keyboard_moddifier_state.control.Assign(current_status.value);
|
||||
break;
|
||||
case Settings::NativeKeyboard::LeftShift:
|
||||
case Settings::NativeKeyboard::RightShift:
|
||||
device_status.keyboard_moddifier_state.shift.Assign(current_status.value);
|
||||
break;
|
||||
case Settings::NativeKeyboard::LeftAlt:
|
||||
device_status.keyboard_moddifier_state.left_alt.Assign(current_status.value);
|
||||
break;
|
||||
case Settings::NativeKeyboard::RightAlt:
|
||||
device_status.keyboard_moddifier_state.right_alt.Assign(current_status.value);
|
||||
break;
|
||||
case Settings::NativeKeyboard::CapsLock:
|
||||
device_status.keyboard_moddifier_state.caps_lock.Assign(current_status.value);
|
||||
break;
|
||||
case Settings::NativeKeyboard::ScrollLock:
|
||||
device_status.keyboard_moddifier_state.scroll_lock.Assign(current_status.value);
|
||||
break;
|
||||
case Settings::NativeKeyboard::NumLock:
|
||||
device_status.keyboard_moddifier_state.num_lock.Assign(current_status.value);
|
||||
break;
|
||||
}
|
||||
|
||||
TriggerOnChange(DeviceTriggerType::KeyboardModdifier);
|
||||
}
|
||||
|
||||
void EmulatedDevices::SetMouseButton(const Common::Input::CallbackStatus& callback,
|
||||
std::size_t index) {
|
||||
if (index >= device_status.mouse_button_values.size()) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard lock{mutex};
|
||||
bool value_changed = false;
|
||||
const auto new_status = TransformToButton(callback);
|
||||
auto& current_status = device_status.mouse_button_values[index];
|
||||
current_status.toggle = new_status.toggle;
|
||||
|
||||
// Update button status with current
|
||||
if (!current_status.toggle) {
|
||||
current_status.locked = false;
|
||||
if (current_status.value != new_status.value) {
|
||||
current_status.value = new_status.value;
|
||||
value_changed = true;
|
||||
}
|
||||
} else {
|
||||
// Toggle button and lock status
|
||||
if (new_status.value && !current_status.locked) {
|
||||
current_status.locked = true;
|
||||
current_status.value = !current_status.value;
|
||||
value_changed = true;
|
||||
}
|
||||
|
||||
// Unlock button ready for next press
|
||||
if (!new_status.value && current_status.locked) {
|
||||
current_status.locked = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!value_changed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_configuring) {
|
||||
TriggerOnChange(DeviceTriggerType::Mouse);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (index) {
|
||||
case Settings::NativeMouseButton::Left:
|
||||
device_status.mouse_button_state.left.Assign(current_status.value);
|
||||
break;
|
||||
case Settings::NativeMouseButton::Right:
|
||||
device_status.mouse_button_state.right.Assign(current_status.value);
|
||||
break;
|
||||
case Settings::NativeMouseButton::Middle:
|
||||
device_status.mouse_button_state.middle.Assign(current_status.value);
|
||||
break;
|
||||
case Settings::NativeMouseButton::Forward:
|
||||
device_status.mouse_button_state.forward.Assign(current_status.value);
|
||||
break;
|
||||
case Settings::NativeMouseButton::Back:
|
||||
device_status.mouse_button_state.back.Assign(current_status.value);
|
||||
break;
|
||||
}
|
||||
|
||||
TriggerOnChange(DeviceTriggerType::Mouse);
|
||||
}
|
||||
|
||||
void EmulatedDevices::SetMouseAnalog(const Common::Input::CallbackStatus& callback,
|
||||
std::size_t index) {
|
||||
if (index >= device_status.mouse_analog_values.size()) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard lock{mutex};
|
||||
const auto analog_value = TransformToAnalog(callback);
|
||||
|
||||
device_status.mouse_analog_values[index] = analog_value;
|
||||
|
||||
if (is_configuring) {
|
||||
device_status.mouse_position_state = {};
|
||||
TriggerOnChange(DeviceTriggerType::Mouse);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (index) {
|
||||
case Settings::NativeMouseWheel::X:
|
||||
device_status.mouse_wheel_state.x = static_cast<s32>(analog_value.value);
|
||||
break;
|
||||
case Settings::NativeMouseWheel::Y:
|
||||
device_status.mouse_wheel_state.y = static_cast<s32>(analog_value.value);
|
||||
break;
|
||||
}
|
||||
|
||||
TriggerOnChange(DeviceTriggerType::Mouse);
|
||||
}
|
||||
|
||||
void EmulatedDevices::SetMouseStick(const Common::Input::CallbackStatus& callback) {
|
||||
std::lock_guard lock{mutex};
|
||||
const auto touch_value = TransformToTouch(callback);
|
||||
|
||||
device_status.mouse_stick_value = touch_value;
|
||||
|
||||
if (is_configuring) {
|
||||
device_status.mouse_position_state = {};
|
||||
TriggerOnChange(DeviceTriggerType::Mouse);
|
||||
return;
|
||||
}
|
||||
|
||||
device_status.mouse_position_state.x = touch_value.x.value;
|
||||
device_status.mouse_position_state.y = touch_value.y.value;
|
||||
|
||||
TriggerOnChange(DeviceTriggerType::Mouse);
|
||||
}
|
||||
|
||||
KeyboardValues EmulatedDevices::GetKeyboardValues() const {
|
||||
return device_status.keyboard_values;
|
||||
}
|
||||
|
||||
KeyboardModifierValues EmulatedDevices::GetKeyboardModdifierValues() const {
|
||||
return device_status.keyboard_moddifier_values;
|
||||
}
|
||||
|
||||
MouseButtonValues EmulatedDevices::GetMouseButtonsValues() const {
|
||||
return device_status.mouse_button_values;
|
||||
}
|
||||
|
||||
KeyboardKey EmulatedDevices::GetKeyboard() const {
|
||||
return device_status.keyboard_state;
|
||||
}
|
||||
|
||||
KeyboardModifier EmulatedDevices::GetKeyboardModifier() const {
|
||||
return device_status.keyboard_moddifier_state;
|
||||
}
|
||||
|
||||
MouseButton EmulatedDevices::GetMouseButtons() const {
|
||||
return device_status.mouse_button_state;
|
||||
}
|
||||
|
||||
MousePosition EmulatedDevices::GetMousePosition() const {
|
||||
return device_status.mouse_position_state;
|
||||
}
|
||||
|
||||
AnalogStickState EmulatedDevices::GetMouseWheel() const {
|
||||
return device_status.mouse_wheel_state;
|
||||
}
|
||||
|
||||
void EmulatedDevices::TriggerOnChange(DeviceTriggerType type) {
|
||||
for (const auto& poller_pair : callback_list) {
|
||||
const InterfaceUpdateCallback& poller = poller_pair.second;
|
||||
if (poller.on_change) {
|
||||
poller.on_change(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int EmulatedDevices::SetCallback(InterfaceUpdateCallback update_callback) {
|
||||
std::lock_guard lock{mutex};
|
||||
callback_list.insert_or_assign(last_callback_key, std::move(update_callback));
|
||||
return last_callback_key++;
|
||||
}
|
||||
|
||||
void EmulatedDevices::DeleteCallback(int key) {
|
||||
std::lock_guard lock{mutex};
|
||||
const auto& iterator = callback_list.find(key);
|
||||
if (iterator == callback_list.end()) {
|
||||
LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
|
||||
return;
|
||||
}
|
||||
callback_list.erase(iterator);
|
||||
}
|
||||
} // namespace Core::HID
|
@ -0,0 +1,210 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "common/common_types.h"
|
||||
#include "common/input.h"
|
||||
#include "common/param_package.h"
|
||||
#include "common/settings.h"
|
||||
#include "core/hid/hid_types.h"
|
||||
|
||||
namespace Core::HID {
|
||||
using KeyboardDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
|
||||
Settings::NativeKeyboard::NumKeyboardKeys>;
|
||||
using KeyboardModifierDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
|
||||
Settings::NativeKeyboard::NumKeyboardMods>;
|
||||
using MouseButtonDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
|
||||
Settings::NativeMouseButton::NumMouseButtons>;
|
||||
using MouseAnalogDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
|
||||
Settings::NativeMouseWheel::NumMouseWheels>;
|
||||
using MouseStickDevice = std::unique_ptr<Common::Input::InputDevice>;
|
||||
|
||||
using MouseButtonParams =
|
||||
std::array<Common::ParamPackage, Settings::NativeMouseButton::NumMouseButtons>;
|
||||
|
||||
using KeyboardValues =
|
||||
std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardKeys>;
|
||||
using KeyboardModifierValues =
|
||||
std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardMods>;
|
||||
using MouseButtonValues =
|
||||
std::array<Common::Input::ButtonStatus, Settings::NativeMouseButton::NumMouseButtons>;
|
||||
using MouseAnalogValues =
|
||||
std::array<Common::Input::AnalogStatus, Settings::NativeMouseWheel::NumMouseWheels>;
|
||||
using MouseStickValue = Common::Input::TouchStatus;
|
||||
|
||||
struct MousePosition {
|
||||
f32 x;
|
||||
f32 y;
|
||||
};
|
||||
|
||||
struct DeviceStatus {
|
||||
// Data from input_common
|
||||
KeyboardValues keyboard_values{};
|
||||
KeyboardModifierValues keyboard_moddifier_values{};
|
||||
MouseButtonValues mouse_button_values{};
|
||||
MouseAnalogValues mouse_analog_values{};
|
||||
MouseStickValue mouse_stick_value{};
|
||||
|
||||
// Data for HID serices
|
||||
KeyboardKey keyboard_state{};
|
||||
KeyboardModifier keyboard_moddifier_state{};
|
||||
MouseButton mouse_button_state{};
|
||||
MousePosition mouse_position_state{};
|
||||
AnalogStickState mouse_wheel_state{};
|
||||
};
|
||||
|
||||
enum class DeviceTriggerType {
|
||||
Keyboard,
|
||||
KeyboardModdifier,
|
||||
Mouse,
|
||||
};
|
||||
|
||||
struct InterfaceUpdateCallback {
|
||||
std::function<void(DeviceTriggerType)> on_change;
|
||||
};
|
||||
|
||||
class EmulatedDevices {
|
||||
public:
|
||||
/**
|
||||
* Contains all input data related to external devices that aren't necesarily a controller
|
||||
* This includes devices such as the keyboard or mouse
|
||||
*/
|
||||
explicit EmulatedDevices();
|
||||
~EmulatedDevices();
|
||||
|
||||
YUZU_NON_COPYABLE(EmulatedDevices);
|
||||
YUZU_NON_MOVEABLE(EmulatedDevices);
|
||||
|
||||
/// Removes all callbacks created from input devices
|
||||
void UnloadInput();
|
||||
|
||||
/**
|
||||
* Sets the emulated devices into configuring mode
|
||||
* This prevents the modification of the HID state of the emulated devices by input commands
|
||||
*/
|
||||
void EnableConfiguration();
|
||||
|
||||
/// Returns the emulated devices into normal mode, allowing the modification of the HID state
|
||||
void DisableConfiguration();
|
||||
|
||||
/// Returns true if the emulated device is in configuring mode
|
||||
bool IsConfiguring() const;
|
||||
|
||||
/// Reload all input devices
|
||||
void ReloadInput();
|
||||
|
||||
/// Overrides current mapped devices with the stored configuration and reloads all input devices
|
||||
void ReloadFromSettings();
|
||||
|
||||
/// Saves the current mapped configuration
|
||||
void SaveCurrentConfig();
|
||||
|
||||
/// Reverts any mapped changes made that weren't saved
|
||||
void RestoreConfig();
|
||||
|
||||
/// Returns the latest status of button input from the keyboard with parameters
|
||||
KeyboardValues GetKeyboardValues() const;
|
||||
|
||||
/// Returns the latest status of button input from the keyboard modifiers with parameters
|
||||
KeyboardModifierValues GetKeyboardModdifierValues() const;
|
||||
|
||||
/// Returns the latest status of button input from the mouse with parameters
|
||||
MouseButtonValues GetMouseButtonsValues() const;
|
||||
|
||||
/// Returns the latest status of button input from the keyboard
|
||||
KeyboardKey GetKeyboard() const;
|
||||
|
||||
/// Returns the latest status of button input from the keyboard modifiers
|
||||
KeyboardModifier GetKeyboardModifier() const;
|
||||
|
||||
/// Returns the latest status of button input from the mouse
|
||||
MouseButton GetMouseButtons() const;
|
||||
|
||||
/// Returns the latest mouse coordinates
|
||||
MousePosition GetMousePosition() const;
|
||||
|
||||
/// Returns the latest mouse wheel change
|
||||
AnalogStickState GetMouseWheel() const;
|
||||
|
||||
/**
|
||||
* Adds a callback to the list of events
|
||||
* @param update_callback InterfaceUpdateCallback that will be triggered
|
||||
* @return an unique key corresponding to the callback index in the list
|
||||
*/
|
||||
int SetCallback(InterfaceUpdateCallback update_callback);
|
||||
|
||||
/**
|
||||
* Removes a callback from the list stopping any future events to this object
|
||||
* @param key Key corresponding to the callback index in the list
|
||||
*/
|
||||
void DeleteCallback(int key);
|
||||
|
||||
private:
|
||||
/// Helps assigning a value to keyboard_state
|
||||
void UpdateKey(std::size_t key_index, bool status);
|
||||
|
||||
/**
|
||||
* Updates the touch status of the keyboard device
|
||||
* @param callback A CallbackStatus containing the key status
|
||||
* @param index key ID to be updated
|
||||
*/
|
||||
void SetKeyboardButton(const Common::Input::CallbackStatus& callback, std::size_t index);
|
||||
|
||||
/**
|
||||
* Updates the keyboard status of the keyboard device
|
||||
* @param callback A CallbackStatus containing the modifier key status
|
||||
* @param index modifier key ID to be updated
|
||||
*/
|
||||
void SetKeyboardModifier(const Common::Input::CallbackStatus& callback, std::size_t index);
|
||||
|
||||
/**
|
||||
* Updates the mouse button status of the mouse device
|
||||
* @param callback A CallbackStatus containing the button status
|
||||
* @param index Button ID to be updated
|
||||
*/
|
||||
void SetMouseButton(const Common::Input::CallbackStatus& callback, std::size_t index);
|
||||
|
||||
/**
|
||||
* Updates the mouse wheel status of the mouse device
|
||||
* @param callback A CallbackStatus containing the wheel status
|
||||
* @param index wheel ID to be updated
|
||||
*/
|
||||
void SetMouseAnalog(const Common::Input::CallbackStatus& callback, std::size_t index);
|
||||
|
||||
/**
|
||||
* Updates the mouse position status of the mouse device
|
||||
* @param callback A CallbackStatus containing the position status
|
||||
*/
|
||||
void SetMouseStick(const Common::Input::CallbackStatus& callback);
|
||||
|
||||
/**
|
||||
* Triggers a callback that something has changed on the device status
|
||||
* @param type Input type of the event to trigger
|
||||
*/
|
||||
void TriggerOnChange(DeviceTriggerType type);
|
||||
|
||||
bool is_configuring{false};
|
||||
|
||||
KeyboardDevices keyboard_devices;
|
||||
KeyboardModifierDevices keyboard_modifier_devices;
|
||||
MouseButtonDevices mouse_button_devices;
|
||||
MouseAnalogDevices mouse_analog_devices;
|
||||
MouseStickDevice mouse_stick_device;
|
||||
|
||||
mutable std::mutex mutex;
|
||||
std::unordered_map<int, InterfaceUpdateCallback> callback_list;
|
||||
int last_callback_key = 0;
|
||||
|
||||
// Stores the current status of all external device input
|
||||
DeviceStatus device_status;
|
||||
};
|
||||
|
||||
} // namespace Core::HID
|
@ -0,0 +1,214 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "common/assert.h"
|
||||
#include "core/hid/emulated_console.h"
|
||||
#include "core/hid/emulated_controller.h"
|
||||
#include "core/hid/emulated_devices.h"
|
||||
#include "core/hid/hid_core.h"
|
||||
|
||||
namespace Core::HID {
|
||||
|
||||
HIDCore::HIDCore()
|
||||
: player_1{std::make_unique<EmulatedController>(NpadIdType::Player1)},
|
||||
player_2{std::make_unique<EmulatedController>(NpadIdType::Player2)},
|
||||
player_3{std::make_unique<EmulatedController>(NpadIdType::Player3)},
|
||||
player_4{std::make_unique<EmulatedController>(NpadIdType::Player4)},
|
||||
player_5{std::make_unique<EmulatedController>(NpadIdType::Player5)},
|
||||
player_6{std::make_unique<EmulatedController>(NpadIdType::Player6)},
|
||||
player_7{std::make_unique<EmulatedController>(NpadIdType::Player7)},
|
||||
player_8{std::make_unique<EmulatedController>(NpadIdType::Player8)},
|
||||
other{std::make_unique<EmulatedController>(NpadIdType::Other)},
|
||||
handheld{std::make_unique<EmulatedController>(NpadIdType::Handheld)},
|
||||
console{std::make_unique<EmulatedConsole>()}, devices{std::make_unique<EmulatedDevices>()} {}
|
||||
|
||||
HIDCore::~HIDCore() = default;
|
||||
|
||||
EmulatedController* HIDCore::GetEmulatedController(NpadIdType npad_id_type) {
|
||||
switch (npad_id_type) {
|
||||
case NpadIdType::Player1:
|
||||
return player_1.get();
|
||||
case NpadIdType::Player2:
|
||||
return player_2.get();
|
||||
case NpadIdType::Player3:
|
||||
return player_3.get();
|
||||
case NpadIdType::Player4:
|
||||
return player_4.get();
|
||||
case NpadIdType::Player5:
|
||||
return player_5.get();
|
||||
case NpadIdType::Player6:
|
||||
return player_6.get();
|
||||
case NpadIdType::Player7:
|
||||
return player_7.get();
|
||||
case NpadIdType::Player8:
|
||||
return player_8.get();
|
||||
case NpadIdType::Other:
|
||||
return other.get();
|
||||
case NpadIdType::Handheld:
|
||||
return handheld.get();
|
||||
case NpadIdType::Invalid:
|
||||
default:
|
||||
UNREACHABLE_MSG("Invalid NpadIdType={}", npad_id_type);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
const EmulatedController* HIDCore::GetEmulatedController(NpadIdType npad_id_type) const {
|
||||
switch (npad_id_type) {
|
||||
case NpadIdType::Player1:
|
||||
return player_1.get();
|
||||
case NpadIdType::Player2:
|
||||
return player_2.get();
|
||||
case NpadIdType::Player3:
|
||||
return player_3.get();
|
||||
case NpadIdType::Player4:
|
||||
return player_4.get();
|
||||
case NpadIdType::Player5:
|
||||
return player_5.get();
|
||||
case NpadIdType::Player6:
|
||||
return player_6.get();
|
||||
case NpadIdType::Player7:
|
||||
return player_7.get();
|
||||
case NpadIdType::Player8:
|
||||
return player_8.get();
|
||||
case NpadIdType::Other:
|
||||
return other.get();
|
||||
case NpadIdType::Handheld:
|
||||
return handheld.get();
|
||||
case NpadIdType::Invalid:
|
||||
default:
|
||||
UNREACHABLE_MSG("Invalid NpadIdType={}", npad_id_type);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
EmulatedConsole* HIDCore::GetEmulatedConsole() {
|
||||
return console.get();
|
||||
}
|
||||
|
||||
const EmulatedConsole* HIDCore::GetEmulatedConsole() const {
|
||||
return console.get();
|
||||
}
|
||||
|
||||
EmulatedDevices* HIDCore::GetEmulatedDevices() {
|
||||
return devices.get();
|
||||
}
|
||||
|
||||
const EmulatedDevices* HIDCore::GetEmulatedDevices() const {
|
||||
return devices.get();
|
||||
}
|
||||
|
||||
EmulatedController* HIDCore::GetEmulatedControllerByIndex(std::size_t index) {
|
||||
return GetEmulatedController(IndexToNpadIdType(index));
|
||||
}
|
||||
|
||||
const EmulatedController* HIDCore::GetEmulatedControllerByIndex(std::size_t index) const {
|
||||
return GetEmulatedController(IndexToNpadIdType(index));
|
||||
}
|
||||
|
||||
void HIDCore::SetSupportedStyleTag(NpadStyleTag style_tag) {
|
||||
supported_style_tag.raw = style_tag.raw;
|
||||
player_1->SetSupportedNpadStyleTag(supported_style_tag);
|
||||
player_2->SetSupportedNpadStyleTag(supported_style_tag);
|
||||
player_3->SetSupportedNpadStyleTag(supported_style_tag);
|
||||
player_4->SetSupportedNpadStyleTag(supported_style_tag);
|
||||
player_5->SetSupportedNpadStyleTag(supported_style_tag);
|
||||
player_6->SetSupportedNpadStyleTag(supported_style_tag);
|
||||
player_7->SetSupportedNpadStyleTag(supported_style_tag);
|
||||
player_8->SetSupportedNpadStyleTag(supported_style_tag);
|
||||
other->SetSupportedNpadStyleTag(supported_style_tag);
|
||||
handheld->SetSupportedNpadStyleTag(supported_style_tag);
|
||||
}
|
||||
|
||||
NpadStyleTag HIDCore::GetSupportedStyleTag() const {
|
||||
return supported_style_tag;
|
||||
}
|
||||
|
||||
s8 HIDCore::GetPlayerCount() const {
|
||||
s8 active_players = 0;
|
||||
for (std::size_t player_index = 0; player_index < available_controllers - 2; ++player_index) {
|
||||
const auto* const controller = GetEmulatedControllerByIndex(player_index);
|
||||
if (controller->IsConnected()) {
|
||||
active_players++;
|
||||
}
|
||||
}
|
||||
return active_players;
|
||||
}
|
||||
|
||||
NpadIdType HIDCore::GetFirstNpadId() const {
|
||||
for (std::size_t player_index = 0; player_index < available_controllers; ++player_index) {
|
||||
const auto* const controller = GetEmulatedControllerByIndex(player_index);
|
||||
if (controller->IsConnected()) {
|
||||
return controller->GetNpadIdType();
|
||||
}
|
||||
}
|
||||
return NpadIdType::Player1;
|
||||
}
|
||||
|
||||
NpadIdType HIDCore::GetFirstDisconnectedNpadId() const {
|
||||
for (std::size_t player_index = 0; player_index < available_controllers; ++player_index) {
|
||||
const auto* const controller = GetEmulatedControllerByIndex(player_index);
|
||||
if (!controller->IsConnected()) {
|
||||
return controller->GetNpadIdType();
|
||||
}
|
||||
}
|
||||
return NpadIdType::Player1;
|
||||
}
|
||||
|
||||
void HIDCore::EnableAllControllerConfiguration() {
|
||||
player_1->EnableConfiguration();
|
||||
player_2->EnableConfiguration();
|
||||
player_3->EnableConfiguration();
|
||||
player_4->EnableConfiguration();
|
||||
player_5->EnableConfiguration();
|
||||
player_6->EnableConfiguration();
|
||||
player_7->EnableConfiguration();
|
||||
player_8->EnableConfiguration();
|
||||
other->EnableConfiguration();
|
||||
handheld->EnableConfiguration();
|
||||
}
|
||||
|
||||
void HIDCore::DisableAllControllerConfiguration() {
|
||||
player_1->DisableConfiguration();
|
||||
player_2->DisableConfiguration();
|
||||
player_3->DisableConfiguration();
|
||||
player_4->DisableConfiguration();
|
||||
player_5->DisableConfiguration();
|
||||
player_6->DisableConfiguration();
|
||||
player_7->DisableConfiguration();
|
||||
player_8->DisableConfiguration();
|
||||
other->DisableConfiguration();
|
||||
handheld->DisableConfiguration();
|
||||
}
|
||||
|
||||
void HIDCore::ReloadInputDevices() {
|
||||
player_1->ReloadFromSettings();
|
||||
player_2->ReloadFromSettings();
|
||||
player_3->ReloadFromSettings();
|
||||
player_4->ReloadFromSettings();
|
||||
player_5->ReloadFromSettings();
|
||||
player_6->ReloadFromSettings();
|
||||
player_7->ReloadFromSettings();
|
||||
player_8->ReloadFromSettings();
|
||||
other->ReloadFromSettings();
|
||||
handheld->ReloadFromSettings();
|
||||
console->ReloadFromSettings();
|
||||
devices->ReloadFromSettings();
|
||||
}
|
||||
|
||||
void HIDCore::UnloadInputDevices() {
|
||||
player_1->UnloadInput();
|
||||
player_2->UnloadInput();
|
||||
player_3->UnloadInput();
|
||||
player_4->UnloadInput();
|
||||
player_5->UnloadInput();
|
||||
player_6->UnloadInput();
|
||||
player_7->UnloadInput();
|
||||
player_8->UnloadInput();
|
||||
other->UnloadInput();
|
||||
handheld->UnloadInput();
|
||||
console->UnloadInput();
|
||||
devices->UnloadInput();
|
||||
}
|
||||
|
||||
} // namespace Core::HID
|
@ -0,0 +1,82 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "core/hid/hid_types.h"
|
||||
|
||||
namespace Core::HID {
|
||||
class EmulatedConsole;
|
||||
class EmulatedController;
|
||||
class EmulatedDevices;
|
||||
} // namespace Core::HID
|
||||
|
||||
namespace Core::HID {
|
||||
|
||||
class HIDCore {
|
||||
public:
|
||||
explicit HIDCore();
|
||||
~HIDCore();
|
||||
|
||||
YUZU_NON_COPYABLE(HIDCore);
|
||||
YUZU_NON_MOVEABLE(HIDCore);
|
||||
|
||||
EmulatedController* GetEmulatedController(NpadIdType npad_id_type);
|
||||
const EmulatedController* GetEmulatedController(NpadIdType npad_id_type) const;
|
||||
|
||||
EmulatedController* GetEmulatedControllerByIndex(std::size_t index);
|
||||
const EmulatedController* GetEmulatedControllerByIndex(std::size_t index) const;
|
||||
|
||||
EmulatedConsole* GetEmulatedConsole();
|
||||
const EmulatedConsole* GetEmulatedConsole() const;
|
||||
|
||||
EmulatedDevices* GetEmulatedDevices();
|
||||
const EmulatedDevices* GetEmulatedDevices() const;
|
||||
|
||||
void SetSupportedStyleTag(NpadStyleTag style_tag);
|
||||
NpadStyleTag GetSupportedStyleTag() const;
|
||||
|
||||
/// Counts the connected players from P1-P8
|
||||
s8 GetPlayerCount() const;
|
||||
|
||||
/// Returns the first connected npad id
|
||||
NpadIdType GetFirstNpadId() const;
|
||||
|
||||
/// Returns the first disconnected npad id
|
||||
NpadIdType GetFirstDisconnectedNpadId() const;
|
||||
|
||||
/// Sets all emulated controllers into configuring mode.
|
||||
void EnableAllControllerConfiguration();
|
||||
|
||||
/// Sets all emulated controllers into normal mode.
|
||||
void DisableAllControllerConfiguration();
|
||||
|
||||
/// Reloads all input devices from settings
|
||||
void ReloadInputDevices();
|
||||
|
||||
/// Removes all callbacks from input common
|
||||
void UnloadInputDevices();
|
||||
|
||||
/// Number of emulated controllers
|
||||
static constexpr std::size_t available_controllers{10};
|
||||
|
||||
private:
|
||||
std::unique_ptr<EmulatedController> player_1;
|
||||
std::unique_ptr<EmulatedController> player_2;
|
||||
std::unique_ptr<EmulatedController> player_3;
|
||||
std::unique_ptr<EmulatedController> player_4;
|
||||
std::unique_ptr<EmulatedController> player_5;
|
||||
std::unique_ptr<EmulatedController> player_6;
|
||||
std::unique_ptr<EmulatedController> player_7;
|
||||
std::unique_ptr<EmulatedController> player_8;
|
||||
std::unique_ptr<EmulatedController> other;
|
||||
std::unique_ptr<EmulatedController> handheld;
|
||||
std::unique_ptr<EmulatedConsole> console;
|
||||
std::unique_ptr<EmulatedDevices> devices;
|
||||
NpadStyleTag supported_style_tag{NpadStyleSet::All};
|
||||
};
|
||||
|
||||
} // namespace Core::HID
|
@ -0,0 +1,635 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common/bit_field.h"
|
||||
#include "common/common_funcs.h"
|
||||
#include "common/common_types.h"
|
||||
#include "common/point.h"
|
||||
#include "common/uuid.h"
|
||||
|
||||
namespace Core::HID {
|
||||
|
||||
enum class DeviceIndex : u8 {
|
||||
Left = 0,
|
||||
Right = 1,
|
||||
None = 2,
|
||||
MaxDeviceIndex = 3,
|
||||
};
|
||||
|
||||
// This is nn::hid::NpadButton
|
||||
enum class NpadButton : u64 {
|
||||
None = 0,
|
||||
A = 1U << 0,
|
||||
B = 1U << 1,
|
||||
X = 1U << 2,
|
||||
Y = 1U << 3,
|
||||
StickL = 1U << 4,
|
||||
StickR = 1U << 5,
|
||||
L = 1U << 6,
|
||||
R = 1U << 7,
|
||||
ZL = 1U << 8,
|
||||
ZR = 1U << 9,
|
||||
Plus = 1U << 10,
|
||||
Minus = 1U << 11,
|
||||
|
||||
Left = 1U << 12,
|
||||
Up = 1U << 13,
|
||||
Right = 1U << 14,
|
||||
Down = 1U << 15,
|
||||
|
||||
StickLLeft = 1U << 16,
|
||||
StickLUp = 1U << 17,
|
||||
StickLRight = 1U << 18,
|
||||
StickLDown = 1U << 19,
|
||||
|
||||
StickRLeft = 1U << 20,
|
||||
StickRUp = 1U << 21,
|
||||
StickRRight = 1U << 22,
|
||||
StickRDown = 1U << 23,
|
||||
|
||||
LeftSL = 1U << 24,
|
||||
LeftSR = 1U << 25,
|
||||
|
||||
RightSL = 1U << 26,
|
||||
RightSR = 1U << 27,
|
||||
|
||||
Palma = 1U << 28,
|
||||
Verification = 1U << 29,
|
||||
HandheldLeftB = 1U << 30,
|
||||
LagonCLeft = 1U << 31,
|
||||
LagonCUp = 1ULL << 32,
|
||||
LagonCRight = 1ULL << 33,
|
||||
LagonCDown = 1ULL << 34,
|
||||
|
||||
All = 0xFFFFFFFFFFFFFFFFULL,
|
||||
};
|
||||
DECLARE_ENUM_FLAG_OPERATORS(NpadButton);
|
||||
|
||||
enum class KeyboardKeyIndex : u32 {
|
||||
A = 4,
|
||||
B = 5,
|
||||
C = 6,
|
||||
D = 7,
|
||||
E = 8,
|
||||
F = 9,
|
||||
G = 10,
|
||||
H = 11,
|
||||
I = 12,
|
||||
J = 13,
|
||||
K = 14,
|
||||
L = 15,
|
||||
M = 16,
|
||||
N = 17,
|
||||
O = 18,
|
||||
P = 19,
|
||||
Q = 20,
|
||||
R = 21,
|
||||
S = 22,
|
||||
T = 23,
|
||||
U = 24,
|
||||
V = 25,
|
||||
W = 26,
|
||||
X = 27,
|
||||
Y = 28,
|
||||
Z = 29,
|
||||
D1 = 30,
|
||||
D2 = 31,
|
||||
D3 = 32,
|
||||
D4 = 33,
|
||||
D5 = 34,
|
||||
D6 = 35,
|
||||
D7 = 36,
|
||||
D8 = 37,
|
||||
D9 = 38,
|
||||
D0 = 39,
|
||||
Return = 40,
|
||||
Escape = 41,
|
||||
Backspace = 42,
|
||||
Tab = 43,
|
||||
Space = 44,
|
||||
Minus = 45,
|
||||
Plus = 46,
|
||||
OpenBracket = 47,
|
||||
CloseBracket = 48,
|
||||
Pipe = 49,
|
||||
Tilde = 50,
|
||||
Semicolon = 51,
|
||||
Quote = 52,
|
||||
Backquote = 53,
|
||||
Comma = 54,
|
||||
Period = 55,
|
||||
Slash = 56,
|
||||
CapsLock = 57,
|
||||
F1 = 58,
|
||||
F2 = 59,
|
||||
F3 = 60,
|
||||
F4 = 61,
|
||||
F5 = 62,
|
||||
F6 = 63,
|
||||
F7 = 64,
|
||||
F8 = 65,
|
||||
F9 = 66,
|
||||
F10 = 67,
|
||||
F11 = 68,
|
||||
F12 = 69,
|
||||
PrintScreen = 70,
|
||||
ScrollLock = 71,
|
||||
Pause = 72,
|
||||
Insert = 73,
|
||||
Home = 74,
|
||||
PageUp = 75,
|
||||
Delete = 76,
|
||||
End = 77,
|
||||
PageDown = 78,
|
||||
RightArrow = 79,
|
||||
LeftArrow = 80,
|
||||
DownArrow = 81,
|
||||
UpArrow = 82,
|
||||
NumLock = 83,
|
||||
NumPadDivide = 84,
|
||||
NumPadMultiply = 85,
|
||||
NumPadSubtract = 86,
|
||||
NumPadAdd = 87,
|
||||
NumPadEnter = 88,
|
||||
NumPad1 = 89,
|
||||
NumPad2 = 90,
|
||||
NumPad3 = 91,
|
||||
NumPad4 = 92,
|
||||
NumPad5 = 93,
|
||||
NumPad6 = 94,
|
||||
NumPad7 = 95,
|
||||
NumPad8 = 96,
|
||||
NumPad9 = 97,
|
||||
NumPad0 = 98,
|
||||
NumPadDot = 99,
|
||||
Backslash = 100,
|
||||
Application = 101,
|
||||
Power = 102,
|
||||
NumPadEquals = 103,
|
||||
F13 = 104,
|
||||
F14 = 105,
|
||||
F15 = 106,
|
||||
F16 = 107,
|
||||
F17 = 108,
|
||||
F18 = 109,
|
||||
F19 = 110,
|
||||
F20 = 111,
|
||||
F21 = 112,
|
||||
F22 = 113,
|
||||
F23 = 114,
|
||||
F24 = 115,
|
||||
NumPadComma = 133,
|
||||
Ro = 135,
|
||||
KatakanaHiragana = 136,
|
||||
Yen = 137,
|
||||
Henkan = 138,
|
||||
Muhenkan = 139,
|
||||
NumPadCommaPc98 = 140,
|
||||
HangulEnglish = 144,
|
||||
Hanja = 145,
|
||||
Katakana = 146,
|
||||
Hiragana = 147,
|
||||
ZenkakuHankaku = 148,
|
||||
LeftControl = 224,
|
||||
LeftShift = 225,
|
||||
LeftAlt = 226,
|
||||
LeftGui = 227,
|
||||
RightControl = 228,
|
||||
RightShift = 229,
|
||||
RightAlt = 230,
|
||||
RightGui = 231,
|
||||
};
|
||||
|
||||
// This is nn::hid::NpadIdType
|
||||
enum class NpadIdType : u32 {
|
||||
Player1 = 0x0,
|
||||
Player2 = 0x1,
|
||||
Player3 = 0x2,
|
||||
Player4 = 0x3,
|
||||
Player5 = 0x4,
|
||||
Player6 = 0x5,
|
||||
Player7 = 0x6,
|
||||
Player8 = 0x7,
|
||||
Other = 0x10,
|
||||
Handheld = 0x20,
|
||||
|
||||
Invalid = 0xFFFFFFFF,
|
||||
};
|
||||
|
||||
// This is nn::hid::NpadStyleIndex
|
||||
enum class NpadStyleIndex : u8 {
|
||||
None = 0,
|
||||
ProController = 3,
|
||||
Handheld = 4,
|
||||
HandheldNES = 4,
|
||||
JoyconDual = 5,
|
||||
JoyconLeft = 6,
|
||||
JoyconRight = 7,
|
||||
GameCube = 8,
|
||||
Pokeball = 9,
|
||||
NES = 10,
|
||||
SNES = 12,
|
||||
N64 = 13,
|
||||
SegaGenesis = 14,
|
||||
SystemExt = 32,
|
||||
System = 33,
|
||||
MaxNpadType = 34,
|
||||
};
|
||||
|
||||
// This is nn::hid::NpadStyleSet
|
||||
enum class NpadStyleSet : u32 {
|
||||
None = 0,
|
||||
Fullkey = 1U << 0,
|
||||
Handheld = 1U << 1,
|
||||
JoyDual = 1U << 2,
|
||||
JoyLeft = 1U << 3,
|
||||
JoyRight = 1U << 4,
|
||||
Gc = 1U << 5,
|
||||
Palma = 1U << 6,
|
||||
Lark = 1U << 7,
|
||||
HandheldLark = 1U << 8,
|
||||
Lucia = 1U << 9,
|
||||
Lagoon = 1U << 10,
|
||||
Lager = 1U << 11,
|
||||
SystemExt = 1U << 29,
|
||||
System = 1U << 30,
|
||||
|
||||
All = 0xFFFFFFFFU,
|
||||
};
|
||||
static_assert(sizeof(NpadStyleSet) == 4, "NpadStyleSet is an invalid size");
|
||||
|
||||
// This is nn::hid::VibrationDevicePosition
|
||||
enum class VibrationDevicePosition : u32 {
|
||||
None = 0,
|
||||
Left = 1,
|
||||
Right = 2,
|
||||
};
|
||||
|
||||
// This is nn::hid::VibrationDeviceType
|
||||
enum class VibrationDeviceType : u32 {
|
||||
Unknown = 0,
|
||||
LinearResonantActuator = 1,
|
||||
GcErm = 2,
|
||||
};
|
||||
|
||||
// This is nn::hid::VibrationGcErmCommand
|
||||
enum class VibrationGcErmCommand : u64 {
|
||||
Stop = 0,
|
||||
Start = 1,
|
||||
StopHard = 2,
|
||||
};
|
||||
|
||||
// This is nn::hid::NpadStyleTag
|
||||
struct NpadStyleTag {
|
||||
union {
|
||||
NpadStyleSet raw{};
|
||||
|
||||
BitField<0, 1, u32> fullkey;
|
||||
BitField<1, 1, u32> handheld;
|
||||
BitField<2, 1, u32> joycon_dual;
|
||||
BitField<3, 1, u32> joycon_left;
|
||||
BitField<4, 1, u32> joycon_right;
|
||||
BitField<5, 1, u32> gamecube;
|
||||
BitField<6, 1, u32> palma;
|
||||
BitField<7, 1, u32> lark;
|
||||
BitField<8, 1, u32> handheld_lark;
|
||||
BitField<9, 1, u32> lucia;
|
||||
BitField<10, 1, u32> lagoon;
|
||||
BitField<11, 1, u32> lager;
|
||||
BitField<29, 1, u32> system_ext;
|
||||
BitField<30, 1, u32> system;
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(NpadStyleTag) == 4, "NpadStyleTag is an invalid size");
|
||||
|
||||
// This is nn::hid::TouchAttribute
|
||||
struct TouchAttribute {
|
||||
union {
|
||||
u32 raw{};
|
||||
BitField<0, 1, u32> start_touch;
|
||||
BitField<1, 1, u32> end_touch;
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(TouchAttribute) == 0x4, "TouchAttribute is an invalid size");
|
||||
|
||||
// This is nn::hid::TouchState
|
||||
struct TouchState {
|
||||
u64 delta_time;
|
||||
TouchAttribute attribute;
|
||||
u32 finger;
|
||||
Common::Point<u32> position;
|
||||
u32 diameter_x;
|
||||
u32 diameter_y;
|
||||
u32 rotation_angle;
|
||||
};
|
||||
static_assert(sizeof(TouchState) == 0x28, "Touchstate is an invalid size");
|
||||
|
||||
// This is nn::hid::NpadControllerColor
|
||||
struct NpadControllerColor {
|
||||
u32 body;
|
||||
u32 button;
|
||||
};
|
||||
static_assert(sizeof(NpadControllerColor) == 8, "NpadControllerColor is an invalid size");
|
||||
|
||||
// This is nn::hid::AnalogStickState
|
||||
struct AnalogStickState {
|
||||
s32 x;
|
||||
s32 y;
|
||||
};
|
||||
static_assert(sizeof(AnalogStickState) == 8, "AnalogStickState is an invalid size");
|
||||
|
||||
// This is nn::hid::server::NpadGcTriggerState
|
||||
struct NpadGcTriggerState {
|
||||
s64 sampling_number{};
|
||||
s32 left{};
|
||||
s32 right{};
|
||||
};
|
||||
static_assert(sizeof(NpadGcTriggerState) == 0x10, "NpadGcTriggerState is an invalid size");
|
||||
|
||||
// This is nn::hid::system::NpadBatteryLevel
|
||||
using NpadBatteryLevel = u32;
|
||||
static_assert(sizeof(NpadBatteryLevel) == 0x4, "NpadBatteryLevel is an invalid size");
|
||||
|
||||
// This is nn::hid::system::NpadPowerInfo
|
||||
struct NpadPowerInfo {
|
||||
bool is_powered;
|
||||
bool is_charging;
|
||||
INSERT_PADDING_BYTES(0x6);
|
||||
NpadBatteryLevel battery_level;
|
||||
};
|
||||
static_assert(sizeof(NpadPowerInfo) == 0xC, "NpadPowerInfo is an invalid size");
|
||||
|
||||
struct LedPattern {
|
||||
explicit LedPattern(u64 light1, u64 light2, u64 light3, u64 light4) {
|
||||
position1.Assign(light1);
|
||||
position2.Assign(light2);
|
||||
position3.Assign(light3);
|
||||
position4.Assign(light4);
|
||||
}
|
||||
union {
|
||||
u64 raw{};
|
||||
BitField<0, 1, u64> position1;
|
||||
BitField<1, 1, u64> position2;
|
||||
BitField<2, 1, u64> position3;
|
||||
BitField<3, 1, u64> position4;
|
||||
};
|
||||
};
|
||||
|
||||
struct NpadButtonState {
|
||||
union {
|
||||
NpadButton raw{};
|
||||
|
||||
// Buttons
|
||||
BitField<0, 1, u64> a;
|
||||
BitField<1, 1, u64> b;
|
||||
BitField<2, 1, u64> x;
|
||||
BitField<3, 1, u64> y;
|
||||
BitField<4, 1, u64> stick_l;
|
||||
BitField<5, 1, u64> stick_r;
|
||||
BitField<6, 1, u64> l;
|
||||
BitField<7, 1, u64> r;
|
||||
BitField<8, 1, u64> zl;
|
||||
BitField<9, 1, u64> zr;
|
||||
BitField<10, 1, u64> plus;
|
||||
BitField<11, 1, u64> minus;
|
||||
|
||||
// D-Pad
|
||||
BitField<12, 1, u64> left;
|
||||
BitField<13, 1, u64> up;
|
||||
BitField<14, 1, u64> right;
|
||||
BitField<15, 1, u64> down;
|
||||
|
||||
// Left JoyStick
|
||||
BitField<16, 1, u64> stick_l_left;
|
||||
BitField<17, 1, u64> stick_l_up;
|
||||
BitField<18, 1, u64> stick_l_right;
|
||||
BitField<19, 1, u64> stick_l_down;
|
||||
|
||||
// Right JoyStick
|
||||
BitField<20, 1, u64> stick_r_left;
|
||||
BitField<21, 1, u64> stick_r_up;
|
||||
BitField<22, 1, u64> stick_r_right;
|
||||
BitField<23, 1, u64> stick_r_down;
|
||||
|
||||
BitField<24, 1, u64> left_sl;
|
||||
BitField<25, 1, u64> left_sr;
|
||||
|
||||
BitField<26, 1, u64> right_sl;
|
||||
BitField<27, 1, u64> right_sr;
|
||||
|
||||
BitField<28, 1, u64> palma;
|
||||
BitField<29, 1, u64> verification;
|
||||
BitField<30, 1, u64> handheld_left_b;
|
||||
BitField<31, 1, u64> lagon_c_left;
|
||||
BitField<32, 1, u64> lagon_c_up;
|
||||
BitField<33, 1, u64> lagon_c_right;
|
||||
BitField<34, 1, u64> lagon_c_down;
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(NpadButtonState) == 0x8, "NpadButtonState has incorrect size.");
|
||||
|
||||
// This is nn::hid::DebugPadButton
|
||||
struct DebugPadButton {
|
||||
union {
|
||||
u32 raw{};
|
||||
BitField<0, 1, u32> a;
|
||||
BitField<1, 1, u32> b;
|
||||
BitField<2, 1, u32> x;
|
||||
BitField<3, 1, u32> y;
|
||||
BitField<4, 1, u32> l;
|
||||
BitField<5, 1, u32> r;
|
||||
BitField<6, 1, u32> zl;
|
||||
BitField<7, 1, u32> zr;
|
||||
BitField<8, 1, u32> plus;
|
||||
BitField<9, 1, u32> minus;
|
||||
BitField<10, 1, u32> d_left;
|
||||
BitField<11, 1, u32> d_up;
|
||||
BitField<12, 1, u32> d_right;
|
||||
BitField<13, 1, u32> d_down;
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(DebugPadButton) == 0x4, "DebugPadButton is an invalid size");
|
||||
|
||||
// This is nn::hid::ConsoleSixAxisSensorHandle
|
||||
struct ConsoleSixAxisSensorHandle {
|
||||
u8 unknown_1;
|
||||
u8 unknown_2;
|
||||
INSERT_PADDING_BYTES_NOINIT(2);
|
||||
};
|
||||
static_assert(sizeof(ConsoleSixAxisSensorHandle) == 4,
|
||||
"ConsoleSixAxisSensorHandle is an invalid size");
|
||||
|
||||
// This is nn::hid::SixAxisSensorHandle
|
||||
struct SixAxisSensorHandle {
|
||||
NpadStyleIndex npad_type;
|
||||
u8 npad_id;
|
||||
DeviceIndex device_index;
|
||||
INSERT_PADDING_BYTES_NOINIT(1);
|
||||
};
|
||||
static_assert(sizeof(SixAxisSensorHandle) == 4, "SixAxisSensorHandle is an invalid size");
|
||||
|
||||
struct SixAxisSensorFusionParameters {
|
||||
f32 parameter1;
|
||||
f32 parameter2;
|
||||
};
|
||||
static_assert(sizeof(SixAxisSensorFusionParameters) == 8,
|
||||
"SixAxisSensorFusionParameters is an invalid size");
|
||||
|
||||
// This is nn::hid::VibrationDeviceHandle
|
||||
struct VibrationDeviceHandle {
|
||||
NpadStyleIndex npad_type;
|
||||
u8 npad_id;
|
||||
DeviceIndex device_index;
|
||||
INSERT_PADDING_BYTES_NOINIT(1);
|
||||
};
|
||||
static_assert(sizeof(VibrationDeviceHandle) == 4, "SixAxisSensorHandle is an invalid size");
|
||||
|
||||
// This is nn::hid::VibrationValue
|
||||
struct VibrationValue {
|
||||
f32 low_amplitude;
|
||||
f32 low_frequency;
|
||||
f32 high_amplitude;
|
||||
f32 high_frequency;
|
||||
};
|
||||
static_assert(sizeof(VibrationValue) == 0x10, "VibrationValue has incorrect size.");
|
||||
|
||||
// This is nn::hid::VibrationDeviceInfo
|
||||
struct VibrationDeviceInfo {
|
||||
VibrationDeviceType type{};
|
||||
VibrationDevicePosition position{};
|
||||
};
|
||||
static_assert(sizeof(VibrationDeviceInfo) == 0x8, "VibrationDeviceInfo has incorrect size.");
|
||||
|
||||
// This is nn::hid::KeyboardModifier
|
||||
struct KeyboardModifier {
|
||||
union {
|
||||
u32 raw{};
|
||||
BitField<0, 1, u32> control;
|
||||
BitField<1, 1, u32> shift;
|
||||
BitField<2, 1, u32> left_alt;
|
||||
BitField<3, 1, u32> right_alt;
|
||||
BitField<4, 1, u32> gui;
|
||||
BitField<8, 1, u32> caps_lock;
|
||||
BitField<9, 1, u32> scroll_lock;
|
||||
BitField<10, 1, u32> num_lock;
|
||||
BitField<11, 1, u32> katakana;
|
||||
BitField<12, 1, u32> hiragana;
|
||||
};
|
||||
};
|
||||
|
||||
static_assert(sizeof(KeyboardModifier) == 0x4, "KeyboardModifier is an invalid size");
|
||||
|
||||
// This is nn::hid::KeyboardAttribute
|
||||
struct KeyboardAttribute {
|
||||
union {
|
||||
u32 raw{};
|
||||
BitField<0, 1, u32> is_connected;
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(KeyboardAttribute) == 0x4, "KeyboardAttribute is an invalid size");
|
||||
|
||||
// This is nn::hid::KeyboardKey
|
||||
struct KeyboardKey {
|
||||
// This should be a 256 bit flag
|
||||
std::array<u8, 32> key;
|
||||
};
|
||||
static_assert(sizeof(KeyboardKey) == 0x20, "KeyboardKey is an invalid size");
|
||||
|
||||
// This is nn::hid::MouseButton
|
||||
struct MouseButton {
|
||||
union {
|
||||
u32_le raw{};
|
||||
BitField<0, 1, u32> left;
|
||||
BitField<1, 1, u32> right;
|
||||
BitField<2, 1, u32> middle;
|
||||
BitField<3, 1, u32> forward;
|
||||
BitField<4, 1, u32> back;
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(MouseButton) == 0x4, "MouseButton is an invalid size");
|
||||
|
||||
// This is nn::hid::MouseAttribute
|
||||
struct MouseAttribute {
|
||||
union {
|
||||
u32 raw{};
|
||||
BitField<0, 1, u32> transferable;
|
||||
BitField<1, 1, u32> is_connected;
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(MouseAttribute) == 0x4, "MouseAttribute is an invalid size");
|
||||
|
||||
// This is nn::hid::detail::MouseState
|
||||
struct MouseState {
|
||||
s64 sampling_number;
|
||||
s32 x;
|
||||
s32 y;
|
||||
s32 delta_x;
|
||||
s32 delta_y;
|
||||
// Axis Order in HW is switched for the wheel
|
||||
s32 delta_wheel_y;
|
||||
s32 delta_wheel_x;
|
||||
MouseButton button;
|
||||
MouseAttribute attribute;
|
||||
};
|
||||
static_assert(sizeof(MouseState) == 0x28, "MouseState is an invalid size");
|
||||
|
||||
/// Converts a NpadIdType to an array index.
|
||||
constexpr size_t NpadIdTypeToIndex(NpadIdType npad_id_type) {
|
||||
switch (npad_id_type) {
|
||||
case NpadIdType::Player1:
|
||||
return 0;
|
||||
case NpadIdType::Player2:
|
||||
return 1;
|
||||
case NpadIdType::Player3:
|
||||
return 2;
|
||||
case NpadIdType::Player4:
|
||||
return 3;
|
||||
case NpadIdType::Player5:
|
||||
return 4;
|
||||
case NpadIdType::Player6:
|
||||
return 5;
|
||||
case NpadIdType::Player7:
|
||||
return 6;
|
||||
case NpadIdType::Player8:
|
||||
return 7;
|
||||
case NpadIdType::Handheld:
|
||||
return 8;
|
||||
case NpadIdType::Other:
|
||||
return 9;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts an array index to a NpadIdType
|
||||
constexpr NpadIdType IndexToNpadIdType(size_t index) {
|
||||
switch (index) {
|
||||
case 0:
|
||||
return NpadIdType::Player1;
|
||||
case 1:
|
||||
return NpadIdType::Player2;
|
||||
case 2:
|
||||
return NpadIdType::Player3;
|
||||
case 3:
|
||||
return NpadIdType::Player4;
|
||||
case 4:
|
||||
return NpadIdType::Player5;
|
||||
case 5:
|
||||
return NpadIdType::Player6;
|
||||
case 6:
|
||||
return NpadIdType::Player7;
|
||||
case 7:
|
||||
return NpadIdType::Player8;
|
||||
case 8:
|
||||
return NpadIdType::Handheld;
|
||||
case 9:
|
||||
return NpadIdType::Other;
|
||||
default:
|
||||
return NpadIdType::Invalid;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Core::HID
|
@ -0,0 +1,383 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included
|
||||
|
||||
#include <random>
|
||||
|
||||
#include "common/input.h"
|
||||
#include "core/hid/input_converter.h"
|
||||
|
||||
namespace Core::HID {
|
||||
|
||||
Common::Input::BatteryStatus TransformToBattery(const Common::Input::CallbackStatus& callback) {
|
||||
Common::Input::BatteryStatus battery{Common::Input::BatteryStatus::None};
|
||||
switch (callback.type) {
|
||||
case Common::Input::InputType::Analog:
|
||||
case Common::Input::InputType::Trigger: {
|
||||
const auto value = TransformToTrigger(callback).analog.value;
|
||||
battery = Common::Input::BatteryLevel::Empty;
|
||||
if (value > 0.2f) {
|
||||
battery = Common::Input::BatteryLevel::Critical;
|
||||
}
|
||||
if (value > 0.4f) {
|
||||
battery = Common::Input::BatteryLevel::Low;
|
||||
}
|
||||
if (value > 0.6f) {
|
||||
battery = Common::Input::BatteryLevel::Medium;
|
||||
}
|
||||
if (value > 0.8f) {
|
||||
battery = Common::Input::BatteryLevel::Full;
|
||||
}
|
||||
if (value >= 1.0f) {
|
||||
battery = Common::Input::BatteryLevel::Charging;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Common::Input::InputType::Button:
|
||||
battery = callback.button_status.value ? Common::Input::BatteryLevel::Charging
|
||||
: Common::Input::BatteryLevel::Critical;
|
||||
break;
|
||||
case Common::Input::InputType::Battery:
|
||||
battery = callback.battery_status;
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR(Input, "Conversion from type {} to battery not implemented", callback.type);
|
||||
break;
|
||||
}
|
||||
|
||||
return battery;
|
||||
}
|
||||
|
||||
Common::Input::ButtonStatus TransformToButton(const Common::Input::CallbackStatus& callback) {
|
||||
Common::Input::ButtonStatus status{};
|
||||
switch (callback.type) {
|
||||
case Common::Input::InputType::Analog:
|
||||
case Common::Input::InputType::Trigger:
|
||||
status.value = TransformToTrigger(callback).pressed.value;
|
||||
break;
|
||||
case Common::Input::InputType::Button:
|
||||
status = callback.button_status;
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR(Input, "Conversion from type {} to button not implemented", callback.type);
|
||||
break;
|
||||
}
|
||||
|
||||
if (status.inverted) {
|
||||
status.value = !status.value;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
Common::Input::MotionStatus TransformToMotion(const Common::Input::CallbackStatus& callback) {
|
||||
Common::Input::MotionStatus status{};
|
||||
switch (callback.type) {
|
||||
case Common::Input::InputType::Button: {
|
||||
Common::Input::AnalogProperties properties{
|
||||
.deadzone = 0.0f,
|
||||
.range = 1.0f,
|
||||
.offset = 0.0f,
|
||||
};
|
||||
status.delta_timestamp = 5000;
|
||||
status.force_update = true;
|
||||
status.accel.x = {
|
||||
.value = 0.0f,
|
||||
.raw_value = 0.0f,
|
||||
.properties = properties,
|
||||
};
|
||||
status.accel.y = {
|
||||
.value = 0.0f,
|
||||
.raw_value = 0.0f,
|
||||
.properties = properties,
|
||||
};
|
||||
status.accel.z = {
|
||||
.value = 0.0f,
|
||||
.raw_value = -1.0f,
|
||||
.properties = properties,
|
||||
};
|
||||
status.gyro.x = {
|
||||
.value = 0.0f,
|
||||
.raw_value = 0.0f,
|
||||
.properties = properties,
|
||||
};
|
||||
status.gyro.y = {
|
||||
.value = 0.0f,
|
||||
.raw_value = 0.0f,
|
||||
.properties = properties,
|
||||
};
|
||||
status.gyro.z = {
|
||||
.value = 0.0f,
|
||||
.raw_value = 0.0f,
|
||||
.properties = properties,
|
||||
};
|
||||
if (TransformToButton(callback).value) {
|
||||
std::random_device device;
|
||||
std::mt19937 gen(device());
|
||||
std::uniform_int_distribution<s16> distribution(-1000, 1000);
|
||||
status.accel.x.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
||||
status.accel.y.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
||||
status.accel.z.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
||||
status.gyro.x.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
||||
status.gyro.y.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
||||
status.gyro.z.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Common::Input::InputType::Motion:
|
||||
status = callback.motion_status;
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR(Input, "Conversion from type {} to motion not implemented", callback.type);
|
||||
break;
|
||||
}
|
||||
SanitizeAnalog(status.accel.x, false);
|
||||
SanitizeAnalog(status.accel.y, false);
|
||||
SanitizeAnalog(status.accel.z, false);
|
||||
SanitizeAnalog(status.gyro.x, false);
|
||||
SanitizeAnalog(status.gyro.y, false);
|
||||
SanitizeAnalog(status.gyro.z, false);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
Common::Input::StickStatus TransformToStick(const Common::Input::CallbackStatus& callback) {
|
||||
Common::Input::StickStatus status{};
|
||||
|
||||
switch (callback.type) {
|
||||
case Common::Input::InputType::Stick:
|
||||
status = callback.stick_status;
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR(Input, "Conversion from type {} to stick not implemented", callback.type);
|
||||
break;
|
||||
}
|
||||
|
||||
SanitizeStick(status.x, status.y, true);
|
||||
const auto& properties_x = status.x.properties;
|
||||
const auto& properties_y = status.y.properties;
|
||||
const float x = status.x.value;
|
||||
const float y = status.y.value;
|
||||
|
||||
// Set directional buttons
|
||||
status.right = x > properties_x.threshold;
|
||||
status.left = x < -properties_x.threshold;
|
||||
status.up = y > properties_y.threshold;
|
||||
status.down = y < -properties_y.threshold;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
Common::Input::TouchStatus TransformToTouch(const Common::Input::CallbackStatus& callback) {
|
||||
Common::Input::TouchStatus status{};
|
||||
|
||||
switch (callback.type) {
|
||||
case Common::Input::InputType::Touch:
|
||||
status = callback.touch_status;
|
||||
break;
|
||||
case Common::Input::InputType::Stick:
|
||||
status.x = callback.stick_status.x;
|
||||
status.y = callback.stick_status.y;
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR(Input, "Conversion from type {} to touch not implemented", callback.type);
|
||||
break;
|
||||
}
|
||||
|
||||
SanitizeAnalog(status.x, true);
|
||||
SanitizeAnalog(status.y, true);
|
||||
float& x = status.x.value;
|
||||
float& y = status.y.value;
|
||||
|
||||
// Adjust if value is inverted
|
||||
x = status.x.properties.inverted ? 1.0f + x : x;
|
||||
y = status.y.properties.inverted ? 1.0f + y : y;
|
||||
|
||||
// clamp value
|
||||
x = std::clamp(x, 0.0f, 1.0f);
|
||||
y = std::clamp(y, 0.0f, 1.0f);
|
||||
|
||||
if (status.pressed.inverted) {
|
||||
status.pressed.value = !status.pressed.value;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
Common::Input::TriggerStatus TransformToTrigger(const Common::Input::CallbackStatus& callback) {
|
||||
Common::Input::TriggerStatus status{};
|
||||
float& raw_value = status.analog.raw_value;
|
||||
bool calculate_button_value = true;
|
||||
|
||||
switch (callback.type) {
|
||||
case Common::Input::InputType::Analog:
|
||||
status.analog.properties = callback.analog_status.properties;
|
||||
raw_value = callback.analog_status.raw_value;
|
||||
break;
|
||||
case Common::Input::InputType::Button:
|
||||
status.analog.properties.range = 1.0f;
|
||||
status.analog.properties.inverted = callback.button_status.inverted;
|
||||
raw_value = callback.button_status.value ? 1.0f : 0.0f;
|
||||
break;
|
||||
case Common::Input::InputType::Trigger:
|
||||
status = callback.trigger_status;
|
||||
calculate_button_value = false;
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR(Input, "Conversion from type {} to trigger not implemented", callback.type);
|
||||
break;
|
||||
}
|
||||
|
||||
SanitizeAnalog(status.analog, true);
|
||||
const auto& properties = status.analog.properties;
|
||||
float& value = status.analog.value;
|
||||
|
||||
// Set button status
|
||||
if (calculate_button_value) {
|
||||
status.pressed.value = value > properties.threshold;
|
||||
}
|
||||
|
||||
// Adjust if value is inverted
|
||||
value = properties.inverted ? 1.0f + value : value;
|
||||
|
||||
// clamp value
|
||||
value = std::clamp(value, 0.0f, 1.0f);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
Common::Input::AnalogStatus TransformToAnalog(const Common::Input::CallbackStatus& callback) {
|
||||
Common::Input::AnalogStatus status{};
|
||||
|
||||
switch (callback.type) {
|
||||
case Common::Input::InputType::Analog:
|
||||
status.properties = callback.analog_status.properties;
|
||||
status.raw_value = callback.analog_status.raw_value;
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR(Input, "Conversion from type {} to analog not implemented", callback.type);
|
||||
break;
|
||||
}
|
||||
|
||||
SanitizeAnalog(status, false);
|
||||
|
||||
// Adjust if value is inverted
|
||||
status.value = status.properties.inverted ? -status.value : status.value;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
void SanitizeAnalog(Common::Input::AnalogStatus& analog, bool clamp_value) {
|
||||
const auto& properties = analog.properties;
|
||||
float& raw_value = analog.raw_value;
|
||||
float& value = analog.value;
|
||||
|
||||
if (!std::isnormal(raw_value)) {
|
||||
raw_value = 0;
|
||||
}
|
||||
|
||||
// Apply center offset
|
||||
raw_value -= properties.offset;
|
||||
|
||||
// Set initial values to be formated
|
||||
value = raw_value;
|
||||
|
||||
// Calculate vector size
|
||||
const float r = std::abs(value);
|
||||
|
||||
// Return zero if value is smaller than the deadzone
|
||||
if (r <= properties.deadzone || properties.deadzone == 1.0f) {
|
||||
analog.value = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// Adjust range of value
|
||||
const float deadzone_factor =
|
||||
1.0f / r * (r - properties.deadzone) / (1.0f - properties.deadzone);
|
||||
value = value * deadzone_factor / properties.range;
|
||||
|
||||
// Invert direction if needed
|
||||
if (properties.inverted) {
|
||||
value = -value;
|
||||
}
|
||||
|
||||
// Clamp value
|
||||
if (clamp_value) {
|
||||
value = std::clamp(value, -1.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
void SanitizeStick(Common::Input::AnalogStatus& analog_x, Common::Input::AnalogStatus& analog_y,
|
||||
bool clamp_value) {
|
||||
const auto& properties_x = analog_x.properties;
|
||||
const auto& properties_y = analog_y.properties;
|
||||
float& raw_x = analog_x.raw_value;
|
||||
float& raw_y = analog_y.raw_value;
|
||||
float& x = analog_x.value;
|
||||
float& y = analog_y.value;
|
||||
|
||||
if (!std::isnormal(raw_x)) {
|
||||
raw_x = 0;
|
||||
}
|
||||
if (!std::isnormal(raw_y)) {
|
||||
raw_y = 0;
|
||||
}
|
||||
|
||||
// Apply center offset
|
||||
raw_x += properties_x.offset;
|
||||
raw_y += properties_y.offset;
|
||||
|
||||
// Apply X scale correction from offset
|
||||
if (std::abs(properties_x.offset) < 0.5f) {
|
||||
if (raw_x > 0) {
|
||||
raw_x /= 1 + properties_x.offset;
|
||||
} else {
|
||||
raw_x /= 1 - properties_x.offset;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply Y scale correction from offset
|
||||
if (std::abs(properties_y.offset) < 0.5f) {
|
||||
if (raw_y > 0) {
|
||||
raw_y /= 1 + properties_y.offset;
|
||||
} else {
|
||||
raw_y /= 1 - properties_y.offset;
|
||||
}
|
||||
}
|
||||
|
||||
// Invert direction if needed
|
||||
raw_x = properties_x.inverted ? -raw_x : raw_x;
|
||||
raw_y = properties_y.inverted ? -raw_y : raw_y;
|
||||
|
||||
// Set initial values to be formated
|
||||
x = raw_x;
|
||||
y = raw_y;
|
||||
|
||||
// Calculate vector size
|
||||
float r = x * x + y * y;
|
||||
r = std::sqrt(r);
|
||||
|
||||
// TODO(German77): Use deadzone and range of both axis
|
||||
|
||||
// Return zero if values are smaller than the deadzone
|
||||
if (r <= properties_x.deadzone || properties_x.deadzone >= 1.0f) {
|
||||
x = 0;
|
||||
y = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// Adjust range of joystick
|
||||
const float deadzone_factor =
|
||||
1.0f / r * (r - properties_x.deadzone) / (1.0f - properties_x.deadzone);
|
||||
x = x * deadzone_factor / properties_x.range;
|
||||
y = y * deadzone_factor / properties_x.range;
|
||||
r = r * deadzone_factor / properties_x.range;
|
||||
|
||||
// Normalize joystick
|
||||
if (clamp_value && r > 1.0f) {
|
||||
x /= r;
|
||||
y /= r;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Core::HID
|
@ -0,0 +1,96 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace Common::Input {
|
||||
struct CallbackStatus;
|
||||
enum class BatteryLevel : u32;
|
||||
using BatteryStatus = BatteryLevel;
|
||||
struct AnalogStatus;
|
||||
struct ButtonStatus;
|
||||
struct MotionStatus;
|
||||
struct StickStatus;
|
||||
struct TouchStatus;
|
||||
struct TriggerStatus;
|
||||
}; // namespace Common::Input
|
||||
|
||||
namespace Core::HID {
|
||||
|
||||
/**
|
||||
* Converts raw input data into a valid battery status.
|
||||
*
|
||||
* @param callback Supported callbacks: Analog, Battery, Trigger.
|
||||
* @return A valid BatteryStatus object.
|
||||
*/
|
||||
Common::Input::BatteryStatus TransformToBattery(const Common::Input::CallbackStatus& callback);
|
||||
|
||||
/**
|
||||
* Converts raw input data into a valid button status. Applies invert properties to the output.
|
||||
*
|
||||
* @param callback Supported callbacks: Analog, Button, Trigger.
|
||||
* @return A valid TouchStatus object.
|
||||
*/
|
||||
Common::Input::ButtonStatus TransformToButton(const Common::Input::CallbackStatus& callback);
|
||||
|
||||
/**
|
||||
* Converts raw input data into a valid motion status.
|
||||
*
|
||||
* @param callback Supported callbacks: Motion.
|
||||
* @return A valid TouchStatus object.
|
||||
*/
|
||||
Common::Input::MotionStatus TransformToMotion(const Common::Input::CallbackStatus& callback);
|
||||
|
||||
/**
|
||||
* Converts raw input data into a valid stick status. Applies offset, deadzone, range and invert
|
||||
* properties to the output.
|
||||
*
|
||||
* @param callback Supported callbacks: Stick.
|
||||
* @return A valid StickStatus object.
|
||||
*/
|
||||
Common::Input::StickStatus TransformToStick(const Common::Input::CallbackStatus& callback);
|
||||
|
||||
/**
|
||||
* Converts raw input data into a valid touch status.
|
||||
*
|
||||
* @param callback Supported callbacks: Touch.
|
||||
* @return A valid TouchStatus object.
|
||||
*/
|
||||
Common::Input::TouchStatus TransformToTouch(const Common::Input::CallbackStatus& callback);
|
||||
|
||||
/**
|
||||
* Converts raw input data into a valid trigger status. Applies offset, deadzone, range and
|
||||
* invert properties to the output. Button status uses the threshold property if necessary.
|
||||
*
|
||||
* @param callback Supported callbacks: Analog, Button, Trigger.
|
||||
* @return A valid TriggerStatus object.
|
||||
*/
|
||||
Common::Input::TriggerStatus TransformToTrigger(const Common::Input::CallbackStatus& callback);
|
||||
|
||||
/**
|
||||
* Converts raw input data into a valid analog status. Applies offset, deadzone, range and
|
||||
* invert properties to the output.
|
||||
*
|
||||
* @param callback Supported callbacks: Analog.
|
||||
* @return A valid AnalogStatus object.
|
||||
*/
|
||||
Common::Input::AnalogStatus TransformToAnalog(const Common::Input::CallbackStatus& callback);
|
||||
|
||||
/**
|
||||
* Converts raw analog data into a valid analog value
|
||||
* @param analog An analog object containing raw data and properties
|
||||
* @param clamp_value determines if the value needs to be clamped between -1.0f and 1.0f.
|
||||
*/
|
||||
void SanitizeAnalog(Common::Input::AnalogStatus& analog, bool clamp_value);
|
||||
|
||||
/**
|
||||
* Converts raw stick data into a valid stick value
|
||||
* @param analog_x raw analog data and properties for the x-axis
|
||||
* @param analog_y raw analog data and properties for the y-axis
|
||||
* @param clamp_value bool that determines if the value needs to be clamped into the unit circle.
|
||||
*/
|
||||
void SanitizeStick(Common::Input::AnalogStatus& analog_x, Common::Input::AnalogStatus& analog_y,
|
||||
bool clamp_value);
|
||||
|
||||
} // namespace Core::HID
|
@ -0,0 +1,146 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "common/common_types.h"
|
||||
#include "core/device_memory.h"
|
||||
#include "core/hle/kernel/k_auto_object.h"
|
||||
#include "core/hle/kernel/k_code_memory.h"
|
||||
#include "core/hle/kernel/k_light_lock.h"
|
||||
#include "core/hle/kernel/k_memory_block.h"
|
||||
#include "core/hle/kernel/k_page_linked_list.h"
|
||||
#include "core/hle/kernel/k_page_table.h"
|
||||
#include "core/hle/kernel/k_process.h"
|
||||
#include "core/hle/kernel/slab_helpers.h"
|
||||
#include "core/hle/kernel/svc_types.h"
|
||||
#include "core/hle/result.h"
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
KCodeMemory::KCodeMemory(KernelCore& kernel_)
|
||||
: KAutoObjectWithSlabHeapAndContainer{kernel_}, m_lock(kernel_) {}
|
||||
|
||||
ResultCode KCodeMemory::Initialize(Core::DeviceMemory& device_memory, VAddr addr, size_t size) {
|
||||
// Set members.
|
||||
m_owner = kernel.CurrentProcess();
|
||||
|
||||
// Get the owner page table.
|
||||
auto& page_table = m_owner->PageTable();
|
||||
|
||||
// Construct the page group.
|
||||
KMemoryInfo kBlockInfo = page_table.QueryInfo(addr);
|
||||
m_page_group = KPageLinkedList(kBlockInfo.GetAddress(), kBlockInfo.GetNumPages());
|
||||
|
||||
// Lock the memory.
|
||||
R_TRY(page_table.LockForCodeMemory(addr, size))
|
||||
|
||||
// Clear the memory.
|
||||
for (const auto& block : m_page_group.Nodes()) {
|
||||
std::memset(device_memory.GetPointer(block.GetAddress()), 0xFF, block.GetSize());
|
||||
}
|
||||
|
||||
// Set remaining tracking members.
|
||||
m_address = addr;
|
||||
m_is_initialized = true;
|
||||
m_is_owner_mapped = false;
|
||||
m_is_mapped = false;
|
||||
|
||||
// We succeeded.
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
void KCodeMemory::Finalize() {
|
||||
// Unlock.
|
||||
if (!m_is_mapped && !m_is_owner_mapped) {
|
||||
const size_t size = m_page_group.GetNumPages() * PageSize;
|
||||
m_owner->PageTable().UnlockForCodeMemory(m_address, size);
|
||||
}
|
||||
}
|
||||
|
||||
ResultCode KCodeMemory::Map(VAddr address, size_t size) {
|
||||
// Validate the size.
|
||||
R_UNLESS(m_page_group.GetNumPages() == Common::DivideUp(size, PageSize), ResultInvalidSize);
|
||||
|
||||
// Lock ourselves.
|
||||
KScopedLightLock lk(m_lock);
|
||||
|
||||
// Ensure we're not already mapped.
|
||||
R_UNLESS(!m_is_mapped, ResultInvalidState);
|
||||
|
||||
// Map the memory.
|
||||
R_TRY(kernel.CurrentProcess()->PageTable().MapPages(
|
||||
address, m_page_group, KMemoryState::CodeOut, KMemoryPermission::UserReadWrite));
|
||||
|
||||
// Mark ourselves as mapped.
|
||||
m_is_mapped = true;
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
ResultCode KCodeMemory::Unmap(VAddr address, size_t size) {
|
||||
// Validate the size.
|
||||
R_UNLESS(m_page_group.GetNumPages() == Common::DivideUp(size, PageSize), ResultInvalidSize);
|
||||
|
||||
// Lock ourselves.
|
||||
KScopedLightLock lk(m_lock);
|
||||
|
||||
// Unmap the memory.
|
||||
R_TRY(kernel.CurrentProcess()->PageTable().UnmapPages(address, m_page_group,
|
||||
KMemoryState::CodeOut));
|
||||
|
||||
// Mark ourselves as unmapped.
|
||||
m_is_mapped = false;
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
ResultCode KCodeMemory::MapToOwner(VAddr address, size_t size, Svc::MemoryPermission perm) {
|
||||
// Validate the size.
|
||||
R_UNLESS(m_page_group.GetNumPages() == Common::DivideUp(size, PageSize), ResultInvalidSize);
|
||||
|
||||
// Lock ourselves.
|
||||
KScopedLightLock lk(m_lock);
|
||||
|
||||
// Ensure we're not already mapped.
|
||||
R_UNLESS(!m_is_owner_mapped, ResultInvalidState);
|
||||
|
||||
// Convert the memory permission.
|
||||
KMemoryPermission k_perm{};
|
||||
switch (perm) {
|
||||
case Svc::MemoryPermission::Read:
|
||||
k_perm = KMemoryPermission::UserRead;
|
||||
break;
|
||||
case Svc::MemoryPermission::ReadExecute:
|
||||
k_perm = KMemoryPermission::UserReadExecute;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Map the memory.
|
||||
R_TRY(
|
||||
m_owner->PageTable().MapPages(address, m_page_group, KMemoryState::GeneratedCode, k_perm));
|
||||
|
||||
// Mark ourselves as mapped.
|
||||
m_is_owner_mapped = true;
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
ResultCode KCodeMemory::UnmapFromOwner(VAddr address, size_t size) {
|
||||
// Validate the size.
|
||||
R_UNLESS(m_page_group.GetNumPages() == Common::DivideUp(size, PageSize), ResultInvalidSize);
|
||||
|
||||
// Lock ourselves.
|
||||
KScopedLightLock lk(m_lock);
|
||||
|
||||
// Unmap the memory.
|
||||
R_TRY(m_owner->PageTable().UnmapPages(address, m_page_group, KMemoryState::GeneratedCode));
|
||||
|
||||
// Mark ourselves as unmapped.
|
||||
m_is_owner_mapped = false;
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
} // namespace Kernel
|
@ -0,0 +1,66 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common/common_types.h"
|
||||
#include "core/device_memory.h"
|
||||
#include "core/hle/kernel/k_auto_object.h"
|
||||
#include "core/hle/kernel/k_light_lock.h"
|
||||
#include "core/hle/kernel/k_page_linked_list.h"
|
||||
#include "core/hle/kernel/k_process.h"
|
||||
#include "core/hle/kernel/slab_helpers.h"
|
||||
#include "core/hle/kernel/svc_types.h"
|
||||
#include "core/hle/result.h"
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
enum class CodeMemoryOperation : u32 {
|
||||
Map = 0,
|
||||
MapToOwner = 1,
|
||||
Unmap = 2,
|
||||
UnmapFromOwner = 3,
|
||||
};
|
||||
|
||||
class KCodeMemory final
|
||||
: public KAutoObjectWithSlabHeapAndContainer<KCodeMemory, KAutoObjectWithList> {
|
||||
KERNEL_AUTOOBJECT_TRAITS(KCodeMemory, KAutoObject);
|
||||
|
||||
public:
|
||||
explicit KCodeMemory(KernelCore& kernel_);
|
||||
|
||||
ResultCode Initialize(Core::DeviceMemory& device_memory, VAddr address, size_t size);
|
||||
void Finalize();
|
||||
|
||||
ResultCode Map(VAddr address, size_t size);
|
||||
ResultCode Unmap(VAddr address, size_t size);
|
||||
ResultCode MapToOwner(VAddr address, size_t size, Svc::MemoryPermission perm);
|
||||
ResultCode UnmapFromOwner(VAddr address, size_t size);
|
||||
|
||||
bool IsInitialized() const {
|
||||
return m_is_initialized;
|
||||
}
|
||||
static void PostDestroy([[maybe_unused]] uintptr_t arg) {}
|
||||
|
||||
KProcess* GetOwner() const {
|
||||
return m_owner;
|
||||
}
|
||||
VAddr GetSourceAddress() const {
|
||||
return m_address;
|
||||
}
|
||||
size_t GetSize() const {
|
||||
return m_is_initialized ? m_page_group.GetNumPages() * PageSize : 0;
|
||||
}
|
||||
|
||||
private:
|
||||
KPageLinkedList m_page_group{};
|
||||
KProcess* m_owner{};
|
||||
VAddr m_address{};
|
||||
KLightLock m_lock;
|
||||
bool m_is_initialized{};
|
||||
bool m_is_owner_mapped{};
|
||||
bool m_is_mapped{};
|
||||
};
|
||||
|
||||
} // namespace Kernel
|
@ -0,0 +1,80 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "core/hle/kernel/k_light_condition_variable.h"
|
||||
#include "core/hle/kernel/k_scheduler.h"
|
||||
#include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h"
|
||||
#include "core/hle/kernel/k_thread_queue.h"
|
||||
#include "core/hle/kernel/svc_results.h"
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
namespace {
|
||||
|
||||
class ThreadQueueImplForKLightConditionVariable final : public KThreadQueue {
|
||||
public:
|
||||
ThreadQueueImplForKLightConditionVariable(KernelCore& kernel_, KThread::WaiterList* wl,
|
||||
bool term)
|
||||
: KThreadQueue(kernel_), m_wait_list(wl), m_allow_terminating_thread(term) {}
|
||||
|
||||
void CancelWait(KThread* waiting_thread, ResultCode wait_result,
|
||||
bool cancel_timer_task) override {
|
||||
// Only process waits if we're allowed to.
|
||||
if (ResultTerminationRequested == wait_result && m_allow_terminating_thread) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove the thread from the waiting thread from the light condition variable.
|
||||
m_wait_list->erase(m_wait_list->iterator_to(*waiting_thread));
|
||||
|
||||
// Invoke the base cancel wait handler.
|
||||
KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task);
|
||||
}
|
||||
|
||||
private:
|
||||
KThread::WaiterList* m_wait_list;
|
||||
bool m_allow_terminating_thread;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
void KLightConditionVariable::Wait(KLightLock* lock, s64 timeout, bool allow_terminating_thread) {
|
||||
// Create thread queue.
|
||||
KThread* owner = GetCurrentThreadPointer(kernel);
|
||||
|
||||
ThreadQueueImplForKLightConditionVariable wait_queue(kernel, std::addressof(wait_list),
|
||||
allow_terminating_thread);
|
||||
|
||||
// Sleep the thread.
|
||||
{
|
||||
KScopedSchedulerLockAndSleep lk(kernel, owner, timeout);
|
||||
|
||||
if (!allow_terminating_thread && owner->IsTerminationRequested()) {
|
||||
lk.CancelSleep();
|
||||
return;
|
||||
}
|
||||
|
||||
lock->Unlock();
|
||||
|
||||
// Add the thread to the queue.
|
||||
wait_list.push_back(*owner);
|
||||
|
||||
// Begin waiting.
|
||||
owner->BeginWait(std::addressof(wait_queue));
|
||||
}
|
||||
|
||||
// Re-acquire the lock.
|
||||
lock->Lock();
|
||||
}
|
||||
|
||||
void KLightConditionVariable::Broadcast() {
|
||||
KScopedSchedulerLock lk(kernel);
|
||||
|
||||
// Signal all threads.
|
||||
for (auto it = wait_list.begin(); it != wait_list.end(); it = wait_list.erase(it)) {
|
||||
it->EndWait(ResultSuccess);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Kernel
|
@ -0,0 +1,49 @@
|
||||
// Copyright 2021 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "core/hle/kernel/k_thread_queue.h"
|
||||
#include "core/hle/kernel/kernel.h"
|
||||
#include "core/hle/kernel/time_manager.h"
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
void KThreadQueue::NotifyAvailable([[maybe_unused]] KThread* waiting_thread,
|
||||
[[maybe_unused]] KSynchronizationObject* signaled_object,
|
||||
[[maybe_unused]] ResultCode wait_result) {}
|
||||
|
||||
void KThreadQueue::EndWait(KThread* waiting_thread, ResultCode wait_result) {
|
||||
// Set the thread's wait result.
|
||||
waiting_thread->SetWaitResult(wait_result);
|
||||
|
||||
// Set the thread as runnable.
|
||||
waiting_thread->SetState(ThreadState::Runnable);
|
||||
|
||||
// Clear the thread's wait queue.
|
||||
waiting_thread->ClearWaitQueue();
|
||||
|
||||
// Cancel the thread task.
|
||||
kernel.TimeManager().UnscheduleTimeEvent(waiting_thread);
|
||||
}
|
||||
|
||||
void KThreadQueue::CancelWait(KThread* waiting_thread, ResultCode wait_result,
|
||||
bool cancel_timer_task) {
|
||||
// Set the thread's wait result.
|
||||
waiting_thread->SetWaitResult(wait_result);
|
||||
|
||||
// Set the thread as runnable.
|
||||
waiting_thread->SetState(ThreadState::Runnable);
|
||||
|
||||
// Clear the thread's wait queue.
|
||||
waiting_thread->ClearWaitQueue();
|
||||
|
||||
// Cancel the thread task.
|
||||
if (cancel_timer_task) {
|
||||
kernel.TimeManager().UnscheduleTimeEvent(waiting_thread);
|
||||
}
|
||||
}
|
||||
|
||||
void KThreadQueueWithoutEndWait::EndWait([[maybe_unused]] KThread* waiting_thread,
|
||||
[[maybe_unused]] ResultCode wait_result) {}
|
||||
|
||||
} // namespace Kernel
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue