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