From edc0646ab2d653905031bb2f8916a1720d03bd9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Garapich?= Date: Mon, 2 Mar 2015 21:38:07 +0100 Subject: [PATCH] Make FSX works with ISimulatorListener. SimConnect tries to connect to FSX every 5 seconds. TODO: Use WinAPI to wait for FSX process to show up --- src/blackcore/simulator.cpp | 2 +- src/blackcore/simulator.h | 2 +- src/blacksim/simulatorinfo.cpp | 4 +- src/plugins/simulator/fsx/simulator_fsx.cpp | 43 ++++++++++++++----- src/plugins/simulator/fsx/simulator_fsx.h | 19 ++++++++ .../simulator/fsx/simulator_fsxfactory.cpp | 12 +++--- .../simulator/fsx/simulator_fsxfactory.h | 3 ++ 7 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/blackcore/simulator.cpp b/src/blackcore/simulator.cpp index bcabb8355..d0c22576f 100644 --- a/src/blackcore/simulator.cpp +++ b/src/blackcore/simulator.cpp @@ -21,7 +21,7 @@ namespace BlackCore { void ISimulator::emitSimulatorCombinedStatus() { - quint8 status = + int status = (isConnected() ? Connected : static_cast(0)) | (isSimulating() ? Running : static_cast(0)) | (isPaused() ? Paused : static_cast(0)) diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index 549d03088..5b36a6482 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -176,7 +176,7 @@ namespace BlackCore signals: //! Simulator combined status - void simulatorStatusChanged(quint8 status); + void simulatorStatusChanged(int status); //! Emitted when own aircraft model has changed void ownAircraftModelChanged(BlackMisc::Simulation::CSimulatedAircraft aircraft); diff --git a/src/blacksim/simulatorinfo.cpp b/src/blacksim/simulatorinfo.cpp index 42ac9506f..e6a33a067 100644 --- a/src/blacksim/simulatorinfo.cpp +++ b/src/blacksim/simulatorinfo.cpp @@ -24,8 +24,8 @@ namespace BlackSim { Q_ASSERT(json["IID"].toString() == "net.vatsim.PilotClient.BlackCore.SimulatorInterface"); QJsonObject data = json["MetaData"].toObject(); - m_fullName = data["full_name"].toString("Unknown"); - m_shortName = data["short_name"].toString("Unknown"); + m_fullName = data["full_name"].toString(); + m_shortName = data["short_name"].toString(); } CVariant CSimulatorInfo::getSimulatorSetupValue(int index) const diff --git a/src/plugins/simulator/fsx/simulator_fsx.cpp b/src/plugins/simulator/fsx/simulator_fsx.cpp index 4b78dc0e7..1c7d1bb2e 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.cpp +++ b/src/plugins/simulator/fsx/simulator_fsx.cpp @@ -71,7 +71,6 @@ namespace BlackSimPlugin if (m_simConnected) { return true; } if (FAILED(SimConnect_Open(&m_hSimConnect, BlackMisc::CProject::systemNameAndVersionChar(), nullptr, 0, 0, 0))) { - emit connectionStatusChanged(ConnectionFailed); emitSimulatorCombinedStatus(); return false; } @@ -84,7 +83,6 @@ namespace BlackSimPlugin m_simconnectTimerId = startTimer(10); m_simConnected = true; - emit connectionStatusChanged(Connected); emitSimulatorCombinedStatus(); return true; } @@ -123,7 +121,6 @@ namespace BlackSimPlugin m_simconnectTimerId = -1; m_simConnected = false; - emit connectionStatusChanged(Disconnected); emitSimulatorCombinedStatus(); return true; } @@ -332,17 +329,16 @@ namespace BlackSimPlugin return; } - emit simulatorStarted(); emitSimulatorCombinedStatus(); } void CSimulatorFsx::onSimStopped() { - if (!m_simRunning) { return; } - m_simRunning = false; - mapperInstance()->gracefulShutdown(); // stop background reading if ongoing - emit simulatorStopped(); - emitSimulatorCombinedStatus(); // 3 states together + if (m_simRunning) { + m_simRunning = false; + mapperInstance()->gracefulShutdown(); // stop background reading if ongoing + } + emitSimulatorCombinedStatus(); } void CSimulatorFsx::onSimFrame() @@ -352,6 +348,7 @@ namespace BlackSimPlugin void CSimulatorFsx::onSimExit() { + m_simConnected = false; this->onSimStopped(); } @@ -495,13 +492,11 @@ namespace BlackSimPlugin m_simconnectTimerId = startTimer(10); m_simConnected = true; - emit connectionStatusChanged(Connected); emitSimulatorCombinedStatus(); } else { m_simConnected = false; - emit connectionStatusChanged(ConnectionFailed); emitSimulatorCombinedStatus(); } } @@ -812,5 +807,31 @@ namespace BlackSimPlugin CLogMessage(this).info("Synchronized time to UTC: %1") << myTime.toString(); } } + + CSimulatorFsxListener::CSimulatorFsxListener(QObject *parent) : ISimulatorListener(parent), + m_timer(new QTimer(this)) + { + Q_CONSTEXPR int QueryInterval = 5 * 1000; // 5 seconds + m_timer->setInterval(QueryInterval); + + connect(m_timer, &QTimer::timeout, [this]() { + HANDLE hSimConnect; + HRESULT result = SimConnect_Open(&hSimConnect, BlackMisc::CProject::systemNameAndVersionChar(), nullptr, 0, 0, 0); + SimConnect_Close(hSimConnect); + + if (result == S_OK) + emit simulatorStarted(m_simulatorInfo); + }); + } + + void CSimulatorFsxListener::start() + { + m_timer->start(); + } + + void CSimulatorFsxListener::stop() + { + m_timer->stop(); + } } // namespace } // namespace diff --git a/src/plugins/simulator/fsx/simulator_fsx.h b/src/plugins/simulator/fsx/simulator_fsx.h index e0d3ddad1..d5d3cfd9d 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.h +++ b/src/plugins/simulator/fsx/simulator_fsx.h @@ -201,6 +201,25 @@ namespace BlackSimPlugin qint64 m_statsUpdateAircraftTimeAvg = 0; int m_statsUpdateAircraftCount = 0; }; + + class CSimulatorFsxListener : public BlackCore::ISimulatorListener { + Q_OBJECT + + public: + //! Constructor + CSimulatorFsxListener(QObject* parent); + + //! \copydoc BlackCore::ISimulatorListener::start + virtual void start() override; + + //! \copydoc BlackCore::ISimulatorListener::stop + virtual void stop() override; + + private: + QTimer* m_timer; + const BlackSim::CSimulatorInfo m_simulatorInfo = BlackSim::CSimulatorInfo::FSX(); + + }; } } // namespace BlackCore diff --git a/src/plugins/simulator/fsx/simulator_fsxfactory.cpp b/src/plugins/simulator/fsx/simulator_fsxfactory.cpp index b9bab696f..67ff6e387 100644 --- a/src/plugins/simulator/fsx/simulator_fsxfactory.cpp +++ b/src/plugins/simulator/fsx/simulator_fsxfactory.cpp @@ -14,9 +14,6 @@ #include #include -using namespace BlackSim; -using namespace BlackCore; - namespace BlackSimPlugin { namespace Fsx @@ -27,9 +24,14 @@ namespace BlackSimPlugin return new CSimulatorFsx(ownAircraftProvider, renderedAircraftProvider, parent); } - CSimulatorInfo CSimulatorFsxFactory::getSimulatorInfo() const + BlackSim::CSimulatorInfo CSimulatorFsxFactory::getSimulatorInfo() const { - return CSimulatorInfo::FSX(); + return BlackSim::CSimulatorInfo::FSX(); + } + + BlackCore::ISimulatorListener *CSimulatorFsxFactory::createListener(QObject *parent) + { + return new CSimulatorFsxListener(parent); } } // namespace diff --git a/src/plugins/simulator/fsx/simulator_fsxfactory.h b/src/plugins/simulator/fsx/simulator_fsxfactory.h index bc5148b19..81c91e4ca 100644 --- a/src/plugins/simulator/fsx/simulator_fsxfactory.h +++ b/src/plugins/simulator/fsx/simulator_fsxfactory.h @@ -40,6 +40,9 @@ namespace BlackSimPlugin //! \copydoc BlackCore::ISimulatorFactory::getSimulatorInfo virtual BlackSim::CSimulatorInfo getSimulatorInfo() const override; + + //! \copydoc BlackCore::ISimulatorFactory::getListener + virtual BlackCore::ISimulatorListener *createListener(QObject *parent = nullptr) override; }; } // namespace