Skip to content

Commit a65cc43

Browse files
committed
feat: Add custom X-Plane dataref for connection state
Fixes #304
1 parent 1a0b2a8 commit a65cc43

9 files changed

+102
-0
lines changed

src/plugins/simulator/xplane/simulatorxplane.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ namespace swift::simplugin::xplane
217217
void CSimulatorXPlane::setFlightNetworkConnected(bool connected)
218218
{
219219
if (connected && !this->isShuttingDownOrDisconnected()) { m_serviceProxy->resetFrameTotals(); }
220+
m_serviceProxy->setFlightNetworkConnected(connected);
220221
CSimulatorPluginCommon::setFlightNetworkConnected(connected);
221222
}
222223

src/plugins/simulator/xplane/xswiftbusserviceproxy.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ namespace swift::simplugin::xplane
368368

369369
void CXSwiftBusServiceProxy::resetFrameTotals() { m_dbusInterface->callDBus(QLatin1String("resetFrameTotals")); }
370370

371+
void CXSwiftBusServiceProxy::setFlightNetworkConnected(bool connected)
372+
{
373+
m_dbusInterface->callDBus(QLatin1String("setFlightNetworkConnected"), connected);
374+
}
375+
371376
double CXSwiftBusServiceProxy::getLatitudeDeg() const
372377
{
373378
return m_dbusInterface->callDBusRet<double>(QLatin1String("getLatitudeDeg"));

src/plugins/simulator/xplane/xswiftbusserviceproxy.h

+3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ namespace swift::simplugin::xplane
219219
//! \copydoc XSwiftBus::CService::resetFrameTotals
220220
void resetFrameTotals();
221221

222+
//! \copydoc XSwiftBus::CService::setFlightNetworkConnected
223+
void setFlightNetworkConnected(bool connected);
224+
222225
//! @{
223226
//! \copydoc XSwiftBus::CService::getLatitudeDeg
224227
double getLatitudeDeg() const;

src/xswiftbus/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_library(xswiftbus SHARED
1212
command.h
1313
config.cpp
1414
config.h
15+
custom_datarefs.h
1516
datarefs.h
1617
dbuscallbacks.h
1718
dbusconnection.cpp

src/xswiftbus/custom_datarefs.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-FileCopyrightText: Copyright (C) swift Project Community / Contributors
2+
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3+
4+
#ifndef SWIFT_SIM_XSWIFTBUS_CUSTOM_DATAREFS_H
5+
#define SWIFT_SIM_XSWIFTBUS_CUSTOM_DATAREFS_H
6+
7+
namespace XSwiftBus
8+
{
9+
//! Is swift connected to a network?
10+
struct TSwiftNetworkConnected
11+
{
12+
//! Dataref name
13+
static constexpr const char *name() { return "org/swift-project/xswiftbus/connected"; }
14+
//! Can be written to?
15+
static constexpr bool writable = false;
16+
//! Dataref type
17+
using type = int;
18+
//! Not an array dataref
19+
static constexpr bool is_array = false;
20+
};
21+
22+
} // namespace XSwiftBus
23+
24+
#endif // SWIFT_SIM_XSWIFTBUS_CUSTOM_DATAREFS_H

src/xswiftbus/datarefs.h

+49
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,55 @@ namespace XSwiftBus
223223
XPLMDataRef m_ref;
224224
};
225225

226+
/*!
227+
* Class providing a custom variable + dataref
228+
* \tparam DataRefTraits The trait class representing the dataref.
229+
*/
230+
template <class DataRefTraits>
231+
class CustomDataRef
232+
{
233+
public:
234+
//! Constructor
235+
CustomDataRef()
236+
{
237+
if constexpr (std::is_same_v<typename DataRefTraits::type, int>)
238+
{
239+
m_ref = XPLMRegisterDataAccessor(DataRefTraits::name(), xplmType_Int, 0, read, NULL, NULL, NULL, NULL,
240+
NULL, NULL, NULL, NULL, NULL, NULL, NULL, this, NULL);
241+
}
242+
else { static_assert(false, "Unsupported type"); }
243+
if (!m_ref)
244+
{
245+
XPLMDebugString("Missing dataref:");
246+
XPLMDebugString(DataRefTraits::name());
247+
XPLMDebugString("\n");
248+
}
249+
}
250+
251+
CustomDataRef(const CustomDataRef &) = delete;
252+
CustomDataRef &operator=(const CustomDataRef &) = delete;
253+
CustomDataRef(CustomDataRef &&other) = default;
254+
CustomDataRef &operator=(CustomDataRef &&other) = default;
255+
~CustomDataRef() { XPLMUnregisterDataAccessor(m_ref); }
256+
257+
static typename DataRefTraits::type read(void *refcon)
258+
{
259+
return reinterpret_cast<CustomDataRef *>(refcon)->get();
260+
}
261+
262+
//! True if the dataref exists
263+
bool isValid() const { return m_ref != nullptr; }
264+
265+
//! Set the value
266+
void set(typename DataRefTraits::type val) { m_datarefVal = val; }
267+
268+
//! Get the value
269+
typename DataRefTraits::type get() const { return m_datarefVal; }
270+
271+
XPLMDataRef m_ref;
272+
typename DataRefTraits::type m_datarefVal;
273+
};
274+
226275
template <>
227276
inline void DataRefImpl::implSet<int>(int d)
228277
{

src/xswiftbus/org.swift_project.xswiftbus.service.xml

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ R"XML(<node>
115115
</method>
116116
<method name="resetFrameTotals">
117117
</method>
118+
<method name="setFlightNetworkConnected">
119+
<arg type="b" direction="in"/>
120+
</method>
118121
<method name="getLatitudeDeg">
119122
<arg type="d" direction="out"/>
120123
</method>

src/xswiftbus/service.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ namespace XSwiftBus
9494
this->updateAirportsInRange();
9595
this->updateMessageBoxFromSettings();
9696
m_framePeriodSampler->show();
97+
m_swiftNetworkConnected.set(0);
9798
}
9899

99100
CService::~CService() = default;
@@ -136,6 +137,8 @@ namespace XSwiftBus
136137
}
137138
}
138139

