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