-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXAlert.cpp
1109 lines (905 loc) · 30.3 KB
/
XAlert.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* XAlert.cpp
*
* By Jiaqi Liu
* E-mail: [email protected]
*
*/
#include <stdio.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <cmath>
#include <sstream>
#include "XPLMDisplay.h"
#include "XPLMGraphics.h"
#include "XPLMProcessing.h"
#include "XPLMDataAccess.h"
#include "XPLMMenus.h"
#include "XPLMUtilities.h"
#include "XPWidgets.h"
#include "XPStandardWidgets.h"
#include "XPLMPlugin.h"
#if APL
#include <Carbon/Carbon.h>
#endif
#ifdef _WIN32
#include <al.h>
#include <alc.h>
#else
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#endif
/**************************************************************************************************************
* WAVE FILE LOADING
**************************************************************************************************************/
// You can just use alutCreateBufferFromFile to load a wave file, but there seems to be a lot of problems with
// alut not beign available, being deprecated, etc. So...here's a stupid routine to load a wave file. I have
// tested this only on x86 machines, so if you find a bug on PPC please let me know.
// Macros to swap endian-values.
#define SWAP_32(value) \
(((((unsigned short)value)<<8) & 0xFF00) | \
((((unsigned short)value)>>8) & 0x00FF))
#define SWAP_16(value) \
(((((unsigned int)value)<<24) & 0xFF000000) | \
((((unsigned int)value)<< 8) & 0x00FF0000) | \
((((unsigned int)value)>> 8) & 0x0000FF00) | \
((((unsigned int)value)>>24) & 0x000000FF))
// Wave files are RIFF files, which are "chunky" - each section has an ID and a length. This lets us skip
// things we can't understand to find the parts we want. This header is common to all RIFF chunks.
struct chunk_header {
int id;
int size;
};
// WAVE file format info. We pass this through to OpenAL so we can support mono/stereo, 8/16/bit, etc.
struct format_info {
short format; // PCM = 1, not sure what other values are legal.
short num_channels;
int sample_rate;
int byte_rate;
short block_align;
short bits_per_sample;
};
// Airports meta data
struct airport_metadata
{
int airportId;
char name[256];
char city[256];
char country[256];
char IATA[32];
char ICAO[32];
double latitude;
double longitude;
double altitude;
double timezone;
char DST[16];
char tzDatabaseTimezone[256];
char type[256];
char source[256];
};
static airport_metadata airports[10000];
static int airports_cout;
#define FAIL(X) { XPLMDebugString(X); free(mem); return 0; }
#define RIFF_ID 0x46464952 // 'RIFF'
#define FMT_ID 0x20746D66 // 'FMT '
#define DATA_ID 0x61746164 // 'DATA'
#define earthRadiusKm 6371.0
#define KilometersToNauticalMilesRatio 0.539956804 // 1 KM = 0.539956804 NM
#define MAX_ITEMS 11
static XPLMDataRef gPositionDataRef[MAX_ITEMS];
static char DataRefString[MAX_ITEMS][255] = { "sim/flightmodel/position/local_x", "sim/flightmodel/position/local_y", "sim/flightmodel/position/local_z",
"sim/flightmodel/position/lat_ref", "sim/flightmodel/position/lon_ref", "sim/flightmodel/position/theta",
"sim/flightmodel/position/phi", "sim/flightmodel/position/psi",
"sim/flightmodel/position/latitude", "sim/flightmodel/position/longitude", "sim/flightmodel/position/elevation" };
static char DataRefDesc[MAX_ITEMS][255] = { "Local x", "Local y", "Local z", "Lat Ref", "Lon Ref", "Theta", "Phi", "Psi" };
static char Description[3][255] = { "Latitude", "Longitude", "Elevation" };
static int Element = 0, IntVals[128];
static float FloatVals[128];
static int ByteVals[128];
static int MenuItem1;
static long AlertDistance;
static double DestinationAirpotLat, DestinationAirpotlon;
static bool alertSet;
static XPWidgetID ConfigWidget = NULL, ConfigWindow = NULL;
static XPWidgetID CreateAlertButton = NULL;
static XPWidgetID DistanceText = NULL;
static XPWidgetID DistanceEdit = NULL;
static XPWidgetID DistanceUpArrow = NULL;
static XPWidgetID DistanceDownArrow = NULL;
static XPWidgetID DestinationText = NULL;
static XPWidgetID DestinationEdit = NULL;
static XPWidgetID WarningText = NULL;
static XPWidgetID ConfigApplyButton = NULL, LatLonRefApplyButton = NULL, ReloadSceneryButton = NULL;
static XPWidgetID Position2Text[3] = { NULL };
static XPWidgetID Position2Edit[3] = { NULL };
static void ConfigMenuHandler(void *, void *);
static void CreateConfigMenu(int x1, int y1, int w, int h);
static int ConfigHandler(
XPWidgetMessage inMessage,
XPWidgetID inWidget,
intptr_t inParam1,
intptr_t inParam2);
static bool setAlert(char *err);
static double distanceEarth(double lat1d, double lon1d, double lat2d, double lon2d);
static void buildFilePath(char *fileName, char *buf);
static bool launchVisualStudioDebugger();
static int getAirportIndexByICAO(char *ICAO);
static void cancelAlert();
static ALuint snd_src = 0; // Sample source and buffer - this is one "sound" we play.
static ALuint snd_buffer = 0;
static float pitch = 1.0f; // Start with 1.0 pitch - no pitch shift.
static ALCdevice * my_dev = NULL; // We make our own device and context to play sound through.
static ALCcontext * my_ctx = NULL;
// This is a stupid logging error function...useful for debugging, but not good error checking.
static void CHECK_ERR(void)
{
ALuint e = alGetError();
if (e != AL_NO_ERROR)
printf("ERROR: %d\n", e);
}
// Mac specific: this converts file paths from HFS (which we get from the SDK) to Unix (which the OS wants).
// See this for more info:
//
// http://www.xsquawkbox.net/xpsdk/mediawiki/FilePathsAndMacho
#if APL
int ConvertPath(const char * inPath, char * outPath, int outPathMaxLen) {
CFStringRef inStr = CFStringCreateWithCString(kCFAllocatorDefault, inPath, kCFStringEncodingMacRoman);
if (inStr == NULL)
return -1;
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, inStr, kCFURLHFSPathStyle, 0);
CFStringRef outStr = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
if (!CFStringGetCString(outStr, outPath, outPathMaxLen, kCFURLPOSIXPathStyle))
return -1;
CFRelease(outStr);
CFRelease(url);
CFRelease(inStr);
return 0;
}
#endif
// This utility returns the start of data for a chunk given a range of bytes it might be within. Pass 1 for
// swapped if the machine is not the same endian as the file.
static char * find_chunk(char * file_begin, char * file_end, int desired_id, int swapped)
{
while (file_begin < file_end)
{
chunk_header * h = (chunk_header *)file_begin;
if (h->id == desired_id && !swapped)
return file_begin + sizeof(chunk_header);
if (h->id == SWAP_32(desired_id) && swapped)
return file_begin + sizeof(chunk_header);
int chunk_size = swapped ? SWAP_32(h->size) : h->size;
char * next = file_begin + chunk_size + sizeof(chunk_header);
if (next > file_end || next <= file_begin)
return NULL;
file_begin = next;
}
return NULL;
}
// Given a chunk, find its end by going back to the header.
static char * chunk_end(char * chunk_start, int swapped)
{
chunk_header * h = (chunk_header *)(chunk_start - sizeof(chunk_header));
return chunk_start + (swapped ? SWAP_32(h->size) : h->size);
}
inline float HACKFLOAT(float val)
{
return val;
}
/*
#if IBM
inline float HACKFLOAT(float val)
{
return val;
}
#else
inline long long HACKFLOAT(float val)
{
double d = val;
long long temp;
temp = *((long long *) &d);
return temp;
}
#endif
*/
static XPLMWindowID gWindow = NULL;
static int gClicked = 0;
static void MyDrawWindowCallback(
XPLMWindowID inWindowID,
void * inRefcon);
static void MyHandleKeyCallback(
XPLMWindowID inWindowID,
char inKey,
XPLMKeyFlags inFlags,
char inVirtualKey,
void * inRefcon,
int losingFocus);
static int MyHandleMouseClickCallback(
XPLMWindowID inWindowID,
int x,
int y,
XPLMMouseStatus inMouse,
void * inRefcon);
static ALuint load_wave(const char * file_name);
static void load_airports(const char * file_name);
static float init_sound(float elapsed, float elapsed_sim, int counter, void * ref);
/*
* XPluginStart
*
* Our start routine registers our window and does any other initialization we
* must do.
*
*/
PLUGIN_API int XPluginStart(
char * outName,
char * outSig,
char * outDesc)
{
/* First we must fill in the passed in buffers to describe our
* plugin to the plugin-system. */
//launchVisualStudioDebugger();
strcpy(outName, "XAlert");
strcpy(outSig, "jackil.xplugin.XAlert");
strcpy(outDesc, "A plug-in that alert user on demand based on distance from current location to destination airport.");
// init sound
XPLMRegisterFlightLoopCallback(init_sound, -1.0, NULL);
// init airports data
char buf[2048];
buildFilePath("airports.dat", buf);
load_airports(buf);
/* Now we create a window. We pass in a rectangle in left, top,
* right, bottom screen coordinates. We pass in three callbacks. */
gWindow = XPLMCreateWindow(
50, 90, 325, 50, /* Area of the window. */
0, /* Start visible. */
MyDrawWindowCallback, /* Callbacks */
MyHandleKeyCallback,
MyHandleMouseClickCallback,
NULL); /* Refcon - not used. */
/* We must return 1 to indicate successful initialization, otherwise we
* will not be called back again. */
XPLMMenuID id;
int item;
item = XPLMAppendMenuItem(XPLMFindPluginsMenu(), "XAlert", NULL, 1);
id = XPLMCreateMenu("Config", XPLMFindPluginsMenu(), item, ConfigMenuHandler, NULL);
XPLMAppendMenuItem(id, "Config", (void *)"Config", 1);
MenuItem1 = 0;
for (int Item = 0; Item<MAX_ITEMS; Item++)
gPositionDataRef[Item] = XPLMFindDataRef(DataRefString[Item]);
return 1;
}
/*
* XPluginStop
*
* Our cleanup routine deallocates our window.
*
*/
PLUGIN_API void XPluginStop(void)
{
XPLMDestroyWindow(gWindow);
if (MenuItem1 == 1)
{
XPDestroyWidget(ConfigWidget, 1);
MenuItem1 = 0;
}
// Cleanup: nuke our context if we have it. This is hacky and bad - we should really destroy
// our buffers and sources. I have _no_ idea if OpenAL will leak memory.
if (my_ctx) alcDestroyContext(my_ctx);
if (my_dev) alcCloseDevice(my_dev);
my_ctx = NULL;
my_dev = NULL;
}
/*
* XPluginDisable
*
* We do not need to do anything when we are disabled, but we must provide the handler.
*
*/
PLUGIN_API void XPluginDisable(void)
{
}
/*
* XPluginEnable.
*
* We don't do any enable-specific initialization, but we must return 1 to indicate
* that we may be enabled at this time.
*
*/
PLUGIN_API int XPluginEnable(void)
{
return 1;
}
/*
* XPluginReceiveMessage
*
* We don't have to do anything in our receive message handler, but we must provide one.
*
*/
PLUGIN_API void XPluginReceiveMessage(
XPLMPluginID inFromWho,
int inMessage,
void * inParam)
{
}
/*
* MyDrawingWindowCallback
*
* This callback does the work of drawing our window once per sim cycle each time
* it is needed. It dynamically changes the text depending on the saved mouse
* status. Note that we don't have to tell X-Plane to redraw us when our text
* changes; we are redrawn by the sim continuously.
*
*/
void MyDrawWindowCallback(
XPLMWindowID inWindowID,
void * inRefcon)
{
int left, top, right, bottom;
float color[] = { 1.0, 1.0, 1.0 }; /* RGB White */
/* First we get the location of the window passed in to us. */
XPLMGetWindowGeometry(inWindowID, &left, &top, &right, &bottom);
/* We now use an XPLMGraphics routine to draw a translucent dark
* rectangle that is our window's shape. */
XPLMDrawTranslucentDarkBox(left, top, right, bottom);
float FloatValue[MAX_ITEMS];
double DoubleValue[3];
char buffer[512];
int Item;
for (Item = 0; Item<MAX_ITEMS; Item++)
FloatValue[Item] = XPLMGetDataf(gPositionDataRef[Item]);
/// X, Y, Z, Lat, Lon, Alt
XPLMLocalToWorld(FloatValue[0], FloatValue[1], FloatValue[2], &DoubleValue[0], &DoubleValue[1], &DoubleValue[2]);
DoubleValue[2] *= 3.28;
char lat[128], lon[128], elevation[128];
snprintf(lat, sizeof(lat), "%lf", HACKFLOAT(DoubleValue[0]));
snprintf(lon, sizeof(lon), "%lf", HACKFLOAT(DoubleValue[1]));
snprintf(elevation, sizeof(elevation), "%lf", HACKFLOAT(DoubleValue[2]));
char out1[200];
if (alertSet)
{
double distanceToDestination = distanceEarth(atof(lat), atof(lon), DestinationAirpotLat, DestinationAirpotlon) * KilometersToNauticalMilesRatio;
snprintf(out1, sizeof(out1), "Distance to destination airport:%.2lf nm", distanceToDestination);
if (distanceToDestination < AlertDistance)
{
if (my_ctx)
{
ALCcontext * old_ctx = alcGetCurrentContext();
alcMakeContextCurrent(my_ctx);
alSourcef(snd_src, AL_PITCH, pitch);
alSourcePlay(snd_src);
CHECK_ERR();
if (old_ctx)
alcMakeContextCurrent(old_ctx);
}
cancelAlert();
}
}
char out2[200];
snprintf(out2, sizeof(out2), "Alert distance:%ld nm", AlertDistance);
/* Finally we draw the text into the window, also using XPLMGraphics
* routines. The NULL indicates no word wrapping. */
XPLMDrawString(color, left + 10, top - 23,
(char*)(gClicked ? out2 : out1), NULL, xplmFont_Basic);
}
/*
* MyHandleKeyCallback
*
* Our key handling callback does nothing in this plugin. This is ok;
* we simply don't use keyboard input.
*
*/
void MyHandleKeyCallback(
XPLMWindowID inWindowID,
char inKey,
XPLMKeyFlags inFlags,
char inVirtualKey,
void * inRefcon,
int losingFocus)
{
}
/*
* MyHandleMouseClickCallback
*
* Our mouse click callback toggles the status of our mouse variable
* as the mouse is clicked. We then update our text on the next sim
* cycle.
*
*/
int MyHandleMouseClickCallback(
XPLMWindowID inWindowID,
int x,
int y,
XPLMMouseStatus inMouse,
void * inRefcon)
{
/* If we get a down or up, toggle our status click. We will
* never get a down without an up if we accept the down. */
if ((inMouse == xplm_MouseDown) || (inMouse == xplm_MouseUp))
gClicked = 1 - gClicked;
/* Returning 1 tells X-Plane that we 'accepted' the click; otherwise
* it would be passed to the next window behind us. If we accept
* the click we get mouse moved and mouse up callbacks, if we don't
* we do not get any more callbacks. It is worth noting that we
* will receive mouse moved and mouse up even if the mouse is dragged
* out of our window's box as long as the click started in our window's
* box. */
return 1;
}
void ConfigMenuHandler(void * mRef, void * iRef)
{
if (!strcmp((char *)iRef, "Config"))
{if (MenuItem1 == 0)
{
CreateConfigMenu(300, 600, 300, 280);
MenuItem1 = 1;
}
else
if (!XPIsWidgetVisible(ConfigWidget))
XPShowWidget(ConfigWidget);
}
}
void CreateConfigMenu(int x, int y, int w, int h)
{
int x2 = x + w;
int y2 = y - h;
float FloatValue[MAX_ITEMS];
double DoubleValue[3];
char buffer[512];
for (int i = 0; i<MAX_ITEMS; i++)
FloatValue[i] = XPLMGetDataf(gPositionDataRef[i]);
/// X, Y, Z, Lat, Lon, Alt
XPLMLocalToWorld(FloatValue[0], FloatValue[1], FloatValue[2], &DoubleValue[0], &DoubleValue[1], &DoubleValue[2]);
DoubleValue[2] *= 3.28;
ConfigWidget = XPCreateWidget(x, y, x2, y2,
1, // Visible
"XAlert Config", // desc
1, // root
NULL, // no container
xpWidgetClass_MainWindow);
XPSetWidgetProperty(ConfigWidget, xpProperty_MainWindowHasCloseBoxes, 1);
ConfigWindow = XPCreateWidget(x + 20, y - 35, x2 - 20, y2 + 20,
1, // Visible
"", // desc
0, // root
ConfigWidget,
xpWidgetClass_SubWindow);
XPSetWidgetProperty(ConfigWindow, xpProperty_SubWindowType, xpSubWindowStyle_SubWindow);
int Item = 0;
// Distance config item
{
DistanceText = XPCreateWidget(x + 30, y - (70 + (Item * 30)), x + 130, y - (92 + (Item * 30)),
1, // Visible
"Alert Distance (nm)",// desc
0, // root
ConfigWidget,
xpWidgetClass_Caption);
sprintf(buffer, "100");
DistanceEdit = XPCreateWidget(x + 150, y - (70 + (Item * 30)), x + 240, y - (92 + (Item * 30)),
1, buffer, 0, ConfigWidget,
xpWidgetClass_TextField);
XPSetWidgetProperty(DistanceEdit, xpProperty_TextFieldType, xpTextEntryField);
DistanceUpArrow = XPCreateWidget(x + 250, y - (66 + (Item * 30)), x + 262, y - (81 + (Item * 30)),
1, "", 0, ConfigWidget,
xpWidgetClass_Button);
XPSetWidgetProperty(DistanceUpArrow, xpProperty_ButtonType, xpLittleUpArrow);
DistanceDownArrow = XPCreateWidget(x + 250, y - (81 + (Item * 30)), x + 262, y - (96 + (Item * 30)),
1, "", 0, ConfigWidget,
xpWidgetClass_Button);
XPSetWidgetProperty(DistanceDownArrow, xpProperty_ButtonType, xpLittleDownArrow);
}
Item++;
// Destination config item
{
DestinationText = XPCreateWidget(x + 30, y - (70 + (Item * 30)), x + 130, y - (92 + (Item * 30)),
1, // Visible
"Destination ICAO",// desc
0, // root
ConfigWidget,
xpWidgetClass_Caption);
sprintf(buffer, "KSEA");
DestinationEdit = XPCreateWidget(x + 150, y - (70 + (Item * 30)), x + 240, y - (92 + (Item * 30)),
1, buffer, 0, ConfigWidget,
xpWidgetClass_TextField);
XPSetWidgetProperty(DestinationEdit, xpProperty_TextFieldType, xpTextEntryField);
}
Item++;
// Warning text
{
WarningText = XPCreateWidget(x + 50, y - (90 + (Item * 30)), x + 240, y - (112 + (Item * 30)),
1, // Visible
"",// desc
0, // root
ConfigWidget,
xpWidgetClass_Caption);
}
ConfigApplyButton = XPCreateWidget(x + 70, y - 180, x + 220, y - 202,
1, "Set alert", 0, ConfigWidget,
xpWidgetClass_Button);
// Version & info
{
XPCreateWidget(x + 50, y - 205, x + 240, y - 225,
1, // Visible
" XAlert alpha by Jiaqi Liu",// desc
0, // root
ConfigWidget,
xpWidgetClass_Caption);
XPCreateWidget(x + 50, y - 225, x + 240, y - 250,
1, // Visible
" E-mail: [email protected]",// desc
0, // root
ConfigWidget,
xpWidgetClass_Caption);
}
XPSetWidgetProperty(ConfigApplyButton, xpProperty_ButtonType, xpPushButton);
XPAddWidgetCallback(ConfigWidget, ConfigHandler);
}
void cancelAlert()
{
alertSet = false;
XPLMSetWindowIsVisible(gWindow, 0);
XPSetWidgetDescriptor(WarningText, NULL);
XPSetWidgetDescriptor(ConfigApplyButton, "Set alert");
}
int ConfigHandler(
XPWidgetMessage inMessage,
XPWidgetID inWidget,
intptr_t inParam1,
intptr_t inParam2)
{
char buffer[512];
int Item;
if (inMessage == xpMessage_CloseButtonPushed)
{
if (MenuItem1 == 1)
{
XPHideWidget(ConfigWidget);
}
return 1;
}
if (inMessage == xpMsg_PushButtonPressed)
{
if (inParam1 == (intptr_t)ConfigApplyButton)
{
if (alertSet)
{
cancelAlert();
return 1;
}
char err[100];
if (!setAlert(err))
{
XPSetWidgetDescriptor(WarningText, err);
} else
{
XPLMSetWindowIsVisible(gWindow, 1);
XPSetWidgetDescriptor(WarningText, " Alert Set!");
XPSetWidgetDescriptor(ConfigApplyButton, "Cancel alert");
}
return 1;
}
if (inParam1 == (intptr_t)DistanceUpArrow)
{
char buffer[128];
XPGetWidgetDescriptor(DistanceEdit, buffer, 128);
int distance = atoi(buffer) + 1;
sprintf(buffer, "%d", distance);
XPSetWidgetDescriptor(DistanceEdit, buffer);
return 1;
}
if (inParam1 == (intptr_t)DistanceDownArrow)
{
char buffer[128];
XPGetWidgetDescriptor(DistanceEdit, buffer, 128);
int distance = atoi(buffer) - 1;
sprintf(buffer, "%d", distance);
XPSetWidgetDescriptor(DistanceEdit, buffer);
return 1;
}
}
return 0;
}
inline bool isInteger(const std::string & s)
{
if (s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false;
char * p;
strtol(s.c_str(), &p, 10);
return (*p == 0);
}
bool setAlert(char *err)
{
char buffer[128];
XPGetWidgetDescriptor(DistanceEdit, buffer, 128);
if (!isInteger(buffer))
{
strcpy(err, " Distance must be an integer");
return false;
}
AlertDistance = atoi(buffer);
if (AlertDistance <= 0)
{
strcpy(err, " Distance should be positive");
return false;
}
char ICAO[128];
XPGetWidgetDescriptor(DestinationEdit, ICAO, 128);
int index = getAirportIndexByICAO(ICAO);
if (index == -1)
{
strcpy(err, " Airport not found in database");
return false;
}
DestinationAirpotLat = airports[index].latitude;
DestinationAirpotlon = airports[index].longitude;
alertSet = true;
return true;
}
// This function converts decimal degrees to radians
double deg2rad(double deg) {
return (deg * M_PI / 180);
}
// This function converts radians to decimal degrees
double rad2deg(double rad) {
return (rad * 180 / M_PI);
}
/**
* Returns the distance between two points on the Earth.
* Direct translation from http://en.wikipedia.org/wiki/Haversine_formula
* @param lat1d Latitude of the first point in degrees
* @param lon1d Longitude of the first point in degrees
* @param lat2d Latitude of the second point in degrees
* @param lon2d Longitude of the second point in degrees
* @return The distance between the two points in kilometers
*/
double distanceEarth(double lat1d, double lon1d, double lat2d, double lon2d) {
double lat1r, lon1r, lat2r, lon2r, u, v;
lat1r = deg2rad(lat1d);
lon1r = deg2rad(lon1d);
lat2r = deg2rad(lat2d);
lon2r = deg2rad(lon2d);
u = sin((lat2r - lat1r) / 2);
v = sin((lon2r - lon1r) / 2);
return 2.0 * earthRadiusKm * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
}
void buildFilePath(char *fileName, char *buf)
{
char dirchar = *XPLMGetDirectorySeparator();
XPLMGetPluginInfo(XPLMGetMyID(), NULL, buf, NULL, NULL);
char * p = buf;
char * slash = p;
while (*p)
{
if (*p == dirchar) slash = p;
++p;
}
++slash;
*slash = 0;
strcat(buf, fileName);
#if APL
ConvertPath(buf, buf, sizeof(buf));
#endif
}
char* replace_char(char* str, char find, char replace) {
char *current_pos = strchr(str, find);
while (current_pos) {
*current_pos = replace;
current_pos = strchr(current_pos, find);
}
return str;
}
// I put '$' symbol in the file to replace empty space so that I can easily parse it
// So I need to replace it back to fix it :)
void hacky_empty_fix(char *str)
{
replace_char(str, '$', ' ');
}
void load_airports(const char * file_name)
{
//open and get the file handle
FILE* fp;
fopen_s(&fp, file_name, "rb");
//check if file exists
if (fp == NULL) {
printf("file does not exists %s", file_name);
return;
}
int line_size = 2048;
char line[2048];
int index = 0;
while (fgets(line, line_size, fp) != NULL) {
sscanf(line, "%d%s%s%s%s%s%lf%lf%lf%lf%s%s%s%s",
&airports[index].airportId, airports[index].name, airports[index].city, airports[index].country,
airports[index].IATA, airports[index].ICAO, &airports[index].latitude, &airports[index].longitude, &airports[index].altitude,
&airports[index].timezone, &airports[index].DST, airports[index].tzDatabaseTimezone, airports[index].type, airports[index].source);
hacky_empty_fix(airports[index].name);
hacky_empty_fix(airports[index].city);
hacky_empty_fix(airports[index].country);
hacky_empty_fix(airports[index].type);
hacky_empty_fix(airports[index].source);
index++;
}
airports_cout = index;
fclose(fp);
}
int getAirportIndexByICAO(char *ICAO)
{
char *p = ICAO;
while (*p++ = toupper(*p));
for (int i = 0; i < airports_cout; i++)
{
if (strcmp(ICAO, airports[i].ICAO) == 0) return i;
}
return -1;
}
ALuint load_wave(const char * file_name)
{
// First: we open the file and copy it into a single large memory buffer for processing.
FILE * fi = fopen(file_name, "rb");
if (fi == NULL)
{
XPLMDebugString("WAVE file load failed - could not open.\n");
return 0;
}
fseek(fi, 0, SEEK_END);
int file_size = ftell(fi);
fseek(fi, 0, SEEK_SET);
char * mem = (char*)malloc(file_size);
if (mem == NULL)
{
XPLMDebugString("WAVE file load failed - could not allocate memory.\n");
fclose(fi);
return 0;
}
if (fread(mem, 1, file_size, fi) != file_size)
{
XPLMDebugString("WAVE file load failed - could not read file.\n");
free(mem);
fclose(fi);
return 0;
}
fclose(fi);
char * mem_end = mem + file_size;
// Second: find the RIFF chunk. Note that by searching for RIFF both normal
// and reversed, we can automatically determine the endian swap situation for
// this file regardless of what machine we are on.
int swapped = 0;
char * riff = find_chunk(mem, mem_end, RIFF_ID, 0);
if (riff == NULL)
{
riff = find_chunk(mem, mem_end, RIFF_ID, 1);
if (riff)
swapped = 1;
else
FAIL("Could not find RIFF chunk in wave file.\n")
}
// The wave chunk isn't really a chunk at all. :-( It's just a "WAVE" tag
// followed by more chunks. This strikes me as totally inconsistent, but
// anyway, confirm the WAVE ID and move on.
if (riff[0] != 'W' ||
riff[1] != 'A' ||
riff[2] != 'V' ||
riff[3] != 'E')
FAIL("Could not find WAVE signature in wave file.\n")
char * format = find_chunk(riff + 4, chunk_end(riff, swapped), FMT_ID, swapped);
if (format == NULL)
FAIL("Could not find FMT chunk in wave file.\n")
// Find the format chunk, and swap the values if needed. This gives us our real format.
format_info * fmt = (format_info *)format;
if (swapped)
{
fmt->format = SWAP_16(fmt->format);
fmt->num_channels = SWAP_16(fmt->num_channels);
fmt->sample_rate = SWAP_32(fmt->sample_rate);
fmt->byte_rate = SWAP_32(fmt->byte_rate);
fmt->block_align = SWAP_16(fmt->block_align);
fmt->bits_per_sample = SWAP_16(fmt->bits_per_sample);
}
// Reject things we don't understand...expand this code to support weirder audio formats.
if (fmt->format != 1) FAIL("Wave file is not PCM format data.\n")
if (fmt->num_channels != 1 && fmt->num_channels != 2) FAIL("Must have mono or stereo sound.\n")
if (fmt->bits_per_sample != 8 && fmt->bits_per_sample != 16) FAIL("Must have 8 or 16 bit sounds.\n")
char * data = find_chunk(riff + 4, chunk_end(riff, swapped), DATA_ID, swapped);
if (data == NULL)
FAIL("I could not find the DATA chunk.\n")
int sample_size = fmt->num_channels * fmt->bits_per_sample / 8;
int data_bytes = chunk_end(data, swapped) - data;
int data_samples = data_bytes / sample_size;
// If the file is swapped and we have 16-bit audio, we need to endian-swap the audio too or we'll
// get something that sounds just astoundingly bad!
if (fmt->bits_per_sample == 16 && swapped)
{
short * ptr = (short *)data;
int words = data_samples * fmt->num_channels;
while (words--)
{
*ptr = SWAP_16(*ptr);
++ptr;
}
}