Basic gamepad support through SDL (#407)

* Add basic gamepad support through SDL

* lightbar, vibration, code style changes

* okay fine

* one day clang format will finally pass
This commit is contained in:
counter185 2024-08-13 11:54:08 +02:00 committed by GitHub
parent d1a033b6af
commit bb159eafb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 128 additions and 4 deletions

View file

@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <SDL3/SDL.h>
#include "core/libraries/kernel/time_management.h"
#include "core/libraries/pad/pad.h"
#include "input/controller.h"
@ -117,4 +118,29 @@ void GameController::Axis(int id, Input::Axis axis, int value) {
AddState(state);
}
void GameController::SetLightBarRGB(u8 r, u8 g, u8 b) {
if (m_sdl_gamepad != nullptr) {
SDL_SetGamepadLED(m_sdl_gamepad, r, g, b);
}
}
bool GameController::SetVibration(u8 smallMotor, u8 largeMotor) {
if (m_sdl_gamepad != nullptr) {
return SDL_RumbleGamepad(m_sdl_gamepad, (smallMotor / 255.0f) * 0xFFFF,
(largeMotor / 255.0f) * 0xFFFF, -1) == 0;
}
return true;
}
void GameController::TryOpenSDLController() {
if (m_sdl_gamepad == nullptr || !SDL_GamepadConnected(m_sdl_gamepad)) {
int gamepad_count;
SDL_JoystickID* gamepads = SDL_GetGamepads(&gamepad_count);
m_sdl_gamepad = gamepad_count > 0 ? SDL_OpenGamepad(gamepads[0]) : nullptr;
SDL_free(gamepads);
}
SetLightBarRGB(0, 0, 255);
}
} // namespace Input

View file

@ -6,6 +6,8 @@
#include <mutex>
#include "common/types.h"
struct SDL_Gamepad;
namespace Input {
enum class Axis {
@ -43,6 +45,9 @@ public:
void CheckButton(int id, u32 button, bool isPressed);
void AddState(const State& state);
void Axis(int id, Input::Axis axis, int value);
void SetLightBarRGB(u8 r, u8 g, u8 b);
bool SetVibration(u8 smallMotor, u8 largeMotor);
void TryOpenSDLController();
private:
struct StateInternal {
@ -57,6 +62,8 @@ private:
u32 m_first_state = 0;
std::array<State, MAX_STATES> m_states;
std::array<StateInternal, MAX_STATES> m_private;
SDL_Gamepad* m_sdl_gamepad = nullptr;
};
} // namespace Input