Fix:core:reverted previous accidental commit to navit.c
[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                         //pass new callback instance for each map in the mapset to make map callback list destruction work correctly
1732                         struct callback *pcb = callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1733                         map_add_callback(map, pcb);
1734                 }
1735                 mapset_close(msh);
1736                 
1737                 if (this_->route) {
1738                         if ((map=route_get_map(this_->route))) {
1739                                 struct attr map_a;
1740                                 map_a.type=attr_map;
1741                                 map_a.u.map=map;
1742                                 mapset_add_attr(ms, &map_a);
1743                         }
1744                         if ((map=route_get_graph_map(this_->route))) {
1745                                 struct attr map_a,active;
1746                                 map_a.type=attr_map;
1747                                 map_a.u.map=map;
1748                                 active.type=attr_active;
1749                                 active.u.num=0;
1750                                 mapset_add_attr(ms, &map_a);
1751                                 map_set_attr(map, &active);
1752                         }
1753                         route_set_mapset(this_->route, ms);
1754                         route_set_projection(this_->route, transform_get_projection(this_->trans));
1755                 }
1756                 if (this_->tracking) {
1757                         tracking_set_mapset(this_->tracking, ms);
1758                         if (this_->route)
1759                                 tracking_set_route(this_->tracking, this_->route);
1760                 }
1761                 if (this_->navigation) {
1762                         if ((map=navigation_get_map(this_->navigation))) {
1763                                 struct attr map_a,active;
1764                                 map_a.type=attr_map;
1765                                 map_a.u.map=map;
1766                                 active.type=attr_active;
1767                                 active.u.num=0;
1768                                 mapset_add_attr(ms, &map_a);
1769                                 map_set_attr(map, &active);
1770                         }
1771                 }
1772                 if (this_->tracking) {
1773                         if ((map=tracking_get_map(this_->tracking))) {
1774                                 struct attr map_a,active;
1775                                 map_a.type=attr_map;
1776                                 map_a.u.map=map;
1777                                 active.type=attr_active;
1778                                 active.u.num=0;
1779                                 mapset_add_attr(ms, &map_a);
1780                                 map_set_attr(map, &active);
1781                         }
1782                 }
1783                 navit_add_former_destinations_from_file(this_);
1784         }
1785         if (this_->route) {
1786                 struct attr callback;
1787                 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1788                 callback.type=attr_callback;
1789                 callback.u.callback=this_->route_cb;
1790                 route_add_attr(this_->route, &callback);
1791         }
1792         if (this_->navigation) {
1793                 if (this_->speech) {
1794                         this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1795                         navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1796                 }
1797                 if (this_->route)
1798                         navigation_set_route(this_->navigation, this_->route);
1799         }
1800         dbg(2,"Setting Center\n");
1801         center_file = bookmarks_get_center_file(FALSE);
1802         bookmarks_set_center_from_file(this_->bookmarks, center_file);
1803         g_free(center_file);
1804 #if 0
1805         if (this_->menubar) {
1806                 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1807                 if (men) {
1808                         navit_add_menu_windows_items(this_, men);
1809                 }
1810         }
1811 #endif
1812         global_navit=this_;
1813 #if 0
1814         navit_window_roadbook_new(this_);
1815         navit_window_items_new(this_);
1816 #endif
1817
1818         messagelist_init(this_->messages);
1819
1820         navit_set_cursors(this_);
1821
1822         callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1823         callback=(this_->ready == 2);
1824         this_->ready|=1;
1825         dbg(2,"ready=%d\n",this_->ready);
1826         if (this_->ready == 3)
1827                 navit_draw_async(this_, 1);
1828         if (callback)
1829                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1830 #if 0
1831         routech_test(this_);
1832 #endif
1833 }
1834
1835 void
1836 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
1837 {
1838         struct coord c;
1839         int scale=16;
1840
1841         c.x=(r->rl.x+r->lu.x)/2;
1842         c.y=(r->rl.y+r->lu.y)/2;
1843         transform_set_center(this_->trans, &c);
1844         dbg(1,"%x,%x-%x,%x\n", r->rl.x,r->rl.y,r->lu.x,r->lu.y);
1845         while (scale < 1<<20) {
1846                 struct point p1,p2;
1847                 transform_set_scale(this_->trans, scale);
1848                 transform_setup_source_rect(this_->trans);
1849                 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
1850                 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
1851                 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1852                 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1853                     p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1854                         scale*=2;
1855                 else
1856                         break;
1857         
1858         }
1859         if (this_->ready == 3)
1860                 navit_draw_async(this_,0);
1861 }
1862
1863 void
1864 navit_zoom_to_route(struct navit *this_, int orientation)
1865 {
1866         struct map *map;
1867         struct map_rect *mr=NULL;
1868         struct item *item;
1869         struct coord c;
1870         struct coord_rect r;
1871         int count=0;
1872         if (! this_->route)
1873                 return;
1874         dbg(1,"enter\n");
1875         map=route_get_map(this_->route);
1876         dbg(1,"map=%p\n",map);
1877         if (map)
1878                 mr=map_rect_new(map, NULL);
1879         dbg(1,"mr=%p\n",mr);
1880         if (mr) {
1881                 while ((item=map_rect_get_item(mr))) {
1882                         dbg(1,"item=%s\n", item_to_name(item->type));
1883                         while (item_coord_get(item, &c, 1)) {
1884                                 dbg(1,"coord\n");
1885                                 if (!count) 
1886                                         r.lu=r.rl=c;
1887                                 else
1888                                         coord_rect_extend(&r, &c);      
1889                                 count++;
1890                         }
1891                 }
1892                 map_rect_destroy(mr);
1893         }
1894         if (! count)
1895                 return;
1896         if (orientation != -1)
1897                 transform_set_yaw(this_->trans, orientation);
1898         navit_zoom_to_rect(this_, &r);
1899 }
1900
1901 static void
1902 navit_cmd_zoom_to_route(struct navit *this)
1903 {
1904         navit_zoom_to_route(this, 0);
1905 }
1906
1907
1908 /**
1909  * Change the current zoom level
1910  *
1911  * @param navit The navit instance
1912  * @param center The point where to center the map, including its projection
1913  * @returns nothing
1914  */
1915 void
1916 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1917 {
1918         struct coord *c=transform_center(this_->trans);
1919         struct coord c1,c2;
1920         enum projection pro = transform_get_projection(this_->trans);
1921         if (pro != center->pro) {
1922                 c1.x = center->x;
1923                 c1.y = center->y;
1924                 transform_from_to(&c1, center->pro, &c2, pro);
1925         } else {
1926                 c2.x = center->x;
1927                 c2.y = center->y;
1928         }
1929         *c=c2;
1930         if (set_timeout) 
1931                 navit_set_timeout(this_);
1932         if (this_->ready == 3)
1933                 navit_draw(this_);
1934 }
1935
1936 static void
1937 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1938 {
1939         int width, height;
1940         struct point po;
1941         transform_set_center(this_->trans, c);
1942         transform_get_size(this_->trans, &width, &height);
1943         po.x=width/2;
1944         po.y=height/2;
1945         update_transformation(this_->trans, &po, p, NULL);
1946         if (set_timeout)
1947                 navit_set_timeout(this_);
1948 }
1949
1950 /**
1951  * Links all vehicles to a cursor depending on the current profile.
1952  *
1953  * @param this_ A navit instance
1954  * @author Ralph Sennhauser (10/2009)
1955  */
1956 static void
1957 navit_set_cursors(struct navit *this_)
1958 {
1959         struct attr name;
1960         struct navit_vehicle *nv;
1961         struct cursor *c;
1962         GList *v;
1963
1964         v=g_list_first(this_->vehicles); // GList of navit_vehicles
1965         while (v) {
1966                 nv=v->data;
1967                 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
1968                         if (!strcmp(name.u.str,"none"))
1969                                 c=NULL;
1970                         else
1971                                 c=layout_get_cursor(this_->layout_current, name.u.str);
1972                 } else
1973                         c=layout_get_cursor(this_->layout_current, "default");
1974                 vehicle_set_cursor(nv->vehicle, c, 0);
1975                 v=g_list_next(v);
1976         }
1977         return;
1978 }
1979
1980 static int
1981 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
1982 {
1983         int width, height;
1984         struct navit_vehicle *nv=this_->vehicle;
1985
1986         float offset=this_->radius;      // Cursor offset from the center of the screen (percent).
1987 #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 */
1988         float min_offset = 0.;      // Percent offset at min_offset_speed.
1989         float max_offset = 30.;     // Percent offset at max_offset_speed.
1990         int min_offset_speed = 2;   // Speed in km/h
1991         int max_offset_speed = 50;  // Speed ini km/h
1992         // Calculate cursor offset from the center of the screen, upon speed.
1993         if (nv->speed <= min_offset_speed) {
1994             offset = min_offset;
1995         } else if (nv->speed > max_offset_speed) {
1996             offset = max_offset;
1997         } else {
1998             offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1999         }
2000 #endif
2001
2002         transform_get_size(this_->trans, &width, &height);
2003         if (this_->orientation == -1 || keep_orientation) {
2004                 p->x=50*width/100;
2005                 p->y=(50 + offset)*height/100;
2006                 if (dir) 
2007                         *dir=keep_orientation?this_->orientation:nv->dir;
2008         } else {
2009                 int mdir;
2010                 if (this_->tracking && this_->tracking_flag) {
2011                         mdir = tracking_get_angle(this_->tracking) - this_->orientation;
2012                 } else {
2013                         mdir=nv->dir-this_->orientation;
2014                 }
2015
2016                 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
2017                 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
2018                 if (dir)
2019                         *dir=this_->orientation;
2020         }
2021         return 1;
2022 }
2023
2024 void
2025 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
2026 {
2027         int dir;
2028         struct point pn;
2029         struct navit_vehicle *nv=this_->vehicle;
2030         navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
2031         transform_set_yaw(this_->trans, dir);
2032         navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
2033         if (autozoom)
2034                 navit_autozoom(this_, &nv->coord, nv->speed, 0);
2035 }
2036
2037 static void
2038 navit_set_center_cursor_draw(struct navit *this_)
2039 {
2040         navit_set_center_cursor(this_,1,0);
2041         if (this_->ready == 3)
2042                 navit_draw_async(this_, 1);
2043 }
2044
2045 static void
2046 navit_cmd_set_center_cursor(struct navit *this_)
2047 {
2048         navit_set_center_cursor_draw(this_);
2049 }
2050
2051 void
2052 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
2053 {
2054         struct coord c;
2055         struct pcoord pc;
2056         transform_reverse(this_->trans, p, &c);
2057         pc.x = c.x;
2058         pc.y = c.y;
2059         pc.pro = transform_get_projection(this_->trans);
2060         navit_set_center(this_, &pc, set_timeout);
2061 }
2062
2063 #if 0
2064                 switch((*attrs)->type) {
2065                 case attr_zoom:
2066                         zoom=(*attrs)->u.num;
2067                         break;
2068                 case attr_center:
2069                         g=*((*attrs)->u.coord_geo);
2070                         break;
2071 #endif
2072
2073 static int
2074 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
2075 {
2076         int dir=0, orient_old=0, attr_updated=0;
2077         struct coord co;
2078         long zoom;
2079         GList *l;
2080         struct navit_vehicle *nv;
2081         struct layout *lay;
2082         struct attr active;
2083         active.type=attr_active;
2084         active.u.num=0;
2085
2086         switch (attr->type) {
2087         case attr_autozoom:
2088                 attr_updated=(this_->autozoom_secs != attr->u.num);
2089                 this_->autozoom_secs = attr->u.num;
2090                 break;
2091         case attr_autozoom_active:
2092                 attr_updated=(this_->autozoom_active != attr->u.num);
2093                 this_->autozoom_active = attr->u.num;
2094                 break;
2095         case attr_center:
2096                 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
2097                 dbg(1,"0x%x,0x%x\n",co.x,co.y);
2098                 transform_set_center(this_->trans, &co);
2099                 break;
2100         case attr_drag_bitmap:
2101                 attr_updated=(this_->drag_bitmap != !!attr->u.num);
2102                 this_->drag_bitmap=!!attr->u.num;
2103                 break;
2104         case attr_flags:
2105                 attr_updated=(this_->flags != attr->u.num);
2106                 this_->flags=attr->u.num;
2107                 break;
2108         case attr_flags_graphics:
2109                 attr_updated=(this_->graphics_flags != attr->u.num);
2110                 this_->graphics_flags=attr->u.num;
2111                 break;
2112         case attr_follow:
2113                 if (!this_->vehicle)
2114                         return 0;
2115                 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
2116                 this_->vehicle->follow_curr = attr->u.num;
2117                 break;
2118         case attr_layout:
2119                 if(this_->layout_current!=attr->u.layout) {
2120                         this_->layout_current=attr->u.layout;
2121                         graphics_font_destroy_all(this_->gra);
2122                         navit_set_cursors(this_);
2123                         if (this_->ready == 3)
2124                                 navit_draw(this_);
2125                         attr_updated=1;
2126                 }
2127                 break;
2128         case attr_layout_name:
2129                 l=this_->layouts;
2130                 while (l) {
2131                         lay=l->data;
2132                         if (!strcmp(lay->name,attr->u.str)) {
2133                                 struct attr attr;
2134                                 attr.type=attr_layout;
2135                                 attr.u.layout=lay;
2136                                 return navit_set_attr_do(this_, &attr, init);
2137                         }
2138                         l=g_list_next(l);
2139                 }               
2140                 return 0;
2141         case attr_map_border:
2142                 if (this_->border != attr->u.num) {
2143                         this_->border=attr->u.num;
2144                         attr_updated=1;
2145                 }
2146                 break;
2147         case attr_orientation:
2148                 orient_old=this_->orientation;
2149                 this_->orientation=attr->u.num;
2150                 if (!init) {
2151                         if (this_->orientation != -1) {
2152                                 dir = this_->orientation;
2153                         } else {
2154                                 if (this_->vehicle) {
2155                                         dir = this_->vehicle->dir;
2156                                 }
2157                         }
2158                         transform_set_yaw(this_->trans, dir);
2159                         if (orient_old != this_->orientation) {
2160 #if 0
2161                                 if (this_->ready == 3)
2162                                         navit_draw(this_);
2163 #endif
2164                                 attr_updated=1;
2165                         }
2166                 }
2167                 break;
2168         case attr_osd_configuration:
2169                 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
2170                 attr_updated=(this_->osd_configuration != attr->u.num);
2171                 this_->osd_configuration=attr->u.num;
2172                 break;
2173         case attr_pitch:
2174                 attr_updated=(this_->pitch != attr->u.num);
2175                 this_->pitch=attr->u.num;
2176                 transform_set_pitch(this_->trans, this_->pitch);
2177                 if (!init && attr_updated && this_->ready == 3)
2178                         navit_draw(this_);
2179                 break;
2180         case attr_projection:
2181                 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
2182                         navit_projection_set(this_, attr->u.projection, !init);
2183                         attr_updated=1;
2184                 }
2185                 break;
2186         case attr_radius:
2187                 attr_updated=(this_->radius != attr->u.num);
2188                 this_->radius=attr->u.num;
2189                 break;
2190         case attr_recent_dest:
2191                 attr_updated=(this_->recentdest_count != attr->u.num);
2192                 this_->recentdest_count=attr->u.num;
2193                 break;
2194         case attr_speech:
2195                 if(this_->speech && this_->speech != attr->u.speech) {
2196                         attr_updated=1;
2197                         this_->speech = attr->u.speech;
2198                 }
2199                 break;
2200         case attr_timeout:
2201                 attr_updated=(this_->center_timeout != attr->u.num);
2202                 this_->center_timeout = attr->u.num;
2203                 break;
2204         case attr_tracking:
2205                 attr_updated=(this_->tracking_flag != !!attr->u.num);
2206                 this_->tracking_flag=!!attr->u.num;
2207                 break;
2208         case attr_transformation:
2209                 this_->trans=attr->u.transformation;
2210                 break;
2211         case attr_use_mousewheel:
2212                 attr_updated=(this_->use_mousewheel != !!attr->u.num);
2213                 this_->use_mousewheel=!!attr->u.num;
2214                 break;
2215         case attr_vehicle:
2216                 l=this_->vehicles;
2217                 while(l) {
2218                         nv=l->data;
2219                         if (nv->vehicle == attr->u.vehicle) {
2220                                 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
2221                                         if (this_->vehicle)
2222                                                 vehicle_set_attr(this_->vehicle->vehicle, &active);
2223                                         active.u.num=1;
2224                                         vehicle_set_attr(nv->vehicle, &active);
2225                                         attr_updated=1;
2226                                 }
2227                                 navit_set_vehicle(this_, nv);
2228                         }
2229                         l=g_list_next(l);
2230                 }
2231                 break;
2232         case attr_zoom:
2233                 zoom=transform_get_scale(this_->trans);
2234                 attr_updated=(zoom != attr->u.num);
2235                 transform_set_scale(this_->trans, attr->u.num);
2236                 if (attr_updated && !init) 
2237                         navit_draw(this_);
2238                 break;
2239         case attr_zoom_min:
2240                 attr_updated=(attr->u.num != this_->zoom_min);
2241                 this_->zoom_min=attr->u.num;
2242                 break;
2243         case attr_zoom_max:
2244                 attr_updated=(attr->u.num != this_->zoom_max);
2245                 this_->zoom_max=attr->u.num;
2246                 break;
2247         case attr_message:
2248                 navit_add_message(this_, attr->u.str);
2249                 break;
2250         case attr_follow_cursor:
2251                 attr_updated=(this_->follow_cursor != !!attr->u.num);
2252                 this_->follow_cursor=!!attr->u.num;
2253                 break;
2254         case attr_imperial: 
2255                 attr_updated=(this_->imperial != attr->u.num); 
2256                 this_->imperial=attr->u.num; 
2257                 break; 
2258         default:
2259                 return 0;
2260         }
2261         if (attr_updated && !init) {
2262                 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2263                 if (attr->type == attr_osd_configuration)
2264                         graphics_draw_mode(this_->gra, draw_mode_end);
2265         }
2266         return 1;
2267 }
2268
2269 int
2270 navit_set_attr(struct navit *this_, struct attr *attr)
2271 {
2272         return navit_set_attr_do(this_, attr, 0);
2273 }
2274
2275 int
2276 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
2277 {
2278         struct message *msg;
2279         int len,offset;
2280         int ret=1;
2281
2282         switch (type) {
2283         case attr_message:
2284                 msg = navit_get_messages(this_);
2285                 
2286                 if (!msg) {
2287                         return 0;
2288                 }
2289
2290                 len = 0;
2291                 while (msg) {
2292                         len += strlen(msg->text) + 1;
2293                         msg = msg->next;
2294                 }
2295                 attr->u.str = g_malloc(len + 1);
2296                 
2297                 msg = navit_get_messages(this_);
2298                 offset = 0;
2299                 while (msg) {
2300                         g_stpcpy((attr->u.str + offset), msg->text);
2301                         offset += strlen(msg->text);
2302                         attr->u.str[offset] = '\n';
2303                         offset++;
2304
2305                         msg = msg->next;
2306                 }
2307
2308                 attr->u.str[len] = '\0';
2309                 break;
2310         case attr_imperial: 
2311                 attr->u.num=this_->imperial; 
2312                 break; 
2313         case attr_bookmark_map:
2314                 attr->u.map=bookmarks_get_map(this_->bookmarks);
2315                 break;
2316         case attr_bookmarks:
2317                 attr->u.bookmarks=this_->bookmarks;
2318                 break;
2319         case attr_callback_list:
2320                 attr->u.callback_list=this_->attr_cbl;
2321                 break;
2322         case attr_destination:
2323                 if (! this_->destination_valid)
2324                         return 0;
2325                 attr->u.pcoord=&this_->destination;
2326                 break;
2327         case attr_displaylist:
2328                 attr->u.displaylist=this_->displaylist;
2329                 return (attr->u.displaylist != NULL);
2330         case attr_follow:
2331                 if (!this_->vehicle)
2332                         return 0;
2333                 attr->u.num=this_->vehicle->follow_curr;
2334                 break;
2335         case attr_former_destination_map:
2336                 attr->u.map=this_->former_destination;
2337                 break;
2338         case attr_graphics:
2339                 attr->u.graphics=this_->gra;
2340                 ret=(attr->u.graphics != NULL);
2341                 break;
2342         case attr_gui:
2343                 attr->u.gui=this_->gui;
2344                 ret=(attr->u.gui != NULL);
2345                 break;
2346         case attr_layout:
2347                 if (iter) {
2348                         if (iter->u.list) {
2349                                 iter->u.list=g_list_next(iter->u.list);
2350                         } else { 
2351                                 iter->u.list=this_->layouts;
2352                         }
2353                         if (!iter->u.list)
2354                                 return 0;
2355                         attr->u.layout=(struct layout *)iter->u.list->data;
2356                 } else {
2357                         attr->u.layout=this_->layout_current;
2358                 }
2359                 break;
2360         case attr_map:
2361                 if (iter && this_->mapsets) {
2362                         if (!iter->u.mapset_handle) {
2363                                 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2364                         }
2365                         attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2366                         if(!attr->u.map) {
2367                                 mapset_close(iter->u.mapset_handle);
2368                                 return 0;
2369                         }
2370                 } else {
2371                         return 0;
2372                 }
2373                 break;
2374         case attr_mapset:
2375                 attr->u.mapset=this_->mapsets->data;
2376                 ret=(attr->u.mapset != NULL);
2377                 break;
2378         case attr_navigation:
2379                 attr->u.navigation=this_->navigation;
2380                 break;
2381         case attr_orientation:
2382                 attr->u.num=this_->orientation;
2383                 break;
2384         case attr_osd_configuration:
2385                 attr->u.num=this_->osd_configuration;
2386                 break;
2387         case attr_pitch:
2388                 attr->u.num=transform_get_pitch(this_->trans);
2389                 break;
2390         case attr_projection:
2391                 if(this_->trans) {
2392                         attr->u.num=transform_get_projection(this_->trans);
2393                 } else {
2394                         return 0;
2395                 }
2396                 break;
2397         case attr_route:
2398                 attr->u.route=this_->route;
2399                 break;
2400         case attr_speech:
2401                 attr->u.speech=this_->speech;
2402                 break;
2403         case attr_tracking:
2404                 attr->u.num=this_->tracking_flag;
2405                 break;
2406         case attr_trackingo:
2407                 attr->u.tracking=this_->tracking;
2408                 break;
2409         case attr_transformation:
2410                 attr->u.transformation=this_->trans;
2411                 break;
2412         case attr_vehicle:
2413                 if(iter) {
2414                         if(iter->u.list) {
2415                                 iter->u.list=g_list_next(iter->u.list);
2416                         } else { 
2417                                 iter->u.list=this_->vehicles;
2418                         }
2419                         if(!iter->u.list)
2420                                 return 0;
2421                         attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2422                 } else {
2423                         if(this_->vehicle) {
2424                                 attr->u.vehicle=this_->vehicle->vehicle;
2425                         } else {
2426                                 return 0;
2427                         }
2428                 }
2429                 break;
2430         case attr_vehicleprofile:
2431                 attr->u.vehicleprofile=this_->vehicleprofile;
2432                 break;
2433         case attr_zoom:
2434                 attr->u.num=transform_get_scale(this_->trans);
2435                 break;
2436         case attr_autozoom_active:
2437                 attr->u.num=this_->autozoom_active;
2438                 break;
2439         case attr_follow_cursor:
2440                 attr->u.num=this_->follow_cursor;
2441                 break;
2442         default:
2443                 return 0;
2444         }
2445         attr->type=type;
2446         return ret;
2447 }
2448
2449 static int
2450 navit_add_log(struct navit *this_, struct log *log)
2451 {
2452         struct attr type_attr;
2453         if (!log_get_attr(log, attr_type, &type_attr, NULL))
2454                 return 0;
2455         if (!strcmp(type_attr.u.str, "textfile_debug")) {
2456                 char *header = "type=track_tracked\n";
2457                 if (this_->textfile_debug_log)
2458                         return 0;
2459                 log_set_header(log, header, strlen(header));
2460                 this_->textfile_debug_log=log;
2461                 return 1;
2462         }
2463         return 0;
2464 }
2465
2466 static int
2467 navit_add_layout(struct navit *this_, struct layout *layout)
2468 {
2469         struct attr active;
2470         this_->layouts = g_list_append(this_->layouts, layout);
2471         layout_get_attr(layout, attr_active, &active, NULL);
2472         if(active.u.num || !this_->layout_current) { 
2473                 this_->layout_current=layout;
2474                 return 1;
2475         }
2476         return 0;
2477 }
2478
2479 int
2480 navit_add_attr(struct navit *this_, struct attr *attr)
2481 {
2482         int ret=1;
2483         switch (attr->type) {
2484         case attr_callback:
2485                 navit_add_callback(this_, attr->u.callback);
2486                 break;
2487         case attr_log:
2488                 ret=navit_add_log(this_, attr->u.log);
2489                 break;
2490         case attr_gui:
2491                 ret=navit_set_gui(this_, attr->u.gui);
2492                 break;
2493         case attr_graphics:
2494                 ret=navit_set_graphics(this_, attr->u.graphics);
2495                 break;
2496         case attr_layout:
2497                 ret=navit_add_layout(this_, attr->u.layout);
2498                 break;
2499         case attr_route:
2500                 this_->route=attr->u.route;
2501                 break;
2502         case attr_mapset:
2503                 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2504                 break;
2505         case attr_navigation:
2506                 this_->navigation=attr->u.navigation;
2507                 break;
2508         case attr_recent_dest:
2509                 this_->recentdest_count = attr->u.num;
2510                 break;
2511         case attr_speech:
2512                 this_->speech=attr->u.speech;
2513                 break;
2514         case attr_tracking:
2515                 this_->tracking=attr->u.tracking;
2516                 break;
2517         case attr_vehicle:
2518                 ret=navit_add_vehicle(this_, attr->u.vehicle);
2519                 break;
2520         case attr_vehicleprofile:
2521                 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2522                 break;
2523         case attr_autozoom_min:
2524                 this_->autozoom_min = attr->u.num;
2525                 break;
2526         default:
2527                 return 0;
2528         }
2529         callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2530         return ret;
2531 }
2532
2533 int
2534 navit_remove_attr(struct navit *this_, struct attr *attr)
2535 {
2536         int ret=1;
2537         switch (attr->type) {
2538         case attr_callback:
2539                 navit_remove_callback(this_, attr->u.callback);
2540                 break;
2541         default:
2542                 return 0;
2543         }
2544         return ret;
2545 }
2546
2547 struct attr_iter *
2548 navit_attr_iter_new(void)
2549 {
2550         return g_new0(struct attr_iter, 1);
2551 }
2552
2553 void
2554 navit_attr_iter_destroy(struct attr_iter *iter)
2555 {
2556         g_free(iter);
2557 }
2558
2559 void
2560 navit_add_callback(struct navit *this_, struct callback *cb)
2561 {
2562         callback_list_add(this_->attr_cbl, cb);
2563 }
2564
2565 void
2566 navit_remove_callback(struct navit *this_, struct callback *cb)
2567 {
2568         callback_list_remove(this_->attr_cbl, cb);
2569 }
2570
2571 /**
2572  * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2573  *
2574  * @param navit The navit instance
2575  * @returns nothing
2576  */
2577
2578 static void
2579 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2580 {
2581         struct point cursor_pnt;
2582         enum projection pro;
2583
2584         if (this_->blocked)
2585                 return;
2586         if (pnt)
2587                 cursor_pnt=*pnt;
2588         else {
2589                 pro=transform_get_projection(this_->trans_cursor);
2590                 if (!pro)
2591                         return;
2592                 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2593         }
2594         vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2595 #if 0   
2596         if (pnt)
2597                 pnt2=*pnt;
2598         else {
2599                 pro=transform_get_projection(this_->trans);
2600                 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2601         }
2602 #if 1
2603         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2604 #else
2605         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2606 #endif
2607 #endif
2608 }
2609
2610 static void
2611 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2612 {
2613         struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2614         struct pcoord cursor_pc;
2615         struct point cursor_pnt, *pnt=&cursor_pnt;
2616         struct tracking *tracking=NULL;
2617         struct pcoord pc[16];
2618         enum projection pro=transform_get_projection(this_->trans_cursor);
2619         int count;
2620         int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2621         void *attr_object;
2622         char *destination_file;
2623
2624         profile(0,NULL);
2625         if (this_->ready != 3) {
2626                 profile(0,"return 1\n");
2627                 return;
2628         }
2629         navit_layout_switch(this_);
2630         if (this_->vehicle == nv && this_->tracking_flag)
2631                 tracking=this_->tracking;
2632         if (tracking) {
2633                 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2634                 attr_object=tracking;
2635                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2636         } else {
2637                 attr_object=nv->vehicle;
2638                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2639         }
2640         if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2641                 if (!attr_valid.u.num != attr_position_valid_invalid)
2642                         return;
2643         if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2644             ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2645             ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2646                 profile(0,"return 2\n");
2647                 return;
2648         }
2649         nv->dir=*attr_dir.u.numd;
2650         nv->speed=*attr_speed.u.numd;
2651         transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2652         if (nv != this_->vehicle) {
2653                 navit_vehicle_draw(this_, nv, NULL);
2654                 profile(0,"return 3\n");
2655                 return;
2656         }
2657         cursor_pc.x = nv->coord.x;
2658         cursor_pc.y = nv->coord.y;
2659         cursor_pc.pro = pro;
2660         if (this_->route) {
2661                 if (tracking)
2662                         route_set_position_from_tracking(this_->route, tracking, pro);
2663                 else
2664                         route_set_position(this_->route, &cursor_pc);
2665         }
2666         callback_list_call_attr_0(this_->attr_cbl, attr_position);
2667         navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2668         if (this_->gui && nv->speed > 2)
2669                 navit_disable_suspend();
2670
2671         transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2672         if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow && 
2673                 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2674                 navit_set_center_cursor_draw(this_);
2675         else
2676                 navit_vehicle_draw(this_, nv, pnt);
2677
2678         if (nv->follow_curr > 1)
2679                 nv->follow_curr--;
2680         else
2681                 nv->follow_curr=nv->follow;
2682         callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2683
2684         /* Finally, if we reached our destination, stop navigation. */
2685         if (this_->route) {
2686                 switch(route_destination_reached(this_->route)) {
2687                 case 1:
2688                         route_remove_waypoint(this_->route);
2689                         count=route_get_destinations(this_->route, pc, 16);
2690                         destination_file = bookmarks_get_destination_file(TRUE);
2691                         bookmarks_append_coord(this_->bookmarks, destination_file, pc, count, "former_itinerary_part", NULL, NULL, this_->recentdest_count);
2692                         break;  
2693                 case 2:
2694                         navit_set_destination(this_, NULL, NULL, 0);
2695                         break;
2696                 }
2697         }
2698         profile(0,"return 5\n");
2699 }
2700
2701 /**
2702  * Set the position of the vehicle
2703  *
2704  * @param navit The navit instance
2705  * @param c The coordinate to set as position
2706  * @returns nothing
2707  */
2708
2709 void
2710 navit_set_position(struct navit *this_, struct pcoord *c)
2711 {
2712         if (this_->route) {
2713                 route_set_position(this_->route, c);
2714                 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2715         }
2716         if (this_->ready == 3)
2717                 navit_draw(this_);
2718 }
2719
2720 static int
2721 navit_set_vehicleprofile(struct navit *this_, char *name)
2722 {
2723         struct attr attr;
2724         GList *l;
2725         l=this_->vehicleprofiles;
2726         while (l) {
2727                 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2728                         if (!strcmp(attr.u.str, name)) {
2729                                 this_->vehicleprofile=l->data;
2730                                 if (this_->route)
2731                                         route_set_profile(this_->route, this_->vehicleprofile);
2732                                 return 1;
2733                         }
2734                 }
2735                 l=g_list_next(l);
2736         }
2737         return 0;
2738 }
2739
2740 static void
2741 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2742 {
2743         struct attr attr;
2744         this_->vehicle=nv;
2745         if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2746                 if (navit_set_vehicleprofile(this_, attr.u.str))
2747                         return;
2748         }
2749         if (!navit_set_vehicleprofile(this_,"car")) {
2750                 /* We do not have a fallback "car" profile
2751                 * so lets set any profile */
2752                 GList *l;
2753                 l=this_->vehicleprofiles;
2754                 if (l) {
2755                     this_->vehicleprofile=l->data;
2756                     if (this_->route)
2757                         route_set_profile(this_->route, this_->vehicleprofile);
2758                 }
2759         }
2760 }
2761
2762 /**
2763  * Register a new vehicle
2764  *
2765  * @param navit The navit instance
2766  * @param v The vehicle instance
2767  * @returns 1 for success
2768  */
2769 static int
2770 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2771 {
2772         struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2773         struct attr follow, active, animate;
2774         nv->vehicle=v;
2775         nv->follow=0;
2776         nv->last.x = 0;
2777         nv->last.y = 0;
2778         nv->animate_cursor=0;
2779         if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2780                 nv->follow=follow.u.num;
2781         nv->follow_curr=nv->follow;
2782         this_->vehicles=g_list_append(this_->vehicles, nv);
2783         if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2784                 navit_set_vehicle(this_, nv);
2785         if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2786                 nv->animate_cursor=animate.u.num;
2787         nv->callback.type=attr_callback;
2788         nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2789         vehicle_add_attr(nv->vehicle, &nv->callback);
2790         vehicle_set_attr(nv->vehicle, &this_->self);
2791         return 1;
2792 }
2793
2794
2795
2796
2797 struct gui *
2798 navit_get_gui(struct navit *this_)
2799 {
2800         return this_->gui;
2801 }
2802
2803 struct transformation *
2804 navit_get_trans(struct navit *this_)
2805 {
2806         return this_->trans;
2807 }
2808
2809 struct route *
2810 navit_get_route(struct navit *this_)
2811 {
2812         return this_->route;
2813 }
2814
2815 struct navigation *
2816 navit_get_navigation(struct navit *this_)
2817 {
2818         return this_->navigation;
2819 }
2820
2821 struct displaylist *
2822 navit_get_displaylist(struct navit *this_)
2823 {
2824         return this_->displaylist;
2825 }
2826
2827 void
2828 navit_layout_switch(struct navit *n) 
2829 {
2830
2831     int currTs=0;
2832     struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2833     double trise,tset,trise_actual;
2834     struct layout *l;
2835     int year, month, day;
2836     
2837     if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2838         return; //No layout - nothing to switch
2839     }
2840     if (!n->vehicle)
2841         return;
2842     l=layout_attr.u.layout;
2843     
2844     if (l->dayname || l->nightname) {
2845         //Ok, we know that we have profile to switch
2846         
2847         //Check that we aren't calculating too fast
2848         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2849                 currTs=iso8601_to_secs(iso8601_attr.u.str);
2850                 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2851         }
2852         if (currTs-(n->prevTs)<60) {
2853             //We've have to wait a little
2854             return;
2855         }
2856         if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2857                 return;
2858         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2859                 return; //No valid fix yet
2860         }
2861         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2862                 //No position - no sun
2863                 return;
2864         }
2865         
2866         //We calculate sunrise anyway, cause it is needed both for day and for night
2867         if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2868                 //near the pole sun never rises/sets, so we should never switch profiles
2869                 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
2870                 n->prevTs=currTs;
2871                 return;
2872             }
2873         
2874         trise_actual=trise;
2875         dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2876         if (l->dayname) {
2877         
2878             if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) || 
2879                     (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2880                 //The sun is rising now!
2881                 if (strcmp(l->name,l->dayname)) {
2882                     navit_set_layout_by_name(n,l->dayname);
2883                 }
2884             }
2885         }
2886         if (l->nightname) {
2887             if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2888                 //near the pole sun never rises/sets, so we should never switch profiles
2889                 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
2890                 n->prevTs=currTs;
2891                 return;
2892             }
2893             dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2894             if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2895                 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) || 
2896                         ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2897                 //Time to sleep
2898                 if (strcmp(l->name,l->nightname)) {
2899                     navit_set_layout_by_name(n,l->nightname);
2900                 }
2901             }   
2902         }
2903         
2904         n->prevTs=currTs;
2905     }
2906 }
2907
2908 int 
2909 navit_set_vehicle_by_name(struct navit *n,const char *name) 
2910 {
2911     struct vehicle *v;
2912     struct attr_iter *iter;
2913     struct attr vehicle_attr, name_attr;
2914
2915         iter=navit_attr_iter_new();
2916
2917     while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
2918                 v=vehicle_attr.u.vehicle;
2919                 vehicle_get_attr(v,attr_name,&name_attr,NULL);
2920                 if (name_attr.type==attr_name) {
2921                         if (!strcmp(name,name_attr.u.str)) {
2922                                 navit_set_attr(n,&vehicle_attr);                                
2923                                 navit_attr_iter_destroy(iter);
2924                                 return 1;
2925                         }
2926                 }
2927         }
2928     navit_attr_iter_destroy(iter);
2929     return 0;
2930 }
2931
2932 int 
2933 navit_set_layout_by_name(struct navit *n,const char *name) 
2934 {
2935     struct layout *l;
2936     struct attr_iter iter;
2937     struct attr layout_attr;
2938
2939     iter.u.list=0x00;
2940
2941     if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2942         return 0; //No layouts - nothing to do
2943     }
2944     if (iter.u.list==NULL) {
2945         return 0;
2946     }
2947     
2948     iter.u.list=g_list_first(iter.u.list);
2949     
2950     while(iter.u.list) {
2951         l=(struct layout*)iter.u.list->data;
2952         if (!strcmp(name,l->name)) {
2953             layout_attr.u.layout=l;
2954             layout_attr.type=attr_layout;
2955             navit_set_attr(n,&layout_attr);
2956             iter.u.list=g_list_first(iter.u.list);
2957             return 1;
2958         }
2959         iter.u.list=g_list_next(iter.u.list);
2960     }
2961
2962     iter.u.list=g_list_first(iter.u.list);
2963     return 0;
2964 }
2965
2966 void
2967 navit_disable_suspend() {
2968         gui_disable_suspend(global_navit->gui);
2969         callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
2970 }
2971
2972 int
2973 navit_block(struct navit *this_, int block)
2974 {
2975         if (block > 0) {
2976                 this_->blocked |= 1;
2977                 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2978                         this_->blocked |= 2;
2979                 return 0;
2980         }
2981         if ((this_->blocked & 2) || block < 0) {
2982                 this_->blocked=0;
2983                 navit_draw(this_);
2984                 return 1;
2985         }
2986         this_->blocked=0;
2987         return 0;
2988 }
2989
2990 void
2991 navit_destroy(struct navit *this_)
2992 {
2993         struct mapset*ms;
2994         callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
2995
2996         /* TODO: destroy objects contained in this_ */
2997         if (this_->vehicle)
2998                 vehicle_destroy(this_->vehicle->vehicle);
2999         if (this_->bookmarks) {
3000                 char *center_file = bookmarks_get_center_file(TRUE);
3001                 bookmarks_write_center_to_file(this_->bookmarks, center_file);
3002                 g_free(center_file);
3003                 bookmarks_destroy(this_->bookmarks);
3004         }
3005         callback_destroy(this_->nav_speech_cb);
3006         callback_destroy(this_->roadbook_callback);
3007         callback_destroy(this_->popup_callback);
3008         callback_destroy(this_->motion_timeout_callback);
3009         callback_destroy(this_->progress_cb);
3010         if(this_->gra)
3011           graphics_remove_callback(this_->gra, this_->resize_callback);
3012         callback_destroy(this_->resize_callback);
3013         if(this_->gra)
3014           graphics_remove_callback(this_->gra, this_->button_callback);
3015         callback_destroy(this_->button_callback);
3016         if(this_->gra)
3017           graphics_remove_callback(this_->gra, this_->motion_callback);
3018         callback_destroy(this_->motion_callback);
3019         if(this_->gra)
3020           graphics_remove_callback(this_->gra, this_->predraw_callback);
3021         callback_destroy(this_->predraw_callback);
3022         route_destroy(this_->route);
3023         ms = navit_get_mapset(this_);
3024         if(ms)
3025                 mapset_destroy(ms);
3026         graphics_free(this_->gra);
3027         g_free(this_);
3028 }
3029
3030 /** @} */