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