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
31 #include "icons/icons.h"
38 #include <glib/gprintf.h>
39 #include <glib/gi18n.h>
40 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
44 #if ! GLIB_CHECK_VERSION(2,14,0)
45 inline guint
g_timeout_add_seconds(guint interval
, GSourceFunc function
, gpointer data
) {
46 return g_timeout_add(interval
*1000, function
, data
);
50 #define DISCONNECT_UPDATE_SIGNAL(vl, val) g_signal_handlers_disconnect_matched(vl, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, val)
51 static VikGpsLayer
*vik_gps_layer_create (VikViewport
*vp
);
52 static void vik_gps_layer_realize ( VikGpsLayer
*val
, VikTreeview
*vt
, GtkTreeIter
*layer_iter
);
53 static void vik_gps_layer_free ( VikGpsLayer
*val
);
54 static void vik_gps_layer_draw ( VikGpsLayer
*val
, gpointer data
);
55 VikGpsLayer
*vik_gps_layer_new ();
57 static void gps_layer_marshall( VikGpsLayer
*val
, guint8
**data
, gint
*len
);
58 static VikGpsLayer
*gps_layer_unmarshall( guint8
*data
, gint len
, VikViewport
*vvp
);
59 static gboolean
gps_layer_set_param ( VikGpsLayer
*vgl
, guint16 id
, VikLayerParamData data
, VikViewport
*vp
);
60 static VikLayerParamData
gps_layer_get_param ( VikGpsLayer
*vgl
, guint16 id
);
62 static void gps_layer_change_coord_mode ( VikGpsLayer
*val
, VikCoordMode mode
);
63 static void gps_layer_add_menu_items( VikGpsLayer
*vtl
, GtkMenu
*menu
, gpointer vlp
);
64 static void gps_layer_drag_drop_request ( VikGpsLayer
*val_src
, VikGpsLayer
*val_dest
, GtkTreeIter
*src_item_iter
, GtkTreePath
*dest_path
);
66 static void gps_upload_cb( gpointer layer_and_vlp
[2] );
67 static void gps_download_cb( gpointer layer_and_vlp
[2] );
68 static void gps_empty_upload_cb( gpointer layer_and_vlp
[2] );
69 static void gps_empty_download_cb( gpointer layer_and_vlp
[2] );
70 static void gps_empty_all_cb( gpointer layer_and_vlp
[2] );
71 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
72 static void gps_start_stop_tracking_cb( gpointer layer_and_vlp
[2] );
73 static void realtime_tracking_draw(VikGpsLayer
*vgl
, VikViewport
*vp
);
76 typedef enum {GARMIN_P
= 0, MAGELLAN_P
, NUM_PROTOCOLS
} vik_gps_proto
;
77 static gchar
* params_protocols
[] = {"Garmin", "Magellan", NULL
};
78 static gchar
* protocols_args
[] = {"garmin", "magellan"};
79 /*#define NUM_PROTOCOLS (sizeof(params_protocols)/sizeof(params_protocols[0]) - 1) */
81 static gchar
* params_ports
[] = {"com1", "usb:", NULL
};
83 static gchar
* params_ports
[] = {"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyUSB0", "/dev/ttyUSB1", "usb:", NULL
};
85 #define NUM_PORTS (sizeof(params_ports)/sizeof(params_ports[0]) - 1)
86 typedef enum {GPS_DOWN
=0, GPS_UP
} gps_dir
;
99 GtkWidget
*status_label
;
100 GtkWidget
*gps_label
;
101 GtkWidget
*ver_label
;
104 GtkWidget
*progress_label
;
105 GtkWidget
*trk_label
;
107 static void gps_session_delete(GpsSession
*sess
);
109 static gchar
*params_groups
[] = {
111 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
112 "Realtime Tracking Mode",
116 enum {GROUP_DATA_MODE
, GROUP_REALTIME_MODE
};
118 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
119 static gchar
*params_vehicle_position
[] = {
120 "Keep vehicle at center",
121 "Keep vehicle on screen",
126 VEHICLE_POSITION_CENTERED
= 0,
127 VEHICLE_POSITION_ON_SCREEN
,
128 VEHICLE_POSITION_NONE
,
132 static VikLayerParam gps_layer_params
[] = {
133 { "gps_protocol", VIK_LAYER_PARAM_UINT
, GROUP_DATA_MODE
, N_("GPS Protocol:"), VIK_LAYER_WIDGET_COMBOBOX
, params_protocols
, NULL
},
134 { "gps_port", VIK_LAYER_PARAM_UINT
, GROUP_DATA_MODE
, N_("Serial Port:"), VIK_LAYER_WIDGET_COMBOBOX
, params_ports
, NULL
},
136 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
137 { "record_tracking", VIK_LAYER_PARAM_BOOLEAN
, GROUP_REALTIME_MODE
, N_("Recording tracks"), VIK_LAYER_WIDGET_CHECKBUTTON
},
138 { "center_start_tracking", VIK_LAYER_PARAM_BOOLEAN
, GROUP_REALTIME_MODE
, N_("Jump to current position on start"), VIK_LAYER_WIDGET_CHECKBUTTON
},
139 { "moving_map_method", VIK_LAYER_PARAM_UINT
, GROUP_REALTIME_MODE
, N_("Moving Map Method:"), VIK_LAYER_WIDGET_RADIOGROUP_STATIC
, params_vehicle_position
, NULL
},
140 { "gpsd_host", VIK_LAYER_PARAM_STRING
, GROUP_REALTIME_MODE
, N_("Gpsd Host:"), VIK_LAYER_WIDGET_ENTRY
},
141 { "gpsd_port", VIK_LAYER_PARAM_STRING
, GROUP_REALTIME_MODE
, N_("Gpsd Port:"), VIK_LAYER_WIDGET_ENTRY
},
142 { "gpsd_retry_interval", VIK_LAYER_PARAM_STRING
, GROUP_REALTIME_MODE
, N_("Gpsd Retry Interval (seconds):"), VIK_LAYER_WIDGET_ENTRY
},
143 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */
146 PARAM_PROTOCOL
=0, PARAM_PORT
,
147 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
148 PARAM_REALTIME_REC
, PARAM_REALTIME_CENTER_START
, PARAM_VEHICLE_POSITION
, PARAM_GPSD_HOST
, PARAM_GPSD_PORT
, PARAM_GPSD_RETRY_INTERVAL
,
149 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */
152 VikLayerInterface vik_gps_layer_interface
= {
162 sizeof(params_groups
)/sizeof(params_groups
[0]),
164 VIK_MENU_ITEM_ALL
& ~(VIK_MENU_ITEM_CUT
|VIK_MENU_ITEM_COPY
),
166 (VikLayerFuncCreate
) vik_gps_layer_create
,
167 (VikLayerFuncRealize
) vik_gps_layer_realize
,
168 (VikLayerFuncPostRead
) NULL
,
169 (VikLayerFuncFree
) vik_gps_layer_free
,
171 (VikLayerFuncProperties
) NULL
,
172 (VikLayerFuncDraw
) vik_gps_layer_draw
,
173 (VikLayerFuncChangeCoordMode
) gps_layer_change_coord_mode
,
175 (VikLayerFuncSetMenuItemsSelection
) NULL
,
176 (VikLayerFuncGetMenuItemsSelection
) NULL
,
178 (VikLayerFuncAddMenuItems
) gps_layer_add_menu_items
,
179 (VikLayerFuncSublayerAddMenuItems
) NULL
,
181 (VikLayerFuncSublayerRenameRequest
) NULL
,
182 (VikLayerFuncSublayerToggleVisible
) NULL
,
184 (VikLayerFuncMarshall
) gps_layer_marshall
,
185 (VikLayerFuncUnmarshall
) gps_layer_unmarshall
,
187 (VikLayerFuncSetParam
) gps_layer_set_param
,
188 (VikLayerFuncGetParam
) gps_layer_get_param
,
190 (VikLayerFuncReadFileData
) NULL
,
191 (VikLayerFuncWriteFileData
) NULL
,
193 (VikLayerFuncDeleteItem
) NULL
,
194 (VikLayerFuncCopyItem
) NULL
,
195 (VikLayerFuncPasteItem
) NULL
,
196 (VikLayerFuncFreeCopiedItem
) NULL
,
197 (VikLayerFuncDragDropRequest
) gps_layer_drag_drop_request
,
200 enum {TRW_DOWNLOAD
=0, TRW_UPLOAD
,
201 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
205 static gchar
* trw_names
[] = {
206 N_("GPS Download"), N_("GPS Upload"),
207 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
208 N_("GPS Realtime Tracking"),
212 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
214 struct gps_data_t gpsd
;
219 struct gps_fix_t fix
;
220 gint satellites_used
;
221 gboolean dirty
; /* needs to be saved */
223 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */
225 struct _VikGpsLayer
{
227 VikTrwLayer
* trw_children
[NUM_TRW
];
228 GList
* children
; /* used only for writing file */
229 int cur_read_child
; /* used only for reading file */
230 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
232 gboolean realtime_tracking
; /* set/reset only by the callback */
233 gboolean first_realtime_trackpoint
;
237 enum unit realtime_gpsd_unit
;
239 VikTrack
*realtime_track
;
240 gchar
*realtime_track_name
;
242 GIOChannel
*realtime_io_channel
;
243 guint realtime_io_watch_id
;
244 guint realtime_retry_timer
;
245 GdkGC
*realtime_track_gc
;
246 GdkGC
*realtime_track_bg_gc
;
247 GdkGC
*realtime_track_pt_gc
;
248 GdkGC
*realtime_track_pt1_gc
;
249 GdkGC
*realtime_track_pt2_gc
;
254 gint gpsd_retry_interval
;
255 gboolean realtime_record
;
256 gboolean realtime_jump_to_start
;
257 guint vehicle_position
;
258 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */
260 guint serial_port_id
;
263 GType
vik_gps_layer_get_type ()
265 static GType val_type
= 0;
269 static const GTypeInfo val_info
=
271 sizeof (VikGpsLayerClass
),
272 NULL
, /* base_init */
273 NULL
, /* base_finalize */
274 NULL
, /* class init */
275 NULL
, /* class_finalize */
276 NULL
, /* class_data */
277 sizeof (VikGpsLayer
),
279 NULL
/* instance init */
281 val_type
= g_type_register_static ( VIK_LAYER_TYPE
, "VikGpsLayer", &val_info
, 0 );
287 static VikGpsLayer
*vik_gps_layer_create (VikViewport
*vp
)
291 VikGpsLayer
*rv
= vik_gps_layer_new (vp
);
292 vik_layer_rename ( VIK_LAYER(rv
), vik_gps_layer_interface
.name
);
294 for (i
= 0; i
< NUM_TRW
; i
++) {
295 rv
->trw_children
[i
] = VIK_TRW_LAYER(vik_layer_create ( VIK_LAYER_TRW
, vp
, NULL
, FALSE
));
296 vik_layer_set_menu_items_selection(VIK_LAYER(rv
->trw_children
[i
]), VIK_MENU_ITEM_ALL
& ~(VIK_MENU_ITEM_CUT
|VIK_MENU_ITEM_DELETE
));
302 static void gps_layer_marshall( VikGpsLayer
*vgl
, guint8
**data
, gint
*datalen
)
304 VikLayer
*child_layer
;
307 GByteArray
* b
= g_byte_array_new ();
311 #define alm_append(obj, sz) \
313 g_byte_array_append ( b, (guint8 *)&len, sizeof(len) ); \
314 g_byte_array_append ( b, (guint8 *)(obj), len );
316 vik_layer_marshall_params(VIK_LAYER(vgl
), &ld
, &ll
);
320 for (i
= 0; i
< NUM_TRW
; i
++) {
321 child_layer
= VIK_LAYER(vgl
->trw_children
[i
]);
322 vik_layer_marshall(child_layer
, &ld
, &ll
);
330 g_byte_array_free(b
, FALSE
);
335 static VikGpsLayer
*gps_layer_unmarshall( guint8
*data
, gint len
, VikViewport
*vvp
)
337 #define alm_size (*(gint *)data)
339 len -= sizeof(gint) + alm_size; \
340 data += sizeof(gint) + alm_size;
342 VikGpsLayer
*rv
= vik_gps_layer_new();
343 VikLayer
*child_layer
;
346 vik_layer_unmarshall_params ( VIK_LAYER(rv
), data
+sizeof(gint
), alm_size
, vvp
);
350 while (len
>0 && i
< NUM_TRW
) {
351 child_layer
= vik_layer_unmarshall ( data
+ sizeof(gint
), alm_size
, vvp
);
353 rv
->trw_children
[i
++] = (VikTrwLayer
*)child_layer
;
354 g_signal_connect_swapped ( G_OBJECT(child_layer
), "update", G_CALLBACK(vik_layer_emit_update_secondary
), rv
);
358 // g_print("gps_layer_unmarshall ended with len=%d\n", len);
365 static gboolean
gps_layer_set_param ( VikGpsLayer
*vgl
, guint16 id
, VikLayerParamData data
, VikViewport
*vp
)
370 if (data
.u
< NUM_PROTOCOLS
)
371 vgl
->protocol_id
= data
.u
;
373 g_warning(_("Unknown GPS Protocol"));
376 if (data
.u
< NUM_PORTS
)
377 vgl
->serial_port_id
= data
.u
;
379 g_warning(_("Unknown serial port device"));
381 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
382 case PARAM_GPSD_HOST
:
384 g_free(vgl
->gpsd_host
);
385 vgl
->gpsd_host
= g_strdup(data
.s
);
387 case PARAM_GPSD_PORT
:
389 g_free(vgl
->gpsd_port
);
390 vgl
->gpsd_port
= g_strdup(data
.s
); /* TODO: check for number */
392 case PARAM_GPSD_RETRY_INTERVAL
:
393 vgl
->gpsd_retry_interval
= strtol(data
.s
, NULL
, 10);
395 case PARAM_REALTIME_REC
:
396 vgl
->realtime_record
= data
.b
;
398 case PARAM_REALTIME_CENTER_START
:
399 vgl
->realtime_jump_to_start
= data
.b
;
401 case PARAM_VEHICLE_POSITION
:
402 vgl
->vehicle_position
= data
.u
;
404 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */
406 g_warning("gps_layer_set_param(): unknown parameter");
412 static VikLayerParamData
gps_layer_get_param ( VikGpsLayer
*vgl
, guint16 id
)
414 VikLayerParamData rv
;
418 rv
.u
= vgl
->protocol_id
;
421 rv
.u
= vgl
->serial_port_id
;
423 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
424 case PARAM_GPSD_HOST
:
425 rv
.s
= vgl
->gpsd_host
? vgl
->gpsd_host
: "";
427 case PARAM_GPSD_PORT
:
428 rv
.s
= vgl
->gpsd_port
? vgl
->gpsd_port
: g_strdup(DEFAULT_GPSD_PORT
);
430 case PARAM_GPSD_RETRY_INTERVAL
:
431 rv
.s
= g_strdup_printf("%d", vgl
->gpsd_retry_interval
);
433 case PARAM_REALTIME_REC
:
434 rv
.b
= vgl
->realtime_record
;
436 case PARAM_REALTIME_CENTER_START
:
437 rv
.b
= vgl
->realtime_jump_to_start
;
439 case PARAM_VEHICLE_POSITION
:
440 rv
.u
= vgl
->vehicle_position
;
442 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */
444 g_warning(_("%s: unknown parameter"), __FUNCTION__
);
450 VikGpsLayer
*vik_gps_layer_new (VikViewport
*vp
)
453 VikGpsLayer
*vgl
= VIK_GPS_LAYER ( g_object_new ( VIK_GPS_LAYER_TYPE
, NULL
) );
454 vik_layer_init ( VIK_LAYER(vgl
), VIK_LAYER_GPS
);
455 for (i
= 0; i
< NUM_TRW
; i
++) {
456 vgl
->trw_children
[i
] = NULL
;
458 vgl
->children
= NULL
;
459 vgl
->cur_read_child
= 0;
461 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
462 vgl
->realtime_tracking
= FALSE
;
463 vgl
->first_realtime_trackpoint
= FALSE
;
465 vgl
->realtime_io_channel
= NULL
;
466 vgl
->realtime_io_watch_id
= 0;
467 vgl
->realtime_retry_timer
= 0;
468 vgl
->realtime_track_gc
= vik_viewport_new_gc ( vp
, "#203070", 2 );
469 vgl
->realtime_track_bg_gc
= vik_viewport_new_gc ( vp
, "grey", 2 );
470 vgl
->realtime_track_pt1_gc
= vik_viewport_new_gc ( vp
, "red", 2 );
471 vgl
->realtime_track_pt2_gc
= vik_viewport_new_gc ( vp
, "green", 2 );
472 vgl
->realtime_track_pt_gc
= vgl
->realtime_track_pt1_gc
;
473 vgl
->realtime_gpsd_unit
= gpsd_units();
474 // fprintf(stderr, "DEBUG: gpsd_unit = %d\n", vgl->realtime_gpsd_unit);
475 vgl
->realtime_track
= NULL
;
477 /* Setting params here */
478 vgl
->gpsd_host
= g_strdup("localhost");
479 vgl
->gpsd_port
= g_strdup(DEFAULT_GPSD_PORT
);
480 vgl
->realtime_record
= TRUE
;
481 vgl
->realtime_jump_to_start
= TRUE
;
482 vgl
->vehicle_position
= VEHICLE_POSITION_ON_SCREEN
;
483 vgl
->gpsd_retry_interval
= 10;
484 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */
485 vgl
->protocol_id
= 0;
486 vgl
->serial_port_id
= 0;
491 static void vik_gps_layer_draw ( VikGpsLayer
*vgl
, gpointer data
)
495 VikLayer
*trigger
= VIK_LAYER(vik_viewport_get_trigger( VIK_VIEWPORT(data
) ));
497 for (i
= 0; i
< NUM_TRW
; i
++) {
498 vl
= VIK_LAYER(vgl
->trw_children
[i
]);
500 if ( vik_viewport_get_half_drawn ( VIK_VIEWPORT(data
) ) ) {
501 vik_viewport_set_half_drawn ( VIK_VIEWPORT(data
), FALSE
);
502 vik_viewport_snapshot_load( VIK_VIEWPORT(data
) );
504 vik_viewport_snapshot_save( VIK_VIEWPORT(data
) );
507 if (!vik_viewport_get_half_drawn( VIK_VIEWPORT(data
)))
508 vik_layer_draw ( vl
, data
);
510 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
511 if (vgl
->realtime_tracking
) {
512 if (VIK_LAYER(vgl
) == trigger
) {
513 if ( vik_viewport_get_half_drawn ( VIK_VIEWPORT(data
) ) ) {
514 vik_viewport_set_half_drawn ( VIK_VIEWPORT(data
), FALSE
);
515 vik_viewport_snapshot_load( VIK_VIEWPORT(data
) );
517 vik_viewport_snapshot_save( VIK_VIEWPORT(data
) );
520 if (!vik_viewport_get_half_drawn( VIK_VIEWPORT(data
)))
521 realtime_tracking_draw(vgl
, VIK_VIEWPORT(data
));
523 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */
526 static void gps_layer_change_coord_mode ( VikGpsLayer
*vgl
, VikCoordMode mode
)
529 for (i
= 0; i
< NUM_TRW
; i
++) {
530 vik_layer_change_coord_mode(VIK_LAYER(vgl
->trw_children
[i
]), mode
);
534 static void gps_layer_add_menu_items( VikGpsLayer
*vgl
, GtkMenu
*menu
, gpointer vlp
)
536 static gpointer pass_along
[2];
541 item
= gtk_menu_item_new();
542 gtk_menu_shell_append ( GTK_MENU_SHELL(menu
), item
);
543 gtk_widget_show ( item
);
545 item
= gtk_menu_item_new_with_label ( _("Upload to GPS") );
546 g_signal_connect_swapped ( G_OBJECT(item
), "activate", G_CALLBACK(gps_upload_cb
), pass_along
);
547 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), item
);
548 gtk_widget_show ( item
);
550 item
= gtk_menu_item_new_with_label ( _("Download from GPS") );
551 g_signal_connect_swapped ( G_OBJECT(item
), "activate", G_CALLBACK(gps_download_cb
), pass_along
);
552 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), item
);
553 gtk_widget_show ( item
);
555 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
556 item
= gtk_menu_item_new_with_label ( vgl
->realtime_tracking
?
557 "Stop realtime tracking" :
558 "Start realtime tracking" );
559 g_signal_connect_swapped ( G_OBJECT(item
), "activate", G_CALLBACK(gps_start_stop_tracking_cb
), pass_along
);
560 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), item
);
561 gtk_widget_show ( item
);
563 item
= gtk_menu_item_new();
564 gtk_menu_shell_append ( GTK_MENU_SHELL(menu
), item
);
565 gtk_widget_show ( item
);
566 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */
568 item
= gtk_menu_item_new_with_label ( _("Empty Upload") );
569 g_signal_connect_swapped ( G_OBJECT(item
), "activate", G_CALLBACK(gps_empty_upload_cb
), pass_along
);
570 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), item
);
571 gtk_widget_show ( item
);
573 item
= gtk_menu_item_new_with_label ( _("Empty Download") );
574 g_signal_connect_swapped ( G_OBJECT(item
), "activate", G_CALLBACK(gps_empty_download_cb
), pass_along
);
575 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), item
);
576 gtk_widget_show ( item
);
578 item
= gtk_menu_item_new_with_label ( _("Empty All") );
579 g_signal_connect_swapped ( G_OBJECT(item
), "activate", G_CALLBACK(gps_empty_all_cb
), pass_along
);
580 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), item
);
581 gtk_widget_show ( item
);
585 static void disconnect_layer_signal ( VikLayer
*vl
, VikGpsLayer
*vgl
)
587 g_assert(DISCONNECT_UPDATE_SIGNAL(vl
,vgl
)==1);
590 static void vik_gps_layer_free ( VikGpsLayer
*vgl
)
593 for (i
= 0; i
< NUM_TRW
; i
++) {
594 if (vgl
->vl
.realized
)
595 disconnect_layer_signal(VIK_LAYER(vgl
->trw_children
[i
]), vgl
);
596 g_object_unref(vgl
->trw_children
[i
]);
598 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
599 if (vgl
->realtime_track_gc
!= NULL
)
600 g_object_unref(vgl
->realtime_track_gc
);
601 if (vgl
->realtime_track_bg_gc
!= NULL
)
602 g_object_unref(vgl
->realtime_track_bg_gc
);
603 if (vgl
->realtime_track_pt1_gc
!= NULL
)
604 g_object_unref(vgl
->realtime_track_pt1_gc
);
605 if (vgl
->realtime_track_pt2_gc
!= NULL
)
606 g_object_unref(vgl
->realtime_track_pt2_gc
);
607 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */
610 gboolean
vik_gps_layer_delete ( VikGpsLayer
*vgl
, GtkTreeIter
*iter
)
613 VikLayer
*l
= VIK_LAYER( vik_treeview_item_get_pointer ( VIK_LAYER(vgl
)->vt
, iter
) );
614 gboolean was_visible
= l
->visible
;
616 vik_treeview_item_delete ( VIK_LAYER(vgl
)->vt
, iter
);
617 for (i
= 0; i
< NUM_TRW
; i
++) {
618 if (VIK_LAYER(vgl
->trw_children
[i
]) == l
)
619 vgl
->trw_children
[i
] = NULL
;
621 g_assert(DISCONNECT_UPDATE_SIGNAL(l
,vgl
)==1);
622 g_object_unref ( l
);
627 static void vik_gps_layer_realize ( VikGpsLayer
*vgl
, VikTreeview
*vt
, GtkTreeIter
*layer_iter
)
632 for (ix
= 0; ix
< NUM_TRW
; ix
++) {
633 VikLayer
* trw
= VIK_LAYER(vgl
->trw_children
[ix
]);
634 vik_treeview_add_layer ( VIK_LAYER(vgl
)->vt
, layer_iter
, &iter
,
635 _(trw_names
[ix
]), vgl
,
636 trw
, trw
->type
, trw
->type
);
637 if ( ! trw
->visible
)
638 vik_treeview_item_set_visible ( VIK_LAYER(vgl
)->vt
, &iter
, FALSE
);
639 vik_layer_realize ( trw
, VIK_LAYER(vgl
)->vt
, &iter
);
640 g_signal_connect_swapped ( G_OBJECT(trw
), "update", G_CALLBACK(vik_layer_emit_update_secondary
), vgl
);
644 const GList
*vik_gps_layer_get_children ( VikGpsLayer
*vgl
)
648 if (vgl
->children
== NULL
) {
649 for (i
= NUM_TRW
- 1; i
>= 0; i
--)
650 vgl
->children
= g_list_prepend(vgl
->children
, vgl
->trw_children
[i
]);
652 return vgl
->children
;
655 VikTrwLayer
* vik_gps_layer_get_a_child(VikGpsLayer
*vgl
)
657 g_assert ((vgl
->cur_read_child
>= 0) && (vgl
->cur_read_child
< NUM_TRW
));
659 VikTrwLayer
* vtl
= vgl
->trw_children
[vgl
->cur_read_child
];
660 if (++(vgl
->cur_read_child
) >= NUM_TRW
)
661 vgl
->cur_read_child
= 0;
665 gboolean
vik_gps_layer_is_empty ( VikGpsLayer
*vgl
)
667 if ( vgl
->trw_children
[0] )
672 static void gps_layer_drag_drop_request ( VikGpsLayer
*val_src
, VikGpsLayer
*val_dest
, GtkTreeIter
*src_item_iter
, GtkTreePath
*dest_path
)
674 VikTreeview
*vt
= VIK_LAYER(val_src
)->vt
;
675 VikLayer
*vl
= vik_treeview_item_get_pointer(vt
, src_item_iter
);
676 GtkTreeIter dest_iter
;
678 gboolean target_exists
;
680 dp
= gtk_tree_path_to_string(dest_path
);
681 target_exists
= vik_treeview_get_iter_from_path_str(vt
, &dest_iter
, dp
);
683 /* vik_gps_layer_delete unrefs, but we don't want that here.
684 * we're still using the layer. */
686 vik_gps_layer_delete(val_src
, src_item_iter
);
691 static void gps_session_delete(GpsSession
*sess
)
694 g_mutex_free(sess
->mutex
);
695 g_free(sess
->cmd_args
);
701 static void set_total_count(gint cnt
, GpsSession
*sess
)
705 g_mutex_lock(sess
->mutex
);
707 const gchar
*tmp_str
;
708 if (sess
->direction
== GPS_DOWN
)
710 if (sess
->progress_label
== sess
->wp_label
)
711 tmp_str
= ngettext("Downloading %d waypoint...", "Downloading %d waypoints...", cnt
);
713 tmp_str
= ngettext("Downloading %d trackpoint...", "Downloading %d trackpoints...", cnt
);
717 if (sess
->progress_label
== sess
->wp_label
)
718 tmp_str
= ngettext("Uploading %d waypoint...", "Uploading %d waypoints...", cnt
);
720 tmp_str
= ngettext("Uploading %d trackpoint...", "Uploading %d trackpoints...", cnt
);
723 g_snprintf(s
, 128, tmp_str
, cnt
);
724 gtk_label_set_text ( GTK_LABEL(sess
->progress_label
), s
);
725 gtk_widget_show ( sess
->progress_label
);
726 sess
->total_count
= cnt
;
728 g_mutex_unlock(sess
->mutex
);
732 static void set_current_count(gint cnt
, GpsSession
*sess
)
735 const gchar
*tmp_str
;
738 g_mutex_lock(sess
->mutex
);
740 if (cnt
< sess
->total_count
) {
741 if (sess
->direction
== GPS_DOWN
)
743 if (sess
->progress_label
== sess
->wp_label
)
744 tmp_str
= ngettext("Downloaded %d out of %d waypoint...", "Downloaded %d out of %d waypoints...", sess
->total_count
);
746 tmp_str
= ngettext("Downloaded %d out of %d trackpoint...", "Downloaded %d out of %d trackpoints...", sess
->total_count
);
749 if (sess
->progress_label
== sess
->wp_label
)
750 tmp_str
= ngettext("Uploaded %d out of %d waypoint...", "Uploaded %d out of %d waypoints...", sess
->total_count
);
752 tmp_str
= ngettext("Uploaded %d out of %d trackpoint...", "Uploaded %d out of %d trackpoints...", sess
->total_count
);
754 g_snprintf(s
, 128, tmp_str
, cnt
, sess
->total_count
);
756 if (sess
->direction
== GPS_DOWN
)
758 if (sess
->progress_label
== sess
->wp_label
)
759 tmp_str
= ngettext("Downloaded %d waypoint", "Downloaded %d waypoints", cnt
);
761 tmp_str
= ngettext("Downloaded %d trackpoint", "Downloaded %d trackpoints", cnt
);
764 if (sess
->progress_label
== sess
->wp_label
)
765 tmp_str
= ngettext("Uploaded %d waypoint", "Uploaded %d waypoints", cnt
);
767 tmp_str
= ngettext("Uploaded %d trackpoint", "Uploaded %d trackpoints", cnt
);
769 g_snprintf(s
, 128, tmp_str
, cnt
);
771 gtk_label_set_text ( GTK_LABEL(sess
->progress_label
), s
);
773 g_mutex_unlock(sess
->mutex
);
777 static void set_gps_info(const gchar
*info
, GpsSession
*sess
)
781 g_mutex_lock(sess
->mutex
);
783 g_snprintf(s
, 256, _("GPS Device: %s"), info
);
784 gtk_label_set_text ( GTK_LABEL(sess
->gps_label
), s
);
786 g_mutex_unlock(sess
->mutex
);
790 static void gps_download_progress_func(BabelProgressCode c
, gpointer data
, GpsSession
* sess
)
794 gdk_threads_enter ();
795 g_mutex_lock(sess
->mutex
);
797 g_mutex_unlock(sess
->mutex
);
798 gps_session_delete(sess
);
800 g_thread_exit ( NULL
);
802 g_mutex_unlock(sess
->mutex
);
803 gdk_threads_leave ();
806 case BABEL_DIAG_OUTPUT
:
807 line
= (gchar
*)data
;
809 /* tells us how many items there will be */
810 if (strstr(line
, "Xfer Wpt")) {
811 sess
->progress_label
= sess
->wp_label
;
813 if (strstr(line
, "Xfer Trk")) {
814 sess
->progress_label
= sess
->trk_label
;
816 if (strstr(line
, "PRDDAT")) {
817 gchar
**tokens
= g_strsplit(line
, " ", 0);
823 while (tokens
[n_tokens
])
827 for (i
=8; tokens
[i
] && ilen
< sizeof(info
)-2 && strcmp(tokens
[i
], "00"); i
++) {
829 sscanf(tokens
[i
], "%x", &ch
);
833 set_gps_info(info
, sess
);
837 if (strstr(line
, "RECORD")) {
840 if (strlen(line
) > 20) {
841 sscanf(line
+17, "%x", &lsb
);
842 sscanf(line
+20, "%x", &msb
);
843 cnt
= lsb
+ msb
* 256;
844 set_total_count(cnt
, sess
);
848 if ( strstr(line
, "WPTDAT") || strstr(line
, "TRKHDR") || strstr(line
, "TRKDAT") ) {
850 set_current_count(sess
->count
, sess
);
861 static void gps_upload_progress_func(BabelProgressCode c
, gpointer data
, GpsSession
* sess
)
866 gdk_threads_enter ();
867 g_mutex_lock(sess
->mutex
);
869 g_mutex_unlock(sess
->mutex
);
870 gps_session_delete(sess
);
872 g_thread_exit ( NULL
);
874 g_mutex_unlock(sess
->mutex
);
875 gdk_threads_leave ();
878 case BABEL_DIAG_OUTPUT
:
879 line
= (gchar
*)data
;
881 if (strstr(line
, "PRDDAT")) {
882 gchar
**tokens
= g_strsplit(line
, " ", 0);
888 while (tokens
[n_tokens
])
892 for (i
=8; tokens
[i
] && ilen
< sizeof(info
)-2 && strcmp(tokens
[i
], "00"); i
++) {
894 sscanf(tokens
[i
], "%x", &ch
);
898 set_gps_info(info
, sess
);
902 if (strstr(line
, "RECORD")) {
905 if (strlen(line
) > 20) {
906 sscanf(line
+17, "%x", &lsb
);
907 sscanf(line
+20, "%x", &msb
);
908 cnt
= lsb
+ msb
* 256;
909 /* set_total_count(cnt, sess); */
913 if ( strstr(line
, "WPTDAT")) {
914 if (sess
->count
== 0) {
915 sess
->progress_label
= sess
->wp_label
;
916 set_total_count(cnt
, sess
);
919 set_current_count(sess
->count
, sess
);
922 if ( strstr(line
, "TRKHDR") || strstr(line
, "TRKDAT") ) {
923 if (sess
->count
== 0) {
924 sess
->progress_label
= sess
->trk_label
;
925 set_total_count(cnt
, sess
);
928 set_current_count(sess
->count
, sess
);
939 static void gps_comm_thread(GpsSession
*sess
)
943 if (sess
->direction
== GPS_DOWN
)
944 result
= a_babel_convert_from (sess
->vtl
, sess
->cmd_args
,
945 (BabelStatusFunc
) gps_download_progress_func
, sess
->port
, sess
);
947 result
= a_babel_convert_to (sess
->vtl
, sess
->cmd_args
,
948 (BabelStatusFunc
) gps_upload_progress_func
, sess
->port
, sess
);
952 gtk_label_set_text ( GTK_LABEL(sess
->status_label
), _("Error: couldn't find gpsbabel.") );
955 g_mutex_lock(sess
->mutex
);
957 gtk_label_set_text ( GTK_LABEL(sess
->status_label
), _("Done.") );
958 gtk_dialog_set_response_sensitive ( GTK_DIALOG(sess
->dialog
), GTK_RESPONSE_ACCEPT
, TRUE
);
959 gtk_dialog_set_response_sensitive ( GTK_DIALOG(sess
->dialog
), GTK_RESPONSE_REJECT
, FALSE
);
963 g_mutex_unlock(sess
->mutex
);
966 g_mutex_lock(sess
->mutex
);
969 g_mutex_unlock(sess
->mutex
);
972 g_mutex_unlock(sess
->mutex
);
973 gps_session_delete(sess
);
979 static gint
gps_comm(VikTrwLayer
*vtl
, gps_dir dir
, vik_gps_proto proto
, gchar
*port
) {
980 GpsSession
*sess
= g_malloc(sizeof(GpsSession
));
982 sess
->mutex
= g_mutex_new();
983 sess
->direction
= dir
;
985 sess
->port
= g_strdup(port
);
987 sess
->cmd_args
= g_strdup_printf("-D 9 -t -w -%c %s",
988 (dir
== GPS_DOWN
) ? 'i' : 'o', protocols_args
[proto
]);
989 sess
->window_title
= (dir
== GPS_DOWN
) ? _("GPS Download") : _("GPS Upload");
991 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
);
992 gtk_dialog_set_response_sensitive ( GTK_DIALOG(sess
->dialog
),
993 GTK_RESPONSE_ACCEPT
, FALSE
);
994 gtk_window_set_title ( GTK_WINDOW(sess
->dialog
), sess
->window_title
);
996 sess
->status_label
= gtk_label_new (_("Status: detecting gpsbabel"));
997 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(sess
->dialog
)->vbox
),
998 sess
->status_label
, FALSE
, FALSE
, 5 );
999 gtk_widget_show_all(sess
->status_label
);
1001 sess
->gps_label
= gtk_label_new (_("GPS device: N/A"));
1002 sess
->ver_label
= gtk_label_new ("");
1003 sess
->id_label
= gtk_label_new ("");
1004 sess
->wp_label
= gtk_label_new ("");
1005 sess
->trk_label
= gtk_label_new ("");
1007 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(sess
->dialog
)->vbox
), sess
->gps_label
, FALSE
, FALSE
, 5 );
1008 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(sess
->dialog
)->vbox
), sess
->wp_label
, FALSE
, FALSE
, 5 );
1009 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(sess
->dialog
)->vbox
), sess
->trk_label
, FALSE
, FALSE
, 5 );
1011 gtk_widget_show_all(sess
->dialog
);
1013 sess
->progress_label
= sess
->wp_label
;
1014 sess
->total_count
= -1;
1016 /* TODO: starting gps read/write thread here */
1017 g_thread_create((GThreadFunc
)gps_comm_thread
, sess
, FALSE
, NULL
);
1019 gtk_dialog_run(GTK_DIALOG(sess
->dialog
));
1021 gtk_widget_destroy(sess
->dialog
);
1023 g_mutex_lock(sess
->mutex
);
1025 sess
->ok
= FALSE
; /* tell thread to stop */
1026 g_mutex_unlock(sess
->mutex
);
1029 g_mutex_unlock(sess
->mutex
);
1030 gps_session_delete(sess
);
1036 static void gps_upload_cb( gpointer layer_and_vlp
[2] )
1038 VikGpsLayer
*vgl
= (VikGpsLayer
*)layer_and_vlp
[0];
1039 VikTrwLayer
*vtl
= vgl
->trw_children
[TRW_UPLOAD
];
1040 gps_comm(vtl
, GPS_UP
, vgl
->protocol_id
, params_ports
[vgl
->serial_port_id
]);
1043 static void gps_download_cb( gpointer layer_and_vlp
[2] )
1045 VikGpsLayer
*vgl
= (VikGpsLayer
*)layer_and_vlp
[0];
1046 VikTrwLayer
*vtl
= vgl
->trw_children
[TRW_DOWNLOAD
];
1047 gps_comm(vtl
, GPS_DOWN
, vgl
->protocol_id
, params_ports
[vgl
->serial_port_id
]);
1050 static void gps_empty_upload_cb( gpointer layer_and_vlp
[2] )
1052 VikGpsLayer
*vgl
= (VikGpsLayer
*)layer_and_vlp
[0];
1053 vik_trw_layer_delete_all_waypoints ( vgl
-> trw_children
[TRW_UPLOAD
]);
1054 vik_trw_layer_delete_all_tracks ( vgl
-> trw_children
[TRW_UPLOAD
]);
1057 static void gps_empty_download_cb( gpointer layer_and_vlp
[2] )
1059 VikGpsLayer
*vgl
= (VikGpsLayer
*)layer_and_vlp
[0];
1060 vik_trw_layer_delete_all_waypoints ( vgl
-> trw_children
[TRW_DOWNLOAD
]);
1061 vik_trw_layer_delete_all_tracks ( vgl
-> trw_children
[TRW_DOWNLOAD
]);
1064 static void gps_empty_all_cb( gpointer layer_and_vlp
[2] )
1066 VikGpsLayer
*vgl
= (VikGpsLayer
*)layer_and_vlp
[0];
1067 vik_trw_layer_delete_all_waypoints ( vgl
-> trw_children
[TRW_UPLOAD
]);
1068 vik_trw_layer_delete_all_tracks ( vgl
-> trw_children
[TRW_UPLOAD
]);
1069 vik_trw_layer_delete_all_waypoints ( vgl
-> trw_children
[TRW_DOWNLOAD
]);
1070 vik_trw_layer_delete_all_tracks ( vgl
-> trw_children
[TRW_DOWNLOAD
]);
1073 #ifdef VIK_CONFIG_REALTIME_GPS_TRACKING
1074 static void rt_gpsd_disconnect(VikGpsLayer
*vgl
);
1075 static gboolean
rt_gpsd_connect(VikGpsLayer
*vgl
, gboolean ask_if_failed
);
1077 static void realtime_tracking_draw(VikGpsLayer
*vgl
, VikViewport
*vp
)
1081 struct LatLon lnw
, lse
;
1082 vik_viewport_screen_to_coord ( vp
, -20, -20, &nw
);
1083 vik_viewport_screen_to_coord ( vp
, vik_viewport_get_width(vp
)+20, vik_viewport_get_width(vp
)+20, &se
);
1084 vik_coord_to_latlon ( &nw
, &lnw
);
1085 vik_coord_to_latlon ( &se
, &lse
);
1086 if ( vgl
->realtime_fix
.fix
.latitude
> lse
.lat
&&
1087 vgl
->realtime_fix
.fix
.latitude
< lnw
.lat
&&
1088 vgl
->realtime_fix
.fix
.longitude
> lnw
.lon
&&
1089 vgl
->realtime_fix
.fix
.longitude
< lse
.lon
) {
1092 gint half_back_x
, half_back_y
;
1093 gint half_back_bg_x
, half_back_bg_y
;
1095 gint ptbg_x
, ptbg_y
;
1096 gint side1_x
, side1_y
, side2_x
, side2_y
;
1097 gint side1bg_x
, side1bg_y
, side2bg_x
, side2bg_y
;
1099 ll
.lat
= vgl
->realtime_fix
.fix
.latitude
;
1100 ll
.lon
= vgl
->realtime_fix
.fix
.longitude
;
1101 vik_coord_load_from_latlon ( &gps
, vik_viewport_get_coord_mode(vp
), &ll
);
1102 vik_viewport_coord_to_screen ( vp
, &gps
, &x
, &y
);
1104 gdouble heading_cos
= cos(M_PI
/180*vgl
->realtime_fix
.fix
.track
);
1105 gdouble heading_sin
= sin(M_PI
/180*vgl
->realtime_fix
.fix
.track
);
1107 half_back_y
= y
+8*heading_cos
;
1108 half_back_x
= x
-8*heading_sin
;
1109 half_back_bg_y
= y
+10*heading_cos
;
1110 half_back_bg_x
= x
-10*heading_sin
;
1112 pt_y
= half_back_y
-24*heading_cos
;
1113 pt_x
= half_back_x
+24*heading_sin
;
1114 ptbg_y
= half_back_bg_y
-28*heading_cos
;
1115 ptbg_x
= half_back_bg_x
+28*heading_sin
;
1117 side1_y
= half_back_y
+9*heading_sin
;
1118 side1_x
= half_back_x
+9*heading_cos
;
1119 side1bg_y
= half_back_bg_y
+11*heading_sin
;
1120 side1bg_x
= half_back_bg_x
+11*heading_cos
;
1122 side2_y
= half_back_y
-9*heading_sin
;
1123 side2_x
= half_back_x
-9*heading_cos
;
1124 side2bg_y
= half_back_bg_y
-11*heading_sin
;
1125 side2bg_x
= half_back_bg_x
-11*heading_cos
;
1127 GdkPoint trian
[3] = { { pt_x
, pt_y
}, {side1_x
, side1_y
}, {side2_x
, side2_y
} };
1128 GdkPoint trian_bg
[3] = { { ptbg_x
, pt_y
}, {side1bg_x
, side1bg_y
}, {side2bg_x
, side2bg_y
} };
1130 vik_viewport_draw_polygon ( vp
, vgl
->realtime_track_bg_gc
, TRUE
, trian_bg
, 3 );
1131 vik_viewport_draw_polygon ( vp
, vgl
->realtime_track_gc
, TRUE
, trian
, 3 );
1132 vik_viewport_draw_rectangle ( vp
,
1133 (vgl
->realtime_fix
.fix
.mode
> MODE_2D
) ? vgl
->realtime_track_pt2_gc
: vgl
->realtime_track_pt1_gc
,
1134 TRUE
, x
-2, y
-2, 4, 4 );
1135 //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;
1139 static void create_realtime_trackpoint(VikGpsLayer
*vgl
, gboolean forced
)
1144 /* Note that fix.time is a double, but it should not affect the precision
1146 time_t cur_timestamp
= vgl
->realtime_fix
.fix
.time
;
1147 time_t last_timestamp
= vgl
->last_fix
.fix
.time
;
1149 if (cur_timestamp
< last_timestamp
) {
1153 if (vgl
->realtime_record
&& vgl
->realtime_fix
.dirty
) {
1154 gboolean replace
= FALSE
;
1155 int heading
= (int)floor(vgl
->realtime_fix
.fix
.track
);
1156 int last_heading
= (int)floor(vgl
->last_fix
.fix
.track
);
1157 int alt
= isnan(vgl
->realtime_fix
.fix
.altitude
) ? VIK_DEFAULT_ALTITUDE
: floor(vgl
->realtime_fix
.fix
.altitude
);
1158 int last_alt
= isnan(vgl
->last_fix
.fix
.altitude
) ? VIK_DEFAULT_ALTITUDE
: floor(vgl
->last_fix
.fix
.altitude
);
1159 if (((last_tp
= g_list_last(vgl
->realtime_track
->trackpoints
)) != NULL
) &&
1160 (vgl
->realtime_fix
.fix
.mode
> MODE_2D
) &&
1161 (vgl
->last_fix
.fix
.mode
<= MODE_2D
) &&
1162 ((cur_timestamp
- last_timestamp
) < 2)) {
1163 g_free(last_tp
->data
);
1164 vgl
->realtime_track
->trackpoints
= g_list_delete_link(vgl
->realtime_track
->trackpoints
, last_tp
);
1168 ((cur_timestamp
!= last_timestamp
) &&
1170 ((heading
< last_heading
) && (heading
< (last_heading
- 3))) ||
1171 ((heading
> last_heading
) && (heading
> (last_heading
+ 3))) ||
1172 ((alt
!= VIK_DEFAULT_ALTITUDE
) && (alt
!= last_alt
)))))) {
1173 /* TODO: check for new segments */
1174 VikTrackpoint
*tp
= vik_trackpoint_new();
1175 tp
->newsegment
= FALSE
;
1176 tp
->has_timestamp
= TRUE
;
1177 tp
->timestamp
= vgl
->realtime_fix
.fix
.time
;
1179 /* speed only available for 3D fix. Check for NAN when use this speed */
1180 tp
->speed
= vgl
->realtime_fix
.fix
.speed
;
1181 tp
->course
= vgl
->realtime_fix
.fix
.track
;
1182 tp
->nsats
= vgl
->realtime_fix
.satellites_used
;
1183 tp
->fix_mode
= vgl
->realtime_fix
.fix
.mode
;
1184 tp
->extended
= TRUE
;
1186 ll
.lat
= vgl
->realtime_fix
.fix
.latitude
;
1187 ll
.lon
= vgl
->realtime_fix
.fix
.longitude
;
1188 vik_coord_load_from_latlon(&tp
->coord
,
1189 vik_trw_layer_get_coord_mode(vgl
->trw_children
[TRW_REALTIME
]), &ll
);
1191 vgl
->realtime_track
->trackpoints
= g_list_append(vgl
->realtime_track
->trackpoints
, tp
);
1192 vgl
->realtime_fix
.dirty
= FALSE
;
1193 vgl
->realtime_fix
.satellites_used
= 0;
1194 vgl
->last_fix
= vgl
->realtime_fix
;
1200 static void gpsd_raw_hook(VglGpsd
*vgpsd
, gchar
*data
)
1202 gboolean update_all
= FALSE
;
1203 VikGpsLayer
*vgl
= vgpsd
->vgl
;
1205 if (!vgl
->realtime_tracking
) {
1206 g_warning("%s: receiving GPS data while not in realtime mode", __PRETTY_FUNCTION__
);
1210 if ((vgpsd
->gpsd
.fix
.mode
>= MODE_2D
) &&
1211 !isnan(vgpsd
->gpsd
.fix
.latitude
) &&
1212 !isnan(vgpsd
->gpsd
.fix
.longitude
) &&
1213 !isnan(vgpsd
->gpsd
.fix
.track
)) {
1215 VikWindow
*vw
= VIK_WINDOW(VIK_GTK_WINDOW_FROM_LAYER(vgl
));
1216 VikViewport
*vvp
= vik_window_viewport(vw
);
1217 vgl
->realtime_fix
.fix
= vgpsd
->gpsd
.fix
;
1218 vgl
->realtime_fix
.satellites_used
= vgpsd
->gpsd
.satellites_used
;
1219 vgl
->realtime_fix
.dirty
= TRUE
;
1222 VikCoord vehicle_coord
;
1224 ll
.lat
= vgl
->realtime_fix
.fix
.latitude
;
1225 ll
.lon
= vgl
->realtime_fix
.fix
.longitude
;
1226 vik_coord_load_from_latlon(&vehicle_coord
,
1227 vik_trw_layer_get_coord_mode(vgl
->trw_children
[TRW_REALTIME
]), &ll
);
1229 if ((vgl
->vehicle_position
== VEHICLE_POSITION_CENTERED
) ||
1230 (vgl
->realtime_jump_to_start
&& vgl
->first_realtime_trackpoint
)) {
1231 vik_viewport_set_center_coord(vvp
, &vehicle_coord
);
1237 const int px
= 20; /* adjust ment in pixels to make sure vehicle is inside the box */
1238 gint width
= vik_viewport_get_width(vvp
);
1239 gint height
= vik_viewport_get_height(vvp
);
1242 vik_viewport_coord_to_screen(vvp
, &vehicle_coord
, &vx
, &vy
);
1244 if (vx
< (width
/hdiv
))
1245 vik_viewport_set_center_screen(vvp
, vx
- width
/2 + width
/hdiv
+ px
, vy
);
1246 else if (vx
> (width
- width
/hdiv
))
1247 vik_viewport_set_center_screen(vvp
, vx
+ width
/2 - width
/hdiv
- px
, vy
);
1248 else if (vy
< (height
/vdiv
))
1249 vik_viewport_set_center_screen(vvp
, vx
, vy
- height
/2 + height
/vdiv
+ px
);
1250 else if (vy
> (height
- height
/vdiv
))
1251 vik_viewport_set_center_screen(vvp
, vx
, vy
+ height
/2 - height
/vdiv
- px
);
1256 vgl
->first_realtime_trackpoint
= FALSE
;
1257 create_realtime_trackpoint(vgl
, FALSE
);
1259 vik_layer_emit_update ( update_all
? VIK_LAYER(vgl
) : VIK_LAYER(vgl
->trw_children
[TRW_REALTIME
]));
1263 static gboolean
gpsd_data_available(GIOChannel
*source
, GIOCondition condition
, gpointer data
)
1265 VikGpsLayer
*vgl
= data
;
1266 if (condition
== G_IO_IN
) {
1267 if (!gps_poll(&vgl
->vgpsd
->gpsd
))
1270 g_warning("Disconnected from gpsd. Trying to reconnect");
1271 rt_gpsd_disconnect(vgl
);
1272 rt_gpsd_connect(vgl
, FALSE
);
1275 return FALSE
; /* no further calling */
1278 static gchar
*make_track_name(VikTrwLayer
*vtl
)
1280 const gchar basename
[] = "REALTIME";
1281 const gint bufsize
= sizeof(basename
) + 5;
1282 gchar
*name
= g_malloc(bufsize
);
1283 strcpy(name
, basename
);
1286 while (vik_trw_layer_get_track(vtl
, name
) != NULL
) {
1287 g_snprintf(name
, bufsize
, "%s#%d", basename
, i
);
1294 static gboolean
rt_gpsd_try_connect(gpointer
*data
)
1296 VikGpsLayer
*vgl
= (VikGpsLayer
*)data
;
1297 struct gps_data_t
*gpsd
= gps_open(vgl
->gpsd_host
, vgl
->gpsd_port
);
1300 g_warning("Failed to connect to gpsd at %s (port %s). Will retry in %d seconds",
1301 vgl
->gpsd_host
, vgl
->gpsd_port
, vgl
->gpsd_retry_interval
);
1302 return TRUE
; /* keep timer running */
1305 vgl
->vgpsd
= g_realloc(gpsd
, sizeof(VglGpsd
));
1306 vgl
->vgpsd
->vgl
= vgl
;
1308 vgl
->realtime_fix
.dirty
= vgl
->last_fix
.dirty
= FALSE
;
1309 /* track alt/time graph uses VIK_DEFAULT_ALTITUDE (0.0) as invalid */
1310 vgl
->realtime_fix
.fix
.altitude
= vgl
->last_fix
.fix
.altitude
= VIK_DEFAULT_ALTITUDE
;
1311 vgl
->realtime_fix
.fix
.speed
= vgl
->last_fix
.fix
.speed
= NAN
;
1313 if (vgl
->realtime_record
) {
1314 VikTrwLayer
*vtl
= vgl
->trw_children
[TRW_REALTIME
];
1315 vgl
->realtime_track
= vik_track_new();
1316 vgl
->realtime_track
->visible
= TRUE
;
1317 vgl
->realtime_track_name
= make_track_name(vtl
);
1318 vik_trw_layer_add_track(vtl
, vgl
->realtime_track_name
, vgl
->realtime_track
);
1321 gps_set_raw_hook(&vgl
->vgpsd
->gpsd
, gpsd_raw_hook
);
1322 vgl
->realtime_io_channel
= g_io_channel_unix_new(vgl
->vgpsd
->gpsd
.gps_fd
);
1323 vgl
->realtime_io_watch_id
= g_io_add_watch( vgl
->realtime_io_channel
,
1324 G_IO_IN
| G_IO_ERR
| G_IO_HUP
, gpsd_data_available
, vgl
);
1325 gps_query(&vgl
->vgpsd
->gpsd
, "w+x");
1326 return FALSE
; /* no longer called by timeout */
1329 static gboolean
rt_ask_retry(VikGpsLayer
*vgl
)
1331 GtkWidget
*dialog
= gtk_message_dialog_new (VIK_GTK_WINDOW_FROM_LAYER(vgl
),
1332 GTK_DIALOG_DESTROY_WITH_PARENT
,
1333 GTK_MESSAGE_QUESTION
,
1335 "Failed to connect to gpsd at %s (port %s)\n"
1336 "Should Viking keep trying (every %d seconds)?",
1337 vgl
->gpsd_host
, vgl
->gpsd_port
,
1338 vgl
->gpsd_retry_interval
);
1340 gint res
= gtk_dialog_run(GTK_DIALOG(dialog
));
1341 gtk_widget_destroy(dialog
);
1342 return (res
== GTK_RESPONSE_YES
);
1345 static gboolean
rt_gpsd_connect(VikGpsLayer
*vgl
, gboolean ask_if_failed
)
1347 vgl
->realtime_retry_timer
= 0;
1348 if (rt_gpsd_try_connect((gpointer
*)vgl
)) {
1349 if (vgl
->gpsd_retry_interval
<= 0) {
1350 g_warning("Failed to connect to gpsd but will not retry because retry intervel was set to %d (which is 0 or negative)", vgl
->gpsd_retry_interval
);
1353 else if (ask_if_failed
&& !rt_ask_retry(vgl
))
1356 vgl
->realtime_retry_timer
= g_timeout_add_seconds(vgl
->gpsd_retry_interval
,
1357 (GSourceFunc
)rt_gpsd_try_connect
, (gpointer
*)vgl
);
1362 static void rt_gpsd_disconnect(VikGpsLayer
*vgl
)
1364 if (vgl
->realtime_io_watch_id
) {
1365 g_source_remove(vgl
->realtime_io_watch_id
);
1366 vgl
->realtime_io_watch_id
= 0;
1368 if (vgl
->realtime_retry_timer
) {
1369 g_source_remove(vgl
->realtime_retry_timer
);
1370 vgl
->realtime_retry_timer
= 0;
1372 if (vgl
->realtime_io_channel
) {
1373 GError
*error
= NULL
;
1374 g_io_channel_shutdown (vgl
->realtime_io_channel
, FALSE
, &error
);
1375 vgl
->realtime_io_channel
= NULL
;
1378 gps_close(&vgl
->vgpsd
->gpsd
);
1382 if (vgl
->realtime_record
&& vgl
->realtime_track
) {
1383 create_realtime_trackpoint(vgl
, TRUE
);
1384 if ((vgl
->realtime_track
->trackpoints
== NULL
) || (vgl
->realtime_track
->trackpoints
->next
== NULL
))
1385 vik_trw_layer_delete_track(vgl
->trw_children
[TRW_REALTIME
], vgl
->realtime_track_name
);
1386 vgl
->realtime_track
= NULL
;
1390 static void gps_start_stop_tracking_cb( gpointer layer_and_vlp
[2])
1392 VikGpsLayer
*vgl
= (VikGpsLayer
*)layer_and_vlp
[0];
1393 vgl
->realtime_tracking
= (vgl
->realtime_tracking
== FALSE
);
1395 /* Make sure we are still in the boat with libgps */
1396 g_assert((VIK_GPS_MODE_2D
== MODE_2D
) && (VIK_GPS_MODE_3D
== MODE_3D
));
1398 if (vgl
->realtime_tracking
) {
1399 vgl
->first_realtime_trackpoint
= TRUE
;
1400 if (!rt_gpsd_connect(vgl
, TRUE
)) {
1401 vgl
->first_realtime_trackpoint
= FALSE
;
1402 vgl
->realtime_tracking
= FALSE
;
1405 else { /* stop realtime tracking */
1406 vgl
->first_realtime_trackpoint
= FALSE
;
1407 rt_gpsd_disconnect(vgl
);
1410 #endif /* VIK_CONFIG_REALTIME_GPS_TRACKING */