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