Add:Core:Adding cumulative displacement filter
[navit-package] / navit / navit.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <glib.h>
27 #include <math.h>
28 #include <time.h>
29 #include "config.h"
30 #include "debug.h"
31 #include "navit.h"
32 #include "callback.h"
33 #include "gui.h"
34 #include "item.h"
35 #include "projection.h"
36 #include "map.h"
37 #include "mapset.h"
38 #include "main.h"
39 #include "coord.h"
40 #include "point.h"
41 #include "transform.h"
42 #include "param.h"
43 #include "menu.h"
44 #include "graphics.h"
45 #include "cursor.h"
46 #include "popup.h"
47 #include "data_window.h"
48 #include "route.h"
49 #include "navigation.h"
50 #include "speech.h"
51 #include "track.h"
52 #include "vehicle.h"
53 #include "color.h"
54 #include "layout.h"
55 #include "log.h"
56 #include "attr.h"
57 #include "event.h"
58 #include "file.h"
59 #include "profile.h"
60 #include "command.h"
61 #include "navit_nls.h"
62
63 /**
64  * @defgroup navit the navit core instance. navit is the object containing nearly everything: A set of maps, one or more vehicle, a graphics object for rendering the map, a gui object for displaying the user interface, a route object, a navigation object and so on. Be warned that it is theoretically possible to have more than one navit object
65  * @{
66  */
67
68 //! The navit_vehicule
69 struct navit_vehicle {
70         int follow;
71         /*! Limit of the follow counter. See navit_add_vehicle */
72         int follow_curr;
73         /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
74         struct coord coord;
75         int dir;
76         int speed;
77         struct coord last; /*< Position of the last update of this vehicle */
78         struct color c;
79         struct color *c2;
80         struct cursor *cursor;
81         struct vehicle *vehicle;
82         struct attr callback;
83         int animate_cursor;
84 };
85
86 struct navit {
87         struct attr self;
88         GList *mapsets;
89         GList *layouts;
90         struct gui *gui;
91         struct layout *layout_current;
92         struct graphics *gra;
93         struct action *action;
94         struct transformation *trans;
95         struct compass *compass;
96         struct route *route;
97         struct navigation *navigation;
98         struct speech *speech;
99         struct tracking *tracking;
100         int ready;
101         struct window *win;
102         struct displaylist *displaylist;
103         int tracking_flag;
104         int orientation;
105         int recentdest_count;
106         int osd_configuration;
107         GList *vehicles;
108         GList *windows_items;
109         struct navit_vehicle *vehicle;
110         struct callback_list *attr_cbl;
111         struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb;
112         struct datawindow *roadbook_window;
113         struct map *bookmark;
114         struct map *former_destination;
115         GHashTable *bookmarks_hash;
116         struct point pressed, last, current;
117         int button_pressed,moved,popped,zoomed;
118         int center_timeout;
119         int autozoom_secs;
120         int autozoom_min;
121         int autozoom_active;
122         struct event_timeout *button_timeout, *motion_timeout;
123         struct callback *motion_timeout_callback;
124         int ignore_button;
125         int ignore_graphics_events;
126         struct log *textfile_debug_log;
127         struct pcoord destination;
128         int destination_valid;
129         int blocked;
130         int w,h;
131         int drag_bitmap;
132         int use_mousewheel;
133         struct callback *resize_callback,*button_callback,*motion_callback;
134 };
135
136 struct gui *main_loop_gui;
137
138 struct attr_iter {
139         union {
140                 GList *list;
141                 struct mapset_handle *mapset_handle;
142         } u;
143 };
144
145 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
146 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
147 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
148 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
149 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int *dir);
150 static void navit_cmd_zoom_to_route(struct navit *this);
151 static void navit_cmd_set_center_cursor(struct navit *this_);
152
153 void
154 navit_add_mapset(struct navit *this_, struct mapset *ms)
155 {
156         this_->mapsets = g_list_append(this_->mapsets, ms);
157 }
158
159 struct mapset *
160 navit_get_mapset(struct navit *this_)
161 {
162         if(this_->mapsets){
163                 return this_->mapsets->data;
164         } else {
165                 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
166         }
167         return NULL;
168 }
169
170 struct tracking *
171 navit_get_tracking(struct navit *this_)
172 {
173         return this_->tracking;
174 }
175
176 void
177 navit_draw_async(struct navit *this_, int async)
178 {
179         GList *l;
180         struct navit_vehicle *nv;
181
182         if (this_->blocked) {
183                 this_->blocked |= 2;
184                 return;
185         }
186         transform_setup_source_rect(this_->trans);
187         l=this_->vehicles;
188         while (l) {
189                 nv=l->data;
190                 navit_vehicle_draw(this_, nv, NULL);
191                 l=g_list_next(l);
192         }
193         graphics_draw(this_->gra, this_->displaylist, this_->mapsets, this_->trans, this_->layout_current, async, NULL);
194 }
195
196 void
197 navit_draw(struct navit *this_)
198 {
199         navit_draw_async(this_, 0);
200 }
201
202
203 void
204 navit_draw_displaylist(struct navit *this_)
205 {
206         if (this_->ready == 3)
207                 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, 1);
208 }
209
210 static void
211 navit_redraw_route(struct navit *this_, int updated)
212 {
213         dbg(1,"enter %d\n", updated);
214         if (this_->ready != 3)
215                 return;
216         if (updated <= 3)
217                 return;
218         if (this_->vehicle) {
219                 if (this_->vehicle->follow_curr == 1)
220                         return;
221                 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
222                         this_->vehicle->follow_curr=this_->vehicle->follow;
223         }
224         navit_draw(this_);
225 }
226
227 void
228 navit_handle_resize(struct navit *this_, int w, int h)
229 {
230         struct map_selection sel;
231         memset(&sel, 0, sizeof(sel));
232         this_->w=w;
233         this_->h=h;
234         sel.u.p_rect.rl.x=w;
235         sel.u.p_rect.rl.y=h;
236         transform_set_screen_selection(this_->trans, &sel);
237         this_->ready |= 2;
238         graphics_set_rect(this_->gra, &sel.u.p_rect);
239         if (this_->ready == 3)
240                 navit_draw(this_);
241 }
242
243 static void
244 navit_resize(void *data, int w, int h)
245 {
246         struct navit *this=data;
247         if (!this->ignore_graphics_events)
248                 navit_handle_resize(this, w, h);
249 }
250
251 int
252 navit_get_width(struct navit *this_)
253 {
254         return this_->w;
255 }
256
257
258 int
259 navit_get_height(struct navit *this_)
260 {
261         return this_->h;
262 }
263
264 static void
265 navit_popup(void *data)
266 {
267         struct navit *this_=data;
268         popup(this_, 1, &this_->pressed);
269         this_->button_timeout=NULL;
270         this_->popped=1;
271 }
272
273
274 int
275 navit_ignore_button(struct navit *this_)
276 {
277         if (this_->ignore_button)
278                 return 1;
279         this_->ignore_button=1;
280         return 0;
281 }
282
283 void
284 navit_ignore_graphics_events(struct navit *this_, int ignore)
285 {
286         this_->ignore_graphics_events=ignore;
287 }
288
289 static void
290 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
291 {
292         struct coord co,cn;
293         struct coord c,*cp;
294         int yaw;
295         double angleo,anglen;
296
297         transform_reverse(tr, old, &co);
298         if (rot) {
299                 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
300                 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
301                 yaw=transform_get_yaw(tr)+angleo-anglen;
302                 transform_set_yaw(tr, yaw % 360);
303         }
304         transform_reverse(tr, new, &cn);
305         cp=transform_get_center(tr);
306         c.x=cp->x+co.x-cn.x;
307         c.y=cp->y+co.y-cn.y;
308         dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
309         transform_set_center(tr, &c);
310 }
311
312 int
313 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
314 {
315         int border=16;
316
317         callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, (void *)pressed, (void *)button, p);
318         if (this_->ignore_button) {
319                 this_->ignore_button=0;
320                 return 0;
321         }
322         if (pressed) {
323                 this_->pressed=*p;
324                 this_->last=*p;
325                 this_->zoomed=0;
326                 if (button == 1) {
327                         this_->button_pressed=1;
328                         this_->moved=0;
329                         this_->popped=0;
330                         if (popup_callback)
331                                 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
332                 }
333                 if (button == 2)
334                         navit_set_center_screen(this_, p);
335                 if (button == 3)
336                         popup(this_, button, p);
337                 if (button == 4 && this_->use_mousewheel) {
338                         this_->zoomed = 1;
339                         navit_zoom_in(this_, 2, p);
340                 }
341                 if (button == 5 && this_->use_mousewheel) {
342                         this_->zoomed = 1;
343                         navit_zoom_out(this_, 2, p);
344                 }
345         } else {
346                 struct attr follow;
347                 follow.type=attr_follow;
348                 follow.u.num=this_->center_timeout;
349
350                 this_->button_pressed=0;
351                 if (this_->button_timeout) {
352                         event_remove_timeout(this_->button_timeout);
353                         this_->button_timeout=NULL;
354                         if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
355                                 if (!this_->zoomed) 
356                                         navit_set_attr(this_, &follow);
357                                 navit_set_center_screen(this_, p);
358                         }
359                 }
360                 if (this_->motion_timeout) {
361                         event_remove_timeout(this_->motion_timeout);
362                         this_->motion_timeout=NULL;
363                 }
364                 if (this_->moved) {
365                         struct point pr;
366                         pr.x=this_->w/2;
367                         pr.y=this_->h;
368 #if 0
369                         update_transformation(this_->trans, &this_->pressed, p, &pr);
370 #else
371                         update_transformation(this_->trans, &this_->pressed, p, NULL);
372 #endif
373                         graphics_draw_drag(this_->gra, NULL);
374                         graphics_overlay_disable(this_->gra, 0);
375                         if (!this_->zoomed) 
376                                 navit_set_attr(this_, &follow);
377                         navit_draw(this_);
378                 } else
379                         return 1;
380         }
381         return 0;
382 }
383
384 static void
385 navit_button(void *data, int pressed, int button, struct point *p)
386 {
387         struct navit *this=data;
388         if (!this->ignore_graphics_events) {
389                 if (! this->popup_callback)
390                         this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
391                 navit_handle_button(this, pressed, button, p, this->popup_callback);
392         }
393 }
394
395
396 static void
397 navit_motion_timeout(struct navit *this_)
398 {
399         int dx, dy;
400
401         if (this_->drag_bitmap) {
402                 struct point point;
403                 point.x=(this_->current.x-this_->pressed.x);
404                 point.y=(this_->current.y-this_->pressed.y);
405                 if (graphics_draw_drag(this_->gra, &point)) {
406                         graphics_overlay_disable(this_->gra, 1);
407                         graphics_draw_mode(this_->gra, draw_mode_end);
408                         this_->moved=1;
409                         this_->motion_timeout=NULL;
410                         return;
411                 }
412         } 
413         dx=(this_->current.x-this_->last.x);
414         dy=(this_->current.y-this_->last.y);
415         if (dx || dy) {
416                 struct transformation *tr;
417                 struct point pr;
418                 this_->last=this_->current;
419                 graphics_overlay_disable(this_->gra, 1);
420                 tr=transform_dup(this_->trans);
421                 pr.x=this_->w/2;
422                 pr.y=this_->h;
423 #if 0
424                 update_transformation(tr, &this_->pressed, &this_->current, &pr);
425 #else
426                 update_transformation(tr, &this_->pressed, &this_->current, NULL);
427 #endif
428 #if 0
429                 graphics_displaylist_move(this_->displaylist, dx, dy);
430 #endif
431                 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, 0);
432                 transform_destroy(tr);
433                 this_->moved=1;
434         }
435         this_->motion_timeout=NULL;
436         return;
437 }
438
439 void
440 navit_handle_motion(struct navit *this_, struct point *p)
441 {
442         int dx, dy;
443
444         if (this_->button_pressed && !this_->popped) {
445                 dx=(p->x-this_->pressed.x);
446                 dy=(p->y-this_->pressed.y);
447                 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
448                         if (this_->button_timeout) {
449                                 event_remove_timeout(this_->button_timeout);
450                                 this_->button_timeout=NULL;
451                         }
452                         this_->current=*p;
453                         if (! this_->motion_timeout_callback)
454                                 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
455                         if (! this_->motion_timeout)
456                                 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
457                 }
458         }
459 }
460
461 static void
462 navit_motion(void *data, struct point *p)
463 {
464         struct navit *this=data;
465         if (!this->ignore_graphics_events) 
466                 navit_handle_motion(this, p);
467 }
468
469 static void
470 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
471 {
472         struct coord c1, c2, *center;
473         if (p)
474                 transform_reverse(this_->trans, p, &c1);
475         transform_set_scale(this_->trans, scale);
476         if (p) {
477                 transform_reverse(this_->trans, p, &c2);
478                 center = transform_center(this_->trans);
479                 center->x += c1.x - c2.x;
480                 center->y += c1.y - c2.y;
481         }
482         if (draw)
483                 navit_draw(this_);
484 }
485
486 /**
487  * @brief Automatically adjusts zoom level
488  *
489  * This function automatically adjusts the current
490  * zoom level according to the current speed.
491  *
492  * @param this_ The navit struct
493  * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
494  * @param speed The vehicles speed in meters per second
495  * @param dir The direction into which the vehicle moves
496  */
497 static void
498 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
499 {
500         struct coord c;
501         struct point pc;
502         int distance,w,h;
503         double new_scale;
504         long scale;
505
506         if (! this_->autozoom_active) {
507                 return;
508         }
509
510         distance = speed * this_->autozoom_secs;
511
512         transform_get_size(this_->trans, &w, &h);
513         transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
514         scale = transform_get_scale(this_->trans);
515
516         /* We make shure that the point we want to see is within a certain range
517          * around the vehicle. The radius of this circle is the size of the
518          * screen. This doesn't necessarily mean the point is visible because of
519          * perspective etc. Quite rough, but should be enough. */
520         
521         if (w > h) {
522                 new_scale = (double)distance / h * 16; 
523         } else {
524                 new_scale = (double)distance / w * 16; 
525         }
526
527         if (abs(new_scale - scale) < 2) { 
528                 return; // Smoothing
529         }
530         
531         if (new_scale >= this_->autozoom_min) {
532                 navit_scale(this_, (long)new_scale, &pc, 0);
533         } else {
534                 if (scale != this_->autozoom_min) {
535                         navit_scale(this_, this_->autozoom_min, &pc, 0);
536                 }
537         }
538 }
539
540 /**
541  * Change the current zoom level, zooming closer to the ground
542  *
543  * @param navit The navit instance
544  * @param factor The zoom factor, usually 2
545  * @param p The invariant point (if set to NULL, default to center)
546  * @returns nothing
547  */
548 void
549 navit_zoom_in(struct navit *this_, int factor, struct point *p)
550 {
551         long scale=transform_get_scale(this_->trans)/factor;
552         if (scale < 1)
553                 scale=1;
554         navit_scale(this_, scale, p, 1);
555 }
556
557 /**
558  * Change the current zoom level
559  *
560  * @param navit The navit instance
561  * @param factor The zoom factor, usually 2
562  * @param p The invariant point (if set to NULL, default to center)
563  * @returns nothing
564  */
565 void
566 navit_zoom_out(struct navit *this_, int factor, struct point *p)
567 {
568         long scale=transform_get_scale(this_->trans)*factor;
569         navit_scale(this_, scale, p, 1);
570 }
571
572 static int
573 navit_cmd_zoom_in(struct navit *this_)
574 {
575         struct point p;
576         if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
577                 navit_zoom_in(this_, 2, &p);
578                 this_->vehicle->follow_curr=this_->vehicle->follow;
579         } else
580                 navit_zoom_in(this_, 2, NULL);
581         return 0;
582 }
583
584 static int
585 navit_cmd_zoom_out(struct navit *this_)
586 {
587         struct point p;
588         if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
589                 navit_zoom_out(this_, 2, &p);
590                 this_->vehicle->follow_curr=this_->vehicle->follow;
591         } else
592                 navit_zoom_out(this_, 2, NULL);
593         return 0;
594 }
595
596 static struct command_table commands[] = {
597         {"zoom_in",navit_cmd_zoom_in},
598         {"zoom_out",navit_cmd_zoom_out},
599         {"zoom_to_route",navit_cmd_zoom_to_route},
600         {"set_center_cursor",navit_cmd_set_center_cursor},
601 };
602         
603
604 struct navit *
605 navit_new(struct attr *parent, struct attr **attrs)
606 {
607         struct navit *this_=g_new0(struct navit, 1);
608         struct pcoord center;
609         struct coord co;
610         struct coord_geo g;
611         enum projection pro=projection_mg;
612         int zoom = 256;
613         g.lat=53.13;
614         g.lng=11.70;
615
616         this_->self.type=attr_navit;
617         this_->self.u.navit=this_;
618         this_->attr_cbl=callback_list_new();
619         main_add_navit(this_);
620
621         this_->bookmarks_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
622
623         this_->orientation=-1;
624         this_->tracking_flag=1;
625         this_->recentdest_count=10;
626         this_->osd_configuration=-1;
627
628         this_->center_timeout = 10;
629         this_->use_mousewheel = 1;
630         this_->autozoom_secs = 10;
631         this_->autozoom_min = 7;
632         this_->autozoom_active = 0;
633
634         this_->trans = transform_new();
635         transform_from_geo(pro, &g, &co);
636         center.x=co.x;
637         center.y=co.y;
638         center.pro = pro;
639
640         transform_setup(this_->trans, &center, zoom, (this_->orientation != -1) ? this_->orientation : 0);
641         for (;*attrs; attrs++) {
642                 navit_set_attr_do(this_, *attrs, 1);
643         }
644         this_->displaylist=graphics_displaylist_new();
645         command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
646         return this_;
647 }
648
649 static int
650 navit_set_gui(struct navit *this_, struct gui *gui)
651 {
652         if (this_->gui)
653                 return 0;
654         this_->gui=gui;
655         if (gui_has_main_loop(this_->gui)) {
656                 if (! main_loop_gui) {
657                         main_loop_gui=this_->gui;
658                 } else {
659                         dbg(0,"gui with main loop already active, ignoring this instance");
660                         return 0;
661                 }
662         }
663         return 1;
664 }
665
666 static int
667 navit_set_graphics(struct navit *this_, struct graphics *gra)
668 {
669         if (this_->gra)
670                 return 0;
671         this_->gra=gra;
672         this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
673         graphics_add_callback(gra, this_->resize_callback);
674         this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
675         graphics_add_callback(gra, this_->button_callback);
676         this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
677         graphics_add_callback(gra, this_->motion_callback);
678         return 1;
679 }
680
681 struct graphics *
682 navit_get_graphics(struct navit *this_)
683 {
684         return this_->gra;
685 }
686
687 static void
688 navit_projection_set(struct navit *this_, enum projection pro)
689 {
690         struct coord_geo g;
691         struct coord *c;
692
693         c=transform_center(this_->trans);
694         transform_to_geo(transform_get_projection(this_->trans), c, &g);
695         transform_set_projection(this_->trans, pro);
696         transform_from_geo(pro, &g, c);
697         navit_draw(this_);
698 }
699
700 /**
701  * @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
702  */
703 static void
704 navit_append_coord(struct navit *this_, char *file, struct pcoord *c, const char *type, const char *description, GHashTable *h, int limit)
705 {
706         FILE *f;
707         int offset=0;
708         char *buffer;
709         int ch,prev,lines=0;
710         int numc,readc;
711         int fd;
712         const char *prostr;
713
714         f=fopen(file, "r");
715         if (!f)
716                 goto new_file;
717         if (limit != 0) {
718                 prev = '\n';
719                 while ((ch = fgetc(f)) != EOF) {
720                         if ((ch == '\n') && (prev != '\n')) {
721                                 lines++;
722                         }
723                         prev = ch;
724                 }
725
726                 if (prev != '\n') { // Last line did not end with a newline
727                         lines++;
728                 }
729
730                 fclose(f);
731                 f = fopen(file, "r+");
732                 fd = fileno(f);
733                 while (lines >= limit) { // We have to "scroll up"
734                         rewind(f);
735                         numc = 0; // Counts how many bytes we have in our line to scroll up
736                         while ((ch = fgetc(f)) != EOF) {
737                                 numc++;
738                                 if (ch == '\n') {
739                                         break;
740                                 }
741                         }
742
743                         buffer=g_malloc(numc);
744                         offset = numc; // Offset holds where we currently are
745                         
746                         do {
747                                 fseek(f,offset,SEEK_SET);
748                                 readc = fread(buffer,1,numc,f);
749                                 
750                                 fseek(f,-(numc+readc),SEEK_CUR);
751                                 fwrite(buffer,1,readc,f);
752
753                                 offset += readc;
754                         } while (readc == numc);
755
756                         g_free(buffer);
757                         fflush(f);
758                         ftruncate(fd,(offset-numc));
759 #ifdef HAVE_FSYNC
760                         fsync(fd);
761 #endif
762
763                         lines--;
764                 }
765                 fclose(f);
766         }
767
768 new_file:
769         f=fopen(file, "a");
770         if (f) {
771                 if (c) {
772                         prostr = projection_to_name(c->pro);
773                         fprintf(f,"%s%s%s0x%x %s0x%x type=%s label=\"%s\"\n",
774                                  prostr, *prostr ? ":" : "", 
775                                  c->x >= 0 ? "":"-", c->x >= 0 ? c->x : -c->x, 
776                                  c->y >= 0 ? "":"-", c->y >= 0 ? c->y : -c->y, 
777                                  type, description);
778                 } else
779                         fprintf(f,"\n");
780                 fclose(f);
781         }
782 }
783
784 /*
785  * navit_get_user_data_directory
786  * 
787  * returns the directory used to store user data files (center.txt,
788  * destination.txt, bookmark.txt, ...)
789  *
790  * arg: gboolean create: create the directory if it does not exist
791  */
792 static char*
793 navit_get_user_data_directory(gboolean create) {
794         char *dir;
795         dir = getenv("NAVIT_USER_DATADIR");
796         if (create && !file_exists(dir)) {
797                 dbg(0,"creating dir %s\n", dir);
798                 if (file_mkdir(dir,0)) {
799                         dbg(0,"failed creating dir %s\n", dir);
800                         return NULL;
801                 }
802         }
803
804         return dir;
805 }
806
807 /*
808  * navit_get_destination_file
809  * 
810  * returns the name of the file used to store destinations with its
811  * full path
812  *
813  * arg: gboolean create: create the directory where the file is stored
814  * if it does not exist
815  */
816 static char*
817 navit_get_destination_file(gboolean create)
818 {
819         return g_strjoin(NULL, navit_get_user_data_directory(create), "/destination.txt", NULL);
820 }
821
822 /*
823  * navit_get_bookmark_file
824  * 
825  * returns the name of the file used to store bookmarks with its
826  * full path
827  *
828  * arg: gboolean create: create the directory where the file is stored
829  * if it does not exist
830  */
831 static char*
832 navit_get_bookmark_file(gboolean create)
833 {
834         return g_strjoin(NULL, navit_get_user_data_directory(create), "/bookmark.txt", NULL);
835 }
836
837
838 /*
839  * navit_get_bookmark_file
840  * 
841  * returns the name of the file used to store the center file  with its
842  * full path
843  *
844  * arg: gboolean create: create the directory where the file is stored
845  * if it does not exist
846  */
847 static char*
848 navit_get_center_file(gboolean create)
849 {
850         return g_strjoin(NULL, navit_get_user_data_directory(create), "/center.txt", NULL);
851 }
852
853 static void
854 navit_set_center_from_file(struct navit *this_, char *file)
855 {
856         FILE *f;
857         char *line = NULL;
858
859         size_t line_size = 0;
860         enum projection pro;
861         struct coord *center;
862
863         f = fopen(file, "r");
864         if (! f)
865                 return;
866         getline(&line, &line_size, f);
867         fclose(f);
868         if (line) {
869                 center = transform_center(this_->trans);
870                 pro = transform_get_projection(this_->trans);
871                 coord_parse(g_strchomp(line), pro, center);
872                 free(line);
873         }
874         return;
875 }
876  
877 static void
878 navit_write_center_to_file(struct navit *this_, char *file)
879 {
880         FILE *f;
881         enum projection pro;
882         struct coord *center;
883
884         f = fopen(file, "w+");
885         if (f) {
886                 center = transform_center(this_->trans);
887                 pro = transform_get_projection(this_->trans);
888                 coord_print(pro, center, f);
889                 fclose(f);
890         } else {
891                 perror(file);
892         }
893         return;
894 }
895
896
897 /**
898  * Start the route computing to a given set of coordinates
899  *
900  * @param navit The navit instance
901  * @param c The coordinate to start routing to
902  * @param description A label which allows the user to later identify this destination in the former destinations selection
903  * @returns nothing
904  */
905 void
906 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description)
907 {
908         if (c) {
909                 this_->destination=*c;
910                 this_->destination_valid=1;
911         } else
912                 this_->destination_valid=0;
913         char *destination_file = navit_get_destination_file(TRUE);
914         navit_append_coord(this_, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
915         g_free(destination_file);
916         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
917         if (this_->route) {
918                 route_set_destination(this_->route, c);
919
920                 if (this_->ready == 3)
921                         navit_draw(this_);
922         }
923 }
924
925 /**
926  * @brief Checks if a route is calculated
927  *
928  * This function checks if a route is calculated.
929  *
930  * @param this_ The navit struct whose route should be checked.
931  * @return True if the route is set, false otherwise.
932  */
933 int
934 navit_check_route(struct navit *this_)
935 {
936         if (this_->route) {
937                 return route_get_path_set(this_->route);
938         }
939
940         return 0;
941 }
942
943 /**
944  * Record the given set of coordinates as a bookmark
945  *
946  * @param navit The navit instance
947  * @param c The coordinate to store
948  * @param description A label which allows the user to later identify this bookmark
949  * @returns nothing
950  */
951 void
952 navit_add_bookmark(struct navit *this_, struct pcoord *c, const char *description)
953 {
954         char *bookmark_file = navit_get_bookmark_file(TRUE);
955         navit_append_coord(this_,bookmark_file, c, "bookmark", description, this_->bookmarks_hash,0);
956         g_free(bookmark_file);
957
958         callback_list_call_attr_0(this_->attr_cbl, attr_bookmark_map);
959 }
960
961 struct navit *global_navit;
962
963 static void
964 navit_add_bookmarks_from_file(struct navit *this_)
965 {
966         char *bookmark_file = navit_get_bookmark_file(FALSE);
967         struct attr parent={attr_navit, .u.navit=this_};
968         struct attr type={attr_type, {"textfile"}}, data={attr_data, {bookmark_file}};
969         struct attr *attrs[]={&type, &data, NULL};
970
971         this_->bookmark=map_new(&parent, attrs);
972         g_free(bookmark_file);
973 }
974
975 static int
976 navit_former_destinations_active(struct navit *this_)
977 {
978         char *destination_file = navit_get_destination_file(FALSE);
979         FILE *f;
980         int active=0;
981         char buffer[3];
982         f=fopen(destination_file,"r");
983         if (f) {
984                 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n')) 
985                         active=1;
986                 fclose(f);
987         }
988         g_free(destination_file);
989         return active;
990 }
991
992 static void
993 navit_add_former_destinations_from_file(struct navit *this_)
994 {
995         char *destination_file = navit_get_destination_file(FALSE);
996         struct attr parent={attr_navit, .u.navit=this_};
997         struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
998         struct attr *attrs[]={&type, &data, NULL};
999         struct map_rect *mr;
1000         struct item *item;
1001         int valid=0;
1002         struct coord c;
1003         struct pcoord pc;
1004
1005         this_->former_destination=map_new(&parent, attrs);
1006         g_free(destination_file);
1007         if (!this_->route || !navit_former_destinations_active(this_))
1008                 return; 
1009         mr=map_rect_new(this_->former_destination, NULL);
1010         while ((item=map_rect_get_item(mr))) {
1011                 if (item->type == type_former_destination && item_coord_get(item, &c, 1)) 
1012                         valid=1;
1013         }
1014         map_rect_destroy(mr);
1015         pc.pro=map_projection(this_->former_destination);
1016         pc.x=c.x;
1017         pc.y=c.y;
1018         if (valid) {
1019                 route_set_destination(this_->route, &pc);
1020                 this_->destination=pc;
1021                 this_->destination_valid=1;
1022         }
1023 }
1024
1025
1026 static void
1027 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1028 {
1029         va_list ap;
1030         char *str1,*str2;
1031         va_start(ap, fmt);
1032         if (this_->textfile_debug_log && this_->vehicle) {
1033                 str1=g_strdup_vprintf(fmt, ap);
1034                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1035                 log_write(this_->textfile_debug_log, str2, strlen(str2));
1036                 g_free(str2);
1037                 g_free(str1);
1038         }
1039         va_end(ap);
1040 }
1041
1042 int 
1043 navit_speech_estimate(struct navit *this_, char *str)
1044 {
1045         return speech_estimate_duration(this_->speech, str);
1046 }
1047
1048 void
1049 navit_say(struct navit *this_, char *text)
1050 {
1051         speech_say(this_->speech, text);
1052 }
1053
1054 /**
1055  * @brief Toggles the navigation announcer for navit
1056  * @param this_ The navit object
1057  */
1058 void
1059 navit_announcer_toggle(struct navit *this_)
1060 {
1061     struct attr attr, speechattr;
1062
1063     // search for the speech attribute
1064     if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1065         return;
1066     // find out if the corresponding attribute attr_active has been set
1067     if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1068         // flip it then...
1069         attr.u.num = !attr.u.num;
1070     } else {
1071         // otherwise disable it because voice is enabled by default
1072         attr.type = attr_active;
1073         attr.u.num = 0;
1074     }
1075
1076     // apply the new state
1077     if(!speech_set_attr(speechattr.u.speech, &attr))
1078         return;
1079
1080     // announce that the speech attribute has changed
1081     callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1082 }
1083
1084 void
1085 navit_speak(struct navit *this_)
1086 {
1087         struct navigation *nav=this_->navigation;
1088         struct map *map=NULL;
1089         struct map_rect *mr=NULL;
1090         struct item *item;
1091         struct attr attr;
1092
1093     if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1094         attr.u.num = 1;
1095     dbg(1, "this_.speech->active %i\n", attr.u.num);
1096     if(!attr.u.num)
1097         return;
1098
1099         if (nav)
1100                 map=navigation_get_map(nav);
1101         if (map)
1102                 mr=map_rect_new(map, NULL);
1103         if (mr) {
1104                 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1105                 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1106                         speech_say(this_->speech, attr.u.str);
1107                         navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1108                 }
1109                 map_rect_destroy(mr);
1110         }
1111 }
1112
1113 static void
1114 navit_window_roadbook_update(struct navit *this_)
1115 {
1116         struct navigation *nav=this_->navigation;
1117         struct map *map=NULL;
1118         struct map_rect *mr=NULL;
1119         struct item *item;
1120         struct attr attr;
1121         struct param_list param[5];
1122         int secs;
1123
1124         dbg(1,"enter\n");
1125         datawindow_mode(this_->roadbook_window, 1);
1126         if (nav)
1127                 map=navigation_get_map(nav);
1128         if (map)
1129                 mr=map_rect_new(map, NULL);
1130         dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1131         if (mr) {
1132                 dbg(0,"while loop\n");
1133                 while ((item=map_rect_get_item(mr))) {
1134                         dbg(0,"item=%p\n", item);
1135                         attr.u.str=NULL;
1136                         if (item->type != type_nav_position) {
1137                                 item_attr_get(item, attr_navigation_long, &attr);
1138                                 dbg(2, "Command='%s'\n", attr.u.str);
1139                                 param[0].value=g_strdup(attr.u.str);
1140                         } else
1141                                 param[0].value=_("Position");
1142                         param[0].name=_("Command");
1143
1144                         item_attr_get(item, attr_length, &attr);
1145                         dbg(2, "Length=%d\n", attr.u.num);
1146                         param[1].name=_("Length");
1147
1148                         if ( attr.u.num >= 2000 )
1149                         {
1150                                 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1151                         }
1152                         else
1153                         {
1154                                 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1155                         }
1156
1157                         item_attr_get(item, attr_time, &attr);
1158                         dbg(2, "Time=%d\n", attr.u.num);
1159                         secs=attr.u.num/10;
1160                         param[2].name=_("Time");
1161                         if ( secs >= 3600 )
1162                         {
1163                                 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1164                         }
1165                         else
1166                         {
1167                                 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1168                         }
1169
1170                         item_attr_get(item, attr_destination_length, &attr);
1171                         dbg(2, "Destlength=%d\n", attr.u.num);
1172                         param[3].name=_("Destination Length");
1173                         if ( attr.u.num >= 2000 )
1174                         {
1175                                 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1176                         }
1177                         else
1178                         {
1179                                 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1180                         }
1181
1182                         item_attr_get(item, attr_destination_time, &attr);
1183                         dbg(2, "Desttime=%d\n", attr.u.num);
1184                         secs=attr.u.num/10;
1185                         param[4].name=_("Destination Time");
1186                         if ( secs >= 3600 )
1187                         {
1188                                 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1189                         }
1190                         else
1191                         {
1192                                 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1193                         }
1194                         datawindow_add(this_->roadbook_window, param, 5);
1195                 }
1196                 map_rect_destroy(mr);
1197         }
1198         datawindow_mode(this_->roadbook_window, 0);
1199 }
1200
1201 void
1202 navit_window_roadbook_destroy(struct navit *this_)
1203 {
1204         dbg(0, "enter\n");
1205         navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1206         this_->roadbook_window=NULL;
1207         this_->roadbook_callback=NULL;
1208 }
1209 void
1210 navit_window_roadbook_new(struct navit *this_)
1211 {
1212         if (this_->roadbook_callback || this_->roadbook_window) {
1213                 return;
1214         }
1215
1216         this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1217         navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1218         this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1219         navit_window_roadbook_update(this_);
1220 }
1221
1222 void
1223 navit_init(struct navit *this_)
1224 {
1225         struct mapset *ms;
1226         struct map *map;
1227
1228         if (!this_->gui) {
1229                 dbg(0,"no gui\n");
1230                 navit_destroy(this_);
1231                 return;
1232         }
1233         if (!this_->gra) {
1234                 dbg(0,"no graphics\n");
1235                 navit_destroy(this_);
1236                 return;
1237         }
1238         if (gui_set_graphics(this_->gui, this_->gra)) {
1239                 struct attr attr_type_gui, attr_type_graphics;
1240                 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1241                 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1242                 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1243                 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1244                 dbg(0," for explanations and solutions\n");
1245
1246                 navit_destroy(this_);
1247                 return;
1248         }
1249         graphics_init(this_->gra);
1250         if (this_->mapsets) {
1251                 ms=this_->mapsets->data;
1252                 if (this_->route) {
1253                         if ((map=route_get_map(this_->route)))
1254                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1255                         if ((map=route_get_graph_map(this_->route))) {
1256                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1257                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1258                         }
1259                         route_set_mapset(this_->route, ms);
1260                         route_set_projection(this_->route, transform_get_projection(this_->trans));
1261                 }
1262                 if (this_->tracking) {
1263                         tracking_set_mapset(this_->tracking, ms);
1264                         if (this_->route)
1265                                 tracking_set_route(this_->tracking, this_->route);
1266                 }
1267                 if (this_->navigation) {
1268                         if ((map=navigation_get_map(this_->navigation))) {
1269                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1270                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1271                         }
1272                 }
1273                 if (this_->tracking) {
1274                         if ((map=tracking_get_map(this_->tracking))) {
1275                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1276                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1277                         }
1278                 }
1279                 navit_add_bookmarks_from_file(this_);
1280                 navit_add_former_destinations_from_file(this_);
1281         }
1282         if (this_->route) {
1283                 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route, this_);
1284                 route_add_callback(this_->route, this_->route_cb);
1285         }
1286         if (this_->navigation) {
1287                 if (this_->speech) {
1288                         this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1289                         navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1290                 }
1291                 if (this_->route)
1292                         navigation_set_route(this_->navigation, this_->route);
1293         }
1294         char *center_file = navit_get_center_file(FALSE);
1295         navit_set_center_from_file(this_, center_file);
1296         g_free(center_file);
1297 #if 0
1298         if (this_->menubar) {
1299                 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1300                 if (men) {
1301                         navit_add_menu_windows_items(this_, men);
1302                 }
1303         }
1304 #endif
1305         global_navit=this_;
1306 #if 0
1307         navit_window_roadbook_new(this_);
1308         navit_window_items_new(this_);
1309 #endif
1310         callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1311         this_->ready|=1;
1312         if (this_->ready == 3)
1313                 navit_draw(this_);
1314 }
1315
1316 void
1317 navit_zoom_to_route(struct navit *this_, int orientation)
1318 {
1319         struct map *map;
1320         struct map_rect *mr=NULL;
1321         struct item *item;
1322         struct coord c;
1323         struct coord_rect r;
1324         int count=0,scale=16;
1325         if (! this_->route)
1326                 return;
1327         dbg(1,"enter\n");
1328         map=route_get_map(this_->route);
1329         dbg(1,"map=%p\n",map);
1330         if (map)
1331                 mr=map_rect_new(map, NULL);
1332         dbg(1,"mr=%p\n",mr);
1333         if (mr) {
1334                 while ((item=map_rect_get_item(mr))) {
1335                         dbg(1,"item=%s\n", item_to_name(item->type));
1336                         while (item_coord_get(item, &c, 1)) {
1337                                 dbg(1,"coord\n");
1338                                 if (!count) 
1339                                         r.lu=r.rl=c;
1340                                 else
1341                                         coord_rect_extend(&r, &c);      
1342                                 count++;
1343                         }
1344                 }
1345         }
1346         if (! count)
1347                 return;
1348         c.x=(r.rl.x+r.lu.x)/2;
1349         c.y=(r.rl.y+r.lu.y)/2;
1350         dbg(1,"count=%d\n",count);
1351         if (orientation != -1)
1352                 transform_set_yaw(this_->trans, orientation);
1353         transform_set_center(this_->trans, &c);
1354         dbg(1,"%x,%x-%x,%x\n", r.rl.x,r.rl.y,r.lu.x,r.lu.y);
1355         while (scale < 1<<20) {
1356                 struct point p1,p2;
1357                 transform_set_scale(this_->trans, scale);
1358                 transform_setup_source_rect(this_->trans);
1359                 transform(this_->trans, transform_get_projection(this_->trans), &r.lu, &p1, 1, 0, 0, NULL);
1360                 transform(this_->trans, transform_get_projection(this_->trans), &r.rl, &p2, 1, 0, 0, NULL);
1361                 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1362                 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1363                     p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1364                         scale*=2;
1365                 else
1366                         break;
1367         
1368         }
1369         if (this_->ready == 3)
1370                 navit_draw_async(this_,0);
1371 }
1372
1373 static void
1374 navit_cmd_zoom_to_route(struct navit *this)
1375 {
1376         navit_zoom_to_route(this, 0);
1377 }
1378
1379 /**
1380  * Change the current zoom level
1381  *
1382  * @param navit The navit instance
1383  * @param center The point where to center the map, including its projection
1384  * @returns nothing
1385  */
1386 void
1387 navit_set_center(struct navit *this_, struct pcoord *center)
1388 {
1389         struct coord *c=transform_center(this_->trans);
1390         struct coord c1,c2;
1391         enum projection pro = transform_get_projection(this_->trans);
1392         if (pro != center->pro) {
1393                 c1.x = center->x;
1394                 c1.y = center->y;
1395                 transform_from_to(&c1, center->pro, &c2, pro);
1396         } else {
1397                 c2.x = center->x;
1398                 c2.y = center->y;
1399         }
1400         *c=c2;
1401         if (this_->ready == 3)
1402                 navit_draw(this_);
1403 }
1404
1405 static void
1406 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p)
1407 {
1408         int width, height;
1409         struct point po;
1410         transform_set_center(this_->trans, c);
1411         transform_get_size(this_->trans, &width, &height);
1412         po.x=width/2;
1413         po.y=height/2;
1414         update_transformation(this_->trans, &po, p, NULL);
1415 }
1416
1417 static int
1418 navit_get_cursor_pnt(struct navit *this_, struct point *p, int *dir)
1419 {
1420         int width, height;
1421         struct navit_vehicle *nv=this_->vehicle;
1422         transform_get_size(this_->trans, &width, &height);
1423         if (this_->orientation == -1) {
1424                 p->x=50*width/100;
1425                 p->y=80*height/100;
1426                 if (dir)
1427                         *dir=nv->dir;
1428         } else {
1429                 int mdir;
1430                 if (this_->tracking && this_->tracking_flag) {
1431                         mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1432                 } else {
1433                         mdir=nv->dir-this_->orientation;
1434                 }
1435
1436                 p->x=(50 - 30.*sin(M_PI*mdir/180.))*width/100;
1437                 p->y=(50 + 30.*cos(M_PI*mdir/180.))*height/100;
1438                 if (dir)
1439                         *dir=this_->orientation;
1440         }
1441         return 1;
1442 }
1443
1444 static void
1445 navit_set_center_cursor(struct navit *this_)
1446 {
1447         int dir;
1448         struct point pn;
1449         struct navit_vehicle *nv=this_->vehicle;
1450         navit_get_cursor_pnt(this_, &pn, &dir);
1451         transform_set_yaw(this_->trans, dir);
1452         navit_set_center_coord_screen(this_, &nv->coord, &pn);
1453         navit_autozoom(this_, &nv->coord, nv->speed, 0);
1454         if (this_->ready == 3)
1455                 navit_draw_async(this_, 1);
1456 }
1457
1458 static void
1459 navit_cmd_set_center_cursor(struct navit *this_)
1460 {
1461         navit_set_center_cursor(this_);
1462 }
1463
1464 void
1465 navit_set_center_screen(struct navit *this_, struct point *p)
1466 {
1467         struct coord c;
1468         struct pcoord pc;
1469         transform_reverse(this_->trans, p, &c);
1470         pc.x = c.x;
1471         pc.y = c.y;
1472         pc.pro = transform_get_projection(this_->trans);
1473         navit_set_center(this_, &pc);
1474 }
1475
1476 #if 0
1477                 switch((*attrs)->type) {
1478                 case attr_zoom:
1479                         zoom=(*attrs)->u.num;
1480                         break;
1481                 case attr_center:
1482                         g=*((*attrs)->u.coord_geo);
1483                         break;
1484 #endif
1485
1486 static int
1487 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1488 {
1489         int dir=0, orient_old=0, attr_updated=0;
1490         struct coord co;
1491         long zoom;
1492
1493         switch (attr->type) {
1494         case attr_autozoom:
1495                 attr_updated=(this_->autozoom_secs != attr->u.num);
1496                 this_->autozoom_secs = attr->u.num;
1497                 break;
1498         case attr_autozoom_active:
1499                 attr_updated=(this_->autozoom_active != attr->u.num);
1500                 this_->autozoom_active = attr->u.num;
1501                 break;
1502         case attr_center:
1503                 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1504                 dbg(0,"0x%x,0x%x\n",co.x,co.y);
1505                 transform_set_center(this_->trans, &co);
1506                 break;
1507         case attr_drag_bitmap:
1508                 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1509                 this_->drag_bitmap=!!attr->u.num;
1510                 break;
1511         case attr_follow:
1512                 if (!this_->vehicle)
1513                         return 0;
1514                 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1515                 this_->vehicle->follow_curr = attr->u.num;
1516                 break;
1517         case attr_layout:
1518                 if(this_->layout_current!=attr->u.layout) {
1519                         this_->layout_current=attr->u.layout;
1520                         graphics_font_destroy_all(this_->gra);
1521                         navit_draw(this_);
1522                         attr_updated=1;
1523                 }
1524                 break;
1525         case attr_orientation:
1526                 orient_old=this_->orientation;
1527                 this_->orientation=attr->u.num;
1528                 if (!init) {
1529                         if (this_->orientation != -1) {
1530                                 dir = this_->orientation;
1531                         } else {
1532                                 if (this_->vehicle) {
1533                                         dir = this_->vehicle->dir;
1534                                 }
1535                         }
1536                         transform_set_yaw(this_->trans, dir);
1537                         if (orient_old != this_->orientation) {
1538                                 if (this_->ready == 3)
1539                                         navit_draw(this_);
1540                                 attr_updated=1;
1541                         }
1542                 }
1543                 break;
1544         case attr_osd_configuration:
1545                 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1546                 attr_updated=(this_->osd_configuration != attr->u.num);
1547                 this_->osd_configuration=attr->u.num;
1548                 break;
1549         case attr_projection:
1550                 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1551                         navit_projection_set(this_, attr->u.projection);
1552                         attr_updated=1;
1553                 }
1554                 break;
1555         case attr_recent_dest:
1556                 attr_updated=(this_->recentdest_count != attr->u.num);
1557                 this_->recentdest_count=attr->u.num;
1558                 break;
1559         case attr_speech:
1560                 if(this_->speech && this_->speech != attr->u.speech) {
1561                         attr_updated=1;
1562                         this_->speech = attr->u.speech;
1563                 }
1564                 break;
1565         case attr_timeout:
1566                 attr_updated=(this_->center_timeout != attr->u.num);
1567                 this_->center_timeout = attr->u.num;
1568                 break;
1569         case attr_tracking:
1570                 attr_updated=(this_->tracking_flag != !!attr->u.num);
1571                 this_->tracking_flag=!!attr->u.num;
1572                 break;
1573         case attr_use_mousewheel:
1574                 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1575                 this_->use_mousewheel=!!attr->u.num;
1576                 break;
1577         case attr_vehicle:
1578                 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1579                         GList *l;
1580                         struct navit_vehicle *nv;
1581                         struct attr active=(struct attr){attr_active,{(void *)0}};
1582                         l=this_->vehicles;
1583                         while(l) {
1584                                 nv=l->data;
1585                                 if (nv->vehicle == attr->u.vehicle) {
1586                                         if (this_->vehicle)
1587                                                 vehicle_set_attr(this_->vehicle->vehicle, &active, NULL);
1588                                         active.u.num=1;
1589                                         vehicle_set_attr(nv->vehicle, &active, NULL);
1590                                         this_->vehicle=nv;
1591                                         attr_updated=1;
1592                                 }
1593                                 l=g_list_next(l);
1594                         }
1595                 }
1596                 break;
1597         case attr_zoom:
1598                 zoom=transform_get_scale(this_->trans);
1599                 attr_updated=(zoom != attr->u.num);
1600                 transform_set_scale(this_->trans, attr->u.num);
1601                 if (attr_updated && !init) 
1602                         navit_draw(this_);
1603                 break;
1604         default:
1605                 return 0;
1606         }
1607         if (attr_updated && !init) {
1608                 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1609                 if (attr->type == attr_osd_configuration)
1610                         graphics_draw_mode(this_->gra, draw_mode_end);
1611         }
1612         return 1;
1613 }
1614
1615 int
1616 navit_set_attr(struct navit *this_, struct attr *attr)
1617 {
1618         return navit_set_attr_do(this_, attr, 0);
1619 }
1620
1621 int
1622 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1623 {
1624         switch (type) {
1625         case attr_bookmark_map:
1626                 attr->u.map=this_->bookmark;
1627                 break;
1628         case attr_callback_list:
1629                 attr->u.callback_list=this_->attr_cbl;
1630                 break;
1631         case attr_destination:
1632                 if (! this_->destination_valid)
1633                         return 0;
1634                 attr->u.pcoord=&this_->destination;
1635                 break;
1636         case attr_former_destination_map:
1637                 attr->u.map=this_->former_destination;
1638                 break;
1639         case attr_gui:
1640                 attr->u.gui=this_->gui;
1641                 break;
1642         case attr_layout:
1643                 if (iter) {
1644                         if (iter->u.list) {
1645                                 iter->u.list=g_list_next(iter->u.list);
1646                         } else { 
1647                                 iter->u.list=this_->layouts;
1648                         }
1649                         if (!iter->u.list)
1650                                 return 0;
1651                         attr->u.layout=(struct layout *)iter->u.list->data;
1652                 } else {
1653                         attr->u.layout=this_->layout_current;
1654                 }
1655                 break;
1656         case attr_map:
1657                 if (iter && this_->mapsets) {
1658                         if (!iter->u.mapset_handle) {
1659                                 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
1660                         }
1661                         attr->u.map=mapset_next(iter->u.mapset_handle, 0);
1662                         if(!attr->u.map) {
1663                                 mapset_close(iter->u.mapset_handle);
1664                                 return 0;
1665                         }
1666                 } else {
1667                         return 0;
1668                 }
1669                 break;
1670         case attr_navigation:
1671                 attr->u.navigation=this_->navigation;
1672                 break;
1673         case attr_orientation:
1674                 attr->u.num=this_->orientation;
1675                 break;
1676         case attr_osd_configuration:
1677                 attr->u.num=this_->osd_configuration;
1678                 break;
1679         case attr_projection:
1680                 if(this_->trans) {
1681                         attr->u.num=transform_get_projection(this_->trans);
1682                 } else {
1683                         return 0;
1684                 }
1685                 break;
1686         case attr_route:
1687                 attr->u.route=this_->route;
1688                 break;
1689         case attr_speech:
1690                 attr->u.speech=this_->speech;
1691                 break;
1692         case attr_tracking:
1693                 attr->u.num=this_->tracking_flag;
1694                 break;
1695         case attr_vehicle:
1696                 if(iter) {
1697                         if(iter->u.list) {
1698                                 iter->u.list=g_list_next(iter->u.list);
1699                         } else { 
1700                                 iter->u.list=this_->vehicles;
1701                         }
1702                         if(!iter->u.list)
1703                                 return 0;
1704                         attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
1705                 } else {
1706                         if(this_->vehicle) {
1707                                 attr->u.vehicle=this_->vehicle->vehicle;
1708                         } else {
1709                                 return 0;
1710                         }
1711                 }
1712                 break;
1713         case attr_zoom:
1714                 attr->u.num=transform_get_scale(this_->trans);
1715                 break;
1716         case attr_autozoom_active:
1717                 attr->u.num=this_->autozoom_active;
1718                 break;
1719         default:
1720                 return 0;
1721         }
1722         attr->type=type;
1723         return 1;
1724 }
1725
1726 static int
1727 navit_add_log(struct navit *this_, struct log *log)
1728 {
1729         struct attr type_attr;
1730         if (!log_get_attr(log, attr_type, &type_attr, NULL))
1731                 return 0;
1732         if (!strcmp(type_attr.u.str, "textfile_debug")) {
1733                 char *header = "type=track_tracked\n";
1734                 if (this_->textfile_debug_log)
1735                         return 0;
1736                 log_set_header(log, header, strlen(header));
1737                 this_->textfile_debug_log=log;
1738                 return 1;
1739         }
1740         return 0;
1741 }
1742
1743 int
1744 navit_add_attr(struct navit *this_, struct attr *attr)
1745 {
1746         int ret=1;
1747         switch (attr->type) {
1748         case attr_log:
1749                 ret=navit_add_log(this_, attr->u.log);
1750                 break;
1751         case attr_gui:
1752                 ret=navit_set_gui(this_, attr->u.gui);
1753                 break;
1754         case attr_graphics:
1755                 ret=navit_set_graphics(this_, attr->u.graphics);
1756                 break;
1757         case attr_layout:
1758                 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
1759                 if(!this_->layout_current) 
1760                         this_->layout_current=attr->u.layout;
1761                 ret=1;
1762                 break;
1763         case attr_route:
1764                 this_->route=attr->u.route;
1765                 break;
1766         case attr_mapset:
1767                 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
1768                 break;
1769         case attr_navigation:
1770                 this_->navigation=attr->u.navigation;
1771                 break;
1772         case attr_recent_dest:
1773                 this_->recentdest_count = attr->u.num;
1774                 break;
1775         case attr_speech:
1776                 this_->speech=attr->u.speech;
1777                 break;
1778         case attr_tracking:
1779                 this_->tracking=attr->u.tracking;
1780                 break;
1781         case attr_vehicle:
1782                 ret=navit_add_vehicle(this_, attr->u.vehicle);
1783                 break;
1784         case attr_autozoom_min:
1785                 this_->autozoom_min = attr->u.num;
1786                 break;
1787         default:
1788                 return 0;
1789         }
1790         callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1791         return ret;
1792 }
1793
1794 int
1795 navit_remove_attr(struct navit *this_, struct attr *attr)
1796 {
1797         return 0;
1798 }
1799
1800 struct attr_iter *
1801 navit_attr_iter_new()
1802 {
1803         return g_new0(struct attr_iter, 1);
1804 }
1805
1806 void
1807 navit_attr_iter_destroy(struct attr_iter *iter)
1808 {
1809         g_free(iter);
1810 }
1811
1812 void
1813 navit_add_callback(struct navit *this_, struct callback *cb)
1814 {
1815         callback_list_add(this_->attr_cbl, cb);
1816 }
1817
1818 void
1819 navit_remove_callback(struct navit *this_, struct callback *cb)
1820 {
1821         callback_list_remove(this_->attr_cbl, cb);
1822 }
1823
1824 /**
1825  * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
1826  *
1827  * @param navit The navit instance
1828  * @returns nothing
1829  */
1830
1831 static void
1832 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
1833 {
1834         struct point cursor_pnt;
1835         enum projection pro;
1836         struct attr cursor;
1837
1838         if (this_->blocked)
1839                 return;
1840         if (! vehicle_get_attr(nv->vehicle, attr_cursor, &cursor, NULL))
1841                 return;
1842         if (! cursor.u.cursor)
1843                 return;
1844         if (pnt)
1845                 cursor_pnt=*pnt;
1846         else {
1847                 pro=transform_get_projection(this_->trans);
1848                 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
1849         }
1850         cursor_draw(cursor.u.cursor, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans), nv->speed);
1851 #if 0   
1852         if (pnt)
1853                 pnt2=*pnt;
1854         else {
1855                 pro=transform_get_projection(this_->trans);
1856                 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
1857         }
1858 #if 1
1859         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
1860 #else
1861         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
1862 #endif
1863 #endif
1864 }
1865
1866 static void
1867 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
1868 {
1869         struct attr attr_dir, attr_speed, attr_pos, attr_hdop, attr_time;
1870         struct pcoord cursor_pc;
1871         struct point cursor_pnt, *pnt=&cursor_pnt;
1872         enum projection pro;
1873         int border=16;
1874         time_t fixtime;
1875         struct tm fixtime_tm;
1876         int recenter = 1; // indicates if we should recenter the map
1877
1878         profile(0,NULL);
1879         if (this_->ready != 3) {
1880                 profile(0,"return 1\n");
1881                 return;
1882         }
1883
1884         if (! vehicle_get_attr(nv->vehicle, attr_position_direction, &attr_dir, NULL) ||
1885             ! vehicle_get_attr(nv->vehicle, attr_position_speed, &attr_speed, NULL) ||
1886             ! vehicle_get_attr(nv->vehicle, attr_position_coord_geo, &attr_pos, NULL)) {
1887                 profile(0,"return 2\n");
1888                 return;
1889         }
1890         nv->dir=*attr_dir.u.numd;
1891         nv->speed=*attr_speed.u.numd;
1892         pro=transform_get_projection(this_->trans);
1893         transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
1894         if (nv != this_->vehicle) {
1895                 navit_vehicle_draw(this_, nv, NULL);
1896                 profile(0,"return 3\n");
1897                 return;
1898         }
1899
1900         if (nv->speed < 10) {
1901                 long long diff,diff_x,diff_y;
1902
1903                 diff_x = abs(nv->coord.x - nv->last.x);
1904                 diff_y = abs(nv->coord.y - nv->last.y);
1905                 diff = (diff_x * diff_x) + (diff_y * diff_y);
1906
1907                 if ((diff < 20) && (diff > 0)) { // if our long is only 32 bit wide, we could run into an overflow here
1908                         recenter = 0;
1909                 }
1910         }
1911         if (recenter) {
1912                 nv->last = nv->coord;
1913         }
1914
1915         cursor_pc.x = nv->coord.x;
1916         cursor_pc.y = nv->coord.y;
1917         cursor_pc.pro = pro;
1918         if (this_->tracking && this_->tracking_flag) {
1919                 if (! vehicle_get_attr(nv->vehicle, attr_position_hdop, &attr_hdop, NULL)) {
1920                         attr_hdop.u.numd = -1;
1921                 }
1922
1923                 if (! vehicle_get_attr(nv->vehicle, attr_position_time_iso8601, &attr_time, NULL)) {
1924                         fixtime = time(NULL);
1925                 } else {
1926                         strptime(attr_time.u.str, "%Y-%m-%dT%TZ", &fixtime_tm);
1927                         fixtime = mktime(&fixtime_tm);
1928                 }
1929
1930                 if (tracking_update(this_->tracking, &cursor_pc, nv->dir, attr_hdop.u.numd, nv->speed, fixtime)) {
1931                         nv->coord.x=cursor_pc.x;
1932                         nv->coord.y=cursor_pc.y;
1933                 }
1934         }
1935         if (this_->route) {
1936                 if (this_->tracking && this_->tracking_flag)
1937                         route_set_position_from_tracking(this_->route, this_->tracking);
1938                 else
1939                         route_set_position(this_->route, &cursor_pc);
1940         }
1941         callback_list_call_attr_0(this_->attr_cbl, attr_position);
1942         navit_textfile_debug_log(this_, "type=trackpoint_tracked");
1943         if (this_->gui && nv->speed > 2)
1944                 gui_disable_suspend(this_->gui);
1945
1946         transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
1947         if (this_->button_pressed != 1 && nv->follow_curr <= nv->follow && 
1948                 (nv->follow_curr == 1 || !transform_within_border(this_->trans, &cursor_pnt, border)))
1949                 navit_set_center_cursor(this_);
1950         else
1951                 navit_vehicle_draw(this_, nv, pnt);
1952
1953         if (nv->follow_curr > 1)
1954                 nv->follow_curr--;
1955         else
1956                 nv->follow_curr=nv->follow;
1957         callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
1958
1959         /* Finally, if we reached our destination, stop navigation. */
1960         if (this_->route && route_destination_reached(this_->route)) {
1961                 navit_set_destination(this_, NULL, NULL);
1962         }
1963         profile(0,"return 5\n");
1964 }
1965
1966 /**
1967  * Set the position of the vehicle
1968  *
1969  * @param navit The navit instance
1970  * @param c The coordinate to set as position
1971  * @returns nothing
1972  */
1973
1974 void
1975 navit_set_position(struct navit *this_, struct pcoord *c)
1976 {
1977         if (this_->route) {
1978                 route_set_position(this_->route, c);
1979                 callback_list_call_attr_0(this_->attr_cbl, attr_position);
1980         }
1981         if (this_->ready == 3)
1982                 navit_draw(this_);
1983 }
1984
1985 static void
1986 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
1987 {
1988         this_->vehicle=nv;
1989 }
1990
1991 /**
1992  * Register a new vehicle
1993  *
1994  * @param navit The navit instance
1995  * @param v The vehicle instance
1996  * @returns 1 for success
1997  */
1998 static int
1999 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2000 {
2001         struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2002         struct attr follow,color,active, color2, animate;
2003         nv->vehicle=v;
2004         nv->follow=0;
2005         nv->last.x = 0;
2006         nv->last.y = 0;
2007         nv->animate_cursor=0;
2008         if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2009                 nv->follow=nv->follow=follow.u.num;
2010         if ((vehicle_get_attr(v, attr_color, &color, NULL)))
2011                 nv->c=*(color.u.color);
2012         if ((vehicle_get_attr(v, attr_color2, &color2, NULL)))
2013                 nv->c2=color2.u.color;
2014         else
2015                 nv->c2=NULL;
2016         nv->follow_curr=nv->follow;
2017         this_->vehicles=g_list_append(this_->vehicles, nv);
2018         if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2019                 navit_set_vehicle(this_, nv);
2020         if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2021                 nv->animate_cursor=animate.u.num;
2022         nv->callback.type=attr_callback;
2023         nv->callback.u.callback=callback_new_2(callback_cast(navit_vehicle_update), this_, nv);
2024         vehicle_add_attr(nv->vehicle, &nv->callback);
2025         vehicle_set_attr(nv->vehicle, &this_->self, NULL);
2026         return 1;
2027 }
2028
2029
2030
2031
2032 struct gui *
2033 navit_get_gui(struct navit *this_)
2034 {
2035         return this_->gui;
2036 }
2037
2038 struct transformation *
2039 navit_get_trans(struct navit *this_)
2040 {
2041         return this_->trans;
2042 }
2043
2044 struct route *
2045 navit_get_route(struct navit *this_)
2046 {
2047         return this_->route;
2048 }
2049
2050 struct navigation *
2051 navit_get_navigation(struct navit *this_)
2052 {
2053         return this_->navigation;
2054 }
2055
2056 struct displaylist *
2057 navit_get_displaylist(struct navit *this_)
2058 {
2059         return this_->displaylist;
2060 }
2061
2062 int
2063 navit_block(struct navit *this_, int block)
2064 {
2065         if (block) {
2066                 this_->blocked |= 1;
2067                 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2068                         this_->blocked |= 2;
2069                 return 0;
2070         }
2071         if (this_->blocked & 2) {
2072                 this_->blocked=0;
2073                 navit_draw(this_);
2074                 return 1;
2075         }
2076         this_->blocked=0;
2077         return 0;
2078 }
2079
2080 void
2081 navit_destroy(struct navit *this_)
2082 {
2083         /* TODO: destroy objects contained in this_ */
2084         if (this_->vehicle)
2085                 vehicle_destroy(this_->vehicle->vehicle);
2086         main_remove_navit(this_);
2087         char *center_file = navit_get_center_file(TRUE);
2088         navit_write_center_to_file(this_, center_file);
2089         g_free(center_file);
2090         callback_destroy(this_->nav_speech_cb);
2091         callback_destroy(this_->roadbook_callback);
2092         callback_destroy(this_->popup_callback);
2093         callback_destroy(this_->motion_timeout_callback);
2094         if(this_->gra)
2095           graphics_remove_callback(this_->gra, this_->resize_callback);
2096         callback_destroy(this_->resize_callback);
2097         if(this_->gra)
2098           graphics_remove_callback(this_->gra, this_->button_callback);
2099         callback_destroy(this_->button_callback);
2100         if(this_->gra)
2101           graphics_remove_callback(this_->gra, this_->motion_callback);
2102         callback_destroy(this_->motion_callback);
2103         g_free(this_);
2104 }
2105
2106 /** @} */