-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.cpp
758 lines (629 loc) · 15.8 KB
/
content.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
/*
* This file is part of fra-airtraffic screensaver,
* https://github.com/flederwiesel/fra-airtraffic
* Copyright © 2017 Tobias Kühne
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "container.h"
#include "content.h"
#include "fra-airtraffic.h"
#include "gc.h"
#include "stdpaths.h"
#include <float.h>
#include <limits>
#include <stdexcept>
#ifdef _MSC_VER
#ifdef NAN
#undef NAN
#endif
static const double NAN = std::numeric_limits<double>::quiet_NaN();
#endif
wxBitmap SaverContent::bkgnd;
wxBitmap SaverContent::clock;
int SaverContent::PointSize = 0;
airport_t *SaverContent::fra = NULL;
std::vector<airport_t> SaverContent::airports;
std::vector<flight_t> SaverContent::flights;
long SaverContent::now = -1;
unsigned long SaverContent::arrivals = 0;
unsigned long SaverContent::departures = 0;
#ifdef _DEBUG
size_t SaverContent::selected = 0;
#endif
unsigned long SaverContent::refs = 0;
SaverContent *SaverContent::savers[16];
BEGIN_EVENT_TABLE(SaverContent, wxWindow)
EVT_PAINT(SaverContent::OnPaint)
EVT_SIZE(SaverContent::OnSize)
EVT_KEY_DOWN(SaverContent::OnKeyDown)
EVT_MOUSE_EVENTS(SaverContent::OnMouse)
END_EVENT_TABLE()
SaverContent::SaverContent(wxWindow* parent) :
wxWindow(parent, 0)
{
static int init = 1;
if (init)
{
InitialiseStaticVars();
init = 0;
}
SetBackgroundStyle(wxBG_STYLE_PAINT);
if (refs < ElementsIn(savers))
savers[refs++] = this;
}
SaverContent::~SaverContent()
{
size_t n;
if (0 == --refs)
{
for (n = 0; n < airports.size(); n++)
{
if (airports[n].icao)
free((void*)airports[n].icao);
#ifdef _DEBUG
if (airports[n].iata)
free((void*)airports[n].iata);
if (airports[n].name)
free((void*)airports[n].name);
#endif
}
#ifdef _DEBUG
for (n = 0; n < flights.size(); n++)
{
if (flights[n].code)
free((void*)flights[n].code);
}
#endif
}
}
long SaverContent::InitialiseStaticVars()
{
size_t num;
unsigned int month;
struct tm *local;
airport_t *ap;
flight_t *f;
size_t i;
// Initialise `now` to the current 10-min interval
time((time_t*)&now);
local = localtime((time_t*)&now);
if (local)
{
now = local->tm_hour * 3600 + (local->tm_min - local->tm_min % 10) * 60;
month = (unsigned int)local->tm_mon + 1;
}
else
{
now %= 86400;
now -= now % 600;
month = 2;
}
// Load Images
wxImage::AddHandler(new wxJPEGHandler);
wxImage::AddHandler(new wxPNGHandler);
// Earth background image
// https://visibleearth.nasa.gov/useterms.php
#ifdef _DEBUG
wxString imagepath(wxGetCwd() + wxT("/img/bluemarble"));
#else
wxString imagepath(MyStandardPaths::Get().GetResourceDir() +
wxT("/flederwiesel/fra-airtraffic/img"));
#endif
imagepath.append(wxString::Format(wxT("/1920x960/bmng/%02d.jpg"), month));
bkgnd.LoadFile(imagepath, wxBITMAP_TYPE_JPEG);
// Clock
#ifdef _DEBUG
imagepath = wxGetCwd() + wxT("/img/clock.png");
#else
imagepath = MyStandardPaths::Get().GetResourceDir() +
wxT("/flederwiesel/fra-airtraffic/img/clock.png");
#endif
clock.LoadFile(imagepath, wxBITMAP_TYPE_PNG);
// Flight data
#ifdef _DEBUG
wxString data(wxGetCwd() + wxT("/json/schedule-debug.json"));
#else
wxString data(MyStandardPaths::Get().GetDataDir() +
wxT("/flederwiesel/fra-airtraffic/schedule.json"));
#endif
// ParseSchedule() populates `airports` and `flights`
ParseJSON(data.c_str(), ParseSchedule);
if (0 == airports.size() ||
0 == flights.size())
{
throw std::runtime_error("Error initialising tables.");
}
else
{
// Get FRA coordinates
num = airports.size();
while (num--)
{
if (airports[num].icao)
if (0 == strcmp("EDDF", airports[num].icao))
{
fra = &airports[num];
break;
}
}
if (NULL == fra)
throw std::runtime_error("FRA disappeared!");
// Get distance and bearing for each flight
for (i = 0; i < flights.size(); i++)
{
f = &flights[i];
if (f->airport < airports.size())
{
ap = &airports[f->airport];
if (!wxIsNaN(ap->lat) &&
!wxIsNaN(ap->lon))
{
if (fra == ap)
{
f->distance = 0.0;
f->bearing = 0.0;
}
else
{
f->distance = GCDistance(fra->lat, fra->lon, ap->lat, ap->lon);
f->distance *= EARTH_RADIUS;
// openflights.org calculates flight duration as
// "30 min plus 1 hour per every 500 miles".
// If it fits their purpose, it is good enough for us...
if (f->duration < 0)
f->duration = -1800 - (long)(f->distance / 5.0 * 36.0);
else
f->duration = 1800 + (long)(f->distance / 5.0 * 36.0);
if (f->duration > 0)
f->bearing = GCBearing(fra->lat, fra->lon, ap->lat, ap->lon);
else
f->bearing = GCBearing(ap->lat, ap->lon, fra->lat, fra->lon);
}
}
}
}
}
return now;
}
void SaverContent::SetPointSize(unsigned int size)
{
PointSize = size;
}
void SaverContent::OnSize(wxSizeEvent& event)
{
int cx, cy;
int padding;
wxFontMetrics fm;
wxWindowDC dc(this);
dc.GetSize(&cx, &cy);
// Determine if we're boxed, if so, do not display text
SaverContainer *parent = dynamic_cast<SaverContainer*>(GetParent());
if (NULL == parent)
{
padding = 0;
}
else
{
dc.SetFont(wxFont(12,
wxFONTFAMILY_DEFAULT,
wxFONTSTYLE_NORMAL,
wxFONTWEIGHT_NORMAL));
fm = dc.GetFontMetrics();
padding = 3 * fm.height;
}
// Scale background image to full screen width,
// unless this would overflow screen height
if (cx / 2 + 2 * padding > cy)
{
rc.y = padding;
rc.height = cy - rc.y * 2;
rc.width = rc.height * 2;
rc.x = (cx - rc.width) / 2;
}
else
{
rc.width = cx;
rc.height = cx / 2;
rc.y = (cy - rc.height) / 2;
rc.x = 0;
}
Refresh(true);
}
void SaverContent::DrawInfoText(wxAutoBufferedPaintDC &dc)
{
wxMemoryDC mdc;
wxFontMetrics fm;
wxString text;
wxSize extent;
int ch;
int x, y;
int radius;
int min;
// Draw area above/below bitmap
dc.SetPen(*wxTRANSPARENT_PEN); // No bounding rect
dc.SetTextForeground(wxColour(255, 255, 255));
dc.SetFont(wxFont(12,
wxFONTFAMILY_DEFAULT,
wxFONTSTYLE_NORMAL,
wxFONTWEIGHT_NORMAL));
fm = dc.GetFontMetrics();
ch = fm.ascent - fm.externalLeading - fm.internalLeading;
x = rc.x;
y = rc.y - 2 * fm.height - fm.descent;
////////////////////////////////////////////////////////////////////
//// FRA
text = wxT("FRA");
extent = dc.GetMultiLineTextExtent(text);
dc.DrawText(text, x, y);
x += extent.x + fm.averageWidth / 2;
//// FRA (└)
// Make the clock height odd, as for small images the pointer
// will be rendered better - as middle pixel
radius = ch + (ch + 1) % 2;
mdc.SelectObject(clock);
dc.StretchBlit(x, y + fm.externalLeading + fm.internalLeading - (ch + 1) % 2,
radius, radius,
&mdc, 0, 0, clock.GetWidth(), clock.GetHeight());
mdc.SelectObject(wxNullBitmap);
x += radius + fm.averageWidth / 2;
//// FRA (└) 00:00
min = (now % 3600) / 60;
min -= min % 10;
text = wxString::Format(wxT("%02lu:%02lu"), now / 3600, min);
dc.DrawText(text, x, y);
#ifdef _DEBUG
////////////////////////////////////////////////////////////////////
//// [0] FLIGHT#
if (flights[selected].code)
{
x = rc.x;
dc.DrawText(wxString::Format("[%llu] %s",
(unsigned long long)selected, flights[selected].code),
x, y + fm.height);
}
#endif
////////////////////////////////////////////////////////////////////
//// []
//// []
x = rc.x + rc.width - ch + ch / 4;
y = rc.y - 2 * fm.height + ch / 4 + 1;
dc.SetBrush(wxBrush(wxColour(255, 255, 0)));
dc.DrawRectangle(x, y, ch / 2, ch / 2);
dc.SetBrush(wxBrush(wxColour(255, 127, 0)));
dc.DrawRectangle(x, y + fm.height, ch / 2, ch / 2);
//// arrivals []
//// departures []
x -= ch / 2;
y = rc.y - 2 * fm.height - fm.descent;
text = wxString::Format(wxT("%lu"), arrivals);
extent = dc.GetMultiLineTextExtent(text);
dc.DrawText(text, x - extent.x, y);
text = wxString::Format(wxT("%lu"), departures);
extent = dc.GetMultiLineTextExtent(text);
dc.DrawText(text, x - extent.x, y + fm.height);
////////////////////////////////////////////////////////////////////
//// Credit of earth image
dc.SetFont(wxFont(8,
wxFONTFAMILY_DEFAULT,
wxFONTSTYLE_NORMAL,
wxFONTWEIGHT_NORMAL));
dc.DrawText(wxT("Earth image: Credit Reto St\u00f6ckli, ")
wxT("NASA Earth Observatory http://visibleearth.nasa.gov"),
rc.x, rc.y + rc.height);
}
void SaverContent::OnPaint(wxPaintEvent &event)
{
wxAutoBufferedPaintDC dc(this);
wxMemoryDC mdc;
airport_t *ap;
flight_t *f;
gps_t pos;
long departure;
long arrival;
unsigned long arr, dep;
double completed;
unsigned int i;
int x, y;
int cx, cy;
int w;
wxString text;
wxSize extent;
dc.SetBrush(wxBrush(wxColour(0, 0, 0)));
dc.GetSize(&cx, &cy);
if (rc.x)
{
// Draw area left/right of bitmap
dc.DrawRectangle(0, 0, rc.x, cy);
dc.DrawRectangle(rc.x + rc.width, 0, rc.x, cy);
}
if (rc.y)
{
// Draw area above/below bitmap
dc.DrawRectangle(0, 0, cx, rc.y);
dc.DrawRectangle(0, rc.y + rc.height, cx, rc.y);
}
// Draw bitmap
if (bkgnd.IsNull())
{
dc.FloodFill(0, 0, wxColour(0, 127, 0), wxFLOOD_SURFACE);
}
else
{
mdc.SelectObject(bkgnd);
dc.StretchBlit(rc.x, rc.y, rc.width, rc.height,
&mdc, 0, 0, bkgnd.GetWidth(), bkgnd.GetHeight());
mdc.SelectObject(wxNullBitmap);
}
dc.SetBrush(wxBrush(wxColour(0, 127, 127)));
dc.SetPen(*wxTRANSPARENT_PEN); // No bounding rect
if (PointSize)
w = PointSize;
else
w = (rc.width >= 640) ? 3 : (rc.width > 320) ? 2 : 1;
for (i = 0; i < airports.size(); i++)
{
if (!wxIsNaN(airports[i].lat) &&
!wxIsNaN(airports[i].lon))
{
x = (int)(( airports[i].lon + 180.0) / 360.0 * (double)rc.width) + rc.x;
y = (int)((-airports[i].lat + 90.0) / 180.0 * (double)rc.height) + rc.y;
dc.DrawRectangle(x - w / 2, y - w / 2, w, w);
}
}
// When painting, also count arrivals/departures
arr = 0;
dep = 0;
for (i = 0; i < flights.size(); i++)
{
f = &flights[i];
if (f->airport < airports.size())
{
ap = &airports[f->airport];
if (!wxIsNaN(ap->lat) &&
!wxIsNaN(ap->lon))
{
// Is this flight airborne?
if (f->duration > 0)
{
departure = f->scheduled;
arrival = f->scheduled + f->duration;
if (arrival - now > 86400)
arrival -= 86400;
if (departure > arrival)
departure -= 86400;
}
else
{
arrival = f->scheduled;
departure = f->scheduled + f->duration;
if (now - departure > 86400)
departure += 86400;
if (arrival < departure)
arrival += 86400;
}
if (now >= departure && now <= arrival)
{
// yes
if (f->duration < 0)
arr++;
else
dep++;
if (0.0 == f->duration)
{
pos.lat = ap->lat;
pos.lon = ap->lon;
}
else
{
completed = f->distance * (double)(now - departure) /
(double)(f->duration < 0 ? -f->duration : f->duration);
completed /= EARTH_RADIUS;
if (f->duration > 0)
pos = GCTravel(fra->lat, fra->lon, completed, f->bearing);
else
pos = GCTravel(ap->lat, ap->lon, completed, f->bearing);
}
// As from FRA no flights will wrap around, this is sufficient
x = (int)(( pos.lon + 180.0) / 360.0 * (double)rc.width) + rc.x;
y = (int)((-pos.lat + 90.0) / 180.0 * (double)rc.height) + rc.y;
#ifdef _DEBUG
if (selected == i)
{
// Draw cross hairs
dc.SetPen(wxPen(wxColour(255, 0, 0), 1, wxPENSTYLE_LONG_DASH));
dc.SetBrush(*wxTRANSPARENT_BRUSH);
dc.DrawCircle(x, y, 12);
dc.DrawLine(x + 3, y, x + 100, y);
dc.DrawLine(x - 3, y, x - 100, y);
dc.DrawLine(x, y + 3, x, y + 100);
dc.DrawLine(x, y - 3, x, y - 100);
dc.SetPen(*wxTRANSPARENT_PEN);
}
#endif
if (f->duration < 0)
dc.SetBrush(wxBrush(wxColour(255, 255, 0)));
else
dc.SetBrush(wxBrush(wxColour(255, 127, 0)));
dc.DrawRectangle(x - w / 2, y - w / 2, w, w);
}
}
}
}
if (0 == arrivals || 0 == now % 600)
arrivals = arr;
if (0 == departures || 0 == now % 600)
departures = dep;
// Determine if we're boxed, if so, do not display text
SaverContainer *parent = dynamic_cast<SaverContainer*>(GetParent());
if (parent)
if (wxIS_KIND_OF(parent, SaverContainer))
DrawInfoText(dc);
}
// Mouse events will be deliviered to the widget the cursor is hovering above
// Keyboard events, however will be delivered to the SaverContainer widget, which
// has the keyboard focus
void SaverContent::OnMouse(wxMouseEvent &event)
{
#ifndef _DEBUG
event.ResumePropagation(1);
event.Skip ();
#endif
}
void SaverContent::OnKeyDown(wxKeyEvent &event)
{
#ifdef _DEBUG
unsigned long i;
switch (event.GetKeyCode())
{
case WXK_UP:
selected++;
selected %= flights.size();
for (i = 0; i < refs; i++)
savers[i]->Refresh(true);
break;
case WXK_DOWN:
if (0 == selected--)
selected = flights.size() - 1;
for (i = 0; i < refs; i++)
savers[i]->Refresh(true);
break;
default:
#endif
event.ResumePropagation(1);
event.Skip ();
#ifdef _DEBUG
}
#endif
}
#define append push_back
void SaverContent::ParseSchedule(const char *name, const char *value, JsonType_t type)
{
size_t n;
static airport_t a;
static flight_t f;
static enum
{
OBJTYPE_UNKNOWN,
OBJTYPE_AIRPORT,
OBJTYPE_FLIGHT,
} objtype = OBJTYPE_UNKNOWN;
if (JSON_NULL == type)
{
// error
throw(std::runtime_error((const char*)(wxString(name) + wxT(": ") + wxString(value))));
}
else if (JSON_ARRAY == type)
{
if (name)
{
if (0 == strcmp("airports", name))
objtype = OBJTYPE_AIRPORT;
if (0 == strcmp("flights", name))
objtype = OBJTYPE_FLIGHT;
}
}
else if (JSON_OBJECT == type)
{
if (OBJTYPE_AIRPORT == objtype)
{
a.icao = NULL;
#ifdef _DEBUG
a.iata = NULL;
a.name = NULL;
#endif
a.lat = NAN;
a.lon = NAN;
airports.append(a);
}
if (OBJTYPE_FLIGHT == objtype)
{
#ifdef _DEBUG
f.code = NULL;
#endif
f.airport = 0;
f.scheduled = 0;
f.duration = 0;
flights.append(f);
}
}
else
{
if (OBJTYPE_AIRPORT == objtype)
{
n = airports.size();
if (n-- > 0)
{
if (0 == strcmp("lat", name))
{
sscanf(value, "%lf", &airports[n].lat);
}
else if (0 == strcmp("lon", name))
{
sscanf(value, "%lf", &airports[n].lon);
}
else if (0 == strcmp("icao", name))
{
airports[n].icao = strdup(value);
}
#ifdef _DEBUG
else if (0 == strcmp("iata", name))
{
airports[n].iata = strdup(value);
}
else if (0 == strcmp("name", name))
{
airports[n].name = strdup(value);
}
#endif
}
}
if (OBJTYPE_FLIGHT == objtype)
{
n = flights.size();
if (n--)
{
if (0 == strcmp("airport", name))
{
sscanf(value, "%lu", &flights[n].airport);
}
else if (0 == strcmp("scheduled", name))
{
sscanf(value, "%ld", &flights[n].scheduled);
}
else if (0 == strcmp("duration", name))
{
sscanf(value, "%ld", &flights[n].duration);
}
#ifdef _DEBUG
else if (0 == strcmp("flight", name))
{
flights[n].code = strdup(value);
}
#endif
}
}
}
}
#undef append
void SaverContent::GetTime(long &time)
{
time = now;
}
void SaverContent::SetTime(long time)
{
now = time % 86400;
for (unsigned long i = 0; i < refs; i++)
savers[i]->Refresh(0 == now % 600);
}