]> git.street.me.uk Git - andy/viking.git/blame - src/datasource_gps.c
Sort out the viewport header file a bit for better readability and in
[andy/viking.git] / src / datasource_gps.c
CommitLineData
7b3479e3
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
8aad4ca3
GB
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
7b3479e3 24#include <string.h>
4c77d5e0 25
7b3479e3 26#include <glib/gprintf.h>
4c77d5e0 27#include <glib/gi18n.h>
7b3479e3
EB
28
29#include "viking.h"
30#include "babel.h"
31#include "gpx.h"
32#include "acquire.h"
33
1a8437ab
AF
34#if GTK_CHECK_VERSION(2,6,0)
35#define USE_NEW_COMBO_BOX
36#endif
37
7b3479e3
EB
38static gboolean gps_acquire_in_progress = FALSE;
39
65f0ccab 40static gpointer datasource_gps_init_func ( );
7b3479e3 41static void datasource_gps_get_cmd_string ( gpointer add_widgets_data_not_used, gchar **babelargs, gchar **input_file );
65f0ccab 42static void datasource_gps_cleanup ( gpointer user_data );
7b3479e3 43static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w );
65f0ccab
AF
44static void datasource_gps_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
45static void datasource_gps_add_progress_widgets ( GtkWidget *dialog, gpointer user_data );
7b3479e3
EB
46
47VikDataSourceInterface vik_datasource_gps_interface = {
4c77d5e0
GB
48 N_("Acquire from GPS"),
49 N_("Acquired from GPS"),
7b3479e3 50 VIK_DATASOURCE_GPSBABEL_DIRECT,
805d282e 51 VIK_DATASOURCE_CREATENEWLAYER,
28c82d8b
EB
52 VIK_DATASOURCE_INPUTTYPE_NONE,
53 TRUE,
65f0ccab 54 (VikDataSourceInitFunc) datasource_gps_init_func,
92255687 55 (VikDataSourceCheckExistenceFunc) NULL,
65f0ccab 56 (VikDataSourceAddSetupWidgetsFunc) datasource_gps_add_setup_widgets,
7b3479e3 57 (VikDataSourceGetCmdStringFunc) datasource_gps_get_cmd_string,
7b3479e3
EB
58 (VikDataSourceProgressFunc) datasource_gps_progress,
59 (VikDataSourceAddProgressWidgetsFunc) datasource_gps_add_progress_widgets,
60 (VikDataSourceCleanupFunc) datasource_gps_cleanup
61};
62
63/*********************************************************
64 * Definitions and routines for acquiring data from GPS
65 *********************************************************/
66
65f0ccab 67/* widgets in setup dialog specific to GPS */
7b3479e3
EB
68/* widgets in progress dialog specific to GPS */
69/* also counts needed for progress */
70typedef struct {
65f0ccab
AF
71 /* setup dialog */
72 GtkWidget *proto_l;
73 GtkComboBox *proto_b;
74 GtkWidget *ser_l;
75 GtkComboBox *ser_b;
76
77 /* progress dialog */
7b3479e3
EB
78 GtkWidget *gps_label;
79 GtkWidget *ver_label;
80 GtkWidget *id_label;
81 GtkWidget *wp_label;
82 GtkWidget *trk_label;
83 GtkWidget *progress_label;
65f0ccab
AF
84
85 /* state */
7b3479e3
EB
86 int total_count;
87 int count;
65f0ccab
AF
88} gps_user_data_t;
89
90static gpointer datasource_gps_init_func ()
91{
92 return g_malloc (sizeof(gps_user_data_t));
93}
7b3479e3 94
65f0ccab 95static void datasource_gps_get_cmd_string ( gpointer user_data, gchar **babelargs, gchar **input_file )
7b3479e3 96{
cf7fdc15
GB
97 char *proto = NULL;
98 char *ser = NULL;
99 char *device = NULL;
1a8437ab 100#ifndef USE_NEW_COMBO_BOX
f3f1fa6c 101 GtkTreeIter iter;
1a8437ab 102#endif
65f0ccab
AF
103 gps_user_data_t *w = (gps_user_data_t *)user_data;
104
7b3479e3
EB
105 if (gps_acquire_in_progress) {
106 *babelargs = *input_file = NULL;
107 }
65f0ccab
AF
108
109 gps_acquire_in_progress = TRUE;
110
1a8437ab 111#ifdef USE_NEW_COMBO_BOX
cf7fdc15 112 proto = gtk_combo_box_get_active_text(GTK_COMBO_BOX(w->proto_b));
1a8437ab 113#else
cf7fdc15 114 proto = gtk_combo_box_get_active_iter(GTK_COMBO_BOX(w->proto_b),&iter);
1a8437ab 115#endif
cf7fdc15
GB
116 if (!strcmp(proto, "Garmin")) {
117 device = "garmin";
65f0ccab 118 } else {
cf7fdc15 119 device = "magellan";
65f0ccab 120 }
cf7fdc15
GB
121 *babelargs = g_strdup_printf("-D 9 -t -w -i %s", device);
122 /* device points to static content => no free */
123 device = NULL;
f3f1fa6c
AF
124
125 /* Old stuff */
1a8437ab 126#ifdef USE_NEW_COMBO_BOX
cf7fdc15 127 ser = gtk_combo_box_get_active_text(GTK_COMBO_BOX(w->ser_b));
1a8437ab 128#else
cf7fdc15 129 ser = gtk_combo_box_get_active_iter(GTK_COMBO_BOX(w->ser_b),&iter);
1a8437ab 130#endif
cf7fdc15 131 *input_file = g_strdup(ser);
7b3479e3 132
4c77d5e0 133 g_debug(_("using cmdline '%s' and file '%s'\n"), *babelargs, *input_file);
7b3479e3
EB
134}
135
65f0ccab 136static void datasource_gps_cleanup ( gpointer user_data )
7b3479e3 137{
65f0ccab 138 g_free ( user_data );
7b3479e3
EB
139 gps_acquire_in_progress = FALSE;
140}
141
142static void set_total_count(gint cnt, acq_dialog_widgets_t *w)
143{
cf7fdc15 144 gchar *s = NULL;
7b3479e3
EB
145 gdk_threads_enter();
146 if (w->ok) {
65f0ccab 147 gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data;
7760b0cf 148 s = g_strdup_printf(_("Downloading %d %s..."), cnt, (gps_data->progress_label == gps_data->wp_label) ? _("waypoints") : _("trackpoints"));
7b3479e3
EB
149 gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s );
150 gtk_widget_show ( gps_data->progress_label );
151 gps_data->total_count = cnt;
152 }
cf7fdc15 153 g_free(s); s = NULL;
7b3479e3
EB
154 gdk_threads_leave();
155}
156
157static void set_current_count(gint cnt, acq_dialog_widgets_t *w)
158{
cf7fdc15 159 gchar *s = NULL;
7b3479e3
EB
160 gdk_threads_enter();
161 if (w->ok) {
65f0ccab 162 gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data;
7b3479e3
EB
163
164 if (cnt < gps_data->total_count) {
4c77d5e0 165 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");
7b3479e3 166 } else {
4c77d5e0 167 s = g_strdup_printf(_("Downloaded %d %s."), cnt, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
7b3479e3
EB
168 }
169 gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s );
170 }
cf7fdc15 171 g_free(s); s = NULL;
7b3479e3
EB
172 gdk_threads_leave();
173}
174
175static void set_gps_info(const gchar *info, acq_dialog_widgets_t *w)
176{
cf7fdc15 177 gchar *s = NULL;
7b3479e3
EB
178 gdk_threads_enter();
179 if (w->ok) {
4c77d5e0 180 s = g_strdup_printf(_("GPS Device: %s"), info);
65f0ccab 181 gtk_label_set_text ( GTK_LABEL(((gps_user_data_t *)w->user_data)->gps_label), s );
7b3479e3 182 }
cf7fdc15 183 g_free(s); s = NULL;
7b3479e3
EB
184 gdk_threads_leave();
185}
186
187/*
188 * This routine relies on gpsbabel's diagnostic output to display the progress information.
189 * These outputs differ when different GPS devices are used, so we will need to test
190 * them on several and add the corresponding support.
191 */
192static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
193{
194 gchar *line;
65f0ccab 195 gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data;
7b3479e3 196
7b3479e3
EB
197 switch(c) {
198 case BABEL_DIAG_OUTPUT:
199 line = (gchar *)data;
200
201 /* tells us how many items there will be */
202 if (strstr(line, "Xfer Wpt")) {
203 gps_data->progress_label = gps_data->wp_label;
204 }
205 if (strstr(line, "Xfer Trk")) {
206 gps_data->progress_label = gps_data->trk_label;
207 }
208 if (strstr(line, "PRDDAT")) {
209 gchar **tokens = g_strsplit(line, " ", 0);
210 gchar info[128];
211 int ilen = 0;
212 int i;
c83b5ad9
QT
213 int n_tokens = 0;
214
215 while (tokens[n_tokens])
216 n_tokens++;
217
218 if (n_tokens > 8) {
219 for (i=8; tokens[i] && ilen < sizeof(info)-2 && strcmp(tokens[i], "00"); i++) {
220 guint ch;
221 sscanf(tokens[i], "%x", &ch);
222 info[ilen++] = ch;
223 }
224 info[ilen++] = 0;
225 set_gps_info(info, w);
7b3479e3 226 }
c83b5ad9 227 g_strfreev(tokens);
7b3479e3
EB
228 }
229 if (strstr(line, "RECORD")) {
230 int lsb, msb, cnt;
231
c83b5ad9
QT
232 if (strlen(line) > 20) {
233 sscanf(line+17, "%x", &lsb);
234 sscanf(line+20, "%x", &msb);
235 cnt = lsb + msb * 256;
236 set_total_count(cnt, w);
237 gps_data->count = 0;
238 }
7b3479e3
EB
239 }
240 if ( strstr(line, "WPTDAT") || strstr(line, "TRKHDR") || strstr(line, "TRKDAT") ) {
241 gps_data->count++;
242 set_current_count(gps_data->count, w);
243 }
244 break;
245 case BABEL_DONE:
246 break;
247 default:
248 break;
249 }
250}
251
65f0ccab
AF
252void datasource_gps_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
253{
254 gps_user_data_t *w = (gps_user_data_t *)user_data;
255 GtkTable* box;
256
4c77d5e0 257 w->proto_l = gtk_label_new (_("GPS Protocol:"));
65f0ccab
AF
258 w->proto_b = GTK_COMBO_BOX(gtk_combo_box_new_text ());
259 gtk_combo_box_append_text (w->proto_b, "Garmin");
260 gtk_combo_box_append_text (w->proto_b, "Magellan");
261 gtk_combo_box_set_active (w->proto_b, 0);
262 g_object_ref(w->proto_b);
263
4c77d5e0 264 w->ser_l = gtk_label_new (_("Serial Port:"));
65f0ccab
AF
265 w->ser_b = GTK_COMBO_BOX(gtk_combo_box_entry_new_text ());
266 gtk_combo_box_append_text (w->ser_b, "/dev/ttyS0");
267 gtk_combo_box_append_text (w->ser_b, "/dev/ttyS1");
268 gtk_combo_box_append_text (w->ser_b, "/dev/ttyUSB0");
269 gtk_combo_box_append_text (w->ser_b, "/dev/ttyUSB1");
7963d365 270 gtk_combo_box_append_text (w->ser_b, "usb:");
65f0ccab
AF
271 gtk_combo_box_set_active (w->ser_b, 0);
272 g_object_ref(w->ser_b);
273
274 box = GTK_TABLE(gtk_table_new(2, 2, FALSE));
275 gtk_table_attach_defaults(box, GTK_WIDGET(w->proto_l), 0, 1, 0, 1);
276 gtk_table_attach_defaults(box, GTK_WIDGET(w->proto_b), 1, 2, 0, 1);
277 gtk_table_attach_defaults(box, GTK_WIDGET(w->ser_l), 0, 1, 1, 2);
278 gtk_table_attach_defaults(box, GTK_WIDGET(w->ser_b), 1, 2, 1, 2);
279 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), GTK_WIDGET(box), FALSE, FALSE, 5 );
280
281 gtk_widget_show_all ( dialog );
282}
283
284void datasource_gps_add_progress_widgets ( GtkWidget *dialog, gpointer user_data )
7b3479e3
EB
285{
286 GtkWidget *gpslabel, *verlabel, *idlabel, *wplabel, *trklabel;
287
65f0ccab 288 gps_user_data_t *w_gps = (gps_user_data_t *)user_data;
7b3479e3 289
4c77d5e0 290 gpslabel = gtk_label_new (_("GPS device: N/A"));
7b3479e3
EB
291 verlabel = gtk_label_new ("");
292 idlabel = gtk_label_new ("");
293 wplabel = gtk_label_new ("");
294 trklabel = gtk_label_new ("");
295
296 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), gpslabel, FALSE, FALSE, 5 );
297 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), wplabel, FALSE, FALSE, 5 );
298 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), trklabel, FALSE, FALSE, 5 );
299
7b3479e3
EB
300 gtk_widget_show_all ( dialog );
301
302 w_gps->gps_label = gpslabel;
303 w_gps->id_label = idlabel;
304 w_gps->ver_label = verlabel;
305 w_gps->progress_label = w_gps->wp_label = wplabel;
306 w_gps->trk_label = trklabel;
307 w_gps->total_count = -1;
7b3479e3 308}