summaryrefslogtreecommitdiff
path: root/cgps.c
blob: 933c00a0acb496bf42e6b8571cb192a915cc9f96 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
/*
 * Copyright (c) 2005 Jeff Francis <jeff@gritch.org>
 *
 * This file is Copyright (c)2010-2018 by the GPSD project
 * SPDX-License-Identifier: BSD-2-clause
 */

/*
  Jeff Francis
  jeff@gritch.org

  Kind of a curses version of xgps for use with gpsd.
*/

/*
 * The True North compass fails with current gpsd versions for reasons
 * the dev team has been unable to diagnose due to not having test hardware.
 * The support for it is conditioned out in order to simplify moving
 * to the new JSON-based protocol and reduce startup time.
 */
#undef TRUENORTH

/* ==================================================================
   These #defines should be modified if changing the number of fields
   to be displayed.
   ================================================================== */

/* This defines how much overhead is contained in the 'datawin' window
   (eg, box around the window takes two lines). */
#define DATAWIN_OVERHEAD 2

/* This defines how much overhead is contained in the 'satellites'
   window (eg, box around the window takes two lines, plus the column
   headers take another line). */
#define SATWIN_OVERHEAD 2

/* This is how many display fields are output in the 'datawin' window
   when in GPS mode.  Change this value if you add or remove fields
   from the 'datawin' window for the GPS mode. */
#define DATAWIN_GPS_FIELDS 8

/* Count of optional fields that we'll display if we have the room. */
#define DATAWIN_OPTIONAL_FIELDS 7

/* This is how many display fields are output in the 'datawin' window
   when in COMPASS mode.  Change this value if you add or remove fields
   from the 'datawin' window for the COMPASS mode. */
#define DATAWIN_COMPASS_FIELDS 6

/* This is how far over in the 'datawin' window to indent the field
   descriptions. */
#define DATAWIN_DESC_OFFSET 2

/* This is how far over in the 'datawin' window to indent the field
   values. */
#define DATAWIN_VALUE_OFFSET 17

/* This is the width of the 'datawin' window.  It's recommended to
   keep DATAWIN_WIDTH + SATELLITES_WIDTH <= 80 so it'll fit on a
   "standard" 80x24 screen. */
#define DATAWIN_WIDTH 45

/* This is the width of the 'satellites' window.  It's recommended to
   keep DATAWIN_WIDTH + SATELLITES_WIDTH <= 80 so it'll fit on a
   "standard" 80x24 screen. */
#define SATELLITES_WIDTH 35

/* ================================================================
   You shouldn't have to modify any #define values below this line.
   ================================================================ */

/* This is the minimum ysize we'll accept for the 'datawin' window in
   GPS mode. */
#define MIN_GPS_DATAWIN_YSIZE (DATAWIN_GPS_FIELDS + DATAWIN_OVERHEAD)

/* And the maximum ysize we'll try to use */
#define MAX_GPS_DATAWIN_YSIZE (DATAWIN_GPS_FIELDS + DATAWIN_OPTIONAL_FIELDS + DATAWIN_OVERHEAD)

/* This is the minimum ysize we'll accept for the 'datawin' window in
   COMPASS mode. */
#define MIN_COMPASS_DATAWIN_YSIZE (DATAWIN_COMPASS_FIELDS + DATAWIN_OVERHEAD)

/* This is the maximum number of satellites gpsd can track. */
#define MAX_POSSIBLE_SATS (MAXCHANNELS - 2)

/* This is the maximum ysize we need for the 'satellites' window. */
#define MAX_SATWIN_SIZE (MAX_POSSIBLE_SATS + SATWIN_OVERHEAD)

/* Minimum xsize to display 3rd window with DOPs, etc. */
#define MIN_ERRWIN_SIZE 100

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <curses.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <ctype.h>

#include "gpsd_config.h"
#include "gps.h"
#include "gps_json.h"   /* for GPS_JSON_RESPONSE_MAX */
#include "compiler.h"   /* for UNUSED */
#include "gpsdclient.h"
#include "revision.h"
#include "os_compat.h"

static struct gps_data_t gpsdata;
static time_t status_timer;     /* Time of last state change. */
static int state = 0;           /* or MODE_NO_FIX=1, MODE_2D=2, MODE_3D=3 */
static float altfactor = METERS_TO_FEET;
static float speedfactor = MPS_TO_MPH;
static char *altunits = "ft";
static char *speedunits = "mph";
static struct fixsource_t source;
static int debug;

static WINDOW *datawin, *satellites, *messages;

static bool raw_flag = false;
static bool show_ecefs = false;     /* taller screen, show ECEFs */
static bool show_more_dops = false;      /* tall screen, show more DOPs */
static bool silent_flag = false;
static bool magnetic_flag = false;
static int window_ysize = 0;
static int display_sats = 0;        /* number of rows of sats to display */
#ifdef TRUENORTH
static bool compass_flag = false;
#endif /* TRUENORTH */

