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