]> Pileus Git - aweather/blobdiff - src/plugins/gps-plugin.c
Switch to newer API for gps input sources
[aweather] / src / plugins / gps-plugin.c
index befb40ecb23184088a061d8146d453b5a6a35659..ee68310d960f90d6f35d3242e27b1d0869ca0d67 100644 (file)
@@ -832,7 +832,7 @@ static void gps_init_track_log_frame(GritsPluginGps *gps, GtkWidget *gbox)
  * GPSD interfaces *
  *******************/
 
-static void process_gps(gpointer data, gint source, GdkInputCondition condition)
+static gboolean process_gps(GIOChannel *source, GIOCondition condition, gpointer data)
 {
        struct gps_data_t *gps_data = (struct gps_data_t *)data;
 
@@ -840,13 +840,17 @@ static void process_gps(gpointer data, gint source, GdkInputCondition condition)
 
        /* Process any data from the gps and call the hook function */
        if (gps_data != NULL) {
+               gdouble track = gps_data->fix.track;
                gint result = gps_read(gps_data);
+               if (isnan(gps_data->fix.track))
+                       gps_data->fix.track = track;
                g_debug("GritsPluginGps: process_gps - gps_read returned %d, "
                        "position %f, %f.", result,
                        gps_data->fix.latitude, gps_data->fix.longitude);
        } else {
                g_warning("GritsPluginGps: process_gps - gps_data == NULL.");
        }
+       return TRUE;
 }
 
 static gint initialize_gpsd(char *server, gchar *port, struct gps_data_t *gps_data)
@@ -854,19 +858,19 @@ static gint initialize_gpsd(char *server, gchar *port, struct gps_data_t *gps_da
 #if GPSD_API_MAJOR_VERSION < 5
 #error "GPSD protocol version 5 or greater required."
 #endif
-       gint result;
-
-       if ((result = gps_open(server, port, gps_data)) != 0) {
+       gint result = gps_open(server, port, gps_data);
+       if (result > 0) {
                g_warning("Unable to open gpsd connection to %s:%s: %d, %d, %s",
-               server, port, result, errno, gps_errstr(errno));
-       } else {
-               (void)gps_stream(gps_data, WATCH_ENABLE|WATCH_JSON, NULL);
-               g_debug("GritsPluginGps: initialize_gpsd - gpsd fd %u.",
-                       gps_data->gps_fd);
-               gdk_input_add(gps_data->gps_fd, GDK_INPUT_READ, process_gps, gps_data);
+                               server, port, result, errno, gps_errstr(errno));
+               return 0;
        }
 
-       return result;
+       (void)gps_stream(gps_data, WATCH_ENABLE|WATCH_JSON, NULL);
+       g_debug("GritsPluginGps: initialize_gpsd - gpsd fd %u.", gps_data->gps_fd);
+       GIOChannel *channel = g_io_channel_unix_new(gps_data->gps_fd);
+       gint input_tag = g_io_add_watch(channel, G_IO_IN, process_gps, gps_data);
+       g_io_channel_unref(channel);
+       return input_tag;
 }
 
 
@@ -883,7 +887,7 @@ GritsPluginGps *grits_plugin_gps_new(GritsViewer *viewer, GritsPrefs *prefs)
        gps->viewer  = g_object_ref(viewer);
        gps->prefs   = g_object_ref(prefs);
 
-       initialize_gpsd("localhost", DEFAULT_GPSD_PORT, &gps->gps_data);
+       gps->input_tag = initialize_gpsd("localhost", DEFAULT_GPSD_PORT, &gps->gps_data);
        gps->follow_gps = FALSE;
 
        gps_track_init(&gps->track);
@@ -926,6 +930,18 @@ static void grits_plugin_gps_dispose(GObject *gobject)
 
        g_debug("GritsPluginGps: dispose");
 
+       if (gps->gps_update_timeout_id) {
+               g_source_remove(gps->gps_update_timeout_id);
+               gps->gps_update_timeout_id = 0;
+       }
+       if (gps->ui.gps_log_timeout_id) {
+               g_source_remove(gps->ui.gps_log_timeout_id);
+               gps->ui.gps_log_timeout_id = 0;
+       }
+       if (gps->input_tag) {
+               g_source_remove(gps->input_tag);
+               gps->input_tag = 0;
+       }
        if (gps->viewer) {
                GritsViewer *viewer = gps->viewer;
                gps->viewer = NULL;