/* pseudo-signals indicating reason for termination */
#define CGPS_QUIT       0       /* voluntary termination */
#define GPS_GONE        -1      /* GPS device went away */
#define GPS_ERROR       -2      /* low-level failure in GPS read */
#define GPS_TIMEOUT     -3      /* low-level failure in GPS waiting */

/* range test an int, return 4 chars + NUL */
static const char *int_to_str(int val, int min, int max)
{
    static char buf[20];

    if (val < min || val > max) {
        return " n/a";
    }
    (void)snprintf(buf, sizeof(buf), "%4d", val);
    return buf;
}

/* format a DOP into a 5 char string, handle NAN, INFINITE */
static char *dop_to_str(double dop)
{
    static char buf[20];

    if (isfinite(dop) == 0) {
        return " n/a ";
    }
    (void)snprintf(buf, sizeof(buf), "%5.2f", dop);
    return buf;
}

/* format an EP into a string, handle NAN, INFINITE */
static char *ep_to_str(double ep, double factor, char *units)
{
    static char buf[20];
    double val;

    if (isfinite(ep) == 0) {
        return " n/a  ";
    }
    val = ep * factor;
    if ( 100 <= val ) {
        (void)snprintf(buf, sizeof(buf), "+/-%5d %.3s", (int)val, units);
    } else {
        (void)snprintf(buf, sizeof(buf), "+/-%5.1f %.3s", val, units);
    }
    return buf;
}

/* format an ECEF p and v into a string, handle NAN, INFINITE */
static char *ecef_to_str(double pos, double vel, double factor, char *units)
{
    static char buf[128];

    if (isfinite(pos) == 0) {
        if (isfinite(vel) == 0) {
            return "  n/a    n/a ";
        } else {
            (void)snprintf(buf, sizeof(buf), "  n/a %8.3f%.4s/s",
                           vel * factor, units);
        }
    } else {
        (void)snprintf(buf, sizeof(buf), "% 14.3f%.4s %8.3f%.4s/s",
                       pos * factor, units,  vel * factor, units);
    }
    return buf;
}

/* Function to call when we're all done.  Does a bit of clean-up. */
static void die(int sig)
{
    if (!isendwin())
    {
        /* Move the cursor to the bottom left corner. */
        (void)mvcur(0, COLS - 1, LINES - 1, 0);

        /* Put input attributes back the way they were. */
        (void)echo();

        /* Done with curses. */
        (void)endwin();
    }

    /* We're done talking to gpsd. */
    (void)gps_close(&gpsdata);

    switch (sig) {
    case CGPS_QUIT:
        break;
    case GPS_GONE:
        (void)fprintf(stderr, "cgps: GPS hung up.\n");
        break;
    case GPS_ERROR:
        (void)fprintf(stderr, "cgps: GPS read returned error\n");
        break;
    case GPS_TIMEOUT:
        (void)fprintf(stderr, "cgps: GPS timeout\n");
        break;
    default:
        (void)fprintf(stderr, "cgps: caught signal %d\n", sig);
        break;
    }

    /* Bye! */
    exit(EXIT_SUCCESS);
}

static enum deg_str_type deg_type = deg_dd;

