]> Pileus Git - grits/blob - src/plugin-ridge.c
some site-switching code
[grits] / src / plugin-ridge.c
1 #include <config.h>
2 #include <gtk/gtk.h>
3 #include <curl/curl.h>
4 #include <GL/gl.h>
5
6 #include <stdio.h>
7
8 #include "aweather-gui.h"
9
10 enum {
11         LAYER_TOPO,
12         LAYER_COUNTY,
13         LAYER_RIVERS,
14         LAYER_HIGHWAYS,
15         LAYER_CITY,
16         LAYER_COUNT
17 };
18
19 static struct {
20         char *fmt;
21         float z;
22         guint tex;
23 } layers[] = {
24         [LAYER_TOPO]     = { "Overlays/" "Topo/"     "Short/" "%s_Topo_Short.jpg",     1, 0 },
25         [LAYER_COUNTY]   = { "Overlays/" "County/"   "Short/" "%s_County_Short.gif",   3, 0 },
26         [LAYER_RIVERS]   = { "Overlays/" "Rivers/"   "Short/" "%s_Rivers_Short.gif",   4, 0 },
27         [LAYER_HIGHWAYS] = { "Overlays/" "Highways/" "Short/" "%s_Highways_Short.gif", 5, 0 },
28         [LAYER_CITY]     = { "Overlays/" "Cities/"   "Short/" "%s_City_Short.gif",     6, 0 },
29 };
30
31 static CURL *curl_handle;
32
33 /**
34  * Cache a image from Ridge to the local disk
35  * \param  path  Path to the Ridge file, starting after /ridge/
36  * \return The local path to the cached image
37  */
38 char *cache_image(char *path)
39 {
40         gchar base[] = "http://radar.weather.gov/ridge/";
41         gchar *url   = g_strconcat(base, path, NULL);
42         gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
43         if (!g_file_test(local, G_FILE_TEST_EXISTS)) {
44                 if (!g_file_test(g_path_get_dirname(local), G_FILE_TEST_IS_DIR)) {
45                         //g_printerr("Making directory %s\n", g_path_get_dirname(local));
46                         g_mkdir_with_parents(g_path_get_dirname(local), 0755);
47                 }
48                 //g_printerr("Fetching image %s -> %s\n", url, local);
49                 long http_code;
50                 FILE *cached_image = fopen(local, "w+");
51                 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
52                 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, cached_image);
53                 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, NULL);
54                 curl_easy_perform(curl_handle);
55                 curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
56                 fflush(cached_image);
57                 if (http_code != 200) {
58                         g_message("http %ld while fetching %s", http_code, url);
59                         remove(local);
60                         return NULL;
61                 }
62         }
63         return local;
64 }
65
66 /**
67  * Load an image into an OpenGL texture
68  * \param  filename  Path to the image file
69  * \return The OpenGL identifier for the texture
70  */
71 guint load_texture(char *filename)
72 {
73         /* Load image */
74         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
75         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
76         int        width  = gdk_pixbuf_get_width(pixbuf);
77         int        height = gdk_pixbuf_get_height(pixbuf);
78         int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
79
80         /* Create Texture */
81         guint id;
82         glGenTextures(1, &id);
83         glBindTexture(GL_TEXTURE_2D, id);   // 2d texture (x and y size)
84
85         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
86         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
87                         format, GL_UNSIGNED_BYTE, pixels);
88         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
89         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
90
91         g_message("loaded image:  w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
92                 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
93                 g_path_get_basename(filename));
94         return id;
95 }
96
97 static void set_site(AWeatherView *view, char *site, AWeatherGui *gui)
98 {
99         g_message("location changed to %s", site);
100         aweather_gui_gl_begin(gui);
101         for (int i = 0; i < LAYER_COUNT; i++) {
102                 if (layers[i].tex != 0)
103                         continue;
104                 char *path  = g_strdup_printf(layers[i].fmt, site);
105                 char *local = cache_image(path);
106                 layers[i].tex = load_texture(local);
107                 g_free(local);
108                 g_free(path);
109         }
110         aweather_gui_gl_end(gui);
111 }
112
113 static gboolean expose(GtkWidget *da, GdkEventExpose *event, AWeatherGui *gui)
114 {
115         //g_message("ridge:expose");
116         aweather_gui_gl_begin(gui);
117         glPushMatrix();
118         glEnable(GL_TEXTURE_2D);
119         glColor4f(1,1,1,1);
120
121         for (int i = 0; i < LAYER_COUNT; i++) {
122                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
123
124                 glBegin(GL_POLYGON);
125                 glTexCoord2f(0.0, 0.0); glVertex3f(500*1000*-1.0, 500*1000* 1.0, layers[i].z);
126                 glTexCoord2f(0.0, 1.0); glVertex3f(500*1000*-1.0, 500*1000*-1.0, layers[i].z);
127                 glTexCoord2f(1.0, 1.0); glVertex3f(500*1000* 1.0, 500*1000*-1.0, layers[i].z);
128                 glTexCoord2f(1.0, 0.0); glVertex3f(500*1000* 1.0, 500*1000* 1.0, layers[i].z);
129                 glEnd();
130         }
131
132         glPopMatrix();
133         aweather_gui_gl_end(gui);
134         return FALSE;
135 }
136
137 gboolean ridge_init(AWeatherGui *gui)
138 {
139         GtkDrawingArea *drawing = aweather_gui_get_drawing(gui);
140         AWeatherView *view = aweather_gui_get_view(gui);
141
142         /* Set up OpenGL Stuff */
143         g_signal_connect(drawing, "expose-event",     G_CALLBACK(expose),    gui);
144         g_signal_connect(view,    "location-changed", G_CALLBACK(set_site),  gui);
145
146         curl_global_init(CURL_GLOBAL_ALL);
147         curl_handle = curl_easy_init();
148
149         return TRUE;
150 }