Skip to content

Commit 87f192b

Browse files
filter the sdl joystick number via command line
Signed-off-by: Nicolas Adenis-Lamarre <[email protected]>
1 parent 0a069f3 commit 87f192b

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

SDL/SDLJoystick.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static int SDLJoystickEventHandlerWrapper(void* userdata, SDL_Event* event)
1919
return 0;
2020
}
2121

22-
SDLJoystick::SDLJoystick(bool init_SDL ) : registeredAsEventHandler(false) {
22+
SDLJoystick::SDLJoystick(bool init_SDL, int njoy) : registeredAsEventHandler(false) {
2323
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
2424
if (init_SDL) {
2525
SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
@@ -41,16 +41,19 @@ SDLJoystick::SDLJoystick(bool init_SDL ) : registeredAsEventHandler(false) {
4141
cout << "gamecontrollerdb.txt missing" << endl;
4242
}
4343
cout << "SUCCESS!" << endl;
44-
setUpControllers();
44+
//setUpControllers();
45+
njoy_ = njoy;
46+
4547
}
4648

4749
void SDLJoystick::setUpControllers() {
4850
int numjoys = SDL_NumJoysticks();
49-
for (int i = 0; i < numjoys; i++) {
50-
setUpController(i);
51-
}
52-
if (controllers.size() > 0) {
53-
cout << "pad 1 has been assigned to control pad: " << SDL_GameControllerName(controllers.front()) << endl;
51+
52+
if(njoy_ < numjoys) {
53+
setUpController(njoy_);
54+
if (controllers.size() > 0) {
55+
cout << "pad 1 has been assigned to control pad: " << SDL_JoystickNameForIndex(njoy_) << endl;
56+
}
5457
}
5558
}
5659

@@ -163,7 +166,7 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
163166
KeyInput key;
164167
key.flags = KEY_DOWN;
165168
key.keyCode = code;
166-
key.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex(event.cbutton.which);
169+
key.deviceId = DEVICE_ID_PAD_0;
167170
NativeKey(key);
168171
}
169172
}
@@ -175,7 +178,7 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
175178
KeyInput key;
176179
key.flags = KEY_UP;
177180
key.keyCode = code;
178-
key.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex(event.cbutton.which);
181+
key.deviceId = DEVICE_ID_PAD_0;
179182
NativeKey(key);
180183
}
181184
}
@@ -186,7 +189,7 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
186189
axis.value = event.caxis.value / 32767.0f;
187190
if (axis.value > 1.0f) axis.value = 1.0f;
188191
if (axis.value < -1.0f) axis.value = -1.0f;
189-
axis.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex(event.caxis.which);
192+
axis.deviceId = DEVICE_ID_PAD_0;
190193
NativeAxis(axis);
191194
break;
192195
case SDL_CONTROLLERDEVICEREMOVED:
@@ -200,11 +203,9 @@ void SDLJoystick::ProcessInput(SDL_Event &event){
200203
}
201204
break;
202205
case SDL_CONTROLLERDEVICEADDED:
203-
// for add events, "which" is the device index!
204-
int prevNumControllers = controllers.size();
205-
setUpController(event.cdevice.which);
206-
if (prevNumControllers == 0 && controllers.size() > 0) {
207-
cout << "pad 1 has been assigned to control pad: " << SDL_GameControllerName(controllers.front()) << endl;
206+
if(event.cdevice.which == njoy_) {
207+
setUpController(njoy_);
208+
cout << "pad 1 has been assigned to control pad: " << SDL_JoystickNameForIndex(njoy_) << endl;
208209
}
209210
break;
210211
}

SDL/SDLJoystick.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class SDLJoystick{
1818
public:
19-
SDLJoystick(bool init_SDL = false);
19+
SDLJoystick(bool init_SDL = false, int njoy = 0);
2020
~SDLJoystick();
2121

2222
void registerEventHandler();
@@ -30,4 +30,5 @@ class SDLJoystick{
3030
bool registeredAsEventHandler;
3131
std::vector<SDL_GameController *> controllers;
3232
std::map<int, int> controllerDeviceMap;
33+
int njoy_;
3334
};

SDL/SDLMain.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ int main(int argc, char *argv[]) {
646646
bool set_ipad = false;
647647
float set_dpi = 1.0f;
648648
float set_scale = 1.0f;
649+
int set_njoy = 0;
649650

650651
// Produce a new set of arguments with the ones we skip.
651652
int remain_argc = 1;
@@ -664,6 +665,8 @@ int main(int argc, char *argv[]) {
664665
set_dpi = parseFloat(argv[i]);
665666
else if (set_scale == -2)
666667
set_scale = parseFloat(argv[i]);
668+
else if (set_njoy == -2)
669+
set_njoy = parseInt(argv[i]);
667670
else if (!strcmp(argv[i],"--xres"))
668671
set_xres = -2;
669672
else if (!strcmp(argv[i],"--yres"))
@@ -676,6 +679,8 @@ int main(int argc, char *argv[]) {
676679
set_ipad = true;
677680
else if (!strcmp(argv[i],"--portrait"))
678681
portrait = true;
682+
else if (!strcmp(argv[i],"--njoy"))
683+
set_njoy = -2;
679684
else {
680685
remain_argv[remain_argc++] = argv[i];
681686
}
@@ -908,7 +913,7 @@ int main(int argc, char *argv[]) {
908913
InitSDLAudioDevice();
909914

910915
if (joystick_enabled) {
911-
joystick = new SDLJoystick();
916+
joystick = new SDLJoystick(false, set_njoy);
912917
} else {
913918
joystick = nullptr;
914919
}

0 commit comments

Comments
 (0)