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