]> err.no Git - mapper/blob - src/utils.h
Merge branch 'master' of git+ssh://tal.org/home/git/mapper
[mapper] / src / utils.h
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2006-2007 John Costigan.
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #ifndef _MAPPER_UTILS_H
22 #define _MAPPER_UTILS_H
23
24 #define _GNU_SOURCE
25
26 #include <config.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <stddef.h>
32 #include <locale.h>
33 #include <math.h>
34 #include <errno.h>
35 #include <sys/wait.h>
36 #include <glib/gstdio.h>
37 #include <glib/gi18n.h>
38 #include <gtk/gtk.h>
39 #include <curl/multi.h>
40 #include <gconf/gconf-client.h>
41
42 #include "path.h"
43 #include "mapper-types.h"
44 #include "gpsdata.h"
45
46 /****************************************************************************
47  * BELOW: DEFINES ***********************************************************
48  ****************************************************************************/
49
50 /* #define DEBUG */
51
52 #ifndef DEBUG
53 #define printf(...)
54 #endif
55
56 /* Set the below if to determine whether to get verbose output. */
57 #if 1
58 #define vprintf printf
59 #else
60 #define vprintf(...)
61 #endif
62
63 #define BOUND(x, a, b) { \
64     if((x) < (a)) \
65         (x) = (a); \
66     else if((x) > (b)) \
67         (x) = (b); \
68 }
69
70 #define PI   (3.14159265358979323846f)
71
72 #define MERCATOR_SPAN (-6.28318377773622f)
73 #define MERCATOR_TOP (3.14159188886811f)
74
75 #define SQR(s) ((s)*(s))
76
77 /** MAX_ZOOM defines the largest map zoom level we will download.
78  * (MAX_ZOOM - 1) is the largest map zoom level that the user can zoom to.
79  */
80 #define MAX_ZOOM 16
81
82 #define TILE_SIZE_PIXELS (256)
83 #define TILE_SIZE_P2 (8)
84
85 #ifdef WITH_DEVICE_770
86 #define BUF_WIDTH_TILES (4)
87 #define BUF_HEIGHT_TILES (3)
88 #define BUF_WIDTH_PIXELS (1024)
89 #define BUF_HEIGHT_PIXELS (768)
90 #else
91 #define BUF_WIDTH_TILES (8)
92 #define BUF_HEIGHT_TILES (8)
93 #define BUF_WIDTH_PIXELS (2048)
94 #define BUF_HEIGHT_PIXELS (2048)
95 #endif
96
97 #define ARRAY_CHUNK_SIZE (1024)
98 #define BUFFER_SIZE (2048)
99 #define WORLD_SIZE_UNITS (2 << (MAX_ZOOM + TILE_SIZE_P2))
100
101 #define tile2grid(tile) ((tile) << 3)
102 #define grid2tile(grid) ((grid) >> 3)
103 #define tile2pixel(tile) ((tile) << 8)
104 #define pixel2tile(pixel) ((pixel) >> 8)
105 #define tile2unit(tile) ((tile) << (8 + _zoom))
106 #define unit2tile(unit) ((unit) >> (8 + _zoom))
107 #define tile2zunit(tile, zoom) ((tile) << (8 + zoom))
108 #define unit2ztile(unit, zoom) ((unit) >> (8 + zoom))
109
110 #define grid2pixel(grid) ((grid) << 5)
111 #define pixel2grid(pixel) ((pixel) >> 5)
112 #define grid2unit(grid) ((grid) << (5 + _zoom))
113 #define unit2grid(unit) ((unit) >> (5 + _zoom))
114
115 #define pixel2unit(pixel) ((pixel) << _zoom)
116 #define unit2pixel(pixel) ((pixel) >> _zoom)
117 #define pixel2zunit(pixel, zoom) ((pixel) << (zoom))
118
119 #define unit2bufx(unit) (unit2pixel(unit) - tile2pixel(_base_tilex))
120 #define bufx2unit(x) (pixel2unit(x) + tile2unit(_base_tilex))
121 #define unit2bufy(unit) (unit2pixel(unit) - tile2pixel(_base_tiley))
122 #define bufy2unit(y) (pixel2unit(y) + tile2unit(_base_tiley))
123
124 #define unit2x(unit) (unit2pixel(unit) - tile2pixel(_base_tilex) - _offsetx)
125 #define x2unit(x) (pixel2unit(x + _offsetx) + tile2unit(_base_tilex))
126 #define unit2y(unit) (unit2pixel(unit) - tile2pixel(_base_tiley) - _offsety)
127 #define y2unit(y) (pixel2unit(y + _offsety) + tile2unit(_base_tiley))
128
129 #define leadx2unit() (_pos.unitx + (_lead_ratio) * pixel2unit(_gps.vel_offsetx))
130 #define leady2unit() (_pos.unity + (0.6f*_lead_ratio)*pixel2unit(_gps.vel_offsety))
131
132 /* Pans are done two "grids" at a time, or 64 pixels. */
133 #define PAN_UNITS (grid2unit(2))
134
135 #define INITIAL_DOWNLOAD_RETRIES (3)
136
137 #define MACRO_PARSE_INT(tofill, str) { \
138     gchar *error_check; \
139     (tofill) = strtol((str), &error_check, 10); \
140     if(error_check == (str)) \
141     { \
142         g_printerr("Line %d: Failed to parse string as int: %s\n", __LINE__, str); \
143         MACRO_BANNER_SHOW_INFO(_window, _("Invalid NMEA input from receiver!")); \
144         return; \
145     } \
146 }
147 #define MACRO_PARSE_FLOAT(tofill, str) { \
148     gchar *error_check; \
149     (tofill) = g_ascii_strtod((str), &error_check); \
150     if(error_check == (str)) \
151     { \
152         g_printerr("Failed to parse string as float: %s\n", str); \
153         MACRO_BANNER_SHOW_INFO(_window, _("Invalid NMEA input from receiver!")); \
154         return; \
155     } \
156 }
157
158 #define MACRO_RECALC_CENTER(center_unitx, center_unity) { \
159     switch(_center_mode) \
160     { \
161         case CENTER_LEAD: \
162             center_unitx = leadx2unit(); \
163             center_unity = leady2unit(); \
164             break; \
165         case CENTER_LATLON: \
166             center_unitx = _pos.unitx; \
167             center_unity = _pos.unity; \
168             break; \
169         default: \
170              center_unitx = _center.unitx; \
171              center_unity = _center.unity; \
172             ; \
173     } \
174 };
175
176 #define latlon2unit(lat, lon, unitx, unity) { \
177     gdouble tmp; \
178     unitx = (lon + 180.f) * (WORLD_SIZE_UNITS / 360.f) + 0.5f; \
179     tmp = sinf(lat * (PI / 180.f)); \
180     unity = 0.5f + (WORLD_SIZE_UNITS / MERCATOR_SPAN) \
181         * (logf((1.f + tmp) / (1.f - tmp)) * 0.5f - MERCATOR_TOP); \
182 }
183
184 #define unit2latlon(unitx, unity, lat, lon) { \
185     (lon) = ((unitx) * (360.f / WORLD_SIZE_UNITS)) - 180.f; \
186     (lat) = (360.f * (atanf(expf(((unity) \
187                                   * (MERCATOR_SPAN / WORLD_SIZE_UNITS)) \
188                      + MERCATOR_TOP)))) * (1.f / PI) - 90.f; \
189 }
190
191 #define MACRO_RECALC_OFFSET() { \
192     _offsetx = grid2pixel( \
193             unit2grid(_center.unitx) \
194             - _screen_grids_halfwidth \
195             - tile2grid(_base_tilex)); \
196     _offsety = grid2pixel( \
197             unit2grid(_center.unity) \
198             - _screen_grids_halfheight \
199             - tile2grid(_base_tiley)); \
200 }
201
202 #define MACRO_RECALC_FOCUS_BASE() { \
203     _focus.unitx = x2unit(_screen_width_pixels * _center_ratio / 20); \
204     _focus.unity = y2unit(_screen_height_pixels * _center_ratio / 20); \
205 }
206
207 #define MACRO_RECALC_FOCUS_SIZE() { \
208     _focus_unitwidth = pixel2unit( \
209             (10 - _center_ratio) * _screen_width_pixels / 10); \
210     _focus_unitheight = pixel2unit( \
211             (10 - _center_ratio) * _screen_height_pixels / 10); \
212 }
213
214 #define MACRO_RECALC_CENTER_BOUNDS() { \
215   _min_center.unitx = pixel2unit(grid2pixel(_screen_grids_halfwidth)); \
216   _min_center.unity = pixel2unit(grid2pixel(_screen_grids_halfheight)); \
217   _max_center.unitx = WORLD_SIZE_UNITS-grid2unit(_screen_grids_halfwidth) - 1;\
218   _max_center.unity = WORLD_SIZE_UNITS-grid2unit(_screen_grids_halfheight)- 1;\
219 }
220
221 #define MACRO_PATH_INIT(path) { \
222     (path).head = (path).tail = g_new(Point, ARRAY_CHUNK_SIZE); \
223     *((path).tail) = _point_null; \
224     (path).cap = (path).head + ARRAY_CHUNK_SIZE; \
225     (path).whead = g_new(WayPoint, ARRAY_CHUNK_SIZE); \
226     (path).wtail = (path).whead - 1; \
227     (path).wcap = (path).whead + ARRAY_CHUNK_SIZE; \
228 }
229
230 #define MACRO_PATH_FREE(path) if((path).head) { \
231     WayPoint *curr; \
232     g_free((path).head); \
233     (path).head = (path).tail = (path).cap = NULL; \
234     for(curr = (path).whead - 1; curr++ != (path).wtail; ) \
235         g_free(curr->desc); \
236     g_free((path).whead); \
237     (path).whead = (path).wtail = (path).wcap = NULL; \
238 }
239
240 #define MACRO_PATH_INCREMENT_TAIL(route) { \
241     if(++(route).tail == (route).cap) \
242         path_resize(&(route), (route).cap - (route).head + ARRAY_CHUNK_SIZE);\
243 }
244
245 #define MACRO_PATH_INCREMENT_WTAIL(route) { \
246     if(++(route).wtail == (route).wcap) \
247         path_wresize(&(route), \
248                 (route).wcap - (route).whead + ARRAY_CHUNK_SIZE); \
249 }
250
251 #define DISTANCE_SQUARED(a, b) \
252    ((guint64)((((gint64)(b).unitx)-(a).unitx)*(((gint64)(b).unitx)-(a).unitx))\
253   + (guint64)((((gint64)(b).unity)-(a).unity)*(((gint64)(b).unity)-(a).unity)))
254
255 #define MACRO_QUEUE_DRAW_AREA() \
256     gtk_widget_queue_draw_area( \
257             _map_widget, \
258             0, 0, \
259             _screen_width_pixels, \
260             _screen_height_pixels)
261
262 /* Render all on-map metadata an annotations, including POI and paths. */
263 #define MACRO_MAP_RENDER_DATA() { \
264     if(_show_poi) \
265         map_render_poi(); \
266     if(_show_tracks > 0) \
267         map_render_paths(); \
268 }
269
270 #ifdef WITH_OSSO
271 #define KEEP_DISPLAY_ON() { \
272     /* Note that the flag means keep on ONLY when fullscreen. */ \
273     if(_always_keep_on || _fullscreen) \
274     { \
275         osso_display_state_on(_osso); \
276         osso_display_blanking_pause(_osso); \
277     } \
278 }
279 #else
280 #define KEEP_DISPLAY_ON()
281 #endif
282
283 #define TRACKS_MASK 0x00000001
284 #define ROUTES_MASK 0x00000002
285
286 #define g_timeout_add(I, F, D) g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, \
287           (I), (F), (D), NULL)
288
289 #define MACRO_CURL_EASY_INIT(C) { \
290     C = curl_easy_init(); \
291     curl_easy_setopt(C, CURLOPT_NOPROGRESS, 1); \
292     curl_easy_setopt(C, CURLOPT_FOLLOWLOCATION, 1); \
293     curl_easy_setopt(C, CURLOPT_MAXREDIRS, 20); \
294     curl_easy_setopt(C, CURLOPT_FAILONERROR, 1); \
295     curl_easy_setopt(C, CURLOPT_USERAGENT, \
296             "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.4) Gecko/20060701 Firefox/1.5.0.4"); \
297     curl_easy_setopt(C, CURLOPT_TIMEOUT, 30); \
298     curl_easy_setopt(C, CURLOPT_CONNECTTIMEOUT, 10); \
299 }
300
301 #define MACRO_CURL_PROXY(C) { \
302 if (_http_proxy_host) { \
303         curl_easy_setopt(C, CURLOPT_PROXY, _http_proxy_host);\
304         if (_http_proxy_port) \
305                 curl_easy_setopt(C, CURLOPT_PROXYPORT, _http_proxy_port); \
306 } \
307 }
308
309 #ifdef WITH_HILDON
310 #define MACRO_BANNER_SHOW_INFO(A, S) { \
311     gchar *my_macro_buffer = g_strdup_printf("<span size='%s'>%s</span>", \
312             INFO_FONT_TEXT[_info_font_size], (S)); \
313     hildon_banner_show_information_with_markup(A, NULL, my_macro_buffer); \
314     g_free(my_macro_buffer); \
315 }
316 #else
317 #define MACRO_BANNER_SHOW_INFO(A, S) { hildon_banner_show_information(A, NULL, S); }
318 #endif
319
320 void sound_noise(void);
321 gint download_comparefunc(const ProgressUpdateInfo * a, const ProgressUpdateInfo * b, gpointer user_data);
322 void integerize_data(GpsData *gps, Point *pos);
323
324 #endif