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