From 45f48f75944a6ce72fe70c7a8b2f854057b53525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Garapich?= Date: Tue, 31 Mar 2015 20:10:17 +0200 Subject: [PATCH] Listen for X-Plane to start --- src/blackcore/context_runtime.cpp | 11 ++--- src/blackcore/context_simulator.h | 6 +++ src/blackcore/context_simulator_impl.cpp | 53 +++++++++++++++++++++++ src/blackcore/context_simulator_impl.h | 10 +++++ src/blackcore/context_simulator_proxy.cpp | 10 +++++ src/blackcore/context_simulator_proxy.h | 6 +++ 6 files changed, 91 insertions(+), 5 deletions(-) diff --git a/src/blackcore/context_runtime.cpp b/src/blackcore/context_runtime.cpp index ca8431cb4..830834beb 100644 --- a/src/blackcore/context_runtime.cpp +++ b/src/blackcore/context_runtime.cpp @@ -196,11 +196,12 @@ namespace BlackCore if (this->m_contextSettings) { connect(this->m_contextSettings, &IContextSettings::changedSettings, this->m_contextSimulator, &IContextSimulator::settingsChanged); - if (!this->m_contextSimulator->loadSimulatorPluginFromSettings()) - { - CLogMessage(this).warning("No simulator plugin loaded"); - } - times.insert("Post setup, load sim. plugin", time.restart()); +// if (!this->m_contextSimulator->loadSimulatorPluginFromSettings()) +// { +// CLogMessage(this).warning("No simulator plugin loaded"); +// } + this->m_contextSimulator->listenForSimulatorFromSettings(); + times.insert("Post setup, load sim. listener(s)", time.restart()); } } diff --git a/src/blackcore/context_simulator.h b/src/blackcore/context_simulator.h index 66d970ccc..443b0fd47 100644 --- a/src/blackcore/context_simulator.h +++ b/src/blackcore/context_simulator.h @@ -170,6 +170,12 @@ namespace BlackCore //! Load specific simulator plugin as set in settings virtual bool loadSimulatorPluginFromSettings() = 0; + + //! Listen for the specific simulator to start, load plugin automatically + virtual void listenForSimulator(const BlackSim::CSimulatorInfo &simulatorInfo) = 0; + + //! Listen for simulator as set in settings + virtual void listenForSimulatorFromSettings() = 0; //! Unload simulator plugin virtual void unloadSimulatorPlugin() = 0; diff --git a/src/blackcore/context_simulator_impl.cpp b/src/blackcore/context_simulator_impl.cpp index c89d31249..2a42a2feb 100644 --- a/src/blackcore/context_simulator_impl.cpp +++ b/src/blackcore/context_simulator_impl.cpp @@ -356,6 +356,44 @@ namespace BlackCore return false; } } + + void CContextSimulator::listenForSimulator(const CSimulatorInfo &simulatorInfo) + { + Q_ASSERT(this->getIContextApplication()); + Q_ASSERT(this->getIContextApplication()->isUsingImplementingObject()); + + if (this->m_simulator && this->m_simulator->getSimulatorInfo() == simulatorInfo) { // already loaded + qWarning("Cannot listen for simulator while still plugin is loaded"); + return; + } + + if (simulatorInfo.isUnspecified()) { + return; + } + + // warning if we do not have any plugins + if (m_simulatorListeners.isEmpty()) { + CLogMessage(this).error("No simulator listeners"); + return; + } + + if (!m_simulatorListeners.contains(simulatorInfo)) { + CLogMessage(this).error("Listener not found for '%1'") << simulatorInfo.toQString(true); + return; + } else { + ISimulatorListener *listener = m_simulatorListeners[simulatorInfo]; + Q_ASSERT(listener); + listener->start(); + CLogMessage(this).info("Listening for simulator: '%1'") << simulatorInfo.toQString(true); + } + + } + + void CContextSimulator::listenForSimulatorFromSettings() + { + Q_ASSERT(this->getIContextSettings()); + listenForSimulator(getIContextSettings()->getSimulatorSettings().getSelectedPlugin()); + } void CContextSimulator::unloadSimulatorPlugin() { @@ -505,6 +543,12 @@ namespace BlackCore if (!this->m_simulator) return false; return this->m_simulator->isSimulating(); } + + void CContextSimulator::ps_simulatorStarted(CSimulatorInfo simulatorInfo) + { + CLogMessage(this).info("Simulator %1 started.") << simulatorInfo.toQString(); + loadSimulatorPlugin(simulatorInfo); + } void CContextSimulator::findSimulatorPlugins() { @@ -532,6 +576,15 @@ namespace BlackCore { CSimulatorInfo simulatorInfo = factory->getSimulatorInfo(); m_simulatorFactories.insert(factory); + + ISimulatorListener *listener = factory->createListener(this); + Q_ASSERT(listener); + Q_ASSERT(listener->parent() == this); // requirement + m_simulatorListeners.insert(simulatorInfo, listener); + + /* Will not happen unless start() is called */ + connect(listener, &ISimulatorListener::simulatorStarted, this, &CContextSimulator::ps_simulatorStarted); + CLogMessage(this).info("Loaded plugin: %1") << simulatorInfo.toQString(); } } diff --git a/src/blackcore/context_simulator_impl.h b/src/blackcore/context_simulator_impl.h index 514552513..b3efb768f 100644 --- a/src/blackcore/context_simulator_impl.h +++ b/src/blackcore/context_simulator_impl.h @@ -124,6 +124,12 @@ namespace BlackCore //! \copydoc IContextSimulator::loadSimulatorPluginFromSettings() virtual bool loadSimulatorPluginFromSettings() override; + + //! \copydoc IContextSimulator::listenForSimulator() + virtual void listenForSimulator(const BlackSim::CSimulatorInfo &simulatorInfo) override; + + //! \copydoc IContextSimulator::listenForSimulatorFromSettings() + virtual void listenForSimulatorFromSettings() override; //! \copydoc IContextSimulator::unloadSimulatorPlugin() virtual void unloadSimulatorPlugin() override; @@ -164,6 +170,9 @@ namespace BlackCore //! Text message received void ps_textMessagesReceived(const BlackMisc::Network::CTextMessageList &textMessages); + + //! Listener reports the simulator has started + void ps_simulatorStarted(BlackSim::CSimulatorInfo simulatorInfo); //! Simulator has changed cockpit void ps_cockitChangedFromSim(const BlackMisc::Simulation::CSimulatedAircraft &ownAircraft); @@ -185,6 +194,7 @@ namespace BlackCore BlackCore::ISimulator *m_simulator = nullptr; //!< simulator plugin QDir m_pluginsDir; QSet m_simulatorFactories; + QMap m_simulatorListeners; QFuture m_canConnectResult; }; diff --git a/src/blackcore/context_simulator_proxy.cpp b/src/blackcore/context_simulator_proxy.cpp index d69265be6..f44fb2dae 100644 --- a/src/blackcore/context_simulator_proxy.cpp +++ b/src/blackcore/context_simulator_proxy.cpp @@ -186,6 +186,16 @@ namespace BlackCore { return m_dBusInterface->callDBusRet(QLatin1Literal("loadSimulatorPluginFromSettings")); } + + void CContextSimulatorProxy::listenForSimulator(const CSimulatorInfo &simulatorInfo) + { + m_dBusInterface->callDBus(QLatin1Literal("listenForSimulator"), simulatorInfo); + } + + void CContextSimulatorProxy::listenForSimulatorFromSettings() + { + m_dBusInterface->callDBus(QLatin1Literal("listenForSimulatorFromSettings")); + } void CContextSimulatorProxy::unloadSimulatorPlugin() { diff --git a/src/blackcore/context_simulator_proxy.h b/src/blackcore/context_simulator_proxy.h index 10d13fc10..3bb8df22f 100644 --- a/src/blackcore/context_simulator_proxy.h +++ b/src/blackcore/context_simulator_proxy.h @@ -127,6 +127,12 @@ namespace BlackCore //! \copydoc IContextSimulator::loadSimulatorPluginFromSettings() virtual bool loadSimulatorPluginFromSettings(); + + //! \copydoc IContextSimulator::listenForSimulator() + virtual void listenForSimulator(const BlackSim::CSimulatorInfo &simulatorInfo) override; + + //! \copydoc IContextSimulator::listenForSimulatorFromSettings() + virtual void listenForSimulatorFromSettings() override; //! \copydoc IContextSimulator::unloadSimulatorPlugin() virtual void unloadSimulatorPlugin() override;