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