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