Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter the sdl joystick number via command line #17503

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this comments out all calls to setUpControllers(), but also modifies it. That's confusing.

Git remembers code forever, so if we don't need this anymore the line should be removed and so should the entire method.

-[Unknown]

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So for anyone who is not using your new parameter, and who has multiple controller devices, such as probably the author of #8993, this will break their use of PPSSPP.

Wouldn't it be better if your use case was only activated when your parameter was passed?

-[Unknown]

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