summaryrefslogtreecommitdiff
path: root/xgps
blob: d6f6c686b392024389cb41c211ab985b21b7119e (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
#!/usr/bin/env python
# -*- coding: UTF-8
'''
xgps -- test client for gpsd

usage: xgps [-D level] [-hV?] [-l degmfmt] [-u units] [-r rotation]
            [server[:port[:device]]]
'''

# This file is Copyright (c) 2010 by the GPSD project
# BSD terms apply: see the file COPYING in the distribution root for details.
#
# This code runs compatibly under Python 2 and 3.x for x >= 2.
# Preserve this property!
from __future__ import absolute_import, print_function, division

import math
import socket
import sys
import time

import gps
import gps.clienthelpers

import cairo

# Gtk3 imports.  Gtk3 requires the require_version(), which then causes
# pylint to complain about the subsequent "non-top" imports.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GObject  # pylint: disable=wrong-import-position
from gi.repository import Gtk  # pylint: disable=wrong-import-position
from gi.repository import Gdk  # pylint: disable=wrong-import-position

gui_about = '''\
This is xgps, a test client for the gpsd daemon.

By Eric S. Raymond for the GPSD project, December 2009
'''

# Use our own MAXCHANNELS value, due to the tradeoff between max sats and
# the window size.  Ideally, this should be dynamic.
MAXCHANNELS = 28

# how to sort the Satellite List
# some of ("PRN","el","az","ss","used") with optional '-' to reverse sort
# by default, used at the top, then sort PRN
SKY_VIEW_SORT_FIELDS = ('-used', 'PRN')

# Each GNSS constellation reuses the same PRNs.  To differentiate they are
# all mushed into the PRN by Universal Satellite Index (USI)

# here is the mapping.
# USI           constellation
# ----------- | --------------
# 0             Unused. Ignore satellites with this USI
# [1...37]      GPS PRNs [1...37]
# [38...69]     GLONASS FCNs [-7...24]
# 70            GLONASS satellite with unknown FCN
# [71...119]    GALILEO PRNs [1...49]
# [120...142]   SBAS PRNs [120...142]
# [143...192]   Reserved
# [193...197]   QZSS PRNs [193...197]
# [198...210]   Reserved
# [211...247]   BeiDou (COMPASS) PRNs [1...37]
# [248...254]   Reserved
# 255           Unused.  Ignore satellites with this USI

# FIXME: should be in gps.py, will move when tested
def gps_type( prn ):
     # return a string of the GPS constellation this PRN is in
     if prn == 0:
         return 'Unused'
     if prn < 38:
         return 'GPS'
     if prn < 71:
         return 'GLONASS'
     if prn < 120:
         return 'GALILEO'
     if prn < 143:
         return 'SBAS'
     if prn < 193:
         return 'Reserved'
     if prn < 198:
         return 'QZSS'
     if prn < 211:
         return 'Reserved'
     if prn < 248:
         return 'BeiDou'
     if prn < 255:
         return 'Reserved'
     # else 255
     return 'Unused'

class unit_adjustments(object):
    "Encapsulate adjustments for unit systems."

    def __init__(self, units=None):
        self.altfactor = gps.METERS_TO_FEET
        self.altunits = "ft"
        self.speedfactor = gps.MPS_TO_MPH
        self.speedunits = "mph"
        if units is None:
            units = gps.clienthelpers.gpsd_units()
        if units in (gps.clienthelpers.unspecified, gps.clienthelpers.imperial,
                     "imperial", "i"):
            pass
        elif units in (gps.clienthelpers.nautical, "nautical", "n"):
            self.altfactor = gps.METERS_TO_FEET
            self.altunits = "ft"
            self.speedfactor = gps.MPS_TO_KNOTS
            self.speedunits = "knots"
        elif units in (gps.clienthelpers.metric, "metric", "m"):
            self.altfactor = 1.0
            self.altunits = "m"
            self.speedfactor = gps.MPS_TO_KPH
            self.speedunits = "kph"
        else:
            raise ValueError  # Should never happen


def fit_to_grid(x, y, line_width):
    "Adjust coordinates to produce sharp lines."
    if line_width % 1.0 != 0:
        # Can't have sharp lines for non-integral line widths.
        return float(x), float(y)  # Be consistent about returning floats
    if line_width % 2 == 0:
        # Round to a pixel corner.
        return round(x), round(y)
    else:
        # Round to a pixel center.
        return int(x) + 0.5, int(y) + 0.5


def fit_circle_to_grid(x, y, radius, line_width):
    """Adjust circle coordinates and radius to produce sharp horizontal
    and vertical tangents."""
    r = radius
    x1, y1 = fit_to_grid(x - r, y - r, line_width)
    x2, y2 = fit_to_grid(x + r, y + r, line_width)
    x, y = (x1 + x2) / 2, (y1 + y2) / 2
    r = (x2 - x1 + y2 - y1) / 4
    return x, y, r


class SkyView(Gtk.DrawingArea):
    "Satellite skyview, encapsulates pygtk's draw-on-expose behavior."
    # See <http://faq.pygtk.org/index.py?req=show&file=faq18.008.htp>
    HORIZON_PAD = 40    # How much whitespace to leave around horizon
    SAT_RADIUS = 5      # Diameter of satellite circle

    def __init__(self, rotate=0.0):
        Gtk.DrawingArea.__init__(self)
        # GObject.GObject.__init__(self)
        self.set_size_request(400, 400)
        self.cr = None   # New cairo context for each expose event
        self.step_of_grid = 45  # default step of polar grid
        self.connect('size-allocate', self.on_size_allocate)
        self.connect('draw', self.on_draw)
        self.satellites = []
        self.center_x = self.center_y = self.radius = None
        self.rotate = rotate

    def on_size_allocate(self, _unused, allocation):
        width = allocation.width
        height = allocation.height
        x = width // 2
        y = height // 2
        r = (min(width, height) - SkyView.HORIZON_PAD) // 2
        x, y, r = fit_circle_to_grid(x, y, r, 1)
        self.center_x = x
        self.center_y = y
        self.radius = r

    def set_color(self, spec):
        "Set foreground color for drawing."
        gdkcolor = Gdk.color_parse(spec)
        r = gdkcolor.red / 65535.0
        g = gdkcolor.green / 65535.0
        b = gdkcolor.blue / 65535.0
        self.cr.set_source_rgb(r, g, b)

    def draw_circle(self, x, y, radius, filled=False):
        "Draw a circle centered on the specified midpoint."
        lw = self.cr.get_line_width()
        r = int(2 * radius + 0.5) // 2

        x, y, r = fit_circle_to_grid(x, y, radius, lw)

        self.cr.arc(x, y, r, 0, math.pi * 2.0)
        self.cr.close_path()

        if filled:
            self.cr.fill()
        else:
            self.cr.stroke()

    def draw_line(self, x1, y1, x2, y2):
        "Draw a line between specified points."
        lw = self.cr.get_line_width()
        x1, y1 = fit_to_grid(x1, y1, lw)
        x2, y2 = fit_to_grid(x2, y2, lw)

        self.cr.move_to(x1, y1)
        self.cr.line_to(x2, y2)

        self.cr.stroke()

    def draw_square(self, x, y, radius, filled=False):
        "Draw a square centered on the specified midpoint."
        lw = self.cr.get_line_width()
        x1, y1 = fit_to_grid(x - radius, y - radius, lw)
        x2, y2 = fit_to_grid(x + radius, y + radius, lw)

        self.cr.rectangle(x1, y1, x2 - x1, y2 - y1)

        if filled:
            self.cr.fill()
        else:
            self.cr.stroke()

    def draw_string(self, x, y, text, centered=True):
        "Draw a text on the skyview."
        self.cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                                 cairo.FONT_WEIGHT_BOLD)
        self.cr.set_font_size(10)

        if centered:
            extents = self.cr.text_extents(text)
            # width / 2 + x_bearing
            x -= extents[2] / 2 + extents[0]
            # height / 2 + y_bearing
            y -= extents[3] / 2 + extents[1]

        self.cr.move_to(x, y)
        self.cr.show_text(text)
        self.cr.new_path()

    def draw_triangle(self, x, y, radius, filled=False, flip=False):
        "Draw a triangle centered on the specified midpoint."
        lw = self.cr.get_line_width()
        if flip:
            ytop = y + radius
            ybot = y - radius
        else:
            ytop = y - radius
            ybot = y + radius

        x1, y1 = fit_to_grid(x, ytop, lw)
        x2, y2 = fit_to_grid(x + radius, ybot, lw)
        x3, y3 = fit_to_grid(x - radius, ybot, lw)

        self.cr.move_to(x1, y1)
        self.cr.line_to(x2, y2)
        self.cr.line_to(x3, y3)
        self.cr.close_path()

        self.cr.stroke()

        if filled:
            self.cr.fill()
        else:
            self.cr.stroke()

    def pol2cart(self, az, el):
        "Polar to Cartesian coordinates within the horizon circle."
        az = (az - self.rotate) % 360.0
        az *= (math.pi / 180)  # Degrees to radians
        # Exact spherical projection would be like this:
        # el = sin((90.0 - el) * DEG_2_RAD);
        el = ((90.0 - el) / 90.0)
        xout = self.center_x + math.sin(az) * el * self.radius
        yout = self.center_y - math.cos(az) * el * self.radius
        return (xout, yout)

    def on_draw(self, widget, _unused):
        self.cr = widget.get_window().cairo_create()

        self.cr.set_line_width(1)

        self.cr.set_source_rgb(0, 0, 0)
        self.cr.paint()

        self.cr.set_source_rgb(1, 1, 1)
        # The zenith marker
        self.draw_circle(self.center_x, self.center_y, 6, filled=False)

        # The horizon circle
        if self.step_of_grid == 45:
            # The circle corresponding to 45 degrees elevation.
            # There are two ways we could plot this.  Projecting the sphere
            # on the display plane, the circle would have a diameter of
            # sin(45) ~ 0.7.  But the naive linear mapping, just splitting
            # the horizon diameter in half, seems to work better visually.
            self.draw_circle(self.center_x, self.center_y, self.radius / 2,
                             filled=False)
        elif self.step_of_grid == 30:
            self.draw_circle(self.center_x, self.center_y, self.radius * 2 / 3,
                             filled=False)
            self.draw_circle(self.center_x, self.center_y, self.radius / 3,
                             filled=False)
        self.draw_circle(self.center_x, self.center_y, self.radius,
                         filled=False)

        (x1, y1) = self.pol2cart(0, 0)
        (x2, y2) = self.pol2cart(180, 0)
        self.draw_line(x1, y1, x2, y2)

        (x1, y1) = self.pol2cart(90, 0)
        (x2, y2) = self.pol2cart(270, 0)
        self.draw_line(x1, y1, x2, y2)

        # The compass-point letters
        (x, y) = self.pol2cart(0, -5)
        self.draw_string(x, y, "N")
        (x, y) = self.pol2cart(90, -5)
        self.draw_string(x, y, "E")
        (x, y) = self.pol2cart(180, -5)
        self.draw_string(x, y, "S")
        (x, y) = self.pol2cart(270, -5)
        self.draw_string(x, y, "W")

        # The satellites
        self.cr.set_line_width(2)
        for sat in self.satellites:
            if sat.az == 0 and sat.el == 0:
                continue  # Skip satellites with unknown position
            (x, y) = self.pol2cart(sat.az, sat.el)
            # colorize by signal strength
            if sat.ss < 10:
                self.set_color("Gray")
            elif sat.ss < 30:
                self.set_color("Red")
            elif sat.ss < 35:
                self.set_color("Yellow")
            elif sat.ss < 40:
                self.set_color("Green3")
            else:
                self.set_color("Green1")

            # shape by constellation
            constellation = gps_type(sat.PRN)
            if constellation == 'GPS':
                self.draw_circle(x, y, SkyView.SAT_RADIUS, sat.used)
            elif constellation == 'SBAS':
                self.draw_square(x, y, SkyView.SAT_RADIUS, sat.used)
            elif constellation == 'GALILEO':
                self.draw_triangle(x, y, SkyView.SAT_RADIUS, sat.used, False)
            else:
                # QZSS, GLONASS, BeiDou, Reserved or Unused
                self.draw_triangle(x, y, SkyView.SAT_RADIUS, sat.used, True)

            self.cr.set_source_rgb(1, 1, 1)
            self.draw_string(x + SkyView.SAT_RADIUS,
                             y + (SkyView.SAT_RADIUS * 2), str(sat.PRN),
                             centered=False)

        self.cr = None

    def redraw(self, satellites):
        "Redraw the skyview."
        self.satellites = satellites
        self.queue_draw()


