]> Pileus Git - aweather/blobdiff - src/plugins/gps-plugin.c
Switch to newer API for gps input sources
[aweather] / src / plugins / gps-plugin.c
index 1ec820d2ed564ccca1abe54f823251a8efa2e8dc..ee68310d960f90d6f35d3242e27b1d0869ca0d67 100644 (file)
@@ -178,31 +178,31 @@ static gchar *gps_get_online(struct gps_data_t *gps_data)
 
 static gchar *gps_get_latitude(struct gps_data_t *gps_data)
 {
-       return g_strdup_printf("%3.4f", gps_data->fix.latitude);
+       return g_strdup_printf("%10.4f", gps_data->fix.latitude);
 }
 
 static gchar *gps_get_longitude(struct gps_data_t *gps_data)
 {
-       return g_strdup_printf("%3.4f", gps_data->fix.longitude);
+       return g_strdup_printf("%10.4f", gps_data->fix.longitude);
 }
 
 static gchar *gps_get_elevation(struct gps_data_t *gps_data)
 {
        /* XXX Make units (m/ft) settable */
-       return g_strdup_printf("%.1lf %s",
+       return g_strdup_printf("%5.0lf %4s",
                    (gps_data->fix.altitude * METERS_TO_FEET), "ft");
 }
 
 static gchar *gps_get_heading(struct gps_data_t *gps_data)
 {
        /* XXX Make units (m/ft) settable */
-       return g_strdup_printf("%03.0lf", gps_data->fix.track);
+       return g_strdup_printf("%5.0lf %4s", gps_data->fix.track, "deg");
 }
 
 static gchar *gps_get_speed(struct gps_data_t *gps_data)
 {
        /* XXX Make units (m/ft) settable */
-       return g_strdup_printf("%1.1f %s",
+       return g_strdup_printf("%5.0f %4s",
                (gps_data->fix.speed*3600.0*METERS_TO_FEET/5280.0), "mph");
 }
 
@@ -472,11 +472,9 @@ static void gps_update_status(GritsPluginGps *gps)
        struct gps_data_t *gps_data = &gps->gps_data;
 
        /* gps table update */
-       gint i;
-       gchar *str;
-       for (i = 0; i < G_N_ELEMENTS(gps_table); i++) {
-               gtk_label_set_markup(GTK_LABEL(gps_table[i].value_widget),
-                           (str = gps_table[i].get_data(gps_data)));
+       for (gint i = 0; i < G_N_ELEMENTS(gps_table); i++) {
+               gchar *str = gps_table[i].get_data(gps_data);
+               gtk_label_set_markup(GTK_LABEL(gps_table[i].value_widget), str);
                g_free(str);
        }
 }
@@ -598,32 +596,38 @@ static void gps_init_status_info(GritsPluginGps *gps, GtkWidget *gbox)
        gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
        gtk_frame_set_shadow_type(GTK_FRAME(gps->ui.gps_status_frame),
                           GTK_SHADOW_NONE);
-       gps->ui.gps_status_table = gtk_table_new(5, 2, TRUE);
+       gps->ui.gps_status_table = gtk_table_new(5, 2, FALSE);
        gtk_container_add(GTK_CONTAINER(gps->ui.gps_status_frame),
                   gps->ui.gps_status_table);
 
+
        /* gps data table setup */
        gint i;
        for (i = 0; i < G_N_ELEMENTS(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->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->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_misc_set_alignment(GTK_MISC(gps_table[i].label_widget), 0, 0);
+               gtk_misc_set_alignment(GTK_MISC(gps_table[i].value_widget), 1, 0);
+
+               gtk_table_attach(GTK_TABLE(gps->ui.gps_status_table),
+                               gps_table[i].label_widget, 0, 1, i, i+1,
+                               GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
+               gtk_table_attach(GTK_TABLE(gps->ui.gps_status_table),
+                               gps_table[i].value_widget, 1, 2, i, i+1,
+                               GTK_EXPAND|GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
+
+               PangoFontDescription *roman = pango_font_description_new();
+               PangoFontDescription *mono  = pango_font_description_from_string("monospace");
+               pango_font_description_set_size(roman, gps_table[i].font_size*PANGO_SCALE);
+               pango_font_description_set_size(mono,  gps_table[i].font_size*PANGO_SCALE);
+               gtk_widget_modify_font(gps_table[i].label_widget, roman);
+               gtk_widget_modify_font(gps_table[i].value_widget, mono);
+               pango_font_description_free(roman);
+               pango_font_description_free(mono);
        }
        gtk_box_pack_start(GTK_BOX(gbox), gps->ui.gps_status_frame,
-                           TRUE, FALSE, 0);
+                           FALSE, FALSE, 0);
 
        /* Start UI refresh task, which will reschedule itgps. */
        gps_redraw_all(gps);
@@ -828,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;
 
@@ -836,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)
@@ -850,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;
 }
 
 
@@ -876,10 +884,10 @@ GritsPluginGps *grits_plugin_gps_new(GritsViewer *viewer, GritsPrefs *prefs)
        /* TODO: move to constructor if possible */
        g_debug("GritsPluginGps: new");
        GritsPluginGps *gps = g_object_new(GRITS_TYPE_PLUGIN_GPS, NULL);
-       gps->viewer = viewer;
-       gps->prefs  = 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);
@@ -922,17 +930,28 @@ 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) {
-               if (gps->marker) {
-                       grits_viewer_remove(gps->viewer,
-                              GRITS_OBJECT(gps->marker));
-               }
-               g_object_unref(gps->viewer);
+               GritsViewer *viewer = gps->viewer;
                gps->viewer = NULL;
+               if (gps->marker)
+                       grits_viewer_remove(viewer,
+                              GRITS_OBJECT(gps->marker));
+               g_object_unref(gps->prefs);
+               g_object_unref(viewer);
        }
 
-       gps_track_free(&gps->track);
-
        /* Drop references */
        G_OBJECT_CLASS(grits_plugin_gps_parent_class)->dispose(gobject);
 }
@@ -944,7 +963,7 @@ static void grits_plugin_gps_finalize(GObject *gobject)
        g_debug("GritsPluginGps: finalize");
 
        /* Free data */
-       gtk_widget_destroy(gps->config);
+       gps_track_free(&gps->track);
        G_OBJECT_CLASS(grits_plugin_gps_parent_class)->finalize(gobject);
 }