- GritsPluginGPS *gps_state = (GritsPluginGPS *)user_data;
-
- assert(gps_state);
-
- g_debug("gps interval changed, value = %f",
- gtk_range_get_value(GTK_RANGE(widget)));
-
- if (gtk_toggle_button_get_active(
- GTK_TOGGLE_BUTTON(gps_state->ui.gps_log_checkbox))) {
- assert(gps_state->ui.gps_log_timeout_id != 0);
-
- /* disable old timeout */
- g_source_remove(gps_state->ui.gps_log_timeout_id);
- gps_state->ui.gps_log_timeout_id = 0;
-
- /* Schedule new log file write */
- gps_state->ui.gps_log_timeout_id = g_timeout_add(
- gtk_range_get_value(GTK_RANGE(widget))*1000,
- gps_write_log, gps_state);
- gps_write_log(gps_state);
- }
-
- return FALSE;
-}
-
-static gboolean
-gps_write_log(gpointer data)
-{
- GritsPluginGPS *gps_state = (GritsPluginGPS *)data;
- struct gps_data_t *gps_data = &gps_state->gps_data;
- char buf[256];
- char filename[256];
- int fd;
- gboolean new_file = FALSE;
-
- if (gps_data == NULL) {
- g_warning("Skipped write to GPS log file: "
- "can not get GPS coordinates.");
- GPS_STATUS(gps_state, "Skipped write to GPS log file: "
- "can not get GPS coordinates.");
- return TRUE;
- }
-
- /* get filename from text entry box. If empty, generate a name from
- * the date and time and set it.
- */
- if (strlen(gtk_entry_get_text(GTK_ENTRY(gps_state->ui.gps_log_filename_entry)))
- == 0) {
- snprintf(filename, sizeof(filename),
- "%sT%s.%s",
- gps_get_date_string(gps_state->gps_data.fix.time),
- gps_get_time_string(gps_state->gps_data.fix.time),
- GPS_LOG_EXT);
- gtk_entry_set_text(GTK_ENTRY(gps_state->ui.gps_log_filename_entry),
- filename);
- }
-
- strncpy(filename,
- gtk_entry_get_text(GTK_ENTRY(gps_state->ui.gps_log_filename_entry)),
- sizeof (filename));
-
- if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
- new_file = TRUE;
- }
-
- if ((fd = open(filename, O_CREAT|O_APPEND|O_WRONLY, 0644)) == -1) {
- g_warning("Error opening log file %s: %s",
- filename, strerror(errno));
- return FALSE;
- }
-
- if (new_file) {
- /* write header and reset record counter */
- snprintf(buf, sizeof(buf),
- "No,Date,Time,Lat,Lon,Ele,Head,Speed,RTR\n");
- if (write(fd, buf, strlen(buf)) == -1) {
- g_warning("Error writing header to log file %s: %s",
- filename, strerror(errno));
- }
- gps_state->ui.gps_log_number = 1;
- }
-
- /* Write log entry. Make sure this matches the header */
- /* "No,Date,Time,Lat,Lon,Ele,Head,Speed,Fix,RTR\n" */
- /* RTR values: T=time, B=button push, S=speed, D=distance */
- snprintf(buf, sizeof(buf), "%d,%s,%s,%lf,%lf,%lf,%lf,%lf,%c\n",
- gps_state->ui.gps_log_number++,
- gps_get_date_string(gps_state->gps_data.fix.time),
- gps_get_time_string(gps_state->gps_data.fix.time),
-// gps_data->fix.time,
- gps_data->fix.latitude,
- gps_data->fix.longitude,
- gps_data->fix.altitude * METERS_TO_FEET,
- gps_data->fix.track,
- gps_data->fix.speed * METERS_TO_FEET,
- 'T'); /* position due to timer expired */
-
- if (write(fd, buf, strlen(buf)) == -1) {
- g_warning("Could not write log number %d to log file %s: %s",
- gps_state->ui.gps_log_number-1, filename, strerror(errno));
- }
- close(fd);
-
- GPS_STATUS(gps_state, "Updated GPS log file %s.", filename);
-
- /* reschedule */
- return TRUE;
-}
-
-static gboolean
-on_gps_follow_clicked_event (GtkWidget *widget, gpointer user_data)
-{
- GritsPluginGPS *gps_state = (GritsPluginGPS *)user_data;
-
- g_debug("on_gps_follow_clicked_event called!, button status %d",
- gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)));
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
- gps_state->follow_gps = TRUE;
- else
- gps_state->follow_gps = FALSE;
-
- return FALSE;
-}
-
-
-static void
-gps_init_track_log_frame(GritsPluginGPS *gps_state, GtkWidget *gbox)
-{
- /* Track log box with enable checkbox and filename entry */
- GtkWidget *gps_log_frame = gtk_frame_new ("Track Log");
- GtkWidget *lbox = gtk_vbox_new (FALSE, 2);
- gtk_container_add (GTK_CONTAINER (gps_log_frame), lbox);
- gtk_box_pack_start (GTK_BOX(gbox), gps_log_frame,
- FALSE, FALSE, 0);
-
- gps_state->ui.gps_log_checkbox = gtk_check_button_new_with_label("Log Position to File");
- g_signal_connect (G_OBJECT (gps_state->ui.gps_log_checkbox), "clicked",
- G_CALLBACK (on_gps_log_clicked_event),
- (gpointer)gps_state);
- gtk_box_pack_start (GTK_BOX(lbox), gps_state->ui.gps_log_checkbox,
- FALSE, FALSE, 0);
-
- /* Set up filename entry box */
- GtkWidget *fbox = gtk_hbox_new (FALSE, 2);
- GtkWidget *filename_label = gtk_label_new ("Filename:");
- gtk_box_pack_start (GTK_BOX(fbox), filename_label, FALSE, FALSE, 0);
- gps_state->ui.gps_log_filename_entry = gtk_entry_new();
- gtk_box_pack_start (GTK_BOX(fbox), gps_state->ui.gps_log_filename_entry,
- TRUE, TRUE, 0);
- gtk_box_pack_start (GTK_BOX(lbox), fbox, FALSE, FALSE, 0);
-
- /* set up gps log interval slider */
- GtkWidget *ubox = gtk_hbox_new (FALSE, 4);
- GtkWidget *interval_label = gtk_label_new ("Update Interval:");
- gtk_box_pack_start (GTK_BOX(ubox), interval_label, FALSE, FALSE, 0);
- gps_state->ui.gps_log_interval_slider =
- gtk_hscale_new_with_range(1.0, 600.0, 30.0);
- gtk_range_set_value (GTK_RANGE(gps_state->ui.gps_log_interval_slider),
- GPS_LOG_DEFAULT_UPDATE_INTERVAL);
- g_signal_connect (G_OBJECT (gps_state->ui.gps_log_interval_slider),
- "value-changed",
- G_CALLBACK(on_gps_log_interval_changed_event),
- (gpointer)gps_state);
- gtk_range_set_increments (GTK_RANGE(gps_state->ui.gps_log_interval_slider),
- 10.0 /* step */, 30.0 /* page up/down */);
- gtk_range_set_update_policy (GTK_RANGE(gps_state->ui.gps_log_interval_slider),
- GTK_UPDATE_DELAYED);
- gtk_box_pack_start (GTK_BOX(ubox), gps_state->ui.gps_log_interval_slider,
- TRUE, TRUE, 0);
- gtk_box_pack_start (GTK_BOX(lbox), ubox, FALSE, FALSE, 0);
-}
-
-static gboolean
-on_gps_log_clicked_event (GtkWidget *widget, gpointer user_data)
-{
- GritsPluginGPS *gps_state = (GritsPluginGPS *)user_data;
-
- g_debug("on_gps_log_clicked_event called!");
-
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) {
- gps_write_log(gps_state);
-
- /* Schedule log file write */
- gps_state->ui.gps_log_timeout_id = g_timeout_add(
- gtk_range_get_value(
- GTK_RANGE(gps_state->ui.gps_log_interval_slider))*1000,
- gps_write_log, gps_state);
- } else {
- /* button unchecked */
- g_source_remove(gps_state->ui.gps_log_timeout_id);
- gps_state->ui.gps_log_timeout_id = 0;
- g_debug("Closed log file.");
- }
-
- return FALSE;
-}
-
-
-static void
-gps_init_status_info(GritsPluginGPS *gps_state, GtkWidget *gbox)
-{
- gps_state->ui.gps_status_frame = gtk_frame_new ("GPS Data");
- gps_state->ui.gps_status_table = gtk_table_new (5, 2, TRUE);
- gtk_container_add (GTK_CONTAINER (gps_state->ui.gps_status_frame),
- gps_state->ui.gps_status_table);
-
- /* gps data table setup */
- int i;
- for (i = 0; i < sizeof(gps_table)/sizeof(*gps_table); i++) {
- gps_table[i].label_widget = gtk_label_new (gps_table[i].label);
- gtk_label_set_justify(GTK_LABEL(gps_table[i].label_widget),
- GTK_JUSTIFY_LEFT);
- gtk_table_attach( GTK_TABLE(gps_state->ui.gps_status_table),
- gps_table[i].label_widget, 0, 1, i, i+1, 0, 0, 0, 0);
- gps_table[i].value_widget = gtk_label_new (gps_table[i].initial_val);
- gtk_table_attach( GTK_TABLE(gps_state->ui.gps_status_table),
- gps_table[i].value_widget, 1, 2, i, i+1, 0, 0, 0, 0);
-
- PangoFontDescription *font_desc = pango_font_description_new ();
- pango_font_description_set_size (font_desc,
- gps_table[i].font_size*PANGO_SCALE);
- gtk_widget_modify_font (gps_table[i].label_widget, font_desc);
- gtk_widget_modify_font (gps_table[i].value_widget, font_desc);
- pango_font_description_free (font_desc);
- }
- gtk_box_pack_start (GTK_BOX(gbox), gps_state->ui.gps_status_frame,
- FALSE, FALSE, 0);
-
- /* Start UI refresh task, which will reschedule itself periodically. */
- gps_redraw_all(gps_state);
- gps_state->gps_update_timeout_id = g_timeout_add(
- GPS_UPDATE_INTERVAL*1000,
- gps_redraw_all, gps_state);
-