1
0
Fork 0

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
This commit is contained in:
James Turner 2020-12-18 12:17:42 +00:00
parent fe17cfc84d
commit 4f7d84ed73
3 changed files with 18 additions and 23 deletions

View file

@ -272,7 +272,11 @@ public:
FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_CLOSED_HAND; FGMouseCursor::Cursor cur = FGMouseCursor::CURSOR_CLOSED_HAND;
osg::Vec2d windowPos; 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); SGSceneryPicks pickList = globals->get_renderer()->pick(windowPos);
if(pickList.empty()) if(pickList.empty())
@ -557,7 +561,11 @@ void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindo
// them consumes the event. // them consumes the event.
osg::Vec2d windowPos; 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; SGSceneryPicks pickList;

View file

@ -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) double& x, double& y)
{ {
using namespace osg; using namespace osg;
const GraphicsContext* gc = ea->getGraphicsContext(); const GraphicsContext* gc = ea->getGraphicsContext();
if (!gc || !gc->getTraits()) {
return false;
}
const GraphicsContext::Traits* traits = gc->getTraits() ; const GraphicsContext::Traits* traits = gc->getTraits() ;
// Scale x, y to the dimensions of the window // Scale x, y to the dimensions of the window
x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin())) x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
@ -479,23 +483,8 @@ void eventToWindowCoords(const osgGA::GUIEventAdapter* ea,
* (double)traits->height); * (double)traits->height);
if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
y = (double)traits->height - y; 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
} }

View file

@ -124,8 +124,6 @@ protected:
SGPropertyNode_ptr _display, _print; SGPropertyNode_ptr _display, _print;
}; };
void eventToWindowCoords(const osgGA::GUIEventAdapter* ea, double& x, double& y); bool eventToWindowCoords(const osgGA::GUIEventAdapter* ea, double& x, double& y);
void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea,
double& x, double& y);
} }
#endif #endif