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