suyu/src/core/hid/emulated_devices.h

190 lines
5.9 KiB
C++
Raw Normal View History

2021-09-21 02:43:16 +02: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/settings.h"
#include "core/hid/hid_types.h"
#include "core/hid/motion_input.h"
namespace Core::HID {
using KeyboardDevices =
std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeKeyboard::NumKeyboardKeys>;
using KeyboardModifierDevices =
std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeKeyboard::NumKeyboardMods>;
using MouseButtonDevices =
std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeMouseButton::NumMouseButtons>;
using MouseButtonParams =
std::array<Common::ParamPackage, Settings::NativeMouseButton::NumMouseButtons>;
using KeyboardValues = std::array<Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardKeys>;
using KeyboardModifierValues =
std::array<Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardMods>;
using MouseButtonValues =
std::array<Input::ButtonStatus, Settings::NativeMouseButton::NumMouseButtons>;
struct MousePosition {
s32 x;
s32 y;
s32 delta_wheel_x;
s32 delta_wheel_y;
};
struct DeviceStatus {
// Data from input_common
KeyboardValues keyboard_values{};
KeyboardModifierValues keyboard_moddifier_values{};
MouseButtonValues mouse_button_values{};
2021-10-17 07:33:00 +02:00
// Data for HID serices
2021-09-21 02:43:16 +02:00
KeyboardKey keyboard_state{};
KeyboardModifier keyboard_moddifier_state{};
MouseButton mouse_button_state{};
MousePosition mouse_position_state{};
};
enum class DeviceTriggerType {
Keyboard,
KeyboardModdifier,
Mouse,
};
struct InterfaceUpdateCallback {
std::function<void(DeviceTriggerType)> on_change;
};
class EmulatedDevices {
public:
/**
2021-10-17 07:33:00 +02:00
* Contains all input data related to external devices that aren't necesarily a controller
* like keyboard and mouse
2021-09-21 02:43:16 +02:00
*/
2021-10-17 07:33:00 +02:00
EmulatedDevices();
2021-09-21 02:43:16 +02:00
~EmulatedDevices();
YUZU_NON_COPYABLE(EmulatedDevices);
YUZU_NON_MOVEABLE(EmulatedDevices);
2021-10-17 07:33:00 +02:00
/// Removes all callbacks created from input devices
2021-09-21 02:43:16 +02:00
void UnloadInput();
2021-10-17 07:33:00 +02:00
/// Sets the emulated console into configuring mode. Locking all HID service events from being
/// moddified
2021-09-21 02:43:16 +02:00
void EnableConfiguration();
2021-10-17 07:33:00 +02:00
/// Returns the emulated console to the normal behaivour
2021-09-21 02:43:16 +02:00
void DisableConfiguration();
2021-10-17 07:33:00 +02:00
/// Returns true if the emulated device is on configuring mode
2021-09-21 02:43:16 +02:00
bool IsConfiguring() const;
2021-10-17 07:33:00 +02: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 02:43:16 +02:00
void SaveCurrentConfig();
2021-10-17 07:33:00 +02:00
/// Reverts any mapped changes made that weren't saved
void RestoreConfig();
2021-09-21 02:43:16 +02:00
2021-10-17 07:33:00 +02:00
/// Returns the current mapped motion device
2021-09-21 02:43:16 +02:00
Common::ParamPackage GetMouseButtonParam(std::size_t index) const;
2021-10-17 07:33:00 +02:00
/**
* Updates the current mapped mouse button device
* @param ParamPackage with controller data to be mapped
*/
void SetMouseButtonParam(std::size_t index, Common::ParamPackage param);
2021-09-21 02:43:16 +02:00
2021-10-17 07:33:00 +02:00
/// Returns the latest status of button input from the keyboard with parameters
2021-09-21 02:43:16 +02:00
KeyboardValues GetKeyboardValues() const;
2021-10-17 07:33:00 +02:00
/// Returns the latest status of button input from the keyboard modifiers with parameters
2021-09-21 02:43:16 +02:00
KeyboardModifierValues GetKeyboardModdifierValues() const;
2021-10-17 07:33:00 +02:00
/// Returns the latest status of button input from the mouse with parameters
2021-09-21 02:43:16 +02:00
MouseButtonValues GetMouseButtonsValues() const;
2021-10-17 07:33:00 +02:00
/// Returns the latest status of button input from the keyboard
2021-09-21 02:43:16 +02:00
KeyboardKey GetKeyboard() const;
2021-10-17 07:33:00 +02:00
/// Returns the latest status of button input from the keyboard modifiers
2021-09-21 02:43:16 +02:00
KeyboardModifier GetKeyboardModifier() const;
2021-10-17 07:33:00 +02:00
/// Returns the latest status of button input from the mouse
2021-09-21 02:43:16 +02:00
MouseButton GetMouseButtons() const;
2021-10-17 07:33:00 +02:00
/// Returns the latest mouse coordinates
2021-09-21 02:43:16 +02:00
MousePosition GetMousePosition() const;
2021-10-17 07:33:00 +02: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-21 02:43:16 +02:00
int SetCallback(InterfaceUpdateCallback update_callback);
2021-10-17 07:33:00 +02: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-21 02:43:16 +02:00
void DeleteCallback(int key);
private:
/**
2021-10-17 07:33:00 +02:00
* Updates the touch status of the console
* @param callback: A CallbackStatus containing the key status
* @param index: key ID to be updated
2021-09-21 02:43:16 +02:00
*/
void SetKeyboardButton(Input::CallbackStatus callback, std::size_t index);
2021-10-17 07:33:00 +02:00
/**
* Updates the touch status of the console
* @param callback: A CallbackStatus containing the modifier key status
* @param index: modifier key ID to be updated
*/
2021-09-21 02:43:16 +02:00
void SetKeyboardModifier(Input::CallbackStatus callback, std::size_t index);
2021-10-17 07:33:00 +02:00
/**
* Updates the touch status of the console
* @param callback: A CallbackStatus containing the button status
* @param index: Button ID of the to be updated
*/
2021-09-21 02:43:16 +02:00
void SetMouseButton(Input::CallbackStatus callback, std::size_t index);
/**
2021-10-17 07:33:00 +02:00
* Triggers a callback that something has changed on the device status
* @param Input type of the event to trigger
2021-09-21 02:43:16 +02:00
*/
void TriggerOnChange(DeviceTriggerType type);
bool is_configuring{false};
MouseButtonParams mouse_button_params;
KeyboardDevices keyboard_devices;
KeyboardModifierDevices keyboard_modifier_devices;
MouseButtonDevices mouse_button_devices;
mutable std::mutex mutex;
std::unordered_map<int, InterfaceUpdateCallback> callback_list;
int last_callback_key = 0;
2021-10-17 07:33:00 +02:00
// Stores the current status of all external device input
2021-09-21 02:43:16 +02:00
DeviceStatus device_status;
};
} // namespace Core::HID