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
|
|
|
|
|
2021-10-30 23:23:10 -04:00
|
|
|
#include <array>
|
2021-09-20 20:43:16 -04:00
|
|
|
#include <functional>
|
2021-10-30 23:23:10 -04:00
|
|
|
#include <memory>
|
2021-09-20 20:43:16 -04:00
|
|
|
#include <mutex>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2021-10-30 23:23:10 -04:00
|
|
|
#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 {
|
|
|
|
|
|
|
|
struct ConsoleMotionInfo {
|
2021-10-30 23:23:10 -04:00
|
|
|
Common::Input::MotionStatus raw_status{};
|
2021-09-20 20:43:16 -04:00
|
|
|
MotionInput emulated{};
|
|
|
|
};
|
|
|
|
|
2021-10-30 23:23:10 -04:00
|
|
|
using ConsoleMotionDevices = std::unique_ptr<Common::Input::InputDevice>;
|
|
|
|
using TouchDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, 16>;
|
2021-09-20 20:43:16 -04:00
|
|
|
|
|
|
|
using ConsoleMotionParams = Common::ParamPackage;
|
|
|
|
using TouchParams = std::array<Common::ParamPackage, 16>;
|
|
|
|
|
|
|
|
using ConsoleMotionValues = ConsoleMotionInfo;
|
2021-10-30 23:23:10 -04:00
|
|
|
using TouchValues = std::array<Common::Input::TouchStatus, 16>;
|
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{};
|
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, 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
|
|
|
*/
|
2021-10-30 23:23:10 -04:00
|
|
|
void SetMotion(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-30 23:23:10 -04:00
|
|
|
void SetTouch(Common::Input::CallbackStatus callback, std::size_t index);
|
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 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
|