Skip to content

Commit

Permalink
Initial attempt at adding SDL's game controller database to PPSSPP.
Browse files Browse the repository at this point in the history
  • Loading branch information
neilmunday committed Sep 17, 2016
1 parent 6d0d36b commit 67425d7
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 118 deletions.
78 changes: 67 additions & 11 deletions SDL/SDLJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,31 @@ SDLJoystick::SDLJoystick(bool init_SDL ): thread(NULL), running(true) {
if (init_SDL)
{
SDL_setenv("SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS", "1", 0);
SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO);
SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
}

if (SDL_GameControllerAddMappingsFromFile("assets/gamecontrollerdb.txt") == -1)
{
printf("Failed to load control pad mappings, please place gamecontrollerdb.txt in your assets directory\n");
}
else {
printf("Loaded game controller mappings for SDL2\n");
}

int numjoys = SDL_NumJoysticks();
int firstController = -1;
SDL_JoystickEventState(SDL_ENABLE);
for (int i = 0; i < numjoys; i++) {
joys.push_back(SDL_JoystickOpen(i));
// printf("Initialized joystick %d: %s",i,SDL_JoystickNameForIndex(i));
if(strstr(SDL_JoystickNameForIndex(i),"PLAYSTATION(R)3 Controller"))
g_Config.bPS3Controller = true;
//printf("Initialized joystick %d: %s\n",i,SDL_JoystickNameForIndex(i));
if (firstController == -1 && SDL_IsGameController(i))
{
firstController = i;
}
}

if (g_Config.bPS3Controller)
fillMappingPS3();
else
fillMapping();



printf("Player 1 will be using joystick %d, %s\n", firstController, SDL_JoystickNameForIndex(firstController));
fillMapping(firstController);
}

SDLJoystick::~SDLJoystick(){
Expand Down Expand Up @@ -185,3 +192,52 @@ void SDLJoystick::runLoop(){
}
}
}

void SDLJoystick::buttonMappingHelper(SDL_GameController* controller, SDL_GameControllerButton button, keycode_t buttonKeyCode)
{
SDL_GameControllerButtonBind bind = SDL_GameControllerGetBindForButton(controller, button);
if (bind.bindType == SDL_CONTROLLER_BINDTYPE_BUTTON)
{
SDLJoyButtonMap[bind.value.button] = buttonKeyCode;
}
}

void SDLJoystick::axisMappingHelper(SDL_GameController* controller, SDL_GameControllerAxis axis, keycode_t buttonKeyCode, AndroidJoystickAxis axisCode)
{
SDL_GameControllerButtonBind bind = SDL_GameControllerGetBindForAxis(controller, axis);
if (bind.bindType == SDL_CONTROLLER_BINDTYPE_BUTTON && buttonKeyCode != NKCODE_UNKNOWN)
{
SDLJoyButtonMap[bind.value.button] = buttonKeyCode;
}
else if (bind.bindType == SDL_CONTROLLER_BINDTYPE_AXIS)
{
SDLJoyAxisMap[bind.value.axis] = axisCode;
}
}

void SDLJoystick::fillMapping(int joyIndex)
{
SDL_GameController *controller = SDL_GameControllerOpen(joyIndex);
printf("control pad mapping: %s\n", SDL_GameControllerMapping(controller));

buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_DPAD_UP, NKCODE_DPAD_UP);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_DPAD_DOWN, NKCODE_DPAD_DOWN);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_DPAD_LEFT, NKCODE_DPAD_LEFT);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, NKCODE_DPAD_RIGHT);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_A, NKCODE_BUTTON_2);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_B, NKCODE_BUTTON_3);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_X, NKCODE_BUTTON_4);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_Y, NKCODE_BUTTON_1);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_START, NKCODE_BUTTON_10);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_BACK, NKCODE_BUTTON_9); // back == select
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, NKCODE_BUTTON_5);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, NKCODE_BUTTON_6);
buttonMappingHelper(controller, SDL_CONTROLLER_BUTTON_GUIDE, NKCODE_BACK); // pause emulator

