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