From 3405ea2aaac1e4be21b5d50d50cfaa72205938f1 Mon Sep 17 00:00:00 2001 From: Erik Hofman Date: Sun, 26 Jan 2020 14:10:07 +0100 Subject: [PATCH] Joystick axes can get initialized to extreme values, at least on Linux. Hold of working with the axis values until all values have become sane at least once --- src/Input/FGJoystickInput.cxx | 20 ++++++++++++++++++-- src/Input/FGJoystickInput.hxx | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Input/FGJoystickInput.cxx b/src/Input/FGJoystickInput.cxx index c161d449c..545e77a54 100644 --- a/src/Input/FGJoystickInput.cxx +++ b/src/Input/FGJoystickInput.cxx @@ -30,6 +30,8 @@ # include #endif +#include + #include #include "FGDeviceConfigurationMap.hxx" #include
@@ -118,6 +120,7 @@ void FGJoystickInput::init() for (int i = 0; i < MAX_JOYSTICKS; i++) { jsJoystick * js = new jsJoystick(i); joysticks[i].plibJS.reset(js); + initializing[i] = true; if (js->notWorking()) { SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found"); @@ -337,12 +340,25 @@ void FGJoystickInput::updateJoystick(int index, FGJoystickInput::joystick* joy, float delay; jsJoystick * js = joy->plibJS.get(); - if (js == 0 || js->notWorking()) + if (js == 0 || js->notWorking()) { + initializing[index] = true; return; + } js->read(&buttons, axis_values); - if (js->notWorking()) // If js is disconnected + if (js->notWorking()) { // If js is disconnected + initializing[index] = true; return; + } + + // Joystick axes can get initialized to extreme values, at least on Linux. + // https://sourceforge.net/p/flightgear/codetickets/2185/ + if (initializing[index]) { + for (int j = 0; j < joy->naxes; j++) { + if (fabsf(axis_values[j] > 0.5f)) return; + } + initializing[index] = false; + } // Update device status SGPropertyNode_ptr status = status_node->getChild("joystick", index, true); diff --git a/src/Input/FGJoystickInput.hxx b/src/Input/FGJoystickInput.hxx index 642950d6f..8a6f425c4 100644 --- a/src/Input/FGJoystickInput.hxx +++ b/src/Input/FGJoystickInput.hxx @@ -105,6 +105,7 @@ private: void clearAxesAndButtons(); }; + bool initializing[MAX_JOYSTICKS]; joystick joysticks[MAX_JOYSTICKS]; void updateJoystick(int index, joystick* joy, double dt); };