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