Allow up to 16 gamepads with Sunshine hosts

This commit is contained in:
Cameron Gutman 2023-06-30 20:55:20 -05:00
parent 5bb47ce8b6
commit 0169cedb27
2 changed files with 12 additions and 6 deletions

View File

@ -105,6 +105,9 @@ static const int hat_constants[3][3] = {{HAT_UP | HAT_LEFT, HAT_UP, HAT_UP | HAT
// Determines the maximum motion amount before allowing movement // Determines the maximum motion amount before allowing movement
#define MOUSE_EMULATION_DEADZONE 2 #define MOUSE_EMULATION_DEADZONE 2
// Limited by number of bits in activeGamepadMask
#define MAX_GAMEPADS 16
static struct input_device* devices = NULL; static struct input_device* devices = NULL;
static int numDevices = 0; static int numDevices = 0;
static int assignedControllerIds = 0; static int assignedControllerIds = 0;
@ -283,7 +286,7 @@ static bool evdev_handle_event(struct input_event *ev, struct input_device *dev)
} }
if (dev->gamepadModified) { if (dev->gamepadModified) {
if (dev->controllerId < 0) { if (dev->controllerId < 0) {
for (int i = 0; i < 4; i++) { for (int i = 0; i < MAX_GAMEPADS; i++) {
if ((assignedControllerIds & (1 << i)) == 0) { if ((assignedControllerIds & (1 << i)) == 0) {
assignedControllerIds |= (1 << i); assignedControllerIds |= (1 << i);
dev->controllerId = i; dev->controllerId = i;

View File

@ -43,7 +43,10 @@ typedef struct _GAMEPAD_STATE {
bool initialized; bool initialized;
} GAMEPAD_STATE, *PGAMEPAD_STATE; } GAMEPAD_STATE, *PGAMEPAD_STATE;
static GAMEPAD_STATE gamepads[4]; // Limited by number of bits in activeGamepadMask
#define MAX_GAMEPADS 16
static GAMEPAD_STATE gamepads[MAX_GAMEPADS];
static int keyboard_modifiers; static int keyboard_modifiers;
static int activeGamepadMask = 0; static int activeGamepadMask = 0;
@ -52,7 +55,7 @@ int sdl_gamepads = 0;
static PGAMEPAD_STATE get_gamepad(SDL_JoystickID sdl_id, bool add) { static PGAMEPAD_STATE get_gamepad(SDL_JoystickID sdl_id, bool add) {
// See if a gamepad already exists // See if a gamepad already exists
for (int i = 0;i<4;i++) { for (int i = 0;i<MAX_GAMEPADS;i++) {
if (gamepads[i].initialized && gamepads[i].sdl_id == sdl_id) if (gamepads[i].initialized && gamepads[i].sdl_id == sdl_id)
return &gamepads[i]; return &gamepads[i];
} }
@ -60,7 +63,7 @@ static PGAMEPAD_STATE get_gamepad(SDL_JoystickID sdl_id, bool add) {
if (!add) if (!add)
return NULL; return NULL;
for (int i = 0;i<4;i++) { for (int i = 0;i<MAX_GAMEPADS;i++) {
if (!gamepads[i].initialized) { if (!gamepads[i].initialized) {
gamepads[i].sdl_id = sdl_id; gamepads[i].sdl_id = sdl_id;
gamepads[i].id = i; gamepads[i].id = i;
@ -109,7 +112,7 @@ static void add_gamepad(int joystick_index) {
} }
static void remove_gamepad(SDL_JoystickID sdl_id) { static void remove_gamepad(SDL_JoystickID sdl_id) {
for (int i = 0;i<4;i++) { for (int i = 0;i<MAX_GAMEPADS;i++) {
if (gamepads[i].initialized && gamepads[i].sdl_id == sdl_id) { if (gamepads[i].initialized && gamepads[i].sdl_id == sdl_id) {
#if !SDL_VERSION_ATLEAST(2, 0, 9) #if !SDL_VERSION_ATLEAST(2, 0, 9)
if (gamepads[i].haptic_effect_id >= 0) { if (gamepads[i].haptic_effect_id >= 0) {
@ -353,7 +356,7 @@ int sdlinput_handle_event(SDL_Window* window, SDL_Event* event) {
} }
void sdlinput_rumble(unsigned short controller_id, unsigned short low_freq_motor, unsigned short high_freq_motor) { void sdlinput_rumble(unsigned short controller_id, unsigned short low_freq_motor, unsigned short high_freq_motor) {
if (controller_id >= 4) if (controller_id >= MAX_GAMEPADS)
return; return;
PGAMEPAD_STATE state = &gamepads[controller_id]; PGAMEPAD_STATE state = &gamepads[controller_id];