axisMappingHelper(controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT, NKCODE_BUTTON_7, JOYSTICK_AXIS_LTRIGGER); // L2
axisMappingHelper(controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, NKCODE_BUTTON_8, JOYSTICK_AXIS_RTRIGGER); // R2
axisMappingHelper(controller, SDL_CONTROLLER_AXIS_LEFTX, NKCODE_UNKNOWN, JOYSTICK_AXIS_X);
axisMappingHelper(controller, SDL_CONTROLLER_AXIS_LEFTY, NKCODE_UNKNOWN, JOYSTICK_AXIS_Y);
axisMappingHelper(controller, SDL_CONTROLLER_AXIS_RIGHTX, NKCODE_UNKNOWN, JOYSTICK_AXIS_Z);
axisMappingHelper(controller, SDL_CONTROLLER_AXIS_RIGHTY, NKCODE_UNKNOWN, JOYSTICK_AXIS_RZ);
}
112 changes: 5 additions & 107 deletions SDL/SDLJoystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#ifdef _WIN32
#include "SDL/SDL.h"
#include "SDL/SDL_joystick.h"
#include "SDL/SDL_gamecontroller.h"
#include "SDL/SDL_thread.h"
#else
#include "SDL.h"
#include "SDL_joystick.h"
#include "SDL_gamecontroller.h"
#include "SDL_thread.h"
#endif

