forked from jonls/redshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
location-corelocation.m
284 lines (220 loc) · 7.14 KB
/
location-corelocation.m
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
/* location-corelocation.m -- CoreLocation (OSX) location provider source
This file is part of Redshift.
Redshift 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, either version 3 of the License, or
(at your option) any later version.
Redshift 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 Redshift. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) 2014-2017 Jon Lund Steffensen <jonlst@gmail.com>
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#include "location-corelocation.h"
#include "pipeutils.h"
#include "redshift.h"
#include <stdio.h>
#include <unistd.h>
#ifdef ENABLE_NLS
# include <libintl.h>
# define _(s) gettext(s)
#else
# define _(s) s
#endif
typedef struct {
NSThread *thread;
NSLock *lock;
int pipe_fd_read;
int pipe_fd_write;
int available;
int error;
float latitude;
float longitude;
} location_corelocation_state_t;
@interface LocationDelegate : NSObject <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (nonatomic) location_corelocation_state_t *state;
@end
@implementation LocationDelegate;
- (void)start
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 50000;
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
CLAuthorizationStatus authStatus =
[CLLocationManager authorizationStatus];
if (authStatus != kCLAuthorizationStatusNotDetermined &&
authStatus != kCLAuthorizationStatusAuthorized) {
fputs(_("Not authorized to obtain location"
" from CoreLocation.\n"), stderr);
[self markError];
} else {
[self.locationManager startUpdatingLocation];
}
}
- (void)markError
{
[self.state->lock lock];
self.state->error = 1;
[self.state->lock unlock];
pipeutils_signal(self.state->pipe_fd_write);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations firstObject];
[self.state->lock lock];
self.state->latitude = newLocation.coordinate.latitude;
self.state->longitude = newLocation.coordinate.longitude;
self.state->available = 1;
[self.state->lock unlock];
pipeutils_signal(self.state->pipe_fd_write);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
fprintf(stderr, _("Error obtaining location from CoreLocation: %s\n"),
[[error localizedDescription] UTF8String]);
[self markError];
}
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (status == kCLAuthorizationStatusNotDetermined) {
fputs(_("Waiting for authorization to obtain location...\n"), stderr);
} else if (status != kCLAuthorizationStatusAuthorized) {
fputs(_("Request for location was not authorized!\n"), stderr);
[self markError];
}
}
@end
// Callback when the pipe is closed.
//
// Stops the run loop causing the thread to end.
static void
pipe_close_callback(
CFFileDescriptorRef fdref, CFOptionFlags callBackTypes, void *info)
{
CFFileDescriptorInvalidate(fdref);
CFRelease(fdref);
CFRunLoopStop(CFRunLoopGetCurrent());
}
@interface LocationThread : NSThread
@property (nonatomic) location_corelocation_state_t *state;
@end
@implementation LocationThread;
// Run loop for location provider thread.
- (void)main
{
@autoreleasepool {
LocationDelegate *locationDelegate = [[LocationDelegate alloc] init];
locationDelegate.state = self.state;
// Start the location delegate on the run loop in this thread.
[locationDelegate performSelector:@selector(start)
withObject:nil afterDelay:0];
// Create a callback that is triggered when the pipe is closed. This will
// trigger the main loop to quit and the thread to stop.
CFFileDescriptorRef fdref = CFFileDescriptorCreate(
kCFAllocatorDefault, self.state->pipe_fd_write, false,
pipe_close_callback, NULL);
CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack);
CFRunLoopSourceRef source = CFFileDescriptorCreateRunLoopSource(
kCFAllocatorDefault, fdref, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
// Run the loop
CFRunLoopRun();
close(self.state->pipe_fd_write);
}
}
@end
static int
location_corelocation_init(location_corelocation_state_t **state)
{
*state = malloc(sizeof(location_corelocation_state_t));
if (*state == NULL) return -1;
return 0;
}
static int
location_corelocation_start(location_corelocation_state_t *state)
{
state->pipe_fd_read = -1;
state->pipe_fd_write = -1;
state->available = 0;
state->error = 0;
state->latitude = 0;
state->longitude = 0;
int pipefds[2];
int r = pipeutils_create_nonblocking(pipefds);
if (r < 0) {
fputs(_("Failed to start CoreLocation provider!\n"), stderr);
return -1;
}
state->pipe_fd_read = pipefds[0];
state->pipe_fd_write = pipefds[1];
pipeutils_signal(state->pipe_fd_write);
state->lock = [[NSLock alloc] init];
LocationThread *thread = [[LocationThread alloc] init];
thread.state = state;
[thread start];
state->thread = thread;
return 0;
}
static void
location_corelocation_free(location_corelocation_state_t *state)
{
if (state->pipe_fd_read != -1) {
close(state->pipe_fd_read);
}
free(state);
}
static void
location_corelocation_print_help(FILE *f)
{
fputs(_("Use the location as discovered by the Corelocation provider.\n"), f);
fputs("\n", f);
}
static int
location_corelocation_set_option(
location_corelocation_state_t *state, const char *key, const char *value)
{
fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
return -1;
}
static int
location_corelocation_get_fd(location_corelocation_state_t *state)
{
return state->pipe_fd_read;
}
static int
location_corelocation_handle(
location_corelocation_state_t *state,
location_t *location, int *available)
{
pipeutils_handle_signal(state->pipe_fd_read);
[state->lock lock];
int error = state->error;
location->lat = state->latitude;
location->lon = state->longitude;
*available = state->available;
[state->lock unlock];
if (error) return -1;
return 0;
}
const location_provider_t corelocation_location_provider = {
"corelocation",
(location_provider_init_func *)location_corelocation_init,
(location_provider_start_func *)location_corelocation_start,
(location_provider_free_func *)location_corelocation_free,
(location_provider_print_help_func *)location_corelocation_print_help,
(location_provider_set_option_func *)location_corelocation_set_option,
(location_provider_get_fd_func *)location_corelocation_get_fd,
(location_provider_handle_func *)location_corelocation_handle
};