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