diff options
Diffstat (limited to 'src/input_common/drivers/joycon.h')
| -rw-r--r-- | src/input_common/drivers/joycon.h | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/input_common/drivers/joycon.h b/src/input_common/drivers/joycon.h new file mode 100644 index 000000000..56c117270 --- /dev/null +++ b/src/input_common/drivers/joycon.h | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <array> | ||
| 7 | #include <span> | ||
| 8 | #include <thread> | ||
| 9 | #include <SDL_hidapi.h> | ||
| 10 | |||
| 11 | #include "input_common/input_engine.h" | ||
| 12 | |||
| 13 | namespace InputCommon::Joycon { | ||
| 14 | using SerialNumber = std::array<u8, 15>; | ||
| 15 | struct Battery; | ||
| 16 | struct Color; | ||
| 17 | struct MotionData; | ||
| 18 | enum class ControllerType; | ||
| 19 | enum class DriverResult; | ||
| 20 | class JoyconDriver; | ||
| 21 | } // namespace InputCommon::Joycon | ||
| 22 | |||
| 23 | namespace InputCommon { | ||
| 24 | |||
| 25 | class Joycons final : public InputCommon::InputEngine { | ||
| 26 | public: | ||
| 27 | explicit Joycons(const std::string& input_engine_); | ||
| 28 | |||
| 29 | ~Joycons(); | ||
| 30 | |||
| 31 | bool IsVibrationEnabled(const PadIdentifier& identifier) override; | ||
| 32 | Common::Input::VibrationError SetVibration( | ||
| 33 | const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) override; | ||
| 34 | |||
| 35 | void SetLeds(const PadIdentifier& identifier, | ||
| 36 | const Common::Input::LedStatus& led_status) override; | ||
| 37 | |||
| 38 | Common::Input::CameraError SetCameraFormat(const PadIdentifier& identifier_, | ||
| 39 | Common::Input::CameraFormat camera_format) override; | ||
| 40 | |||
| 41 | Common::Input::NfcState SupportsNfc(const PadIdentifier& identifier_) const override; | ||
| 42 | Common::Input::NfcState WriteNfcData(const PadIdentifier& identifier_, | ||
| 43 | const std::vector<u8>& data) override; | ||
| 44 | |||
| 45 | Common::Input::PollingError SetPollingMode( | ||
| 46 | const PadIdentifier& identifier, const Common::Input::PollingMode polling_mode) override; | ||
| 47 | |||
| 48 | /// Used for automapping features | ||
| 49 | std::vector<Common::ParamPackage> GetInputDevices() const override; | ||
| 50 | ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) override; | ||
| 51 | AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override; | ||
| 52 | MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& params) override; | ||
| 53 | Common::Input::ButtonNames GetUIName(const Common::ParamPackage& params) const override; | ||
| 54 | |||
| 55 | private: | ||
| 56 | static constexpr std::size_t MaxSupportedControllers = 8; | ||
| 57 | |||
| 58 | /// For shutting down, clear all data, join all threads, release usb devices | ||
| 59 | void Reset(); | ||
| 60 | |||
| 61 | /// Registers controllers, clears all data and starts the scan thread | ||
| 62 | void Setup(); | ||
| 63 | |||
| 64 | /// Actively searchs for new devices | ||
| 65 | void ScanThread(std::stop_token stop_token); | ||
| 66 | |||
| 67 | /// Returns true if device is valid and not registered | ||
| 68 | bool IsDeviceNew(SDL_hid_device_info* device_info) const; | ||
| 69 | |||
| 70 | /// Tries to connect to the new device | ||
| 71 | void RegisterNewDevice(SDL_hid_device_info* device_info); | ||
| 72 | |||
| 73 | /// Returns the next free handle | ||
| 74 | std::shared_ptr<Joycon::JoyconDriver> GetNextFreeHandle(Joycon::ControllerType type) const; | ||
| 75 | |||
| 76 | void OnBatteryUpdate(std::size_t port, Joycon::ControllerType type, Joycon::Battery value); | ||
| 77 | void OnColorUpdate(std::size_t port, Joycon::ControllerType type, const Joycon::Color& value); | ||
| 78 | void OnButtonUpdate(std::size_t port, Joycon::ControllerType type, int id, bool value); | ||
| 79 | void OnStickUpdate(std::size_t port, Joycon::ControllerType type, int id, f32 value); | ||
| 80 | void OnMotionUpdate(std::size_t port, Joycon::ControllerType type, int id, | ||
| 81 | const Joycon::MotionData& value); | ||
| 82 | void OnRingConUpdate(f32 ring_data); | ||
| 83 | void OnAmiiboUpdate(std::size_t port, const std::vector<u8>& amiibo_data); | ||
| 84 | |||
| 85 | /// Returns a JoyconHandle corresponding to a PadIdentifier | ||
| 86 | std::shared_ptr<Joycon::JoyconDriver> GetHandle(PadIdentifier identifier) const; | ||
| 87 | |||
| 88 | /// Returns a PadIdentifier corresponding to the port number | ||
| 89 | PadIdentifier GetIdentifier(std::size_t port, Joycon::ControllerType type) const; | ||
| 90 | |||
| 91 | std::string JoyconName(std::size_t port) const; | ||
| 92 | |||
| 93 | Common::Input::ButtonNames GetUIButtonName(const Common::ParamPackage& params) const; | ||
| 94 | |||
| 95 | /// Returns the name of the device in text format | ||
| 96 | std::string JoyconName(Joycon::ControllerType type) const; | ||
| 97 | |||
| 98 | std::jthread scan_thread; | ||
| 99 | bool scan_thread_running{}; | ||
| 100 | |||
| 101 | // Joycon types are split by type to ease supporting dualjoycon configurations | ||
| 102 | std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> left_joycons{}; | ||
| 103 | std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> right_joycons{}; | ||
| 104 | std::array<std::shared_ptr<Joycon::JoyconDriver>, MaxSupportedControllers> pro_joycons{}; | ||
| 105 | }; | ||
| 106 | |||
| 107 | } // namespace InputCommon | ||