SWITCH: properly implement multiple joysticks support

This commit is contained in:
Cpasjuste 2018-03-22 09:56:39 +01:00 committed by Dave Murphy
parent fd0204cc42
commit 1a52258796
2 changed files with 53 additions and 27 deletions

View File

@ -29,7 +29,7 @@
#include <switch.h>
#define JOYSTICK_COUNT 9
#define JOYSTICK_COUNT 8
typedef struct JoystickState
{
@ -43,22 +43,23 @@ typedef struct JoystickState
static JoystickState pad[JOYSTICK_COUNT];
static HidControllerID pad_id[JOYSTICK_COUNT] = {
CONTROLLER_P1_AUTO, CONTROLLER_PLAYER_1, CONTROLLER_PLAYER_2,
CONTROLLER_PLAYER_3, CONTROLLER_PLAYER_4, CONTROLLER_PLAYER_5,
CONTROLLER_PLAYER_6, CONTROLLER_PLAYER_7, CONTROLLER_PLAYER_8
CONTROLLER_PLAYER_1, CONTROLLER_PLAYER_2,
CONTROLLER_PLAYER_3, CONTROLLER_PLAYER_4,
CONTROLLER_PLAYER_5, CONTROLLER_PLAYER_6,
CONTROLLER_PLAYER_7, CONTROLLER_PLAYER_8
};
static const HidControllerKeys pad_mapping[] = {
KEY_A, KEY_B, KEY_X, KEY_Y, KEY_LSTICK, KEY_RSTICK,
KEY_L, KEY_R, KEY_ZL, KEY_ZR, KEY_PLUS, KEY_MINUS,
KEY_A, KEY_B, KEY_X, KEY_Y,
KEY_LSTICK, KEY_RSTICK,
KEY_L, KEY_R,
KEY_ZL, KEY_ZR,
KEY_PLUS, KEY_MINUS,
KEY_DLEFT, KEY_DUP, KEY_DRIGHT, KEY_DDOWN,
KEY_LSTICK_LEFT, KEY_LSTICK_UP, KEY_LSTICK_RIGHT, KEY_LSTICK_DOWN,
KEY_RSTICK_LEFT, KEY_RSTICK_UP, KEY_RSTICK_RIGHT, KEY_RSTICK_DOWN,
KEY_SL, KEY_SR, KEY_TOUCH
KEY_RSTICK_LEFT, KEY_RSTICK_UP, KEY_RSTICK_RIGHT, KEY_RSTICK_DOWN
};
static int SDL_numjoysticks = JOYSTICK_COUNT;
/* Function to scan the system for joysticks.
* It should return 0, or -1 on an unrecoverable fatal error.
*/
@ -67,21 +68,23 @@ SDL_SYS_JoystickInit(void)
{
for (int i = 0; i < JOYSTICK_COUNT; i++) {
pad[i].id = pad_id[i];
hidSetNpadJoyAssignmentModeSingleByDefault(pad[i].id);
}
return SDL_numjoysticks;
return JOYSTICK_COUNT;
}
int
SDL_SYS_NumJoysticks(void)
{
return SDL_numjoysticks;
return JOYSTICK_COUNT;
}
void
SDL_SYS_JoystickDetect(void)
{
// TODO: handle joysticks change
pad[0].id = hidGetHandheldMode() ?
CONTROLLER_HANDHELD : CONTROLLER_PLAYER_1;
}
/* Function to get the device-dependent name of a joystick */
@ -105,7 +108,7 @@ SDL_SYS_JoystickNameForDeviceIndex(int device_index)
if (device_index == 8)
return "Switch Controller #8";
return "Switch Controller #0 (Auto)";
return "Switch Controller #0";
}
/* Function to perform the mapping from device index to the instance id for this index */
@ -175,13 +178,13 @@ SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
}
// Buttons
changed = pad_old[index].buttons ^pad[index].buttons;
changed = pad_old[index].buttons ^ pad[index].buttons;
pad_old[index].buttons = pad[index].buttons;
if (changed) {
for (int i = 0; i < sizeof(pad_mapping) / sizeof(*pad_mapping); i++) {
for (int i = 0; i < joystick->nbuttons; i++) {
if (changed & pad_mapping[i]) {
SDL_PrivateJoystickButton(
joystick, (Uint8) BIT(i),
joystick, (Uint8) i,
(Uint8) ((pad[index].buttons & pad_mapping[i]) ? SDL_PRESSED : SDL_RELEASED));
}
}
@ -198,6 +201,9 @@ SDL_SYS_JoystickClose(SDL_Joystick *joystick)
void
SDL_SYS_JoystickQuit(void)
{
for (int i = 0; i < JOYSTICK_COUNT; i++) {
hidSetNpadJoyAssignmentModeDual(pad[i].id);
}
}
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)

View File

@ -14,7 +14,6 @@
#include <stdio.h>
#include <switch.h>
#include <render/SDL_sysrender.h>
#include <video/SDL_sysvideo.h>
#include "SDL2/SDL.h"
@ -53,21 +52,42 @@ int main(int argc, char *argv[])
return -1;
}
// open CONTROLLER_P1_AUTO
// open CONTROLLER_PLAYER_1 and CONTROLLER_PLAYER_2
// when connected, both joycons are mapped to joystick #0,
// else joycons are individually mapped to joystick #0, joystick #1, ...
// https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L45
if (SDL_JoystickOpen(0) == NULL) {
printf("SDL_JoystickOpen: %s\n", SDL_GetError());
SDL_Quit();
return -1;
for (int i = 0; i < 2; i++) {
if (SDL_JoystickOpen(i) == NULL) {
printf("SDL_JoystickOpen: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
}
while (!done) {
while (SDL_PollEvent(&event)) {
// seek for (B) button press
// https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L51
if (event.type == SDL_JOYBUTTONDOWN && event.jbutton.button == KEY_B) {
done = 1;
switch (event.type) {
case SDL_JOYAXISMOTION:
printf("Joystick %d axis %d value: %d\n",
event.jaxis.which,
event.jaxis.axis, event.jaxis.value);
break;
case SDL_JOYBUTTONDOWN:
printf("Joystick %d button %d down\n",
event.jbutton.which, event.jbutton.button);
// seek for joystick #0 down (B)
// https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L51
if (event.jbutton.which == 0 && event.jbutton.button == 1) {
done = 1;
}
break;
default:
break;
}
}