]> git.street.me.uk Git - andy/viking.git/blob - src/datasource_gps.c
More data source fixes
[andy/viking.git] / src / datasource_gps.c
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  */
21 #include <string.h>
22 #include <glib/gprintf.h>
23
24 #include "viking.h"
25 #include "babel.h"
26 #include "gpx.h"
27 #include "acquire.h"
28
29 static gboolean gps_acquire_in_progress = FALSE;
30
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 );
35
36 VikDataSourceInterface vik_datasource_gps_interface = {
37   "Acquire from GPS",
38   "Acquired from GPS",
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
48 };
49
50 /*********************************************************
51  * Definitions and routines for acquiring data from GPS
52  *********************************************************/
53
54 /* widgets in progress dialog specific to GPS */
55 /* also counts needed for progress */
56 typedef struct {
57   GtkWidget *gps_label;
58   GtkWidget *ver_label;
59   GtkWidget *id_label;
60   GtkWidget *wp_label;
61   GtkWidget *trk_label;
62   GtkWidget *progress_label;
63   int total_count;
64   int count;
65 } gps_acq_dialog_widgets_t;
66
67 static void datasource_gps_get_cmd_string ( gpointer add_widgets_data_not_used, gchar **babelargs, gchar **input_file )
68 {
69   if (gps_acquire_in_progress) {
70     *babelargs = *input_file = NULL;
71   }
72
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" );
76 }
77
78 static void datasource_gps_cleanup ( gpointer data )
79 {
80   g_free ( data );
81   gps_acquire_in_progress = FALSE;
82 }
83
84 static void set_total_count(gint cnt, acq_dialog_widgets_t *w)
85 {
86   gchar s[128];
87   gdk_threads_enter();
88   if (w->ok) {
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;
94   }
95   gdk_threads_leave();
96 }
97
98 static void set_current_count(gint cnt, acq_dialog_widgets_t *w)
99 {
100   gchar s[128];
101   gdk_threads_enter();
102   if (w->ok) {
103     gps_acq_dialog_widgets_t *gps_data = (gps_acq_dialog_widgets_t *)w->specific_data;
104
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");
107     } else {
108       g_sprintf(s, "Downloaded %d %s.", cnt, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints");
109     }     
110     gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s );
111   }
112   gdk_threads_leave();
113 }
114
115 static void set_gps_info(const gchar *info, acq_dialog_widgets_t *w)
116 {
117   gchar s[256];
118   gdk_threads_enter();
119   if (w->ok) {
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 );
122   }
123   gdk_threads_leave();
124 }
125
126 /* 
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.
130  */
131 static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
132 {
133   gchar *line;
134   gps_acq_dialog_widgets_t *gps_data = (gps_acq_dialog_widgets_t *)w->specific_data;
135
136   switch(c) {
137   case BABEL_DIAG_OUTPUT:
138     line = (gchar *)data;
139
140     /* tells us how many items there will be */
141     if (strstr(line, "Xfer Wpt")) { 
142       gps_data->progress_label = gps_data->wp_label;
143     }
144     if (strstr(line, "Xfer Trk")) { 
145       gps_data->progress_label = gps_data->trk_label;
146     }
147     if (strstr(line, "PRDDAT")) {
148       gchar **tokens = g_strsplit(line, " ", 0);
149       gchar info[128];
150       int ilen = 0;
151       int i;
152
153       for (i=8; tokens[i] && ilen < sizeof(info)-2 && strcmp(tokens[i], "00"); i++) {
154         guint ch;
155         sscanf(tokens[i], "%x", &ch);
156         info[ilen++] = ch;
157       }
158       info[ilen++] = 0;
159       set_gps_info(info, w);
160     }
161     if (strstr(line, "RECORD")) { 
162       int lsb, msb, cnt;
163
164       sscanf(line+17, "%x", &lsb); 
165       sscanf(line+20, "%x", &msb);
166       cnt = lsb + msb * 256;
167       set_total_count(cnt, w);
168       gps_data->count = 0;
169     }
170     if ( strstr(line, "WPTDAT") || strstr(line, "TRKHDR") || strstr(line, "TRKDAT") ) {
171       gps_data->count++;
172       set_current_count(gps_data->count, w);
173     }
174     break;
175   case BABEL_DONE:
176     break;
177   default:
178     break;
179   }
180 }
181
182 gpointer datasource_gps_add_progress_widgets ( GtkWidget *dialog )
183 {
184   GtkWidget *gpslabel, *verlabel, *idlabel, *wplabel, *trklabel;
185
186   gps_acq_dialog_widgets_t *w_gps = g_malloc(sizeof(*w_gps));
187
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 ("");
193
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 );
197
198   gtk_widget_show_all ( dialog );
199
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;
206   return w_gps;
207 }