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