static void windowsetup(void)
/* initialize curses and set up screen windows */
{
    /* Set the window sizes per the following criteria:
     *
     * 1.  Set the window size to display the maximum number of
     * satellites possible, but not more than can be fit in a
     * window the size of the GPS report window. We have to set
     * the limit that way because MAXCHANNELS has been made large
     * in order to prepare for survey-grade receivers..
     *
     * 2.  If the screen size will not allow for the full complement of
     * satellites to be displayed, set the windows sizes smaller, but
     * not smaller than the number of lines necessary to display all of
     * the fields in the 'datawin'.  The list of displayed satellites
     * will be truncated to fit the available window size.  (TODO: If
     * the satellite list is truncated, omit the satellites not used to
     * obtain the current fix.)
     *
     * 3.  If the screen is tall enough to display all possible
     * satellites (MAXCHANNELS - 2) with space still left at the bottom,
     * add a window at the bottom in which to scroll raw gpsd data.
     *
     * 4.  If the screen is tall enough to display extra data, expand
     * data window down to show DOPs, ECEFs, etc.
     */
    int xsize, ysize;

    /* Fire up curses. */
    (void)initscr();
    (void)noecho();
    getmaxyx(stdscr, ysize, xsize);
    /* turn off cursor */
    curs_set(0);

#ifdef TRUENORTH
    if (compass_flag) {
        if (ysize == MIN_COMPASS_DATAWIN_YSIZE) {
            raw_flag = false;
            window_ysize = MIN_COMPASS_DATAWIN_YSIZE;
        } else if (ysize > MIN_COMPASS_DATAWIN_YSIZE) {
            raw_flag = true;
            window_ysize = MIN_COMPASS_DATAWIN_YSIZE;
        } else {
            (void)mvprintw(0, 0,
                           "Your screen must be at least 80x%d to run cgps.",
                           MIN_COMPASS_DATAWIN_YSIZE);
            (void)refresh();
            (void)sleep(5);
            die(0);
        }
    } else
#endif /* TRUENORTH */
    {
        if (ysize > MAX_GPS_DATAWIN_YSIZE + 10) {
            raw_flag = true;
            show_ecefs = true;
            show_more_dops = true;
            window_ysize = MAX_GPS_DATAWIN_YSIZE + 7;
        } else if (ysize > MAX_GPS_DATAWIN_YSIZE + 6) {
            raw_flag = true;
            show_ecefs = false;
            show_more_dops = true;
            window_ysize = MAX_GPS_DATAWIN_YSIZE + 4;
        } else if (ysize > MAX_GPS_DATAWIN_YSIZE) {
            raw_flag = true;
            show_ecefs = false;
            show_more_dops = false;
            window_ysize = MAX_GPS_DATAWIN_YSIZE;
        } else if (ysize == MAX_GPS_DATAWIN_YSIZE) {
            raw_flag = false;
            show_ecefs = false;
            show_more_dops = false;
            window_ysize = MAX_GPS_DATAWIN_YSIZE;
        } else if (ysize > MIN_GPS_DATAWIN_YSIZE) {
            raw_flag = true;
            show_ecefs = false;
            show_more_dops = false;
            window_ysize = MIN_GPS_DATAWIN_YSIZE;
        } else if (ysize == MIN_GPS_DATAWIN_YSIZE) {
            raw_flag = false;
            show_ecefs = false;
            show_more_dops = false;
            window_ysize = MIN_GPS_DATAWIN_YSIZE;
        } else {
            (void)mvprintw(0, 0,
                           "Your screen must be at least 80x%d to run cgps.",
                           MIN_GPS_DATAWIN_YSIZE);
            (void)refresh();
            (void)sleep(5);
            die(0);
        }
        display_sats = window_ysize - SATWIN_OVERHEAD - (int)raw_flag;
    }

#ifdef TRUENORTH
    /* Set up the screen for either a compass or a gps receiver. */
    if (compass_flag) {
        /* We're a compass, set up accordingly. */
        int row = 1;

        datawin = newwin(window_ysize, DATAWIN_WIDTH, 0, 0);
        (void)nodelay(datawin, (bool) TRUE);
        if (raw_flag) {
            messages = newwin(0, 0, window_ysize, 0);

            (void)scrollok(messages, true);
            (void)wsetscrreg(messages, 0, ysize - (window_ysize));
        }

        (void)refresh();

        /* Do the initial field label setup. */
        (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Time:");
        (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Heading:");
        (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Pitch:");
        (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Roll:");
        (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Dip:");
        (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Rcvr Type:");
        (void)wborder(datawin, 0, 0, 0, 0, 0, 0, 0, 0);

    } else
#endif /* TRUENORTH */
    {
        /* We're a GPS, set up accordingly. */
        int row = 1;

        datawin = newwin(window_ysize, DATAWIN_WIDTH, 0, 0);
        satellites =
            newwin(window_ysize, SATELLITES_WIDTH, 0, DATAWIN_WIDTH);
        (void)nodelay(datawin, (bool) TRUE);
        if (raw_flag) {
            messages =
                newwin(ysize - (window_ysize), xsize, window_ysize, 0);

            (void)scrollok(messages, true);
            (void)wsetscrreg(messages, 0, ysize - (window_ysize));
        }

        (void)refresh();

        /* Do the initial field label setup. */
        (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Time:");
        (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Latitude:");
        (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Longitude:");
        (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Altitude:");
        (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Speed:");
        (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Heading:");
        (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Climb:");
        (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Status:");

        /* Note that the following fields are exceptions to the
         * sizing rule.  The minimum window size does not include these
         * fields, if the window is too small, they get excluded.  This
         * may or may not change if/when the output for these fields is
         * fixed and/or people request their permanance.  They're only
         * there in the first place because I arbitrarily thought they
         * sounded interesting. ;^) */

        if (window_ysize >= MAX_GPS_DATAWIN_YSIZE) {
            (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                            "Long Err  (XDOP, EPX):");
            (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                            "Lat Err   (YDOP, EPY):");
            (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                            "Alt Err   (VDOP, EPV):");

            if (show_more_dops) {
                (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                                "2D Err    (HDOP, CEP):");
                (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                                "3D Err    (PDOP, SEP):");
                (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                                "Time Err  (TDOP):");
                (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                                "Geo Err   (GDOP):");
            }

            if (show_ecefs) {
                (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                                "ECEF X, VX:");
                (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                                "ECEF Y, VY:");
                (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                                "ECEF Z, VZ:");
            }

            (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                            "Speed Err (EPS):");
            (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                            "Head Err  (EPD):");
            /* it's actually esr that thought *these* were interesting */
            (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                            "Time offset:");
            (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
                            "Grid Square:");
        }

        (void)wborder(datawin, 0, 0, 0, 0, 0, 0, 0, 0);
        /* PRN is not unique for all GNSS systems.
         * Each GNSS (GPS, GALILEO, BeiDou, etc.) numbers their PRN from 1.
         * What we really have here is USI, Universal Sat ID
         * The USI for each GNSS satellite is unique, starting at 1.
         * Not all GPS receivers compute the USI the same way. YMMV
         *
         * Javad (GREIS) GPS receivers compute USI this way:
         * GPS is USI 1-37, GLONASS 38-70, GALILEO 71-119, SBAS 120-142,
         * QZSS 193-197, BeiDou 211-247
         *
         * Geostar GPS receivers compute USI this way:
         * GPS is USI 1 to 32, SBAS is 33 to 64, GLONASS is 65 to 96 */
        (void)mvwaddstr(satellites, 1, 1, "    PRN  Elev   Azim   SNR  Use   ");
        (void)wborder(satellites, 0, 0, 0, 0, 0, 0, 0, 0);
    }
}


static void resize(int sig UNUSED)
/* cope with terminal resize */
{
    if (!isendwin())
    {
        (void)endwin();
        windowsetup();
    }
}

#ifdef TRUENORTH
/* This gets called once for each new compass sentence. */
static void update_compass_panel(struct gps_data_t *gpsdata)
{
    char scr[128];
    int row = 1;
    /* Print time/date. */
    if (isfinite(gpsdata->fix.time) != 0) {
        (void)unix_to_iso8601(gpsdata->fix.time, scr, sizeof(scr));
    } else
        (void)strncpy(scr, "n/a", sizeof(scr));
    (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);

    /* Fill in the heading. */
    if (isfinite(gpsdata->fix.track) != 0) {
        (void)snprintf(scr, sizeof(scr), "%.1f degrees", gpsdata->fix.track);
    } else
        (void)strncpy(scr, "n/a", sizeof(scr));
    (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);

    /* Fill in the climb. */
    if (isfinite(gpsdata->fix.climb) != 0) {
        (void)snprintf(scr, sizeof(scr), "%.2f", gpsdata->fix.climb);
    } else
        (void)strncpy(scr, "n/a", sizeof(scr));
    (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);

    /* Fill in the speed. */
    if (isfinite(gpsdata->fix.speed) != 0)
        (void)snprintf(scr, sizeof(scr), "%.2f", gpsdata->fix.speed);
    else
        (void)strncpy(scr, "n/a", sizeof(scr));
    (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);

    /* Fill in the altitude. */
    if (isfinite(gpsdata->fix.altitude) != 0)
        (void)snprintf(scr, sizeof(scr), "%.3f", gpsdata->fix.altitude);
    else
        (void)strncpy(scr, "n/a", sizeof(scr));
    (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);

    /* When we need to fill in receiver type again, do it here. */
    (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);

    (void)wrefresh(datawin);

    if (raw_flag && !silent_flag) {
        /* Be quiet if the user requests silence. */
        (void)waddstr(messages, message);
        (void)wrefresh(messages);
    }
}
#endif /* TRUENORTH */

/* sort the skyviews
 * Used = Y first, then used = N
 * then sort by PRN
 */
static int sat_cmp(const void *p1, const void *p2)
{

   if ( ((struct satellite_t*)p2)->used - ((struct satellite_t*)p1)->used ) {
        return ((struct satellite_t*)p2)->used - ((struct satellite_t*)p1)->used;
   }
   return ((struct satellite_t*)p1)->PRN - ((struct satellite_t*)p2)->PRN;
}


static void update_gps_panel(struct gps_data_t *gpsdata, char *message)
/* This gets called once for each new GPS sentence. */
{
    int newstate;
    char scr[60];

    /* This is for the satellite status display.  Originally lifted from
     * xgps.c.  Note that the satellite list may be truncated based on
     * available screen size, or may only show satellites used for the
     * fix.  */
    (void)mvwprintw(satellites, 0, 19, "Seen %2d/Used %2d",
                    gpsdata->satellites_visible,
                    gpsdata->satellites_used);

    if (0 != (VERSION_SET &gpsdata->set)) {
        /* got version, check it */
        /* FIXME: expected API version not available ? */
        if (0 != strcmp(gpsdata->version.release, VERSION)) {
	    (void)fprintf(stderr, "cgps: WARNING gpsd release %s, API: %d.%d, "
                                  "expected %s ",
			  gpsdata->version.release,
			  gpsdata->version.proto_major,
			  gpsdata->version.proto_minor,
			  VERSION);
	    sleep(2);
        }
    }

    if (gpsdata->satellites_visible != 0) {
        int sat_no;
        int loop_end = (display_sats < gpsdata->satellites_visible) ? \
                display_sats : gpsdata->satellites_visible;

        qsort( gpsdata->skyview, gpsdata->satellites_visible,
                sizeof( struct satellite_t), sat_cmp);
        /* displayed all sats that fit, maybe all of them */
        for (sat_no = 0; sat_no < loop_end; sat_no++) {
            int column = 1;     /* column to write to */
            char *gnssid;

            if ( 0 == gpsdata->skyview[sat_no].svid) {
                gnssid = "  ";
            } else {
                switch (gpsdata->skyview[sat_no].gnssid) {
                default:
                    gnssid = "  ";
                    break;
                case 0:
                    gnssid = "GP";  /* GPS */
                    break;
                case 1:
                    gnssid = "SB";  /* GPS */
                    break;
                case 2:
                    gnssid = "GA";  /* GALILEO */
                    break;
                case 3:
                    gnssid = "BD";  /* BeiDou */
                    break;
                case 4:
                    gnssid = "IM";  /* IMES */
                    break;
                case 5:
                    gnssid = "QZ";  /* QZSS */
                    break;
                case 6:
                    gnssid = "GL";  /* GLONASS */
                    break;
                }
            }
            (void)mvwaddstr(satellites, sat_no + 2, column, gnssid);

            /* no GPS uses PRN 0, some use 255 for 'unknown'
             * u-blox uses PRN  1-255, NMEA 4.0 uses 1-437 */
            column += 3;
            (void)mvwaddstr(satellites, sat_no + 2, column,
                            int_to_str(gpsdata->skyview[sat_no].PRN,
                                       1, 438));
            /* u-blox uses -91 to signal something undocumented */
            column += 6;
            (void)mvwaddstr(satellites, sat_no + 2, column,
                            int_to_str(gpsdata->skyview[sat_no].elevation,
                                       -90, 90));
            column += 7;
            (void)mvwaddstr(satellites, sat_no + 2, column,
                            int_to_str(gpsdata->skyview[sat_no].azimuth,
                                       0, 359));
            column += 6;
            (void)mvwaddstr(satellites, sat_no + 2, column,
                            int_to_str((int)round(gpsdata->skyview[sat_no].ss),
                                       0, 254));
            column += 6;
            (void)mvwprintw(satellites, sat_no + 2, column, "  %c  ",
                            gpsdata->skyview[sat_no].used ? 'Y' : 'N');
        }

        /* Display More... ? */
        if (sat_no < gpsdata->satellites_visible) {
            /* Too many sats to show them all, tell the user. */
            (void)mvwprintw(satellites, sat_no + 2, 1, "%s", "More...");
        } else {
            /* Clear old data from the unused lines at bottom. */
            for ( ; sat_no < display_sats; sat_no++) {
                (void)mvwprintw(satellites, sat_no + 2, 1, "%-*s",
                                SATELLITES_WIDTH - 3, "");
            }
            /* remove More... */
            (void)mvwhline(satellites, sat_no + 2, 1, 0, 8);
        }
    } else {
        int sat_no = 0;
        /* no sats, clear screen */
        for ( ; sat_no < display_sats; sat_no++) {
            (void)mvwprintw(satellites, sat_no + 2, 1, "%-*s",
                            SATELLITES_WIDTH - 3, "");
        }
        /* remove More... */
        (void)mvwhline(satellites, sat_no + 2, 1, 0, 8);
    }
    /* turn off cursor */
    curs_set(0);

    /* Print time/date. */
    if (isfinite(gpsdata->fix.time) != 0) {
        (void)unix_to_iso8601(gpsdata->fix.time, scr, sizeof(scr));
    } else
        (void)strncpy(scr, "  n/a", sizeof(scr));
    (void)mvwprintw(datawin, 1, DATAWIN_VALUE_OFFSET, "%-*s", 26, scr);


    /* Fill in the latitude. */
    if (gpsdata->fix.mode >= MODE_2D) {
	deg_to_str2(deg_type, gpsdata->fix.latitude,
		    scr, sizeof(scr), " N", " S");
    } else
        (void)strncpy(scr, "n/a", sizeof(scr));
    (void)mvwprintw(datawin, 2, DATAWIN_VALUE_OFFSET, "  %-*s", 25, scr);

    /* Fill in the longitude. */
    if (gpsdata->fix.mode >= MODE_2D) {
	deg_to_str2(deg_type, gpsdata->fix.longitude,
	            scr, sizeof(scr), " E", " W");
    } else
        (void)strncpy(scr, "n/a", sizeof(scr));
    (void)mvwprintw(datawin, 3, DATAWIN_VALUE_OFFSET, "  %-*s", 25, scr);

    /* Fill in the altitude. */
    if (gpsdata->fix.mode >= MODE_3D && isfinite(gpsdata->fix.altitude) != 0)
        (void)snprintf(scr, sizeof(scr), "%9.3f %s",
                       gpsdata->fix.altitude * altfactor, altunits);
    else
        (void)strncpy(scr, "  n/a", sizeof(scr));
    (void)mvwprintw(datawin, 4, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);

    /* Fill in the speed. */
    if (isfinite(gpsdata->fix.speed) != 0)
        (void)snprintf(scr, sizeof(scr), "%8.2f %s",
                       gpsdata->fix.speed * speedfactor, speedunits);
    else
        (void)strncpy(scr, "  n/a", sizeof(scr));
    (void)mvwprintw(datawin, 5, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);

    /* Fill in the heading. */
    if (gpsdata->fix.mode >= MODE_2D && isfinite(gpsdata->fix.track) != 0) {
        double magheading = true2magnetic(gpsdata->fix.latitude,
                                         gpsdata->fix.longitude,
                                          gpsdata->fix.track);
        if (!magnetic_flag || isfinite(magheading) == 0) {
            (void)snprintf(scr, sizeof(scr), "%5.1f deg (true)",
                           gpsdata->fix.track);
        } else {
            (void)snprintf(scr, sizeof(scr), "%5.1f deg (mag) ",
                magheading);
        }
    } else
        (void)strncpy(scr, "n/a", sizeof(scr));
    (void)mvwprintw(datawin, 6, DATAWIN_VALUE_OFFSET, "  %-*s", 25, scr);

    /* Fill in the rate of climb. */
    if (isfinite(gpsdata->fix.climb) != 0)
        (void)snprintf(scr, sizeof(scr), "%8.2f %s/min",
                       gpsdata->fix.climb * altfactor * 60, altunits);
    else
        (void)strncpy(scr, "  n/a", sizeof(scr));
    (void)mvwprintw(datawin, 7, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);

    /* Fill in the GPS status and the time since the last state
     * change. */
    if (gpsdata->online == 0) {
        newstate = 0;
        (void)strncpy(scr, "OFFLINE", sizeof(scr));
    } else {
        const char *fmt;
        const char *mod = "";

        newstate = gpsdata->fix.mode;
	switch (gpsdata->status) {
	case STATUS_DGPS_FIX:
            mod = "DIFF ";
	    break;
	case STATUS_RTK_FIX:
            mod = "RTK ";
	    break;
	case STATUS_RTK_FLT:
            mod = "RTK ";
	    break;
	case STATUS_DR:
            mod = "DR ";
	    break;
	case STATUS_GNSSDR:
            mod = "+DR ";
	    break;
	default:
	    /* ignore: */
	    mod = "";
	    break;
	}
        switch (gpsdata->fix.mode) {
        case MODE_2D:
            fmt = "2D %sFIX (%d secs)";
            break;
        case MODE_3D:
	    if (STATUS_TIME == gpsdata->status) {
		fmt = "%sSURVEYED (%d secs)";
	    } else {
		fmt = "3D %sFIX (%d secs)";
	    }
            break;
        default:
            fmt = "NO %sFIX (%d secs)";
            break;
        }
	(void)snprintf(scr, sizeof(scr), fmt,  mod,
	               (int)(time(NULL) - status_timer));
    }
    (void)mvwprintw(datawin, 8, DATAWIN_VALUE_OFFSET + 1, "%-*s", 26, scr);

    /* Note that the following fields are exceptions to the
     * sizing rule.  The minimum window size does not include these
     * fields, if the window is too small, they get excluded.  This
     * may or may not change if/when the output for these fields is
     * fixed and/or people request their permanence.  They're only
     * there in the first place because I arbitrarily thought they
     * sounded interesting. ;^) */

    if (window_ysize >= (MIN_GPS_DATAWIN_YSIZE + 5)) {
        int row = 9;
        char *ep_str;
        char *dop_str;
        char *str;

        /* Fill in the estimated latitude position error, XDOP. */
        ep_str = ep_to_str(gpsdata->fix.epx, altfactor, altunits);
        dop_str = dop_to_str(gpsdata->dop.xdop);
        (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%s, %-*s",
                        dop_str, 11, ep_str);

        /* Fill in the estimated longitude position error, YDOP. */
        ep_str = ep_to_str(gpsdata->fix.epy, altfactor, altunits);
        dop_str = dop_to_str(gpsdata->dop.ydop);
        (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%s, %-*s",
                        dop_str, 11, ep_str);

        /* Fill in the estimated velocity error, VDOP. */
        ep_str = ep_to_str(gpsdata->fix.epv, altfactor, altunits);
        dop_str = dop_to_str(gpsdata->dop.vdop);
        (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%s, %-*s",
                        dop_str, 11, ep_str);

        /* extra tall screen, show more DOPs */
        if (show_more_dops) {

            /* Fill in the estimated horizontal (2D) error, HDOP */
            ep_str = ep_to_str(gpsdata->fix.eph, altfactor, altunits);
            dop_str = dop_to_str(gpsdata->dop.hdop);
            (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8,
                            "%s, %-*s", dop_str, 11, ep_str);

            /* (spherical) position error, 3D error, PDOP */
            ep_str = ep_to_str(gpsdata->fix.sep, altfactor, altunits);
            dop_str = dop_to_str(gpsdata->dop.pdop);
            (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8,
                            "%s, %-*s", dop_str, 11, ep_str);

            /* time dilution of precision, TDOP */
            /* FIXME: time ep? */
            dop_str = dop_to_str(gpsdata->dop.tdop);
            (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s",
                            18, dop_str);

            /* geometric dilution of precision, GDOP */
            /* FIXME: gdop ep? */
            dop_str = dop_to_str(gpsdata->dop.gdop);
            (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s",
                            18, dop_str);

        }

        /* extra large screen, show ECEF */
        if (show_ecefs) {
            char *estr;

            /* Fill in the ECEF's. */
            estr = ecef_to_str(gpsdata->fix.ecef.x, gpsdata->fix.ecef.vx,
                               1, " m");
            (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET - 3, "%-*s",
                            27, estr);

            estr = ecef_to_str(gpsdata->fix.ecef.y, gpsdata->fix.ecef.vy,
                               1, " m");
            (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET - 3, "%-*s",
                            27, estr);

            estr = ecef_to_str(gpsdata->fix.ecef.z, gpsdata->fix.ecef.vz,
                               1, " m");
            (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET - 3, "%-*s",
                            27, estr);
        }


        /* Fill in the estimated speed error, EPS. */
        ep_str = ep_to_str(gpsdata->fix.eps, speedfactor, speedunits);
        (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8,
                        "%-*s", 12, ep_str);

        /* Fill in the estimated track error, EPD. */
        ep_str = ep_to_str(gpsdata->fix.epd, speedfactor, "deg");
        (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s", 18,
                        ep_str);

        /* Fill in the time offset, milliseconds. */
        if (isfinite(gpsdata->fix.time) != 0)
            (void)snprintf(scr, sizeof(scr), "%6.3f sec",
                           (double)(timestamp()-gpsdata->fix.time));
        else
            (void)strncpy(scr, " n/a", sizeof(scr));
        (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s", 18,
                        scr);
        /* Fill in the grid square (esr thought *this* one was interesting). */
        if ((isfinite(gpsdata->fix.longitude) != 0 &&
             isfinite(gpsdata->fix.latitude) != 0))
            str = maidenhead(gpsdata->fix.latitude,gpsdata->fix.longitude);
        else
            str = "n/a";
        (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 9, "%-*s",
                        18, str);

    }

    /* Be quiet if the user requests silence. */
    if (!silent_flag && raw_flag) {
        if (NULL != message) {
            size_t message_len = strlen(message);
            if (0 < message_len) {
                if ( '\r' == message[message_len - 1]) {
                    /* remove any trailing \r */
                    message[message_len - 1] = '\0';
                }
                (void)wprintw(messages, "\n%s", message);
                (void)wrefresh(messages);
            }
        }
    }

    /* Reset the status_timer if the state has changed. */
    if (newstate != state) {
        status_timer = time(NULL);
        state = newstate;
    }

    (void)wrefresh(datawin);
    (void)wrefresh(satellites);
}

static void usage(char *prog)
{
    (void)fprintf(stderr,
        "Usage: %s [-h] [-l {d|m|s}] [-m] [-s] [-V] "
        "[server[:port:[device]]]\n\n"
        "  -D debug-level  Set debug level\n"
        "  -h              Show this help, then exit\n"
        "  -l {d|m|s}      Select lat/lon format\n"
        "                      d = DD.ddddddd\n"
        "                      m = DD MM.mmmmmm'\n"
        "                      s = DD MM' SS.sssss\"\n"
        "  -m              Display heading as the estimated magnetic heading\n"
        "                  Valid or USA (Lower 48 + AK) and Western Europe.\n"
        "  -s              Be silent (don't print raw gpsd data)\n"
        "  -V              Show version, then exit\n",
        prog);

    exit(EXIT_FAILURE);
}

/*
 * No protocol dependencies above this line
 */

int main(int argc, char *argv[])
{
    int option;
    unsigned int flags = WATCH_ENABLE;
    int wait_clicks = 0;  /* cycles to wait before gpsd timeout */
    /* buffer to hold one JSON message */
    char message[GPS_JSON_RESPONSE_MAX];

    switch (gpsd_units()) {
    case imperial:
        altfactor = METERS_TO_FEET;
        altunits = "ft";
        speedfactor = MPS_TO_MPH;
        speedunits = "mph";
        break;
    case nautical:
        altfactor = METERS_TO_FEET;
        altunits = "ft";
        speedfactor = MPS_TO_KNOTS;
        speedunits = "kts";
        break;
    case metric:
        altfactor = 1;
        altunits = "m";
        speedfactor = MPS_TO_KPH;
        speedunits = "kph";
        break;
    default:
        /* leave the default alone */
        break;
    }

    /* Process the options.  Print help if requested. */
    while ((option = getopt(argc, argv, "D:hl:msu:V")) != -1) {
        switch (option) {
        case 'D':
            debug = atoi(optarg);
            gps_enable_debug(debug, stderr);
            break;
        case 'm':
            magnetic_flag = true;
            break;
        case 's':
            silent_flag = true;
            break;
        case 'u':
            switch (optarg[0]) {
            case 'i':
                altfactor = METERS_TO_FEET;
                altunits = "ft";
                speedfactor = MPS_TO_MPH;
                speedunits = "mph";
                continue;
            case 'n':
                altfactor = METERS_TO_FEET;
                altunits = "ft";
                speedfactor = MPS_TO_KNOTS;
                speedunits = "knots";
                continue;
            case 'm':
                altfactor = 1;
                altunits = "m";
                speedfactor = MPS_TO_KPH;
                speedunits = "kph";
                continue;
            default:
                (void)fprintf(stderr, "Unknown -u argument: %s\n", optarg);
            }
            break;
        case 'V':
            (void)fprintf(stderr, "%s: %s (revision %s)\n",
                          argv[0], VERSION, REVISION);
            exit(EXIT_SUCCESS);
        case 'l':
            switch (optarg[0]) {
            case 'd':
                deg_type = deg_dd;
                continue;
            case 'm':
                deg_type = deg_ddmm;
                continue;
            case 's':
                deg_type = deg_ddmmss;
                continue;
            default:
                (void)fprintf(stderr, "Unknown -l argument: %s\n", optarg);
            }
            break;
        case 'h':
        default:
            usage(argv[0]);
            break;
        }
    }

    /* Grok the server, port, and device. */
    if (optind < argc) {
        gpsd_source_spec(argv[optind], &source);
    } else
        gpsd_source_spec(NULL, &source);

    /* Open the stream to gpsd. */
    if (gps_open(source.server, source.port, &gpsdata) != 0) {
        (void)fprintf(stderr,
                      "cgps: no gpsd running or network error: %d, %s\n",
                      errno, gps_errstr(errno));
        exit(EXIT_FAILURE);
    }

    /* note: we're assuming BSD-style reliable signals here */
    (void)signal(SIGINT, die);
    (void)signal(SIGHUP, die);
    (void)signal(SIGWINCH, resize);

    windowsetup();

    status_timer = time(NULL);

    if (source.device != NULL)
        flags |= WATCH_DEVICE;
    (void)gps_stream(&gpsdata, flags, source.device);

    /* heart of the client */
    for (;;) {
        int c;

        /* wait 1/2 second for gpsd */
        if (!gps_waiting(&gpsdata, 500000)) {
            /* 240 tries at 0.5 seconds a try is a 2 minute timeout */
            if ( 240 < wait_clicks++ )
                die(GPS_TIMEOUT);
        } else {
            wait_clicks = 0;
            errno = 0;
            *message = '\0';
            if (gps_read(&gpsdata, message, sizeof(message)) == -1) {
                (void)fprintf(stderr, "cgps: socket error 4\n");
                die(errno == 0 ? GPS_GONE : GPS_ERROR);
            } else {
                /* Here's where updates go now that things are established. */
#ifdef TRUENORTH
                if (compass_flag)
                    update_compass_panel(&gpsdata);
                else
#endif /* TRUENORTH */
                    update_gps_panel(&gpsdata, message);
            }
        }

        /* Check for user input. */
        c = wgetch(datawin);

        switch (c) {
            /* Quit */
        case 'q':
            die(CGPS_QUIT);
            break;

            /* Toggle spewage of raw gpsd data. */
        case 's':
            silent_flag = !silent_flag;
            break;

            /* Clear the spewage area. */
        case 'c':
            (void)werase(messages);
            break;

        default:
            break;
        }
    }
}