Compare commits
1 Commits
sdl_who
...
ring_proto
Author | SHA1 | Date | |
---|---|---|---|
0495a13e31 |
File diff suppressed because it is too large
Load Diff
@ -21,6 +21,13 @@ using SDL_JoystickID = s32;
|
||||
|
||||
namespace InputCommon {
|
||||
|
||||
class SDLJoystick;
|
||||
|
||||
using ButtonBindings =
|
||||
std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerButton>, 18>;
|
||||
using ZButtonBindings =
|
||||
std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerAxis>, 2>;
|
||||
|
||||
class SDLDriver : public InputEngine {
|
||||
public:
|
||||
/// Initializes and registers SDL device factories
|
||||
@ -34,12 +41,15 @@ public:
|
||||
/// Handle SDL_Events for joysticks from SDL_PollEvent
|
||||
void HandleGameControllerEvent(const SDL_Event& event);
|
||||
|
||||
bool IsStickInverted(const Common::ParamPackage& params) override;
|
||||
/// Get the nth joystick with the corresponding GUID
|
||||
std::shared_ptr<SDLJoystick> GetSDLJoystickBySDLID(SDL_JoystickID sdl_id);
|
||||
|
||||
bool IsVibrationEnabled(const PadIdentifier& identifier) override;
|
||||
|
||||
Common::Input::VibrationError SetVibration(
|
||||
const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) override;
|
||||
/**
|
||||
* Check how many identical joysticks (by guid) were connected before the one with sdl_id and so
|
||||
* tie it to a SDLJoystick with the same guid and that port
|
||||
*/
|
||||
std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const Common::UUID& guid, int port);
|
||||
std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port);
|
||||
|
||||
std::vector<Common::ParamPackage> GetInputDevices() const override;
|
||||
|
||||
@ -51,43 +61,75 @@ public:
|
||||
std::string GetHatButtonName(u8 direction_value) const override;
|
||||
u8 GetHatButtonId(const std::string& direction_name) const override;
|
||||
|
||||
bool IsStickInverted(const Common::ParamPackage& params) override;
|
||||
|
||||
Common::Input::DriverResult SetVibration(
|
||||
const PadIdentifier& identifier, const Common::Input::VibrationStatus& vibration) override;
|
||||
|
||||
bool IsVibrationEnabled(const PadIdentifier& identifier) override;
|
||||
|
||||
private:
|
||||
struct VibrationRequest {
|
||||
PadIdentifier identifier;
|
||||
Common::Input::VibrationStatus vibration;
|
||||
};
|
||||
|
||||
struct JoystickID {
|
||||
int sdl_index;
|
||||
bool has_rumble;
|
||||
PadIdentifier identifier;
|
||||
};
|
||||
|
||||
std::shared_ptr<JoystickID> GetSDLJoystickBySDLID(SDL_JoystickID sdl_id);
|
||||
|
||||
void InitJoystick(int joystick_index);
|
||||
void CloseJoystick(int joystick_index);
|
||||
void CloseJoystick(SDL_Joystick* sdl_joystick);
|
||||
|
||||
/// Needs to be called before SDL_QuitSubSystem.
|
||||
void CloseJoysticks();
|
||||
|
||||
void UpdateControllerStatus(u32 event, int joystick_index, int index, int value);
|
||||
|
||||
Common::Input::BatteryLevel GetBatteryLevel(int value);
|
||||
|
||||
/// Takes all vibrations from the queue and sends the command to the controller
|
||||
void SendVibrations();
|
||||
|
||||
/// Map of GUID of a list of corresponding virtual Joysticks
|
||||
std::unordered_map<Common::UUID, std::vector<std::shared_ptr<JoystickID>>> joystick_map;
|
||||
std::mutex joystick_map_mutex;
|
||||
Common::ParamPackage BuildAnalogParamPackageForButton(int port, const Common::UUID& guid,
|
||||
s32 axis, float value = 0.1f) const;
|
||||
Common::ParamPackage BuildButtonParamPackageForButton(int port, const Common::UUID& guid,
|
||||
s32 button) const;
|
||||
|
||||
std::atomic<bool> initialized = false;
|
||||
Common::ParamPackage BuildHatParamPackageForButton(int port, const Common::UUID& guid, s32 hat,
|
||||
u8 value) const;
|
||||
|
||||
Common::ParamPackage BuildMotionParam(int port, const Common::UUID& guid) const;
|
||||
|
||||
Common::ParamPackage BuildParamPackageForBinding(
|
||||
int port, const Common::UUID& guid, const SDL_GameControllerButtonBind& binding) const;
|
||||
|
||||
Common::ParamPackage BuildParamPackageForAnalog(PadIdentifier identifier, int axis_x,
|
||||
int axis_y, float offset_x,
|
||||
float offset_y) const;
|
||||
|
||||
/// Returns the default button bindings list for generic controllers
|
||||
ButtonBindings GetDefaultButtonBinding() const;
|
||||
|
||||
/// Returns the default button bindings list for nintendo controllers
|
||||
ButtonBindings GetNintendoButtonBinding(const std::shared_ptr<SDLJoystick>& joystick) const;
|
||||
|
||||
/// Returns the button mappings from a single controller
|
||||
ButtonMapping GetSingleControllerMapping(const std::shared_ptr<SDLJoystick>& joystick,
|
||||
const ButtonBindings& switch_to_sdl_button,
|
||||
const ZButtonBindings& switch_to_sdl_axis) const;
|
||||
|
||||
/// Returns the button mappings from two different controllers
|
||||
ButtonMapping GetDualControllerMapping(const std::shared_ptr<SDLJoystick>& joystick,
|
||||
const std::shared_ptr<SDLJoystick>& joystick2,
|
||||
const ButtonBindings& switch_to_sdl_button,
|
||||
const ZButtonBindings& switch_to_sdl_axis) const;
|
||||
|
||||
/// Returns true if the button is on the left joycon
|
||||
bool IsButtonOnLeftSide(Settings::NativeButton::Values button) const;
|
||||
|
||||
/// Queue of vibration request to controllers
|
||||
Common::SPSCQueue<VibrationRequest> vibration_queue;
|
||||
|
||||
/// Thread that handles request from the vibration queue
|
||||
/// Map of GUID of a list of corresponding virtual Joysticks
|
||||
std::unordered_map<Common::UUID, std::vector<std::shared_ptr<SDLJoystick>>> joystick_map;
|
||||
std::mutex joystick_map_mutex;
|
||||
|
||||
bool start_thread = false;
|
||||
std::atomic<bool> initialized = false;
|
||||
|
||||
std::thread vibration_thread;
|
||||
};
|
||||
} // namespace InputCommon
|
||||
|
@ -388,6 +388,32 @@ enum class ExternalDeviceId : u16 {
|
||||
Starlink = 0x2800,
|
||||
};
|
||||
|
||||
// Most missing command names are leftovers from other firmware versions
|
||||
enum class RingConCommand : u32 {
|
||||
GetFirmwareVersion = 0x00020000,
|
||||
ReadId = 0x00020100,
|
||||
JoyPolling = 0x00020101,
|
||||
Unknown1 = 0x00020104,
|
||||
c20105 = 0x00020105,
|
||||
Unknown2 = 0x00020204,
|
||||
Unknown3 = 0x00020304,
|
||||
Unknown4 = 0x00020404,
|
||||
ReadUnkCal = 0x00020504,
|
||||
ReadFactoryCal = 0x00020A04,
|
||||
Unknown5 = 0x00021104,
|
||||
Unknown6 = 0x00021204,
|
||||
Unknown7 = 0x00021304,
|
||||
ReadUserCal = 0x00021A04,
|
||||
ReadRepCount = 0x00023104,
|
||||
ReadTotalPushCount = 0x00023204,
|
||||
ResetRepCount = 0x04013104,
|
||||
Unknown8 = 0x04011104,
|
||||
Unknown9 = 0x04011204,
|
||||
Unknown10 = 0x04011304,
|
||||
SaveCalData = 0x10011A04,
|
||||
Error = 0xFFFFFFFF,
|
||||
};
|
||||
|
||||
enum class DriverResult {
|
||||
Success,
|
||||
WrongReply,
|
||||
@ -687,6 +713,57 @@ struct RingStatus {
|
||||
s16 min_value;
|
||||
};
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct RingCommandData {
|
||||
u8 data_type;
|
||||
RingConCommand command;
|
||||
};
|
||||
static_assert(sizeof(RingCommandData) == 0x5, "RingCommandData is an invalid size");
|
||||
#pragma pack(pop)
|
||||
|
||||
struct RingFactoryCalibration {
|
||||
s32_le os_max;
|
||||
s32_le hk_max;
|
||||
s32_le zero_min;
|
||||
s32_le zero_max;
|
||||
};
|
||||
static_assert(sizeof(RingFactoryCalibration) == 0x10, "RingFactoryCalibration is an invalid size");
|
||||
|
||||
struct RingCalibrationValue {
|
||||
s16 value;
|
||||
u16 crc;
|
||||
};
|
||||
static_assert(sizeof(RingCalibrationValue) == 0x4, "RingCalibrationValue is an invalid size");
|
||||
|
||||
struct RingUserCalibration {
|
||||
RingCalibrationValue os_max;
|
||||
RingCalibrationValue hk_max;
|
||||
RingCalibrationValue zero;
|
||||
};
|
||||
static_assert(sizeof(RingUserCalibration) == 0xC, "RingUserCalibration is an invalid size");
|
||||
|
||||
struct RingReadId {
|
||||
u16 id_l_x0;
|
||||
u16 id_l_x0_2;
|
||||
u16 id_l_x4;
|
||||
u16 id_h_x0;
|
||||
u16 id_h_x0_2;
|
||||
u16 id_h_x4;
|
||||
};
|
||||
static_assert(sizeof(RingReadId) == 0xC, "RingReadId is an invalid size");
|
||||
|
||||
struct RingCommandReply {
|
||||
DriverResult result;
|
||||
RingConCommand command;
|
||||
FirmwareVersion firmware;
|
||||
RingFactoryCalibration factory_calibration;
|
||||
RingUserCalibration user_calibration;
|
||||
RingReadId read_id;
|
||||
u8 c20105;
|
||||
u8 total_push_count;
|
||||
u8 total_rep_count;
|
||||
};
|
||||
|
||||
struct VibrationPacket {
|
||||
OutputReport output_report;
|
||||
u8 packet_counter;
|
||||
|
@ -108,6 +108,65 @@ DriverResult RingConProtocol::ConfigureRing() {
|
||||
return SendSubCommand(SubCommand::ENABLE_EXTERNAL_POLLING, ringcon_data);
|
||||
}
|
||||
|
||||
|
||||
DriverResult RingConProtocol::SendCommand(RingConCommand command) {
|
||||
SubCommandResponce output{};
|
||||
RingCommandData data{
|
||||
.data_type = 0x04, // This seems to be always 4 for this command
|
||||
.command = command,
|
||||
};
|
||||
RingCommandReply reply{
|
||||
.result = DriverResult::Success,
|
||||
.command = command,
|
||||
};
|
||||
|
||||
std::array<u8, sizeof(RingCommandData)> raw_data{};
|
||||
memcpy(raw_data.data(), &data, sizeof(RingCommandData));
|
||||
const DriverResult result = SendSubCommand(SubCommand::SET_EXTERNAL_CONFIG, raw_data, output);
|
||||
|
||||
if (result != DriverResult::Success) {
|
||||
reply.result = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
switch (command) {
|
||||
case RingConCommand::GetFirmwareVersion:
|
||||
memcpy(&reply.firmware, output.command_data.data() + 6, sizeof(FirmwareVersion));
|
||||
break;
|
||||
case RingConCommand::ReadId:
|
||||
memcpy(&reply.read_id, output.command_data.data() + 6,
|
||||
sizeof(RingReadId));
|
||||
break;
|
||||
case RingConCommand::c20105:
|
||||
reply.c20105 = output.command_data[6];
|
||||
break;
|
||||
case RingConCommand::ReadUnkCal:
|
||||
break;
|
||||
case RingConCommand::ReadFactoryCal:
|
||||
memcpy(&reply.factory_calibration, output.command_data.data() + 6,
|
||||
sizeof(RingFactoryCalibration));
|
||||
break;
|
||||
case RingConCommand::ReadUserCal:
|
||||
memcpy(&reply.user_calibration, output.command_data.data() + 6, sizeof(RingUserCalibration));
|
||||
break;
|
||||
case RingConCommand::ReadRepCount:
|
||||
reply.total_rep_count = output.command_data[6];
|
||||
break;
|
||||
case RingConCommand::ReadTotalPushCount:
|
||||
reply.total_push_count = output.command_data[6];
|
||||
break;
|
||||
case RingConCommand::ResetRepCount:
|
||||
break;
|
||||
case RingConCommand::SaveCalData:
|
||||
break;
|
||||
default:
|
||||
reply.result = DriverResult::NotSupported;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool RingConProtocol::IsEnabled() const {
|
||||
return is_enabled;
|
||||
}
|
||||
|
@ -25,6 +25,9 @@ public:
|
||||
|
||||
DriverResult StartRingconPolling();
|
||||
|
||||
// Hidbus can send individual commands to the ring each one with an unique reply
|
||||
DriverResult SendCommand(RingConCommand command);
|
||||
|
||||
bool IsEnabled() const;
|
||||
|
||||
private:
|
||||
|
Reference in New Issue
Block a user