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