-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathxplaneplugin.cpp
405 lines (369 loc) · 15.6 KB
/
xplaneplugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "xplaneplugin.h"
#include "../extplane-server/datarefs/dataref.h"
#include "../extplane-server/datarefs/floatdataref.h"
#include "../extplane-server/datarefs/floatarraydataref.h"
#include "../extplane-server/datarefs/intdataref.h"
#include "../extplane-server/datarefs/intarraydataref.h"
#include "../extplane-server/datarefs/doubledataref.h"
#include "../extplane-server/datarefs/datadataref.h"
#include <console.h>
#include "customdata/navcustomdata.h"
#include "customdata/atccustomdata.h"
#include <clocale>
#include <cstring>
#include <XPLMUtilities.h>
XPlanePlugin::XPlanePlugin(QObject *parent) : QObject(parent) { }
XPlanePlugin::~XPlanePlugin() { }
float XPlanePlugin::flightLoop(float inElapsedSinceLastCall,
float inElapsedTimeSinceLastFlightLoop,
int inCounter,
void *inRefcon) {
Q_UNUSED(inElapsedSinceLastCall)
Q_UNUSED(inElapsedTimeSinceLastFlightLoop)
Q_UNUSED(inCounter)
Q_UNUSED(inRefcon)
// Tell each dataref to update its value through the XPLM api
for(DataRef *ref : m_refs) updateDataRef(ref);
if(m_server) m_server->flightLoop();
// Tell Qt to process it's own runloop
m_app->processEvents();
return m_flightLoopInterval;
}
int XPlanePlugin::pluginStart(char * outName, char * outSig, char *outDesc) {
// Set plugin info
INFO << "Plugin started";
std::strcpy(outName, "ExtPlane");
std::strcpy(outSig, "org.vranki.extplaneplugin");
std::strcpy(outDesc, "Read and write X-Plane datarefs from external programs on TCP port " EXTPLANE_PORT_STR " with protocol " EXTPLANE_PROTOCOL_STR " version " EXTPLANE_VERSION_STR);
Q_ASSERT(!m_app);
// Init application and server
m_app = new QCoreApplication(argc, &argv);
setlocale(LC_NUMERIC, "C"); // See http://stackoverflow.com/questions/25661295/why-does-qcoreapplication-call-setlocalelc-all-by-default-on-unix-linux
return 1;
}
DataRef* XPlanePlugin::subscribeRef(QString &name) {
// Search in list of already subscribed datarefs - if found return that
for(DataRef *ref : m_refs) {
if(ref->name() == name) {
DEBUG << "Already subscribed to " << name;
ref->setSubscriberCount(ref->subscriberCount() + 1);
return ref;
}
}
// Not yet subscribed - create a new dataref
QString realName = refNameWithoutModifiers(name);
XPLMDataRef ref = XPLMFindDataRef(realName.toLatin1());
if(ref) {
XPLMDataTypeID refType = XPLMGetDataRefTypes(ref);
DataRef *dr = nullptr;
if(refType & xplmType_Double) {
dr = new DoubleDataRef(this, name, ref);
} else if(refType & xplmType_Float) {
dr = new FloatDataRef(this, name, ref);
} else if(refType & xplmType_Int) {
dr = new IntDataRef(this, name, ref);
} else if (refType & xplmType_FloatArray) {
dr = new FloatArrayDataRef(this, name, ref);
} else if (refType & xplmType_IntArray) {
dr = new IntArrayDataRef(this, name, ref);
} else if (refType & xplmType_Data) {
dr = new DataDataRef(this, name, ref);
}
if(dr) {
dr->setSubscriberCount(1);
dr->setWritable(XPLMCanWriteDataRef(ref));
DEBUG << "Subscribed to ref " << dr->name()
<< ", type: " << dr->typeString()
<< ", writable:" << dr->isWritable();
m_refs.insert(dr);
return dr;
} else {
m_server->extplaneWarning(QString("Dataref type %1 not supported").arg(refType));
}
} else {
m_server->extplaneWarning(QString("Can't find dataref %1").arg(name));
}
return nullptr;
}
void XPlanePlugin::unsubscribeRef(DataRef *ref) {
Q_ASSERT(m_refs.find(ref) != m_refs.end());
ref->setSubscriberCount(ref->subscriberCount() - 1);
if(ref->subscriberCount() == 0) {
m_refs.erase(ref);
DEBUG << "Ref " << ref->name() << " not subscribed by anyone - removing.";
ref->deleteLater();
}
}
// Called for each ref on every flight loop
void XPlanePlugin::updateDataRef(DataRef *ref) {
Q_ASSERT(ref);
switch (ref->type()) {
case extplaneRefTypeFloat:
{
float newValue = XPLMGetDataf(ref->ref());
qobject_cast<FloatDataRef*>(ref)->updateValue(newValue);
break;
}
case extplaneRefTypeFloatArray:
{
FloatArrayDataRef *faRef = qobject_cast<FloatArrayDataRef*>(ref);
int arrayLength = faRef->value().size();
if(arrayLength == 0) {
arrayLength = XPLMGetDatavf(faRef->ref(), nullptr, 0, 0);
faRef->setLength(arrayLength);
}
int valuesCopied = XPLMGetDatavf(faRef->ref(), faRef->valueArray(), 0, arrayLength);
Q_ASSERT(valuesCopied == arrayLength);
faRef->updateValue();
break;
}
case extplaneRefTypeIntArray:
{
IntArrayDataRef *iaRef = qobject_cast<IntArrayDataRef*>(ref);
int arrayLength = iaRef->value().size();
if(arrayLength <= 0) {
arrayLength = XPLMGetDatavi(iaRef->ref(), nullptr, 0, 0);
iaRef->setLength(arrayLength);
}
int valuesCopied = XPLMGetDatavi(iaRef->ref(), iaRef->valueArray(), 0, arrayLength);
Q_ASSERT(valuesCopied == arrayLength);
iaRef->updateValue();
break;
}
case extplaneRefTypeInt:
{
int newValue = XPLMGetDatai(ref->ref());
qobject_cast<IntDataRef*>(ref)->updateValue(newValue);
break;
}
case extplaneRefTypeDouble:
{
double newValue = XPLMGetDatad(ref->ref());
qobject_cast<DoubleDataRef*>(ref)->updateValue(newValue);
break;
}
case extplaneRefTypeData:
{
DataDataRef *bRef = qobject_cast<DataDataRef*>(ref);
Q_ASSERT(bRef);
int arrayLength = XPLMGetDatab(ref->ref(), nullptr, 0, 0);
bRef->setLength(arrayLength);
int valuesCopied = XPLMGetDatab(ref->ref(), bRef->newValue().data(), 0, arrayLength);
Q_ASSERT(valuesCopied == arrayLength);
bRef->updateValue();
break;
}
default:
break;
}
}
void XPlanePlugin::keyStroke(int keyid) {
DEBUG << keyid;
XPLMCommandKeyStroke(keyid);
}
void XPlanePlugin::buttonPress(int buttonid) {
DEBUG << buttonid;
XPLMCommandButtonPress(buttonid);
}
void XPlanePlugin::buttonRelease(int buttonid) {
DEBUG << buttonid;
XPLMCommandButtonRelease(buttonid);
}
void XPlanePlugin::changeDataRef(DataRef *ref)
{
if(!ref->isWritable()) {
m_server->extplaneWarning(QString("Tried to write read-only dataref %1").arg(ref->name()));
return;
}
switch (ref->type()) {
case extplaneRefTypeFloat:
{
XPLMSetDataf(ref->ref(), qobject_cast<FloatDataRef*>(ref)->value());
break;
}
case extplaneRefTypeFloatArray:
{
FloatArrayDataRef *faRef = qobject_cast<FloatArrayDataRef*>(ref);
XPLMSetDatavf(ref->ref(), faRef->valueArray(), 0, faRef->value().size());
break;
}
case extplaneRefTypeIntArray:
{
IntArrayDataRef *iaRef = qobject_cast<IntArrayDataRef*>(ref);
XPLMSetDatavi(ref->ref(), iaRef->valueArray(), 0, iaRef->value().size());
break;
}
case extplaneRefTypeInt:
{
XPLMSetDatai(ref->ref(), qobject_cast<IntDataRef*>(ref)->value());
break;
}
case extplaneRefTypeDouble:
{
XPLMSetDatad(ref->ref(), qobject_cast<DoubleDataRef*>(ref)->value());
break;
}
case extplaneRefTypeData:
{
QString value = ref->valueString();
Q_ASSERT(value.count("\"") >= 2);
value = value.mid(value.indexOf("\"") + 1);
value = value.mid(0, value.lastIndexOf("\""));
// Check ref length
int length = XPLMGetDatab(ref->ref(), nullptr, 0, 0);
// Create a bytearray of correct size and fill with 0's
QByteArray valueBa(length, 0);
valueBa.insert(0, value.toUtf8());
// Make sure size is still correct
valueBa.resize(length);
XPLMSetDatab(ref->ref(), valueBa.data(), 0, valueBa.length());
break;
}
default:
break;
}
}
void XPlanePlugin::command(QString &name, extplaneCommandType type)
{
XPLMCommandRef cmdRef = XPLMFindCommand(name.toUtf8().constData());
if (cmdRef) {
switch (type) {
case extplaneCommandTypeOnce:
XPLMCommandOnce(cmdRef);
break;
case extplaneCommandTypeBegin:
XPLMCommandBegin(cmdRef);
break;
case extplaneCommandTypeEnd:
XPLMCommandEnd(cmdRef);
break;
default:
break;
}
} else {
m_server->extplaneWarning(QString("Command %1 not found").arg(name));
}
}
void XPlanePlugin::setFlightLoopInterval(float newInterval) {
m_flightLoopInterval = newInterval;
DEBUG << "New interval" << m_flightLoopInterval;
}
QString XPlanePlugin::refNameWithoutModifiers(QString &original) {
return original.contains(":") ? original.left(original.indexOf(":")) : original;
}
/**
* @brief XPlanePlugin::loadSituation
* @param name : situation file location -
* relative to XPlane root folder, e.g. Output/situations/XXX.sit
*/
bool XPlanePlugin::loadSituation(QString sitFileLocation) {
// Remove quotes from filename
sitFileLocation = sitFileLocation.replace("\"", "");
// XPLMLoadDataFile's return value is not documented, assuming it returns
// 1 on success and 0 on fail. TODO: Check this.
return XPLMLoadDataFile(xplm_DataFile_Situation, sitFileLocation.toUtf8().data());
}
/**
* @brief XPlanePlugin::addFMSEntryLatLon
* @param fmsEntryLine - string in format "id,latitude,longitude,altitude"
* example: "1,50.0267,8.51,198"
*/
void XPlanePlugin::addFMSEntryLatLon(QString fmsEntryLine){
//verify if string is in valid format
int commaCount = fmsEntryLine.count(QLatin1Char(','));
if(commaCount != 3){
return;
}
QStringList params = fmsEntryLine.split(",", QString::SkipEmptyParts);
int id = params.value(0).toInt();
float lat = params.value(1).toFloat();
float lon = params.value(2).toFloat();
int alt = params.value(3).toInt();
XPLMSetFMSEntryLatLon(id, lat, lon, alt);
}
/**
* @brief XPlanePlugin::clearAllFmsEntries
* removes all entries inserted to FMS
*/
void XPlanePlugin::clearAllFmsEntries(){
int count = XPLMCountFMSEntries();
for(int i = 0; i < count;i++){
XPLMClearFMSEntry(i);
}
}
/**
* @brief XPlanePlugin::setDestinationFmsEntry
* @param index entry the FMS is flying the aircraft toward.
*/
void XPlanePlugin::setDestinationFmsEntry(int index) {
XPLMSetDestinationFMSEntry(index);
}
void XPlanePlugin::pluginStop() {
DEBUG;
Q_ASSERT(m_app);
delete m_app;
m_app = nullptr;
}
int XPlanePlugin::pluginEnable() {
m_menu_container_idx = XPLMAppendMenuItem(XPLMFindPluginsMenu(), "ExtPlane", nullptr, 0);
m_menu_id = XPLMCreateMenu("ExtPlane", XPLMFindPluginsMenu(), m_menu_container_idx, nullptr, nullptr);
XPLMAppendMenuItem(m_menu_id, "Listening on TCP port " EXTPLANE_PORT_STR " with protocol " EXTPLANE_PROTOCOL_STR " version " EXTPLANE_VERSION_STR ". No GUI yet.", nullptr, 1);
Q_ASSERT(!m_server);
m_server = new TcpServer(this, this);
connect(m_server, &TcpServer::setFlightLoopInterval, this, &XPlanePlugin::setFlightLoopInterval);
// Log that we have started
XPLMDebugString ("ExtPlane listening on TCP port " EXTPLANE_PORT_STR " with protocol " EXTPLANE_PROTOCOL_STR " version " EXTPLANE_VERSION_STR "\n");
// Register the nav custom data accessors
XPLMRegisterDataAccessor("extplane/navdata/5km",
xplmType_Data, // The types we support
0, // Writable
nullptr, nullptr, // Integer accessors
nullptr, nullptr, // Float accessors
nullptr, nullptr, // Doubles accessors
nullptr, nullptr, // Int array accessors
nullptr, nullptr, // Float array accessors
NavCustomData::DataCallback_5km, nullptr, // Raw data accessors
nullptr, nullptr); // Refcons not used
XPLMRegisterDataAccessor("extplane/navdata/20km",
xplmType_Data, // The types we support
0, // Writable
nullptr, nullptr, // Integer accessors
nullptr, nullptr, // Float accessors
nullptr, nullptr, // Doubles accessors
nullptr, nullptr, // Int array accessors
nullptr, nullptr, // Float array accessors
NavCustomData::DataCallback_20km, nullptr, // Raw data accessors
nullptr, nullptr); // Refcons not used
XPLMRegisterDataAccessor("extplane/navdata/100km",
xplmType_Data, // The types we support
0, // Writable
nullptr, nullptr, // Integer accessors
nullptr, nullptr, // Float accessors
nullptr, nullptr, // Doubles accessors
nullptr, nullptr, // Int array accessors
nullptr, nullptr, // Float array accessors
NavCustomData::DataCallback_100km, nullptr, // Raw data accessors
nullptr, nullptr); // Refcons not used
XPLMRegisterDataAccessor("extplane/atc/124thatc/latest",
xplmType_Data, // The types we support
0, // Writable
nullptr, nullptr, // Integer accessors
nullptr, nullptr, // Float accessors
nullptr, nullptr, // Doubles accessors
nullptr, nullptr, // Int array accessors
nullptr, nullptr, // Float array accessors
ATCCustomData::DataCallback, nullptr, // Raw data accessors
nullptr, nullptr);
return 1;
}
void XPlanePlugin::pluginDisable() {
XPLMRemoveMenuItem(XPLMFindPluginsMenu(), m_menu_container_idx);
m_menu_container_idx = -1;
XPLMDestroyMenu(m_menu_id);
m_menu_id = nullptr;
if(m_server)
m_server->disconnectClients();
delete m_server;
Q_ASSERT(m_refs.empty());
m_server = nullptr;
}