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
|
/* $Id$ */
/* GPS speedometer as a wrapper around an Athena widget Tachometer
* - Derrick J Brashear <shadow@dementia.org>
*/
#include <sys/types.h>
#include <stdlib.h>
#ifndef S_SPLINT_S
#include <unistd.h>
#endif /* S_SPLINT_S */
#include <stdio.h>
#include <math.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Paned.h>
#include <Tachometer.h>
#include "gpsd_config.h"
#include "gps.h"
#include "gpsdclient.h"
#include "xgpsspeed.icon"
/*@ -nullassign @*/
static XrmOptionDescRec options[] = {
{"-rv", "*reverseVideo", XrmoptionNoArg, "TRUE"},
{"-nc", "*needleColor", XrmoptionSepArg, NULL},
{"-needlecolor","*needleColor", XrmoptionSepArg, NULL},
{"-speedunits", "*speedunits", XrmoptionSepArg, NULL},
};
String fallback_resources[] = {NULL};
/*@ +nullassign @*/
static struct gps_data_t *gpsdata;
static Widget tacho;
static double speedfactor;
static Widget toplevel;
static struct fixsource_t source;
static void update_display(struct gps_data_t *gpsdata,
char *buf UNUSED, size_t len UNUSED)
{
/* this is where we implement source-device filtering */
if (gpsdata->dev.path[0]!='\0' && source.device!=NULL && strcmp(source.device, gpsdata->dev.path) != 0)
return;
else {
int temp_int = (int)rint(gpsdata->fix.speed * speedfactor);
if (temp_int < 0) temp_int = 0;
else if (temp_int > 100) temp_int = 100;
(void)TachometerSetValue(tacho, temp_int);
}
}
static void handle_input(XtPointer client_data UNUSED,
int *source UNUSED, XtInputId * id UNUSED)
{
if (gps_poll(gpsdata) < 0) {
(void)fprintf(stderr, "Read error on server socket.");
exit(1);
}
}
static char *get_resource(Widget w, char *name, char *default_value)
{
XtResource xtr;
char *value = NULL;
/*@ -observertrans -statictrans -immediatetrans -compdestroy -nullpass @*/
xtr.resource_name = name;
xtr.resource_class = "AnyClass";
xtr.resource_type = XtRString;
xtr.resource_size = (Cardinal)sizeof(String);
xtr.resource_offset = 0;
xtr.default_type = XtRImmediate;
xtr.default_addr = default_value;
XtGetApplicationResources(w, &value, &xtr, 1, NULL, 0);
if (value) return value;
/*@ +observertrans +statictrans +immediatetrans +compdestroy +nullpass @*/
/*@i@*/return default_value;
}
/*@ -mustfreefresh @*/
int main(int argc, char **argv)
{
Arg args[10];
XtAppContext app;
int option;
char *speedunits;
Widget base;
/*@ -compdef -nullpass -onlytrans @*/
toplevel = XtVaAppInitialize(&app, "xgpsspeed",
options, XtNumber(options),
&argc, argv, fallback_resources, NULL);
/*@ +compdef +nullpass +onlytrans @*/
speedfactor = MPS_TO_MPH; /* Software maintained in US */
speedunits = get_resource(toplevel, "speedunits", "mph");
if (strcmp(speedunits, "kph")==0)
speedfactor = MPS_TO_KPH;
else if (strcmp(speedunits, "knots")==0)
speedfactor = MPS_TO_KNOTS;
while ((option = getopt(argc, argv, "hV")) != -1) {
switch (option) {
case 'V':
(void)printf("xgpsspeed %s\n", VERSION);
exit(0);
case 'h': default:
(void)fputs("usage: gps [-h] [-V] [-rv] [-nc] [-needlecolor] [-speedunits {mph,kph,knots}] [server[:port]]\n", stderr);
exit(1);
}
}
/*@ -compdestroy @*/
if (optind < argc) {
gpsd_source_spec(argv[optind], &source);
} else
gpsd_source_spec(NULL, &source);
/*@ -immediatetrans -usedef -observertrans -statictrans -nullpass @*/
/**** Shell Widget ****/
(void)XtSetArg(args[0], XtNiconPixmap,
XCreateBitmapFromData(XtDisplay(toplevel),
XtScreen(toplevel)->root, (char*)xgps_bits,
xgps_width, xgps_height));
(void)XtSetValues(toplevel, args, 1);
/**** Form widget ****/
base = XtCreateManagedWidget("pane", panedWidgetClass, toplevel, NULL, 0);
/**** Label widget (Title) ****/
(void)XtSetArg(args[0], XtNlabel, "GPS Speedometer");
(void)XtCreateManagedWidget("title", labelWidgetClass, base, args, 1);
/**** Label widget ****/
if (speedfactor == MPS_TO_MPH)
(void)XtSetArg(args[0], XtNlabel, "Miles per Hour");
else if (speedfactor == MPS_TO_KPH)
(void)XtSetArg(args[0], XtNlabel, "Km per Hour");
else
(void)XtSetArg(args[0], XtNlabel, "Knots");
/*@ +immediatetrans +usedef +observertrans +statictrans -compmempass @*/
(void)XtCreateManagedWidget("name", labelWidgetClass, base, args, 1);
/**** Tachometer widget ****/
/*@ -onlytrans -mustfreeonly @*/
tacho = XtCreateManagedWidget("meter", tachometerWidgetClass,base,NULL,0);
(void)XtRealizeWidget(toplevel);
if (!(gpsdata = gps_open(source.server, source.port))) {
(void)fputs("xgpsspeed: no gpsd running or network error\n", stderr);
exit(2);
}
/*@ +onlytrans +mustfreeonly @*/
/*@ -usedef @*/
(void)XtAppAddInput(app, gpsdata->gps_fd, (XtPointer) XtInputReadMask,
handle_input, NULL);
/*@ +nullpass +usedef @*/
gps_set_raw_hook(gpsdata, update_display);
(void)gps_stream(gpsdata, WATCH_ENABLE, NULL);
(void)XtAppMainLoop(app);
(void)gps_close(gpsdata);
return 0;
/*@ +compdestroy @*/
}
/*@ +mustfreefresh @*/
|