yuzu/src/core/hid/emulated_console.h

191 lines
5.3 KiB
C++
Raw Normal View History

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