Skip to content

Commit

Permalink
filter the sdl joystick number via command line
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Adenis-Lamarre <[email protected]>
  • Loading branch information
nadenislamarre committed May 25, 2023
1 parent 0a069f3 commit 87f192b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
31 changes: 16 additions & 15 deletions SDL/SDLJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static int SDLJoystickEventHandlerWrapper(void* userdata, SDL_Event* event)
return 0;
}

SDLJoystick::SDLJoystick(bool init_SDL ) : registeredAsEventHandler(false) {
SDLJoystick::SDLJoystick(bool init_SDL, int njoy) : registeredAsEventHandler(false) {
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
if (init_SDL) {
SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
Expand All @@ -41,16 +41,19 @@ SDLJoystick::SDLJoystick(bool init_SDL ) : registeredAsEventHandler(false) {
cout << "gamecontrollerdb.txt missing" << endl;
}
cout << "SUCCESS!" << endl;
setUpControllers();
//setUpControllers();
njoy_ = njoy;

}

void SDLJoystick::setUpControllers() {
int numjoys = SDL_NumJoysticks();
for (int i = 0; i < numjoys; i++) {
setUpController(i);
}
if (controllers.size() > 0) {
cout << "pad 1 has been assigned to control pad: " << SDL_GameControllerName(controllers.front()) << endl;

if(njoy_ < numjoys) {
setUpController(njoy_);
if (controllers.size() > 0) {
cout << "pad 1 has been assigned to control pad: " << SDL_JoystickNameForIndex(njoy_) << endl;
}
}
}

Expand Down Expand Up @@ -163,7 +166,7 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
KeyInput key;
key.flags = KEY_DOWN;
key.keyCode = code;
key.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex(event.cbutton.which);
key.deviceId = DEVICE_ID_PAD_0;
NativeKey(key);
}
}
Expand All @@ -175,7 +178,7 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
KeyInput key;
key.flags = KEY_UP;
key.keyCode = code;
key.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex(event.cbutton.which);
key.deviceId = DEVICE_ID_PAD_0;
NativeKey(key);
}
}
Expand All @@ -186,7 +189,7 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
axis.value = event.caxis.value / 32767.0f;
if (axis.value > 1.0f) axis.value = 1.0f;
if (axis.value < -1.0f) axis.value = -1.0f;
axis.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex(event.caxis.which);
axis.deviceId = DEVICE_ID_PAD_0;
NativeAxis(axis);
break;
case SDL_CONTROLLERDEVICEREMOVED:
Expand All @@ -200,11 +203,9 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
}
break;
case SDL_CONTROLLERDEVICEADDED:
// for add events, "which" is the device index!
int prevNumControllers = controllers.size();
setUpController(event.cdevice.which);
if (prevNumControllers == 0 && controllers.size() > 0) {
cout << "pad 1 has been assigned to control pad: " << SDL_GameControllerName(controllers.front()) << endl;
if(event.cdevice.which == njoy_) {
setUpController(njoy_);
cout << "pad 1 has been assigned to control pad: " << SDL_JoystickNameForIndex(njoy_) << endl;
}
break;
}
Expand Down
3 changes: 2 additions & 1 deletion SDL/SDLJoystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class SDLJoystick{
public:
SDLJoystick(bool init_SDL = false);
SDLJoystick(bool init_SDL = false, int njoy = 0);
~SDLJoystick();

void registerEventHandler();
Expand All @@ -30,4 +30,5 @@ class SDLJoystick{
bool registeredAsEventHandler;
std::vector<SDL_GameController *> controllers;
std::map<int, int> controllerDeviceMap;
int njoy_;
};
7 changes: 6 additions & 1 deletion SDL/SDLMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ int main(int argc, char *argv[]) {
bool set_ipad = false;
float set_dpi = 1.0f;
float set_scale = 1.0f;
int set_njoy = 0;

// Produce a new set of arguments with the ones we skip.
int remain_argc = 1;
Expand All @@ -664,6 +665,8 @@ int main(int argc, char *argv[]) {
set_dpi = parseFloat(argv[i]);
else if (set_scale == -2)
set_scale = parseFloat(argv[i]);
else if (set_njoy == -2)
set_njoy = parseInt(argv[i]);
else if (!strcmp(argv[i],"--xres"))
set_xres = -2;
else if (!strcmp(argv[i],"--yres"))
Expand All @@ -676,6 +679,8 @@ int main(int argc, char *argv[]) {
set_ipad = true;
else if (!strcmp(argv[i],"--portrait"))
portrait = true;
else if (!strcmp(argv[i],"--njoy"))
set_njoy = -2;
else {
remain_argv[remain_argc++] = argv[i];
}
Expand Down Expand Up @@ -908,7 +913,7 @@ int main(int argc, char *argv[]) {
InitSDLAudioDevice();

if (joystick_enabled) {
joystick = new SDLJoystick();
joystick = new SDLJoystick(false, set_njoy);
} else {
joystick = nullptr;
}
Expand Down

0 comments on commit 87f192b

Please sign in to comment.