Expand All @@ -31,114 +33,10 @@ class SDLJoystick{
private:

void runLoop();
void fillMappingPS3()
{
SDLJoyButtonMap[14] = NKCODE_BUTTON_1; // Cross
SDLJoyButtonMap[13] = NKCODE_BUTTON_2; // Circle
SDLJoyButtonMap[15] = NKCODE_BUTTON_3; // Sqlare
SDLJoyButtonMap[12] = NKCODE_BUTTON_4; // Triangle
SDLJoyButtonMap[10] = NKCODE_BUTTON_5; // L1
SDLJoyButtonMap[11] = NKCODE_BUTTON_6; // R1
SDLJoyButtonMap[8] = NKCODE_BUTTON_7; // L2
SDLJoyButtonMap[9] = NKCODE_BUTTON_8; // R2
SDLJoyButtonMap[0] = NKCODE_BUTTON_9; // Select
SDLJoyButtonMap[3] = NKCODE_BUTTON_10; // Start
SDLJoyButtonMap[1] = NKCODE_BUTTON_11; // L3
SDLJoyButtonMap[2] = NKCODE_BUTTON_12; // R3
SDLJoyButtonMap[16] = NKCODE_BUTTON_13; // PS
SDLJoyButtonMap[4] = NKCODE_DPAD_UP;
SDLJoyButtonMap[6] = NKCODE_DPAD_DOWN;
SDLJoyButtonMap[7] = NKCODE_DPAD_LEFT;
SDLJoyButtonMap[5] = NKCODE_DPAD_RIGHT;
SDLJoyAxisMap[0] = JOYSTICK_AXIS_X;
SDLJoyAxisMap[1] = JOYSTICK_AXIS_Y;
SDLJoyAxisMap[2] = JOYSTICK_AXIS_Z;
SDLJoyAxisMap[3] = JOYSTICK_AXIS_RZ;
}
void fillMapping()
{
//TODO: C++11 aggregate initialization
//would remove runtime overhead completely

// Mapping on Windows
#ifdef _WIN32
SDLJoyButtonMap[0] = NKCODE_BUTTON_2;
SDLJoyButtonMap[1] = NKCODE_BUTTON_3;
SDLJoyButtonMap[2] = NKCODE_BUTTON_4;
SDLJoyButtonMap[3] = NKCODE_BUTTON_1;
SDLJoyButtonMap[4] = NKCODE_BUTTON_7;
SDLJoyButtonMap[5] = NKCODE_BUTTON_8;
SDLJoyButtonMap[6] = NKCODE_BUTTON_9;
SDLJoyButtonMap[7] = NKCODE_BUTTON_10;
SDLJoyButtonMap[8] = NKCODE_BUTTON_11;
SDLJoyButtonMap[9] = NKCODE_BUTTON_12;
SDLJoyButtonMap[10] = NKCODE_BUTTON_5;
SDLJoyButtonMap[11] = NKCODE_BUTTON_6;
SDLJoyButtonMap[12] = NKCODE_BUTTON_7;
SDLJoyButtonMap[13] = NKCODE_BUTTON_8;
SDLJoyButtonMap[14] = NKCODE_BUTTON_9;

SDLJoyAxisMap[0] = JOYSTICK_AXIS_X;
SDLJoyAxisMap[1] = JOYSTICK_AXIS_Y;
SDLJoyAxisMap[2] = JOYSTICK_AXIS_Z;
SDLJoyAxisMap[3] = JOYSTICK_AXIS_RZ;
SDLJoyAxisMap[4] = JOYSTICK_AXIS_LTRIGGER;
SDLJoyAxisMap[5] = JOYSTICK_AXIS_RTRIGGER;
// Mapping on MacOSX to match X360 controller
#elif __APPLE__
SDLJoyButtonMap[0] = NKCODE_DPAD_UP;
SDLJoyButtonMap[1] = NKCODE_DPAD_DOWN;
SDLJoyButtonMap[2] = NKCODE_DPAD_LEFT;
SDLJoyButtonMap[3] = NKCODE_DPAD_RIGHT;
SDLJoyButtonMap[4] = NKCODE_BUTTON_10;
SDLJoyButtonMap[5] = NKCODE_BUTTON_9;
SDLJoyButtonMap[6] = NKCODE_BUTTON_5;
SDLJoyButtonMap[7] = NKCODE_BUTTON_6;
SDLJoyButtonMap[8] = NKCODE_BUTTON_7;
SDLJoyButtonMap[9] = NKCODE_BUTTON_8;
SDLJoyButtonMap[10] = NKCODE_BUTTON_SELECT;
SDLJoyButtonMap[11] = NKCODE_BUTTON_2;
SDLJoyButtonMap[12] = NKCODE_BUTTON_3;
SDLJoyButtonMap[13] = NKCODE_BUTTON_4;
SDLJoyButtonMap[14] = NKCODE_BUTTON_1;
SDLJoyButtonMap[15] = NKCODE_BUTTON_11;
SDLJoyButtonMap[16] = NKCODE_BUTTON_12;

SDLJoyAxisMap[0] = JOYSTICK_AXIS_X;
SDLJoyAxisMap[1] = JOYSTICK_AXIS_Y;
SDLJoyAxisMap[2] = JOYSTICK_AXIS_Z;
SDLJoyAxisMap[3] = JOYSTICK_AXIS_RZ;
SDLJoyAxisMap[4] = JOYSTICK_AXIS_LTRIGGER;
SDLJoyAxisMap[5] = JOYSTICK_AXIS_RTRIGGER;
// Mapping on Linux/Android/Other to match X360 and wii-u pro controller
#else
SDLJoyButtonMap[0] = NKCODE_BUTTON_1;
SDLJoyButtonMap[1] = NKCODE_BUTTON_2;
SDLJoyButtonMap[2] = NKCODE_BUTTON_3;
SDLJoyButtonMap[3] = NKCODE_BUTTON_4;
SDLJoyButtonMap[4] = NKCODE_BUTTON_5;
SDLJoyButtonMap[5] = NKCODE_BUTTON_6;
SDLJoyButtonMap[6] = NKCODE_BUTTON_7;
SDLJoyButtonMap[7] = NKCODE_BUTTON_8;
SDLJoyButtonMap[8] = NKCODE_BUTTON_9;
SDLJoyButtonMap[9] = NKCODE_BUTTON_10;
SDLJoyButtonMap[10] = NKCODE_BUTTON_11;
SDLJoyButtonMap[11] = NKCODE_BUTTON_12;
SDLJoyButtonMap[12] = NKCODE_BUTTON_13;
SDLJoyButtonMap[13] = NKCODE_DPAD_UP;
SDLJoyButtonMap[14] = NKCODE_DPAD_DOWN;
SDLJoyButtonMap[15] = NKCODE_DPAD_LEFT;
SDLJoyButtonMap[16] = NKCODE_DPAD_RIGHT;
void fillMapping(int joyIndex);
void axisMappingHelper(SDL_GameController* controller, SDL_GameControllerAxis axis, keycode_t buttonKeyCode, AndroidJoystickAxis axisCode);
void buttonMappingHelper(SDL_GameController* controller, SDL_GameControllerButton button, keycode_t buttonKeyCode);

SDLJoyAxisMap[0] = JOYSTICK_AXIS_X;
SDLJoyAxisMap[1] = JOYSTICK_AXIS_Y;
SDLJoyAxisMap[2] = JOYSTICK_AXIS_Z;
SDLJoyAxisMap[3] = JOYSTICK_AXIS_RZ;
SDLJoyAxisMap[4] = JOYSTICK_AXIS_LTRIGGER;
SDLJoyAxisMap[5] = JOYSTICK_AXIS_RTRIGGER;

#endif
}
std::map<int, int> SDLJoyButtonMap;
std::map<int, int> SDLJoyAxisMap;

Expand Down
Loading

0 comments on commit 67425d7

Please sign in to comment.