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