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