Parent Directory | Revision Log
Fremantle widget sensitivity cleanups
1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of OSM2Go. |
5 | * |
6 | * OSM2Go is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * OSM2Go is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with OSM2Go. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include <stdio.h> |
21 | #include <string.h> |
22 | #include <math.h> |
23 | |
24 | #include "appdata.h" |
25 | |
26 | #ifdef ENABLE_LIBLOCATION |
27 | |
28 | #include <location/location-gps-device.h> |
29 | |
30 | gboolean gps_get_pos(appdata_t *appdata, pos_t *pos, float *alt) { |
31 | if(!appdata->settings || !appdata->settings->enable_gps) |
32 | return FALSE; |
33 | |
34 | gps_state_t *gps_state = appdata->gps_state; |
35 | |
36 | if(!gps_state->fix) |
37 | return FALSE; |
38 | |
39 | if(pos) { |
40 | pos->lat = gps_state->latitude; |
41 | pos->lon = gps_state->longitude; |
42 | } |
43 | |
44 | if(alt) |
45 | *alt = gps_state->altitude; |
46 | |
47 | return TRUE; |
48 | } |
49 | |
50 | static void |
51 | location_changed(LocationGPSDevice *device, gps_state_t *gps_state) { |
52 | |
53 | gps_state->fix = |
54 | (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET); |
55 | |
56 | if(gps_state->fix) { |
57 | gps_state->latitude = device->fix->latitude; |
58 | gps_state->longitude = device->fix->longitude; |
59 | } |
60 | |
61 | if(device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET) |
62 | gps_state->altitude = device->fix->altitude; |
63 | else |
64 | gps_state->altitude = NAN; |
65 | } |
66 | |
67 | void gps_init(appdata_t *appdata) { |
68 | gps_state_t *gps_state = appdata->gps_state = g_new0(gps_state_t, 1); |
69 | |
70 | printf("GPS init: Using liblocation\n"); |
71 | |
72 | gps_state->device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL); |
73 | if(!gps_state->device) { |
74 | printf("Unable to connect to liblocation\n"); |
75 | return; |
76 | } |
77 | |
78 | gps_state->idd_changed = |
79 | g_signal_connect(gps_state->device, "changed", |
80 | G_CALLBACK(location_changed), gps_state); |
81 | |
82 | #if MAEMO_VERSION_MAJOR < 5 |
83 | gps_state->control = location_gpsd_control_get_default(); |
84 | |
85 | if(gps_state->control && gps_state->control->can_control) { |
86 | printf("Having control over GPSD and GPS is to be enabled, starting it\n"); |
87 | location_gpsd_control_start(gps_state->control); |
88 | } |
89 | #endif |
90 | } |
91 | |
92 | void gps_release(appdata_t *appdata) { |
93 | gps_state_t *gps_state = appdata->gps_state; |
94 | |
95 | if(!gps_state->device) return; |
96 | |
97 | #if MAEMO_VERSION_MAJOR < 5 |
98 | if(gps_state->control && gps_state->control->can_control) { |
99 | printf("Having control over GPSD, stopping it\n"); |
100 | location_gpsd_control_stop(gps_state->control); |
101 | } |
102 | #endif |
103 | |
104 | /* Disconnect signal */ |
105 | g_signal_handler_disconnect(gps_state->device, gps_state->idd_changed); |
106 | |
107 | g_free(appdata->gps_state); |
108 | appdata->gps_state = NULL; |
109 | } |
110 | |
111 | void gps_enable(appdata_t *appdata, gboolean enable) { |
112 | if(appdata->settings) |
113 | appdata->settings->enable_gps = enable; |
114 | } |
115 | |
116 | #else // ENABLE_LIBLOCATION |
117 | |
118 | #ifdef ENABLE_GPSBT |
119 | #include <gpsbt.h> |
120 | #include <gpsmgr.h> |
121 | #include <errno.h> |
122 | #endif |
123 | |
124 | /* maybe user configurable later on ... */ |
125 | #define GPSD_HOST "127.0.0.1" |
126 | #define GPSD_PORT 2947 |
127 | |
128 | gboolean gps_get_pos(appdata_t *appdata, pos_t *pos, float *alt) { |
129 | if(pos) pos->lat = NAN; |
130 | |
131 | g_mutex_lock(appdata->gps_state->mutex); |
132 | if(appdata->gps_state->gpsdata.set & STATUS_SET) { |
133 | if(appdata->gps_state->gpsdata.status != STATUS_NO_FIX) { |
134 | if(appdata->gps_state->gpsdata.set & LATLON_SET) |
135 | *pos = appdata->gps_state->gpsdata.fix.pos; |
136 | if(appdata->gps_state->gpsdata.set & ALTITUDE_SET) |
137 | *alt = appdata->gps_state->gpsdata.fix.alt; |
138 | } |
139 | } |
140 | |
141 | g_mutex_unlock(appdata->gps_state->mutex); |
142 | |
143 | return(!isnan(pos->lat)); |
144 | } |
145 | |
146 | static int gps_connect(gps_state_t *gps_state) { |
147 | GnomeVFSResult vfs_result; |
148 | #ifdef ENABLE_GPSBT |
149 | char errstr[256] = ""; |
150 | |
151 | /* We need to start gpsd (via gpsbt) first. */ |
152 | memset(&gps_state->context, 0, sizeof(gpsbt_t)); |
153 | errno = 0; |
154 | |
155 | if(gpsbt_start(NULL, 0, 0, 0, errstr, sizeof(errstr), |
156 | 0, &gps_state->context) < 0) { |
157 | printf("Error connecting to GPS receiver: (%d) %s (%s)\n", |
158 | errno, strerror(errno), errstr); |
159 | } |
160 | #endif |
161 | |
162 | /************** from here down pure gnome/gtk/gpsd ********************/ |
163 | |
164 | /* try to connect to gpsd */ |
165 | /* Create a socket to interact with GPSD. */ |
166 | |
167 | printf("GPSD: trying to connect to %s %d\n", GPSD_HOST, GPSD_PORT); |
168 | |
169 | int retries = 5; |
170 | while(retries && |
171 | (GNOME_VFS_OK != (vfs_result = gnome_vfs_inet_connection_create( |
172 | &gps_state->iconn, GPSD_HOST, GPSD_PORT, NULL)))) { |
173 | printf("Error creating connection to GPSD, retrying ...\n"); |
174 | |
175 | retries--; |
176 | sleep(1); |
177 | } |
178 | |
179 | if(!retries) { |
180 | printf("Finally failed ...\n"); |
181 | return -1; |
182 | } |
183 | |
184 | retries = 5; |
185 | while(retries && ((gps_state->socket = |
186 | gnome_vfs_inet_connection_to_socket(gps_state->iconn)) == NULL)) { |
187 | printf("Error creating connecting GPSD socket, retrying ...\n"); |
188 | |
189 | retries--; |
190 | sleep(1); |
191 | } |
192 | |
193 | if(!retries) { |
194 | printf("Finally failed ...\n"); |
195 | gnome_vfs_inet_connection_destroy(gps_state->iconn, NULL); |
196 | return -1; |
197 | } |
198 | |
199 | GTimeVal timeout = { 10, 0 }; |
200 | if(GNOME_VFS_OK != (vfs_result = gnome_vfs_socket_set_timeout( |
201 | gps_state->socket, &timeout, NULL))) { |
202 | printf("Error setting GPSD timeout\n"); |
203 | gnome_vfs_inet_connection_destroy(gps_state->iconn, NULL); |
204 | return -1; |
205 | } |
206 | |
207 | printf("GPSD connected ...\n"); |
208 | |
209 | return 0; |
210 | } |
211 | |
212 | void gps_clear_fix(struct gps_fix_t *fixp) { |
213 | fixp->mode = MODE_NOT_SEEN; |
214 | fixp->pos.lat = fixp->pos.lon = NAN; |
215 | fixp->alt = NAN; |
216 | fixp->eph = NAN; |
217 | } |
218 | |
219 | /* unpack a daemon response into a status structure */ |
220 | static void gps_unpack(char *buf, struct gps_data_t *gpsdata) { |
221 | char *ns, *sp, *tp; |
222 | |
223 | for(ns = buf; ns; ns = strstr(ns+1, "GPSD")) { |
224 | if(strncmp(ns, "GPSD", 4) == 0) { |
225 | /* the following should execute each time we have a good next sp */ |
226 | for (sp = ns + 5; *sp != '\0'; sp = tp+1) { |
227 | tp = sp + strcspn(sp, ",\r\n"); |
228 | if (*tp == '\0') tp--; |
229 | else *tp = '\0'; |
230 | |
231 | switch (*sp) { |
232 | case 'O': |
233 | if (sp[2] == '?') { |
234 | gpsdata->set = |
235 | (gpsdata->set & SATELLITE_SET) | // fix for below |
236 | MODE_SET | STATUS_SET; // this clears sat info?? |
237 | gpsdata->status = STATUS_NO_FIX; |
238 | gps_clear_fix(&gpsdata->fix); |
239 | } else { |
240 | struct gps_fix_t nf; |
241 | char tag[MAXTAGLEN+1], alt[20], eph[20], lat[20], lon[20], mode[2]; |
242 | int st = sscanf(sp+2, |
243 | "%8s %*s %*s %19s %19s " |
244 | "%19s %19s %*s %*s %*s %*s " |
245 | "%*s %*s %*s %1s", |
246 | tag, lat, lon, |
247 | alt, eph, |
248 | mode); |
249 | if (st >= 5) { |
250 | #define DEFAULT(val) (val[0] == '?') ? NAN : g_ascii_strtod(val, NULL) |
251 | nf.pos.lat = DEFAULT(lat); |
252 | nf.pos.lon = DEFAULT(lon); |
253 | nf.eph = DEFAULT(eph); |
254 | nf.alt = DEFAULT(alt); |
255 | #undef DEFAULT |
256 | if (st >= 6) |
257 | nf.mode = (mode[0] == '?') ? MODE_NOT_SEEN : atoi(mode); |
258 | else |
259 | nf.mode = (alt[0] == '?') ? MODE_2D : MODE_3D; |
260 | gpsdata->fix = nf; |
261 | gpsdata->set |= LATLON_SET|MODE_SET; |
262 | gpsdata->status = STATUS_FIX; |
263 | gpsdata->set |= STATUS_SET; |
264 | |
265 | if(alt[0] != '?') |
266 | gpsdata->set |= ALTITUDE_SET; |
267 | } |
268 | } |
269 | break; |
270 | } |
271 | } |
272 | } |
273 | } |
274 | } |
275 | |
276 | void gps_enable(appdata_t *appdata, gboolean enable) { |
277 | if(appdata->settings) |
278 | appdata->settings->enable_gps = enable; |
279 | } |
280 | |
281 | gpointer gps_thread(gpointer data) { |
282 | GnomeVFSFileSize bytes_read; |
283 | GnomeVFSResult vfs_result; |
284 | char str[512]; |
285 | appdata_t *appdata = (appdata_t*)data; |
286 | |
287 | const char *msg = "o\r\n"; /* pos request */ |
288 | |
289 | appdata->gps_state->gpsdata.set = 0; |
290 | |
291 | gboolean connected = FALSE; |
292 | |
293 | while(1) { |
294 | if(appdata->settings->enable_gps) { |
295 | if(!connected) { |
296 | printf("trying to connect\n"); |
297 | |
298 | if(gps_connect(appdata->gps_state) < 0) |
299 | sleep(10); |
300 | else |
301 | connected = TRUE; |
302 | } else { |
303 | if(GNOME_VFS_OK == |
304 | (vfs_result = gnome_vfs_socket_write(appdata->gps_state->socket, |
305 | msg, strlen(msg)+1, &bytes_read, NULL))) { |
306 | |
307 | /* update every second, wait here to make sure a complete */ |
308 | /* reply is received */ |
309 | sleep(1); |
310 | |
311 | if(bytes_read == (strlen(msg)+1)) { |
312 | vfs_result = gnome_vfs_socket_read(appdata->gps_state->socket, |
313 | str, sizeof(str)-1, &bytes_read, NULL); |
314 | if(vfs_result == GNOME_VFS_OK) { |
315 | str[bytes_read] = 0; |
316 | |
317 | printf("msg: %s (%d)\n", str, strlen(str)); |
318 | |
319 | g_mutex_lock(appdata->gps_state->mutex); |
320 | |
321 | appdata->gps_state->gpsdata.set &= |
322 | ~(LATLON_SET|MODE_SET|STATUS_SET); |
323 | |
324 | gps_unpack(str, &appdata->gps_state->gpsdata); |
325 | g_mutex_unlock(appdata->gps_state->mutex); |
326 | } |
327 | } |
328 | } |
329 | } |
330 | } else { |
331 | if(connected) { |
332 | printf("stopping GPS connection due to user request\n"); |
333 | gnome_vfs_inet_connection_destroy(appdata->gps_state->iconn, NULL); |
334 | |
335 | #ifdef ENABLE_GPSBT |
336 | gpsbt_stop(&appdata->gps_state->context); |
337 | #endif |
338 | connected = FALSE; |
339 | } else |
340 | sleep(1); |
341 | } |
342 | } |
343 | |
344 | printf("GPS thread ended???\n"); |
345 | return NULL; |
346 | } |
347 | |
348 | void gps_init(appdata_t *appdata) { |
349 | appdata->gps_state = g_new0(gps_state_t, 1); |
350 | |
351 | printf("GPS init: Using gpsd\n"); |
352 | |
353 | /* start a new thread to listen to gpsd */ |
354 | appdata->gps_state->mutex = g_mutex_new(); |
355 | appdata->gps_state->thread_p = |
356 | g_thread_create(gps_thread, appdata, FALSE, NULL); |
357 | } |
358 | |
359 | void gps_release(appdata_t *appdata) { |
360 | #ifdef ENABLE_GPSBT |
361 | gpsbt_stop(&appdata->gps_state->context); |
362 | #endif |
363 | g_free(appdata->gps_state); |
364 | appdata->gps_state = NULL; |
365 | } |
366 | |
367 | #endif // ENABLE_LIBLOCATION |