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