yuzu/src/core/hid/emulated_console.h

201 lines
5.8 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2021-09-20 20:43:16 -04:00
#pragma once
#include <array>
2021-09-20 20:43:16 -04:00
#include <functional>
#include <memory>
2021-09-20 20:43:16 -04:00
#include <mutex>
#include <optional>
2021-09-20 20:43:16 -04:00
#include <unordered_map>
#include "common/common_funcs.h"
#include "common/common_types.h"
2021-09-20 20:43:16 -04: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 {
static constexpr std::size_t MaxTouchDevices = 32;
static constexpr std::size_t MaxActiveTouchInputs = 16;
2021-09-20 20:43:16 -04:00
struct ConsoleMotionInfo {
Common::Input::MotionStatus raw_status{};
2021-09-20 20:43:16 -04:00
MotionInput emulated{};
};
using ConsoleMotionDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, 2>;
using TouchDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, MaxTouchDevices>;
2021-09-20 20:43:16 -04:00
using ConsoleMotionParams = std::array<Common::ParamPackage, 2>;
using TouchParams = std::array<Common::ParamPackage, MaxTouchDevices>;
2021-09-20 20:43:16 -04:00
using ConsoleMotionValues = ConsoleMotionInfo;
using TouchValues = std::array<Common::Input::TouchStatus, MaxTouchDevices>;
2021-09-20 20:43:16 -04:00
struct TouchFinger {
2021-10-23 00:04:06 -04:00
u64 last_touch{};
2021-09-20 20:43:16 -04:00
Common::Point<float> position{};
2021-10-23 00:04:06 -04:00
u32 id{};
2021-10-17 01:33:00 -04:00
TouchAttribute attribute{};
2021-10-23 00:04:06 -04:00
bool pressed{};
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 {
Common::Vec3f accel{};
Common::Vec3f gyro{};
Common::Vec3f rotation{};
std::array<Common::Vec3f, 3> orientation{};
Common::Quaternion<f32> quaternion{};
Common::Vec3f gyro_bias{};
f32 verticalization_error{};
2021-10-23 00:04:06 -04:00
bool is_at_rest{};
2021-09-20 20:43:16 -04:00
};
using TouchFingerState = std::array<TouchFinger, MaxActiveTouchInputs>;
2021-09-20 20:43:16 -04:00
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:
/**
* Contains all input data within the emulated switch console tablet such as touch and motion
2021-09-20 20:43:16 -04:00
*/
explicit 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();
/**
* Sets the emulated console into configuring mode
* This prevents the modification of the HID state of the emulated console by input commands
*/
2021-09-20 20:43:16 -04:00
void EnableConfiguration();
2021-10-17 01:33:00 -04:00
/// Returns the emulated console into normal mode, allowing the modification of the HID state
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 in 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 param ParamPackage with controller data to be mapped
2021-10-17 01:33:00 -04:00
*/
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 update_callback A ConsoleUpdateCallback that will be triggered
2021-10-17 01:33:00 -04:00
* @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 Key corresponding to the callback index in the list
2021-10-17 01:33:00 -04:00
*/
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 callback A CallbackStatus containing gyro and accelerometer data
2021-09-20 20:43:16 -04:00
*/
void SetMotion(const Common::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-10-17 01:33:00 -04:00
*/
void SetTouch(const Common::Input::CallbackStatus& callback, std::size_t index);
2021-09-20 20:43:16 -04:00
std::optional<std::size_t> GetIndexFromFingerId(std::size_t finger_id) const;
std::optional<std::size_t> GetNextFreeIndex() const;
2021-09-20 20:43:16 -04:00
/**
2021-10-17 01:33:00 -04:00
* Triggers a callback that something has changed on the console status
* @param type 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;
mutable std::mutex callback_mutex;
2021-09-20 20:43:16 -04:00
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