summaryrefslogtreecommitdiff
path: root/src/core/hid/emulated_controller.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hid/emulated_controller.h')
-rw-r--r--src/core/hid/emulated_controller.h619
1 files changed, 0 insertions, 619 deletions
diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h
deleted file mode 100644
index d6e20ab66..000000000
--- a/src/core/hid/emulated_controller.h
+++ /dev/null
@@ -1,619 +0,0 @@
1// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <array>
7#include <functional>
8#include <memory>
9#include <mutex>
10#include <unordered_map>
11#include <vector>
12
13#include "common/common_types.h"
14#include "common/input.h"
15#include "common/param_package.h"
16#include "common/settings.h"
17#include "common/vector_math.h"
18#include "core/hid/hid_types.h"
19#include "core/hid/irs_types.h"
20#include "core/hid/motion_input.h"
21
22namespace Core::HID {
23const std::size_t max_emulated_controllers = 2;
24const std::size_t output_devices_size = 4;
25struct ControllerMotionInfo {
26 Common::Input::MotionStatus raw_status{};
27 MotionInput emulated{};
28};
29
30using ButtonDevices =
31 std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeButton::NumButtons>;
32using StickDevices =
33 std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeAnalog::NumAnalogs>;
34using ControllerMotionDevices =
35 std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeMotion::NumMotions>;
36using TriggerDevices =
37 std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeTrigger::NumTriggers>;
38using ColorDevices =
39 std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
40using BatteryDevices =
41 std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
42using CameraDevices =
43 std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
44using RingAnalogDevices =
45 std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
46using NfcDevices =
47 std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
48using OutputDevices = std::array<std::unique_ptr<Common::Input::OutputDevice>, output_devices_size>;
49
50using ButtonParams = std::array<Common::ParamPackage, Settings::NativeButton::NumButtons>;
51using StickParams = std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs>;
52using ControllerMotionParams = std::array<Common::ParamPackage, Settings::NativeMotion::NumMotions>;
53using TriggerParams = std::array<Common::ParamPackage, Settings::NativeTrigger::NumTriggers>;
54using ColorParams = std::array<Common::ParamPackage, max_emulated_controllers>;
55using BatteryParams = std::array<Common::ParamPackage, max_emulated_controllers>;
56using CameraParams = std::array<Common::ParamPackage, max_emulated_controllers>;
57using RingAnalogParams = std::array<Common::ParamPackage, max_emulated_controllers>;
58using NfcParams = std::array<Common::ParamPackage, max_emulated_controllers>;
59using OutputParams = std::array<Common::ParamPackage, output_devices_size>;
60
61using ButtonValues = std::array<Common::Input::ButtonStatus, Settings::NativeButton::NumButtons>;
62using SticksValues = std::array<Common::Input::StickStatus, Settings::NativeAnalog::NumAnalogs>;
63using TriggerValues =
64 std::array<Common::Input::TriggerStatus, Settings::NativeTrigger::NumTriggers>;
65using ControllerMotionValues = std::array<ControllerMotionInfo, Settings::NativeMotion::NumMotions>;
66using ColorValues = std::array<Common::Input::BodyColorStatus, max_emulated_controllers>;
67using BatteryValues = std::array<Common::Input::BatteryStatus, max_emulated_controllers>;
68using CameraValues = Common::Input::CameraStatus;
69using RingAnalogValue = Common::Input::AnalogStatus;
70using NfcValues = Common::Input::NfcStatus;
71using VibrationValues = std::array<Common::Input::VibrationStatus, max_emulated_controllers>;
72
73struct AnalogSticks {
74 AnalogStickState left{};
75 AnalogStickState right{};
76};
77
78struct ControllerColors {
79 NpadControllerColor fullkey{};
80 NpadControllerColor left{};
81 NpadControllerColor right{};
82};
83
84struct BatteryLevelState {
85 NpadPowerInfo dual{};
86 NpadPowerInfo left{};
87 NpadPowerInfo right{};
88};
89
90struct CameraState {
91 Core::IrSensor::ImageTransferProcessorFormat format{};
92 std::vector<u8> data{};
93 std::size_t sample{};
94};
95
96struct RingSensorForce {
97 f32 force;
98};
99
100using NfcState = Common::Input::NfcStatus;
101
102struct ControllerMotion {
103 Common::Vec3f accel{};
104 Common::Vec3f gyro{};
105 Common::Vec3f rotation{};
106 Common::Vec3f euler{};
107 std::array<Common::Vec3f, 3> orientation{};
108 bool is_at_rest{};
109};
110
111enum EmulatedDeviceIndex : u8 {
112 LeftIndex,
113 RightIndex,
114 DualIndex,
115 AllDevices,
116};
117
118using MotionState = std::array<ControllerMotion, 2>;
119
120struct ControllerStatus {
121 // Data from input_common
122 ButtonValues button_values{};
123 SticksValues stick_values{};
124 ControllerMotionValues motion_values{};
125 TriggerValues trigger_values{};
126 ColorValues color_values{};
127 BatteryValues battery_values{};
128 VibrationValues vibration_values{};
129 CameraValues camera_values{};
130 RingAnalogValue ring_analog_value{};
131 NfcValues nfc_values{};
132
133 // Data for HID services
134 HomeButtonState home_button_state{};
135 CaptureButtonState capture_button_state{};
136 NpadButtonState npad_button_state{};
137 DebugPadButton debug_pad_button_state{};
138 AnalogSticks analog_stick_state{};
139 MotionState motion_state{};
140 NpadGcTriggerState gc_trigger_state{};
141 ControllerColors colors_state{};
142 BatteryLevelState battery_state{};
143 CameraState camera_state{};
144 RingSensorForce ring_analog_state{};
145 NfcState nfc_state{};
146 Common::Input::PollingMode left_polling_mode{};
147 Common::Input::PollingMode right_polling_mode{};
148};
149
150enum class ControllerTriggerType {
151 Button,
152 Stick,
153 Trigger,
154 Motion,
155 Color,
156 Battery,
157 Vibration,
158 IrSensor,
159 RingController,
160 Nfc,
161 Connected,
162 Disconnected,
163 Type,
164 All,
165};
166
167struct ControllerUpdateCallback {
168 std::function<void(ControllerTriggerType)> on_change;
169 bool is_npad_service;
170};
171
172class EmulatedController {
173public:
174 /**
175 * Contains all input data (buttons, joysticks, vibration, and motion) within this controller.
176 * @param npad_id_type npad id type for this specific controller
177 */
178 explicit EmulatedController(NpadIdType npad_id_type_);
179 ~EmulatedController();
180
181 YUZU_NON_COPYABLE(EmulatedController);
182 YUZU_NON_MOVEABLE(EmulatedController);
183
184 /// Converts the controller type from settings to npad type
185 static NpadStyleIndex MapSettingsTypeToNPad(Settings::ControllerType type);
186
187 /// Converts npad type to the equivalent of controller type from settings
188 static Settings::ControllerType MapNPadToSettingsType(NpadStyleIndex type);
189
190 /// Gets the NpadIdType for this controller
191 NpadIdType GetNpadIdType() const;
192
193 /// Sets the NpadStyleIndex for this controller
194 void SetNpadStyleIndex(NpadStyleIndex npad_type_);
195
196 /**
197 * Gets the NpadStyleIndex for this controller
198 * @param get_temporary_value If true tmp_npad_type will be returned
199 * @return NpadStyleIndex set on the controller
200 */
201 NpadStyleIndex GetNpadStyleIndex(bool get_temporary_value = false) const;
202
203 /**
204 * Sets the supported controller types. Disconnects the controller if current type is not
205 * supported
206 * @param supported_styles bitflag with supported types
207 */
208 void SetSupportedNpadStyleTag(NpadStyleTag supported_styles);
209
210 /**
211 * Sets the connected status to true
212 * @param use_temporary_value If true tmp_npad_type will be used
213 */
214 void Connect(bool use_temporary_value = false);
215
216 /// Sets the connected status to false
217 void Disconnect();
218
219 /**
220 * Is the emulated connected
221 * @param get_temporary_value If true tmp_is_connected will be returned
222 * @return true if the controller has the connected status
223 */
224 bool IsConnected(bool get_temporary_value = false) const;
225
226 /// Removes all callbacks created from input devices
227 void UnloadInput();
228
229 /**
230 * Sets the emulated controller into configuring mode
231 * This prevents the modification of the HID state of the emulated controller by input commands
232 */
233 void EnableConfiguration();
234
235 /// Returns the emulated controller into normal mode, allowing the modification of the HID state
236 void DisableConfiguration();
237
238 /// Enables Home and Screenshot buttons
239 void EnableSystemButtons();
240
241 /// Disables Home and Screenshot buttons
242 void DisableSystemButtons();
243
244 /// Sets Home and Screenshot buttons to false
245 void ResetSystemButtons();
246
247 /// Returns true if the emulated controller is in configuring mode
248 bool IsConfiguring() const;
249
250 /// Reload all input devices
251 void ReloadInput();
252
253 /// Overrides current mapped devices with the stored configuration and reloads all input devices
254 void ReloadFromSettings();
255
256 /// Updates current colors with the ones stored in the configuration
257 void ReloadColorsFromSettings();
258
259 /// Saves the current mapped configuration
260 void SaveCurrentConfig();
261
262 /// Reverts any mapped changes made that weren't saved
263 void RestoreConfig();
264
265 /// Returns a vector of mapped devices from the mapped button and stick parameters
266 std::vector<Common::ParamPackage> GetMappedDevices() const;
267
268 // Returns the current mapped button device
269 Common::ParamPackage GetButtonParam(std::size_t index) const;
270
271 // Returns the current mapped stick device
272 Common::ParamPackage GetStickParam(std::size_t index) const;
273
274 // Returns the current mapped motion device
275 Common::ParamPackage GetMotionParam(std::size_t index) const;
276
277 /**
278 * Updates the current mapped button device
279 * @param param ParamPackage with controller data to be mapped
280 */
281 void SetButtonParam(std::size_t index, Common::ParamPackage param);
282
283 /**
284 * Updates the current mapped stick device
285 * @param param ParamPackage with controller data to be mapped
286 */
287 void SetStickParam(std::size_t index, Common::ParamPackage param);
288
289 /**
290 * Updates the current mapped motion device
291 * @param param ParamPackage with controller data to be mapped
292 */
293 void SetMotionParam(std::size_t index, Common::ParamPackage param);
294
295 /// Auto calibrates the current motion devices
296 void StartMotionCalibration();
297
298 /// Returns the latest button status from the controller with parameters
299 ButtonValues GetButtonsValues() const;
300
301 /// Returns the latest analog stick status from the controller with parameters
302 SticksValues GetSticksValues() const;
303
304 /// Returns the latest trigger status from the controller with parameters
305 TriggerValues GetTriggersValues() const;
306
307 /// Returns the latest motion status from the controller with parameters
308 ControllerMotionValues GetMotionValues() const;
309
310 /// Returns the latest color status from the controller with parameters
311 ColorValues GetColorsValues() const;
312
313 /// Returns the latest battery status from the controller with parameters
314 BatteryValues GetBatteryValues() const;
315
316 /// Returns the latest camera status from the controller with parameters
317 CameraValues GetCameraValues() const;
318
319 /// Returns the latest status of analog input from the ring sensor with parameters
320 RingAnalogValue GetRingSensorValues() const;
321
322 /// Returns the latest status of button input for the hid::HomeButton service
323 HomeButtonState GetHomeButtons() const;
324
325 /// Returns the latest status of button input for the hid::CaptureButton service
326 CaptureButtonState GetCaptureButtons() const;
327
328 /// Returns the latest status of button input for the hid::Npad service
329 NpadButtonState GetNpadButtons() const;
330
331 /// Returns the latest status of button input for the debug pad service
332 DebugPadButton GetDebugPadButtons() const;
333
334 /// Returns the latest status of stick input from the mouse
335 AnalogSticks GetSticks() const;
336
337 /// Returns the latest status of trigger input from the mouse
338 NpadGcTriggerState GetTriggers() const;
339
340 /// Returns the latest status of motion input from the mouse
341 MotionState GetMotions() const;
342
343 /// Returns the latest color value from the controller
344 ControllerColors GetColors() const;
345
346 /// Returns the latest battery status from the controller
347 BatteryLevelState GetBattery() const;
348
349 /// Returns the latest camera status from the controller
350 const CameraState& GetCamera() const;
351
352 /// Returns the latest ringcon force sensor value
353 RingSensorForce GetRingSensorForce() const;
354
355 /// Returns the latest ntag status from the controller
356 const NfcState& GetNfc() const;
357
358 /**
359 * Sends a specific vibration to the output device
360 * @return true if vibration had no errors
361 */
362 bool SetVibration(std::size_t device_index, VibrationValue vibration);
363
364 /**
365 * Sends a small vibration to the output device
366 * @return true if SetVibration was successful
367 */
368 bool IsVibrationEnabled(std::size_t device_index);
369
370 /**
371 * Sets the desired data to be polled from a controller
372 * @param device_index index of the controller to set the polling mode
373 * @param polling_mode type of input desired buttons, gyro, nfc, ir, etc.
374 * @return driver result from this command
375 */
376 Common::Input::DriverResult SetPollingMode(EmulatedDeviceIndex device_index,
377 Common::Input::PollingMode polling_mode);
378 /**
379 * Get the current polling mode from a controller
380 * @param device_index index of the controller to set the polling mode
381 * @return current polling mode
382 */
383 Common::Input::PollingMode GetPollingMode(EmulatedDeviceIndex device_index) const;
384
385 /**
386 * Sets the desired camera format to be polled from a controller
387 * @param camera_format size of each frame
388 * @return true if SetCameraFormat was successful
389 */
390 bool SetCameraFormat(Core::IrSensor::ImageTransferProcessorFormat camera_format);
391
392 // Returns the current mapped ring device
393 Common::ParamPackage GetRingParam() const;
394
395 /**
396 * Updates the current mapped ring device
397 * @param param ParamPackage with ring sensor data to be mapped
398 */
399 void SetRingParam(Common::ParamPackage param);
400
401 /// Returns true if the device has nfc support
402 bool HasNfc() const;
403
404 /// Sets the joycon in nfc mode and increments the handle count
405 bool AddNfcHandle();
406
407 /// Decrements the handle count if zero sets the joycon in active mode
408 bool RemoveNfcHandle();
409
410 /// Start searching for nfc tags
411 bool StartNfcPolling();
412
413 /// Stop searching for nfc tags
414 bool StopNfcPolling();
415
416 /// Returns true if the nfc tag was readable
417 bool ReadAmiiboData(std::vector<u8>& data);
418
419 /// Returns true if the nfc tag was written
420 bool WriteNfc(const std::vector<u8>& data);
421
422 /// Returns true if the nfc tag was readable
423 bool ReadMifareData(const Common::Input::MifareRequest& request,
424 Common::Input::MifareRequest& out_data);
425
426 /// Returns true if the nfc tag was written
427 bool WriteMifareData(const Common::Input::MifareRequest& request);
428
429 /// Returns the led pattern corresponding to this emulated controller
430 LedPattern GetLedPattern() const;
431
432 /// Asks the output device to change the player led pattern
433 void SetLedPattern();
434
435 /// Changes sensitivity of the motion sensor
436 void SetGyroscopeZeroDriftMode(GyroscopeZeroDriftMode mode);
437
438 /**
439 * Adds a callback to the list of events
440 * @param update_callback A ConsoleUpdateCallback that will be triggered
441 * @return an unique key corresponding to the callback index in the list
442 */
443 int SetCallback(ControllerUpdateCallback update_callback);
444
445 /**
446 * Removes a callback from the list stopping any future events to this object
447 * @param key Key corresponding to the callback index in the list
448 */
449 void DeleteCallback(int key);
450
451 /// Swaps the state of the turbo buttons and updates motion input
452 void StatusUpdate();
453
454private:
455 /// creates input devices from params
456 void LoadDevices();
457
458 /// Set the params for TAS devices
459 void LoadTASParams();
460
461 /// Set the params for virtual pad devices
462 void LoadVirtualGamepadParams();
463
464 /**
465 * @param use_temporary_value If true tmp_npad_type will be used
466 * @return true if the controller style is fullkey
467 */
468 bool IsControllerFullkey(bool use_temporary_value = false) const;
469
470 /**
471 * Checks the current controller type against the supported_style_tag
472 * @param use_temporary_value If true tmp_npad_type will be used
473 * @return true if the controller is supported
474 */
475 bool IsControllerSupported(bool use_temporary_value = false) const;
476
477 /**
478 * Updates the button status of the controller
479 * @param callback A CallbackStatus containing the button status
480 * @param index Button ID of the to be updated
481 */
482 void SetButton(const Common::Input::CallbackStatus& callback, std::size_t index,
483 Common::UUID uuid);
484
485 /**
486 * Updates the analog stick status of the controller
487 * @param callback A CallbackStatus containing the analog stick status
488 * @param index stick ID of the to be updated
489 */
490 void SetStick(const Common::Input::CallbackStatus& callback, std::size_t index,
491 Common::UUID uuid);
492
493 /**
494 * Updates the trigger status of the controller
495 * @param callback A CallbackStatus containing the trigger status
496 * @param index trigger ID of the to be updated
497 */
498 void SetTrigger(const Common::Input::CallbackStatus& callback, std::size_t index,
499 Common::UUID uuid);
500
501 /**
502 * Updates the motion status of the controller
503 * @param callback A CallbackStatus containing gyro and accelerometer data
504 * @param index motion ID of the to be updated
505 */
506 void SetMotion(const Common::Input::CallbackStatus& callback, std::size_t index);
507
508 /**
509 * Updates the color status of the controller
510 * @param callback A CallbackStatus containing the color status
511 * @param index color ID of the to be updated
512 */
513 void SetColors(const Common::Input::CallbackStatus& callback, std::size_t index);
514
515 /**
516 * Updates the battery status of the controller
517 * @param callback A CallbackStatus containing the battery status
518 * @param index battery ID of the to be updated
519 */
520 void SetBattery(const Common::Input::CallbackStatus& callback, std::size_t index);
521
522 /**
523 * Updates the camera status of the controller
524 * @param callback A CallbackStatus containing the camera status
525 */
526 void SetCamera(const Common::Input::CallbackStatus& callback);
527
528 /**
529 * Updates the ring analog sensor status of the ring controller
530 * @param callback A CallbackStatus containing the force status
531 */
532 void SetRingAnalog(const Common::Input::CallbackStatus& callback);
533
534 /**
535 * Updates the nfc status of the controller
536 * @param callback A CallbackStatus containing the nfc status
537 */
538 void SetNfc(const Common::Input::CallbackStatus& callback);
539
540 /**
541 * Converts a color format from bgra to rgba
542 * @param color in bgra format
543 * @return NpadColor in rgba format
544 */
545 NpadColor GetNpadColor(u32 color);
546
547 /**
548 * Triggers a callback that something has changed on the controller status
549 * @param type Input type of the event to trigger
550 * @param is_service_update indicates if this event should only be sent to HID services
551 */
552 void TriggerOnChange(ControllerTriggerType type, bool is_service_update);
553
554 NpadButton GetTurboButtonMask() const;
555
556 const NpadIdType npad_id_type;
557 NpadStyleIndex npad_type{NpadStyleIndex::None};
558 NpadStyleIndex original_npad_type{NpadStyleIndex::None};
559 NpadStyleTag supported_style_tag{NpadStyleSet::All};
560 bool is_connected{false};
561 bool is_configuring{false};
562 bool is_initalized{false};
563 bool system_buttons_enabled{true};
564 f32 motion_sensitivity{Core::HID::MotionInput::IsAtRestStandard};
565 u32 turbo_button_state{0};
566 std::size_t nfc_handles{0};
567
568 // Temporary values to avoid doing changes while the controller is in configuring mode
569 NpadStyleIndex tmp_npad_type{NpadStyleIndex::None};
570 bool tmp_is_connected{false};
571
572 ButtonParams button_params;
573 StickParams stick_params;
574 ControllerMotionParams motion_params;
575 TriggerParams trigger_params;
576 BatteryParams battery_params;
577 ColorParams color_params;
578 CameraParams camera_params;
579 RingAnalogParams ring_params;
580 NfcParams nfc_params;
581 OutputParams output_params;
582
583 ButtonDevices button_devices;
584 StickDevices stick_devices;
585 ControllerMotionDevices motion_devices;
586 TriggerDevices trigger_devices;
587 BatteryDevices battery_devices;
588 ColorDevices color_devices;
589 CameraDevices camera_devices;
590 RingAnalogDevices ring_analog_devices;
591 NfcDevices nfc_devices;
592 OutputDevices output_devices;
593
594 // TAS related variables
595 ButtonParams tas_button_params;
596 StickParams tas_stick_params;
597 ButtonDevices tas_button_devices;
598 StickDevices tas_stick_devices;
599
600 // Virtual gamepad related variables
601 ButtonParams virtual_button_params;
602 StickParams virtual_stick_params;
603 ControllerMotionParams virtual_motion_params;
604 ButtonDevices virtual_button_devices;
605 StickDevices virtual_stick_devices;
606 ControllerMotionDevices virtual_motion_devices;
607
608 mutable std::mutex mutex;
609 mutable std::mutex callback_mutex;
610 mutable std::mutex npad_mutex;
611 mutable std::mutex connect_mutex;
612 std::unordered_map<int, ControllerUpdateCallback> callback_list;
613 int last_callback_key = 0;
614
615 // Stores the current status of all controller input
616 ControllerStatus controller;
617};
618
619} // namespace Core::HID