From 6df768fa5f8535bcf28c000e98be429979bfd0ba Mon Sep 17 00:00:00 2001 From: Erik Hofman Date: Mon, 27 Jan 2020 18:12:09 +0100 Subject: [PATCH] Store the initial axis values and if just one of them changes declare the joystick initialized --- src/Input/FGJoystickInput.cxx | 30 ++++++++++++++++++------------ src/Input/FGJoystickInput.hxx | 4 +++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Input/FGJoystickInput.cxx b/src/Input/FGJoystickInput.cxx index 2f1c21fc6..c35228ef2 100644 --- a/src/Input/FGJoystickInput.cxx +++ b/src/Input/FGJoystickInput.cxx @@ -120,7 +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; + joysticks[i].initializing = true; if (js->notWorking()) { SG_LOG(SG_INPUT, SG_DEBUG, "Joystick " << i << " not found"); @@ -341,31 +341,37 @@ void FGJoystickInput::updateJoystick(int index, FGJoystickInput::joystick* joy, jsJoystick * js = joy->plibJS.get(); if (js == 0 || js->notWorking()) { - initializing[index] = true; + joysticks[index].initializing = true; if (js) { - jsJoystick * js = new jsJoystick(index); - joysticks[index].plibJS.reset(js); + joysticks[index].plibJS.reset( new jsJoystick(index) ); } return; } js->read(&buttons, axis_values); if (js->notWorking()) { // If js is disconnected - initializing[index] = true; + joysticks[index].initializing = true; return; } // Joystick axes can get initialized to extreme values, at least on Linux. + // Wait until one of the axes get a different value before continuing. // https://sourceforge.net/p/flightgear/codetickets/2185/ - if (initializing[index]) { - float axis_values[MAX_JOYSTICK_AXES]; - int buttons; + if (joysticks[index].initializing) { - js->rawRead(&buttons, axis_values); - for (int j = 0; j < joy->naxes; j++) { - if (fabsf(axis_values[j]) > js->getSaturation(j)) return; + if (!joysticks[index].initialized) { + js->read(NULL, joysticks[index].values); + joysticks[index].initialized = true; } - initializing[index] = false; + + int j; + for (j = 0; j < joy->naxes; j++) { + if (axis_values[j] != joysticks[index].values[j]) break; + } + if (j == joy->naxes) { + return; + } + joysticks[index].initializing = false; } // Update device status diff --git a/src/Input/FGJoystickInput.hxx b/src/Input/FGJoystickInput.hxx index 8a6f425c4..247225575 100644 --- a/src/Input/FGJoystickInput.hxx +++ b/src/Input/FGJoystickInput.hxx @@ -101,11 +101,13 @@ private: axis * axes; FGButton * buttons; bool predefined; + bool initializing = true; + bool initialized = false; + float values[MAX_JOYSTICK_AXES]; void clearAxesAndButtons(); }; - bool initializing[MAX_JOYSTICKS]; joystick joysticks[MAX_JOYSTICKS]; void updateJoystick(int index, joystick* joy, double dt); };