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 #if GTK_CHECK_VERSION(2,6,0)
30 #define USE_NEW_COMBO_BOX
33 static gboolean gps_acquire_in_progress = FALSE;
35 static gpointer datasource_gps_init_func ( );
36 static void datasource_gps_get_cmd_string ( gpointer add_widgets_data_not_used, gchar **babelargs, gchar **input_file );
37 static void datasource_gps_cleanup ( gpointer user_data );
38 static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w );
39 static void datasource_gps_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
40 static void datasource_gps_add_progress_widgets ( GtkWidget *dialog, gpointer user_data );
42 VikDataSourceInterface vik_datasource_gps_interface = {
45 VIK_DATASOURCE_GPSBABEL_DIRECT,
46 VIK_DATASOURCE_CREATENEWLAYER,
47 (VikDataSourceInitFunc) datasource_gps_init_func,
48 (VikDataSourceCheckExistenceFunc) NULL,
49 (VikDataSourceAddSetupWidgetsFunc) datasource_gps_add_setup_widgets,
50 (VikDataSourceGetCmdStringFunc) datasource_gps_get_cmd_string,
51 (VikDataSourceProgressFunc) datasource_gps_progress,
52 (VikDataSourceAddProgressWidgetsFunc) datasource_gps_add_progress_widgets,
53 (VikDataSourceCleanupFunc) datasource_gps_cleanup
56 /*********************************************************
57 * Definitions and routines for acquiring data from GPS
58 *********************************************************/
60 /* widgets in setup dialog specific to GPS */
61 /* widgets in progress dialog specific to GPS */
62 /* also counts needed for progress */
76 GtkWidget *progress_label;
83 static gpointer datasource_gps_init_func ()
85 return g_malloc (sizeof(gps_user_data_t));
88 static void datasource_gps_get_cmd_string ( gpointer user_data, gchar **babelargs, gchar **input_file )
93 #ifndef USE_NEW_COMBO_BOX
96 gps_user_data_t *w = (gps_user_data_t *)user_data;
98 if (gps_acquire_in_progress) {
99 *babelargs = *input_file = NULL;
102 gps_acquire_in_progress = TRUE;
104 #ifdef USE_NEW_COMBO_BOX
105 proto = gtk_combo_box_get_active_text(GTK_COMBO_BOX(w->proto_b));
107 proto = gtk_combo_box_get_active_iter(GTK_COMBO_BOX(w->proto_b),&iter);
109 if (!strcmp(proto, "Garmin")) {
114 *babelargs = g_strdup_printf("-D 9 -t -w -i %s", device);
115 /* device points to static content => no free */
119 #ifdef USE_NEW_COMBO_BOX
120 ser = gtk_combo_box_get_active_text(GTK_COMBO_BOX(w->ser_b));
122 ser = gtk_combo_box_get_active_iter(GTK_COMBO_BOX(w->ser_b),&iter);
124 *input_file = g_strdup(ser);
126 g_debug("using cmdline '%s' and file '%s'\n", *babelargs, *input_file);
129 static void datasource_gps_cleanup ( gpointer user_data )
131 g_free ( user_data );
132 gps_acquire_in_progress = FALSE;
135 static void set_total_count(gint cnt, acq_dialog_widgets_t *w)
140 gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data;
141 s = g_strdup_printf("Downloading %d %s...", cnt, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
142 gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s );
143 gtk_widget_show ( gps_data->progress_label );
144 gps_data->total_count = cnt;
150 static void set_current_count(gint cnt, acq_dialog_widgets_t *w)
155 gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data;
157 if (cnt < gps_data->total_count) {
158 s = g_strdup_printf("Downloaded %d out of %d %s...", cnt, gps_data->total_count, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
160 s = g_strdup_printf("Downloaded %d %s.", cnt, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
162 gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s );
168 static void set_gps_info(const gchar *info, acq_dialog_widgets_t *w)
173 s = g_strdup_printf("GPS Device: %s", info);
174 gtk_label_set_text ( GTK_LABEL(((gps_user_data_t *)w->user_data)->gps_label), s );
181 * This routine relies on gpsbabel's diagnostic output to display the progress information.
182 * These outputs differ when different GPS devices are used, so we will need to test
183 * them on several and add the corresponding support.
185 static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
188 gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data;
191 case BABEL_DIAG_OUTPUT:
192 line = (gchar *)data;
194 /* tells us how many items there will be */
195 if (strstr(line, "Xfer Wpt")) {
196 gps_data->progress_label = gps_data->wp_label;
198 if (strstr(line, "Xfer Trk")) {
199 gps_data->progress_label = gps_data->trk_label;
201 if (strstr(line, "PRDDAT")) {
202 gchar **tokens = g_strsplit(line, " ", 0);
208 while (tokens[n_tokens])
212 for (i=8; tokens[i] && ilen < sizeof(info)-2 && strcmp(tokens[i], "00"); i++) {
214 sscanf(tokens[i], "%x", &ch);
218 set_gps_info(info, w);
222 if (strstr(line, "RECORD")) {
225 if (strlen(line) > 20) {
226 sscanf(line+17, "%x", &lsb);
227 sscanf(line+20, "%x", &msb);
228 cnt = lsb + msb * 256;
229 set_total_count(cnt, w);
233 if ( strstr(line, "WPTDAT") || strstr(line, "TRKHDR") || strstr(line, "TRKDAT") ) {
235 set_current_count(gps_data->count, w);
245 void datasource_gps_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
247 gps_user_data_t *w = (gps_user_data_t *)user_data;
250 w->proto_l = gtk_label_new ("GPS Protocol:");
251 w->proto_b = GTK_COMBO_BOX(gtk_combo_box_new_text ());
252 gtk_combo_box_append_text (w->proto_b, "Garmin");
253 gtk_combo_box_append_text (w->proto_b, "Magellan");
254 gtk_combo_box_set_active (w->proto_b, 0);
255 g_object_ref(w->proto_b);
257 w->ser_l = gtk_label_new ("Serial Port:");
258 w->ser_b = GTK_COMBO_BOX(gtk_combo_box_entry_new_text ());
259 gtk_combo_box_append_text (w->ser_b, "/dev/ttyS0");
260 gtk_combo_box_append_text (w->ser_b, "/dev/ttyS1");
261 gtk_combo_box_append_text (w->ser_b, "/dev/ttyUSB0");
262 gtk_combo_box_append_text (w->ser_b, "/dev/ttyUSB1");
263 gtk_combo_box_append_text (w->ser_b, "usb:");
264 gtk_combo_box_set_active (w->ser_b, 0);
265 g_object_ref(w->ser_b);
267 box = GTK_TABLE(gtk_table_new(2, 2, FALSE));
268 gtk_table_attach_defaults(box, GTK_WIDGET(w->proto_l), 0, 1, 0, 1);
269 gtk_table_attach_defaults(box, GTK_WIDGET(w->proto_b), 1, 2, 0, 1);
270 gtk_table_attach_defaults(box, GTK_WIDGET(w->ser_l), 0, 1, 1, 2);
271 gtk_table_attach_defaults(box, GTK_WIDGET(w->ser_b), 1, 2, 1, 2);
272 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), GTK_WIDGET(box), FALSE, FALSE, 5 );
274 gtk_widget_show_all ( dialog );
277 void datasource_gps_add_progress_widgets ( GtkWidget *dialog, gpointer user_data )
279 GtkWidget *gpslabel, *verlabel, *idlabel, *wplabel, *trklabel;
281 gps_user_data_t *w_gps = (gps_user_data_t *)user_data;
283 gpslabel = gtk_label_new ("GPS device: N/A");
284 verlabel = gtk_label_new ("");
285 idlabel = gtk_label_new ("");
286 wplabel = gtk_label_new ("");
287 trklabel = gtk_label_new ("");
289 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), gpslabel, FALSE, FALSE, 5 );
290 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), wplabel, FALSE, FALSE, 5 );
291 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), trklabel, FALSE, FALSE, 5 );
293 gtk_widget_show_all ( dialog );
295 w_gps->gps_label = gpslabel;
296 w_gps->id_label = idlabel;
297 w_gps->ver_label = verlabel;
298 w_gps->progress_label = w_gps->wp_label = wplabel;
299 w_gps->trk_label = trklabel;
300 w_gps->total_count = -1;