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