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