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