]> Pileus Git - grits/blob - src/plugins/sat.c
Improve shutdown code
[grits] / src / plugins / sat.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 /**
19  * SECTION:sat
20  * @short_description: Satellite plugin
21  *
22  * #GritsPluginSat provides overlays using satellite imagery. This is mostly
23  * provided by NASA's Blue Marble Next Generation.
24  */
25
26 #include <time.h>
27 #include <string.h>
28 #include <glib/gstdio.h>
29 #include <GL/gl.h>
30
31 #include <grits.h>
32
33 #include "sat.h"
34
35 #define MAX_RESOLUTION 500
36 #define TILE_WIDTH     1024
37 #define TILE_HEIGHT    512
38
39 struct _LoadTileData {
40         GritsPluginSat *sat;
41         GritsTile      *tile;
42         gchar          *path;
43 };
44 static gboolean _load_tile_cb(gpointer _data)
45 {
46         struct _LoadTileData *data = _data;
47         GritsPluginSat *sat  = data->sat;
48         GritsTile      *tile = data->tile;
49         gchar          *path = data->path;
50         g_free(data);
51
52         /* Load pixbuf */
53         g_debug("GritsPluginSat: _load_tile_cb start");
54         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
55         if (!pixbuf) {
56                 g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path);
57                 g_remove(path);
58                 g_free(path);
59                 return FALSE;
60         }
61         g_free(path);
62
63         /* Create Texture */
64         guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
65         gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
66         gint      width  = gdk_pixbuf_get_width(pixbuf);
67         gint      height = gdk_pixbuf_get_height(pixbuf);
68
69         /* Draw a border */
70         //gint border = 10;
71         //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
72         //for (int i = 0; i < border; i++) {
73         //      memset(&pixels[(       i)*stride], 0xff, stride);
74         //      memset(&pixels[(height-i)*stride], 0xff, stride);
75         //}
76         //for (int i = 0; i < height; i++) {
77         //      memset(&pixels[(i*stride)], 0xff, border*4);
78         //      memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
79         //}
80
81         guint *tex = g_new0(guint, 1);
82         glGenTextures(1, tex);
83         glBindTexture(GL_TEXTURE_2D, *tex);
84
85         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
86         glPixelStorei(GL_PACK_ALIGNMENT, 1);
87         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
88                         (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
89         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
90         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
92         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
93         glFlush();
94
95         tile->data = tex;
96         gtk_widget_queue_draw(GTK_WIDGET(sat->viewer));
97         g_object_unref(pixbuf);
98         return FALSE;
99 }
100
101 static void _load_tile(GritsTile *tile, gpointer _sat)
102 {
103         GritsPluginSat *sat = _sat;
104         g_debug("GritsPluginSat: _load_tile start %p", g_thread_self());
105         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
106         data->sat  = sat;
107         data->tile = tile;
108         data->path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
109         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
110         g_debug("GritsPluginSat: _load_tile end %p", g_thread_self());
111 }
112
113 static gboolean _free_tile_cb(gpointer data)
114 {
115         glDeleteTextures(1, data);
116         g_free(data);
117         return FALSE;
118 }
119 static void _free_tile(GritsTile *tile, gpointer _sat)
120 {
121         g_debug("GritsPluginSat: _free_tile: %p", tile->data);
122         if (tile->data)
123                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
124 }
125
126 static gpointer _update_tiles(gpointer _sat)
127 {
128         g_debug("GritsPluginSat: _update_tiles");
129         GritsPluginSat *sat = _sat;
130         if (!g_mutex_trylock(sat->mutex))
131                 return NULL;
132         GritsPoint eye;
133         grits_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
134         grits_tile_update(sat->tiles, &eye,
135                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
136                         _load_tile, sat);
137         grits_tile_gc(sat->tiles, time(NULL)-10,
138                         _free_tile, sat);
139         g_mutex_unlock(sat->mutex);
140         return NULL;
141 }
142
143 /*************
144  * Callbacks *
145  *************/
146 static void _on_location_changed(GritsViewer *viewer,
147                 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
148 {
149         g_thread_create(_update_tiles, sat, FALSE, NULL);
150 }
151
152 static gpointer _threaded_init(GritsPluginSat *sat)
153 {
154         _load_tile(sat->tiles, sat);
155         _update_tiles(sat);
156         return NULL;
157 }
158
159 /***********
160  * Methods *
161  ***********/
162 /**
163  * grits_plugin_sat_new:
164  * @viewer: the #GritsViewer to use for drawing
165  *
166  * Create a new instance of the satellite plugin.
167  *
168  * Returns: the new #GritsPluginSat
169  */
170 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
171 {
172         g_debug("GritsPluginSat: new");
173         GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
174         sat->viewer = g_object_ref(viewer);
175
176         /* Load initial tiles */
177         g_thread_create((GThreadFunc)_threaded_init, sat, FALSE, NULL);
178
179         /* Connect signals */
180         sat->sigid = g_signal_connect(sat->viewer, "location-changed",
181                         G_CALLBACK(_on_location_changed), sat);
182
183         /* Add renderers */
184         grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
185
186         return sat;
187 }
188
189
190 /****************
191  * GObject code *
192  ****************/
193 /* Plugin init */
194 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface);
195 G_DEFINE_TYPE_WITH_CODE(GritsPluginSat, grits_plugin_sat, G_TYPE_OBJECT,
196                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
197                         grits_plugin_sat_plugin_init));
198 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface)
199 {
200         g_debug("GritsPluginSat: plugin_init");
201         /* Add methods to the interface */
202 }
203 /* Class/Object init */
204 static void grits_plugin_sat_init(GritsPluginSat *sat)
205 {
206         g_debug("GritsPluginSat: init");
207         /* Set defaults */
208         sat->mutex = g_mutex_new();
209         sat->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
210         sat->wms   = grits_wms_new(
211                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
212                 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
213         g_object_ref(sat->tiles);
214 }
215 static void grits_plugin_sat_dispose(GObject *gobject)
216 {
217         g_debug("GritsPluginSat: dispose");
218         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
219         /* Drop references */
220         if (sat->viewer) {
221                 g_signal_handler_disconnect(sat->viewer, sat->sigid);
222                 grits_viewer_remove(sat->viewer, sat->tiles);
223                 soup_session_abort(sat->wms->http->soup);
224                 while (gtk_events_pending())
225                         gtk_main_iteration();
226                 g_object_unref(sat->viewer);
227                 sat->viewer = NULL;
228         }
229         G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
230 }
231 static void grits_plugin_sat_finalize(GObject *gobject)
232 {
233         g_debug("GritsPluginSat: finalize");
234         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
235         /* Free data */
236         grits_tile_free(sat->tiles, _free_tile, sat);
237         grits_wms_free(sat->wms);
238         g_mutex_free(sat->mutex);
239         G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject);
240
241 }
242 static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
243 {
244         g_debug("GritsPluginSat: class_init");
245         GObjectClass *gobject_class = (GObjectClass*)klass;
246         gobject_class->dispose  = grits_plugin_sat_dispose;
247         gobject_class->finalize = grits_plugin_sat_finalize;
248 }