suyu/src/core/hid/emulated_console.h

187 lines
5.1 KiB
C++
Raw Normal View History

2021-09-20 20:43:16 -04:00
// Copyright 2021 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <functional>
#include <mutex>
#include <unordered_map>
#include "common/input.h"
#include "common/param_package.h"
#include "common/point.h"
#include "common/quaternion.h"
#include "common/settings.h"
#include "common/vector_math.h"
#include "core/hid/hid_types.h"
#include "core/hid/motion_input.h"
namespace Core::HID {
struct ConsoleMotionInfo {
Input::MotionStatus raw_status;
MotionInput emulated{};
};
using ConsoleMotionDevices = std::unique_ptr<Input::InputDevice>;
using TouchDevices = std::array<std::unique_ptr<Input::InputDevice>, 16>;
using ConsoleMotionParams = Common::ParamPackage;
using TouchParams = std::array<Common::ParamPackage, 16>;
using ConsoleMotionValues = ConsoleMotionInfo;
using TouchValues = std::array<Input::TouchStatus, 16>;
struct TouchFinger {
u64_le last_touch{};
Common::Point<float> position{};
u32_le id{};
bool pressed{};
2021-10-17 01:33:00 -04:00
TouchAttribute attribute{};
2021-09-20 20:43:16 -04:00
};
2021-10-17 01:33:00 -04:00
// Contains all motion related data that is used on the services
2021-09-20 20:43:16 -04:00
struct ConsoleMotion {
bool is_at_rest{};
Common::Vec3f accel{};
Common::Vec3f gyro{};
Common::Vec3f rotation{};
std::array<Common::Vec3f, 3> orientation{};
Common::Quaternion<f32> quaternion{};
};
using TouchFingerState = std::array<TouchFinger, 16>;
struct ConsoleStatus {
// Data from input_common
ConsoleMotionValues motion_values{};
TouchValues touch_values{};
2021-10-17 01:33:00 -04:00
// Data for HID services
2021-09-20 20:43:16 -04:00
ConsoleMotion motion_state{};
TouchFingerState touch_state{};
};
enum class ConsoleTriggerType {
Motion,
Touch,
All,
};
struct ConsoleUpdateCallback {
std::function<void(ConsoleTriggerType)> on_change;
};
class EmulatedConsole {
public:
/**
2021-10-17 01:33:00 -04:00
* Contains all input data related to the console like motion and touch input
2021-09-20 20:43:16 -04:00
*/
2021-10-17 01:33:00 -04:00
EmulatedConsole();
2021-09-20 20:43:16 -04:00
~EmulatedConsole();
YUZU_NON_COPYABLE(EmulatedConsole);
YUZU_NON_MOVEABLE(EmulatedConsole);
2021-10-17 01:33:00 -04:00
/// Removes all callbacks created from input devices
2021-09-20 20:43:16 -04:00
void UnloadInput();
2021-10-17 01:33:00 -04:00
/// Sets the emulated console into configuring mode. Locking all HID service events from being
/// moddified
2021-09-20 20:43:16 -04:00
void EnableConfiguration();
2021-10-17 01:33:00 -04:00
/// Returns the emulated console to the normal behaivour
2021-09-20 20:43:16 -04:00
void DisableConfiguration();
2021-10-17 01:33:00 -04:00
/// Returns true if the emulated console is on configuring mode
2021-09-20 20:43:16 -04:00
bool IsConfiguring() const;
2021-10-17 01:33:00 -04:00
/// 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
2021-09-20 20:43:16 -04:00
void SaveCurrentConfig();
2021-10-17 01:33:00 -04:00
/// Reverts any mapped changes made that weren't saved
2021-09-20 20:43:16 -04:00
void RestoreConfig();
2021-10-17 01:33:00 -04:00
// Returns the current mapped motion device
2021-09-20 20:43:16 -04:00
Common::ParamPackage GetMotionParam() const;
2021-10-17 01:33:00 -04:00
/**
* Updates the current mapped motion device
* @param ParamPackage with controller data to be mapped
*/
2021-09-20 20:43:16 -04:00
void SetMotionParam(Common::ParamPackage param);
2021-10-17 01:33:00 -04:00
/// Returns the latest status of motion input from the console with parameters
2021-09-20 20:43:16 -04:00
ConsoleMotionValues GetMotionValues() const;
2021-10-17 01:33:00 -04:00
/// Returns the latest status of touch input from the console with parameters
2021-09-20 20:43:16 -04:00
TouchValues GetTouchValues() const;
2021-10-17 01:33:00 -04:00
/// Returns the latest status of motion input from the console
2021-09-20 20:43:16 -04:00
ConsoleMotion GetMotion() const;
2021-10-17 01:33:00 -04:00
/// Returns the latest status of touch input from the console
2021-09-20 20:43:16 -04:00
TouchFingerState GetTouch() const;
2021-10-17 01:33:00 -04:00
/**
* Adds a callback to the list of events
* @param ConsoleUpdateCallback that will be triggered
* @return an unique key corresponding to the callback index in the list
*/
2021-09-20 20:43:16 -04:00
int SetCallback(ConsoleUpdateCallback update_callback);
2021-10-17 01:33:00 -04:00
/**
* Removes a callback from the list stopping any future events to this object
* @param Key corresponding to the callback index in the list
*/
2021-09-20 20:43:16 -04:00
void DeleteCallback(int key);
private:
2021-10-21 00:18:04 -04:00
/// Creates and stores the touch params
void SetTouchParams();
2021-09-20 20:43:16 -04:00
/**
2021-10-17 01:33:00 -04:00
* Updates the motion status of the console
* @param A CallbackStatus containing gyro and accelerometer data
2021-09-20 20:43:16 -04:00
*/
void SetMotion(Input::CallbackStatus callback);
2021-10-17 01:33:00 -04:00
/**
* Updates the touch status of the console
* @param callback: A CallbackStatus containing the touch position
* @param index: Finger ID to be updated
*/
2021-09-20 20:43:16 -04:00
void SetTouch(Input::CallbackStatus callback, std::size_t index);
/**
2021-10-17 01:33:00 -04:00
* Triggers a callback that something has changed on the console status
* @param Input type of the event to trigger
2021-09-20 20:43:16 -04:00
*/
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;
2021-10-17 01:33:00 -04:00
// Stores the current status of all console input
2021-09-20 20:43:16 -04:00
ConsoleStatus console;
};
} // namespace Core::HID