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