2 * Copyright (C) 2012 Adam Boggs <boggs@aircrafter.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * If gpsd connection fails, try to connect again periodically.
20 * If gps stops sending data there should be an indication that it's stale.
31 #include <glib/gstdio.h>
39 #include "gps-plugin.h"
41 #include "../aweather-location.h"
43 /* interval to update map with new gps data in seconds. */
44 #define GPS_UPDATE_INTERVAL (2)
46 /* Filename and search path to use for gps marker, should be configurable */
47 #define GPS_MARKER_ICON_PATH ".:" PKGDATADIR
48 #define GPS_MARKER_ICON "arrow.png"
50 /* number of track points per group and number of groups to maintain */
51 #define NUM_TRACK_POINTS (6)
52 #define NUM_TRACK_GROUPS (4)
53 #define NUM_TRACK_POINTS_FACTOR (1.5)
55 /* interval to update log file in seconds (default value for slider) */
56 #define GPS_LOG_DEFAULT_UPDATE_INTERVAL (30)
57 #define GPS_LOG_EXT "csv"
59 /* For updating the status bar conveniently */
60 #define GPS_STATUSBAR_CONTEXT "GPS"
63 #define GPS_STATUS(gps, format, args...) \
65 gchar *buf = g_strdup_printf(format, ##args); \
66 gtk_statusbar_push(GTK_STATUSBAR(gps->status_bar), \
67 gtk_statusbar_get_context_id( \
68 GTK_STATUSBAR(gps->status_bar), \
69 GPS_STATUSBAR_CONTEXT), \
74 #define GPS_STATUS(gps, format, args...) \
76 gchar *buf = g_strdup_printf(format, ##args); \
77 g_debug("STATUS: %s", buf); \
85 /* Find a readable file in a colon delimeted path */
86 static gchar *find_path(const gchar *path, const gchar *filename)
88 gchar *end_ptr, *fullpath;
90 end_ptr = (gchar *)path;
93 while (*end_ptr != ':' && *end_ptr != '\0')
95 fullpath = g_strdup_printf("%.*s/%s", (int)(end_ptr-path), path,
97 g_debug("GritsPluginGps: find_path - searching %s", fullpath);
98 if (access(fullpath, R_OK) == 0) {
99 g_debug("GritsPluginGps: find_path - found %s", fullpath);
100 return fullpath; /* caller frees */
105 if (*end_ptr == '\0') {
108 return find_path(end_ptr + 1, filename);
113 /********************
115 ********************/
117 static gchar *gps_get_status(struct gps_data_t *gps_data)
122 switch (gps_data->fix.mode) {
124 status_color = "red";
125 status_text = "No Signal";
128 status_color = "red";
129 status_text = "No Fix";
132 status_color = "yellow";
133 status_text = "2D Mode";
136 status_color = "green";
137 status_text = "3D Mode";
140 status_color = "black";
141 status_text = "Unknown";
144 return g_strdup_printf("<span foreground=\"%s\">%s</span>",
145 status_color, status_text);
149 static gchar *gps_get_online(struct gps_data_t *gps_data)
154 if (gps_data->online == -1.0) {
155 online_str = "Offline";
157 online_str = "Online";
160 switch (gps_data->status) {
162 status_str = "No Fix";
165 status_str = "Fix Acquired";
168 status_str = "DGPS Fix";
171 status_str = "Unknown Status";
175 return g_strdup_printf("%lf,%s,%s", gps_data->online, online_str, status_str);
179 static gchar *gps_get_latitude(struct gps_data_t *gps_data)
181 return g_strdup_printf("%3.4f", gps_data->fix.latitude);
184 static gchar *gps_get_longitude(struct gps_data_t *gps_data)
186 return g_strdup_printf("%3.4f", gps_data->fix.longitude);
189 static gchar *gps_get_elevation(struct gps_data_t *gps_data)
191 /* XXX Make units (m/ft) settable */
192 return g_strdup_printf("%.1lf %s",
193 (gps_data->fix.altitude * METERS_TO_FEET), "ft");
196 static gchar *gps_get_heading(struct gps_data_t *gps_data)
198 /* XXX Make units (m/ft) settable */
199 return g_strdup_printf("%03.0lf", gps_data->fix.track);
202 static gchar *gps_get_speed(struct gps_data_t *gps_data)
204 /* XXX Make units (m/ft) settable */
205 return g_strdup_printf("%1.1f %s",
206 (gps_data->fix.speed*3600.0*METERS_TO_FEET/5280.0), "mph");
211 const gchar *initial_val;
212 gchar *(*get_data)(struct gps_data_t *);
214 GtkWidget *label_widget;
215 GtkWidget *value_widget;
217 {"Status:", "No Data", gps_get_status, 14, NULL, NULL},
218 // {"Online:", "No Data", gps_get_online, 14, NULL, NULL},
219 {"Latitude:", "No Data", gps_get_latitude, 14, NULL, NULL},
220 {"Longitude:", "No Data", gps_get_longitude, 14, NULL, NULL},
221 {"Elevation:", "No Data", gps_get_elevation, 14, NULL, NULL},
222 {"Heading:", "No Data", gps_get_heading, 14, NULL, NULL},
223 {"Speed:", "No Data", gps_get_speed, 14, NULL, NULL},
231 static void gps_track_init(GpsTrack *track)
233 /* Save a spot at the end for the NULL termination */
234 track->points = (gpointer)g_new0(double*, NUM_TRACK_GROUPS + 1);
235 track->cur_point = 0;
236 track->cur_group = 0;
237 track->num_points = 1; /* starts at 1 so realloc logic works */
241 static void gps_track_clear(GpsTrack *track)
244 for (pi = 0; pi < NUM_TRACK_GROUPS; pi++) {
245 if (track->points[pi] != NULL) {
246 g_free(track->points[pi]);
247 track->points[pi] = NULL;
250 track->cur_point = 0;
251 track->cur_group = 0;
252 track->num_points = 1; /* starts at 1 so realloc logic works */
255 static void gps_track_free(GpsTrack *track)
257 gps_track_clear(track);
258 g_free(track->points);
261 /* add a new track group (points in a track group are connected, and
262 * separated from points in other track groups). */
263 static void gps_track_group_incr(GpsTrack *track)
265 gdouble (**points)[3] = track->points; /* for simplicity */
267 /* Just return if they increment it again before any points have
270 if (points[track->cur_group] == NULL) {
274 g_debug("GritsPluginGps: track_group_incr - track group %u->%u.",
275 track->cur_group, track->cur_group + 1);
278 track->cur_point = 0;
279 track->num_points = 1; /* starts at 1 so realloc logic works */
281 if (track->cur_group >= NUM_TRACK_GROUPS) {
282 g_debug("GritsPluginGps: track_group_incr - track group %u "
283 "is at max %u, shifting groups",
284 track->cur_group, NUM_TRACK_GROUPS);
286 /* Free the oldest one which falls off the end */
289 /* shift the rest down, last one should be NULL already */
290 /* note we alloc NUM_TRACK_GROUPS+1 */
291 for (int pi = 0; pi < NUM_TRACK_GROUPS; pi++) {
292 points[pi] = points[pi+1];
295 /* always write into the last group */
296 track->cur_group = NUM_TRACK_GROUPS - 1;
300 static void gps_track_add_point(GpsTrack *track,
301 gdouble lat, gdouble lon, gdouble elevation)
303 gdouble (**points)[3] = track->points; /* for simplicity */
305 g_debug("GritsPluginGps: track_add_point");
307 g_assert(track->cur_group < NUM_TRACK_GROUPS &&
308 (track->cur_point <= track->num_points));
310 /* resize/allocate the point group if the current one is full */
311 if (track->cur_point >= track->num_points - 1) {
312 guint new_size = track->num_points == 1 ?
314 track->num_points * NUM_TRACK_POINTS_FACTOR;
315 g_debug("GritsPluginGps: track_add_point - reallocating points "
316 "array from %u points to %u points.\n",
317 track->num_points, new_size);
318 points[track->cur_group] = (gpointer)g_renew(gdouble,
319 points[track->cur_group], 3*(new_size+1));
320 track->num_points = new_size;
323 g_assert(points[track->cur_group] != NULL);
325 /* Add the coordinate */
326 lle2xyz(lat, lon, elevation,
327 &points[track->cur_group][track->cur_point][0],
328 &points[track->cur_group][track->cur_point][1],
329 &points[track->cur_group][track->cur_point][2]);
333 /* make sure last point is always 0s so the line drawing stops. */
334 points[track->cur_group][track->cur_point][0] = 0.0;
335 points[track->cur_group][track->cur_point][1] = 0.0;
336 points[track->cur_group][track->cur_point][2] = 0.0;
344 static gchar *gps_get_date_string(double gps_time)
346 static gchar buf[256];
347 time_t int_time = (time_t)gps_time;
350 gmtime_r(&int_time, &tm_time);
352 snprintf(buf, sizeof(buf), "%04d-%02d-%02d",
353 tm_time.tm_year+1900, tm_time.tm_mon+1, tm_time.tm_mday);
358 static gchar *gps_get_time_string(time_t gps_time)
360 static gchar buf[256];
361 time_t int_time = (time_t)gps_time;
364 gmtime_r(&int_time, &tm_time);
366 snprintf(buf, sizeof(buf), "%02d:%02d:%02dZ",
367 tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
372 static gboolean gps_write_log(gpointer data)
374 GritsPluginGps *gps = (GritsPluginGps *)data;
375 struct gps_data_t *gps_data = &gps->gps_data;
379 gboolean new_file = FALSE;
381 if (gps_data == NULL) {
382 g_warning("Skipped write to GPS log file: "
383 "can not get GPS coordinates.");
384 GPS_STATUS(gps, "Skipped write to GPS log file: "
385 "can not get GPS coordinates.");
389 /* get filename from text entry box. If empty, generate a name from
390 * the date and time and set it. */
391 if (strlen(gtk_entry_get_text(
392 GTK_ENTRY(gps->ui.gps_log_filename_entry))) == 0) {
393 snprintf(filename, sizeof(filename),
395 gps_get_date_string(gps->gps_data.fix.time),
396 gps_get_time_string(gps->gps_data.fix.time),
398 gtk_entry_set_text(GTK_ENTRY(gps->ui.gps_log_filename_entry),
403 gtk_entry_get_text(GTK_ENTRY(gps->ui.gps_log_filename_entry)),
406 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
410 if ((fd = open(filename, O_CREAT|O_APPEND|O_WRONLY, 0644)) == -1) {
411 g_warning("Error opening log file %s: %s",
412 filename, strerror(errno));
417 /* write header and reset record counter */
418 snprintf(buf, sizeof(buf),
419 "No,Date,Time,Lat,Lon,Ele,Head,Speed,RTR\n");
420 if (write(fd, buf, strlen(buf)) == -1) {
421 g_warning("Error writing header to log file %s: %s",
422 filename, strerror(errno));
424 gps->ui.gps_log_number = 1;
427 /* Write log entry. Make sure this matches the header */
428 /* "No,Date,Time,Lat,Lon,Ele,Head,Speed,Fix,RTR\n" */
429 /* RTR values: T=time, B=button push, S=speed, D=distance */
430 snprintf(buf, sizeof(buf), "%d,%s,%s,%lf,%lf,%lf,%lf,%lf,%c\n",
431 gps->ui.gps_log_number++,
432 gps_get_date_string(gps->gps_data.fix.time),
433 gps_get_time_string(gps->gps_data.fix.time),
434 //gps_data->fix.time,
435 gps_data->fix.latitude,
436 gps_data->fix.longitude,
437 gps_data->fix.altitude * METERS_TO_FEET,
439 gps_data->fix.speed * METERS_TO_FEET,
440 'T'); /* position due to timer expired */
442 if (write(fd, buf, strlen(buf)) == -1) {
443 g_warning("Could not write log number %d to log file %s: %s",
444 gps->ui.gps_log_number-1, filename, strerror(errno));
448 GPS_STATUS(gps, "Updated GPS log file %s.", filename);
459 #ifdef GPS_RANGE_RINGS
460 static gboolean on_gps_rangering_clicked_event(GtkWidget *widget, gpointer user_data)
462 GritsPluginGps *gps = (GritsPluginGps *)user_data;
464 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
465 gps->gps_rangering_active = TRUE;
467 gps->gps_rangering_active = FALSE;
470 /* XXX force a redraw */
475 static void gps_init_range_rings(GritsPluginGps *gps, GtkWidget *gbox)
477 GtkWidget *gps_range_ring_frame = gtk_frame_new("Range Rings");
478 GtkWidget *cbox = gtk_vbox_new(FALSE, 2);
479 gtk_container_add(GTK_CONTAINER(gps_range_ring_frame), cbox);
480 gtk_box_pack_start(GTK_BOX(gbox), gps_range_ring_frame, FALSE, FALSE, 0);
482 gps->ui.gps_rangering_checkbox = gtk_check_button_new_with_label("Enable Range Rings");
483 g_signal_connect(G_OBJECT(gps->ui.gps_rangering_checkbox),
484 "clicked", G_CALLBACK(on_gps_rangering_clicked_event),
486 gtk_box_pack_start(GTK_BOX(cbox), gps->ui.gps_rangering_checkbox,
489 #endif /* GPS_RANGE_RINGS */
496 static gboolean gps_data_is_valid(struct gps_data_t *gps_data)
498 if (gps_data != NULL && gps_data->online != -1.0 &&
499 gps_data->fix.mode >= MODE_2D &&
500 gps_data->status > STATUS_NO_FIX) {
507 static void gps_update_status(GritsPluginGps *gps)
509 struct gps_data_t *gps_data = &gps->gps_data;
511 /* gps table update */
514 for (i = 0; i < G_N_ELEMENTS(gps_table); i++) {
515 gtk_label_set_markup (GTK_LABEL(gps_table[i].value_widget),
516 (str = gps_table[i].get_data(gps_data)));
521 /* external interface to update UI from latest GPS data. */
522 gboolean gps_redraw_all(gpointer data)
524 GritsPluginGps *gps = (GritsPluginGps *)data;
527 struct gps_data_t *gps_data = &gps->gps_data;
529 g_debug("GritsPluginGps: redraw_all");
532 if (!gps_data_is_valid(gps_data)) {
533 g_debug("GritsPluginGps: redraw_all - gps_data is not valid.");
534 /* XXX Change marker to indicate data is not valid */
538 /* update position labels */
539 gps_update_status(gps);
541 /* Update track and marker position */
542 if (gps_data_is_valid(gps_data) && gps->track.active) {
543 g_debug("GritsPluginGps: redraw_all - updating track group %u "
544 "point %u at lat = %f, long = %f, track = %f",
545 gps->track.cur_group,
546 gps->track.cur_point,
547 gps_data->fix.latitude,
548 gps_data->fix.longitude,
549 gps_data->fix.track);
551 gps_track_add_point(&gps->track,
552 gps_data->fix.latitude, gps_data->fix.longitude, 0.0);
554 if (gps->track.line) {
555 grits_viewer_remove(gps->viewer,
556 GRITS_OBJECT(gps->track.line));
557 gps->track.line = NULL;
560 gps->track.line = grits_line_new(gps->track.points);
561 gps->track.line->color[0] = 1.0;
562 gps->track.line->color[1] = 0;
563 gps->track.line->color[2] = 0.1;
564 gps->track.line->color[3] = 0.5;
565 gps->track.line->width = 3;
567 grits_viewer_add(gps->viewer, GRITS_OBJECT(gps->track.line),
568 GRITS_LEVEL_OVERLAY, TRUE);
569 grits_object_queue_draw(GRITS_OBJECT(gps->track.line));
572 if (gps_data_is_valid(gps_data)) {
574 grits_viewer_remove(gps->viewer,
575 GRITS_OBJECT(gps->marker));
579 gchar *path = find_path(GPS_MARKER_ICON_PATH, GPS_MARKER_ICON);
581 gps->marker = grits_marker_icon_new("GPS", path,
582 gps_data->fix.track, TRUE, MARKER_DMASK_ICON);
585 /* if icon not found just use a point */
586 g_warning("Could not find GPS marker icon %s in path %s.",
587 GPS_MARKER_ICON, GPS_MARKER_ICON_PATH);
588 gps->marker = grits_marker_icon_new("GPS", NULL,
589 gps_data->fix.track, FALSE,
590 MARKER_DMASK_POINT|MARKER_DMASK_LABEL);
593 GRITS_OBJECT(gps->marker)->center.lat = gps_data->fix.latitude;
594 GRITS_OBJECT(gps->marker)->center.lon = gps_data->fix.longitude;
595 GRITS_OBJECT(gps->marker)->center.elev = 0.0;
596 GRITS_OBJECT(gps->marker)->lod = EARTH_R;
598 grits_viewer_add(gps->viewer,
599 GRITS_OBJECT(gps->marker),
600 GRITS_LEVEL_OVERLAY, TRUE);
601 grits_object_queue_draw(GRITS_OBJECT(gps->marker));
604 if (gps->follow_gps && gps_data_is_valid(gps_data)) {
605 /* Center map at current GPS position. */
606 g_debug("GritsPluginGps: redraw_all - centering map at "
607 "lat = %f, long = %f, track = %f",
608 gps_data->fix.latitude,
609 gps_data->fix.longitude,
610 gps_data->fix.track);
612 double lat, lon, elev;
613 grits_viewer_get_location(gps->viewer, &lat, &lon, &elev);
614 grits_viewer_set_location(gps->viewer, gps_data->fix.latitude,
615 gps_data->fix.longitude, elev);
616 //grits_viewer_set_rotation(gps->viewer, 0, 0, 0);
629 static void gps_init_status_info(GritsPluginGps *gps, GtkWidget *gbox)
631 gps->ui.gps_status_frame = gtk_frame_new("GPS Data");
632 gps->ui.gps_status_table = gtk_table_new(5, 2, TRUE);
633 gtk_container_add(GTK_CONTAINER (gps->ui.gps_status_frame),
634 gps->ui.gps_status_table);
636 /* gps data table setup */
638 for (i = 0; i < G_N_ELEMENTS(gps_table); i++) {
639 gps_table[i].label_widget = gtk_label_new (gps_table[i].label);
640 gtk_label_set_justify(GTK_LABEL(gps_table[i].label_widget),
642 gtk_table_attach(GTK_TABLE(gps->ui.gps_status_table),
643 gps_table[i].label_widget,
644 0, 1, i, i+1, 0, 0, 0, 0);
645 gps_table[i].value_widget = gtk_label_new(gps_table[i].initial_val);
646 gtk_table_attach( GTK_TABLE(gps->ui.gps_status_table),
647 gps_table[i].value_widget, 1, 2, i, i+1, 0, 0, 0, 0);
649 PangoFontDescription *font_desc = pango_font_description_new ();
650 pango_font_description_set_size (font_desc,
651 gps_table[i].font_size*PANGO_SCALE);
652 gtk_widget_modify_font (gps_table[i].label_widget, font_desc);
653 gtk_widget_modify_font (gps_table[i].value_widget, font_desc);
654 pango_font_description_free (font_desc);
656 gtk_box_pack_start(GTK_BOX(gbox), gps->ui.gps_status_frame,
659 /* Start UI refresh task, which will reschedule itgps. */
661 gps->gps_update_timeout_id = g_timeout_add(
662 GPS_UPDATE_INTERVAL*1000,
663 gps_redraw_all, gps);
667 /* GPS Control Frame */
668 static gboolean on_gps_follow_clicked_event (GtkWidget *widget, gpointer user_data)
670 GritsPluginGps *gps = (GritsPluginGps *)user_data;
672 g_debug("GritsPluginGps: follow_clicked_event - button status %d",
673 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)));
674 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) {
675 gps->follow_gps = TRUE;
677 gps->follow_gps = FALSE;
683 static gboolean on_gps_track_enable_clicked_event(GtkWidget *widget, gpointer user_data)
685 GritsPluginGps *gps = (GritsPluginGps *)user_data;
687 g_debug("GritsPluginGps: track_enable_clicked_event");
689 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) {
690 /* start logging trip history */
691 GPS_STATUS(gps, "Enabled GPS track.");
692 gps->track.active = TRUE;
694 /* stop logging trip history */
695 GPS_STATUS(gps, "Disabled GPS track.");
696 gps->track.active = FALSE;
697 /* advance to the next track group, moving everything down if
699 gps_track_group_incr(&gps->track);
705 static gboolean on_gps_track_clear_clicked_event(GtkWidget *widget, gpointer user_data)
707 GritsPluginGps *gps = (GritsPluginGps *)user_data;
709 g_debug("GritsPluginGps: track_clear_clicked_event");
710 GPS_STATUS(gps, "Cleared GPS track.");
711 gps_track_clear(&gps->track);
716 static void gps_init_control_frame(GritsPluginGps *gps, GtkWidget *gbox)
718 /* Control checkboxes */
719 GtkWidget *gps_control_frame = gtk_frame_new("GPS Control");
720 GtkWidget *cbox = gtk_vbox_new(FALSE, 2);
721 gtk_container_add(GTK_CONTAINER(gps_control_frame), cbox);
722 gtk_box_pack_start(GTK_BOX(gbox), gps_control_frame, FALSE, FALSE, 0);
724 gps->ui.gps_follow_checkbox =
725 gtk_check_button_new_with_label("Follow GPS");
726 g_signal_connect(G_OBJECT(gps->ui.gps_follow_checkbox), "clicked",
727 G_CALLBACK (on_gps_follow_clicked_event),
729 gtk_box_pack_start(GTK_BOX(cbox), gps->ui.gps_follow_checkbox,
732 gps->ui.gps_track_checkbox =
733 gtk_check_button_new_with_label("Record Track");
734 g_signal_connect(G_OBJECT(gps->ui.gps_track_checkbox), "clicked",
735 G_CALLBACK (on_gps_track_enable_clicked_event),
737 gtk_box_pack_start(GTK_BOX(cbox), gps->ui.gps_track_checkbox,
740 gps->ui.gps_clear_button = gtk_button_new_with_label("Clear Track");
741 g_signal_connect(G_OBJECT(gps->ui.gps_clear_button), "clicked",
742 G_CALLBACK (on_gps_track_clear_clicked_event),
744 gtk_box_pack_start(GTK_BOX(cbox), gps->ui.gps_clear_button,
748 /* Track Log Frame */
749 static gboolean on_gps_log_clicked_event(GtkWidget *widget, gpointer user_data)
751 GritsPluginGps *gps = (GritsPluginGps *)user_data;
753 g_debug("GritsPluginGps: log_clicked_event");
755 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) {
758 /* Schedule log file write */
759 gps->ui.gps_log_timeout_id = g_timeout_add(
761 GTK_RANGE(gps->ui.gps_log_interval_slider))*1000,
764 /* button unchecked */
765 g_source_remove(gps->ui.gps_log_timeout_id);
766 gps->ui.gps_log_timeout_id = 0;
767 g_debug("GritsPluginGps: log_clicked_event - closed log file.");
773 static gboolean on_gps_log_interval_changed_event(GtkWidget *widget, gpointer user_data)
775 GritsPluginGps *gps = (GritsPluginGps *)user_data;
779 g_debug("GritsPluginGps: log_interval_changed_event - value = %f",
780 gtk_range_get_value(GTK_RANGE(widget)));
782 if (gtk_toggle_button_get_active(
783 GTK_TOGGLE_BUTTON(gps->ui.gps_log_checkbox))) {
784 g_assert(gps->ui.gps_log_timeout_id != 0);
786 /* disable old timeout */
787 g_source_remove(gps->ui.gps_log_timeout_id);
788 gps->ui.gps_log_timeout_id = 0;
790 /* Schedule new log file write */
791 gps->ui.gps_log_timeout_id = g_timeout_add(
792 gtk_range_get_value(GTK_RANGE(widget))*1000,
800 static void gps_init_track_log_frame(GritsPluginGps *gps, GtkWidget *gbox)
802 /* Track log box with enable checkbox and filename entry */
803 GtkWidget *gps_log_frame = gtk_frame_new ("Track Log");
804 GtkWidget *lbox = gtk_vbox_new (FALSE, 2);
805 gtk_container_add (GTK_CONTAINER (gps_log_frame), lbox);
806 gtk_box_pack_start (GTK_BOX(gbox), gps_log_frame,
809 gps->ui.gps_log_checkbox =
810 gtk_check_button_new_with_label("Log Position to File");
811 g_signal_connect (G_OBJECT (gps->ui.gps_log_checkbox), "clicked",
812 G_CALLBACK (on_gps_log_clicked_event),
814 gtk_box_pack_start (GTK_BOX(lbox), gps->ui.gps_log_checkbox,
817 /* Set up filename entry box */
818 GtkWidget *fbox = gtk_hbox_new (FALSE, 2);
819 GtkWidget *filename_label = gtk_label_new ("Filename:");
820 gtk_box_pack_start (GTK_BOX(fbox), filename_label, FALSE, FALSE, 0);
821 gps->ui.gps_log_filename_entry = gtk_entry_new();
822 gtk_box_pack_start (GTK_BOX(fbox), gps->ui.gps_log_filename_entry,
824 gtk_box_pack_start (GTK_BOX(lbox), fbox, FALSE, FALSE, 0);
826 /* set up gps log interval slider */
827 GtkWidget *ubox = gtk_hbox_new (FALSE, 4);
828 GtkWidget *interval_label = gtk_label_new ("Update Interval:");
829 gtk_box_pack_start (GTK_BOX(ubox), interval_label, FALSE, FALSE, 0);
830 gps->ui.gps_log_interval_slider =
831 gtk_hscale_new_with_range(1.0, 600.0, 30.0);
832 gtk_range_set_value (GTK_RANGE(gps->ui.gps_log_interval_slider),
833 GPS_LOG_DEFAULT_UPDATE_INTERVAL);
834 g_signal_connect (G_OBJECT (gps->ui.gps_log_interval_slider),
836 G_CALLBACK(on_gps_log_interval_changed_event),
838 gtk_range_set_increments(
839 GTK_RANGE(gps->ui.gps_log_interval_slider),
840 10.0 /* step */, 30.0 /* page up/down */);
841 gtk_range_set_update_policy(
842 GTK_RANGE(gps->ui.gps_log_interval_slider),
844 gtk_box_pack_start (GTK_BOX(ubox), gps->ui.gps_log_interval_slider,
846 gtk_box_pack_start (GTK_BOX(lbox), ubox, FALSE, FALSE, 0);
854 static void process_gps(gpointer data, gint source, GdkInputCondition condition)
856 struct gps_data_t *gps_data = (struct gps_data_t *)data;
858 g_debug("GritsPluginGps: process_gps");
860 /* Process any data from the gps and call the hook function */
861 if (gps_data != NULL) {
862 gint result = gps_read(gps_data);
863 g_debug("GritsPluginGps: process_gps - gps_read returned %d, "
864 "position %f, %f.", result,
865 gps_data->fix.latitude, gps_data->fix.longitude);
867 g_warning("GritsPluginGps: process_gps - gps_data == NULL.");
871 static gint initialize_gpsd(char *server, gchar *port, struct gps_data_t *gps_data)
873 #if GPSD_API_MAJOR_VERSION < 5
874 #error "GPSD protocol version 5 or greater required."
878 if ((result = gps_open(server, port, gps_data)) != 0) {
879 g_warning("Unable to open gpsd connection to %s:%s: %d, %d, %s",
880 server, port, result, errno, gps_errstr(errno));
882 (void)gps_stream(gps_data, WATCH_ENABLE|WATCH_JSON, NULL);
883 g_debug("GritsPluginGps: initialize_gpsd - gpsd fd %u.",
885 gdk_input_add(gps_data->gps_fd, GDK_INPUT_READ, process_gps, gps_data);
892 /**********************
893 * GPS Plugin Methods *
894 **********************/
897 GritsPluginGps *grits_plugin_gps_new(GritsViewer *viewer, GritsPrefs *prefs)
899 /* TODO: move to constructor if possible */
900 g_debug("GritsPluginGps: new");
901 GritsPluginGps *gps = g_object_new(GRITS_TYPE_PLUGIN_GPS, NULL);
902 gps->viewer = viewer;
905 initialize_gpsd("localhost", DEFAULT_GPSD_PORT, &gps->gps_data);
906 gps->follow_gps = FALSE;
908 gps_track_init(&gps->track);
909 gps_init_status_info(gps, gps->hbox);
910 gps_init_control_frame(gps, gps->hbox);
911 gps_init_track_log_frame(gps, gps->hbox);
912 #ifdef GPS_RANGE_RINGS
913 gps_init_range_rings(gps, gps->hbox);
919 static GtkWidget *grits_plugin_gps_get_config(GritsPlugin *_gps)
921 GritsPluginGps *gps = GRITS_PLUGIN_GPS(_gps);
926 static void grits_plugin_gps_plugin_init(GritsPluginInterface *iface);
927 G_DEFINE_TYPE_WITH_CODE(GritsPluginGps, grits_plugin_gps, G_TYPE_OBJECT,
928 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
929 grits_plugin_gps_plugin_init));
931 static void grits_plugin_gps_plugin_init(GritsPluginInterface *iface)
933 g_debug("GritsPluginGps: plugin_init");
934 /* Add methods to the interface */
935 iface->get_config = grits_plugin_gps_get_config;
938 static void grits_plugin_gps_init(GritsPluginGps *gps)
940 g_debug("GritsPluginGps: gps_init");
942 gps->config = gtk_notebook_new();
944 gps->hbox = gtk_hbox_new(FALSE, 2);
945 gtk_notebook_insert_page(GTK_NOTEBOOK(gps->config),
946 GTK_WIDGET(gps->hbox),
947 gtk_label_new("GPS"), 0);
948 /* Need to position on the top because of Win32 bug */
949 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(gps->config), GTK_POS_LEFT);
952 static void grits_plugin_gps_dispose(GObject *gobject)
954 GritsPluginGps *gps = GRITS_PLUGIN_GPS(gobject);
956 g_debug("GritsPluginGps: dispose");
960 grits_viewer_remove(gps->viewer,
961 GRITS_OBJECT(gps->marker));
963 g_object_unref(gps->viewer);
967 gps_track_free(&gps->track);
969 /* Drop references */
970 G_OBJECT_CLASS(grits_plugin_gps_parent_class)->dispose(gobject);
973 static void grits_plugin_gps_finalize(GObject *gobject)
975 GritsPluginGps *gps = GRITS_PLUGIN_GPS(gobject);
977 g_debug("GritsPluginGps: finalize");
980 gtk_widget_destroy(gps->config);
981 G_OBJECT_CLASS(grits_plugin_gps_parent_class)->finalize(gobject);
984 static void grits_plugin_gps_class_init(GritsPluginGpsClass *klass)
986 g_debug("GritsPluginGps: class_init");
987 GObjectClass *gobject_class = (GObjectClass*)klass;
988 gobject_class->dispose = grits_plugin_gps_dispose;
989 gobject_class->finalize = grits_plugin_gps_finalize;