]> Pileus Git - grits/blob - src/plugins/map.c
Improve shutdown code
[grits] / src / plugins / map.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:map
20  * @short_description: Map plugin
21  *
22  * #GritsPluginMap provides map overlays. Much of this data is obtained from the
23  * OpenStreetMap project.
24  */
25
26 #include <time.h>
27 #include <stdlib.h>
28 #include <glib/gstdio.h>
29 #include <GL/gl.h>
30
31 #include <grits.h>
32
33 #include "map.h"
34
35 #define MAX_RESOLUTION 500
36 #define TILE_WIDTH     1024
37 #define TILE_HEIGHT    512
38
39 static const guchar colormap[][2][4] = {
40         {{0x73, 0x91, 0xad}, {0x73, 0x91, 0xad, 0x20}}, // Oceans
41         {{0xf6, 0xee, 0xee}, {0xf6, 0xee, 0xee, 0x00}}, // Ground
42         {{0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}}, // Borders
43         {{0x73, 0x93, 0xad}, {0x73, 0x93, 0xad, 0x40}}, // Lakes
44         {{0xff, 0xe1, 0x80}, {0xff, 0xe1, 0x80, 0x60}}, // Cities
45 };
46
47 struct _LoadTileData {
48         GritsPluginMap *map;
49         GritsTile      *tile;
50         gchar          *path;
51 };
52 static gboolean _load_tile_cb(gpointer _data)
53 {
54         struct _LoadTileData *data = _data;
55         GritsPluginMap *map  = data->map;
56         GritsTile      *tile = data->tile;
57         gchar          *path = data->path;
58         g_free(data);
59
60         /* Load pixbuf */
61         g_debug("GritsPluginMap: _load_tile_cb start");
62         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
63         if (!pixbuf) {
64                 g_warning("GritsPluginMap: _load_tile - Error loading pixbuf %s", path);
65                 g_remove(path);
66                 g_free(path);
67                 return FALSE;
68         }
69         g_free(path);
70
71         /* Create Texture */
72         guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
73         gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
74         gint      width  = gdk_pixbuf_get_width(pixbuf);
75         gint      height = gdk_pixbuf_get_height(pixbuf);
76
77         for (int i = 0; i < width*height; i++) {
78                 for (int j = 0; j < G_N_ELEMENTS(colormap); j++) {
79                         if (pixels[i*4+0] == colormap[j][0][0] &&
80                             pixels[i*4+1] == colormap[j][0][1] &&
81                             pixels[i*4+2] == colormap[j][0][2]) {
82                                 pixels[i*4+0] = colormap[j][1][0];
83                                 pixels[i*4+1] = colormap[j][1][1];
84                                 pixels[i*4+2] = colormap[j][1][2];
85                                 pixels[i*4+3] = colormap[j][1][3];
86                                 break;
87                         }
88                 }
89         }
90
91         guint *tex = g_new0(guint, 1);
92         glGenTextures(1, tex);
93         glBindTexture(GL_TEXTURE_2D, *tex);
94
95         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
96         glPixelStorei(GL_PACK_ALIGNMENT, 1);
97         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
98                         (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
99         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
100         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
101         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
102         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
103         glFlush();
104
105         tile->data = tex;
106         gtk_widget_queue_draw(GTK_WIDGET(map->viewer));
107         g_object_unref(pixbuf);
108         return FALSE;
109 }
110
111 static void _load_tile(GritsTile *tile, gpointer _map)
112 {
113         GritsPluginMap *map = _map;
114         g_debug("GritsPluginMap: _load_tile start %p", g_thread_self());
115         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
116         data->map  = map;
117         data->tile = tile;
118         data->path = grits_wms_fetch(map->wms, tile, GRITS_ONCE, NULL, NULL);
119         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
120         g_debug("GritsPluginMap: _load_tile end %p", g_thread_self());
121 }
122
123 static gboolean _free_tile_cb(gpointer data)
124 {
125         glDeleteTextures(1, data);
126         g_free(data);
127         return FALSE;
128 }
129 static void _free_tile(GritsTile *tile, gpointer _map)
130 {
131         g_debug("GritsPluginMap: _free_tile: %p", tile->data);
132         if (tile->data)
133                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
134 }
135
136 static gpointer _update_tiles(gpointer _map)
137 {
138         g_debug("GritsPluginMap: _update_tiles");
139         GritsPluginMap *map = _map;
140         if (!g_mutex_trylock(map->mutex))
141                 return NULL;
142         GritsPoint eye;
143         grits_viewer_get_location(map->viewer, &eye.lat, &eye.lon, &eye.elev);
144         grits_tile_update(map->tiles, &eye,
145                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
146                         _load_tile, map);
147         grits_tile_gc(map->tiles, time(NULL)-10,
148                         _free_tile, map);
149         g_mutex_unlock(map->mutex);
150         return NULL;
151 }
152
153 /*************
154  * Callbacks *
155  *************/
156 static void _on_location_changed(GritsViewer *viewer,
157                 gdouble lat, gdouble lon, gdouble elev, GritsPluginMap *map)
158 {
159         g_thread_create(_update_tiles, map, FALSE, NULL);
160 }
161
162 static gpointer _threaded_init(GritsPluginMap *map)
163 {
164         _load_tile(map->tiles, map);
165         _update_tiles(map);
166         return NULL;
167 }
168
169 /***********
170  * Methods *
171  ***********/
172 /**
173  * grits_plugin_map_new:
174  * @viewer: the #GritsViewer to use for drawing
175  *
176  * Create a new instance of the map plugin.
177  *
178  * Returns: the new #GritsPluginMap
179  */
180 GritsPluginMap *grits_plugin_map_new(GritsViewer *viewer)
181 {
182         g_debug("GritsPluginMap: new");
183         GritsPluginMap *map = g_object_new(GRITS_TYPE_PLUGIN_MAP, NULL);
184         map->viewer = g_object_ref(viewer);
185
186         /* Load initial tiles */
187         g_thread_create((GThreadFunc)_threaded_init, map, FALSE, NULL);
188
189         /* Connect signals */
190         map->sigid = g_signal_connect(map->viewer, "location-changed",
191                         G_CALLBACK(_on_location_changed), map);
192
193         /* Add renderers */
194         grits_viewer_add(viewer, GRITS_OBJECT(map->tiles), GRITS_LEVEL_OVERLAY-1, 0);
195
196         return map;
197 }
198
199
200 /****************
201  * GObject code *
202  ****************/
203 /* Plugin init */
204 static void grits_plugin_map_plugin_init(GritsPluginInterface *iface);
205 G_DEFINE_TYPE_WITH_CODE(GritsPluginMap, grits_plugin_map, G_TYPE_OBJECT,
206                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
207                         grits_plugin_map_plugin_init));
208 static void grits_plugin_map_plugin_init(GritsPluginInterface *iface)
209 {
210         g_debug("GritsPluginMap: plugin_init");
211         /* Add methods to the interface */
212 }
213 /* Class/Object init */
214 static void grits_plugin_map_init(GritsPluginMap *map)
215 {
216         g_debug("GritsPluginMap: init");
217         /* Set defaults */
218         map->mutex = g_mutex_new();
219         map->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
220         map->wms   = grits_wms_new(
221                 "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
222                 "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
223         g_object_ref(map->tiles);
224 }
225 static void grits_plugin_map_dispose(GObject *gobject)
226 {
227         g_debug("GritsPluginMap: dispose");
228         GritsPluginMap *map = GRITS_PLUGIN_MAP(gobject);
229         /* Drop references */
230         if (map->viewer) {
231                 g_signal_handler_disconnect(map->viewer, map->sigid);
232                 grits_viewer_remove(map->viewer, map->tiles);
233                 soup_session_abort(map->wms->http->soup);
234                 while (gtk_events_pending())
235                         gtk_main_iteration();
236                 g_object_unref(map->viewer);
237                 map->viewer = NULL;
238         }
239         G_OBJECT_CLASS(grits_plugin_map_parent_class)->dispose(gobject);
240 }
241 static void grits_plugin_map_finalize(GObject *gobject)
242 {
243         g_debug("GritsPluginMap: finalize");
244         GritsPluginMap *map = GRITS_PLUGIN_MAP(gobject);
245         /* Free data */
246         grits_tile_free(map->tiles, _free_tile, map);
247         grits_wms_free(map->wms);
248         g_mutex_free(map->mutex);
249         G_OBJECT_CLASS(grits_plugin_map_parent_class)->finalize(gobject);
250
251 }
252 static void grits_plugin_map_class_init(GritsPluginMapClass *klass)
253 {
254         g_debug("GritsPluginMap: class_init");
255         GObjectClass *gobject_class = (GObjectClass*)klass;
256         gobject_class->dispose  = grits_plugin_map_dispose;
257         gobject_class->finalize = grits_plugin_map_finalize;
258 }