diff --git a/src/plugins/simulator/xplane/simulatorxplane.cpp b/src/plugins/simulator/xplane/simulatorxplane.cpp index 67b389b51..5cace5c50 100644 --- a/src/plugins/simulator/xplane/simulatorxplane.cpp +++ b/src/plugins/simulator/xplane/simulatorxplane.cpp @@ -217,6 +217,7 @@ namespace swift::simplugin::xplane void CSimulatorXPlane::setFlightNetworkConnected(bool connected) { if (connected && !this->isShuttingDownOrDisconnected()) { m_serviceProxy->resetFrameTotals(); } + m_serviceProxy->setFlightNetworkConnected(connected); CSimulatorPluginCommon::setFlightNetworkConnected(connected); } diff --git a/src/plugins/simulator/xplane/xswiftbusserviceproxy.cpp b/src/plugins/simulator/xplane/xswiftbusserviceproxy.cpp index a4748c9ff..bc20402c6 100644 --- a/src/plugins/simulator/xplane/xswiftbusserviceproxy.cpp +++ b/src/plugins/simulator/xplane/xswiftbusserviceproxy.cpp @@ -368,6 +368,11 @@ namespace swift::simplugin::xplane void CXSwiftBusServiceProxy::resetFrameTotals() { m_dbusInterface->callDBus(QLatin1String("resetFrameTotals")); } + void CXSwiftBusServiceProxy::setFlightNetworkConnected(bool connected) + { + m_dbusInterface->callDBus(QLatin1String("setFlightNetworkConnected"), connected); + } + double CXSwiftBusServiceProxy::getLatitudeDeg() const { return m_dbusInterface->callDBusRet(QLatin1String("getLatitudeDeg")); diff --git a/src/plugins/simulator/xplane/xswiftbusserviceproxy.h b/src/plugins/simulator/xplane/xswiftbusserviceproxy.h index c4dbccd1b..4dd703ea6 100644 --- a/src/plugins/simulator/xplane/xswiftbusserviceproxy.h +++ b/src/plugins/simulator/xplane/xswiftbusserviceproxy.h @@ -219,6 +219,9 @@ namespace swift::simplugin::xplane //! \copydoc XSwiftBus::CService::resetFrameTotals void resetFrameTotals(); + //! \copydoc XSwiftBus::CService::setFlightNetworkConnected + void setFlightNetworkConnected(bool connected); + //! @{ //! \copydoc XSwiftBus::CService::getLatitudeDeg double getLatitudeDeg() const; diff --git a/src/xswiftbus/CMakeLists.txt b/src/xswiftbus/CMakeLists.txt index d0b526540..1cb957923 100644 --- a/src/xswiftbus/CMakeLists.txt +++ b/src/xswiftbus/CMakeLists.txt @@ -12,6 +12,7 @@ add_library(xswiftbus SHARED command.h config.cpp config.h + custom_datarefs.h datarefs.h dbuscallbacks.h dbusconnection.cpp diff --git a/src/xswiftbus/custom_datarefs.h b/src/xswiftbus/custom_datarefs.h new file mode 100644 index 000000000..626c93310 --- /dev/null +++ b/src/xswiftbus/custom_datarefs.h @@ -0,0 +1,24 @@ +// SPDX-FileCopyrightText: Copyright (C) swift Project Community / Contributors +// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1 + +#ifndef SWIFT_SIM_XSWIFTBUS_CUSTOM_DATAREFS_H +#define SWIFT_SIM_XSWIFTBUS_CUSTOM_DATAREFS_H + +namespace XSwiftBus +{ + //! Is swift connected to a network? + struct TSwiftNetworkConnected + { + //! Dataref name + static constexpr const char *name() { return "org/swift-project/xswiftbus/connected"; } + //! Can be written to? + static constexpr bool writable = false; + //! Dataref type + using type = int; + //! Not an array dataref + static constexpr bool is_array = false; + }; + +} // namespace XSwiftBus + +#endif // SWIFT_SIM_XSWIFTBUS_CUSTOM_DATAREFS_H diff --git a/src/xswiftbus/datarefs.h b/src/xswiftbus/datarefs.h index b2a430aaf..abf02d869 100644 --- a/src/xswiftbus/datarefs.h +++ b/src/xswiftbus/datarefs.h @@ -223,6 +223,58 @@ namespace XSwiftBus XPLMDataRef m_ref; }; + /*! + * Class providing a custom variable + dataref + * \tparam DataRefTraits The trait class representing the dataref. + */ + template + class CustomDataRef + { + public: + //! Constructor + CustomDataRef() + { + if constexpr (std::is_same_v) + { + m_ref = XPLMRegisterDataAccessor(DataRefTraits::name(), xplmType_Int, 0, read, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, this, NULL); + } + else { static_assert(false, "Unsupported type"); } + if (!m_ref) + { + XPLMDebugString("Missing dataref:"); + XPLMDebugString(DataRefTraits::name()); + XPLMDebugString("\n"); + } + } + + CustomDataRef(const CustomDataRef &) = delete; + CustomDataRef &operator=(const CustomDataRef &) = delete; + CustomDataRef(CustomDataRef &&other) = default; + CustomDataRef &operator=(CustomDataRef &&other) = default; + ~CustomDataRef() { XPLMUnregisterDataAccessor(m_ref); } + + static typename DataRefTraits::type read(void *refcon) + { + return reinterpret_cast(refcon)->get(); + } + + //! True if the dataref exists + bool isValid() const { return m_ref != nullptr; } + + //! Set the value + void set(typename DataRefTraits::type val) + { + m_datarefVal = val; + } + + //! Get the value + typename DataRefTraits::type get() const { return m_datarefVal; } + + XPLMDataRef m_ref; + typename DataRefTraits::type m_datarefVal; + }; + template <> inline void DataRefImpl::implSet(int d) { diff --git a/src/xswiftbus/org.swift_project.xswiftbus.service.xml b/src/xswiftbus/org.swift_project.xswiftbus.service.xml index ef6bb9a6b..9a0898725 100644 --- a/src/xswiftbus/org.swift_project.xswiftbus.service.xml +++ b/src/xswiftbus/org.swift_project.xswiftbus.service.xml @@ -115,6 +115,9 @@ R"XML( + + + diff --git a/src/xswiftbus/service.cpp b/src/xswiftbus/service.cpp index b9645df9d..b8306b32a 100644 --- a/src/xswiftbus/service.cpp +++ b/src/xswiftbus/service.cpp @@ -94,6 +94,7 @@ namespace XSwiftBus this->updateAirportsInRange(); this->updateMessageBoxFromSettings(); m_framePeriodSampler->show(); + m_swiftNetworkConnected.set(0); } CService::~CService() = default; @@ -136,6 +137,11 @@ namespace XSwiftBus } } + void CService::setFlightNetworkConnected(bool connected) + { + m_swiftNetworkConnected.set(connected); + } + void CService::addTextMessage(const std::string &text, double red, double green, double blue) { if (text.empty()) { return; } @@ -550,6 +556,14 @@ namespace XSwiftBus maybeSendEmptyDBusReply(wantsReply, sender, serial); queueDBusCall([=]() { resetFrameTotals(); }); } + else if (message.getMethodName() == "setFlightNetworkConnected") + { + maybeSendEmptyDBusReply(wantsReply, sender, serial); + bool connected = false; + message.beginArgumentRead(); + message.getArgument(connected); + queueDBusCall([=]() { setFlightNetworkConnected(connected); }); + } else if (message.getMethodName() == "getLatitudeDeg") { queueDBusCall([=]() { sendDBusReply(sender, serial, getLatitudeDeg()); }); diff --git a/src/xswiftbus/service.h b/src/xswiftbus/service.h index 45b985470..b6f746aab 100644 --- a/src/xswiftbus/service.h +++ b/src/xswiftbus/service.h @@ -16,6 +16,7 @@ #include "messages.h" #include "navdatareference.h" #include "terrainprobe.h" +#include "custom_datarefs.h" #include #include #include @@ -123,6 +124,9 @@ namespace XSwiftBus //! Reset the monitoring of total miles and minutes lost due to low frame rate. void resetFrameTotals(); + //! Set the current connection state + void setFlightNetworkConnected(bool connected); + //! Get aircraft latitude in degrees double getLatitudeDeg() const { return m_latitude.get(); } @@ -361,6 +365,7 @@ namespace XSwiftBus DataRef m_sceneryIsLoading; int m_sceneryWasLoading = 0; + CustomDataRef m_swiftNetworkConnected; StringDataRef m_liveryPath; StringDataRef m_icao; StringDataRef m_descrip;