diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp index fcf77ec23..7132da981 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp @@ -1317,7 +1317,7 @@ namespace BlackSimPlugin constexpr int QueryInterval = 5 * 1000; // 5 seconds m_timer->setInterval(QueryInterval); m_timer->setObjectName(this->objectName().append(":m_timer")); - connect(m_timer, &QTimer::timeout, this, &CSimulatorFsxCommonListener::ps_checkConnection); + connect(m_timer, &QTimer::timeout, this, &CSimulatorFsxCommonListener::checkConnection); } void CSimulatorFsxCommonListener::start() @@ -1330,16 +1330,70 @@ namespace BlackSimPlugin m_timer->stop(); } - void CSimulatorFsxCommonListener::ps_checkConnection() + void CSimulatorFsxCommonListener::checkConnection() { + Q_ASSERT_X(sApp, Q_FUNC_INFO, "Missing sApp"); Q_ASSERT_X(!CThreadUtils::isCurrentThreadApplicationThread(), Q_FUNC_INFO, "Expect to run in background"); HANDLE hSimConnect; HRESULT result = SimConnect_Open(&hSimConnect, sApp->swiftVersionChar(), nullptr, 0, 0, 0); - SimConnect_Close(hSimConnect); + bool check = false; if (result == S_OK) + { + for (int i = 0; !check && i < 3; i++) + { + // result not always in first dispatch + result = SimConnect_CallDispatch(hSimConnect, CSimulatorFsxCommonListener::SimConnectProc, this); + if (result != S_OK) { break; } // means serious failure + check = this->checkVersionAndSimulator(); + sApp->processEventsFor(500); + } + } + SimConnect_Close(hSimConnect); + + if (check) { emit simulatorStarted(this->getPluginInfo()); } } + + bool CSimulatorFsxCommonListener::checkVersionAndSimulator() const + { + const CSimulatorInfo sim(getPluginInfo().getIdentifier()); + const QString simName = m_simulatorName.toLower().trimmed(); + + if (simName.isEmpty()) { return false; } + if (sim.p3d()) + { + return simName.contains("lockheed") || simName.contains("martin") || simName.contains("p3d") || simName.contains("prepar"); + } + else if (sim.fsx()) + { + return simName.contains("fsx") || simName.contains("microsoft") || simName.contains("simulator x"); + } + return false; + } + + void CSimulatorFsxCommonListener::SimConnectProc(SIMCONNECT_RECV *pData, DWORD cbData, void *pContext) + { + Q_UNUSED(cbData); + CSimulatorFsxCommonListener *simListener = static_cast(pContext); + switch (pData->dwID) + { + case SIMCONNECT_RECV_ID_OPEN: + { + SIMCONNECT_RECV_OPEN *event = (SIMCONNECT_RECV_OPEN *)pData; + simListener->m_simulatorVersion = QString("%1.%2.%3.%4").arg(event->dwApplicationVersionMajor).arg(event->dwApplicationVersionMinor).arg(event->dwApplicationBuildMajor).arg(event->dwApplicationBuildMinor); + simListener->m_simConnectVersion = QString("%1.%2.%3.%4").arg(event->dwSimConnectVersionMajor).arg(event->dwSimConnectVersionMinor).arg(event->dwSimConnectBuildMajor).arg(event->dwSimConnectBuildMinor); + simListener->m_simulatorName = QString(event->szApplicationName); + simListener->m_simulatorDetails = QString("Open: AppName=\"%1\" AppVersion=%2 SimConnectVersion=%3").arg(simListener->m_simulatorName, simListener->m_simulatorVersion, simListener->m_simConnectVersion); + CLogMessage(static_cast(nullptr)).info("Connect to FSX/P3D: %1") << sApp->swiftVersionString(); + break; + } + case SIMCONNECT_RECV_ID_EXCEPTION: + break; + default: + break; + } + } } // namespace } // namespace diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h index 884787cd8..1e7047fa9 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h @@ -298,12 +298,22 @@ namespace BlackSimPlugin //! \copydoc BlackCore::ISimulatorListener::stop virtual void stop() override; - protected slots: + protected: //! Test if connection can be established - void ps_checkConnection(); + void checkConnection(); + + //! Check simulator version and type + bool checkVersionAndSimulator() const; private: QTimer *m_timer { nullptr }; + QString m_simulatorVersion; + QString m_simConnectVersion; + QString m_simulatorName; + QString m_simulatorDetails; + + //! SimConnect Callback + static void CALLBACK SimConnectProc(SIMCONNECT_RECV *pData, DWORD cbData, void *pContext); }; } } // namespace