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