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