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