]>
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 | */ | |
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 |
38 | static gboolean gps_acquire_in_progress = FALSE; |
39 | ||
65f0ccab | 40 | static gpointer datasource_gps_init_func ( ); |
7b3479e3 | 41 | static void datasource_gps_get_cmd_string ( gpointer add_widgets_data_not_used, gchar **babelargs, gchar **input_file ); |
65f0ccab | 42 | static void datasource_gps_cleanup ( gpointer user_data ); |
7b3479e3 | 43 | static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w ); |
65f0ccab AF |
44 | static void datasource_gps_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data ); |
45 | static void datasource_gps_add_progress_widgets ( GtkWidget *dialog, gpointer user_data ); | |
7b3479e3 EB |
46 | |
47 | VikDataSourceInterface 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 */ | |
70 | typedef 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 | ||
90 | static gpointer datasource_gps_init_func () | |
91 | { | |
92 | return g_malloc (sizeof(gps_user_data_t)); | |
93 | } | |
7b3479e3 | 94 | |
65f0ccab | 95 | static 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 | 136 | static 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 | ||
142 | static 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; |
97634600 GB |
148 | const gchar *tmp_str; |
149 | if (gps_data->progress_label == gps_data->wp_label) | |
150 | tmp_str = ngettext("Downloading %d waypoint...", "Downloading %d waypoints...", cnt); | |
151 | else | |
152 | tmp_str = ngettext("Downloading %d trackpoint...", "Downloading %d trackpoints...", cnt); | |
153 | s = g_strdup_printf(tmp_str, cnt); | |
7b3479e3 EB |
154 | gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s ); |
155 | gtk_widget_show ( gps_data->progress_label ); | |
156 | gps_data->total_count = cnt; | |
157 | } | |
cf7fdc15 | 158 | g_free(s); s = NULL; |
7b3479e3 EB |
159 | gdk_threads_leave(); |
160 | } | |
161 | ||
162 | static void set_current_count(gint cnt, acq_dialog_widgets_t *w) | |
163 | { | |
cf7fdc15 | 164 | gchar *s = NULL; |
7b3479e3 EB |
165 | gdk_threads_enter(); |
166 | if (w->ok) { | |
65f0ccab | 167 | gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data; |
7b3479e3 EB |
168 | |
169 | if (cnt < gps_data->total_count) { | |
4c77d5e0 | 170 | 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 | 171 | } else { |
4c77d5e0 | 172 | s = g_strdup_printf(_("Downloaded %d %s."), cnt, (gps_data->progress_label == gps_data->wp_label) ? "waypoints" : "trackpoints"); |
7b3479e3 EB |
173 | } |
174 | gtk_label_set_text ( GTK_LABEL(gps_data->progress_label), s ); | |
175 | } | |
cf7fdc15 | 176 | g_free(s); s = NULL; |
7b3479e3 EB |
177 | gdk_threads_leave(); |
178 | } | |
179 | ||
180 | static void set_gps_info(const gchar *info, acq_dialog_widgets_t *w) | |
181 | { | |
cf7fdc15 | 182 | gchar *s = NULL; |
7b3479e3 EB |
183 | gdk_threads_enter(); |
184 | if (w->ok) { | |
4c77d5e0 | 185 | s = g_strdup_printf(_("GPS Device: %s"), info); |
65f0ccab | 186 | gtk_label_set_text ( GTK_LABEL(((gps_user_data_t *)w->user_data)->gps_label), s ); |
7b3479e3 | 187 | } |
cf7fdc15 | 188 | g_free(s); s = NULL; |
7b3479e3 EB |
189 | gdk_threads_leave(); |
190 | } | |
191 | ||
192 | /* | |
193 | * This routine relies on gpsbabel's diagnostic output to display the progress information. | |
194 | * These outputs differ when different GPS devices are used, so we will need to test | |
195 | * them on several and add the corresponding support. | |
196 | */ | |
197 | static void datasource_gps_progress ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w ) | |
198 | { | |
199 | gchar *line; | |
65f0ccab | 200 | gps_user_data_t *gps_data = (gps_user_data_t *)w->user_data; |
7b3479e3 | 201 | |
7b3479e3 EB |
202 | switch(c) { |
203 | case BABEL_DIAG_OUTPUT: | |
204 | line = (gchar *)data; | |
205 | ||
206 | /* tells us how many items there will be */ | |
207 | if (strstr(line, "Xfer Wpt")) { | |
208 | gps_data->progress_label = gps_data->wp_label; | |
209 | } | |
210 | if (strstr(line, "Xfer Trk")) { | |
211 | gps_data->progress_label = gps_data->trk_label; | |
212 | } | |
213 | if (strstr(line, "PRDDAT")) { | |
214 | gchar **tokens = g_strsplit(line, " ", 0); | |
215 | gchar info[128]; | |
216 | int ilen = 0; | |
217 | int i; | |
c83b5ad9 QT |
218 | int n_tokens = 0; |
219 | ||
220 | while (tokens[n_tokens]) | |
221 | n_tokens++; | |
222 | ||
223 | if (n_tokens > 8) { | |
224 | for (i=8; tokens[i] && ilen < sizeof(info)-2 && strcmp(tokens[i], "00"); i++) { | |
225 | guint ch; | |
226 | sscanf(tokens[i], "%x", &ch); | |
227 | info[ilen++] = ch; | |
228 | } | |
229 | info[ilen++] = 0; | |
230 | set_gps_info(info, w); | |
7b3479e3 | 231 | } |
c83b5ad9 | 232 | g_strfreev(tokens); |
7b3479e3 EB |
233 | } |
234 | if (strstr(line, "RECORD")) { | |
235 | int lsb, msb, cnt; | |
236 | ||
c83b5ad9 QT |
237 | if (strlen(line) > 20) { |
238 | sscanf(line+17, "%x", &lsb); | |
239 | sscanf(line+20, "%x", &msb); | |
240 | cnt = lsb + msb * 256; | |
241 | set_total_count(cnt, w); | |
242 | gps_data->count = 0; | |
243 | } | |
7b3479e3 EB |
244 | } |
245 | if ( strstr(line, "WPTDAT") || strstr(line, "TRKHDR") || strstr(line, "TRKDAT") ) { | |
246 | gps_data->count++; | |
247 | set_current_count(gps_data->count, w); | |
248 | } | |
249 | break; | |
250 | case BABEL_DONE: | |
251 | break; | |
252 | default: | |
253 | break; | |
254 | } | |
255 | } | |
256 | ||
65f0ccab AF |
257 | void datasource_gps_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data ) |
258 | { | |
259 | gps_user_data_t *w = (gps_user_data_t *)user_data; | |
260 | GtkTable* box; | |
261 | ||
4c77d5e0 | 262 | w->proto_l = gtk_label_new (_("GPS Protocol:")); |
65f0ccab AF |
263 | w->proto_b = GTK_COMBO_BOX(gtk_combo_box_new_text ()); |
264 | gtk_combo_box_append_text (w->proto_b, "Garmin"); | |
265 | gtk_combo_box_append_text (w->proto_b, "Magellan"); | |
266 | gtk_combo_box_set_active (w->proto_b, 0); | |
267 | g_object_ref(w->proto_b); | |
268 | ||
4c77d5e0 | 269 | w->ser_l = gtk_label_new (_("Serial Port:")); |
65f0ccab AF |
270 | w->ser_b = GTK_COMBO_BOX(gtk_combo_box_entry_new_text ()); |
271 | gtk_combo_box_append_text (w->ser_b, "/dev/ttyS0"); | |
272 | gtk_combo_box_append_text (w->ser_b, "/dev/ttyS1"); | |
273 | gtk_combo_box_append_text (w->ser_b, "/dev/ttyUSB0"); | |
274 | gtk_combo_box_append_text (w->ser_b, "/dev/ttyUSB1"); | |
7963d365 | 275 | gtk_combo_box_append_text (w->ser_b, "usb:"); |
65f0ccab AF |
276 | gtk_combo_box_set_active (w->ser_b, 0); |
277 | g_object_ref(w->ser_b); | |
278 | ||
279 | box = GTK_TABLE(gtk_table_new(2, 2, FALSE)); | |
280 | gtk_table_attach_defaults(box, GTK_WIDGET(w->proto_l), 0, 1, 0, 1); | |
281 | gtk_table_attach_defaults(box, GTK_WIDGET(w->proto_b), 1, 2, 0, 1); | |
282 | gtk_table_attach_defaults(box, GTK_WIDGET(w->ser_l), 0, 1, 1, 2); | |
283 | gtk_table_attach_defaults(box, GTK_WIDGET(w->ser_b), 1, 2, 1, 2); | |
284 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), GTK_WIDGET(box), FALSE, FALSE, 5 ); | |
285 | ||
286 | gtk_widget_show_all ( dialog ); | |
287 | } | |
288 | ||
289 | void datasource_gps_add_progress_widgets ( GtkWidget *dialog, gpointer user_data ) | |
7b3479e3 EB |
290 | { |
291 | GtkWidget *gpslabel, *verlabel, *idlabel, *wplabel, *trklabel; | |
292 | ||
65f0ccab | 293 | gps_user_data_t *w_gps = (gps_user_data_t *)user_data; |
7b3479e3 | 294 | |
4c77d5e0 | 295 | gpslabel = gtk_label_new (_("GPS device: N/A")); |
7b3479e3 EB |
296 | verlabel = gtk_label_new (""); |
297 | idlabel = gtk_label_new (""); | |
298 | wplabel = gtk_label_new (""); | |
299 | trklabel = gtk_label_new (""); | |
300 | ||
301 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), gpslabel, FALSE, FALSE, 5 ); | |
302 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), wplabel, FALSE, FALSE, 5 ); | |
303 | gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), trklabel, FALSE, FALSE, 5 ); | |
304 | ||
7b3479e3 EB |
305 | gtk_widget_show_all ( dialog ); |
306 | ||
307 | w_gps->gps_label = gpslabel; | |
308 | w_gps->id_label = idlabel; | |
309 | w_gps->ver_label = verlabel; | |
310 | w_gps->progress_label = w_gps->wp_label = wplabel; | |
311 | w_gps->trk_label = trklabel; | |
312 | w_gps->total_count = -1; | |
7b3479e3 | 313 | } |