140+
void CService::setFlightNetworkConnected(bool connected) { m_swiftNetworkConnected.set(connected); }
141+
139142
void CService::addTextMessage(const std::string &text, double red, double green, double blue)
140143
{
141144
if (text.empty()) { return; }
@@ -550,6 +553,14 @@ namespace XSwiftBus
550553
maybeSendEmptyDBusReply(wantsReply, sender, serial);
551554
queueDBusCall([=]() { resetFrameTotals(); });
552555
}
556+
else if (message.getMethodName() == "setFlightNetworkConnected")
557+
{
558+
maybeSendEmptyDBusReply(wantsReply, sender, serial);
559+
bool connected = false;
560+
message.beginArgumentRead();
561+
message.getArgument(connected);
562+
queueDBusCall([=]() { setFlightNetworkConnected(connected); });
563+
}
553564
else if (message.getMethodName() == "getLatitudeDeg")
554565
{
555566
queueDBusCall([=]() { sendDBusReply(sender, serial, getLatitudeDeg()); });

src/xswiftbus/service.h

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "messages.h"
1717
#include "navdatareference.h"
1818
#include "terrainprobe.h"
19+
#include "custom_datarefs.h"
1920
#include <XPLM/XPLMNavigation.h>
2021
#include <string>
2122
#include <chrono>
@@ -123,6 +124,9 @@ namespace XSwiftBus
123124
//! Reset the monitoring of total miles and minutes lost due to low frame rate.
124125
void resetFrameTotals();
125126

127+
//! Set the current connection state
128+
void setFlightNetworkConnected(bool connected);
129+
126130
//! Get aircraft latitude in degrees
127131
double getLatitudeDeg() const { return m_latitude.get(); }
128132

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

368+
CustomDataRef<TSwiftNetworkConnected> m_swiftNetworkConnected;
364369
StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath;
365370
StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao;
366371
StringDataRef<xplane::data::sim::aircraft::view::acf_descrip> m_descrip;

0 commit comments

Comments
 (0)