From ea5b4962fb5fe93fc908b19fe555ea3a7f590b39 Mon Sep 17 00:00:00 2001 From: Lars Toenning Date: Sun, 17 Nov 2024 14:44:34 +0100 Subject: [PATCH] feat: Add custom X-Plane dataref for connection state Fixes #304 --- .../simulator/xplane/simulatorxplane.cpp | 1 + .../xplane/xswiftbusserviceproxy.cpp | 5 ++ .../simulator/xplane/xswiftbusserviceproxy.h | 3 ++ src/xswiftbus/CMakeLists.txt | 1 + src/xswiftbus/custom_datarefs.h | 24 +++++++++ src/xswiftbus/datarefs.h | 52 +++++++++++++++++++ .../org.swift_project.xswiftbus.service.xml | 3 ++ src/xswiftbus/service.cpp | 14 +++++ src/xswiftbus/service.h | 5 ++ 9 files changed, 108 insertions(+) create mode 100644 src/xswiftbus/custom_datarefs.h diff --git a/src/plugins/simulator/xplane/simulatorxplane.cpp b/src/plugins/simulator/xplane/simulatorxplane.cpp index 67b389b518..5cace5c507 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 a4748c9ff4..bc20402c6c 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 c4dbccd1b1..4dd703ea60 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 d0b5265405..1cb9579239 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 0000000000..626c93310f --- /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 b2a430aaf4..abf02d869c 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 ef6bb9a6b0..9a0898725f 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 b9645df9db..b8306b32aa 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 45b9854708..b6f746aabc 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;