Fix:Core:Make vehicle cursor have priority over layout cursor
[profile/ivi/navit.git] / navit / 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 #define _USE_MATH_DEFINES 1
21 #include "config.h"
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <glib.h>
31 #include <math.h>
32 #include <time.h>
33 #include "debug.h"
34 #include "navit.h"
35 #include "callback.h"
36 #include "gui.h"
37 #include "item.h"
38 #include "projection.h"
39 #include "map.h"
40 #include "mapset.h"
41 #include "main.h"
42 #include "coord.h"
43 #include "point.h"
44 #include "transform.h"
45 #include "param.h"
46 #include "menu.h"
47 #include "graphics.h"
48 #include "popup.h"
49 #include "data_window.h"
50 #include "route.h"
51 #include "navigation.h"
52 #include "speech.h"
53 #include "track.h"
54 #include "vehicle.h"
55 #include "layout.h"
56 #include "log.h"
57 #include "attr.h"
58 #include "event.h"
59 #include "file.h"
60 #include "profile.h"
61 #include "command.h"
62 #include "navit_nls.h"
63 #include "map.h"
64 #include "util.h"
65 #include "messages.h"
66 #include "vehicleprofile.h"
67 #include "sunriset.h"
68 #include "bookmarks.h"
69
70 /**
71  * @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
72  * @{
73  */
74
75 //! The navit_vehicule
76 struct navit_vehicle {
77         int follow;
78         /*! Limit of the follow counter. See navit_add_vehicle */
79         int follow_curr;
80         /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
81         struct coord coord;
82         int dir;
83         int speed;
84         struct coord last; /*< Position of the last update of this vehicle */
85         struct vehicle *vehicle;
86         struct attr callback;
87         int animate_cursor;
88 };
89
90 struct navit {
91         struct attr self;
92         GList *mapsets;
93         GList *layouts;
94         struct gui *gui;
95         struct layout *layout_current;
96         struct graphics *gra;
97         struct action *action;
98         struct transformation *trans, *trans_cursor;
99         struct compass *compass;
100         struct route *route;
101         struct navigation *navigation;
102         struct speech *speech;
103         struct tracking *tracking;
104         int ready;
105         struct window *win;
106         struct displaylist *displaylist;
107         int tracking_flag;
108         int orientation;
109         int recentdest_count;
110         int osd_configuration;
111         GList *vehicles;
112         GList *windows_items;
113         struct navit_vehicle *vehicle;
114         struct callback_list *attr_cbl;
115         struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb, *progress_cb;
116         struct datawindow *roadbook_window;
117         struct map *former_destination;
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,*predraw_callback;
137         struct vehicleprofile *vehicleprofile;
138         GList *vehicleprofiles;
139         int pitch;
140         int follow_cursor;
141         int prevTs;
142         int graphics_flags;
143         int zoom_min, zoom_max;
144         int radius;
145         struct bookmarks *bookmarks;
146         int flags;
147                  /* 1=No graphics ok */
148                  /* 2=No gui ok */
149         int border;
150         int imperial;
151 };
152
153 struct gui *main_loop_gui;
154
155 struct attr_iter {
156         union {
157                 GList *list;
158                 struct mapset_handle *mapset_handle;
159         } u;
160 };
161
162 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
163 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
164 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
165 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
166 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir);
167 static void navit_set_cursors(struct navit *this_);
168 static void navit_cmd_zoom_to_route(struct navit *this);
169 static void navit_cmd_set_center_cursor(struct navit *this_);
170 static void navit_cmd_announcer_toggle(struct navit *this_);
171 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
172
173 struct navit *global_navit;
174
175 void
176 navit_add_mapset(struct navit *this_, struct mapset *ms)
177 {
178         this_->mapsets = g_list_append(this_->mapsets, ms);
179 }
180
181 struct mapset *
182 navit_get_mapset(struct navit *this_)
183 {
184         if(this_->mapsets){
185                 return this_->mapsets->data;
186         } else {
187                 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
188         }
189         return NULL;
190 }
191
192 struct tracking *
193 navit_get_tracking(struct navit *this_)
194 {
195         return this_->tracking;
196 }
197
198 /**
199  * @brief       Get the user data directory.
200  * @param[in]    create - create the directory if it does not exist
201  *
202  * @return      char * to the data directory string.
203  *
204  * returns the directory used to store user data files (center.txt,
205  * destination.txt, bookmark.txt, ...)
206  *
207  */
208 char*
209 navit_get_user_data_directory(int create) {
210         char *dir;
211         dir = getenv("NAVIT_USER_DATADIR");
212         if (create && !file_exists(dir)) {
213                 dbg(0,"creating dir %s\n", dir);
214                 if (file_mkdir(dir,0)) {
215                         dbg(0,"failed creating dir %s\n", dir);
216                         return NULL;
217                 }
218         }
219         return dir;
220 } /* end: navit_get_user_data_directory(gboolean create) */
221
222
223 void
224 navit_draw_async(struct navit *this_, int async)
225 {
226
227         if (this_->blocked) {
228                 this_->blocked |= 2;
229                 return;
230         }
231         transform_setup_source_rect(this_->trans);
232         graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
233 }
234
235 void
236 navit_draw(struct navit *this_)
237 {
238         if (this_->ready == 3)
239                 navit_draw_async(this_, 0);
240 }
241
242 int
243 navit_get_ready(struct navit *this_)
244 {
245         return this_->ready;
246 }
247
248
249
250 void
251 navit_draw_displaylist(struct navit *this_)
252 {
253         if (this_->ready == 3)
254                 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
255 }
256
257 static void
258 navit_map_progress(struct navit *this_)
259 {
260         struct map *map;
261         struct mapset *ms;
262         struct mapset_handle *msh;
263         struct attr attr;
264         struct point p;
265         if (this_->ready != 3)
266                 return;
267         p.x=10;
268         p.y=32;
269
270         ms=this_->mapsets->data;
271         msh=mapset_open(ms);
272         while (msh && (map=mapset_next(msh, 0))) {
273                 if (map_get_attr(map, attr_progress, &attr, NULL)) {
274                         char *str=g_strdup_printf("%s           ",attr.u.str);
275                         graphics_draw_mode(this_->gra, draw_mode_begin);
276                         graphics_draw_text_std(this_->gra, 16, str, &p);
277                         g_free(str);
278                         p.y+=32;
279                         graphics_draw_mode(this_->gra, draw_mode_end);
280                 }
281         }
282         mapset_close(msh);
283 }
284
285 static void
286 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
287 {
288         int updated;
289         if (attr->type != attr_route_status)
290                 return;
291         updated=attr->u.num;
292         if (this_->ready != 3)
293                 return;
294         if (updated != route_status_path_done_new)
295                 return;
296         if (this_->vehicle) {
297                 if (this_->vehicle->follow_curr == 1)
298                         return;
299                 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
300                         this_->vehicle->follow_curr=this_->vehicle->follow;
301         }
302         navit_draw(this_);
303 }
304
305 void
306 navit_handle_resize(struct navit *this_, int w, int h)
307 {
308         struct map_selection sel;
309         int callback=(this_->ready == 1);
310         this_->ready |= 2;
311         memset(&sel, 0, sizeof(sel));
312         this_->w=w;
313         this_->h=h;
314         sel.u.p_rect.rl.x=w;
315         sel.u.p_rect.rl.y=h;
316         transform_set_screen_selection(this_->trans, &sel);
317         graphics_init(this_->gra);
318         graphics_set_rect(this_->gra, &sel.u.p_rect);
319         if (callback) 
320                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
321         if (this_->ready == 3)
322                 navit_draw_async(this_, 1);
323 }
324
325 static void
326 navit_resize(void *data, int w, int h)
327 {
328         struct navit *this=data;
329         if (!this->ignore_graphics_events)
330                 navit_handle_resize(this, w, h);
331 }
332
333 int
334 navit_get_width(struct navit *this_)
335 {
336         return this_->w;
337 }
338
339
340 int
341 navit_get_height(struct navit *this_)
342 {
343         return this_->h;
344 }
345
346 static void
347 navit_popup(void *data)
348 {
349         struct navit *this_=data;
350         popup(this_, 1, &this_->pressed);
351         this_->button_timeout=NULL;
352         this_->popped=1;
353 }
354
355
356 int
357 navit_ignore_button(struct navit *this_)
358 {
359         if (this_->ignore_button)
360                 return 1;
361         this_->ignore_button=1;
362         return 0;
363 }
364
365 void
366 navit_ignore_graphics_events(struct navit *this_, int ignore)
367 {
368         this_->ignore_graphics_events=ignore;
369 }
370
371 static void
372 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
373 {
374         struct coord co,cn;
375         struct coord c,*cp;
376         int yaw;
377         double angleo,anglen;
378
379         if (!transform_reverse(tr, old, &co))
380                 return;
381         if (rot) {
382                 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
383                 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
384                 yaw=transform_get_yaw(tr)+angleo-anglen;
385                 transform_set_yaw(tr, yaw % 360);
386         }
387         if (!transform_reverse(tr, new, &cn))
388                 return;
389         cp=transform_get_center(tr);
390         c.x=cp->x+co.x-cn.x;
391         c.y=cp->y+co.y-cn.y;
392         dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
393         transform_set_center(tr, &c);
394 }
395
396 void
397 navit_set_timeout(struct navit *this_)
398 {
399         struct attr follow;
400         follow.type=attr_follow;
401         follow.u.num=this_->center_timeout;
402         navit_set_attr(this_, &follow);
403 }
404
405 int
406 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
407 {
408         int border=16;
409
410         dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
411         callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
412         if (this_->ignore_button) {
413                 this_->ignore_button=0;
414                 return 0;
415         }
416         if (pressed) {
417                 this_->pressed=*p;
418                 this_->last=*p;
419                 this_->zoomed=0;
420                 if (button == 1) {
421                         this_->button_pressed=1;
422                         this_->moved=0;
423                         this_->popped=0;
424                         if (popup_callback)
425                                 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
426                 }
427                 if (button == 2)
428                         navit_set_center_screen(this_, p, 1);
429                 if (button == 3)
430                         popup(this_, button, p);
431                 if (button == 4 && this_->use_mousewheel) {
432                         this_->zoomed = 1;
433                         navit_zoom_in(this_, 2, p);
434                 }
435                 if (button == 5 && this_->use_mousewheel) {
436                         this_->zoomed = 1;
437                         navit_zoom_out(this_, 2, p);
438                 }
439         } else {
440
441                 this_->button_pressed=0;
442                 if (this_->button_timeout) {
443                         event_remove_timeout(this_->button_timeout);
444                         this_->button_timeout=NULL;
445                         if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
446                                 navit_set_center_screen(this_, p, !this_->zoomed);
447                         }
448                 }
449                 if (this_->motion_timeout) {
450                         event_remove_timeout(this_->motion_timeout);
451                         this_->motion_timeout=NULL;
452                 }
453                 if (this_->moved) {
454                         struct point pr;
455                         pr.x=this_->w/2;
456                         pr.y=this_->h;
457 #if 0
458                         update_transformation(this_->trans, &this_->pressed, p, &pr);
459 #else
460                         update_transformation(this_->trans, &this_->pressed, p, NULL);
461 #endif
462                         graphics_draw_drag(this_->gra, NULL);
463                         transform_copy(this_->trans, this_->trans_cursor);
464                         graphics_overlay_disable(this_->gra, 0);
465                         if (!this_->zoomed) 
466                                 navit_set_timeout(this_);
467                         navit_draw(this_);
468                 } else
469                         return 1;
470         }
471         return 0;
472 }
473
474 static void
475 navit_button(void *data, int pressed, int button, struct point *p)
476 {
477         struct navit *this=data;
478         dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
479         if (!this->ignore_graphics_events) {
480                 if (! this->popup_callback)
481                         this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
482                 navit_handle_button(this, pressed, button, p, this->popup_callback);
483         }
484 }
485
486
487 static void
488 navit_motion_timeout(struct navit *this_)
489 {
490         int dx, dy;
491
492         if (this_->drag_bitmap) {
493                 struct point point;
494                 point.x=(this_->current.x-this_->pressed.x);
495                 point.y=(this_->current.y-this_->pressed.y);
496                 if (graphics_draw_drag(this_->gra, &point)) {
497                         graphics_overlay_disable(this_->gra, 1);
498                         graphics_draw_mode(this_->gra, draw_mode_end);
499                         this_->moved=1;
500                         this_->motion_timeout=NULL;
501                         return;
502                 }
503         } 
504         dx=(this_->current.x-this_->last.x);
505         dy=(this_->current.y-this_->last.y);
506         if (dx || dy) {
507                 struct transformation *tr;
508                 struct point pr;
509                 this_->last=this_->current;
510                 graphics_overlay_disable(this_->gra, 1);
511                 tr=transform_dup(this_->trans);
512                 pr.x=this_->w/2;
513                 pr.y=this_->h;
514 #if 0
515                 update_transformation(tr, &this_->pressed, &this_->current, &pr);
516 #else
517                 update_transformation(tr, &this_->pressed, &this_->current, NULL);
518 #endif
519 #if 0
520                 graphics_displaylist_move(this_->displaylist, dx, dy);
521 #endif
522                 graphics_draw_cancel(this_->gra, this_->displaylist);
523                 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
524                 transform_destroy(tr);
525                 this_->moved=1;
526         }
527         this_->motion_timeout=NULL;
528         return;
529 }
530
531 void
532 navit_handle_motion(struct navit *this_, struct point *p)
533 {
534         int dx, dy;
535
536         if (this_->button_pressed && !this_->popped) {
537                 dx=(p->x-this_->pressed.x);
538                 dy=(p->y-this_->pressed.y);
539                 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
540                         this_->moved=1;
541                         if (this_->button_timeout) {
542                                 event_remove_timeout(this_->button_timeout);
543                                 this_->button_timeout=NULL;
544                         }
545                         this_->current=*p;
546                         if (! this_->motion_timeout_callback)
547                                 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
548                         if (! this_->motion_timeout)
549                                 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
550                 }
551         }
552 }
553
554 static void
555 navit_motion(void *data, struct point *p)
556 {
557         struct navit *this=data;
558         if (!this->ignore_graphics_events) 
559                 navit_handle_motion(this, p);
560 }
561
562 static void
563 navit_predraw(struct navit *this_)
564 {
565         GList *l;
566         struct navit_vehicle *nv;
567         transform_copy(this_->trans, this_->trans_cursor);
568         l=this_->vehicles;
569         while (l) {
570                 nv=l->data;
571                 navit_vehicle_draw(this_, nv, NULL);
572                 l=g_list_next(l);
573         }
574 }
575
576 static void
577 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
578 {
579         struct coord c1, c2, *center;
580         if (scale < this_->zoom_min)
581                 scale=this_->zoom_min;
582         if (scale > this_->zoom_max)
583                 scale=this_->zoom_max;
584         if (p)
585                 transform_reverse(this_->trans, p, &c1);
586         transform_set_scale(this_->trans, scale);
587         if (p) {
588                 transform_reverse(this_->trans, p, &c2);
589                 center = transform_center(this_->trans);
590                 center->x += c1.x - c2.x;
591                 center->y += c1.y - c2.y;
592         }
593         if (draw)
594                 navit_draw(this_);
595 }
596
597 /**
598  * @brief Automatically adjusts zoom level
599  *
600  * This function automatically adjusts the current
601  * zoom level according to the current speed.
602  *
603  * @param this_ The navit struct
604  * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
605  * @param speed The vehicles speed in meters per second
606  * @param dir The direction into which the vehicle moves
607  */
608 static void
609 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
610 {
611         struct point pc;
612         int distance,w,h;
613         double new_scale;
614         long scale;
615
616         if (! this_->autozoom_active) {
617                 return;
618         }
619
620         distance = speed * this_->autozoom_secs;
621
622         transform_get_size(this_->trans, &w, &h);
623         transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
624         scale = transform_get_scale(this_->trans);
625
626         /* We make sure that the point we want to see is within a certain range
627          * around the vehicle. The radius of this circle is the size of the
628          * screen. This doesn't necessarily mean the point is visible because of
629          * perspective etc. Quite rough, but should be enough. */
630         
631         if (w > h) {
632                 new_scale = (double)distance / h * 16; 
633         } else {
634                 new_scale = (double)distance / w * 16; 
635         }
636
637         if (abs(new_scale - scale) < 2) { 
638                 return; // Smoothing
639         }
640         
641         if (new_scale >= this_->autozoom_min) {
642                 navit_scale(this_, (long)new_scale, &pc, 0);
643         } else {
644                 if (scale != this_->autozoom_min) {
645                         navit_scale(this_, this_->autozoom_min, &pc, 0);
646                 }
647         }
648 }
649
650 /**
651  * Change the current zoom level, zooming closer to the ground
652  *
653  * @param navit The navit instance
654  * @param factor The zoom factor, usually 2
655  * @param p The invariant point (if set to NULL, default to center)
656  * @returns nothing
657  */
658 void
659 navit_zoom_in(struct navit *this_, int factor, struct point *p)
660 {
661         long scale=transform_get_scale(this_->trans)/factor;
662         if (scale < 1)
663                 scale=1;
664         navit_scale(this_, scale, p, 1);
665 }
666
667 /**
668  * Change the current zoom level
669  *
670  * @param navit The navit instance
671  * @param factor The zoom factor, usually 2
672  * @param p The invariant point (if set to NULL, default to center)
673  * @returns nothing
674  */
675 void
676 navit_zoom_out(struct navit *this_, int factor, struct point *p)
677 {
678         long scale=transform_get_scale(this_->trans)*factor;
679         navit_scale(this_, scale, p, 1);
680 }
681
682 void
683 navit_zoom_in_cursor(struct navit *this_, int factor)
684 {
685         struct point p;
686         if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
687                 navit_zoom_in(this_, factor, &p);
688                 this_->vehicle->follow_curr=this_->vehicle->follow;
689         } else
690                 navit_zoom_in(this_, factor, NULL);
691 }
692
693 void
694 navit_zoom_out_cursor(struct navit *this_, int factor)
695 {
696         struct point p;
697         if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
698                 navit_zoom_out(this_, 2, &p);
699                 this_->vehicle->follow_curr=this_->vehicle->follow;
700         } else
701                 navit_zoom_out(this_, 2, NULL);
702 }
703
704 static int
705 navit_cmd_zoom_in(struct navit *this_)
706 {
707         navit_zoom_in_cursor(this_, 2);
708         return 0;
709 }
710
711 static int
712 navit_cmd_zoom_out(struct navit *this_)
713 {
714         navit_zoom_out_cursor(this_, 2);
715         return 0;
716 }
717
718
719 static void
720 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
721 {
722         if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) 
723                 navit_say(this, in[0]->u.str);
724 }
725
726 static GHashTable *cmd_int_var_hash = NULL;
727
728 /**
729  * Store key value pair for the  command system
730  *
731  * @param navit The navit instance
732  * @param function unused (needed to match command function signiture)
733  * @param in input attributes in[0] is the key string, in[1] is the integer value to store
734  * @param out output attributes, unused 
735  * @param valid unused 
736  * @returns nothing
737  */
738 static void
739 navit_cmd_set_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
740 {
741         if(!cmd_int_var_hash) {
742                 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
743         }
744
745         if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
746              (in && in[1] && ATTR_IS_NUMERIC(in[1]->type))) {
747                 char*key;
748                 struct attr*val = g_new(struct attr,1);
749                 attr_dup_content(in[1],val);
750                 key = g_strdup(in[0]->u.str);
751                 g_hash_table_insert(cmd_int_var_hash, key, val);
752         }
753 }
754
755 /**
756  * command to toggle the active state of a named layer of the current layout
757  *
758  * @param navit The navit instance
759  * @param function unused (needed to match command function signiture)
760  * @param in input attribute in[0] is the name of the layer
761  * @param out output unused
762  * @param valid unused 
763  * @returns nothing
764  */
765 static void
766 navit_cmd_toggle_layer(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
767 {
768         if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
769                 if(this->layout_current && this->layout_current->layers) {
770                         GList* layers = this->layout_current->layers;
771                         while (layers) {
772                                 struct layer*l=layers->data;
773                                 if(l && !strcmp(l->name,in[0]->u.str) ) {
774                                         l->active ^= 1;
775                                         navit_draw(this);
776                                         return;
777                                 }
778                                 layers=g_list_next(layers);
779                         }
780                 }
781         }
782 }
783
784
785 /**
786  * Get value given a key string for the command system
787  *
788  * @param navit The navit instance
789  * @param function unused (needed to match command function signiture)
790  * @param in input attribute in[0] is the key string
791  * @param out output attribute, the value for the given key string if exists or 0  
792  * @param valid unused 
793  * @returns nothing
794  */
795 static void
796 navit_cmd_get_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
797 {
798         struct attr **list = g_new0(struct attr *,2);
799         if(!cmd_int_var_hash) {
800                 struct attr*val = g_new0(struct attr,1);
801                 val->type   = attr_type_int_begin;
802                 val->u.num  = 0;
803                 list[0]     = val;
804         }
805         if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
806                 struct attr*ret = g_hash_table_lookup(cmd_int_var_hash, in[0]->u.str);
807                 if(ret) {
808                         list[0] = ret;
809                 }
810                 else {
811                         struct attr*val = g_new0(struct attr,1);
812                         val->type   = attr_type_int_begin;
813                         val->u.num  = 0;
814                         list[0]   = val;
815                 }
816         }
817         list[1] = NULL;
818         *out = list;
819 }
820
821 GList *cmd_int_var_stack = NULL;
822
823 /**
824  * Push an integer to the stack for the command system
825  *
826  * @param navit The navit instance
827  * @param function unused (needed to match command function signiture)
828  * @param in input attribute in[0] is the integer attibute to push
829  * @param out output attributes, unused 
830  * @param valid unused 
831  * @returns nothing
832  */
833 static void
834 navit_cmd_push_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
835 {
836         if (in && in[0] && ATTR_IS_NUMERIC(in[0]->type)) {
837                 struct attr*val = g_new(struct attr,1);
838                 attr_dup_content(in[0],val);
839                 cmd_int_var_stack = g_list_prepend(cmd_int_var_stack, val);
840         }
841 }
842
843 /**
844  * Pop an integer from the command system's integer stack
845  *
846  * @param navit The navit instance
847  * @param function unused (needed to match command function signiture)
848  * @param in input attributes unused
849  * @param out output attribute, the value popped if stack isn't empty or 0
850  * @param valid unused 
851  * @returns nothing
852  */
853 static void
854 navit_cmd_pop_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
855 {
856         struct attr **list = g_new0(struct attr *,2);
857         if(!cmd_int_var_stack) {
858                 struct attr*val = g_new0(struct attr,1);
859                 val->type = attr_type_int_begin;
860                 val->u.num  = 0;
861                 list[0]   = val;
862         }
863         else {
864                 list[0] = cmd_int_var_stack->data;
865                 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
866         }
867         list[1] = NULL;
868         *out = list;
869 }
870
871 /**
872  * Get current size of command system's integer stack
873  *
874  * @param navit The navit instance
875  * @param function unused (needed to match command function signiture)
876  * @param in input attributes unused
877  * @param out output attribute, the size of stack
878  * @param valid unused 
879  * @returns nothing
880  */
881 static void
882 navit_cmd_int_stack_size(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
883 {
884         struct attr **list;
885         struct attr *attr  = g_new0(struct attr  ,1);
886         attr->type  = attr_type_int_begin;
887         if(!cmd_int_var_stack) {
888                 attr->u.num = 0; 
889         }
890         else {
891                 attr->u.num = g_list_length(cmd_int_var_stack); 
892         }
893         list = g_new0(struct attr *,2);
894         list[0] = attr;
895         list[1] = NULL;
896         *out = list;
897         cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
898 }
899
900 static void
901 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
902 {
903         struct pcoord pc;
904         char *description=NULL;
905         if (!in)
906                 return;
907         if (!in[0])
908                 return;
909         pc.pro = transform_get_projection(this->trans);
910         if (ATTR_IS_COORD(in[0]->type)) {
911                 pc.x=in[0]->u.coord->x;
912                 pc.y=in[0]->u.coord->y;
913                 in++;
914         } else if (ATTR_IS_PCOORD(in[0]->type)) {
915                 pc=*in[0]->u.pcoord;
916                 in++;
917         } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
918                 pc.pro=in[0]->u.num;
919                 pc.x=in[1]->u.num;
920                 pc.y=in[2]->u.num;
921                 in+=3;
922         } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
923                 pc.x=in[0]->u.num;
924                 pc.y=in[1]->u.num;
925                 in+=2;
926         } else
927                 return;
928         if (in[0] && ATTR_IS_STRING(in[0]->type))
929                 description=in[0]->u.str;
930         navit_set_destination(this, &pc, description, 1);
931 }
932
933 static void
934 navit_cmd_fmt_coordinates(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
935 {
936         struct attr attr;
937         attr.type=attr_type_string_begin;
938         attr.u.str="Fix me";
939         if (out) {
940                 *out=attr_generic_add_attr(*out, &attr);
941         }
942 }
943
944 static struct command_table commands[] = {
945         {"zoom_in",command_cast(navit_cmd_zoom_in)},
946         {"zoom_out",command_cast(navit_cmd_zoom_out)},
947         {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
948         {"say",command_cast(navit_cmd_say)},
949         {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
950         {"set_destination",command_cast(navit_cmd_set_destination)},
951         {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
952         {"fmt_coordinates",command_cast(navit_cmd_fmt_coordinates)},
953         {"set_int_var",command_cast(navit_cmd_set_int_var)},
954         {"get_int_var",command_cast(navit_cmd_get_int_var)},
955         {"push_int",command_cast(navit_cmd_push_int)},
956         {"pop_int",command_cast(navit_cmd_pop_int)},
957         {"int_stack_size",command_cast(navit_cmd_int_stack_size)},
958         {"toggle_layer",command_cast(navit_cmd_toggle_layer)},
959 };
960         
961 void 
962 navit_command_add_table(struct navit*this_, struct command_table *commands, int count)
963 {
964   command_add_table(this_->attr_cbl, commands, count, this_);
965 }
966
967 struct navit *
968 navit_new(struct attr *parent, struct attr **attrs)
969 {
970         struct navit *this_=g_new0(struct navit, 1);
971         struct pcoord center;
972         struct coord co;
973         struct coord_geo g;
974         enum projection pro=projection_mg;
975         int zoom = 256;
976         g.lat=53.13;
977         g.lng=11.70;
978
979         this_->self.type=attr_navit;
980         this_->self.u.navit=this_;
981         this_->attr_cbl=callback_list_new();
982
983         this_->orientation=-1;
984         this_->tracking_flag=1;
985         this_->recentdest_count=10;
986         this_->osd_configuration=-1;
987
988         this_->center_timeout = 10;
989         this_->use_mousewheel = 1;
990         this_->autozoom_secs = 10;
991         this_->autozoom_min = 7;
992         this_->autozoom_active = 0;
993         this_->zoom_min = 1;
994         this_->zoom_max = 2097152;
995         this_->follow_cursor = 1;
996         this_->radius = 30;
997         this_->border = 16;
998
999         this_->trans = transform_new();
1000         this_->trans_cursor = transform_new();
1001         transform_from_geo(pro, &g, &co);
1002         center.x=co.x;
1003         center.y=co.y;
1004         center.pro = pro;
1005         
1006         transform_setup(this_->trans, &center, zoom, (this_->orientation != -1) ? this_->orientation : 0);
1007
1008         this_->bookmarks=bookmarks_new(&this_->self, NULL, this_->trans);
1009
1010         this_->prevTs=0;
1011
1012         for (;*attrs; attrs++) {
1013                 navit_set_attr_do(this_, *attrs, 1);
1014         }
1015         this_->displaylist=graphics_displaylist_new();
1016         command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
1017
1018         this_->messages = messagelist_new(attrs);
1019         
1020         return this_;
1021 }
1022
1023 static int
1024 navit_set_gui(struct navit *this_, struct gui *gui)
1025 {
1026         if (this_->gui)
1027                 return 0;
1028         this_->gui=gui;
1029         if (gui_has_main_loop(this_->gui)) {
1030                 if (! main_loop_gui) {
1031                         main_loop_gui=this_->gui;
1032                 } else {
1033                         dbg(0,"gui with main loop already active, ignoring this instance");
1034                         return 0;
1035                 }
1036         }
1037         return 1;
1038 }
1039
1040 void 
1041 navit_add_message(struct navit *this_, char *message)
1042 {
1043         message_new(this_->messages, message);
1044 }
1045
1046 struct message
1047 *navit_get_messages(struct navit *this_)
1048 {
1049         return message_get(this_->messages);
1050 }
1051
1052 static int
1053 navit_set_graphics(struct navit *this_, struct graphics *gra)
1054 {
1055         if (this_->gra)
1056                 return 0;
1057         this_->gra=gra;
1058         this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
1059         graphics_add_callback(gra, this_->resize_callback);
1060         this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
1061         graphics_add_callback(gra, this_->button_callback);
1062         this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
1063         graphics_add_callback(gra, this_->motion_callback);
1064         this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
1065         graphics_add_callback(gra, this_->predraw_callback);
1066         return 1;
1067 }
1068
1069 struct graphics *
1070 navit_get_graphics(struct navit *this_)
1071 {
1072         return this_->gra;
1073 }
1074
1075 struct vehicleprofile *
1076 navit_get_vehicleprofile(struct navit *this_)
1077 {
1078         return this_->vehicleprofile;
1079 }
1080
1081 GList *
1082 navit_get_vehicleprofiles(struct navit *this_)
1083 {
1084         return this_->vehicleprofiles;
1085 }
1086
1087 static void
1088 navit_projection_set(struct navit *this_, enum projection pro, int draw)
1089 {
1090         struct coord_geo g;
1091         struct coord *c;
1092
1093         c=transform_center(this_->trans);
1094         transform_to_geo(transform_get_projection(this_->trans), c, &g);
1095         transform_set_projection(this_->trans, pro);
1096         transform_from_geo(pro, &g, c);
1097         if (draw)
1098                 navit_draw(this_);
1099 }
1100
1101 /**
1102  * Start the route computing to a given set of coordinates
1103  *
1104  * @param navit The navit instance
1105  * @param c The coordinate to start routing to
1106  * @param description A label which allows the user to later identify this destination in the former destinations selection
1107  * @returns nothing
1108  */
1109 void
1110 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1111 {
1112         char *destination_file;
1113         if (c) {
1114                 this_->destination=*c;
1115                 this_->destination_valid=1;
1116
1117             dbg(1, "navit->navit_set_destination %i\n", c->x);
1118             dbg(1, "navit->navit_set_destination %i\n", c->y);
1119
1120         } else
1121                 this_->destination_valid=0;
1122         destination_file = bookmarks_get_destination_file(TRUE);
1123         bookmarks_append_coord(this_->bookmarks, destination_file, c, 1, "former_destination", description, NULL, this_->recentdest_count);
1124         g_free(destination_file);
1125         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1126         if (this_->route) {
1127                 route_set_destination(this_->route, c, async);
1128
1129                 if (this_->ready == 3)
1130                         navit_draw(this_);
1131         }
1132 }
1133
1134 /**
1135  * Start the route computing to a given set of coordinates including waypoints
1136  *
1137  * @param navit The navit instance
1138  * @param c The coordinate to start routing to
1139  * @param description A label which allows the user to later identify this destination in the former destinations selection
1140  * @returns nothing
1141  */
1142 void
1143 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1144 {
1145         char *destination_file;
1146         if (c && count) {
1147                 this_->destination=c[count-1];
1148                 this_->destination_valid=1;
1149         } else
1150                 this_->destination_valid=0;
1151         destination_file = bookmarks_get_destination_file(TRUE);
1152         bookmarks_append_coord(this_->bookmarks, destination_file, c, count, "former_itinerary", description, NULL, this_->recentdest_count);
1153         g_free(destination_file);
1154         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1155         if (this_->route) {
1156                 route_set_destinations(this_->route, c, count, async);
1157
1158                 if (this_->ready == 3)
1159                         navit_draw(this_);
1160         }
1161 }
1162
1163 /**
1164  * @brief Checks if a route is calculated
1165  *
1166  * This function checks if a route is calculated.
1167  *
1168  * @param this_ The navit struct whose route should be checked.
1169  * @return True if the route is set, false otherwise.
1170  */
1171 int
1172 navit_check_route(struct navit *this_)
1173 {
1174         if (this_->route) {
1175                 return route_get_path_set(this_->route);
1176         }
1177
1178         return 0;
1179 }
1180
1181 static int
1182 navit_former_destinations_active(struct navit *this_)
1183 {
1184         char *destination_file = bookmarks_get_destination_file(FALSE);
1185         FILE *f;
1186         int active=0;
1187         char buffer[3];
1188         f=fopen(destination_file,"r");
1189         if (f) {
1190                 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n')) 
1191                         active=1;
1192                 fclose(f);
1193         }
1194         g_free(destination_file);
1195         return active;
1196 }
1197
1198 static void
1199 navit_add_former_destinations_from_file(struct navit *this_)
1200 {
1201         char *destination_file = bookmarks_get_destination_file(FALSE);
1202         struct attr *attrs[4];
1203         struct map_rect *mr;
1204         struct item *item;
1205         int i,valid=0,count=0;
1206         struct coord c[16];
1207         struct pcoord pc[16];
1208         struct attr parent;
1209         struct attr type;
1210         struct attr data;
1211         struct attr flags;
1212
1213         parent.type=attr_navit;
1214         parent.u.navit=this_;
1215
1216         type.type=attr_type;
1217         type.u.str="textfile";
1218
1219         data.type=attr_data;
1220         data.u.str=destination_file;
1221
1222         flags.type=attr_flags;
1223         flags.u.num=1;
1224
1225         attrs[0]=&type; attrs[1]=&data; attrs[2]=&flags; attrs[3]=NULL;
1226
1227         this_->former_destination=map_new(&parent, attrs);
1228         g_free(destination_file);
1229         if (!this_->route || !navit_former_destinations_active(this_))
1230                 return; 
1231         mr=map_rect_new(this_->former_destination, NULL);
1232         while ((item=map_rect_get_item(mr))) {
1233                 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16))) 
1234                         valid=1;
1235         }
1236         map_rect_destroy(mr);
1237         if (valid && count > 0) {
1238                 for (i = 0 ; i < count ; i++) {
1239                         pc[i].pro=map_projection(this_->former_destination);
1240                         pc[i].x=c[i].x;
1241                         pc[i].y=c[i].y;
1242                 }
1243                 if (count == 1)
1244                         route_set_destination(this_->route, &pc[0], 1);
1245                 else
1246                         route_set_destinations(this_->route, pc, count, 1);
1247                 this_->destination=pc[count-1];
1248                 this_->destination_valid=1;
1249         }
1250 }
1251
1252
1253 void
1254 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1255 {
1256         va_list ap;
1257         char *str1,*str2;
1258         va_start(ap, fmt);
1259         if (this_->textfile_debug_log && this_->vehicle) {
1260                 str1=g_strdup_vprintf(fmt, ap);
1261                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1262                 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1263                 g_free(str2);
1264                 g_free(str1);
1265         }
1266         va_end(ap);
1267 }
1268
1269 void
1270 navit_textfile_debug_log_at(struct navit *this_, struct pcoord *pc, const char *fmt, ...)
1271 {
1272         va_list ap;
1273         char *str1,*str2;
1274         va_start(ap, fmt);
1275         if (this_->textfile_debug_log && this_->vehicle) {
1276                 str1=g_strdup_vprintf(fmt, ap);
1277                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", pc->x, pc->y, strlen(str1) ? " " : "", str1);
1278                 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1279                 g_free(str2);
1280                 g_free(str1);
1281         }
1282         va_end(ap);
1283 }
1284
1285 void
1286 navit_say(struct navit *this_, char *text)
1287 {
1288         if(this_->speech) {
1289                 speech_say(this_->speech, text);
1290         }
1291 }
1292
1293 /**
1294  * @brief Toggles the navigation announcer for navit
1295  * @param this_ The navit object
1296  */
1297 static void
1298 navit_cmd_announcer_toggle(struct navit *this_)
1299 {
1300     struct attr attr, speechattr;
1301
1302     // search for the speech attribute
1303     if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1304         return;
1305     // find out if the corresponding attribute attr_active has been set
1306     if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1307         // flip it then...
1308         attr.u.num = !attr.u.num;
1309     } else {
1310         // otherwise disable it because voice is enabled by default
1311         attr.type = attr_active;
1312         attr.u.num = 0;
1313     }
1314
1315     // apply the new state
1316     if(!speech_set_attr(speechattr.u.speech, &attr))
1317         return;
1318
1319     // announce that the speech attribute has changed
1320     callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1321 }
1322
1323 void
1324 navit_speak(struct navit *this_)
1325 {
1326         struct navigation *nav=this_->navigation;
1327         struct map *map=NULL;
1328         struct map_rect *mr=NULL;
1329         struct item *item;
1330         struct attr attr;
1331
1332     if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1333         attr.u.num = 1;
1334     dbg(1, "this_.speech->active %i\n", attr.u.num);
1335     if(!attr.u.num)
1336         return;
1337
1338         if (nav)
1339                 map=navigation_get_map(nav);
1340         if (map)
1341                 mr=map_rect_new(map, NULL);
1342         if (mr) {
1343                 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1344                 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1345                         speech_say(this_->speech, attr.u.str);
1346                         navit_add_message(this_, attr.u.str);
1347                         navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1348                 }
1349                 map_rect_destroy(mr);
1350         }
1351 }
1352
1353 static void
1354 navit_window_roadbook_update(struct navit *this_)
1355 {
1356         struct navigation *nav=this_->navigation;
1357         struct map *map=NULL;
1358         struct map_rect *mr=NULL;
1359         struct item *item;
1360         struct attr attr;
1361         struct param_list param[5];
1362         int secs;
1363
1364         dbg(1,"enter\n");
1365         datawindow_mode(this_->roadbook_window, 1);
1366         if (nav)
1367                 map=navigation_get_map(nav);
1368         if (map)
1369                 mr=map_rect_new(map, NULL);
1370         dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1371         if (mr) {
1372                 dbg(0,"while loop\n");
1373                 while ((item=map_rect_get_item(mr))) {
1374                         dbg(0,"item=%p\n", item);
1375                         attr.u.str=NULL;
1376                         if (item->type != type_nav_position) {
1377                                 item_attr_get(item, attr_navigation_long, &attr);
1378                                 if (attr.u.str == NULL) {
1379                                         continue;
1380                                 }
1381                                 dbg(2, "Command='%s'\n", attr.u.str);
1382                                 param[0].value=g_strdup(attr.u.str);
1383                         } else
1384                                 param[0].value=_("Position");
1385                         param[0].name=_("Command");
1386
1387                         item_attr_get(item, attr_length, &attr);
1388                         dbg(2, "Length=%d\n", attr.u.num);
1389                         param[1].name=_("Length");
1390
1391                         if ( attr.u.num >= 2000 )
1392                         {
1393                                 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1394                         }
1395                         else
1396                         {
1397                                 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1398                         }
1399
1400                         item_attr_get(item, attr_time, &attr);
1401                         dbg(2, "Time=%d\n", attr.u.num);
1402                         secs=attr.u.num/10;
1403                         param[2].name=_("Time");
1404                         if ( secs >= 3600 )
1405                         {
1406                                 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1407                         }
1408                         else
1409                         {
1410                                 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1411                         }
1412
1413                         item_attr_get(item, attr_destination_length, &attr);
1414                         dbg(2, "Destlength=%d\n", attr.u.num);
1415                         param[3].name=_("Destination Length");
1416                         if ( attr.u.num >= 2000 )
1417                         {
1418                                 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1419                         }
1420                         else
1421                         {
1422                                 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1423                         }
1424
1425                         item_attr_get(item, attr_destination_time, &attr);
1426                         dbg(2, "Desttime=%d\n", attr.u.num);
1427                         secs=attr.u.num/10;
1428                         param[4].name=_("Destination Time");
1429                         if ( secs >= 3600 )
1430                         {
1431                                 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1432                         }
1433                         else
1434                         {
1435                                 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1436                         }
1437                         datawindow_add(this_->roadbook_window, param, 5);
1438                 }
1439                 map_rect_destroy(mr);
1440         }
1441         datawindow_mode(this_->roadbook_window, 0);
1442 }
1443
1444 void
1445 navit_window_roadbook_destroy(struct navit *this_)
1446 {
1447         dbg(0, "enter\n");
1448         navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1449         this_->roadbook_window=NULL;
1450         this_->roadbook_callback=NULL;
1451 }
1452 void
1453 navit_window_roadbook_new(struct navit *this_)
1454 {
1455         if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1456                 return;
1457         }
1458
1459         this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1460         navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1461         this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1462         navit_window_roadbook_update(this_);
1463 }
1464
1465 void
1466 navit_init(struct navit *this_)
1467 {
1468         struct mapset *ms;
1469         struct map *map;
1470         int callback;
1471         char *center_file;
1472
1473         dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1474
1475         if (!this_->gui && !(this_->flags & 2)) {
1476                 dbg(0,"no gui\n");
1477                 navit_destroy(this_);
1478                 return;
1479         }
1480         if (!this_->gra && !(this_->flags & 1)) {
1481                 dbg(0,"no graphics\n");
1482                 navit_destroy(this_);
1483                 return;
1484         }
1485         dbg(2,"Connecting gui to graphics\n");
1486         if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1487                 struct attr attr_type_gui, attr_type_graphics;
1488                 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1489                 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1490                 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1491                 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1492                 dbg(0," for explanations and solutions\n");
1493
1494                 navit_destroy(this_);
1495                 return;
1496         }
1497         if (this_->speech && this_->navigation) {
1498                 struct attr speech;
1499                 speech.type=attr_speech;
1500                 speech.u.speech=this_->speech;
1501                 navigation_set_attr(this_->navigation, &speech);
1502         }
1503         dbg(2,"Initializing graphics\n");
1504         dbg(2,"Setting Vehicle\n");
1505         navit_set_vehicle(this_, this_->vehicle);
1506         dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1507         if (this_->mapsets) {
1508                 struct mapset_handle *msh;
1509                 ms=this_->mapsets->data;
1510                 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1511                 msh=mapset_open(ms);
1512                 while (msh && (map=mapset_next(msh, 0))) {
1513                         map_add_callback(map, this_->progress_cb);
1514                 }
1515                 mapset_close(msh);
1516                 
1517                 if (this_->route) {
1518                         if ((map=route_get_map(this_->route))) {
1519                                 struct attr map_a;
1520                                 map_a.type=attr_map;
1521                                 map_a.u.map=map;
1522                                 mapset_add_attr(ms, &map_a);
1523                         }
1524                         if ((map=route_get_graph_map(this_->route))) {
1525                                 struct attr map_a,active;
1526                                 map_a.type=attr_map;
1527                                 map_a.u.map=map;
1528                                 active.type=attr_active;
1529                                 active.u.num=0;
1530                                 mapset_add_attr(ms, &map_a);
1531                                 map_set_attr(map, &active);
1532                         }
1533                         route_set_mapset(this_->route, ms);
1534                         route_set_projection(this_->route, transform_get_projection(this_->trans));
1535                 }
1536                 if (this_->tracking) {
1537                         tracking_set_mapset(this_->tracking, ms);
1538                         if (this_->route)
1539                                 tracking_set_route(this_->tracking, this_->route);
1540                 }
1541                 if (this_->navigation) {
1542                         if ((map=navigation_get_map(this_->navigation))) {
1543                                 struct attr map_a,active;
1544                                 map_a.type=attr_map;
1545                                 map_a.u.map=map;
1546                                 active.type=attr_active;
1547                                 active.u.num=0;
1548                                 mapset_add_attr(ms, &map_a);
1549                                 map_set_attr(map, &active);
1550                         }
1551                 }
1552                 if (this_->tracking) {
1553                         if ((map=tracking_get_map(this_->tracking))) {
1554                                 struct attr map_a,active;
1555                                 map_a.type=attr_map;
1556                                 map_a.u.map=map;
1557                                 active.type=attr_active;
1558                                 active.u.num=0;
1559                                 mapset_add_attr(ms, &map_a);
1560                                 map_set_attr(map, &active);
1561                         }
1562                 }
1563                 navit_add_former_destinations_from_file(this_);
1564         }
1565         if (this_->route) {
1566                 struct attr callback;
1567                 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1568                 callback.type=attr_callback;
1569                 callback.u.callback=this_->route_cb;
1570                 route_add_attr(this_->route, &callback);
1571         }
1572         if (this_->navigation) {
1573                 if (this_->speech) {
1574                         this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1575                         navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1576                 }
1577                 if (this_->route)
1578                         navigation_set_route(this_->navigation, this_->route);
1579         }
1580         dbg(2,"Setting Center\n");
1581         center_file = bookmarks_get_center_file(FALSE);
1582         bookmarks_set_center_from_file(this_->bookmarks, center_file);
1583         g_free(center_file);
1584 #if 0
1585         if (this_->menubar) {
1586                 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1587                 if (men) {
1588                         navit_add_menu_windows_items(this_, men);
1589                 }
1590         }
1591 #endif
1592         global_navit=this_;
1593 #if 0
1594         navit_window_roadbook_new(this_);
1595         navit_window_items_new(this_);
1596 #endif
1597
1598         messagelist_init(this_->messages);
1599
1600         navit_set_cursors(this_);
1601
1602         callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1603         callback=(this_->ready == 2);
1604         this_->ready|=1;
1605         dbg(2,"ready=%d\n",this_->ready);
1606         if (this_->ready == 3)
1607                 navit_draw_async(this_, 1);
1608         if (callback)
1609                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1610 #if 0
1611         routech_test(this_);
1612 #endif
1613 }
1614
1615 void
1616 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
1617 {
1618         struct coord c;
1619         int scale=16;
1620
1621         c.x=(r->rl.x+r->lu.x)/2;
1622         c.y=(r->rl.y+r->lu.y)/2;
1623         transform_set_center(this_->trans, &c);
1624         dbg(1,"%x,%x-%x,%x\n", r->rl.x,r->rl.y,r->lu.x,r->lu.y);
1625         while (scale < 1<<20) {
1626                 struct point p1,p2;
1627                 transform_set_scale(this_->trans, scale);
1628                 transform_setup_source_rect(this_->trans);
1629                 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
1630                 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
1631                 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1632                 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1633                     p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1634                         scale*=2;
1635                 else
1636                         break;
1637         
1638         }
1639         if (this_->ready == 3)
1640                 navit_draw_async(this_,0);
1641 }
1642
1643 void
1644 navit_zoom_to_route(struct navit *this_, int orientation)
1645 {
1646         struct map *map;
1647         struct map_rect *mr=NULL;
1648         struct item *item;
1649         struct coord c;
1650         struct coord_rect r;
1651         int count=0;
1652         if (! this_->route)
1653                 return;
1654         dbg(1,"enter\n");
1655         map=route_get_map(this_->route);
1656         dbg(1,"map=%p\n",map);
1657         if (map)
1658                 mr=map_rect_new(map, NULL);
1659         dbg(1,"mr=%p\n",mr);
1660         if (mr) {
1661                 while ((item=map_rect_get_item(mr))) {
1662                         dbg(1,"item=%s\n", item_to_name(item->type));
1663                         while (item_coord_get(item, &c, 1)) {
1664                                 dbg(1,"coord\n");
1665                                 if (!count) 
1666                                         r.lu=r.rl=c;
1667                                 else
1668                                         coord_rect_extend(&r, &c);      
1669                                 count++;
1670                         }
1671                 }
1672         }
1673         if (! count)
1674                 return;
1675         if (orientation != -1)
1676                 transform_set_yaw(this_->trans, orientation);
1677         navit_zoom_to_rect(this_, &r);
1678 }
1679
1680 static void
1681 navit_cmd_zoom_to_route(struct navit *this)
1682 {
1683         navit_zoom_to_route(this, 0);
1684 }
1685
1686
1687 /**
1688  * Change the current zoom level
1689  *
1690  * @param navit The navit instance
1691  * @param center The point where to center the map, including its projection
1692  * @returns nothing
1693  */
1694 void
1695 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1696 {
1697         struct coord *c=transform_center(this_->trans);
1698         struct coord c1,c2;
1699         enum projection pro = transform_get_projection(this_->trans);
1700         if (pro != center->pro) {
1701                 c1.x = center->x;
1702                 c1.y = center->y;
1703                 transform_from_to(&c1, center->pro, &c2, pro);
1704         } else {
1705                 c2.x = center->x;
1706                 c2.y = center->y;
1707         }
1708         *c=c2;
1709         if (set_timeout) 
1710                 navit_set_timeout(this_);
1711         if (this_->ready == 3)
1712                 navit_draw(this_);
1713 }
1714
1715 static void
1716 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1717 {
1718         int width, height;
1719         struct point po;
1720         transform_set_center(this_->trans, c);
1721         transform_get_size(this_->trans, &width, &height);
1722         po.x=width/2;
1723         po.y=height/2;
1724         update_transformation(this_->trans, &po, p, NULL);
1725         if (set_timeout)
1726                 navit_set_timeout(this_);
1727 }
1728
1729 /**
1730  * Links all vehicles to a cursor depending on the current profile.
1731  *
1732  * @param this_ A navit instance
1733  * @author Ralph Sennhauser (10/2009)
1734  */
1735 static void
1736 navit_set_cursors(struct navit *this_)
1737 {
1738         struct attr name;
1739         struct navit_vehicle *nv;
1740         struct cursor *c;
1741         GList *v;
1742
1743         v=g_list_first(this_->vehicles); // GList of navit_vehicles
1744         while (v) {
1745                 nv=v->data;
1746                 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
1747                         if (!strcmp(name.u.str,"none"))
1748                                 c=NULL;
1749                         else
1750                                 c=layout_get_cursor(this_->layout_current, name.u.str);
1751                 } else
1752                         c=layout_get_cursor(this_->layout_current, "default");
1753                 vehicle_set_cursor(nv->vehicle, c, 0);
1754                 v=g_list_next(v);
1755         }
1756         return;
1757 }
1758
1759 static int
1760 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
1761 {
1762         int width, height;
1763         struct navit_vehicle *nv=this_->vehicle;
1764
1765         float offset=this_->radius;      // Cursor offset from the center of the screen (percent).
1766 #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 */
1767         float min_offset = 0.;      // Percent offset at min_offset_speed.
1768         float max_offset = 30.;     // Percent offset at max_offset_speed.
1769         int min_offset_speed = 2;   // Speed in km/h
1770         int max_offset_speed = 50;  // Speed ini km/h
1771         // Calculate cursor offset from the center of the screen, upon speed.
1772         if (nv->speed <= min_offset_speed) {
1773             offset = min_offset;
1774         } else if (nv->speed > max_offset_speed) {
1775             offset = max_offset;
1776         } else {
1777             offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1778         }
1779 #endif
1780
1781         transform_get_size(this_->trans, &width, &height);
1782         if (this_->orientation == -1 || keep_orientation) {
1783                 p->x=50*width/100;
1784                 p->y=(50 + offset)*height/100;
1785                 if (dir) 
1786                         *dir=keep_orientation?this_->orientation:nv->dir;
1787         } else {
1788                 int mdir;
1789                 if (this_->tracking && this_->tracking_flag) {
1790                         mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1791                 } else {
1792                         mdir=nv->dir-this_->orientation;
1793                 }
1794
1795                 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1796                 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1797                 if (dir)
1798                         *dir=this_->orientation;
1799         }
1800         return 1;
1801 }
1802
1803 void
1804 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
1805 {
1806         int dir;
1807         struct point pn;
1808         struct navit_vehicle *nv=this_->vehicle;
1809         navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
1810         transform_set_yaw(this_->trans, dir);
1811         navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1812         if (autozoom)
1813                 navit_autozoom(this_, &nv->coord, nv->speed, 0);
1814 }
1815
1816 static void
1817 navit_set_center_cursor_draw(struct navit *this_)
1818 {
1819         navit_set_center_cursor(this_,1,0);
1820         if (this_->ready == 3)
1821                 navit_draw_async(this_, 1);
1822 }
1823
1824 static void
1825 navit_cmd_set_center_cursor(struct navit *this_)
1826 {
1827         navit_set_center_cursor_draw(this_);
1828 }
1829
1830 void
1831 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1832 {
1833         struct coord c;
1834         struct pcoord pc;
1835         transform_reverse(this_->trans, p, &c);
1836         pc.x = c.x;
1837         pc.y = c.y;
1838         pc.pro = transform_get_projection(this_->trans);
1839         navit_set_center(this_, &pc, set_timeout);
1840 }
1841
1842 #if 0
1843                 switch((*attrs)->type) {
1844                 case attr_zoom:
1845                         zoom=(*attrs)->u.num;
1846                         break;
1847                 case attr_center:
1848                         g=*((*attrs)->u.coord_geo);
1849                         break;
1850 #endif
1851
1852 static int
1853 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1854 {
1855         int dir=0, orient_old=0, attr_updated=0;
1856         struct coord co;
1857         long zoom;
1858         GList *l;
1859         struct navit_vehicle *nv;
1860         struct layout *lay;
1861         struct attr active;
1862         active.type=attr_active;
1863         active.u.num=0;
1864
1865         switch (attr->type) {
1866         case attr_autozoom:
1867                 attr_updated=(this_->autozoom_secs != attr->u.num);
1868                 this_->autozoom_secs = attr->u.num;
1869                 break;
1870         case attr_autozoom_active:
1871                 attr_updated=(this_->autozoom_active != attr->u.num);
1872                 this_->autozoom_active = attr->u.num;
1873                 break;
1874         case attr_center:
1875                 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1876                 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1877                 transform_set_center(this_->trans, &co);
1878                 break;
1879         case attr_drag_bitmap:
1880                 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1881                 this_->drag_bitmap=!!attr->u.num;
1882                 break;
1883         case attr_flags:
1884                 attr_updated=(this_->flags != attr->u.num);
1885                 this_->flags=attr->u.num;
1886                 break;
1887         case attr_flags_graphics:
1888                 attr_updated=(this_->graphics_flags != attr->u.num);
1889                 this_->graphics_flags=attr->u.num;
1890                 break;
1891         case attr_follow:
1892                 if (!this_->vehicle)
1893                         return 0;
1894                 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1895                 this_->vehicle->follow_curr = attr->u.num;
1896                 break;
1897         case attr_layout:
1898                 if(this_->layout_current!=attr->u.layout) {
1899                         this_->layout_current=attr->u.layout;
1900                         graphics_font_destroy_all(this_->gra);
1901                         navit_set_cursors(this_);
1902                         if (this_->ready == 3)
1903                                 navit_draw(this_);
1904                         attr_updated=1;
1905                 }
1906                 break;
1907         case attr_layout_name:
1908                 l=this_->layouts;
1909                 while (l) {
1910                         lay=l->data;
1911                         if (!strcmp(lay->name,attr->u.str)) {
1912                                 struct attr attr;
1913                                 attr.type=attr_layout;
1914                                 attr.u.layout=lay;
1915                                 return navit_set_attr_do(this_, &attr, init);
1916                         }
1917                         l=g_list_next(l);
1918                 }               
1919                 return 0;
1920         case attr_map_border:
1921                 if (this_->border != attr->u.num) {
1922                         this_->border=attr->u.num;
1923                         attr_updated=1;
1924                 }
1925                 break;
1926         case attr_orientation:
1927                 orient_old=this_->orientation;
1928                 this_->orientation=attr->u.num;
1929                 if (!init) {
1930                         if (this_->orientation != -1) {
1931                                 dir = this_->orientation;
1932                         } else {
1933                                 if (this_->vehicle) {
1934                                         dir = this_->vehicle->dir;
1935                                 }
1936                         }
1937                         transform_set_yaw(this_->trans, dir);
1938                         if (orient_old != this_->orientation) {
1939 #if 0
1940                                 if (this_->ready == 3)
1941                                         navit_draw(this_);
1942 #endif
1943                                 attr_updated=1;
1944                         }
1945                 }
1946                 break;
1947         case attr_osd_configuration:
1948                 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1949                 attr_updated=(this_->osd_configuration != attr->u.num);
1950                 this_->osd_configuration=attr->u.num;
1951                 break;
1952         case attr_pitch:
1953                 attr_updated=(this_->pitch != attr->u.num);
1954                 this_->pitch=attr->u.num;
1955                 transform_set_pitch(this_->trans, this_->pitch);
1956                 if (!init && attr_updated && this_->ready == 3)
1957                         navit_draw(this_);
1958                 break;
1959         case attr_projection:
1960                 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1961                         navit_projection_set(this_, attr->u.projection, !init);
1962                         attr_updated=1;
1963                 }
1964                 break;
1965         case attr_radius:
1966                 attr_updated=(this_->radius != attr->u.num);
1967                 this_->radius=attr->u.num;
1968                 break;
1969         case attr_recent_dest:
1970                 attr_updated=(this_->recentdest_count != attr->u.num);
1971                 this_->recentdest_count=attr->u.num;
1972                 break;
1973         case attr_speech:
1974                 if(this_->speech && this_->speech != attr->u.speech) {
1975                         attr_updated=1;
1976                         this_->speech = attr->u.speech;
1977                 }
1978                 break;
1979         case attr_timeout:
1980                 attr_updated=(this_->center_timeout != attr->u.num);
1981                 this_->center_timeout = attr->u.num;
1982                 break;
1983         case attr_tracking:
1984                 attr_updated=(this_->tracking_flag != !!attr->u.num);
1985                 this_->tracking_flag=!!attr->u.num;
1986                 break;
1987         case attr_transformation:
1988                 this_->trans=attr->u.transformation;
1989                 break;
1990         case attr_use_mousewheel:
1991                 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1992                 this_->use_mousewheel=!!attr->u.num;
1993                 break;
1994         case attr_vehicle:
1995                 l=this_->vehicles;
1996                 while(l) {
1997                         nv=l->data;
1998                         if (nv->vehicle == attr->u.vehicle) {
1999                                 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
2000                                         if (this_->vehicle)
2001                                                 vehicle_set_attr(this_->vehicle->vehicle, &active);
2002                                         active.u.num=1;
2003                                         vehicle_set_attr(nv->vehicle, &active);
2004                                         attr_updated=1;
2005                                 }
2006                                 navit_set_vehicle(this_, nv);
2007                         }
2008                         l=g_list_next(l);
2009                 }
2010                 break;
2011         case attr_zoom:
2012                 zoom=transform_get_scale(this_->trans);
2013                 attr_updated=(zoom != attr->u.num);
2014                 transform_set_scale(this_->trans, attr->u.num);
2015                 if (attr_updated && !init) 
2016                         navit_draw(this_);
2017                 break;
2018         case attr_zoom_min:
2019                 attr_updated=(attr->u.num != this_->zoom_min);
2020                 this_->zoom_min=attr->u.num;
2021                 break;
2022         case attr_zoom_max:
2023                 attr_updated=(attr->u.num != this_->zoom_max);
2024                 this_->zoom_max=attr->u.num;
2025                 break;
2026         case attr_message:
2027                 navit_add_message(this_, attr->u.str);
2028                 break;
2029         case attr_follow_cursor:
2030                 attr_updated=(this_->follow_cursor != !!attr->u.num);
2031                 this_->follow_cursor=!!attr->u.num;
2032                 break;
2033         case attr_imperial: 
2034                 attr_updated=(this_->imperial != attr->u.num); 
2035                 this_->imperial=attr->u.num; 
2036                 break; 
2037         default:
2038                 return 0;
2039         }
2040         if (attr_updated && !init) {
2041                 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2042                 if (attr->type == attr_osd_configuration)
2043                         graphics_draw_mode(this_->gra, draw_mode_end);
2044         }
2045         return 1;
2046 }
2047
2048 int
2049 navit_set_attr(struct navit *this_, struct attr *attr)
2050 {
2051         return navit_set_attr_do(this_, attr, 0);
2052 }
2053
2054 int
2055 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
2056 {
2057         struct message *msg;
2058         int len,offset;
2059         int ret=1;
2060
2061         switch (type) {
2062         case attr_message:
2063                 msg = navit_get_messages(this_);
2064                 
2065                 if (!msg) {
2066                         return 0;
2067                 }
2068
2069                 len = 0;
2070                 while (msg) {
2071                         len += strlen(msg->text) + 1;
2072                         msg = msg->next;
2073                 }
2074                 attr->u.str = g_malloc(len + 1);
2075                 
2076                 msg = navit_get_messages(this_);
2077                 offset = 0;
2078                 while (msg) {
2079                         g_stpcpy((attr->u.str + offset), msg->text);
2080                         offset += strlen(msg->text);
2081                         attr->u.str[offset] = '\n';
2082                         offset++;
2083
2084                         msg = msg->next;
2085                 }
2086
2087                 attr->u.str[len] = '\0';
2088                 break;
2089         case attr_imperial: 
2090                 attr->u.num=this_->imperial; 
2091                 break; 
2092         case attr_bookmark_map:
2093                 attr->u.map=bookmarks_get_map(this_->bookmarks);
2094                 break;
2095         case attr_bookmarks:
2096                 attr->u.bookmarks=this_->bookmarks;
2097                 break;
2098         case attr_callback_list:
2099                 attr->u.callback_list=this_->attr_cbl;
2100                 break;
2101         case attr_destination:
2102                 if (! this_->destination_valid)
2103                         return 0;
2104                 attr->u.pcoord=&this_->destination;
2105                 break;
2106         case attr_displaylist:
2107                 attr->u.displaylist=this_->displaylist;
2108                 return (attr->u.displaylist != NULL);
2109         case attr_follow:
2110                 if (!this_->vehicle)
2111                         return 0;
2112                 attr->u.num=this_->vehicle->follow_curr;
2113                 break;
2114         case attr_former_destination_map:
2115                 attr->u.map=this_->former_destination;
2116                 break;
2117         case attr_graphics:
2118                 attr->u.graphics=this_->gra;
2119                 ret=(attr->u.graphics != NULL);
2120                 break;
2121         case attr_gui:
2122                 attr->u.gui=this_->gui;
2123                 ret=(attr->u.gui != NULL);
2124                 break;
2125         case attr_layout:
2126                 if (iter) {
2127                         if (iter->u.list) {
2128                                 iter->u.list=g_list_next(iter->u.list);
2129                         } else { 
2130                                 iter->u.list=this_->layouts;
2131                         }
2132                         if (!iter->u.list)
2133                                 return 0;
2134                         attr->u.layout=(struct layout *)iter->u.list->data;
2135                 } else {
2136                         attr->u.layout=this_->layout_current;
2137                 }
2138                 break;
2139         case attr_map:
2140                 if (iter && this_->mapsets) {
2141                         if (!iter->u.mapset_handle) {
2142                                 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2143                         }
2144                         attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2145                         if(!attr->u.map) {
2146                                 mapset_close(iter->u.mapset_handle);
2147                                 return 0;
2148                         }
2149                 } else {
2150                         return 0;
2151                 }
2152                 break;
2153         case attr_mapset:
2154                 attr->u.mapset=this_->mapsets->data;
2155                 ret=(attr->u.mapset != NULL);
2156                 break;
2157         case attr_navigation:
2158                 attr->u.navigation=this_->navigation;
2159                 break;
2160         case attr_orientation:
2161                 attr->u.num=this_->orientation;
2162                 break;
2163         case attr_osd_configuration:
2164                 attr->u.num=this_->osd_configuration;
2165                 break;
2166         case attr_pitch:
2167                 attr->u.num=transform_get_pitch(this_->trans);
2168                 break;
2169         case attr_projection:
2170                 if(this_->trans) {
2171                         attr->u.num=transform_get_projection(this_->trans);
2172                 } else {
2173                         return 0;
2174                 }
2175                 break;
2176         case attr_route:
2177                 attr->u.route=this_->route;
2178                 break;
2179         case attr_speech:
2180                 attr->u.speech=this_->speech;
2181                 break;
2182         case attr_tracking:
2183                 attr->u.num=this_->tracking_flag;
2184                 break;
2185         case attr_trackingo:
2186                 attr->u.tracking=this_->tracking;
2187                 break;
2188         case attr_transformation:
2189                 attr->u.transformation=this_->trans;
2190                 break;
2191         case attr_vehicle:
2192                 if(iter) {
2193                         if(iter->u.list) {
2194                                 iter->u.list=g_list_next(iter->u.list);
2195                         } else { 
2196                                 iter->u.list=this_->vehicles;
2197                         }
2198                         if(!iter->u.list)
2199                                 return 0;
2200                         attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2201                 } else {
2202                         if(this_->vehicle) {
2203                                 attr->u.vehicle=this_->vehicle->vehicle;
2204                         } else {
2205                                 return 0;
2206                         }
2207                 }
2208                 break;
2209         case attr_vehicleprofile:
2210                 attr->u.vehicleprofile=this_->vehicleprofile;
2211                 break;
2212         case attr_zoom:
2213                 attr->u.num=transform_get_scale(this_->trans);
2214                 break;
2215         case attr_autozoom_active:
2216                 attr->u.num=this_->autozoom_active;
2217                 break;
2218         case attr_follow_cursor:
2219                 attr->u.num=this_->follow_cursor;
2220                 break;
2221         default:
2222                 return 0;
2223         }
2224         attr->type=type;
2225         return ret;
2226 }
2227
2228 static int
2229 navit_add_log(struct navit *this_, struct log *log)
2230 {
2231         struct attr type_attr;
2232         if (!log_get_attr(log, attr_type, &type_attr, NULL))
2233                 return 0;
2234         if (!strcmp(type_attr.u.str, "textfile_debug")) {
2235                 char *header = "type=track_tracked\n";
2236                 if (this_->textfile_debug_log)
2237                         return 0;
2238                 log_set_header(log, header, strlen(header));
2239                 this_->textfile_debug_log=log;
2240                 return 1;
2241         }
2242         return 0;
2243 }
2244
2245 int
2246 navit_add_attr(struct navit *this_, struct attr *attr)
2247 {
2248         int ret=1;
2249         switch (attr->type) {
2250         case attr_callback:
2251                 navit_add_callback(this_, attr->u.callback);
2252                 break;
2253         case attr_log:
2254                 ret=navit_add_log(this_, attr->u.log);
2255                 break;
2256         case attr_gui:
2257                 ret=navit_set_gui(this_, attr->u.gui);
2258                 break;
2259         case attr_graphics:
2260                 ret=navit_set_graphics(this_, attr->u.graphics);
2261                 break;
2262         case attr_layout:
2263                 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
2264                 if(!this_->layout_current) 
2265                         this_->layout_current=attr->u.layout;
2266                 break;
2267         case attr_route:
2268                 this_->route=attr->u.route;
2269                 break;
2270         case attr_mapset:
2271                 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2272                 break;
2273         case attr_navigation:
2274                 this_->navigation=attr->u.navigation;
2275                 break;
2276         case attr_recent_dest:
2277                 this_->recentdest_count = attr->u.num;
2278                 break;
2279         case attr_speech:
2280                 this_->speech=attr->u.speech;
2281                 break;
2282         case attr_tracking:
2283                 this_->tracking=attr->u.tracking;
2284                 break;
2285         case attr_vehicle:
2286                 ret=navit_add_vehicle(this_, attr->u.vehicle);
2287                 break;
2288         case attr_vehicleprofile:
2289                 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2290                 break;
2291         case attr_autozoom_min:
2292                 this_->autozoom_min = attr->u.num;
2293                 break;
2294         default:
2295                 return 0;
2296         }
2297         callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2298         return ret;
2299 }
2300
2301 int
2302 navit_remove_attr(struct navit *this_, struct attr *attr)
2303 {
2304         int ret=1;
2305         switch (attr->type) {
2306         case attr_callback:
2307                 navit_remove_callback(this_, attr->u.callback);
2308                 break;
2309         default:
2310                 return 0;
2311         }
2312         return ret;
2313 }
2314
2315 struct attr_iter *
2316 navit_attr_iter_new(void)
2317 {
2318         return g_new0(struct attr_iter, 1);
2319 }
2320
2321 void
2322 navit_attr_iter_destroy(struct attr_iter *iter)
2323 {
2324         g_free(iter);
2325 }
2326
2327 void
2328 navit_add_callback(struct navit *this_, struct callback *cb)
2329 {
2330         callback_list_add(this_->attr_cbl, cb);
2331 }
2332
2333 void
2334 navit_remove_callback(struct navit *this_, struct callback *cb)
2335 {
2336         callback_list_remove(this_->attr_cbl, cb);
2337 }
2338
2339 /**
2340  * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2341  *
2342  * @param navit The navit instance
2343  * @returns nothing
2344  */
2345
2346 static void
2347 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2348 {
2349         struct point cursor_pnt;
2350         enum projection pro;
2351
2352         if (this_->blocked)
2353                 return;
2354         if (pnt)
2355                 cursor_pnt=*pnt;
2356         else {
2357                 pro=transform_get_projection(this_->trans_cursor);
2358                 if (!pro)
2359                         return;
2360                 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2361         }
2362         vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2363 #if 0   
2364         if (pnt)
2365                 pnt2=*pnt;
2366         else {
2367                 pro=transform_get_projection(this_->trans);
2368                 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2369         }
2370 #if 1
2371         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2372 #else
2373         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2374 #endif
2375 #endif
2376 }
2377
2378 static void
2379 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2380 {
2381         struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2382         struct pcoord cursor_pc;
2383         struct point cursor_pnt, *pnt=&cursor_pnt;
2384         struct tracking *tracking=NULL;
2385         struct pcoord pc[16];
2386         enum projection pro=transform_get_projection(this_->trans_cursor);
2387         int count;
2388         int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2389         void *attr_object;
2390         char *destination_file;
2391
2392         profile(0,NULL);
2393         if (this_->ready != 3) {
2394                 profile(0,"return 1\n");
2395                 return;
2396         }
2397         navit_layout_switch(this_);
2398         if (this_->vehicle == nv && this_->tracking_flag)
2399                 tracking=this_->tracking;
2400         if (tracking) {
2401                 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2402                 attr_object=tracking;
2403                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2404         } else {
2405                 attr_object=nv->vehicle;
2406                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2407         }
2408         if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2409                 if (!attr_valid.u.num != attr_position_valid_invalid)
2410                         return;
2411         if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2412             ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2413             ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2414                 profile(0,"return 2\n");
2415                 return;
2416         }
2417         nv->dir=*attr_dir.u.numd;
2418         nv->speed=*attr_speed.u.numd;
2419         transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2420         if (nv != this_->vehicle) {
2421                 navit_vehicle_draw(this_, nv, NULL);
2422                 profile(0,"return 3\n");
2423                 return;
2424         }
2425         cursor_pc.x = nv->coord.x;
2426         cursor_pc.y = nv->coord.y;
2427         cursor_pc.pro = pro;
2428         if (this_->route) {
2429                 if (tracking)
2430                         route_set_position_from_tracking(this_->route, tracking, pro);
2431                 else
2432                         route_set_position(this_->route, &cursor_pc);
2433         }
2434         callback_list_call_attr_0(this_->attr_cbl, attr_position);
2435         navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2436         if (this_->gui && nv->speed > 2)
2437                 navit_disable_suspend();
2438
2439         transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2440         if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow && 
2441                 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2442                 navit_set_center_cursor_draw(this_);
2443         else
2444                 navit_vehicle_draw(this_, nv, pnt);
2445
2446         if (nv->follow_curr > 1)
2447                 nv->follow_curr--;
2448         else
2449                 nv->follow_curr=nv->follow;
2450         callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2451
2452         /* Finally, if we reached our destination, stop navigation. */
2453         if (this_->route) {
2454                 switch(route_destination_reached(this_->route)) {
2455                 case 1:
2456                         route_remove_waypoint(this_->route);
2457                         count=route_get_destinations(this_->route, pc, 16);
2458                         destination_file = bookmarks_get_destination_file(TRUE);
2459                         bookmarks_append_coord(this_->bookmarks, destination_file, pc, count, "former_itinerary_part", NULL, NULL, this_->recentdest_count);
2460                         break;  
2461                 case 2:
2462                         navit_set_destination(this_, NULL, NULL, 0);
2463                         break;
2464                 }
2465         }
2466         profile(0,"return 5\n");
2467 }
2468
2469 /**
2470  * Set the position of the vehicle
2471  *
2472  * @param navit The navit instance
2473  * @param c The coordinate to set as position
2474  * @returns nothing
2475  */
2476
2477 void
2478 navit_set_position(struct navit *this_, struct pcoord *c)
2479 {
2480         if (this_->route) {
2481                 route_set_position(this_->route, c);
2482                 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2483         }
2484         if (this_->ready == 3)
2485                 navit_draw(this_);
2486 }
2487
2488 static int
2489 navit_set_vehicleprofile(struct navit *this_, char *name)
2490 {
2491         struct attr attr;
2492         GList *l;
2493         l=this_->vehicleprofiles;
2494         while (l) {
2495                 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2496                         if (!strcmp(attr.u.str, name)) {
2497                                 this_->vehicleprofile=l->data;
2498                                 if (this_->route)
2499                                         route_set_profile(this_->route, this_->vehicleprofile);
2500                                 return 1;
2501                         }
2502                 }
2503                 l=g_list_next(l);
2504         }
2505         return 0;
2506 }
2507
2508 static void
2509 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2510 {
2511         struct attr attr;
2512         this_->vehicle=nv;
2513         if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2514                 if (navit_set_vehicleprofile(this_, attr.u.str))
2515                         return;
2516         }
2517         if (!navit_set_vehicleprofile(this_,"car")) {
2518                 /* We do not have a fallback "car" profile
2519                 * so lets set any profile */
2520                 GList *l;
2521                 l=this_->vehicleprofiles;
2522                 if (l) {
2523                     this_->vehicleprofile=l->data;
2524                     if (this_->route)
2525                         route_set_profile(this_->route, this_->vehicleprofile);
2526                 }
2527         }
2528 }
2529
2530 /**
2531  * Register a new vehicle
2532  *
2533  * @param navit The navit instance
2534  * @param v The vehicle instance
2535  * @returns 1 for success
2536  */
2537 static int
2538 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2539 {
2540         struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2541         struct attr follow, active, animate;
2542         nv->vehicle=v;
2543         nv->follow=0;
2544         nv->last.x = 0;
2545         nv->last.y = 0;
2546         nv->animate_cursor=0;
2547         if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2548                 nv->follow=follow.u.num;
2549         nv->follow_curr=nv->follow;
2550         this_->vehicles=g_list_append(this_->vehicles, nv);
2551         if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2552                 navit_set_vehicle(this_, nv);
2553         if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2554                 nv->animate_cursor=animate.u.num;
2555         nv->callback.type=attr_callback;
2556         nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2557         vehicle_add_attr(nv->vehicle, &nv->callback);
2558         vehicle_set_attr(nv->vehicle, &this_->self);
2559         return 1;
2560 }
2561
2562
2563
2564
2565 struct gui *
2566 navit_get_gui(struct navit *this_)
2567 {
2568         return this_->gui;
2569 }
2570
2571 struct transformation *
2572 navit_get_trans(struct navit *this_)
2573 {
2574         return this_->trans;
2575 }
2576
2577 struct route *
2578 navit_get_route(struct navit *this_)
2579 {
2580         return this_->route;
2581 }
2582
2583 struct navigation *
2584 navit_get_navigation(struct navit *this_)
2585 {
2586         return this_->navigation;
2587 }
2588
2589 struct displaylist *
2590 navit_get_displaylist(struct navit *this_)
2591 {
2592         return this_->displaylist;
2593 }
2594
2595 void
2596 navit_layout_switch(struct navit *n) 
2597 {
2598
2599     int currTs=0;
2600     struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2601     double trise,tset,trise_actual;
2602     struct layout *l;
2603     int year, month, day;
2604     
2605     if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2606         return; //No layout - nothing to switch
2607     }
2608     if (!n->vehicle)
2609         return;
2610     l=layout_attr.u.layout;
2611     
2612     if (l->dayname || l->nightname) {
2613         //Ok, we know that we have profile to switch
2614         
2615         //Check that we aren't calculating too fast
2616         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2617                 currTs=iso8601_to_secs(iso8601_attr.u.str);
2618                 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2619         }
2620         if (currTs-(n->prevTs)<60) {
2621             //We've have to wait a little
2622             return;
2623         }
2624         if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2625                 return;
2626         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2627                 return; //No valid fix yet
2628         }
2629         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2630                 //No position - no sun
2631                 return;
2632         }
2633         
2634         //We calculate sunrise anyway, cause it is needed both for day and for night
2635         if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2636                 //near the pole sun never rises/sets, so we should never switch profiles
2637                 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
2638                 n->prevTs=currTs;
2639                 return;
2640             }
2641         
2642         trise_actual=trise;
2643         dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2644         if (l->dayname) {
2645         
2646             if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) || 
2647                     (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2648                 //The sun is rising now!
2649                 if (strcmp(l->name,l->dayname)) {
2650                     navit_set_layout_by_name(n,l->dayname);
2651                 }
2652             }
2653         }
2654         if (l->nightname) {
2655             if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2656                 //near the pole sun never rises/sets, so we should never switch profiles
2657                 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
2658                 n->prevTs=currTs;
2659                 return;
2660             }
2661             dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2662             if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2663                 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) || 
2664                         ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2665                 //Time to sleep
2666                 if (strcmp(l->name,l->nightname)) {
2667                     navit_set_layout_by_name(n,l->nightname);
2668                 }
2669             }   
2670         }
2671         
2672         n->prevTs=currTs;
2673     }
2674 }
2675
2676 int 
2677 navit_set_vehicle_by_name(struct navit *n,const char *name) 
2678 {
2679     struct vehicle *v;
2680     struct attr_iter *iter;
2681     struct attr vehicle_attr, name_attr;
2682
2683         iter=navit_attr_iter_new();
2684
2685     while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
2686                 v=vehicle_attr.u.vehicle;
2687                 vehicle_get_attr(v,attr_name,&name_attr,NULL);
2688                 if (name_attr.type==attr_name) {
2689                         if (!strcmp(name,name_attr.u.str)) {
2690                                 navit_set_attr(n,&vehicle_attr);                                
2691                                 navit_attr_iter_destroy(iter);
2692                                 return 1;
2693                         }
2694                 }
2695         }
2696     navit_attr_iter_destroy(iter);
2697     return 0;
2698 }
2699
2700 int 
2701 navit_set_layout_by_name(struct navit *n,const char *name) 
2702 {
2703     struct layout *l;
2704     struct attr_iter iter;
2705     struct attr layout_attr;
2706
2707     iter.u.list=0x00;
2708
2709     if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2710         return 0; //No layouts - nothing to do
2711     }
2712     if (iter.u.list==NULL) {
2713         return 0;
2714     }
2715     
2716     iter.u.list=g_list_first(iter.u.list);
2717     
2718     while(iter.u.list) {
2719         l=(struct layout*)iter.u.list->data;
2720         if (!strcmp(name,l->name)) {
2721             layout_attr.u.layout=l;
2722             layout_attr.type=attr_layout;
2723             navit_set_attr(n,&layout_attr);
2724             iter.u.list=g_list_first(iter.u.list);
2725             return 1;
2726         }
2727         iter.u.list=g_list_next(iter.u.list);
2728     }
2729
2730     iter.u.list=g_list_first(iter.u.list);
2731     return 0;
2732 }
2733
2734 void
2735 navit_disable_suspend() {
2736         gui_disable_suspend(global_navit->gui);
2737         callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
2738 }
2739
2740 int
2741 navit_block(struct navit *this_, int block)
2742 {
2743         if (block > 0) {
2744                 this_->blocked |= 1;
2745                 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2746                         this_->blocked |= 2;
2747                 return 0;
2748         }
2749         if ((this_->blocked & 2) || block < 0) {
2750                 this_->blocked=0;
2751                 navit_draw(this_);
2752                 return 1;
2753         }
2754         this_->blocked=0;
2755         return 0;
2756 }
2757
2758 void
2759 navit_destroy(struct navit *this_)
2760 {
2761         callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
2762
2763         /* TODO: destroy objects contained in this_ */
2764         if (this_->vehicle)
2765                 vehicle_destroy(this_->vehicle->vehicle);
2766         if (this_->bookmarks) {
2767                 char *center_file = bookmarks_get_center_file(TRUE);
2768                 bookmarks_write_center_to_file(this_->bookmarks, center_file);
2769                 g_free(center_file);
2770                 bookmarks_destroy(this_->bookmarks);
2771         }
2772         callback_destroy(this_->nav_speech_cb);
2773         callback_destroy(this_->roadbook_callback);
2774         callback_destroy(this_->popup_callback);
2775         callback_destroy(this_->motion_timeout_callback);
2776         if(this_->gra)
2777           graphics_remove_callback(this_->gra, this_->resize_callback);
2778         callback_destroy(this_->resize_callback);
2779         if(this_->gra)
2780           graphics_remove_callback(this_->gra, this_->button_callback);
2781         callback_destroy(this_->button_callback);
2782         if(this_->gra)
2783           graphics_remove_callback(this_->gra, this_->motion_callback);
2784         callback_destroy(this_->motion_callback);
2785         if(this_->gra)
2786           graphics_remove_callback(this_->gra, this_->predraw_callback);
2787         callback_destroy(this_->predraw_callback);
2788         route_destroy(this_->route);
2789         graphics_free(this_->gra);
2790         g_free(this_);
2791 }
2792
2793 /** @} */