forked from vranki/ExtPlane
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxplaneplugin.cpp
More file actions
131 lines (118 loc) · 4.04 KB
/
xplaneplugin.cpp
File metadata and controls
131 lines (118 loc) · 4.04 KB
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
#include "xplaneplugin.h"
#include "datarefs/dataref.h"
#include "datarefs/floatdataref.h"
#include "datarefs/floatarraydataref.h"
#include "datarefs/intdataref.h"
#include "datarefs/intarraydataref.h"
#include "datarefs/doubledataref.h"
#include "datarefs/datadataref.h"
#include "util/console.h"
XPlanePlugin::XPlanePlugin(QObject *parent) :
QObject(parent), argc(0), argv(0), app(0), server(0), flightLoopInterval(0.31f) { // Default to 30hz
}
XPlanePlugin::~XPlanePlugin() {
DEBUG << Q_FUNC_INFO;
}
float XPlanePlugin::flightLoop(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop,
int inCounter, void *inRefcon) {
foreach(DataRef *ref, refs)
ref->updateValue();
app->processEvents();
return flightLoopInterval;
}
int XPlanePlugin::pluginStart(char * outName, char * outSig, char *outDesc) {
INFO << "Plugin started";
app = new QCoreApplication(argc, &argv);
server = new TcpServer(this, this);
connect(server, SIGNAL(setFlightLoopInterval(float)), this, SLOT(setFlightLoopInterval(float)));
strcpy(outName, "ExtPlane");
strcpy(outSig, "org.vranki.extplaneplugin");
strcpy(outDesc, "Read and write X-Plane datarefs from external programs using TCP socket.");
app->processEvents();
return 1;
}
DataRef* XPlanePlugin::subscribeRef(QString name) {
DEBUG << name;
foreach(DataRef *ref, refs) {
if(ref->name()==name) {
DEBUG << "Already subscribed to " << name;
ref->setSubscribers(ref->subscribers() + 1);
return ref;
}
}
XPLMDataRef ref = XPLMFindDataRef(name.toLatin1());
if(ref) {
XPLMDataTypeID refType = XPLMGetDataRefTypes(ref);
DataRef *dr = 0;
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->setSubscribers(1);
dr->setWritable(XPLMCanWriteDataRef(ref) != 0);
DEBUG << "Subscribed to ref " << dr->name() << ", type: " << dr->typeString() << ", writable:" << dr->isWritable();
refs.append(dr);
return dr;
} else {
INFO << "Dataref type " << refType << "not supported";
}
} else {
INFO << "Can't find dataref " << name;
}
return 0;
}
void XPlanePlugin::unsubscribeRef(DataRef *ref) {
Q_ASSERT(refs.contains(ref));
DEBUG << ref->name() << ref->subscribers();
ref->setSubscribers(ref->subscribers() - 1);
if(ref->subscribers() == 0) {
refs.removeOne(ref);
DEBUG << "Ref " << ref->name() << " not subscribed by anyone - removing.";
ref->deleteLater();
}
}
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::setFlightLoopInterval(float newInterval) {
if(newInterval > 0) {
flightLoopInterval = newInterval;
DEBUG << "New interval" << flightLoopInterval;
} else {
DEBUG << "Invalid interval " << newInterval;
}
}
void XPlanePlugin::pluginStop() {
DEBUG;
app->processEvents();
delete server;
server = 0;
app->quit();
app->processEvents();
delete app;
app = 0;
qDeleteAll(refs);
refs.clear();
}
void XPlanePlugin::receiveMessage(XPLMPluginID inFromWho, long inMessage, void *inParam) {
DEBUG << inFromWho << inMessage;
}