2 * This file is part of mapper
4 * Copyright (C) 2006-2007 John Costigan.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #ifndef _MAPPER_UTILS_H
22 #define _MAPPER_UTILS_H
36 #include <glib/gstdio.h>
37 #include <glib/gi18n.h>
39 #include <libgnomevfs/gnome-vfs.h>
40 #include <curl/multi.h>
41 #include <gconf/gconf-client.h>
42 #include <libxml/parser.h>
45 #include "mapper-types.h"
48 /****************************************************************************
49 * BELOW: DEFINES ***********************************************************
50 ****************************************************************************/
58 /* Set the below if to determine whether to get verbose output. */
60 #define vprintf printf
65 #define BOUND(x, a, b) { \
72 #define PI (3.14159265358979323846f)
74 #define MERCATOR_SPAN (-6.28318377773622f)
75 #define MERCATOR_TOP (3.14159188886811f)
77 #define SQR(s) ((s)*(s))
79 /** MAX_ZOOM defines the largest map zoom level we will download.
80 * (MAX_ZOOM - 1) is the largest map zoom level that the user can zoom to.
84 #define TILE_SIZE_PIXELS (256)
85 #define TILE_SIZE_P2 (8)
87 #ifdef WITH_DEVICE_770
88 #define BUF_WIDTH_TILES (4)
89 #define BUF_HEIGHT_TILES (3)
90 #define BUF_WIDTH_PIXELS (1024)
91 #define BUF_HEIGHT_PIXELS (768)
93 #define BUF_WIDTH_TILES (8)
94 #define BUF_HEIGHT_TILES (8)
95 #define BUF_WIDTH_PIXELS (2048)
96 #define BUF_HEIGHT_PIXELS (2048)
99 #define ARRAY_CHUNK_SIZE (1024)
100 #define BUFFER_SIZE (2048)
101 #define WORLD_SIZE_UNITS (2 << (MAX_ZOOM + TILE_SIZE_P2))
103 #define tile2grid(tile) ((tile) << 3)
104 #define grid2tile(grid) ((grid) >> 3)
105 #define tile2pixel(tile) ((tile) << 8)
106 #define pixel2tile(pixel) ((pixel) >> 8)
107 #define tile2unit(tile) ((tile) << (8 + _zoom))
108 #define unit2tile(unit) ((unit) >> (8 + _zoom))
109 #define tile2zunit(tile, zoom) ((tile) << (8 + zoom))
110 #define unit2ztile(unit, zoom) ((unit) >> (8 + zoom))
112 #define grid2pixel(grid) ((grid) << 5)
113 #define pixel2grid(pixel) ((pixel) >> 5)
114 #define grid2unit(grid) ((grid) << (5 + _zoom))
115 #define unit2grid(unit) ((unit) >> (5 + _zoom))
117 #define pixel2unit(pixel) ((pixel) << _zoom)
118 #define unit2pixel(pixel) ((pixel) >> _zoom)
119 #define pixel2zunit(pixel, zoom) ((pixel) << (zoom))
121 #define unit2bufx(unit) (unit2pixel(unit) - tile2pixel(_base_tilex))
122 #define bufx2unit(x) (pixel2unit(x) + tile2unit(_base_tilex))
123 #define unit2bufy(unit) (unit2pixel(unit) - tile2pixel(_base_tiley))
124 #define bufy2unit(y) (pixel2unit(y) + tile2unit(_base_tiley))
126 #define unit2x(unit) (unit2pixel(unit) - tile2pixel(_base_tilex) - _offsetx)
127 #define x2unit(x) (pixel2unit(x + _offsetx) + tile2unit(_base_tilex))
128 #define unit2y(unit) (unit2pixel(unit) - tile2pixel(_base_tiley) - _offsety)
129 #define y2unit(y) (pixel2unit(y + _offsety) + tile2unit(_base_tiley))
131 #define leadx2unit() (_pos.unitx + (_lead_ratio) * pixel2unit(_gps.vel_offsetx))
132 #define leady2unit() (_pos.unity + (0.6f*_lead_ratio)*pixel2unit(_gps.vel_offsety))
134 /* Pans are done two "grids" at a time, or 64 pixels. */
135 #define PAN_UNITS (grid2unit(2))
137 #define INITIAL_DOWNLOAD_RETRIES (3)
139 #define MACRO_PARSE_INT(tofill, str) { \
140 gchar *error_check; \
141 (tofill) = strtol((str), &error_check, 10); \
142 if(error_check == (str)) \
144 g_printerr("Line %d: Failed to parse string as int: %s\n", __LINE__, str); \
145 MACRO_BANNER_SHOW_INFO(_window, _("Invalid NMEA input from receiver!")); \
149 #define MACRO_PARSE_FLOAT(tofill, str) { \
150 gchar *error_check; \
151 (tofill) = g_ascii_strtod((str), &error_check); \
152 if(error_check == (str)) \
154 g_printerr("Failed to parse string as float: %s\n", str); \
155 MACRO_BANNER_SHOW_INFO(_window, _("Invalid NMEA input from receiver!")); \
160 #define MACRO_RECALC_CENTER(center_unitx, center_unity) { \
161 switch(_center_mode) \
164 center_unitx = leadx2unit(); \
165 center_unity = leady2unit(); \
167 case CENTER_LATLON: \
168 center_unitx = _pos.unitx; \
169 center_unity = _pos.unity; \
172 center_unitx = _center.unitx; \
173 center_unity = _center.unity; \
178 #define latlon2unit(lat, lon, unitx, unity) { \
180 unitx = (lon + 180.f) * (WORLD_SIZE_UNITS / 360.f) + 0.5f; \
181 tmp = sinf(lat * (PI / 180.f)); \
182 unity = 0.5f + (WORLD_SIZE_UNITS / MERCATOR_SPAN) \
183 * (logf((1.f + tmp) / (1.f - tmp)) * 0.5f - MERCATOR_TOP); \
186 #define unit2latlon(unitx, unity, lat, lon) { \
187 (lon) = ((unitx) * (360.f / WORLD_SIZE_UNITS)) - 180.f; \
188 (lat) = (360.f * (atanf(expf(((unity) \
189 * (MERCATOR_SPAN / WORLD_SIZE_UNITS)) \
190 + MERCATOR_TOP)))) * (1.f / PI) - 90.f; \
193 #define MACRO_RECALC_OFFSET() { \
194 _offsetx = grid2pixel( \
195 unit2grid(_center.unitx) \
196 - _screen_grids_halfwidth \
197 - tile2grid(_base_tilex)); \
198 _offsety = grid2pixel( \
199 unit2grid(_center.unity) \
200 - _screen_grids_halfheight \
201 - tile2grid(_base_tiley)); \
204 #define MACRO_RECALC_FOCUS_BASE() { \
205 _focus.unitx = x2unit(_screen_width_pixels * _center_ratio / 20); \
206 _focus.unity = y2unit(_screen_height_pixels * _center_ratio / 20); \
209 #define MACRO_RECALC_FOCUS_SIZE() { \
210 _focus_unitwidth = pixel2unit( \
211 (10 - _center_ratio) * _screen_width_pixels / 10); \
212 _focus_unitheight = pixel2unit( \
213 (10 - _center_ratio) * _screen_height_pixels / 10); \
216 #define MACRO_RECALC_CENTER_BOUNDS() { \
217 _min_center.unitx = pixel2unit(grid2pixel(_screen_grids_halfwidth)); \
218 _min_center.unity = pixel2unit(grid2pixel(_screen_grids_halfheight)); \
219 _max_center.unitx = WORLD_SIZE_UNITS-grid2unit(_screen_grids_halfwidth) - 1;\
220 _max_center.unity = WORLD_SIZE_UNITS-grid2unit(_screen_grids_halfheight)- 1;\
223 #define MACRO_PATH_INIT(path) { \
224 (path).head = (path).tail = g_new(Point, ARRAY_CHUNK_SIZE); \
225 *((path).tail) = _point_null; \
226 (path).cap = (path).head + ARRAY_CHUNK_SIZE; \
227 (path).whead = g_new(WayPoint, ARRAY_CHUNK_SIZE); \
228 (path).wtail = (path).whead - 1; \
229 (path).wcap = (path).whead + ARRAY_CHUNK_SIZE; \
232 #define MACRO_PATH_FREE(path) if((path).head) { \
234 g_free((path).head); \
235 (path).head = (path).tail = (path).cap = NULL; \
236 for(curr = (path).whead - 1; curr++ != (path).wtail; ) \
237 g_free(curr->desc); \
238 g_free((path).whead); \
239 (path).whead = (path).wtail = (path).wcap = NULL; \
242 #define MACRO_PATH_INCREMENT_TAIL(route) { \
243 if(++(route).tail == (route).cap) \
244 path_resize(&(route), (route).cap - (route).head + ARRAY_CHUNK_SIZE);\
247 #define MACRO_PATH_INCREMENT_WTAIL(route) { \
248 if(++(route).wtail == (route).wcap) \
249 path_wresize(&(route), \
250 (route).wcap - (route).whead + ARRAY_CHUNK_SIZE); \
253 #define DISTANCE_SQUARED(a, b) \
254 ((guint64)((((gint64)(b).unitx)-(a).unitx)*(((gint64)(b).unitx)-(a).unitx))\
255 + (guint64)((((gint64)(b).unity)-(a).unity)*(((gint64)(b).unity)-(a).unity)))
257 #define MACRO_QUEUE_DRAW_AREA() \
258 gtk_widget_queue_draw_area( \
261 _screen_width_pixels, \
262 _screen_height_pixels)
264 /* Render all on-map metadata an annotations, including POI and paths. */
265 #define MACRO_MAP_RENDER_DATA() { \
268 if(_show_tracks > 0) \
269 map_render_paths(); \
273 #define KEEP_DISPLAY_ON() { \
274 /* Note that the flag means keep on ONLY when fullscreen. */ \
275 if(_always_keep_on || _fullscreen) \
277 osso_display_state_on(_osso); \
278 osso_display_blanking_pause(_osso); \
282 #define KEEP_DISPLAY_ON()
285 #define TRACKS_MASK 0x00000001
286 #define ROUTES_MASK 0x00000002
288 #define g_timeout_add(I, F, D) g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, \
291 #define MACRO_CURL_EASY_INIT(C) { \
292 C = curl_easy_init(); \
293 curl_easy_setopt(C, CURLOPT_NOPROGRESS, 1); \
294 curl_easy_setopt(C, CURLOPT_FOLLOWLOCATION, 1); \
295 curl_easy_setopt(C, CURLOPT_MAXREDIRS, 20); \
296 curl_easy_setopt(C, CURLOPT_FAILONERROR, 1); \
297 curl_easy_setopt(C, CURLOPT_USERAGENT, \
298 "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.4) Gecko/20060701 Firefox/1.5.0.4"); \
299 curl_easy_setopt(C, CURLOPT_TIMEOUT, 30); \
300 curl_easy_setopt(C, CURLOPT_CONNECTTIMEOUT, 10); \
303 #define MACRO_CURL_PROXY(C) { \
304 if (_http_proxy_host) { \
305 curl_easy_setopt(C, CURLOPT_PROXY, _http_proxy_host);\
306 if (_http_proxy_port) \
307 curl_easy_setopt(C, CURLOPT_PROXYPORT, _http_proxy_port); \
312 #define MACRO_BANNER_SHOW_INFO(A, S) { \
313 gchar *my_macro_buffer = g_strdup_printf("<span size='%s'>%s</span>", \
314 INFO_FONT_TEXT[_info_font_size], (S)); \
315 hildon_banner_show_information_with_markup(A, NULL, my_macro_buffer); \
316 g_free(my_macro_buffer); \
319 #define MACRO_BANNER_SHOW_INFO(A, S) { hildon_banner_show_information(A, NULL, S); }
322 #define WRITE_STRING(string) { \
323 GnomeVFSResult vfs_result; \
324 GnomeVFSFileSize size; \
325 if(GNOME_VFS_OK != (vfs_result = gnome_vfs_write( \
326 handle, (string), strlen((string)), &size))) \
328 gchar buffer[BUFFER_SIZE]; \
329 snprintf(buffer, sizeof(buffer), \
330 "%s:\n%s\n%s", _("Error while writing to file"), \
331 _("File is incomplete."), \
332 gnome_vfs_result_to_string(vfs_result)); \
333 popup_error(_window, buffer); \
338 void sound_noise(void);
339 gint download_comparefunc(const ProgressUpdateInfo * a, const ProgressUpdateInfo * b, gpointer user_data);
340 void integerize_data(GpsData *gps, Point *pos);