-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation.go
More file actions
350 lines (313 loc) · 9.11 KB
/
location.go
File metadata and controls
350 lines (313 loc) · 9.11 KB
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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/godbus/dbus/v5"
)
/*
GeoClue (geoclue2) location integration.
Overview:
- On startup call:
err := InitLocationTracking("whereami.desktop")
This will:
* Ensure a matching .desktop file exists (writes one into
~/.local/share/applications if missing).
* Spawn a goroutine that connects to GeoClue on the system bus,
creates a client, sets accuracy & thresholds, starts updates,
and listens for property changes to keep the in‑memory location
fresh.
- Optionally call RegisterLocationAPI(http.DefaultServeMux) to expose
GET /api/location (200 JSON or 204 if unknown)
Data exposed:
currentLocation (guarded by locationMu)
locationValid (true once we have at least one fix)
Failure strategy:
- If GeoClue is unavailable or permission denied, we log and
continue (API will return 204 No Content).
- The goroutine retries a few times initially, then backs off.
Security / Permissions:
- GeoClue requires a valid DesktopId property that matches a
.desktop file (basename) in XDG data dirs and contains
X-Geoclue-2-Client=true.
- Without it you'll usually get org.freedesktop.DBus.Error.AccessDenied
or the Start call will silently not produce locations.
Adding dependency:
- Ensure go.mod has: require github.com/godbus/dbus/v5 latest
*/
const (
geoService = "org.freedesktop.GeoClue2"
managerPath = dbus.ObjectPath("/org/freedesktop/GeoClue2/Manager")
managerIface = "org.freedesktop.GeoClue2.Manager"
clientIface = "org.freedesktop.GeoClue2.Client"
locationIface = "org.freedesktop.GeoClue2.Location"
propsIface = "org.freedesktop.DBus.Properties"
)
// LocationFix holds the last known position.
type LocationFix struct {
Latitude float64 `json:"lat"`
Longitude float64 `json:"lon"`
Accuracy float64 `json:"accuracy_m,omitempty"`
Altitude float64 `json:"altitude_m,omitempty"`
Timestamp time.Time `json:"timestamp"`
}
// Shared state.
var (
locationMu sync.RWMutex
currentLocation LocationFix
locationValid bool
locationCancel context.CancelFunc
)
// InitLocationTracking ensures a .desktop file is present and starts GeoClue client tracking.
func InitLocationTracking(desktopID string) error {
if err := ensureDesktopFile(desktopID); err != nil {
// Non-fatal but inform user.
log.Printf("location: failed to ensure desktop file: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
locationCancel = cancel
go runGeoClueLoop(ctx, desktopID)
return nil
}
// StopLocationTracking stops the background loop (optional).
func StopLocationTracking() {
if locationCancel != nil {
locationCancel()
}
}
// RegisterLocationAPI registers /api/location endpoint.
func RegisterLocationAPI(mux *http.ServeMux) {
if mux == nil {
mux = http.DefaultServeMux
}
mux.HandleFunc("/api/location", func(w http.ResponseWriter, r *http.Request) {
locationMu.RLock()
defer locationMu.RUnlock()
if !locationValid {
w.WriteHeader(http.StatusNoContent)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(currentLocation)
})
}
// ensureDesktopFile writes a minimal desktop file if it does not already exist.
// Returns nil if the file already exists.
func ensureDesktopFile(desktopID string) error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
appsDir := filepath.Join(home, ".local", "share", "applications")
if err := os.MkdirAll(appsDir, 0o755); err != nil {
return err
}
dest := filepath.Join(appsDir, desktopID)
if _, err := os.Stat(dest); err == nil {
// Exists; do not overwrite to allow user customization.
return nil
}
content := `[Desktop Entry]
Type=Application
Name=WhereAmI
Comment=Waypoint viewer (GeoClue client)
Exec=whereami
Icon=whereami
Terminal=false
Categories=Utility;
X-Geoclue-2-Client=true
X-Geoclue-2-Access-Fine=true
`
return os.WriteFile(dest, []byte(content), 0o644)
}
// -- GeoClue integration internals --
type geoClient struct {
path dbus.ObjectPath
bus *dbus.Conn
}
// runGeoClueLoop keeps trying to establish location updates until context cancelled.
func runGeoClueLoop(ctx context.Context, desktopID string) {
const (
maxInitialRetries = 5
retryBaseDelay = 2 * time.Second
requestedAccuracy = uint32(5) // "exact"
distanceThreshold = uint32(25) // meters between updates
timeThreshold = uint32(5) // seconds between updates
)
var attempt int
for {
select {
case <-ctx.Done():
return
default:
}
err := func() error {
cl, err := newGeoClueClient(desktopID, requestedAccuracy, distanceThreshold, timeThreshold)
if err != nil {
return err
}
defer cl.close()
if err := cl.start(); err != nil {
return err
}
// Get initial fix (if any)
cl.fetchInitialLocation()
// Subscribe to updates (blocks until context canceled or bus error)
return cl.runSignalLoop(ctx)
}()
if err == nil {
return
}
attempt++
var delay time.Duration
if attempt <= maxInitialRetries {
delay = retryBaseDelay * time.Duration(attempt)
} else {
delay = 30 * time.Second
}
log.Printf("location: retrying after error (%v), attempt=%d delay=%s", err, attempt, delay)
select {
case <-time.After(delay):
case <-ctx.Done():
return
}
}
}
func newGeoClueClient(desktopID string, acc, dist, sec uint32) (*geoClient, error) {
bus, err := dbus.SystemBus()
if err != nil {
return nil, err
}
manager := bus.Object(geoService, managerPath)
var clientPath dbus.ObjectPath
if call := manager.Call(managerIface+".CreateClient", 0); call.Err != nil {
return nil, call.Err
} else if err := call.Store(&clientPath); err != nil {
return nil, err
}
clientObj := bus.Object(geoService, clientPath)
// Helper to set property.
setProp := func(name string, val interface{}) error {
call := clientObj.Call(propsIface+".Set", 0, clientIface, name, dbus.MakeVariant(val))
return call.Err
}
if err := setProp("DesktopId", desktopID); err != nil {
return nil, fmt.Errorf("set DesktopId: %w", err)
}
if err := setProp("RequestedAccuracyLevel", acc); err != nil {
return nil, fmt.Errorf("set accuracy: %w", err)
}
_ = setProp("DistanceThreshold", dist)
_ = setProp("TimeThreshold", sec)
return &geoClient{path: clientPath, bus: bus}, nil
}
func (c *geoClient) start() error {
call := c.bus.Object(geoService, c.path).Call(clientIface+".Start", 0)
return call.Err
}
func (c *geoClient) close() {
_ = c.bus.Object(geoService, c.path).Call(clientIface+".Stop", 0)
c.bus.Close()
}
func (c *geoClient) fetchInitialLocation() {
locPath, err := c.getLocationPath()
if err != nil || locPath == "" {
return
}
c.readAndStoreLocation(locPath)
}
func (c *geoClient) getLocationPath() (dbus.ObjectPath, error) {
var variant dbus.Variant
call := c.bus.Object(geoService, c.path).Call(propsIface+".Get", 0, clientIface, "Location")
if call.Err != nil {
return "", call.Err
}
if err := call.Store(&variant); err != nil {
return "", err
}
locPath, _ := variant.Value().(dbus.ObjectPath)
return locPath, nil
}
func (c *geoClient) runSignalLoop(ctx context.Context) error {
// Match rule for PropertiesChanged on the client path
matchRule := fmt.Sprintf("type='signal',interface='%s',path='%s'", propsIface, c.path)
if call := c.bus.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, matchRule); call.Err != nil {
return call.Err
}
sigCh := make(chan *dbus.Signal, 10)
c.bus.Signal(sigCh)
for {
select {
case <-ctx.Done():
return nil
case sig := <-sigCh:
if sig == nil {
return errors.New("dbus signal channel closed")
}
if sig.Name == propsIface+".PropertiesChanged" && sig.Path == c.path {
// Body[1] should be changed map[string]Variant
if len(sig.Body) >= 2 {
if changed, ok := sig.Body[1].(map[string]dbus.Variant); ok {
if v, ok := changed["Location"]; ok {
if lp, ok := v.Value().(dbus.ObjectPath); ok && lp != "" {
c.readAndStoreLocation(lp)
}
}
}
}
}
}
}
}
func (c *geoClient) readAndStoreLocation(locPath dbus.ObjectPath) {
locObj := c.bus.Object(geoService, locPath)
var props map[string]dbus.Variant
call := locObj.Call(propsIface+".GetAll", 0, locationIface)
if call.Err != nil {
return
}
if err := call.Store(&props); err != nil {
return
}
getF64 := func(key string) (float64, bool) {
if v, ok := props[key]; ok {
if f, ok2 := v.Value().(float64); ok2 {
return f, true
}
}
return 0, false
}
lat, _ := getF64("Latitude")
lon, _ := getF64("Longitude")
acc, _ := getF64("Accuracy")
alt, _ := getF64("Altitude")
if lat == 0 && lon == 0 {
return // ignore obviously invalid fix
}
locationMu.Lock()
currentLocation = LocationFix{
Latitude: lat,
Longitude: lon,
Accuracy: acc,
Altitude: alt,
Timestamp: time.Now().UTC(),
}
locationValid = true
locationMu.Unlock()
}
// Helper so other packages (or QML integration wrappers later) can get current fix.
func GetCurrentLocation() (LocationFix, bool) {
locationMu.RLock()
defer locationMu.RUnlock()
if !locationValid {
return LocationFix{}, false
}
return currentLocation, true
}