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