X-Git-Url: http://pileus.org/git/?p=aweather;a=blobdiff_plain;f=src%2Fplugins%2Fradar.c;h=2dcea83986dc6a0ad45400998a8c0e85d8789b6b;hp=52d94c92d0ed1411c67abb3bafd8a4d255863609;hb=cc82e9478bb47cccb085fb2738053b0c5f6b8298;hpb=8addcb17f030c9d9330f125f6160eedbccddf63d diff --git a/src/plugins/radar.c b/src/plugins/radar.c index 52d94c9..2dcea83 100644 --- a/src/plugins/radar.c +++ b/src/plugins/radar.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2010 Andy Spencer + * Copyright (C) 2009-2012 Andy Spencer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,12 +16,11 @@ */ #define _XOPEN_SOURCE -#include +#include #include #include #include #include -#include #include #include @@ -31,7 +30,9 @@ #include "level2.h" #include "../aweather-location.h" -static void _gtk_bin_set_child(GtkBin *bin, GtkWidget *new) +#include "compat.h" + +static void aweather_bin_set_child(GtkBin *bin, GtkWidget *new) { GtkWidget *old = gtk_bin_get_child(bin); if (old) @@ -41,7 +42,7 @@ static void _gtk_bin_set_child(GtkBin *bin, GtkWidget *new) } static gchar *_find_nearest(time_t time, GList *files, - gsize offset, gchar *format) + gsize offset) { g_debug("RadarSite: find_nearest ..."); time_t nearest_time = 0; @@ -50,7 +51,11 @@ static gchar *_find_nearest(time_t time, GList *files, struct tm tm = {}; for (GList *cur = files; cur; cur = cur->next) { gchar *file = cur->data; - strptime(file+offset, format, &tm); + sscanf(file+offset, "%4d%2d%2d_%2d%2d", + &tm.tm_year, &tm.tm_mon, &tm.tm_mday, + &tm.tm_hour, &tm.tm_min); + tm.tm_year -= 1900; + tm.tm_mon -= 1; if (ABS(time - mktime(&tm)) < ABS(time - nearest_time)) { nearest_file = file; @@ -97,6 +102,7 @@ struct _RadarSite { guint time_id; // "time-changed" callback ID guint refresh_id; // "refresh" callback ID guint location_id; // "locaiton-changed" callback ID + guint idle_source; // _site_update_end idle source }; /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */ @@ -117,13 +123,22 @@ gboolean _site_update_end(gpointer _site) RadarSite *site = _site; if (site->message) { g_warning("RadarSite: update_end - %s", site->message); - _gtk_bin_set_child(GTK_BIN(site->config), - gtk_label_new(site->message)); + const char *fmt = "http://forecast.weather.gov/product.php?site=NWS&product=FTM&format=TXT&issuedby=%s"; + char *uri = g_strdup_printf(fmt, site->city->code+1); + GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); + GtkWidget *msg = gtk_label_new(site->message); + GtkWidget *btn = gtk_link_button_new_with_label(uri, "View Radar Status"); + gtk_box_set_homogeneous(GTK_BOX(box), TRUE); + gtk_box_pack_start(GTK_BOX(box), msg, TRUE, TRUE, 0); + gtk_box_pack_start(GTK_BOX(box), btn, TRUE, TRUE, 0); + aweather_bin_set_child(GTK_BIN(site->config), box); + g_free(uri); } else { - _gtk_bin_set_child(GTK_BIN(site->config), + aweather_bin_set_child(GTK_BIN(site->config), aweather_level2_get_config(site->level2)); } site->status = STATUS_LOADED; + site->idle_source = 0; return FALSE; } gpointer _site_update_thread(gpointer _site) @@ -144,7 +159,7 @@ gpointer _site_update_thread(gpointer _site) "^\\w{4}_\\d{8}_\\d{4}$", site->city->code, "\\d+ (.*)", (offline ? NULL : dir_list)); g_free(dir_list); - gchar *nearest = _find_nearest(site->time, files, 5, "%Y%m%d_%H%M"); + gchar *nearest = _find_nearest(site->time, files, 5); g_list_foreach(files, (GFunc)g_free, NULL); g_list_free(files); if (!nearest) { @@ -179,10 +194,11 @@ gpointer _site_update_thread(gpointer _site) } grits_object_hide(GRITS_OBJECT(site->level2), site->hidden); grits_viewer_add(site->viewer, GRITS_OBJECT(site->level2), - GRITS_LEVEL_WORLD+1, TRUE); + GRITS_LEVEL_WORLD+3, TRUE); out: - g_idle_add(_site_update_end, site); + if (!site->idle_source) + site->idle_source = g_idle_add(_site_update_end, site); return NULL; } void _site_update(RadarSite *site) @@ -198,7 +214,7 @@ void _site_update(RadarSite *site) /* Add a progress bar */ GtkWidget *progress = gtk_progress_bar_new(); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading..."); - _gtk_bin_set_child(GTK_BIN(site->config), progress); + aweather_bin_set_child(GTK_BIN(site->config), progress); /* Remove old volume */ g_debug("RadarSite: update - remove - %s", site->city->code); @@ -209,7 +225,7 @@ void _site_update(RadarSite *site) /* Fork loading right away so updating the * list of times doesn't take too long */ - g_thread_create(_site_update_thread, site, FALSE, NULL); + g_thread_new("site-update-thread", _site_update_thread, site); } /* RadarSite methods */ @@ -224,6 +240,9 @@ void radar_site_unload(RadarSite *site) g_signal_handler_disconnect(site->viewer, site->time_id); if (site->refresh_id) g_signal_handler_disconnect(site->viewer, site->refresh_id); + if (site->idle_source) + g_source_remove(site->idle_source); + site->idle_source = 0; /* Remove tab */ if (site->config) @@ -248,6 +267,8 @@ void radar_site_load(RadarSite *site) gtk_notebook_append_page(GTK_NOTEBOOK(site->pconfig), site->config, gtk_label_new(site->city->name)); gtk_widget_show_all(site->config); + if (gtk_notebook_get_current_page(GTK_NOTEBOOK(site->pconfig)) == 0) + gtk_notebook_set_current_page(GTK_NOTEBOOK(site->pconfig), -1); /* Set up radar loading */ site->time_id = g_signal_connect_swapped(site->viewer, "time-changed", @@ -278,16 +299,24 @@ void _site_on_location_changed(GritsViewer *viewer, radar_site_unload(site); } -gboolean _site_add_marker(gpointer _site) +static gboolean on_marker_clicked(GritsObject *marker, GdkEvent *event, RadarSite *site) { - RadarSite *site = _site; - site->marker = grits_marker_new(site->city->name); - GRITS_OBJECT(site->marker)->center = site->city->pos; - GRITS_OBJECT(site->marker)->lod = EARTH_R*site->city->lod; - grits_viewer_add(site->viewer, GRITS_OBJECT(site->marker), - GRITS_LEVEL_OVERLAY, FALSE); - return FALSE; + GritsViewer *viewer = site->viewer; + GritsPoint center = marker->center; + grits_viewer_set_location(viewer, center.lat, center.lon, EARTH_R/35); + grits_viewer_set_rotation(viewer, 0, 0, 0); + /* Recursivly set notebook tabs */ + GtkWidget *widget, *parent; + for (widget = site->config; widget; widget = parent) { + parent = gtk_widget_get_parent(widget); + if (GTK_IS_NOTEBOOK(parent)) { + gint i = gtk_notebook_page_num(GTK_NOTEBOOK(parent), widget); + gtk_notebook_set_current_page(GTK_NOTEBOOK(parent), i); + } + } + return TRUE; } + RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig, GritsViewer *viewer, GritsPrefs *prefs, GritsHttp *http) { @@ -300,6 +329,7 @@ RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig, "level2" G_DIR_SEPARATOR_S); site->city = city; site->pconfig = pconfig; + site->hidden = TRUE; /* Set initial location */ gdouble lat, lon, elev; @@ -307,7 +337,14 @@ RadarSite *radar_site_new(city_t *city, GtkWidget *pconfig, _site_on_location_changed(viewer, lat, lon, elev, site); /* Add marker */ - g_idle_add_full(G_PRIORITY_LOW, _site_add_marker, site, NULL); + site->marker = grits_marker_new(site->city->name); + GRITS_OBJECT(site->marker)->center = site->city->pos; + GRITS_OBJECT(site->marker)->lod = EARTH_R*0.75*site->city->lod; + grits_viewer_add(site->viewer, GRITS_OBJECT(site->marker), + GRITS_LEVEL_HUD, FALSE); + g_signal_connect(site->marker, "clicked", + G_CALLBACK(on_marker_clicked), site); + grits_object_set_cursor(GRITS_OBJECT(site->marker), GDK_HAND2); /* Connect signals */ site->location_id = g_signal_connect(viewer, "location-changed", @@ -343,13 +380,14 @@ struct _RadarConus { GtkWidget *config; time_t time; const gchar *message; - GStaticMutex loading; + GMutex loading; gchar *path; GritsTile *tile[2]; guint time_id; // "time-changed" callback ID guint refresh_id; // "refresh" callback ID + guint idle_source; // _conus_update_end idle source }; void _conus_update_loading(gchar *file, goffset cur, @@ -368,29 +406,28 @@ void _conus_update_loading(gchar *file, goffset cur, /* Copy images to graphics memory */ static void _conus_update_end_copy(GritsTile *tile, guchar *pixels) { - if (!tile->data) { - tile->data = g_new0(guint, 1); - glGenTextures(1, tile->data); - } + if (!tile->tex) + glGenTextures(1, &tile->tex); - guint *tex = tile->data; - glBindTexture(GL_TEXTURE_2D, *tex); + gchar *clear = g_malloc0(2048*2048*4); + glBindTexture(GL_TEXTURE_2D, tile->tex); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, 4, 2048, 2048, 0, - GL_RGBA, GL_UNSIGNED_BYTE, NULL); + GL_RGBA, GL_UNSIGNED_BYTE, clear); glTexSubImage2D(GL_TEXTURE_2D, 0, 1,1, CONUS_WIDTH/2,CONUS_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, pixels); tile->coords.n = 1.0/(CONUS_WIDTH/2); tile->coords.w = 1.0/ CONUS_HEIGHT; tile->coords.s = tile->coords.n + CONUS_HEIGHT / 2048.0; tile->coords.e = tile->coords.w + (CONUS_WIDTH/2) / 2048.0; - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFlush(); + g_free(clear); } /* Split the pixbuf into east and west halves (with 2K sides) @@ -419,7 +456,7 @@ static void _conus_update_end_split(guchar *pixels, guchar *west, guchar *east, dst[0] = src[0]; dst[1] = src[1]; dst[2] = src[2]; - dst[3] = 0xff; + dst[3] = 0xff * 0.75; for (int j = 0; j < G_N_ELEMENTS(alphamap); j++) if (src[0] == alphamap[j][0] && src[1] == alphamap[j][1] && @@ -437,7 +474,7 @@ gboolean _conus_update_end(gpointer _conus) /* Check error status */ if (conus->message) { g_warning("Conus: update_end - %s", conus->message); - _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new(conus->message)); + aweather_bin_set_child(GTK_BIN(conus->config), gtk_label_new(conus->message)); goto out; } @@ -446,7 +483,7 @@ gboolean _conus_update_end(gpointer _conus) GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(conus->path, &error); if (!pixbuf || error) { g_warning("Conus: update_end - error loading pixbuf: %s", conus->path); - _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new("Error loading pixbuf")); + aweather_bin_set_child(GTK_BIN(conus->config), gtk_label_new("Error loading pixbuf")); g_remove(conus->path); goto out; } @@ -470,13 +507,14 @@ gboolean _conus_update_end(gpointer _conus) /* Update GUI */ gchar *label = g_path_get_basename(conus->path); - _gtk_bin_set_child(GTK_BIN(conus->config), gtk_label_new(label)); - gtk_widget_queue_draw(GTK_WIDGET(conus->viewer)); + aweather_bin_set_child(GTK_BIN(conus->config), gtk_label_new(label)); + grits_viewer_queue_draw(conus->viewer); g_free(label); out: + conus->idle_source = 0; g_free(conus->path); - g_static_mutex_unlock(&conus->loading); + g_mutex_unlock(&conus->loading); return FALSE; } @@ -493,17 +531,18 @@ gpointer _conus_update_thread(gpointer _conus) if (time(NULL) - conus->time < 60*60*5 && !offline) { /* radar.weather.gov is full of lies. * the index pages get cached and out of date */ - struct tm tm; - gmtime_r(&conus->time, &tm); - time_t onthe8 = conus->time - 60*((tm.tm_min+1)%10+1); - gmtime_r(&onthe8, &tm); + /* gmtime is not thread safe, but it's not used very often so + * hopefully it'll be alright for now... :-( */ + struct tm *tm = gmtime(&conus->time); + time_t onthe8 = conus->time - 60*((tm->tm_min+1)%10+1); + tm = gmtime(&onthe8); nearest = g_strdup_printf("Conus_%04d%02d%02d_%02d%02d_N0Ronly.gif", - tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, - tm.tm_hour, tm.tm_min); + tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, + tm->tm_hour, tm->tm_min); } else { GList *files = grits_http_available(conus->http, "^Conus_[^\"]*_N0Ronly.gif$", "", NULL, NULL); - nearest = _find_nearest(conus->time, files, 6, "%Y%m%d_%H%M"); + nearest = _find_nearest(conus->time, files, 6); g_list_foreach(files, (GFunc)g_free, NULL); g_list_free(files); if (!nearest) { @@ -527,13 +566,14 @@ gpointer _conus_update_thread(gpointer _conus) out: g_debug("Conus: update_thread - done"); - g_idle_add(_conus_update_end, conus); + if (!conus->idle_source) + conus->idle_source = g_idle_add(_conus_update_end, conus); return NULL; } void _conus_update(RadarConus *conus) { - if (!g_static_mutex_trylock(&conus->loading)) + if (!g_mutex_trylock(&conus->loading)) return; conus->time = grits_viewer_get_time(conus->viewer); g_debug("Conus: update - %d", @@ -542,9 +582,9 @@ void _conus_update(RadarConus *conus) /* Add a progress bar */ GtkWidget *progress = gtk_progress_bar_new(); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), "Loading..."); - _gtk_bin_set_child(GTK_BIN(conus->config), progress); + aweather_bin_set_child(GTK_BIN(conus->config), progress); - g_thread_create(_conus_update_thread, conus, FALSE, NULL); + g_thread_new("conus-update-thread", _conus_update_thread, conus); } RadarConus *radar_conus_new(GtkWidget *pconfig, @@ -554,7 +594,7 @@ RadarConus *radar_conus_new(GtkWidget *pconfig, conus->viewer = g_object_ref(viewer); conus->http = http; conus->config = gtk_alignment_new(0, 0, 1, 1); - g_static_mutex_init(&conus->loading); + g_mutex_init(&conus->loading); gdouble south = CONUS_NORTH - CONUS_DEG_PER_PX*CONUS_HEIGHT; gdouble east = CONUS_WEST + CONUS_DEG_PER_PX*CONUS_WIDTH; @@ -563,8 +603,8 @@ RadarConus *radar_conus_new(GtkWidget *pconfig, conus->tile[1] = grits_tile_new(NULL, CONUS_NORTH, south, east, mid); conus->tile[0]->zindex = 2; conus->tile[1]->zindex = 1; - grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[0]), GRITS_LEVEL_WORLD, TRUE); - grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[1]), GRITS_LEVEL_WORLD, TRUE); + grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[0]), GRITS_LEVEL_WORLD+2, FALSE); + grits_viewer_add(viewer, GRITS_OBJECT(conus->tile[1]), GRITS_LEVEL_WORLD+2, FALSE); conus->time_id = g_signal_connect_swapped(viewer, "time-changed", G_CALLBACK(_conus_update), conus); @@ -583,14 +623,13 @@ void radar_conus_free(RadarConus *conus) { g_signal_handler_disconnect(conus->viewer, conus->time_id); g_signal_handler_disconnect(conus->viewer, conus->refresh_id); + if (conus->idle_source) + g_source_remove(conus->idle_source); for (int i = 0; i < 2; i++) { GritsTile *tile = conus->tile[i]; - if (tile->data) { - glDeleteTextures(1, tile->data); - g_free(tile->data); - } grits_viewer_remove(conus->viewer, GRITS_OBJECT(tile)); + g_object_unref(tile); } g_object_unref(conus->viewer); @@ -665,7 +704,7 @@ out: } static void _update_hidden(GtkNotebook *notebook, - GtkNotebookPage *page, guint page_num, gpointer viewer) + gpointer _, guint page_num, gpointer viewer) { g_debug("GritsPluginRadar: _update_hidden - 0..%d = %d", gtk_notebook_get_n_pages(notebook), page_num); @@ -688,7 +727,7 @@ static void _update_hidden(GtkNotebook *notebook, g_warning("GritsPluginRadar: _update_hidden - no site or counus found"); } } - gtk_widget_queue_draw(GTK_WIDGET(viewer)); + grits_viewer_queue_draw(viewer); } /* Methods */ @@ -697,8 +736,8 @@ GritsPluginRadar *grits_plugin_radar_new(GritsViewer *viewer, GritsPrefs *prefs) /* TODO: move to constructor if possible */ g_debug("GritsPluginRadar: new"); GritsPluginRadar *self = g_object_new(GRITS_TYPE_PLUGIN_RADAR, NULL); - self->viewer = viewer; - self->prefs = prefs; + self->viewer = g_object_ref(viewer); + self->prefs = g_object_ref(prefs); /* Setup page switching */ self->tab_id = g_signal_connect(self->config, "switch-page", @@ -752,7 +791,7 @@ static void grits_plugin_radar_init(GritsPluginRadar *self) "conus" G_DIR_SEPARATOR_S); self->sites = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)radar_site_free); - self->config = gtk_notebook_new(); + self->config = g_object_ref(gtk_notebook_new()); /* Load colormaps */ for (int i = 0; colormaps[i].file; i++) { @@ -769,9 +808,18 @@ static void grits_plugin_radar_dispose(GObject *gobject) { g_debug("GritsPluginRadar: dispose"); GritsPluginRadar *self = GRITS_PLUGIN_RADAR(gobject); - g_signal_handler_disconnect(self->config, self->tab_id); - grits_viewer_remove(self->viewer, GRITS_OBJECT(self->hud)); - radar_conus_free(self->conus); + if (self->viewer) { + GritsViewer *viewer = self->viewer; + self->viewer = NULL; + g_signal_handler_disconnect(self->config, self->tab_id); + grits_viewer_remove(viewer, GRITS_OBJECT(self->hud)); + radar_conus_free(self->conus); + g_hash_table_destroy(self->sites); + g_object_unref(self->config); + g_object_unref(self->hud); + g_object_unref(self->prefs); + g_object_unref(viewer); + } /* Drop references */ G_OBJECT_CLASS(grits_plugin_radar_parent_class)->dispose(gobject); } @@ -782,7 +830,6 @@ static void grits_plugin_radar_finalize(GObject *gobject) /* Free data */ grits_http_free(self->conus_http); grits_http_free(self->sites_http); - g_hash_table_destroy(self->sites); gtk_widget_destroy(self->config); G_OBJECT_CLASS(grits_plugin_radar_parent_class)->finalize(gobject);