From f7b50b3f5a2c20d2a49f0c0f0663ddc43a38b321 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 10 Apr 2015 00:16:00 +0200 Subject: [PATCH] refs #366, passing Simulator plugin info to drivers Add. some finetuning --- src/blackcore/context_simulator_impl.cpp | 90 ++++++++++--------- src/blackcore/simulator.cpp | 30 ++++--- src/blackcore/simulator.h | 43 +++++---- .../components/settingsfsxcomponent.cpp | 3 +- .../simulation/simulatorplugininfo.cpp | 10 ++- src/plugins/simulator/fs9/simulator_fs9.cpp | 27 +++--- src/plugins/simulator/fs9/simulator_fs9.h | 17 ++-- .../simulator/fscommon/simulator_fscommon.cpp | 8 +- .../simulator/fscommon/simulator_fscommon.h | 2 +- src/plugins/simulator/fsx/simulator_fsx.cpp | 17 ++-- src/plugins/simulator/fsx/simulator_fsx.h | 9 +- .../simulator/fsx/simulator_fsxfactory.cpp | 4 +- .../simulator/fsx/simulator_fsxfactory.h | 9 +- .../simulator/xplane/simulator_xplane.cpp | 64 ++++++++----- .../simulator/xplane/simulator_xplane.h | 38 ++++---- 15 files changed, 221 insertions(+), 150 deletions(-) diff --git a/src/blackcore/context_simulator_impl.cpp b/src/blackcore/context_simulator_impl.cpp index 806f7d717..9fdd1f50a 100644 --- a/src/blackcore/context_simulator_impl.cpp +++ b/src/blackcore/context_simulator_impl.cpp @@ -32,8 +32,8 @@ using namespace BlackMisc::Simulation::Settings; namespace BlackCore { CContextSimulator::CContextSimulator(CRuntimeConfig::ContextMode mode, CRuntime *runtime) : - IContextSimulator(mode, runtime), - m_mapper(new QSignalMapper(this)) + IContextSimulator(mode, runtime), + m_mapper(new QSignalMapper(this)) { findSimulatorPlugins(); // Maps listener instance @@ -45,13 +45,12 @@ namespace BlackCore disconnectFromSimulator(); unloadSimulatorPlugin(); } - + ISimulatorFactory *CContextSimulator::getSimulatorFactory(const CSimulatorPluginInfo &simulator) { PluginData *plugin = findPlugin(simulator); - if (!plugin) - return nullptr; - + if (!plugin) { return nullptr; } + if (!plugin->factory) { QPluginLoader loader(plugin->fileName); @@ -62,14 +61,16 @@ namespace BlackCore if (factory) { plugin->factory = factory; - CLogMessage(this).debug() << "Loaded plugin: " << plugin->info.toQString(); + CLogMessage(this).info("Loaded driver: %1") << plugin->info.toQString(); } - } else { + } + else + { QString errorMsg = loader.errorString().append(" ").append("Also check if required dll/libs of plugin exists"); CLogMessage(this).error(errorMsg); } } - + return plugin->factory; } @@ -77,11 +78,11 @@ namespace BlackCore { CSimulatorPluginInfoList list; - std::for_each(m_simulatorPlugins.begin(), m_simulatorPlugins.end(), [&list](const PluginData &driver) + std::for_each(m_simulatorPlugins.begin(), m_simulatorPlugins.end(), [&list](const PluginData & driver) { list.push_back(driver.info); }); - + return list; } @@ -349,7 +350,7 @@ namespace BlackCore Q_ASSERT(!simulatorInfo.isUnspecified()); // warning if we do not have any plugins - if (m_simulatorPlugins.isEmpty()) + if (m_simulatorPlugins.isEmpty() || simulatorInfo.isUnspecified()) { CLogMessage(this).error("No simulator plugins"); return false; @@ -364,12 +365,12 @@ namespace BlackCore Q_ASSERT(this->getIContextNetwork()->isUsingImplementingObject()); IOwnAircraftProvider *ownAircraftProvider = this->getRuntime()->getCContextOwnAircraft(); IRemoteAircraftProvider *renderedAircraftProvider = this->getRuntime()->getCContextNetwork(); - ISimulator *newSimulator = factory->create(ownAircraftProvider, renderedAircraftProvider, this); + ISimulator *newSimulator = factory->create(simulatorInfo, ownAircraftProvider, renderedAircraftProvider, this); Q_ASSERT(newSimulator); unloadSimulatorPlugin(); // old plugin unloaded - PluginData* plugin = findPlugin(simulatorInfo); + PluginData *plugin = findPlugin(simulatorInfo); plugin->simulator = newSimulator; m_simulator = plugin; @@ -381,7 +382,7 @@ namespace BlackCore // log from context to simulator connect(CLogHandler::instance(), &CLogHandler::localMessageLogged, m_simulator->simulator, &ISimulator::displayStatusMessage); - connect(CLogHandler::instance(), &CLogHandler::remoteMessageLogged,m_simulator->simulator, &ISimulator::displayStatusMessage); + connect(CLogHandler::instance(), &CLogHandler::remoteMessageLogged, m_simulator->simulator, &ISimulator::displayStatusMessage); // connect with network IContextNetwork *networkContext = this->getIContextNetwork(); @@ -433,7 +434,7 @@ namespace BlackCore return false; } } - + void CContextSimulator::listenForSimulator(const CSimulatorPluginInfo &simulatorInfo) { Q_ASSERT(this->getIContextApplication()); @@ -441,8 +442,9 @@ namespace BlackCore Q_ASSERT(!simulatorInfo.isUnspecified()); if (this->m_simulator) - { // already loaded - qWarning("Cannot listen for simulator while the driver is still loaded"); + { + // already loaded + CLogMessage(this).warning("Cannot listen for simulator while the driver %1 is still loaded") << m_simulator->info.toQString(); return; } @@ -453,7 +455,7 @@ namespace BlackCore return; } - PluginData* plugin = findPlugin(simulatorInfo); + PluginData *plugin = findPlugin(simulatorInfo); if (!plugin) { CLogMessage(this).error("Driver not found for '%1'") << simulatorInfo.toQString(); @@ -462,11 +464,17 @@ namespace BlackCore if (!plugin->listener) { - ISimulatorFactory* factory = getSimulatorFactory(simulatorInfo); + ISimulatorFactory *factory = getSimulatorFactory(simulatorInfo); Q_ASSERT(factory); plugin->listener = factory->createListener(); - connect(plugin->listener, SIGNAL(simulatorStarted()), m_mapper, SLOT(map())); + bool c = connect(plugin->listener, &ISimulatorListener::simulatorStarted, + m_mapper, static_cast (&QSignalMapper::map)); + if (!c) + { + CLogMessage(this).error("Unable to use '%1'") << simulatorInfo.toQString(); + return; + } m_mapper->setMapping(plugin->listener, plugin->listener); plugin->listener->moveToThread(&m_listenersThread); } @@ -480,16 +488,18 @@ namespace BlackCore } QMetaObject::invokeMethod(listener, "start"); - CLogMessage(this).debug() << "Listening for simulator:" << simulatorInfo.toQString(true); + CLogMessage(this).debug() << "Listening for simulator: " << simulatorInfo.toQString(true); } - + void CContextSimulator::listenForAllSimulators() { auto plugins = getAvailableSimulatorPlugins(); - for (const auto &p: plugins) + for (const auto &p : plugins) + { listenForSimulator(p); + } } - + void CContextSimulator::listenForSimulatorFromSettings() { Q_ASSERT(this->getIContextSettings()); @@ -515,7 +525,7 @@ namespace BlackCore Q_ASSERT(networkContext->isLocalObject()); Q_UNUSED(networkContext); Q_ASSERT(m_simulator->simulator); - + m_simulator->simulator->disconnect(); CLogHandler::instance()->disconnect(m_simulator->simulator); this->disconnect(m_simulator->simulator); @@ -534,7 +544,7 @@ namespace BlackCore // todo: // This was previously an assert and it should be one again in the future. // This slot should not even be called when no simulator is available. - if(!m_simulator) + if (!m_simulator) { // Do something if no simulator is running return; @@ -551,7 +561,7 @@ namespace BlackCore // todo: // This was previously an assert and it should be one again in the future. // This slot should not even be called when no simulator is available. - if(!m_simulator) + if (!m_simulator) { // Do something if no simulator is running return; @@ -580,7 +590,7 @@ namespace BlackCore // todo: // This was previously an assert and it should be one again in the future. // This slot should not even be called when no simulator is available. - if(!m_simulator) + if (!m_simulator) { // Do something if no simulator is running return; @@ -617,7 +627,7 @@ namespace BlackCore // todo: // This was previously an assert and it should be one again in the future. // This slot should not even be called when no simulator is available. - if(!m_simulator) + if (!m_simulator) { // Do something if no simulator is running return; @@ -641,7 +651,7 @@ namespace BlackCore // plugin CSettingsSimulator settingsSim = this->getIContextSettings()->getSimulatorSettings(); CSimulatorPluginInfo plugin = getIContextSettings()->getSimulatorSettings().getSelectedPlugin(); - + // no simulator loaded yet, listen if (!m_simulator) { @@ -713,18 +723,18 @@ namespace BlackCore Q_ASSERT(m_simulator->simulator); return m_simulator->simulator->isSimulating(); } - + void CContextSimulator::ps_simulatorStarted(QObject *listener) { Q_ASSERT(listener); Q_ASSERT(listener->inherits("BlackCore::ISimulatorListener")); /* Find caller */ - PluginData *plugin = [this](QObject *listener) + PluginData *plugin = [this](QObject * listener) { - auto it = std::find_if(m_simulatorPlugins.begin(), m_simulatorPlugins.end(), [listener](const PluginData &plugin) + auto it = std::find_if(m_simulatorPlugins.begin(), m_simulatorPlugins.end(), [listener](const PluginData & plugin) { - return plugin.listener == listener; + return plugin.listener == listener; }); return &(*it); }(listener); @@ -747,7 +757,7 @@ namespace BlackCore QStringList fileNames = m_pluginsDir.entryList(QDir::Files); fileNames.sort(Qt::CaseInsensitive); // give a certain order, rather than random file order - for (const auto& fileName: fileNames) + for (const auto &fileName : fileNames) { if (!QLibrary::isLibrary(fileName)) { @@ -762,7 +772,7 @@ namespace BlackCore simulatorPluginInfo.convertFromJson(json); if (simulatorPluginInfo.isValid()) { - m_simulatorPlugins << PluginData{ simulatorPluginInfo, nullptr, nullptr, nullptr, pluginPath}; + m_simulatorPlugins << PluginData { simulatorPluginInfo, nullptr, nullptr, nullptr, pluginPath}; CLogMessage(this).debug() << "Found simulator driver: " << simulatorPluginInfo.toQString(); } else @@ -771,10 +781,10 @@ namespace BlackCore } } } - + void CContextSimulator::stopSimulatorListeners() { - std::for_each(m_simulatorPlugins.begin(), m_simulatorPlugins.end(), [](PluginData& plugin) + std::for_each(m_simulatorPlugins.begin(), m_simulatorPlugins.end(), [](PluginData & plugin) { if (plugin.listener) QMetaObject::invokeMethod(plugin.listener, "stop"); @@ -783,7 +793,7 @@ namespace BlackCore CContextSimulator::PluginData *CContextSimulator::findPlugin(const CSimulatorPluginInfo &info) { - auto it = std::find_if(m_simulatorPlugins.begin(), m_simulatorPlugins.end(), [&info](PluginData& plugin) + auto it = std::find_if(m_simulatorPlugins.begin(), m_simulatorPlugins.end(), [&info](PluginData & plugin) { return plugin.info == info; }); diff --git a/src/blackcore/simulator.cpp b/src/blackcore/simulator.cpp index 6fd452d29..4d9973d38 100644 --- a/src/blackcore/simulator.cpp +++ b/src/blackcore/simulator.cpp @@ -9,6 +9,7 @@ #include "simulator.h" #include "interpolator.h" +#include "blackmisc/logmessage.h" #include "blackmisc/collection.h" using namespace BlackMisc; @@ -23,26 +24,30 @@ namespace BlackCore { int status = (isConnected() ? Connected : static_cast(0)) - | (isSimulating() ? Running : static_cast(0)) - | (isPaused() ? Paused : static_cast(0)) - ; + | (isSimulating() ? Running : static_cast(0)) + | (isPaused() ? Paused : static_cast(0)) + ; emit simulatorStatusChanged(status); } - - ISimulatorListener::ISimulatorListener(QObject* parent) : QObject(parent) - { - } - CSimulatorCommon::CSimulatorCommon(BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, + ISimulatorListener::ISimulatorListener(QObject *parent) : QObject(parent) + { } + + CSimulatorCommon::CSimulatorCommon(const CSimulatorPluginInfo &info, + BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, QObject *parent) : ISimulator(parent), + m_simulatorPluginInfo(info), COwnAircraftAware(ownAircraftProvider), CRemoteAircraftAware(remoteAircraftProvider) { + this->setObjectName("CSimulatorCommon"); m_oneSecondTimer = new QTimer(this); + m_oneSecondTimer->setObjectName(this->objectName().append(":OneSecondTimer")); connect(this->m_oneSecondTimer, &QTimer::timeout, this, &CSimulatorCommon::ps_oneSecondTimer); m_oneSecondTimer->start(1000); + CLogMessage(this).info("Initialized simulator driver %1") << m_simulatorPluginInfo.toQString(); } void CSimulatorCommon::blinkHighlightedAircraft() @@ -197,12 +202,12 @@ namespace BlackCore { return !m_maxRenderedDistance.isNull(); } - + void CSimulatorCommon::enableDebugMessages(bool driver, bool interpolator) { this->m_debugMessages = driver; Q_UNUSED(interpolator); - + } int CSimulatorCommon::getInstalledModelsCount() const @@ -235,6 +240,11 @@ namespace BlackCore return this->isMaxDistanceRestricted() || this->isMaxAircraftRestricted(); } + const CSimulatorPluginInfo &CSimulatorCommon::getSimulatorPluginInfo() const + { + return m_simulatorPluginInfo; + } + void CSimulatorCommon::deleteAllRenderingRestrictions() { if (!isRenderingEnabled()) { return; } diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index 43d20b696..7f022d1e2 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -62,6 +62,9 @@ namespace BlackCore //! Simulator running? virtual bool isSimulating() const = 0; + //! Get the simulator info + virtual const BlackMisc::Simulation::CSimulatorPluginInfo &getSimulatorPluginInfo() const = 0; + //! Originator const QString &simulatorOriginator() { @@ -196,39 +199,39 @@ namespace BlackCore void emitSimulatorCombinedStatus(); }; - + /*! * Interface to a simulator listener. - * + * * The simulator listener is responsible for letting the core know when * the corresponding simulator is started. */ class ISimulatorListener : public QObject { Q_OBJECT - + public: - + //! Constructor //! \sa ISimulatorFactory::createListener(). //! \note msvc2015: use inherited constructor - ISimulatorListener(QObject* parent); - + ISimulatorListener(QObject *parent); + //! Destructor virtual ~ISimulatorListener() = default; public slots: //! Start listening for the simulator to start. virtual void start() = 0; - + //! Stops listening. virtual void stop() = 0; - + signals: - + //! Emitted when the listener discovers the simulator running. void simulatorStarted(); - + }; //! Factory pattern class to create instances of ISimulator @@ -241,19 +244,21 @@ namespace BlackCore //! //! Create a new instance of a driver + //! \param info metadata about simulator //! \param ownAircraftProvider in memory access to own aircraft data //! \param renderedAircraftProvider in memory access to rendered aircraft data such as situation history and aircraft itself //! \param parent QObject //! \return driver instance //! virtual ISimulator *create( + const BlackMisc::Simulation::CSimulatorPluginInfo &info, BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, BlackMisc::Simulation::IRemoteAircraftProvider *renderedAircraftProvider, QObject *parent = nullptr) = 0; - + //! Simulator listener instance virtual ISimulatorListener *createListener(QObject *parent = nullptr) = 0; - + }; //! Common base class with providers, interface and some base functionality @@ -302,6 +307,9 @@ namespace BlackCore //! \copydoc IContextSimulator::isRenderingRestricted virtual bool isRenderingRestricted() const override; + //! \copydoc IContextSimulator::getSimulatorPluginInfo + virtual const BlackMisc::Simulation::CSimulatorPluginInfo &getSimulatorPluginInfo() const override; + //! \copydoc IContextSimulator::deleteAllRenderingRestrictions virtual void deleteAllRenderingRestrictions(); @@ -311,10 +319,10 @@ namespace BlackCore protected: //! Constructor - CSimulatorCommon( - BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, - BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, - QObject *parent = nullptr); + CSimulatorCommon(const BlackMisc::Simulation::CSimulatorPluginInfo &info, + BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, + BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, + QObject *parent = nullptr); //! Blink the highlighted aircraft void blinkHighlightedAircraft(); @@ -334,8 +342,9 @@ namespace BlackCore qint64 m_highlightEndTimeMsEpoch = 0; //!< end highlighting int m_timerCounter = 0; //!< allows to calculate n seconds QTimer *m_oneSecondTimer = nullptr; //!< timer + BlackMisc::Simulation::CSimulatorPluginInfo m_simulatorPluginInfo; //!< info object BlackMisc::Simulation::CSimulatedAircraftList m_highlightedAircraft; //!< all other aircraft are to be ignored - BlackMisc::Aviation::CCallsignSet m_callsignsToBeRendered; //!< callsigns which will be rendered + BlackMisc::Aviation::CCallsignSet m_callsignsToBeRendered; //!< callsigns which will be rendered int m_maxRenderedAircraft = MaxAircraftInfinite; //!< max.rendered aircraft BlackMisc::PhysicalQuantities::CLength m_maxRenderedDistance { 0.0, BlackMisc::PhysicalQuantities::CLengthUnit::nullUnit()}; //!< max.distance for rendering }; diff --git a/src/blackgui/components/settingsfsxcomponent.cpp b/src/blackgui/components/settingsfsxcomponent.cpp index d419b1ca5..041f45bfa 100644 --- a/src/blackgui/components/settingsfsxcomponent.cpp +++ b/src/blackgui/components/settingsfsxcomponent.cpp @@ -108,10 +108,11 @@ namespace BlackGui return; } int p = port.toInt(); + //! \todo filename is only available if driver has been loaded QString fileName = this->getIContextSimulator()->getSimulatorInfo().getSimulatorSetupValueAsString(CFsxSimulatorSetup::SetupSimConnectCfgFile); if (fileName.isEmpty()) { - CLogMessage(this).validationError("Invalid filename or filename empty"); + CLogMessage(this).validationError("Invalid or empty filename empty, driver loaded?"); return; } diff --git a/src/blackmisc/simulation/simulatorplugininfo.cpp b/src/blackmisc/simulation/simulatorplugininfo.cpp index 7c84a9339..385cf1041 100644 --- a/src/blackmisc/simulation/simulatorplugininfo.cpp +++ b/src/blackmisc/simulation/simulatorplugininfo.cpp @@ -52,8 +52,14 @@ namespace BlackMisc QString CSimulatorPluginInfo::getSimulatorSetupValueAsString(int index) const { CVariant qv = getSimulatorSetupValue(index); - Q_ASSERT(qv.canConvert()); - return qv.toQString(); + if (qv.canConvert()) + { + return qv.toQString(); + } + else + { + return ""; + } } void CSimulatorPluginInfo::setSimulatorSetup(const BlackMisc::CPropertyIndexVariantMap &setup) diff --git a/src/plugins/simulator/fs9/simulator_fs9.cpp b/src/plugins/simulator/fs9/simulator_fs9.cpp index a2db8f675..582d92063 100644 --- a/src/plugins/simulator/fs9/simulator_fs9.cpp +++ b/src/plugins/simulator/fs9/simulator_fs9.cpp @@ -34,7 +34,8 @@ using namespace BlackSimPlugin::Fs9; using namespace BlackSimPlugin::FsCommon; -namespace { +namespace +{ /* These instances should be global, as they are shared between all classes * this file contains. They are instantied by CFs9Factory. */ QSharedPointer fs9Host; @@ -45,9 +46,12 @@ namespace BlackSimPlugin { namespace Fs9 { - CSimulatorFs9::CSimulatorFs9(IOwnAircraftProvider *ownAircraftProvider, - IRemoteAircraftProvider *remoteAircraftProvider, QObject *parent) : - CSimulatorFsCommon(ownAircraftProvider, remoteAircraftProvider, parent) + CSimulatorFs9::CSimulatorFs9( + const CSimulatorPluginInfo &info, + IOwnAircraftProvider *ownAircraftProvider, + IRemoteAircraftProvider *remoteAircraftProvider, + QObject *parent) : + CSimulatorFsCommon(info, ownAircraftProvider, remoteAircraftProvider, parent) { connect(lobbyClient.data(), &CLobbyClient::disconnected, this, std::bind(&CSimulatorFs9::simulatorStatusChanged, this, 0)); connect(fs9Host.data(), &CFs9Host::customPacketReceived, this, &CSimulatorFs9::ps_processFs9Message); @@ -65,7 +69,7 @@ namespace BlackSimPlugin { Q_ASSERT(m_fsuipc); Q_ASSERT(fs9Host->isConnected()); - + if (m_useFsuipc) { m_fsuipc->connect(); // connect FSUIPC too @@ -288,7 +292,7 @@ namespace BlackSimPlugin if (m_lobbyConnected || lobbyClient->connectFs9ToHost(fs9Host->getHostAddress()) == S_OK) { m_lobbyConnected = true; - CLogMessage(this).info("Swift is joining FS9 to the multiplayer session..."); + CLogMessage(this).info("Swift is joining FS9 to the multiplayer session..."); } if (m_lobbyConnected && fs9Host->isConnected()) @@ -332,17 +336,18 @@ namespace BlackSimPlugin } BlackCore::ISimulator *CSimulatorFs9Factory::create( - IOwnAircraftProvider *ownAircraftProvider, - IRemoteAircraftProvider *remoteAircraftProvider, - QObject *parent) + const CSimulatorPluginInfo &info, + IOwnAircraftProvider *ownAircraftProvider, + IRemoteAircraftProvider *remoteAircraftProvider, + QObject *parent) { - return new CSimulatorFs9(ownAircraftProvider, remoteAircraftProvider, parent); + return new CSimulatorFs9(info, ownAircraftProvider, remoteAircraftProvider, parent); } BlackCore::ISimulatorListener *CSimulatorFs9Factory::createListener(QObject *parent) { return new CSimulatorFs9Listener(parent); } - + } // namespace } // namespace diff --git a/src/plugins/simulator/fs9/simulator_fs9.h b/src/plugins/simulator/fs9/simulator_fs9.h index 62f7f67f4..7a4acbbf6 100644 --- a/src/plugins/simulator/fs9/simulator_fs9.h +++ b/src/plugins/simulator/fs9/simulator_fs9.h @@ -40,6 +40,7 @@ namespace BlackSimPlugin public: //! Constructor, parameters as in \sa BlackCore::ISimulatorFactory::create CSimulatorFs9( + const BlackMisc::Simulation::CSimulatorPluginInfo &info, BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, QObject *parent = nullptr); @@ -112,12 +113,13 @@ namespace BlackSimPlugin //! Listener starts the FS9 multiplayer host and tries to make the running instance //! of simulator to connect to it. When emitting the simulatorStarted() signal, //! FS9 is already connected. - class CSimulatorFs9Listener : public BlackCore::ISimulatorListener { + class CSimulatorFs9Listener : public BlackCore::ISimulatorListener + { Q_OBJECT public: //! Constructor - CSimulatorFs9Listener(QObject* parent); + CSimulatorFs9Listener(QObject *parent); public slots: //! \copydoc BlackCore::ISimulatorListener::start @@ -128,7 +130,7 @@ namespace BlackSimPlugin private: - QTimer* m_timer = nullptr; + QTimer *m_timer = nullptr; bool m_lobbyConnected = false; }; @@ -142,16 +144,17 @@ namespace BlackSimPlugin public: //! Constructor - CSimulatorFs9Factory(QObject* parent = nullptr); + CSimulatorFs9Factory(QObject *parent = nullptr); //! Destructor virtual ~CSimulatorFs9Factory(); //! \copydoc BlackCore::ISimulatorFactory::create(ownAircraftProvider, remoteAircraftProvider, parent) virtual BlackCore::ISimulator *create( - BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, - BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, - QObject *parent) override; + const BlackMisc::Simulation::CSimulatorPluginInfo &info, + BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, + BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, + QObject *parent) override; //! \copydoc BlackCore::ISimulatorFactory::createListener virtual BlackCore::ISimulatorListener *createListener(QObject *parent = nullptr) override; diff --git a/src/plugins/simulator/fscommon/simulator_fscommon.cpp b/src/plugins/simulator/fscommon/simulator_fscommon.cpp index 2116b21b1..7e0bd3dc6 100644 --- a/src/plugins/simulator/fscommon/simulator_fscommon.cpp +++ b/src/plugins/simulator/fscommon/simulator_fscommon.cpp @@ -25,8 +25,12 @@ namespace BlackSimPlugin { namespace FsCommon { - CSimulatorFsCommon::CSimulatorFsCommon(BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, IRemoteAircraftProvider *renderedAircraftProvider, QObject *parent) : - CSimulatorCommon(ownAircraftProvider, renderedAircraftProvider, parent), + CSimulatorFsCommon::CSimulatorFsCommon( + const BlackMisc::Simulation::CSimulatorPluginInfo &info, + BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, + IRemoteAircraftProvider *renderedAircraftProvider, + QObject *parent) : + CSimulatorCommon(info, ownAircraftProvider, renderedAircraftProvider, parent), m_fsuipc(new FsCommon::CFsuipc()) { // hack to init mapper diff --git a/src/plugins/simulator/fscommon/simulator_fscommon.h b/src/plugins/simulator/fscommon/simulator_fscommon.h index 6d6552374..0df6c21ab 100644 --- a/src/plugins/simulator/fscommon/simulator_fscommon.h +++ b/src/plugins/simulator/fscommon/simulator_fscommon.h @@ -80,7 +80,7 @@ namespace BlackSimPlugin protected: //! Constructor - CSimulatorFsCommon( + CSimulatorFsCommon(const BlackMisc::Simulation::CSimulatorPluginInfo &info, BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, BlackMisc::Simulation::IRemoteAircraftProvider *renderedAircraftProvider, QObject *parent = nullptr); diff --git a/src/plugins/simulator/fsx/simulator_fsx.cpp b/src/plugins/simulator/fsx/simulator_fsx.cpp index ac1095ad6..28c1245ec 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.cpp +++ b/src/plugins/simulator/fsx/simulator_fsx.cpp @@ -38,17 +38,22 @@ namespace BlackSimPlugin { namespace Fsx { - CSimulatorFsx::CSimulatorFsx(IOwnAircraftProvider *ownAircraftProvider, IRemoteAircraftProvider *remoteAircraftProvider, QObject *parent) : - CSimulatorFsCommon(ownAircraftProvider, remoteAircraftProvider, parent) + CSimulatorFsx::CSimulatorFsx( + const CSimulatorPluginInfo &info, + IOwnAircraftProvider *ownAircraftProvider, + IRemoteAircraftProvider *remoteAircraftProvider, + QObject *parent) : + CSimulatorFsCommon(info, ownAircraftProvider, remoteAircraftProvider, parent) { Q_ASSERT(ownAircraftProvider); Q_ASSERT(remoteAircraftProvider); CFsxSimulatorSetup setup; setup.init(); // this fetches important settings on local side + this->m_simulatorPluginInfo.setSimulatorSetup(setup.getSettings()); + m_useFsuipc = false; // do not use FSUIPC at the moment with FSX this->m_interpolator = new CInterpolatorLinear(remoteAircraftProvider, this); this->m_interpolator->start(); - this->m_simulatorInfo.setSimulatorSetup(setup.getSettings()); } CSimulatorFsx::~CSimulatorFsx() @@ -334,7 +339,8 @@ namespace BlackSimPlugin void CSimulatorFsx::onSimStopped() { - if (m_simRunning) { + if (m_simRunning) + { m_simRunning = false; mapperInstance()->gracefulShutdown(); // stop background reading if ongoing } @@ -814,7 +820,8 @@ namespace BlackSimPlugin Q_CONSTEXPR int QueryInterval = 5 * 1000; // 5 seconds m_timer->setInterval(QueryInterval); - connect(m_timer, &QTimer::timeout, [this]() { + connect(m_timer, &QTimer::timeout, [this]() + { HANDLE hSimConnect; HRESULT result = SimConnect_Open(&hSimConnect, BlackMisc::CProject::systemNameAndVersionChar(), nullptr, 0, 0, 0); SimConnect_Close(hSimConnect); diff --git a/src/plugins/simulator/fsx/simulator_fsx.h b/src/plugins/simulator/fsx/simulator_fsx.h index 61cdcfe1a..a684b4689 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.h +++ b/src/plugins/simulator/fsx/simulator_fsx.h @@ -74,6 +74,7 @@ namespace BlackSimPlugin public: //! Constructor, parameters as in \sa BlackCore::ISimulatorFactory::create CSimulatorFsx( + const BlackMisc::Simulation::CSimulatorPluginInfo &info, BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, QObject *parent = nullptr); @@ -195,7 +196,6 @@ namespace BlackSimPlugin uint m_nextObjID = 1; //!< object ID TODO: also used as request id, where to we place other request ids as for facilities QHash m_simConnectObjects; QFutureWatcher m_watcherConnect; - BlackMisc::Simulation::CSimulatorPluginInfo m_simulatorInfo; // statistics qint64 m_statsUpdateAircraftTimeTotal = 0; @@ -204,12 +204,13 @@ namespace BlackSimPlugin }; //! Listener for FSX - class CSimulatorFsxListener : public BlackCore::ISimulatorListener { + class CSimulatorFsxListener : public BlackCore::ISimulatorListener + { Q_OBJECT public: //! Constructor - CSimulatorFsxListener(QObject* parent); + CSimulatorFsxListener(QObject *parent); public slots: //! \copydoc BlackCore::ISimulatorListener::start @@ -219,7 +220,7 @@ namespace BlackSimPlugin virtual void stop() override; private: - QTimer* m_timer; + QTimer *m_timer; }; } diff --git a/src/plugins/simulator/fsx/simulator_fsxfactory.cpp b/src/plugins/simulator/fsx/simulator_fsxfactory.cpp index 531c7000f..b83ef44a4 100644 --- a/src/plugins/simulator/fsx/simulator_fsxfactory.cpp +++ b/src/plugins/simulator/fsx/simulator_fsxfactory.cpp @@ -18,10 +18,10 @@ namespace BlackSimPlugin { namespace Fsx { - BlackCore::ISimulator *CSimulatorFsxFactory::create(BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, BlackMisc::Simulation::IRemoteAircraftProvider *renderedAircraftProvider, QObject *parent) + BlackCore::ISimulator *CSimulatorFsxFactory::create(const BlackMisc::Simulation::CSimulatorPluginInfo &info, BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, BlackMisc::Simulation::IRemoteAircraftProvider *renderedAircraftProvider, QObject *parent) { Q_ASSERT(ownAircraftProvider); - return new CSimulatorFsx(ownAircraftProvider, renderedAircraftProvider, parent); + return new CSimulatorFsx(info, ownAircraftProvider, renderedAircraftProvider, parent); } BlackCore::ISimulatorListener *CSimulatorFsxFactory::createListener(QObject *parent) diff --git a/src/plugins/simulator/fsx/simulator_fsxfactory.h b/src/plugins/simulator/fsx/simulator_fsxfactory.h index 564664edf..6152aced9 100644 --- a/src/plugins/simulator/fsx/simulator_fsxfactory.h +++ b/src/plugins/simulator/fsx/simulator_fsxfactory.h @@ -27,16 +27,15 @@ namespace BlackSimPlugin class CSimulatorFsxFactory : public QObject, public BlackCore::ISimulatorFactory { Q_OBJECT - // TODO: @RW, move this string into CProject please Q_PLUGIN_METADATA(IID "org.swift.pilotclient.BlackCore.SimulatorInterface" FILE "simulator_fsx.json") Q_INTERFACES(BlackCore::ISimulatorFactory) public: //! \copydoc BlackCore::ISimulatorFactory::create - virtual BlackCore::ISimulator *create( - BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, - BlackMisc::Simulation::IRemoteAircraftProvider *renderedAircraftProvider, - QObject *parent) override; + virtual BlackCore::ISimulator *create(const BlackMisc::Simulation::CSimulatorPluginInfo &info, + BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, + BlackMisc::Simulation::IRemoteAircraftProvider *renderedAircraftProvider, + QObject *parent) override; //! \copydoc BlackCore::ISimulatorFactory::getListener virtual BlackCore::ISimulatorListener *createListener(QObject *parent = nullptr) override; diff --git a/src/plugins/simulator/xplane/simulator_xplane.cpp b/src/plugins/simulator/xplane/simulator_xplane.cpp index f65a22007..cd4a489d6 100644 --- a/src/plugins/simulator/xplane/simulator_xplane.cpp +++ b/src/plugins/simulator/xplane/simulator_xplane.cpp @@ -24,8 +24,10 @@ using namespace BlackMisc::Simulation; using namespace BlackMisc::Geo; using namespace BlackMisc::Simulation; -namespace { - inline QString xbusServiceName() { +namespace +{ + inline QString xbusServiceName() + { return QStringLiteral("org.swift.xbus"); } } @@ -35,8 +37,12 @@ namespace BlackSimPlugin namespace XPlane { - CSimulatorXPlane::CSimulatorXPlane(IOwnAircraftProvider *ownAircraftProvider, IRemoteAircraftProvider *remoteAircraftProvider, QObject *parent) : - CSimulatorCommon(ownAircraftProvider, remoteAircraftProvider, parent) + CSimulatorXPlane::CSimulatorXPlane( + const BlackMisc::Simulation::CSimulatorPluginInfo &info, + IOwnAircraftProvider *ownAircraftProvider, + IRemoteAircraftProvider *remoteAircraftProvider, + QObject *parent) : + CSimulatorCommon(info, ownAircraftProvider, remoteAircraftProvider, parent) { m_watcher = new QDBusServiceWatcher(this); m_watcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration); @@ -161,16 +167,18 @@ namespace BlackSimPlugin bool CSimulatorXPlane::connectTo() { - if (isConnected()) { + if (isConnected()) + { qWarning("X-Plane already connected"); return true; } - + m_conn = QDBusConnection::sessionBus(); // TODO make this configurable m_service = new CXBusServiceProxy(m_conn, this); m_traffic = new CXBusTrafficProxy(m_conn, this); - - if (m_service->isValid() && m_traffic->isValid() && m_traffic->initialize()) { + + if (m_service->isValid() && m_traffic->isValid() && m_traffic->initialize()) + { // FIXME duplication connect(m_service, &CXBusServiceProxy::aircraftModelChanged, this, &CSimulatorXPlane::ps_emitOwnAircraftModelChanged); connect(m_service, &CXBusServiceProxy::airportsInRangeUpdated, this, &CSimulatorXPlane::ps_setAirportsInRange); @@ -178,7 +186,9 @@ namespace BlackSimPlugin m_watcher->setConnection(m_conn); emitSimulatorCombinedStatus(); return true; - } else { + } + else + { disconnectFrom(); return false; } @@ -192,10 +202,11 @@ namespace BlackSimPlugin bool CSimulatorXPlane::disconnectFrom() { - if (m_traffic) { + if (m_traffic) + { m_traffic->cleanup(); } - + m_conn = QDBusConnection { "default" }; m_watcher->setConnection(m_conn); delete m_service; @@ -237,8 +248,8 @@ namespace BlackSimPlugin /* We do not assert here as status message may come because of network problems */ if (!isConnected()) return; - - // TODO XPLMSpeakString()? + + // TODO XPLMSpeakString()? // http://www.xsquawkbox.net/xpsdk/mediawiki/XPLMSpeakString Q_UNUSED(message); } @@ -247,8 +258,8 @@ namespace BlackSimPlugin { if (!isConnected()) return; - - // TODO XPLMSpeakString()? + + // TODO XPLMSpeakString()? // http://www.xsquawkbox.net/xpsdk/mediawiki/XPLMSpeakString Q_UNUSED(message); } @@ -258,7 +269,7 @@ namespace BlackSimPlugin Q_ASSERT(isConnected()); //! \todo XP driver, function not available CLogMessage(this).error("Function not avialable"); - + return {}; } @@ -427,21 +438,25 @@ namespace BlackSimPlugin return true; } - BlackCore::ISimulator *CSimulatorXPlaneFactory::create(IOwnAircraftProvider *ownAircraftProvider, IRemoteAircraftProvider *renderedAircraftProvider, QObject *parent) + BlackCore::ISimulator *CSimulatorXPlaneFactory::create( + const BlackMisc::Simulation::CSimulatorPluginInfo &info, + IOwnAircraftProvider *ownAircraftProvider, + IRemoteAircraftProvider *renderedAircraftProvider, + QObject *parent) { - return new CSimulatorXPlane(ownAircraftProvider, renderedAircraftProvider, parent); + return new CSimulatorXPlane(info, ownAircraftProvider, renderedAircraftProvider, parent); } - CSimulatorXPlaneListener::CSimulatorXPlaneListener(QObject* parent): ISimulatorListener(parent) + CSimulatorXPlaneListener::CSimulatorXPlaneListener(QObject *parent): ISimulatorListener(parent) { - + } - + void CSimulatorXPlaneListener::start() { if (m_watcher) // already started return; - + m_conn = QDBusConnection::sessionBus(); // TODO make this configurable m_watcher = new QDBusServiceWatcher(xbusServiceName(), m_conn, QDBusServiceWatcher::WatchForRegistration, this); connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, &CSimulatorXPlaneListener::ps_serviceRegistered); @@ -449,12 +464,13 @@ namespace BlackSimPlugin void CSimulatorXPlaneListener::stop() { - if (m_watcher) { + if (m_watcher) + { m_watcher->deleteLater(); m_watcher = nullptr; } } - + void CSimulatorXPlaneListener::ps_serviceRegistered(const QString &serviceName) { if (serviceName == xbusServiceName()) diff --git a/src/plugins/simulator/xplane/simulator_xplane.h b/src/plugins/simulator/xplane/simulator_xplane.h index 9e6920686..a28590209 100644 --- a/src/plugins/simulator/xplane/simulator_xplane.h +++ b/src/plugins/simulator/xplane/simulator_xplane.h @@ -35,10 +35,10 @@ namespace BlackSimPlugin public: //! \copydoc BlackCore::ISimulatorFactory::create(ownAircraftProvider, remoteAircraftProvider, parent) - CSimulatorXPlane( - BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, - BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, - QObject *parent = nullptr); + CSimulatorXPlane(const BlackMisc::Simulation::CSimulatorPluginInfo &info, + BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, + BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, + QObject *parent = nullptr); //! \copydoc BlackCore::ISimulator::isConnected virtual bool isConnected() const override; @@ -171,30 +171,30 @@ namespace BlackSimPlugin } }; - + //! Listener waits for xbus service to show up class CSimulatorXPlaneListener : public BlackCore::ISimulatorListener { Q_OBJECT - + public: //! Constructor - CSimulatorXPlaneListener(QObject* parent); - + CSimulatorXPlaneListener(QObject *parent); + public slots: //! \copydoc BlackCore::ISimulatorListener::start virtual void start() override; - + //! \copydoc BlackCore::ISimulatorListener::stop virtual void stop() override; - + private slots: void ps_serviceRegistered(const QString &serviceName); - + private: QDBusConnection m_conn { "default" }; - QDBusServiceWatcher* m_watcher { nullptr }; - + QDBusServiceWatcher *m_watcher { nullptr }; + }; //! Factory for creating CSimulatorXPlane instance @@ -206,16 +206,16 @@ namespace BlackSimPlugin public: //! \copydoc BlackCore::ISimulatorFactory::create() - virtual BlackCore::ISimulator *create( - BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, - BlackMisc::Simulation::IRemoteAircraftProvider *renderedAircraftProvider, - QObject *parent) override; + virtual BlackCore::ISimulator *create(const BlackMisc::Simulation::CSimulatorPluginInfo &info, + BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, + BlackMisc::Simulation::IRemoteAircraftProvider *renderedAircraftProvider, + QObject *parent) override; //! \copydoc BlackCore::ISimulatorFactory::createListener virtual BlackCore::ISimulatorListener *createListener(QObject *parent = nullptr) override { return new CSimulatorXPlaneListener(parent); } }; - } -} + } // ns +} // ns #endif // guard