From 4f7d84ed7373c87690d1b81d4fdfb7e409542616 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 18 Dec 2020 12:17:42 +0000 Subject: [PATCH] Avoid crash when OSG event context is nil Seems to relate to certain window managers and focus policies. Ticket-Id: https://sourceforge.net/p/flightgear/codetickets/2218/ Sentry-Id: FLIGHTGEAR-30J --- src/Input/FGMouseInput.cxx | 12 ++++++++++-- src/Viewer/FGEventHandler.cxx | 25 +++++++------------------ src/Viewer/FGEventHandler.hxx | 4 +--- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/Input/FGMouseInput.cxx b/src/Input/FGMouseInput.cxx index 2f7b31de9..af22b3929 100644 --- a/src/Input/FGMouseInput.cxx +++ b/src/Input/FGMouseInput.cxx @@ -272,7 +272,11 @@ public: FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_CLOSED_HAND; osg::Vec2d windowPos; - flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y()); + const bool ok = flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y()); + if (!ok) { + SG_LOG(SG_GUI, SG_WARN, "doMouseMoveWithCallbacks: ignoring mouse move with missing context/traits"); + return; + } SGSceneryPicks pickList = globals->get_renderer()->pick(windowPos); if(pickList.empty()) @@ -557,7 +561,11 @@ void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindo // them consumes the event. osg::Vec2d windowPos; - flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y()); + bool ok = flightgear::eventToWindowCoords(ea, windowPos.x(), windowPos.y()); + if (!ok) { + SG_LOG(SG_GUI, SG_WARN, "Ignoring mouse click with null context/traits"); + return; + } SGSceneryPicks pickList; diff --git a/src/Viewer/FGEventHandler.cxx b/src/Viewer/FGEventHandler.cxx index 54f03f986..c9f5db121 100644 --- a/src/Viewer/FGEventHandler.cxx +++ b/src/Viewer/FGEventHandler.cxx @@ -466,11 +466,15 @@ void FGEventHandler::handleStats(osgGA::GUIActionAdapter& us) } } -void eventToWindowCoords(const osgGA::GUIEventAdapter* ea, +bool eventToWindowCoords(const osgGA::GUIEventAdapter* ea, double& x, double& y) { using namespace osg; const GraphicsContext* gc = ea->getGraphicsContext(); + if (!gc || !gc->getTraits()) { + return false; + } + const GraphicsContext::Traits* traits = gc->getTraits() ; // Scale x, y to the dimensions of the window x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin())) @@ -479,23 +483,8 @@ void eventToWindowCoords(const osgGA::GUIEventAdapter* ea, * (double)traits->height); if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) y = (double)traits->height - y; + + return true; } -#if 0 -void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea, - double& x, double& y) -{ - using namespace osg; - const GraphicsContext* gc = ea->getGraphicsContext(); - const GraphicsContext::Traits* traits = gc->getTraits() ; - // Scale x, y to the dimensions of the window - x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin())) - * (double)traits->width); - y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin())) - * (double)traits->height); - if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS) - y = (double)traits->height - y; -} -#endif - } diff --git a/src/Viewer/FGEventHandler.hxx b/src/Viewer/FGEventHandler.hxx index d40fffb89..9e1bbbb24 100644 --- a/src/Viewer/FGEventHandler.hxx +++ b/src/Viewer/FGEventHandler.hxx @@ -124,8 +124,6 @@ protected: SGPropertyNode_ptr _display, _print; }; -void eventToWindowCoords(const osgGA::GUIEventAdapter* ea, double& x, double& y); -void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea, - double& x, double& y); +bool eventToWindowCoords(const osgGA::GUIEventAdapter* ea, double& x, double& y); } #endif