suyu/src/core/hid/emulated_devices.h

246 lines
8.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/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>;
2021-11-14 20:09:29 +00:00
using MouseAnalogDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
Settings::NativeMouseWheel::NumMouseWheels>;
using MouseStickDevice = std::unique_ptr<Common::Input::InputDevice>;
2022-01-09 05:23:40 +00:00
using RingAnalogDevice = std::unique_ptr<Common::Input::InputDevice>;
2021-09-21 00:43:16 +00:00
using MouseButtonParams =
std::array<Common::ParamPackage, Settings::NativeMouseButton::NumMouseButtons>;
2022-01-09 05:23:40 +00:00
using RingAnalogParams = Common::ParamPackage;
2021-09-21 00:43:16 +00:00
using KeyboardValues =
std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardKeys>;
2021-09-21 00:43:16 +00:00
using KeyboardModifierValues =
std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardMods>;
2021-09-21 00:43:16 +00:00
using MouseButtonValues =
std::array<Common::Input::ButtonStatus, Settings::NativeMouseButton::NumMouseButtons>;
2021-11-14 20:09:29 +00:00
using MouseAnalogValues =
std::array<Common::Input::AnalogStatus, Settings::NativeMouseWheel::NumMouseWheels>;
using MouseStickValue = Common::Input::TouchStatus;
2022-01-09 05:23:40 +00:00
using RingAnalogValue = Common::Input::AnalogStatus;
2021-09-21 00:43:16 +00:00
struct MousePosition {
2021-11-14 20:09:29 +00:00
f32 x;
f32 y;
2021-09-21 00:43:16 +00:00
};
2022-01-09 05:23:40 +00:00
struct RingSensorForce {
f32 force;
};
2021-09-21 00:43:16 +00:00
struct DeviceStatus {
// Data from input_common
KeyboardValues keyboard_values{};
KeyboardModifierValues keyboard_moddifier_values{};
MouseButtonValues mouse_button_values{};
2021-11-14 20:09:29 +00:00
MouseAnalogValues mouse_analog_values{};
MouseStickValue mouse_stick_value{};
2022-01-09 05:23:40 +00:00
RingAnalogValue ring_analog_value{};
2021-09-21 00:43:16 +00:00
2021-10-17 05:33:00 +00:00
// Data for HID serices
2021-09-21 00:43:16 +00:00
KeyboardKey keyboard_state{};
KeyboardModifier keyboard_moddifier_state{};
MouseButton mouse_button_state{};
MousePosition mouse_position_state{};
2021-11-14 20:09:29 +00:00
AnalogStickState mouse_wheel_state{};
2022-01-09 05:23:40 +00:00
RingSensorForce ring_analog_state{};
2021-09-21 00:43:16 +00:00
};
enum class DeviceTriggerType {
Keyboard,
KeyboardModdifier,
Mouse,
2022-01-09 05:23:40 +00:00
RingController,
2021-09-21 00:43:16 +00:00
};
struct InterfaceUpdateCallback {
std::function<void(DeviceTriggerType)> on_change;
};
class EmulatedDevices {
public:
/**
2021-10-17 05:33:00 +00:00
* Contains all input data related to external devices that aren't necesarily a controller
* This includes devices such as the keyboard or mouse
2021-09-21 00:43:16 +00:00
*/
explicit EmulatedDevices();
2021-09-21 00:43:16 +00:00
~EmulatedDevices();
YUZU_NON_COPYABLE(EmulatedDevices);
YUZU_NON_MOVEABLE(EmulatedDevices);
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 devices into configuring mode
* This prevents the modification of the HID state of the emulated devices by input commands
*/
2021-09-21 00:43:16 +00:00
void EnableConfiguration();
2021-10-17 05:33:00 +00:00
/// Returns the emulated devices 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 device 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
void RestoreConfig();
2021-09-21 00:43:16 +00:00
2022-01-09 05:23:40 +00:00
// Returns the current mapped ring device
Common::ParamPackage GetRingParam() const;
/**
* Updates the current mapped ring device
* @param param ParamPackage with ring sensor data to be mapped
*/
void SetRingParam(Common::ParamPackage param);
2021-10-17 05:33:00 +00:00
/// Returns the latest status of button input from the keyboard with parameters
2021-09-21 00:43:16 +00:00
KeyboardValues GetKeyboardValues() const;
2021-10-17 05:33:00 +00:00
/// Returns the latest status of button input from the keyboard modifiers with parameters
2021-09-21 00:43:16 +00:00
KeyboardModifierValues GetKeyboardModdifierValues() const;
2021-10-17 05:33:00 +00:00
/// Returns the latest status of button input from the mouse with parameters
2021-09-21 00:43:16 +00:00
MouseButtonValues GetMouseButtonsValues() const;
2022-01-09 05:23:40 +00:00
/// Returns the latest status of analog input from the ring sensor with parameters
RingAnalogValue GetRingSensorValues() const;
2021-10-17 05:33:00 +00:00
/// Returns the latest status of button input from the keyboard
2021-09-21 00:43:16 +00:00
KeyboardKey GetKeyboard() const;
2021-10-17 05:33:00 +00:00
/// Returns the latest status of button input from the keyboard modifiers
2021-09-21 00:43:16 +00:00
KeyboardModifier GetKeyboardModifier() const;
2021-10-17 05:33:00 +00:00
/// Returns the latest status of button input from the mouse
2021-09-21 00:43:16 +00:00
MouseButton GetMouseButtons() const;
2021-10-17 05:33:00 +00:00
/// Returns the latest mouse coordinates
2021-09-21 00:43:16 +00:00
MousePosition GetMousePosition() const;
2021-11-14 20:09:29 +00:00
/// Returns the latest mouse wheel change
AnalogStickState GetMouseWheel() const;
2021-11-14 20:09:29 +00:00
2022-01-09 05:23:40 +00:00
/// Returns the latest ringcon force sensor value
RingSensorForce GetRingSensorForce() const;
2021-10-17 05:33:00 +00:00
/**
* Adds a callback to the list of events
* @param update_callback InterfaceUpdateCallback 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(InterfaceUpdateCallback 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:
/// Helps assigning a value to keyboard_state
void UpdateKey(std::size_t key_index, bool status);
2021-09-21 00:43:16 +00:00
/**
2021-11-14 20:09:29 +00:00
* Updates the touch status of the keyboard device
* @param callback A CallbackStatus containing the key status
* @param index key ID to be updated
2021-09-21 00:43:16 +00:00
*/
void SetKeyboardButton(const Common::Input::CallbackStatus& callback, std::size_t index);
2021-10-17 05:33:00 +00:00
/**
2021-11-14 20:09:29 +00:00
* 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
2021-10-17 05:33:00 +00:00
*/
void SetKeyboardModifier(const Common::Input::CallbackStatus& callback, std::size_t index);
2021-10-17 05:33:00 +00:00
/**
2021-11-14 20:09:29 +00:00
* Updates the mouse button status of the mouse device
* @param callback A CallbackStatus containing the button status
* @param index Button ID to be updated
2021-10-17 05:33:00 +00:00
*/
void SetMouseButton(const Common::Input::CallbackStatus& callback, std::size_t index);
2021-09-21 00:43:16 +00:00
2021-11-14 20:09:29 +00:00
/**
* Updates the mouse wheel status of the mouse device
* @param callback A CallbackStatus containing the wheel status
* @param index wheel ID to be updated
2021-11-14 20:09:29 +00:00
*/
void SetMouseAnalog(const Common::Input::CallbackStatus& callback, std::size_t index);
2021-11-14 20:09:29 +00:00
/**
* Updates the mouse position status of the mouse device
* @param callback A CallbackStatus containing the position status
2021-11-14 20:09:29 +00:00
*/
void SetMouseStick(const Common::Input::CallbackStatus& callback);
2021-11-14 20:09:29 +00:00
2022-01-09 05:23:40 +00:00
/**
* Updates the ring analog sensor status of the ring controller
* @param callback A CallbackStatus containing the force status
*/
void SetRingAnalog(const Common::Input::CallbackStatus& callback);
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 device status
* @param type Input type of the event to trigger
2021-09-21 00:43:16 +00:00
*/
void TriggerOnChange(DeviceTriggerType type);
bool is_configuring{false};
2022-01-09 05:23:40 +00:00
RingAnalogParams ring_params;
2021-09-21 00:43:16 +00:00
KeyboardDevices keyboard_devices;
KeyboardModifierDevices keyboard_modifier_devices;
MouseButtonDevices mouse_button_devices;
2021-11-14 20:09:29 +00:00
MouseAnalogDevices mouse_analog_devices;
MouseStickDevice mouse_stick_device;
2022-01-09 05:23:40 +00:00
RingAnalogDevice ring_analog_device;
2021-09-21 00:43:16 +00:00
mutable std::mutex mutex;
mutable std::mutex callback_mutex;
2021-09-21 00:43:16 +00:00
std::unordered_map<int, InterfaceUpdateCallback> callback_list;
int last_callback_key = 0;
2021-10-17 05:33:00 +00:00
// Stores the current status of all external device input
2021-09-21 00:43:16 +00:00
DeviceStatus device_status;
};
} // namespace Core::HID