class NoiseView(object):
    "Encapsulate view object for watching noise statistics."
    COLUMNS = 2
    ROWS = 4
    noisefields = (
        # First column
        ("Time", "time"),
        ("Latitude", "lat"),
        ("Longitude", "lon"),
        ("Altitude", "alt"),
        # Second column
        ("RMS", "rms"),
        ("Major", "major"),
        ("Minor", "minor"),
        ("Orient", "orient"),
    )

    def __init__(self):
        self.widget = Gtk.Table(NoiseView.COLUMNS, NoiseView.ROWS, False)
        self.noisewidgets = []
        for i in range(len(NoiseView.noisefields)):
            colbase = (i // NoiseView.ROWS) * 2
            label = Gtk.Label(label=NoiseView.noisefields[i][0] + ": ")
            # Wacky way to force right alignment
            label.set_alignment(xalign=1, yalign=0.5)
            self.widget.attach(
                label, colbase, colbase + 1,
                i % NoiseView.ROWS, i % NoiseView.ROWS + 1)
            entry = Gtk.Entry()
            # The right size for the ISO8601 timestamp
            entry.set_width_chars(20)
            entry.set_text("n/a")
            self.widget.attach(
                entry, colbase + 1, colbase + 2,
                i % NoiseView.ROWS, i % NoiseView.ROWS + 1)
            self.noisewidgets.append((NoiseView.noisefields[i][1], entry))

    def update(self, noise):
        "Update the GPGST data fields."
        for (attrname, widget) in self.noisewidgets:
            if hasattr(noise, attrname):
                widget.set_text(str(getattr(noise, attrname)))
            else:
                widget.set_text("n/a")


class MaidenheadView(object):
    "Encapsulate view object for watching Maidenhead grid location."

    def __init__(self):
        self.widget = Gtk.Entry()
        self.widget.set_editable(False)

    def update(self, tpv):
        if ((tpv.mode >= gps.MODE_2D
             and hasattr(tpv, "lat")
             and hasattr(tpv, "lon"))):
            self.widget.set_text(gps.clienthelpers.maidenhead(tpv.lat,
                                                              tpv.lon))
        else:
            return self.widget.set_text("n/a")


class AISView(object):
    "Encapsulate store and view objects for watching AIS data."
    AIS_ENTRIES = 10
    DWELLTIME = 360

    def __init__(self, deg_type):
        "Initialize the store and view."
        self.deg_type = deg_type
        self.name_to_mmsi = {}
        self.named = {}
        self.store = Gtk.ListStore(str, str, str, str, str, str)
        self.widget = Gtk.ScrolledWindow()
        self.widget.set_policy(Gtk.PolicyType.AUTOMATIC,
                               Gtk.PolicyType.AUTOMATIC)
        self.view = Gtk.TreeView(model=self.store)
        self.widget.set_size_request(-1, 300)
        self.widget.add_with_viewport(self.view)

        for (i, label) in enumerate(('#', 'Name:', 'Callsign:',
                                     'Destination:', "Lat/Lon:",
                                     "Information")):
            column = Gtk.TreeViewColumn(label)
            renderer = Gtk.CellRendererText()
            column.pack_start(renderer, expand=True)
            column.add_attribute(renderer, 'text', i)
            self.view.append_column(column)

    def enter(self, ais, name):
        "Add a named object (ship or station) to the store."
        if ais.mmsi in self.named:
            return False
        else:
            ais.entry_time = time.time()
            self.named[ais.mmsi] = ais
            self.name_to_mmsi[name] = ais.mmsi
            # Garbage-collect old entries
            try:
                for i in range(len(self.store)):
                    here = self.store.get_iter(i)
                    name = self.store.get_value(here, 1)
                    mmsi = self.name_to_mmsi[name]
                    if ((self.named[mmsi].entry_time
                         < time.time() - AISView.DWELLTIME)):
                        del self.named[mmsi]
                        if name in self.name_to_mmsi:
                            del self.name_to_mmsi[name]
                        self.store.remove(here)
            except (ValueError, KeyError):  # Invalid TreeIters throw these
                pass
            return True

    def latlon(self, lat, lon):
        "Latitude/longitude display in nice format."
        if lat < 0:
            latsuff = "S"
        elif lat > 0:
            latsuff = "N"
        else:
            latsuff = ""
        lat = abs(lat)
        lat = gps.clienthelpers.deg_to_str(self.deg_type, lat)
        if lon < 0:
            lonsuff = "W"
        elif lon > 0:
            lonsuff = "E"
        else:
            lonsuff = ""
        lon = abs(lon)
        lon = gps.clienthelpers.deg_to_str(gps.clienthelpers.deg_ddmmss, lon)
        return lat + latsuff + "/" + lon + lonsuff

    def update(self, ais):
        "Update the AIS data fields."
        if ais.type in (1, 2, 3, 18):
            if ais.mmsi in self.named:
                for i in range(len(self.store)):
                    here = self.store.get_iter(i)
                    name = self.store.get_value(here, 1)
                    if name in self.name_to_mmsi:
                        mmsi = self.name_to_mmsi[name]
                        if mmsi == ais.mmsi:
                            latlon = self.latlon(ais.lat, ais.lon)
                            self.store.set_value(here, 4, latlon)
        elif ais.type == 4:
            if self.enter(ais, ais.mmsi):
                where = self.latlon(ais.lat, ais.lon)
                self.store.prepend(
                    (ais.type, ais.mmsi, "(shore)", ais.timestamp, where,
                     ais.epfd_text))
        elif ais.type == 5:
            if self.enter(ais, ais.shipname):
                self.store.prepend(
                    (ais.type, ais.shipname, ais.callsign, ais.destination,
                     "", ais.shiptype))
        elif ais.type == 12:
            sender = ais.mmsi
            if sender in self.named:
                sender = self.named[sender].shipname
            recipient = ais.dest_mmsi
            if ((recipient in self.named
                 and hasattr(self.named[recipient], "shipname"))):
                recipient = self.named[recipient].shipname
            self.store.prepend(
                (ais.type, sender, "", recipient, "", ais.text))
        elif ais.type == 14:
            sender = ais.mmsi
            if sender in self.named:
                sender = self.named[sender].shipname
            self.store.prepend(
                (ais.type, sender, "", "(broadcast)", "", ais.text))
        elif ais.type in (19, 24):
            if self.enter(ais, ais.shipname):
                self.store.prepend(
                    (ais.type, ais.shipname, "(class B)", "", "",
                     ais.shiptype_text))
        elif ais.type == 21:
            if self.enter(ais, ais.name):
                where = self.latlon(ais.lat, ais.lon)
                self.store.prepend(
                    (ais.type, ais.name, "(%s navaid)" % ais.epfd_text,
                     "", where, ais.aid_type_text))


class Base(object):
    COLUMNS = 3
    ROWS = 7
    gpsfields = (
        # First column
        ("Time", lambda s, r: s.update_time(r)),
        ("Latitude", lambda s, r: s.update_latitude(r)),
        ("Longitude", lambda s, r: s.update_longitude(r)),
        ("Altitude", lambda s, r: s.update_altitude(r)),
        ("Speed", lambda s, r: s.update_speed(r)),
        ("Climb", lambda s, r: s.update_climb(r)),
        ("Track", lambda s, r: s.update_track(r)),
        # Second column
        ("Status", lambda s, r: s.update_status(r)),
        ("EPX", lambda s, r: s.update_err(r, "epx")),
        ("EPY", lambda s, r: s.update_err(r, "epy")),
        ("EPV", lambda s, r: s.update_err(r, "epv")),
        ("EPS", lambda s, r: s.update_err_speed(r, "eps")),
        ("EPC", lambda s, r: s.update_err_speed(r, "epc")),
        ("EPD", lambda s, r: s.update_err_degrees(r, "epd")),
    )

    def __init__(self, deg_type, rotate=0.0, target=""):
        self.deg_type = deg_type
        self.rotate = rotate
        self.conversions = unit_adjustments()
        self.saved_mode = -1
        self.ais_latch = False
        self.noise_latch = False

        self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        if not self.window.get_display():
            raise Exception("Can't open display")
        if len(target):
            target = " " + target
        self.window.set_title("xgps" + target)
        self.window.connect("delete-event", self.delete_event)
        self.window.set_resizable(False)

        vbox = Gtk.VBox(False, 0)
        self.window.add(vbox)

        self.window.connect("destroy", lambda _unused: Gtk.main_quit())

        self.uimanager = Gtk.UIManager()
        self.accelgroup = self.uimanager.get_accel_group()
        self.window.add_accel_group(self.accelgroup)
        self.actiongroup = Gtk.ActionGroup('xgps')
        self.actiongroup.add_actions(
            [('Quit', Gtk.STOCK_QUIT, '_Quit', None,
              'Quit the Program', lambda _unused: Gtk.main_quit()),
             ('File', None, '_File'),
             ('View', None, '_View'),
             ('Units', None, '_Units'),
             ('Step of grid', None, '_Step of grid')])
        self.actiongroup.add_toggle_actions(
            [('Skyview', None, '_Skyview', '<Control>s',
              'Enable Skyview', self.view_toggle),
             ('Responses', None, '_Responses', '<Control>r',
              'Enable Response Reports', self.view_toggle),
             ('GPS', None, '_GPS Data', '<Control>g',
              'Enable GPS Data', self.view_toggle),
             ('Noise', None, '_Noise Statistics', '<Control>n',
              'Enable Noise Statistics', self.view_toggle),
             ('Maidenhead', None, '_Maidenhead', '<Control>m',
              'Enable Maidenhead locator', self.view_toggle),
             ('AIS', None, '_AIS Data', '<Control>a',
              'Enable AIS Data', self.view_toggle),
             ])
        self.actiongroup.add_radio_actions(
            [('Imperial', None, '_Imperial', '<Control>i',
              'Imperial units', 0),
             ('Nautical', None, '_Nautical', '<Control>n',
              'Nautical units', 1),
             ('Metric', None, '_Metric', '<Control>m',
              'Metric Units', 2),
             ], 0, lambda a, _unused: self.set_units(
                 ['i', 'n', 'm'][a.get_current_value()]))
        self.actiongroup.add_radio_actions(
            [('30deg', None, '30°', None, '30°', 30),
             ('45deg', None, '45°', None, '45°', 45),
             ('Off', None, 'Off', None, 'Off', 0),
             ], 45, lambda a, _unused: self.set_step_of_grid(
                 a.get_current_value()))
        self.uimanager.insert_action_group(self.actiongroup, 0)
        self.uimanager.add_ui_from_string('''
<ui>
    <menubar name="MenuBar">
      <menu action="File">
        <menuitem action="Quit"/>
      </menu>
      <menu action="View">
        <menuitem action="Skyview"/>
        <menuitem action="Responses"/>
        <menuitem action="GPS"/>
        <menuitem action="Noise"/>
        <menuitem action="Maidenhead"/>
        <menuitem action="AIS"/>
      </menu>
      <menu action="Units">
        <menuitem action="Imperial"/>
        <menuitem action="Nautical"/>
        <menuitem action="Metric"/>
      </menu>
      <menu action="Step of grid">
        <menuitem action="30deg"/>
        <menuitem action="45deg"/>
        <menuitem action="Off"/>
      </menu>
    </menubar>
</ui>
''')
        self.uimanager.get_widget('/MenuBar/View/Skyview').set_active(True)
        self.uimanager.get_widget('/MenuBar/View/Responses').set_active(True)
        self.uimanager.get_widget('/MenuBar/View/GPS').set_active(True)
        self.uimanager.get_widget('/MenuBar/View/Noise').set_active(True)
        self.uimanager.get_widget('/MenuBar/View/Maidenhead').set_active(True)
        self.uimanager.get_widget('/MenuBar/View/AIS').set_active(True)
        menubar = self.uimanager.get_widget('/MenuBar')
        vbox.pack_start(menubar, expand=False, fill=True, padding=0)

        self.satbox = Gtk.HBox(False, 0)
        vbox.add(self.satbox)

        skyframe = Gtk.Frame(label="Satellite List")
        self.satbox.add(skyframe)

        self.satlist = Gtk.ListStore(str, str, str, str, str)
        view = Gtk.TreeView(model=self.satlist)

        for (i, label) in enumerate(('PRN', 'Elev', 'Azim', 'SNR', 'Used')):
            column = Gtk.TreeViewColumn(label)
            renderer = Gtk.CellRendererText()
            column.pack_start(renderer, expand=True)
            column.add_attribute(renderer, 'text', i)
            view.append_column(column)

        self.row_iters = []
        for i in range(MAXCHANNELS):
            self.satlist.append(["", "", "", "", ""])
            self.row_iters.append(self.satlist.get_iter(i))

        skyframe.add(view)

        viewframe = Gtk.Frame(label="Skyview")
        self.satbox.add(viewframe)
        self.skyview = SkyView(self.rotate)
        viewframe.add(self.skyview)

        self.rawdisplay = Gtk.Entry()
        self.rawdisplay.set_editable(False)
        vbox.add(self.rawdisplay)

        self.dataframe = Gtk.Frame(label="GPS data")
        datatable = Gtk.Table(Base.COLUMNS, Base.ROWS, False)
        self.dataframe.add(datatable)
        gpswidgets = []
        for i in range(len(Base.gpsfields)):
            colbase = (i // Base.ROWS) * 2
            label = Gtk.Label(label=Base.gpsfields[i][0] + ": ")
            # Wacky way to force right alignment
            label.set_alignment(xalign=1, yalign=0.5)
            datatable.attach(label, colbase, colbase + 1,
                             i % Base.ROWS, i % Base.ROWS + 1)
            entry = Gtk.Entry()
            # The right size for the ISO8601 timestamp
            entry.set_width_chars(20)
            entry.set_text("n/a")
            datatable.attach(entry, colbase + 1, colbase + 2,
                             i % Base.ROWS, i % Base.ROWS + 1)
            gpswidgets.append(entry)
        vbox.add(self.dataframe)

        self.noisebox = Gtk.HBox(False, 0)
        vbox.add(self.noisebox)
        noiseframe = Gtk.Frame(label="Noise Statistics")
        self.noisebox.add(noiseframe)
        self.noiseview = NoiseView()
        noiseframe.add(self.noiseview.widget)

        self.gsbox = Gtk.HBox(False, 0)
        vbox.add(self.gsbox)
        gsframe = Gtk.Frame(label="Maidenhead Grid Square")
        self.gsbox.add(gsframe)
        self.gsview = MaidenheadView()
        gsframe.add(self.gsview.widget)

        self.aisbox = Gtk.HBox(False, 0)
        vbox.add(self.aisbox)
        aisframe = Gtk.Frame(label="AIS Data")
        self.aisbox.add(aisframe)
        self.aisview = AISView(self.deg_type)
        aisframe.add(self.aisview.widget)

        self.window.show_all()
        # Hide the Noise Statistics window until user selects it.
        self.uimanager.get_widget('/MenuBar/View/Noise').set_active(False)
        self.noisebox.hide()
        # Hide the Maidenhead window until user selects it.
        self.uimanager.get_widget('/MenuBar/View/Maidenhead').set_active(False)
        self.gsbox.hide()
        # Hide the AIS window until user selects it.
        self.uimanager.get_widget('/MenuBar/View/AIS').set_active(False)
        self.aisbox.hide()

        self.view_name_to_widget = {
            "Skyview": self.satbox,
            "Responses": self.rawdisplay,
            "GPS": self.dataframe,
            "Noise": self.noisebox,
            "Maidenhead": self.gsbox,
            "AIS": self.aisbox}

        # Discard field labels and associate data hooks with their widgets
        Base.gpsfields = [(label_hook_widget[0][1], label_hook_widget[1])
                          for label_hook_widget
                          in zip(Base.gpsfields, gpswidgets)]

    def view_toggle(self, action):
        # print("View toggle:", action.get_active(), action.get_name())
        if hasattr(self, 'view_name_to_widget'):
            if action.get_active():
                self.view_name_to_widget[action.get_name()].show()
            else:
                self.view_name_to_widget[action.get_name()].hide()
        # The effect we're after is to make the top-level window
        # resize itself to fit when we show or hide widgets.
        # This is undocumented magic to do that.
        self.window.resize(1, 1)

    def set_satlist_field(self, row, column, value):
        "Set a specified field in the satellite list."
        try:
            self.satlist.set_value(self.row_iters[row], column, str(value))
        except IndexError:
            sys.stderr.write("xgps: channel = %d, MAXCHANNELS = %d\n"
                             % (row, MAXCHANNELS))

    def delete_event(self, _widget, _event, _data=None):
        Gtk.main_quit()
        return False

    # State updates

    def update_time(self, data):
        if hasattr(data, "time"):
            # str() just in case we get an old-style float.
            return str(data.time)
        else:
            return "n/a"

    def update_latitude(self, data):
        if data.mode >= gps.MODE_2D and hasattr(data, "lat"):
            lat = gps.clienthelpers.deg_to_str(self.deg_type, abs(data.lat))
            if data.lat < 0:
                ns = 'S'
            else:
                ns = 'N'
            return "%s %s" % (lat, ns)
        else:
            return "n/a"

    def update_longitude(self, data):
        if data.mode >= gps.MODE_2D and hasattr(data, "lon"):
            lon = gps.clienthelpers.deg_to_str(self.deg_type, abs(data.lon))
            if data.lon < 0:
                ew = 'W'
            else:
                ew = 'E'
            return "%s %s" % (lon, ew)
        else:
            return "n/a"

    def update_altitude(self, data):
        if data.mode >= gps.MODE_3D and hasattr(data, "alt"):
            return "%9.3f %s" % (
                data.alt * self.conversions.altfactor,
                self.conversions.altunits)
        else:
            return "n/a"

    def update_speed(self, data):
        if hasattr(data, "speed"):
            return "%9.3f %s" % (
                data.speed * self.conversions.speedfactor,
                self.conversions.speedunits)
        else:
            return "n/a"

    def update_climb(self, data):
        if hasattr(data, "climb"):
            return "%9.3f %s" % (
                data.climb * self.conversions.speedfactor,
                self.conversions.speedunits)
        else:
            return "n/a"

    def update_track(self, data):
        if hasattr(data, "track"):
            return gps.clienthelpers.deg_to_str(self.deg_type, abs(data.track))
        else:
            return "n/a"

    def update_err(self, data, errtype):
        if hasattr(data, errtype):
            return "%.3f %s" % (
                getattr(data, errtype) * self.conversions.altfactor,
                self.conversions.altunits)
        else:
            return "n/a"

    def update_err_speed(self, data, errtype):
        if hasattr(data, errtype):
            return "%.3f %s" % (
                getattr(data, errtype) * self.conversions.speedfactor,
                self.conversions.speedunits)
        else:
            return "n/a"

    def update_err_degrees(self, data, errtype):
        if hasattr(data, errtype):
            return "%.3f °" % (getattr(data, errtype))
        else:
            return "n/a"

    def update_status(self, data):
        if data.mode == gps.MODE_2D:
            status = "2D FIX"
        elif data.mode == gps.MODE_3D:
            status = "3D FIX"
        else:
            status = "NO FIX"

        if hasattr(data, 'status') and data.status == gps.STATUS_DGPS_FIX:
            status += " DIFF"
        if data.mode != self.saved_mode:
            self.last_transition = time.time()
            self.saved_mode = data.mode
        return status + " (%d secs)" % (time.time() - self.last_transition)

    def update_gpsdata(self, tpv):
        "Update the GPS data fields."
        # the first 14 fields are updated using TPV data
        for (hook, widget) in Base.gpsfields[:14]:
            if hook:  # Remove this guard when we have all hooks
                widget.set_text(hook(self, tpv))
        self.gsview.update(tpv)

    def update_skyview(self, data):
        "Update the satellite list and skyview."
        if hasattr(data, 'satellites'):
            satellites = data.satellites
            for fld in reversed(SKY_VIEW_SORT_FIELDS):
                rev = (fld[0] == '-')
                if rev:
                    fld = fld[1:]
                satellites = sorted(
                    satellites[:],
                    key=lambda x: x[fld], reverse=rev)

            for (i, satellite) in enumerate(satellites):
                self.set_satlist_field(i, 0, "%3d" % satellite.PRN)
                self.set_satlist_field(i, 1, "%3d" % satellite.el)
                self.set_satlist_field(i, 2, "%3d" % satellite.az)
                self.set_satlist_field(i, 3, "%3d" % satellite.ss)
                yesno = 'N'
                if satellite.used:
                    yesno = 'Y'
                self.set_satlist_field(i, 4, yesno)
            for i in range(len(satellites), MAXCHANNELS):
                for j in range(0, 5):
                    self.set_satlist_field(i, j, "")
            self.skyview.redraw(satellites)

    # Preferences

    def set_units(self, system):
        "Change the display units."
        self.conversions = unit_adjustments(system)

    def set_step_of_grid(self, system):
        "Change the step of grid."
        self.skyview.step_of_grid = system

    # I/O monitoring and gtk housekeeping

    def watch(self, daemon, device):
        "Set up monitoring of a daemon instance."
        self.daemon = daemon
        self.device = device
        GObject.io_add_watch(daemon.sock, GObject.IO_IN, self.handle_response)
        GObject.io_add_watch(daemon.sock, GObject.IO_ERR, self.handle_hangup)
        GObject.io_add_watch(daemon.sock, GObject.IO_HUP, self.handle_hangup)

    def handle_response(self, source, condition):
        "Handle ordinary I/O ready condition from the daemon."
        if self.daemon.read() == -1:
            self.handle_hangup(source, condition)
        if self.daemon.valid & gps.PACKET_SET:
            if ((self.device
                 and "device" in self.daemon.data
                 and self.device != self.daemon.data["device"])):
                return True
            self.rawdisplay.set_text(self.daemon.response.strip())
            if self.daemon.data["class"] == "SKY":
                self.update_skyview(self.daemon.data)
            elif self.daemon.data["class"] == "TPV":
                self.update_gpsdata(self.daemon.data)
            elif self.daemon.data["class"] == "GST":
                self.noiseview.update(self.daemon.data)
                if not self.noise_latch:
                    self.noise_latch = True
                    self.uimanager.get_widget(
                        '/MenuBar/View/Noise').set_active(True)
                    self.noisebox.show()
            elif self.daemon.data["class"] == "AIS":
                self.aisview.update(self.daemon.data)
                if not self.ais_latch:
                    self.ais_latch = True
                    self.uimanager.get_widget(
                        '/MenuBar/View/AIS').set_active(True)
                    self.aisbox.show()

        return True

    def handle_hangup(self, _source, _condition):
        "Handle hangup condition from the daemon."
        w = Gtk.MessageDialog(parent=self.window,
                              type=Gtk.MessageType.ERROR,
                              flags=Gtk.DialogFlags.DESTROY_WITH_PARENT,
                              buttons=Gtk.ButtonsType.CANCEL)
        w.connect("destroy", lambda _unused: Gtk.main_quit())
        w.set_markup("gpsd has stopped sending data.")
        w.run()
        Gtk.main_quit()
        return True

    def main(self):
        Gtk.main()

if __name__ == "__main__":
    try:
        import getopt
        (options, arguments) = getopt.getopt(sys.argv[1:], "D:hl:u:r:V?",
                                             ['verbose'])
        debug = 0
        degreefmt = 'd'
        unit_system = None
        rotate = 0.0
        for (opt, val) in options:
            if opt in '-D':
                debug = int(val)
            elif opt == '-l':
                degreeformat = val
            elif opt == '-u':
                unit_system = val
            elif opt == '-r':
                try:
                    rotate = float(val)
                except ValueError:
                    rotate = 0.0
            elif opt in ('-?', '-h', '--help'):
                print(__doc__)
                sys.exit(0)
            elif opt == 'V':
                sys.stderr.write("xgps 1.0\n")
                sys.exit(0)

        degreefmt = {'d': gps.clienthelpers.deg_dd,
                     'm': gps.clienthelpers.deg_ddmm,
                     's': gps.clienthelpers.deg_ddmmss}[degreefmt]

        (host, port, device) = ("localhost", gps.GPSD_PORT, None)
        if len(arguments):
            args = arguments[0].split(":")
            if len(args) >= 1 and args[0]:
                host = args[0]
            if len(args) >= 2 and args[1]:
                port = args[1]
            if len(args) >= 3:
                device = args[2]
            target = ":".join(arguments[0:])
        else:
            target = ""

        base = Base(deg_type=degreefmt, rotate=rotate, target=target)
        base.set_units(unit_system)
        try:
            daemon = gps.gps(host=host,
                             port=port,
                             mode=gps.WATCH_ENABLE | gps.WATCH_JSON
                             | gps.WATCH_SCALED,
                             verbose=debug)
            base.watch(daemon, device)
            base.main()
        except socket.error:
            w = Gtk.MessageDialog(parent=base.window,
                                  type=Gtk.MessageType.ERROR,
                                  flags=Gtk.DialogFlags.DESTROY_WITH_PARENT,
                                  buttons=Gtk.ButtonsType.CANCEL)
            w.set_markup("gpsd is not running.")
            w.run()
            w.destroy()
    except KeyboardInterrupt:
        pass