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