From 31d3596995316bf2b7209fd2494ab2ef40b7f13b Mon Sep 17 00:00:00 2001 From: Alayan <25536748+Alayan-stk-2@users.noreply.github.com> Date: Sun, 29 Dec 2024 19:21:52 +0100 Subject: [PATCH] Improve command-line input device handling - 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 --- src/input/device_manager.cpp | 18 ++++++++++++++++++ src/input/device_manager.hpp | 6 ++---- src/main.cpp | 21 +++++++++++++++++---- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/input/device_manager.cpp b/src/input/device_manager.cpp index 6a423b516cc..189172c9fa2 100644 --- a/src/input/device_manager.cpp +++ b/src/input/device_manager.cpp @@ -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() diff --git a/src/input/device_manager.hpp b/src/input/device_manager.hpp index 126718679d6..b6ca0366b3e 100644 --- a/src/input/device_manager.hpp +++ b/src/input/device_manager.hpp @@ -98,8 +98,6 @@ class DeviceManager: public NoCopy void shutdown(); public: - - DeviceManager(); ~DeviceManager(); @@ -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(); @@ -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); diff --git a/src/main.cpp b/src/main.cpp index 7fa0f567615..c52e014cfc7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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);