Skip to content

An OpenMW mod that lets you play the whole Elder Scrolls III: Morrowind as a top-down isometric game — with full online co-op or MMO modes.

C++

Controller: a Bluetooth Xbox pad works with Sunshine's virtual one still there

Alan Saunders364cdb4cca13
diff --git a/apps/openmw/mwinput/bindingsmanager.cpp b/apps/openmw/mwinput/bindingsmanager.cpp
index a4c3f3e..f35d727 100644
--- a/apps/openmw/mwinput/bindingsmanager.cpp
+++ b/apps/openmw/mwinput/bindingsmanager.cpp
@@ -684,8 +684,8 @@ namespace MWInput
const auto& controllers = mInputBinder->getJoystickInstanceMap();
if (controllers.empty())
return nullptr;
- else
- return controllers.begin()->second;
+ const auto used = controllers.find(mControllerUsed);
+ return used != controllers.end() ? used->second : controllers.begin()->second;
}
void BindingsManager::actionValueChanged(int action, float currentValue, float previousValue)
diff --git a/apps/openmw/mwinput/bindingsmanager.hpp b/apps/openmw/mwinput/bindingsmanager.hpp
index 40c2076..96dc581 100644
--- a/apps/openmw/mwinput/bindingsmanager.hpp
+++ b/apps/openmw/mwinput/bindingsmanager.hpp
@@ -45,7 +45,11 @@ namespace MWInput
bool actionIsActive(int id) const;
float getActionValue(int id) const; // returns value in range [0, 1]
+ // Isowind: the controller used last (pressed or pushed), or the first one opened until one is: its sticks,
+ // held buttons and type are what gets read. An idle one, as a stream leaves behind, would hide the rest.
SDL_GameController* getControllerOrNull() const;
+ SDL_JoystickID getControllerUsed() const { return mControllerUsed; }
+ void setControllerUsed(SDL_JoystickID instanceID) { mControllerUsed = instanceID; }
void mousePressed(const SDL_MouseButtonEvent& evt, Uint8 deviceID);
void mouseReleased(const SDL_MouseButtonEvent& arg, Uint8 deviceID);
@@ -76,6 +80,8 @@ namespace MWInput
std::filesystem::path mUserFile;
bool mDragDrop;
+
+ SDL_JoystickID mControllerUsed = -1;
};
}
#endif
diff --git a/apps/openmw/mwinput/controllermanager.cpp b/apps/openmw/mwinput/controllermanager.cpp
index d9dd036..c8cd61e 100644
--- a/apps/openmw/mwinput/controllermanager.cpp
+++ b/apps/openmw/mwinput/controllermanager.cpp
@@ -31,6 +31,10 @@ namespace MWInput
constexpr std::chrono::milliseconds sGuiRepeatDelay(400);
constexpr std::chrono::milliseconds sGuiRepeatInterval(100);
+ // Isowind: how far a stick or trigger has to move (of 32767) to make its controller the one read, so a resting
+ // stick's drift doesn't
+ constexpr int sControllerUsedAxisValue = 8000;
+
bool isDpadButton(Uint8 button)
{
return button == SDL_CONTROLLER_BUTTON_DPAD_UP || button == SDL_CONTROLLER_BUTTON_DPAD_DOWN
@@ -146,6 +150,7 @@ namespace MWInput
{
// Isowind: another button pressed stops a D-pad direction repeating
mGuiRepeatButton = SDL_CONTROLLER_BUTTON_INVALID;
+ useController(arg.which);
if (!Settings::input().mEnableController || mBindingsManager->isDetectingBindingState())
return;
@@ -243,6 +248,9 @@ namespace MWInput
void ControllerManager::axisMoved(int deviceID, const SDL_ControllerAxisEvent& arg)
{
+ if (std::abs(arg.value) > sControllerUsedAxisValue)
+ useController(arg.which);
+
if (mBindingsManager->isDetectingBindingState())
{
mBindingsManager->controllerAxisMoved(deviceID, arg);
@@ -598,6 +606,14 @@ namespace MWInput
mGyroAvailable = true;
}
+ void ControllerManager::useController(SDL_JoystickID instanceID)
+ {
+ if (mBindingsManager->getControllerUsed() == instanceID)
+ return;
+ mBindingsManager->setControllerUsed(instanceID);
+ enableGyroSensor();
+ }
+
bool ControllerManager::isGyroAvailable() const
{
return mGyroAvailable;
diff --git a/apps/openmw/mwinput/controllermanager.hpp b/apps/openmw/mwinput/controllermanager.hpp
index e46f055..17044ad 100644
--- a/apps/openmw/mwinput/controllermanager.hpp
+++ b/apps/openmw/mwinput/controllermanager.hpp
@@ -68,6 +68,9 @@ namespace MWInput
void enableGyroSensor();
+ // Isowind: the controller a button or stick event came from is the one read from now on
+ void useController(SDL_JoystickID instanceID);
+
int getControllerType();
BindingsManager* mBindingsManager;
diff --git a/isowind/linux/isowind.sh b/isowind/linux/isowind.sh
index 847a7f3..a24ccef 100755
--- a/isowind/linux/isowind.sh
+++ b/isowind/linux/isowind.sh
@@ -158,6 +158,9 @@ play() {
if ls /dev/input/js* >/dev/null 2>&1; then MENUS=true; else MENUS=false; fi
fi
set_setting "$ROOT/profiles/$LOOK/settings.cfg" GUI "controller menus" "$MENUS"
+ # Xbox controllers through the kernel's driver, not SDL's own: SDL's skips one it can't open (a Bluetooth one,
+ # whose hidraw only root reads) when it has another with the same ids (Sunshine's virtual one, left after a stream)
+ export SDL_JOYSTICK_HIDAPI_XBOX=${SDL_JOYSTICK_HIDAPI_XBOX:-0}
exec "$ENGINE/openmw" --config "$ROOT/profiles/$LOOK" $CONNECT
}