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 <glib/gprintf.h>
29 static gboolean gps_acquire_in_progress = FALSE;
31 static void datasource_gps_get_cmd_string ( gpointer add_widgets_data_not_used, gchar **babelargs, gchar **input_file );
32 static void datasource_gps_cleanup ( gpointer data );
33 static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w );
34 gpointer datasource_gps_add_progress_widgets ( GtkWidget *dialog );
36 VikDataSourceInterface vik_datasource_gps_interface = {
39 VIK_DATASOURCE_GPSBABEL_DIRECT,
40 VIK_DATASOURCE_CREATENEWLAYER,
41 (VikDataSourceCheckExistenceFunc) NULL,
42 (VikDataSourceAddWidgetsFunc) NULL,
43 (VikDataSourceGetCmdStringFunc) datasource_gps_get_cmd_string,
44 (VikDataSourceFirstCleanupFunc) NULL,
45 (VikDataSourceProgressFunc) datasource_gps_progress,
46 (VikDataSourceAddProgressWidgetsFunc) datasource_gps_add_progress_widgets,
47 (VikDataSourceCleanupFunc) datasource_gps_cleanup
50 /*********************************************************
51 * Definitions and routines for acquiring data from GPS
52 *********************************************************/
54 /* widgets in progress dialog specific to GPS */
55 /* also counts needed for progress */
62 GtkWidget *progress_label;
65 } gps_acq_dialog_widgets_t;
67 static void datasource_gps_get_cmd_string ( gpointer add_widgets_data_not_used, gchar **babelargs, gchar **input_file )
69 if (gps_acquire_in_progress) {
70 *babelargs = *input_file = NULL;
73 gps_acquire_in_progress = TRUE;
74 *babelargs = g_strdup_printf("%s", "-D 9 -t -w -i garmin");
75 *input_file = g_strdup_printf("%s", "/dev/ttyS1" );
78 static void datasource_gps_cleanup ( gpointer data )
81 gps_acquire_in_progress = FALSE;
84 static void set_total_count(gint cnt, acq_dialog_widgets_t *w)
89 gps_acq_dialog_widgets_t *gps_data = (gps_acq_dialog_widgets_t *)w->specific_data;
90 g_sprintf(s, "Downloading %d %s...", cnt, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
91 gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s );
92 gtk_widget_show ( gps_data->progress_label );
93 gps_data->total_count = cnt;
98 static void set_current_count(gint cnt, acq_dialog_widgets_t *w)
103 gps_acq_dialog_widgets_t *gps_data = (gps_acq_dialog_widgets_t *)w->specific_data;
105 if (cnt < gps_data->total_count) {
106 g_sprintf(s, "Downloaded %d out of %d %s...", cnt, gps_data->total_count, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
108 g_sprintf(s, "Downloaded %d %s.", cnt, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
110 gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s );
115 static void set_gps_info(const gchar *info, acq_dialog_widgets_t *w)
120 g_sprintf(s, "GPS Device: %s", info);
121 gtk_label_set_text ( GTK_LABEL(((gps_acq_dialog_widgets_t *)w->specific_data)->gps_label), s );
127 * This routine relies on gpsbabel's diagnostic output to display the progress information.
128 * These outputs differ when different GPS devices are used, so we will need to test
129 * them on several and add the corresponding support.
131 static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
134 gps_acq_dialog_widgets_t *gps_data = (gps_acq_dialog_widgets_t *)w->specific_data;
137 case BABEL_DIAG_OUTPUT:
138 line = (gchar *)data;
140 /* tells us how many items there will be */
141 if (strstr(line, "Xfer Wpt")) {
142 gps_data->progress_label = gps_data->wp_label;
144 if (strstr(line, "Xfer Trk")) {
145 gps_data->progress_label = gps_data->trk_label;
147 if (strstr(line, "PRDDAT")) {
148 gchar **tokens = g_strsplit(line, " ", 0);
153 for (i=8; tokens[i] && ilen < sizeof(info)-2 && strcmp(tokens[i], "00"); i++) {
155 sscanf(tokens[i], "%x", &ch);
159 set_gps_info(info, w);
161 if (strstr(line, "RECORD")) {
164 sscanf(line+17, "%x", &lsb);
165 sscanf(line+20, "%x", &msb);
166 cnt = lsb + msb * 256;
167 set_total_count(cnt, w);
170 if ( strstr(line, "WPTDAT") || strstr(line, "TRKHDR") || strstr(line, "TRKDAT") ) {
172 set_current_count(gps_data->count, w);
182 gpointer datasource_gps_add_progress_widgets ( GtkWidget *dialog )
184 GtkWidget *gpslabel, *verlabel, *idlabel, *wplabel, *trklabel;
186 gps_acq_dialog_widgets_t *w_gps = g_malloc(sizeof(*w_gps));
188 gpslabel = gtk_label_new ("GPS device: N/A");
189 verlabel = gtk_label_new ("");
190 idlabel = gtk_label_new ("");
191 wplabel = gtk_label_new ("");
192 trklabel = gtk_label_new ("");
194 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), gpslabel, FALSE, FALSE, 5 );
195 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), wplabel, FALSE, FALSE, 5 );
196 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), trklabel, FALSE, FALSE, 5 );
198 gtk_widget_show_all ( dialog );
200 w_gps->gps_label = gpslabel;
201 w_gps->id_label = idlabel;
202 w_gps->ver_label = verlabel;
203 w_gps->progress_label = w_gps->wp_label = wplabel;
204 w_gps->trk_label = trklabel;
205 w_gps->total_count = -1;