Skip to content

Commit

Permalink
Improve command-line input device handling
Browse files Browse the repository at this point in the history
- Fallback to the first keyboard config instead of crashing if the requested device doesn't exist
- Automatically enable the requested config if it's available but disabled
  • Loading branch information
Alayan-stk-2 committed Dec 29, 2024
1 parent 53ff6e6 commit 31d3596
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/input/device_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,24 @@ KeyboardDevice* DeviceManager::getKeyboardFromBtnID(const int button_id)
return NULL;
} // getKeyboardFromButtonID

// -----------------------------------------------------------------------------
GamePadDevice* DeviceManager::getGamePad(const int i)
{
if (i>= 0 && i < (int)m_gamepads.size())
return m_gamepads.get(i);
else
return NULL;
}

// -----------------------------------------------------------------------------
KeyboardDevice* DeviceManager::getKeyboard(const int i)
{
if (i >= 0 && i < (int)m_keyboards.size())
return m_keyboards.get(i);
else
return NULL;
}

// -----------------------------------------------------------------------------

void DeviceManager::shutdown()
Expand Down
6 changes: 2 additions & 4 deletions src/input/device_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ class DeviceManager: public NoCopy
void shutdown();

public:


DeviceManager();
~DeviceManager();

Expand All @@ -111,7 +109,7 @@ class DeviceManager: public NoCopy
void addGamepad(GamePadDevice* d);
int getGamePadAmount() const { return m_gamepads.size(); }
int getGamePadConfigAmount() const { return m_gamepad_configs.size(); }
GamePadDevice* getGamePad(const int i) { return m_gamepads.get(i); }
GamePadDevice* getGamePad(const int i);
GamepadConfig* getGamepadConfig(const int i) { return m_gamepad_configs.get(i); }
GamePadDevice* getGamePadFromIrrID(const int i);
void clearGamepads();
Expand All @@ -135,7 +133,7 @@ class DeviceManager: public NoCopy
return active;
}
int getKeyboardConfigAmount() const { return m_keyboard_configs.size(); }
KeyboardDevice* getKeyboard(const int i) { return m_keyboards.get(i); }
KeyboardDevice* getKeyboard(const int i);
KeyboardConfig* getKeyboardConfig(const int i) { return m_keyboard_configs.get(i); }
KeyboardDevice* getKeyboardFromBtnID(const int btnID);

Expand Down
21 changes: 17 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,22 +514,35 @@ void setupRaceStart()
// a current player
PlayerManager::get()->enforceCurrentPlayer();

InputDevice *device;
InputDevice *device = NULL;

// Assign the player a device; check the command line params for preferences; by default use keyboard 0

if(UserConfigParams::m_default_keyboard > -1) {
if(UserConfigParams::m_default_keyboard > -1)
{
device = input_manager->getDeviceManager()->getKeyboard(UserConfigParams::m_default_keyboard);
}
else if(UserConfigParams::m_default_gamepad > -1) {
else if(UserConfigParams::m_default_gamepad > -1)
{
// getGamePad(int) returns a GamePadDevice which is a subclass of InputDevice
// However, the compiler doesn't like it so it has to be manually casted in
device = (InputDevice *) input_manager->getDeviceManager()->getGamePad(UserConfigParams::m_default_gamepad);
}
else {
// If no config requested or if the requested config doesn't exist
if (device == NULL)
{
if (UserConfigParams::m_default_keyboard > -1 ||
UserConfigParams::m_default_gamepad > -1)
{
Log::error("main", "Requested input device unavailable, fallback to the default keyboard");
}
device = input_manager->getDeviceManager()->getKeyboard(0);
}

// In case the requested config was disabled, enable it.
if (!device->getConfiguration()->isEnabled())
device->getConfiguration()->setEnabled(true);

// Create player and associate player with device
StateManager::get()->createActivePlayer(
PlayerManager::get()->getPlayer(0), device);
Expand Down

0 comments on commit 31d3596

Please sign in to comment.