Contents of /trunk/src/osm-gps-map.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 150 - (hide annotations)
Thu Oct 29 20:25:11 2009 UTC (14 years, 7 months ago) by harbaum
File MIME type: text/plain
File size: 95493 byte(s)
Finally found and fixed diablo map crash
1 harbaum 32 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*- */
2     /* vim:set et sw=4 ts=4 cino=t0,(0: */
3     /*
4     * osm-gps-map.c
5     * Copyright (C) Marcus Bauer 2008 <marcus.bauer@gmail.com>
6     * Copyright (C) John Stowers 2009 <john.stowers@gmail.com>
7     *
8     * Contributions by
9     * Everaldo Canuto 2009 <everaldo.canuto@gmail.com>
10     *
11     * osm-gps-map.c is free software: you can redistribute it and/or modify it
12     * under the terms of the GNU General Public License as published by the
13     * Free Software Foundation, either version 3 of the License, or
14     * (at your option) any later version.
15     *
16     * osm-gps-map.c is distributed in the hope that it will be useful, but
17     * WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19     * See the GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU General Public License along
22     * with this program. If not, see <http://www.gnu.org/licenses/>.
23     */
24    
25     #include "config.h"
26    
27     #include <fcntl.h>
28     #include <math.h>
29     #include <unistd.h>
30     #include <stdio.h>
31     #include <stdlib.h>
32     #include <string.h>
33    
34     #include <gdk/gdk.h>
35     #include <glib.h>
36 harbaum 55 #include <glib/gstdio.h>
37 harbaum 32 #include <glib/gprintf.h>
38     #include <libsoup/soup.h>
39    
40     #include "converter.h"
41     #include "osm-gps-map-types.h"
42     #include "osm-gps-map.h"
43    
44     #ifdef USE_CAIRO
45     #include <cairo.h>
46     #endif
47    
48     #define ENABLE_DEBUG 0
49    
50     #define EXTRA_BORDER (TILESIZE / 2)
51    
52 harbaum 77 #define OSM_GPS_MAP_SCROLL_STEP 10
53    
54     /* any defined key enables key support */
55     #if (defined(OSM_GPS_MAP_KEY_FULLSCREEN) || \
56     defined(OSM_GPS_MAP_KEY_ZOOMIN) || \
57     defined(OSM_GPS_MAP_KEY_ZOOMOUT) || \
58     defined(OSM_GPS_MAP_KEY_UP) || \
59     defined(OSM_GPS_MAP_KEY_DOWN) || \
60     defined(OSM_GPS_MAP_KEY_LEFT) || \
61     defined(OSM_GPS_MAP_KEY_RIGHT))
62     #define OSM_GPS_MAP_KEYS
63     #endif
64    
65     #ifdef OSM_GPS_MAP_KEYS
66     #include <gdk/gdkkeysyms.h>
67     #endif
68    
69 harbaum 89 #define USER_AGENT "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"
70    
71 harbaum 32 struct _OsmGpsMapPrivate
72     {
73     GHashTable *tile_queue;
74     GHashTable *missing_tiles;
75     GHashTable *tile_cache;
76    
77     int map_zoom;
78     int max_zoom;
79     int min_zoom;
80     gboolean map_auto_center;
81     gboolean map_auto_download;
82     int map_x;
83     int map_y;
84    
85     /* Latitude and longitude of the center of the map, in radians */
86     gfloat center_rlat;
87     gfloat center_rlon;
88    
89     guint max_tile_cache_size;
90     /* Incremented at each redraw */
91     guint redraw_cycle;
92     /* ID of the idle redraw operation */
93     guint idle_map_redraw;
94    
95     //how we download tiles
96     SoupSession *soup_session;
97     char *proxy_uri;
98    
99     //where downloaded tiles are cached
100 harbaum 89 char *tile_dir;
101 harbaum 32 char *cache_dir;
102    
103     //contains flags indicating the various special characters
104     //the uri string contains, that will be replaced when calculating
105     //the uri to download.
106 harbaum 55 OsmGpsMapSource_t map_source;
107 harbaum 32 char *repo_uri;
108 harbaum 55 char *image_format;
109 harbaum 32 int uri_format;
110     //flag indicating if the map source is located on the google
111     gboolean the_google;
112    
113     //gps tracking state
114     gboolean record_trip_history;
115     gboolean show_trip_history;
116     GSList *trip_history;
117     coord_t *gps;
118 harbaum 97 float gps_heading;
119 harbaum 32 gboolean gps_valid;
120    
121 harbaum 61 #ifdef ENABLE_OSD
122 harbaum 73 //the osd controls (if present)
123     osm_gps_map_osd_t *osd;
124 harbaum 75 #ifdef OSD_DOUBLE_BUFFER
125     GdkPixmap *dbuf_pixmap;
126 harbaum 61 #endif
127 harbaum 75 #endif
128 harbaum 77
129     #ifdef OSM_GPS_MAP_KEY_FULLSCREEN
130     gboolean fullscreen;
131     #endif
132 harbaum 73
133 harbaum 32 //additional images or tracks added to the map
134     GSList *tracks;
135     GSList *images;
136    
137     //Used for storing the joined tiles
138     GdkPixmap *pixmap;
139     GdkGC *gc_map;
140    
141     //The tile painted when one cannot be found
142 harbaum 55 GdkPixbuf *null_tile;
143 harbaum 32
144     //For tracking click and drag
145     int drag_counter;
146     int drag_mouse_dx;
147     int drag_mouse_dy;
148     int drag_start_mouse_x;
149     int drag_start_mouse_y;
150     int drag_start_map_x;
151     int drag_start_map_y;
152 harbaum 121 guint drag_expose;
153 harbaum 32
154     //for customizing the redering of the gps track
155     int ui_gps_track_width;
156     int ui_gps_point_inner_radius;
157     int ui_gps_point_outer_radius;
158    
159     guint is_disposed : 1;
160     guint dragging : 1;
161     guint center_coord_set : 1;
162     };
163    
164     #define OSM_GPS_MAP_PRIVATE(o) (OSM_GPS_MAP (o)->priv)
165    
166     typedef struct
167     {
168     GdkPixbuf *pixbuf;
169     /* We keep track of the number of the redraw cycle this tile was last used,
170     * so that osm_gps_map_purge_cache() can remove the older ones */
171     guint redraw_cycle;
172     } OsmCachedTile;
173    
174     enum
175     {
176     PROP_0,
177    
178     PROP_AUTO_CENTER,
179     PROP_RECORD_TRIP_HISTORY,
180     PROP_SHOW_TRIP_HISTORY,
181     PROP_AUTO_DOWNLOAD,
182     PROP_REPO_URI,
183     PROP_PROXY_URI,
184     PROP_TILE_CACHE_DIR,
185     PROP_ZOOM,
186     PROP_MAX_ZOOM,
187     PROP_MIN_ZOOM,
188     PROP_LATITUDE,
189     PROP_LONGITUDE,
190     PROP_MAP_X,
191     PROP_MAP_Y,
192     PROP_TILES_QUEUED,
193     PROP_GPS_TRACK_WIDTH,
194     PROP_GPS_POINT_R1,
195 harbaum 55 PROP_GPS_POINT_R2,
196     PROP_MAP_SOURCE,
197     PROP_IMAGE_FORMAT
198 harbaum 32 };
199    
200     G_DEFINE_TYPE (OsmGpsMap, osm_gps_map, GTK_TYPE_DRAWING_AREA);
201    
202     /*
203     * Drawing function forward defintions
204     */
205     static gchar *replace_string(const gchar *src, const gchar *from, const gchar *to);
206     static void inspect_map_uri(OsmGpsMap *map);
207     static gchar *replace_map_uri(OsmGpsMap *map, const gchar *uri, int zoom, int x, int y);
208     static void osm_gps_map_print_images (OsmGpsMap *map);
209     static void osm_gps_map_draw_gps_point (OsmGpsMap *map);
210     static void osm_gps_map_blit_tile(OsmGpsMap *map, GdkPixbuf *pixbuf, int offset_x, int offset_y);
211 harbaum 55 #ifdef LIBSOUP22
212     static void osm_gps_map_tile_download_complete (SoupMessage *msg, gpointer user_data);
213     #else
214     static void osm_gps_map_tile_download_complete (SoupSession *session, SoupMessage *msg, gpointer user_data);
215     #endif
216 harbaum 32 static void osm_gps_map_download_tile (OsmGpsMap *map, int zoom, int x, int y, gboolean redraw);
217     static void osm_gps_map_load_tile (OsmGpsMap *map, int zoom, int x, int y, int offset_x, int offset_y);
218     static void osm_gps_map_fill_tiles_pixel (OsmGpsMap *map);
219     static gboolean osm_gps_map_map_redraw (OsmGpsMap *map);
220     static void osm_gps_map_map_redraw_idle (OsmGpsMap *map);
221    
222     static void
223     cached_tile_free (OsmCachedTile *tile)
224     {
225     g_object_unref (tile->pixbuf);
226     g_slice_free (OsmCachedTile, tile);
227     }
228    
229     /*
230     * Description:
231     * Find and replace text within a string.
232     *
233     * Parameters:
234     * src (in) - pointer to source string
235     * from (in) - pointer to search text
236     * to (in) - pointer to replacement text
237     *
238     * Returns:
239     * Returns a pointer to dynamically-allocated memory containing string
240     * with occurences of the text pointed to by 'from' replaced by with the
241     * text pointed to by 'to'.
242     */
243     static gchar *
244     replace_string(const gchar *src, const gchar *from, const gchar *to)
245     {
246     size_t size = strlen(src) + 1;
247     size_t fromlen = strlen(from);
248     size_t tolen = strlen(to);
249    
250     /* Allocate the first chunk with enough for the original string. */
251     gchar *value = g_malloc(size);
252    
253    
254     /* We need to return 'value', so let's make a copy to mess around with. */
255     gchar *dst = value;
256    
257     if ( value != NULL )
258     {
259     for ( ;; )
260     {
261     /* Try to find the search text. */
262     const gchar *match = g_strstr_len(src, size, from);
263     if ( match != NULL )
264     {
265     gchar *temp;
266     /* Find out how many characters to copy up to the 'match'. */
267     size_t count = match - src;
268    
269    
270     /* Calculate the total size the string will be after the
271     * replacement is performed. */
272     size += tolen - fromlen;
273    
274     temp = g_realloc(value, size);
275     if ( temp == NULL )
276     {
277     g_free(value);
278     return NULL;
279     }
280    
281     /* we'll want to return 'value' eventually, so let's point it
282     * to the memory that we are now working with.
283     * And let's not forget to point to the right location in
284     * the destination as well. */
285     dst = temp + (dst - value);
286     value = temp;
287    
288     /*
289     * Copy from the source to the point where we matched. Then
290     * move the source pointer ahead by the amount we copied. And
291     * move the destination pointer ahead by the same amount.
292     */
293     g_memmove(dst, src, count);
294     src += count;
295     dst += count;
296    
297     /* Now copy in the replacement text 'to' at the position of
298     * the match. Adjust the source pointer by the text we replaced.
299     * Adjust the destination pointer by the amount of replacement
300     * text. */
301     g_memmove(dst, to, tolen);
302     src += fromlen;
303     dst += tolen;
304     }
305     else
306     {
307     /*
308     * Copy any remaining part of the string. This includes the null
309     * termination character.
310     */
311     strcpy(dst, src);
312     break;
313     }
314     }
315     }
316     return value;
317     }
318    
319     static void
320     map_convert_coords_to_quadtree_string(OsmGpsMap *map, gint x, gint y, gint zoomlevel,
321     gchar *buffer, const gchar initial,
322     const gchar *const quadrant)
323     {
324     gchar *ptr = buffer;
325     gint n;
326    
327     if (initial)
328     *ptr++ = initial;
329    
330     for(n = zoomlevel-1; n >= 0; n--)
331     {
332     gint xbit = (x >> n) & 1;
333     gint ybit = (y >> n) & 1;
334     *ptr++ = quadrant[xbit + 2 * ybit];
335     }
336    
337     *ptr++ = '\0';
338     }
339    
340    
341     static void
342     inspect_map_uri(OsmGpsMap *map)
343     {
344     OsmGpsMapPrivate *priv = map->priv;
345 harbaum 89 priv->uri_format = 0;
346     priv->the_google = FALSE;
347 harbaum 32
348     if (g_strrstr(priv->repo_uri, URI_MARKER_X))
349     priv->uri_format |= URI_HAS_X;
350    
351     if (g_strrstr(priv->repo_uri, URI_MARKER_Y))
352     priv->uri_format |= URI_HAS_Y;
353    
354     if (g_strrstr(priv->repo_uri, URI_MARKER_Z))
355     priv->uri_format |= URI_HAS_Z;
356    
357     if (g_strrstr(priv->repo_uri, URI_MARKER_S))
358     priv->uri_format |= URI_HAS_S;
359    
360     if (g_strrstr(priv->repo_uri, URI_MARKER_Q))
361     priv->uri_format |= URI_HAS_Q;
362    
363     if (g_strrstr(priv->repo_uri, URI_MARKER_Q0))
364     priv->uri_format |= URI_HAS_Q0;
365    
366     if (g_strrstr(priv->repo_uri, URI_MARKER_YS))
367     priv->uri_format |= URI_HAS_YS;
368    
369     if (g_strrstr(priv->repo_uri, URI_MARKER_R))
370     priv->uri_format |= URI_HAS_R;
371    
372     if (g_strrstr(priv->repo_uri, "google.com"))
373     priv->the_google = TRUE;
374    
375     g_debug("URI Format: 0x%X (google: %X)", priv->uri_format, priv->the_google);
376    
377     }
378    
379     static gchar *
380     replace_map_uri(OsmGpsMap *map, const gchar *uri, int zoom, int x, int y)
381     {
382     OsmGpsMapPrivate *priv = map->priv;
383     char *url;
384     unsigned int i;
385     char location[22];
386    
387     i = 1;
388     url = g_strdup(uri);
389     while (i < URI_FLAG_END)
390     {
391     char *s = NULL;
392     char *old;
393    
394     old = url;
395     switch(i & priv->uri_format)
396     {
397     case URI_HAS_X:
398     s = g_strdup_printf("%d", x);
399     url = replace_string(url, URI_MARKER_X, s);
400     //g_debug("FOUND " URI_MARKER_X);
401     break;
402     case URI_HAS_Y:
403     s = g_strdup_printf("%d", y);
404     url = replace_string(url, URI_MARKER_Y, s);
405     //g_debug("FOUND " URI_MARKER_Y);
406     break;
407     case URI_HAS_Z:
408     s = g_strdup_printf("%d", zoom);
409     url = replace_string(url, URI_MARKER_Z, s);
410     //g_debug("FOUND " URI_MARKER_Z);
411     break;
412     case URI_HAS_S:
413     s = g_strdup_printf("%d", priv->max_zoom-zoom);
414     url = replace_string(url, URI_MARKER_S, s);
415     //g_debug("FOUND " URI_MARKER_S);
416     break;
417     case URI_HAS_Q:
418     map_convert_coords_to_quadtree_string(map,x,y,zoom,location,'t',"qrts");
419     s = g_strdup_printf("%s", location);
420     url = replace_string(url, URI_MARKER_Q, s);
421     //g_debug("FOUND " URI_MARKER_Q);
422     break;
423     case URI_HAS_Q0:
424     map_convert_coords_to_quadtree_string(map,x,y,zoom,location,'\0', "0123");
425     s = g_strdup_printf("%s", location);
426     url = replace_string(url, URI_MARKER_Q0, s);
427     //g_debug("FOUND " URI_MARKER_Q0);
428     break;
429     case URI_HAS_YS:
430     // s = g_strdup_printf("%d", y);
431     // url = replace_string(url, URI_MARKER_YS, s);
432     g_warning("FOUND " URI_MARKER_YS " NOT IMPLEMENTED");
433     // retval = g_strdup_printf(repo->url,
434     // tilex,
435     // (1 << (MAX_ZOOM - zoom)) - tiley - 1,
436     // zoom - (MAX_ZOOM - 17));
437     break;
438     case URI_HAS_R:
439     s = g_strdup_printf("%d", g_random_int_range(0,4));
440     url = replace_string(url, URI_MARKER_R, s);
441     //g_debug("FOUND " URI_MARKER_R);
442     break;
443     default:
444     s = NULL;
445     break;
446     }
447    
448     if (s) {
449     g_free(s);
450     g_free(old);
451     }
452    
453     i = (i << 1);
454    
455     }
456    
457     return url;
458     }
459    
460     static void
461     my_log_handler (const gchar * log_domain, GLogLevelFlags log_level, const gchar * message, gpointer user_data)
462     {
463     if (!(log_level & G_LOG_LEVEL_DEBUG) || ENABLE_DEBUG)
464     g_log_default_handler (log_domain, log_level, message, user_data);
465     }
466    
467 harbaum 55 static float
468     osm_gps_map_get_scale_at_point(int zoom, float rlat, float rlon)
469     {
470     /* world at zoom 1 == 512 pixels */
471     return cos(rlat) * M_PI * OSM_EQ_RADIUS / (1<<(7+zoom));
472     }
473    
474 harbaum 32 /* clears the trip list and all resources */
475     static void
476     osm_gps_map_free_trip (OsmGpsMap *map)
477     {
478     OsmGpsMapPrivate *priv = map->priv;
479     if (priv->trip_history) {
480     g_slist_foreach(priv->trip_history, (GFunc) g_free, NULL);
481     g_slist_free(priv->trip_history);
482     priv->trip_history = NULL;
483     }
484     }
485    
486     /* clears the tracks and all resources */
487     static void
488     osm_gps_map_free_tracks (OsmGpsMap *map)
489     {
490     OsmGpsMapPrivate *priv = map->priv;
491     if (priv->tracks)
492     {
493     GSList* tmp = priv->tracks;
494     while (tmp != NULL)
495     {
496     g_slist_foreach(tmp->data, (GFunc) g_free, NULL);
497     g_slist_free(tmp->data);
498     tmp = g_slist_next(tmp);
499     }
500     g_slist_free(priv->tracks);
501     priv->tracks = NULL;
502     }
503     }
504    
505     /* free the poi image lists */
506     static void
507     osm_gps_map_free_images (OsmGpsMap *map)
508     {
509     OsmGpsMapPrivate *priv = map->priv;
510     if (priv->images) {
511     GSList *list;
512     for(list = priv->images; list != NULL; list = list->next)
513     {
514     image_t *im = list->data;
515     g_object_unref(im->image);
516     g_free(im);
517     }
518     g_slist_free(priv->images);
519     priv->images = NULL;
520     }
521     }
522    
523     static void
524     osm_gps_map_print_images (OsmGpsMap *map)
525     {
526     GSList *list;
527     int x,y,pixel_x,pixel_y;
528     int min_x = 0,min_y = 0,max_x = 0,max_y = 0;
529     int map_x0, map_y0;
530     OsmGpsMapPrivate *priv = map->priv;
531    
532     map_x0 = priv->map_x - EXTRA_BORDER;
533     map_y0 = priv->map_y - EXTRA_BORDER;
534     for(list = priv->images; list != NULL; list = list->next)
535     {
536     image_t *im = list->data;
537    
538     // pixel_x,y, offsets
539     pixel_x = lon2pixel(priv->map_zoom, im->pt.rlon);
540     pixel_y = lat2pixel(priv->map_zoom, im->pt.rlat);
541    
542     g_debug("Image %dx%d @: %f,%f (%d,%d)",
543     im->w, im->h,
544     im->pt.rlat, im->pt.rlon,
545     pixel_x, pixel_y);
546    
547     x = pixel_x - map_x0;
548     y = pixel_y - map_y0;
549    
550     gdk_draw_pixbuf (
551     priv->pixmap,
552     priv->gc_map,
553     im->image,
554     0,0,
555     x-(im->w/2),y-(im->h/2),
556     im->w,im->h,
557     GDK_RGB_DITHER_NONE, 0, 0);
558    
559     max_x = MAX(x+im->w,max_x);
560     min_x = MIN(x-im->w,min_x);
561     max_y = MAX(y+im->h,max_y);
562     min_y = MIN(y-im->h,min_y);
563     }
564    
565     gtk_widget_queue_draw_area (
566     GTK_WIDGET(map),
567     min_x + EXTRA_BORDER, min_y + EXTRA_BORDER,
568     max_x + EXTRA_BORDER, max_y + EXTRA_BORDER);
569    
570     }
571    
572     static void
573     osm_gps_map_draw_gps_point (OsmGpsMap *map)
574     {
575     OsmGpsMapPrivate *priv = map->priv;
576    
577     //incase we get called before we have got a gps point
578     if (priv->gps_valid) {
579     int map_x0, map_y0;
580     int x, y;
581     int r = priv->ui_gps_point_inner_radius;
582     int r2 = priv->ui_gps_point_outer_radius;
583 harbaum 97 int mr = MAX(3*r,r2);
584 harbaum 32
585     map_x0 = priv->map_x - EXTRA_BORDER;
586     map_y0 = priv->map_y - EXTRA_BORDER;
587     x = lon2pixel(priv->map_zoom, priv->gps->rlon) - map_x0;
588     y = lat2pixel(priv->map_zoom, priv->gps->rlat) - map_y0;
589     #ifdef USE_CAIRO
590     cairo_t *cr;
591     cairo_pattern_t *pat;
592     #else
593     GdkColor color;
594     GdkGC *marker;
595     #endif
596    
597     #ifdef USE_CAIRO
598     cr = gdk_cairo_create(priv->pixmap);
599    
600     // draw transparent area
601     if (r2 > 0) {
602     cairo_set_line_width (cr, 1.5);
603     cairo_set_source_rgba (cr, 0.75, 0.75, 0.75, 0.4);
604     cairo_arc (cr, x, y, r2, 0, 2 * M_PI);
605     cairo_fill (cr);
606     // draw transparent area border
607     cairo_set_source_rgba (cr, 0.55, 0.55, 0.55, 0.4);
608     cairo_arc (cr, x, y, r2, 0, 2 * M_PI);
609     cairo_stroke(cr);
610     }
611    
612     // draw ball gradient
613     if (r > 0) {
614 harbaum 97 // draw direction arrow
615     if(!isnan(priv->gps_heading))
616     {
617     cairo_move_to (cr, x-r*cos(priv->gps_heading), y-r*sin(priv->gps_heading));
618     cairo_line_to (cr, x+3*r*sin(priv->gps_heading), y-3*r*cos(priv->gps_heading));
619     cairo_line_to (cr, x+r*cos(priv->gps_heading), y+r*sin(priv->gps_heading));
620     cairo_close_path (cr);
621    
622     cairo_set_source_rgba (cr, 0.3, 0.3, 1.0, 0.5);
623     cairo_fill_preserve (cr);
624    
625     cairo_set_line_width (cr, 1.0);
626     cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
627     cairo_stroke(cr);
628     }
629    
630 harbaum 32 pat = cairo_pattern_create_radial (x-(r/5), y-(r/5), (r/5), x, y, r);
631     cairo_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1.0);
632     cairo_pattern_add_color_stop_rgba (pat, 1, 0, 0, 1, 1.0);
633     cairo_set_source (cr, pat);
634     cairo_arc (cr, x, y, r, 0, 2 * M_PI);
635     cairo_fill (cr);
636     cairo_pattern_destroy (pat);
637     // draw ball border
638     cairo_set_line_width (cr, 1.0);
639     cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
640     cairo_arc (cr, x, y, r, 0, 2 * M_PI);
641     cairo_stroke(cr);
642     }
643 harbaum 97
644 harbaum 32 cairo_destroy(cr);
645     gtk_widget_queue_draw_area (GTK_WIDGET(map),
646     x-mr,
647     y-mr,
648     mr*2,
649     mr*2);
650     #else
651     marker = gdk_gc_new(priv->pixmap);
652     color.red = 5000;
653     color.green = 5000;
654     color.blue = 55000;
655     gdk_gc_set_rgb_fg_color(marker, &color);
656     gdk_gc_set_line_attributes(marker, lw, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
657    
658     if (r2 > 0) {
659     gdk_draw_arc (priv->pixmap,
660     marker,
661     FALSE, //filled
662     x-r2, y-r2, // x,y
663     r2*2,r2*2, // width, height
664     0, 360*64); // start-end angle 64th, from 3h, anti clockwise
665     }
666     if (r > 0) {
667     gdk_draw_arc (priv->pixmap,
668     marker,
669     TRUE, //filled
670     x-r, y-r, // x,y
671     r*2,r*2, // width, height
672     0, 360*64); // start-end angle 64th, from 3h, anti clockwise
673     }
674    
675     g_object_unref(marker);
676     gtk_widget_queue_draw_area (GTK_WIDGET(map),
677     x-(mr+lw),
678     y-(mr+lw),
679     (mr*2)+lw+lw,
680     (mr*2)+lw+lw);
681     #endif
682     }
683 harbaum 57 }
684    
685 harbaum 32 static void
686     osm_gps_map_blit_tile(OsmGpsMap *map, GdkPixbuf *pixbuf, int offset_x, int offset_y)
687     {
688     OsmGpsMapPrivate *priv = map->priv;
689    
690 harbaum 55 g_debug("Queing redraw @ %d,%d (w:%d h:%d)", offset_x,offset_y, TILESIZE,TILESIZE);
691 harbaum 32
692     /* draw pixbuf onto pixmap */
693     gdk_draw_pixbuf (priv->pixmap,
694     priv->gc_map,
695     pixbuf,
696     0,0,
697     offset_x,offset_y,
698     TILESIZE,TILESIZE,
699     GDK_RGB_DITHER_NONE, 0, 0);
700     }
701    
702     /* libsoup-2.2 and libsoup-2.4 use different ways to store the body data */
703     #ifdef LIBSOUP22
704     #define soup_message_headers_append(a,b,c) soup_message_add_header(a,b,c)
705     #define MSG_RESPONSE_BODY(a) ((a)->response.body)
706     #define MSG_RESPONSE_LEN(a) ((a)->response.length)
707     #define MSG_RESPONSE_LEN_FORMAT "%u"
708     #else
709     #define MSG_RESPONSE_BODY(a) ((a)->response_body->data)
710     #define MSG_RESPONSE_LEN(a) ((a)->response_body->length)
711     #define MSG_RESPONSE_LEN_FORMAT "%lld"
712     #endif
713    
714     #ifdef LIBSOUP22
715     static void
716     osm_gps_map_tile_download_complete (SoupMessage *msg, gpointer user_data)
717     #else
718     static void
719     osm_gps_map_tile_download_complete (SoupSession *session, SoupMessage *msg, gpointer user_data)
720     #endif
721     {
722 harbaum 55 FILE *file;
723 harbaum 32 tile_download_t *dl = (tile_download_t *)user_data;
724     OsmGpsMap *map = OSM_GPS_MAP(dl->map);
725     OsmGpsMapPrivate *priv = map->priv;
726     gboolean file_saved = FALSE;
727    
728     if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
729     {
730     /* save tile into cachedir if one has been specified */
731     if (priv->cache_dir)
732     {
733     if (g_mkdir_with_parents(dl->folder,0700) == 0)
734     {
735 harbaum 55 file = g_fopen(dl->filename, "wb");
736     if (file != NULL)
737 harbaum 32 {
738 harbaum 55 fwrite (MSG_RESPONSE_BODY(msg), 1, MSG_RESPONSE_LEN(msg), file);
739 harbaum 32 file_saved = TRUE;
740 harbaum 55 g_debug("Wrote "MSG_RESPONSE_LEN_FORMAT" bytes to %s", MSG_RESPONSE_LEN(msg), dl->filename);
741     fclose (file);
742 harbaum 32
743     }
744     }
745     else
746     {
747 harbaum 62 g_warning("Error creating tile download directory: %s",
748     dl->folder);
749     perror("perror:");
750 harbaum 32 }
751     }
752    
753     if (dl->redraw)
754     {
755     GdkPixbuf *pixbuf = NULL;
756    
757 harbaum 55 /* if the file was actually stored on disk, we can simply */
758     /* load and decode it from that file */
759 harbaum 32 if (priv->cache_dir)
760     {
761     if (file_saved)
762     {
763     pixbuf = gdk_pixbuf_new_from_file (dl->filename, NULL);
764     }
765     }
766     else
767     {
768     GdkPixbufLoader *loader;
769     char *extension = strrchr (dl->filename, '.');
770    
771     /* parse file directly from memory */
772     if (extension)
773     {
774     loader = gdk_pixbuf_loader_new_with_type (extension+1, NULL);
775     if (!gdk_pixbuf_loader_write (loader, (unsigned char*)MSG_RESPONSE_BODY(msg), MSG_RESPONSE_LEN(msg), NULL))
776     {
777     g_warning("Error: Decoding of image failed");
778     }
779     gdk_pixbuf_loader_close(loader, NULL);
780    
781     pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
782    
783     /* give up loader but keep the pixbuf */
784     g_object_ref(pixbuf);
785     g_object_unref(loader);
786     }
787     else
788     {
789     g_warning("Error: Unable to determine image file format");
790     }
791     }
792 harbaum 55
793 harbaum 32 /* Store the tile into the cache */
794     if (G_LIKELY (pixbuf))
795     {
796     OsmCachedTile *tile = g_slice_new (OsmCachedTile);
797     tile->pixbuf = pixbuf;
798     tile->redraw_cycle = priv->redraw_cycle;
799     /* if the tile is already in the cache (it could be one
800     * rendered from another zoom level), it will be
801     * overwritten */
802     g_hash_table_insert (priv->tile_cache, dl->filename, tile);
803     /* NULL-ify dl->filename so that it won't be freed, as
804     * we are using it as a key in the hash table */
805     dl->filename = NULL;
806     }
807     osm_gps_map_map_redraw_idle (map);
808     }
809     g_hash_table_remove(priv->tile_queue, dl->uri);
810    
811     g_free(dl->uri);
812     g_free(dl->folder);
813     g_free(dl->filename);
814     g_free(dl);
815     }
816     else
817     {
818     g_warning("Error downloading tile: %d - %s", msg->status_code, msg->reason_phrase);
819     if (msg->status_code == SOUP_STATUS_NOT_FOUND)
820     {
821     g_hash_table_insert(priv->missing_tiles, dl->uri, NULL);
822     g_hash_table_remove(priv->tile_queue, dl->uri);
823     }
824     else if (msg->status_code == SOUP_STATUS_CANCELLED)
825     {
826     ;//application exiting
827     }
828     else
829     {
830     #ifdef LIBSOUP22
831     soup_session_requeue_message(dl->session, msg);
832     #else
833     soup_session_requeue_message(session, msg);
834     #endif
835     return;
836     }
837     }
838    
839    
840     }
841    
842     static void
843     osm_gps_map_download_tile (OsmGpsMap *map, int zoom, int x, int y, gboolean redraw)
844     {
845     SoupMessage *msg;
846     OsmGpsMapPrivate *priv = map->priv;
847     tile_download_t *dl = g_new0(tile_download_t,1);
848    
849     //calculate the uri to download
850     dl->uri = replace_map_uri(map, priv->repo_uri, zoom, x, y);
851    
852     #ifdef LIBSOUP22
853     dl->session = priv->soup_session;
854     #endif
855    
856     //check the tile has not already been queued for download,
857     //or has been attempted, and its missing
858     if (g_hash_table_lookup_extended(priv->tile_queue, dl->uri, NULL, NULL) ||
859     g_hash_table_lookup_extended(priv->missing_tiles, dl->uri, NULL, NULL) )
860     {
861     g_debug("Tile already downloading (or missing)");
862     g_free(dl->uri);
863     g_free(dl);
864     } else {
865 harbaum 55 dl->folder = g_strdup_printf("%s%c%d%c%d%c",
866     priv->cache_dir, G_DIR_SEPARATOR,
867     zoom, G_DIR_SEPARATOR,
868     x, G_DIR_SEPARATOR);
869     dl->filename = g_strdup_printf("%s%c%d%c%d%c%d.%s",
870     priv->cache_dir, G_DIR_SEPARATOR,
871     zoom, G_DIR_SEPARATOR,
872     x, G_DIR_SEPARATOR,
873     y,
874     priv->image_format);
875 harbaum 32 dl->map = map;
876     dl->redraw = redraw;
877    
878     g_debug("Download tile: %d,%d z:%d\n\t%s --> %s", x, y, zoom, dl->uri, dl->filename);
879    
880     msg = soup_message_new (SOUP_METHOD_GET, dl->uri);
881     if (msg) {
882     if (priv->the_google) {
883     //Set maps.google.com as the referrer
884     g_debug("Setting Google Referrer");
885     soup_message_headers_append(msg->request_headers, "Referer", "http://maps.google.com/");
886     //For google satelite also set the appropriate cookie value
887     if (priv->uri_format & URI_HAS_Q) {
888     const char *cookie = g_getenv("GOOGLE_COOKIE");
889     if (cookie) {
890     g_debug("Adding Google Cookie");
891     soup_message_headers_append(msg->request_headers, "Cookie", cookie);
892     }
893     }
894     }
895    
896 harbaum 89 #ifdef LIBSOUP22
897     soup_message_headers_append(msg->request_headers,
898     "User-Agent", USER_AGENT);
899     #endif
900    
901 harbaum 32 g_hash_table_insert (priv->tile_queue, dl->uri, msg);
902     soup_session_queue_message (priv->soup_session, msg, osm_gps_map_tile_download_complete, dl);
903     } else {
904     g_warning("Could not create soup message");
905     g_free(dl->uri);
906     g_free(dl->folder);
907     g_free(dl->filename);
908     g_free(dl);
909     }
910     }
911     }
912    
913     static GdkPixbuf *
914     osm_gps_map_load_cached_tile (OsmGpsMap *map, int zoom, int x, int y)
915     {
916     OsmGpsMapPrivate *priv = map->priv;
917     gchar *filename;
918 harbaum 55 GdkPixbuf *pixbuf = NULL;
919 harbaum 32 OsmCachedTile *tile;
920    
921 harbaum 55 filename = g_strdup_printf("%s%c%d%c%d%c%d.%s",
922     priv->cache_dir, G_DIR_SEPARATOR,
923     zoom, G_DIR_SEPARATOR,
924     x, G_DIR_SEPARATOR,
925     y,
926     priv->image_format);
927 harbaum 32
928     tile = g_hash_table_lookup (priv->tile_cache, filename);
929     if (tile)
930     {
931     g_free (filename);
932     }
933 harbaum 55 else
934 harbaum 32 {
935     pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
936     if (pixbuf)
937     {
938     tile = g_slice_new (OsmCachedTile);
939     tile->pixbuf = pixbuf;
940     g_hash_table_insert (priv->tile_cache, filename, tile);
941     }
942     }
943    
944     /* set/update the redraw_cycle timestamp on the tile */
945     if (tile)
946     {
947     tile->redraw_cycle = priv->redraw_cycle;
948     pixbuf = g_object_ref (tile->pixbuf);
949 harbaum 55 }
950 harbaum 32
951     return pixbuf;
952     }
953    
954     static GdkPixbuf *
955     osm_gps_map_find_bigger_tile (OsmGpsMap *map, int zoom, int x, int y,
956     int *zoom_found)
957     {
958     GdkPixbuf *pixbuf;
959     int next_zoom, next_x, next_y;
960    
961     if (zoom == 0) return NULL;
962     next_zoom = zoom - 1;
963     next_x = x / 2;
964     next_y = y / 2;
965     pixbuf = osm_gps_map_load_cached_tile (map, next_zoom, next_x, next_y);
966     if (pixbuf)
967     *zoom_found = next_zoom;
968     else
969     pixbuf = osm_gps_map_find_bigger_tile (map, next_zoom, next_x, next_y,
970     zoom_found);
971     return pixbuf;
972     }
973    
974     static GdkPixbuf *
975     osm_gps_map_render_missing_tile_upscaled (OsmGpsMap *map, int zoom,
976     int x, int y)
977     {
978     GdkPixbuf *pixbuf, *big, *area;
979     int zoom_big, zoom_diff, area_size, area_x, area_y;
980     int modulo;
981    
982     big = osm_gps_map_find_bigger_tile (map, zoom, x, y, &zoom_big);
983     if (!big) return NULL;
984    
985     g_debug ("Found bigger tile (zoom = %d, wanted = %d)", zoom_big, zoom);
986    
987     /* get a Pixbuf for the area to magnify */
988     zoom_diff = zoom - zoom_big;
989     area_size = TILESIZE >> zoom_diff;
990     modulo = 1 << zoom_diff;
991     area_x = (x % modulo) * area_size;
992     area_y = (y % modulo) * area_size;
993     area = gdk_pixbuf_new_subpixbuf (big, area_x, area_y,
994     area_size, area_size);
995     g_object_unref (big);
996     pixbuf = gdk_pixbuf_scale_simple (area, TILESIZE, TILESIZE,
997     GDK_INTERP_NEAREST);
998     g_object_unref (area);
999     return pixbuf;
1000     }
1001    
1002     static GdkPixbuf *
1003     osm_gps_map_render_missing_tile (OsmGpsMap *map, int zoom, int x, int y)
1004     {
1005     /* maybe TODO: render from downscaled tiles, if the following fails */
1006     return osm_gps_map_render_missing_tile_upscaled (map, zoom, x, y);
1007     }
1008    
1009     static void
1010     osm_gps_map_load_tile (OsmGpsMap *map, int zoom, int x, int y, int offset_x, int offset_y)
1011     {
1012     OsmGpsMapPrivate *priv = map->priv;
1013 harbaum 55 gchar *filename;
1014 harbaum 32 GdkPixbuf *pixbuf;
1015    
1016     g_debug("Load tile %d,%d (%d,%d) z:%d", x, y, offset_x, offset_y, zoom);
1017    
1018 harbaum 55 if (priv->map_source == OSM_GPS_MAP_SOURCE_NULL) {
1019     osm_gps_map_blit_tile(map, priv->null_tile, offset_x,offset_y);
1020     return;
1021     }
1022 harbaum 32
1023 harbaum 55 filename = g_strdup_printf("%s%c%d%c%d%c%d.%s",
1024     priv->cache_dir, G_DIR_SEPARATOR,
1025     zoom, G_DIR_SEPARATOR,
1026     x, G_DIR_SEPARATOR,
1027     y,
1028     priv->image_format);
1029    
1030     /* try to get file from internal cache first */
1031     if(!(pixbuf = osm_gps_map_load_cached_tile(map, zoom, x, y)))
1032 harbaum 32 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
1033    
1034 harbaum 55 if(pixbuf)
1035 harbaum 32 {
1036 harbaum 55 g_debug("Found tile %s", filename);
1037 harbaum 32 osm_gps_map_blit_tile(map, pixbuf, offset_x,offset_y);
1038     g_object_unref (pixbuf);
1039     }
1040     else
1041     {
1042     if (priv->map_auto_download)
1043     osm_gps_map_download_tile(map, zoom, x, y, TRUE);
1044    
1045     /* try to render the tile by scaling cached tiles from other zoom
1046     * levels */
1047     pixbuf = osm_gps_map_render_missing_tile (map, zoom, x, y);
1048     if (pixbuf)
1049     {
1050     gdk_draw_pixbuf (priv->pixmap,
1051     priv->gc_map,
1052     pixbuf,
1053     0,0,
1054     offset_x,offset_y,
1055     TILESIZE,TILESIZE,
1056     GDK_RGB_DITHER_NONE, 0, 0);
1057     g_object_unref (pixbuf);
1058     }
1059     else
1060     {
1061     //prevent some artifacts when drawing not yet loaded areas.
1062     gdk_draw_rectangle (priv->pixmap,
1063     GTK_WIDGET(map)->style->white_gc,
1064     TRUE, offset_x, offset_y, TILESIZE, TILESIZE);
1065     }
1066     }
1067 harbaum 55 g_free(filename);
1068 harbaum 32 }
1069    
1070     static void
1071     osm_gps_map_fill_tiles_pixel (OsmGpsMap *map)
1072     {
1073     OsmGpsMapPrivate *priv = map->priv;
1074     int i,j, width, height, tile_x0, tile_y0, tiles_nx, tiles_ny;
1075     int offset_xn = 0;
1076     int offset_yn = 0;
1077     int offset_x;
1078     int offset_y;
1079    
1080     g_debug("Fill tiles: %d,%d z:%d", priv->map_x, priv->map_y, priv->map_zoom);
1081    
1082     offset_x = - priv->map_x % TILESIZE;
1083     offset_y = - priv->map_y % TILESIZE;
1084     if (offset_x > 0) offset_x -= TILESIZE;
1085     if (offset_y > 0) offset_y -= TILESIZE;
1086    
1087     offset_xn = offset_x + EXTRA_BORDER;
1088     offset_yn = offset_y + EXTRA_BORDER;
1089    
1090     width = GTK_WIDGET(map)->allocation.width;
1091     height = GTK_WIDGET(map)->allocation.height;
1092    
1093     tiles_nx = (width - offset_x) / TILESIZE + 1;
1094     tiles_ny = (height - offset_y) / TILESIZE + 1;
1095    
1096     tile_x0 = floor((float)priv->map_x / (float)TILESIZE);
1097     tile_y0 = floor((float)priv->map_y / (float)TILESIZE);
1098    
1099     //TODO: implement wrap around
1100     for (i=tile_x0; i<(tile_x0+tiles_nx);i++)
1101     {
1102     for (j=tile_y0; j<(tile_y0+tiles_ny); j++)
1103     {
1104     if( j<0 || i<0 || i>=exp(priv->map_zoom * M_LN2) || j>=exp(priv->map_zoom * M_LN2))
1105     {
1106     gdk_draw_rectangle (priv->pixmap,
1107     GTK_WIDGET(map)->style->white_gc,
1108     TRUE,
1109     offset_xn, offset_yn,
1110     TILESIZE,TILESIZE);
1111     }
1112     else
1113     {
1114     osm_gps_map_load_tile(map,
1115     priv->map_zoom,
1116     i,j,
1117     offset_xn,offset_yn);
1118     }
1119     offset_yn += TILESIZE;
1120     }
1121     offset_xn += TILESIZE;
1122     offset_yn = offset_y + EXTRA_BORDER;
1123     }
1124     }
1125    
1126     static void
1127     osm_gps_map_print_track (OsmGpsMap *map, GSList *trackpoint_list)
1128     {
1129     OsmGpsMapPrivate *priv = map->priv;
1130    
1131     GSList *list;
1132     int x,y;
1133     int min_x = 0,min_y = 0,max_x = 0,max_y = 0;
1134     int lw = priv->ui_gps_track_width;
1135     int map_x0, map_y0;
1136     #ifdef USE_CAIRO
1137     cairo_t *cr;
1138     #else
1139     int last_x = 0, last_y = 0;
1140     GdkColor color;
1141     GdkGC *gc;
1142     #endif
1143    
1144     #ifdef USE_CAIRO
1145     cr = gdk_cairo_create(priv->pixmap);
1146     cairo_set_line_width (cr, lw);
1147     cairo_set_source_rgba (cr, 60000.0/65535.0, 0.0, 0.0, 0.6);
1148     cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
1149     cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
1150     #else
1151     gc = gdk_gc_new(priv->pixmap);
1152     color.green = 0;
1153     color.blue = 0;
1154     color.red = 60000;
1155     gdk_gc_set_rgb_fg_color(gc, &color);
1156     gdk_gc_set_line_attributes(gc, lw, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
1157     #endif
1158    
1159     map_x0 = priv->map_x - EXTRA_BORDER;
1160     map_y0 = priv->map_y - EXTRA_BORDER;
1161     for(list = trackpoint_list; list != NULL; list = list->next)
1162     {
1163     coord_t *tp = list->data;
1164    
1165     x = lon2pixel(priv->map_zoom, tp->rlon) - map_x0;
1166     y = lat2pixel(priv->map_zoom, tp->rlat) - map_y0;
1167    
1168     // first time through loop
1169     if (list == trackpoint_list) {
1170     #ifdef USE_CAIRO
1171     cairo_move_to(cr, x, y);
1172     #else
1173     last_x = x;
1174     last_y = y;
1175     #endif
1176     }
1177    
1178     #ifdef USE_CAIRO
1179     cairo_line_to(cr, x, y);
1180     #else
1181     gdk_draw_line (priv->pixmap, gc, x, y, last_x, last_y);
1182     last_x = x;
1183     last_y = y;
1184     #endif
1185    
1186     max_x = MAX(x,max_x);
1187     min_x = MIN(x,min_x);
1188     max_y = MAX(y,max_y);
1189     min_y = MIN(y,min_y);
1190     }
1191    
1192     gtk_widget_queue_draw_area (
1193     GTK_WIDGET(map),
1194     min_x - lw,
1195     min_y - lw,
1196     max_x + (lw * 2),
1197     max_y + (lw * 2));
1198    
1199     #ifdef USE_CAIRO
1200     cairo_stroke(cr);
1201     cairo_destroy(cr);
1202     #else
1203     g_object_unref(gc);
1204     #endif
1205     }
1206    
1207     /* Prints the gps trip history, and any other tracks */
1208     static void
1209     osm_gps_map_print_tracks (OsmGpsMap *map)
1210     {
1211     OsmGpsMapPrivate *priv = map->priv;
1212    
1213     if (priv->show_trip_history)
1214     osm_gps_map_print_track (map, priv->trip_history);
1215 harbaum 55
1216 harbaum 32 if (priv->tracks)
1217     {
1218     GSList* tmp = priv->tracks;
1219     while (tmp != NULL)
1220     {
1221     osm_gps_map_print_track (map, tmp->data);
1222     tmp = g_slist_next(tmp);
1223     }
1224     }
1225     }
1226    
1227 harbaum 55 static gboolean
1228     osm_gps_map_purge_cache_check(gpointer key, gpointer value, gpointer user)
1229 harbaum 32 {
1230 harbaum 55 return (((OsmCachedTile*)value)->redraw_cycle != ((OsmGpsMapPrivate*)user)->redraw_cycle);
1231 harbaum 32 }
1232    
1233     static void
1234     osm_gps_map_purge_cache (OsmGpsMap *map)
1235     {
1236 harbaum 55 OsmGpsMapPrivate *priv = map->priv;
1237 harbaum 32
1238 harbaum 55 if (g_hash_table_size (priv->tile_cache) < priv->max_tile_cache_size)
1239     return;
1240 harbaum 32
1241 harbaum 55 /* run through the cache, and remove the tiles which have not been used
1242     * during the last redraw operation */
1243     g_hash_table_foreach_remove(priv->tile_cache, osm_gps_map_purge_cache_check, priv);
1244 harbaum 32 }
1245    
1246 harbaum 63 static gboolean
1247 harbaum 32 osm_gps_map_map_redraw (OsmGpsMap *map)
1248     {
1249     OsmGpsMapPrivate *priv = map->priv;
1250    
1251 harbaum 147 /* on diablo the map comes up at 1x1 pixel size and */
1252 harbaum 150 /* isn't really usable. we'll just ignore this ... */
1253     if((GTK_WIDGET(map)->allocation.width < 2) ||
1254     (GTK_WIDGET(map)->allocation.height < 2)) {
1255     printf("not a useful sized map yet ...\n");
1256 harbaum 147 return FALSE;
1257 harbaum 150 }
1258 harbaum 147
1259 harbaum 32 priv->idle_map_redraw = 0;
1260    
1261 harbaum 87 /* don't redraw the entire map while the OSD is doing */
1262     /* some animation or the like. This is to keep the animation */
1263     /* fluid */
1264     if (priv->osd->busy(priv->osd))
1265     return FALSE;
1266    
1267 harbaum 111 #ifdef DRAG_DEBUG
1268     printf("trying redraw\n");
1269     #endif
1270    
1271 harbaum 32 /* the motion_notify handler uses priv->pixmap to redraw the area; if we
1272     * change it while we are dragging, we will end up showing it in the wrong
1273     * place. This could be fixed by carefully recompute the coordinates, but
1274     * for now it's easier just to disable redrawing the map while dragging */
1275     if (priv->dragging)
1276     return FALSE;
1277    
1278 harbaum 111 /* undo all offsets that may have happened when dragging */
1279     priv->drag_mouse_dx = 0;
1280     priv->drag_mouse_dy = 0;
1281    
1282 harbaum 32 priv->redraw_cycle++;
1283    
1284     /* draw white background to initialise pixmap */
1285     gdk_draw_rectangle (
1286     priv->pixmap,
1287     GTK_WIDGET(map)->style->white_gc,
1288     TRUE,
1289     0, 0,
1290     GTK_WIDGET(map)->allocation.width + EXTRA_BORDER * 2,
1291     GTK_WIDGET(map)->allocation.height + EXTRA_BORDER * 2);
1292    
1293     osm_gps_map_fill_tiles_pixel(map);
1294    
1295     osm_gps_map_print_tracks(map);
1296     osm_gps_map_draw_gps_point(map);
1297     osm_gps_map_print_images(map);
1298 harbaum 73
1299 harbaum 111 #ifdef ENABLE_OSD
1300     /* OSD may contain a coordinate/scale, so we may have to re-render it */
1301     if(priv->osd && OSM_IS_GPS_MAP (priv->osd->widget))
1302     priv->osd->render (priv->osd);
1303     #endif
1304    
1305 harbaum 32 osm_gps_map_purge_cache(map);
1306     gtk_widget_queue_draw (GTK_WIDGET (map));
1307    
1308     return FALSE;
1309     }
1310    
1311     static void
1312     osm_gps_map_map_redraw_idle (OsmGpsMap *map)
1313     {
1314     OsmGpsMapPrivate *priv = map->priv;
1315    
1316     if (priv->idle_map_redraw == 0)
1317     priv->idle_map_redraw = g_idle_add ((GSourceFunc)osm_gps_map_map_redraw, map);
1318     }
1319    
1320 harbaum 77 #ifdef OSM_GPS_MAP_KEYS
1321     static gboolean
1322     on_window_key_press(GtkWidget *widget,
1323     GdkEventKey *event, OsmGpsMapPrivate *priv) {
1324     gboolean handled = FALSE;
1325     int step = GTK_WIDGET(widget)->allocation.width/OSM_GPS_MAP_SCROLL_STEP;
1326    
1327     // the map handles some keys on its own ...
1328     switch(event->keyval) {
1329     #ifdef OSM_GPS_MAP_KEY_FULLSCREEN
1330     case OSM_GPS_MAP_KEY_FULLSCREEN: {
1331     GtkWidget *toplevel = gtk_widget_get_toplevel(GTK_WIDGET(widget));
1332     if(!priv->fullscreen)
1333     gtk_window_fullscreen(GTK_WINDOW(toplevel));
1334     else
1335     gtk_window_unfullscreen(GTK_WINDOW(toplevel));
1336    
1337     priv->fullscreen = !priv->fullscreen;
1338     handled = TRUE;
1339     } break;
1340     #endif
1341    
1342     #ifdef OSM_GPS_MAP_KEY_ZOOMIN
1343     case OSM_GPS_MAP_KEY_ZOOMIN:
1344     osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom+1);
1345     handled = TRUE;
1346     break;
1347     #endif
1348    
1349     #ifdef OSM_GPS_MAP_KEY_ZOOMOUT
1350     case OSM_GPS_MAP_KEY_ZOOMOUT:
1351     osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom-1);
1352     handled = TRUE;
1353     break;
1354     #endif
1355    
1356     #ifdef OSM_GPS_MAP_KEY_UP
1357     case OSM_GPS_MAP_KEY_UP:
1358     priv->map_y -= step;
1359     priv->center_coord_set = FALSE;
1360     osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1361     handled = TRUE;
1362     break;
1363     #endif
1364    
1365     #ifdef OSM_GPS_MAP_KEY_DOWN
1366     case OSM_GPS_MAP_KEY_DOWN:
1367     priv->map_y += step;
1368     priv->center_coord_set = FALSE;
1369     osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1370     handled = TRUE;
1371     break;
1372     #endif
1373    
1374     #ifdef OSM_GPS_MAP_KEY_LEFT
1375     case OSM_GPS_MAP_KEY_LEFT:
1376     priv->map_x -= step;
1377     priv->center_coord_set = FALSE;
1378     osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1379     handled = TRUE;
1380     break;
1381     #endif
1382    
1383     #ifdef OSM_GPS_MAP_KEY_RIGHT
1384     case OSM_GPS_MAP_KEY_RIGHT:
1385     priv->map_x += step;
1386     priv->center_coord_set = FALSE;
1387     osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1388     handled = TRUE;
1389     break;
1390     #endif
1391    
1392     default:
1393     break;
1394     }
1395    
1396     return handled;
1397     }
1398     #endif
1399    
1400 harbaum 32 static void
1401     osm_gps_map_init (OsmGpsMap *object)
1402     {
1403     OsmGpsMapPrivate *priv;
1404    
1405     priv = G_TYPE_INSTANCE_GET_PRIVATE (object, OSM_TYPE_GPS_MAP, OsmGpsMapPrivate);
1406     object->priv = priv;
1407    
1408     priv->pixmap = NULL;
1409    
1410     priv->trip_history = NULL;
1411     priv->gps = g_new0(coord_t, 1);
1412     priv->gps_valid = FALSE;
1413 harbaum 97 priv->gps_heading = OSM_GPS_MAP_INVALID;
1414 harbaum 32
1415 harbaum 61 #ifdef ENABLE_OSD
1416 harbaum 73 priv->osd = NULL;
1417 harbaum 61 #endif
1418    
1419 harbaum 77 #ifdef OSM_GPS_MAP_BUTTON_FULLSCREEN
1420     priv->fullscreen = FALSE;
1421     #endif
1422    
1423 harbaum 32 priv->tracks = NULL;
1424     priv->images = NULL;
1425    
1426     priv->drag_counter = 0;
1427     priv->drag_mouse_dx = 0;
1428     priv->drag_mouse_dy = 0;
1429     priv->drag_start_mouse_x = 0;
1430     priv->drag_start_mouse_y = 0;
1431    
1432     priv->uri_format = 0;
1433     priv->the_google = FALSE;
1434    
1435 harbaum 55 priv->map_source = -1;
1436    
1437 harbaum 32 #ifndef LIBSOUP22
1438     //Change naumber of concurrent connections option?
1439 harbaum 89 priv->soup_session =
1440     soup_session_async_new_with_options(SOUP_SESSION_USER_AGENT,
1441     USER_AGENT, NULL);
1442 harbaum 32 #else
1443 harbaum 89 /* libsoup-2.2 has no special way to set the user agent, so we */
1444     /* set it seperately as an extra header field for each reuest */
1445 harbaum 32 priv->soup_session = soup_session_async_new();
1446     #endif
1447    
1448     //Hash table which maps tile d/l URIs to SoupMessage requests
1449     priv->tile_queue = g_hash_table_new (g_str_hash, g_str_equal);
1450    
1451     //Some mapping providers (Google) have varying degrees of tiles at multiple
1452     //zoom levels
1453     priv->missing_tiles = g_hash_table_new (g_str_hash, g_str_equal);
1454    
1455     /* memory cache for most recently used tiles */
1456     priv->tile_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1457     g_free, (GDestroyNotify)cached_tile_free);
1458     priv->max_tile_cache_size = 20;
1459    
1460     gtk_widget_add_events (GTK_WIDGET (object),
1461     GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1462     GDK_POINTER_MOTION_MASK |
1463     GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
1464     GTK_WIDGET_SET_FLAGS (object, GTK_CAN_FOCUS);
1465    
1466     g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MASK, my_log_handler, NULL);
1467    
1468 harbaum 77 #ifdef OSM_GPS_MAP_KEYS
1469     g_signal_connect(G_OBJECT(object), "key_press_event",
1470     G_CALLBACK(on_window_key_press), priv);
1471     #endif
1472 harbaum 32 }
1473    
1474 harbaum 91 static void
1475     osm_gps_map_setup(OsmGpsMapPrivate *priv) {
1476 harbaum 55 const char *uri;
1477 harbaum 32
1478 harbaum 55 //user can specify a map source ID, or a repo URI as the map source
1479     uri = osm_gps_map_source_get_repo_uri(OSM_GPS_MAP_SOURCE_NULL);
1480     if ( (priv->map_source == 0) || (strcmp(priv->repo_uri, uri) == 0) ) {
1481     g_debug("Using null source");
1482     priv->map_source = OSM_GPS_MAP_SOURCE_NULL;
1483    
1484     priv->null_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 256, 256);
1485     gdk_pixbuf_fill(priv->null_tile, 0xcccccc00);
1486     }
1487     else if (priv->map_source >= 0) {
1488     //check if the source given is valid
1489     uri = osm_gps_map_source_get_repo_uri(priv->map_source);
1490     if (uri) {
1491     g_debug("Setting map source from ID");
1492     g_free(priv->repo_uri);
1493    
1494     priv->repo_uri = g_strdup(uri);
1495     priv->image_format = g_strdup(
1496     osm_gps_map_source_get_image_format(priv->map_source));
1497     priv->max_zoom = osm_gps_map_source_get_max_zoom(priv->map_source);
1498     priv->min_zoom = osm_gps_map_source_get_min_zoom(priv->map_source);
1499     }
1500     }
1501    
1502 harbaum 82 const char *fname = osm_gps_map_source_get_friendly_name(priv->map_source);
1503     if(!fname) fname = "_unknown_";
1504 harbaum 32
1505 harbaum 89 if (priv->tile_dir) {
1506     //the new cachedir is the given cache dir + the friendly name of the repo_uri
1507     priv->cache_dir = g_strdup_printf("%s%c%s", priv->tile_dir, G_DIR_SEPARATOR, fname);
1508     g_debug("Adjusting cache dir %s -> %s", priv->tile_dir, priv->cache_dir);
1509 harbaum 32 }
1510 harbaum 91 }
1511 harbaum 32
1512 harbaum 91 static GObject *
1513     osm_gps_map_constructor (GType gtype, guint n_properties, GObjectConstructParam *properties)
1514     {
1515     //Always chain up to the parent constructor
1516     GObject *object =
1517     G_OBJECT_CLASS(osm_gps_map_parent_class)->constructor(gtype, n_properties, properties);
1518 harbaum 55
1519 harbaum 91 osm_gps_map_setup(OSM_GPS_MAP_PRIVATE(object));
1520    
1521     inspect_map_uri(OSM_GPS_MAP(object));
1522    
1523 harbaum 32 return object;
1524     }
1525    
1526     static void
1527     osm_gps_map_dispose (GObject *object)
1528     {
1529     OsmGpsMap *map = OSM_GPS_MAP(object);
1530     OsmGpsMapPrivate *priv = map->priv;
1531    
1532     if (priv->is_disposed)
1533     return;
1534    
1535     priv->is_disposed = TRUE;
1536    
1537     soup_session_abort(priv->soup_session);
1538     g_object_unref(priv->soup_session);
1539    
1540     g_hash_table_destroy(priv->tile_queue);
1541     g_hash_table_destroy(priv->missing_tiles);
1542     g_hash_table_destroy(priv->tile_cache);
1543    
1544     osm_gps_map_free_images(map);
1545    
1546     if(priv->pixmap)
1547     g_object_unref (priv->pixmap);
1548    
1549 harbaum 55 if (priv->null_tile)
1550     g_object_unref (priv->null_tile);
1551    
1552 harbaum 32 if(priv->gc_map)
1553     g_object_unref(priv->gc_map);
1554    
1555     if (priv->idle_map_redraw != 0)
1556     g_source_remove (priv->idle_map_redraw);
1557    
1558 harbaum 121 if (priv->drag_expose != 0)
1559     g_source_remove (priv->drag_expose);
1560    
1561 harbaum 57 g_free(priv->gps);
1562 harbaum 65
1563 harbaum 61 #ifdef ENABLE_OSD
1564 harbaum 73 if(priv->osd)
1565     priv->osd->free(priv->osd);
1566 harbaum 75
1567     #ifdef OSD_DOUBLE_BUFFER
1568     if(priv->dbuf_pixmap)
1569     g_object_unref (priv->dbuf_pixmap);
1570 harbaum 61 #endif
1571 harbaum 75 #endif
1572 harbaum 61
1573 harbaum 32 G_OBJECT_CLASS (osm_gps_map_parent_class)->dispose (object);
1574     }
1575    
1576     static void
1577     osm_gps_map_finalize (GObject *object)
1578     {
1579     OsmGpsMap *map = OSM_GPS_MAP(object);
1580     OsmGpsMapPrivate *priv = map->priv;
1581    
1582 harbaum 89 if(priv->tile_dir)
1583     g_free(priv->tile_dir);
1584    
1585     if(priv->cache_dir)
1586     g_free(priv->cache_dir);
1587    
1588 harbaum 32 g_free(priv->repo_uri);
1589 harbaum 55 g_free(priv->image_format);
1590 harbaum 32
1591     osm_gps_map_free_trip(map);
1592     osm_gps_map_free_tracks(map);
1593    
1594     G_OBJECT_CLASS (osm_gps_map_parent_class)->finalize (object);
1595     }
1596    
1597     static void
1598     osm_gps_map_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
1599     {
1600     g_return_if_fail (OSM_IS_GPS_MAP (object));
1601     OsmGpsMap *map = OSM_GPS_MAP(object);
1602     OsmGpsMapPrivate *priv = map->priv;
1603    
1604     switch (prop_id)
1605     {
1606     case PROP_AUTO_CENTER:
1607     priv->map_auto_center = g_value_get_boolean (value);
1608     break;
1609     case PROP_RECORD_TRIP_HISTORY:
1610     priv->record_trip_history = g_value_get_boolean (value);
1611     break;
1612     case PROP_SHOW_TRIP_HISTORY:
1613     priv->show_trip_history = g_value_get_boolean (value);
1614     break;
1615     case PROP_AUTO_DOWNLOAD:
1616     priv->map_auto_download = g_value_get_boolean (value);
1617     break;
1618     case PROP_REPO_URI:
1619     priv->repo_uri = g_value_dup_string (value);
1620     break;
1621     case PROP_PROXY_URI:
1622     if ( g_value_get_string(value) ) {
1623     priv->proxy_uri = g_value_dup_string (value);
1624     g_debug("Setting proxy server: %s", priv->proxy_uri);
1625 harbaum 55
1626 harbaum 32 #ifndef LIBSOUP22
1627     GValue val = {0};
1628 harbaum 55
1629 harbaum 32 SoupURI* uri = soup_uri_new(priv->proxy_uri);
1630     g_value_init(&val, SOUP_TYPE_URI);
1631     g_value_take_boxed(&val, uri);
1632    
1633     g_object_set_property(G_OBJECT(priv->soup_session),SOUP_SESSION_PROXY_URI,&val);
1634     #else
1635     SoupUri* uri = soup_uri_new(priv->proxy_uri);
1636     g_object_set(G_OBJECT(priv->soup_session), SOUP_SESSION_PROXY_URI, uri, NULL);
1637     #endif
1638     } else
1639     priv->proxy_uri = NULL;
1640    
1641     break;
1642     case PROP_TILE_CACHE_DIR:
1643 harbaum 55 if ( g_value_get_string(value) )
1644 harbaum 89 priv->tile_dir = g_value_dup_string (value);
1645 harbaum 32 break;
1646     case PROP_ZOOM:
1647     priv->map_zoom = g_value_get_int (value);
1648     break;
1649     case PROP_MAX_ZOOM:
1650     priv->max_zoom = g_value_get_int (value);
1651     break;
1652     case PROP_MIN_ZOOM:
1653     priv->min_zoom = g_value_get_int (value);
1654     break;
1655     case PROP_MAP_X:
1656     priv->map_x = g_value_get_int (value);
1657     priv->center_coord_set = FALSE;
1658     break;
1659     case PROP_MAP_Y:
1660     priv->map_y = g_value_get_int (value);
1661     priv->center_coord_set = FALSE;
1662     break;
1663     case PROP_GPS_TRACK_WIDTH:
1664     priv->ui_gps_track_width = g_value_get_int (value);
1665     break;
1666     case PROP_GPS_POINT_R1:
1667     priv->ui_gps_point_inner_radius = g_value_get_int (value);
1668     break;
1669     case PROP_GPS_POINT_R2:
1670     priv->ui_gps_point_outer_radius = g_value_get_int (value);
1671     break;
1672 harbaum 89 case PROP_MAP_SOURCE: {
1673     gint old = priv->map_source;
1674 harbaum 55 priv->map_source = g_value_get_int (value);
1675 harbaum 89 if(old >= OSM_GPS_MAP_SOURCE_NULL &&
1676     priv->map_source != old &&
1677     priv->map_source >= OSM_GPS_MAP_SOURCE_NULL &&
1678     priv->map_source <= OSM_GPS_MAP_SOURCE_LAST) {
1679    
1680     /* we now have to switch the entire map */
1681    
1682     /* flush the ram cache */
1683     g_hash_table_remove_all(priv->tile_cache);
1684    
1685 harbaum 91 osm_gps_map_setup(priv);
1686 harbaum 89
1687 harbaum 91 inspect_map_uri(map);
1688 harbaum 89
1689     /* adjust zoom if necessary */
1690     if(priv->map_zoom > priv->max_zoom)
1691     osm_gps_map_set_zoom(map, priv->max_zoom);
1692    
1693     if(priv->map_zoom < priv->min_zoom)
1694     osm_gps_map_set_zoom(map, priv->min_zoom);
1695    
1696     } } break;
1697 harbaum 55 case PROP_IMAGE_FORMAT:
1698     priv->image_format = g_value_dup_string (value);
1699     break;
1700 harbaum 32 default:
1701     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1702     break;
1703     }
1704     }
1705    
1706     static void
1707     osm_gps_map_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
1708     {
1709     g_return_if_fail (OSM_IS_GPS_MAP (object));
1710     float lat,lon;
1711     OsmGpsMap *map = OSM_GPS_MAP(object);
1712     OsmGpsMapPrivate *priv = map->priv;
1713    
1714     switch (prop_id)
1715     {
1716     case PROP_AUTO_CENTER:
1717     g_value_set_boolean(value, priv->map_auto_center);
1718     break;
1719     case PROP_RECORD_TRIP_HISTORY:
1720     g_value_set_boolean(value, priv->record_trip_history);
1721     break;
1722     case PROP_SHOW_TRIP_HISTORY:
1723     g_value_set_boolean(value, priv->show_trip_history);
1724     break;
1725     case PROP_AUTO_DOWNLOAD:
1726     g_value_set_boolean(value, priv->map_auto_download);
1727     break;
1728     case PROP_REPO_URI:
1729     g_value_set_string(value, priv->repo_uri);
1730     break;
1731     case PROP_PROXY_URI:
1732     g_value_set_string(value, priv->proxy_uri);
1733     break;
1734     case PROP_TILE_CACHE_DIR:
1735     g_value_set_string(value, priv->cache_dir);
1736     break;
1737     case PROP_ZOOM:
1738     g_value_set_int(value, priv->map_zoom);
1739     break;
1740     case PROP_MAX_ZOOM:
1741     g_value_set_int(value, priv->max_zoom);
1742     break;
1743     case PROP_MIN_ZOOM:
1744     g_value_set_int(value, priv->min_zoom);
1745     break;
1746     case PROP_LATITUDE:
1747     lat = pixel2lat(priv->map_zoom,
1748     priv->map_y + (GTK_WIDGET(map)->allocation.height / 2));
1749     g_value_set_float(value, rad2deg(lat));
1750     break;
1751     case PROP_LONGITUDE:
1752     lon = pixel2lon(priv->map_zoom,
1753     priv->map_x + (GTK_WIDGET(map)->allocation.width / 2));
1754     g_value_set_float(value, rad2deg(lon));
1755     break;
1756     case PROP_MAP_X:
1757     g_value_set_int(value, priv->map_x);
1758     break;
1759     case PROP_MAP_Y:
1760     g_value_set_int(value, priv->map_y);
1761     break;
1762     case PROP_TILES_QUEUED:
1763     g_value_set_int(value, g_hash_table_size(priv->tile_queue));
1764     break;
1765     case PROP_GPS_TRACK_WIDTH:
1766     g_value_set_int(value, priv->ui_gps_track_width);
1767     break;
1768     case PROP_GPS_POINT_R1:
1769     g_value_set_int(value, priv->ui_gps_point_inner_radius);
1770     break;
1771     case PROP_GPS_POINT_R2:
1772     g_value_set_int(value, priv->ui_gps_point_outer_radius);
1773     break;
1774 harbaum 55 case PROP_MAP_SOURCE:
1775     g_value_set_int(value, priv->map_source);
1776     break;
1777     case PROP_IMAGE_FORMAT:
1778     g_value_set_string(value, priv->image_format);
1779     break;
1780 harbaum 32 default:
1781     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1782     break;
1783     }
1784     }
1785    
1786     static gboolean
1787 harbaum 55 osm_gps_map_scroll_event (GtkWidget *widget, GdkEventScroll *event)
1788 harbaum 32 {
1789     OsmGpsMap *map = OSM_GPS_MAP(widget);
1790     OsmGpsMapPrivate *priv = map->priv;
1791    
1792     if (event->direction == GDK_SCROLL_UP)
1793     {
1794     osm_gps_map_set_zoom(map, priv->map_zoom+1);
1795     }
1796     else
1797     {
1798     osm_gps_map_set_zoom(map, priv->map_zoom-1);
1799     }
1800    
1801     return FALSE;
1802     }
1803    
1804     static gboolean
1805     osm_gps_map_button_press (GtkWidget *widget, GdkEventButton *event)
1806     {
1807     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);
1808    
1809 harbaum 63 #ifdef ENABLE_OSD
1810 harbaum 66 /* pressed inside OSD control? */
1811 harbaum 73 if(priv->osd) {
1812 harbaum 114 osd_button_t but =
1813     priv->osd->check(priv->osd, TRUE, event->x, event->y);
1814    
1815 harbaum 73 if(but != OSD_NONE)
1816     {
1817 harbaum 114 int step =
1818     GTK_WIDGET(widget)->allocation.width/OSM_GPS_MAP_SCROLL_STEP;
1819 harbaum 73 priv->drag_counter = -1;
1820    
1821     switch(but) {
1822     case OSD_UP:
1823 harbaum 77 priv->map_y -= step;
1824 harbaum 73 priv->center_coord_set = FALSE;
1825 harbaum 95 g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);
1826 harbaum 77 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1827 harbaum 73 break;
1828 harbaum 66
1829 harbaum 73 case OSD_DOWN:
1830 harbaum 77 priv->map_y += step;
1831 harbaum 73 priv->center_coord_set = FALSE;
1832 harbaum 95 g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);
1833 harbaum 77 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1834 harbaum 73 break;
1835 harbaum 66
1836 harbaum 73 case OSD_LEFT:
1837 harbaum 77 priv->map_x -= step;
1838 harbaum 73 priv->center_coord_set = FALSE;
1839 harbaum 95 g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);
1840 harbaum 77 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1841 harbaum 73 break;
1842    
1843     case OSD_RIGHT:
1844 harbaum 77 priv->map_x += step;
1845 harbaum 73 priv->center_coord_set = FALSE;
1846 harbaum 95 g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);
1847 harbaum 77 osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1848 harbaum 73 break;
1849    
1850     case OSD_IN:
1851     osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom+1);
1852     break;
1853    
1854     case OSD_OUT:
1855     osm_gps_map_set_zoom(OSM_GPS_MAP(widget), priv->map_zoom-1);
1856     break;
1857    
1858     default:
1859     /* all custom buttons are forwarded to the application */
1860     if(priv->osd->cb)
1861     priv->osd->cb(but, priv->osd->data);
1862     break;
1863     }
1864    
1865     return FALSE;
1866 harbaum 63 }
1867     }
1868     #endif
1869    
1870 harbaum 66 priv->drag_counter = 0;
1871     priv->drag_start_mouse_x = (int) event->x;
1872     priv->drag_start_mouse_y = (int) event->y;
1873     priv->drag_start_map_x = priv->map_x;
1874     priv->drag_start_map_y = priv->map_y;
1875    
1876     return FALSE;
1877     }
1878    
1879     static gboolean
1880     osm_gps_map_button_release (GtkWidget *widget, GdkEventButton *event)
1881     {
1882     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);
1883    
1884 harbaum 32 if (priv->dragging)
1885     {
1886     priv->dragging = FALSE;
1887    
1888     priv->map_x = priv->drag_start_map_x;
1889     priv->map_y = priv->drag_start_map_y;
1890    
1891     priv->map_x += (priv->drag_start_mouse_x - (int) event->x);
1892     priv->map_y += (priv->drag_start_mouse_y - (int) event->y);
1893    
1894     priv->center_coord_set = FALSE;
1895    
1896     osm_gps_map_map_redraw_idle(OSM_GPS_MAP(widget));
1897     }
1898 harbaum 114 #ifdef ENABLE_OSD
1899     /* pressed inside OSD control? */
1900     else if(priv->osd)
1901     priv->osd->check(priv->osd, FALSE, event->x, event->y);
1902     #endif
1903 harbaum 32
1904 harbaum 111 #ifdef DRAG_DEBUG
1905     printf("dragging done\n");
1906     #endif
1907    
1908 harbaum 58 priv->drag_counter = -1;
1909 harbaum 32
1910     return FALSE;
1911     }
1912    
1913     static gboolean
1914 harbaum 68 osm_gps_map_expose (GtkWidget *widget, GdkEventExpose *event);
1915    
1916     static gboolean
1917 harbaum 121 osm_gps_map_map_expose (GtkWidget *widget)
1918     {
1919     OsmGpsMapPrivate *priv = OSM_GPS_MAP(widget)->priv;
1920    
1921     priv->drag_expose = 0;
1922     osm_gps_map_expose (widget, NULL);
1923     return FALSE;
1924     }
1925    
1926     static gboolean
1927 harbaum 32 osm_gps_map_motion_notify (GtkWidget *widget, GdkEventMotion *event)
1928     {
1929     int x, y;
1930     GdkModifierType state;
1931     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);
1932    
1933     if (event->is_hint)
1934     gdk_window_get_pointer (event->window, &x, &y, &state);
1935     else
1936     {
1937     x = event->x;
1938     y = event->y;
1939     state = event->state;
1940     }
1941    
1942     // are we being dragged
1943     if (!(state & GDK_BUTTON1_MASK))
1944     return FALSE;
1945    
1946 harbaum 58 if (priv->drag_counter < 0)
1947     return FALSE;
1948    
1949 harbaum 121 /* not yet dragged far enough? */
1950     if(!priv->drag_counter &&
1951     ( (x - priv->drag_start_mouse_x) * (x - priv->drag_start_mouse_x) +
1952     (y - priv->drag_start_mouse_y) * (y - priv->drag_start_mouse_y) <
1953     10*10))
1954 harbaum 32 return FALSE;
1955    
1956 harbaum 121 priv->drag_counter++;
1957 harbaum 68
1958 harbaum 32 priv->dragging = TRUE;
1959    
1960     if (priv->map_auto_center)
1961     g_object_set(G_OBJECT(widget), "auto-center", FALSE, NULL);
1962    
1963     priv->drag_mouse_dx = x - priv->drag_start_mouse_x;
1964     priv->drag_mouse_dy = y - priv->drag_start_mouse_y;
1965    
1966 harbaum 121 /* instead of redrawing directly just add an idle function */
1967     if (!priv->drag_expose)
1968     priv->drag_expose =
1969     g_idle_add ((GSourceFunc)osm_gps_map_map_expose, widget);
1970 harbaum 68
1971     return FALSE;
1972     }
1973    
1974     static gboolean
1975     osm_gps_map_configure (GtkWidget *widget, GdkEventConfigure *event)
1976     {
1977     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);
1978    
1979     /* create pixmap */
1980     if (priv->pixmap)
1981     g_object_unref (priv->pixmap);
1982    
1983     priv->pixmap = gdk_pixmap_new (
1984 harbaum 75 widget->window,
1985     widget->allocation.width + EXTRA_BORDER * 2,
1986     widget->allocation.height + EXTRA_BORDER * 2,
1987     -1);
1988 harbaum 68
1989 harbaum 73 #ifdef ENABLE_OSD
1990 harbaum 75
1991     #ifdef OSD_DOUBLE_BUFFER
1992     if (priv->dbuf_pixmap)
1993     g_object_unref (priv->dbuf_pixmap);
1994    
1995     priv->dbuf_pixmap = gdk_pixmap_new (
1996     widget->window,
1997     widget->allocation.width,
1998     widget->allocation.height,
1999     -1);
2000     #endif
2001    
2002 harbaum 73 /* the osd needs some references to map internal objects */
2003     if(priv->osd)
2004     priv->osd->widget = widget;
2005     #endif
2006    
2007 harbaum 68 /* and gc, used for clipping (I think......) */
2008     if(priv->gc_map)
2009     g_object_unref(priv->gc_map);
2010    
2011     priv->gc_map = gdk_gc_new(priv->pixmap);
2012    
2013     osm_gps_map_map_redraw(OSM_GPS_MAP(widget));
2014    
2015     return FALSE;
2016     }
2017    
2018     static gboolean
2019     osm_gps_map_expose (GtkWidget *widget, GdkEventExpose *event)
2020     {
2021     OsmGpsMapPrivate *priv = OSM_GPS_MAP_PRIVATE(widget);
2022    
2023 harbaum 75 #if defined(ENABLE_OSD) && defined(OSD_DOUBLE_BUFFER)
2024     GdkDrawable *drawable = priv->dbuf_pixmap;
2025     #else
2026     GdkDrawable *drawable = widget->window;
2027     #endif
2028    
2029 harbaum 111 #ifdef DRAG_DEBUG
2030     printf("expose, map %d/%d\n", priv->map_x, priv->map_y);
2031     #endif
2032    
2033     if (!priv->drag_mouse_dx && !priv->drag_mouse_dy && event)
2034 harbaum 32 {
2035 harbaum 111 #ifdef DRAG_DEBUG
2036     printf(" dragging = %d, event = %p\n", priv->dragging, event);
2037     #endif
2038    
2039 harbaum 83 gdk_draw_drawable (drawable,
2040     widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
2041     priv->pixmap,
2042     event->area.x + EXTRA_BORDER, event->area.y + EXTRA_BORDER,
2043     event->area.x, event->area.y,
2044     event->area.width, event->area.height);
2045 harbaum 32 }
2046 harbaum 83 else
2047 harbaum 32 {
2048 harbaum 111 #ifdef DRAG_DEBUG
2049     printf(" drag_mouse %d/%d\n",
2050     priv->drag_mouse_dx - EXTRA_BORDER,
2051     priv->drag_mouse_dy - EXTRA_BORDER);
2052     #endif
2053    
2054 harbaum 83 gdk_draw_drawable (drawable,
2055     widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
2056     priv->pixmap,
2057     0,0,
2058     priv->drag_mouse_dx - EXTRA_BORDER,
2059     priv->drag_mouse_dy - EXTRA_BORDER,
2060     -1,-1);
2061    
2062     //Paint white outside of the map if dragging. Its less
2063     //ugly than painting the corrupted map
2064     if(priv->drag_mouse_dx>EXTRA_BORDER) {
2065     gdk_draw_rectangle (drawable,
2066     widget->style->white_gc,
2067     TRUE,
2068     0, 0,
2069     priv->drag_mouse_dx - EXTRA_BORDER,
2070     widget->allocation.height);
2071     }
2072     else if (-priv->drag_mouse_dx > EXTRA_BORDER)
2073     {
2074     gdk_draw_rectangle (drawable,
2075     widget->style->white_gc,
2076     TRUE,
2077     priv->drag_mouse_dx + widget->allocation.width + EXTRA_BORDER, 0,
2078     -priv->drag_mouse_dx - EXTRA_BORDER,
2079     widget->allocation.height);
2080     }
2081    
2082     if (priv->drag_mouse_dy>EXTRA_BORDER) {
2083     gdk_draw_rectangle (drawable,
2084     widget->style->white_gc,
2085     TRUE,
2086     0, 0,
2087     widget->allocation.width,
2088     priv->drag_mouse_dy - EXTRA_BORDER);
2089     }
2090     else if (-priv->drag_mouse_dy > EXTRA_BORDER)
2091     {
2092     gdk_draw_rectangle (drawable,
2093     widget->style->white_gc,
2094     TRUE,
2095     0, priv->drag_mouse_dy + widget->allocation.height + EXTRA_BORDER,
2096     widget->allocation.width,
2097     -priv->drag_mouse_dy - EXTRA_BORDER);
2098     }
2099 harbaum 32 }
2100 harbaum 75
2101     #ifdef ENABLE_OSD
2102     /* draw new OSD */
2103     if(priv->osd)
2104     priv->osd->draw (priv->osd, drawable);
2105    
2106     #ifdef OSD_DOUBLE_BUFFER
2107     gdk_draw_drawable (widget->window,
2108 harbaum 32 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
2109 harbaum 75 priv->dbuf_pixmap,
2110     0,0,0,0,-1,-1);
2111 harbaum 68 #endif
2112 harbaum 75
2113     #endif
2114    
2115 harbaum 32 return FALSE;
2116     }
2117    
2118     static void
2119     osm_gps_map_class_init (OsmGpsMapClass *klass)
2120     {
2121     GObjectClass* object_class = G_OBJECT_CLASS (klass);
2122     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2123    
2124     g_type_class_add_private (klass, sizeof (OsmGpsMapPrivate));
2125    
2126     object_class->dispose = osm_gps_map_dispose;
2127     object_class->finalize = osm_gps_map_finalize;
2128     object_class->constructor = osm_gps_map_constructor;
2129     object_class->set_property = osm_gps_map_set_property;
2130     object_class->get_property = osm_gps_map_get_property;
2131    
2132     widget_class->expose_event = osm_gps_map_expose;
2133     widget_class->configure_event = osm_gps_map_configure;
2134     widget_class->button_press_event = osm_gps_map_button_press;
2135     widget_class->button_release_event = osm_gps_map_button_release;
2136     widget_class->motion_notify_event = osm_gps_map_motion_notify;
2137 harbaum 55 widget_class->scroll_event = osm_gps_map_scroll_event;
2138 harbaum 32
2139     g_object_class_install_property (object_class,
2140     PROP_AUTO_CENTER,
2141     g_param_spec_boolean ("auto-center",
2142     "auto center",
2143     "map auto center",
2144     TRUE,
2145     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2146    
2147     g_object_class_install_property (object_class,
2148     PROP_RECORD_TRIP_HISTORY,
2149     g_param_spec_boolean ("record-trip-history",
2150     "record trip history",
2151     "should all gps points be recorded in a trip history",
2152     TRUE,
2153     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2154    
2155     g_object_class_install_property (object_class,
2156     PROP_SHOW_TRIP_HISTORY,
2157     g_param_spec_boolean ("show-trip-history",
2158     "show trip history",
2159     "should the recorded trip history be shown on the map",
2160     TRUE,
2161     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2162    
2163     g_object_class_install_property (object_class,
2164     PROP_AUTO_DOWNLOAD,
2165     g_param_spec_boolean ("auto-download",
2166     "auto download",
2167     "map auto download",
2168     TRUE,
2169     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2170    
2171     g_object_class_install_property (object_class,
2172     PROP_REPO_URI,
2173     g_param_spec_string ("repo-uri",
2174     "repo uri",
2175 harbaum 55 "map source tile repository uri",
2176     OSM_REPO_URI,
2177 harbaum 32 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2178    
2179 harbaum 55 g_object_class_install_property (object_class,
2180 harbaum 32 PROP_PROXY_URI,
2181     g_param_spec_string ("proxy-uri",
2182     "proxy uri",
2183     "http proxy uri on NULL",
2184     NULL,
2185     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2186    
2187     g_object_class_install_property (object_class,
2188     PROP_TILE_CACHE_DIR,
2189     g_param_spec_string ("tile-cache",
2190     "tile cache",
2191     "osm local tile cache dir",
2192 harbaum 55 NULL,
2193 harbaum 32 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2194    
2195     g_object_class_install_property (object_class,
2196     PROP_ZOOM,
2197     g_param_spec_int ("zoom",
2198     "zoom",
2199     "zoom level",
2200     MIN_ZOOM, /* minimum property value */
2201     MAX_ZOOM, /* maximum property value */
2202     3,
2203     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2204    
2205     g_object_class_install_property (object_class,
2206     PROP_MAX_ZOOM,
2207     g_param_spec_int ("max-zoom",
2208     "max zoom",
2209     "maximum zoom level",
2210     MIN_ZOOM, /* minimum property value */
2211     MAX_ZOOM, /* maximum property value */
2212 harbaum 55 OSM_MAX_ZOOM,
2213 harbaum 32 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2214    
2215     g_object_class_install_property (object_class,
2216     PROP_MIN_ZOOM,
2217     g_param_spec_int ("min-zoom",
2218     "min zoom",
2219     "minimum zoom level",
2220     MIN_ZOOM, /* minimum property value */
2221     MAX_ZOOM, /* maximum property value */
2222 harbaum 55 OSM_MIN_ZOOM,
2223 harbaum 32 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2224    
2225     g_object_class_install_property (object_class,
2226     PROP_LATITUDE,
2227     g_param_spec_float ("latitude",
2228     "latitude",
2229     "latitude in degrees",
2230     -90.0, /* minimum property value */
2231     90.0, /* maximum property value */
2232     0,
2233     G_PARAM_READABLE));
2234    
2235     g_object_class_install_property (object_class,
2236     PROP_LONGITUDE,
2237     g_param_spec_float ("longitude",
2238     "longitude",
2239     "longitude in degrees",
2240     -180.0, /* minimum property value */
2241     180.0, /* maximum property value */
2242     0,
2243     G_PARAM_READABLE));
2244    
2245     g_object_class_install_property (object_class,
2246     PROP_MAP_X,
2247     g_param_spec_int ("map-x",
2248     "map-x",
2249     "initial map x location",
2250     G_MININT, /* minimum property value */
2251     G_MAXINT, /* maximum property value */
2252     890,
2253     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2254    
2255     g_object_class_install_property (object_class,
2256     PROP_MAP_Y,
2257     g_param_spec_int ("map-y",
2258     "map-y",
2259     "initial map y location",
2260     G_MININT, /* minimum property value */
2261     G_MAXINT, /* maximum property value */
2262     515,
2263     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2264    
2265     g_object_class_install_property (object_class,
2266     PROP_TILES_QUEUED,
2267     g_param_spec_int ("tiles-queued",
2268     "tiles-queued",
2269     "number of tiles currently waiting to download",
2270     G_MININT, /* minimum property value */
2271     G_MAXINT, /* maximum property value */
2272     0,
2273     G_PARAM_READABLE));
2274    
2275     g_object_class_install_property (object_class,
2276     PROP_GPS_TRACK_WIDTH,
2277     g_param_spec_int ("gps-track-width",
2278     "gps-track-width",
2279     "width of the lines drawn for the gps track",
2280     1, /* minimum property value */
2281     G_MAXINT, /* maximum property value */
2282     4,
2283     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2284    
2285     g_object_class_install_property (object_class,
2286     PROP_GPS_POINT_R1,
2287     g_param_spec_int ("gps-track-point-radius",
2288     "gps-track-point-radius",
2289     "radius of the gps point inner circle",
2290     0, /* minimum property value */
2291     G_MAXINT, /* maximum property value */
2292 harbaum 66 10,
2293 harbaum 32 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2294    
2295     g_object_class_install_property (object_class,
2296     PROP_GPS_POINT_R2,
2297     g_param_spec_int ("gps-track-highlight-radius",
2298     "gps-track-highlight-radius",
2299     "radius of the gps point highlight circle",
2300     0, /* minimum property value */
2301     G_MAXINT, /* maximum property value */
2302     20,
2303     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2304    
2305 harbaum 55 g_object_class_install_property (object_class,
2306     PROP_MAP_SOURCE,
2307     g_param_spec_int ("map-source",
2308     "map source",
2309     "map source ID",
2310     -1, /* minimum property value */
2311     G_MAXINT, /* maximum property value */
2312     -1,
2313 harbaum 89 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
2314 harbaum 55
2315     g_object_class_install_property (object_class,
2316     PROP_IMAGE_FORMAT,
2317     g_param_spec_string ("image-format",
2318     "image format",
2319     "map source tile repository image format (jpg, png)",
2320     OSM_IMAGE_FORMAT,
2321     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
2322 harbaum 32 }
2323    
2324 harbaum 55 const char*
2325     osm_gps_map_source_get_friendly_name(OsmGpsMapSource_t source)
2326     {
2327     switch(source)
2328     {
2329     case OSM_GPS_MAP_SOURCE_NULL:
2330     return "None";
2331     case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:
2332 harbaum 91 return "OpenStreetMap I";
2333 harbaum 55 case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:
2334 harbaum 91 return "OpenStreetMap II";
2335 harbaum 84 case OSM_GPS_MAP_SOURCE_OPENCYCLEMAP:
2336     return "OpenCycleMap";
2337 harbaum 92 case OSM_GPS_MAP_SOURCE_OSMC_TRAILS:
2338     return "OSMC Trails";
2339 harbaum 55 case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:
2340     return "Maps-For-Free";
2341     case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:
2342     return "Google Maps";
2343     case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:
2344     return "Google Satellite";
2345     case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:
2346     return "Google Hybrid";
2347     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:
2348     return "Virtual Earth";
2349     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:
2350     return "Virtual Earth Satellite";
2351     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:
2352     return "Virtual Earth Hybrid";
2353     case OSM_GPS_MAP_SOURCE_YAHOO_STREET:
2354     return "Yahoo Maps";
2355     case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:
2356     return "Yahoo Satellite";
2357     case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:
2358     return "Yahoo Hybrid";
2359     default:
2360     return NULL;
2361     }
2362     return NULL;
2363     }
2364    
2365     //http://www.internettablettalk.com/forums/showthread.php?t=5209
2366     //https://garage.maemo.org/plugins/scmsvn/viewcvs.php/trunk/src/maps.c?root=maemo-mapper&view=markup
2367     //http://www.ponies.me.uk/maps/GoogleTileUtils.java
2368     //http://www.mgmaps.com/cache/MapTileCacher.perl
2369     const char*
2370     osm_gps_map_source_get_repo_uri(OsmGpsMapSource_t source)
2371     {
2372     switch(source)
2373     {
2374     case OSM_GPS_MAP_SOURCE_NULL:
2375     return "none://";
2376     case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:
2377     return OSM_REPO_URI;
2378     case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:
2379     return "http://tah.openstreetmap.org/Tiles/tile/#Z/#X/#Y.png";
2380 harbaum 84 case OSM_GPS_MAP_SOURCE_OPENCYCLEMAP:
2381     return "http://c.andy.sandbox.cloudmade.com/tiles/cycle/#Z/#X/#Y.png";
2382 harbaum 92 case OSM_GPS_MAP_SOURCE_OSMC_TRAILS:
2383     return "http://topo.geofabrik.de/trails/#Z/#X/#Y.png";
2384 harbaum 55 case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:
2385     return "http://maps-for-free.com/layer/relief/z#Z/row#Y/#Z_#X-#Y.jpg";
2386     case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:
2387     return "http://mt#R.google.com/vt/v=w2.97&x=#X&y=#Y&z=#Z";
2388     case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:
2389     return "http://khm#R.google.com/kh?n=404&v=3&t=#Q";
2390     case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:
2391     return NULL; /* No longer working "http://mt#R.google.com/mt?n=404&v=w2t.99&x=#X&y=#Y&zoom=#S" */
2392     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:
2393     return "http://a#R.ortho.tiles.virtualearth.net/tiles/r#W.jpeg?g=50";
2394     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:
2395     return "http://a#R.ortho.tiles.virtualearth.net/tiles/a#W.jpeg?g=50";
2396     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:
2397     return "http://a#R.ortho.tiles.virtualearth.net/tiles/h#W.jpeg?g=50";
2398     case OSM_GPS_MAP_SOURCE_YAHOO_STREET:
2399     case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:
2400     case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:
2401     /* TODO: Implement signed Y, aka U
2402     * http://us.maps3.yimg.com/aerial.maps.yimg.com/ximg?v=1.7&t=a&s=256&x=%d&y=%-d&z=%d
2403     * x = tilex,
2404     * y = (1 << (MAX_ZOOM - zoom)) - tiley - 1,
2405     * z = zoom - (MAX_ZOOM - 17));
2406     */
2407     return NULL;
2408     default:
2409     return NULL;
2410     }
2411     return NULL;
2412     }
2413    
2414     const char *
2415     osm_gps_map_source_get_image_format(OsmGpsMapSource_t source)
2416     {
2417     switch(source) {
2418     case OSM_GPS_MAP_SOURCE_NULL:
2419     case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:
2420     case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:
2421 harbaum 84 case OSM_GPS_MAP_SOURCE_OPENCYCLEMAP:
2422 harbaum 92 case OSM_GPS_MAP_SOURCE_OSMC_TRAILS:
2423 harbaum 55 return "png";
2424 harbaum 89 case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:
2425 harbaum 55 case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:
2426 harbaum 89 case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:
2427 harbaum 55 case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:
2428     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:
2429     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:
2430     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:
2431     case OSM_GPS_MAP_SOURCE_YAHOO_STREET:
2432     case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:
2433     case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:
2434     return "jpg";
2435     default:
2436     return "bin";
2437     }
2438     return "bin";
2439     }
2440    
2441    
2442     int
2443     osm_gps_map_source_get_min_zoom(OsmGpsMapSource_t source)
2444     {
2445     return 1;
2446     }
2447    
2448     int
2449     osm_gps_map_source_get_max_zoom(OsmGpsMapSource_t source)
2450     {
2451     switch(source) {
2452     case OSM_GPS_MAP_SOURCE_NULL:
2453     return 18;
2454     case OSM_GPS_MAP_SOURCE_OPENSTREETMAP:
2455 harbaum 105 case OSM_GPS_MAP_SOURCE_OPENCYCLEMAP:
2456 harbaum 55 return OSM_MAX_ZOOM;
2457     case OSM_GPS_MAP_SOURCE_OPENSTREETMAP_RENDERER:
2458     case OSM_GPS_MAP_SOURCE_GOOGLE_STREET:
2459     case OSM_GPS_MAP_SOURCE_GOOGLE_HYBRID:
2460     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_STREET:
2461     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_SATELLITE:
2462     case OSM_GPS_MAP_SOURCE_VIRTUAL_EARTH_HYBRID:
2463     case OSM_GPS_MAP_SOURCE_YAHOO_STREET:
2464     case OSM_GPS_MAP_SOURCE_YAHOO_SATELLITE:
2465     case OSM_GPS_MAP_SOURCE_YAHOO_HYBRID:
2466     return 17;
2467 harbaum 92 case OSM_GPS_MAP_SOURCE_OSMC_TRAILS:
2468     return 15;
2469 harbaum 55 case OSM_GPS_MAP_SOURCE_MAPS_FOR_FREE:
2470     return 11;
2471     case OSM_GPS_MAP_SOURCE_GOOGLE_SATELLITE:
2472     return 18;
2473     default:
2474     return 17;
2475     }
2476     return 17;
2477     }
2478    
2479 harbaum 32 void
2480     osm_gps_map_download_maps (OsmGpsMap *map, coord_t *pt1, coord_t *pt2, int zoom_start, int zoom_end)
2481     {
2482     int i,j,zoom,num_tiles;
2483     OsmGpsMapPrivate *priv = map->priv;
2484    
2485     if (pt1 && pt2)
2486     {
2487     gchar *filename;
2488     num_tiles = 0;
2489     zoom_end = CLAMP(zoom_end, priv->min_zoom, priv->max_zoom);
2490     g_debug("Download maps: z:%d->%d",zoom_start, zoom_end);
2491    
2492     for(zoom=zoom_start; zoom<=zoom_end; zoom++)
2493     {
2494     int x1,y1,x2,y2;
2495    
2496     x1 = (int)floor((float)lon2pixel(zoom, pt1->rlon) / (float)TILESIZE);
2497     y1 = (int)floor((float)lat2pixel(zoom, pt1->rlat) / (float)TILESIZE);
2498    
2499     x2 = (int)floor((float)lon2pixel(zoom, pt2->rlon) / (float)TILESIZE);
2500     y2 = (int)floor((float)lat2pixel(zoom, pt2->rlat) / (float)TILESIZE);
2501    
2502     // loop x1-x2
2503     for(i=x1; i<=x2; i++)
2504     {
2505     // loop y1 - y2
2506     for(j=y1; j<=y2; j++)
2507     {
2508     // x = i, y = j
2509 harbaum 55 filename = g_strdup_printf("%s%c%d%c%d%c%d.%s",
2510     priv->cache_dir, G_DIR_SEPARATOR,
2511     zoom, G_DIR_SEPARATOR,
2512     i, G_DIR_SEPARATOR,
2513     j,
2514     priv->image_format);
2515     if (!g_file_test(filename, G_FILE_TEST_EXISTS))
2516 harbaum 32 {
2517     osm_gps_map_download_tile(map, zoom, i, j, FALSE);
2518     num_tiles++;
2519     }
2520     g_free(filename);
2521     }
2522     }
2523     g_debug("DL @Z:%d = %d tiles",zoom,num_tiles);
2524     }
2525     }
2526     }
2527    
2528     void
2529     osm_gps_map_get_bbox (OsmGpsMap *map, coord_t *pt1, coord_t *pt2)
2530     {
2531     OsmGpsMapPrivate *priv = map->priv;
2532    
2533     if (pt1 && pt2) {
2534     pt1->rlat = pixel2lat(priv->map_zoom, priv->map_y);
2535     pt1->rlon = pixel2lon(priv->map_zoom, priv->map_x);
2536     pt2->rlat = pixel2lat(priv->map_zoom, priv->map_y + GTK_WIDGET(map)->allocation.height);
2537     pt2->rlon = pixel2lon(priv->map_zoom, priv->map_x + GTK_WIDGET(map)->allocation.width);
2538    
2539     g_debug("BBOX: %f %f %f %f", pt1->rlat, pt1->rlon, pt2->rlat, pt2->rlon);
2540     }
2541     }
2542    
2543     void
2544     osm_gps_map_set_mapcenter (OsmGpsMap *map, float latitude, float longitude, int zoom)
2545     {
2546     osm_gps_map_set_center (map, latitude, longitude);
2547     osm_gps_map_set_zoom (map, zoom);
2548     }
2549    
2550     void
2551     osm_gps_map_set_center (OsmGpsMap *map, float latitude, float longitude)
2552     {
2553     int pixel_x, pixel_y;
2554     OsmGpsMapPrivate *priv;
2555    
2556     g_return_if_fail (OSM_IS_GPS_MAP (map));
2557     priv = map->priv;
2558    
2559     priv->center_rlat = deg2rad(latitude);
2560     priv->center_rlon = deg2rad(longitude);
2561     priv->center_coord_set = TRUE;
2562    
2563     // pixel_x,y, offsets
2564     pixel_x = lon2pixel(priv->map_zoom, priv->center_rlon);
2565     pixel_y = lat2pixel(priv->map_zoom, priv->center_rlat);
2566    
2567     priv->map_x = pixel_x - GTK_WIDGET(map)->allocation.width/2;
2568     priv->map_y = pixel_y - GTK_WIDGET(map)->allocation.height/2;
2569    
2570     osm_gps_map_map_redraw_idle(map);
2571     }
2572    
2573     int
2574     osm_gps_map_set_zoom (OsmGpsMap *map, int zoom)
2575     {
2576     int zoom_old;
2577 harbaum 55 double factor = 0.0;
2578 harbaum 32 int width_center, height_center;
2579     OsmGpsMapPrivate *priv;
2580    
2581     g_return_val_if_fail (OSM_IS_GPS_MAP (map), 0);
2582     priv = map->priv;
2583    
2584     if (zoom != priv->map_zoom)
2585     {
2586     width_center = GTK_WIDGET(map)->allocation.width / 2;
2587     height_center = GTK_WIDGET(map)->allocation.height / 2;
2588    
2589     zoom_old = priv->map_zoom;
2590     //constrain zoom min_zoom -> max_zoom
2591     priv->map_zoom = CLAMP(zoom, priv->min_zoom, priv->max_zoom);
2592    
2593     if (priv->center_coord_set)
2594     {
2595     priv->map_x = lon2pixel(priv->map_zoom, priv->center_rlon) - width_center;
2596     priv->map_y = lat2pixel(priv->map_zoom, priv->center_rlat) - height_center;
2597     }
2598     else
2599     {
2600     factor = exp(priv->map_zoom * M_LN2)/exp(zoom_old * M_LN2);
2601     priv->map_x = ((priv->map_x + width_center) * factor) - width_center;
2602     priv->map_y = ((priv->map_y + height_center) * factor) - height_center;
2603     }
2604    
2605     g_debug("Zoom changed from %d to %d factor:%f x:%d",
2606     zoom_old, priv->map_zoom, factor, priv->map_x);
2607    
2608 harbaum 98 #ifdef ENABLE_OSD
2609     /* OSD may contain a scale, so we may have to re-render it */
2610     if(priv->osd && OSM_IS_GPS_MAP (priv->osd->widget))
2611     priv->osd->render (priv->osd);
2612     #endif
2613    
2614 harbaum 32 osm_gps_map_map_redraw_idle(map);
2615     }
2616     return priv->map_zoom;
2617     }
2618    
2619     void
2620     osm_gps_map_add_track (OsmGpsMap *map, GSList *track)
2621     {
2622     OsmGpsMapPrivate *priv;
2623    
2624     g_return_if_fail (OSM_IS_GPS_MAP (map));
2625     priv = map->priv;
2626    
2627     if (track) {
2628     priv->tracks = g_slist_append(priv->tracks, track);
2629     osm_gps_map_map_redraw_idle(map);
2630     }
2631     }
2632    
2633     void
2634     osm_gps_map_clear_tracks (OsmGpsMap *map)
2635     {
2636     g_return_if_fail (OSM_IS_GPS_MAP (map));
2637    
2638     osm_gps_map_free_tracks(map);
2639     osm_gps_map_map_redraw_idle(map);
2640     }
2641    
2642     void
2643     osm_gps_map_add_image (OsmGpsMap *map, float latitude, float longitude, GdkPixbuf *image)
2644     {
2645     g_return_if_fail (OSM_IS_GPS_MAP (map));
2646    
2647     if (image) {
2648     OsmGpsMapPrivate *priv = map->priv;
2649     image_t *im;
2650    
2651     //cache w/h for speed, and add image to list
2652     im = g_new0(image_t,1);
2653     im->w = gdk_pixbuf_get_width(image);
2654 harbaum 55 im->h = gdk_pixbuf_get_height(image);
2655 harbaum 32 im->pt.rlat = deg2rad(latitude);
2656     im->pt.rlon = deg2rad(longitude);
2657    
2658     g_object_ref(image);
2659     im->image = image;
2660    
2661     priv->images = g_slist_append(priv->images, im);
2662    
2663     osm_gps_map_map_redraw_idle(map);
2664     }
2665     }
2666    
2667 harbaum 55 gboolean
2668     osm_gps_map_remove_image (OsmGpsMap *map, GdkPixbuf *image)
2669     {
2670     OsmGpsMapPrivate *priv = map->priv;
2671     if (priv->images) {
2672     GSList *list;
2673     for(list = priv->images; list != NULL; list = list->next)
2674     {
2675     image_t *im = list->data;
2676     if (im->image == image)
2677     {
2678     priv->images = g_slist_remove_link(priv->images, list);
2679     g_object_unref(im->image);
2680     g_free(im);
2681     osm_gps_map_map_redraw_idle(map);
2682     return TRUE;
2683     }
2684     }
2685     }
2686     return FALSE;
2687     }
2688    
2689 harbaum 32 void
2690     osm_gps_map_clear_images (OsmGpsMap *map)
2691     {
2692     g_return_if_fail (OSM_IS_GPS_MAP (map));
2693    
2694     osm_gps_map_free_images(map);
2695     osm_gps_map_map_redraw_idle(map);
2696     }
2697    
2698     void
2699     osm_gps_map_draw_gps (OsmGpsMap *map, float latitude, float longitude, float heading)
2700     {
2701     int pixel_x, pixel_y;
2702     OsmGpsMapPrivate *priv;
2703    
2704     g_return_if_fail (OSM_IS_GPS_MAP (map));
2705     priv = map->priv;
2706    
2707     priv->gps->rlat = deg2rad(latitude);
2708     priv->gps->rlon = deg2rad(longitude);
2709     priv->gps_valid = TRUE;
2710 harbaum 97 priv->gps_heading = deg2rad(heading);
2711 harbaum 32
2712     // pixel_x,y, offsets
2713     pixel_x = lon2pixel(priv->map_zoom, priv->gps->rlon);
2714     pixel_y = lat2pixel(priv->map_zoom, priv->gps->rlat);
2715    
2716     //If trip marker add to list of gps points.
2717     if (priv->record_trip_history) {
2718     coord_t *tp = g_new0(coord_t,1);
2719     tp->rlat = priv->gps->rlat;
2720     tp->rlon = priv->gps->rlon;
2721     priv->trip_history = g_slist_append(priv->trip_history, tp);
2722     }
2723    
2724     // dont draw anything if we are dragging
2725     if (priv->dragging) {
2726     g_debug("Dragging");
2727     return;
2728     }
2729    
2730     //Automatically center the map if the track approaches the edge
2731     if(priv->map_auto_center) {
2732     int x = pixel_x - priv->map_x;
2733     int y = pixel_y - priv->map_y;
2734     int width = GTK_WIDGET(map)->allocation.width;
2735     int height = GTK_WIDGET(map)->allocation.height;
2736     if( x < (width/2 - width/8) || x > (width/2 + width/8) ||
2737     y < (height/2 - height/8) || y > (height/2 + height/8)) {
2738    
2739     priv->map_x = pixel_x - GTK_WIDGET(map)->allocation.width/2;
2740     priv->map_y = pixel_y - GTK_WIDGET(map)->allocation.height/2;
2741     priv->center_coord_set = FALSE;
2742     }
2743     }
2744    
2745     // this redraws the map (including the gps track, and adjusts the
2746     // map center if it was changed
2747     osm_gps_map_map_redraw_idle(map);
2748     }
2749    
2750     void
2751     osm_gps_map_clear_gps (OsmGpsMap *map)
2752     {
2753     osm_gps_map_free_trip(map);
2754     osm_gps_map_map_redraw_idle(map);
2755     }
2756    
2757     coord_t
2758     osm_gps_map_get_co_ordinates (OsmGpsMap *map, int pixel_x, int pixel_y)
2759     {
2760     coord_t coord;
2761     OsmGpsMapPrivate *priv = map->priv;
2762    
2763     coord.rlat = pixel2lat(priv->map_zoom, priv->map_y + pixel_y);
2764     coord.rlon = pixel2lon(priv->map_zoom, priv->map_x + pixel_x);
2765     return coord;
2766     }
2767    
2768     GtkWidget *
2769     osm_gps_map_new (void)
2770     {
2771     return g_object_new (OSM_TYPE_GPS_MAP, NULL);
2772     }
2773    
2774     void
2775     osm_gps_map_screen_to_geographic (OsmGpsMap *map, gint pixel_x, gint pixel_y,
2776     gfloat *latitude, gfloat *longitude)
2777     {
2778     OsmGpsMapPrivate *priv;
2779    
2780     g_return_if_fail (OSM_IS_GPS_MAP (map));
2781     priv = map->priv;
2782    
2783     if (latitude)
2784     *latitude = rad2deg(pixel2lat(priv->map_zoom, priv->map_y + pixel_y));
2785     if (longitude)
2786     *longitude = rad2deg(pixel2lon(priv->map_zoom, priv->map_x + pixel_x));
2787     }
2788    
2789     void
2790     osm_gps_map_geographic_to_screen (OsmGpsMap *map,
2791     gfloat latitude, gfloat longitude,
2792     gint *pixel_x, gint *pixel_y)
2793     {
2794     OsmGpsMapPrivate *priv;
2795    
2796     g_return_if_fail (OSM_IS_GPS_MAP (map));
2797     priv = map->priv;
2798    
2799     if (pixel_x)
2800 harbaum 113 *pixel_x = lon2pixel(priv->map_zoom, deg2rad(longitude)) -
2801     priv->map_x + priv->drag_mouse_dx;
2802 harbaum 32 if (pixel_y)
2803 harbaum 113 *pixel_y = lat2pixel(priv->map_zoom, deg2rad(latitude)) -
2804     priv->map_y + priv->drag_mouse_dy;
2805 harbaum 32 }
2806    
2807     void
2808     osm_gps_map_scroll (OsmGpsMap *map, gint dx, gint dy)
2809     {
2810     OsmGpsMapPrivate *priv;
2811    
2812     g_return_if_fail (OSM_IS_GPS_MAP (map));
2813     priv = map->priv;
2814    
2815     priv->center_coord_set = FALSE;
2816     priv->map_x += dx;
2817     priv->map_y += dy;
2818    
2819 harbaum 111 #ifdef ENABLE_OSD
2820     /* OSD may contain a coordinate, so we may have to re-render it */
2821     if(priv->osd && OSM_IS_GPS_MAP (priv->osd->widget))
2822     priv->osd->render (priv->osd);
2823     #endif
2824    
2825 harbaum 32 osm_gps_map_map_redraw_idle (map);
2826     }
2827    
2828 harbaum 55 float
2829     osm_gps_map_get_scale(OsmGpsMap *map)
2830     {
2831     OsmGpsMapPrivate *priv;
2832    
2833 harbaum 97 g_return_val_if_fail (OSM_IS_GPS_MAP (map), OSM_GPS_MAP_INVALID);
2834 harbaum 55 priv = map->priv;
2835    
2836     return osm_gps_map_get_scale_at_point(priv->map_zoom, priv->center_rlat, priv->center_rlon);
2837     }
2838    
2839 harbaum 66 #ifdef ENABLE_OSD
2840 harbaum 73
2841     void
2842     osm_gps_map_redraw (OsmGpsMap *map)
2843     {
2844     osm_gps_map_map_redraw_idle(map);
2845     }
2846    
2847 harbaum 132 osm_gps_map_osd_t *
2848     osm_gps_map_osd_get(OsmGpsMap *map)
2849     {
2850 harbaum 73 g_return_val_if_fail (OSM_IS_GPS_MAP (map), NULL);
2851     return map->priv->osd;
2852     }
2853    
2854 harbaum 132 void
2855     osm_gps_map_register_osd(OsmGpsMap *map, osm_gps_map_osd_t *osd)
2856     {
2857 harbaum 66 OsmGpsMapPrivate *priv;
2858    
2859     g_return_if_fail (OSM_IS_GPS_MAP (map));
2860 harbaum 73
2861 harbaum 66 priv = map->priv;
2862 harbaum 73 g_return_if_fail (!priv->osd);
2863 harbaum 66
2864 harbaum 73 priv->osd = osd;
2865 harbaum 66 }
2866 harbaum 73
2867 harbaum 86 void
2868 harbaum 132 osm_gps_map_repaint (OsmGpsMap *map)
2869     {
2870 harbaum 86 osm_gps_map_expose (GTK_WIDGET(map), NULL);
2871     }
2872    
2873 harbaum 132 coord_t *
2874     osm_gps_map_get_gps (OsmGpsMap *map)
2875     {
2876     g_return_val_if_fail (OSM_IS_GPS_MAP (map), NULL);
2877    
2878     if(!map->priv->gps_valid)
2879     return NULL;
2880    
2881     return map->priv->gps;
2882     }
2883    
2884 harbaum 66 #endif