forked from g8bpq/OldLinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XAprs.c
4300 lines (3258 loc) · 94.2 KB
/
XAprs.c
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
/*
Copyright 2001-2015 John Wiseman G8BPQ
This file is part of LinBPQ/BPQ32.
LinBPQ/BPQ32 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 3 of the License, or
(at your option) any later version.
LinBPQ/BPQ32 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 LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses
*/
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif
#define LINBPQ
#include "compatbits.h"
#include "bpqaprs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <X11/Xlib.h>
#include <X11/X.h>
#define XK_MISCELLANY
#include <X11/keysymdef.h>
#include <setjmp.h>
#include </usr/include/jpeglib.h>
#include <unistd.h>
#include <sys/mman.h>
#define LIBCONFIG_STATIC
#include "libconfig.h"
#include <gtk/gtk.h>
#include <gtk/gtkadjustment.h>
#include <gtk/gtkwidget.h>
#define VOID void
#define UCHAR unsigned char
#define BOOL int
#define BYTE unsigned char
#define UINT unsigned int
#define TRUE 1
#define FALSE 0
GtkWidget *dialog;
GtkWidget *window;
GtkWidget *dialog;
GtkWidget *window;
GtkWidget *box1;
GtkWidget *box2;
GtkWidget *box3;
GtkWidget *hbox;
GtkWidget *button;
GtkWidget *button2;
GtkWidget *checklabel;
GtkWidget *check1;
GtkWidget *check2;
GtkWidget *check3;
GtkWidget *checkhbox;
GtkWidget *separator;
GtkWidget *table;
GtkWidget *vscrollbar;
GtkWidget *vscrollbar2;
GtkTextBuffer *text;
GtkTextBuffer *text2;
GtkWidget *entry;
GtkWidget *vpaned;
GtkWidget *frame1;
GtkWidget *frame2;
GtkWidget *view;
GtkWidget* scrolledwin;
GtkWidget *view2;
GtkWidget* scrolledwin2;
GtkWidget *box10;
GtkWidget *menubar;
GtkWidget *combo;
GtkWidget *label1, *label2;
GtkListStore *receiveditems;
GtkListStore *sentitems;
GtkTreeModel *model;
char MyFont[50] = "Monospace 10";
gchar *fontname;
char RX_SOCK_PATH[] = "BPQAPRSrxsock";
char TX_SOCK_PATH[] = "BPQAPRStxsock";
int sfd;
struct sockaddr_un my_addr, peer_addr;
socklen_t peer_addr_size;
int maxfd;
VOID ProcessMessage(char * Payload, struct STATIONRECORD * Station);
void UpdateTXMessageLine(struct APRSMESSAGE * Message);
VOID SecTimer();
void plotLine(int x0, int y0, int x1, int y1, COLORREF rgb);
void SelectTXMsg (GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data);
struct SEM
{
UINT Flag;
int Clashes;
int Gets;
int Rels;
};
struct SEM Semaphore = {0, 0, 0, 0};
void GetSemaphore(struct SEM * Semaphore)
{
//
// Wait for it to be free
//
if (Semaphore->Flag != 0)
{
Semaphore->Clashes++;
}
loop1:
while (Semaphore->Flag != 0)
{
Sleep(10);
}
// try to get semaphore
if (__sync_lock_test_and_set(&Semaphore->Flag, 1) != 0)
// Failed to get it
goto loop1; // try again;
//Ok. got it
Semaphore->Gets++;
return;
}
void FreeSemaphore(struct SEM * Semaphore)
{
if (Semaphore->Flag == 0)
printf("Free Semaphore Called when Sem not held\n");
Semaphore->Rels++;
Semaphore->Flag = 0;
return;
}
unsigned long _beginthread(void(*start_address)(), unsigned stack_size, VOID * arglist)
{
pthread_t thread;
if (pthread_create(&thread, NULL, (void * (*)(void *))start_address, (void*) arglist) != 0)
perror("New Thread");
else
pthread_detach(thread);
return thread;
}
int Sleep(int ms)
{
usleep(ms * 1000);
return 0;
}
struct OSMQUEUE OSMQueue = {NULL,0,0,0};
int OSMQueueCount = 0;
static int cxWinSize = 788, cyWinSize = 788;
static int cxImgSize = 768, cyImgSize = 768;
static int topBorder = 30, bottomBorder = 0;
static int leftBorder = 2, rightBorder = 2;
static int cImgChannels = 3;
static int ImgChannels;
int Bytesperpixel = 4;
int ExpireTime = 120;
int TrackExpireTime = 1440;
BOOL SuppressNullPosn = FALSE;
BOOL DefaultNoTracks = FALSE;
BOOL LocalTime = TRUE;
int SlowTimer = 0;
BOOL CreateJPEG = TRUE;
int JPEGInterval = 300;
int JPEGCounter = 0;
char JPEGFileName[MAX_PATH] = "BPQAPRS/HTML/APRSImage.jpg";
char *month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Display * display;
Window root, win;
GC gc;
XImage * image, * popupimage;
int SetBaseX = 0; // Lowest Tiles in currently loaded set
int SetBaseY = 0;
int TileX = 0;
int TileY = 0;
int Zoom = 2;
int MaxZoom = 16;
int MapCentreX = 256;
int MapCentreY = 256;
int MouseX, MouseY;
int PopupX, PopupY;
double MouseLat, MouseLon;
BOOL NeedRefresh = FALSE;
int NeedRedraw = 0;
int ScrollX = 128;
int ScrollY = 128;
int WindowX = 100, WindowY = 100; // Position of window on screen
int WindowWidth = 788;
int WindowHeight = 788;
BOOL popupActive = FALSE;
BOOL selActive = FALSE;
char OSMDir[256] = "BPQAPRS/OSMTiles";
struct STATIONRECORD ** StationRecords = NULL;
struct STATIONRECORD * ControlRecord;
int MaxStations = 5000;
int StationCount;
struct APRSMESSAGE * Messages = NULL;
struct APRSMESSAGE * OutstandingMsgs = NULL;
UCHAR NextSeq = 1;
char APRSCall[10];
char LoppedAPRSCall[10];
// Image chunks are 256 rows of 3 * 256 bytes
// Read 8 * 8 files, and copy to a 2048 * 3 * 2048 array. The display scrolls over this area, and
// it is refreshed when window approaches the edge of the array.
int WIDTH;
int HEIGHT;
int WIDTHTILES = 4;
int HEIGHTTILES = 4;
UCHAR * Image = NULL;
UCHAR * iconImage = NULL;
UCHAR * PopupImage = NULL;
BOOL ImageChanged = 0;
int RetryCount = 7;
int RetryIntervals[] = {0, 512, 256, 128, 64, 32, 16, 8};
// Station Name Font
const unsigned char ASCII[][5] = {
//const u08 ASCII[][5] = {
{0x00, 0x00, 0x00, 0x00, 0x00} // 20
,{0x00, 0x00, 0x5f, 0x00, 0x00} // 21 !
,{0x00, 0x07, 0x00, 0x07, 0x00} // 22 "
,{0x14, 0x7f, 0x14, 0x7f, 0x14} // 23 #
,{0x24, 0x2a, 0x7f, 0x2a, 0x12} // 24 $
,{0x23, 0x13, 0x08, 0x64, 0x62} // 25 %
,{0x36, 0x49, 0x55, 0x22, 0x50} // 26 &
,{0x00, 0x05, 0x03, 0x00, 0x00} // 27 '
,{0x00, 0x1c, 0x22, 0x41, 0x00} // 28 (
,{0x00, 0x41, 0x22, 0x1c, 0x00} // 29 )
,{0x14, 0x08, 0x3e, 0x08, 0x14} // 2a *
,{0x08, 0x08, 0x3e, 0x08, 0x08} // 2b +
,{0x00, 0x50, 0x30, 0x00, 0x00} // 2c ,
,{0x08, 0x08, 0x08, 0x08, 0x08} // 2d -
,{0x00, 0x60, 0x60, 0x00, 0x00} // 2e .
,{0x20, 0x10, 0x08, 0x04, 0x02} // 2f /
,{0x3e, 0x51, 0x49, 0x45, 0x3e} // 30 0
,{0x00, 0x42, 0x7f, 0x40, 0x00} // 31 1
,{0x42, 0x61, 0x51, 0x49, 0x46} // 32 2
,{0x21, 0x41, 0x45, 0x4b, 0x31} // 33 3
,{0x18, 0x14, 0x12, 0x7f, 0x10} // 34 4
,{0x27, 0x45, 0x45, 0x45, 0x39} // 35 5
,{0x3c, 0x4a, 0x49, 0x49, 0x30} // 36 6
,{0x01, 0x71, 0x09, 0x05, 0x03} // 37 7
,{0x36, 0x49, 0x49, 0x49, 0x36} // 38 8
,{0x06, 0x49, 0x49, 0x29, 0x1e} // 39 9
,{0x00, 0x36, 0x36, 0x00, 0x00} // 3a :
,{0x00, 0x56, 0x36, 0x00, 0x00} // 3b ;
,{0x08, 0x14, 0x22, 0x41, 0x00} // 3c <
,{0x14, 0x14, 0x14, 0x14, 0x14} // 3d =
,{0x00, 0x41, 0x22, 0x14, 0x08} // 3e >
,{0x02, 0x01, 0x51, 0x09, 0x06} // 3f ?
,{0x32, 0x49, 0x79, 0x41, 0x3e} // 40 @
,{0x7e, 0x11, 0x11, 0x11, 0x7e} // 41 A
,{0x7f, 0x49, 0x49, 0x49, 0x36} // 42 B
,{0x3e, 0x41, 0x41, 0x41, 0x22} // 43 C
,{0x7f, 0x41, 0x41, 0x22, 0x1c} // 44 D
,{0x7f, 0x49, 0x49, 0x49, 0x41} // 45 E
,{0x7f, 0x09, 0x09, 0x09, 0x01} // 46 F
,{0x3e, 0x41, 0x49, 0x49, 0x7a} // 47 G
,{0x7f, 0x08, 0x08, 0x08, 0x7f} // 48 H
,{0x00, 0x41, 0x7f, 0x41, 0x00} // 49 I
,{0x20, 0x40, 0x41, 0x3f, 0x01} // 4a J
,{0x7f, 0x08, 0x14, 0x22, 0x41} // 4b K
,{0x7f, 0x40, 0x40, 0x40, 0x40} // 4c L
,{0x7f, 0x02, 0x0c, 0x02, 0x7f} // 4d M
,{0x7f, 0x04, 0x08, 0x10, 0x7f} // 4e N
,{0x3e, 0x41, 0x41, 0x41, 0x3e} // 4f O
,{0x7f, 0x09, 0x09, 0x09, 0x06} // 50 P
,{0x3e, 0x41, 0x51, 0x21, 0x5e} // 51 Q
,{0x7f, 0x09, 0x19, 0x29, 0x46} // 52 R
,{0x46, 0x49, 0x49, 0x49, 0x31} // 53 S
,{0x01, 0x01, 0x7f, 0x01, 0x01} // 54 T
,{0x3f, 0x40, 0x40, 0x40, 0x3f} // 55 U
,{0x1f, 0x20, 0x40, 0x20, 0x1f} // 56 V
,{0x3f, 0x40, 0x38, 0x40, 0x3f} // 57 W
,{0x63, 0x14, 0x08, 0x14, 0x63} // 58 X
,{0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y
,{0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z
,{0x00, 0x7f, 0x41, 0x41, 0x00} // 5b [
,{0x02, 0x04, 0x08, 0x10, 0x20} // 5c
,{0x00, 0x41, 0x41, 0x7f, 0x00} // 5d ]
,{0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^
,{0x40, 0x40, 0x40, 0x40, 0x40} // 5f _
,{0x00, 0x01, 0x02, 0x04, 0x00} // 60 `
,{0x20, 0x54, 0x54, 0x54, 0x78} // 61 a
,{0x7f, 0x48, 0x44, 0x44, 0x38} // 62 b
,{0x38, 0x44, 0x44, 0x44, 0x20} // 63 c
,{0x38, 0x44, 0x44, 0x48, 0x7f} // 64 d
,{0x38, 0x54, 0x54, 0x54, 0x18} // 65 e
,{0x08, 0x7e, 0x09, 0x01, 0x02} // 66 f
,{0x0c, 0x52, 0x52, 0x52, 0x3e} // 67 g
,{0x7f, 0x08, 0x04, 0x04, 0x78} // 68 h
,{0x00, 0x44, 0x7d, 0x40, 0x00} // 69 i
,{0x20, 0x40, 0x44, 0x3d, 0x00} // 6a j
,{0x7f, 0x10, 0x28, 0x44, 0x00} // 6b k
,{0x00, 0x41, 0x7f, 0x40, 0x00} // 6c l
,{0x7c, 0x04, 0x18, 0x04, 0x78} // 6d m
,{0x7c, 0x08, 0x04, 0x04, 0x78} // 6e n
,{0x38, 0x44, 0x44, 0x44, 0x38} // 6f o
,{0x7c, 0x14, 0x14, 0x14, 0x08} // 70 p
,{0x08, 0x14, 0x14, 0x18, 0x7c} // 71 q
,{0x7c, 0x08, 0x04, 0x04, 0x08} // 72 r
,{0x48, 0x54, 0x54, 0x54, 0x20} // 73 s
,{0x04, 0x3f, 0x44, 0x40, 0x20} // 74 t
,{0x3c, 0x40, 0x40, 0x20, 0x7c} // 75 u
,{0x1c, 0x20, 0x40, 0x20, 0x1c} // 76 v
,{0x3c, 0x40, 0x30, 0x40, 0x3c} // 77 w
,{0x44, 0x28, 0x10, 0x28, 0x44} // 78 x
,{0x0c, 0x50, 0x50, 0x50, 0x3c} // 79 y
,{0x44, 0x64, 0x54, 0x4c, 0x44} // 7a z
,{0x00, 0x08, 0x36, 0x41, 0x00} // 7b {
,{0x00, 0x00, 0x7f, 0x00, 0x00} // 7c |
,{0x00, 0x41, 0x36, 0x08, 0x00} // 7d }
,{0x10, 0x08, 0x08, 0x10, 0x08} // 7e ~
,{0x78, 0x46, 0x41, 0x46, 0x78} // 7f DEL
};
COLORREF Colours[256] = {0, RGB(0,0,255), RGB(0,128,0), RGB(0,128,192),
RGB(0,192,0), RGB(0,192,255), RGB(0,255,0), RGB(128,0,128),
RGB(128,64,0), RGB(128,128,128), RGB(192,0,0), RGB(192,0,255),
RGB(192,64,128), RGB(192,128,255), RGB(255,0,0), RGB(255,0,255), // 81
RGB(255,64,0), RGB(255,64,128), RGB(255,64,192), RGB(255,128,0)};
struct my_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
typedef struct my_error_mgr * my_error_ptr;
void my_error_exit (j_common_ptr cinfo)
{
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
my_error_ptr myerr = (my_error_ptr) cinfo->err;
char buffer[JMSG_LENGTH_MAX];
/* Create the message */
(*cinfo->err->format_message) (cinfo, buffer);
/* Always display the message. */
printf("JPEG Fatal Error");
/* Return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1);
}
int memicmp(unsigned char *a, unsigned char *b, int n)
{
if (n)
{
while (n && toupper(*a) == toupper(*b))
n--, a++, b++;
if (n)
return toupper(*a) - toupper(*b);
}
return 0;
}
int stricmp(const unsigned char * pStr1, const unsigned char *pStr2)
{
unsigned char c1, c2;
int v;
if (pStr1 == NULL)
{
return 1;
}
do {
c1 = *pStr1++;
c2 = *pStr2++;
/* The casts are necessary when pStr1 is shorter & char is signed */
v = tolower(c1) - tolower(c2);
} while ((v == 0) && (c1 != '\0') && (c2 != '\0') );
return v;
}
char * strupr(char* s)
{
char* p = s;
if (s == 0)
return 0;
while (*p = toupper( *p )) p++;
return s;
}
// Return coorinates in tiles.
double long2x(double lon, int z)
{
return (lon + 180.0) / 360.0 * pow(2.0, z);
}
double lat2y(double lat, int z)
{
return (1.0 - log( tan(lat * M_PI/180.0) + 1.0 / cos(lat * M_PI/180.0)) / M_PI) / 2.0 * pow(2.0, z);
}
double tilex2long(double x, int z)
{
return x / pow(2.0, z) * 360.0 - 180;
}
double tiley2lat(double y, int z)
{
double n = M_PI - 2.0 * M_PI * y / pow(2.0, z);
return 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
}
void GetMouseLatLon(double * Lat, double * Lon)
{
int X = ScrollX + MouseX;
int Y = ScrollY + MouseY;
*Lat = tiley2lat(SetBaseY + (Y / 256.0), Zoom);
*Lon = tilex2long(SetBaseX + (X / 256.0), Zoom);
}
BOOL GetLocPixels(double Lat, double Lon, int * X, int * Y)
{
// Get the pixel offet of supplied location in current image.
// If location is outside current image, return FAlSE
int TileX;
int TileY;
int OffsetX, OffsetY;
double FX;
double FY;
// if TileX or TileY are outside the window, return null
FX = long2x(Lon, Zoom);
TileX = (int)floor(FX);
OffsetX = TileX - SetBaseX;
if (OffsetX < 0 || OffsetX > 7)
return FALSE;
FY = lat2y(Lat, Zoom);
TileY = (int)floor(FY);
OffsetY = TileY - SetBaseY;
if (OffsetY < 0 || OffsetY > 7)
return FALSE;
FX -= TileX;
FX = FX * 256.0;
*X = (int)FX + 256 * OffsetX;
FY -= TileY;
FY = FY * 256.0;
*Y = (int)FY + 256 * OffsetY;
return TRUE;
}
int long2tilex(double lon, int z)
{
return (int)(floor((lon + 180.0) / 360.0 * pow(2.0, z)));
}
int lat2tiley(double lat, int z)
{
return (int)(floor((1.0 - log( tan(lat * M_PI/180.0) + 1.0 / cos(lat * M_PI/180.0)) / M_PI) / 2.0 * pow(2.0, z)));
}
BOOL CentrePositionToMouse(double Lat, double Lon)
{
// Positions specified location at the mouse
int X, Y;
SetBaseX = long2tilex(Lon, Zoom) - 2;
SetBaseY = lat2tiley(Lat, Zoom) - 2; // Set Location at middle
if (GetLocPixels(Lat, Lon, &X, &Y) == FALSE)
return FALSE; // Off map
ScrollX = X - cxWinSize/2;
ScrollY = Y - cyWinSize/2;
// Map is now centered at loc cursor was at
// Need to move by distance mouse is from centre
// if ScrollX, Y are zero, the centre of the map corresponds to 1024, 1024
// ScrollX -= 1024 - X; // Posn to centre
// ScrollY -= 1024 - Y;
ScrollX += cxWinSize/2 - MouseX;
ScrollY += cyWinSize/2 - MouseY;
// May Need to move image
while(ScrollX < 0)
{
SetBaseX--;
ScrollX += 256;
}
while(ScrollY < 0)
{
SetBaseY--;
ScrollY += 256;
}
while(ScrollX > 255)
{
SetBaseX++;
ScrollX -= 256;
}
while(ScrollY > 255)
{
SetBaseY++;
ScrollY -= 256;
}
return TRUE;
}
SOCKADDR_IN destaddr = {0};
unsigned int ipaddr = 0;
//char Host[] = "tile.openstreetmap.org";
//char Host[] = "oatile1.mqcdn.com"; //SAT
char Host[] = "otile1.mqcdn.com";
char HeaderTemplate[] = "Accept: */*\r\nHost: %s\r\nConnection: close\r\nContent-Length: 0\r\nUser-Agent: BPQ32(G8BPQ)\r\n\r\n";
VOID ResolveThread()
{
struct hostent * HostEnt;
int err;
// while (TRUE)
{
// Resolve Name if needed
HostEnt = gethostbyname(Host);
if (!HostEnt)
{
err = WSAGetLastError();
printf("Resolve Failed for %s %d %x", Host, err, err);
}
else
{
memcpy(&destaddr.sin_addr.s_addr,HostEnt->h_addr,4);
}
// Sleep(60 * 15 * 1000);
}
}
VOID OSMGet(int x, int y, int zoom)
{
struct OSMQUEUE * OSMRec = malloc(sizeof(struct OSMQUEUE));
GetSemaphore(&Semaphore);
OSMQueueCount++;
OSMRec->Next = OSMQueue.Next;
OSMQueue.Next = OSMRec;
OSMRec->x = x;
OSMRec->y = y;
OSMRec->Zoom = zoom;
FreeSemaphore(&Semaphore);
}
VOID RefreshTile(char * FN, int TileZoom, int Tilex, int Tiley);
VOID OSMThread()
{
// Request a page from OSM
char FN[256];
char Tile[80];
struct OSMQUEUE * OSMRec;
int Zoom, x, y;
SOCKET sock;
SOCKADDR_IN sinx;
int addrlen=sizeof(sinx);
int err;
u_long param=1;
BOOL bcopt=TRUE;
char Request[100];
char Header[256];
UCHAR Buffer[200000];
int Len, InputLen = 0;
UCHAR * ptr;
int inptr = 0;
struct stat STAT;
FILE * Handle;
destaddr.sin_family = AF_INET;
destaddr.sin_port = htons(80);
while (TRUE)
{
while (OSMQueue.Next)
{
GetSemaphore(&Semaphore);
OSMRec = OSMQueue.Next;
OSMQueue.Next = OSMRec->Next;
OSMQueueCount--;
FreeSemaphore(&Semaphore);
x = OSMRec->x;
y = OSMRec->y;
Zoom = OSMRec->Zoom;
free(OSMRec);
// wsprintf(Tile, "/%02d/%d/%d.png", Zoom, x, y);
// wsprintf(Tile, "/tiles/1.0.0/sat/%02d/%d/%d.jpg", Zoom, x, y);
sprintf(Tile, "/tiles/1.0.0/osm/%02d/%d/%d.jpg", Zoom, x, y);
sprintf(FN, "%s/%02d/%d/%d.jpg", OSMDir, Zoom, x, y);
if (stat(FN, &STAT) == 0)
{
printf(" File %s Exists - skipping\n", FN);
continue;
}
printf("Getting %s\n", FN);
Len = sprintf(Request, "GET %s HTTP/1.0\r\n", Tile);
// Allocate a Socket entry
sock=socket(AF_INET,SOCK_STREAM,0);
if (sock == INVALID_SOCKET)
return;
setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (const char FAR *)&bcopt,4);
if (connect(sock,(LPSOCKADDR) &destaddr, sizeof(destaddr)) != 0)
{
err=WSAGetLastError();
//
// Connect failed
//
break;
}
//GET /15/15810/9778.png HTTP/1.0
//Accept: */*
//Host: tile.openstreetmap.org
//Connection: close
//Content-Length: 0
//User-Agent: APRSIS32(G8BPQ)
InputLen = 0;
inptr = 0;
send(sock, Request, Len, 0);
sprintf(Header, HeaderTemplate, Host);
send(sock, Header, strlen(Header), 0);
while (InputLen != -1)
{
InputLen = recv(sock, &Buffer[inptr], 200000 - inptr, 0);
if (InputLen > 0)
inptr += InputLen;
else
{
// File Complete??
if (strstr(Buffer, " 200 OK"))
{
ptr = strstr(Buffer, "Content-Length:");
if (ptr)
{
int FileLen = atoi(ptr + 15);
ptr = strstr(Buffer, "\r\n\r\n");
if (ptr)
{
ptr += 4;
char Dir[256];
if (FileLen == inptr - (ptr - Buffer))
{
// File is OK
int cnt;
Handle = fopen(FN, "wb");
if (Handle)
{
fwrite(ptr, 1, FileLen, Handle);
fclose(Handle);
printf("Tile %s Loaded\n", FN);
RefreshTile(FN, Zoom, x, y);
break;
}
if (errno != 2) // Bad Path
{
printf("Create %s failed %d\n", FN, errno);
perror("fopen");
break;
}
sprintf(Dir, "%s/%02d/%d", OSMDir, Zoom, x);
if (mkdir(Dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
{
printf("Error Creating %s\n", FN);
perror("mkdir");
break;
}
// Retry Create
Handle = fopen(FN, "wb");
if (Handle)
{
fwrite(ptr, 1, FileLen, Handle);
fclose(Handle);
printf("Tile %s Loaded\n", FN);
RefreshTile(FN, Zoom, x, y);
break;
}
printf("Create %s falled\n", FN);
perror("fopen");
break;
}
}
}
}
printf("OSM GET Bad Response %s ", Buffer);
sprintf(FN, "%s/DummyTile.jpg", OSMDir);
RefreshTile(FN, Zoom, x, y);
break;
}
}
closesocket(sock);
}
// Queue is empty
sleep(1);
}
}
double radians(double Degrees)
{
return M_PI * Degrees / 180;
}
double degrees(double Radians)
{
return Radians * 180 / M_PI;
}
double Distance(double laa, double loa)
{
double lah = ControlRecord->Lat;
double loh = ControlRecord->Lon;
/*
'Great Circle Calculations.
'dif = longitute home - longitute away
' (this should be within -180 to +180 degrees)
' (Hint: This number should be non-zero, programs should check for
' this and make dif=0.0001 as a minimum)
'lah = latitude of home
'laa = latitude of away
'dis = ArcCOS(Sin(lah) * Sin(laa) + Cos(lah) * Cos(laa) * Cos(dif))
'distance = dis / 180 * pi * ERAD
'angle = ArcCOS((Sin(laa) - Sin(lah) * Cos(dis)) / (Cos(lah) * Sin(dis)))
'p1 = 3.1415926535: P2 = p1 / 180: Rem -- PI, Deg =>= Radians
*/
loh = radians(loh); lah = radians(lah);
loa = radians(loa); laa = radians(laa);
return 60*degrees(acos(sin(lah) * sin(laa) + cos(lah) * cos(laa) * cos(loa-loh))) * 1.15077945;
}
double Bearing(double lat2, double lon2)
{
double lat1 = ControlRecord->Lat;
double lon1 = ControlRecord->Lon;
double dlat, dlon, TC1;
lat1 = radians(lat1);
lat2 = radians(lat2);
lon1 = radians(lon1);
lon2 = radians(lon2);
dlat = lat2 - lat1;
dlon = lon2 - lon1;
if (dlat == 0 || dlon == 0) return 0;
TC1 = atan((sin(lon1 - lon2) * cos(lat2)) / (cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lon1 - lon2)));
TC1 = degrees(TC1);
if (fabs(TC1) > 89.5) if (dlon > 0) return 90; else return 270;
if (dlat > 0)
{
if (dlon > 0) return -TC1;
if (dlon < 0) return 360 - TC1;
return 0;
}
if (dlat < 0)
{
if (dlon > 0) return TC1 = 180 - TC1;
if (dlon < 0) return TC1 = 180 - TC1; // 'ok?
return 180;
}
return 0;
}
VOID DecodeWXReport(struct APRSConnectionInfo * sockptr, char * WX)
{
UCHAR * ptr = strchr(WX, '_');
char Type;
int Val;
if (ptr == 0)
return;
sockptr->WindDirn = atoi(++ptr);
ptr += 4;
sockptr->WindSpeed = atoi(ptr);
ptr += 3;
WXLoop:
Type = *(ptr++);
if (*ptr =='.') // Missing Value
{
while (*ptr == '.')
ptr++;
goto WXLoop;
}
Val = atoi(ptr);
switch (Type)
{
case 'c': // = wind direction (in degrees).
sockptr->WindDirn = Val;
break;
case 's': // = sustained one-minute wind speed (in mph).
sockptr->WindSpeed = Val;
break;
case 'g': // = gust (peak wind speed in mph in the last 5 minutes).
sockptr->WindGust = Val;
break;
case 't': // = temperature (in degrees Fahrenheit). Temperatures below zero are expressed as -01 to -99.