Experimental "begin track" tool
[viking/gosmore.git] / src / vikgpslayer.c
blobd55d433f66975366fb68f40114992ce20790d3a3
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
11 * This program 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdlib.h>
23 #include <math.h>
24 #include "viking.h"
25 #include "vikgpslayer_pixmap.h"
26 #include "babel.h"
28 #include <string.h>
29 #include <glib.h>
30 #include <glib/gprintf.h>
31 #include <gps.h>
33 #define DISCONNECT_UPDATE_SIGNAL(vl, val) g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val)
34 static VikGpsLayer *vik_gps_layer_create (VikViewport *vp);
35 static void vik_gps_layer_realize ( VikGpsLayer *val, VikTreeview *vt, GtkTreeIter *layer_iter );
36 static void vik_gps_layer_free ( VikGpsLayer *val );
37 static void vik_gps_layer_draw ( VikGpsLayer *val, gpointer data );
38 VikGpsLayer *vik_gps_layer_new ();
40 static void gps_layer_marshall( VikGpsLayer *val, guint8 **data, gint *len );
41 static VikGpsLayer *gps_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
42 static gboolean gps_layer_set_param ( VikGpsLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp );
43 static VikLayerParamData gps_layer_get_param ( VikGpsLayer *vgl, guint16 id );
45 static void gps_layer_change_coord_mode ( VikGpsLayer *val, VikCoordMode mode );
46 static void gps_layer_add_menu_items( VikGpsLayer *vtl, GtkMenu *menu, gpointer vlp );
47 static void gps_layer_drag_drop_request ( VikGpsLayer *val_src, VikGpsLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path );
49 static void realtime_tracking_draw(VikGpsLayer *vgl, VikViewport *vp);
51 static void gps_upload_cb( gpointer layer_and_vlp[2] );
52 static void gps_download_cb( gpointer layer_and_vlp[2] );
53 static void gps_empty_upload_cb( gpointer layer_and_vlp[2] );
54 static void gps_empty_download_cb( gpointer layer_and_vlp[2] );
55 static void gps_empty_all_cb( gpointer layer_and_vlp[2] );
56 static void gps_start_stop_tracking_cb( gpointer layer_and_vlp[2] );
58 typedef enum {GARMIN_P = 0, MAGELLAN_P, NUM_PROTOCOLS} vik_gps_proto;
59 static gchar * params_protocols[] = {"Garmin", "Magellan", NULL};
60 static gchar * protocols_args[] = {"garmin", "magellan"};
61 /*#define NUM_PROTOCOLS (sizeof(params_protocols)/sizeof(params_protocols[0]) - 1) */
62 static gchar * params_ports[] = {"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyUSB0", "/dev/ttyUSB1", "usb:", NULL};
63 #define NUM_PORTS (sizeof(params_ports)/sizeof(params_ports[0]) - 1)
64 typedef enum {GPS_DOWN=0, GPS_UP} gps_dir;
66 typedef struct {
67 GMutex *mutex;
68 gps_dir direction;
69 gchar *port;
70 gboolean ok;
71 gint total_count;
72 gint count;
73 VikTrwLayer *vtl;
74 gchar *cmd_args;
75 gchar * window_title;
76 GtkWidget *dialog;
77 GtkWidget *status_label;
78 GtkWidget *gps_label;
79 GtkWidget *ver_label;
80 GtkWidget *id_label;
81 GtkWidget *wp_label;
82 GtkWidget *progress_label;
83 GtkWidget *trk_label;
84 } GpsSession;
85 static void gps_session_delete(GpsSession *sess);
87 static gchar *params_groups[] = {"Data Mode", "Realtime Tracking Mode"};
88 enum {GROUP_DATA_MODE, GROUP_REALTIME_MODE};
90 static VikLayerParam gps_layer_params[] = {
91 { "gps_protocol", VIK_LAYER_PARAM_UINT, GROUP_DATA_MODE, "GPS Protocol:", VIK_LAYER_WIDGET_COMBOBOX, params_protocols, NULL},
92 { "gps_port", VIK_LAYER_PARAM_UINT, GROUP_DATA_MODE, "Serial Port:", VIK_LAYER_WIDGET_COMBOBOX, params_ports, NULL},
94 { "record_tracking", VIK_LAYER_PARAM_BOOLEAN, GROUP_REALTIME_MODE, "Recording tracks", VIK_LAYER_WIDGET_CHECKBUTTON},
95 { "center_start_tracking", VIK_LAYER_PARAM_BOOLEAN, GROUP_REALTIME_MODE, "Jump to current position on start", VIK_LAYER_WIDGET_CHECKBUTTON},
96 { "centered_tracking", VIK_LAYER_PARAM_BOOLEAN, GROUP_REALTIME_MODE, "Keep current position at center", VIK_LAYER_WIDGET_CHECKBUTTON},
97 { "gpsd_host", VIK_LAYER_PARAM_STRING, GROUP_REALTIME_MODE, "Gpsd Host:", VIK_LAYER_WIDGET_ENTRY},
98 { "gpsd_port", VIK_LAYER_PARAM_STRING, GROUP_REALTIME_MODE, "Gpsd Port:", VIK_LAYER_WIDGET_ENTRY},
100 enum {PARAM_PROTOCOL=0, PARAM_PORT, PARAM_REALTIME_REC, PARAM_REALTIME_CENTER_START, PARAM_REALTIME_CENTERED, PARAM_GPSD_HOST, PARAM_GPSD_PORT, NUM_PARAMS};
102 VikLayerInterface vik_gps_layer_interface = {
103 "GPS",
104 &gpslayer_pixbuf,
106 NULL,
109 gps_layer_params,
110 NUM_PARAMS,
111 params_groups,
112 sizeof(params_groups)/sizeof(params_groups[0]),
114 VIK_MENU_ITEM_ALL & ~(VIK_MENU_ITEM_CUT|VIK_MENU_ITEM_COPY),
116 (VikLayerFuncCreate) vik_gps_layer_create,
117 (VikLayerFuncRealize) vik_gps_layer_realize,
118 (VikLayerFuncPostRead) NULL,
119 (VikLayerFuncFree) vik_gps_layer_free,
121 (VikLayerFuncProperties) NULL,
122 (VikLayerFuncDraw) vik_gps_layer_draw,
123 (VikLayerFuncChangeCoordMode) gps_layer_change_coord_mode,
125 (VikLayerFuncSetMenuItemsSelection) NULL,
126 (VikLayerFuncGetMenuItemsSelection) NULL,
128 (VikLayerFuncAddMenuItems) gps_layer_add_menu_items,
129 (VikLayerFuncSublayerAddMenuItems) NULL,
131 (VikLayerFuncSublayerRenameRequest) NULL,
132 (VikLayerFuncSublayerToggleVisible) NULL,
134 (VikLayerFuncMarshall) gps_layer_marshall,
135 (VikLayerFuncUnmarshall) gps_layer_unmarshall,
137 (VikLayerFuncSetParam) gps_layer_set_param,
138 (VikLayerFuncGetParam) gps_layer_get_param,
140 (VikLayerFuncReadFileData) NULL,
141 (VikLayerFuncWriteFileData) NULL,
143 (VikLayerFuncDeleteItem) NULL,
144 (VikLayerFuncCopyItem) NULL,
145 (VikLayerFuncPasteItem) NULL,
146 (VikLayerFuncFreeCopiedItem) NULL,
147 (VikLayerFuncDragDropRequest) gps_layer_drag_drop_request,
150 enum {TRW_DOWNLOAD=0, TRW_UPLOAD, TRW_REALTIME, NUM_TRW};
151 static gchar * trw_names[] = {"GPS Download", "GPS Upload", "GPS Realtime Tracking"};
153 typedef struct {
154 struct gps_data_t gpsd;
155 VikGpsLayer *vgl;
156 } VglGpsd;
158 typedef struct {
159 struct gps_fix_t fix;
160 gboolean dirty; /* needs to be saved */
161 } GpsFix;
163 struct _VikGpsLayer {
164 VikLayer vl;
165 VikTrwLayer * trw_children[NUM_TRW];
166 GList * children; /* used only for writing file */
167 int cur_read_child; /* used only for reading file */
168 VglGpsd *vgpsd;
169 gboolean realtime_tracking;
170 GMutex *realtime_tracking_mutex;
171 GpsFix realtime_fix;
172 GpsFix last_fix;
174 enum unit realtime_gpsd_unit;
176 VikTrack *realtime_track;
177 gchar *realtime_track_name;
179 GIOChannel *realtime_io_channel;
180 guint realtime_io_watch_id;
181 GdkGC *realtime_track_gc;
182 GdkGC *realtime_track_bg_gc;
183 GdkGC *realtime_track_pt_gc;
184 GdkGC *realtime_track_pt1_gc;
185 GdkGC *realtime_track_pt2_gc;
187 /* params */
188 guint protocol_id;
189 guint serial_port_id;
190 /* TODO: show on diaglog */
191 gchar *gpsd_host;
192 gchar *gpsd_port;
193 gboolean realtime_record;
194 gboolean realtime_jump_to_start;
195 gboolean realtime_keep_at_center;
198 GType vik_gps_layer_get_type ()
200 static GType val_type = 0;
202 if (!val_type)
204 static const GTypeInfo val_info =
206 sizeof (VikGpsLayerClass),
207 NULL, /* base_init */
208 NULL, /* base_finalize */
209 NULL, /* class init */
210 NULL, /* class_finalize */
211 NULL, /* class_data */
212 sizeof (VikGpsLayer),
214 NULL /* instance init */
216 val_type = g_type_register_static ( VIK_LAYER_TYPE, "VikGpsLayer", &val_info, 0 );
219 return val_type;
222 static VikGpsLayer *vik_gps_layer_create (VikViewport *vp)
224 int i;
226 VikGpsLayer *rv = vik_gps_layer_new (vp);
227 vik_layer_rename ( VIK_LAYER(rv), vik_gps_layer_interface.name );
229 for (i = 0; i < NUM_TRW; i++) {
230 rv->trw_children[i] = VIK_TRW_LAYER(vik_layer_create ( VIK_LAYER_TRW, vp, NULL, FALSE ));
231 vik_layer_set_menu_items_selection(VIK_LAYER(rv->trw_children[i]), VIK_MENU_ITEM_ALL & ~(VIK_MENU_ITEM_CUT|VIK_MENU_ITEM_DELETE));
233 return rv;
236 /* "Copy" */
237 static void gps_layer_marshall( VikGpsLayer *vgl, guint8 **data, gint *datalen )
239 VikLayer *child_layer;
240 guint8 *ld;
241 gint ll;
242 GByteArray* b = g_byte_array_new ();
243 gint len;
244 gint i;
246 #define alm_append(obj, sz) \
247 len = (sz); \
248 g_byte_array_append ( b, (guint8 *)&len, sizeof(len) ); \
249 g_byte_array_append ( b, (guint8 *)(obj), len );
251 vik_layer_marshall_params(VIK_LAYER(vgl), &ld, &ll);
252 alm_append(ld, ll);
253 g_free(ld);
255 for (i = 0; i < NUM_TRW; i++) {
256 child_layer = VIK_LAYER(vgl->trw_children[i]);
257 vik_layer_marshall(child_layer, &ld, &ll);
258 if (ld) {
259 alm_append(ld, ll);
260 g_free(ld);
263 *data = b->data;
264 *datalen = b->len;
265 g_byte_array_free(b, FALSE);
266 #undef alm_append
269 /* "Paste" */
270 static VikGpsLayer *gps_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
272 #define alm_size (*(gint *)data)
273 #define alm_next \
274 len -= sizeof(gint) + alm_size; \
275 data += sizeof(gint) + alm_size;
277 VikGpsLayer *rv = vik_gps_layer_new();
278 VikLayer *child_layer;
279 gint i;
281 vik_layer_unmarshall_params ( VIK_LAYER(rv), data+sizeof(gint), alm_size, vvp );
282 alm_next;
284 i = 0;
285 while (len>0 && i < NUM_TRW) {
286 child_layer = vik_layer_unmarshall ( data + sizeof(gint), alm_size, vvp );
287 if (child_layer) {
288 rv->trw_children[i++] = (VikTrwLayer *)child_layer;
289 g_signal_connect_swapped ( G_OBJECT(child_layer), "update", G_CALLBACK(vik_layer_emit_update_secondary), rv );
291 alm_next;
293 // g_print("gps_layer_unmarshall ended with len=%d\n", len);
294 g_assert(len == 0);
295 return rv;
296 #undef alm_size
297 #undef alm_next
300 static gboolean gps_layer_set_param ( VikGpsLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp )
302 switch ( id )
304 /* TODO: gpsd_host, gpsd_port */
305 case PARAM_PROTOCOL:
306 if (data.u < NUM_PROTOCOLS)
307 vgl->protocol_id = data.u;
308 else
309 g_warning("Unknown GPS Protocol");
310 break;
311 case PARAM_PORT:
312 if (data.u < NUM_PORTS)
313 vgl->serial_port_id = data.u;
314 else
315 g_warning("Unknown serial port device");
316 break;
317 case PARAM_GPSD_HOST:
318 if (vgl->gpsd_host)
319 g_free(vgl->gpsd_host);
320 vgl->gpsd_host = g_strdup(data.s);
321 break;
322 case PARAM_GPSD_PORT:
323 if (vgl->gpsd_port)
324 g_free(vgl->gpsd_port);
325 vgl->gpsd_port = g_strdup(data.s); /* TODO: check for number */
326 break;
327 case PARAM_REALTIME_REC:
328 vgl->realtime_record = data.b;
329 break;
330 case PARAM_REALTIME_CENTER_START:
331 vgl->realtime_jump_to_start = data.b;
332 break;
333 case PARAM_REALTIME_CENTERED:
334 vgl->realtime_keep_at_center = data.b;
335 break;
336 default:
337 g_warning("gps_layer_set_param(): unknown parameter");
340 return TRUE;
343 static VikLayerParamData gps_layer_get_param ( VikGpsLayer *vgl, guint16 id )
345 VikLayerParamData rv;
346 switch ( id )
348 /* TODO: gpsd_host, gpsd_port */
349 case PARAM_PROTOCOL:
350 rv.u = vgl->protocol_id;
351 break;
352 case PARAM_PORT:
353 rv.u = vgl->serial_port_id;
354 break;
355 case PARAM_GPSD_HOST:
356 rv.s = vgl->gpsd_host ? vgl->gpsd_host : "";
357 break;
358 case PARAM_GPSD_PORT:
359 rv.s = vgl->gpsd_port ? vgl->gpsd_port : g_strdup(DEFAULT_GPSD_PORT);
360 break;
361 case PARAM_REALTIME_REC:
362 rv.b = vgl->realtime_record;
363 break;
364 case PARAM_REALTIME_CENTER_START:
365 rv.b = vgl->realtime_jump_to_start;
366 break;
367 case PARAM_REALTIME_CENTERED:
368 rv.b = vgl->realtime_keep_at_center;
369 break;
370 default:
371 g_warning("gps_layer_get_param(): unknown parameter");
374 return rv;
377 VikGpsLayer *vik_gps_layer_new (VikViewport *vp)
379 gint i;
380 VikGpsLayer *vgl = VIK_GPS_LAYER ( g_object_new ( VIK_GPS_LAYER_TYPE, NULL ) );
381 vik_layer_init ( VIK_LAYER(vgl), VIK_LAYER_GPS );
382 for (i = 0; i < NUM_TRW; i++) {
383 vgl->trw_children[i] = NULL;
385 vgl->children = NULL;
386 vgl->cur_read_child = 0;
388 vgl->realtime_tracking_mutex = g_mutex_new();
389 vgl->realtime_tracking = FALSE;
390 vgl->vgpsd = NULL;
391 vgl->realtime_track_gc = vik_viewport_new_gc ( vp, "#203070", 2 );
392 vgl->realtime_track_bg_gc = vik_viewport_new_gc ( vp, "grey", 2 );
393 vgl->realtime_track_pt1_gc = vik_viewport_new_gc ( vp, "red", 2 );
394 vgl->realtime_track_pt2_gc = vik_viewport_new_gc ( vp, "green", 2 );
395 vgl->realtime_track_pt_gc = vgl->realtime_track_pt1_gc;
396 vgl->realtime_gpsd_unit = gpsd_units();
397 // fprintf(stderr, "DEBUG: gpsd_unit = %d\n", vgl->realtime_gpsd_unit);
398 vgl->realtime_track = NULL;
400 /* Setting params here */
401 vgl->protocol_id = 0;
402 vgl->serial_port_id = 0;
403 vgl->gpsd_host = g_strdup("localhost");
404 vgl->gpsd_port = g_strdup(DEFAULT_GPSD_PORT);
405 vgl->realtime_record = TRUE;
406 vgl->realtime_jump_to_start = TRUE;
407 vgl->realtime_keep_at_center = FALSE;
409 return vgl;
412 static void vik_gps_layer_draw ( VikGpsLayer *vgl, gpointer data )
414 gint i;
415 VikLayer *vl;
416 VikLayer *trigger = VIK_LAYER(vik_viewport_get_trigger( VIK_VIEWPORT(data) ));
418 for (i = 0; i < NUM_TRW; i++) {
419 vl = VIK_LAYER(vgl->trw_children[i]);
420 if (vl == trigger) {
421 if ( vik_viewport_get_half_drawn ( VIK_VIEWPORT(data) ) ) {
422 vik_viewport_set_half_drawn ( VIK_VIEWPORT(data), FALSE );
423 vik_viewport_snapshot_load( VIK_VIEWPORT(data) );
424 } else {
425 vik_viewport_snapshot_save( VIK_VIEWPORT(data) );
428 if (!vik_viewport_get_half_drawn( VIK_VIEWPORT(data)))
429 vik_layer_draw ( vl, data );
431 if (vgl->realtime_tracking) {
432 if (VIK_LAYER(vgl) == trigger) {
433 if ( vik_viewport_get_half_drawn ( VIK_VIEWPORT(data) ) ) {
434 vik_viewport_set_half_drawn ( VIK_VIEWPORT(data), FALSE );
435 vik_viewport_snapshot_load( VIK_VIEWPORT(data) );
436 } else {
437 vik_viewport_snapshot_save( VIK_VIEWPORT(data) );
440 if (!vik_viewport_get_half_drawn( VIK_VIEWPORT(data)))
441 realtime_tracking_draw(vgl, VIK_VIEWPORT(data));
445 static void gps_layer_change_coord_mode ( VikGpsLayer *vgl, VikCoordMode mode )
447 gint i;
448 for (i = 0; i < NUM_TRW; i++) {
449 vik_layer_change_coord_mode(VIK_LAYER(vgl->trw_children[i]), mode);
453 static void gps_layer_add_menu_items( VikGpsLayer *vgl, GtkMenu *menu, gpointer vlp )
455 static gpointer pass_along[2];
456 GtkWidget *item;
457 pass_along[0] = vgl;
458 pass_along[1] = vlp;
460 item = gtk_menu_item_new();
461 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
462 gtk_widget_show ( item );
464 item = gtk_menu_item_new_with_label ( "Upload to GPS" );
465 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(gps_upload_cb), pass_along );
466 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
467 gtk_widget_show ( item );
469 item = gtk_menu_item_new_with_label ( "Download from GPS" );
470 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(gps_download_cb), pass_along );
471 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
472 gtk_widget_show ( item );
474 item = gtk_menu_item_new_with_label ( vgl->realtime_tracking ?
475 "Stop realtime tracking" :
476 "Start realtime tracking" );
477 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(gps_start_stop_tracking_cb), pass_along );
478 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
479 gtk_widget_show ( item );
481 item = gtk_menu_item_new();
482 gtk_menu_shell_append ( GTK_MENU_SHELL(menu), item );
483 gtk_widget_show ( item );
485 item = gtk_menu_item_new_with_label ( "Empty Upload" );
486 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(gps_empty_upload_cb), pass_along );
487 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
488 gtk_widget_show ( item );
490 item = gtk_menu_item_new_with_label ( "Empty Download" );
491 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(gps_empty_download_cb), pass_along );
492 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
493 gtk_widget_show ( item );
495 item = gtk_menu_item_new_with_label ( "Empty All" );
496 g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(gps_empty_all_cb), pass_along );
497 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
498 gtk_widget_show ( item );
502 static void disconnect_layer_signal ( VikLayer *vl, VikGpsLayer *vgl )
504 g_assert(DISCONNECT_UPDATE_SIGNAL(vl,vgl)==1);
507 static void vik_gps_layer_free ( VikGpsLayer *vgl )
509 gint i;
510 for (i = 0; i < NUM_TRW; i++) {
511 if (vgl->vl.realized)
512 disconnect_layer_signal(VIK_LAYER(vgl->trw_children[i]), vgl);
513 g_object_unref(vgl->trw_children[i]);
515 if (vgl->realtime_track_gc != NULL)
516 g_object_unref(vgl->realtime_track_gc);
517 if (vgl->realtime_track_bg_gc != NULL)
518 g_object_unref(vgl->realtime_track_bg_gc);
519 if (vgl->realtime_track_pt1_gc != NULL)
520 g_object_unref(vgl->realtime_track_pt1_gc);
521 if (vgl->realtime_track_pt2_gc != NULL)
522 g_object_unref(vgl->realtime_track_pt2_gc);
523 if (vgl->realtime_tracking_mutex != NULL)
524 g_mutex_free(vgl->realtime_tracking_mutex);
527 gboolean vik_gps_layer_delete ( VikGpsLayer *vgl, GtkTreeIter *iter )
529 gint i;
530 VikLayer *l = VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(vgl)->vt, iter ) );
531 gboolean was_visible = l->visible;
533 vik_treeview_item_delete ( VIK_LAYER(vgl)->vt, iter );
534 for (i = 0; i < NUM_TRW; i++) {
535 if (VIK_LAYER(vgl->trw_children[i]) == l)
536 vgl->trw_children[i] = NULL;
538 g_assert(DISCONNECT_UPDATE_SIGNAL(l,vgl)==1);
539 g_object_unref ( l );
541 return was_visible;
544 static void vik_gps_layer_realize ( VikGpsLayer *vgl, VikTreeview *vt, GtkTreeIter *layer_iter )
546 GtkTreeIter iter;
547 int ix;
549 for (ix = 0; ix < NUM_TRW; ix++) {
550 VikLayer * trw = VIK_LAYER(vgl->trw_children[ix]);
551 vik_treeview_add_layer ( VIK_LAYER(vgl)->vt, layer_iter, &iter,
552 trw_names[ix], vgl,
553 trw, trw->type, trw->type );
554 if ( ! trw->visible )
555 vik_treeview_item_set_visible ( VIK_LAYER(vgl)->vt, &iter, FALSE );
556 vik_layer_realize ( trw, VIK_LAYER(vgl)->vt, &iter );
557 g_signal_connect_swapped ( G_OBJECT(trw), "update", G_CALLBACK(vik_layer_emit_update_secondary), vgl );
561 const GList *vik_gps_layer_get_children ( VikGpsLayer *vgl )
563 int i;
565 if (vgl->children == NULL) {
566 for (i = NUM_TRW - 1; i >= 0; i--)
567 vgl->children = g_list_prepend(vgl->children, vgl->trw_children[i]);
569 return vgl->children;
572 VikTrwLayer * vik_gps_layer_get_a_child(VikGpsLayer *vgl)
574 g_assert ((vgl->cur_read_child >= 0) && (vgl->cur_read_child < NUM_TRW));
576 VikTrwLayer * vtl = vgl->trw_children[vgl->cur_read_child];
577 if (++(vgl->cur_read_child) >= NUM_TRW)
578 vgl->cur_read_child = 0;
579 return(vtl);
582 gboolean vik_gps_layer_is_empty ( VikGpsLayer *vgl )
584 if ( vgl->trw_children[0] )
585 return FALSE;
586 return TRUE;
589 static void gps_layer_drag_drop_request ( VikGpsLayer *val_src, VikGpsLayer *val_dest, GtkTreeIter *src_item_iter, GtkTreePath *dest_path )
591 VikTreeview *vt = VIK_LAYER(val_src)->vt;
592 VikLayer *vl = vik_treeview_item_get_pointer(vt, src_item_iter);
593 GtkTreeIter dest_iter;
594 gchar *dp;
595 gboolean target_exists;
597 dp = gtk_tree_path_to_string(dest_path);
598 target_exists = vik_treeview_get_iter_from_path_str(vt, &dest_iter, dp);
600 /* vik_gps_layer_delete unrefs, but we don't want that here.
601 * we're still using the layer. */
602 g_object_ref ( vl );
603 vik_gps_layer_delete(val_src, src_item_iter);
605 g_free(dp);
608 static void gps_session_delete(GpsSession *sess)
610 /* TODO */
611 g_mutex_free(sess->mutex);
612 g_free(sess->cmd_args);
614 g_free(sess);
618 static void set_total_count(gint cnt, GpsSession *sess)
620 gchar s[128];
621 gdk_threads_enter();
622 g_mutex_lock(sess->mutex);
623 if (sess->ok) {
624 g_sprintf(s, "%s %d %s...",
625 (sess->direction == GPS_DOWN) ? "Downloading" : "Uploading", cnt,
626 (sess->progress_label == sess->wp_label) ? "waypoints" : "trackpoints");
627 gtk_label_set_text ( GTK_LABEL(sess->progress_label), s );
628 gtk_widget_show ( sess->progress_label );
629 sess->total_count = cnt;
631 g_mutex_unlock(sess->mutex);
632 gdk_threads_leave();
635 static void set_current_count(gint cnt, GpsSession *sess)
637 gchar s[128];
638 gchar *dir_str = (sess->direction == GPS_DOWN) ? "Downloaded" : "Uploaded";
640 gdk_threads_enter();
641 g_mutex_lock(sess->mutex);
642 if (sess->ok) {
643 if (cnt < sess->total_count) {
644 g_sprintf(s, "%s %d out of %d %s...", dir_str, cnt, sess->total_count, (sess->progress_label == sess->wp_label) ? "waypoints" : "trackpoints");
645 } else {
646 g_sprintf(s, "%s %d %s.", dir_str, cnt, (sess->progress_label == sess->wp_label) ? "waypoints" : "trackpoints");
648 gtk_label_set_text ( GTK_LABEL(sess->progress_label), s );
650 g_mutex_unlock(sess->mutex);
651 gdk_threads_leave();
654 static void set_gps_info(const gchar *info, GpsSession *sess)
656 gchar s[256];
657 gdk_threads_enter();
658 g_mutex_lock(sess->mutex);
659 if (sess->ok) {
660 g_sprintf(s, "GPS Device: %s", info);
661 gtk_label_set_text ( GTK_LABEL(sess->gps_label), s );
663 g_mutex_unlock(sess->mutex);
664 gdk_threads_leave();
667 static void gps_download_progress_func(BabelProgressCode c, gpointer data, GpsSession * sess )
669 gchar *line;
671 gdk_threads_enter ();
672 g_mutex_lock(sess->mutex);
673 if (!sess->ok) {
674 g_mutex_unlock(sess->mutex);
675 gps_session_delete(sess);
676 gdk_threads_leave();
677 g_thread_exit ( NULL );
679 g_mutex_unlock(sess->mutex);
680 gdk_threads_leave ();
682 switch(c) {
683 case BABEL_DIAG_OUTPUT:
684 line = (gchar *)data;
686 /* tells us how many items there will be */
687 if (strstr(line, "Xfer Wpt")) {
688 sess->progress_label = sess->wp_label;
690 if (strstr(line, "Xfer Trk")) {
691 sess->progress_label = sess->trk_label;
693 if (strstr(line, "PRDDAT")) {
694 gchar **tokens = g_strsplit(line, " ", 0);
695 gchar info[128];
696 int ilen = 0;
697 int i;
698 int n_tokens = 0;
700 while (tokens[n_tokens])
701 n_tokens++;
703 if (n_tokens > 8) {
704 for (i=8; tokens[i] && ilen < sizeof(info)-2 && strcmp(tokens[i], "00"); i++) {
705 guint ch;
706 sscanf(tokens[i], "%x", &ch);
707 info[ilen++] = ch;
709 info[ilen++] = 0;
710 set_gps_info(info, sess);
712 g_strfreev(tokens);
714 if (strstr(line, "RECORD")) {
715 int lsb, msb, cnt;
717 if (strlen(line) > 20) {
718 sscanf(line+17, "%x", &lsb);
719 sscanf(line+20, "%x", &msb);
720 cnt = lsb + msb * 256;
721 set_total_count(cnt, sess);
722 sess->count = 0;
725 if ( strstr(line, "WPTDAT") || strstr(line, "TRKHDR") || strstr(line, "TRKDAT") ) {
726 sess->count++;
727 set_current_count(sess->count, sess);
729 break;
730 case BABEL_DONE:
731 break;
732 default:
733 break;
738 static void gps_upload_progress_func(BabelProgressCode c, gpointer data, GpsSession * sess )
740 gchar *line;
741 static int cnt = 0;
743 gdk_threads_enter ();
744 g_mutex_lock(sess->mutex);
745 if (!sess->ok) {
746 g_mutex_unlock(sess->mutex);
747 gps_session_delete(sess);
748 gdk_threads_leave();
749 g_thread_exit ( NULL );
751 g_mutex_unlock(sess->mutex);
752 gdk_threads_leave ();
754 switch(c) {
755 case BABEL_DIAG_OUTPUT:
756 line = (gchar *)data;
758 if (strstr(line, "PRDDAT")) {
759 gchar **tokens = g_strsplit(line, " ", 0);
760 gchar info[128];
761 int ilen = 0;
762 int i;
763 int n_tokens = 0;
765 while (tokens[n_tokens])
766 n_tokens++;
768 if (n_tokens > 8) {
769 for (i=8; tokens[i] && ilen < sizeof(info)-2 && strcmp(tokens[i], "00"); i++) {
770 guint ch;
771 sscanf(tokens[i], "%x", &ch);
772 info[ilen++] = ch;
774 info[ilen++] = 0;
775 set_gps_info(info, sess);
777 g_strfreev(tokens);
779 if (strstr(line, "RECORD")) {
780 int lsb, msb;
782 if (strlen(line) > 20) {
783 sscanf(line+17, "%x", &lsb);
784 sscanf(line+20, "%x", &msb);
785 cnt = lsb + msb * 256;
786 /* set_total_count(cnt, sess); */
787 sess->count = 0;
790 if ( strstr(line, "WPTDAT")) {
791 if (sess->count == 0) {
792 sess->progress_label = sess->wp_label;
793 set_total_count(cnt, sess);
795 sess->count++;
796 set_current_count(sess->count, sess);
799 if ( strstr(line, "TRKHDR") || strstr(line, "TRKDAT") ) {
800 if (sess->count == 0) {
801 sess->progress_label = sess->trk_label;
802 set_total_count(cnt, sess);
804 sess->count++;
805 set_current_count(sess->count, sess);
807 break;
808 case BABEL_DONE:
809 break;
810 default:
811 break;
816 static void gps_comm_thread(GpsSession *sess)
818 gboolean result;
820 if (sess->direction == GPS_DOWN)
821 result = a_babel_convert_from (sess->vtl, sess->cmd_args,
822 (BabelStatusFunc) gps_download_progress_func, sess->port, sess);
823 else
824 result = a_babel_convert_to (sess->vtl, sess->cmd_args,
825 (BabelStatusFunc) gps_upload_progress_func, sess->port, sess);
827 gdk_threads_enter();
828 if (!result) {
829 gtk_label_set_text ( GTK_LABEL(sess->status_label), "Error: couldn't find gpsbabel." );
831 else {
832 g_mutex_lock(sess->mutex);
833 if (sess->ok) {
834 gtk_label_set_text ( GTK_LABEL(sess->status_label), "Done." );
835 gtk_dialog_set_response_sensitive ( GTK_DIALOG(sess->dialog), GTK_RESPONSE_ACCEPT, TRUE );
836 gtk_dialog_set_response_sensitive ( GTK_DIALOG(sess->dialog), GTK_RESPONSE_REJECT, FALSE );
837 } else {
838 /* canceled */
840 g_mutex_unlock(sess->mutex);
843 g_mutex_lock(sess->mutex);
844 if (sess->ok) {
845 sess->ok = FALSE;
846 g_mutex_unlock(sess->mutex);
848 else {
849 g_mutex_unlock(sess->mutex);
850 gps_session_delete(sess);
852 gdk_threads_leave();
853 g_thread_exit(NULL);
856 static gint gps_comm(VikTrwLayer *vtl, gps_dir dir, vik_gps_proto proto, gchar *port) {
857 GpsSession *sess = g_malloc(sizeof(GpsSession));
859 sess->mutex = g_mutex_new();
860 sess->direction = dir;
861 sess->vtl = vtl;
862 sess->port = g_strdup(port);
863 sess->ok = TRUE;
864 sess->cmd_args = g_strdup_printf("-D 9 -t -w -%c %s",
865 (dir == GPS_DOWN) ? 'i' : 'o', protocols_args[proto]);
866 sess->window_title = (dir == GPS_DOWN) ? "GPS Download" : "GPS Upload";
868 sess->dialog = gtk_dialog_new_with_buttons ( "", VIK_GTK_WINDOW_FROM_LAYER(vtl), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
869 gtk_dialog_set_response_sensitive ( GTK_DIALOG(sess->dialog),
870 GTK_RESPONSE_ACCEPT, FALSE );
871 gtk_window_set_title ( GTK_WINDOW(sess->dialog), sess->window_title );
873 sess->status_label = gtk_label_new ("Status: detecting gpsbabel");
874 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(sess->dialog)->vbox),
875 sess->status_label, FALSE, FALSE, 5 );
876 gtk_widget_show_all(sess->status_label);
878 sess->gps_label = gtk_label_new ("GPS device: N/A");
879 sess->ver_label = gtk_label_new ("");
880 sess->id_label = gtk_label_new ("");
881 sess->wp_label = gtk_label_new ("");
882 sess->trk_label = gtk_label_new ("");
884 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(sess->dialog)->vbox), sess->gps_label, FALSE, FALSE, 5 );
885 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(sess->dialog)->vbox), sess->wp_label, FALSE, FALSE, 5 );
886 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(sess->dialog)->vbox), sess->trk_label, FALSE, FALSE, 5 );
888 gtk_widget_show_all(sess->dialog);
890 sess->progress_label = sess->wp_label;
891 sess->total_count = -1;
893 /* TODO: starting gps read/write thread here */
894 g_thread_create((GThreadFunc)gps_comm_thread, sess, FALSE, NULL );
896 gtk_dialog_run(GTK_DIALOG(sess->dialog));
898 gtk_widget_destroy(sess->dialog);
900 g_mutex_lock(sess->mutex);
901 if (sess->ok) {
902 sess->ok = FALSE; /* tell thread to stop */
903 g_mutex_unlock(sess->mutex);
905 else {
906 g_mutex_unlock(sess->mutex);
907 gps_session_delete(sess);
910 return 0;
913 static void gps_upload_cb( gpointer layer_and_vlp[2] )
915 VikGpsLayer *vgl = (VikGpsLayer *)layer_and_vlp[0];
916 VikTrwLayer *vtl = vgl->trw_children[TRW_UPLOAD];
917 gps_comm(vtl, GPS_UP, vgl->protocol_id, params_ports[vgl->serial_port_id]);
920 static void gps_download_cb( gpointer layer_and_vlp[2] )
922 VikGpsLayer *vgl = (VikGpsLayer *)layer_and_vlp[0];
923 VikTrwLayer *vtl = vgl->trw_children[TRW_DOWNLOAD];
924 gps_comm(vtl, GPS_DOWN, vgl->protocol_id, params_ports[vgl->serial_port_id]);
927 static void gps_empty_upload_cb( gpointer layer_and_vlp[2] )
929 VikGpsLayer *vgl = (VikGpsLayer *)layer_and_vlp[0];
930 vik_trw_layer_delete_all_waypoints ( vgl-> trw_children[TRW_UPLOAD]);
931 vik_trw_layer_delete_all_tracks ( vgl-> trw_children[TRW_UPLOAD]);
934 static void gps_empty_download_cb( gpointer layer_and_vlp[2] )
936 VikGpsLayer *vgl = (VikGpsLayer *)layer_and_vlp[0];
937 vik_trw_layer_delete_all_waypoints ( vgl-> trw_children[TRW_DOWNLOAD]);
938 vik_trw_layer_delete_all_tracks ( vgl-> trw_children[TRW_DOWNLOAD]);
941 static void gps_empty_all_cb( gpointer layer_and_vlp[2] )
943 VikGpsLayer *vgl = (VikGpsLayer *)layer_and_vlp[0];
944 vik_trw_layer_delete_all_waypoints ( vgl-> trw_children[TRW_UPLOAD]);
945 vik_trw_layer_delete_all_tracks ( vgl-> trw_children[TRW_UPLOAD]);
946 vik_trw_layer_delete_all_waypoints ( vgl-> trw_children[TRW_DOWNLOAD]);
947 vik_trw_layer_delete_all_tracks ( vgl-> trw_children[TRW_DOWNLOAD]);
950 static void realtime_tracking_draw(VikGpsLayer *vgl, VikViewport *vp)
952 struct LatLon ll;
953 VikCoord nw, se;
954 struct LatLon lnw, lse;
955 vik_viewport_screen_to_coord ( vp, -20, -20, &nw );
956 vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp)+20, vik_viewport_get_width(vp)+20, &se );
957 vik_coord_to_latlon ( &nw, &lnw );
958 vik_coord_to_latlon ( &se, &lse );
959 if ( vgl->realtime_fix.fix.latitude > lse.lat &&
960 vgl->realtime_fix.fix.latitude < lnw.lat &&
961 vgl->realtime_fix.fix.longitude > lnw.lon &&
962 vgl->realtime_fix.fix.longitude < lse.lon ) {
963 VikCoord gps;
964 gint x, y;
965 gint half_back_x, half_back_y;
966 gint half_back_bg_x, half_back_bg_y;
967 gint pt_x, pt_y;
968 gint ptbg_x, ptbg_y;
969 gint side1_x, side1_y, side2_x, side2_y;
970 gint side1bg_x, side1bg_y, side2bg_x, side2bg_y;
972 ll.lat = vgl->realtime_fix.fix.latitude;
973 ll.lon = vgl->realtime_fix.fix.longitude;
974 vik_coord_load_from_latlon ( &gps, vik_viewport_get_coord_mode(vp), &ll);
975 vik_viewport_coord_to_screen ( vp, &gps, &x, &y );
977 gdouble heading_cos = cos(M_PI/180*vgl->realtime_fix.fix.track);
978 gdouble heading_sin = sin(M_PI/180*vgl->realtime_fix.fix.track);
980 half_back_y = y+8*heading_cos;
981 half_back_x = x-8*heading_sin;
982 half_back_bg_y = y+10*heading_cos;
983 half_back_bg_x = x-10*heading_sin;
985 pt_y = half_back_y-24*heading_cos;
986 pt_x = half_back_x+24*heading_sin;
987 ptbg_y = half_back_bg_y-28*heading_cos;
988 ptbg_x = half_back_bg_x+28*heading_sin;
990 side1_y = half_back_y+9*heading_sin;
991 side1_x = half_back_x+9*heading_cos;
992 side1bg_y = half_back_bg_y+11*heading_sin;
993 side1bg_x = half_back_bg_x+11*heading_cos;
995 side2_y = half_back_y-9*heading_sin;
996 side2_x = half_back_x-9*heading_cos;
997 side2bg_y = half_back_bg_y-11*heading_sin;
998 side2bg_x = half_back_bg_x-11*heading_cos;
1000 GdkPoint trian[3] = { { pt_x, pt_y }, {side1_x, side1_y}, {side2_x, side2_y} };
1001 GdkPoint trian_bg[3] = { { ptbg_x, pt_y }, {side1bg_x, side1bg_y}, {side2bg_x, side2bg_y} };
1003 vik_viewport_draw_polygon ( vp, vgl->realtime_track_bg_gc, TRUE, trian_bg, 3 );
1004 vik_viewport_draw_polygon ( vp, vgl->realtime_track_gc, TRUE, trian, 3 );
1005 vik_viewport_draw_rectangle ( vp,
1006 (vgl->realtime_fix.fix.mode > MODE_2D) ? vgl->realtime_track_pt2_gc : vgl->realtime_track_pt1_gc,
1007 TRUE, x-2, y-2, 4, 4 );
1008 //vgl->realtime_track_pt_gc = (vgl->realtime_track_pt_gc == vgl->realtime_track_pt1_gc) ? vgl->realtime_track_pt2_gc : vgl->realtime_track_pt1_gc;
1012 /* lock/unlock realtime_tracking_mutex when call this */
1013 static void create_realtime_trackpoint(VikGpsLayer *vgl, gboolean forced)
1015 struct LatLon ll;
1016 GList *last_tp;
1018 /* Note that fix.time is a double, but it should not affect the precision
1019 for most GPS */
1020 time_t cur_timestamp = vgl->realtime_fix.fix.time;
1021 time_t last_timestamp = vgl->last_fix.fix.time;
1023 if (cur_timestamp < last_timestamp) {
1024 return;
1027 if (vgl->realtime_record && vgl->realtime_fix.dirty) {
1028 gboolean replace = FALSE;
1029 int heading = (int)floor(vgl->realtime_fix.fix.track);
1030 int last_heading = (int)floor(vgl->last_fix.fix.track);
1031 int alt = isnan(vgl->realtime_fix.fix.altitude) ? VIK_DEFAULT_ALTITUDE : floor(vgl->realtime_fix.fix.altitude);
1032 int last_alt = isnan(vgl->last_fix.fix.altitude) ? VIK_DEFAULT_ALTITUDE : floor(vgl->last_fix.fix.altitude);
1033 if (((last_tp = g_list_last(vgl->realtime_track->trackpoints)) != NULL) &&
1034 (vgl->realtime_fix.fix.mode > MODE_2D) &&
1035 (vgl->last_fix.fix.mode <= MODE_2D) &&
1036 ((cur_timestamp - last_timestamp) < 2)) {
1037 g_free(last_tp->data);
1038 vgl->realtime_track->trackpoints = g_list_delete_link(vgl->realtime_track->trackpoints, last_tp);
1039 replace = TRUE;
1041 if (replace ||
1042 ((cur_timestamp != last_timestamp) &&
1043 ((forced ||
1044 ((heading < last_heading) && (heading < (last_heading - 3))) ||
1045 ((heading > last_heading) && (heading > (last_heading + 3))) ||
1046 ((alt != VIK_DEFAULT_ALTITUDE) && (alt != last_alt)))))) {
1047 /* TODO: check for new segments */
1048 VikTrackpoint *tp = vik_trackpoint_new();
1049 tp->newsegment = FALSE;
1050 // tp->altitude = isnan(vgl->realtime_fix.fix.altitude) ? VIK_DEFAULT_ALTITUDE : vgl->realtime_fix.fix.altitude;
1051 tp->altitude = alt;
1052 tp->has_timestamp = TRUE;
1053 tp->timestamp = vgl->realtime_fix.fix.time;
1055 ll.lat = vgl->realtime_fix.fix.latitude;
1056 ll.lon = vgl->realtime_fix.fix.longitude;
1057 vik_coord_load_from_latlon(&tp->coord,
1058 vik_trw_layer_get_coord_mode(vgl->trw_children[TRW_REALTIME]), &ll);
1060 vgl->realtime_track->trackpoints = g_list_append(vgl->realtime_track->trackpoints, tp);
1061 vgl->realtime_fix.dirty = FALSE;
1062 vgl->last_fix = vgl->realtime_fix;
1068 void gpsd_raw_hook(VglGpsd *vgpsd, gchar *data)
1070 gboolean update_all = FALSE;
1071 VikGpsLayer *vgl = vgpsd->vgl;
1073 if (!vgl->realtime_tracking) {
1074 g_warning("%s: receiving GPS data while not in realtime mode", __PRETTY_FUNCTION__);
1075 return;
1078 #ifdef XXXXXXXXXXXXX
1079 if ((time_t)vgl->realtime_fix.fix.time == (time_t)vgpsd->gpsd.fix.time)
1080 return;
1081 #endif /*XXXXXXXXXXXX*/
1083 if ((vgpsd->gpsd.fix.mode >= MODE_2D) &&
1084 !isnan(vgpsd->gpsd.fix.latitude) &&
1085 !isnan(vgpsd->gpsd.fix.longitude) &&
1086 !isnan(vgpsd->gpsd.fix.track)) {
1087 g_mutex_lock(vgl->realtime_tracking_mutex);
1088 vgl->realtime_fix.fix = vgpsd->gpsd.fix;
1089 vgl->realtime_fix.dirty = TRUE;
1091 if (vgl->realtime_keep_at_center ||
1092 (vgl->realtime_jump_to_start && !vgl->realtime_track->trackpoints)) {
1093 struct LatLon ll;
1094 VikCoord center;
1096 ll.lat = vgl->realtime_fix.fix.latitude;
1097 ll.lon = vgl->realtime_fix.fix.longitude;
1098 vik_coord_load_from_latlon(&center,
1099 vik_trw_layer_get_coord_mode(vgl->trw_children[TRW_REALTIME]), &ll);
1100 VikWindow *vw = VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vgl));
1101 VikViewport *vvp = vik_window_viewport(vw);
1102 vik_viewport_set_center_coord(vvp, &center);
1103 update_all = TRUE;
1106 create_realtime_trackpoint(vgl, FALSE);
1107 g_mutex_unlock(vgl->realtime_tracking_mutex);
1109 vik_layer_emit_update ( update_all ? VIK_LAYER(vgl) : VIK_LAYER(vgl->trw_children[TRW_REALTIME]));
1113 static gboolean gpsd_data_available(GIOChannel *source, GIOCondition condition, gpointer data)
1115 VikGpsLayer *vgl = data;
1116 gps_poll(&vgl->vgpsd->gpsd);
1117 return TRUE;
1120 static gchar *make_track_name(VikTrwLayer *vtl)
1122 const gchar basename[] = "REALTIME";
1123 const gint bufsize = sizeof(basename) + 5;
1124 gchar *name = g_malloc(bufsize);
1125 strcpy(name, basename);
1126 gint i = 2;
1128 while (vik_trw_layer_get_track(vtl, name) != NULL) {
1129 g_snprintf(name, bufsize, "%s#%d", basename, i);
1130 i++;
1132 return(name);
1136 static void gps_start_stop_tracking_cb( gpointer layer_and_vlp[2])
1138 VikGpsLayer *vgl = (VikGpsLayer *)layer_and_vlp[0];
1139 vgl->realtime_tracking = (vgl->realtime_tracking == FALSE);
1141 if (vgl->realtime_tracking) {
1142 struct gps_data_t *gpsd = gps_open(vgl->gpsd_host, vgl->gpsd_port);
1143 if (gpsd == NULL) {
1144 GtkWidget *dialog = gtk_message_dialog_new (VIK_GTK_WINDOW_FROM_LAYER(vgl),
1145 GTK_DIALOG_DESTROY_WITH_PARENT,
1146 GTK_MESSAGE_ERROR,
1147 GTK_BUTTONS_CLOSE,
1148 "Failed to connect to gpsd at %s (port %s)",
1149 vgl->gpsd_host, vgl->gpsd_port);
1150 gtk_dialog_run (GTK_DIALOG (dialog));
1151 gtk_widget_destroy (dialog);
1153 vgl->realtime_tracking = FALSE;
1154 return;
1156 vgl->vgpsd = g_realloc(gpsd, sizeof(VglGpsd));
1157 vgl->vgpsd->vgl = vgl;
1159 vgl->realtime_fix.dirty = vgl->last_fix.dirty = FALSE;
1160 /* track alt/time graph uses VIK_DEFAULT_ALTITUDE (0.0) as invalid */
1161 vgl->realtime_fix.fix.altitude = vgl->last_fix.fix.altitude = VIK_DEFAULT_ALTITUDE;
1162 vgl->realtime_fix.fix.speed = vgl->last_fix.fix.speed = NAN;
1164 if (vgl->realtime_record) {
1165 VikTrwLayer *vtl = vgl->trw_children[TRW_REALTIME];
1166 vgl->realtime_track = vik_track_new();
1167 vgl->realtime_track->visible = TRUE;
1168 vgl->realtime_track_name = make_track_name(vtl);
1169 vik_trw_layer_add_track(vtl, vgl->realtime_track_name, vgl->realtime_track);
1172 gps_set_raw_hook(&vgl->vgpsd->gpsd, gpsd_raw_hook);
1173 vgl->realtime_io_channel = g_io_channel_unix_new(vgl->vgpsd->gpsd.gps_fd);
1174 vgl->realtime_io_watch_id = g_io_add_watch( vgl->realtime_io_channel,
1175 G_IO_IN, gpsd_data_available, vgl);
1177 gps_query(&vgl->vgpsd->gpsd, "w+x");
1180 else { /* stop realtime tracking */
1181 /* TODO: handle race condition here , make sure vgpsd is NULL */
1182 g_mutex_lock(vgl->realtime_tracking_mutex);
1183 gps_close(&vgl->vgpsd->gpsd);
1184 vgl->vgpsd = NULL;
1185 // g_source_remove(vgl->realtime_timeout);
1186 g_source_remove(vgl->realtime_io_watch_id);
1187 g_io_channel_unref (vgl->realtime_io_channel);
1189 if (vgl->realtime_record) {
1190 create_realtime_trackpoint(vgl, TRUE);
1191 if ((vgl->realtime_track->trackpoints == NULL) || (vgl->realtime_track->trackpoints->next == NULL))
1192 vik_trw_layer_delete_track(vgl->trw_children[TRW_REALTIME], vgl->realtime_track_name);
1194 g_mutex_unlock(vgl->realtime_tracking_mutex);