pineapple/src/core/hid/emulated_devices.h

210 lines
7 KiB
C
Raw Normal View History

2021-11-02 05:02:57 +01:00
// Copyright 2021 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <array>
#include <functional>
#include <memory>
#include <mutex>
#include <unordered_map>
#include "common/common_types.h"
#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-15 02:13:48 +01:00
using MouseAnalogDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
Settings::NativeMouseWheel::NumMouseWheels>;
using MouseStickDevice = std::unique_ptr<Common::Input::InputDevice>;
2021-11-02 05:02:57 +01:00
using MouseButtonParams =
std::array<Common::ParamPackage, Settings::NativeMouseButton::NumMouseButtons>;
using KeyboardValues =
std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardKeys>;
using KeyboardModifierValues =
std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardMods>;
using MouseButtonValues =
std::array<Common::Input::ButtonStatus, Settings::NativeMouseButton::NumMouseButtons>;
2021-11-15 02:13:48 +01:00
using MouseAnalogValues =
std::array<Common::Input::AnalogStatus, Settings::NativeMouseWheel::NumMouseWheels>;
2021-11-15 21:44:06 +01:00
using MouseStickValue = Common::Input::TouchStatus;
2021-11-02 05:02:57 +01:00
struct MousePosition {
2021-11-15 02:13:48 +01:00
f32 x;
f32 y;
2021-11-02 05:02:57 +01:00
};
struct DeviceStatus {
// Data from input_common
KeyboardValues keyboard_values{};
KeyboardModifierValues keyboard_moddifier_values{};
MouseButtonValues mouse_button_values{};
2021-11-15 02:13:48 +01:00
MouseAnalogValues mouse_analog_values{};
MouseStickValue mouse_stick_value{};
2021-11-02 05:02:57 +01:00
// Data for HID serices
KeyboardKey keyboard_state{};
KeyboardModifier keyboard_moddifier_state{};
MouseButton mouse_button_state{};
MousePosition mouse_position_state{};
2021-11-15 02:13:48 +01:00
AnalogStickState mouse_wheel_state{};
2021-11-02 05:02:57 +01:00
};
enum class DeviceTriggerType {
Keyboard,
KeyboardModdifier,
Mouse,
};
struct InterfaceUpdateCallback {
std::function<void(DeviceTriggerType)> on_change;
};
class EmulatedDevices {
public:
/**
* Contains all input data related to external devices that aren't necesarily a controller
* like keyboard and mouse
*/
EmulatedDevices();
~EmulatedDevices();
YUZU_NON_COPYABLE(EmulatedDevices);
YUZU_NON_MOVEABLE(EmulatedDevices);
/// Removes all callbacks created from input devices
void UnloadInput();
/// Sets the emulated console into configuring mode. Locking all HID service events from being
/// moddified
void EnableConfiguration();
/// Returns the emulated console to the normal behaivour
void DisableConfiguration();
/// Returns true if the emulated device is on configuring mode
bool IsConfiguring() const;
/// 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
void SaveCurrentConfig();
/// Reverts any mapped changes made that weren't saved
void RestoreConfig();
/// Returns the latest status of button input from the keyboard with parameters
KeyboardValues GetKeyboardValues() const;
/// Returns the latest status of button input from the keyboard modifiers with parameters
KeyboardModifierValues GetKeyboardModdifierValues() const;
/// Returns the latest status of button input from the mouse with parameters
MouseButtonValues GetMouseButtonsValues() const;
/// Returns the latest status of button input from the keyboard
KeyboardKey GetKeyboard() const;
/// Returns the latest status of button input from the keyboard modifiers
KeyboardModifier GetKeyboardModifier() const;
/// Returns the latest status of button input from the mouse
MouseButton GetMouseButtons() const;
/// Returns the latest mouse coordinates
MousePosition GetMousePosition() const;
2021-11-15 02:13:48 +01:00
/// Returns the latest mouse wheel change
2021-11-15 21:44:06 +01:00
AnalogStickState GetMouseWheel() const;
2021-11-15 02:13:48 +01:00
2021-11-02 05:02:57 +01:00
/**
* Adds a callback to the list of events
2021-11-15 02:13:48 +01:00
* @param InterfaceUpdateCallback that will be triggered
2021-11-02 05:02:57 +01:00
* @return an unique key corresponding to the callback index in the list
*/
int SetCallback(InterfaceUpdateCallback update_callback);
/**
* Removes a callback from the list stopping any future events to this object
* @param Key corresponding to the callback index in the list
*/
void DeleteCallback(int key);
private:
/// Helps assigning a value to keyboard_state
void UpdateKey(std::size_t key_index, bool status);
/**
2021-11-15 02:13:48 +01:00
* Updates the touch status of the keyboard device
2021-11-02 05:02:57 +01:00
* @param callback: A CallbackStatus containing the key status
* @param index: key ID to be updated
*/
void SetKeyboardButton(Common::Input::CallbackStatus callback, std::size_t index);
/**
2021-11-15 02:13:48 +01:00
* Updates the keyboard status of the keyboard device
2021-11-02 05:02:57 +01:00
* @param callback: A CallbackStatus containing the modifier key status
* @param index: modifier key ID to be updated
*/
void SetKeyboardModifier(Common::Input::CallbackStatus callback, std::size_t index);
/**
2021-11-15 02:13:48 +01:00
* Updates the mouse button status of the mouse device
2021-11-02 05:02:57 +01:00
* @param callback: A CallbackStatus containing the button status
2021-11-15 02:13:48 +01:00
* @param index: Button ID to be updated
2021-11-02 05:02:57 +01:00
*/
void SetMouseButton(Common::Input::CallbackStatus callback, std::size_t index);
2021-11-15 02:13:48 +01: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
*/
void SetMouseAnalog(Common::Input::CallbackStatus callback, std::size_t index);
/**
* Updates the mouse position status of the mouse device
* @param callback: A CallbackStatus containing the position status
* @param index: stick ID to be updated
*/
void SetMouseStick(Common::Input::CallbackStatus callback);
2021-11-02 05:02:57 +01:00
/**
* Triggers a callback that something has changed on the device status
* @param Input type of the event to trigger
*/
void TriggerOnChange(DeviceTriggerType type);
bool is_configuring{false};
KeyboardDevices keyboard_devices;
KeyboardModifierDevices keyboard_modifier_devices;
MouseButtonDevices mouse_button_devices;
2021-11-15 02:13:48 +01:00
MouseAnalogDevices mouse_analog_devices;
MouseStickDevice mouse_stick_device;
2021-11-02 05:02:57 +01:00
mutable std::mutex mutex;
std::unordered_map<int, InterfaceUpdateCallback> callback_list;
int last_callback_key = 0;
// Stores the current status of all external device input
DeviceStatus device_status;
};
} // namespace Core::HID