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
This commit is contained in:
Michał Garapich
2015-03-02 21:38:07 +01:00
committed by Roland Winklmeier
parent 5b4c2377b6
commit edc0646ab2
7 changed files with 65 additions and 20 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -14,9 +14,6 @@
#include <QTimer>
#include <QtConcurrent>
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

View File

@@ -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