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