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
25 #include "vikgpslayer_pixmap.h"
30 #include <glib/gprintf.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
;
77 GtkWidget
*status_label
;
82 GtkWidget
*progress_label
;
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
= {
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"};
154 struct gps_data_t gpsd
;
159 struct gps_fix_t fix
;
160 gboolean dirty
; /* needs to be saved */
163 struct _VikGpsLayer
{
165 VikTrwLayer
* trw_children
[NUM_TRW
];
166 GList
* children
; /* used only for writing file */
167 int cur_read_child
; /* used only for reading file */
169 gboolean realtime_tracking
;
170 GMutex
*realtime_tracking_mutex
;
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
;
189 guint serial_port_id
;
190 /* TODO: show on diaglog */
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;
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 );
222 static VikGpsLayer
*vik_gps_layer_create (VikViewport
*vp
)
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
));
237 static void gps_layer_marshall( VikGpsLayer
*vgl
, guint8
**data
, gint
*datalen
)
239 VikLayer
*child_layer
;
242 GByteArray
* b
= g_byte_array_new ();
246 #define alm_append(obj, 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
);
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
);
265 g_byte_array_free(b
, FALSE
);
270 static VikGpsLayer
*gps_layer_unmarshall( guint8
*data
, gint len
, VikViewport
*vvp
)
272 #define alm_size (*(gint *)data)
274 len -= sizeof(gint) + alm_size; \
275 data += sizeof(gint) + alm_size;
277 VikGpsLayer
*rv
= vik_gps_layer_new();
278 VikLayer
*child_layer
;
281 vik_layer_unmarshall_params ( VIK_LAYER(rv
), data
+sizeof(gint
), alm_size
, vvp
);
285 while (len
>0 && i
< NUM_TRW
) {
286 child_layer
= vik_layer_unmarshall ( data
+ sizeof(gint
), alm_size
, vvp
);
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
);
293 // g_print("gps_layer_unmarshall ended with len=%d\n", len);
300 static gboolean
gps_layer_set_param ( VikGpsLayer
*vgl
, guint16 id
, VikLayerParamData data
, VikViewport
*vp
)
304 /* TODO: gpsd_host, gpsd_port */
306 if (data
.u
< NUM_PROTOCOLS
)
307 vgl
->protocol_id
= data
.u
;
309 g_warning("Unknown GPS Protocol");
312 if (data
.u
< NUM_PORTS
)
313 vgl
->serial_port_id
= data
.u
;
315 g_warning("Unknown serial port device");
317 case PARAM_GPSD_HOST
:
319 g_free(vgl
->gpsd_host
);
320 vgl
->gpsd_host
= g_strdup(data
.s
);
322 case PARAM_GPSD_PORT
:
324 g_free(vgl
->gpsd_port
);
325 vgl
->gpsd_port
= g_strdup(data
.s
); /* TODO: check for number */
327 case PARAM_REALTIME_REC
:
328 vgl
->realtime_record
= data
.b
;
330 case PARAM_REALTIME_CENTER_START
:
331 vgl
->realtime_jump_to_start
= data
.b
;
333 case PARAM_REALTIME_CENTERED
:
334 vgl
->realtime_keep_at_center
= data
.b
;
337 g_warning("gps_layer_set_param(): unknown parameter");
343 static VikLayerParamData
gps_layer_get_param ( VikGpsLayer
*vgl
, guint16 id
)
345 VikLayerParamData rv
;
348 /* TODO: gpsd_host, gpsd_port */
350 rv
.u
= vgl
->protocol_id
;
353 rv
.u
= vgl
->serial_port_id
;
355 case PARAM_GPSD_HOST
:
356 rv
.s
= vgl
->gpsd_host
? vgl
->gpsd_host
: "";
358 case PARAM_GPSD_PORT
:
359 rv
.s
= vgl
->gpsd_port
? vgl
->gpsd_port
: g_strdup(DEFAULT_GPSD_PORT
);
361 case PARAM_REALTIME_REC
:
362 rv
.b
= vgl
->realtime_record
;
364 case PARAM_REALTIME_CENTER_START
:
365 rv
.b
= vgl
->realtime_jump_to_start
;
367 case PARAM_REALTIME_CENTERED
:
368 rv
.b
= vgl
->realtime_keep_at_center
;
371 g_warning("gps_layer_get_param(): unknown parameter");
377 VikGpsLayer
*vik_gps_layer_new (VikViewport
*vp
)
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
;
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
;
412 static void vik_gps_layer_draw ( VikGpsLayer
*vgl
, gpointer data
)
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
]);
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
) );
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
) );
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
)
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];
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
)
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
)
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
);
544 static void vik_gps_layer_realize ( VikGpsLayer
*vgl
, VikTreeview
*vt
, GtkTreeIter
*layer_iter
)
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
,
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
)
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;
582 gboolean
vik_gps_layer_is_empty ( VikGpsLayer
*vgl
)
584 if ( vgl
->trw_children
[0] )
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
;
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. */
603 vik_gps_layer_delete(val_src
, src_item_iter
);
608 static void gps_session_delete(GpsSession
*sess
)
611 g_mutex_free(sess
->mutex
);
612 g_free(sess
->cmd_args
);
618 static void set_total_count(gint cnt
, GpsSession
*sess
)
622 g_mutex_lock(sess
->mutex
);
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
);
635 static void set_current_count(gint cnt
, GpsSession
*sess
)
638 gchar
*dir_str
= (sess
->direction
== GPS_DOWN
) ? "Downloaded" : "Uploaded";
641 g_mutex_lock(sess
->mutex
);
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");
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
);
654 static void set_gps_info(const gchar
*info
, GpsSession
*sess
)
658 g_mutex_lock(sess
->mutex
);
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
);
667 static void gps_download_progress_func(BabelProgressCode c
, gpointer data
, GpsSession
* sess
)
671 gdk_threads_enter ();
672 g_mutex_lock(sess
->mutex
);
674 g_mutex_unlock(sess
->mutex
);
675 gps_session_delete(sess
);
677 g_thread_exit ( NULL
);
679 g_mutex_unlock(sess
->mutex
);
680 gdk_threads_leave ();
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);
700 while (tokens
[n_tokens
])
704 for (i
=8; tokens
[i
] && ilen
< sizeof(info
)-2 && strcmp(tokens
[i
], "00"); i
++) {
706 sscanf(tokens
[i
], "%x", &ch
);
710 set_gps_info(info
, sess
);
714 if (strstr(line
, "RECORD")) {
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
);
725 if ( strstr(line
, "WPTDAT") || strstr(line
, "TRKHDR") || strstr(line
, "TRKDAT") ) {
727 set_current_count(sess
->count
, sess
);
738 static void gps_upload_progress_func(BabelProgressCode c
, gpointer data
, GpsSession
* sess
)
743 gdk_threads_enter ();
744 g_mutex_lock(sess
->mutex
);
746 g_mutex_unlock(sess
->mutex
);
747 gps_session_delete(sess
);
749 g_thread_exit ( NULL
);
751 g_mutex_unlock(sess
->mutex
);
752 gdk_threads_leave ();
755 case BABEL_DIAG_OUTPUT
:
756 line
= (gchar
*)data
;
758 if (strstr(line
, "PRDDAT")) {
759 gchar
**tokens
= g_strsplit(line
, " ", 0);
765 while (tokens
[n_tokens
])
769 for (i
=8; tokens
[i
] && ilen
< sizeof(info
)-2 && strcmp(tokens
[i
], "00"); i
++) {
771 sscanf(tokens
[i
], "%x", &ch
);
775 set_gps_info(info
, sess
);
779 if (strstr(line
, "RECORD")) {
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); */
790 if ( strstr(line
, "WPTDAT")) {
791 if (sess
->count
== 0) {
792 sess
->progress_label
= sess
->wp_label
;
793 set_total_count(cnt
, sess
);
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
);
805 set_current_count(sess
->count
, sess
);
816 static void gps_comm_thread(GpsSession
*sess
)
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
);
824 result
= a_babel_convert_to (sess
->vtl
, sess
->cmd_args
,
825 (BabelStatusFunc
) gps_upload_progress_func
, sess
->port
, sess
);
829 gtk_label_set_text ( GTK_LABEL(sess
->status_label
), "Error: couldn't find gpsbabel." );
832 g_mutex_lock(sess
->mutex
);
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
);
840 g_mutex_unlock(sess
->mutex
);
843 g_mutex_lock(sess
->mutex
);
846 g_mutex_unlock(sess
->mutex
);
849 g_mutex_unlock(sess
->mutex
);
850 gps_session_delete(sess
);
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
;
862 sess
->port
= g_strdup(port
);
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
);
902 sess
->ok
= FALSE
; /* tell thread to stop */
903 g_mutex_unlock(sess
->mutex
);
906 g_mutex_unlock(sess
->mutex
);
907 gps_session_delete(sess
);
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
)
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
) {
965 gint half_back_x
, half_back_y
;
966 gint half_back_bg_x
, half_back_bg_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
)
1018 /* Note that fix.time is a double, but it should not affect the precision
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
) {
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
);
1042 ((cur_timestamp
!= last_timestamp
) &&
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;
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__
);
1078 #ifdef XXXXXXXXXXXXX
1079 if ((time_t)vgl
->realtime_fix
.fix
.time
== (time_t)vgpsd
->gpsd
.fix
.time
)
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
)) {
1096 ll
.lat
= vgl
->realtime_fix
.fix
.latitude
;
1097 ll
.lon
= vgl
->realtime_fix
.fix
.longitude
;
1098 vik_coord_load_from_latlon(¢er
,
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
, ¢er
);
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
);
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
);
1128 while (vik_trw_layer_get_track(vtl
, name
) != NULL
) {
1129 g_snprintf(name
, bufsize
, "%s#%d", basename
, i
);
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
);
1144 GtkWidget
*dialog
= gtk_message_dialog_new (VIK_GTK_WINDOW_FROM_LAYER(vgl
),
1145 GTK_DIALOG_DESTROY_WITH_PARENT
,
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
;
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
);
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
);