-
Notifications
You must be signed in to change notification settings - Fork 6
/
localtimed.go
237 lines (197 loc) · 4.98 KB
/
localtimed.go
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
// Copyright (c) Steven Allen 2016
//
// This file is part of localtime.
//
// Localtime is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 only.
//
// Foobar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with localtime. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"log"
"github.com/bradfitz/latlong"
"github.com/godbus/dbus/v5"
)
const (
GeoclueBus = "org.freedesktop.GeoClue2"
TimedateBus = "org.freedesktop.timedate1"
GeoclueClientInterface = GeoclueBus + ".Client"
GeoclueLocationInterface = GeoclueBus + ".Location"
GeoclueManagerInterface = GeoclueBus + ".Manager"
TimedateInterface = TimedateBus
)
//nolint
const (
GCLUE_ACCURACY_LEVEL_NONE = 0
GCLUE_ACCURACY_LEVEL_COUNTRY = 1
GCLUE_ACCURACY_LEVEL_CITY = 4
GCLUE_ACCURACY_LEVEL_NEIGHBORHOOD = 5
GCLUE_ACCURACY_LEVEL_STREET = 6
GCLUE_ACCURACY_LEVEL_EXACT = 8
)
func NewGeoclueClient(conn *dbus.Conn) (*GeoclueClient, error) {
manager := conn.Object(
GeoclueBus,
"/org/freedesktop/GeoClue2/Manager",
)
var clientPath dbus.ObjectPath
if err := manager.Call(
GeoclueManagerInterface+".GetClient",
0,
).Store(&clientPath); err != nil {
return nil, err
}
clientObject := conn.Object(
GeoclueBus,
clientPath,
)
if err := clientObject.SetProperty(
GeoclueClientInterface+".DistanceThreshold",
dbus.MakeVariant(uint32(1000)),
); err != nil {
return nil, err
}
if err := clientObject.SetProperty(
GeoclueClientInterface+".RequestedAccuracyLevel",
dbus.MakeVariant(uint32(GCLUE_ACCURACY_LEVEL_CITY)),
); err != nil {
return nil, err
}
if err := clientObject.SetProperty(
GeoclueClientInterface+".DesktopId",
dbus.MakeVariant("localtimed"),
); err != nil {
return nil, err
}
return &GeoclueClient{
client: clientObject,
conn: conn,
done: make(chan struct{}),
}, nil
}
func setTimezone(conn *dbus.Conn, timezone string) error {
timedate := conn.Object(TimedateBus, "/org/freedesktop/timedate1")
return timedate.Call(
TimedateInterface+".SetTimezone",
0,
timezone,
false).Err
}
type Location struct {
Latitude float64
Longitude float64
}
type GeoclueClient struct {
client dbus.BusObject
conn *dbus.Conn
done chan struct{}
}
func (g *GeoclueClient) Close() error {
err := g.client.Call(
GeoclueClientInterface+".Stop",
0,
).Err
close(g.done)
return err
}
func (g *GeoclueClient) Start() (chan Location, error) {
// We don't need to add any matches because the signals are directed at us.
signals := make(chan *dbus.Signal, 10)
g.conn.Signal(signals)
// Try once without starting an agent.
if call := g.client.Call(
GeoclueClientInterface+".Start",
0,
); call.Err != nil {
return nil, call.Err
}
output := make(chan Location)
go func() {
for {
select {
case signal := <-signals:
var (
oldLocation dbus.ObjectPath
newLocation dbus.ObjectPath
)
if signal.Name != GeoclueClientInterface+".LocationUpdated" {
continue
}
if err := dbus.Store(
signal.Body,
&oldLocation,
&newLocation,
); err != nil {
panic("signal type")
}
output <- g.readLocation(newLocation)
case <-g.done:
close(output)
return
}
}
}()
return output, nil
}
func (g *GeoclueClient) readLocation(location dbus.ObjectPath) Location {
locationObject := g.conn.Object(GeoclueBus, location)
lat, err := locationObject.GetProperty(
GeoclueLocationInterface + ".Latitude",
)
if err != nil {
panic("Missing latitude")
}
lon, err := locationObject.GetProperty(
GeoclueLocationInterface + ".Longitude",
)
if err != nil {
panic("Missing longitude")
}
return Location{
Latitude: lat.Value().(float64),
Longitude: lon.Value().(float64),
}
}
func main() {
conn, err := dbus.SystemBus()
if err != nil {
log.Fatalln("Failed to connect to system bus:", err)
}
client, err := NewGeoclueClient(conn)
if err != nil {
log.Fatalln("Failed to initialize GeoClue2 Client:", err)
}
updates, err := client.Start()
if err != nil {
log.Fatalln("Failed to start GeoClue2 Client:", err)
}
defer client.Close()
old_timezone := ""
for loc := range updates {
if err != nil {
log.Println("Failed to get location:", err)
continue
}
new_timezone := latlong.LookupZoneName(loc.Latitude, loc.Longitude)
if new_timezone == "" {
log.Println("Failed to get timezone from location:", loc)
continue
}
if new_timezone == old_timezone {
continue
}
if err := setTimezone(conn, new_timezone); err != nil {
log.Println("Failed to set timezone:", new_timezone)
continue
}
log.Println("Updated timezone to:", new_timezone)
old_timezone = new_timezone
}
}