diff options
Diffstat (limited to 'src/core/hid/emulated_console.h')
| -rw-r--r-- | src/core/hid/emulated_console.h | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/src/core/hid/emulated_console.h b/src/core/hid/emulated_console.h new file mode 100644 index 000000000..3afd284d5 --- /dev/null +++ b/src/core/hid/emulated_console.h | |||
| @@ -0,0 +1,190 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <functional> | ||
| 9 | #include <memory> | ||
| 10 | #include <mutex> | ||
| 11 | #include <unordered_map> | ||
| 12 | |||
| 13 | #include "common/common_types.h" | ||
| 14 | #include "common/input.h" | ||
| 15 | #include "common/param_package.h" | ||
| 16 | #include "common/point.h" | ||
| 17 | #include "common/quaternion.h" | ||
| 18 | #include "common/vector_math.h" | ||
| 19 | #include "core/hid/hid_types.h" | ||
| 20 | #include "core/hid/motion_input.h" | ||
| 21 | |||
| 22 | namespace Core::HID { | ||
| 23 | |||
| 24 | struct ConsoleMotionInfo { | ||
| 25 | Common::Input::MotionStatus raw_status{}; | ||
| 26 | MotionInput emulated{}; | ||
| 27 | }; | ||
| 28 | |||
| 29 | using ConsoleMotionDevices = std::unique_ptr<Common::Input::InputDevice>; | ||
| 30 | using TouchDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, 16>; | ||
| 31 | |||
| 32 | using ConsoleMotionParams = Common::ParamPackage; | ||
| 33 | using TouchParams = std::array<Common::ParamPackage, 16>; | ||
| 34 | |||
| 35 | using ConsoleMotionValues = ConsoleMotionInfo; | ||
| 36 | using TouchValues = std::array<Common::Input::TouchStatus, 16>; | ||
| 37 | |||
| 38 | struct TouchFinger { | ||
| 39 | u64 last_touch{}; | ||
| 40 | Common::Point<float> position{}; | ||
| 41 | u32 id{}; | ||
| 42 | TouchAttribute attribute{}; | ||
| 43 | bool pressed{}; | ||
| 44 | }; | ||
| 45 | |||
| 46 | // Contains all motion related data that is used on the services | ||
| 47 | struct ConsoleMotion { | ||
| 48 | Common::Vec3f accel{}; | ||
| 49 | Common::Vec3f gyro{}; | ||
| 50 | Common::Vec3f rotation{}; | ||
| 51 | std::array<Common::Vec3f, 3> orientation{}; | ||
| 52 | Common::Quaternion<f32> quaternion{}; | ||
| 53 | bool is_at_rest{}; | ||
| 54 | }; | ||
| 55 | |||
| 56 | using TouchFingerState = std::array<TouchFinger, 16>; | ||
| 57 | |||
| 58 | struct ConsoleStatus { | ||
| 59 | // Data from input_common | ||
| 60 | ConsoleMotionValues motion_values{}; | ||
| 61 | TouchValues touch_values{}; | ||
| 62 | |||
| 63 | // Data for HID services | ||
| 64 | ConsoleMotion motion_state{}; | ||
| 65 | TouchFingerState touch_state{}; | ||
| 66 | }; | ||
| 67 | |||
| 68 | enum class ConsoleTriggerType { | ||
| 69 | Motion, | ||
| 70 | Touch, | ||
| 71 | All, | ||
| 72 | }; | ||
| 73 | |||
| 74 | struct ConsoleUpdateCallback { | ||
| 75 | std::function<void(ConsoleTriggerType)> on_change; | ||
| 76 | }; | ||
| 77 | |||
| 78 | class EmulatedConsole { | ||
| 79 | public: | ||
| 80 | /** | ||
| 81 | * Contains all input data within the emulated switch console tablet such as touch and motion | ||
| 82 | */ | ||
| 83 | explicit EmulatedConsole(); | ||
| 84 | ~EmulatedConsole(); | ||
| 85 | |||
| 86 | YUZU_NON_COPYABLE(EmulatedConsole); | ||
| 87 | YUZU_NON_MOVEABLE(EmulatedConsole); | ||
| 88 | |||
| 89 | /// Removes all callbacks created from input devices | ||
| 90 | void UnloadInput(); | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Sets the emulated console into configuring mode | ||
| 94 | * This prevents the modification of the HID state of the emulated console by input commands | ||
| 95 | */ | ||
| 96 | void EnableConfiguration(); | ||
| 97 | |||
| 98 | /// Returns the emulated console into normal mode, allowing the modification of the HID state | ||
| 99 | void DisableConfiguration(); | ||
| 100 | |||
| 101 | /// Returns true if the emulated console is in configuring mode | ||
| 102 | bool IsConfiguring() const; | ||
| 103 | |||
| 104 | /// Reload all input devices | ||
| 105 | void ReloadInput(); | ||
| 106 | |||
| 107 | /// Overrides current mapped devices with the stored configuration and reloads all input devices | ||
| 108 | void ReloadFromSettings(); | ||
| 109 | |||
| 110 | /// Saves the current mapped configuration | ||
| 111 | void SaveCurrentConfig(); | ||
| 112 | |||
| 113 | /// Reverts any mapped changes made that weren't saved | ||
| 114 | void RestoreConfig(); | ||
| 115 | |||
| 116 | // Returns the current mapped motion device | ||
| 117 | Common::ParamPackage GetMotionParam() const; | ||
| 118 | |||
| 119 | /** | ||
| 120 | * Updates the current mapped motion device | ||
| 121 | * @param param ParamPackage with controller data to be mapped | ||
| 122 | */ | ||
| 123 | void SetMotionParam(Common::ParamPackage param); | ||
| 124 | |||
| 125 | /// Returns the latest status of motion input from the console with parameters | ||
| 126 | ConsoleMotionValues GetMotionValues() const; | ||
| 127 | |||
| 128 | /// Returns the latest status of touch input from the console with parameters | ||
| 129 | TouchValues GetTouchValues() const; | ||
| 130 | |||
| 131 | /// Returns the latest status of motion input from the console | ||
| 132 | ConsoleMotion GetMotion() const; | ||
| 133 | |||
| 134 | /// Returns the latest status of touch input from the console | ||
| 135 | TouchFingerState GetTouch() const; | ||
| 136 | |||
| 137 | /** | ||
| 138 | * Adds a callback to the list of events | ||
| 139 | * @param update_callback A ConsoleUpdateCallback that will be triggered | ||
| 140 | * @return an unique key corresponding to the callback index in the list | ||
| 141 | */ | ||
| 142 | int SetCallback(ConsoleUpdateCallback update_callback); | ||
| 143 | |||
| 144 | /** | ||
| 145 | * Removes a callback from the list stopping any future events to this object | ||
| 146 | * @param key Key corresponding to the callback index in the list | ||
| 147 | */ | ||
| 148 | void DeleteCallback(int key); | ||
| 149 | |||
| 150 | private: | ||
| 151 | /// Creates and stores the touch params | ||
| 152 | void SetTouchParams(); | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Updates the motion status of the console | ||
| 156 | * @param callback A CallbackStatus containing gyro and accelerometer data | ||
| 157 | */ | ||
| 158 | void SetMotion(const Common::Input::CallbackStatus& callback); | ||
| 159 | |||
| 160 | /** | ||
| 161 | * Updates the touch status of the console | ||
| 162 | * @param callback A CallbackStatus containing the touch position | ||
| 163 | * @param index Finger ID to be updated | ||
| 164 | */ | ||
| 165 | void SetTouch(const Common::Input::CallbackStatus& callback, std::size_t index); | ||
| 166 | |||
| 167 | /** | ||
| 168 | * Triggers a callback that something has changed on the console status | ||
| 169 | * @param type Input type of the event to trigger | ||
| 170 | */ | ||
| 171 | void TriggerOnChange(ConsoleTriggerType type); | ||
| 172 | |||
| 173 | bool is_configuring{false}; | ||
| 174 | f32 motion_sensitivity{0.01f}; | ||
| 175 | |||
| 176 | ConsoleMotionParams motion_params; | ||
| 177 | TouchParams touch_params; | ||
| 178 | |||
| 179 | ConsoleMotionDevices motion_devices; | ||
| 180 | TouchDevices touch_devices; | ||
| 181 | |||
| 182 | mutable std::mutex mutex; | ||
| 183 | std::unordered_map<int, ConsoleUpdateCallback> callback_list; | ||
| 184 | int last_callback_key = 0; | ||
| 185 | |||
| 186 | // Stores the current status of all console input | ||
| 187 | ConsoleStatus console; | ||
| 188 | }; | ||
| 189 | |||
| 190 | } // namespace Core::HID | ||