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 gpointer datasource_gps_init_func ( );
32 static void datasource_gps_get_cmd_string ( gpointer add_widgets_data_not_used, gchar **babelargs, gchar **input_file );
33 static void datasource_gps_cleanup ( gpointer user_data );
34 static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w );
35 static void datasource_gps_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
36 static void datasource_gps_add_progress_widgets ( GtkWidget *dialog, gpointer user_data );
38 VikDataSourceInterface vik_datasource_gps_interface = {
41 VIK_DATASOURCE_GPSBABEL_DIRECT,
42 VIK_DATASOURCE_CREATENEWLAYER,
43 (VikDataSourceInitFunc) datasource_gps_init_func,
44 (VikDataSourceCheckExistenceFunc) NULL,
45 (VikDataSourceAddSetupWidgetsFunc) datasource_gps_add_setup_widgets,
46 (VikDataSourceGetCmdStringFunc) datasource_gps_get_cmd_string,
47 (VikDataSourceProgressFunc) datasource_gps_progress,
48 (VikDataSourceAddProgressWidgetsFunc) datasource_gps_add_progress_widgets,
49 (VikDataSourceCleanupFunc) datasource_gps_cleanup
52 /*********************************************************
53 * Definitions and routines for acquiring data from GPS
54 *********************************************************/
56 /* widgets in setup dialog specific to GPS */
57 /* widgets in progress dialog specific to GPS */
58 /* also counts needed for progress */
72 GtkWidget *progress_label;
79 static gpointer datasource_gps_init_func ()
81 return g_malloc (sizeof(gps_user_data_t));
84 static void datasource_gps_get_cmd_string ( gpointer user_data, gchar **babelargs, gchar **input_file )
86 gps_user_data_t *w = (gps_user_data_t *)user_data;
88 if (gps_acquire_in_progress) {
89 *babelargs = *input_file = NULL;
92 gps_acquire_in_progress = TRUE;
94 if (!strcmp(gtk_combo_box_get_active_text(GTK_COMBO_BOX(w->proto_b)), "Garmin")) {
95 *babelargs = g_strdup_printf("%s", "-D 9 -t -w -i garmin");
97 *babelargs = g_strdup_printf("%s", "-D 9 -t -w -i magellan");
99 *input_file = g_strdup_printf("%s", gtk_combo_box_get_active_text(GTK_COMBO_BOX(w->ser_b)));
101 fprintf(stderr, "using cmdline '%s' and file '%s'\n", *babelargs, *input_file);
104 static void datasource_gps_cleanup ( gpointer user_data )
106 g_free ( user_data );
107 gps_acquire_in_progress = FALSE;
110 static void set_total_count(gint cnt, acq_dialog_widgets_t *w)
115 gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data;
116 g_sprintf(s, "Downloading %d %s...", cnt, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
117 gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s );
118 gtk_widget_show ( gps_data->progress_label );
119 gps_data->total_count = cnt;
124 static void set_current_count(gint cnt, acq_dialog_widgets_t *w)
129 gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data;
131 if (cnt < gps_data->total_count) {
132 g_sprintf(s, "Downloaded %d out of %d %s...", cnt, gps_data->total_count, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
134 g_sprintf(s, "Downloaded %d %s.", cnt, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
136 gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s );
141 static void set_gps_info(const gchar *info, acq_dialog_widgets_t *w)
146 g_sprintf(s, "GPS Device: %s", info);
147 gtk_label_set_text ( GTK_LABEL(((gps_user_data_t *)w->user_data)->gps_label), s );
153 * This routine relies on gpsbabel's diagnostic output to display the progress information.
154 * These outputs differ when different GPS devices are used, so we will need to test
155 * them on several and add the corresponding support.
157 static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
160 gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data;
163 case BABEL_DIAG_OUTPUT:
164 line = (gchar *)data;
166 /* tells us how many items there will be */
167 if (strstr(line, "Xfer Wpt")) {
168 gps_data->progress_label = gps_data->wp_label;
170 if (strstr(line, "Xfer Trk")) {
171 gps_data->progress_label = gps_data->trk_label;
173 if (strstr(line, "PRDDAT")) {
174 gchar **tokens = g_strsplit(line, " ", 0);
179 for (i=8; tokens[i] && ilen < sizeof(info)-2 && strcmp(tokens[i], "00"); i++) {
181 sscanf(tokens[i], "%x", &ch);
185 set_gps_info(info, w);
187 if (strstr(line, "RECORD")) {
190 sscanf(line+17, "%x", &lsb);
191 sscanf(line+20, "%x", &msb);
192 cnt = lsb + msb * 256;
193 set_total_count(cnt, w);
196 if ( strstr(line, "WPTDAT") || strstr(line, "TRKHDR") || strstr(line, "TRKDAT") ) {
198 set_current_count(gps_data->count, w);
208 void datasource_gps_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
210 gps_user_data_t *w = (gps_user_data_t *)user_data;
213 w->proto_l = gtk_label_new ("GPS Protocol:");
214 w->proto_b = GTK_COMBO_BOX(gtk_combo_box_new_text ());
215 gtk_combo_box_append_text (w->proto_b, "Garmin");
216 gtk_combo_box_append_text (w->proto_b, "Magellan");
217 gtk_combo_box_set_active (w->proto_b, 0);
218 g_object_ref(w->proto_b);
220 w->ser_l = gtk_label_new ("Serial Port:");
221 w->ser_b = GTK_COMBO_BOX(gtk_combo_box_entry_new_text ());
222 gtk_combo_box_append_text (w->ser_b, "/dev/ttyS0");
223 gtk_combo_box_append_text (w->ser_b, "/dev/ttyS1");
224 gtk_combo_box_append_text (w->ser_b, "/dev/ttyUSB0");
225 gtk_combo_box_append_text (w->ser_b, "/dev/ttyUSB1");
226 gtk_combo_box_set_active (w->ser_b, 0);
227 g_object_ref(w->ser_b);
229 box = GTK_TABLE(gtk_table_new(2, 2, FALSE));
230 gtk_table_attach_defaults(box, GTK_WIDGET(w->proto_l), 0, 1, 0, 1);
231 gtk_table_attach_defaults(box, GTK_WIDGET(w->proto_b), 1, 2, 0, 1);
232 gtk_table_attach_defaults(box, GTK_WIDGET(w->ser_l), 0, 1, 1, 2);
233 gtk_table_attach_defaults(box, GTK_WIDGET(w->ser_b), 1, 2, 1, 2);
234 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), GTK_WIDGET(box), FALSE, FALSE, 5 );
236 gtk_widget_show_all ( dialog );
239 void datasource_gps_add_progress_widgets ( GtkWidget *dialog, gpointer user_data )
241 GtkWidget *gpslabel, *verlabel, *idlabel, *wplabel, *trklabel;
243 gps_user_data_t *w_gps = (gps_user_data_t *)user_data;
245 gpslabel = gtk_label_new ("GPS device: N/A");
246 verlabel = gtk_label_new ("");
247 idlabel = gtk_label_new ("");
248 wplabel = gtk_label_new ("");
249 trklabel = gtk_label_new ("");
251 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), gpslabel, FALSE, FALSE, 5 );
252 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), wplabel, FALSE, FALSE, 5 );
253 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), trklabel, FALSE, FALSE, 5 );
255 gtk_widget_show_all ( dialog );
257 w_gps->gps_label = gpslabel;
258 w_gps->id_label = idlabel;
259 w_gps->ver_label = verlabel;
260 w_gps->progress_label = w_gps->wp_label = wplabel;
261 w_gps->trk_label = trklabel;
262 w_gps->total_count = -1;