Fix:Core:Do initial draw asynchronously
[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
701 static int
702 navit_cmd_zoom_in(struct navit *this_)
703 {
704         navit_zoom_in_cursor(this_, 2);
705         return 0;
706 }
707
708 static int
709 navit_cmd_zoom_out(struct navit *this_)
710 {
711         navit_zoom_out_cursor(this_, 2);
712         return 0;
713 }
714
715
716 static void
717 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
718 {
719         if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) 
720                 navit_say(this, in[0]->u.str);
721 }
722
723 static GHashTable *cmd_int_var_hash = NULL;
724
725 /**
726  * Store key value pair for the  command system
727  *
728  * @param navit The navit instance
729  * @param function unused (needed to match command function signiture)
730  * @param in input attributes in[0] is the key string, in[1] is the integer value to store
731  * @param out output attributes, unused 
732  * @param valid unused 
733  * @returns nothing
734  */
735 static void
736 navit_cmd_set_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
737 {
738         if(!cmd_int_var_hash) {
739                 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
740         }
741
742         if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
743              (in && in[1] && ATTR_IS_NUMERIC(in[1]->type))) {
744                 struct attr*val = g_new(struct attr,1);
745                 attr_dup_content(in[1],val);
746                 char*key = g_strdup(in[0]->u.str);
747                 g_hash_table_insert(cmd_int_var_hash, key, val);
748         }
749 }
750
751 /**
752  * Get value given a key string for the command system
753  *
754  * @param navit The navit instance
755  * @param function unused (needed to match command function signiture)
756  * @param in input attribute in[0] is the key string
757  * @param out output attribute, the value for the given key string if exists or 0  
758  * @param valid unused 
759  * @returns nothing
760  */
761 static void
762 navit_cmd_get_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
763 {
764         struct attr **list = g_new0(struct attr *,2);
765         if(!cmd_int_var_hash) {
766                 struct attr*val = g_new0(struct attr,1);
767                 val->type   = attr_type_int_begin;
768                 val->u.num  = 0;
769                 list[0]     = val;
770         }
771         if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
772                 struct attr*ret = g_hash_table_lookup(cmd_int_var_hash, in[0]->u.str);
773                 if(ret) {
774                         list[0] = ret;
775                 }
776                 else {
777                         struct attr*val = g_new0(struct attr,1);
778                         val->type   = attr_type_int_begin;
779                         val->u.num  = 0;
780                         list[0]   = val;
781                 }
782         }
783         list[1] = NULL;
784         *out = list;
785 }
786
787 GList *cmd_int_var_stack = NULL;
788
789 /**
790  * Push an integer to the stack for the command system
791  *
792  * @param navit The navit instance
793  * @param function unused (needed to match command function signiture)
794  * @param in input attribute in[0] is the integer attibute to push
795  * @param out output attributes, unused 
796  * @param valid unused 
797  * @returns nothing
798  */
799 static void
800 navit_cmd_push_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
801 {
802         if (in && in[0] && ATTR_IS_NUMERIC(in[0]->type)) {
803                 struct attr*val = g_new(struct attr,1);
804                 attr_dup_content(in[0],val);
805                 cmd_int_var_stack = g_list_prepend(cmd_int_var_stack, val);
806         }
807 }
808
809 /**
810  * Pop an integer from the command system's integer stack
811  *
812  * @param navit The navit instance
813  * @param function unused (needed to match command function signiture)
814  * @param in input attributes unused
815  * @param out output attribute, the value popped if stack isn't empty or 0
816  * @param valid unused 
817  * @returns nothing
818  */
819 static void
820 navit_cmd_pop_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
821 {
822         struct attr **list = g_new0(struct attr *,2);
823         if(!cmd_int_var_stack) {
824                 struct attr*val = g_new0(struct attr,1);
825                 val->type = attr_type_int_begin;
826                 val->u.num  = 0;
827                 list[0]   = val;
828         }
829         else {
830                 list[0] = cmd_int_var_stack->data;
831                 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
832         }
833         list[1] = NULL;
834         *out = list;
835 }
836
837 /**
838  * Get current size of command system's integer stack
839  *
840  * @param navit The navit instance
841  * @param function unused (needed to match command function signiture)
842  * @param in input attributes unused
843  * @param out output attribute, the size of stack
844  * @param valid unused 
845  * @returns nothing
846  */
847 static void
848 navit_cmd_int_stack_size(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
849 {
850         struct attr *attr  = g_new0(struct attr  ,1);
851         attr->type  = attr_type_int_begin;
852         if(!cmd_int_var_stack) {
853                 attr->u.num = 0; 
854         }
855         else {
856                 attr->u.num = g_list_length(cmd_int_var_stack); 
857         }
858         struct attr **list = g_new0(struct attr *,2);
859         list[0] = attr;
860         list[1] = NULL;
861         *out = list;
862         cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
863 }
864
865 static void
866 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
867 {
868         struct pcoord pc;
869         char *description=NULL;
870         if (!in)
871                 return;
872         if (!in[0])
873                 return;
874         pc.pro = transform_get_projection(this->trans);
875         if (ATTR_IS_COORD(in[0]->type)) {
876                 pc.x=in[0]->u.coord->x;
877                 pc.y=in[0]->u.coord->y;
878                 in++;
879         } else if (ATTR_IS_PCOORD(in[0]->type)) {
880                 pc=*in[0]->u.pcoord;
881                 in++;
882         } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
883                 pc.pro=in[0]->u.num;
884                 pc.x=in[1]->u.num;
885                 pc.y=in[2]->u.num;
886                 in+=3;
887         } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
888                 pc.x=in[0]->u.num;
889                 pc.y=in[1]->u.num;
890                 in+=2;
891         } else
892                 return;
893         if (in[0] && ATTR_IS_STRING(in[0]->type))
894                 description=in[0]->u.str;
895         navit_set_destination(this, &pc, description, 1);
896 }
897
898 static void
899 navit_cmd_fmt_coordinates(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
900 {
901         struct attr attr;
902         attr.type=attr_type_string_begin;
903         attr.u.str="Fix me";
904         if (out) {
905                 *out=attr_generic_add_attr(*out, &attr);
906         }
907 }
908
909 static struct command_table commands[] = {
910         {"zoom_in",command_cast(navit_cmd_zoom_in)},
911         {"zoom_out",command_cast(navit_cmd_zoom_out)},
912         {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
913         {"say",command_cast(navit_cmd_say)},
914         {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
915         {"set_destination",command_cast(navit_cmd_set_destination)},
916         {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
917         {"fmt_coordinates",command_cast(navit_cmd_fmt_coordinates)},
918         {"set_int_var",command_cast(navit_cmd_set_int_var)},
919         {"get_int_var",command_cast(navit_cmd_get_int_var)},
920         {"push_int",command_cast(navit_cmd_push_int)},
921         {"pop_int",command_cast(navit_cmd_pop_int)},
922         {"int_stack_size",command_cast(navit_cmd_int_stack_size)},
923 };
924         
925 void 
926 navit_command_add_table(struct navit*this_, struct command_table *commands, int count)
927 {
928   command_add_table(this_->attr_cbl, commands, count, this_);
929 }
930
931 struct navit *
932 navit_new(struct attr *parent, struct attr **attrs)
933 {
934         struct navit *this_=g_new0(struct navit, 1);
935         struct pcoord center;
936         struct coord co;
937         struct coord_geo g;
938         enum projection pro=projection_mg;
939         int zoom = 256;
940         g.lat=53.13;
941         g.lng=11.70;
942
943         this_->self.type=attr_navit;
944         this_->self.u.navit=this_;
945         this_->attr_cbl=callback_list_new();
946
947         this_->orientation=-1;
948         this_->tracking_flag=1;
949         this_->recentdest_count=10;
950         this_->osd_configuration=-1;
951
952         this_->center_timeout = 10;
953         this_->use_mousewheel = 1;
954         this_->autozoom_secs = 10;
955         this_->autozoom_min = 7;
956         this_->autozoom_active = 0;
957         this_->zoom_min = 1;
958         this_->zoom_max = 2097152;
959         this_->follow_cursor = 1;
960         this_->radius = 30;
961         this_->border = 16;
962
963         this_->trans = transform_new();
964         this_->trans_cursor = transform_new();
965         transform_from_geo(pro, &g, &co);
966         center.x=co.x;
967         center.y=co.y;
968         center.pro = pro;
969         
970         transform_setup(this_->trans, &center, zoom, (this_->orientation != -1) ? this_->orientation : 0);
971
972         this_->bookmarks=bookmarks_new(&this_->self, NULL, this_->trans);
973
974         this_->prevTs=0;
975
976         for (;*attrs; attrs++) {
977                 navit_set_attr_do(this_, *attrs, 1);
978         }
979         this_->displaylist=graphics_displaylist_new();
980         command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
981
982         this_->messages = messagelist_new(attrs);
983         
984         return this_;
985 }
986
987 static int
988 navit_set_gui(struct navit *this_, struct gui *gui)
989 {
990         if (this_->gui)
991                 return 0;
992         this_->gui=gui;
993         if (gui_has_main_loop(this_->gui)) {
994                 if (! main_loop_gui) {
995                         main_loop_gui=this_->gui;
996                 } else {
997                         dbg(0,"gui with main loop already active, ignoring this instance");
998                         return 0;
999                 }
1000         }
1001         return 1;
1002 }
1003
1004 void 
1005 navit_add_message(struct navit *this_, char *message)
1006 {
1007         message_new(this_->messages, message);
1008 }
1009
1010 struct message
1011 *navit_get_messages(struct navit *this_)
1012 {
1013         return message_get(this_->messages);
1014 }
1015
1016 static int
1017 navit_set_graphics(struct navit *this_, struct graphics *gra)
1018 {
1019         if (this_->gra)
1020                 return 0;
1021         this_->gra=gra;
1022         this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
1023         graphics_add_callback(gra, this_->resize_callback);
1024         this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
1025         graphics_add_callback(gra, this_->button_callback);
1026         this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
1027         graphics_add_callback(gra, this_->motion_callback);
1028         this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
1029         graphics_add_callback(gra, this_->predraw_callback);
1030         return 1;
1031 }
1032
1033 struct graphics *
1034 navit_get_graphics(struct navit *this_)
1035 {
1036         return this_->gra;
1037 }
1038
1039 struct vehicleprofile *
1040 navit_get_vehicleprofile(struct navit *this_)
1041 {
1042         return this_->vehicleprofile;
1043 }
1044
1045 GList *
1046 navit_get_vehicleprofiles(struct navit *this_)
1047 {
1048         return this_->vehicleprofiles;
1049 }
1050
1051 static void
1052 navit_projection_set(struct navit *this_, enum projection pro, int draw)
1053 {
1054         struct coord_geo g;
1055         struct coord *c;
1056
1057         c=transform_center(this_->trans);
1058         transform_to_geo(transform_get_projection(this_->trans), c, &g);
1059         transform_set_projection(this_->trans, pro);
1060         transform_from_geo(pro, &g, c);
1061         if (draw)
1062                 navit_draw(this_);
1063 }
1064
1065 /**
1066  * Start the route computing to a given set of coordinates
1067  *
1068  * @param navit The navit instance
1069  * @param c The coordinate to start routing to
1070  * @param description A label which allows the user to later identify this destination in the former destinations selection
1071  * @returns nothing
1072  */
1073 void
1074 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1075 {
1076         if (c) {
1077                 this_->destination=*c;
1078                 this_->destination_valid=1;
1079         } else
1080                 this_->destination_valid=0;
1081         char *destination_file = bookmarks_get_destination_file(TRUE);
1082         bookmarks_append_coord(this_->bookmarks, destination_file, c, 1, "former_destination", description, NULL, this_->recentdest_count);
1083         g_free(destination_file);
1084         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1085         if (this_->route) {
1086                 route_set_destination(this_->route, c, async);
1087
1088                 if (this_->ready == 3)
1089                         navit_draw(this_);
1090         }
1091 }
1092
1093 /**
1094  * Start the route computing to a given set of coordinates including waypoints
1095  *
1096  * @param navit The navit instance
1097  * @param c The coordinate to start routing to
1098  * @param description A label which allows the user to later identify this destination in the former destinations selection
1099  * @returns nothing
1100  */
1101 void
1102 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1103 {
1104         if (c && count) {
1105                 this_->destination=c[count-1];
1106                 this_->destination_valid=1;
1107         } else
1108                 this_->destination_valid=0;
1109         char *destination_file = bookmarks_get_destination_file(TRUE);
1110         bookmarks_append_coord(this_->bookmarks, destination_file, c, count, "former_itinerary", description, NULL, this_->recentdest_count);
1111         g_free(destination_file);
1112         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1113         if (this_->route) {
1114                 route_set_destinations(this_->route, c, count, async);
1115
1116                 if (this_->ready == 3)
1117                         navit_draw(this_);
1118         }
1119 }
1120
1121 /**
1122  * @brief Checks if a route is calculated
1123  *
1124  * This function checks if a route is calculated.
1125  *
1126  * @param this_ The navit struct whose route should be checked.
1127  * @return True if the route is set, false otherwise.
1128  */
1129 int
1130 navit_check_route(struct navit *this_)
1131 {
1132         if (this_->route) {
1133                 return route_get_path_set(this_->route);
1134         }
1135
1136         return 0;
1137 }
1138
1139 static int
1140 navit_former_destinations_active(struct navit *this_)
1141 {
1142         char *destination_file = bookmarks_get_destination_file(FALSE);
1143         FILE *f;
1144         int active=0;
1145         char buffer[3];
1146         f=fopen(destination_file,"r");
1147         if (f) {
1148                 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n')) 
1149                         active=1;
1150                 fclose(f);
1151         }
1152         g_free(destination_file);
1153         return active;
1154 }
1155
1156 static void
1157 navit_add_former_destinations_from_file(struct navit *this_)
1158 {
1159         char *destination_file = bookmarks_get_destination_file(FALSE);
1160         struct attr parent={attr_navit, .u.navit=this_};
1161         struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}}, flags={attr_flags, {(void *)1}};
1162         struct attr *attrs[]={&type, &data, &flags, NULL};
1163         struct map_rect *mr;
1164         struct item *item;
1165         int i,valid=0,count=0;
1166         struct coord c[16];
1167         struct pcoord pc[16];
1168
1169         this_->former_destination=map_new(&parent, attrs);
1170         g_free(destination_file);
1171         if (!this_->route || !navit_former_destinations_active(this_))
1172                 return; 
1173         mr=map_rect_new(this_->former_destination, NULL);
1174         while ((item=map_rect_get_item(mr))) {
1175                 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16))) 
1176                         valid=1;
1177         }
1178         map_rect_destroy(mr);
1179         if (valid && count > 0) {
1180                 for (i = 0 ; i < count ; i++) {
1181                         pc[i].pro=map_projection(this_->former_destination);
1182                         pc[i].x=c[i].x;
1183                         pc[i].y=c[i].y;
1184                 }
1185                 if (count == 1)
1186                         route_set_destination(this_->route, &pc[0], 1);
1187                 else
1188                         route_set_destinations(this_->route, pc, count, 1);
1189                 this_->destination=pc[count-1];
1190                 this_->destination_valid=1;
1191         }
1192 }
1193
1194
1195 void
1196 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1197 {
1198         va_list ap;
1199         char *str1,*str2;
1200         va_start(ap, fmt);
1201         if (this_->textfile_debug_log && this_->vehicle) {
1202                 str1=g_strdup_vprintf(fmt, ap);
1203                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1204                 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1205                 g_free(str2);
1206                 g_free(str1);
1207         }
1208         va_end(ap);
1209 }
1210
1211 int 
1212 navit_speech_estimate(struct navit *this_, char *str)
1213 {
1214         return speech_estimate_duration(this_->speech, str);
1215 }
1216
1217 void
1218 navit_say(struct navit *this_, char *text)
1219 {
1220         if(this_->speech) {
1221                 speech_say(this_->speech, text);
1222         }
1223 }
1224
1225 /**
1226  * @brief Toggles the navigation announcer for navit
1227  * @param this_ The navit object
1228  */
1229 static void
1230 navit_cmd_announcer_toggle(struct navit *this_)
1231 {
1232     struct attr attr, speechattr;
1233
1234     // search for the speech attribute
1235     if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1236         return;
1237     // find out if the corresponding attribute attr_active has been set
1238     if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1239         // flip it then...
1240         attr.u.num = !attr.u.num;
1241     } else {
1242         // otherwise disable it because voice is enabled by default
1243         attr.type = attr_active;
1244         attr.u.num = 0;
1245     }
1246
1247     // apply the new state
1248     if(!speech_set_attr(speechattr.u.speech, &attr))
1249         return;
1250
1251     // announce that the speech attribute has changed
1252     callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1253 }
1254
1255 void
1256 navit_speak(struct navit *this_)
1257 {
1258         struct navigation *nav=this_->navigation;
1259         struct map *map=NULL;
1260         struct map_rect *mr=NULL;
1261         struct item *item;
1262         struct attr attr;
1263
1264     if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1265         attr.u.num = 1;
1266     dbg(1, "this_.speech->active %i\n", attr.u.num);
1267     if(!attr.u.num)
1268         return;
1269
1270         if (nav)
1271                 map=navigation_get_map(nav);
1272         if (map)
1273                 mr=map_rect_new(map, NULL);
1274         if (mr) {
1275                 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1276                 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1277                         speech_say(this_->speech, attr.u.str);
1278                         navit_add_message(this_, attr.u.str);
1279                         navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1280                 }
1281                 map_rect_destroy(mr);
1282         }
1283 }
1284
1285 static void
1286 navit_window_roadbook_update(struct navit *this_)
1287 {
1288         struct navigation *nav=this_->navigation;
1289         struct map *map=NULL;
1290         struct map_rect *mr=NULL;
1291         struct item *item;
1292         struct attr attr;
1293         struct param_list param[5];
1294         int secs;
1295
1296         dbg(1,"enter\n");
1297         datawindow_mode(this_->roadbook_window, 1);
1298         if (nav)
1299                 map=navigation_get_map(nav);
1300         if (map)
1301                 mr=map_rect_new(map, NULL);
1302         dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1303         if (mr) {
1304                 dbg(0,"while loop\n");
1305                 while ((item=map_rect_get_item(mr))) {
1306                         dbg(0,"item=%p\n", item);
1307                         attr.u.str=NULL;
1308                         if (item->type != type_nav_position) {
1309                                 item_attr_get(item, attr_navigation_long, &attr);
1310                                 if (attr.u.str == NULL) {
1311                                         continue;
1312                                 }
1313                                 dbg(2, "Command='%s'\n", attr.u.str);
1314                                 param[0].value=g_strdup(attr.u.str);
1315                         } else
1316                                 param[0].value=_("Position");
1317                         param[0].name=_("Command");
1318
1319                         item_attr_get(item, attr_length, &attr);
1320                         dbg(2, "Length=%d\n", attr.u.num);
1321                         param[1].name=_("Length");
1322
1323                         if ( attr.u.num >= 2000 )
1324                         {
1325                                 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1326                         }
1327                         else
1328                         {
1329                                 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1330                         }
1331
1332                         item_attr_get(item, attr_time, &attr);
1333                         dbg(2, "Time=%d\n", attr.u.num);
1334                         secs=attr.u.num/10;
1335                         param[2].name=_("Time");
1336                         if ( secs >= 3600 )
1337                         {
1338                                 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1339                         }
1340                         else
1341                         {
1342                                 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1343                         }
1344
1345                         item_attr_get(item, attr_destination_length, &attr);
1346                         dbg(2, "Destlength=%d\n", attr.u.num);
1347                         param[3].name=_("Destination Length");
1348                         if ( attr.u.num >= 2000 )
1349                         {
1350                                 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1351                         }
1352                         else
1353                         {
1354                                 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1355                         }
1356
1357                         item_attr_get(item, attr_destination_time, &attr);
1358                         dbg(2, "Desttime=%d\n", attr.u.num);
1359                         secs=attr.u.num/10;
1360                         param[4].name=_("Destination Time");
1361                         if ( secs >= 3600 )
1362                         {
1363                                 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1364                         }
1365                         else
1366                         {
1367                                 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1368                         }
1369                         datawindow_add(this_->roadbook_window, param, 5);
1370                 }
1371                 map_rect_destroy(mr);
1372         }
1373         datawindow_mode(this_->roadbook_window, 0);
1374 }
1375
1376 void
1377 navit_window_roadbook_destroy(struct navit *this_)
1378 {
1379         dbg(0, "enter\n");
1380         navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1381         this_->roadbook_window=NULL;
1382         this_->roadbook_callback=NULL;
1383 }
1384 void
1385 navit_window_roadbook_new(struct navit *this_)
1386 {
1387         if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1388                 return;
1389         }
1390
1391         this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1392         navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1393         this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1394         navit_window_roadbook_update(this_);
1395 }
1396
1397 void
1398 navit_init(struct navit *this_)
1399 {
1400         struct mapset *ms;
1401         struct map *map;
1402         int callback;
1403
1404         dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1405
1406         if (!this_->gui && !(this_->flags & 2)) {
1407                 dbg(0,"no gui\n");
1408                 navit_destroy(this_);
1409                 return;
1410         }
1411         if (!this_->gra && !(this_->flags & 1)) {
1412                 dbg(0,"no graphics\n");
1413                 navit_destroy(this_);
1414                 return;
1415         }
1416         dbg(2,"Connecting gui to graphics\n");
1417         if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1418                 struct attr attr_type_gui, attr_type_graphics;
1419                 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1420                 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1421                 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1422                 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1423                 dbg(0," for explanations and solutions\n");
1424
1425                 navit_destroy(this_);
1426                 return;
1427         }
1428         dbg(2,"Initializing graphics\n");
1429         dbg(2,"Setting Vehicle\n");
1430         navit_set_vehicle(this_, this_->vehicle);
1431         dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1432         if (this_->mapsets) {
1433                 struct mapset_handle *msh;
1434                 ms=this_->mapsets->data;
1435                 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1436                 msh=mapset_open(ms);
1437                 while (msh && (map=mapset_next(msh, 0))) {
1438                         map_add_callback(map, this_->progress_cb);
1439                 }
1440                 mapset_close(msh);
1441                 
1442                 if (this_->route) {
1443                         if ((map=route_get_map(this_->route)))
1444                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1445                         if ((map=route_get_graph_map(this_->route))) {
1446                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1447                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1448                         }
1449                         route_set_mapset(this_->route, ms);
1450                         route_set_projection(this_->route, transform_get_projection(this_->trans));
1451                 }
1452                 if (this_->tracking) {
1453                         tracking_set_mapset(this_->tracking, ms);
1454                         if (this_->route)
1455                                 tracking_set_route(this_->tracking, this_->route);
1456                 }
1457                 if (this_->navigation) {
1458                         if ((map=navigation_get_map(this_->navigation))) {
1459                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1460                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1461                         }
1462                 }
1463                 if (this_->tracking) {
1464                         if ((map=tracking_get_map(this_->tracking))) {
1465                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1466                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1467                         }
1468                 }
1469                 navit_add_former_destinations_from_file(this_);
1470         }
1471         if (this_->route) {
1472                 struct attr callback;
1473                 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1474                 callback.type=attr_callback;
1475                 callback.u.callback=this_->route_cb;
1476                 route_add_attr(this_->route, &callback);
1477         }
1478         if (this_->navigation) {
1479                 if (this_->speech) {
1480                         this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1481                         navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1482                 }
1483                 if (this_->route)
1484                         navigation_set_route(this_->navigation, this_->route);
1485         }
1486         dbg(2,"Setting Center\n");
1487         char *center_file = bookmarks_get_center_file(FALSE);
1488         bookmarks_set_center_from_file(this_->bookmarks, center_file);
1489         g_free(center_file);
1490 #if 0
1491         if (this_->menubar) {
1492                 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1493                 if (men) {
1494                         navit_add_menu_windows_items(this_, men);
1495                 }
1496         }
1497 #endif
1498         global_navit=this_;
1499 #if 0
1500         navit_window_roadbook_new(this_);
1501         navit_window_items_new(this_);
1502 #endif
1503
1504         messagelist_init(this_->messages);
1505
1506         navit_set_cursors(this_);
1507
1508         callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1509         callback=(this_->ready == 2);
1510         this_->ready|=1;
1511         dbg(2,"ready=%d\n",this_->ready);
1512         if (this_->ready == 3)
1513                 navit_draw_async(this_, 1);
1514         if (callback)
1515                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1516 #if 0
1517         routech_test(this_);
1518 #endif
1519 }
1520
1521 void
1522 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
1523 {
1524         struct coord c;
1525         int scale=16;
1526
1527         c.x=(r->rl.x+r->lu.x)/2;
1528         c.y=(r->rl.y+r->lu.y)/2;
1529         transform_set_center(this_->trans, &c);
1530         dbg(1,"%x,%x-%x,%x\n", r->rl.x,r->rl.y,r->lu.x,r->lu.y);
1531         while (scale < 1<<20) {
1532                 struct point p1,p2;
1533                 transform_set_scale(this_->trans, scale);
1534                 transform_setup_source_rect(this_->trans);
1535                 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
1536                 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
1537                 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1538                 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1539                     p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1540                         scale*=2;
1541                 else
1542                         break;
1543         
1544         }
1545         if (this_->ready == 3)
1546                 navit_draw_async(this_,0);
1547 }
1548
1549 void
1550 navit_zoom_to_route(struct navit *this_, int orientation)
1551 {
1552         struct map *map;
1553         struct map_rect *mr=NULL;
1554         struct item *item;
1555         struct coord c;
1556         struct coord_rect r;
1557         int count=0;
1558         if (! this_->route)
1559                 return;
1560         dbg(1,"enter\n");
1561         map=route_get_map(this_->route);
1562         dbg(1,"map=%p\n",map);
1563         if (map)
1564                 mr=map_rect_new(map, NULL);
1565         dbg(1,"mr=%p\n",mr);
1566         if (mr) {
1567                 while ((item=map_rect_get_item(mr))) {
1568                         dbg(1,"item=%s\n", item_to_name(item->type));
1569                         while (item_coord_get(item, &c, 1)) {
1570                                 dbg(1,"coord\n");
1571                                 if (!count) 
1572                                         r.lu=r.rl=c;
1573                                 else
1574                                         coord_rect_extend(&r, &c);      
1575                                 count++;
1576                         }
1577                 }
1578         }
1579         if (! count)
1580                 return;
1581         if (orientation != -1)
1582                 transform_set_yaw(this_->trans, orientation);
1583         navit_zoom_to_rect(this_, &r);
1584 }
1585
1586 static void
1587 navit_cmd_zoom_to_route(struct navit *this)
1588 {
1589         navit_zoom_to_route(this, 0);
1590 }
1591
1592
1593 /**
1594  * Change the current zoom level
1595  *
1596  * @param navit The navit instance
1597  * @param center The point where to center the map, including its projection
1598  * @returns nothing
1599  */
1600 void
1601 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1602 {
1603         struct coord *c=transform_center(this_->trans);
1604         struct coord c1,c2;
1605         enum projection pro = transform_get_projection(this_->trans);
1606         if (pro != center->pro) {
1607                 c1.x = center->x;
1608                 c1.y = center->y;
1609                 transform_from_to(&c1, center->pro, &c2, pro);
1610         } else {
1611                 c2.x = center->x;
1612                 c2.y = center->y;
1613         }
1614         *c=c2;
1615         if (set_timeout) 
1616                 navit_set_timeout(this_);
1617         if (this_->ready == 3)
1618                 navit_draw(this_);
1619 }
1620
1621 static void
1622 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1623 {
1624         int width, height;
1625         struct point po;
1626         transform_set_center(this_->trans, c);
1627         transform_get_size(this_->trans, &width, &height);
1628         po.x=width/2;
1629         po.y=height/2;
1630         update_transformation(this_->trans, &po, p, NULL);
1631         if (set_timeout)
1632                 navit_set_timeout(this_);
1633 }
1634
1635 /**
1636  * Links all vehicles to a cursor depending on the current profile.
1637  *
1638  * @param this_ A navit instance
1639  * @author Ralph Sennhauser (10/2009)
1640  */
1641 static void
1642 navit_set_cursors(struct navit *this_)
1643 {
1644         struct attr name;
1645         struct navit_vehicle *nv;
1646         struct cursor *c;
1647         GList *v;
1648
1649         v=g_list_first(this_->vehicles); // GList of navit_vehicles
1650         while (v) {
1651                 nv=v->data;
1652                 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
1653                         if (!strcmp(name.u.str,"none"))
1654                                 c=NULL;
1655                         else
1656                                 c=layout_get_cursor(this_->layout_current, name.u.str);
1657                 } else
1658                         c=layout_get_cursor(this_->layout_current, "default");
1659                 vehicle_set_cursor(nv->vehicle, c);
1660                 v=g_list_next(v);
1661         }
1662         return;
1663 }
1664
1665 static int
1666 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
1667 {
1668         int width, height;
1669         struct navit_vehicle *nv=this_->vehicle;
1670
1671         float offset=this_->radius;      // Cursor offset from the center of the screen (percent).
1672 #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 */
1673         float min_offset = 0.;      // Percent offset at min_offset_speed.
1674         float max_offset = 30.;     // Percent offset at max_offset_speed.
1675         int min_offset_speed = 2;   // Speed in km/h
1676         int max_offset_speed = 50;  // Speed ini km/h
1677         // Calculate cursor offset from the center of the screen, upon speed.
1678         if (nv->speed <= min_offset_speed) {
1679             offset = min_offset;
1680         } else if (nv->speed > max_offset_speed) {
1681             offset = max_offset;
1682         } else {
1683             offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1684         }
1685 #endif
1686
1687         transform_get_size(this_->trans, &width, &height);
1688         if (this_->orientation == -1 || keep_orientation) {
1689                 p->x=50*width/100;
1690                 p->y=(50 + offset)*height/100;
1691                 if (dir) 
1692                         *dir=keep_orientation?this_->orientation:nv->dir;
1693         } else {
1694                 int mdir;
1695                 if (this_->tracking && this_->tracking_flag) {
1696                         mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1697                 } else {
1698                         mdir=nv->dir-this_->orientation;
1699                 }
1700
1701                 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1702                 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1703                 if (dir)
1704                         *dir=this_->orientation;
1705         }
1706         return 1;
1707 }
1708
1709 void
1710 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
1711 {
1712         int dir;
1713         struct point pn;
1714         struct navit_vehicle *nv=this_->vehicle;
1715         navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
1716         transform_set_yaw(this_->trans, dir);
1717         navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1718         if (autozoom)
1719                 navit_autozoom(this_, &nv->coord, nv->speed, 0);
1720 }
1721
1722 static void
1723 navit_set_center_cursor_draw(struct navit *this_)
1724 {
1725         navit_set_center_cursor(this_,1,0);
1726         if (this_->ready == 3)
1727                 navit_draw_async(this_, 1);
1728 }
1729
1730 static void
1731 navit_cmd_set_center_cursor(struct navit *this_)
1732 {
1733         navit_set_center_cursor_draw(this_);
1734 }
1735
1736 void
1737 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1738 {
1739         struct coord c;
1740         struct pcoord pc;
1741         transform_reverse(this_->trans, p, &c);
1742         pc.x = c.x;
1743         pc.y = c.y;
1744         pc.pro = transform_get_projection(this_->trans);
1745         navit_set_center(this_, &pc, set_timeout);
1746 }
1747
1748 #if 0
1749                 switch((*attrs)->type) {
1750                 case attr_zoom:
1751                         zoom=(*attrs)->u.num;
1752                         break;
1753                 case attr_center:
1754                         g=*((*attrs)->u.coord_geo);
1755                         break;
1756 #endif
1757
1758 static int
1759 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1760 {
1761         int dir=0, orient_old=0, attr_updated=0;
1762         struct coord co;
1763         long zoom;
1764         GList *l;
1765         struct navit_vehicle *nv;
1766         struct attr active=(struct attr){attr_active,{(void *)0}};
1767         struct layout *lay;
1768
1769         switch (attr->type) {
1770         case attr_autozoom:
1771                 attr_updated=(this_->autozoom_secs != attr->u.num);
1772                 this_->autozoom_secs = attr->u.num;
1773                 break;
1774         case attr_autozoom_active:
1775                 attr_updated=(this_->autozoom_active != attr->u.num);
1776                 this_->autozoom_active = attr->u.num;
1777                 break;
1778         case attr_center:
1779                 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1780                 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1781                 transform_set_center(this_->trans, &co);
1782                 break;
1783         case attr_drag_bitmap:
1784                 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1785                 this_->drag_bitmap=!!attr->u.num;
1786                 break;
1787         case attr_flags:
1788                 attr_updated=(this_->flags != attr->u.num);
1789                 this_->flags=attr->u.num;
1790                 break;
1791         case attr_flags_graphics:
1792                 attr_updated=(this_->graphics_flags != attr->u.num);
1793                 this_->graphics_flags=attr->u.num;
1794                 break;
1795         case attr_follow:
1796                 if (!this_->vehicle)
1797                         return 0;
1798                 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1799                 this_->vehicle->follow_curr = attr->u.num;
1800                 break;
1801         case attr_layout:
1802                 if(this_->layout_current!=attr->u.layout) {
1803                         this_->layout_current=attr->u.layout;
1804                         graphics_font_destroy_all(this_->gra);
1805                         navit_set_cursors(this_);
1806                         if (this_->ready == 3)
1807                                 navit_draw(this_);
1808                         attr_updated=1;
1809                 }
1810                 break;
1811         case attr_layout_name:
1812                 l=this_->layouts;
1813                 while (l) {
1814                         lay=l->data;
1815                         if (!strcmp(lay->name,attr->u.str)) {
1816                                 struct attr attr;
1817                                 attr.type=attr_layout;
1818                                 attr.u.layout=lay;
1819                                 return navit_set_attr_do(this_, &attr, init);
1820                         }
1821                         l=g_list_next(l);
1822                 }               
1823                 return 0;
1824         case attr_map_border:
1825                 if (this_->border != attr->u.num) {
1826                         this_->border=attr->u.num;
1827                         attr_updated=1;
1828                 }
1829                 break;
1830         case attr_orientation:
1831                 orient_old=this_->orientation;
1832                 this_->orientation=attr->u.num;
1833                 if (!init) {
1834                         if (this_->orientation != -1) {
1835                                 dir = this_->orientation;
1836                         } else {
1837                                 if (this_->vehicle) {
1838                                         dir = this_->vehicle->dir;
1839                                 }
1840                         }
1841                         transform_set_yaw(this_->trans, dir);
1842                         if (orient_old != this_->orientation) {
1843 #if 0
1844                                 if (this_->ready == 3)
1845                                         navit_draw(this_);
1846 #endif
1847                                 attr_updated=1;
1848                         }
1849                 }
1850                 break;
1851         case attr_osd_configuration:
1852                 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1853                 attr_updated=(this_->osd_configuration != attr->u.num);
1854                 this_->osd_configuration=attr->u.num;
1855                 break;
1856         case attr_pitch:
1857                 attr_updated=(this_->pitch != attr->u.num);
1858                 this_->pitch=attr->u.num;
1859                 transform_set_pitch(this_->trans, this_->pitch);
1860                 if (!init && attr_updated && this_->ready == 3)
1861                         navit_draw(this_);
1862                 break;
1863         case attr_projection:
1864                 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1865                         navit_projection_set(this_, attr->u.projection, !init);
1866                         attr_updated=1;
1867                 }
1868                 break;
1869         case attr_radius:
1870                 attr_updated=(this_->radius != attr->u.num);
1871                 this_->radius=attr->u.num;
1872                 break;
1873         case attr_recent_dest:
1874                 attr_updated=(this_->recentdest_count != attr->u.num);
1875                 this_->recentdest_count=attr->u.num;
1876                 break;
1877         case attr_speech:
1878                 if(this_->speech && this_->speech != attr->u.speech) {
1879                         attr_updated=1;
1880                         this_->speech = attr->u.speech;
1881                 }
1882                 break;
1883         case attr_timeout:
1884                 attr_updated=(this_->center_timeout != attr->u.num);
1885                 this_->center_timeout = attr->u.num;
1886                 break;
1887         case attr_tracking:
1888                 attr_updated=(this_->tracking_flag != !!attr->u.num);
1889                 this_->tracking_flag=!!attr->u.num;
1890                 break;
1891         case attr_transformation:
1892                 this_->trans=attr->u.transformation;
1893                 break;
1894         case attr_use_mousewheel:
1895                 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1896                 this_->use_mousewheel=!!attr->u.num;
1897                 break;
1898         case attr_vehicle:
1899                 l=this_->vehicles;
1900                 while(l) {
1901                         nv=l->data;
1902                         if (nv->vehicle == attr->u.vehicle) {
1903                                 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1904                                         if (this_->vehicle)
1905                                                 vehicle_set_attr(this_->vehicle->vehicle, &active);
1906                                         active.u.num=1;
1907                                         vehicle_set_attr(nv->vehicle, &active);
1908                                         attr_updated=1;
1909                                 }
1910                                 navit_set_vehicle(this_, nv);
1911                         }
1912                         l=g_list_next(l);
1913                 }
1914                 break;
1915         case attr_zoom:
1916                 zoom=transform_get_scale(this_->trans);
1917                 attr_updated=(zoom != attr->u.num);
1918                 transform_set_scale(this_->trans, attr->u.num);
1919                 if (attr_updated && !init) 
1920                         navit_draw(this_);
1921                 break;
1922         case attr_zoom_min:
1923                 attr_updated=(attr->u.num != this_->zoom_min);
1924                 this_->zoom_min=attr->u.num;
1925                 break;
1926         case attr_zoom_max:
1927                 attr_updated=(attr->u.num != this_->zoom_max);
1928                 this_->zoom_max=attr->u.num;
1929                 break;
1930         case attr_message:
1931                 navit_add_message(this_, attr->u.str);
1932                 break;
1933         case attr_follow_cursor:
1934                 attr_updated=(this_->follow_cursor != !!attr->u.num);
1935                 this_->follow_cursor=!!attr->u.num;
1936                 break;
1937         default:
1938                 return 0;
1939         }
1940         if (attr_updated && !init) {
1941                 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1942                 if (attr->type == attr_osd_configuration)
1943                         graphics_draw_mode(this_->gra, draw_mode_end);
1944         }
1945         return 1;
1946 }
1947
1948 int
1949 navit_set_attr(struct navit *this_, struct attr *attr)
1950 {
1951         return navit_set_attr_do(this_, attr, 0);
1952 }
1953
1954 int
1955 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1956 {
1957         struct message *msg;
1958         int len,offset;
1959         int ret=1;
1960
1961         switch (type) {
1962         case attr_message:
1963                 msg = navit_get_messages(this_);
1964                 
1965                 if (!msg) {
1966                         return 0;
1967                 }
1968
1969                 len = 0;
1970                 while (msg) {
1971                         len += strlen(msg->text) + 1;
1972                         msg = msg->next;
1973                 }
1974                 attr->u.str = g_malloc(len + 1);
1975                 
1976                 msg = navit_get_messages(this_);
1977                 offset = 0;
1978                 while (msg) {
1979                         g_stpcpy((attr->u.str + offset), msg->text);
1980                         offset += strlen(msg->text);
1981                         attr->u.str[offset] = '\n';
1982                         offset++;
1983
1984                         msg = msg->next;
1985                 }
1986
1987                 attr->u.str[len] = '\0';
1988                 break;
1989         case attr_bookmark_map:
1990                 attr->u.map=bookmarks_get_map(this_->bookmarks);
1991                 break;
1992         case attr_bookmarks:
1993                 attr->u.bookmarks=this_->bookmarks;
1994                 break;
1995         case attr_callback_list:
1996                 attr->u.callback_list=this_->attr_cbl;
1997                 break;
1998         case attr_destination:
1999                 if (! this_->destination_valid)
2000                         return 0;
2001                 attr->u.pcoord=&this_->destination;
2002                 break;
2003         case attr_displaylist:
2004                 attr->u.displaylist=this_->displaylist;
2005                 return (attr->u.displaylist != NULL);
2006         case attr_follow:
2007                 if (!this_->vehicle)
2008                         return 0;
2009                 attr->u.num=this_->vehicle->follow_curr;
2010                 break;
2011         case attr_former_destination_map:
2012                 attr->u.map=this_->former_destination;
2013                 break;
2014         case attr_graphics:
2015                 attr->u.graphics=this_->gra;
2016                 ret=(attr->u.graphics != NULL);
2017                 break;
2018         case attr_gui:
2019                 attr->u.gui=this_->gui;
2020                 ret=(attr->u.gui != NULL);
2021                 break;
2022         case attr_layout:
2023                 if (iter) {
2024                         if (iter->u.list) {
2025                                 iter->u.list=g_list_next(iter->u.list);
2026                         } else { 
2027                                 iter->u.list=this_->layouts;
2028                         }
2029                         if (!iter->u.list)
2030                                 return 0;
2031                         attr->u.layout=(struct layout *)iter->u.list->data;
2032                 } else {
2033                         attr->u.layout=this_->layout_current;
2034                 }
2035                 break;
2036         case attr_map:
2037                 if (iter && this_->mapsets) {
2038                         if (!iter->u.mapset_handle) {
2039                                 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2040                         }
2041                         attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2042                         if(!attr->u.map) {
2043                                 mapset_close(iter->u.mapset_handle);
2044                                 return 0;
2045                         }
2046                 } else {
2047                         return 0;
2048                 }
2049                 break;
2050         case attr_mapset:
2051                 attr->u.mapset=this_->mapsets->data;
2052                 ret=(attr->u.mapset != NULL);
2053                 break;
2054         case attr_navigation:
2055                 attr->u.navigation=this_->navigation;
2056                 break;
2057         case attr_orientation:
2058                 attr->u.num=this_->orientation;
2059                 break;
2060         case attr_osd_configuration:
2061                 attr->u.num=this_->osd_configuration;
2062                 break;
2063         case attr_pitch:
2064                 attr->u.num=transform_get_pitch(this_->trans);
2065                 break;
2066         case attr_projection:
2067                 if(this_->trans) {
2068                         attr->u.num=transform_get_projection(this_->trans);
2069                 } else {
2070                         return 0;
2071                 }
2072                 break;
2073         case attr_route:
2074                 attr->u.route=this_->route;
2075                 break;
2076         case attr_speech:
2077                 attr->u.speech=this_->speech;
2078                 break;
2079         case attr_tracking:
2080                 attr->u.num=this_->tracking_flag;
2081                 break;
2082         case attr_trackingo:
2083                 attr->u.tracking=this_->tracking;
2084                 break;
2085         case attr_transformation:
2086                 attr->u.transformation=this_->trans;
2087                 break;
2088         case attr_vehicle:
2089                 if(iter) {
2090                         if(iter->u.list) {
2091                                 iter->u.list=g_list_next(iter->u.list);
2092                         } else { 
2093                                 iter->u.list=this_->vehicles;
2094                         }
2095                         if(!iter->u.list)
2096                                 return 0;
2097                         attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2098                 } else {
2099                         if(this_->vehicle) {
2100                                 attr->u.vehicle=this_->vehicle->vehicle;
2101                         } else {
2102                                 return 0;
2103                         }
2104                 }
2105                 break;
2106         case attr_vehicleprofile:
2107                 attr->u.vehicleprofile=this_->vehicleprofile;
2108                 break;
2109         case attr_zoom:
2110                 attr->u.num=transform_get_scale(this_->trans);
2111                 break;
2112         case attr_autozoom_active:
2113                 attr->u.num=this_->autozoom_active;
2114                 break;
2115         case attr_follow_cursor:
2116                 attr->u.num=this_->follow_cursor;
2117                 break;
2118         default:
2119                 return 0;
2120         }
2121         attr->type=type;
2122         return ret;
2123 }
2124
2125 static int
2126 navit_add_log(struct navit *this_, struct log *log)
2127 {
2128         struct attr type_attr;
2129         if (!log_get_attr(log, attr_type, &type_attr, NULL))
2130                 return 0;
2131         if (!strcmp(type_attr.u.str, "textfile_debug")) {
2132                 char *header = "type=track_tracked\n";
2133                 if (this_->textfile_debug_log)
2134                         return 0;
2135                 log_set_header(log, header, strlen(header));
2136                 this_->textfile_debug_log=log;
2137                 return 1;
2138         }
2139         return 0;
2140 }
2141
2142 int
2143 navit_add_attr(struct navit *this_, struct attr *attr)
2144 {
2145         int ret=1;
2146         switch (attr->type) {
2147         case attr_callback:
2148                 navit_add_callback(this_, attr->u.callback);
2149                 break;
2150         case attr_log:
2151                 ret=navit_add_log(this_, attr->u.log);
2152                 break;
2153         case attr_gui:
2154                 ret=navit_set_gui(this_, attr->u.gui);
2155                 break;
2156         case attr_graphics:
2157                 ret=navit_set_graphics(this_, attr->u.graphics);
2158                 break;
2159         case attr_layout:
2160                 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
2161                 if(!this_->layout_current) 
2162                         this_->layout_current=attr->u.layout;
2163                 break;
2164         case attr_route:
2165                 this_->route=attr->u.route;
2166                 break;
2167         case attr_mapset:
2168                 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2169                 break;
2170         case attr_navigation:
2171                 this_->navigation=attr->u.navigation;
2172                 break;
2173         case attr_recent_dest:
2174                 this_->recentdest_count = attr->u.num;
2175                 break;
2176         case attr_speech:
2177                 this_->speech=attr->u.speech;
2178                 break;
2179         case attr_tracking:
2180                 this_->tracking=attr->u.tracking;
2181                 break;
2182         case attr_vehicle:
2183                 ret=navit_add_vehicle(this_, attr->u.vehicle);
2184                 break;
2185         case attr_vehicleprofile:
2186                 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2187                 break;
2188         case attr_autozoom_min:
2189                 this_->autozoom_min = attr->u.num;
2190                 break;
2191         default:
2192                 return 0;
2193         }
2194         callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2195         return ret;
2196 }
2197
2198 int
2199 navit_remove_attr(struct navit *this_, struct attr *attr)
2200 {
2201         int ret=1;
2202         switch (attr->type) {
2203         case attr_callback:
2204                 navit_remove_callback(this_, attr->u.callback);
2205                 break;
2206         default:
2207                 return 0;
2208         }
2209         return ret;
2210 }
2211
2212 struct attr_iter *
2213 navit_attr_iter_new(void)
2214 {
2215         return g_new0(struct attr_iter, 1);
2216 }
2217
2218 void
2219 navit_attr_iter_destroy(struct attr_iter *iter)
2220 {
2221         g_free(iter);
2222 }
2223
2224 void
2225 navit_add_callback(struct navit *this_, struct callback *cb)
2226 {
2227         callback_list_add(this_->attr_cbl, cb);
2228 }
2229
2230 void
2231 navit_remove_callback(struct navit *this_, struct callback *cb)
2232 {
2233         callback_list_remove(this_->attr_cbl, cb);
2234 }
2235
2236 /**
2237  * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2238  *
2239  * @param navit The navit instance
2240  * @returns nothing
2241  */
2242
2243 static void
2244 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2245 {
2246         struct point cursor_pnt;
2247         enum projection pro;
2248
2249         if (this_->blocked)
2250                 return;
2251         if (pnt)
2252                 cursor_pnt=*pnt;
2253         else {
2254                 pro=transform_get_projection(this_->trans_cursor);
2255                 if (!pro)
2256                         return;
2257                 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2258         }
2259         vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2260 #if 0   
2261         if (pnt)
2262                 pnt2=*pnt;
2263         else {
2264                 pro=transform_get_projection(this_->trans);
2265                 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2266         }
2267 #if 1
2268         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2269 #else
2270         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2271 #endif
2272 #endif
2273 }
2274
2275 static void
2276 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2277 {
2278         struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2279         struct pcoord cursor_pc;
2280         struct point cursor_pnt, *pnt=&cursor_pnt;
2281         struct tracking *tracking=NULL;
2282         struct pcoord pc[16];
2283         enum projection pro=transform_get_projection(this_->trans_cursor);
2284         int count;
2285         int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2286         void *attr_object;
2287         char *destination_file;
2288
2289         profile(0,NULL);
2290         if (this_->ready != 3) {
2291                 profile(0,"return 1\n");
2292                 return;
2293         }
2294         navit_layout_switch(this_);
2295         if (this_->vehicle == nv && this_->tracking_flag)
2296                 tracking=this_->tracking;
2297         if (tracking) {
2298                 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2299                 attr_object=tracking;
2300                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2301         } else {
2302                 attr_object=nv->vehicle;
2303                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2304         }
2305         if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2306                 if (!attr_valid.u.num != attr_position_valid_invalid)
2307                         return;
2308         if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2309             ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2310             ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2311                 profile(0,"return 2\n");
2312                 return;
2313         }
2314         nv->dir=*attr_dir.u.numd;
2315         nv->speed=*attr_speed.u.numd;
2316         transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2317         if (nv != this_->vehicle) {
2318                 navit_vehicle_draw(this_, nv, NULL);
2319                 profile(0,"return 3\n");
2320                 return;
2321         }
2322         cursor_pc.x = nv->coord.x;
2323         cursor_pc.y = nv->coord.y;
2324         cursor_pc.pro = pro;
2325         if (this_->route) {
2326                 if (tracking)
2327                         route_set_position_from_tracking(this_->route, tracking, pro);
2328                 else
2329                         route_set_position(this_->route, &cursor_pc);
2330         }
2331         callback_list_call_attr_0(this_->attr_cbl, attr_position);
2332         navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2333         if (this_->gui && nv->speed > 2)
2334                 navit_disable_suspend();
2335
2336         transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2337         if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow && 
2338                 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2339                 navit_set_center_cursor_draw(this_);
2340         else
2341                 navit_vehicle_draw(this_, nv, pnt);
2342
2343         if (nv->follow_curr > 1)
2344                 nv->follow_curr--;
2345         else
2346                 nv->follow_curr=nv->follow;
2347         callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2348
2349         /* Finally, if we reached our destination, stop navigation. */
2350         if (this_->route) {
2351                 switch(route_destination_reached(this_->route)) {
2352                 case 1:
2353                         route_remove_waypoint(this_->route);
2354                         count=route_get_destinations(this_->route, pc, 16);
2355                         destination_file = bookmarks_get_destination_file(TRUE);
2356                         bookmarks_append_coord(this_->bookmarks, destination_file, pc, count, "former_itinerary_part", NULL, NULL, this_->recentdest_count);
2357                         break;  
2358                 case 2:
2359                         navit_set_destination(this_, NULL, NULL, 0);
2360                         break;
2361                 }
2362         }
2363         profile(0,"return 5\n");
2364 }
2365
2366 /**
2367  * Set the position of the vehicle
2368  *
2369  * @param navit The navit instance
2370  * @param c The coordinate to set as position
2371  * @returns nothing
2372  */
2373
2374 void
2375 navit_set_position(struct navit *this_, struct pcoord *c)
2376 {
2377         if (this_->route) {
2378                 route_set_position(this_->route, c);
2379                 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2380         }
2381         if (this_->ready == 3)
2382                 navit_draw(this_);
2383 }
2384
2385 static int
2386 navit_set_vehicleprofile(struct navit *this_, char *name)
2387 {
2388         struct attr attr;
2389         GList *l;
2390         l=this_->vehicleprofiles;
2391         while (l) {
2392                 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2393                         if (!strcmp(attr.u.str, name)) {
2394                                 this_->vehicleprofile=l->data;
2395                                 if (this_->route)
2396                                         route_set_profile(this_->route, this_->vehicleprofile);
2397                                 return 1;
2398                         }
2399                 }
2400                 l=g_list_next(l);
2401         }
2402         return 0;
2403 }
2404
2405 static void
2406 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2407 {
2408         struct attr attr;
2409         this_->vehicle=nv;
2410         if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2411                 if (navit_set_vehicleprofile(this_, attr.u.str))
2412                         return;
2413         }
2414         if (!navit_set_vehicleprofile(this_,"car")) {
2415                 /* We do not have a fallback "car" profile
2416                 * so lets set any profile */
2417                 GList *l;
2418                 l=this_->vehicleprofiles;
2419                 if (l) {
2420                     this_->vehicleprofile=l->data;
2421                     if (this_->route)
2422                         route_set_profile(this_->route, this_->vehicleprofile);
2423                 }
2424         }
2425 }
2426
2427 /**
2428  * Register a new vehicle
2429  *
2430  * @param navit The navit instance
2431  * @param v The vehicle instance
2432  * @returns 1 for success
2433  */
2434 static int
2435 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2436 {
2437         struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2438         struct attr follow, active, animate;
2439         nv->vehicle=v;
2440         nv->follow=0;
2441         nv->last.x = 0;
2442         nv->last.y = 0;
2443         nv->animate_cursor=0;
2444         if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2445                 nv->follow=follow.u.num;
2446         nv->follow_curr=nv->follow;
2447         this_->vehicles=g_list_append(this_->vehicles, nv);
2448         if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2449                 navit_set_vehicle(this_, nv);
2450         if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2451                 nv->animate_cursor=animate.u.num;
2452         nv->callback.type=attr_callback;
2453         nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2454         vehicle_add_attr(nv->vehicle, &nv->callback);
2455         vehicle_set_attr(nv->vehicle, &this_->self);
2456         return 1;
2457 }
2458
2459
2460
2461
2462 struct gui *
2463 navit_get_gui(struct navit *this_)
2464 {
2465         return this_->gui;
2466 }
2467
2468 struct transformation *
2469 navit_get_trans(struct navit *this_)
2470 {
2471         return this_->trans;
2472 }
2473
2474 struct route *
2475 navit_get_route(struct navit *this_)
2476 {
2477         return this_->route;
2478 }
2479
2480 struct navigation *
2481 navit_get_navigation(struct navit *this_)
2482 {
2483         return this_->navigation;
2484 }
2485
2486 struct displaylist *
2487 navit_get_displaylist(struct navit *this_)
2488 {
2489         return this_->displaylist;
2490 }
2491
2492 void
2493 navit_layout_switch(struct navit *n) 
2494 {
2495
2496     int currTs=0;
2497     struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2498     double trise,tset,trise_actual;
2499     struct layout *l;
2500     int year, month, day;
2501     
2502     if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2503         return; //No layout - nothing to switch
2504     }
2505     if (!n->vehicle)
2506         return;
2507     l=layout_attr.u.layout;
2508     
2509     if (l->dayname || l->nightname) {
2510         //Ok, we know that we have profile to switch
2511         
2512         //Check that we aren't calculating too fast
2513         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2514                 currTs=iso8601_to_secs(iso8601_attr.u.str);
2515                 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2516         }
2517         if (currTs-(n->prevTs)<60) {
2518             //We've have to wait a little
2519             return;
2520         }
2521         if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2522                 return;
2523         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2524                 return; //No valid fix yet
2525         }
2526         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2527                 //No position - no sun
2528                 return;
2529         }
2530         
2531         //We calculate sunrise anyway, cause it is needed both for day and for night
2532         if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2533                 //near the pole sun never rises/sets, so we should never switch profiles
2534                 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
2535                 n->prevTs=currTs;
2536                 return;
2537             }
2538         
2539         trise_actual=trise;
2540         dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2541         if (l->dayname) {
2542         
2543             if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) || 
2544                     (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2545                 //The sun is rising now!
2546                 if (strcmp(l->name,l->dayname)) {
2547                     navit_set_layout_by_name(n,l->dayname);
2548                 }
2549             }
2550         }
2551         if (l->nightname) {
2552             if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2553                 //near the pole sun never rises/sets, so we should never switch profiles
2554                 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
2555                 n->prevTs=currTs;
2556                 return;
2557             }
2558             dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2559             if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2560                 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) || 
2561                         ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2562                 //Time to sleep
2563                 if (strcmp(l->name,l->nightname)) {
2564                     navit_set_layout_by_name(n,l->nightname);
2565                 }
2566             }   
2567         }
2568         
2569         n->prevTs=currTs;
2570     }
2571 }
2572
2573 int 
2574 navit_set_vehicle_by_name(struct navit *n,const char *name) 
2575 {
2576     struct vehicle *v;
2577     struct attr_iter *iter;
2578     struct attr vehicle_attr, name_attr;
2579
2580         iter=navit_attr_iter_new();
2581
2582     while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
2583                 v=vehicle_attr.u.vehicle;
2584                 vehicle_get_attr(v,attr_name,&name_attr,NULL);
2585                 if (name_attr.type==attr_name) {
2586                         if (!strcmp(name,name_attr.u.str)) {
2587                                 navit_set_attr(n,&vehicle_attr);                                
2588                                 navit_attr_iter_destroy(iter);
2589                                 return 1;
2590                         }
2591                 }
2592         }
2593     navit_attr_iter_destroy(iter);
2594     return 0;
2595 }
2596
2597 int 
2598 navit_set_layout_by_name(struct navit *n,const char *name) 
2599 {
2600     struct layout *l;
2601     struct attr_iter iter;
2602     struct attr layout_attr;
2603
2604     iter.u.list=0x00;
2605
2606     if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2607         return 0; //No layouts - nothing to do
2608     }
2609     if (iter.u.list==NULL) {
2610         return 0;
2611     }
2612     
2613     iter.u.list=g_list_first(iter.u.list);
2614     
2615     while(iter.u.list) {
2616         l=(struct layout*)iter.u.list->data;
2617         if (!strcmp(name,l->name)) {
2618             layout_attr.u.layout=l;
2619             layout_attr.type=attr_layout;
2620             navit_set_attr(n,&layout_attr);
2621             iter.u.list=g_list_first(iter.u.list);
2622             return 1;
2623         }
2624         iter.u.list=g_list_next(iter.u.list);
2625     }
2626
2627     iter.u.list=g_list_first(iter.u.list);
2628     return 0;
2629 }
2630
2631 void
2632 navit_disable_suspend() {
2633         gui_disable_suspend(global_navit->gui);
2634         callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
2635 }
2636
2637 int
2638 navit_block(struct navit *this_, int block)
2639 {
2640         if (block > 0) {
2641                 this_->blocked |= 1;
2642                 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2643                         this_->blocked |= 2;
2644                 return 0;
2645         }
2646         if ((this_->blocked & 2) || block < 0) {
2647                 this_->blocked=0;
2648                 navit_draw(this_);
2649                 return 1;
2650         }
2651         this_->blocked=0;
2652         return 0;
2653 }
2654
2655 void
2656 navit_destroy(struct navit *this_)
2657 {
2658         callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
2659
2660         /* TODO: destroy objects contained in this_ */
2661         if (this_->vehicle)
2662                 vehicle_destroy(this_->vehicle->vehicle);
2663         if (this_->bookmarks) {
2664                 char *center_file = bookmarks_get_center_file(TRUE);
2665                 bookmarks_write_center_to_file(this_->bookmarks, center_file);
2666                 g_free(center_file);
2667                 bookmarks_destroy(this_->bookmarks);
2668         }
2669         callback_destroy(this_->nav_speech_cb);
2670         callback_destroy(this_->roadbook_callback);
2671         callback_destroy(this_->popup_callback);
2672         callback_destroy(this_->motion_timeout_callback);
2673         if(this_->gra)
2674           graphics_remove_callback(this_->gra, this_->resize_callback);
2675         callback_destroy(this_->resize_callback);
2676         if(this_->gra)
2677           graphics_remove_callback(this_->gra, this_->button_callback);
2678         callback_destroy(this_->button_callback);
2679         if(this_->gra)
2680           graphics_remove_callback(this_->gra, this_->motion_callback);
2681         callback_destroy(this_->motion_callback);
2682         if(this_->gra)
2683           graphics_remove_callback(this_->gra, this_->predraw_callback);
2684         callback_destroy(this_->predraw_callback);
2685         route_destroy(this_->route);
2686         g_free(this_);
2687 }
2688
2689 /** @} */