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