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