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