/
commviewer.cpp
executable file
·554 lines (489 loc) · 17.2 KB
/
commviewer.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// Copyright (c) 2014 Joseph D Poirier
// Distributable under the terms of The New BSD License
// that can be found in the LICENSE file.
#ifdef _WIN32 /* this is set for 64 bit as well */
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
#ifdef _APPLE_
#pragma clang diagnostic ignored "-Wall"
#include <gl.h>
#else
//#include <GL/gl.h>
#endif
#include <string>
#include <atomic>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <fstream>
#include <sstream>
#include "./SDK/CHeaders/XPLM/XPLMPlugin.h"
#include "./SDK/CHeaders/XPLM/XPLMProcessing.h"
#include "./SDK/CHeaders/XPLM/XPLMDataAccess.h"
#include "./SDK/CHeaders/XPLM/XPLMUtilities.h"
#include "./SDK/CHeaders/XPLM/XPLMDisplay.h"
#include "./SDK/CHeaders/XPLM/XPLMGraphics.h"
#include "./include/defs.h"
#include "./include/commviewer.h"
using namespace std;
static int CommandHandler(XPLMCommandRef inCommand, XPLMCommandPhase inPhase,
void* inRefcon);
static void DrawWindowCallback(XPLMWindowID inWindowID, void* inRefcon);
static void HandleKeyCallback(XPLMWindowID inWindowID, char inKey,
XPLMKeyFlags inFlags, char inVirtualKey, void* inRefcon, int losingFocus);
static int HandleMouseCallback(XPLMWindowID inWindowID, int x, int y,
XPLMMouseStatus inMouse, void* inRefcon);
// To define, pass -DVERSION=vX.Y.X when building
#ifndef VERSION
#define VERSION "vUNKNOWN"
#endif
// sigh, two levels of macros are needed to stringify
// the result of expansion of a macro argument
#define STR(v) "CommViewer " #v " " __DATE__ " (jdpoirier@gmail.com)"
#define DESC(v) STR(v)
#define STRING2(x) #x
#define STRING(x) STRING2(x)
#pragma message (DESC(VERSION))
#ifdef TOGGLE_TEST_FEATURE
static XPLMHotKeyID gHotKey = NULL;
static void HotKeyCallback(void* inRefcon);
#endif
static XPLMWindowID gCommWindow = NULL;
static atomic<bool> gPluginEnabled(false);
// static int gPlaneLoaded = 0;
static const float FL_CB_INTERVAL = -1.0;
static atomic<bool> gPTT_On(false);
static atomic<bool> gPilotEdgePlugin(false);
static string gNnumber = "";
static string gAircraftType = "";
#define WINDOW_WIDTH (235)
#define WINDOW_HEIGHT (40)
static int gCommWinPosX;
static int gCommWinPosY;
static int gLastMouseX;
static int gLastMouseY;
// general & misc
enum {
PLUGIN_PLANE_ID = 0
,CMD_CONTACT_ATC
,COMMVIEWER_WINDOW
#ifdef TOGGLE_TEST_FEATURE
#endif
};
// Command Refs
#define sCONTACT_ATC "sim/operation/contact_atc"
#define PILOTEDGE_SIG "com.pilotedge.plugin.xplane"
XPLMDataRef avionics_power_on_dataref;
XPLMDataRef audio_selection_com1_dataref;
XPLMDataRef audio_selection_com2_dataref;
XPLMDataRef pilotedge_rx_status_dataref = NULL;
XPLMDataRef pilotedge_tx_status_dataref = NULL;
XPLMDataRef pilotedge_connected_dataref = NULL;
XPLMDataRef radio_volume_ratio_dataref;
#ifdef TOGGLE_TEST_FEATURE
XPLMDataRef artificial_stability_on_dataref;
XPLMDataRef artificial_stability_pitch_on_dataref;
XPLMDataRef artificial_stability_roll_on_dataref;
#endif
XPLMDataRef panel_visible_win_t_dataref;
/**
*
*/
PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc)
{
LPRINTF("CommViewer Plugin: XPluginStart\n");
strcpy(outName, "CommViewer");
strcpy(outSig , "jdp.comm.viewer");
strcpy(outDesc, DESC(VERSION));
// sim/cockpit/switches/audio_panel_out
// sim/cockpit2/radios/actuators/audio_com_selection
// sim/cockpit2/radios/actuators/audio_selection_com1 int y boolean is com1 selected for listening
// sim/cockpit2/radios/actuators/audio_selection_com2 int y boolean is com2 selected for listening
// sim/cockpit2/radios/actuators/com1_power int y boolean Com radio 1 off or on, 0 or 1.
// sim/cockpit2/radios/actuators/com2_power int y boolean Com radio 2 off or on, 0 or 1.
//avionics_power_on_dataref = XPLMFindDataRef("sim/cockpit2/switches/avionics_power_on");
//LPRINTF("CommViewer Plugin: data/command refs initialized\n");
audio_selection_com1_dataref =
XPLMFindDataRef("sim/cockpit2/radios/actuators/audio_selection_com1");
audio_selection_com2_dataref =
XPLMFindDataRef("sim/cockpit2/radios/actuators/audio_selection_com2");
radio_volume_ratio_dataref =
XPLMFindDataRef("sim/operation/sound/radio_volume_ratio"); // 0.0 - 1.0
#ifdef TOGGLE_TEST_FEATURE
artificial_stability_on_dataref =
XPLMFindDataRef("sim/cockpit2/switches/artificial_stability_on");
artificial_stability_pitch_on_dataref =
XPLMFindDataRef("sim/cockpit2/switches/artificial_stability_pitch_on");
artificial_stability_roll_on_dataref =
XPLMFindDataRef("sim/cockpit2/switches/artificial_stability_roll_on");
XPLMSetDatai(artificial_stability_on_dataref, 1);
XPLMSetDatai(artificial_stability_pitch_on_dataref, 1);
XPLMSetDatai(artificial_stability_roll_on_dataref, 1);
#endif
XPLMCommandRef cmd_ref;
cmd_ref = XPLMCreateCommand(sCONTACT_ATC, "Contact ATC");
XPLMRegisterCommandHandler(cmd_ref,
CommandHandler,
CMD_HNDLR_EPILOG,
(void*)CMD_CONTACT_ATC);
// XPLMRegisterFlightLoopCallback(FlightLoopCallback, FL_CB_INTERVAL, NULL);
panel_visible_win_t_dataref =
XPLMFindDataRef("sim/graphics/view/panel_visible_win_t");
int top = (int)XPLMGetDataf(panel_visible_win_t_dataref);
gCommWinPosX = 0;
gCommWinPosY = top - 200;
gCommWindow = XPLMCreateWindow(gCommWinPosX, // left
gCommWinPosY, // top
gCommWinPosX+WINDOW_WIDTH, // right
gCommWinPosY-WINDOW_HEIGHT, // bottom
true, // is visible
DrawWindowCallback,
HandleKeyCallback,
HandleMouseCallback,
(void*)COMMVIEWER_WINDOW); // Refcon
// printf("CommViewer, left:%d, right:%d, top:%d, bottom:%d\n",
// gCommWinPosX, gCommWinPosX+WINDOW_WIDTH, gCommWinPosY, gCommWinPosY-WINDOW_HEIGHT);
#ifdef TOGGLE_TEST_FEATURE
//gHotKey = XPLMRegisterHotKey(XPLM_VK_F3,
// xplm_DownFlag,
// "Toggle Window Ice",
// HotKeyCallback,
// NULL);
#endif
LPRINTF("CommViewer Plugin: startup completed\n");
return PROCESSED_EVENT;
}
#ifdef TOGGLE_TEST_FEATURE
/**
*
*/
void HotKeyCallback(void* inRefcon)
{
static bool isSaved = false;
if (isSaved)
isSaved = false;
else
isSaved = true;
}
#endif
/**
*
*/
float FlightLoopCallback(float inElapsedSinceLastCall,
float inElapsedTimeSinceLastFlightLoop, int inCounter,
void* inRefcon)
{
if (!gPluginEnabled.load()) {
}
return 1.0;
}
/**
*
*/
int CommandHandler(XPLMCommandRef inCommand, XPLMCommandPhase inPhase,
void* inRefcon)
{
// if (!gPluginEnabled.load()) {
// return IGNORED_EVENT;
// }
switch (reinterpret_cast<size_t>(inRefcon)) {
case CMD_CONTACT_ATC:
switch (inPhase) {
case xplm_CommandBegin:
case xplm_CommandContinue:
gPTT_On.store(true);
break;
case xplm_CommandEnd:
gPTT_On.store(false);
break;
default:
break;
}
break;
default:
break;
}
return IGNORED_EVENT;
}
/**
*
*/
PLUGIN_API void XPluginStop(void)
{
gPluginEnabled.store(false);
//XPLMUnregisterFlightLoopCallback(FlightLoopCallback, NULL);
LPRINTF("CommViewer Plugin: XPluginStop\n");
}
/**
*
*/
PLUGIN_API void XPluginDisable(void)
{
gPluginEnabled.store(false);
LPRINTF("CommViewer Plugin: XPluginDisable\n");
}
/**
*
*/
PLUGIN_API int XPluginEnable(void)
{
gPluginEnabled.store(true);
LPRINTF("CommViewer Plugin: XPluginEnable\n");
return PROCESSED_EVENT;
}
/**
*
*/
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, long inMsg,
void* inParam)
{
if (inFrom == XPLM_PLUGIN_XPLANE) {
// size_t inparam = reinterpret_cast<size_t>(inParam);
switch (inMsg) {
case XPLM_MSG_PLANE_LOADED:
// gPlaneLoaded = true;
// LPRINTF("CommViewer Plugin: XPluginReceiveMessage XPLM_MSG_PLANE_LOADED\n");
break;
case XPLM_MSG_AIRPORT_LOADED:
// LPRINTF("CommViewer Plugin: XPluginReceiveMessage XPLM_MSG_AIRPORT_LOADED\n");
break;
case XPLM_MSG_SCENERY_LOADED:
// LPRINTF("CommViewer Plugin: XPluginReceiveMessage XPLM_MSG_SCENERY_LOADED\n");
break;
case XPLM_MSG_AIRPLANE_COUNT_CHANGED:
// LPRINTF("CommViewer Plugin: XPluginReceiveMessage XPLM_MSG_AIRPLANE_COUNT_CHANGED\n");
break;
case XPLM_MSG_PLANE_CRASHED:
// XXX: system state and procedure, what's difference between
// an unloaded and crashed plane?
// LPRINTF("CommViewer Plugin: XPluginReceiveMessage XPLM_MSG_PLANE_CRASHED\n");
break;
case XPLM_MSG_PLANE_UNLOADED:
// gPlaneLoaded = false;
// LPRINTF("CommViewer Plugin: XPluginReceiveMessage XPLM_MSG_PLANE_UNLOADED\n");
break;
default:
// unknown, anything to do?
break;
} // switch (inMsg)
} // if (inFrom == XPLM_PLUGIN_XPLANE)
}
/**
*
*/
#define CONN_YES "YES"
#define CONN_NO "NO "
void DrawWindowCallback(XPLMWindowID inWindowID, void* inRefcon)
{
// RGB White 1.0, 1.0, 1.0, Green
static float commviewer_color[] = {0.0, 1.0, 0.0};
static bool is_connected = false;
if (inWindowID != gCommWindow)
return;
int left;
int top;
int right;
int bottom;
int rx_status;
int tx_status;
string conn_msg = "";
// XXX: are inWindowIDs our XPLMCreateWindow return pointers
XPLMGetWindowGeometry(inWindowID, &left, &top, &right, &bottom);
// printf("CommViewer, gCommWindow: %p, inWindowID: %p, left:%d, right:%d, top:%d, bottom:%d\n",
// gCommWindow, inWindowID, left, right, top, bottom);
XPLMDrawTranslucentDarkBox(left, top, right, bottom);
if (!gPilotEdgePlugin.load()) {
if ((XPLMFindPluginBySignature(PILOTEDGE_SIG)) != XPLM_NO_PLUGIN_ID) {
gPilotEdgePlugin.store(true);
pilotedge_rx_status_dataref = XPLMFindDataRef("pilotedge/radio/rx_status");
pilotedge_tx_status_dataref = XPLMFindDataRef("pilotedge/radio/tx_status");
pilotedge_connected_dataref = XPLMFindDataRef("pilotedge/status/connected");
}
}
stringstream str1;
stringstream str2;
stringstream str3;
switch (reinterpret_cast<size_t>(inRefcon)) {
case COMMVIEWER_WINDOW:
#if 0
sprintf(str1,"%s\t\t\tCOM1: %d\t\t\tCOM2: %d",
(char*)(gPTT_On.load() ? "PTT: ON" : "PTT: OFF"),
XPLMGetDatai(audio_selection_com1_dataref),
XPLMGetDatai(audio_selection_com2_dataref));
// text to window, NULL indicates no word wrap
XPLMDrawString(commviewer_color,
left+5,
top-20,
str1,
NULL,
xplmFont_Basic);
#else
rx_status = (pilotedge_rx_status_dataref ?
XPLMGetDatai(pilotedge_rx_status_dataref) : false) ? 1 : 0;
tx_status = (pilotedge_tx_status_dataref ?
XPLMGetDatai(pilotedge_tx_status_dataref) : false) ? 1 : 0;
conn_msg = (pilotedge_connected_dataref ?
XPLMGetDatai(pilotedge_connected_dataref) : false) ? CONN_YES : CONN_NO;
if (!is_connected && conn_msg == CONN_YES) {
is_connected = true;
ifstream infile;
infile.open("./Resources/plugins/PilotEdge/VSPro Resources/VSProConnect.ini");
if (infile.is_open()) {
int lines_read = 0;
string sLine = "";
while (infile) {
getline(infile, sLine);
lines_read++;
if (lines_read == 3) {
if (sLine != "none")
gNnumber = " " + sLine;
} else if (lines_read == 4) {
if (sLine != "none")
gAircraftType = " " + sLine;
break;
}
} // while
infile.close();
}
} else if (is_connected && conn_msg == CONN_NO) {
is_connected = false;
gNnumber = "";
gAircraftType = "";
}
str1 << "PilotEdge Connected" << gAircraftType << gNnumber
<< ": " << conn_msg << '\n';
str2 << string(gPTT_On.load() ? "PTT : ON " : "PTT : OFF")
<< "\t\tTX: " << tx_status << "\t\t\tRX: " << rx_status << '\n';
str3 << "COM1: " << XPLMGetDatai(audio_selection_com1_dataref)
<< "\t\tCOM2: " << XPLMGetDatai(audio_selection_com2_dataref);
// text to window, NULL indicates no word wrap
XPLMDrawString(commviewer_color,
left+4,
top-10,
(char*)str1.str().c_str(),
NULL,
xplmFont_Basic);
XPLMDrawString(commviewer_color,
left+4,
top-23,
(char*)str2.str().c_str(),
NULL,
xplmFont_Basic);
XPLMDrawString(commviewer_color,
left+4,
top-35,
(char*)str3.str().c_str(),
NULL,
xplmFont_Basic);
#endif
break;
default:
break;
}
#ifdef TOGGLE_TEST_FEATURE
GLfloat x1 = (GLfloat)(right + 5);
GLfloat y1 = (GLfloat)(top - 5);
GLfloat x2;
GLfloat y2;
GLfloat radius = 0.1f;
glDisable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 0.6f);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x1, y1); // x & y
for (GLfloat angle = 1.0f; angle < 361.0f; angle += 0.2f) {
x2 = x1 + (GLfloat)sin(angle) * radius;
y2 = y1 + (GLfloat)cos(angle) * radius;
glVertex2f(x2, y2);
}
glEnd();
glEnable(GL_TEXTURE_2D);
//glDisable(GL_TEXTURE_2D);
//glColor3f(0.7, 0.7, 0.7);
//glBegin(GL_LINES);
// glVertex2i(right-1, top-1);
// glVertex2i(right-7, top-7);
// glVertex2i(right-7, top-1);
// glVertex2i(right-1, top-7);
//glEnd();
//glEnable(GL_TEXTURE_2D);
#endif
}
/**
*
*/
void HandleKeyCallback(XPLMWindowID inWindowID, char inKey, XPLMKeyFlags inFlags,
char inVirtualKey, void* inRefcon, int losingFocus)
{
if (inWindowID != gCommWindow)
return;
}
/**
*
*
*/
#define COMMS_UNCHANGED (0)
#define COM1_CHANGED (1)
#define COM2_CHANGED (2)
#define COMM_UNSELECTED (0)
#define COMM_SELECTED (1)
int HandleMouseCallback(XPLMWindowID inWindowID, int x, int y,
XPLMMouseStatus inMouse, void* inRefcon)
{
static int com_changed = COMMS_UNCHANGED;
static int MouseDownX;
static int MouseDownY;
if (inWindowID != gCommWindow)
return IGNORED_EVENT;
switch (inMouse) {
case xplm_MouseDown:
// if ((x >= gCommWinPosX+WINDOW_WIDTH-8) &&
// (x <= gCommWinPosX+WINDOW_WIDTH) &&
// (y <= gCommWinPosY) && (y >= gCommWinPosY-8)) {
// windowCloseRequest = 1;
// } else {
MouseDownX = gLastMouseX = x;
MouseDownY = gLastMouseY = y;
// }
break;
case xplm_MouseDrag:
// this event fires while xplm_MouseDown
// and whether the window is being dragged or not
gCommWinPosX += (x - gLastMouseX);
gCommWinPosY += (y - gLastMouseY);
XPLMSetWindowGeometry(gCommWindow,
gCommWinPosX,
gCommWinPosY,
gCommWinPosX+WINDOW_WIDTH,
gCommWinPosY-WINDOW_HEIGHT);
gLastMouseX = x;
gLastMouseY = y;
break;
case xplm_MouseUp:
if (MouseDownX == x || MouseDownY == y) {
int com1 = XPLMGetDatai(audio_selection_com1_dataref);
int com2 = XPLMGetDatai(audio_selection_com2_dataref);
if (com1 && com2 && com_changed) {
switch (com_changed) {
case COM1_CHANGED:
XPLMSetDatai(audio_selection_com1_dataref, COMM_UNSELECTED);
break;
case COM2_CHANGED:
XPLMSetDatai(audio_selection_com2_dataref, COMM_UNSELECTED);
break;
default:
break;
}
com_changed = COMMS_UNCHANGED;
} else if (!com1 && com2) {
com_changed = COM1_CHANGED;
XPLMSetDatai(audio_selection_com1_dataref, COMM_SELECTED);
} else if (com1 && !com2) {
com_changed = COM2_CHANGED;
XPLMSetDatai(audio_selection_com2_dataref, COMM_SELECTED);
}
}
break;
} // switch (inMouse)
return PROCESSED_EVENT;
}