Skip to content

Commit

Permalink
feat: Add custom X-Plane dataref for connection state
Browse files Browse the repository at this point in the history
Fixes #304
  • Loading branch information
ltoenning committed Nov 19, 2024
1 parent 1a0b2a8 commit a65cc43
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/plugins/simulator/xplane/simulatorxplane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions src/plugins/simulator/xplane/xswiftbusserviceproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(QLatin1String("getLatitudeDeg"));
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/simulator/xplane/xswiftbusserviceproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/xswiftbus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_library(xswiftbus SHARED
command.h
config.cpp
config.h
custom_datarefs.h
datarefs.h
dbuscallbacks.h
dbusconnection.cpp
Expand Down
24 changes: 24 additions & 0 deletions src/xswiftbus/custom_datarefs.h
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions src/xswiftbus/datarefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,55 @@ namespace XSwiftBus
XPLMDataRef m_ref;
};

/*!
* Class providing a custom variable + dataref
* \tparam DataRefTraits The trait class representing the dataref.
*/
template <class DataRefTraits>
class CustomDataRef
{
public:
//! Constructor
CustomDataRef()
{
if constexpr (std::is_same_v<typename DataRefTraits::type, int>)
{
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<CustomDataRef *>(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>(int d)
{
Expand Down
3 changes: 3 additions & 0 deletions src/xswiftbus/org.swift_project.xswiftbus.service.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ R"XML(<node>
</method>
<method name="resetFrameTotals">
</method>
<method name="setFlightNetworkConnected">
<arg type="b" direction="in"/>
</method>
<method name="getLatitudeDeg">
<arg type="d" direction="out"/>
</method>
Expand Down
11 changes: 11 additions & 0 deletions src/xswiftbus/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ namespace XSwiftBus
this->updateAirportsInRange();
this->updateMessageBoxFromSettings();
m_framePeriodSampler->show();
m_swiftNetworkConnected.set(0);
}

CService::~CService() = default;
Expand Down Expand Up @@ -136,6 +137,8 @@ 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; }
Expand Down Expand Up @@ -550,6 +553,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()); });
Expand Down
5 changes: 5 additions & 0 deletions src/xswiftbus/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "messages.h"
#include "navdatareference.h"
#include "terrainprobe.h"
#include "custom_datarefs.h"
#include <XPLM/XPLMNavigation.h>
#include <string>
#include <chrono>
Expand Down Expand Up @@ -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(); }

Expand Down Expand Up @@ -361,6 +365,7 @@ namespace XSwiftBus
DataRef<xplane::data::sim::graphics::scenery::async_scenery_load_in_progress> m_sceneryIsLoading;
int m_sceneryWasLoading = 0;

CustomDataRef<TSwiftNetworkConnected> m_swiftNetworkConnected;
StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath;
StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao;
StringDataRef<xplane::data::sim::aircraft::view::acf_descrip> m_descrip;
Expand Down

0 comments on commit a65cc43

Please sign in to comment.